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

Re: [PATCH] bash-completion: Make use of git status

From
Ron Panduwana <panduwana@gmail.com>
Date
Aug 19, 2011, 09:54 UTC
Message-ID
<CAOSnc=g4qYH0m5DKtuN984EOuoFVYhQN26-gOVa+c9O6SMuPcA@mail.gmail.com>
In-Reply-To
<1313474948-13531-1-git-send-email-panduwana@gmail.com>
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:
Show 183 quoted lines
> 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
>
>
Previous: Ron PanduwanaNext: Thomas Rast
Message 2 of 4 in “bash-completion: Make use of git status”
  1. bash-completion: Make use of git statusRon Panduwana, Aug 16, 2011
  2. Ron PanduwanaAug 19, 2011
  3. Thomas RastAug 19, 2011
  4. Ron PanduwanaAug 20, 2011

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.