All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] completion: complete remote branches with switch --track
@ 2020-04-22 20:15 Jacob Keller
  2020-04-23  2:03 ` Jonathan Nieder
  0 siblings, 1 reply; 4+ messages in thread
From: Jacob Keller @ 2020-04-22 20:15 UTC (permalink / raw)
  To: git; +Cc: Jacob Keller

From: Jacob Keller <jacob.keller@gmail.com>

If the --track option is supplied to git switch, then a new branch will
be created tracking the specified remote branch.

Fix git completion support so that remote branches will be completed
when --track is enabled.

Add a couple of simple test cases to help cover this new behavior. Note
that ideally completion for --track would only allow remote branches,
and would not complete all refs like HEAD, FETCH_HEAD, etc, so one of
the new tests is a test_expect_failure to capture this.

Fixes: ae36fe694180 ("completion: support switch")
Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
---
I wasn't able to figure out how to get completion to ignore things like tags
and similar, but I think this is still an improvement.

 contrib/completion/git-completion.bash |  8 +++++---
 t/t9902-completion.sh                  | 22 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index c21786f2fd00..6de341fd1587 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2235,12 +2235,14 @@ _git_switch ()
 		if [ -n "$(__git_find_on_cmdline "--guess")" ]; then
 			track_opt='--track'
 		fi
-		if [ -z "$(__git_find_on_cmdline "-d --detach")" ]; then
-			only_local_ref=y
-		else
+		if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then
 			# --guess --detach is invalid combination, no
 			# dwim will be done when --detach is specified
 			track_opt=
+		elif [ -z "$(__git_find_on_cmdline "--track")" ]; then
+			# if neither --detach or --track are specified then
+			# match only local refs.
+			only_local_ref=y
 		fi
 		if [ $only_local_ref = y -a -z "$track_opt" ]; then
 			__gitcomp_direct "$(__git_heads "" "$cur" " ")"
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 5505e5aa249e..a2478eae6933 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1760,6 +1760,28 @@ do
 	'
 done
 
+test_expect_success 'git switch - default local branches only' '
+	test_completion "git switch m" <<-\EOF
+	master Z
+	master-in-other Z
+	mybranch Z
+	mytag Z
+	EOF
+'
+
+test_expect_failure 'git switch - --track remote branches' '
+	test_completion "git switch --track " <<-\EOF
+	other/branch-in-other Z
+	other/master-in-other Z
+	EOF
+'
+
+test_expect_success 'git switch - --track remote branches partial completion' '
+	test_completion "git switch --track other/master-in" <<-\EOF
+	other/master-in-other Z
+	EOF
+'
+
 test_expect_success 'git config - section' '
 	test_completion "git config br" <<-\EOF
 	branch.Z
-- 
2.25.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-04-23 23:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-22 20:15 [PATCH] completion: complete remote branches with switch --track Jacob Keller
2020-04-23  2:03 ` Jonathan Nieder
2020-04-23  4:33   ` Jacob Keller
2020-04-23 23:46   ` Jacob Keller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.