submodule: rewrite `module_list` shell function in C
[git] / t / t4150-am.sh
1 #!/bin/sh
2
3 test_description='git am running'
4
5 . ./test-lib.sh
6
7 test_expect_success 'setup: messages' '
8         cat >msg <<-\EOF &&
9         second
10
11         Lorem ipsum dolor sit amet, consectetuer sadipscing elitr, sed diam nonumy
12         eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
13         voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
14         kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem
15         ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
16         tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
17         vero eos et accusam et justo duo dolores et ea rebum.
18
19         EOF
20         qz_to_tab_space <<-\EOF >>msg &&
21         QDuis autem vel eum iriure dolor in hendrerit in vulputate velit
22         Qesse molestie consequat, vel illum dolore eu feugiat nulla facilisis
23         Qat vero eros et accumsan et iusto odio dignissim qui blandit
24         Qpraesent luptatum zzril delenit augue duis dolore te feugait nulla
25         Qfacilisi.
26         EOF
27         cat >>msg <<-\EOF &&
28
29         Lorem ipsum dolor sit amet,
30         consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
31         laoreet dolore magna aliquam erat volutpat.
32
33           git
34           ---
35           +++
36
37         Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
38         lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure
39         dolor in hendrerit in vulputate velit esse molestie consequat, vel illum
40         dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio
41         dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te
42         feugait nulla facilisi.
43         EOF
44
45         cat >failmail <<-\EOF &&
46         From foo@example.com Fri May 23 10:43:49 2008
47         From:   foo@example.com
48         To:     bar@example.com
49         Subject: Re: [RFC/PATCH] git-foo.sh
50         Date:   Fri, 23 May 2008 05:23:42 +0200
51
52         Sometimes we have to find out that there'\''s nothing left.
53
54         EOF
55
56         cat >pine <<-\EOF &&
57         From MAILER-DAEMON Fri May 23 10:43:49 2008
58         Date: 23 May 2008 05:23:42 +0200
59         From: Mail System Internal Data <MAILER-DAEMON@example.com>
60         Subject: DON'\''T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA
61         Message-ID: <foo-0001@example.com>
62
63         This text is part of the internal format of your mail folder, and is not
64         a real message.  It is created automatically by the mail system software.
65         If deleted, important folder data will be lost, and it will be re-created
66         with the data reset to initial values.
67
68         EOF
69
70         cat >scissors-msg <<-\EOF &&
71         Test git-am with scissors line
72
73         This line should be included in the commit message.
74         EOF
75
76         cat - scissors-msg >no-scissors-msg <<-\EOF &&
77         This line should not be included in the commit message with --scissors enabled.
78
79          - - >8 - - remove everything above this line - - >8 - -
80
81         EOF
82
83         signoff="Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
84 '
85
86 test_expect_success setup '
87         echo hello >file &&
88         git add file &&
89         test_tick &&
90         git commit -m first &&
91         git tag first &&
92
93         echo world >>file &&
94         git add file &&
95         test_tick &&
96         git commit -s -F msg &&
97         git tag second &&
98
99         git format-patch --stdout first >patch1 &&
100         {
101                 echo "Message-Id: <1226501681-24923-1-git-send-email-bda@mnsspb.ru>" &&
102                 echo "X-Fake-Field: Line One" &&
103                 echo "X-Fake-Field: Line Two" &&
104                 echo "X-Fake-Field: Line Three" &&
105                 git format-patch --stdout first | sed -e "1d"
106         } > patch1.eml &&
107         {
108                 echo "X-Fake-Field: Line One" &&
109                 echo "X-Fake-Field: Line Two" &&
110                 echo "X-Fake-Field: Line Three" &&
111                 git format-patch --stdout first | sed -e "1d"
112         } | append_cr >patch1-crlf.eml &&
113         {
114                 printf "%255s\\n" ""
115                 echo "X-Fake-Field: Line One" &&
116                 echo "X-Fake-Field: Line Two" &&
117                 echo "X-Fake-Field: Line Three" &&
118                 git format-patch --stdout first | sed -e "1d"
119         } > patch1-ws.eml &&
120         {
121                 sed -ne "1p" msg &&
122                 echo &&
123                 echo "From: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" &&
124                 echo "Date: $GIT_AUTHOR_DATE" &&
125                 echo &&
126                 sed -e "1,2d" msg &&
127                 echo &&
128                 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" &&
129                 echo "---" &&
130                 git diff-tree --no-commit-id --stat -p second
131         } >patch1-stgit.eml &&
132         mkdir stgit-series &&
133         cp patch1-stgit.eml stgit-series/patch &&
134         {
135                 echo "# This series applies on GIT commit $(git rev-parse first)" &&
136                 echo "patch"
137         } >stgit-series/series &&
138         {
139                 echo "# HG changeset patch" &&
140                 echo "# User $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" &&
141                 echo "# Date $test_tick 25200" &&
142                 echo "#      $(git show --pretty="%aD" -s second)" &&
143                 echo "# Node ID $_z40" &&
144                 echo "# Parent  $_z40" &&
145                 cat msg &&
146                 echo &&
147                 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" &&
148                 echo &&
149                 git diff-tree --no-commit-id -p second
150         } >patch1-hg.eml &&
151
152
153         echo scissors-file >scissors-file &&
154         git add scissors-file &&
155         git commit -F scissors-msg &&
156         git tag scissors &&
157         git format-patch --stdout scissors^ >scissors-patch.eml &&
158         git reset --hard HEAD^ &&
159
160         echo no-scissors-file >no-scissors-file &&
161         git add no-scissors-file &&
162         git commit -F no-scissors-msg &&
163         git tag no-scissors &&
164         git format-patch --stdout no-scissors^ >no-scissors-patch.eml &&
165         git reset --hard HEAD^ &&
166
167         sed -n -e "3,\$p" msg >file &&
168         git add file &&
169         test_tick &&
170         git commit -m third &&
171
172         git format-patch --stdout first >patch2 &&
173
174         git checkout -b lorem &&
175         sed -n -e "11,\$p" msg >file &&
176         head -n 9 msg >>file &&
177         test_tick &&
178         git commit -a -m "moved stuff" &&
179
180         echo goodbye >another &&
181         git add another &&
182         test_tick &&
183         git commit -m "added another file" &&
184
185         git format-patch --stdout master >lorem-move.patch &&
186         git format-patch --no-prefix --stdout master >lorem-zero.patch &&
187
188         git checkout -b rename &&
189         git mv file renamed &&
190         git commit -m "renamed a file" &&
191
192         git format-patch -M --stdout lorem >rename.patch &&
193
194         git reset --soft lorem^ &&
195         git commit -m "renamed a file and added another" &&
196
197         git format-patch -M --stdout lorem^ >rename-add.patch &&
198
199         # reset time
200         sane_unset test_tick &&
201         test_tick
202 '
203
204 test_expect_success 'am applies patch correctly' '
205         rm -fr .git/rebase-apply &&
206         git reset --hard &&
207         git checkout first &&
208         test_tick &&
209         git am <patch1 &&
210         test_path_is_missing .git/rebase-apply &&
211         git diff --exit-code second &&
212         test "$(git rev-parse second)" = "$(git rev-parse HEAD)" &&
213         test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
214 '
215
216 test_expect_success 'am fails if index is dirty' '
217         test_when_finished "rm -f dirtyfile" &&
218         rm -fr .git/rebase-apply &&
219         git reset --hard &&
220         git checkout first &&
221         echo dirtyfile >dirtyfile &&
222         git add dirtyfile &&
223         test_must_fail git am patch1 &&
224         test_path_is_dir .git/rebase-apply &&
225         test_cmp_rev first HEAD
226 '
227
228 test_expect_success 'am applies patch e-mail not in a mbox' '
229         rm -fr .git/rebase-apply &&
230         git reset --hard &&
231         git checkout first &&
232         git am patch1.eml &&
233         test_path_is_missing .git/rebase-apply &&
234         git diff --exit-code second &&
235         test "$(git rev-parse second)" = "$(git rev-parse HEAD)" &&
236         test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
237 '
238
239 test_expect_success 'am applies patch e-mail not in a mbox with CRLF' '
240         rm -fr .git/rebase-apply &&
241         git reset --hard &&
242         git checkout first &&
243         git am patch1-crlf.eml &&
244         test_path_is_missing .git/rebase-apply &&
245         git diff --exit-code second &&
246         test "$(git rev-parse second)" = "$(git rev-parse HEAD)" &&
247         test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
248 '
249
250 test_expect_success 'am applies patch e-mail with preceding whitespace' '
251         rm -fr .git/rebase-apply &&
252         git reset --hard &&
253         git checkout first &&
254         git am patch1-ws.eml &&
255         test_path_is_missing .git/rebase-apply &&
256         git diff --exit-code second &&
257         test "$(git rev-parse second)" = "$(git rev-parse HEAD)" &&
258         test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
259 '
260
261 test_expect_success 'am applies stgit patch' '
262         rm -fr .git/rebase-apply &&
263         git checkout -f first &&
264         git am patch1-stgit.eml &&
265         test_path_is_missing .git/rebase-apply &&
266         git diff --exit-code second &&
267         test_cmp_rev second HEAD &&
268         test_cmp_rev second^ HEAD^
269 '
270
271 test_expect_success 'am --patch-format=stgit applies stgit patch' '
272         rm -fr .git/rebase-apply &&
273         git checkout -f first &&
274         git am --patch-format=stgit <patch1-stgit.eml &&
275         test_path_is_missing .git/rebase-apply &&
276         git diff --exit-code second &&
277         test_cmp_rev second HEAD &&
278         test_cmp_rev second^ HEAD^
279 '
280
281 test_expect_success 'am applies stgit series' '
282         rm -fr .git/rebase-apply &&
283         git checkout -f first &&
284         git am stgit-series/series &&
285         test_path_is_missing .git/rebase-apply &&
286         git diff --exit-code second &&
287         test_cmp_rev second HEAD &&
288         test_cmp_rev second^ HEAD^
289 '
290
291 test_expect_success 'am applies hg patch' '
292         rm -fr .git/rebase-apply &&
293         git checkout -f first &&
294         git am patch1-hg.eml &&
295         test_path_is_missing .git/rebase-apply &&
296         git diff --exit-code second &&
297         test_cmp_rev second HEAD &&
298         test_cmp_rev second^ HEAD^
299 '
300
301 test_expect_success 'am --patch-format=hg applies hg patch' '
302         rm -fr .git/rebase-apply &&
303         git checkout -f first &&
304         git am --patch-format=hg <patch1-hg.eml &&
305         test_path_is_missing .git/rebase-apply &&
306         git diff --exit-code second &&
307         test_cmp_rev second HEAD &&
308         test_cmp_rev second^ HEAD^
309 '
310
311 test_expect_success 'am with applypatch-msg hook' '
312         test_when_finished "rm -f .git/hooks/applypatch-msg" &&
313         rm -fr .git/rebase-apply &&
314         git reset --hard &&
315         git checkout first &&
316         mkdir -p .git/hooks &&
317         write_script .git/hooks/applypatch-msg <<-\EOF &&
318         cat "$1" >actual-msg &&
319         echo hook-message >"$1"
320         EOF
321         git am patch1 &&
322         test_path_is_missing .git/rebase-apply &&
323         git diff --exit-code second &&
324         echo hook-message >expected &&
325         git log -1 --format=format:%B >actual &&
326         test_cmp expected actual &&
327         git log -1 --format=format:%B second >expected &&
328         test_cmp expected actual-msg
329 '
330
331 test_expect_success 'am with failing applypatch-msg hook' '
332         test_when_finished "rm -f .git/hooks/applypatch-msg" &&
333         rm -fr .git/rebase-apply &&
334         git reset --hard &&
335         git checkout first &&
336         mkdir -p .git/hooks &&
337         write_script .git/hooks/applypatch-msg <<-\EOF &&
338         exit 1
339         EOF
340         test_must_fail git am patch1 &&
341         test_path_is_dir .git/rebase-apply &&
342         git diff --exit-code first &&
343         test_cmp_rev first HEAD
344 '
345
346 test_expect_success 'am with pre-applypatch hook' '
347         test_when_finished "rm -f .git/hooks/pre-applypatch" &&
348         rm -fr .git/rebase-apply &&
349         git reset --hard &&
350         git checkout first &&
351         mkdir -p .git/hooks &&
352         write_script .git/hooks/pre-applypatch <<-\EOF &&
353         git diff first >diff.actual
354         exit 0
355         EOF
356         git am patch1 &&
357         test_path_is_missing .git/rebase-apply &&
358         git diff --exit-code second &&
359         test_cmp_rev second HEAD &&
360         git diff first..second >diff.expected &&
361         test_cmp diff.expected diff.actual
362 '
363
364 test_expect_success 'am with failing pre-applypatch hook' '
365         test_when_finished "rm -f .git/hooks/pre-applypatch" &&
366         rm -fr .git/rebase-apply &&
367         git reset --hard &&
368         git checkout first &&
369         mkdir -p .git/hooks &&
370         write_script .git/hooks/pre-applypatch <<-\EOF &&
371         exit 1
372         EOF
373         test_must_fail git am patch1 &&
374         test_path_is_dir .git/rebase-apply &&
375         git diff --exit-code second &&
376         test_cmp_rev first HEAD
377 '
378
379 test_expect_success 'am with post-applypatch hook' '
380         test_when_finished "rm -f .git/hooks/post-applypatch" &&
381         rm -fr .git/rebase-apply &&
382         git reset --hard &&
383         git checkout first &&
384         mkdir -p .git/hooks &&
385         write_script .git/hooks/post-applypatch <<-\EOF &&
386         git rev-parse HEAD >head.actual
387         git diff second >diff.actual
388         exit 0
389         EOF
390         git am patch1 &&
391         test_path_is_missing .git/rebase-apply &&
392         test_cmp_rev second HEAD &&
393         git rev-parse second >head.expected &&
394         test_cmp head.expected head.actual &&
395         git diff second >diff.expected &&
396         test_cmp diff.expected diff.actual
397 '
398
399 test_expect_success 'am with failing post-applypatch hook' '
400         test_when_finished "rm -f .git/hooks/post-applypatch" &&
401         rm -fr .git/rebase-apply &&
402         git reset --hard &&
403         git checkout first &&
404         mkdir -p .git/hooks &&
405         write_script .git/hooks/post-applypatch <<-\EOF &&
406         git rev-parse HEAD >head.actual
407         exit 1
408         EOF
409         git am patch1 &&
410         test_path_is_missing .git/rebase-apply &&
411         git diff --exit-code second &&
412         test_cmp_rev second HEAD &&
413         git rev-parse second >head.expected &&
414         test_cmp head.expected head.actual
415 '
416
417 test_expect_success 'am --scissors cuts the message at the scissors line' '
418         rm -fr .git/rebase-apply &&
419         git reset --hard &&
420         git checkout second &&
421         git am --scissors scissors-patch.eml &&
422         test_path_is_missing .git/rebase-apply &&
423         git diff --exit-code scissors &&
424         test_cmp_rev scissors HEAD
425 '
426
427 test_expect_success 'am --no-scissors overrides mailinfo.scissors' '
428         rm -fr .git/rebase-apply &&
429         git reset --hard &&
430         git checkout second &&
431         test_config mailinfo.scissors true &&
432         git am --no-scissors no-scissors-patch.eml &&
433         test_path_is_missing .git/rebase-apply &&
434         git diff --exit-code no-scissors &&
435         test_cmp_rev no-scissors HEAD
436 '
437
438 test_expect_success 'setup: new author and committer' '
439         GIT_AUTHOR_NAME="Another Thor" &&
440         GIT_AUTHOR_EMAIL="a.thor@example.com" &&
441         GIT_COMMITTER_NAME="Co M Miter" &&
442         GIT_COMMITTER_EMAIL="c.miter@example.com" &&
443         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
444 '
445
446 compare () {
447         a=$(git cat-file commit "$2" | grep "^$1 ") &&
448         b=$(git cat-file commit "$3" | grep "^$1 ") &&
449         test "$a" = "$b"
450 }
451
452 test_expect_success 'am changes committer and keeps author' '
453         test_tick &&
454         rm -fr .git/rebase-apply &&
455         git reset --hard &&
456         git checkout first &&
457         git am patch2 &&
458         test_path_is_missing .git/rebase-apply &&
459         test "$(git rev-parse master^^)" = "$(git rev-parse HEAD^^)" &&
460         git diff --exit-code master..HEAD &&
461         git diff --exit-code master^..HEAD^ &&
462         compare author master HEAD &&
463         compare author master^ HEAD^ &&
464         test "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" = \
465              "$(git log -1 --pretty=format:"%cn <%ce>" HEAD)"
466 '
467
468 test_expect_success 'am --signoff adds Signed-off-by: line' '
469         rm -fr .git/rebase-apply &&
470         git reset --hard &&
471         git checkout -b master2 first &&
472         git am --signoff <patch2 &&
473         printf "%s\n" "$signoff" >expected &&
474         echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" >>expected &&
475         git cat-file commit HEAD^ | grep "Signed-off-by:" >actual &&
476         test_cmp expected actual &&
477         echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" >expected &&
478         git cat-file commit HEAD | grep "Signed-off-by:" >actual &&
479         test_cmp expected actual
480 '
481
482 test_expect_success 'am stays in branch' '
483         echo refs/heads/master2 >expected &&
484         git symbolic-ref HEAD >actual &&
485         test_cmp expected actual
486 '
487
488 test_expect_success 'am --signoff does not add Signed-off-by: line if already there' '
489         git format-patch --stdout HEAD^ >patch3 &&
490         sed -e "/^Subject/ s,\[PATCH,Re: Re: Re: & 1/5 v2] [foo," patch3 >patch4 &&
491         rm -fr .git/rebase-apply &&
492         git reset --hard &&
493         git checkout HEAD^ &&
494         git am --signoff patch4 &&
495         git cat-file commit HEAD >actual &&
496         test $(grep -c "^Signed-off-by:" actual) -eq 1
497 '
498
499 test_expect_success 'am without --keep removes Re: and [PATCH] stuff' '
500         git rev-parse HEAD >expected &&
501         git rev-parse master2 >actual &&
502         test_cmp expected actual
503 '
504
505 test_expect_success 'am --keep really keeps the subject' '
506         rm -fr .git/rebase-apply &&
507         git reset --hard &&
508         git checkout HEAD^ &&
509         git am --keep patch4 &&
510         test_path_is_missing .git/rebase-apply &&
511         git cat-file commit HEAD >actual &&
512         grep "Re: Re: Re: \[PATCH 1/5 v2\] \[foo\] third" actual
513 '
514
515 test_expect_success 'am --keep-non-patch really keeps the non-patch part' '
516         rm -fr .git/rebase-apply &&
517         git reset --hard &&
518         git checkout HEAD^ &&
519         git am --keep-non-patch patch4 &&
520         test_path_is_missing .git/rebase-apply &&
521         git cat-file commit HEAD >actual &&
522         grep "^\[foo\] third" actual
523 '
524
525 test_expect_success 'setup am -3' '
526         rm -fr .git/rebase-apply &&
527         git reset --hard &&
528         git checkout -b base3way master2 &&
529         sed -n -e "3,\$p" msg >file &&
530         head -n 9 msg >>file &&
531         git add file &&
532         test_tick &&
533         git commit -m "copied stuff"
534 '
535
536 test_expect_success 'am -3 falls back to 3-way merge' '
537         rm -fr .git/rebase-apply &&
538         git reset --hard &&
539         git checkout -b lorem2 base3way &&
540         git am -3 lorem-move.patch &&
541         test_path_is_missing .git/rebase-apply &&
542         git diff --exit-code lorem
543 '
544
545 test_expect_success 'am -3 -p0 can read --no-prefix patch' '
546         rm -fr .git/rebase-apply &&
547         git reset --hard &&
548         git checkout -b lorem3 base3way &&
549         git am -3 -p0 lorem-zero.patch &&
550         test_path_is_missing .git/rebase-apply &&
551         git diff --exit-code lorem
552 '
553
554 test_expect_success 'am can rename a file' '
555         grep "^rename from" rename.patch &&
556         rm -fr .git/rebase-apply &&
557         git reset --hard &&
558         git checkout lorem^0 &&
559         git am rename.patch &&
560         test_path_is_missing .git/rebase-apply &&
561         git update-index --refresh &&
562         git diff --exit-code rename
563 '
564
565 test_expect_success 'am -3 can rename a file' '
566         grep "^rename from" rename.patch &&
567         rm -fr .git/rebase-apply &&
568         git reset --hard &&
569         git checkout lorem^0 &&
570         git am -3 rename.patch &&
571         test_path_is_missing .git/rebase-apply &&
572         git update-index --refresh &&
573         git diff --exit-code rename
574 '
575
576 test_expect_success 'am -3 can rename a file after falling back to 3-way merge' '
577         grep "^rename from" rename-add.patch &&
578         rm -fr .git/rebase-apply &&
579         git reset --hard &&
580         git checkout lorem^0 &&
581         git am -3 rename-add.patch &&
582         test_path_is_missing .git/rebase-apply &&
583         git update-index --refresh &&
584         git diff --exit-code rename
585 '
586
587 test_expect_success 'am -3 -q is quiet' '
588         rm -fr .git/rebase-apply &&
589         git checkout -f lorem2 &&
590         git reset base3way --hard &&
591         git am -3 -q lorem-move.patch >output.out 2>&1 &&
592         ! test -s output.out
593 '
594
595 test_expect_success 'am pauses on conflict' '
596         rm -fr .git/rebase-apply &&
597         git reset --hard &&
598         git checkout lorem2^^ &&
599         test_must_fail git am lorem-move.patch &&
600         test -d .git/rebase-apply
601 '
602
603 test_expect_success 'am --skip works' '
604         echo goodbye >expected &&
605         git am --skip &&
606         test_path_is_missing .git/rebase-apply &&
607         git diff --exit-code lorem2^^ -- file &&
608         test_cmp expected another
609 '
610
611 test_expect_success 'am --abort removes a stray directory' '
612         mkdir .git/rebase-apply &&
613         git am --abort &&
614         test_path_is_missing .git/rebase-apply
615 '
616
617 test_expect_success 'am refuses patches when paused' '
618         rm -fr .git/rebase-apply &&
619         git reset --hard &&
620         git checkout lorem2^^ &&
621
622         test_must_fail git am lorem-move.patch &&
623         test_path_is_dir .git/rebase-apply &&
624         test_cmp_rev lorem2^^ HEAD &&
625
626         test_must_fail git am <lorem-move.patch &&
627         test_path_is_dir .git/rebase-apply &&
628         test_cmp_rev lorem2^^ HEAD
629 '
630
631 test_expect_success 'am --resolved works' '
632         echo goodbye >expected &&
633         rm -fr .git/rebase-apply &&
634         git reset --hard &&
635         git checkout lorem2^^ &&
636         test_must_fail git am lorem-move.patch &&
637         test -d .git/rebase-apply &&
638         echo resolved >>file &&
639         git add file &&
640         git am --resolved &&
641         test_path_is_missing .git/rebase-apply &&
642         test_cmp expected another
643 '
644
645 test_expect_success 'am --resolved fails if index has no changes' '
646         rm -fr .git/rebase-apply &&
647         git reset --hard &&
648         git checkout lorem2^^ &&
649         test_must_fail git am lorem-move.patch &&
650         test_path_is_dir .git/rebase-apply &&
651         test_cmp_rev lorem2^^ HEAD &&
652         test_must_fail git am --resolved &&
653         test_path_is_dir .git/rebase-apply &&
654         test_cmp_rev lorem2^^ HEAD
655 '
656
657 test_expect_success 'am --resolved fails if index has unmerged entries' '
658         rm -fr .git/rebase-apply &&
659         git reset --hard &&
660         git checkout second &&
661         test_must_fail git am -3 lorem-move.patch &&
662         test_path_is_dir .git/rebase-apply &&
663         test_cmp_rev second HEAD &&
664         test_must_fail git am --resolved >err &&
665         test_path_is_dir .git/rebase-apply &&
666         test_cmp_rev second HEAD &&
667         test_i18ngrep "still have unmerged paths" err
668 '
669
670 test_expect_success 'am takes patches from a Pine mailbox' '
671         rm -fr .git/rebase-apply &&
672         git reset --hard &&
673         git checkout first &&
674         cat pine patch1 | git am &&
675         test_path_is_missing .git/rebase-apply &&
676         git diff --exit-code master^..HEAD
677 '
678
679 test_expect_success 'am fails on mail without patch' '
680         rm -fr .git/rebase-apply &&
681         git reset --hard &&
682         test_must_fail git am <failmail &&
683         git am --abort &&
684         test_path_is_missing .git/rebase-apply
685 '
686
687 test_expect_success 'am fails on empty patch' '
688         rm -fr .git/rebase-apply &&
689         git reset --hard &&
690         echo "---" >>failmail &&
691         test_must_fail git am <failmail &&
692         git am --skip &&
693         test_path_is_missing .git/rebase-apply
694 '
695
696 test_expect_success 'am works from stdin in subdirectory' '
697         rm -fr subdir &&
698         rm -fr .git/rebase-apply &&
699         git reset --hard &&
700         git checkout first &&
701         (
702                 mkdir -p subdir &&
703                 cd subdir &&
704                 git am <../patch1
705         ) &&
706         git diff --exit-code second
707 '
708
709 test_expect_success 'am works from file (relative path given) in subdirectory' '
710         rm -fr subdir &&
711         rm -fr .git/rebase-apply &&
712         git reset --hard &&
713         git checkout first &&
714         (
715                 mkdir -p subdir &&
716                 cd subdir &&
717                 git am ../patch1
718         ) &&
719         git diff --exit-code second
720 '
721
722 test_expect_success 'am works from file (absolute path given) in subdirectory' '
723         rm -fr subdir &&
724         rm -fr .git/rebase-apply &&
725         git reset --hard &&
726         git checkout first &&
727         P=$(pwd) &&
728         (
729                 mkdir -p subdir &&
730                 cd subdir &&
731                 git am "$P/patch1"
732         ) &&
733         git diff --exit-code second
734 '
735
736 test_expect_success 'am --committer-date-is-author-date' '
737         rm -fr .git/rebase-apply &&
738         git reset --hard &&
739         git checkout first &&
740         test_tick &&
741         git am --committer-date-is-author-date patch1 &&
742         git cat-file commit HEAD | sed -e "/^\$/q" >head1 &&
743         sed -ne "/^author /s/.*> //p" head1 >at &&
744         sed -ne "/^committer /s/.*> //p" head1 >ct &&
745         test_cmp at ct
746 '
747
748 test_expect_success 'am without --committer-date-is-author-date' '
749         rm -fr .git/rebase-apply &&
750         git reset --hard &&
751         git checkout first &&
752         test_tick &&
753         git am patch1 &&
754         git cat-file commit HEAD | sed -e "/^\$/q" >head1 &&
755         sed -ne "/^author /s/.*> //p" head1 >at &&
756         sed -ne "/^committer /s/.*> //p" head1 >ct &&
757         ! test_cmp at ct
758 '
759
760 # This checks for +0000 because TZ is set to UTC and that should
761 # show up when the current time is used. The date in message is set
762 # by test_tick that uses -0700 timezone; if this feature does not
763 # work, we will see that instead of +0000.
764 test_expect_success 'am --ignore-date' '
765         rm -fr .git/rebase-apply &&
766         git reset --hard &&
767         git checkout first &&
768         test_tick &&
769         git am --ignore-date patch1 &&
770         git cat-file commit HEAD | sed -e "/^\$/q" >head1 &&
771         sed -ne "/^author /s/.*> //p" head1 >at &&
772         grep "+0000" at
773 '
774
775 test_expect_success 'am into an unborn branch' '
776         git rev-parse first^{tree} >expected &&
777         rm -fr .git/rebase-apply &&
778         git reset --hard &&
779         rm -fr subdir &&
780         mkdir subdir &&
781         git format-patch --numbered-files -o subdir -1 first &&
782         (
783                 cd subdir &&
784                 git init &&
785                 git am 1
786         ) &&
787         (
788                 cd subdir &&
789                 git rev-parse HEAD^{tree} >../actual
790         ) &&
791         test_cmp expected actual
792 '
793
794 test_expect_success 'am newline in subject' '
795         rm -fr .git/rebase-apply &&
796         git reset --hard &&
797         git checkout first &&
798         test_tick &&
799         sed -e "s/second/second \\\n foo/" patch1 >patchnl &&
800         git am <patchnl >output.out 2>&1 &&
801         test_i18ngrep "^Applying: second \\\n foo$" output.out
802 '
803
804 test_expect_success 'am -q is quiet' '
805         rm -fr .git/rebase-apply &&
806         git reset --hard &&
807         git checkout first &&
808         test_tick &&
809         git am -q <patch1 >output.out 2>&1 &&
810         ! test -s output.out
811 '
812
813 test_expect_success 'am empty-file does not infloop' '
814         rm -fr .git/rebase-apply &&
815         git reset --hard &&
816         touch empty-file &&
817         test_tick &&
818         test_must_fail git am empty-file 2>actual &&
819         echo Patch format detection failed. >expected &&
820         test_i18ncmp expected actual
821 '
822
823 test_expect_success 'am --message-id really adds the message id' '
824         rm -fr .git/rebase-apply &&
825         git reset --hard &&
826         git checkout HEAD^ &&
827         git am --message-id patch1.eml &&
828         test_path_is_missing .git/rebase-apply &&
829         git cat-file commit HEAD | tail -n1 >actual &&
830         grep Message-Id patch1.eml >expected &&
831         test_cmp expected actual
832 '
833
834 test_expect_success 'am.messageid really adds the message id' '
835         rm -fr .git/rebase-apply &&
836         git reset --hard &&
837         git checkout HEAD^ &&
838         test_config am.messageid true &&
839         git am patch1.eml &&
840         test_path_is_missing .git/rebase-apply &&
841         git cat-file commit HEAD | tail -n1 >actual &&
842         grep Message-Id patch1.eml >expected &&
843         test_cmp expected actual
844 '
845
846 test_expect_success 'am --message-id -s signs off after the message id' '
847         rm -fr .git/rebase-apply &&
848         git reset --hard &&
849         git checkout HEAD^ &&
850         git am -s --message-id patch1.eml &&
851         test_path_is_missing .git/rebase-apply &&
852         git cat-file commit HEAD | tail -n2 | head -n1 >actual &&
853         grep Message-Id patch1.eml >expected &&
854         test_cmp expected actual
855 '
856
857 test_done