diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index efcd875f91..5d70cc4286 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -41,6 +41,9 @@ __gitdir () if [ -z "${1-}" ]; then if [ -n "${__git_dir-}" ]; then echo "$__git_dir" + elif [ -n "${GIT_DIR-}" ]; then + test -d "${GIT_DIR-}" || return 1 + echo "$GIT_DIR" elif [ -d .git ]; then echo .git else diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh index f8fb93f687..f17c1f8b85 100755 --- a/t/t9903-bash-prompt.sh +++ b/t/t9903-bash-prompt.sh @@ -85,7 +85,7 @@ test_expect_success 'gitdir - parent is a .git directory' ' test_cmp expected "$actual" ' -test_expect_failure 'gitdir - $GIT_DIR set while .git directory in cwd' ' +test_expect_success 'gitdir - $GIT_DIR set while .git directory in cwd' ' echo "$TRASH_DIRECTORY/otherrepo/.git" > expected && ( GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" && @@ -106,6 +106,14 @@ test_expect_success 'gitdir - $GIT_DIR set while .git directory in parent' ' test_cmp expected "$actual" ' +test_expect_success 'gitdir - non-existing $GIT_DIR' ' + ( + GIT_DIR="$TRASH_DIRECTORY/non-existing" && + export GIT_DIR && + test_must_fail __gitdir + ) +' + test_expect_success 'gitdir - gitfile in cwd' ' echo "$TRASH_DIRECTORY/otherrepo/.git" > expected && echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&