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