From e94fb4404280c07a668b4669c072983cdd079592 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Wed, 24 Mar 2021 01:36:27 -0700 Subject: git-completion.bash: pass $__git_subcommand_idx from __git_main() Many completion functions perform hardcoded comparisons with $cword. This fails in the case where the main git command is given arguments (e.g. `git -C . bundle` would fail to complete its subcommands). Even _git_worktree(), which uses __git_find_on_cmdline(), could still fail. With something like `git -C add worktree move`, the subcommand would be incorrectly identified as "add" instead of "move". Assign $__git_subcommand_idx in __git_main(), where the git subcommand is actually found and the corresponding completion function is called. Use this variable to replace hardcoded comparisons with $cword. Signed-off-by: Denton Liu Signed-off-by: Junio C Hamano diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 7dc6cd8..a2f1b5e 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -1474,12 +1474,12 @@ _git_branch () _git_bundle () { - local cmd="${words[2]}" + local cmd="${words[__git_subcommand_idx+1]}" case "$cword" in - 2) + $((__git_subcommand_idx+1))) __gitcomp "create list-heads verify unbundle" ;; - 3) + $((__git_subcommand_idx+2))) # looking for a file ;; *) @@ -1894,7 +1894,7 @@ _git_grep () esac case "$cword,$prev" in - 2,*|*,-*) + $((__git_subcommand_idx+1)),*|*,-*) __git_complete_symbol && return ;; esac @@ -3058,7 +3058,7 @@ _git_stash () branch,--*) ;; branch,*) - if [ $cword -eq 3 ]; then + if [ $cword -eq $((__git_subcommand_idx+2)) ]; then __git_complete_refs else __gitcomp_nl "$(__git stash list \ @@ -3277,11 +3277,9 @@ __git_complete_worktree_paths () _git_worktree () { local subcommands="add list lock move prune remove unlock" - local subcommand subcommand_idx + local subcommand - subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")" - subcommand_idx="${subcommand% *}" - subcommand="${subcommand#* }" + subcommand="$(__git_find_on_cmdline "$subcommands")" case "$subcommand,$cur" in ,*) @@ -3306,7 +3304,7 @@ _git_worktree () # be either the 'add' subcommand, the unstuck # argument of an option (e.g. branch for -b|-B), or # the path for the new worktree. - if [ $cword -eq $((subcommand_idx+1)) ]; then + if [ $cword -eq $((__git_subcommand_idx+2)) ]; then # Right after the 'add' subcommand: have to # complete the path, so fall back to Bash # filename completion. @@ -3330,7 +3328,7 @@ _git_worktree () __git_complete_worktree_paths ;; move,*) - if [ $cword -eq $((subcommand_idx+1)) ]; then + if [ $cword -eq $((__git_subcommand_idx+2)) ]; then # The first parameter must be an existing working # tree to be moved. __git_complete_worktree_paths @@ -3398,6 +3396,7 @@ __git_main () { local i c=1 command __git_dir __git_repo_path local __git_C_args C_args_count=0 + local __git_subcommand_idx while [ $c -lt $cword ]; do i="${words[c]}" @@ -3412,7 +3411,7 @@ __git_main () __git_C_args[C_args_count++]="${words[c]}" ;; -*) ;; - *) command="$i"; break ;; + *) command="$i"; __git_subcommand_idx="$c"; break ;; esac ((c++)) done -- cgit v0.10.2-6-g49f6 From 42b30bcbb7929527631225a89a5630057535d766 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Wed, 24 Mar 2021 01:36:28 -0700 Subject: git-completion.bash: extract from else in _git_stash() To save a level of indentation, perform an early return in the "if" arm so we can move the "else" code out of the block. Signed-off-by: Denton Liu Signed-off-by: Junio C Hamano diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index a2f1b5e..8d4d8cc 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -3035,44 +3035,45 @@ _git_stash () fi ;; esac - else - case "$subcommand,$cur" in - push,--*) - __gitcomp "$save_opts --message" - ;; - save,--*) - __gitcomp "$save_opts" - ;; - apply,--*|pop,--*) - __gitcomp "--index --quiet" - ;; - drop,--*) - __gitcomp "--quiet" - ;; - list,--*) - __gitcomp "--name-status --oneline --patch-with-stat" - ;; - show,--*) - __gitcomp "$__git_diff_common_options" - ;; - branch,--*) - ;; - branch,*) - if [ $cword -eq $((__git_subcommand_idx+2)) ]; then - __git_complete_refs - else - __gitcomp_nl "$(__git stash list \ - | sed -n -e 's/:.*//p')" - fi - ;; - show,*|apply,*|drop,*|pop,*) + return + fi + + case "$subcommand,$cur" in + push,--*) + __gitcomp "$save_opts --message" + ;; + save,--*) + __gitcomp "$save_opts" + ;; + apply,--*|pop,--*) + __gitcomp "--index --quiet" + ;; + drop,--*) + __gitcomp "--quiet" + ;; + list,--*) + __gitcomp "--name-status --oneline --patch-with-stat" + ;; + show,--*) + __gitcomp "$__git_diff_common_options" + ;; + branch,--*) + ;; + branch,*) + if [ $cword -eq $((__git_subcommand_idx+2)) ]; then + __git_complete_refs + else __gitcomp_nl "$(__git stash list \ | sed -n -e 's/:.*//p')" - ;; - *) - ;; - esac - fi + fi + ;; + show,*|apply,*|drop,*|pop,*) + __gitcomp_nl "$(__git stash list \ + | sed -n -e 's/:.*//p')" + ;; + *) + ;; + esac } _git_submodule () -- cgit v0.10.2-6-g49f6 From 61318078640dc1bbe07c5d762f7a581b7408d623 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Wed, 24 Mar 2021 01:36:29 -0700 Subject: git-completion.bash: use __gitcomp_builtin() in _git_stash() The completion for 'git stash' has not changed in a major way since it was converted from shell script to builtin. Now that it's a builtin, we can take advantage of the groundwork laid out by parse-options and use the generated options. Rewrite _git_stash() to take use __gitcomp_builtin() to generate completions for subcommands. The main `git stash` command does not take any arguments directly. If no subcommand is given, it automatically defaults to `git stash push`. This means that we can simplify the logic for when no subcommands have been given yet. We only have to offer subcommand completions when we're completing a non-option after "stash". One area that this patch could improve upon is that the `git stash list` command accepts log-options. It would be nice if the completion for this were unified with that of _git_log() and _git_show() which would allow completions to be provided for options such as `--pretty` but that is outside the scope of this patch. Signed-off-by: Denton Liu Signed-off-by: Junio C Hamano diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 8d4d8cc..c926ca2 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -3013,26 +3013,19 @@ _git_sparse_checkout () _git_stash () { - local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked' local subcommands='push list show apply clear drop pop create branch' local subcommand="$(__git_find_on_cmdline "$subcommands save")" - if [ -z "$subcommand" -a -n "$(__git_find_on_cmdline "-p")" ]; then - subcommand="push" - fi + if [ -z "$subcommand" ]; then - case "$cur" in - --*) - __gitcomp "$save_opts" + case "$((cword - __git_subcommand_idx)),$cur" in + *,--*) + __gitcomp_builtin stash_push ;; - sa*) - if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then - __gitcomp "save" - fi + 1,sa*) + __gitcomp "save" ;; - *) - if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then - __gitcomp "$subcommands" - fi + 1,*) + __gitcomp "$subcommands" ;; esac return @@ -3040,24 +3033,29 @@ _git_stash () case "$subcommand,$cur" in push,--*) - __gitcomp "$save_opts --message" + __gitcomp_builtin stash_push ;; save,--*) - __gitcomp "$save_opts" + __gitcomp_builtin stash_save + ;; + pop,--*) + __gitcomp_builtin stash_pop ;; - apply,--*|pop,--*) - __gitcomp "--index --quiet" + apply,--*) + __gitcomp_builtin stash_apply ;; drop,--*) - __gitcomp "--quiet" + __gitcomp_builtin stash_drop ;; list,--*) - __gitcomp "--name-status --oneline --patch-with-stat" + # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show() + __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options" ;; show,--*) - __gitcomp "$__git_diff_common_options" + __gitcomp_builtin stash_show "$__git_diff_common_options" ;; branch,--*) + __gitcomp_builtin stash_branch ;; branch,*) if [ $cword -eq $((__git_subcommand_idx+2)) ]; then -- cgit v0.10.2-6-g49f6