Skip to content

Commit 6880779

Browse files
jacob-kellergitster
authored andcommitted
completion: perform DWIM logic directly in __git_complete_refs
__git_complete_refs is the main function used for completing references. It is primarily used as a wrapper around __git_refs, and is easier to extend since its arguments are option-like. One major downside of __git_complete_refs and __git_refs currently, is the lack of ability to complete only a subset of refs such as branches (refs/heads) or tags (refs/tags). Normally, a caller might just decide to use __git_heads() or __git_tags(). However, in the case of git-switch, it is useful to complete both branches *and* DWIM remote branch names. Due to the complexity and implementation of __git_refs, it is not easy to extend it to support listing only a subset of references. Instead, we can extend __git_complete_refs to do this. For this to be done, we must first ensure that "--dwim" support is not tied to calling __git_refs. Instead of passing $dwim into __git_refs, we can implement a __gitcomp_direct_append function which can append to COMPREPLY after a call to __gitcomp_direct. If --dwim is passed to __git_complete_refs, use __gitcomp_direct_append to add the output of __git_dwim_remote_heads to the completion list. In this way, --dwim support is now independent of calling __git_refs. A future change will add an additional option to control what set of references __git_complete_refs will output. Signed-off-by: Jacob Keller <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 58a2ca3 commit 6880779

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

contrib/completion/git-completion.bash

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,19 @@ __gitcomp_direct ()
301301
COMPREPLY=($1)
302302
}
303303

304+
# Similar to __gitcomp_direct, but appends to COMPREPLY instead.
305+
# Callers must take care of providing only words that match the current word
306+
# to be completed and adding any prefix and/or suffix (trailing space!), if
307+
# necessary.
308+
# 1: List of newline-separated matching completion words, complete with
309+
# prefix and suffix.
310+
__gitcomp_direct_append ()
311+
{
312+
local IFS=$'\n'
313+
314+
COMPREPLY+=($1)
315+
}
316+
304317
__gitcompappend ()
305318
{
306319
local x i=${#COMPREPLY[@]}
@@ -787,7 +800,11 @@ __git_complete_refs ()
787800
shift
788801
done
789802

790-
__gitcomp_direct "$(__git_refs "$remote" "$dwim" "$pfx" "$cur_" "$sfx")"
803+
__gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")"
804+
805+
if [ "$dwim" = "yes" ]; then
806+
__gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
807+
fi
791808
}
792809

793810
# __git_refs2 requires 1 argument (to pass to __git_refs)

0 commit comments

Comments
 (0)