Skip to content

Commit

Permalink
completion: respect $GIT_DIR
Browse files Browse the repository at this point in the history
The __gitdir() helper function finds out the path of the git
repository by running 'git rev-parse --git-dir'.  However, it has a
shortcut first to avoid the overhead of running a git command in a
subshell when the current directory is at the top of the work tree,
i.e. when it contains a '.git' subdirectory.

If the 'GIT_DIR' environment variable is set then it specifies the
path to the git repository, and the autodetection of the '.git'
directory is not necessary.  However, $GIT_DIR is only taken into
acocunt by 'git rev-parse --git-dir', and the check for the '.git'
subdirectory is performed first, so it wins over the path given in
$GIT_DIR.

There are several completion (helper) functions that depend on
__gitdir(), and when the above case triggers the completion script
will do weird things, like offering refs, aliases, or stashes from a
different repository, or displaying wrong or broken prompt, etc.

So check first whether $GIT_DIR is set, and only proceed with checking
the '.git' directory in the current directory if it isn't.  'git
rev-parse' would also check whether the path in $GIT_DIR is a proper
'.git' directory, i.e. 'HEAD', 'refs/', and 'objects/' are present and
accessible, but we don't have to be that thorough for the bash prompt.
And we've lived with an equally permissive check for '.git' in the
current working directory for years anyway.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
SZEDER Gábor authored and Junio C Hamano committed Jun 19, 2012
1 parent ac3eb1c commit b7be436
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions contrib/completion/git-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion t/t9903-bash-prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" &&
Expand All @@ -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 &&
Expand Down

0 comments on commit b7be436

Please sign in to comment.