Skip to content

Commit 4310074

Browse files
committed
Merge branch 'rj/complete-reflog'
The command line completion script (in contrib/) learned to complete "git reflog" better. * rj/complete-reflog: completion: reflog subcommands and options completion: factor out __git_resolve_builtins completion: introduce __git_find_subcommand completion: reflog show <log-options> completion: reflog with implicit "show"
2 parents edae49e + 1284f9c commit 4310074

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

contrib/completion/git-completion.bash

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,18 @@ fi
454454

455455
# This function is equivalent to
456456
#
457-
# __gitcomp "$(git xxx --git-completion-helper) ..."
457+
# ___git_resolved_builtins=$(git xxx --git-completion-helper)
458458
#
459-
# except that the output is cached. Accept 1-3 arguments:
459+
# except that the result of the execution is cached.
460+
#
461+
# Accept 1-3 arguments:
460462
# 1: the git command to execute, this is also the cache key
463+
# (use "_" when the command contains spaces, e.g. "remote add"
464+
# becomes "remote_add")
461465
# 2: extra options to be added on top (e.g. negative forms)
462466
# 3: options to be excluded
463-
__gitcomp_builtin ()
467+
__git_resolve_builtins ()
464468
{
465-
# spaces must be replaced with underscore for multi-word
466-
# commands, e.g. "git remote add" becomes remote_add.
467469
local cmd="$1"
468470
local incl="${2-}"
469471
local excl="${3-}"
@@ -489,7 +491,24 @@ __gitcomp_builtin ()
489491
eval "$var=\"$options\""
490492
fi
491493

492-
__gitcomp "$options"
494+
___git_resolved_builtins="$options"
495+
}
496+
497+
# This function is equivalent to
498+
#
499+
# __gitcomp "$(git xxx --git-completion-helper) ..."
500+
#
501+
# except that the output is cached. Accept 1-3 arguments:
502+
# 1: the git command to execute, this is also the cache key
503+
# (use "_" when the command contains spaces, e.g. "remote add"
504+
# becomes "remote_add")
505+
# 2: extra options to be added on top (e.g. negative forms)
506+
# 3: options to be excluded
507+
__gitcomp_builtin ()
508+
{
509+
__git_resolve_builtins "$1" "$2" "$3"
510+
511+
__gitcomp "$___git_resolved_builtins"
493512
}
494513

495514
# Variation of __gitcomp_nl () that appends to the existing list of
@@ -556,6 +575,26 @@ __gitcomp_file ()
556575
true
557576
}
558577

578+
# Find the current subcommand for commands that follow the syntax:
579+
#
580+
# git <command> <subcommand>
581+
#
582+
# 1: List of possible subcommands.
583+
# 2: Optional subcommand to return when none is found.
584+
__git_find_subcommand ()
585+
{
586+
local subcommand subcommands="$1" default_subcommand="$2"
587+
588+
for subcommand in $subcommands; do
589+
if [ "$subcommand" = "${words[__git_cmd_idx+1]}" ]; then
590+
echo $subcommand
591+
return
592+
fi
593+
done
594+
595+
echo $default_subcommand
596+
}
597+
559598
# Execute 'git ls-files', unless the --committable option is specified, in
560599
# which case it runs 'git diff-index' to find out the files that can be
561600
# committed. It return paths relative to the directory specified in the first
@@ -2471,13 +2510,30 @@ _git_rebase ()
24712510

24722511
_git_reflog ()
24732512
{
2474-
local subcommands="show delete expire"
2475-
local subcommand="$(__git_find_on_cmdline "$subcommands")"
2513+
local subcommands subcommand
24762514

2477-
if [ -z "$subcommand" ]; then
2478-
__gitcomp "$subcommands"
2479-
else
2480-
__git_complete_refs
2515+
__git_resolve_builtins "reflog"
2516+
2517+
subcommands="$___git_resolved_builtins"
2518+
subcommand="$(__git_find_subcommand "$subcommands" "show")"
2519+
2520+
case "$subcommand,$cur" in
2521+
show,--*)
2522+
__gitcomp "
2523+
$__git_log_common_options
2524+
"
2525+
return
2526+
;;
2527+
$subcommand,--*)
2528+
__gitcomp_builtin "reflog_$subcommand"
2529+
return
2530+
;;
2531+
esac
2532+
2533+
__git_complete_refs
2534+
2535+
if [ $((cword - __git_cmd_idx)) -eq 1 ]; then
2536+
__gitcompappend "$subcommands" "" "$cur" " "
24812537
fi
24822538
}
24832539

t/t9902-completion.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,6 +2804,20 @@ test_expect_success 'git clone --config= - value' '
28042804
EOF
28052805
'
28062806

2807+
test_expect_success 'git reflog show' '
2808+
test_when_finished "git checkout - && git branch -d shown" &&
2809+
git checkout -b shown &&
2810+
test_completion "git reflog sho" <<-\EOF &&
2811+
show Z
2812+
shown Z
2813+
EOF
2814+
test_completion "git reflog show sho" "shown " &&
2815+
test_completion "git reflog shown sho" "shown " &&
2816+
test_completion "git reflog --unt" "--until=" &&
2817+
test_completion "git reflog show --unt" "--until=" &&
2818+
test_completion "git reflog shown --unt" "--until="
2819+
'
2820+
28072821
test_expect_success 'options with value' '
28082822
test_completion "git merge -X diff-algorithm=" <<-\EOF
28092823

0 commit comments

Comments
 (0)