From: Ron Panduwana <panduwana@gmail.com>
To: git@vger.kernel.org
Cc: "Shawn O. Pearce" <spearce@spearce.org>,
Junio C Hamano <junkio@cox.net>,
Lee Marlow <lee.marlow@gmail.com>,
Ron Panduwana <panduwana@gmail.com>
Subject: [PATCH] bash-completion: Make use of git status
Date: Tue, 16 Aug 2011 13:09:08 +0700 [thread overview]
Message-ID: <1313474948-13531-1-git-send-email-panduwana@gmail.com> (raw)
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
next reply other threads:[~2011-08-16 6:09 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-08-16 6:09 Ron Panduwana [this message]
2011-08-19 9:54 ` [PATCH] bash-completion: Make use of git status Ron Panduwana
2011-08-19 10:10 ` Thomas Rast
2011-08-20 12:46 ` Ron Panduwana
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1313474948-13531-1-git-send-email-panduwana@gmail.com \
--to=panduwana@gmail.com \
--cc=git@vger.kernel.org \
--cc=junkio@cox.net \
--cc=lee.marlow@gmail.com \
--cc=spearce@spearce.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).