Subject: Re: [PATCH] git-submodule.sh: avoid "test <cond> -a/-o <cond>"

From: Junio C Hamano <[email-masked]>
Date: Tue, 10 Jun 2014 08:32:57 -0700

Junio C Hamano <[email-masked]> writes:

> Torsten Bögershausen <[email-masked]> writes:
>
>> On 2014-06-10 14.28, Elia Pinto wrote:
>> []
>>>  		# before the first commit: compare with an empty tree
>>>  		head=$(git hash-object -w -t tree --stdin </dev/null)
>>> @@ -1056,13 +1056,17 @@ cmd_summary() {
>>>  		while read mod_src mod_dst sha1_src sha1_dst status sm_path
>>>  		do
>>>  			# Always show modules deleted or type-changed (blob<->module)
>>> -			test $status = D -o $status = T && echo "$sm_path" && continue
>>> +			case "$status" in
>>> +			([DT])
>> Does this look strange? ^
>> Should it be
>> case "$status" in
>> D|T)
>
> Actually POSIX allows matching parentheses for case arm labels
> (surprise!).
>
> And some shells misparse
>
> 	var=$( ... case arm) action ;; esac ... )
>
> as if the ')' after the arm label closes the whole command
> substitution.
>
> Having said that, I'd prefer to see the following squashed into that
> patch.
> ...
> I would also have preferred to see the echo to printf substitution
> left out of this patch.  There are other places where $sm_path is
> echoed and fixing only one of them in an otherwise unrelated patch
> feels wrong---it should be a separate follow-up patch, I would
> think.

... which may look like this (after removing s/echo/printf/ in that
hunk from this "test -a/-o" patch).

 git-submodule.sh | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/git-submodule.sh b/git-submodule.sh
index d0d9b58..9245abf 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -235,7 +235,7 @@ module_name()
 		sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
 	test -z "$name" &&
 	die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
-	echo "$name"
+	printf '%s\n' "$name"
 }
 
 #
@@ -305,10 +305,10 @@ module_clone()
 	b=${b%/}
 
 	# Turn each leading "*/" component into "../"
-	rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
-	echo "gitdir: $rel/$a" >"$sm_path/.git"
+	rel=$(printf '%s\n' "$b" | sed -e 's|[^/][^/]*|..|g')
+	printf '%s\n' "gitdir: $rel/$a" >"$sm_path/.git"
 
-	rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
+	rel=$(printf '%s\n' "$a" | sed -e 's|[^/][^/]*|..|g')
 	(clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
 }
 
@@ -389,7 +389,7 @@ cmd_add()
 	sm_path=$2
 
 	if test -z "$sm_path"; then
-		sm_path=$(echo "$repo" |
+		sm_path=$(printf '%s\n' "$repo" |
 			sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 	fi
 
@@ -1058,7 +1058,7 @@ cmd_summary() {
 			# Always show modules deleted or type-changed (blob<->module)
 			if test "$status" = D || test "$status" = T
 			then
-				echo "$sm_path"
+				printf '%s\n' "$sm_path"
 				continue
 			fi
 			# Respect the ignore setting for --for-status.
@@ -1070,7 +1070,7 @@ cmd_summary() {
 			fi
 			# Also show added or modified modules which are checked out
 			GIT_DIR="$sm_path/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
-			echo "$sm_path"
+			printf '%s\n' "$sm_path"
 		done
 	)
 
@@ -1311,7 +1311,7 @@ cmd_sync()
 		./*|../*)
 			# rewrite foo/bar as ../.. to find path from
 			# submodule work tree to superproject work tree
-			up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
+			up_path="$(printf '%s\n' "$sm_path" | sed "s/[^/][^/]*/../g")" &&
 			# guarantee a trailing /
 			up_path=${up_path%/}/ &&
 			# path from submodule work tree to submodule origin repo