git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Work around ash "alternate value" expansion bug
@ 2009-04-18  8:47 Ben Jackson
  2009-04-18 18:30 ` Junio C Hamano
  2009-04-19  3:42 ` [PATCH v2] " Ben Jackson
  0 siblings, 2 replies; 4+ messages in thread
From: Ben Jackson @ 2009-04-18  8:47 UTC (permalink / raw)
  To: git; +Cc: nanako3, gitster, ben

Ash (used as /bin/sh on many distros) has a shell expansion bug
for the form ${var:+word word}.  The result is a single argument
"word word".  Work around by using ${var:+word} ${var:+word} or
equivalent.

Signed-off-by: Ben Jackson <ben@ben.com>
---

I found this by accident while testing another trivial git-am patch.
It was broken about a week ago in git-am.sh by f79d4c8a and one of the
test cases caught it on FreeBSD.

The other instance has been around longer and I found it by grepping.
I added a new testcase (none too exciting) which exposes the problem.
There are more instances of ${x:+alt} which don't have spaces which I
did not touch.

For the curious:

bash on linux:
	$ parent=ok
	$ echo ${parent:+-p $parent}
	-p ok
	$ for i in ${parent:+-p $parent} ; do echo .$i; done
	.-p
	.ok

ash (/bin/sh) on freebsd:
	$ parent=ok
	$ echo ${parent:+-p $parent}
	-p ok
	$ for i in ${parent:+-p $parent} ; do echo .$i; done
	.-p ok

This is probably a bug in ash.  It does expand ${foo:+*} into many words.

 git-am.sh                  |    2 +-
 git-submodule.sh           |    2 +-
 t/t7400-submodule-basic.sh |    8 ++++++++
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/git-am.sh b/git-am.sh
index bfc50c9..e539c60 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -570,7 +570,7 @@ do
 			GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
 			export GIT_COMMITTER_DATE
 		fi &&
-		git commit-tree $tree ${parent:+-p $parent} <"$dotest/final-commit"
+		git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
 	) &&
 	git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
 	stop_here $this
diff --git a/git-submodule.sh b/git-submodule.sh
index 7c2e060..bb3766d 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -204,7 +204,7 @@ cmd_add()
 	else
 
 		module_clone "$path" "$realrepo" || exit
-		(unset GIT_DIR; cd "$path" && git checkout -f -q ${branch:+-b "$branch" "origin/$branch"}) ||
+		(unset GIT_DIR; cd "$path" && git checkout -f -q ${branch:+-b} ${branch:+"$branch"} ${branch:+"origin/$branch"}) ||
 		die "Unable to checkout submodule '$path'"
 	fi
 
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index af690ec..3c05c27 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -64,6 +64,14 @@ test_expect_success 'submodule add' '
 	)
 '
 
+test_expect_success 'submodule add --branch' '
+	(
+		cd addtest &&
+		git submodule add -b initial "$submodurl" submod-branch &&
+		git submodule init
+	)
+'
+
 test_expect_success 'submodule add with ./ in path' '
 	(
 		cd addtest &&
-- 
1.6.0.1

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

* Re: [PATCH] Work around ash "alternate value" expansion bug
  2009-04-18  8:47 [PATCH] Work around ash "alternate value" expansion bug Ben Jackson
@ 2009-04-18 18:30 ` Junio C Hamano
  2009-04-19  3:42 ` [PATCH v2] " Ben Jackson
  1 sibling, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2009-04-18 18:30 UTC (permalink / raw)
  To: Ben Jackson; +Cc: git, nanako3, gitster

Ben Jackson <ben@ben.com> writes:

> Ash (used as /bin/sh on many distros) has a shell expansion bug
> for the form ${var:+word word}.  The result is a single argument
> "word word".  Work around by using ${var:+word} ${var:+word} or
> equivalent.
>
> Signed-off-by: Ben Jackson <ben@ben.com>
> ---
>
> I found this by accident while testing another trivial git-am patch.
> It was broken about a week ago in git-am.sh by f79d4c8a and one of the
> test cases caught it on FreeBSD.
>
> The other instance has been around longer and I found it by grepping.
> I added a new testcase (none too exciting) which exposes the problem.
> There are more instances of ${x:+alt} which don't have spaces which I
> did not touch.
>
> For the curious:
>
> bash on linux:
> 	$ parent=ok
> 	$ echo ${parent:+-p $parent}
> 	-p ok
> 	$ for i in ${parent:+-p $parent} ; do echo .$i; done
> 	.-p
> 	.ok
>
> ash (/bin/sh) on freebsd:
> 	$ parent=ok
> 	$ echo ${parent:+-p $parent}
> 	-p ok
> 	$ for i in ${parent:+-p $parent} ; do echo .$i; done
> 	.-p ok
>
> This is probably a bug in ash.  It does expand ${foo:+*} into many words.
>
>  git-am.sh                  |    2 +-
>  git-submodule.sh           |    2 +-
>  t/t7400-submodule-basic.sh |    8 ++++++++
>  3 files changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/git-am.sh b/git-am.sh
> index bfc50c9..e539c60 100755
> --- a/git-am.sh
> +++ b/git-am.sh
> @@ -570,7 +570,7 @@ do
>  			GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
>  			export GIT_COMMITTER_DATE
>  		fi &&
> -		git commit-tree $tree ${parent:+-p $parent} <"$dotest/final-commit"
> +		git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
>  	) &&
>  	git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
>  	stop_here $this

I do not mind this one, but

> diff --git a/git-submodule.sh b/git-submodule.sh
> index 7c2e060..bb3766d 100755
> --- a/git-submodule.sh
> +++ b/git-submodule.sh
> @@ -204,7 +204,7 @@ cmd_add()
>  	else
>  
>  		module_clone "$path" "$realrepo" || exit
> -		(unset GIT_DIR; cd "$path" && git checkout -f -q ${branch:+-b "$branch" "origin/$branch"}) ||
> +		(unset GIT_DIR; cd "$path" && git checkout -f -q ${branch:+-b} ${branch:+"$branch"} ${branch:+"origin/$branch"}) ||

this is too ugly to live in our codebase, and without an accompanying code
comment, I am sure somebody (if not myself) will "fix" it again.

	(
        	unset GIT_DIR
		cd "$path" &&
		# BSD ash mishandles ${branch:+-b "$branch" ...}, sheesh.
                case "$branch" in
                '')	git checkout -f -q ;;
                ?*)	git checkout -f -q -b "$branch" "origin/$branch" ;;
                esac
	) ||
	...

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

* [PATCH v2] Work around ash "alternate value" expansion bug
  2009-04-18  8:47 [PATCH] Work around ash "alternate value" expansion bug Ben Jackson
  2009-04-18 18:30 ` Junio C Hamano
@ 2009-04-19  3:42 ` Ben Jackson
  2009-04-19  4:38   ` Junio C Hamano
  1 sibling, 1 reply; 4+ messages in thread
From: Ben Jackson @ 2009-04-19  3:42 UTC (permalink / raw)
  To: git; +Cc: gitster, Ben Jackson

Ash (used as /bin/sh on many distros) has a shell expansion bug
for the form ${var:+word word}.  The result is a single argument
"word word".  Work around by using ${var:+word} ${var:+word} or
equivalent.

Signed-off-by: Ben Jackson <ben@ben.com>
---

See http://thread.gmane.org/gmane.comp.version-control.git/116816 for
the original notes which describe the problem in more detail.

This version uses a different workaround suggested by Junio for
git-submodule which is less likely to be "cleaned up" back to the
original problem.  Since new the change is more complex I beefed
up the new test slightly to ensure we are getting into the right case.

 git-am.sh                  |    2 +-
 git-submodule.sh           |   11 +++++++++--
 t/t7400-submodule-basic.sh |   10 ++++++++++
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/git-am.sh b/git-am.sh
index bfc50c9..e539c60 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -570,7 +570,7 @@ do
 			GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
 			export GIT_COMMITTER_DATE
 		fi &&
-		git commit-tree $tree ${parent:+-p $parent} <"$dotest/final-commit"
+		git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
 	) &&
 	git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
 	stop_here $this
diff --git a/git-submodule.sh b/git-submodule.sh
index 7c2e060..8e234a4 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -204,8 +204,15 @@ cmd_add()
 	else
 
 		module_clone "$path" "$realrepo" || exit
-		(unset GIT_DIR; cd "$path" && git checkout -f -q ${branch:+-b "$branch" "origin/$branch"}) ||
-		die "Unable to checkout submodule '$path'"
+		(
+			unset GIT_DIR
+			cd "$path" &&
+			# ash fails to wordsplit ${branch:+-b "$branch"...}
+			case "$branch" in
+			'') git checkout -f -q ;;
+			?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
+			esac
+		) || die "Unable to checkout submodule '$path'"
 	fi
 
 	git add "$path" ||
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index af690ec..0f2ccc6 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -64,6 +64,16 @@ test_expect_success 'submodule add' '
 	)
 '
 
+test_expect_success 'submodule add --branch' '
+	(
+		cd addtest &&
+		git submodule add -b initial "$submodurl" submod-branch &&
+		git submodule init &&
+		cd submod-branch &&
+		git branch | grep initial
+	)
+'
+
 test_expect_success 'submodule add with ./ in path' '
 	(
 		cd addtest &&
-- 
1.6.0.1

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

* Re: [PATCH v2] Work around ash "alternate value" expansion bug
  2009-04-19  3:42 ` [PATCH v2] " Ben Jackson
@ 2009-04-19  4:38   ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2009-04-19  4:38 UTC (permalink / raw)
  To: Ben Jackson; +Cc: git

Thanks, looks good.

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

end of thread, other threads:[~2009-04-19  4:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-18  8:47 [PATCH] Work around ash "alternate value" expansion bug Ben Jackson
2009-04-18 18:30 ` Junio C Hamano
2009-04-19  3:42 ` [PATCH v2] " Ben Jackson
2009-04-19  4:38   ` Junio C Hamano

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).