git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* t0090-cache-tree fails due to wc whitespace
@ 2011-12-14 14:35 Brian Gernhardt
  2011-12-14 14:57 ` Stefano Lattarini
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Brian Gernhardt @ 2011-12-14 14:35 UTC (permalink / raw)
  To: Git List; +Cc: Thomas Rast

It's time for my periodic complaint:  People assuming `wc -l` outputs just a number.  wc on OS X (and perhaps other BSD-like systems) always aligns the output in columns, even with the -l flag.  Generally this results in a quick patch from me to remove some unneeded quotes.  However, this time it's used in a more complex manner:

	echo "SHA " \
	    "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
	cmp_cache_tree expect

This results in errors like:

--- expect	2011-12-14 14:26:26.000000000 +0000
+++ filtered	2011-12-14 14:26:26.000000000 +0000
@@ -1 +1 @@
-SHA  (       1 entries, 0 subtrees)
+SHA  (1 entries, 0 subtrees)

I was able to fix this by adding a sed command to remove leading spaces:

-           "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
+           "($(git ls-files|wc -l|sed -e 's/^ *//') entries, 0 subtrees)" >expect &&

But I'm not sure if this is the best way to solve the issue.

~~ Brian Gernhardt

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: t0090-cache-tree fails due to wc whitespace
  2011-12-14 14:35 t0090-cache-tree fails due to wc whitespace Brian Gernhardt
@ 2011-12-14 14:57 ` Stefano Lattarini
  2011-12-14 15:09 ` Hallvard Breien Furuseth
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Stefano Lattarini @ 2011-12-14 14:57 UTC (permalink / raw)
  To: Brian Gernhardt; +Cc: Git List, Thomas Rast

On Wednesday 14 December 2011, Brian Gernhardt wrote:
> It's time for my periodic complaint:  People assuming `wc -l`
> outputs just a number.  wc on OS X (and perhaps other BSD-like
> systems) always aligns the output in columns, even with the -l
> flag.
>
It surely does so on Solaris 10 as well:

$ echo x | wc -l
       1
$ for i in {1..1000}; do echo x; done | wc -l
    1000

Regards,
  Stefano

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: t0090-cache-tree fails due to wc whitespace
  2011-12-14 14:35 t0090-cache-tree fails due to wc whitespace Brian Gernhardt
  2011-12-14 14:57 ` Stefano Lattarini
@ 2011-12-14 15:09 ` Hallvard Breien Furuseth
  2011-12-14 15:41 ` Johannes Sixt
  2011-12-14 15:43 ` t0090-cache-tree fails due to wc whitespace Thomas Rast
  3 siblings, 0 replies; 8+ messages in thread
From: Hallvard Breien Furuseth @ 2011-12-14 15:09 UTC (permalink / raw)
  To: Brian Gernhardt; +Cc: Git List, Thomas Rast

Brian Gernhardt writes:
> I was able to fix this by adding a sed command to remove leading spaces:
> 
> -           "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
> +           "($(git ls-files|wc -l|sed -e 's/^ *//') entries, 0 subtrees)" >expect &&
> 
> But I'm not sure if this is the best way to solve the issue.

Well,  tr -d ' '  saves all of 7 characters from  sed -e 's/^ *//'.

-- 
Hallvard

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: t0090-cache-tree fails due to wc whitespace
  2011-12-14 14:35 t0090-cache-tree fails due to wc whitespace Brian Gernhardt
  2011-12-14 14:57 ` Stefano Lattarini
  2011-12-14 15:09 ` Hallvard Breien Furuseth
@ 2011-12-14 15:41 ` Johannes Sixt
  2011-12-20  8:24   ` [PATCH] t0090: be prepared that 'wc -l' writes leading blanks Johannes Sixt
  2011-12-14 15:43 ` t0090-cache-tree fails due to wc whitespace Thomas Rast
  3 siblings, 1 reply; 8+ messages in thread
From: Johannes Sixt @ 2011-12-14 15:41 UTC (permalink / raw)
  To: Brian Gernhardt; +Cc: Git List, Thomas Rast

Am 12/14/2011 15:35, schrieb Brian Gernhardt:
> It's time for my periodic complaint:  People assuming `wc -l` outputs
> just a number.  wc on OS X (and perhaps other BSD-like systems) always
> aligns the output in columns, even with the -l flag.  Generally this
> results in a quick patch from me to remove some unneeded quotes.
> However, this time it's used in a more complex manner:
> 
> 	echo "SHA " \
> 	    "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
> 	cmp_cache_tree expect

I'd solve it by moving the command substitution outside the quoted string:

 	printf "SHA (%d entries, 0 subtrees)\n" \
		$(git ls-files | wc -l) >expect &&

Other proposed solutions add another process. I don't like that on Windows ;)

-- Hannes

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: t0090-cache-tree fails due to wc whitespace
  2011-12-14 14:35 t0090-cache-tree fails due to wc whitespace Brian Gernhardt
                   ` (2 preceding siblings ...)
  2011-12-14 15:41 ` Johannes Sixt
@ 2011-12-14 15:43 ` Thomas Rast
  2011-12-14 15:54   ` Andreas Schwab
  3 siblings, 1 reply; 8+ messages in thread
From: Thomas Rast @ 2011-12-14 15:43 UTC (permalink / raw)
  To: Brian Gernhardt; +Cc: Git List

Brian Gernhardt wrote:
> 
> It's time for my periodic complaint: People assuming `wc -l` outputs
> just a number.  wc on OS X (and perhaps other BSD-like systems)
> always aligns the output in columns, even with the -l flag.

Oops.

> Generally this results in a quick patch from me to remove some
> unneeded quotes.  However, this time it's used in a more complex
> manner:
[...]
> -           "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
> +           "($(git ls-files|wc -l|sed -e 's/^ *//') entries, 0 subtrees)" >expect &&

I'm tempted to say we should define

test_wc_l () {
	test $# = 0 || error "bug in test script: passing arguments to wc -l is not portable"
	wc -l | tr -d -c 0-9
}

just to avoid issues if any wc comes across and prints a tab for
padding or says "hi, the number of lines you wanted to know is: 42".



(Oddly, according to 'man 1p wc' here, the POSIXly correct format in
the absence of options is

  "%d %d %d %s\n", <newlines>, <words>, <bytes>, <file>

Taking it literally would mean no padding/alignment whatsoever.
Neither GNU wc on my Linux exactly conforms to this.)

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: t0090-cache-tree fails due to wc whitespace
  2011-12-14 15:43 ` t0090-cache-tree fails due to wc whitespace Thomas Rast
@ 2011-12-14 15:54   ` Andreas Schwab
  0 siblings, 0 replies; 8+ messages in thread
From: Andreas Schwab @ 2011-12-14 15:54 UTC (permalink / raw)
  To: Thomas Rast; +Cc: Brian Gernhardt, Git List

Thomas Rast <trast@student.ethz.ch> writes:

> (Oddly, according to 'man 1p wc' here, the POSIXly correct format in
> the absence of options is
>
>   "%d %d %d %s\n", <newlines>, <words>, <bytes>, <file>
>
> Taking it literally would mean no padding/alignment whatsoever.
> Neither GNU wc on my Linux exactly conforms to this.)

A space in the format string stands for one or more <blank>s.  If only a
single <space> is allowed the standard uses 𝚫.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] t0090: be prepared that 'wc -l' writes leading blanks
  2011-12-14 15:41 ` Johannes Sixt
@ 2011-12-20  8:24   ` Johannes Sixt
  2011-12-20  9:17     ` Thomas Rast
  0 siblings, 1 reply; 8+ messages in thread
From: Johannes Sixt @ 2011-12-20  8:24 UTC (permalink / raw)
  To: Brian Gernhardt; +Cc: Git List, Thomas Rast

From: Johannes Sixt <j6t@kdbg.org>

Use 'printf %d $(whatever|wc -l)' so that the shell removes the blanks
for us.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
Am 12/14/2011 16:41, schrieb Johannes Sixt:
> I'd solve it by moving the command substitution outside the quoted string:
> 
>  	printf "SHA (%d entries, 0 subtrees)\n" \
> 		$(git ls-files | wc -l) >expect &&
> 
> Other proposed solutions add another process. I don't like that on Windows ;)

And here is a proper patch to that effect.

-- Hannes

 t/t0090-cache-tree.sh |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
index f972562..6c33e28 100755
--- a/t/t0090-cache-tree.sh
+++ b/t/t0090-cache-tree.sh
@@ -17,15 +17,13 @@ cmp_cache_tree () {
 # test-dump-cache-tree already verifies that all existing data is
 # correct.
 test_shallow_cache_tree () {
-	echo "SHA " \
-	    "($(git ls-files|wc -l) entries, 0 subtrees)" >expect &&
+	printf "SHA  (%d entries, 0 subtrees)\n" $(git ls-files|wc -l) >expect &&
 	cmp_cache_tree expect
 }
 
 test_invalid_cache_tree () {
 	echo "invalid                                   (0 subtrees)" >expect &&
-	echo "SHA #(ref) " \
-	    "($(git ls-files|wc -l) entries, 0 subtrees)" >>expect &&
+	printf "SHA #(ref)  (%d entries, 0 subtrees)\n" $(git ls-files|wc -l) >>expect &&
 	cmp_cache_tree expect
 }
 
-- 
1.7.8.1499.g39f909

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] t0090: be prepared that 'wc -l' writes leading blanks
  2011-12-20  8:24   ` [PATCH] t0090: be prepared that 'wc -l' writes leading blanks Johannes Sixt
@ 2011-12-20  9:17     ` Thomas Rast
  0 siblings, 0 replies; 8+ messages in thread
From: Thomas Rast @ 2011-12-20  9:17 UTC (permalink / raw)
  To: Johannes Sixt, Junio C Hamano; +Cc: Brian Gernhardt, Git List

Sorry for slacking off on replying to this thread...

Johannes Sixt <j.sixt@viscovery.net> writes:
> Use 'printf %d $(whatever|wc -l)' so that the shell removes the blanks
> for us.
>
> Signed-off-by: Johannes Sixt <j6t@kdbg.org>
> ---
> Am 12/14/2011 16:41, schrieb Johannes Sixt:
>> I'd solve it by moving the command substitution outside the quoted string:
>> 
>>  	printf "SHA (%d entries, 0 subtrees)\n" \
>> 		$(git ls-files | wc -l) >expect &&
>> 
>> Other proposed solutions add another process. I don't like that on Windows ;)
>
> And here is a proper patch to that effect.

Acked-by: Thomas Rast <trast@student.ethz.ch>

This is the best solution also because it the formatting more (instead
of less) readable.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-12-20  9:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-14 14:35 t0090-cache-tree fails due to wc whitespace Brian Gernhardt
2011-12-14 14:57 ` Stefano Lattarini
2011-12-14 15:09 ` Hallvard Breien Furuseth
2011-12-14 15:41 ` Johannes Sixt
2011-12-20  8:24   ` [PATCH] t0090: be prepared that 'wc -l' writes leading blanks Johannes Sixt
2011-12-20  9:17     ` Thomas Rast
2011-12-14 15:43 ` t0090-cache-tree fails due to wc whitespace Thomas Rast
2011-12-14 15:54   ` Andreas Schwab

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).