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

[PATCH/RFC 6/7] contrib/subtree: Use %B for the split commit message

From
Techlive Zheng <techlivezheng@gmail.com>
Date
Jan 13, 2013, 01:52 UTC
Message-ID
<1358041958-1998-7-git-send-email-techlivezheng@gmail.com>
In-Reply-To
<1358041958-1998-1-git-send-email-techlivezheng@gmail.com>

Use %B rather than %s%n%n%b to handle the special case of a commit that only has a subject line. We don't want to introduce a newline after the subject, causing generation of a new hash.

After this commit, the newly split branch might differ from the previous one. If this is the case, --fallback option could help.

Signed-off-by: Techlive Zheng <techlivezheng@gmail.com>
Signed-off-by: David A. Greene <greened@obbligato.org>
---
 contrib/subtree/git-subtree.sh     | 13 ++++++++++-
 contrib/subtree/git-subtree.txt    | 13 +++++++++++
 contrib/subtree/t/t7900-subtree.sh | 47 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 1 deletion(-)
diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 91e6e87..018ee32 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -25,6 +25,7 @@ b,branch=     create a new branch from the split subtree
 ignore-joins  ignore prior --rejoin commits
 onto=         try connecting new tree to an existing one
 rejoin        merge the new branch back into HEAD
+fallback      fallback to the obsolete commit generating mechanism
  options for 'add', 'merge', 'pull' and 'push'
 squash        merge subtree changes as a single commit
 "
@@ -45,6 +46,7 @@ ignore_joins=
 annotate=
 squash=
 message=
+fallback=
 
 debug()
 {
@@ -92,6 +94,8 @@ while [ $# -gt 0 ]; do
 		--no-ignore-joins) ignore_joins= ;;
 		--squash) squash=1 ;;
 		--no-squash) squash= ;;
+		--fallback) fallback=1 ;;
+		--no-fallback) fallback= ;;
 		--) break ;;
 		*) die "Unexpected option: $opt" ;;
 	esac
@@ -296,7 +300,14 @@ copy_commit()
 	# We're going to set some environment vars here, so
 	# do it in a subshell to get rid of them safely later
 	debug copy_commit "{$1}" "{$2}" "{$3}"
-	git log -1 --pretty=format:'%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b' "$1" |
+
+	if [ -z "$fallback" ]; then
+		log_format='%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%B'
+	else
+		log_format='%an%n%ae%n%ad%n%cn%n%ce%n%cd%n%s%n%n%b'
+	fi
+
+	git log -1 --pretty=format:"$log_format" "$1" |
 	(
 		read GIT_AUTHOR_NAME
 		read GIT_AUTHOR_EMAIL
diff --git a/contrib/subtree/git-subtree.txt b/contrib/subtree/git-subtree.txt
index c5bce41..ca9f199 100644
--- a/contrib/subtree/git-subtree.txt
+++ b/contrib/subtree/git-subtree.txt
@@ -254,6 +254,19 @@ OPTIONS FOR split
 	'--rejoin' when you split, because you don't want the
 	subproject's history to be part of your project anyway.
 
+--fallback::
+	Previously, git subtree would introduce an extra new line for
+	the commits whose commit message contains only one line.
+	This behavior has been correct. Unfortunately, for those whose
+	current split branch contains these kind of commits, git subtree
+	will generate a new split branch which differs from the existing
+	split branch in these commits. It is better to use this new
+	split branch, because its commits stay intact within the mainline.
+	
+	Otherwise, the previous fault behavior could still be used with
+	this option. This option is only for a compatible purpose, newly
+	split branch should never use this option.
+
 
 EXAMPLE 1. Add command
 ----------------------
diff --git a/contrib/subtree/t/t7900-subtree.sh b/contrib/subtree/t/t7900-subtree.sh
index ece2064..1492303 100755
--- a/contrib/subtree/t/t7900-subtree.sh
+++ b/contrib/subtree/t/t7900-subtree.sh
@@ -399,6 +399,53 @@ test_expect_success 'split subdir/ with --branch for an incompatible branch' '
     )
 '
 
+test_expect_success 'make sure commits with one line message stay intact after split' '
+    test_create_repo $test_count &&
+    test_create_repo $test_count/subproj &&
+    test_create_commit $test_count main1 &&
+    test_create_commit $test_count/subproj sub1 &&
+    (
+        cd $test_count &&
+        git fetch ./subproj master &&
+        ori_hash=$(git rev-parse FETCH_HEAD) &&
+        git branch subori FETCH_HEAD &&
+        git filter-branch --index-filter '\''git ls-files -s | sed "s-\t-&subdir/-" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"'\'' subori
+        git merge -m "Merge B project as our subdirectory" subori &&
+        git subtree split --prefix subdir --branch splitbr1 &&
+        new_hash_1=$(git rev-parse splitbr1) &&
+        test_equal "$ori_hash" "$new_hash_1" &&
+        git subtree split --prefix subdir --branch splitbr2 --fallback &&
+        new_hash_2=$(git rev-parse splitbr2) &&
+        test_must_fail test_equal "$ori_hash" "$new_hash_2"
+    )
+'
+
+test_expect_success 'make sure --fallback option works correctly for the existing split branch' '
+    test_create_repo "$test_count" &&
+    test_create_repo "$test_count"/subproj &&
+    test_create_commit "$test_count" main1 &&
+    test_create_commit "$test_count"/subproj sub1 &&
+    (
+        cd $test_count &&
+        git fetch ./subproj master &&
+        ori_hash=$(git rev-parse FETCH_HEAD) &&
+        git subtree add --prefix=subdir FETCH_HEAD
+    ) &&
+    test_create_commit "$test_count" subdir/main-sub1 &&
+    (
+        cd $test_count &&
+        git subtree split --prefix subdir --branch splitbr1 &&
+        git subtree split --prefix subdir --branch splitbr2 --fallback &&
+        test_must_fail test_equal "$(git rev-parse splitbr1)" "$(git rev-parse splitbr2)"
+    ) &&
+    test_create_commit "$test_count" subdir/main-sub2 &&
+    (
+        cd $test_count &&
+        test_must_fail git subtree split --prefix subdir --branch splitbr2 &&
+        git subtree split --prefix subdir --branch splitbr2 --fallback
+    )
+'
+
 #
 # Validity checking
 #
-- 
1.8.1
Previous: Techlive ZhengNext: Techlive Zheng
Message 8 of 18 in “mutiple improvements”
  1. 0/7 mutiple improvementsTechlive Zheng, Jan 13, 2013
  2. 1/7 contrib/subtree: Add vim modelineTechlive Zheng, Jan 13, 2013
  3. 2/7 contrib/subtree: Ignore testing directoryTechlive Zheng, Jan 13, 2013
  4. 3/7 contrib/subtree: Remove test number commentsTechlive Zheng, Jan 13, 2013
  5. 4/7 contrib/subtree: Code cleaning and refactoringTechlive Zheng, Jan 13, 2013
  6. 郑文辉 (Techlive Zheng)Jan 14, 2013
  7. 5/7 contrib/subtree: Make each test self-containedTechlive Zheng, Jan 13, 2013
  8. 6/7 contrib/subtree: Use %B for the split commit messageTechlive Zheng, Jan 13, 2013
  9. 7/7 contrib/subtree: Handle '--prefix' argument with a slash appendedTechlive Zheng, Jan 13, 2013
  10. 0/8 contrib/subtree: Reroll to follow Git's whitespace policyTechlive Zheng, Jan 14, 2013
  11. 1/8 contrib/subtree: Fix whitespacesTechlive Zheng, Jan 14, 2013
  12. 2/8 contrib/subtree: Add vim modelineTechlive Zheng, Jan 14, 2013
  13. 3/8 contrib/subtree: Ignore testing directoryTechlive Zheng, Jan 14, 2013
  14. 4/8 contrib/subtree: Remove test number commentsTechlive Zheng, Jan 14, 2013
  15. 5/8 contrib/subtree: Code cleaning and refactoringTechlive Zheng, Jan 14, 2013
  16. 6/8 contrib/subtree: Make each test self-containedTechlive Zheng, Jan 14, 2013
  17. 7/8 contrib/subtree: Use %B for the split commit messageTechlive Zheng, Jan 14, 2013
  18. 8/8 contrib/subtree: Handle '--prefix' argument with a slash appendedTechlive Zheng, Jan 14, 2013

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.