Skip to contentSkip to conversation

subtree split prepends "-n<newline>" to commit messages on OS X

PATCH In review Bryan Head 3 participants 3 messages Jun 15, 2016

lore
Share & tools
lore

Bryan Head

author
#0 lore raw

Looks like this was introduced in 1.8.3.3.

To reproduce, run

git init repo
cd repo
mkdir splitme
touch splitme/foo
git add splitme/
git commit -m 'Add foo'
git subtree split -P splitme -b splitme-only

After that, I get:

$ git log splitme-only
commit 6ce8124a0b5e52d4bba198144d2f3f664d7b19e7
Author: me
Date:   Fri Jul 26 12:22:27 2013 -0500
    -n
    Add foo

Compared with the original:

$ git log master
commit 6d5164076bd88d1dab8963d91ec013372e58a444
Author: me
Date:   Fri Jul 26 12:22:27 2013 -0500
    Add foo

Notice how -n<newline> has been prepended to the commit message.

This was introduced when subtree was changed to use sh instead of
bash, in this commit:https://github.com/git/git/commit/6912ea952bf5d1b2d21d5935d6b726bab02d8bf6#contrib/subtree/git-subtree.sh This was merged in here:
https://github.com/git/git/commit/779fd737d79a3e19a1aa420c33cf1195c7e20dd7#contrib/subtree/git-subtree.sh

I verified that changing the line in question back to #!/bin/bash eliminates the problem.

I believe that it was caused by the fact that sh echos the "-n" in
this line: https://github.com/git/git/blob/master/contrib/subtree/git-subtree.sh#L314

Note that this consequently happens when, for instance, using git subtree push to push the subtree to an upstream repository.

I'm using OS X 10.8.4. The problem does not occur on Ubuntu at least since Ubuntu's sh is actually dash.

#1 lore raw

PATCH 1/1 Avoid using `echo -n` anywhere

echo -n is non-portable. The POSIX specification says:

Conforming applications that wish to do prompting without <newline> characters or that could possibly be expecting to echo a -n, should use the printf utility derived from the Ninth Edition system.

Since all of the affected shell scripts use a POSIX shell shebang, replace echo -n invocations with printf.

---
 contrib/subtree/git-subtree.sh     | 2 +-
 contrib/subtree/t/t7900-subtree.sh | 8 ++++----
 t/perf/perf-lib.sh                 | 4 ++--
 3 files changed, 7 insertions(+), 7 deletions(-)
contrib/subtree/git-subtree.sh +1 -1
index 51ae932..7d7af03 100755
@@ -311,7 +311,7 @@ copy_commit()
 			GIT_COMMITTER_NAME \
 			GIT_COMMITTER_EMAIL \
 			GIT_COMMITTER_DATE
-		(echo -n "$annotate"; cat ) |
+		(printf "%s" "$annotate"; cat ) |
 		git commit-tree "$2" $3  # reads the rest of stdin
 	) || die "Can't copy commit $1"
 }
contrib/subtree/t/t7900-subtree.sh +4 -4
index b0f8536..556a94d 100755
@@ -182,9 +182,9 @@ test_expect_success 'merge new subproj history into subdir' '
 test_expect_success 'Check that prefix argument is required for split' '
         echo "You must provide the --prefix option." > expected &&
         test_must_fail git subtree split > actual 2>&1 &&
-        test_debug "echo -n expected: " &&
+        test_debug "printf '"'"'expected: '"'"'" &&
         test_debug "cat expected" &&
-        test_debug "echo -n actual: " &&
+        test_debug "printf '"'"'actual: '"'"'" &&
         test_debug "cat actual" &&
         test_cmp expected actual &&
         rm -f expected actual
@@ -193,9 +193,9 @@ test_expect_success 'Check that prefix argument is required for split' '
 test_expect_success 'Check that the <prefix> exists for a split' '
         echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
         test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
-        test_debug "echo -n expected: " &&
+        test_debug "printf '"'"'expected: '"'"'" &&
         test_debug "cat expected" &&
-        test_debug "echo -n actual: " &&
+        test_debug "printf '"'"'actual: '"'"'" &&
         test_debug "cat actual" &&
         test_cmp expected actual
 #        rm -f expected actual
t/perf/perf-lib.sh +2 -2
index c61d535..ae44117 100644
@@ -161,7 +161,7 @@ test_perf () {
 		echo "$test_count" >>"$perf_results_dir"/$base.subtests
 		echo "$1" >"$perf_results_dir"/$base.$test_count.descr
 		if test -z "$verbose"; then
-			echo -n "perf $test_count - $1:"
+			printf "%s" "perf $test_count - $1:"
 		else
 			echo "perf $test_count - $1:"
 		fi
@@ -170,7 +170,7 @@ test_perf () {
 			if test_run_perf_ "$2"
 			then
 				if test -z "$verbose"; then
-					echo -n " $i"
+					printf " %d" "$i"
 				else
 					echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:"
 				fi
Signature · 1 line
1.8.3.3.1135.ge2c9e63
#2 lore raw

Re: [PATCH] Avoid using `echo -n` anywhere

Quoted 25 lines · Lukas Fleischer

echo -n is non-portable. The POSIX specification says:

Conforming applications that wish to do prompting without <newline> characters or that could possibly be expecting to echo a -n, should use the printf utility derived from the Ninth Edition system.

Since all of the affected shell scripts use a POSIX shell shebang, replace echo -n invocations with printf.

...

t/perf/perf-lib.sh +1 -1
index c61d535..ae44117 100644
@@ -161,7 +161,7 @@ test_perf () {
 		echo "$test_count" >>"$perf_results_dir"/$base.subtests
 		echo "$1" >"$perf_results_dir"/$base.$test_count.descr
 		if test -z "$verbose"; then
-			echo -n "perf $test_count - $1:"
+			printf "%s" "perf $test_count - $1:"
 		else
 			echo "perf $test_count - $1:"
 		fi

t/perf/perf-lib.sh test_perf () { · line 173

Quoted 5 lines
t/perf/perf-lib.sh +1 -1
@@ -170,7 +170,7 @@ test_perf () {
 			if test_run_perf_ "$2"
 			then
 				if test -z "$verbose"; then
-					echo -n " $i"
+					printf " %d" "$i"

I'd prefer to see '%s' here; it is more faithful transliteration from "echo -n".

Thanks, will queue with the above tweak.

Quoted 3 lines
t/perf/perf-lib.sh +0 -0
 				else
 					echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:"
 				fi