@matheusbsbs / matheus-bagliano / commits / 58a2ca37a1
Fork 0

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>

Jacob Keller committed May 28, 2020 at 11:10 UTC 58a2ca37a1eba901ba14f1991108b1c8e85dfd22
1 file changed +21 -7
contrib/completion/git-completion.bash
+21 -7
@@ -621,6 +621,26 @@ __git_tags ()
621 "refs/tags/$cur_*" "refs/tags/$cur_*/**"
622 }
623
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 +
644 # Lists refs from the local (by default) or from a remote repository.
645 # It accepts 0, 1 or 2 arguments:
646 # 1: The remote to list refs from (optional; ignored, if set but empty).
@@ -696,13 +716,7 @@ __git_refs ()
716 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
717 "${refs[@]}"
718 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"
720 fi
721 return
722 fi