git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] contrib/subtree: fix linefeeds trimming for cmd_split()

From
Danny Lin <danny0838@gmail.com>
Date
May 7, 2015, 05:10 UTC
Message-ID
<CAMbsUu6XT4hB0L0PjgJKniyBJ9svkQoJqnxiYaRWo9EZUXnNhg@mail.gmail.com>
In-Reply-To
<CAMbsUu7vkS4D2z_gNFsujVsyHjRiXseTLGCaic=841V=HZyb_g@mail.gmail.com>

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 <danny0838@gmail.com>
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.

Signed-off-by: Danny Lin <danny0838@gmail.com>
---
 contrib/subtree/git-subtree.sh | 56 +++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 22 deletions(-)
diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index fa1a583..2da1433 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -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
-- 
2.3.7.windows.1
Previous: Danny LinNext: Junio C Hamano
Message 3 of 10 in “Re: [PATCH] contrib/subtree: fix linefeeds trimming for cmd_split()”
  1. Danny LinMay 7, 2015
  2. Danny LinMay 7, 2015
  3. Danny LinMay 7, 2015
  4. Junio C HamanoMay 7, 2015
  5. contrib/subtree: portability fix for string printingDanny Lin, May 8, 2015
  6. contrib/subtree: portability fix for string printingDanny Lin, May 8, 2015
  7. Junio C HamanoMay 8, 2015
  8. Eric SunshineMay 8, 2015
  9. Junio C HamanoMay 8, 2015
  10. Eric SunshineMay 8, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.