Skip to content

Commit 935d937

Browse files
committed
Merge branch 'sg/completion-config'
Code clean-up for completion script (in contrib/). * sg/completion-config: completion: simplify query for config variables completion: add a helper function to get config variables
2 parents faa4b2e + 12bdc88 commit 935d937

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

contrib/completion/git-completion.bash

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -739,30 +739,25 @@ __git_compute_porcelain_commands ()
739739
__git_porcelain_commands=$(__git_list_porcelain_commands)
740740
}
741741

742+
# Lists all set config variables starting with the given section prefix,
743+
# with the prefix removed.
744+
__git_get_config_variables ()
745+
{
746+
local section="$1" i IFS=$'\n'
747+
for i in $(git --git-dir="$(__gitdir)" config --get-regexp "^$section\..*" 2>/dev/null); do
748+
i="${i#$section.}"
749+
echo "${i/ */}"
750+
done
751+
}
752+
742753
__git_pretty_aliases ()
743754
{
744-
local i IFS=$'\n'
745-
for i in $(git --git-dir="$(__gitdir)" config --get-regexp "pretty\..*" 2>/dev/null); do
746-
case "$i" in
747-
pretty.*)
748-
i="${i#pretty.}"
749-
echo "${i/ */}"
750-
;;
751-
esac
752-
done
755+
__git_get_config_variables "pretty"
753756
}
754757

755758
__git_aliases ()
756759
{
757-
local i IFS=$'\n'
758-
for i in $(git --git-dir="$(__gitdir)" config --get-regexp "alias\..*" 2>/dev/null); do
759-
case "$i" in
760-
alias.*)
761-
i="${i#alias.}"
762-
echo "${i/ */}"
763-
;;
764-
esac
765-
done
760+
__git_get_config_variables "alias"
766761
}
767762

768763
# __git_aliased_command requires 1 argument
@@ -2260,12 +2255,7 @@ _git_remote ()
22602255
__git_complete_remote_or_refspec
22612256
;;
22622257
update)
2263-
local i c='' IFS=$'\n'
2264-
for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
2265-
i="${i#remotes.}"
2266-
c="$c ${i/ */}"
2267-
done
2268-
__gitcomp "$c"
2258+
__gitcomp "$(__git_get_config_variables "remotes")"
22692259
;;
22702260
*)
22712261
;;

t/t9902-completion.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,40 @@ test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from
370370
test_cmp expect actual
371371
'
372372

373+
test_expect_success '__git_get_config_variables' '
374+
cat >expect <<-EOF &&
375+
name-1
376+
name-2
377+
EOF
378+
test_config interesting.name-1 good &&
379+
test_config interesting.name-2 good &&
380+
test_config subsection.interesting.name-3 bad &&
381+
__git_get_config_variables interesting >actual &&
382+
test_cmp expect actual
383+
'
384+
385+
test_expect_success '__git_pretty_aliases' '
386+
cat >expect <<-EOF &&
387+
author
388+
hash
389+
EOF
390+
test_config pretty.author "%an %ae" &&
391+
test_config pretty.hash %H &&
392+
__git_pretty_aliases >actual &&
393+
test_cmp expect actual
394+
'
395+
396+
test_expect_success '__git_aliases' '
397+
cat >expect <<-EOF &&
398+
ci
399+
co
400+
EOF
401+
test_config alias.ci commit &&
402+
test_config alias.co checkout &&
403+
__git_aliases >actual &&
404+
test_cmp expect actual
405+
'
406+
373407
test_expect_success 'basic' '
374408
run_completion "git " &&
375409
# built-in

0 commit comments

Comments
 (0)