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

[RFC PATCH] bash completion: complete refs for git-grep

From
Thomas Rast <trast@student.ethz.ch>
Date
Oct 6, 2009, 10:08 UTC
Message-ID
<14ac499280c9b17f862ab13201b48c64b4827713.1254823328.git.trast@student.ethz.ch>

Attempt ref completion once we have seen a regular expression, to help the user with entering the <treeish> arguments.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---

The use-case for this is actually a bit protracted but came up on IRC yesterday: pasky asked if there was a simple way to grep through a certain file in all refs.

Turns out git-grep already has half the required support: when given a series of refs, it prefixes the matches with the ref, so the output is already in a useful format.

Sadly it does not appear to support --all, --branches or similar (which would be material for a separate patch). But bash completion can step in here: with M-*, it can expand all possible completions for the current word onto the command line.

This is still RFC because, as you can see in the code below, I tried to avoid completing at all while the user still needs to supply a regex. Sadly, bash turns the COMPREPLY=() into filename completion anyway. Is there a way to prevent this? Otherwise the regex complication should probably just go away and we can complete refs always.

 contrib/completion/git-completion.bash |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 6fd7e1d..c8cced6 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1048,6 +1048,24 @@ _git_grep ()
 		return
 		;;
 	esac
+
+	local i c=1 have_regex=""
+	while [ $c -lt $COMP_CWORD ]; do
+		i="${COMP_WORDS[c]}"
+		case "$i" in
+		-e) ;;
+		-e*) have_regex="$c" ; break ;;
+		-*) ;;
+		*) have_regex="$c"; break ;;
+		esac
+		c=$((++c))
+	done
+
+	if [ -n "$have_regex" ]; then
+		__gitcomp "$(__git_refs)"
+		return
+	fi
+
 	COMPREPLY=()
 }
 
-- 
1.6.5.rc2.251.g34f85
Next: Shawn O. Pearce
Message 1 of 6 in “bash completion: complete refs for git-grep”
  1. bash completion: complete refs for git-grepThomas Rast, Oct 6, 2009
  2. Shawn O. PearceOct 6, 2009
  3. Thomas RastOct 7, 2009
  4. bash completion: complete refs for git-grepThomas Rast, Oct 12, 2009
  5. Shawn O. PearceOct 12, 2009
  6. Junio C HamanoOct 12, 2009

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.