threads / rfc / 43921

RFC patchcompletion: support excluding refs

Subject: [RFC PATCH] completion: support excluding refs

## tl;dr

2 messages between Aug 24, 2016 and Aug 24, 2016. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Chris Packham· Aug 24, 2016, 08:41 UTC · lore

Allow completion of refs with a ^ prefix. This allows completion of commands like 'git log HEAD ^origin/master'.

Signed-off-by: Chris Packham <judge.packham@gmail.com>
---
I often find myself using variations of 'git log HEAD ^origin/master' to
see commits that I have locally that have not been pushed. It is also
useful when reviewing changes before integrating them e.g. 'git fetch
...; git log FETCH_HEAD ^origin/master'.
 contrib/completion/git-completion.bash | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
Show changes to contrib/completion/git-completion.bash +4 −3
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 9c8f738..21016bf 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -338,7 +338,7 @@ __git_tags ()
 __git_refs ()
 {
 	local i hash dir="$(__gitdir "${1-}")" track="${2-}"
-	local format refs
+	local format refs pfx
 	if [ -d "$dir" ]; then
 		case "$cur" in
 		refs|refs/*)
@@ -347,14 +347,15 @@ __git_refs ()
 			track=""
 			;;
 		*)
+			[[ "$cur" == ^* ]] && pfx="^"
 			for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
-				if [ -e "$dir/$i" ]; then echo $i; fi
+				if [ -e "$dir/$i" ]; then echo $pfx$i; fi
 			done
 			format="refname:short"
 			refs="refs/tags refs/heads refs/remotes"
 			;;
 		esac
-		git --git-dir="$dir" for-each-ref --format="%($format)" \
+		git --git-dir="$dir" for-each-ref --format="$pfx%($format)" \
 			$refs
 		if [ -n "$track" ]; then
 			# employ the heuristic used by git checkout
-- 
2.9.2.518.ged577c6.dirty
Junio C Hamano· Aug 24, 2016, 16:52 UTC · re: Chris Packham · lore

Re: [RFC PATCH] completion: support excluding refs

Chris Packham <judge.packham@gmail.com> writes:
Show 15 quoted lines
> Allow completion of refs with a ^ prefix. This allows completion of
> commands like 'git log HEAD ^origin/master'.
> ...
> +			[[ "$cur" == ^* ]] && pfx="^"
>  			for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
> -				if [ -e "$dir/$i" ]; then echo $i; fi
> +				if [ -e "$dir/$i" ]; then echo $pfx$i; fi
>  			done
>  			format="refname:short"
>  			refs="refs/tags refs/heads refs/remotes"
>  			;;
>  		esac
> -		git --git-dir="$dir" for-each-ref --format="%($format)" \
> +		git --git-dir="$dir" for-each-ref --format="$pfx%($format)" \
>  			$refs

That looks like a reasonable approach to me (but I am no expert in this area myself).

← back to recent threads