blob: e78c7e37969ab3d7caabcd3f39131e134670cf06 [file] [log] [blame]
David A. Greene5f359002016-06-28 05:54:02 -05001#!/bin/sh
2
3test_description='git rebase tests for -Xsubtree
4
5This test runs git rebase and tests the subtree strategy.
6'
Johannes Schindelind1c02d92020-11-18 23:44:25 +00007GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00008export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
David A. Greene5f359002016-06-28 05:54:02 -050010. ./test-lib.sh
11. "$TEST_DIRECTORY"/lib-rebase.sh
12
13commit_message() {
14 git log --pretty=format:%s -1 "$1"
15}
16
Johannes Schindelin8c1e2402019-07-31 08:18:42 -070017# There are a few bugs in the rebase with regards to the subtree strategy, and
18# this test script tries to document them. First, the following commit history
19# is generated (the onelines are shown, time flows from left to right):
20#
Johannes Schindelinb6211b82020-09-26 21:04:21 +000021# topic_1 - topic_2 - topic_3
Johannes Schindelin8c1e2402019-07-31 08:18:42 -070022# \
Johannes Schindelind1c02d92020-11-18 23:44:25 +000023# README ---------------------- Add subproject main - topic_4 - files_subtree/topic_5
Johannes Schindelin8c1e2402019-07-31 08:18:42 -070024#
Johannes Schindelinadbcf532020-11-09 00:09:22 +000025# Where the merge moves the files topic_[123].t into the subdirectory
Johannes Schindelinb6211b82020-09-26 21:04:21 +000026# files_subtree/ and topic_4 as well as files_subtree/topic_5 add files to that
Johannes Schindelin8c1e2402019-07-31 08:18:42 -070027# directory directly.
28#
29# Then, in subsequent test cases, `git filter-branch` is used to distill just
30# the commits that touch files_subtree/. To give it a final pre-rebase touch,
31# an empty commit is added on top. The pre-rebase commit history looks like
32# this:
33#
Johannes Schindelind1c02d92020-11-18 23:44:25 +000034# Add subproject main - topic_4 - files_subtree/topic_5 - Empty commit
Johannes Schindelin8c1e2402019-07-31 08:18:42 -070035#
Johannes Schindelinb6211b82020-09-26 21:04:21 +000036# where the root commit adds three files: topic_1.t, topic_2.t and topic_3.t.
Johannes Schindelin8c1e2402019-07-31 08:18:42 -070037#
Johannes Schindelinb6211b82020-09-26 21:04:21 +000038# This commit history is then rebased onto `topic_3` with the
Johannes Schindelin8c1e2402019-07-31 08:18:42 -070039# `-Xsubtree=files_subtree` option in three different ways:
40#
41# 1. using `--preserve-merges`
42# 2. using `--preserve-merges` and --keep-empty
43# 3. without specifying a rebase backend
44
David A. Greene5f359002016-06-28 05:54:02 -050045test_expect_success 'setup' '
46 test_commit README &&
Johannes Schindelinc248d322019-07-31 08:18:42 -070047
48 git init files &&
Johannes Schindelinb6211b82020-09-26 21:04:21 +000049 test_commit -C files topic_1 &&
50 test_commit -C files topic_2 &&
51 test_commit -C files topic_3 &&
Johannes Schindelinc248d322019-07-31 08:18:42 -070052
53 : perform subtree merge into files_subtree/ &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000054 git fetch files refs/heads/main:refs/heads/files-main &&
Johannes Schindelinc248d322019-07-31 08:18:42 -070055 git merge -s ours --no-commit --allow-unrelated-histories \
Johannes Schindelind1c02d92020-11-18 23:44:25 +000056 files-main &&
57 git read-tree --prefix=files_subtree -u files-main &&
58 git commit -m "Add subproject main" &&
Johannes Schindelinc248d322019-07-31 08:18:42 -070059
60 : add two extra commits to rebase &&
Johannes Schindelinb6211b82020-09-26 21:04:21 +000061 test_commit -C files_subtree topic_4 &&
62 test_commit files_subtree/topic_5 &&
Johannes Schindelind51b7712019-07-31 08:18:43 -070063
64 git checkout -b to-rebase &&
Elijah Newren476998d2019-09-04 14:40:48 -070065 git fast-export --no-data HEAD -- files_subtree/ |
66 sed -e "s%\([0-9a-f]\{40\} \)files_subtree/%\1%" |
67 git fast-import --force --quiet &&
68 git reset --hard &&
Johannes Schindelind51b7712019-07-31 08:18:43 -070069 git commit -m "Empty commit" --allow-empty
David A. Greene5f359002016-06-28 05:54:02 -050070'
71
Johannes Schindelinb6211b82020-09-26 21:04:21 +000072# FAILURE: Does not preserve topic_4.
Johannes Schindelinb8c6f242019-07-31 08:18:44 -070073test_expect_failure REBASE_P 'Rebase -Xsubtree --preserve-merges --onto commit' '
David A. Greene5f359002016-06-28 05:54:02 -050074 reset_rebase &&
Johannes Schindelinb8c6f242019-07-31 08:18:44 -070075 git checkout -b rebase-preserve-merges to-rebase &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000076 git rebase -Xsubtree=files_subtree --preserve-merges --onto files-main main &&
Johannes Schindelinb6211b82020-09-26 21:04:21 +000077 verbose test "$(commit_message HEAD~)" = "topic_4" &&
78 verbose test "$(commit_message HEAD)" = "files_subtree/topic_5"
David A. Greene5f359002016-06-28 05:54:02 -050079'
80
Johannes Schindelinb6211b82020-09-26 21:04:21 +000081# FAILURE: Does not preserve topic_4.
Johannes Schindelinb8c6f242019-07-31 08:18:44 -070082test_expect_failure REBASE_P 'Rebase -Xsubtree --keep-empty --preserve-merges --onto commit' '
David A. Greene5f359002016-06-28 05:54:02 -050083 reset_rebase &&
Johannes Schindelinb8c6f242019-07-31 08:18:44 -070084 git checkout -b rebase-keep-empty to-rebase &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000085 git rebase -Xsubtree=files_subtree --keep-empty --preserve-merges --onto files-main main &&
Johannes Schindelinb6211b82020-09-26 21:04:21 +000086 verbose test "$(commit_message HEAD~2)" = "topic_4" &&
87 verbose test "$(commit_message HEAD~)" = "files_subtree/topic_5" &&
David A. Greene5f359002016-06-28 05:54:02 -050088 verbose test "$(commit_message HEAD)" = "Empty commit"
89'
90
Elijah Newrene98c4262020-02-15 21:36:25 +000091test_expect_success 'Rebase -Xsubtree --empty=ask --onto commit' '
David A. Greene5f359002016-06-28 05:54:02 -050092 reset_rebase &&
Johannes Schindelinb8c6f242019-07-31 08:18:44 -070093 git checkout -b rebase-onto to-rebase &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000094 test_must_fail git rebase -Xsubtree=files_subtree --empty=ask --onto files-main main &&
Johannes Schindelin4e6023b2019-07-31 08:18:46 -070095 : first pick results in no changes &&
Elijah Newrend48e5e22020-02-15 21:36:24 +000096 git rebase --skip &&
Johannes Schindelinb6211b82020-09-26 21:04:21 +000097 verbose test "$(commit_message HEAD~2)" = "topic_4" &&
98 verbose test "$(commit_message HEAD~)" = "files_subtree/topic_5" &&
David A. Greene5f359002016-06-28 05:54:02 -050099 verbose test "$(commit_message HEAD)" = "Empty commit"
100'
101
Elijah Newrene98c4262020-02-15 21:36:25 +0000102test_expect_success 'Rebase -Xsubtree --empty=ask --rebase-merges --onto commit' '
Johannes Schindeline1fac532019-07-31 08:18:49 -0700103 reset_rebase &&
104 git checkout -b rebase-merges-onto to-rebase &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +0000105 test_must_fail git rebase -Xsubtree=files_subtree --empty=ask --rebase-merges --onto files-main --root &&
Johannes Schindeline1fac532019-07-31 08:18:49 -0700106 : first pick results in no changes &&
Elijah Newrend48e5e22020-02-15 21:36:24 +0000107 git rebase --skip &&
Johannes Schindelinb6211b82020-09-26 21:04:21 +0000108 verbose test "$(commit_message HEAD~2)" = "topic_4" &&
109 verbose test "$(commit_message HEAD~)" = "files_subtree/topic_5" &&
Johannes Schindeline1fac532019-07-31 08:18:49 -0700110 verbose test "$(commit_message HEAD)" = "Empty commit"
111'
112
David A. Greene5f359002016-06-28 05:54:02 -0500113test_done