All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] bash-completion: Make use of git status
@ 2011-08-16  6:09 Ron Panduwana
  2011-08-19  9:54 ` Ron Panduwana
  2011-08-19 10:10 ` Thomas Rast
  0 siblings, 2 replies; 4+ messages in thread
From: Ron Panduwana @ 2011-08-16  6:09 UTC (permalink / raw)
  To: git; +Cc: Shawn O. Pearce, Junio C Hamano, Lee Marlow, Ron Panduwana

When autocompleting git add, rm, checkout --, and reset HEAD; Make it so they use git status to offer suitable files.

Signed-off-by: Ron Panduwana <panduwana@gmail.com>
---
Hello,

I made some little improvements to the bash autocompletion script for git add, rm, checkout --, and reset HEAD.
It's because sometimes after doing "git status" I need to do one/some of the commands and hope the autocompletion can make use the status info git already knows.
For example:

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   path/to/deeply/located/added-file
#	modified:   path/to/deeply/located/old-file
#	deleted:    path/to/deeply/located/removed-file
#
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	deleted:    path/to/deeply/located/deleted-file
#	modified:   path/to/deeply/located/modified-file
#	deleted:    path/to/deeply/located/old-file
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	path/to/deeply/located/new-file


If I need to do one of the commands, I'd like my autocompletion to work like these:

$ git add [TAB]path/to/deeply/located/[TAB][TAB]
path/to/deeply/located/modified-file   path/to/deeply/located/new-file

$ git rm [TAB]path/to/deeply/located/[TAB][TAB]
path/to/deeply/located/deleted-file   path/to/deeply/located/old-file

$ git checkout -- [TAB]path/to/deeply/located/[TAB][TAB]
path/to/deeply/located/deleted-file    path/to/deeply/located/modified-file   path/to/deeply/located/old-file

$ git reset HEAD [TAB]path/to/deeply/located/[TAB][TAB]
path/to/deeply/located/added-file     path/to/deeply/located/old-file       path/to/deeply/located/removed-file


I think it's a good thing to be included as default, so here I send the diff of my modification of the autocompletion script.

PS: for the modification I've taken into account:
- has_doubledash
- when we meant to autocomplete the --switches
- for git rm, whether --cached presents
- for git checkout, only if it starts with "git checkout -- "
- for git reset, only if it starts with "git reset HEAD "
- (ie. when we meant to autocomplete git_refs)

Hope it'd be useful.

 contrib/completion/git-completion.bash |   76 ++++++++++++++++++++------------
 1 files changed, 48 insertions(+), 28 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 5a83090..233bdbb 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1010,6 +1010,12 @@ __git_has_doubledash ()
 	return 1
 }

+# __git_files_having_status requires 1 argument
+__git_files_having_status ()
+{
+	echo "$(git status -uall --porcelain . 2>/dev/null | egrep "^$1" | cut -c4-)"
+}
+
 __git_whitespacelist="nowarn warn error error-all fix"

 _git_am ()
@@ -1058,17 +1064,17 @@ _git_apply ()

 _git_add ()
 {
-	__git_has_doubledash && return
-
-	case "$cur" in
-	--*)
-		__gitcomp "
-			--interactive --refresh --patch --update --dry-run
-			--ignore-errors --intent-to-add
-			"
-		return
-	esac
-	COMPREPLY=()
+	if ! __git_has_doubledash; then
+		case "$cur" in
+		--*)
+			__gitcomp "
+				--interactive --refresh --patch --update --dry-run
+				--ignore-errors --intent-to-add
+				"
+			return
+		esac
+	fi
+	__gitcomp "$(__git_files_having_status "(.[MAU]|UD|\?\?)")"
 }

 _git_archive ()
@@ -1171,7 +1177,12 @@ _git_bundle ()

 _git_checkout ()
 {
-	__git_has_doubledash && return
+	if __git_has_doubledash; then
+		if [[ ${words[2]} = "--" ]]; then
+			__gitcomp "$(__git_files_having_status ".[MD]")"
+		fi
+		return
+	fi

 	case "$cur" in
 	--conflict=*)
@@ -2313,14 +2324,18 @@ _git_replace ()

 _git_reset ()
 {
-	__git_has_doubledash && return
-
-	case "$cur" in
-	--*)
-		__gitcomp "--merge --mixed --hard --soft --patch"
+	if ! __git_has_doubledash; then
+		case "$cur" in
+		--*)
+			__gitcomp "--merge --mixed --hard --soft --patch"
+			return
+			;;
+		esac
+	fi
+	if [[ ${words[2]} = "HEAD" ]]; then
+		__gitcomp "$(__git_files_having_status "[ADM].")"
 		return
-		;;
-	esac
+	fi
 	__gitcomp "$(__git_refs)"
 }

@@ -2337,15 +2352,20 @@ _git_revert ()

 _git_rm ()
 {
-	__git_has_doubledash && return
-
-	case "$cur" in
-	--*)
-		__gitcomp "--cached --dry-run --ignore-unmatch --quiet"
-		return
-		;;
-	esac
-	COMPREPLY=()
+	if ! __git_has_doubledash; then
+		case "$cur" in
+		--*)
+			__gitcomp "--cached --dry-run --ignore-unmatch --quiet"
+			return
+			;;
+		esac
+	fi
+	# check if --cached was specified
+	if [ "$(__git_find_on_cmdline "--cached")" ]; then
+		COMPREPLY=()
+	else
+		__gitcomp "$(__git_files_having_status "(.D|DU|UA)")"
+	fi
 }

 _git_shortlog ()
--
1.7.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] bash-completion: Make use of git status
  2011-08-16  6:09 [PATCH] bash-completion: Make use of git status Ron Panduwana
@ 2011-08-19  9:54 ` Ron Panduwana
  2011-08-19 10:10 ` Thomas Rast
  1 sibling, 0 replies; 4+ messages in thread
From: Ron Panduwana @ 2011-08-19  9:54 UTC (permalink / raw)
  To: git; +Cc: Shawn O. Pearce, Junio C Hamano, Lee Marlow, Ron Panduwana

Sorry,

Bounced for Junio, I put his old address. Fixed now.

Ron Panduwana
--
panduwana@gmail.com


On Tue, Aug 16, 2011 at 1:09 PM, Ron Panduwana <panduwana@gmail.com> wrote:
> When autocompleting git add, rm, checkout --, and reset HEAD; Make it so they use git status to offer suitable files.
>
> Signed-off-by: Ron Panduwana <panduwana@gmail.com>
> ---
> Hello,
>
> I made some little improvements to the bash autocompletion script for git add, rm, checkout --, and reset HEAD.
> It's because sometimes after doing "git status" I need to do one/some of the commands and hope the autocompletion can make use the status info git already knows.
> For example:
>
> $ git status
> # On branch master
> # Changes to be committed:
> #   (use "git reset HEAD <file>..." to unstage)
> #
> #       new file:   path/to/deeply/located/added-file
> #       modified:   path/to/deeply/located/old-file
> #       deleted:    path/to/deeply/located/removed-file
> #
> # Changed but not updated:
> #   (use "git add/rm <file>..." to update what will be committed)
> #   (use "git checkout -- <file>..." to discard changes in working directory)
> #
> #       deleted:    path/to/deeply/located/deleted-file
> #       modified:   path/to/deeply/located/modified-file
> #       deleted:    path/to/deeply/located/old-file
> #
> # Untracked files:
> #   (use "git add <file>..." to include in what will be committed)
> #
> #       path/to/deeply/located/new-file
>
>
> If I need to do one of the commands, I'd like my autocompletion to work like these:
>
> $ git add [TAB]path/to/deeply/located/[TAB][TAB]
> path/to/deeply/located/modified-file   path/to/deeply/located/new-file
>
> $ git rm [TAB]path/to/deeply/located/[TAB][TAB]
> path/to/deeply/located/deleted-file   path/to/deeply/located/old-file
>
> $ git checkout -- [TAB]path/to/deeply/located/[TAB][TAB]
> path/to/deeply/located/deleted-file    path/to/deeply/located/modified-file   path/to/deeply/located/old-file
>
> $ git reset HEAD [TAB]path/to/deeply/located/[TAB][TAB]
> path/to/deeply/located/added-file     path/to/deeply/located/old-file       path/to/deeply/located/removed-file
>
>
> I think it's a good thing to be included as default, so here I send the diff of my modification of the autocompletion script.
>
> PS: for the modification I've taken into account:
> - has_doubledash
> - when we meant to autocomplete the --switches
> - for git rm, whether --cached presents
> - for git checkout, only if it starts with "git checkout -- "
> - for git reset, only if it starts with "git reset HEAD "
> - (ie. when we meant to autocomplete git_refs)
>
> Hope it'd be useful.
>
>  contrib/completion/git-completion.bash |   76 ++++++++++++++++++++------------
>  1 files changed, 48 insertions(+), 28 deletions(-)
>
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 5a83090..233bdbb 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -1010,6 +1010,12 @@ __git_has_doubledash ()
>        return 1
>  }
>
> +# __git_files_having_status requires 1 argument
> +__git_files_having_status ()
> +{
> +       echo "$(git status -uall --porcelain . 2>/dev/null | egrep "^$1" | cut -c4-)"
> +}
> +
>  __git_whitespacelist="nowarn warn error error-all fix"
>
>  _git_am ()
> @@ -1058,17 +1064,17 @@ _git_apply ()
>
>  _git_add ()
>  {
> -       __git_has_doubledash && return
> -
> -       case "$cur" in
> -       --*)
> -               __gitcomp "
> -                       --interactive --refresh --patch --update --dry-run
> -                       --ignore-errors --intent-to-add
> -                       "
> -               return
> -       esac
> -       COMPREPLY=()
> +       if ! __git_has_doubledash; then
> +               case "$cur" in
> +               --*)
> +                       __gitcomp "
> +                               --interactive --refresh --patch --update --dry-run
> +                               --ignore-errors --intent-to-add
> +                               "
> +                       return
> +               esac
> +       fi
> +       __gitcomp "$(__git_files_having_status "(.[MAU]|UD|\?\?)")"
>  }
>
>  _git_archive ()
> @@ -1171,7 +1177,12 @@ _git_bundle ()
>
>  _git_checkout ()
>  {
> -       __git_has_doubledash && return
> +       if __git_has_doubledash; then
> +               if [[ ${words[2]} = "--" ]]; then
> +                       __gitcomp "$(__git_files_having_status ".[MD]")"
> +               fi
> +               return
> +       fi
>
>        case "$cur" in
>        --conflict=*)
> @@ -2313,14 +2324,18 @@ _git_replace ()
>
>  _git_reset ()
>  {
> -       __git_has_doubledash && return
> -
> -       case "$cur" in
> -       --*)
> -               __gitcomp "--merge --mixed --hard --soft --patch"
> +       if ! __git_has_doubledash; then
> +               case "$cur" in
> +               --*)
> +                       __gitcomp "--merge --mixed --hard --soft --patch"
> +                       return
> +                       ;;
> +               esac
> +       fi
> +       if [[ ${words[2]} = "HEAD" ]]; then
> +               __gitcomp "$(__git_files_having_status "[ADM].")"
>                return
> -               ;;
> -       esac
> +       fi
>        __gitcomp "$(__git_refs)"
>  }
>
> @@ -2337,15 +2352,20 @@ _git_revert ()
>
>  _git_rm ()
>  {
> -       __git_has_doubledash && return
> -
> -       case "$cur" in
> -       --*)
> -               __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
> -               return
> -               ;;
> -       esac
> -       COMPREPLY=()
> +       if ! __git_has_doubledash; then
> +               case "$cur" in
> +               --*)
> +                       __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
> +                       return
> +                       ;;
> +               esac
> +       fi
> +       # check if --cached was specified
> +       if [ "$(__git_find_on_cmdline "--cached")" ]; then
> +               COMPREPLY=()
> +       else
> +               __gitcomp "$(__git_files_having_status "(.D|DU|UA)")"
> +       fi
>  }
>
>  _git_shortlog ()
> --
> 1.7.1
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] bash-completion: Make use of git status
  2011-08-16  6:09 [PATCH] bash-completion: Make use of git status Ron Panduwana
  2011-08-19  9:54 ` Ron Panduwana
@ 2011-08-19 10:10 ` Thomas Rast
  2011-08-20 12:46   ` Ron Panduwana
  1 sibling, 1 reply; 4+ messages in thread
From: Thomas Rast @ 2011-08-19 10:10 UTC (permalink / raw)
  To: Ron Panduwana; +Cc: git, Shawn O. Pearce, Junio C Hamano, Lee Marlow

Ron Panduwana wrote:
> $ git add [TAB]path/to/deeply/located/[TAB][TAB]
> path/to/deeply/located/modified-file   path/to/deeply/located/new-file
[...]
> +# __git_files_having_status requires 1 argument
> +__git_files_having_status ()
> +{
> +	echo "$(git status -uall --porcelain . 2>/dev/null | egrep "^$1" | cut -c4-)"
> +}
> +

Some thoughts:

* running git-status for . has some issues: it doesn't work in the
  case of

    cd subdir
    git add ../some/file[TAB]

  It's also inefficient if you are at the top level and

    git add path/to/file/a/few/levels/down[TAB]

  since it wouldn't actually have to look for untracked files in the
  entire repo.

* -uall is not required unless you are looking for untracked files.
   For the other commands you could speed up completion by passing
   -uno instead.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] bash-completion: Make use of git status
  2011-08-19 10:10 ` Thomas Rast
@ 2011-08-20 12:46   ` Ron Panduwana
  0 siblings, 0 replies; 4+ messages in thread
From: Ron Panduwana @ 2011-08-20 12:46 UTC (permalink / raw)
  To: Thomas Rast; +Cc: git, Shawn O. Pearce, Junio C Hamano, Lee Marlow

On Fri, Aug 19, 2011 at 5:10 PM, Thomas Rast <trast@student.ethz.ch> wrote:
> Some thoughts:
>
> * running git-status for . has some issues: it doesn't work in the
>  case of
>
>    cd subdir
>    git add ../some/file[TAB]
>
>  It's also inefficient if you are at the top level and
>
>    git add path/to/file/a/few/levels/down[TAB]
>
>  since it wouldn't actually have to look for untracked files in the
>  entire repo.

OK I'll change it to git-status $cur*


> * -uall is not required unless you are looking for untracked files.
>   For the other commands you could speed up completion by passing
>   -uno instead.

Will add second param to __git_files_having_status


Ron Panduwana
--
panduwana@gmail.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-08-20 12:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-16  6:09 [PATCH] bash-completion: Make use of git status Ron Panduwana
2011-08-19  9:54 ` Ron Panduwana
2011-08-19 10:10 ` Thomas Rast
2011-08-20 12:46   ` Ron Panduwana

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.