Skip to content

Commit d773c63

Browse files
szedergitster
authored andcommitted
bash: offer only paths after '--'
Many git commands use '--' to separate subcommands, options, and refs from paths. However, the programmable completion for several of these commands does not respect the '--', and offer subcommands, options, or refs after a '--', although only paths are permitted. e.g. 'git bisect -- <TAB>' offers subcommands, 'git log -- --<TAB>' offers options and 'git log -- git<TAB>' offers all gitgui tags. The completion for the following commands share this wrong behaviour: am add bisect commit diff log reset shortlog submodule gitk. To avoid this, we check the presence of a '--' on the command line first and let the shell do filename completion, if one is found. Signed-off-by: SZEDER Gábor <[email protected]> Acked-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1db4a75 commit d773c63

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,18 @@ __git_find_subcommand ()
451451
done
452452
}
453453

454+
__git_has_doubledash ()
455+
{
456+
local c=1
457+
while [ $c -lt $COMP_CWORD ]; do
458+
if [ "--" = "${COMP_WORDS[c]}" ]; then
459+
return 0
460+
fi
461+
c=$((++c))
462+
done
463+
return 1
464+
}
465+
454466
__git_whitespacelist="nowarn warn error error-all strip"
455467

456468
_git_am ()
@@ -497,6 +509,8 @@ _git_apply ()
497509

498510
_git_add ()
499511
{
512+
__git_has_doubledash && return
513+
500514
local cur="${COMP_WORDS[COMP_CWORD]}"
501515
case "$cur" in
502516
--*)
@@ -511,6 +525,8 @@ _git_add ()
511525

512526
_git_bisect ()
513527
{
528+
__git_has_doubledash && return
529+
514530
local subcommands="start bad good reset visualize replay log"
515531
local subcommand="$(__git_find_subcommand "$subcommands")"
516532
if [ -z "$subcommand" ]; then
@@ -613,6 +629,8 @@ _git_cherry_pick ()
613629

614630
_git_commit ()
615631
{
632+
__git_has_doubledash && return
633+
616634
local cur="${COMP_WORDS[COMP_CWORD]}"
617635
case "$cur" in
618636
--*)
@@ -632,6 +650,8 @@ _git_describe ()
632650

633651
_git_diff ()
634652
{
653+
__git_has_doubledash && return
654+
635655
local cur="${COMP_WORDS[COMP_CWORD]}"
636656
case "$cur" in
637657
--*)
@@ -734,6 +754,8 @@ _git_ls_tree ()
734754

735755
_git_log ()
736756
{
757+
__git_has_doubledash && return
758+
737759
local cur="${COMP_WORDS[COMP_CWORD]}"
738760
case "$cur" in
739761
--pretty=*)
@@ -1085,6 +1107,8 @@ _git_remote ()
10851107

10861108
_git_reset ()
10871109
{
1110+
__git_has_doubledash && return
1111+
10881112
local cur="${COMP_WORDS[COMP_CWORD]}"
10891113
case "$cur" in
10901114
--*)
@@ -1097,6 +1121,8 @@ _git_reset ()
10971121

10981122
_git_shortlog ()
10991123
{
1124+
__git_has_doubledash && return
1125+
11001126
local cur="${COMP_WORDS[COMP_CWORD]}"
11011127
case "$cur" in
11021128
--*)
@@ -1143,6 +1169,8 @@ _git_stash ()
11431169

11441170
_git_submodule ()
11451171
{
1172+
__git_has_doubledash && return
1173+
11461174
local subcommands="add status init update"
11471175
if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
11481176
local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -1349,6 +1377,8 @@ _git ()
13491377

13501378
_gitk ()
13511379
{
1380+
__git_has_doubledash && return
1381+
13521382
local cur="${COMP_WORDS[COMP_CWORD]}"
13531383
local g="$(git rev-parse --git-dir 2>/dev/null)"
13541384
local merge=""

0 commit comments

Comments
 (0)