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

[PATCH v3 13/16] completion: improve handling of --detach in checkout

From
Jacob Keller <jacob.e.keller@intel.com>
Date
May 28, 2020, 18:10 UTC
Message-ID
<20200528181048.3509470-14-jacob.e.keller@intel.com>
In-Reply-To
<20200528181048.3509470-1-jacob.e.keller@intel.com>
From: Jacob Keller <jacob.keller@gmail.com>

Just like git switch, we should not complete DWIM remote branch names if --detach has been specified. To avoid this, refactor _git_checkout in a similar way to _git_switch.

Note that we don't simply clear dwim_opt when we find -d or --detach, as we will be adding other modes and checks, making this flow easier to follow.

Update the previously failing tests to show that the breakage has been resolved.

Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
---
 contrib/completion/git-completion.bash | 7 ++++++-
 t/t9902-completion.sh                  | 6 ++----
 2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 53afd72d0e4e..38b5a5a0d874 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1489,7 +1489,12 @@ _git_checkout ()
 		;;
 	*)
 		local dwim_opt="$(__git_checkout_default_dwim_mode)"
-		__git_complete_refs $dwim_opt
+
+		if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then
+			__git_complete_refs --mode="refs"
+		else
+			__git_complete_refs $dwim_opt --mode="refs"
+		fi
 		;;
 	esac
 }
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index f4f3a3ca3d55..7e56a62a9bb3 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1372,8 +1372,7 @@ test_expect_success 'git switch - with --detach, complete all references' '
 	EOF
 '
 
-#TODO: checkout --detach incorrectly includes DWIM remote branch names
-test_expect_failure 'git checkout - with --detach, complete only references' '
+test_expect_success 'git checkout - with --detach, complete only references' '
 	test_completion "git checkout --detach " <<-\EOF
 	HEAD Z
 	master Z
@@ -1395,8 +1394,7 @@ test_expect_success 'git switch - with -d, complete all references' '
 	EOF
 '
 
-#TODO: checkout -d incorrectly includes DWIM remote branch names
-test_expect_failure 'git checkout - with -d, complete only references' '
+test_expect_success 'git checkout - with -d, complete only references' '
 	test_completion "git checkout -d " <<-\EOF
 	HEAD Z
 	master Z
-- 
2.25.2
Previous: Jacob KellerNext: Jacob Keller
Message 14 of 17 in “improve switch/checkout completion”
  1. 00/16 improve switch/checkout completionJacob Keller, May 28, 2020
  2. 10/16 completion: perform DWIM logic directly in __git_complete_refsJacob Keller, May 28, 2020
  3. 08/16 completion: replace overloaded track term for __git_complete_refsJacob Keller, May 28, 2020
  4. 09/16 completion: extract function __git_dwim_remote_headsJacob Keller, May 28, 2020
  5. 14/16 completion: improve handling of --track in switch/checkoutJacob Keller, May 28, 2020
  6. 16/16 completion: improve handling of --orphan option of switch/checkoutJacob Keller, May 28, 2020
  7. 12/16 completion: improve completion for git switch with no optionsJacob Keller, May 28, 2020
  8. 07/16 completion: add tests showing subpar switch/checkout --orphan logicJacob Keller, May 28, 2020
  9. 11/16 completion: improve handling of DWIM mode for switch/checkoutJacob Keller, May 28, 2020
  10. 03/16 completion: add tests showing subar checkout --detach logicJacob Keller, May 28, 2020
  11. 04/16 completion: add tests showing subpar switch/checkout --track logicJacob Keller, May 28, 2020
  12. 02/16 completion: add tests showing subpar DWIM logic for switch/checkoutJacob Keller, May 28, 2020
  13. 01/16 completion: add test showing subpar git switch completionJacob Keller, May 28, 2020
  14. 13/16 completion: improve handling of --detach in checkoutJacob Keller, May 28, 2020
  15. 05/16 completion: add tests showing subpar -c/-C startpoint completionJacob Keller, May 28, 2020
  16. 06/16 completion: add tests showing subpar -c/C argument completionJacob Keller, May 28, 2020
  17. 15/16 completion: improve handling of -c/-C and -b/-B in switch/checkoutJacob Keller, May 28, 2020

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.