Skip to contentSkip to conversation

Use wc instead of awk to count subtrees in t0090-cache-tree

PATCH In review Ben Walton 3 participants 10 messages Jun 15, 2016

lore
Share & tools
lore

Ben Walton

author
#0 lore raw

PATCH 1/1 Use wc instead of awk to count subtrees in t0090-cache-tree

The awk statements previously used in this test weren't compatible with the native versions of awk on Solaris:

echo "dir" | /bin/awk -v c=0 '$1 {++c} END {print c}'
awk: syntax error near line 1
awk: bailing out near line 1

echo "dir" | /usr/xpg4/bin/awk -v c=0 '$1 {++c} END {print c}' 0

And with GNU awk for comparison:
echo "dir" | /opt/csw/gnu/awk -v c=0 '$1 {++c} END {print c}' 1

Instead of modifying the awk code to work, use wc -w instead as that is both adequate and simpler.

---
 t/t0090-cache-tree.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t0090-cache-tree.sh +1 -1
index 067f4c6..f2b1c9c 100755
@@ -22,7 +22,7 @@ generate_expected_cache_tree_rec () {
 	# 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
 	subtrees=$(git ls-files|grep /|cut -d / -f 1|uniq) &&
-	subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 {++c} END {print c}') &&
+	subtree_count=$(echo "$subtrees"|wc -w) &&
 	entries=$(git ls-files|wc -l) &&
 	printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" &&
 	for subtree in $subtrees
Signature · 1 line
1.9.1
#1 lore raw
Quoted 2 lines · Ben Walton

echo "dir" | /usr/xpg4/bin/awk -v c=0 '$1 {++c} END {print c}' 0

Thanks. Weird. Does

	awk -v c=0 '$1 != "" {++c} END {print c}'

work better?

[...]

t/t0090-cache-tree.sh generate_expected_cache_tree_rec () { · line 27

Quoted 10 lines
t/t0090-cache-tree.sh +1 -1
@@ -22,7 +22,7 @@ generate_expected_cache_tree_rec () {
 	# 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
 	subtrees=$(git ls-files|grep /|cut -d / -f 1|uniq) &&
-	subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 {++c} END {print c}') &&
+	subtree_count=$(echo "$subtrees"|wc -w) &&
 	entries=$(git ls-files|wc -l) &&
 	printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" &&

Some implementations of wc add a trailing space, causing

printf: 1 : invalid number

Using

	printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" $subtree_count &&

(with no quotes around $subtree_count) would avoid trouble, though that's a little subtle.

Hope that helps,
Jonathan

#2 lore raw
In reply to Ben Walton

From: Ben Walton <[email protected]>

The awk statements previously used in this test weren't compatible with the native versions of awk on Solaris:

    echo "dir" | /bin/awk -v c=0 '$1 {++c} END {print c}'
    awk: syntax error near line 1
    awk: bailing out near line 1

    echo "dir" | /usr/xpg4/bin/awk -v c=0 '$1 {++c} END {print c}'
    0

And with GNU awk for comparison:

    echo "dir" | /opt/csw/gnu/awk -v c=0 '$1 {++c} END {print c}'
    1

Work it around by using $1 != "" to state more explicitly that we are skipping empty lines.

---

* Then let's queue this, perhaps?

 t/t0090-cache-tree.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t0090-cache-tree.sh +1 -1
index 067f4c6..601d02d 100755
@@ -22,7 +22,7 @@ generate_expected_cache_tree_rec () {
 	# 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
 	subtrees=$(git ls-files|grep /|cut -d / -f 1|uniq) &&
-	subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 {++c} END {print c}') &&
+	subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 != "" {++c} END {print c}') &&
 	entries=$(git ls-files|wc -l) &&
 	printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" &&
 	for subtree in $subtrees
#3 lore raw
Quoted 12 lines · Junio C Hamano

From: Ben Walton <[email protected]>

The awk statements previously used in this test weren't compatible with the native versions of awk on Solaris:

    echo "dir" | /bin/awk -v c=0 '$1 {++c} END {print c}'
    awk: syntax error near line 1
    awk: bailing out near line 1

    echo "dir" | /usr/xpg4/bin/awk -v c=0 '$1 {++c} END {print c}'
    0
Quoted 7 lines

And with GNU awk for comparison:

    echo "dir" | /opt/csw/gnu/awk -v c=0 '$1 {++c} END {print c}'
    1

Work it around by using $1 != "" to state more explicitly that we are skipping empty lines.

By the way, I was hoping (eh, what kind of hope is that???) that $1 alone is not a kosher POSIX way but a GNUism, but that does not seem to be the case. POSIX has this [*1*]

When an expression is used in a Boolean context, if it has a numeric value, a value of zero shall be treated as false and any other value shall be treated as true. Otherwise, a string value of the null string shall be treated as false and any other value shall be treated as true. A Boolean context shall be one of the following:

and among the "Boolean context" listed is:

* An expression used as a pattern (as in Overall Program Structure)

So the example with /usr/xpg4/bin/awk does not seem to be a behaviour from a conformant implementationd, and it seems to be correct to label this as "work it around by ..." (not "avoid using GNUism").

We learn new things every day (not that I really wanted to learn glitches in various implementations of awk) ;-).

Thanks.

[Reference]

*1* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html

#4 lore raw
In reply to Junio C Hamano
Quoted 20 lines · Junio C Hamano

From: Ben Walton <[email protected]>

The awk statements previously used in this test weren't compatible with the native versions of awk on Solaris:

    echo "dir" | /bin/awk -v c=0 '$1 {++c} END {print c}'
    awk: syntax error near line 1
    awk: bailing out near line 1

    echo "dir" | /usr/xpg4/bin/awk -v c=0 '$1 {++c} END {print c}'
    0

And with GNU awk for comparison:

    echo "dir" | /opt/csw/gnu/awk -v c=0 '$1 {++c} END {print c}'
    1

Work it around by using $1 != "" to state more explicitly that we are skipping empty lines.

Quoted 6 lines

---

* Then let's queue this, perhaps?

heh, not like that without updating the subject, perhaps like this:

Subject: t0090: tweak awk statement for Solaris /usr/xpg4/bin/awk

Sorry for the noise.

Quoted 16 lines
 t/t0090-cache-tree.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t0090-cache-tree.sh +1 -1
index 067f4c6..601d02d 100755
@@ -22,7 +22,7 @@ generate_expected_cache_tree_rec () {
 	# 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
 	subtrees=$(git ls-files|grep /|cut -d / -f 1|uniq) &&
-	subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 {++c} END {print c}') &&
+	subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 != "" {++c} END {print c}') &&
 	entries=$(git ls-files|wc -l) &&
 	printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" &&
 	for subtree in $subtrees
Reviewed-by
#5 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Junio C Hamano

From: Ben Walton <[email protected]>

The awk statements previously used in this test weren't compatible with the native versions of awk on Solaris:

echo "dir" | /bin/awk -v c=0 '$1 {++c} END {print c}'
awk: syntax error near line 1
awk: bailing out near line 1

If I were doing it, I'd leave the above four lines out --- they are describing an unrelated problem. I wonder if we should make the test harness respect SANE_TOOL_PATH to avoid that kind of problem in the future.

[...]

Quoted 3 lines

heh, not like that without updating the subject, perhaps like this:

Subject: t0090: tweak awk statement for Solaris /usr/xpg4/bin/awk

With the updated subject,

Thanks.

Reviewed-by Jonathan Nieder
#6 lore raw
Quoted 3 lines · Jonathan Nieder

With the updated subject,

Thanks. Here is what I tentatively queued for today's pushout.

-- >8 --
From: Ben Walton <[email protected]>
Date: Mon, 22 Dec 2014 15:25:44 -0800
Subject: [PATCH] t0090: tweak awk statement for Solaris /usr/xpg4/bin/awk

The awk statements previously used in this test weren't compatible with the native versions of awk on Solaris:

    echo "dir" | /bin/awk -v c=0 '$1 {++c} END {print c}'
    awk: syntax error near line 1
    awk: bailing out near line 1

    echo "dir" | /usr/xpg4/bin/awk -v c=0 '$1 {++c} END {print c}'
    0

Even though we do not cater to tools in /usr/bin on Solaris that are overridden by corresponding ones in /usr/xpg?/bin, in this case, even the XPG version does not work correctly.

With GNU awk for comparison:

    echo "dir" | /opt/csw/gnu/awk -v c=0 '$1 {++c} END {print c}'
    1

which is what this test expects (and is in line with POSIX; non-empty string is true and an empty string is false).

Work this issue around by using $1 != "" to state more explicitly that we are skipping empty lines.

---
 t/t0090-cache-tree.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t0090-cache-tree.sh +1 -1
index 067f4c6..601d02d 100755
@@ -22,7 +22,7 @@ generate_expected_cache_tree_rec () {
 	# 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
 	subtrees=$(git ls-files|grep /|cut -d / -f 1|uniq) &&
-	subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 {++c} END {print c}') &&
+	subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 != "" {++c} END {print c}') &&
 	entries=$(git ls-files|wc -l) &&
 	printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" &&
 	for subtree in $subtrees
Signature · 1 line
2.2.1-321-gd161b79
#7 lore raw
In reply to Ben Walton
Quoted 10 lines · Ben Walton

The awk statements previously used in this test weren't compatible with the native versions of awk on Solaris:

echo "dir" | /bin/awk -v c=0 '$1 {++c} END {print c}'
awk: syntax error near line 1
awk: bailing out near line 1

echo "dir" | /usr/xpg4/bin/awk -v c=0 '$1 {++c} END {print c}' 0

Quoted 6 lines

And with GNU awk for comparison:
echo "dir" | /opt/csw/gnu/awk -v c=0 '$1 {++c} END {print c}' 1

Instead of modifying the awk code to work, use wc -w instead as that is both adequate and simpler.

Hmm, why "wc -w" not "wc -l", though? Is somebody squashing a one-elem-per-line output from ls-files onto a single line?

#8 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Ben Walton

echo "dir" | /usr/xpg4/bin/awk -v c=0 '$1 {++c} END {print c}' 0

And with GNU awk for comparison:
echo "dir" | /opt/csw/gnu/awk -v c=0 '$1 {++c} END {print c}' 1

Quoted 5 lines
Quoted 2 lines

Instead of modifying the awk code to work, use wc -w instead as that is both adequate and simpler.

Hmm, why "wc -w" not "wc -l", though? Is somebody squashing a one-elem-per-line output from ls-files onto a single line?

The old code was trying to skip empty lines.

#9 lore raw
Quoted 10 lines · Jonathan Nieder
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Ben Walton

echo "dir" | /usr/xpg4/bin/awk -v c=0 '$1 {++c} END {print c}' 0

And with GNU awk for comparison:
echo "dir" | /opt/csw/gnu/awk -v c=0 '$1 {++c} END {print c}' 1

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Instead of modifying the awk code to work, use wc -w instead as that is both adequate and simpler.

Hmm, why "wc -w" not "wc -l", though? Is somebody squashing a one-elem-per-line output from ls-files onto a single line?

The old code was trying to skip empty lines.

Ahh, I misread the original.

Your suggestion to explicitly check $1 != "" makes sense to me now.

To be blunt, I do not have much sympathy to those who insist using /usr/bin versions of various tools on Solaris that are overriden by xpg variants, but it is somewhat disturbing that the one from xpg4 does not work.