commit-graph: when incompatible with graphs, indicate why
[git] / t / t3400-rebase.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Amos Waterland
4 #
5
6 test_description='git rebase assorted tests
7
8 This test runs git rebase and checks that the author information is not lost
9 among other things.
10 '
11 . ./test-lib.sh
12
13 GIT_AUTHOR_NAME=author@name
14 GIT_AUTHOR_EMAIL=bogus@email@address
15 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
16
17 test_expect_success 'prepare repository with topic branches' '
18         git config core.logAllRefUpdates true &&
19         echo First >A &&
20         git update-index --add A &&
21         git commit -m "Add A." &&
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 master &&
28         mkdir D &&
29         git mv A D/A &&
30         git commit -m "Move A." &&
31         git checkout -b my-topic-branch master &&
32         echo Second >B &&
33         git update-index --add B &&
34         git commit -m "Add B." &&
35         git checkout -f master &&
36         echo Third >>A &&
37         git update-index A &&
38         git commit -m "Modify A." &&
39         git checkout -b side my-topic-branch &&
40         echo Side >>C &&
41         git add C &&
42         git commit -m "Add C" &&
43         git checkout -f my-topic-branch &&
44         git tag topic
45 '
46
47 test_expect_success 'rebase on dirty worktree' '
48         echo dirty >>A &&
49         test_must_fail git rebase master
50 '
51
52 test_expect_success 'rebase on dirty cache' '
53         git add A &&
54         test_must_fail git rebase master
55 '
56
57 test_expect_success 'rebase against master' '
58         git reset --hard HEAD &&
59         git rebase master
60 '
61
62 test_expect_success 'rebase sets ORIG_HEAD to pre-rebase state' '
63         git checkout -b orig-head topic &&
64         pre="$(git rev-parse --verify HEAD)" &&
65         git rebase master &&
66         test_cmp_rev "$pre" ORIG_HEAD &&
67         test_cmp_rev ! "$pre" HEAD
68 '
69
70 test_expect_success 'rebase, with <onto> and <upstream> specified as :/quuxery' '
71         test_when_finished "git branch -D torebase" &&
72         git checkout -b torebase my-topic-branch^ &&
73         upstream=$(git rev-parse ":/Add B") &&
74         onto=$(git rev-parse ":/Add A") &&
75         git rebase --onto $onto $upstream &&
76         git reset --hard my-topic-branch^ &&
77         git rebase --onto ":/Add A" ":/Add B" &&
78         git checkout my-topic-branch
79 '
80
81 test_expect_success 'the rebase operation should not have destroyed author information' '
82         ! (git log | grep "Author:" | grep "<>")
83 '
84
85 test_expect_success 'the rebase operation should not have destroyed author information (2)' "
86         git log -1 |
87         grep 'Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>'
88 "
89
90 test_expect_success 'HEAD was detached during rebase' '
91         test $(git rev-parse HEAD@{1}) != $(git rev-parse my-topic-branch@{1})
92 '
93
94 test_expect_success 'rebase from ambiguous branch name' '
95         git checkout -b topic side &&
96         git rebase master
97 '
98
99 test_expect_success 'rebase off of the previous branch using "-"' '
100         git checkout master &&
101         git checkout HEAD^ &&
102         git rebase @{-1} >expect.messages &&
103         git merge-base master HEAD >expect.forkpoint &&
104
105         git checkout master &&
106         git checkout HEAD^ &&
107         git rebase - >actual.messages &&
108         git merge-base master HEAD >actual.forkpoint &&
109
110         test_cmp expect.forkpoint actual.forkpoint &&
111         # the next one is dubious---we may want to say "-",
112         # instead of @{-1}, in the message
113         test_i18ncmp expect.messages actual.messages
114 '
115
116 test_expect_success 'rebase a single mode change' '
117         git checkout master &&
118         git branch -D topic &&
119         echo 1 >X &&
120         git add X &&
121         test_tick &&
122         git commit -m prepare &&
123         git checkout -b modechange HEAD^ &&
124         echo 1 >X &&
125         git add X &&
126         test_chmod +x A &&
127         test_tick &&
128         git commit -m modechange &&
129         GIT_TRACE=1 git rebase master
130 '
131
132 test_expect_success 'rebase is not broken by diff.renames' '
133         test_config diff.renames copies &&
134         git checkout filemove &&
135         GIT_TRACE=1 git rebase force-3way
136 '
137
138 test_expect_success 'setup: recover' '
139         test_might_fail git rebase --abort &&
140         git reset --hard &&
141         git checkout modechange
142 '
143
144 test_expect_success 'Show verbose error when HEAD could not be detached' '
145         >B &&
146         test_when_finished "rm -f B" &&
147         test_must_fail git rebase topic 2>output.err >output.out &&
148         test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" output.err &&
149         test_i18ngrep B output.err
150 '
151
152 test_expect_success 'fail when upstream arg is missing and not on branch' '
153         git checkout topic &&
154         test_must_fail git rebase
155 '
156
157 test_expect_success 'fail when upstream arg is missing and not configured' '
158         git checkout -b no-config topic &&
159         test_must_fail git rebase
160 '
161
162 test_expect_success 'rebase works with format.useAutoBase' '
163         test_config format.useAutoBase true &&
164         git checkout topic &&
165         git rebase master
166 '
167
168 test_expect_success 'default to common base in @{upstream}s reflog if no upstream arg (--merge)' '
169         git checkout -b default-base master &&
170         git checkout -b default topic &&
171         git config branch.default.remote . &&
172         git config branch.default.merge refs/heads/default-base &&
173         git rebase --merge &&
174         git rev-parse --verify default-base >expect &&
175         git rev-parse default~1 >actual &&
176         test_cmp expect actual &&
177         git checkout default-base &&
178         git reset --hard HEAD^ &&
179         git checkout default &&
180         git rebase --merge &&
181         git rev-parse --verify default-base >expect &&
182         git rev-parse default~1 >actual &&
183         test_cmp expect actual
184 '
185
186 test_expect_success 'default to common base in @{upstream}s reflog if no upstream arg (--apply)' '
187         git checkout -B default-base master &&
188         git checkout -B default topic &&
189         git config branch.default.remote . &&
190         git config branch.default.merge refs/heads/default-base &&
191         git rebase --apply &&
192         git rev-parse --verify default-base >expect &&
193         git rev-parse default~1 >actual &&
194         test_cmp expect actual &&
195         git checkout default-base &&
196         git reset --hard HEAD^ &&
197         git checkout default &&
198         git rebase --apply &&
199         git rev-parse --verify default-base >expect &&
200         git rev-parse default~1 >actual &&
201         test_cmp expect actual
202 '
203
204 test_expect_success 'cherry-picked commits and fork-point work together' '
205         git checkout default-base &&
206         echo Amended >A &&
207         git commit -a --no-edit --amend &&
208         test_commit B B &&
209         test_commit new_B B "New B" &&
210         test_commit C C &&
211         git checkout default &&
212         git reset --hard default-base@{4} &&
213         test_commit D D &&
214         git cherry-pick -2 default-base^ &&
215         test_commit final_B B "Final B" &&
216         git rebase &&
217         echo Amended >expect &&
218         test_cmp expect A &&
219         echo "Final B" >expect &&
220         test_cmp expect B &&
221         echo C >expect &&
222         test_cmp expect C &&
223         echo D >expect &&
224         test_cmp expect D
225 '
226
227 test_expect_success 'rebase --apply -q is quiet' '
228         git checkout -b quiet topic &&
229         git rebase --apply -q master >output.out 2>&1 &&
230         test_must_be_empty output.out
231 '
232
233 test_expect_success 'rebase --merge -q is quiet' '
234         git checkout -B quiet topic &&
235         git rebase --merge -q master >output.out 2>&1 &&
236         test_must_be_empty output.out
237 '
238
239 test_expect_success 'Rebase a commit that sprinkles CRs in' '
240         (
241                 echo "One" &&
242                 echo "TwoQ" &&
243                 echo "Three" &&
244                 echo "FQur" &&
245                 echo "Five"
246         ) | q_to_cr >CR &&
247         git add CR &&
248         test_tick &&
249         git commit -a -m "A file with a line with CR" &&
250         git tag file-with-cr &&
251         git checkout HEAD^0 &&
252         git rebase --onto HEAD^^ HEAD^ &&
253         git diff --exit-code file-with-cr:CR HEAD:CR
254 '
255
256 test_expect_success 'rebase can copy notes' '
257         git config notes.rewrite.rebase true &&
258         git config notes.rewriteRef "refs/notes/*" &&
259         test_commit n1 &&
260         test_commit n2 &&
261         test_commit n3 &&
262         git notes add -m"a note" n3 &&
263         git rebase --onto n1 n2 &&
264         test "a note" = "$(git notes show HEAD)"
265 '
266
267 test_expect_success 'rebase -m can copy notes' '
268         git reset --hard n3 &&
269         git rebase -m --onto n1 n2 &&
270         test "a note" = "$(git notes show HEAD)"
271 '
272
273 test_expect_success 'rebase commit with an ancient timestamp' '
274         git reset --hard &&
275
276         >old.one && git add old.one && test_tick &&
277         git commit --date="@12345 +0400" -m "Old one" &&
278         >old.two && git add old.two && test_tick &&
279         git commit --date="@23456 +0500" -m "Old two" &&
280         >old.three && git add old.three && test_tick &&
281         git commit --date="@34567 +0600" -m "Old three" &&
282
283         git cat-file commit HEAD^^ >actual &&
284         grep "author .* 12345 +0400$" actual &&
285         git cat-file commit HEAD^ >actual &&
286         grep "author .* 23456 +0500$" actual &&
287         git cat-file commit HEAD >actual &&
288         grep "author .* 34567 +0600$" actual &&
289
290         git rebase --onto HEAD^^ HEAD^ &&
291
292         git cat-file commit HEAD >actual &&
293         grep "author .* 34567 +0600$" actual
294 '
295
296 test_expect_success 'rebase with "From " line in commit message' '
297         git checkout -b preserve-from master~1 &&
298         cat >From_.msg <<EOF &&
299 Somebody embedded an mbox in a commit message
300
301 This is from so-and-so:
302
303 From a@b Mon Sep 17 00:00:00 2001
304 From: John Doe <nobody@example.com>
305 Date: Sat, 11 Nov 2017 00:00:00 +0000
306 Subject: not this message
307
308 something
309 EOF
310         >From_ &&
311         git add From_ &&
312         git commit -F From_.msg &&
313         git rebase master &&
314         git log -1 --pretty=format:%B >out &&
315         test_cmp From_.msg out
316 '
317
318 test_expect_success 'rebase --apply and --show-current-patch' '
319         test_create_repo conflict-apply &&
320         (
321                 cd conflict-apply &&
322                 test_commit init &&
323                 echo one >>init.t &&
324                 git commit -a -m one &&
325                 echo two >>init.t &&
326                 git commit -a -m two &&
327                 git tag two &&
328                 test_must_fail git rebase --apply -f --onto init HEAD^ &&
329                 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
330                 grep "show.*$(git rev-parse two)" stderr
331         )
332 '
333
334 test_expect_success 'rebase --apply and .gitattributes' '
335         test_create_repo attributes &&
336         (
337                 cd attributes &&
338                 test_commit init &&
339                 git config filter.test.clean "sed -e '\''s/smudged/clean/g'\''" &&
340                 git config filter.test.smudge "sed -e '\''s/clean/smudged/g'\''" &&
341
342                 test_commit second &&
343                 git checkout -b test HEAD^ &&
344
345                 echo "*.txt filter=test" >.gitattributes &&
346                 git add .gitattributes &&
347                 test_commit third &&
348
349                 echo "This text is smudged." >a.txt &&
350                 git add a.txt &&
351                 test_commit fourth &&
352
353                 git checkout -b removal HEAD^ &&
354                 git rm .gitattributes &&
355                 git add -u &&
356                 test_commit fifth &&
357                 git cherry-pick test &&
358
359                 git checkout test &&
360                 git rebase master &&
361                 grep "smudged" a.txt &&
362
363                 git checkout removal &&
364                 git reset --hard &&
365                 git rebase master &&
366                 grep "clean" a.txt
367         )
368 '
369
370 test_expect_success 'rebase--merge.sh and --show-current-patch' '
371         test_create_repo conflict-merge &&
372         (
373                 cd conflict-merge &&
374                 test_commit init &&
375                 echo one >>init.t &&
376                 git commit -a -m one &&
377                 echo two >>init.t &&
378                 git commit -a -m two &&
379                 git tag two &&
380                 test_must_fail git rebase --merge --onto init HEAD^ &&
381                 git rebase --show-current-patch >actual.patch &&
382                 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
383                 grep "show.*REBASE_HEAD" stderr &&
384                 test "$(git rev-parse REBASE_HEAD)" = "$(git rev-parse two)"
385         )
386 '
387
388 test_expect_success 'rebase -c rebase.useBuiltin=false warning' '
389         expected="rebase.useBuiltin support has been removed" &&
390
391         # Only warn when the legacy rebase is requested...
392         test_must_fail git -c rebase.useBuiltin=false rebase 2>err &&
393         test_i18ngrep "$expected" err &&
394         test_must_fail env GIT_TEST_REBASE_USE_BUILTIN=false git rebase 2>err &&
395         test_i18ngrep "$expected" err &&
396
397         # ...not when we would have used the built-in anyway
398         test_must_fail git -c rebase.useBuiltin=true rebase 2>err &&
399         test_must_be_empty err &&
400         test_must_fail env GIT_TEST_REBASE_USE_BUILTIN=true git rebase 2>err &&
401         test_must_be_empty err
402 '
403
404 test_expect_success 'switch to branch checked out here' '
405         git checkout master &&
406         git rebase master master
407 '
408
409 test_expect_success 'switch to branch not checked out' '
410         git checkout master &&
411         git branch other &&
412         git rebase master other
413 '
414
415 test_expect_success 'refuse to switch to branch checked out elsewhere' '
416         git checkout master &&
417         git worktree add wt &&
418         test_must_fail git -C wt rebase master master 2>err &&
419         test_i18ngrep "already checked out" err
420 '
421
422 test_done