# [PATCH] t1301-shared-repo.sh: fix 'stat' portability issue

5 messages from 2007-08-16 to 2007-08-17. Participants: Arjen Laarhoven, Junio C Hamano, Uwe Kleine-König.
Thread: https://gitlist.dev/t/9557

## Arjen Laarhoven, 2007-08-16 15:21

Subject: [PATCH] t1301-shared-repo.sh: fix 'stat' portability issue
Message-ID: <1187277663740-git-send-email-arjen@yaph.org>
URL: https://gitlist.dev/e/1187277663740-git-send-email-arjen%40yaph.org

```
The t1301-shared-repo.sh testscript uses /usr/bin/stat to get the file
mode, which isn't portable.  There already is a dependency on Perl, so
use a Perl one-liner to do the file mode test, but portable.

Signed-off-by: Arjen Laarhoven <arjen@yaph.org>
---
 t/t1301-shared-repo.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/t/t1301-shared-repo.sh b/t/t1301-shared-repo.sh
index bb5f302..888c5fb 100755
--- a/t/t1301-shared-repo.sh
+++ b/t/t1301-shared-repo.sh
@@ -21,7 +21,7 @@ test_expect_success 'update-server-info honors core.sharedRepository' '
 	git commit -m a1 &&
 	umask 0277 &&
 	git update-server-info &&
-	test 444 = $(stat -c %a .git/info/refs)
+	$(perl -e '\''exit !(((stat ".git/info/refs")[2] & 0777) == 0444)'\'')
 '
 
 test_done
-- 
1.5.3.rc4.67.gf9286

```

## Junio C Hamano, 2007-08-16 19:09

Subject: Re: [PATCH] t1301-shared-repo.sh: fix 'stat' portability issue
Message-ID: <7v3ayjjnz7.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v3ayjjnz7.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1187277663740-git-send-email-arjen@yaph.org>

```
Arjen Laarhoven <arjen@yaph.org> writes:

> The t1301-shared-repo.sh testscript uses /usr/bin/stat to get the file
> mode, which isn't portable.  There already is a dependency on Perl, so
> use a Perl one-liner to do the file mode test, but portable.
>
> Signed-off-by: Arjen Laarhoven <arjen@yaph.org>
> ---
>  t/t1301-shared-repo.sh |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
>
> diff --git a/t/t1301-shared-repo.sh b/t/t1301-shared-repo.sh
> index bb5f302..888c5fb 100755
> --- a/t/t1301-shared-repo.sh
> +++ b/t/t1301-shared-repo.sh
> @@ -21,7 +21,7 @@ test_expect_success 'update-server-info honors core.sharedRepository' '
>  	git commit -m a1 &&
>  	umask 0277 &&
>  	git update-server-info &&
> -	test 444 = $(stat -c %a .git/info/refs)
> +	$(perl -e '\''exit !(((stat ".git/info/refs")[2] & 0777) == 0444)'\'')
>  '

Why is this inside a $()?

I am just wondering if this is more portable and readable...

	... &&
	current="$(ls -l .git/info/refs)" &&
	case "$current" in
        -r--r--r--*)
        	: happy
                ;;
	*)
        	echo Oops, .git/info/refs is not 0444
                false
                ;;
	esac

```

## Arjen Laarhoven, 2007-08-16 22:02

Subject: Re: [PATCH] t1301-shared-repo.sh: fix 'stat' portability issue
Message-ID: <20070816220217.GH25161@regex.yaph.org>
URL: https://gitlist.dev/e/20070816220217.GH25161%40regex.yaph.org
In-Reply-To: <7v3ayjjnz7.fsf@gitster.siamese.dyndns.org>

```
The t1301-shared-repo.sh testscript uses /usr/bin/stat to get the file
mode, which isn't portable.  Implement the test in shell using 'ls' as
shown by Junio.

Signed-off-by: Arjen Laarhoven <arjen@yaph.org>
---
 t/t1301-shared-repo.sh |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)

diff --git a/t/t1301-shared-repo.sh b/t/t1301-shared-repo.sh
index bb5f302..6bfe19a 100755
--- a/t/t1301-shared-repo.sh
+++ b/t/t1301-shared-repo.sh
@@ -21,7 +21,16 @@ test_expect_success 'update-server-info honors core.sharedRepository' '
 	git commit -m a1 &&
 	umask 0277 &&
 	git update-server-info &&
-	test 444 = $(stat -c %a .git/info/refs)
+	actual="$(ls -l .git/info/refs)" &&
+	case "$actual" in
+	-r--r--r--*)
+		: happy
+		;;
+	*)
+		echo Oops, .git/info/refs is not 0444
+		false
+		;;
+	esac
 '
 
 test_done
-- 
1.5.3.rc4.67.gf9286

> > -	test 444 = $(stat -c %a .git/info/refs)
> > +	$(perl -e '\''exit !(((stat ".git/info/refs")[2] & 0777) == 0444)'\'')
> >  '
> 
> Why is this inside a $()?

Bah.

> I am just wondering if this is more portable and readable...
> 
> 	... &&
> 	current="$(ls -l .git/info/refs)" &&
> 	case "$current" in
>         -r--r--r--*)
>         	: happy
>                 ;;
> 	*)
>         	echo Oops, .git/info/refs is not 0444
>                 false
>                 ;;
> 	esac

```

## Uwe Kleine-König, 2007-08-17 12:48

Subject: Re: [PATCH] t1301-shared-repo.sh: fix 'stat' portability issue
Message-ID: <20070817124828.GB11107@informatik.uni-freiburg.de>
URL: https://gitlist.dev/e/20070817124828.GB11107%40informatik.uni-freiburg.de
In-Reply-To: <20070816220217.GH25161@regex.yaph.org>

```
Hello,

> +	actual="$(ls -l .git/info/refs)" &&
> +	case "$actual" in
> +	-r--r--r--*)
> +		: happy
> +		;;
> +	*)
> +		echo Oops, .git/info/refs is not 0444
> +		false
> +		;;
> +	esac

Don't know if this matters here, but a while ago I learnd that if I
really need to parse output of ls -l, then I should set LANG=C and
LC_ALL=C.  I didn't see any breakage when LS_COLORS is set, but maybe
you want to reset that, too?

Best regards
Uwe

-- 
Uwe Kleine-König

http://www.google.com/search?q=2004+in+roman+numerals

```

## Junio C Hamano, 2007-08-17 23:37

Subject: Re: [PATCH] t1301-shared-repo.sh: fix 'stat' portability issue
Message-ID: <7v4pixg2az.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v4pixg2az.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <20070817124828.GB11107@informatik.uni-freiburg.de>

```
Uwe Kleine-König <ukleinek@informatik.uni-freiburg.de> writes:

> Don't know if this matters here, but a while ago I learnd that if I
> really need to parse output of ls -l, then I should set LANG=C and
> LC_ALL=C.  I didn't see any breakage when LS_COLORS is set, but maybe
> you want to reset that, too?

Yeah, I usually try to be defensive and I know LANG/LC_ALL
matters if you want to parse "ls -l" for dates, but it should
not matter for perm bits.  If LS_COLORS is honoured when it is
outputting to non-terminal (as in var=`ls`), it's severely
broken, so...

```
