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 <szeder@ira.uka.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed May 10, 2015 at 14:50 UTC e8f9e42829ddb966194f0d38443f321113032bb0
2 files changed +36 -20
contrib/completion/git-completion.bash
+14 -20
@@ -739,30 +739,29 @@ __git_compute_porcelain_commands ()
739 __git_porcelain_commands=$(__git_list_porcelain_commands)
740 }
741
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 ()
745 {
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
748 case "$i" in
747 - pretty.*)
748 - i="${i#pretty.}"
749 + $section.*)
750 + i="${i#$section.}"
751 echo "${i/ */}"
752 ;;
753 esac
754 done
755 }
756
757 +__git_pretty_aliases ()
758 +{
759 + __git_get_config_variables "pretty"
760 +}
761 +
762 __git_aliases ()
763 {
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"
765 }
766
767 # __git_aliased_command requires 1 argument
@@ -2259,12 +2258,7 @@ _git_remote ()
2258 __git_complete_remote_or_refspec
2259 ;;
2260 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")"
2262 ;;
2263 *)
2264 ;;
t/t9902-completion.sh
+22
@@ -370,6 +370,28 @@ test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from
370 test_cmp expect actual
371 '
372
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 +
395 test_expect_success 'basic' '
396 run_completion "git " &&
397 # built-in