Skip to content

Commit

Permalink
Merge branch 'rj/complete-reflog'
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
gitster committed Mar 14, 2024
2 parents edae49e + 1284f9c commit 4310074
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 12 deletions.
80 changes: 68 additions & 12 deletions contrib/completion/git-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,18 @@ fi

# This function is equivalent to
#
# __gitcomp "$(git xxx --git-completion-helper) ..."
# ___git_resolved_builtins=$(git xxx --git-completion-helper)
#
# except that the output is cached. Accept 1-3 arguments:
# except that the result of the execution is cached.
#
# Accept 1-3 arguments:
# 1: the git command to execute, this is also the cache key
# (use "_" when the command contains spaces, e.g. "remote add"
# becomes "remote_add")
# 2: extra options to be added on top (e.g. negative forms)
# 3: options to be excluded
__gitcomp_builtin ()
__git_resolve_builtins ()
{
# spaces must be replaced with underscore for multi-word
# commands, e.g. "git remote add" becomes remote_add.
local cmd="$1"
local incl="${2-}"
local excl="${3-}"
Expand All @@ -489,7 +491,24 @@ __gitcomp_builtin ()
eval "$var=\"$options\""
fi

__gitcomp "$options"
___git_resolved_builtins="$options"
}

# This function is equivalent to
#
# __gitcomp "$(git xxx --git-completion-helper) ..."
#
# except that the output is cached. Accept 1-3 arguments:
# 1: the git command to execute, this is also the cache key
# (use "_" when the command contains spaces, e.g. "remote add"
# becomes "remote_add")
# 2: extra options to be added on top (e.g. negative forms)
# 3: options to be excluded
__gitcomp_builtin ()
{
__git_resolve_builtins "$1" "$2" "$3"

__gitcomp "$___git_resolved_builtins"
}

# Variation of __gitcomp_nl () that appends to the existing list of
Expand Down Expand Up @@ -556,6 +575,26 @@ __gitcomp_file ()
true
}

# Find the current subcommand for commands that follow the syntax:
#
# git <command> <subcommand>
#
# 1: List of possible subcommands.
# 2: Optional subcommand to return when none is found.
__git_find_subcommand ()
{
local subcommand subcommands="$1" default_subcommand="$2"

for subcommand in $subcommands; do
if [ "$subcommand" = "${words[__git_cmd_idx+1]}" ]; then
echo $subcommand
return
fi
done

echo $default_subcommand
}

# Execute 'git ls-files', unless the --committable option is specified, in
# which case it runs 'git diff-index' to find out the files that can be
# committed. It return paths relative to the directory specified in the first
Expand Down Expand Up @@ -2471,13 +2510,30 @@ _git_rebase ()

_git_reflog ()
{
local subcommands="show delete expire"
local subcommand="$(__git_find_on_cmdline "$subcommands")"
local subcommands subcommand

if [ -z "$subcommand" ]; then
__gitcomp "$subcommands"
else
__git_complete_refs
__git_resolve_builtins "reflog"

subcommands="$___git_resolved_builtins"
subcommand="$(__git_find_subcommand "$subcommands" "show")"

case "$subcommand,$cur" in
show,--*)
__gitcomp "
$__git_log_common_options
"
return
;;
$subcommand,--*)
__gitcomp_builtin "reflog_$subcommand"
return
;;
esac

__git_complete_refs

if [ $((cword - __git_cmd_idx)) -eq 1 ]; then
__gitcompappend "$subcommands" "" "$cur" " "
fi
}

Expand Down
14 changes: 14 additions & 0 deletions t/t9902-completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2804,6 +2804,20 @@ test_expect_success 'git clone --config= - value' '
EOF
'

test_expect_success 'git reflog show' '
test_when_finished "git checkout - && git branch -d shown" &&
git checkout -b shown &&
test_completion "git reflog sho" <<-\EOF &&
show Z
shown Z
EOF
test_completion "git reflog show sho" "shown " &&
test_completion "git reflog shown sho" "shown " &&
test_completion "git reflog --unt" "--until=" &&
test_completion "git reflog show --unt" "--until=" &&
test_completion "git reflog shown --unt" "--until="
'

test_expect_success 'options with value' '
test_completion "git merge -X diff-algorithm=" <<-\EOF
Expand Down

0 comments on commit 4310074

Please sign in to comment.