Skip to content

Commit 2c34e4e

Browse files
committed
Merge branch 'rh/complete-symbolic-ref'
Command line completion script (in contrib/) learned to complete "git symbolic-ref" a bit better (you need to enable plumbing commands to be completed with GIT_COMPLETION_SHOW_ALL_COMMANDS). * rh/complete-symbolic-ref: completion: add docs on how to add subcommand completions completion: improve docs for using __git_complete completion: add 'symbolic-ref'
2 parents f526a4f + 6b7c45e commit 2c34e4e

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

contrib/completion/git-completion.bash

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,29 @@
3131
# Note that "git" is optional --- '!f() { : commit; ...}; f' would complete
3232
# just like the 'git commit' command.
3333
#
34-
# If you have a command that is not part of git, but you would still
35-
# like completion, you can use __git_complete:
34+
# To add completion for git subcommands that are implemented in external
35+
# scripts, define a function of the form '_git_${subcommand}' while replacing
36+
# all dashes with underscores, and the main git completion will make use of it.
37+
# For example, to add completion for 'git do-stuff' (which could e.g. live
38+
# in /usr/bin/git-do-stuff), name the completion function '_git_do_stuff'.
39+
# See _git_show, _git_bisect etc. below for more examples.
40+
#
41+
# If you have a shell command that is not part of git (and is not called as a
42+
# git subcommand), but you would still like git-style completion for it, use
43+
# __git_complete. For example, to use the same completion as for 'git log' also
44+
# for the 'gl' command:
3645
#
3746
# __git_complete gl git_log
3847
#
39-
# Or if it's a main command (i.e. git or gitk):
48+
# Or if the 'gk' command should be completed the same as 'gitk':
4049
#
4150
# __git_complete gk gitk
4251
#
52+
# The second parameter of __git_complete gives the completion function; it is
53+
# resolved as a function named "$2", or "__$2_main", or "_$2" in that order.
54+
# In the examples above, the actual functions used for completion will be
55+
# _git_log and __gitk_main.
56+
#
4357
# Compatible with bash 3.2.57.
4458
#
4559
# You can set the following environment variables to influence the behavior of
@@ -3581,6 +3595,17 @@ _git_svn ()
35813595
fi
35823596
}
35833597

3598+
_git_symbolic_ref () {
3599+
case "$cur" in
3600+
--*)
3601+
__gitcomp_builtin symbolic-ref
3602+
return
3603+
;;
3604+
esac
3605+
3606+
__git_complete_refs
3607+
}
3608+
35843609
_git_tag ()
35853610
{
35863611
local i c="$__git_cmd_idx" f=0

t/t9902-completion.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,6 +2518,29 @@ test_expect_success 'complete tree filename with metacharacters' '
25182518
EOF
25192519
'
25202520

2521+
test_expect_success 'symbolic-ref completes builtin options' '
2522+
test_completion "git symbolic-ref --d" <<-\EOF
2523+
--delete Z
2524+
EOF
2525+
'
2526+
2527+
test_expect_success 'symbolic-ref completes short ref names' '
2528+
test_completion "git symbolic-ref foo m" <<-\EOF
2529+
main Z
2530+
mybranch Z
2531+
mytag Z
2532+
EOF
2533+
'
2534+
2535+
test_expect_success 'symbolic-ref completes full ref names' '
2536+
test_completion "git symbolic-ref foo refs/" <<-\EOF
2537+
refs/heads/main Z
2538+
refs/heads/mybranch Z
2539+
refs/tags/mytag Z
2540+
refs/tags/A Z
2541+
EOF
2542+
'
2543+
25212544
test_expect_success PERL 'send-email' '
25222545
test_completion "git send-email --cov" <<-\EOF &&
25232546
--cover-from-description=Z

0 commit comments

Comments
 (0)