diff mbox series

[v2,03/11] cache-tree tests: refactor overly complex function

Message ID 20210116153554.12604-4-avarab@gmail.com (mailing list archive)
State New, archived
Headers show
Series None | expand

Commit Message

Ævar Arnfjörð Bjarmason Jan. 16, 2021, 3:35 p.m. UTC
Refactor overly complex code added in 9c4d6c0297 (cache-tree: Write
updated cache-tree after commit, 2014-07-13).

Interestingly, in the numerous commits[1][2][3] who fixed commits bugs
in this code since its introduction it seems not to have been noticed
that we didn't need to be doing some dance with grep/cut/uniq/awk to
extract this information. It can be done in a much simpler way with
just "ls-tree" and "wc -l".

I'm also removing the comment, because I think now that this code is
trivial to understand it's not needed anymore.

1. c8db708d5d (t0090: avoid passing empty string to printf %d,
   2014-09-30)
2. d69360c6b1 (t0090: tweak awk statement for Solaris
   /usr/xpg4/bin/awk, 2014-12-22)
3. 9b5a9fa60a (t0090: stop losing return codes of git commands,
   2019-11-27)

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/t0090-cache-tree.sh | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

Comments

Junio C Hamano Jan. 16, 2021, 9:51 p.m. UTC | #1
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> Refactor overly complex code added in 9c4d6c0297 (cache-tree: Write
> updated cache-tree after commit, 2014-07-13).

OK.

> Interestingly, in the numerous commits[1][2][3] who fixed commits bugs
> in this code since its introduction it seems not to have been noticed

Sorry but -ECANNOTPARSE.

> that we didn't need to be doing some dance with grep/cut/uniq/awk to
> extract this information. It can be done in a much simpler way with
> just "ls-tree" and "wc -l".

The new code seems to take advantage of the fact that the index and
HEAD are always in sync when test_cache_tree is called; it is worth
mentioning in the log message around here.

> I'm also removing the comment, because I think now that this code is
> trivial to understand it's not needed anymore.

OK.

Thanks.

> 1. c8db708d5d (t0090: avoid passing empty string to printf %d,
>    2014-09-30)
> 2. d69360c6b1 (t0090: tweak awk statement for Solaris
>    /usr/xpg4/bin/awk, 2014-12-22)
> 3. 9b5a9fa60a (t0090: stop losing return codes of git commands,
>    2019-11-27)
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  t/t0090-cache-tree.sh | 16 ++++++----------
>  1 file changed, 6 insertions(+), 10 deletions(-)
>
> diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
> index 2e3efeb80e..f1b0a6a679 100755
> --- a/t/t0090-cache-tree.sh
> +++ b/t/t0090-cache-tree.sh
> @@ -18,20 +18,16 @@ cmp_cache_tree () {
>  # correct.
>  generate_expected_cache_tree_rec () {
>  	dir="$1${1:+/}" &&
> -	# ls-files might have foo/bar, foo/bar/baz, and foo/bar/quux
> -	# We want to count only foo because it's the only direct child
> -	git ls-files >files &&
> -	subtrees=$(grep / files|cut -d / -f 1|uniq) &&
> -	subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 != "" {++c} END {print c}') &&
> -	entries=$(wc -l <files) &&
> -	printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" &&
> -	for subtree in $subtrees
> +	git ls-tree --name-only HEAD >files &&
> +	git ls-tree --name-only -d HEAD >subtrees &&
> +	printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
> +	while read subtree
>  	do
>  		(
>  			cd "$subtree"
> -			generate_expected_cache_tree_rec "$dir$subtree" || return 1
> +			generate_expected_cache_tree_rec "$subtree" || return 1
>  		)
> -	done
> +	done <subtrees
>  }
>  
>  test_cache_tree () {
diff mbox series

Patch

diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
index 2e3efeb80e..f1b0a6a679 100755
--- a/t/t0090-cache-tree.sh
+++ b/t/t0090-cache-tree.sh
@@ -18,20 +18,16 @@  cmp_cache_tree () {
 # correct.
 generate_expected_cache_tree_rec () {
 	dir="$1${1:+/}" &&
-	# ls-files might have foo/bar, foo/bar/baz, and foo/bar/quux
-	# We want to count only foo because it's the only direct child
-	git ls-files >files &&
-	subtrees=$(grep / files|cut -d / -f 1|uniq) &&
-	subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 != "" {++c} END {print c}') &&
-	entries=$(wc -l <files) &&
-	printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" &&
-	for subtree in $subtrees
+	git ls-tree --name-only HEAD >files &&
+	git ls-tree --name-only -d HEAD >subtrees &&
+	printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
+	while read subtree
 	do
 		(
 			cd "$subtree"
-			generate_expected_cache_tree_rec "$dir$subtree" || return 1
+			generate_expected_cache_tree_rec "$subtree" || return 1
 		)
-	done
+	done <subtrees
 }
 
 test_cache_tree () {