completion: don't list 'HEAD' when trying refs completion outside of a repo

When refs completion is attempted while not in a git repository, the completion script offers 'HEAD' erroneously. Check early in __git_refs() that there is either a repository or a remote to work on, and return early if neither is given. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Feb 3, 2017 at 03:48 UTC 62a1b732160c363a84be310f801b5dec59196b9c
2 files changed +7 -3
contrib/completion/git-completion.bash
+6 -2
@@ -346,7 +346,11 @@ __git_refs ()
346 local list_refs_from=path remote="${1-}"
347 local format refs pfx
348
349 - if [ -n "$remote" ]; then
349 + if [ -z "$remote" ]; then
350 + if [ -z "$dir" ]; then
351 + return
352 + fi
353 + else
354 if __git_is_configured_remote "$remote"; then
355 # configured remote takes precedence over a
356 # local directory with the same name
@@ -360,7 +364,7 @@ __git_refs ()
364 fi
365 fi
366
363 - if [ "$list_refs_from" = path ] && [ -d "$dir" ]; then
367 + if [ "$list_refs_from" = path ]; then
368 case "$cur" in
369 refs|refs/*)
370 format="refname"
t/t9902-completion.sh
+1 -1
@@ -599,7 +599,7 @@ test_expect_success '__git_refs - non-existing URL remote - full refs' '
599 test_must_be_empty "$actual"
600 '
601
602 -test_expect_failure '__git_refs - not in a git repository' '
602 +test_expect_success '__git_refs - not in a git repository' '
603 (
604 GIT_CEILING_DIRECTORIES="$ROOT" &&
605 export GIT_CEILING_DIRECTORIES &&