{"thread":{"id":"28110","subject":"[PATCH] bash-completion: Make use of git status","startedAt":"2011-08-16T06:09:08Z","lastAt":"2011-08-20T12:46:14Z","messageCount":4,"participants":["Ron Panduwana","Thomas Rast"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"173572","messageId":"1313474948-13531-1-git-send-email-panduwana@gmail.com","threadId":"28110","inReplyTo":null,"subject":"[PATCH] bash-completion: Make use of git status","fromName":"Ron Panduwana","fromEmail":"panduwana@gmail.com","sentAt":"2011-08-16T06:09:08Z","receivedAt":"2011-08-16T06:09:08Z","isPatch":true,"sender":{"key":"panduwana@gmail.com","avatar":"https://gravatar.com/avatar/5fbac94c48ebedfb6415332993323837eeff8420afe2ee9af57f36002801e75e?d=mp&s=160"},"body":"When autocompleting git add, rm, checkout --, and reset HEAD; Make it so they use git status to offer suitable files.\n\nSigned-off-by: Ron Panduwana <panduwana@gmail.com>\n---\nHello,\n\nI made some little improvements to the bash autocompletion script for git add, rm, checkout --, and reset HEAD.\nIt'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.\nFor example:\n\n$ git status\n# On branch master\n# Changes to be committed:\n#   (use \"git reset HEAD <file>...\" to unstage)\n#\n#\tnew file:   path/to/deeply/located/added-file\n#\tmodified:   path/to/deeply/located/old-file\n#\tdeleted:    path/to/deeply/located/removed-file\n#\n# Changed but not updated:\n#   (use \"git add/rm <file>...\" to update what will be committed)\n#   (use \"git checkout -- <file>...\" to discard changes in working directory)\n#\n#\tdeleted:    path/to/deeply/located/deleted-file\n#\tmodified:   path/to/deeply/located/modified-file\n#\tdeleted:    path/to/deeply/located/old-file\n#\n# Untracked files:\n#   (use \"git add <file>...\" to include in what will be committed)\n#\n#\tpath/to/deeply/located/new-file\n\n\nIf I need to do one of the commands, I'd like my autocompletion to work like these:\n\n$ git add [TAB]path/to/deeply/located/[TAB][TAB]\npath/to/deeply/located/modified-file   path/to/deeply/located/new-file\n\n$ git rm [TAB]path/to/deeply/located/[TAB][TAB]\npath/to/deeply/located/deleted-file   path/to/deeply/located/old-file\n\n$ git checkout -- [TAB]path/to/deeply/located/[TAB][TAB]\npath/to/deeply/located/deleted-file    path/to/deeply/located/modified-file   path/to/deeply/located/old-file\n\n$ git reset HEAD [TAB]path/to/deeply/located/[TAB][TAB]\npath/to/deeply/located/added-file     path/to/deeply/located/old-file       path/to/deeply/located/removed-file\n\n\nI think it's a good thing to be included as default, so here I send the diff of my modification of the autocompletion script.\n\nPS: for the modification I've taken into account:\n- has_doubledash\n- when we meant to autocomplete the --switches\n- for git rm, whether --cached presents\n- for git checkout, only if it starts with \"git checkout -- \"\n- for git reset, only if it starts with \"git reset HEAD \"\n- (ie. when we meant to autocomplete git_refs)\n\nHope it'd be useful.\n\n contrib/completion/git-completion.bash |   76 ++++++++++++++++++++------------\n 1 files changed, 48 insertions(+), 28 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 5a83090..233bdbb 100755\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -1010,6 +1010,12 @@ __git_has_doubledash ()\n \treturn 1\n }\n\n+# __git_files_having_status requires 1 argument\n+__git_files_having_status ()\n+{\n+\techo \"$(git status -uall --porcelain . 2>/dev/null | egrep \"^$1\" | cut -c4-)\"\n+}\n+\n __git_whitespacelist=\"nowarn warn error error-all fix\"\n\n _git_am ()\n@@ -1058,17 +1064,17 @@ _git_apply ()\n\n _git_add ()\n {\n-\t__git_has_doubledash && return\n-\n-\tcase \"$cur\" in\n-\t--*)\n-\t\t__gitcomp \"\n-\t\t\t--interactive --refresh --patch --update --dry-run\n-\t\t\t--ignore-errors --intent-to-add\n-\t\t\t\"\n-\t\treturn\n-\tesac\n-\tCOMPREPLY=()\n+\tif ! __git_has_doubledash; then\n+\t\tcase \"$cur\" in\n+\t\t--*)\n+\t\t\t__gitcomp \"\n+\t\t\t\t--interactive --refresh --patch --update --dry-run\n+\t\t\t\t--ignore-errors --intent-to-add\n+\t\t\t\t\"\n+\t\t\treturn\n+\t\tesac\n+\tfi\n+\t__gitcomp \"$(__git_files_having_status \"(.[MAU]|UD|\\?\\?)\")\"\n }\n\n _git_archive ()\n@@ -1171,7 +1177,12 @@ _git_bundle ()\n\n _git_checkout ()\n {\n-\t__git_has_doubledash && return\n+\tif __git_has_doubledash; then\n+\t\tif [[ ${words[2]} = \"--\" ]]; then\n+\t\t\t__gitcomp \"$(__git_files_having_status \".[MD]\")\"\n+\t\tfi\n+\t\treturn\n+\tfi\n\n \tcase \"$cur\" in\n \t--conflict=*)\n@@ -2313,14 +2324,18 @@ _git_replace ()\n\n _git_reset ()\n {\n-\t__git_has_doubledash && return\n-\n-\tcase \"$cur\" in\n-\t--*)\n-\t\t__gitcomp \"--merge --mixed --hard --soft --patch\"\n+\tif ! __git_has_doubledash; then\n+\t\tcase \"$cur\" in\n+\t\t--*)\n+\t\t\t__gitcomp \"--merge --mixed --hard --soft --patch\"\n+\t\t\treturn\n+\t\t\t;;\n+\t\tesac\n+\tfi\n+\tif [[ ${words[2]} = \"HEAD\" ]]; then\n+\t\t__gitcomp \"$(__git_files_having_status \"[ADM].\")\"\n \t\treturn\n-\t\t;;\n-\tesac\n+\tfi\n \t__gitcomp \"$(__git_refs)\"\n }\n\n@@ -2337,15 +2352,20 @@ _git_revert ()\n\n _git_rm ()\n {\n-\t__git_has_doubledash && return\n-\n-\tcase \"$cur\" in\n-\t--*)\n-\t\t__gitcomp \"--cached --dry-run --ignore-unmatch --quiet\"\n-\t\treturn\n-\t\t;;\n-\tesac\n-\tCOMPREPLY=()\n+\tif ! __git_has_doubledash; then\n+\t\tcase \"$cur\" in\n+\t\t--*)\n+\t\t\t__gitcomp \"--cached --dry-run --ignore-unmatch --quiet\"\n+\t\t\treturn\n+\t\t\t;;\n+\t\tesac\n+\tfi\n+\t# check if --cached was specified\n+\tif [ \"$(__git_find_on_cmdline \"--cached\")\" ]; then\n+\t\tCOMPREPLY=()\n+\telse\n+\t\t__gitcomp \"$(__git_files_having_status \"(.D|DU|UA)\")\"\n+\tfi\n }\n\n _git_shortlog ()\n--\n1.7.1\n"},{"id":"173845","messageId":"CAOSnc=g4qYH0m5DKtuN984EOuoFVYhQN26-gOVa+c9O6SMuPcA@mail.gmail.com","threadId":"28110","inReplyTo":"1313474948-13531-1-git-send-email-panduwana@gmail.com","subject":"Re: [PATCH] bash-completion: Make use of git status","fromName":"Ron Panduwana","fromEmail":"panduwana@gmail.com","sentAt":"2011-08-19T09:54:51Z","receivedAt":"2011-08-19T09:54:51Z","isPatch":true,"sender":{"key":"panduwana@gmail.com","avatar":"https://gravatar.com/avatar/5fbac94c48ebedfb6415332993323837eeff8420afe2ee9af57f36002801e75e?d=mp&s=160"},"body":"Sorry,\n\nBounced for Junio, I put his old address. Fixed now.\n\nRon Panduwana\n--\npanduwana@gmail.com\n\n\nOn Tue, Aug 16, 2011 at 1:09 PM, Ron Panduwana <panduwana@gmail.com> wrote:\n> When autocompleting git add, rm, checkout --, and reset HEAD; Make it so they use git status to offer suitable files.\n>\n> Signed-off-by: Ron Panduwana <panduwana@gmail.com>\n> ---\n> Hello,\n>\n> I made some little improvements to the bash autocompletion script for git add, rm, checkout --, and reset HEAD.\n> 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.\n> For example:\n>\n> $ git status\n> # On branch master\n> # Changes to be committed:\n> #   (use \"git reset HEAD <file>...\" to unstage)\n> #\n> #       new file:   path/to/deeply/located/added-file\n> #       modified:   path/to/deeply/located/old-file\n> #       deleted:    path/to/deeply/located/removed-file\n> #\n> # Changed but not updated:\n> #   (use \"git add/rm <file>...\" to update what will be committed)\n> #   (use \"git checkout -- <file>...\" to discard changes in working directory)\n> #\n> #       deleted:    path/to/deeply/located/deleted-file\n> #       modified:   path/to/deeply/located/modified-file\n> #       deleted:    path/to/deeply/located/old-file\n> #\n> # Untracked files:\n> #   (use \"git add <file>...\" to include in what will be committed)\n> #\n> #       path/to/deeply/located/new-file\n>\n>\n> If I need to do one of the commands, I'd like my autocompletion to work like these:\n>\n> $ git add [TAB]path/to/deeply/located/[TAB][TAB]\n> path/to/deeply/located/modified-file   path/to/deeply/located/new-file\n>\n> $ git rm [TAB]path/to/deeply/located/[TAB][TAB]\n> path/to/deeply/located/deleted-file   path/to/deeply/located/old-file\n>\n> $ git checkout -- [TAB]path/to/deeply/located/[TAB][TAB]\n> path/to/deeply/located/deleted-file    path/to/deeply/located/modified-file   path/to/deeply/located/old-file\n>\n> $ git reset HEAD [TAB]path/to/deeply/located/[TAB][TAB]\n> path/to/deeply/located/added-file     path/to/deeply/located/old-file       path/to/deeply/located/removed-file\n>\n>\n> 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.\n>\n> PS: for the modification I've taken into account:\n> - has_doubledash\n> - when we meant to autocomplete the --switches\n> - for git rm, whether --cached presents\n> - for git checkout, only if it starts with \"git checkout -- \"\n> - for git reset, only if it starts with \"git reset HEAD \"\n> - (ie. when we meant to autocomplete git_refs)\n>\n> Hope it'd be useful.\n>\n>  contrib/completion/git-completion.bash |   76 ++++++++++++++++++++------------\n>  1 files changed, 48 insertions(+), 28 deletions(-)\n>\n> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> index 5a83090..233bdbb 100755\n> --- a/contrib/completion/git-completion.bash\n> +++ b/contrib/completion/git-completion.bash\n> @@ -1010,6 +1010,12 @@ __git_has_doubledash ()\n>        return 1\n>  }\n>\n> +# __git_files_having_status requires 1 argument\n> +__git_files_having_status ()\n> +{\n> +       echo \"$(git status -uall --porcelain . 2>/dev/null | egrep \"^$1\" | cut -c4-)\"\n> +}\n> +\n>  __git_whitespacelist=\"nowarn warn error error-all fix\"\n>\n>  _git_am ()\n> @@ -1058,17 +1064,17 @@ _git_apply ()\n>\n>  _git_add ()\n>  {\n> -       __git_has_doubledash && return\n> -\n> -       case \"$cur\" in\n> -       --*)\n> -               __gitcomp \"\n> -                       --interactive --refresh --patch --update --dry-run\n> -                       --ignore-errors --intent-to-add\n> -                       \"\n> -               return\n> -       esac\n> -       COMPREPLY=()\n> +       if ! __git_has_doubledash; then\n> +               case \"$cur\" in\n> +               --*)\n> +                       __gitcomp \"\n> +                               --interactive --refresh --patch --update --dry-run\n> +                               --ignore-errors --intent-to-add\n> +                               \"\n> +                       return\n> +               esac\n> +       fi\n> +       __gitcomp \"$(__git_files_having_status \"(.[MAU]|UD|\\?\\?)\")\"\n>  }\n>\n>  _git_archive ()\n> @@ -1171,7 +1177,12 @@ _git_bundle ()\n>\n>  _git_checkout ()\n>  {\n> -       __git_has_doubledash && return\n> +       if __git_has_doubledash; then\n> +               if [[ ${words[2]} = \"--\" ]]; then\n> +                       __gitcomp \"$(__git_files_having_status \".[MD]\")\"\n> +               fi\n> +               return\n> +       fi\n>\n>        case \"$cur\" in\n>        --conflict=*)\n> @@ -2313,14 +2324,18 @@ _git_replace ()\n>\n>  _git_reset ()\n>  {\n> -       __git_has_doubledash && return\n> -\n> -       case \"$cur\" in\n> -       --*)\n> -               __gitcomp \"--merge --mixed --hard --soft --patch\"\n> +       if ! __git_has_doubledash; then\n> +               case \"$cur\" in\n> +               --*)\n> +                       __gitcomp \"--merge --mixed --hard --soft --patch\"\n> +                       return\n> +                       ;;\n> +               esac\n> +       fi\n> +       if [[ ${words[2]} = \"HEAD\" ]]; then\n> +               __gitcomp \"$(__git_files_having_status \"[ADM].\")\"\n>                return\n> -               ;;\n> -       esac\n> +       fi\n>        __gitcomp \"$(__git_refs)\"\n>  }\n>\n> @@ -2337,15 +2352,20 @@ _git_revert ()\n>\n>  _git_rm ()\n>  {\n> -       __git_has_doubledash && return\n> -\n> -       case \"$cur\" in\n> -       --*)\n> -               __gitcomp \"--cached --dry-run --ignore-unmatch --quiet\"\n> -               return\n> -               ;;\n> -       esac\n> -       COMPREPLY=()\n> +       if ! __git_has_doubledash; then\n> +               case \"$cur\" in\n> +               --*)\n> +                       __gitcomp \"--cached --dry-run --ignore-unmatch --quiet\"\n> +                       return\n> +                       ;;\n> +               esac\n> +       fi\n> +       # check if --cached was specified\n> +       if [ \"$(__git_find_on_cmdline \"--cached\")\" ]; then\n> +               COMPREPLY=()\n> +       else\n> +               __gitcomp \"$(__git_files_having_status \"(.D|DU|UA)\")\"\n> +       fi\n>  }\n>\n>  _git_shortlog ()\n> --\n> 1.7.1\n>\n>\n"},{"id":"173846","messageId":"201108191210.59538.trast@student.ethz.ch","threadId":"28110","inReplyTo":"1313474948-13531-1-git-send-email-panduwana@gmail.com","subject":"Re: [PATCH] bash-completion: Make use of git status","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2011-08-19T10:10:59Z","receivedAt":"2011-08-19T10:10:59Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"Ron Panduwana wrote:\n> $ git add [TAB]path/to/deeply/located/[TAB][TAB]\n> path/to/deeply/located/modified-file   path/to/deeply/located/new-file\n[...]\n> +# __git_files_having_status requires 1 argument\n> +__git_files_having_status ()\n> +{\n> +\techo \"$(git status -uall --porcelain . 2>/dev/null | egrep \"^$1\" | cut -c4-)\"\n> +}\n> +\n\nSome thoughts:\n\n* running git-status for . has some issues: it doesn't work in the\n  case of\n\n    cd subdir\n    git add ../some/file[TAB]\n\n  It's also inefficient if you are at the top level and\n\n    git add path/to/file/a/few/levels/down[TAB]\n\n  since it wouldn't actually have to look for untracked files in the\n  entire repo.\n\n* -uall is not required unless you are looking for untracked files.\n   For the other commands you could speed up completion by passing\n   -uno instead.\n\n-- \nThomas Rast\ntrast@{inf,student}.ethz.ch\n"},{"id":"173916","messageId":"CAOSnc=hzehOJ9RCs2huZ4b99ZG4Mgok-MfSNXN7OzFq1K9gYrQ@mail.gmail.com","threadId":"28110","inReplyTo":"201108191210.59538.trast@student.ethz.ch","subject":"Re: [PATCH] bash-completion: Make use of git status","fromName":"Ron Panduwana","fromEmail":"panduwana@gmail.com","sentAt":"2011-08-20T12:46:14Z","receivedAt":"2011-08-20T12:46:14Z","isPatch":true,"sender":{"key":"panduwana@gmail.com","avatar":"https://gravatar.com/avatar/5fbac94c48ebedfb6415332993323837eeff8420afe2ee9af57f36002801e75e?d=mp&s=160"},"body":"On Fri, Aug 19, 2011 at 5:10 PM, Thomas Rast <trast@student.ethz.ch> wrote:\n> Some thoughts:\n>\n> * running git-status for . has some issues: it doesn't work in the\n>  case of\n>\n>    cd subdir\n>    git add ../some/file[TAB]\n>\n>  It's also inefficient if you are at the top level and\n>\n>    git add path/to/file/a/few/levels/down[TAB]\n>\n>  since it wouldn't actually have to look for untracked files in the\n>  entire repo.\n\nOK I'll change it to git-status $cur*\n\n\n> * -uall is not required unless you are looking for untracked files.\n>   For the other commands you could speed up completion by passing\n>   -uno instead.\n\nWill add second param to __git_files_having_status\n\n\nRon Panduwana\n--\npanduwana@gmail.com\n"}]}