decorate: add clear_decoration() function
[git.git] / t / t3400-rebase.sh
blob3ce918fdb8062fc5f720bb890ad58a2676547218
1 #!/bin/sh
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
9 among other things.
11 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
12 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14 . ./test-lib.sh
16 GIT_AUTHOR_NAME=author@name
17 GIT_AUTHOR_EMAIL=bogus@email@address
18 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
20 test_expect_success 'prepare repository with topic branches' '
21 test_commit "Add A." A First First &&
22 git checkout -b force-3way &&
23 echo Dummy >Y &&
24 git update-index --add Y &&
25 git commit -m "Add Y." &&
26 git checkout -b filemove &&
27 git reset --soft main &&
28 mkdir D &&
29 git mv A D/A &&
30 git commit -m "Move A." &&
31 git checkout -b my-topic-branch main &&
32 test_commit "Add B." B Second Second &&
33 git checkout -f main &&
34 echo Third >>A &&
35 git update-index A &&
36 git commit -m "Modify A." &&
37 git checkout -b side my-topic-branch &&
38 echo Side >>C &&
39 git add C &&
40 git commit -m "Add C" &&
41 git checkout -f my-topic-branch &&
42 git tag topic
45 test_expect_success 'rebase on dirty worktree' '
46 echo dirty >>A &&
47 test_must_fail git rebase main
50 test_expect_success 'rebase on dirty cache' '
51 git add A &&
52 test_must_fail git rebase main
55 test_expect_success 'rebase against main' '
56 git reset --hard HEAD &&
57 git rebase main
60 test_expect_success 'rebase sets ORIG_HEAD to pre-rebase state' '
61 git checkout -b orig-head topic &&
62 pre="$(git rev-parse --verify HEAD)" &&
63 git rebase main &&
64 test_cmp_rev "$pre" ORIG_HEAD &&
65 test_cmp_rev ! "$pre" HEAD
68 test_expect_success 'rebase, with <onto> and <upstream> specified as :/quuxery' '
69 test_when_finished "git branch -D torebase" &&
70 git checkout -b torebase my-topic-branch^ &&
71 upstream=$(git rev-parse ":/Add B") &&
72 onto=$(git rev-parse ":/Add A") &&
73 git rebase --onto $onto $upstream &&
74 git reset --hard my-topic-branch^ &&
75 git rebase --onto ":/Add A" ":/Add B" &&
76 git checkout my-topic-branch
79 test_expect_success 'the rebase operation should not have destroyed author information' '
80 ! (git log | grep "Author:" | grep "<>")
83 test_expect_success 'the rebase operation should not have destroyed author information (2)' "
84 git log -1 |
85 grep 'Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>'
88 test_expect_success 'HEAD was detached during rebase' '
89 test $(git rev-parse HEAD@{1}) != $(git rev-parse my-topic-branch@{1})
92 test_expect_success 'rebase from ambiguous branch name' '
93 git checkout -b topic side &&
94 git rebase main
97 test_expect_success 'rebase off of the previous branch using "-"' '
98 git checkout main &&
99 git checkout HEAD^ &&
100 git rebase @{-1} >expect.messages &&
101 git merge-base main HEAD >expect.forkpoint &&
103 git checkout main &&
104 git checkout HEAD^ &&
105 git rebase - >actual.messages &&
106 git merge-base main HEAD >actual.forkpoint &&
108 test_cmp expect.forkpoint actual.forkpoint &&
109 # the next one is dubious---we may want to say "-",
110 # instead of @{-1}, in the message
111 test_cmp expect.messages actual.messages
114 test_expect_success 'rebase a single mode change' '
115 git checkout main &&
116 git branch -D topic &&
117 echo 1 >X &&
118 git add X &&
119 test_tick &&
120 git commit -m prepare &&
121 git checkout -b modechange HEAD^ &&
122 echo 1 >X &&
123 git add X &&
124 test_chmod +x A &&
125 test_tick &&
126 git commit -m modechange &&
127 GIT_TRACE=1 git rebase main
130 test_expect_success 'rebase is not broken by diff.renames' '
131 test_config diff.renames copies &&
132 git checkout filemove &&
133 GIT_TRACE=1 git rebase force-3way
136 test_expect_success 'setup: recover' '
137 test_might_fail git rebase --abort &&
138 git reset --hard &&
139 git checkout modechange
142 test_expect_success 'Show verbose error when HEAD could not be detached' '
143 >B &&
144 test_when_finished "rm -f B" &&
145 test_must_fail git rebase topic 2>output.err >output.out &&
146 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" output.err &&
147 test_i18ngrep B output.err
150 test_expect_success 'fail when upstream arg is missing and not on branch' '
151 git checkout topic &&
152 test_must_fail git rebase
155 test_expect_success 'fail when upstream arg is missing and not configured' '
156 git checkout -b no-config topic &&
157 test_must_fail git rebase
160 test_expect_success 'rebase works with format.useAutoBase' '
161 test_config format.useAutoBase true &&
162 git checkout topic &&
163 git rebase main
166 test_expect_success 'default to common base in @{upstream}s reflog if no upstream arg (--merge)' '
167 git checkout -b default-base main &&
168 git checkout -b default topic &&
169 git config branch.default.remote . &&
170 git config branch.default.merge refs/heads/default-base &&
171 git rebase --merge &&
172 git rev-parse --verify default-base >expect &&
173 git rev-parse default~1 >actual &&
174 test_cmp expect actual &&
175 git checkout default-base &&
176 git reset --hard HEAD^ &&
177 git checkout default &&
178 git rebase --merge &&
179 git rev-parse --verify default-base >expect &&
180 git rev-parse default~1 >actual &&
181 test_cmp expect actual
184 test_expect_success 'default to common base in @{upstream}s reflog if no upstream arg (--apply)' '
185 git checkout -B default-base main &&
186 git checkout -B default topic &&
187 git config branch.default.remote . &&
188 git config branch.default.merge refs/heads/default-base &&
189 git rebase --apply &&
190 git rev-parse --verify default-base >expect &&
191 git rev-parse default~1 >actual &&
192 test_cmp expect actual &&
193 git checkout default-base &&
194 git reset --hard HEAD^ &&
195 git checkout default &&
196 git rebase --apply &&
197 git rev-parse --verify default-base >expect &&
198 git rev-parse default~1 >actual &&
199 test_cmp expect actual
202 test_expect_success 'cherry-picked commits and fork-point work together' '
203 git checkout default-base &&
204 echo Amended >A &&
205 git commit -a --no-edit --amend &&
206 test_commit B B &&
207 test_commit new_B B "New B" &&
208 test_commit C C &&
209 git checkout default &&
210 git reset --hard default-base@{4} &&
211 test_commit D D &&
212 git cherry-pick -2 default-base^ &&
213 test_commit final_B B "Final B" &&
214 git rebase &&
215 echo Amended >expect &&
216 test_cmp expect A &&
217 echo "Final B" >expect &&
218 test_cmp expect B &&
219 echo C >expect &&
220 test_cmp expect C &&
221 echo D >expect &&
222 test_cmp expect D
225 test_expect_success 'rebase --apply -q is quiet' '
226 git checkout -b quiet topic &&
227 git rebase --apply -q main >output.out 2>&1 &&
228 test_must_be_empty output.out
231 test_expect_success 'rebase --merge -q is quiet' '
232 git checkout -B quiet topic &&
233 git rebase --merge -q main >output.out 2>&1 &&
234 test_must_be_empty output.out
237 test_expect_success 'Rebase a commit that sprinkles CRs in' '
239 echo "One" &&
240 echo "TwoQ" &&
241 echo "Three" &&
242 echo "FQur" &&
243 echo "Five"
244 ) | q_to_cr >CR &&
245 git add CR &&
246 test_tick &&
247 git commit -a -m "A file with a line with CR" &&
248 git tag file-with-cr &&
249 git checkout HEAD^0 &&
250 git rebase --onto HEAD^^ HEAD^ &&
251 git diff --exit-code file-with-cr:CR HEAD:CR
254 test_expect_success 'rebase can copy notes' '
255 git config notes.rewrite.rebase true &&
256 git config notes.rewriteRef "refs/notes/*" &&
257 test_commit n1 &&
258 test_commit n2 &&
259 test_commit n3 &&
260 git notes add -m"a note" n3 &&
261 git rebase --onto n1 n2 &&
262 test "a note" = "$(git notes show HEAD)"
265 test_expect_success 'rebase -m can copy notes' '
266 git reset --hard n3 &&
267 git rebase -m --onto n1 n2 &&
268 test "a note" = "$(git notes show HEAD)"
271 test_expect_success 'rebase commit with an ancient timestamp' '
272 git reset --hard &&
274 >old.one && git add old.one && test_tick &&
275 git commit --date="@12345 +0400" -m "Old one" &&
276 >old.two && git add old.two && test_tick &&
277 git commit --date="@23456 +0500" -m "Old two" &&
278 >old.three && git add old.three && test_tick &&
279 git commit --date="@34567 +0600" -m "Old three" &&
281 git cat-file commit HEAD^^ >actual &&
282 grep "author .* 12345 +0400$" actual &&
283 git cat-file commit HEAD^ >actual &&
284 grep "author .* 23456 +0500$" actual &&
285 git cat-file commit HEAD >actual &&
286 grep "author .* 34567 +0600$" actual &&
288 git rebase --onto HEAD^^ HEAD^ &&
290 git cat-file commit HEAD >actual &&
291 grep "author .* 34567 +0600$" actual
294 test_expect_success 'rebase with "From " line in commit message' '
295 git checkout -b preserve-from main~1 &&
296 cat >From_.msg <<EOF &&
297 Somebody embedded an mbox in a commit message
299 This is from so-and-so:
301 From a@b Mon Sep 17 00:00:00 2001
302 From: John Doe <nobody@example.com>
303 Date: Sat, 11 Nov 2017 00:00:00 +0000
304 Subject: not this message
306 something
308 >From_ &&
309 git add From_ &&
310 git commit -F From_.msg &&
311 git rebase main &&
312 git log -1 --pretty=format:%B >out &&
313 test_cmp From_.msg out
316 test_expect_success 'rebase --apply and --show-current-patch' '
317 test_create_repo conflict-apply &&
319 cd conflict-apply &&
320 test_commit init &&
321 echo one >>init.t &&
322 git commit -a -m one &&
323 echo two >>init.t &&
324 git commit -a -m two &&
325 git tag two &&
326 test_must_fail git rebase --apply -f --onto init HEAD^ &&
327 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
328 grep "show.*$(git rev-parse two)" stderr
332 test_expect_success 'rebase --apply and .gitattributes' '
333 test_create_repo attributes &&
335 cd attributes &&
336 test_commit init &&
337 git config filter.test.clean "sed -e '\''s/smudged/clean/g'\''" &&
338 git config filter.test.smudge "sed -e '\''s/clean/smudged/g'\''" &&
340 test_commit second &&
341 git checkout -b test HEAD^ &&
343 echo "*.txt filter=test" >.gitattributes &&
344 git add .gitattributes &&
345 test_commit third &&
347 echo "This text is smudged." >a.txt &&
348 git add a.txt &&
349 test_commit fourth &&
351 git checkout -b removal HEAD^ &&
352 git rm .gitattributes &&
353 git add -u &&
354 test_commit fifth &&
355 git cherry-pick test &&
357 git checkout test &&
358 git rebase main &&
359 grep "smudged" a.txt &&
361 git checkout removal &&
362 git reset --hard &&
363 git rebase main &&
364 grep "clean" a.txt
368 test_expect_success 'rebase--merge.sh and --show-current-patch' '
369 test_create_repo conflict-merge &&
371 cd conflict-merge &&
372 test_commit init &&
373 echo one >>init.t &&
374 git commit -a -m one &&
375 echo two >>init.t &&
376 git commit -a -m two &&
377 git tag two &&
378 test_must_fail git rebase --merge --onto init HEAD^ &&
379 git rebase --show-current-patch >actual.patch &&
380 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
381 grep "show.*REBASE_HEAD" stderr &&
382 test "$(git rev-parse REBASE_HEAD)" = "$(git rev-parse two)"
386 test_expect_success 'switch to branch checked out here' '
387 git checkout main &&
388 git rebase main main
391 test_expect_success 'switch to branch checked out elsewhere fails' '
392 test_when_finished "
393 git worktree remove wt1 &&
394 git worktree remove wt2 &&
395 git branch -d shared
396 " &&
397 git worktree add wt1 -b shared &&
398 git worktree add wt2 -f shared &&
399 # we test in both worktrees to ensure that works
400 # as expected with "first" and "next" worktrees
401 test_must_fail git -C wt1 rebase shared shared &&
402 test_must_fail git -C wt2 rebase shared shared
405 test_expect_success 'switch to branch not checked out' '
406 git checkout main &&
407 git branch other &&
408 git rebase main other
411 test_expect_success 'switch to non-branch detaches HEAD' '
412 git checkout main &&
413 old_main=$(git rev-parse HEAD) &&
414 git rebase First Second^0 &&
415 test_cmp_rev HEAD Second &&
416 test_cmp_rev main $old_main &&
417 test_must_fail git symbolic-ref HEAD
420 test_expect_success 'refuse to switch to branch checked out elsewhere' '
421 git checkout main &&
422 git worktree add wt &&
423 test_must_fail git -C wt rebase main main 2>err &&
424 test_i18ngrep "already checked out" err
427 test_expect_success MINGW,SYMLINKS_WINDOWS 'rebase when .git/logs is a symlink' '
428 git checkout main &&
429 mv .git/logs actual_logs &&
430 cmd //c "mklink /D .git\logs ..\actual_logs" &&
431 git rebase -f HEAD^ &&
432 test -L .git/logs &&
433 rm .git/logs &&
434 mv actual_logs .git/logs
437 test_expect_success 'rebase when inside worktree subdirectory' '
438 git init main-wt &&
440 cd main-wt &&
441 git commit --allow-empty -m "initial" &&
442 mkdir -p foo/bar &&
443 test_commit foo/bar/baz &&
444 mkdir -p a/b &&
445 test_commit a/b/c &&
446 # create another branch for our other worktree
447 git branch other &&
448 git worktree add ../other-wt other &&
449 cd ../other-wt &&
450 # create and cd into a subdirectory
451 mkdir -p random/dir &&
452 cd random/dir &&
453 # now do the rebase
454 git rebase --onto HEAD^^ HEAD^ # drops the HEAD^ commit
458 test_done