[PATCH 04/11] completion: deduplicate configuration sections

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The number of configuration variables listed by the completion script
grew quite when we started to auto-generate it from the documentation
[1], so we now complete them in two steps: first we list only the
section names, then the rest [2].  To get the section names we simply
strip everything following the first dot in each variable name,
resulting in a lot of repeated section names, because most sections
contain more than one configuration variable.  This is not a
correctness issue in practice, because Bash's completion facilities
remove all repetitions anyway, but these repetitions make testing a
bit harder.

Replace the small 'sed' script removing subsections and variable names
with an 'awk' script that does the same, and in addition removes any
repeated configuration sections as well (by first creating and filling
an associative array indexed by all encountered configuration
sections, and then iterating over this array and printing the indices,
i.e. the unique section names).  This change makes the failing 'git
config - section' test in 't9902-completion.sh' pass.

Note that this changes the order of section names in the output, and
makes it downright undeterministic, but this is not an issue, because
Bash sorts them before presenting them to the user, and our completion
tests sort them as well before comparing with the expected output.

Yeah, it would be simpler and shorter to just append '| sort -u' to
that command, but that would incur the overhead of one more external
process and pipeline stage every time a user completes configuration
sections.

[1] e17ca92637 (completion: drop the hard coded list of config vars,
    2018-05-26)
[2] f22f682695 (completion: complete general config vars in two steps,
    2018-05-27)

Signed-off-by: SZEDER Gábor <szeder.dev@xxxxxxxxx>
---
 contrib/completion/git-completion.bash | 10 +++++++++-
 t/t9902-completion.sh                  |  2 +-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index c59347daee..f89324d84f 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2398,7 +2398,15 @@ _git_config ()
 		;;
 	*)
 		__git_compute_config_vars
-		__gitcomp "$(echo "$__git_config_vars" | sed 's/\.[^ ]*/./g')"
+		__gitcomp "$(echo "$__git_config_vars" |
+				awk -F . '{
+					sections[$1] = 1
+				}
+				END {
+					for (s in sections)
+						print s "."
+				}
+				')"
 	esac
 }
 
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index e15be1164d..008fba7c89 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1698,7 +1698,7 @@ do
 	'
 done
 
-test_expect_failure 'git config - section' '
+test_expect_success 'git config - section' '
 	test_completion "git config br" <<-\EOF
 	branch.Z
 	browser.Z
-- 
2.23.0.rc2.350.gf4fdc32db7




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux