git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/3] git-completion.bash: use __gitcomp_builtin() in _git_stash()

From
Denton Liu <liu.denton@gmail.com>
Date
Mar 16, 2021, 00:54 UTC
Message-ID
<d6deaecc1feb771b245f50bc8b9cb197bf693405.1615855962.git.liu.denton@gmail.com>
In-Reply-To
<cover.1615855962.git.liu.denton@gmail.com>

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 the word after "stash". Unfortunately, this does not work 100% correctly. For example, in the case of something like `git -C stash stash<TAB>`, this will incorrectly identify the first "stash" as the command. This seems to be an edge-case that we can ignore, though, as other functions, such as _git_worktree(), suffer from the same problem.

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 <liu.denton@gmail.com>
---
 contrib/completion/git-completion.bash | 41 ++++++++++++--------------
 1 file changed, 19 insertions(+), 22 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index da46f46e3c..83b6415b13 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3013,29 +3013,21 @@ _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")"
 	local stash_idx="$(__git_find_on_cmdline --show-idx stash)"
 	stash_idx="${stash_idx% *}"
 
-	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 - stash_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
@@ -3043,24 +3035,29 @@ _git_stash ()
 
 	case "$subcommand,$cur" in
 	push,--*)
-		__gitcomp "$save_opts --message"
+		__gitcomp_builtin stash_push
 		;;
 	save,--*)
-		__gitcomp "$save_opts"
+		__gitcomp_builtin stash_save
 		;;
-	apply,--*|pop,--*)
-		__gitcomp "--index --quiet"
+	pop,--*)
+		__gitcomp_builtin stash_pop
+		;;
+	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 - stash_idx)) -eq 2 ]; then
-- 
2.31.0.rc2.261.g7f71774620
Previous: Denton LiuNext: Denton Liu
Message 4 of 22 in “git-completion.bash: improvements to _git_stash()”
  1. 0/3 git-completion.bash: improvements to _git_stash()Denton Liu, Mar 16, 2021
  2. 1/3 git-completion.bash: extract from else in _git_stash()Denton Liu, Mar 16, 2021
  3. 2/3 git-completion.bash: fix `git <args>... stash branch` bugDenton Liu, Mar 16, 2021
  4. 3/3 git-completion.bash: use __gitcomp_builtin() in _git_stash()Denton Liu, Mar 16, 2021
  5. 0/3 git-completion.bash: improvements to _git_stash()Denton Liu, Mar 18, 2021
  6. 2/3 git-completion.bash: fix `git <args>... stash branch` bugDenton Liu, Mar 18, 2021
  7. Junio C HamanoMar 18, 2021
  8. Denton LiuMar 19, 2021
  9. Junio C HamanoMar 19, 2021
  10. 1/3 git-completion.bash: extract from else in _git_stash()Denton Liu, Mar 18, 2021
  11. 3/3 git-completion.bash: use __gitcomp_builtin() in _git_stash()Denton Liu, Mar 18, 2021
  12. Junio C HamanoMar 18, 2021
  13. Denton LiuMar 19, 2021
  14. Junio C HamanoMar 19, 2021
  15. 0/3 git-completion.bash: improvements to _git_stash()Denton Liu, Mar 24, 2021
  16. 1/3 git-completion.bash: pass $__git_subcommand_idx from __git_main()Denton Liu, Mar 24, 2021
  17. SZEDER GáborMar 27, 2021
  18. SZEDER GáborMar 28, 2021
  19. 3/3 git-completion.bash: use __gitcomp_builtin() in _git_stash()Denton Liu, Mar 24, 2021
  20. SZEDER GáborMar 28, 2021
  21. 2/3 git-completion.bash: extract from else in _git_stash()Denton Liu, Mar 24, 2021
  22. SZEDER GáborMar 28, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.