Skip to contentSkip to conversation

completion: use awk for filtering the config entries

PATCH In review Beat Bolli 4 participants 7 messages Feb 16, 2024 – Feb 19, 2024

lore
Share & tools
lore

Beat Bolli

author
#0 lore raw

PATCH 1/1 completion: use awk for filtering the config entries

Commits 1e0ee4087e (completion: add and use __git_compute_first_level_config_vars_for_section, 2024-02-10) and 6e32f718ff (completion: add and use __git_compute_second_level_config_vars_for_section, 2024-02-10) introduced new helpers for config completion.

Both helpers use a pipeline of grep and awk to filter the list of config entries. awk is perfectly capable of filtering, so let's eliminate the grep process and move the filtering into the awk script.

The "-E" grep option (extended syntax) was not necessary, as $section is a single word.

While at it, wrap the over-long lines to make them more readable.

---

Junio, this goes on top of 'pb/complete-config' which is on next currently.

 contrib/completion/git-completion.bash | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
contrib/completion/git-completion.bash +4 -2
index 444b3efa63..fcf1afd75d 100644
@@ -2673,7 +2673,8 @@ __git_compute_first_level_config_vars_for_section ()
 	__git_compute_config_vars
 	local this_section="__git_first_level_config_vars_for_section_${section}"
 	test -n "${!this_section}" ||
-	printf -v "__git_first_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars" | grep -E "^${section}\.[a-z]" | awk -F. '{print $2}')"
+	printf -v "__git_first_level_config_vars_for_section_${section}" %s \
+		"$(echo "$__git_config_vars" | awk -F. "/^${section}\.[a-z]/ { print \$2 }")"
 }
 
 __git_compute_second_level_config_vars_for_section ()
@@ -2682,7 +2683,8 @@ __git_compute_second_level_config_vars_for_section ()
 	__git_compute_config_vars_all
 	local this_section="__git_second_level_config_vars_for_section_${section}"
 	test -n "${!this_section}" ||
-	printf -v "__git_second_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars_all" | grep -E "^${section}\.<" | awk -F. '{print $3}')"
+	printf -v "__git_second_level_config_vars_for_section_${section}" %s \
+		"$(echo "$__git_config_vars_all" | awk -F. "/^${section}\.</ { print \$3 }")"
 }
 
 __git_config_sections=
Signature · 1 line
2.42.0.583.ga47b40fd90
#1 lore raw
Quoted 9 lines · Beat Bolli

Commits 1e0ee4087e (completion: add and use __git_compute_first_level_config_vars_for_section, 2024-02-10) and 6e32f718ff (completion: add and use __git_compute_second_level_config_vars_for_section, 2024-02-10) introduced new helpers for config completion.

Both helpers use a pipeline of grep and awk to filter the list of config entries. awk is perfectly capable of filtering, so let's eliminate the grep process and move the filtering into the awk script.

Makes sense. I wonder if we can have some simple script sanity checker that catches things like this, e.g., catting a single file into pipe, grep appearing upstream of awk or sed, etc.

Quoted 10 lines

The "-E" grep option (extended syntax) was not necessary, as $section is a single word.

While at it, wrap the over-long lines to make them more readable.

---

Junio, this goes on top of 'pb/complete-config' which is on next currently.

Alternatively we could redo the topic, squashing this fix in, after the release when we rewind 'next'.

Thanks.

Quoted 28 lines
 contrib/completion/git-completion.bash | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
contrib/completion/git-completion.bash +4 -2
index 444b3efa63..fcf1afd75d 100644
@@ -2673,7 +2673,8 @@ __git_compute_first_level_config_vars_for_section ()
 	__git_compute_config_vars
 	local this_section="__git_first_level_config_vars_for_section_${section}"
 	test -n "${!this_section}" ||
-	printf -v "__git_first_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars" | grep -E "^${section}\.[a-z]" | awk -F. '{print $2}')"
+	printf -v "__git_first_level_config_vars_for_section_${section}" %s \
+		"$(echo "$__git_config_vars" | awk -F. "/^${section}\.[a-z]/ { print \$2 }")"
 }
 
 __git_compute_second_level_config_vars_for_section ()
@@ -2682,7 +2683,8 @@ __git_compute_second_level_config_vars_for_section ()
 	__git_compute_config_vars_all
 	local this_section="__git_second_level_config_vars_for_section_${section}"
 	test -n "${!this_section}" ||
-	printf -v "__git_second_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars_all" | grep -E "^${section}\.<" | awk -F. '{print $3}')"
+	printf -v "__git_second_level_config_vars_for_section_${section}" %s \
+		"$(echo "$__git_config_vars_all" | awk -F. "/^${section}\.</ { print \$3 }")"
 }
 
 __git_config_sections=
author
#2 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Beat Bolli

Commits 1e0ee4087e (completion: add and use __git_compute_first_level_config_vars_for_section, 2024-02-10) and 6e32f718ff (completion: add and use __git_compute_second_level_config_vars_for_section, 2024-02-10) introduced new helpers for config completion.

Quoted 7 lines
Quoted 3 lines

Both helpers use a pipeline of grep and awk to filter the list of config entries. awk is perfectly capable of filtering, so let's eliminate the grep process and move the filtering into the awk script.

Makes sense. I wonder if we can have some simple script sanity checker that catches things like this, e.g., catting a single file into pipe, grep appearing upstream of awk or sed, etc.

Yes, there are quite a few cases of these in t/. I'm not sure if it's worth the churn, though. At least it would make the tests faster on Windows...

Quoted 5 lines
Quoted 5 lines

The "-E" grep option (extended syntax) was not necessary, as $section is a single word.

While at it, wrap the over-long lines to make them more readable.

Quoted 8 lines
Quoted 5 lines

---

Junio, this goes on top of 'pb/complete-config' which is on next currently.

Alternatively we could redo the topic, squashing this fix in, after the release when we rewind 'next'.

As you like. This commit would have to be split to apply to the two original commits.

Quoted 27 lines
Quoted 27 lines
  contrib/completion/git-completion.bash | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
contrib/completion/git-completion.bash +4 -2
index 444b3efa63..fcf1afd75d 100644
@@ -2673,7 +2673,8 @@ __git_compute_first_level_config_vars_for_section ()
  	__git_compute_config_vars
  	local this_section="__git_first_level_config_vars_for_section_${section}"
  	test -n "${!this_section}" ||
-	printf -v "__git_first_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars" | grep -E "^${section}\.[a-z]" | awk -F. '{print $2}')"
+	printf -v "__git_first_level_config_vars_for_section_${section}" %s \
+		"$(echo "$__git_config_vars" | awk -F. "/^${section}\.[a-z]/ { print \$2 }")"
  }
 
  __git_compute_second_level_config_vars_for_section ()
@@ -2682,7 +2683,8 @@ __git_compute_second_level_config_vars_for_section ()
  	__git_compute_config_vars_all
  	local this_section="__git_second_level_config_vars_for_section_${section}"
  	test -n "${!this_section}" ||
-	printf -v "__git_second_level_config_vars_for_section_${section}" %s "$(echo "$__git_config_vars_all" | grep -E "^${section}\.<" | awk -F. '{print $3}')"
+	printf -v "__git_second_level_config_vars_for_section_${section}" %s \
+		"$(echo "$__git_config_vars_all" | awk -F. "/^${section}\.</ { print \$3 }")"
  }
 
  __git_config_sections=
#5 lore raw
In reply to Junio C Hamano

Hi Beat and Junio,

Le 2024-02-16 à 12:35, Junio C Hamano a écrit :

Quoted 8 lines
Quoted 6 lines · Beat Bolli

Commits 1e0ee4087e (completion: add and use __git_compute_first_level_config_vars_for_section, 2024-02-10) and 6e32f718ff (completion: add and use __git_compute_second_level_config_vars_for_section, 2024-02-10) introduced new helpers for config completion.

Quoted 5 lines
Quoted 3 lines

Both helpers use a pipeline of grep and awk to filter the list of config entries. awk is perfectly capable of filtering, so let's eliminate the grep process and move the filtering into the awk script.

Makes sense.

Yes, thanks for improving that!

Quoted 12 lines

I wonder if we can have some simple script sanity checker that catches things like this, e.g., catting a single file into pipe, grep appearing upstream of awk or sed, etc.

Quoted 8 lines

The "-E" grep option (extended syntax) was not necessary, as $section is a single word.

While at it, wrap the over-long lines to make them more readable.

---

Quoted 7 lines
Quoted 2 lines

Junio, this goes on top of 'pb/complete-config' which is on next currently.

Alternatively we could redo the topic, squashing this fix in, after the release when we rewind 'next'.

Thanks.

Actually you already merged this topic to master in 89400c3615, so it would go on top, no ?

Thanks,
Philippe.

#6 lore raw
Quoted 10 lines · Philippe Blain
Quoted 7 lines
Quoted 2 lines

Junio, this goes on top of 'pb/complete-config' which is on next currently.

Alternatively we could redo the topic, squashing this fix in, after the release when we rewind 'next'.

Thanks.

Actually you already merged this topic to master in 89400c3615, so it would go on top, no ?

Ah, then that's fine. I didn't check what I read above myself before responding X-<.

#3 lore raw
In reply to Beat Bolli

Hi Beat,

Quoted 13 lines · Beat Bolli
Quoted 12 lines · Junio C Hamano
Quoted 9 lines · Beat Bolli

Commits 1e0ee4087e (completion: add and use __git_compute_first_level_config_vars_for_section, 2024-02-10) and 6e32f718ff (completion: add and use __git_compute_second_level_config_vars_for_section, 2024-02-10) introduced new helpers for config completion.

Both helpers use a pipeline of grep and awk to filter the list of config entries. awk is perfectly capable of filtering, so let's eliminate the grep process and move the filtering into the awk script.

Quoted 6 lines
Quoted 3 lines

Makes sense. I wonder if we can have some simple script sanity checker that catches things like this, e.g., catting a single file into pipe, grep appearing upstream of awk or sed, etc.

Yes, there are quite a few cases of these in t/. I'm not sure if it's worth the churn, though. At least it would make the tests faster on Windows...

Thank you for caring about the speed on Windows!

Ciao,
Johannes

#4 lore raw
Quoted 8 lines · Johannes Schindelin
Quoted 6 lines
Quoted 3 lines

Makes sense. I wonder if we can have some simple script sanity checker that catches things like this, e.g., catting a single file into pipe, grep appearing upstream of awk or sed, etc.

Yes, there are quite a few cases of these in t/. I'm not sure if it's worth the churn, though. At least it would make the tests faster on Windows...

Thank you for caring about the speed on Windows!

Yup, that is why I asked ;-)