setup: merge configuration of repository formats
[git/gitster.git] / t / t3400-rebase.sh
blobae34bfad6071fbb40973eb3b01f1d28e360aa0d9
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_PASSES_SANITIZE_LEAK=true
15 . ./test-lib.sh
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 &&
24 echo Dummy >Y &&
25 git update-index --add Y &&
26 git commit -m "Add Y." &&
27 git checkout -b filemove &&
28 git reset --soft main &&
29 mkdir D &&
30 git mv A D/A &&
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 &&
35 echo Third >>A &&
36 git update-index A &&
37 git commit -m "Modify A." &&
38 git checkout -b side my-topic-branch &&
39 echo Side >>C &&
40 git add C &&
41 git commit -m "Add C" &&
42 git checkout -f my-topic-branch &&
43 git tag topic
46 test_expect_success 'rebase on dirty worktree' '
47 echo dirty >>A &&
48 test_must_fail git rebase main
51 test_expect_success 'rebase on dirty cache' '
52 git add A &&
53 test_must_fail git rebase main
56 test_expect_success 'rebase against main' '
57 git reset --hard HEAD &&
58 git rebase main
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)" &&
64 git rebase main &&
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)' "
85 git log -1 |
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 &&
95 git rebase main
98 test_expect_success 'rebase off of the previous branch using "-"' '
99 git checkout main &&
100 git checkout HEAD^ &&
101 git rebase @{-1} >expect.messages &&
102 git merge-base main HEAD >expect.forkpoint &&
104 git checkout main &&
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' '
116 git checkout main &&
117 git branch -D topic &&
118 echo 1 >X &&
119 git add X &&
120 test_tick &&
121 git commit -m prepare &&
122 git checkout -b modechange HEAD^ &&
123 echo 1 >X &&
124 git add X &&
125 test_chmod +x A &&
126 test_tick &&
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 &&
139 git reset --hard &&
140 git checkout modechange
143 test_expect_success 'Show verbose error when HEAD could not be detached' '
144 >B &&
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 &&
164 git rebase main
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 &&
205 echo Amended >A &&
206 git commit -a --no-edit --amend &&
207 test_commit B B &&
208 test_commit new_B B "New B" &&
209 test_commit C C &&
210 git checkout default &&
211 git reset --hard default-base@{4} &&
212 test_commit D D &&
213 git cherry-pick -2 default-base^ &&
214 test_commit final_B B "Final B" &&
215 git rebase &&
216 echo Amended >expect &&
217 test_cmp expect A &&
218 echo "Final B" >expect &&
219 test_cmp expect B &&
220 echo C >expect &&
221 test_cmp expect C &&
222 echo D >expect &&
223 test_cmp expect D
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' '
240 echo "One" &&
241 echo "TwoQ" &&
242 echo "Three" &&
243 echo "FQur" &&
244 echo "Five"
245 ) | q_to_cr >CR &&
246 git add CR &&
247 test_tick &&
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/*" &&
258 test_commit n1 &&
259 test_commit n2 &&
260 test_commit n3 &&
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' '
273 git reset --hard &&
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
307 something
309 >From_ &&
310 git add From_ &&
311 git commit -F From_.msg &&
312 git rebase main &&
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 &&
320 cd conflict-apply &&
321 test_commit init &&
322 echo one >>init.t &&
323 git commit -a -m one &&
324 echo two >>init.t &&
325 git commit -a -m two &&
326 git tag 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 &&
336 cd attributes &&
337 test_commit init &&
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 &&
346 test_commit third &&
348 echo "This text is smudged." >a.txt &&
349 git add a.txt &&
350 test_commit fourth &&
352 git checkout -b removal HEAD^ &&
353 git rm .gitattributes &&
354 git add -u &&
355 test_commit fifth &&
356 git cherry-pick test &&
358 git checkout test &&
359 git rebase main &&
360 grep "smudged" a.txt &&
362 git checkout removal &&
363 git reset --hard &&
364 git rebase main &&
365 grep "clean" a.txt
369 test_expect_success 'rebase--merge.sh and --show-current-patch' '
370 test_create_repo conflict-merge &&
372 cd conflict-merge &&
373 test_commit init &&
374 echo one >>init.t &&
375 git commit -a -m one &&
376 echo two >>init.t &&
377 git commit -a -m two &&
378 git tag 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' '
388 git checkout main &&
389 git rebase main main
392 test_expect_success 'switch to branch checked out elsewhere fails' '
393 test_when_finished "
394 git worktree remove wt1 &&
395 git worktree remove wt2 &&
396 git branch -d shared
397 " &&
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' '
407 git checkout main &&
408 git branch other &&
409 git rebase main other
412 test_expect_success 'switch to non-branch detaches HEAD' '
413 git checkout main &&
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' '
422 git checkout main &&
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' '
429 git init main-wt &&
431 cd main-wt &&
432 git commit --allow-empty -m "initial" &&
433 mkdir -p foo/bar &&
434 test_commit foo/bar/baz &&
435 mkdir -p a/b &&
436 test_commit a/b/c &&
437 # create another branch for our other worktree
438 git branch other &&
439 git worktree add ../other-wt other &&
440 cd ../other-wt &&
441 # create and cd into a subdirectory
442 mkdir -p random/dir &&
443 cd random/dir &&
444 # now do the rebase
445 git rebase --onto HEAD^^ HEAD^ # drops the HEAD^ commit
449 test_done