Skip to content

Commit c7c0811

Browse files
committed
Merge branch 'ps/completion-with-reftable-fix'
Completion update to prepare for reftable * ps/completion-with-reftable-fix: completion: treat dangling symrefs as existing pseudorefs completion: silence pseudoref existence check completion: improve existence check for pseudo-refs t9902: verify that completion does not print anything completion: discover repo path in `__git_pseudoref_exists ()`
2 parents bb98703 + 020e0a0 commit c7c0811

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

contrib/completion/git-completion.bash

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,18 @@ __git_eread ()
137137
__git_pseudoref_exists ()
138138
{
139139
local ref=$1
140+
local head
141+
142+
__git_find_repo_path
140143

141144
# If the reftable is in use, we have to shell out to 'git rev-parse'
142145
# to determine whether the ref exists instead of looking directly in
143146
# the filesystem to determine whether the ref exists. Otherwise, use
144147
# Bash builtins since executing Git commands are expensive on some
145148
# platforms.
146149
if __git_eread "$__git_repo_path/HEAD" head; then
147-
b="${head#ref: }"
148-
if [ "$b" == "refs/heads/.invalid" ]; then
149-
__git -C "$__git_repo_path" rev-parse --verify --quiet "$ref" 2>/dev/null
150+
if [ "$head" == "ref: refs/heads/.invalid" ]; then
151+
__git show-ref --exists "$ref"
150152
return $?
151153
fi
152154
fi
@@ -1656,7 +1658,6 @@ __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
16561658

16571659
_git_cherry_pick ()
16581660
{
1659-
__git_find_repo_path
16601661
if __git_pseudoref_exists CHERRY_PICK_HEAD; then
16611662
__gitcomp "$__git_cherry_pick_inprogress_options"
16621663
return
@@ -2966,7 +2967,6 @@ _git_reset ()
29662967

29672968
_git_restore ()
29682969
{
2969-
__git_find_repo_path
29702970
case "$prev" in
29712971
-s)
29722972
__git_complete_refs
@@ -2995,7 +2995,6 @@ __git_revert_inprogress_options=$__git_sequencer_inprogress_options
29952995

29962996
_git_revert ()
29972997
{
2998-
__git_find_repo_path
29992998
if __git_pseudoref_exists REVERT_HEAD; then
30002999
__gitcomp "$__git_revert_inprogress_options"
30013000
return

t/t9902-completion.sh

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
test_description='test bash completion'
77

8+
# The Bash completion scripts must not print anything to either stdout or
9+
# stderr, which we try to verify. When tracing is enabled without support for
10+
# BASH_XTRACEFD this assertion will fail, so we have to mark the test as
11+
# untraceable with such ancient Bash versions.
12+
test_untraceable=UnfortunatelyYes
13+
814
. ./lib-bash.sh
915

1016
complete ()
@@ -87,9 +93,11 @@ test_completion ()
8793
else
8894
sed -e 's/Z$//' |sort >expected
8995
fi &&
90-
run_completion "$1" &&
96+
run_completion "$1" >"$TRASH_DIRECTORY"/bash-completion-output 2>&1 &&
9197
sort out >out_sorted &&
92-
test_cmp expected out_sorted
98+
test_cmp expected out_sorted &&
99+
test_must_be_empty "$TRASH_DIRECTORY"/bash-completion-output &&
100+
rm "$TRASH_DIRECTORY"/bash-completion-output
93101
}
94102

95103
# Test __gitcomp.
@@ -1925,6 +1933,14 @@ test_expect_success 'git checkout - --orphan with branch already provided comple
19251933
EOF
19261934
'
19271935

1936+
test_expect_success 'git restore completes modified files' '
1937+
test_commit A a.file &&
1938+
echo B >a.file &&
1939+
test_completion "git restore a." <<-\EOF
1940+
a.file
1941+
EOF
1942+
'
1943+
19281944
test_expect_success 'teardown after ref completion' '
19291945
git branch -d matching-branch &&
19301946
git tag -d matching-tag &&
@@ -2720,4 +2736,31 @@ test_expect_success '__git_complete' '
27202736
test_must_fail __git_complete ga missing
27212737
'
27222738

2739+
test_expect_success '__git_pseudoref_exists' '
2740+
test_when_finished "rm -rf repo" &&
2741+
git init repo &&
2742+
(
2743+
cd repo &&
2744+
sane_unset __git_repo_path &&
2745+
2746+
# HEAD should exist, even if it points to an unborn branch.
2747+
__git_pseudoref_exists HEAD >output 2>&1 &&
2748+
test_must_be_empty output &&
2749+
2750+
# HEAD points to an existing branch, so it should exist.
2751+
test_commit A &&
2752+
__git_pseudoref_exists HEAD >output 2>&1 &&
2753+
test_must_be_empty output &&
2754+
2755+
# CHERRY_PICK_HEAD does not exist, so the existence check should fail.
2756+
! __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
2757+
test_must_be_empty output &&
2758+
2759+
# CHERRY_PICK_HEAD points to a commit, so it should exist.
2760+
git update-ref CHERRY_PICK_HEAD A &&
2761+
__git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
2762+
test_must_be_empty output
2763+
)
2764+
'
2765+
27232766
test_done

0 commit comments

Comments
 (0)