threads / patch / 25796

patchbash completion: add basic support for git-reflog

Subject: [PATCH] bash completion: add basic support for git-reflog

## tl;dr

5 messages between Nov 20, 2010 and Nov 22, 2010. Diffs are folded; open one to read it.

replies: 4people: 3as markdown or json

Tay Ray Chuan· Nov 20, 2010, 17:32 UTC · lore

Add basic completion for the three subcommands - show, expire, delete. Try completing refs for these too.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
 contrib/completion/git-completion.bash |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
Show changes to contrib/completion/git-completion.bash +16 −0
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index f710469..4007ca1 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1632,6 +1632,22 @@ _git_rebase ()
 	__gitcomp "$(__git_refs)"
 }
 
+_git_reflog ()
+{
+	local SUB_CMDS=(show delete expire)
+	local cur="${COMP_WORDS[COMP_CWORD-1]}"
+
+	for val in ${SUB_CMDS[*]}; do
+		if [[ "$val" == "$cur" ]]; then
+			# this is a subcommand
+			__gitcomp "$(__git_refs)"
+			return
+		fi
+	done
+
+	__gitcomp "${SUB_CMDS[*]}"
+}
+
 __git_send_email_confirm_options="always never auto cc compose"
 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
 
-- 
1.7.3.2.496.g9c54
SZEDER Gábor· Nov 22, 2010, 15:24 UTC · re: Tay Ray Chuan · lore

Re: [PATCH] bash completion: add basic support for git-reflog

Hi,
On Sun, Nov 21, 2010 at 01:32:48AM +0800, Tay Ray Chuan wrote:
> Add basic completion for the three subcommands - show, expire, delete.
> Try completing refs for these too.

Heh, I've always thought reflog is plumbing, and that's why the completion script doesn't support it.

Show 23 quoted lines
> Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
> ---
>  contrib/completion/git-completion.bash |   16 ++++++++++++++++
>  1 files changed, 16 insertions(+), 0 deletions(-)
> 
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index f710469..4007ca1 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -1632,6 +1632,22 @@ _git_rebase ()
>  	__gitcomp "$(__git_refs)"
>  }
>  
> +_git_reflog ()
> +{
> +	local SUB_CMDS=(show delete expire)
> +	local cur="${COMP_WORDS[COMP_CWORD-1]}"
> +
> +	for val in ${SUB_CMDS[*]}; do
> +		if [[ "$val" == "$cur" ]]; then
> +			# this is a subcommand
> +			__gitcomp "$(__git_refs)"
> +			return

This only looks at the previous word on the command line, and leads to wrong behavior when an option is used:

  $ git reflog expire --verbose <TAB><TAB>
  delete   expire   show     

To prevent this you could use the __git_find_on_cmdline() helper function and follow how other completion functions use it.

Show 13 quoted lines
> +		fi
> +	done
> +
> +	__gitcomp "${SUB_CMDS[*]}"
> +}
> +
>  __git_send_email_confirm_options="always never auto cc compose"
>  __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
>  
> -- 
> 1.7.3.2.496.g9c54
> 
> 
Tay Ray Chuan· Nov 22, 2010, 15:50 UTC · re: SZEDER Gábor · lore

Re: [PATCH] bash completion: add basic support for git-reflog

Hi,
2010/11/22 SZEDER Gábor <szeder@ira.uka.de>:
Show 6 quoted lines
> On Sun, Nov 21, 2010 at 01:32:48AM +0800, Tay Ray Chuan wrote:
>> Add basic completion for the three subcommands - show, expire, delete.
>> Try completing refs for these too.
>
> Heh, I've always thought reflog is plumbing, and that's why the
> completion script doesn't support it.

That didn't really cross my mind, I just did this out of my irritation with its absence.

(After some checking, it's actually part of git-log, so I don't think it counts as plumbing.)

Show 8 quoted lines
> This only looks at the previous word on the command line, and leads to
> wrong behavior when an option is used:
>
>  $ git reflog expire --verbose <TAB><TAB>
>  delete   expire   show
>
> To prevent this you could use the __git_find_on_cmdline() helper
> function and follow how other completion functions use it.
Yep I'll look into it.
-- 
Cheers,
Ray Chuan
Tay Ray Chuan· Nov 22, 2010, 17:09 UTC · re: Tay Ray Chuan · lore

"Promote" the reflog command out of plumbing, so that we now run completion for it. After all, it's listed under porcelain (ancillary), and we do run completion for those commands.

Add basic completion for the three subcommands - show, expire, delete. Try completing refs for these too.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
Changed from v1: 
 - picked up Gábor's suggestion on using __git_find_on_cmdline()
   to correctly handle situations where subcommands are used with dashed
   options.
 - don't "hide" reflog anymore - run completion for it too.

Gábor: hmm, it really seems that reflog is treated as plumbing - no completion is done for it. Even get-tar-commit-id (I've never used this before) is treated better than reflog. Shall do something about it!

 contrib/completion/git-completion.bash |   13 ++++++++++++-
 1 files changed, 12 insertions(+), 1 deletions(-)
Show changes to contrib/completion/git-completion.bash +12 −1
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index f710469..6732b1d 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -735,7 +735,6 @@ __git_list_porcelain_commands ()
 		quiltimport)      : import;;
 		read-tree)        : plumbing;;
 		receive-pack)     : plumbing;;
-		reflog)           : plumbing;;
 		remote-*)         : transport;;
 		repo-config)      : deprecated;;
 		rerere)           : plumbing;;
@@ -1632,6 +1631,18 @@ _git_rebase ()
 	__gitcomp "$(__git_refs)"
 }
 
+_git_reflog ()
+{
+	local subcommands="show delete expire"
+	local subcommand="$(__git_find_on_cmdline "$subcommands")"
+
+	if [ -z "$subcommand" ]; then
+		__gitcomp "$subcommands"
+	else
+		__gitcomp "$(__git_refs)"
+	fi
+}
+
 __git_send_email_confirm_options="always never auto cc compose"
 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
 
-- 
1.7.3.2.493.g2b058
Jonathan Nieder· Nov 22, 2010, 17:20 UTC · re: Tay Ray Chuan · lore

Re: [PATCH] bash completion: add basic support for git-reflog

Tay Ray Chuan wrote:
> Gábor: hmm, it really seems that reflog is treated as plumbing - no completion
> is done for it.

Probably because it is listed as such in command-list.txt. I guess the user-facing command for reflog expiration is meant to be "git gc"?

Show 9 quoted lines
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -1632,6 +1631,18 @@ _git_rebase ()
>  	__gitcomp "$(__git_refs)"
>  }
>  
> +_git_reflog ()
> +{
> +	local subcommands="show delete expire"

People really do seem to use these commands directly, so I'm all for this.

I wish there were a way to easily point the end user from "git reflog show" to "git log --walk-reflogs" (to make the list of other accepted options more obvious) but I can't think of one.

Regards, Jonathan

← back to recent threads