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 <techlivezh...@gmail.com>
Signed-off-by: David A. Greene <gree...@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 88903c0..d529a76 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 72be8e4..55d0575 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 ef83f31..232ed89 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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to