support git worktrees

Summary:
a [git worktree](https://git-scm.com/docs/git-worktree) allows for a single repository to have multiple local checkouts in different directories (allowing parallel work on multiple branches of the same repo).

in such a worktree, `.git` is a file and not a directory:
```
$ cat .git
gitdir: /path/to/actual/repo/.git/worktrees/name
```

the `_git_prompt` helper in `scm-prompt.sh` expects only directories:
```
$ echo "$(_git_prompt /path/to/worktree/.git)"
# (output is empty)
```

this amends the helper to support them:
```
$ echo "$(_git_prompt /path/to/worktree/.git)"
f140443f|REBASE-i|T123456

$ echo "$(_git_prompt /path/to/parent/.git)"
T54321
```

Reviewed By: quark-zju

Differential Revision: D27509147

fbshipit-source-id: 9a4ab55af99538c6425ad5721c3266386dbda453
This commit is contained in:
Mike Watters 2021-04-08 12:45:46 -07:00 committed by Facebook GitHub Bot
parent 7e0c7c3be0
commit 09fb4128a5

View File

@ -182,6 +182,9 @@ _git_dirty() {
_git_prompt() {
local git br
git="$1"
if [[ -f "$git" ]]; then
git=$(awk '/^gitdir:/ {print $2}' < "$git")
fi
if [[ -f "$git/HEAD" ]]; then
read br < "$git/HEAD"
case $br in
@ -234,7 +237,7 @@ _scm_prompt() {
dir="$PWD"
while : ; do
[[ -n "$HOME_IS_NOT_A_REPO" ]] && [[ "$dir" = "/home" ]] && break
if [[ -d "$dir/.git" ]]; then
if [[ -r "$dir/.git" ]]; then
br="$(_git_prompt "$dir/.git")"
break
elif [[ -d "$dir/.hg" ]]; then