git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: cornelius.weig@tngtech.com
To: git@vger.kernel.org
Cc: Cornelius Weig <cornelius.weig@tngtech.com>,
	szeder.dev@gmail.com,
	bitte.keine.werbung.einwerfen@googlemail.com, j6t@kdbg.org
Subject: [PATCH v2 2/2] completion: checkout: complete paths when ref given
Date: Tue, 14 Feb 2017 22:24:04 +0100	[thread overview]
Message-ID: <20170214212404.31469-2-cornelius.weig@tngtech.com> (raw)
In-Reply-To: <20170214212404.31469-1-cornelius.weig@tngtech.com>

From: Cornelius Weig <cornelius.weig@tngtech.com>

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.

This commit ensures that after specifying a ref, further non-option
words are completed as paths. Four cases are considered:

 - If the word contains a ':', do not treat it as reference and use
   regular revlist completion.
 - If no ref is found on the command line, complete non-options as refs
   as before.
 - If the ref is HEAD or @, complete only with modified files because
   checking out unmodified files is a noop.
   This case also applies if no ref is given, but '--' is present.
 - If a ref other than HEAD or @ is found, offer only valid paths from
   that revision.

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.

Signed-off-by: Cornelius Weig <cornelius.weig@tngtech.com>
---
 contrib/completion/git-completion.bash | 39 +++++++++++++++++++++++++++-------
 1 file changed, 31 insertions(+), 8 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 4ab119d..df46f62 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1068,7 +1068,7 @@ _git_bundle ()
 
 _git_checkout ()
 {
-	__git_has_doubledash && return
+	local i c=2 ref="" seen_double_dash=""
 
 	case "$cur" in
 	--conflict=*)
@@ -1081,13 +1081,36 @@ _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=''
-		fi
-		__gitcomp_nl "$(__git_refs '' $track)"
+		while [ $c -lt $cword ]; do
+			i="${words[c]}"
+			case "$i" in
+			--) seen_double_dash=1 ;;
+			-*|?*:*) ;;
+			*) ref="$i"; break ;;
+			esac
+			((c++))
+		done
+
+		case "$ref,$seen_double_dash,$cur" in
+		,,*:*)
+		    __git_complete_revlist_file
+		    ;;
+		,,*)
+			# 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)"
+			;;
+		,1,*|@,*|HEAD,*)
+			__git_complete_index_file "--modified"
+			;;
+		*)
+			__git_complete_tree_file "$ref" "$cur"
+			;;
+		esac
 		;;
 	esac
 }
-- 
2.10.2


  reply	other threads:[~2017-02-14 21:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-13 23:33 [PATCH] completion: complete modified files for checkout with '--' cornelius.weig
2017-02-14  0:50 ` SZEDER Gábor
2017-02-14 21:13   ` Cornelius Weig
2017-02-14 21:24     ` [PATCH v2 1/2] completion: extract utility to complete paths from tree-ish cornelius.weig
2017-02-14 21:24       ` cornelius.weig [this message]
2017-02-14 21:31         ` [PATCH v2 2/2] completion: checkout: complete paths when ref given Junio C Hamano
2017-02-14 22:13           ` Cornelius Weig
2017-02-14 22:45             ` Junio C Hamano
2017-02-15  3:11         ` SZEDER Gábor
2017-02-15 10:46           ` Cornelius Weig
2017-02-15 14:26         ` SZEDER Gábor
2017-02-15 22:45           ` Cornelius Weig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170214212404.31469-2-cornelius.weig@tngtech.com \
    --to=cornelius.weig@tngtech.com \
    --cc=bitte.keine.werbung.einwerfen@googlemail.com \
    --cc=git@vger.kernel.org \
    --cc=j6t@kdbg.org \
    --cc=szeder.dev@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).