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

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Sat, 16 Jan 2021 16:35:46 +0100

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 <[email-masked]>
---
 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 () {
-- 
2.29.2.222.g5d2a92d10f8