Skip to contentSkip to conversation

contrib/subtree: fix linefeeds trimming for cmd_split()

PATCH New Danny Lin 3 participants 10 messages Jun 15, 2016

lore
Share & tools
lore

Danny Lin

author
#0 lore raw

Replace all echo using printf for better portability.

Also re-wrap previous 'say -n "$str<CR>"' using a new function state() so to prevent CR chars included in the source code, which could be mal-processed on some shells (e.g. MsysGit trims CR before executing a shell script file in order to make it work right on Windows even if it uses CRLF as linefeeds.

---
 contrib/subtree/git-subtree.sh | 56 +++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 22 deletions(-)
contrib/subtree/git-subtree.sh +1 -1
index fa1a583..2da1433 100755
@@ -29,7 +29,7 @@ rejoin        merge the new branch back into HEAD
  options for 'add', 'merge', 'pull' and 'push'
 squash        merge subtree changes as a single commit
 "
-eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
+eval "$(printf %s "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" ||

printf %s "exit $?")"

PATH=$PATH:$(git --exec-path)
. git-sh-setup
@@ -51,17 +51,29 @@ prefix=
debug()

 {
     if [ -n "$debug" ]; then

- echo "$@" >&2
+ printf "%s\n" "$*" >&2
fi
}

say()

 {
     if [ -z "$quiet" ]; then

- echo "$@" >&2
+ printf "%s\n" "$*" >&2
fi
}

+state()
+{
+ if [ -z "$quiet" ]; then
+ printf "%s\r" "$*" >&2
+ fi
+}
+
+log()
+{
+ printf "%s\n" "$*"
+}
+
assert()

 {
     if "$@"; then

@@ -72,7 +84,7 @@ assert()
}

-#echo "Options: $*"
+#log "Options: $*"

while [ $# -gt 0 ]; do

     opt="$1"

@@ -149,7 +161,7 @@ cache_get()
for oldrev in $*; do
if [ -r "$cachedir/$oldrev" ]; then
read newrev <"$cachedir/$oldrev"
- echo $newrev
+ log $newrev
fi
done
}
@@ -158,7 +170,7 @@ cache_miss()
{
for oldrev in $*; do
if [ ! -r "$cachedir/$oldrev" ]; then
- echo $oldrev
+ log $oldrev
fi
done
}
@@ -175,7 +187,7 @@ check_parents()

set_notree()
{
- echo "1" > "$cachedir/notree/$1"
+ log "1" > "$cachedir/notree/$1"
}

cache_set()
@@ -187,7 +199,7 @@ cache_set()
-a -e "$cachedir/$oldrev" ]; then
die "cache for $oldrev already exists!"
fi
- echo "$newrev" >"$cachedir/$oldrev"
+ log "$newrev" >"$cachedir/$oldrev"
}

rev_exists()
@@ -219,7 +231,7 @@ rev_is_descendant_of_branch()
try_remove_previous()

 {
     if rev_exists "$1^"; then

- echo "^$1^"
+ log "^$1^"
fi
}

@@ -247,7 +259,7 @@ find_latest_squash()
sq="$sub"
fi
debug "Squash found: $sq $sub"
- echo "$sq" "$sub"
+ log "$sq" "$sub"

                     break
                 fi
                 sq=
@@ -339,9 +351,9 @@ add_msg()
 add_squashed_msg()
 {
     if [ -n "$message" ]; then

- echo "$message"
+ log "$message"
else
- echo "Merge commit '$1' as '$2'"
+ log "Merge commit '$1' as '$2'"
fi
}

@@ -373,17 +385,17 @@ squash_msg()

     if [ -n "$oldsub" ]; then
         oldsub_short=$(git rev-parse --short "$oldsub")
-        echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
+        log "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
         echo
         git log --pretty=tformat:'%h %s' "$oldsub..$newsub"
         git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
     else

- echo "Squashed '$dir/' content from commit $newsub_short"
+ log "Squashed '$dir/' content from commit $newsub_short"
fi

echo

-    echo "git-subtree-dir: $dir"
-    echo "git-subtree-split: $newsub"
+    log "git-subtree-dir: $dir"
+    log "git-subtree-split: $newsub"

}

toptree_for_commit()
@@ -401,7 +413,7 @@ subtree_for_commit()

         assert [ "$name" = "$dir" ]
         assert [ "$type" = "tree" -o "$type" = "commit" ]
         [ "$type" = "commit" ] && continue  # ignore submodules

- echo $tree
+ log $tree
break
done
}
@@ -474,7 +486,7 @@ copy_or_skip()
done

if [ -n "$identical" ]; then
- echo $identical
+ log $identical

     else
         copy_commit $rev $tree "$p" || exit $?
     fi

@@ -526,7 +538,7 @@ cmd_add()

cmd_add_repository()
{
- echo "git fetch" "$@"
+ log "git fetch" "$@"

     repository=$1
     refspec=$2
     git fetch "$@" || exit $?
@@ -599,7 +611,7 @@ cmd_split()
     eval "$grl" |
     while read rev parents; do
         revcount=$(($revcount + 1))
-        say -n "$revcount/$revmax ($createcount)
"
+        state "$revcount/$revmax ($createcount)"
         debug "Processing commit: $rev"
         exists=$(cache_get $rev)
         if [ -n "$exists" ]; then

@@ -656,7 +668,7 @@ cmd_split()
git update-ref -m 'subtree split' "refs/heads/$branch"
$latest_new || exit $?
say "$action branch '$branch'"
fi
- echo $latest_new
+ log $latest_new
exit 0
}

@@ -726,7 +738,7 @@ cmd_push()

     if [ -e "$dir" ]; then
         repository=$1
         refspec=$2

- echo "git push using: " $repository $refspec
+ log "git push using: " $repository $refspec
localrev=$(git subtree split --prefix="$prefix") || die git push $repository $localrev:refs/heads/$refspec
else

Signature · 5 lines
2.3.7.windows.1



2015-05-07 3:58 GMT+08:00 Eric Sunshine <[email protected]>:
Quoted 8 lines · Junio C Hamano
Quoted 6 lines
Quoted 4 lines · Danny Lin

cmd_split() prints a CR char by assigning a variable with a literal CR in the source code, which could be trimmed or mis-processed in some terminals. Replace with $(printf '\r') to fix it.

Quoted 6 lines

For future readers of the patch who haven't followed the email discussion, it might be a good idea to explain the problem in more detail. Saying merely "could be trimmed or mis-processed in some terminals" doesn't give much for people to latch onto if they want to understand the specific problem. Concrete information would help.

Added related information.

Quoted 24 lines
Quoted 24 lines
Quoted 15 lines

---

contrib/subtree/git-subtree.sh +1 -1
index fa1a583..3a581fc 100755
@@ -596,10 +596,11 @@ cmd_split()
     revmax=$(eval "$grl" | wc -l)
     revcount=0
     createcount=0
+    CR=$(printf '\r')
     eval "$grl" |
     while read rev parents; do
         revcount=$(($revcount + 1))
-        say -n "$revcount/$revmax ($createcount)
Quoted 2 lines

"
+ say -n "$revcount/$revmax ($createcount)$CR"

Interesting. I would have expected, especially this is a portability-fix change, that the change would be a single liner

(quoted diff) +1 -1
-       say -n ...
+       printf "%s\r" "$revcount/$revmax ($createcount)"
Quoted 8 lines
Quoted 1 line

that does not touch any other line.

Unfortunately, that solution does not respect the $quiet flag like say() does. I had envisioned the patch as reimplementing say() using printf rather than echo, and having say() itself either recognizing the -n flag or just update callers to specify \n when they want it (which is probably the cleaner of the two approaches).

If a more thorough portability fix is desired, I'd prefer a work like this (see the patch above).

author
#1 lore raw

Subject: [PATCH] contrib/subtree: portability fix for string printing

Replace all echo using printf for better portability.

Also re-wrap previous 'say -n "$str<CR>"' using a new function state() to prevent CR chars included in the source code, which could be mal-processed on some shells. For example, MsysGit trims CR before executing a shell script file in order to make it work right on Windows even if it uses CRLF as linefeeds.

---
 contrib/subtree/git-subtree.sh | 56 +++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 22 deletions(-)
contrib/subtree/git-subtree.sh +1 -1
index fa1a583..2da1433 100755
@@ -29,7 +29,7 @@ rejoin        merge the new branch back into HEAD
  options for 'add', 'merge', 'pull' and 'push'
 squash        merge subtree changes as a single commit
 "
-eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
+eval "$(printf %s "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" ||

printf %s "exit $?")"

PATH=$PATH:$(git --exec-path)
. git-sh-setup
@@ -51,17 +51,29 @@ prefix=
debug()

 {
     if [ -n "$debug" ]; then

- echo "$@" >&2
+ printf "%s\n" "$*" >&2
fi
}

say()

 {
     if [ -z "$quiet" ]; then

- echo "$@" >&2
+ printf "%s\n" "$*" >&2
fi
}

+state()
+{
+ if [ -z "$quiet" ]; then
+ printf "%s\r" "$*" >&2
+ fi
+}
+
+log()
+{
+ printf "%s\n" "$*"
+}
+
assert()

 {
     if "$@"; then

@@ -72,7 +84,7 @@ assert()
}

-#echo "Options: $*"
+#log "Options: $*"

while [ $# -gt 0 ]; do

     opt="$1"

@@ -149,7 +161,7 @@ cache_get()
for oldrev in $*; do
if [ -r "$cachedir/$oldrev" ]; then
read newrev <"$cachedir/$oldrev"
- echo $newrev
+ log $newrev
fi
done
}
@@ -158,7 +170,7 @@ cache_miss()
{
for oldrev in $*; do
if [ ! -r "$cachedir/$oldrev" ]; then
- echo $oldrev
+ log $oldrev
fi
done
}
@@ -175,7 +187,7 @@ check_parents()

set_notree()
{
- echo "1" > "$cachedir/notree/$1"
+ log "1" > "$cachedir/notree/$1"
}

cache_set()
@@ -187,7 +199,7 @@ cache_set()
-a -e "$cachedir/$oldrev" ]; then
die "cache for $oldrev already exists!"
fi
- echo "$newrev" >"$cachedir/$oldrev"
+ log "$newrev" >"$cachedir/$oldrev"
}

rev_exists()
@@ -219,7 +231,7 @@ rev_is_descendant_of_branch()
try_remove_previous()

 {
     if rev_exists "$1^"; then

- echo "^$1^"
+ log "^$1^"
fi
}

@@ -247,7 +259,7 @@ find_latest_squash()
sq="$sub"
fi
debug "Squash found: $sq $sub"
- echo "$sq" "$sub"
+ log "$sq" "$sub"

                     break
                 fi
                 sq=
@@ -339,9 +351,9 @@ add_msg()
 add_squashed_msg()
 {
     if [ -n "$message" ]; then

- echo "$message"
+ log "$message"
else
- echo "Merge commit '$1' as '$2'"
+ log "Merge commit '$1' as '$2'"
fi
}

@@ -373,17 +385,17 @@ squash_msg()

     if [ -n "$oldsub" ]; then
         oldsub_short=$(git rev-parse --short "$oldsub")
-        echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
+        log "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
         echo
         git log --pretty=tformat:'%h %s' "$oldsub..$newsub"
         git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
     else

- echo "Squashed '$dir/' content from commit $newsub_short"
+ log "Squashed '$dir/' content from commit $newsub_short"
fi

echo

-    echo "git-subtree-dir: $dir"
-    echo "git-subtree-split: $newsub"
+    log "git-subtree-dir: $dir"
+    log "git-subtree-split: $newsub"

}

toptree_for_commit()
@@ -401,7 +413,7 @@ subtree_for_commit()

         assert [ "$name" = "$dir" ]
         assert [ "$type" = "tree" -o "$type" = "commit" ]
         [ "$type" = "commit" ] && continue  # ignore submodules

- echo $tree
+ log $tree
break
done
}
@@ -474,7 +486,7 @@ copy_or_skip()
done

if [ -n "$identical" ]; then
- echo $identical
+ log $identical

     else
         copy_commit $rev $tree "$p" || exit $?
     fi

@@ -526,7 +538,7 @@ cmd_add()

cmd_add_repository()
{
- echo "git fetch" "$@"
+ log "git fetch" "$@"

     repository=$1
     refspec=$2
     git fetch "$@" || exit $?
@@ -599,7 +611,7 @@ cmd_split()
     eval "$grl" |
     while read rev parents; do
         revcount=$(($revcount + 1))
-        say -n "$revcount/$revmax ($createcount)
"
+        state "$revcount/$revmax ($createcount)"
         debug "Processing commit: $rev"
         exists=$(cache_get $rev)
         if [ -n "$exists" ]; then

@@ -656,7 +668,7 @@ cmd_split()
git update-ref -m 'subtree split' "refs/heads/$branch"
$latest_new || exit $?
say "$action branch '$branch'"
fi
- echo $latest_new
+ log $latest_new
exit 0
}

@@ -726,7 +738,7 @@ cmd_push()

     if [ -e "$dir" ]; then
         repository=$1
         refspec=$2

- echo "git push using: " $repository $refspec
+ log "git push using: " $repository $refspec
localrev=$(git subtree split --prefix="$prefix") || die git push $repository $localrev:refs/heads/$refspec
else

Signature · 4 lines
2.3.7.windows.1


Typo fix for previous patch.
author
#2 lore raw

I'm sorry that I cannot get git send-email work currently. I'd submit the updated patch with an attachment until it's working right. ><

From f36d4eab24565894cfe3abba0131f01668a5934b Mon Sep 17 00:00:00 2001
From: Danny Lin <[email protected]>
Date: Thu, 7 May 2015 10:51:31 +0800
Subject: [PATCH] contrib/subtree: portability fix for string printing

Replace all echo using printf for better portability.

Also re-wrap previous 'say -n "$str<CR>"' using a new function state() to prevent CR chars included in the source code, which could be mal-processed in some shells. For example, MsysGit trims CR before executing a shell script file in order to make it work right on Windows even if it uses CRLF as linefeeds.

---
 contrib/subtree/git-subtree.sh | 56 +++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 22 deletions(-)
contrib/subtree/git-subtree.sh +34 -22
index fa1a583..2da1433 100755
@@ -29,7 +29,7 @@ rejoin        merge the new branch back into HEAD
  options for 'add', 'merge', 'pull' and 'push'
 squash        merge subtree changes as a single commit
 "
-eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
+eval "$(printf %s "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || printf %s "exit $?")"
 
 PATH=$PATH:$(git --exec-path)
 . git-sh-setup
@@ -51,17 +51,29 @@ prefix=
 debug()
 {
 	if [ -n "$debug" ]; then
-		echo "$@" >&2
+		printf "%s\n" "$*" >&2
 	fi
 }
 
 say()
 {
 	if [ -z "$quiet" ]; then
-		echo "$@" >&2
+		printf "%s\n" "$*" >&2
 	fi
 }
 
+state()
+{
+	if [ -z "$quiet" ]; then
+		printf "%s\r" "$*" >&2
+	fi
+}
+
+log()
+{
+	printf "%s\n" "$*"
+}
+
 assert()
 {
 	if "$@"; then
@@ -72,7 +84,7 @@ assert()
 }
 
 
-#echo "Options: $*"
+#log "Options: $*"
 
 while [ $# -gt 0 ]; do
 	opt="$1"
@@ -149,7 +161,7 @@ cache_get()
 	for oldrev in $*; do
 		if [ -r "$cachedir/$oldrev" ]; then
 			read newrev <"$cachedir/$oldrev"
-			echo $newrev
+			log $newrev
 		fi
 	done
 }
@@ -158,7 +170,7 @@ cache_miss()
 {
 	for oldrev in $*; do
 		if [ ! -r "$cachedir/$oldrev" ]; then
-			echo $oldrev
+			log $oldrev
 		fi
 	done
 }
@@ -175,7 +187,7 @@ check_parents()
 
 set_notree()
 {
-	echo "1" > "$cachedir/notree/$1"
+	log "1" > "$cachedir/notree/$1"
 }
 
 cache_set()
@@ -187,7 +199,7 @@ cache_set()
 	     -a -e "$cachedir/$oldrev" ]; then
 		die "cache for $oldrev already exists!"
 	fi
-	echo "$newrev" >"$cachedir/$oldrev"
+	log "$newrev" >"$cachedir/$oldrev"
 }
 
 rev_exists()
@@ -219,7 +231,7 @@ rev_is_descendant_of_branch()
 try_remove_previous()
 {
 	if rev_exists "$1^"; then
-		echo "^$1^"
+		log "^$1^"
 	fi
 }
 
@@ -247,7 +259,7 @@ find_latest_squash()
 						sq="$sub"
 					fi
 					debug "Squash found: $sq $sub"
-					echo "$sq" "$sub"
+					log "$sq" "$sub"
 					break
 				fi
 				sq=
@@ -339,9 +351,9 @@ add_msg()
 add_squashed_msg()
 {
 	if [ -n "$message" ]; then
-		echo "$message"
+		log "$message"
 	else
-		echo "Merge commit '$1' as '$2'"
+		log "Merge commit '$1' as '$2'"
 	fi
 }
 
@@ -373,17 +385,17 @@ squash_msg()
 	
 	if [ -n "$oldsub" ]; then
 		oldsub_short=$(git rev-parse --short "$oldsub")
-		echo "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
+		log "Squashed '$dir/' changes from $oldsub_short..$newsub_short"
 		echo
 		git log --pretty=tformat:'%h %s' "$oldsub..$newsub"
 		git log --pretty=tformat:'REVERT: %h %s' "$newsub..$oldsub"
 	else
-		echo "Squashed '$dir/' content from commit $newsub_short"
+		log "Squashed '$dir/' content from commit $newsub_short"
 	fi
 	
 	echo
-	echo "git-subtree-dir: $dir"
-	echo "git-subtree-split: $newsub"
+	log "git-subtree-dir: $dir"
+	log "git-subtree-split: $newsub"
 }
 
 toptree_for_commit()
@@ -401,7 +413,7 @@ subtree_for_commit()
 		assert [ "$name" = "$dir" ]
 		assert [ "$type" = "tree" -o "$type" = "commit" ]
 		[ "$type" = "commit" ] && continue  # ignore submodules
-		echo $tree
+		log $tree
 		break
 	done
 }
@@ -474,7 +486,7 @@ copy_or_skip()
 	done
 	
 	if [ -n "$identical" ]; then
-		echo $identical
+		log $identical
 	else
 		copy_commit $rev $tree "$p" || exit $?
 	fi
@@ -526,7 +538,7 @@ cmd_add()
 
 cmd_add_repository()
 {
-	echo "git fetch" "$@"
+	log "git fetch" "$@"
 	repository=$1
 	refspec=$2
 	git fetch "$@" || exit $?
@@ -599,7 +611,7 @@ cmd_split()
 	eval "$grl" |
 	while read rev parents; do
 		revcount=$(($revcount + 1))
-		say -n "$revcount/$revmax ($createcount)
"
+		state "$revcount/$revmax ($createcount)"
 		debug "Processing commit: $rev"
 		exists=$(cache_get $rev)
 		if [ -n "$exists" ]; then
@@ -656,7 +668,7 @@ cmd_split()
 		git update-ref -m 'subtree split' "refs/heads/$branch" $latest_new || exit $?
 		say "$action branch '$branch'"
 	fi
-	echo $latest_new
+	log $latest_new
 	exit 0
 }
 
@@ -726,7 +738,7 @@ cmd_push()
 	if [ -e "$dir" ]; then
 	    repository=$1
 	    refspec=$2
-	    echo "git push using: " $repository $refspec
+	    log "git push using: " $repository $refspec
 	    localrev=$(git subtree split --prefix="$prefix") || die
 	    git push $repository $localrev:refs/heads/$refspec
 	else
Signature · 1 line
2.3.7.windows.1
#3 lore raw
Quoted 1 line · Danny Lin

Replace all echo using printf for better portability.

I doubt this change is sensible.

It is not like "echo is bad, don't use it". It is more about "some features of 'echo', like 'echo -n $msg' vs 'echo $msg\c' are not portable".

contrib/subtree/git-subtree.sh rejoin merge the new branch back into HEAD · line 32

Quoted 3 lines

"

contrib/subtree/git-subtree.sh +1 -1
-eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
+eval "$(printf %s "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || printf %s "exit $?")"

I do not think we want this.

Quoted 8 lines

PATH=$PATH:$(git --exec-path)
. git-sh-setup

contrib/subtree/git-subtree.sh +1 -1
@@ -51,17 +51,29 @@ prefix=
 debug()
 {
 	if [ -n "$debug" ]; then
-		echo "$@" >&2
+		printf "%s\n" "$*" >&2

contrib/subtree/git-subtree.sh prefix= · line 63

Quoted 10 lines
contrib/subtree/git-subtree.sh +1 -1
 	fi
 }
 
 say()
 {
 	if [ -z "$quiet" ]; then
-		echo "$@" >&2
+		printf "%s\n" "$*" >&2
 	fi
 }

These are OK.

contrib/subtree/git-subtree.sh prefix= · line 70

Quoted 6 lines
contrib/subtree/git-subtree.sh +6 -0
+state()
+{
+	if [ -z "$quiet" ]; then
+		printf "%s\r" "$*" >&2
+	fi
+}

This is good, but I think it is misnamed. "progress" might be more appropriate.

contrib/subtree/git-subtree.sh prefix= · line 75

Quoted 5 lines
contrib/subtree/git-subtree.sh +5 -0
+
+log()
+{
+	printf "%s\n" "$*"
+}

I do not think we need this.

contrib/subtree/git-subtree.sh assert() · line 87

Quoted 6 lines
contrib/subtree/git-subtree.sh +1 -1
@@ -72,7 +84,7 @@ assert()
 }
 
 
-#echo "Options: $*"
+#log "Options: $*"

Definitely not.

contrib/subtree/git-subtree.sh cache_get() · line 164

Quoted 8 lines
contrib/subtree/git-subtree.sh +1 -1
 while [ $# -gt 0 ]; do
 	opt="$1"
@@ -149,7 +161,7 @@ cache_get()
 	for oldrev in $*; do
 		if [ -r "$cachedir/$oldrev" ]; then
 			read newrev <"$cachedir/$oldrev"
-			echo $newrev
+			log $newrev

We know this is 40-hex, and there is no magic, don't we?

contrib/subtree/git-subtree.sh cache_miss() · line 173

Quoted 6 lines
contrib/subtree/git-subtree.sh +1 -1
@@ -158,7 +170,7 @@ cache_miss()
 {
 	for oldrev in $*; do
 		if [ ! -r "$cachedir/$oldrev" ]; then
-			echo $oldrev
+			log $oldrev

Likewise.

And I'll stop saying "Likewise" at this point.

contrib/subtree/git-subtree.sh cmd_split() · line 617

Quoted 9 lines
contrib/subtree/git-subtree.sh +1 -1
@@ -599,7 +611,7 @@ cmd_split()
 	eval "$grl" |
 	while read rev parents; do
 		revcount=$(($revcount + 1))
-		say -n "$revcount/$revmax ($createcount)"
+		state "$revcount/$revmax ($createcount)"
 		debug "Processing commit: $rev"
 		exists=$(cache_get $rev)
 		if [ -n "$exists" ]; then

Good.

If we wanted to make "state" (or "progress") to be usable in a wider context, we may want to change its implementation a little bit, but that is a separate topic. It only has a single caller, and it only feeds ever growing string, so the "print and then carriage-return" is sufficient for now.

Thanks.

author
#4 lore raw

PATCH 1/1 contrib/subtree: portability fix for string printing

Replace echo using printf in debug() and say() for better portability.

Also re-wrap previous 'say -n "$str<CR>"' using a new function progress() to prevent CR chars included in the source code, which could be mal-processed in some shells. For example, MsysGit trims CR before executing a shell script file in order to make it work right on Windows even if it uses CRLF as linefeeds.

---
 contrib/subtree/git-subtree.sh | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
contrib/subtree/git-subtree.sh +10 -4
index fa1a583..6f6ddbe 100755
@@ -51,14 +51,21 @@ prefix=
 debug()
 {
 	if [ -n "$debug" ]; then
-		echo "$@" >&2
+		printf "%s\n" "$*" >&2
 	fi
 }
 
 say()
 {
 	if [ -z "$quiet" ]; then
-		echo "$@" >&2
+		printf "%s\n" "$*" >&2
+	fi
+}
+
+progress()
+{
+	if [ -z "$quiet" ]; then
+		printf "%s\r" "$*" >&2
 	fi
 }
 
@@ -247,7 +254,7 @@ find_latest_squash()
 						sq="$sub"
 					fi
 					debug "Squash found: $sq $sub"
-					echo "$sq" "$sub"
+					log "$sq" "$sub"
 					break
 				fi
 				sq=
@@ -599,7 +606,7 @@ cmd_split()
 	eval "$grl" |
 	while read rev parents; do
 		revcount=$(($revcount + 1))
-		say -n "$revcount/$revmax ($createcount)

"
+ progress "$revcount/$revmax ($createcount)"
debug "Processing commit: $rev" exists=$(cache_get $rev)
if [ -n "$exists" ]; then

Signature · 1 line
2.3.7.windows.1
author
#5 lore raw

PATCH 1/1 contrib/subtree: portability fix for string printing

In reply to Junio C Hamano

Replace echo using printf in debug() and say() for better portability.

Also re-wrap previous 'say -n "$str<CR>"' using a new function progress() to prevent CR chars included in the source code, which could be mal-processed in some shells. For example, MsysGit trims CR before executing a shell script file in order to make it work right on Windows even if it uses CRLF as linefeeds.

---
 contrib/subtree/git-subtree.sh | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
contrib/subtree/git-subtree.sh +9 -3
index fa1a583..d4dae7a 100755
@@ -51,14 +51,21 @@ prefix=
 debug()
 {
 	if [ -n "$debug" ]; then
-		echo "$@" >&2
+		printf "%s\n" "$*" >&2
 	fi
 }
 
 say()
 {
 	if [ -z "$quiet" ]; then
-		echo "$@" >&2
+		printf "%s\n" "$*" >&2
+	fi
+}
+
+progress()
+{
+	if [ -z "$quiet" ]; then
+		printf "%s\r" "$*" >&2
 	fi
 }
 
@@ -599,7 +606,7 @@ cmd_split()
 	eval "$grl" |
 	while read rev parents; do
 		revcount=$(($revcount + 1))
-		say -n "$revcount/$revmax ($createcount)

"
+ progress "$revcount/$revmax ($createcount)"
debug "Processing commit: $rev" exists=$(cache_get $rev)
if [ -n "$exists" ]; then

Signature · 3 lines
2.3.7.windows.1

Previous patch had a flaw, revised.
#6 lore raw

Re: [PATCH] contrib/subtree: portability fix for string printing

Quoted 10 lines · Danny Lin

Replace echo using printf in debug() and say() for better portability.

Also re-wrap previous 'say -n "$str<CR>"' using a new function progress() to prevent CR chars included in the source code, which could be mal-processed in some shells. For example, MsysGit trims CR before executing a shell script file in order to make it work right on Windows even if it uses CRLF as linefeeds.

Quoted 2 lines

---

Thanks, this looks good. Will apply with a little bit of tweak in the log message.

Just for future reference, when shooting many iterations of the same patch in a short timeframe, please be aware that the recipient may not get the messages in the order you sent, and that it may not be apparent to the recipients what changed between the iterations. What we commonly do around here to address these issues is to mention what changed from the previous one below the "---" line before the diffstat. I would have done something like this if I were doing this patch, for example:

...
even if it uses CRLF as linefeeds.

---

* The previous one still used "log" helper by mistake even though I removed the implementation of it and decided to use "echo" for non-tricky cases. This fixes it.

contrib/subtree/git-subtree.sh | 13 ++++++++++---
...

#7 lore raw

Re: [PATCH] contrib/subtree: portability fix for string printing

Quoted 12 lines · Junio C Hamano
Quoted 10 lines · Danny Lin

Replace echo using printf in debug() and say() for better portability.

Also re-wrap previous 'say -n "$str<CR>"' using a new function progress() to prevent CR chars included in the source code, which could be mal-processed in some shells. For example, MsysGit trims CR before executing a shell script file in order to make it work right on Windows even if it uses CRLF as linefeeds.

Quoted 5 lines
Quoted 2 lines

---

Thanks, this looks good. Will apply with a little bit of tweak in the log message.

Hmm, I would say that the changes to debug() and say() should either be dropped or moved to a separate patch (along with the first paragraph of the commit message). With the introduction of the progress() abstraction, there is no longer any need for changes to say(), and the "better portability" rationale for changing say() and debug() is never properly explained, and is thus nebulous at best.

#8 lore raw

Re: [PATCH] contrib/subtree: portability fix for string printing

Quoted 19 lines · Eric Sunshine
Quoted 12 lines · Junio C Hamano
Quoted 10 lines · Danny Lin

Replace echo using printf in debug() and say() for better portability.

Also re-wrap previous 'say -n "$str<CR>"' using a new function progress() to prevent CR chars included in the source code, which could be mal-processed in some shells. For example, MsysGit trims CR before executing a shell script file in order to make it work right on Windows even if it uses CRLF as linefeeds.

Quoted 5 lines
Quoted 2 lines

---

Thanks, this looks good. Will apply with a little bit of tweak in the log message.

Quoted 6 lines

Hmm, I would say that the changes to debug() and say() should either be dropped or moved to a separate patch (along with the first paragraph of the commit message). With the introduction of the progress() abstraction, there is no longer any need for changes to say(), and the "better portability" rationale for changing say() and debug() is never properly explained, and is thus nebulous at best.

I justified them in this way.

contrib/subtree: portability fix for string printing

'echo -n' is not portable, but this script used it as a way to give a string followed by a carriage return for progress messages. Introduce a new helper shell function "progress" and use printf as a more portable way to do this. As a side effect, this makes it unnecessary to have a raw CR in our source, which can be munged in some shells. For example, MsysGit trims CR before executing a shell script file in order to make it work right on Windows even if it uses CRLF as linefeeds.

While at it, replace "echo" using printf in debug() and say() to avoid tempting people introducing the same bug.

#9 lore raw

Re: [PATCH] contrib/subtree: portability fix for string printing

Quoted 16 lines · Junio C Hamano
Quoted 10 lines · Eric Sunshine
Quoted 2 lines · Junio C Hamano

Thanks, this looks good. Will apply with a little bit of tweak in the log message.

Hmm, I would say that the changes to debug() and say() should either be dropped or moved to a separate patch (along with the first paragraph of the commit message). With the introduction of the progress() abstraction, there is no longer any need for changes to say(), and the "better portability" rationale for changing say() and debug() is never properly explained, and is thus nebulous at best.

I justified them in this way.

contrib/subtree: portability fix for string printing

Quoted 8 lines

'echo -n' is not portable, but this script used it as a way to give a string followed by a carriage return for progress messages. Introduce a new helper shell function "progress" and use printf as a more portable way to do this. As a side effect, this makes it unnecessary to have a raw CR in our source, which can be munged in some shells. For example, MsysGit trims CR before executing a shell script file in order to make it work right on Windows even if it uses CRLF as linefeeds.

Very nicely explained.

Quoted 2 lines

While at it, replace "echo" using printf in debug() and say() to avoid tempting people introducing the same bug.

Okay, this works as reasonable justification for including those changes in the same patch.

It might read a bit more fluidly if rephrased something like this:

While at it, replace 'echo' with 'printf' in debug() and say() to eliminate the temptation of reintroducing the same bug.