threads / patch / 16101

patcht4030: Don't use echo -n

Subject: [PATCH] t4030: Don't use echo -n

## tl;dr

15 messages between Oct 30, 2008 and Nov 3, 2008. Diffs are folded; open one to read it.

replies: 14people: 10as markdown or json

Brian Gernhardt· Oct 30, 2008, 22:12 UTC · lore
Signed-off-by: Brian Gernhardt <benji@silverinsanity.com>
---
 t/t4030-diff-textconv.sh |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
Show changes to t/t4030-diff-textconv.sh +2 −3
diff --git a/t/t4030-diff-textconv.sh b/t/t4030-diff-textconv.sh
index 3945731..7ec244f 100755
--- a/t/t4030-diff-textconv.sh
+++ b/t/t4030-diff-textconv.sh
@@ -96,16 +96,15 @@ cat >expect.typechange <<'EOF'
 -1
 diff --git a/file b/file
 new file mode 120000
-index ad8b3d2..67be421
+index ad8b3d2..8e4020b
 --- /dev/null
 +++ b/file
 @@ -0,0 +1 @@
 +frotz
-\ No newline at end of file
 EOF
 # make a symlink the hard way that works on symlink-challenged file systems
 test_expect_success 'textconv does not act on symlinks' '
-	echo -n frotz > file &&
+	echo frotz > file &&
 	git add file &&
 	git ls-files -s | sed -e s/100644/120000/ |
 		git update-index --index-info &&
-- 
1.6.0.3.523.g304d0
Ian Hilt· Oct 30, 2008, 23:52 UTC · re: Brian Gernhardt · lore

Re: [PATCH] t4030: Don't use echo -n

On Thu, Oct 30, 2008 at 06:12:56PM -0400, Brian Gernhardt wrote:
> Signed-off-by: Brian Gernhardt <benji@silverinsanity.com>
No commit message???
Show 29 quoted lines
> ---
>  t/t4030-diff-textconv.sh |    5 ++---
>  1 files changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/t/t4030-diff-textconv.sh b/t/t4030-diff-textconv.sh
> index 3945731..7ec244f 100755
> --- a/t/t4030-diff-textconv.sh
> +++ b/t/t4030-diff-textconv.sh
> @@ -96,16 +96,15 @@ cat >expect.typechange <<'EOF'
>  -1
>  diff --git a/file b/file
>  new file mode 120000
> -index ad8b3d2..67be421
> +index ad8b3d2..8e4020b
>  --- /dev/null
>  +++ b/file
>  @@ -0,0 +1 @@
>  +frotz
> -\ No newline at end of file
>  EOF
>  # make a symlink the hard way that works on symlink-challenged file systems
>  test_expect_success 'textconv does not act on symlinks' '
> -	echo -n frotz > file &&
> +	echo frotz > file &&
>  	git add file &&
>  	git ls-files -s | sed -e s/100644/120000/ |
>  		git update-index --index-info &&
> -- 
> 1.6.0.3.523.g304d0

This doesn't apply for me. My tip is up-to-date, but it doesn't even have this file it t/.

A little bewildered,
	Ian
Brian Gernhardt· Oct 31, 2008, 05:02 UTC · re: Ian Hilt · lore

Re: [PATCH] t4030: Don't use echo -n

On Oct 30, 2008, at 7:52 PM, Ian Hilt wrote:
Show 5 quoted lines
> On Thu, Oct 30, 2008 at 06:12:56PM -0400, Brian Gernhardt wrote:
>> Signed-off-by: Brian Gernhardt <benji@silverinsanity.com>
>
> No commit message???
>>

"Don't use echo -n" seemed simple enough to me, as it's the only thing it does. And the reason is that echo -n isn't portable, as has been determined previously in git. I'll update this and use the better replacement "printf" anyway.

> This doesn't apply for me.  My tip is up-to-date, but it doesn't  
> even have
> this file it t/.
This is based on next and is for a file that isn't in master.
~~ Brian
Brian Gernhardt· Oct 31, 2008, 05:09 UTC · re: Brian Gernhardt · lore

[PATCH] Avoid using non-portable `echo -n` in tests.

Not all /bin/sh have a builtin echo that recognizes -n. Using printf is far more portable.

Discovered on OS X 10.5.5 in t4030-diff-textconv.sh and changed in all the test scripts.

Signed-off-by: Brian Gernhardt <benji@silverinsanity.com>
---
 t/t2005-checkout-index-symlinks.sh |    2 +-
 t/t2102-update-index-symlinks.sh   |    4 ++--
 t/t4030-diff-textconv.sh           |    2 +-
 t/t6025-merge-symlinks.sh          |    4 ++--
 t/t9400-git-cvsserver-server.sh    |    2 +-
 5 files changed, 7 insertions(+), 7 deletions(-)
Show changes to 5 files +7 −7

t/t2005-checkout-index-symlinks.sh, t/t2102-update-index-symlinks.sh, t/t4030-diff-textconv.sh, t/t6025-merge-symlinks.sh, t/t9400-git-cvsserver-server.sh

diff --git a/t/t2005-checkout-index-symlinks.sh b/t/t2005-checkout-index-symlinks.sh
index ed12c4d..9fa5610 100755
--- a/t/t2005-checkout-index-symlinks.sh
+++ b/t/t2005-checkout-index-symlinks.sh
@@ -13,7 +13,7 @@ file if core.symlinks is false.'
 test_expect_success \
 'preparation' '
 git config core.symlinks false &&
-l=$(echo -n file | git hash-object -t blob -w --stdin) &&
+l=$(printf file | git hash-object -t blob -w --stdin) &&
 echo "120000 $l	symlink" | git update-index --index-info'
 
 test_expect_success \
diff --git a/t/t2102-update-index-symlinks.sh b/t/t2102-update-index-symlinks.sh
index f195aef..1ed44ee 100755
--- a/t/t2102-update-index-symlinks.sh
+++ b/t/t2102-update-index-symlinks.sh
@@ -13,12 +13,12 @@ even if a plain file is in the working tree if core.symlinks is false.'
 test_expect_success \
 'preparation' '
 git config core.symlinks false &&
-l=$(echo -n file | git hash-object -t blob -w --stdin) &&
+l=$(printf file | git hash-object -t blob -w --stdin) &&
 echo "120000 $l	symlink" | git update-index --index-info'
 
 test_expect_success \
 'modify the symbolic link' '
-echo -n new-file > symlink &&
+printf new-file > symlink &&
 git update-index symlink'
 
 test_expect_success \
diff --git a/t/t4030-diff-textconv.sh b/t/t4030-diff-textconv.sh
index 3945731..a235955 100755
--- a/t/t4030-diff-textconv.sh
+++ b/t/t4030-diff-textconv.sh
@@ -105,7 +105,7 @@ index ad8b3d2..67be421
 EOF
 # make a symlink the hard way that works on symlink-challenged file systems
 test_expect_success 'textconv does not act on symlinks' '
-	echo -n frotz > file &&
+	printf frotz > file &&
 	git add file &&
 	git ls-files -s | sed -e s/100644/120000/ |
 		git update-index --index-info &&
diff --git a/t/t6025-merge-symlinks.sh b/t/t6025-merge-symlinks.sh
index 53892a5..433c4de 100755
--- a/t/t6025-merge-symlinks.sh
+++ b/t/t6025-merge-symlinks.sh
@@ -18,11 +18,11 @@ git add file &&
 git commit -m initial &&
 git branch b-symlink &&
 git branch b-file &&
-l=$(echo -n file | git hash-object -t blob -w --stdin) &&
+l=$(printf file | git hash-object -t blob -w --stdin) &&
 echo "120000 $l	symlink" | git update-index --index-info &&
 git commit -m master &&
 git checkout b-symlink &&
-l=$(echo -n file-different | git hash-object -t blob -w --stdin) &&
+l=$(printf file-different | git hash-object -t blob -w --stdin) &&
 echo "120000 $l	symlink" | git update-index --index-info &&
 git commit -m b-symlink &&
 git checkout b-file &&
diff --git a/t/t9400-git-cvsserver-server.sh b/t/t9400-git-cvsserver-server.sh
index c1850d2..f6a2dbd 100755
--- a/t/t9400-git-cvsserver-server.sh
+++ b/t/t9400-git-cvsserver-server.sh
@@ -424,7 +424,7 @@ cd "$WORKDIR"
 test_expect_success 'cvs update (-p)' '
     touch really-empty &&
     echo Line 1 > no-lf &&
-    echo -n Line 2 >> no-lf &&
+    printf Line 2 >> no-lf &&
     git add really-empty no-lf &&
     git commit -q -m "Update -p test" &&
     git push gitcvs.git >/dev/null &&
-- 
1.6.0.3.757.g1e5a4
Junio C Hamano· Oct 31, 2008, 08:15 UTC · re: Brian Gernhardt · lore

Re: [PATCH] Avoid using non-portable `echo -n` in tests.

Brian Gernhardt <benji@silverinsanity.com> writes:
> Not all /bin/sh have a builtin echo that recognizes -n.  Using printf
> is far more portable.

This is much better (minor nit: we do not care if echo is built-in or /bin/echo); the point of t4030 is to produce an incomplete line ;-).

Will queue, thanks.
Brian Gernhardt· Oct 31, 2008, 15:38 UTC · re: Junio C Hamano · lore

Re: [PATCH] Avoid using non-portable `echo -n` in tests.

On Oct 31, 2008, at 4:15 AM, Junio C Hamano wrote:
Show 7 quoted lines
> Brian Gernhardt <benji@silverinsanity.com> writes:
>
>> Not all /bin/sh have a builtin echo that recognizes -n.  Using printf
>> is far more portable.
>
> This is much better (minor nit: we do not care if echo is built-in or
> /bin/echo); the point of t4030 is to produce an incomplete line ;-).

Well, yes. The reason I phrased it that way was that my /bin/echo recognizes -n but /bin/sh does not.

~~ Brian
Junio C Hamano· Oct 31, 2008, 08:20 UTC · re: Brian Gernhardt · lore

Re: [PATCH] Avoid using non-portable `echo -n` in tests.

Brian Gernhardt <benji@silverinsanity.com> writes:
Show 5 quoted lines
> Not all /bin/sh have a builtin echo that recognizes -n.  Using printf
> is far more portable.
>
> Discovered on OS X 10.5.5 in t4030-diff-textconv.sh and changed in all
> the test scripts.
I had an impression that OS X was BSDish.  Wasn't "echo -n" a BSDism?
Steve Folly· Oct 31, 2008, 09:36 UTC · re: Junio C Hamano · lore

Re: [PATCH] Avoid using non-portable `echo-n` in tests.

Junio C Hamano <gitster <at> pobox.com> writes:
Show 11 quoted lines
> 
> Brian Gernhardt <benji <at> silverinsanity.com> writes:
> 
> > Not all /bin/sh have a builtin echo that recognizes -n.  Using printf
> > is far more portable.
> >
> > Discovered on OS X 10.5.5 in t4030-diff-textconv.sh and changed in all
> > the test scripts.
> 
> I had an impression that OS X was BSDish.  Wasn't "echo -n" a BSDism?
> 
FYI, "man echo" on OS X 10.5.5 has this to say...
     The following option is available:
     -n    Do not print the trailing newline character.  This may also be
           achieved by appending `\c' to the end of the string, as is done by
           iBCS2 compatible systems.  Note that this option as well as the
           effect of `\c' are implementation-defined in IEEE Std 1003.1-2001
           (``POSIX.1'') as amended by Cor. 1-2002.  Applications aiming for
           maximum portability are strongly encouraged to use printf(1) to
           suppress the newline character.
     Some shells may provide a builtin echo command which is similar or iden-
     tical to this utility.  Most notably, the builtin echo in sh(1) does not
     accept the -n option.  Consult the builtin(1) manual page.

Regards, Steve.

Shawn O. Pearce· Oct 31, 2008, 14:32 UTC · re: Brian Gernhardt · lore

Re: [PATCH] Avoid using non-portable `echo -n` in tests.

Brian Gernhardt <benji@silverinsanity.com> wrote:
> Not all /bin/sh have a builtin echo that recognizes -n.  Using printf
> is far more portable.
Show 10 quoted lines
> diff --git a/t/t9400-git-cvsserver-server.sh b/t/t9400-git-cvsserver-server.sh
> index c1850d2..f6a2dbd 100755
> --- a/t/t9400-git-cvsserver-server.sh
> +++ b/t/t9400-git-cvsserver-server.sh
> @@ -424,7 +424,7 @@ cd "$WORKDIR"
>  test_expect_success 'cvs update (-p)' '
>      touch really-empty &&
>      echo Line 1 > no-lf &&
> -    echo -n Line 2 >> no-lf &&
> +    printf Line 2 >> no-lf &&
That needs to be:
	printf 'Line 2'

to have the same result. Fortunately I don't think it matters in this test, but it does read odd.

-- 
Shawn.
Jeff King· Oct 31, 2008, 18:24 UTC · re: Brian Gernhardt · lore

Re: [PATCH] Avoid using non-portable `echo -n` in tests.

On Fri, Oct 31, 2008 at 01:09:13AM -0400, Brian Gernhardt wrote:
Show 5 quoted lines
> Not all /bin/sh have a builtin echo that recognizes -n.  Using printf
> is far more portable.
> 
> Discovered on OS X 10.5.5 in t4030-diff-textconv.sh and changed in all
> the test scripts.

Hmph. I think this is a good patch, and there is precedent in the past (20fa04ea, 2aad957, 9754563). But I am surprised this was not caught by our recent autobuilding project.

However, it seems to work on FreeBSD (which makes it doubly weird that it is broken on OS X). On Solaris, the /bin/sh is so horribly broken that I have to use bash anyway. Commit 9754563 claims breakage on AIX, but it looks like Mike is doing the AIX builds with bash.

So I guess we just need an OS X autobuild. ;)
>  t/t4030-diff-textconv.sh           |    2 +-

And of course this one was me, but I blame JSixt, whose test I just mindlessly copied. ;)

-Peff
Pierre Habouzit· Oct 31, 2008, 18:36 UTC · re: Jeff King · lore

Re: [PATCH] Avoid using non-portable `echo -n` in tests.

On Fri, Oct 31, 2008 at 06:24:56PM +0000, Jeff King wrote:
Show 11 quoted lines
> On Fri, Oct 31, 2008 at 01:09:13AM -0400, Brian Gernhardt wrote:
> 
> > Not all /bin/sh have a builtin echo that recognizes -n.  Using printf
> > is far more portable.
> > 
> > Discovered on OS X 10.5.5 in t4030-diff-textconv.sh and changed in all
> > the test scripts.
> 
> Hmph. I think this is a good patch, and there is precedent in the past
> (20fa04ea, 2aad957, 9754563). But I am surprised this was not caught by
> our recent autobuilding project.

Set up a Debian autobuilder with dash as a /bin/sh (apt-get install dash, dpkg-reconfigure -plow dash and say 'yes'). You'll see those kind of problems arise immediately.

Dash is a POSIX compatible shell, with almost no extension added (in particular its echo has no -n option) which helps to find those kind of issues.

It would help detecting git shell scripts that use bashism as well.
-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Jeff King· Oct 31, 2008, 18:39 UTC · re: Pierre Habouzit · lore

Re: [PATCH] Avoid using non-portable `echo -n` in tests.

On Fri, Oct 31, 2008 at 07:36:01PM +0100, Pierre Habouzit wrote:
> Set up a Debian autobuilder with dash as a /bin/sh (apt-get install
> dash, dpkg-reconfigure -plow dash and say 'yes'). You'll see those kind
> of problems arise immediately.
I don't need to; my development box is Debian with dash as /bin/sh. :)
Show 5 quoted lines
> Dash is a POSIX compatible shell, with almost no extension added (in
> particular its echo has no -n option) which helps to find those kind of
> issues.
> 
> It would help detecting git shell scripts that use bashism as well.

Agreed, and actually I found such a bashism (test ==) last week (though of course it also broke on FreeBSD).

-Peff
Francis Galiegue· Oct 31, 2008, 19:35 UTC · re: Jeff King · lore

Re: [PATCH] Avoid using non-portable `echo -n` in tests.

Le Friday 31 October 2008 19:39:33 Jeff King, vous avez écrit : [...]

>
> Agreed, and actually I found such a bashism (test ==) last week (though
> of course it also broke on FreeBSD).
>
As for bash-isms, a hunt for $(...) also looks necessary...

$ grep -rl '\$([^)]\+)' $(find -type f)|wc -l 272

Unless I'm mistaken (and I probably am), the $(...) construct is bash-specific, isn't it?

-- 
Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 6 83 87 78 75
Tel : +33 (0) 1 78 94 55 52
fge@one2team.com
40 avenue Raymond Poincaré
75116 Paris
Johannes Schindelin· Oct 31, 2008, 22:53 UTC · re: Francis Galiegue · lore

Re: [PATCH] Avoid using non-portable `echo -n` in tests.

Hi,
On Fri, 31 Oct 2008, Francis Galiegue wrote:
> Unless I'm mistaken (and I probably am), the $(...) construct is 
> bash-specific, isn't it?

You're mistaken, Dscho

P.S.: http://www.opengroup.org/onlinepubs/000095399/utilities/xcu_chap02.html#tag_02_06_03

Mike Ralphson· Nov 3, 2008, 16:30 UTC · re: Jeff King · lore

Re: [PATCH] Avoid using non-portable `echo -n` in tests.

2008/10/31 Jeff King <peff@peff.net>:
Show 16 quoted lines
> On Fri, Oct 31, 2008 at 01:09:13AM -0400, Brian Gernhardt wrote:
>
>> Not all /bin/sh have a builtin echo that recognizes -n.  Using printf
>> is far more portable.
>>
>> Discovered on OS X 10.5.5 in t4030-diff-textconv.sh and changed in all
>> the test scripts.
>
> Hmph. I think this is a good patch, and there is precedent in the past
> (20fa04ea, 2aad957, 9754563). But I am surprised this was not caught by
> our recent autobuilding project.
>
> However, it seems to work on FreeBSD (which makes it doubly weird that
> it is broken on OS X). On Solaris, the /bin/sh is so horribly broken
> that I have to use bash anyway. Commit 9754563 claims breakage on AIX,
> but it looks like Mike is doing the AIX builds with bash.

I've just retested with AIX's standard sh (ksh) instead of bash and there were only two issues.

Oddly enough, one was with the (original) printfs in t4030-diff-textconv.sh. AIX seems to ship with a perfectly good printf, but if you install the GNU tools from the 'IBM toolbox for AIX' (and prepend them to your PATH) it replaces that printf with one which doesn't grok the "\\1\\n" syntax, but I think wants it to be "\\01\\n".

The other problem is the 'trap exit' one discussed here: http://thread.gmane.org/gmane.comp.version-control.git/92748/focus=92944

Mike

← back to recent threads