threads / patch / 14800

patchbash completion: Add completion for 'git grep'

Subject: [PATCH] bash completion: Add completion for 'git grep'

## tl;dr

7 messages between Aug 2, 2008 and Aug 4, 2008. Diffs are folded; open one to read it.

replies: 6people: 2as markdown or json

Lee Marlow· Aug 2, 2008, 00:56 UTC · lore
Added completions for all long options specified in the docs
	--cached
	--text --ignore-case --word-regexp --invert-match
	--full-name
	--extended-regexp --basic-regexp --fixed-strings
	--files-with-matches --name-only
	--files-without-match
	--count
	--and --or --not --all-match
Signed-off-by: Lee Marlow <lee.marlow@gmail.com>
---
 contrib/completion/git-completion.bash |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)
Show changes to contrib/completion/git-completion.bash +24 −0
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 30d8701..b28ac10 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -796,6 +796,29 @@ _git_gc ()
 	COMPREPLY=()
 }
 
+_git_grep ()
+{
+	__git_has_doubledash && return
+
+	local cur="${COMP_WORDS[COMP_CWORD]}"
+	case "$cur" in
+	--*)
+		__gitcomp "
+			--cached
+			--text --ignore-case --word-regexp --invert-match
+			--full-name
+			--extended-regexp --basic-regexp --fixed-strings
+			--files-with-matches --name-only
+			--files-without-match
+			--count
+			--and --or --not --all-match
+			"
+		return
+		;;
+	esac
+	COMPREPLY=()
+}
+
 _git_help ()
 {
 	local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -1486,6 +1509,7 @@ _git ()
 	fetch)       _git_fetch ;;
 	format-patch) _git_format_patch ;;
 	gc)          _git_gc ;;
+	grep)        _git_grep ;;
 	help)        _git_help ;;
 	log)         _git_log ;;
 	ls-remote)   _git_ls_remote ;;
-- 
1.6.0.rc1.27.g9b6bf
Shawn O. Pearce· Aug 2, 2008, 21:05 UTC · re: Lee Marlow · lore

Re: [PATCH] bash completion: Add completion for 'git grep'

Lee Marlow <lee.marlow@gmail.com> wrote:
Show 22 quoted lines
> +_git_grep ()
> +{
> +	__git_has_doubledash && return
> +
> +	local cur="${COMP_WORDS[COMP_CWORD]}"
> +	case "$cur" in
> +	--*)
> +		__gitcomp "
> +			--cached
> +			--text --ignore-case --word-regexp --invert-match
> +			--full-name
> +			--extended-regexp --basic-regexp --fixed-strings
> +			--files-with-matches --name-only
> +			--files-without-match
> +			--count
> +			--and --or --not --all-match
> +			"
> +		return
> +		;;
> +	esac
> +	COMPREPLY=()
> +}

Hmm. The has_doubledash test seems redundant since we don't do anything with args that aren't --foo. Even though git-grep will accept a tree-ish and thus completion of __git_refs here may make sense.

I wonder if we shouldn't just add to the end something like:
	__gitcomp "$(__git_refs)"

like the _git_reset function does. Then we can complete a tree-ish for searching, as well as honor -- to stop tree-ish completion and go back to file/directory completion.

But that is very much a user question. Do users mostly search a file in the current working directory, or do they mostly search a tree-ish?

-- 
Shawn.
Lee Marlow· Aug 3, 2008, 07:31 UTC · re: Shawn O. Pearce · lore

Re: [PATCH] bash completion: Add completion for 'git grep'

On Sat, Aug 2, 2008 at 3:05 PM, Shawn O. Pearce <spearce@spearce.org> wrote:
Show 9 quoted lines
>
> Hmm.  The has_doubledash test seems redundant since we don't do
> anything with args that aren't --foo.  Even though git-grep will
> accept a tree-ish and thus completion of __git_refs here may
> make sense.
>
> But that is very much a user question.  Do users mostly search a
> file in the current working directory, or do they mostly search
> a tree-ish?

I haven't found myself using grep to search anything but the current working directory. I wonder whether __git_complete_file would be better than __git_refs. My issue with __git_complete_file in this case and also doing completion for 'git mv' is that it falls back to just __git_refs. Would it be better if it fell back to __git_refs and ls-tree for HEAD? That way when using completion to get to Documentation/git-grep.txt, it doesn't also show completions for Documentation/git-grep.{1,html,xml}.

-Lee
Shawn O. Pearce· Aug 4, 2008, 04:06 UTC · re: Lee Marlow · lore

Re: [PATCH] bash completion: Add completion for 'git grep'

Lee Marlow <lee.marlow@gmail.com> wrote:
Show 14 quoted lines
> On Sat, Aug 2, 2008 at 3:05 PM, Shawn O. Pearce <spearce@spearce.org> wrote:
> >
> > Hmm.  The has_doubledash test seems redundant since we don't do
> > anything with args that aren't --foo.  Even though git-grep will
> > accept a tree-ish and thus completion of __git_refs here may
> 
> I haven't found myself using grep to search anything but the current
> working directory.  I wonder whether __git_complete_file would be
> better than __git_refs.  My issue with __git_complete_file in this
> case and also doing completion for 'git mv' is that it falls back to
> just __git_refs.  Would it be better if it fell back to __git_refs and
> ls-tree for HEAD?  That way when using completion to get to
> Documentation/git-grep.txt,  it doesn't also show completions for
> Documentation/git-grep.{1,html,xml}.

Hmm. __git_complete_file is about completing something in a tree-ish, which is really most likely not what we want for grep. Its useful for git-show and git-cat-file, and that's about it. Most git tools prefer a calling format of "tree-ish -- paths ..." and not "tree-ish:path".

Though I think you have an excellent point about completing paths by ls-tree for HEAD rather than the working directory itself, as then you can avoid produced files. But in practice how often do you pass a single file or a couple of files to the grep utility and are having problems bypassing the build products? Compared to just running grep on the entire source tree to find hits? I never use grep to search in a single file, but I call it several times a day to look for where a function is defined or called.

After having thought about it your original patch makes the most sense, but without the has_doubledash test.

-- 
Shawn.
Lee Marlow· Aug 4, 2008, 05:26 UTC · re: Shawn O. Pearce · lore
Added completions for all long options specified in the docs
	--cached
	--text --ignore-case --word-regexp --invert-match
	--full-name
	--extended-regexp --basic-regexp --fixed-strings
	--files-with-matches --name-only
	--files-without-match
	--count
	--and --or --not --all-match
Signed-off-by: Lee Marlow <lee.marlow@gmail.com>
---
On Sun, Aug 3, 2008 at 10:06 PM, Shawn O. Pearce <spearce@spearce.org> wrote:
Show 5 quoted lines
> Though I think you have an excellent point about completing paths
> by ls-tree for HEAD rather than the working directory itself,
> as then you can avoid produced files.  But in practice how often
> do you pass a single file or a couple of files to the grep utility
> and are having problems bypassing the build products?

I think you're right that it's not a good fit for grep, but maybe for some other commands. mv and commit come to mind. I'll look into this as I'm trying to add completions for the more common (i.e. ones that I use) porcelain commands.

> After having thought about it your original patch makes the most
> sense, but without the has_doubledash test.

This updated patch removes the __git_has_doublehash line. However, I just noticed that d773c631 added __git_has_doublehash to prevent completions of long options after --. So, maybe it's not too redundant and my first patch might be better.

-Lee
 contrib/completion/git-completion.bash |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)
Show changes to contrib/completion/git-completion.bash +22 −0
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 678a155..96181dc 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -791,6 +791,27 @@ _git_gc ()
 	COMPREPLY=()
 }
 
+_git_grep ()
+{
+	local cur="${COMP_WORDS[COMP_CWORD]}"
+	case "$cur" in
+	--*)
+		__gitcomp "
+			--cached
+			--text --ignore-case --word-regexp --invert-match
+			--full-name
+			--extended-regexp --basic-regexp --fixed-strings
+			--files-with-matches --name-only
+			--files-without-match
+			--count
+			--and --or --not --all-match
+			"
+		return
+		;;
+	esac
+	COMPREPLY=()
+}
+
 _git_help ()
 {
 	local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -1482,6 +1503,7 @@ _git ()
 	fetch)       _git_fetch ;;
 	format-patch) _git_format_patch ;;
 	gc)          _git_gc ;;
+	grep)        _git_grep ;;
 	help)        _git_help ;;
 	log)         _git_log ;;
 	ls-remote)   _git_ls_remote ;;
-- 
1.6.0.rc1.48.g2b6032
Shawn O. Pearce· Aug 4, 2008, 14:52 UTC · re: Lee Marlow · lore

Re: [PATCH] bash completion: Add completion for 'git grep'

Lee Marlow <lee.marlow@gmail.com> wrote:
> This updated patch removes the __git_has_doublehash line.  However, I
> just noticed that d773c631 added __git_has_doublehash to prevent
> completions of long options after --.  So, maybe it's not too
> redundant and my first patch might be better.
Heh.  I forgot about that.  You're right, your first patch is better.
-- 
Shawn.
Shawn O. Pearce· Aug 4, 2008, 14:53 UTC · re: Lee Marlow · lore

Re: [PATCH] bash completion: Add completion for 'git grep'

Lee Marlow <lee.marlow@gmail.com> wrote:
Show 11 quoted lines
> Added completions for all long options specified in the docs
> 	--cached
> 	--text --ignore-case --word-regexp --invert-match
> 	--full-name
> 	--extended-regexp --basic-regexp --fixed-strings
> 	--files-with-matches --name-only
> 	--files-without-match
> 	--count
> 	--and --or --not --all-match
> 
> Signed-off-by: Lee Marlow <lee.marlow@gmail.com>
Acked-by: Shawn O. Pearce <spearce@spearce.org>

This is the "first patch" which has the doubledash test. As you pointed out, its better because it stops completion of long options after the --.

Show 43 quoted lines
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 30d8701..b28ac10 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -796,6 +796,29 @@ _git_gc ()
>  	COMPREPLY=()
>  }
>  
> +_git_grep ()
> +{
> +	__git_has_doubledash && return
> +
> +	local cur="${COMP_WORDS[COMP_CWORD]}"
> +	case "$cur" in
> +	--*)
> +		__gitcomp "
> +			--cached
> +			--text --ignore-case --word-regexp --invert-match
> +			--full-name
> +			--extended-regexp --basic-regexp --fixed-strings
> +			--files-with-matches --name-only
> +			--files-without-match
> +			--count
> +			--and --or --not --all-match
> +			"
> +		return
> +		;;
> +	esac
> +	COMPREPLY=()
> +}
> +
>  _git_help ()
>  {
>  	local cur="${COMP_WORDS[COMP_CWORD]}"
> @@ -1486,6 +1509,7 @@ _git ()
>  	fetch)       _git_fetch ;;
>  	format-patch) _git_format_patch ;;
>  	gc)          _git_gc ;;
> +	grep)        _git_grep ;;
>  	help)        _git_help ;;
>  	log)         _git_log ;;
>  	ls-remote)   _git_ls_remote ;;
> -- 
-- 
Shawn.

← back to recent threads