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

Re: [PATCH v2 2/2] completion: checkout: complete paths when ref given

From
CWCornelius Weig <cornelius.weig@tngtech.com>
Date
Feb 15, 2017, 10:46 UTC
Message-ID
<1cff1dea-baeb-3576-ad33-04b2c5d233d8@tngtech.com>
In-Reply-To
<CAM0VKjkqdto83Qo8PVbxt-2r8prQguNbAtNELxj5AmJEgugC_Q@mail.gmail.com>
Although I'm not convinced that completion of modified files is unnecessary, I'm at least persuaded that not all users would welcome such a change. Given the hint from Gabor that Alt-/ forces filesystem completion, there is even no big win in stopping to offer further refnames after one has already been given.
If you think that this would be desirable, find a revised version below. Otherwise drop it.
On 02/15/2017 04:11 AM, SZEDER Gábor wrote:
Show 7 quoted lines
> On Tue, Feb 14, 2017 at 10:24 PM,  <cornelius.weig@tngtech.com> wrote:
>> From: Cornelius Weig <cornelius.weig@tngtech.com>
>> Note that one corner-case is not covered by the current implementation:
>> if a refname contains a ':' and is followed by '--' the completion would
>> not recognize the valid refname.
> 
> I'm not sure what you mean here.  Refnames can't contain ':'.
Yes, my bad. I was confusing it with the case where filename and ref name are identical.
Show 9 quoted lines
>> +               while [ $c -lt $cword ]; do
>> +                       i="${words[c]}"
>> +                       case "$i" in
>> +                       --) seen_double_dash=1 ;;
>> +                       -*|?*:*) ;;
>> +                       *) ref="$i"; break ;;
> 
> I haven't tried it, but this would trigger on e.g. 'git checkout -b
> new-feature <TAB>', wouldn't it?
Correct, good eyes.
Show 8 quoted lines
> What about
> 
>   $ echo "unintentional change" >>tracked-file && git add -u
>   $ git rm important-file
>   $ git checkout HEAD <TAB>
> 
> ?  It seems it will offer neither 'tracked-file' nor 'important-file',
> but I think it should offer both.
Ideally yes. The latter of the two would also not work with Alt/.
-------------------------------------------------------------------
From d0e0c9af8a30dec479c393ae7fe75205c9b3b229 Mon Sep 17 00:00:00 2001
From: Cornelius Weig <cornelius.weig@tngtech.com>
Date: Tue, 14 Feb 2017 21:01:45 +0100
Subject: [PATCH] completion: checkout: complete paths when ref given

Git-checkout completes words starting with '--' as options and other words as refs. Even after specifying a ref, further words not starting with '--' are completed as refs, which is invalid for git-checkout.

With this commit completion suppresses refname suggestion after finding what looks like a refname. Words before a '--' not starting with a '-' and containing no ':' are considered to be refnames.

Signed-off-by: Cornelius Weig <cornelius.weig@tngtech.com>
---
 contrib/completion/git-completion.bash | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 6c6e1c774d..42e6463b67 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1059,7 +1059,16 @@ _git_bundle ()
 
 _git_checkout ()
 {
-	__git_has_doubledash && return
+	local c=2 seen_ref=""
+	while [ $c -lt $cword ]; do
+		case "${words[c]}" in
+		--) return ;;
+		-b|-B|--orphan|--branch) ((c++)) ;;
+		-*|*:*) ;;
+		*) seen_ref="y" ;;
+		esac
+		((c++))
+	done
 
 	case "$cur" in
 	--conflict=*)
@@ -1072,13 +1081,16 @@ _git_checkout ()
 			"
 		;;
 	*)
-		# check if --track, --no-track, or --no-guess was specified
-		# if so, disable DWIM mode
-		local flags="--track --no-track --no-guess" track=1
-		if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
-			track=''
+		if [ -z "$seen_ref" ]
+		then
+			# check for --track, --no-track, or --no-guess
+			# if so, disable DWIM mode
+			local flags="--track --no-track --no-guess" track=1
+			if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
+				track=''
+			fi
+			__gitcomp_nl "$(__git_refs '' $track)"
 		fi
-		__gitcomp_nl "$(__git_refs '' $track)"
 		;;
 	esac
 }
-- 
2.11.1
Previous: SZEDER GáborNext: SZEDER Gábor
Message 10 of 12 in “completion: complete modified files for checkout with '--'”
  1. completion: complete modified files for checkout with '--'cornelius.weig@tngtech.com, Feb 13, 2017
  2. SZEDER GáborFeb 14, 2017
  3. Cornelius WeigFeb 14, 2017
  4. 1/2 completion: extract utility to complete paths from tree-ishcornelius.weig@tngtech.com, Feb 14, 2017
  5. 2/2 completion: checkout: complete paths when ref givencornelius.weig@tngtech.com, Feb 14, 2017
  6. Junio C HamanoFeb 14, 2017
  7. Cornelius WeigFeb 14, 2017
  8. Junio C HamanoFeb 14, 2017
  9. SZEDER GáborFeb 15, 2017
  10. Cornelius WeigFeb 15, 2017
  11. SZEDER GáborFeb 15, 2017
  12. Cornelius WeigFeb 15, 2017

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.