git-svn tests: rewrite brittle tests to use "--[no-]merges".
[git] / t / t7064-wtstatus-pv2.sh
1 #!/bin/sh
2
3 test_description='git status --porcelain=v2
4
5 This test exercises porcelain V2 output for git status.'
6
7
8 . ./test-lib.sh
9
10
11 test_expect_success setup '
12         git checkout -f --orphan initial-branch &&
13         test_tick &&
14         git config core.autocrlf false &&
15         echo x >file_x &&
16         echo y >file_y &&
17         echo z >file_z &&
18         mkdir dir1 &&
19         echo a >dir1/file_a &&
20         echo b >dir1/file_b
21 '
22
23 test_expect_success 'before initial commit, nothing added, only untracked' '
24         cat >expect <<-EOF &&
25         # branch.oid (initial)
26         # branch.head initial-branch
27         ? actual
28         ? dir1/
29         ? expect
30         ? file_x
31         ? file_y
32         ? file_z
33         EOF
34
35         git status --porcelain=v2 --branch --untracked-files=normal >actual &&
36         test_cmp expect actual
37 '
38
39 test_expect_success 'before initial commit, things added' '
40         git add file_x file_y file_z dir1 &&
41         OID_A=$(git hash-object -t blob -- dir1/file_a) &&
42         OID_B=$(git hash-object -t blob -- dir1/file_b) &&
43         OID_X=$(git hash-object -t blob -- file_x) &&
44         OID_Y=$(git hash-object -t blob -- file_y) &&
45         OID_Z=$(git hash-object -t blob -- file_z) &&
46
47         cat >expect <<-EOF &&
48         # branch.oid (initial)
49         # branch.head initial-branch
50         1 A. N... 000000 100644 100644 $ZERO_OID $OID_A dir1/file_a
51         1 A. N... 000000 100644 100644 $ZERO_OID $OID_B dir1/file_b
52         1 A. N... 000000 100644 100644 $ZERO_OID $OID_X file_x
53         1 A. N... 000000 100644 100644 $ZERO_OID $OID_Y file_y
54         1 A. N... 000000 100644 100644 $ZERO_OID $OID_Z file_z
55         ? actual
56         ? expect
57         EOF
58
59         git status --porcelain=v2 --branch --untracked-files=all >actual &&
60         test_cmp expect actual
61 '
62
63 test_expect_success 'before initial commit, things added (-z)' '
64         lf_to_nul >expect <<-EOF &&
65         # branch.oid (initial)
66         # branch.head initial-branch
67         1 A. N... 000000 100644 100644 $ZERO_OID $OID_A dir1/file_a
68         1 A. N... 000000 100644 100644 $ZERO_OID $OID_B dir1/file_b
69         1 A. N... 000000 100644 100644 $ZERO_OID $OID_X file_x
70         1 A. N... 000000 100644 100644 $ZERO_OID $OID_Y file_y
71         1 A. N... 000000 100644 100644 $ZERO_OID $OID_Z file_z
72         ? actual
73         ? expect
74         EOF
75
76         git status -z --porcelain=v2 --branch --untracked-files=all >actual &&
77         test_cmp expect actual
78 '
79
80 test_expect_success 'make first commit, comfirm HEAD oid and branch' '
81         git commit -m initial &&
82         H0=$(git rev-parse HEAD) &&
83         cat >expect <<-EOF &&
84         # branch.oid $H0
85         # branch.head initial-branch
86         ? actual
87         ? expect
88         EOF
89
90         git status --porcelain=v2 --branch --untracked-files=all >actual &&
91         test_cmp expect actual
92 '
93
94 test_expect_success 'after first commit, create unstaged changes' '
95         echo x >>file_x &&
96         OID_X1=$(git hash-object -t blob -- file_x) &&
97         rm file_z &&
98         H0=$(git rev-parse HEAD) &&
99
100         cat >expect <<-EOF &&
101         # branch.oid $H0
102         # branch.head initial-branch
103         1 .M N... 100644 100644 100644 $OID_X $OID_X file_x
104         1 .D N... 100644 100644 000000 $OID_Z $OID_Z file_z
105         ? actual
106         ? expect
107         EOF
108
109         git status --porcelain=v2 --branch --untracked-files=all >actual &&
110         test_cmp expect actual
111 '
112
113 test_expect_success 'after first commit but omit untracked files and branch' '
114         cat >expect <<-EOF &&
115         1 .M N... 100644 100644 100644 $OID_X $OID_X file_x
116         1 .D N... 100644 100644 000000 $OID_Z $OID_Z file_z
117         EOF
118
119         git status --porcelain=v2 --untracked-files=no >actual &&
120         test_cmp expect actual
121 '
122
123 test_expect_success 'after first commit, stage existing changes' '
124         git add file_x &&
125         git rm file_z &&
126         H0=$(git rev-parse HEAD) &&
127
128         cat >expect <<-EOF &&
129         # branch.oid $H0
130         # branch.head initial-branch
131         1 M. N... 100644 100644 100644 $OID_X $OID_X1 file_x
132         1 D. N... 100644 000000 000000 $OID_Z $ZERO_OID file_z
133         ? actual
134         ? expect
135         EOF
136
137         git status --porcelain=v2 --branch --untracked-files=all >actual &&
138         test_cmp expect actual
139 '
140
141 test_expect_success 'rename causes 2 path lines' '
142         git mv file_y renamed_y &&
143         H0=$(git rev-parse HEAD) &&
144
145         q_to_tab >expect <<-EOF &&
146         # branch.oid $H0
147         # branch.head initial-branch
148         1 M. N... 100644 100644 100644 $OID_X $OID_X1 file_x
149         1 D. N... 100644 000000 000000 $OID_Z $ZERO_OID file_z
150         2 R. N... 100644 100644 100644 $OID_Y $OID_Y R100 renamed_yQfile_y
151         ? actual
152         ? expect
153         EOF
154
155         git status --porcelain=v2 --branch --untracked-files=all >actual &&
156         test_cmp expect actual
157 '
158
159 test_expect_success 'rename causes 2 path lines (-z)' '
160         H0=$(git rev-parse HEAD) &&
161
162         ## Lines use NUL path separator and line terminator, so double transform here.
163         q_to_nul <<-EOF | lf_to_nul >expect &&
164         # branch.oid $H0
165         # branch.head initial-branch
166         1 M. N... 100644 100644 100644 $OID_X $OID_X1 file_x
167         1 D. N... 100644 000000 000000 $OID_Z $ZERO_OID file_z
168         2 R. N... 100644 100644 100644 $OID_Y $OID_Y R100 renamed_yQfile_y
169         ? actual
170         ? expect
171         EOF
172
173         git status --porcelain=v2 --branch --untracked-files=all -z >actual &&
174         test_cmp expect actual
175 '
176
177 test_expect_success 'make second commit, confirm clean and new HEAD oid' '
178         git commit -m second &&
179         H1=$(git rev-parse HEAD) &&
180
181         cat >expect <<-EOF &&
182         # branch.oid $H1
183         # branch.head initial-branch
184         ? actual
185         ? expect
186         EOF
187
188         git status --porcelain=v2 --branch --untracked-files=all >actual &&
189         test_cmp expect actual
190 '
191
192 test_expect_success 'confirm ignored files are not printed' '
193         test_when_finished "rm -f x.ign .gitignore" &&
194         echo x.ign >.gitignore &&
195         echo "ignore me" >x.ign &&
196
197         cat >expect <<-EOF &&
198         ? .gitignore
199         ? actual
200         ? expect
201         EOF
202
203         git status --porcelain=v2 --untracked-files=all >actual &&
204         test_cmp expect actual
205 '
206
207 test_expect_success 'ignored files are printed with --ignored' '
208         test_when_finished "rm -f x.ign .gitignore" &&
209         echo x.ign >.gitignore &&
210         echo "ignore me" >x.ign &&
211
212         cat >expect <<-EOF &&
213         ? .gitignore
214         ? actual
215         ? expect
216         ! x.ign
217         EOF
218
219         git status --porcelain=v2 --ignored --untracked-files=all >actual &&
220         test_cmp expect actual
221 '
222
223 test_expect_success 'create and commit permanent ignore file' '
224         cat >.gitignore <<-EOF &&
225         actual*
226         expect*
227         EOF
228
229         git add .gitignore &&
230         git commit -m ignore_trash &&
231         H1=$(git rev-parse HEAD) &&
232
233         cat >expect <<-EOF &&
234         # branch.oid $H1
235         # branch.head initial-branch
236         EOF
237
238         git status --porcelain=v2 --branch >actual &&
239         test_cmp expect actual
240 '
241
242 test_expect_success 'verify --intent-to-add output' '
243         test_when_finished "git rm -f intent1.add intent2.add" &&
244         touch intent1.add &&
245         echo test >intent2.add &&
246
247         git add --intent-to-add intent1.add intent2.add &&
248
249         cat >expect <<-EOF &&
250         1 .A N... 000000 000000 100644 $ZERO_OID $ZERO_OID intent1.add
251         1 .A N... 000000 000000 100644 $ZERO_OID $ZERO_OID intent2.add
252         EOF
253
254         git status --porcelain=v2 >actual &&
255         test_cmp expect actual
256 '
257
258 test_expect_success 'verify AA (add-add) conflict' '
259         test_when_finished "git reset --hard" &&
260
261         git branch AA_A initial-branch &&
262         git checkout AA_A &&
263         echo "Branch AA_A" >conflict.txt &&
264         OID_AA_A=$(git hash-object -t blob -- conflict.txt) &&
265         git add conflict.txt &&
266         git commit -m "branch aa_a" &&
267
268         git branch AA_B initial-branch &&
269         git checkout AA_B &&
270         echo "Branch AA_B" >conflict.txt &&
271         OID_AA_B=$(git hash-object -t blob -- conflict.txt) &&
272         git add conflict.txt &&
273         git commit -m "branch aa_b" &&
274
275         git branch AA_M AA_B &&
276         git checkout AA_M &&
277         test_must_fail git merge AA_A &&
278
279         HM=$(git rev-parse HEAD) &&
280
281         cat >expect <<-EOF &&
282         # branch.oid $HM
283         # branch.head AA_M
284         u AA N... 000000 100644 100644 100644 $ZERO_OID $OID_AA_B $OID_AA_A conflict.txt
285         EOF
286
287         git status --porcelain=v2 --branch --untracked-files=all >actual &&
288         test_cmp expect actual
289 '
290
291 test_expect_success 'verify UU (edit-edit) conflict' '
292         test_when_finished "git reset --hard" &&
293
294         git branch UU_ANC initial-branch &&
295         git checkout UU_ANC &&
296         echo "Ancestor" >conflict.txt &&
297         OID_UU_ANC=$(git hash-object -t blob -- conflict.txt) &&
298         git add conflict.txt &&
299         git commit -m "UU_ANC" &&
300
301         git branch UU_A UU_ANC &&
302         git checkout UU_A &&
303         echo "Branch UU_A" >conflict.txt &&
304         OID_UU_A=$(git hash-object -t blob -- conflict.txt) &&
305         git add conflict.txt &&
306         git commit -m "branch uu_a" &&
307
308         git branch UU_B UU_ANC &&
309         git checkout UU_B &&
310         echo "Branch UU_B" >conflict.txt &&
311         OID_UU_B=$(git hash-object -t blob -- conflict.txt) &&
312         git add conflict.txt &&
313         git commit -m "branch uu_b" &&
314
315         git branch UU_M UU_B &&
316         git checkout UU_M &&
317         test_must_fail git merge UU_A &&
318
319         HM=$(git rev-parse HEAD) &&
320
321         cat >expect <<-EOF &&
322         # branch.oid $HM
323         # branch.head UU_M
324         u UU N... 100644 100644 100644 100644 $OID_UU_ANC $OID_UU_B $OID_UU_A conflict.txt
325         EOF
326
327         git status --porcelain=v2 --branch --untracked-files=all >actual &&
328         test_cmp expect actual
329 '
330
331 test_expect_success 'verify upstream fields in branch header' '
332         git checkout initial-branch &&
333         test_when_finished "rm -rf sub_repo" &&
334         git clone . sub_repo &&
335         (
336                 ## Confirm local initial-branch tracks remote initial-branch.
337                 cd sub_repo &&
338                 HUF=$(git rev-parse HEAD) &&
339
340                 cat >expect <<-EOF &&
341                 # branch.oid $HUF
342                 # branch.head initial-branch
343                 # branch.upstream origin/initial-branch
344                 # branch.ab +0 -0
345                 EOF
346
347                 git status --porcelain=v2 --branch --untracked-files=all >actual &&
348                 test_cmp expect actual &&
349
350                 ## Test ahead/behind.
351                 echo xyz >file_xyz &&
352                 git add file_xyz &&
353                 git commit -m xyz &&
354
355                 HUF=$(git rev-parse HEAD) &&
356
357                 cat >expect <<-EOF &&
358                 # branch.oid $HUF
359                 # branch.head initial-branch
360                 # branch.upstream origin/initial-branch
361                 # branch.ab +1 -0
362                 EOF
363
364                 git status --porcelain=v2 --branch --untracked-files=all >actual &&
365                 test_cmp expect actual &&
366
367                 ## Repeat the above but without --branch.
368                 git status --porcelain=v2 --untracked-files=all >actual &&
369                 test_must_be_empty actual &&
370
371                 ## Test upstream-gone case. Fake this by pointing
372                 ## origin/initial-branch at a non-existing commit.
373                 OLD=$(git rev-parse origin/initial-branch) &&
374                 NEW=$ZERO_OID &&
375                 mv .git/packed-refs .git/old-packed-refs &&
376                 sed "s/$OLD/$NEW/g" <.git/old-packed-refs >.git/packed-refs &&
377
378                 HUF=$(git rev-parse HEAD) &&
379
380                 cat >expect <<-EOF &&
381                 # branch.oid $HUF
382                 # branch.head initial-branch
383                 # branch.upstream origin/initial-branch
384                 EOF
385
386                 git status --porcelain=v2 --branch --untracked-files=all >actual &&
387                 test_cmp expect actual
388         )
389 '
390
391 test_expect_success 'verify --[no-]ahead-behind with V2 format' '
392         git checkout initial-branch &&
393         test_when_finished "rm -rf sub_repo" &&
394         git clone . sub_repo &&
395         (
396                 ## Confirm local initial-branch tracks remote initial-branch.
397                 cd sub_repo &&
398                 HUF=$(git rev-parse HEAD) &&
399
400                 # Confirm --no-ahead-behind reports traditional branch.ab with 0/0 for equal branches.
401                 cat >expect <<-EOF &&
402                 # branch.oid $HUF
403                 # branch.head initial-branch
404                 # branch.upstream origin/initial-branch
405                 # branch.ab +0 -0
406                 EOF
407
408                 git status --no-ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
409                 test_cmp expect actual &&
410
411                 # Confirm --ahead-behind reports traditional branch.ab with 0/0.
412                 cat >expect <<-EOF &&
413                 # branch.oid $HUF
414                 # branch.head initial-branch
415                 # branch.upstream origin/initial-branch
416                 # branch.ab +0 -0
417                 EOF
418
419                 git status --ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
420                 test_cmp expect actual &&
421
422                 ## Test non-equal ahead/behind.
423                 echo xyz >file_xyz &&
424                 git add file_xyz &&
425                 git commit -m xyz &&
426
427                 HUF=$(git rev-parse HEAD) &&
428
429                 # Confirm --no-ahead-behind reports branch.ab with ?/? for non-equal branches.
430                 cat >expect <<-EOF &&
431                 # branch.oid $HUF
432                 # branch.head initial-branch
433                 # branch.upstream origin/initial-branch
434                 # branch.ab +? -?
435                 EOF
436
437                 git status --no-ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
438                 test_cmp expect actual &&
439
440                 # Confirm --ahead-behind reports traditional branch.ab with 1/0.
441                 cat >expect <<-EOF &&
442                 # branch.oid $HUF
443                 # branch.head initial-branch
444                 # branch.upstream origin/initial-branch
445                 # branch.ab +1 -0
446                 EOF
447
448                 git status --ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
449                 test_cmp expect actual &&
450
451                 # Confirm that "status.aheadbehind" DOES NOT work on V2 format.
452                 git -c status.aheadbehind=false status --porcelain=v2 --branch --untracked-files=all >actual &&
453                 test_cmp expect actual &&
454
455                 # Confirm that "status.aheadbehind" DOES NOT work on V2 format.
456                 git -c status.aheadbehind=true status --porcelain=v2 --branch --untracked-files=all >actual &&
457                 test_cmp expect actual
458         )
459 '
460
461 test_expect_success 'create and add submodule, submodule appears clean (A. S...)' '
462         git checkout initial-branch &&
463         git clone . sub_repo &&
464         git clone . super_repo &&
465         (       cd super_repo &&
466                 git submodule add ../sub_repo sub1 &&
467
468                 ## Confirm stage/add of clean submodule.
469                 HMOD=$(git hash-object -t blob -- .gitmodules) &&
470                 HSUP=$(git rev-parse HEAD) &&
471                 HSUB=$HSUP &&
472
473                 cat >expect <<-EOF &&
474                 # branch.oid $HSUP
475                 # branch.head initial-branch
476                 # branch.upstream origin/initial-branch
477                 # branch.ab +0 -0
478                 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
479                 1 A. S... 000000 160000 160000 $ZERO_OID $HSUB sub1
480                 EOF
481
482                 git status --porcelain=v2 --branch --untracked-files=all >actual &&
483                 test_cmp expect actual
484         )
485 '
486
487 test_expect_success 'untracked changes in added submodule (AM S..U)' '
488         (       cd super_repo &&
489                 ## create untracked file in the submodule.
490                 (       cd sub1 &&
491                         echo "xxxx" >file_in_sub
492                 ) &&
493
494                 HMOD=$(git hash-object -t blob -- .gitmodules) &&
495                 HSUP=$(git rev-parse HEAD) &&
496                 HSUB=$HSUP &&
497
498                 cat >expect <<-EOF &&
499                 # branch.oid $HSUP
500                 # branch.head initial-branch
501                 # branch.upstream origin/initial-branch
502                 # branch.ab +0 -0
503                 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
504                 1 AM S..U 000000 160000 160000 $ZERO_OID $HSUB sub1
505                 EOF
506
507                 git status --porcelain=v2 --branch --untracked-files=all >actual &&
508                 test_cmp expect actual
509         )
510 '
511
512 test_expect_success 'staged changes in added submodule (AM S.M.)' '
513         (       cd super_repo &&
514                 ## stage the changes in the submodule.
515                 (       cd sub1 &&
516                         git add file_in_sub
517                 ) &&
518
519                 HMOD=$(git hash-object -t blob -- .gitmodules) &&
520                 HSUP=$(git rev-parse HEAD) &&
521                 HSUB=$HSUP &&
522
523                 cat >expect <<-EOF &&
524                 # branch.oid $HSUP
525                 # branch.head initial-branch
526                 # branch.upstream origin/initial-branch
527                 # branch.ab +0 -0
528                 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
529                 1 AM S.M. 000000 160000 160000 $ZERO_OID $HSUB sub1
530                 EOF
531
532                 git status --porcelain=v2 --branch --untracked-files=all >actual &&
533                 test_cmp expect actual
534         )
535 '
536
537 test_expect_success 'staged and unstaged changes in added (AM S.M.)' '
538         (       cd super_repo &&
539                 (       cd sub1 &&
540                         ## make additional unstaged changes (on the same file) in the submodule.
541                         ## This does not cause us to get S.MU (because the submodule does not report
542                         ## a "?" line for the unstaged changes).
543                         echo "more changes" >>file_in_sub
544                 ) &&
545
546                 HMOD=$(git hash-object -t blob -- .gitmodules) &&
547                 HSUP=$(git rev-parse HEAD) &&
548                 HSUB=$HSUP &&
549
550                 cat >expect <<-EOF &&
551                 # branch.oid $HSUP
552                 # branch.head initial-branch
553                 # branch.upstream origin/initial-branch
554                 # branch.ab +0 -0
555                 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
556                 1 AM S.M. 000000 160000 160000 $ZERO_OID $HSUB sub1
557                 EOF
558
559                 git status --porcelain=v2 --branch --untracked-files=all >actual &&
560                 test_cmp expect actual
561         )
562 '
563
564 test_expect_success 'staged and untracked changes in added submodule (AM S.MU)' '
565         (       cd super_repo &&
566                 (       cd sub1 &&
567                         ## stage new changes in tracked file.
568                         git add file_in_sub &&
569                         ## create new untracked file.
570                         echo "yyyy" >>another_file_in_sub
571                 ) &&
572
573                 HMOD=$(git hash-object -t blob -- .gitmodules) &&
574                 HSUP=$(git rev-parse HEAD) &&
575                 HSUB=$HSUP &&
576
577                 cat >expect <<-EOF &&
578                 # branch.oid $HSUP
579                 # branch.head initial-branch
580                 # branch.upstream origin/initial-branch
581                 # branch.ab +0 -0
582                 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
583                 1 AM S.MU 000000 160000 160000 $ZERO_OID $HSUB sub1
584                 EOF
585
586                 git status --porcelain=v2 --branch --untracked-files=all >actual &&
587                 test_cmp expect actual
588         )
589 '
590
591 test_expect_success 'commit within the submodule appears as new commit in super (AM SC..)' '
592         (       cd super_repo &&
593                 (       cd sub1 &&
594                         ## Make a new commit in the submodule.
595                         git add file_in_sub &&
596                         rm -f another_file_in_sub &&
597                         git commit -m "new commit"
598                 ) &&
599
600                 HMOD=$(git hash-object -t blob -- .gitmodules) &&
601                 HSUP=$(git rev-parse HEAD) &&
602                 HSUB=$HSUP &&
603
604                 cat >expect <<-EOF &&
605                 # branch.oid $HSUP
606                 # branch.head initial-branch
607                 # branch.upstream origin/initial-branch
608                 # branch.ab +0 -0
609                 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
610                 1 AM SC.. 000000 160000 160000 $ZERO_OID $HSUB sub1
611                 EOF
612
613                 git status --porcelain=v2 --branch --untracked-files=all >actual &&
614                 test_cmp expect actual
615         )
616 '
617
618 test_expect_success 'stage submodule in super and commit' '
619         (       cd super_repo &&
620                 ## Stage the new submodule commit in the super.
621                 git add sub1 &&
622                 ## Commit the super so that the sub no longer appears as added.
623                 git commit -m "super commit" &&
624
625                 HSUP=$(git rev-parse HEAD) &&
626
627                 cat >expect <<-EOF &&
628                 # branch.oid $HSUP
629                 # branch.head initial-branch
630                 # branch.upstream origin/initial-branch
631                 # branch.ab +1 -0
632                 EOF
633
634                 git status --porcelain=v2 --branch --untracked-files=all >actual &&
635                 test_cmp expect actual
636         )
637 '
638
639 test_expect_success 'make unstaged changes in existing submodule (.M S.M.)' '
640         (       cd super_repo &&
641                 (       cd sub1 &&
642                         echo "zzzz" >>file_in_sub
643                 ) &&
644
645                 HSUP=$(git rev-parse HEAD) &&
646                 HSUB=$(cd sub1 && git rev-parse HEAD) &&
647
648                 cat >expect <<-EOF &&
649                 # branch.oid $HSUP
650                 # branch.head initial-branch
651                 # branch.upstream origin/initial-branch
652                 # branch.ab +1 -0
653                 1 .M S.M. 160000 160000 160000 $HSUB $HSUB sub1
654                 EOF
655
656                 git status --porcelain=v2 --branch --untracked-files=all >actual &&
657                 test_cmp expect actual
658         )
659 '
660
661 test_done