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

[PoC PATCH] completion: support 'git worktree'

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Aug 21, 2015, 20:50 UTC
Message-ID
<1440190256-21794-1-git-send-email-szeder@ira.uka.de>
In-Reply-To
<20150821224918.Horde.edB9u314lsP17FLUzwFsQA1@webmail.informatik.kit.edu>
Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
> I wrote a completion function for 'git worktree' as well, turns out a week
> or two before you posted this, but I never submitted it as it was way too
> convoluted.  Anyway, will send it in reply to this, just for reference.

And here it is. From the number of indentation levels and comment lines you can see why I haven't submitted this patch yet :)

OTOH it offers refs for -b and -B, and there are only fairly narrow corner cases when 'git --options' can fool it (but that's a general issue with __git_find_on_cmdline(), I wouldn't go into that).

 contrib/completion/git-completion.bash | 59 ++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index c97c648d7e..20a17e2c50 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2564,6 +2564,65 @@ _git_whatchanged ()
 	_git_log
 }
 
+_git_worktree ()
+{
+	local subcommand subcommand_idx sc c=1
+	local subcommands="add prune"
+
+	while [ $c -lt $cword ] && [ -z "$subcommand" ]; do
+		for sc in $subcommands; do
+			if [ "$sc" = "${words[c]}" ]; then
+				subcommand=$sc
+				subcommand_idx=$c
+				break
+			fi
+		done
+		((c++))
+	done
+
+	case "$subcommand,$cur" in
+	,*)
+		__gitcomp "$subcommands"
+		;;
+	add,--*)
+		__gitcomp "--detach"
+		;;
+	add,*)
+		case "$prev" in
+		-b|-B)
+			__gitcomp_nl "$(__git_refs)"
+			;;
+		-*)	# $prev is an option without argument: have to complete
+			# the path for the new worktree, fall back to bash
+			# filename completion
+			;;
+		*)	# $prev is not an option, so it must be either the
+			# 'add' subcommand, an argument of an option (e.g.
+			# branch for -b|-B), or the path for the new worktree
+			if [ $cword -eq $((subcommand_idx+1)) ]; then
+				# right after the 'add' subcommand, have to
+				# complete the path
+				:
+			else
+				case "${words[cword-2]}" in
+				-b|-B)	# after '-b <branch>', have to complete
+					# the path
+					;;
+				*)	# after the path, have to complete the
+					# branch to be checked out
+					__gitcomp_nl "$(__git_refs)"
+					;;
+				esac
+			fi
+			;;
+		esac
+		;;
+	prune,--*)
+		__gitcomp "--dry-run --verbose --expire"
+		;;
+	esac
+}
+
 __git_main ()
 {
 	local i c=1 command __git_dir
-- 
2.5.0.418.gdd37a9b
Previous: SZEDER GáborNext: Eric Sunshine
Message 5 of 6 in “contrib: teach completion about git-worktree options and arguments”
  1. contrib: teach completion about git-worktree options and argumentsEric Sunshine, Jul 23, 2015
  2. Eric SunshineAug 6, 2015
  3. Junio C HamanoAug 21, 2015
  4. SZEDER GáborAug 21, 2015
  5. completion: support 'git worktree'SZEDER Gábor, Aug 21, 2015
  6. Eric SunshineAug 21, 2015

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.