Skip to content

Commit 58a2ca3

Browse files
jacob-kellergitster
authored andcommitted
completion: extract function __git_dwim_remote_heads
__git_refs() has the ability to report unique remote names for supporting completion of remote branch names for the DWIMery of git checkout and git switch. For git checkout, this is fine, because it always supports completing all local references. However, git switch by default only supports either switching branches or using this DWIMery to create a local branch tracking the remote branch. Future work to cleanup and improve completion support for git switch will be aided if the remote branch names can be completed separately from __git_refs. Extract this logic to a function __git_dwim_remote_heads(), and use it in __git_refs. Signed-off-by: Jacob Keller <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 0408c6b commit 58a2ca3

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

contrib/completion/git-completion.bash

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,26 @@ __git_tags ()
621621
"refs/tags/$cur_*" "refs/tags/$cur_*/**"
622622
}
623623

624+
# List unique branches from refs/remotes used for 'git checkout' and 'git
625+
# switch' tracking DWIMery.
626+
# 1: A prefix to be added to each listed branch (optional)
627+
# 2: List only branches matching this word (optional; list all branches if
628+
# unset or empty).
629+
# 3: A suffix to be appended to each listed branch (optional).
630+
__git_dwim_remote_heads ()
631+
{
632+
local pfx="${1-}" cur_="${2-}" sfx="${3-}"
633+
local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
634+
635+
# employ the heuristic used by git checkout and git switch
636+
# Try to find a remote branch that cur_es the completion word
637+
# but only output if the branch name is unique
638+
__git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
639+
--sort="refname:strip=3" \
640+
"refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
641+
uniq -u
642+
}
643+
624644
# Lists refs from the local (by default) or from a remote repository.
625645
# It accepts 0, 1 or 2 arguments:
626646
# 1: The remote to list refs from (optional; ignored, if set but empty).
@@ -696,13 +716,7 @@ __git_refs ()
696716
__git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
697717
"${refs[@]}"
698718
if [ -n "$track" ]; then
699-
# employ the heuristic used by git checkout
700-
# Try to find a remote branch that matches the completion word
701-
# but only output if the branch name is unique
702-
__git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
703-
--sort="refname:strip=3" \
704-
"refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
705-
uniq -u
719+
__git_dwim_remote_heads "$pfx" "$match" "$sfx"
706720
fi
707721
return
708722
fi

0 commit comments

Comments
 (0)