Skip to content

Commit 911d5da

Browse files
szedergitster
authored andcommitted
completion: fix completion after 'git --option <TAB>'
The bash completion doesn't work when certain options to git itself are specified, e.g. 'git --no-pager <TAB>' errors out with error: invalid key: alias.--no-pager The main _git() completion function finds out the git command name by looping through all the words on the command line and searching for the first word that is not a known option for the git command. Unfortunately the list of known git options was not updated in a long time, and newer options are not skipped but mistaken for a git command. Such a misrecognized "command" is then passed to __git_aliased_command(), which in turn passes it to a 'git config' query, hence the error. Currently the following options are misrecognized for a git command: -c --no-pager --exec-path --html-path --man-path --info-path --no-replace-objects --work-tree= --namespace= To fix this we could just update the list of options to be skipped, but the same issue will likely arise, if the git command learns a new option in the future. Therefore, to make it more future proof against new options, this patch changes that loop to skip all option-looking words, i.e. words starting with a dash. We also have to handle the '-c' option specially, because it takes a configutation parameter in a separate word, which must be skipped, too. [fc: added tests] Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Felipe Contreras <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3ffcd08 commit 911d5da

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

contrib/completion/git-completion.bash

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2623,8 +2623,9 @@ _git ()
26232623
case "$i" in
26242624
--git-dir=*) __git_dir="${i#--git-dir=}" ;;
26252625
--bare) __git_dir="." ;;
2626-
--version|-p|--paginate) ;;
26272626
--help) command="help"; break ;;
2627+
-c) c=$((++c)) ;;
2628+
-*) ;;
26282629
*) command="$i"; break ;;
26292630
esac
26302631
((c++))

t/t9902-completion.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,21 @@ test_expect_success 'general options' '
223223
test_completion "git --inf" "--info-path " &&
224224
test_completion "git --no-r" "--no-replace-objects "
225225
'
226+
227+
test_expect_success 'general options plus command' '
228+
test_completion "git --version check" "checkout " &&
229+
test_completion "git --paginate check" "checkout " &&
230+
test_completion "git --git-dir=foo check" "checkout " &&
231+
test_completion "git --bare check" "checkout " &&
232+
test_completion "git --help des" "describe " &&
233+
test_completion "git --exec-path=foo check" "checkout " &&
234+
test_completion "git --html-path check" "checkout " &&
235+
test_completion "git --no-pager check" "checkout " &&
236+
test_completion "git --work-tree=foo check" "checkout " &&
237+
test_completion "git --namespace=foo check" "checkout " &&
238+
test_completion "git --paginate check" "checkout " &&
239+
test_completion "git --info-path check" "checkout " &&
240+
test_completion "git --no-replace-objects check" "checkout "
241+
'
242+
226243
test_done

0 commit comments

Comments
 (0)