3 # Copyright (c) 2005 Amos Waterland
6 test_description
='git rebase assorted tests
8 This test runs git rebase and checks that the author information is not lost
11 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
12 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14 TEST_PASSES_SANITIZE_LEAK
=true
17 GIT_AUTHOR_NAME
=author@name
18 GIT_AUTHOR_EMAIL
=bogus@email@address
19 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
21 test_expect_success
'prepare repository with topic branches' '
22 test_commit "Add A." A First First &&
23 git checkout -b force-3way &&
25 git update-index --add Y &&
26 git commit -m "Add Y." &&
27 git checkout -b filemove &&
28 git reset --soft main &&
31 git commit -m "Move A." &&
32 git checkout -b my-topic-branch main &&
33 test_commit "Add B." B Second Second &&
34 git checkout -f main &&
37 git commit -m "Modify A." &&
38 git checkout -b side my-topic-branch &&
41 git commit -m "Add C" &&
42 git checkout -f my-topic-branch &&
46 test_expect_success
'rebase on dirty worktree' '
48 test_must_fail git rebase main
51 test_expect_success
'rebase on dirty cache' '
53 test_must_fail git rebase main
56 test_expect_success
'rebase against main' '
57 git reset --hard HEAD &&
61 test_expect_success
'rebase sets ORIG_HEAD to pre-rebase state' '
62 git checkout -b orig-head topic &&
63 pre="$(git rev-parse --verify HEAD)" &&
65 test_cmp_rev "$pre" ORIG_HEAD &&
66 test_cmp_rev ! "$pre" HEAD
69 test_expect_success
'rebase, with <onto> and <upstream> specified as :/quuxery' '
70 test_when_finished "git branch -D torebase" &&
71 git checkout -b torebase my-topic-branch^ &&
72 upstream=$(git rev-parse ":/Add B") &&
73 onto=$(git rev-parse ":/Add A") &&
74 git rebase --onto $onto $upstream &&
75 git reset --hard my-topic-branch^ &&
76 git rebase --onto ":/Add A" ":/Add B" &&
77 git checkout my-topic-branch
80 test_expect_success
'the rebase operation should not have destroyed author information' '
81 ! (git log | grep "Author:" | grep "<>")
84 test_expect_success
'the rebase operation should not have destroyed author information (2)' "
86 grep 'Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>'
89 test_expect_success
'HEAD was detached during rebase' '
90 test $(git rev-parse HEAD@{1}) != $(git rev-parse my-topic-branch@{1})
93 test_expect_success
'rebase from ambiguous branch name' '
94 git checkout -b topic side &&
98 test_expect_success
'rebase off of the previous branch using "-"' '
100 git checkout HEAD^ &&
101 git rebase @{-1} >expect.messages &&
102 git merge-base main HEAD >expect.forkpoint &&
105 git checkout HEAD^ &&
106 git rebase - >actual.messages &&
107 git merge-base main HEAD >actual.forkpoint &&
109 test_cmp expect.forkpoint actual.forkpoint &&
110 # the next one is dubious---we may want to say "-",
111 # instead of @{-1}, in the message
112 test_cmp expect.messages actual.messages
115 test_expect_success
'rebase a single mode change' '
117 git branch -D topic &&
121 git commit -m prepare &&
122 git checkout -b modechange HEAD^ &&
127 git commit -m modechange &&
128 GIT_TRACE=1 git rebase main
131 test_expect_success
'rebase is not broken by diff.renames' '
132 test_config diff.renames copies &&
133 git checkout filemove &&
134 GIT_TRACE=1 git rebase force-3way
137 test_expect_success
'setup: recover' '
138 test_might_fail git rebase --abort &&
140 git checkout modechange
143 test_expect_success
'Show verbose error when HEAD could not be detached' '
145 test_when_finished "rm -f B" &&
146 test_must_fail git rebase topic 2>output.err >output.out &&
147 test_grep "The following untracked working tree files would be overwritten by checkout:" output.err &&
148 test_grep B output.err
151 test_expect_success
'fail when upstream arg is missing and not on branch' '
152 git checkout topic &&
153 test_must_fail git rebase
156 test_expect_success
'fail when upstream arg is missing and not configured' '
157 git checkout -b no-config topic &&
158 test_must_fail git rebase
161 test_expect_success
'rebase works with format.useAutoBase' '
162 test_config format.useAutoBase true &&
163 git checkout topic &&
167 test_expect_success
'default to common base in @{upstream}s reflog if no upstream arg (--merge)' '
168 git checkout -b default-base main &&
169 git checkout -b default topic &&
170 git config branch.default.remote . &&
171 git config branch.default.merge refs/heads/default-base &&
172 git rebase --merge &&
173 git rev-parse --verify default-base >expect &&
174 git rev-parse default~1 >actual &&
175 test_cmp expect actual &&
176 git checkout default-base &&
177 git reset --hard HEAD^ &&
178 git checkout default &&
179 git rebase --merge &&
180 git rev-parse --verify default-base >expect &&
181 git rev-parse default~1 >actual &&
182 test_cmp expect actual
185 test_expect_success
'default to common base in @{upstream}s reflog if no upstream arg (--apply)' '
186 git checkout -B default-base main &&
187 git checkout -B default topic &&
188 git config branch.default.remote . &&
189 git config branch.default.merge refs/heads/default-base &&
190 git rebase --apply &&
191 git rev-parse --verify default-base >expect &&
192 git rev-parse default~1 >actual &&
193 test_cmp expect actual &&
194 git checkout default-base &&
195 git reset --hard HEAD^ &&
196 git checkout default &&
197 git rebase --apply &&
198 git rev-parse --verify default-base >expect &&
199 git rev-parse default~1 >actual &&
200 test_cmp expect actual
203 test_expect_success
'cherry-picked commits and fork-point work together' '
204 git checkout default-base &&
206 git commit -a --no-edit --amend &&
208 test_commit new_B B "New B" &&
210 git checkout default &&
211 git reset --hard default-base@{4} &&
213 git cherry-pick -2 default-base^ &&
214 test_commit final_B B "Final B" &&
216 echo Amended >expect &&
218 echo "Final B" >expect &&
226 test_expect_success
'rebase --apply -q is quiet' '
227 git checkout -b quiet topic &&
228 git rebase --apply -q main >output.out 2>&1 &&
229 test_must_be_empty output.out
232 test_expect_success
'rebase --merge -q is quiet' '
233 git checkout -B quiet topic &&
234 git rebase --merge -q main >output.out 2>&1 &&
235 test_must_be_empty output.out
238 test_expect_success
'Rebase a commit that sprinkles CRs in' '
248 git commit -a -m "A file with a line with CR" &&
249 git tag file-with-cr &&
250 git checkout HEAD^0 &&
251 git rebase --onto HEAD^^ HEAD^ &&
252 git diff --exit-code file-with-cr:CR HEAD:CR
255 test_expect_success
'rebase can copy notes' '
256 git config notes.rewrite.rebase true &&
257 git config notes.rewriteRef "refs/notes/*" &&
261 git notes add -m"a note" n3 &&
262 git rebase --onto n1 n2 &&
263 test "a note" = "$(git notes show HEAD)"
266 test_expect_success
'rebase -m can copy notes' '
267 git reset --hard n3 &&
268 git rebase -m --onto n1 n2 &&
269 test "a note" = "$(git notes show HEAD)"
272 test_expect_success
'rebase commit with an ancient timestamp' '
275 >old.one && git add old.one && test_tick &&
276 git commit --date="@12345 +0400" -m "Old one" &&
277 >old.two && git add old.two && test_tick &&
278 git commit --date="@23456 +0500" -m "Old two" &&
279 >old.three && git add old.three && test_tick &&
280 git commit --date="@34567 +0600" -m "Old three" &&
282 git cat-file commit HEAD^^ >actual &&
283 grep "author .* 12345 +0400$" actual &&
284 git cat-file commit HEAD^ >actual &&
285 grep "author .* 23456 +0500$" actual &&
286 git cat-file commit HEAD >actual &&
287 grep "author .* 34567 +0600$" actual &&
289 git rebase --onto HEAD^^ HEAD^ &&
291 git cat-file commit HEAD >actual &&
292 grep "author .* 34567 +0600$" actual
295 test_expect_success
'rebase with "From " line in commit message' '
296 git checkout -b preserve-from main~1 &&
297 cat >From_.msg <<EOF &&
298 Somebody embedded an mbox in a commit message
300 This is from so-and-so:
302 From a@b Mon Sep 17 00:00:00 2001
303 From: John Doe <nobody@example.com>
304 Date: Sat, 11 Nov 2017 00:00:00 +0000
305 Subject: not this message
311 git commit -F From_.msg &&
313 git log -1 --pretty=format:%B >out &&
314 test_cmp From_.msg out
317 test_expect_success
'rebase --apply and --show-current-patch' '
318 test_create_repo conflict-apply &&
323 git commit -a -m one &&
325 git commit -a -m two &&
327 test_must_fail git rebase --apply -f --onto init HEAD^ &&
328 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
329 grep "show.*$(git rev-parse two)" stderr
333 test_expect_success
'rebase --apply and .gitattributes' '
334 test_create_repo attributes &&
338 git config filter.test.clean "sed -e '\''s/smudged/clean/g'\''" &&
339 git config filter.test.smudge "sed -e '\''s/clean/smudged/g'\''" &&
341 test_commit second &&
342 git checkout -b test HEAD^ &&
344 echo "*.txt filter=test" >.gitattributes &&
345 git add .gitattributes &&
348 echo "This text is smudged." >a.txt &&
350 test_commit fourth &&
352 git checkout -b removal HEAD^ &&
353 git rm .gitattributes &&
356 git cherry-pick test &&
360 grep "smudged" a.txt &&
362 git checkout removal &&
369 test_expect_success
'rebase--merge.sh and --show-current-patch' '
370 test_create_repo conflict-merge &&
375 git commit -a -m one &&
377 git commit -a -m two &&
379 test_must_fail git rebase --merge --onto init HEAD^ &&
380 git rebase --show-current-patch >actual.patch &&
381 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
382 grep "show.*REBASE_HEAD" stderr &&
383 test "$(git rev-parse REBASE_HEAD)" = "$(git rev-parse two)"
387 test_expect_success
'switch to branch checked out here' '
392 test_expect_success
'switch to branch checked out elsewhere fails' '
394 git worktree remove wt1 &&
395 git worktree remove wt2 &&
398 git worktree add wt1 -b shared &&
399 git worktree add wt2 -f shared &&
400 # we test in both worktrees to ensure that works
401 # as expected with "first" and "next" worktrees
402 test_must_fail git -C wt1 rebase shared shared &&
403 test_must_fail git -C wt2 rebase shared shared
406 test_expect_success
'switch to branch not checked out' '
409 git rebase main other
412 test_expect_success
'switch to non-branch detaches HEAD' '
414 old_main=$(git rev-parse HEAD) &&
415 git rebase First Second^0 &&
416 test_cmp_rev HEAD Second &&
417 test_cmp_rev main $old_main &&
418 test_must_fail git symbolic-ref HEAD
421 test_expect_success
'refuse to switch to branch checked out elsewhere' '
423 git worktree add wt &&
424 test_must_fail git -C wt rebase main main 2>err &&
425 test_grep "already used by worktree at" err
428 test_expect_success
'rebase when inside worktree subdirectory' '
432 git commit --allow-empty -m "initial" &&
434 test_commit foo/bar/baz &&
437 # create another branch for our other worktree
439 git worktree add ../other-wt other &&
441 # create and cd into a subdirectory
442 mkdir -p random/dir &&
445 git rebase --onto HEAD^^ HEAD^ # drops the HEAD^ commit