Thread (181 messages) 181 messages, 7 authors, 2021-01-20

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

From: Junio C Hamano <hidden>
Date: 2021-01-16 21:52:17

Ævar Arnfjörð Bjarmason  [off-list ref] 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.
quoted hunk ↗ jump to hunk
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 <redacted>
---
 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 () {
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help