Skip to content

Commit beb6ee7

Browse files
szedergitster
authored andcommitted
completion: extract repository discovery from __gitdir()
To prepare for caching the path to the repository in the following commit, extract the repository discovering part of __gitdir() into the __git_find_repo_path() helper function, which stores the found path in the $__git_repo_path variable instead of printing it. Make __gitdir() a wrapper around this new function. Declare $__git_repo_path local in the toplevel completion functions __git_main() and __gitk_main() to ensure that it never leaks into the environment and influences subsequent completions (though this isn't necessary right now, as __gitdir() is still only executed in subshells, but will matter for the following commit). Adjust tests checking __gitdir() or any other completion function calling __gitdir() to perform those checks in a subshell to prevent $__git_repo_path from leaking into the test environment. Otherwise leave the tests unchanged to demonstrate that this change doesn't alter __gitdir()'s behavior. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a958d40 commit beb6ee7

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

contrib/completion/git-completion.bash

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,35 @@ case "$COMP_WORDBREAKS" in
3434
*) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
3535
esac
3636

37+
# Discovers the path to the git repository taking any '--git-dir=<path>' and
38+
# '-C <path>' options into account and stores it in the $__git_repo_path
39+
# variable.
40+
__git_find_repo_path ()
41+
{
42+
if [ -n "${__git_C_args-}" ]; then
43+
__git_repo_path="$(git "${__git_C_args[@]}" \
44+
${__git_dir:+--git-dir="$__git_dir"} \
45+
rev-parse --absolute-git-dir 2>/dev/null)"
46+
elif [ -n "${__git_dir-}" ]; then
47+
test -d "$__git_dir" &&
48+
__git_repo_path="$__git_dir"
49+
elif [ -n "${GIT_DIR-}" ]; then
50+
test -d "${GIT_DIR-}" &&
51+
__git_repo_path="$GIT_DIR"
52+
elif [ -d .git ]; then
53+
__git_repo_path=.git
54+
else
55+
__git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
56+
fi
57+
}
58+
3759
# __gitdir accepts 0 or 1 arguments (i.e., location)
3860
# returns location of .git repo
3961
__gitdir ()
4062
{
4163
if [ -z "${1-}" ]; then
42-
if [ -n "${__git_C_args-}" ]; then
43-
git "${__git_C_args[@]}" \
44-
${__git_dir:+--git-dir="$__git_dir"} \
45-
rev-parse --absolute-git-dir 2>/dev/null
46-
elif [ -n "${__git_dir-}" ]; then
47-
test -d "$__git_dir" || return 1
48-
echo "$__git_dir"
49-
elif [ -n "${GIT_DIR-}" ]; then
50-
test -d "${GIT_DIR-}" || return 1
51-
echo "$GIT_DIR"
52-
elif [ -d .git ]; then
53-
echo .git
54-
else
55-
git rev-parse --git-dir 2>/dev/null
56-
fi
64+
__git_find_repo_path || return 1
65+
echo "$__git_repo_path"
5766
elif [ -d "$1/.git" ]; then
5867
echo "$1/.git"
5968
else
@@ -2783,7 +2792,7 @@ _git_worktree ()
27832792

27842793
__git_main ()
27852794
{
2786-
local i c=1 command __git_dir
2795+
local i c=1 command __git_dir __git_repo_path
27872796
local __git_C_args C_args_count=0
27882797

27892798
while [ $c -lt $cword ]; do
@@ -2855,6 +2864,7 @@ __gitk_main ()
28552864
{
28562865
__git_has_doubledash && return
28572866

2867+
local __git_repo_path
28582868
local g="$(__gitdir)"
28592869
local merge=""
28602870
if [ -f "$g/MERGE_HEAD" ]; then

t/t9902-completion.sh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,25 @@ test_expect_success '__gitdir - from command line (through $__git_dir)' '
147147

148148
test_expect_success '__gitdir - repo as argument' '
149149
echo "otherrepo/.git" >expected &&
150-
__gitdir "otherrepo" >"$actual" &&
150+
(
151+
__gitdir "otherrepo" >"$actual"
152+
) &&
151153
test_cmp expected "$actual"
152154
'
153155

154156
test_expect_success '__gitdir - remote as argument' '
155157
echo "remote" >expected &&
156-
__gitdir "remote" >"$actual" &&
158+
(
159+
__gitdir "remote" >"$actual"
160+
) &&
157161
test_cmp expected "$actual"
158162
'
159163

160164
test_expect_success '__gitdir - .git directory in cwd' '
161165
echo ".git" >expected &&
162-
__gitdir >"$actual" &&
166+
(
167+
__gitdir >"$actual"
168+
) &&
163169
test_cmp expected "$actual"
164170
'
165171

@@ -450,7 +456,9 @@ test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from
450456
git remote add remote_in_config_1 git://remote_1 &&
451457
test_when_finished "git remote remove remote_in_config_2" &&
452458
git remote add remote_in_config_2 git://remote_2 &&
453-
__git_remotes >actual &&
459+
(
460+
__git_remotes >actual
461+
) &&
454462
test_cmp expect actual
455463
'
456464

@@ -459,8 +467,10 @@ test_expect_success '__git_is_configured_remote' '
459467
git remote add remote_1 git://remote_1 &&
460468
test_when_finished "git remote remove remote_2" &&
461469
git remote add remote_2 git://remote_2 &&
462-
verbose __git_is_configured_remote remote_2 &&
463-
test_must_fail __git_is_configured_remote non-existent
470+
(
471+
verbose __git_is_configured_remote remote_2 &&
472+
test_must_fail __git_is_configured_remote non-existent
473+
)
464474
'
465475

466476
test_expect_success 'setup for ref completion' '

0 commit comments

Comments
 (0)