completion: Support the DWIM mode for git checkout
PATCH 1/1 completion: Support the DWIM mode for git checkout
--- contrib/completion/git-completion.bash | 35 +++++++++++++++++++++++++++++-- 1 files changed, 32 insertions(+), 3 deletions(-)
contrib/completion/git-completion.bash +32 -3
@@ -380,16 +380,19 @@ __git_tags () done } -# __git_refs accepts 0 or 1 arguments (to pass to __gitdir) +# __git_refs accepts 0, 1 (to pass to __gitdir), or 2 arguments +# presence of 2nd argument means use the guess heuristic employed +# by checkout for tracking branches __git_refs () { - local i is_hash=y dir="$(__gitdir "${1-}")" + local i is_hash=y dir="$(__gitdir "${1-}")" track="${2-}" local cur="${COMP_WORDS[COMP_CWORD]}" format refs if [ -d "$dir" ]; then case "$cur" in refs|refs/*) format="refname" refs="${cur%/*}" + track="" ;; *) for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
@@ -397,10 +400,26 @@ __git_refs () done format="refname:short" refs="refs/tags refs/heads refs/remotes" + if [ -z "$cur" ]; then track=""; fi ;; esac git --git-dir="$dir" for-each-ref --format="%($format)" \ $refs + if [ -n "$track" ]; then + # employ the heuristic used by git checkout + # Try to find a remote branch that matches the completion word + # but only output if the branch name is unique + local ref entry + git --git-dir="$dir" for-each-ref --shell --format="ref=%(refname:short)" \ + "refs/remotes/" | \ + while read entry; do + eval "$entry" + ref="${ref#*/}" + if [[ "$ref" == "$cur"* ]]; then + echo "$ref" + fi + done | uniq -u + fi return fi for i in $(git ls-remote "$dir" 2>/dev/null); do
@@ -988,7 +1007,17 @@ _git_checkout () " ;; *) - __gitcomp "$(__git_refs)" + # check if --track, --no-track, or --no-guess was specified + # if so, disable DWIM mode + local i c=1 track=1 + while [ $c -lt $COMP_CWORD ]; do + i="${COMP_WORDS[c]}" + case "$i" in + --track|--no-track|--no-guess) track=''; break ;; + esac + c=$((++c)) + done + __gitcomp "$(__git_refs '' $track)" ;; esac }
Signature · 1 line
1.7.3.1.184.g5b1fd
Hi,
Quoted 2 lines · Kevin Ballard
Signed-off-by Kevin Ballard <[email protected]>---
I think the commit message should add some details about how the patch changes the completion script's behavior. At least I didn't know offhand what "DWIM mode for git checkout" is, and once I found it (70c9ac2 (DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz", 2009-10-18), right?), I didn't know how the completion script is supposed to support that, and once I applied the patch and played around a bit, I was surprised that only 'git checkout h<tab>' includes the 'html' branch but 'git checkout <tab>' don't.
Quoted 15 lines
contrib/completion/git-completion.bash | 35 +++++++++++++++++++++++++++++-- 1 files changed, 32 insertions(+), 3 deletions(-)contrib/completion/git-completion.bash +1
-1@@ -988,7 +1007,17 @@ _git_checkout () " ;; *) - __gitcomp "$(__git_refs)" + # check if --track, --no-track, or --no-guess was specified
contrib/completion/git-completion.bash _git_checkout () · line 1020
Quoted 10 lines
contrib/completion/git-completion.bash +10
-0+ # if so, disable DWIM mode + local i c=1 track=1 + while [ $c -lt $COMP_CWORD ]; do + i="${COMP_WORDS[c]}" + case "$i" in + --track|--no-track|--no-guess) track=''; break ;; + esac + c=$((++c)) + done + __gitcomp "$(__git_refs '' $track)"
You could use the __git_find_on_cmdline() helper function instead.
Best,
Gábor
Quoted 7 lines · SZEDER Gábor
Hi,
Quoted 2 lines · Kevin Ballard
Signed-off-by Kevin Ballard <[email protected]>---
Quoted 8 lines
I think the commit message should add some details about how the patch changes the completion script's behavior. At least I didn't know offhand what "DWIM mode for git checkout" is, and once I found it (70c9ac2 (DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz", 2009-10-18), right?), I didn't know how the completion script is supposed to support that, and once I applied the patch and played around a bit, I was surprised that only 'git checkout h<tab>' includes the 'html' branch but 'git checkout <tab>' don't.
Good point. I'll write a real commit message for the next version. And another good point about 'git checkout <tab>'. I don't remember why, but for some reason I explicitly avoided DWIM mode if the current word was empty. But you're right, there's no good reason to do that.
Quoted 19 lines
Quoted 19 lines
contrib/completion/git-completion.bash | 35 +++++++++++++++++++++++++++++--
1 files changed, 32 insertions(+), 3 deletions(-)contrib/completion/git-completion.bash +5
-1@@ -988,7 +1007,17 @@ _git_checkout () " ;; *) - __gitcomp "$(__git_refs)" + # check if --track, --no-track, or --no-guess was specified + # if so, disable DWIM mode + local i c=1 track=1 + while [ $c -lt $COMP_CWORD ]; do + i="${COMP_WORDS[c]}"
Quoted 8 lines
contrib/completion/git-completion.bash _git_checkout () · line 1020
Quoted 6 lines
contrib/completion/git-completion.bash +6
-0+ case "$i" in + --track|--no-track|--no-guess) track=''; break ;; + esac + c=$((++c)) + done + __gitcomp "$(__git_refs '' $track)"You could use the
__git_find_on_cmdline()helper function instead.
I'll look into that, thanks.
-Kevin Ballard
PATCH v2 1/1 completion: Support the DWIM mode for git checkout
Ever since commit 70c9ac2 (DWIM: "git checkout frotz" to "git checkout -b frotz origin/frotz"), git checkout has supported a DWIM mode where it creates a local tracking branch for a remote branch if just the name of the remote branch is specified on the command-line and only one remote has a branch with that name. Teach the bash completion script to understand this DWIM mode and provide such remote-tracking branch names as possible completions.
--- contrib/completion/git-completion.bash | 30 +++++++++++++++++++++++++++--- 1 files changed, 27 insertions(+), 3 deletions(-)
contrib/completion/git-completion.bash +27 -3
@@ -386,16 +386,19 @@ __git_tags () done } -# __git_refs accepts 0 or 1 arguments (to pass to __gitdir) +# __git_refs accepts 0, 1 (to pass to __gitdir), or 2 arguments +# presence of 2nd argument means use the guess heuristic employed +# by checkout for tracking branches __git_refs () { - local i is_hash=y dir="$(__gitdir "${1-}")" + local i is_hash=y dir="$(__gitdir "${1-}")" track="${2-}" local cur="${COMP_WORDS[COMP_CWORD]}" format refs if [ -d "$dir" ]; then case "$cur" in refs|refs/*) format="refname" refs="${cur%/*}" + track="" ;; *) for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
@@ -407,6 +410,21 @@ __git_refs () esac git --git-dir="$dir" for-each-ref --format="%($format)" \ $refs + if [ -n "$track" ]; then + # employ the heuristic used by git checkout + # Try to find a remote branch that matches the completion word + # but only output if the branch name is unique + local ref entry + git --git-dir="$dir" for-each-ref --shell --format="ref=%(refname:short)" \ + "refs/remotes/" | \ + while read entry; do + eval "$entry" + ref="${ref#*/}" + if [[ "$ref" == "$cur"* ]]; then + echo "$ref" + fi + done | uniq -u + fi return fi for i in $(git ls-remote "$dir" 2>/dev/null); do
@@ -994,7 +1012,13 @@ _git_checkout () " ;; *) - __gitcomp "$(__git_refs)" + # check if --track, --no-track, or --no-guess was specified + # if so, disable DWIM mode + local flags="--track --no-track --no-guess" track=1 + if [ -n "$(__git_find_on_cmdline "$flags")" ]; then + track='' + fi + __gitcomp "$(__git_refs '' $track)" ;; esac }
Signature · 1 line
1.7.3.1.185.gd09c4