Skip to content

Commit e8f9e42

Browse files
szedergitster
authored andcommitted
completion: add a helper function to get config variables
Currently there are a few completion functions that perform similar 'git config' queries and filtering to get config variable names: the completion of pretty aliases, aliases, and remote groups for 'git remote update'. Unify those 'git config' queries in a helper function to eliminate code duplication. Though the helper functions to get pretty aliases and alieses are reduced to mere one-liner wrappers around the newly added function, keep these helpers still, because users' completion functions out there might depend on them. And they keep their callers a tad easier to read, too. Add tests for the pretty alias and alias helper to show that they work as before; not for the remote groups query, though, because that's not extracted into a helper function and it's not worth the effort to do so for a sole callsite. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c518059 commit e8f9e42

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

contrib/completion/git-completion.bash

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

742-
__git_pretty_aliases ()
742+
# Lists all set config variables starting with the given section prefix,
743+
# with the prefix removed.
744+
__git_get_config_variables ()
743745
{
744-
local i IFS=$'\n'
745-
for i in $(git --git-dir="$(__gitdir)" config --get-regexp "pretty\..*" 2>/dev/null); do
746+
local section="$1" i IFS=$'\n'
747+
for i in $(git --git-dir="$(__gitdir)" config --get-regexp "$section\..*" 2>/dev/null); do
746748
case "$i" in
747-
pretty.*)
748-
i="${i#pretty.}"
749+
$section.*)
750+
i="${i#$section.}"
749751
echo "${i/ */}"
750752
;;
751753
esac
752754
done
753755
}
754756

757+
__git_pretty_aliases ()
758+
{
759+
__git_get_config_variables "pretty"
760+
}
761+
755762
__git_aliases ()
756763
{
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
764+
__git_get_config_variables "alias"
766765
}
767766

768767
# __git_aliased_command requires 1 argument
@@ -2259,12 +2258,7 @@ _git_remote ()
22592258
__git_complete_remote_or_refspec
22602259
;;
22612260
update)
2262-
local i c='' IFS=$'\n'
2263-
for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
2264-
i="${i#remotes.}"
2265-
c="$c ${i/ */}"
2266-
done
2267-
__gitcomp "$c"
2261+
__gitcomp "$(__git_get_config_variables "remotes")"
22682262
;;
22692263
*)
22702264
;;

t/t9902-completion.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,28 @@ 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_pretty_aliases' '
374+
cat >expect <<-EOF &&
375+
author
376+
hash
377+
EOF
378+
test_config pretty.author "%an %ae" &&
379+
test_config pretty.hash %H &&
380+
__git_pretty_aliases >actual &&
381+
test_cmp expect actual
382+
'
383+
384+
test_expect_success '__git_aliases' '
385+
cat >expect <<-EOF &&
386+
ci
387+
co
388+
EOF
389+
test_config alias.ci commit &&
390+
test_config alias.co checkout &&
391+
__git_aliases >actual &&
392+
test_cmp expect actual
393+
'
394+
373395
test_expect_success 'basic' '
374396
run_completion "git " &&
375397
# built-in

0 commit comments

Comments
 (0)