config: unify code paths to get global config paths
[git] / t / t4108-apply-threeway.sh
1 #!/bin/sh
2
3 test_description='git apply --3way'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 print_sanitized_conflicted_diff () {
11         git diff HEAD >diff.raw &&
12         sed -e '
13                 /^index /d
14                 s/^\(+[<>|][<>|][<>|][<>|]*\) .*/\1/
15         ' diff.raw
16 }
17
18 test_expect_success setup '
19         test_tick &&
20         test_write_lines 1 2 3 4 5 6 7 >one &&
21         cat one >two &&
22         git add one two &&
23         git commit -m initial &&
24
25         git branch side &&
26
27         test_tick &&
28         test_write_lines 1 two 3 4 5 six 7 >one &&
29         test_write_lines 1 two 3 4 5 6 7 >two &&
30         git commit -a -m main &&
31
32         git checkout side &&
33         test_write_lines 1 2 3 4 five 6 7 >one &&
34         test_write_lines 1 2 3 4 five 6 7 >two &&
35         git commit -a -m side &&
36
37         git checkout main
38 '
39
40 test_expect_success 'apply without --3way' '
41         git diff side^ side >P.diff &&
42
43         # should fail to apply
44         git reset --hard &&
45         git checkout main^0 &&
46         test_must_fail git apply --index P.diff &&
47         # should leave things intact
48         git diff-files --exit-code &&
49         git diff-index --exit-code --cached HEAD
50 '
51
52 test_apply_with_3way () {
53         # Merging side should be similar to applying this patch
54         git diff ...side >P.diff &&
55
56         # The corresponding conflicted merge
57         git reset --hard &&
58         git checkout main^0 &&
59         test_must_fail git merge --no-commit side &&
60         git ls-files -s >expect.ls &&
61         print_sanitized_conflicted_diff >expect.diff &&
62
63         # should fail to apply
64         git reset --hard &&
65         git checkout main^0 &&
66         test_must_fail git apply --index --3way P.diff &&
67         git ls-files -s >actual.ls &&
68         print_sanitized_conflicted_diff >actual.diff &&
69
70         # The result should resemble the corresponding merge
71         test_cmp expect.ls actual.ls &&
72         test_cmp expect.diff actual.diff
73 }
74
75 test_expect_success 'apply with --3way' '
76         test_apply_with_3way
77 '
78
79 test_expect_success 'apply with --3way with merge.conflictStyle = diff3' '
80         test_config merge.conflictStyle diff3 &&
81         test_apply_with_3way
82 '
83
84 test_expect_success 'apply with --3way with rerere enabled' '
85         test_config rerere.enabled true &&
86
87         # Merging side should be similar to applying this patch
88         git diff ...side >P.diff &&
89
90         # The corresponding conflicted merge
91         git reset --hard &&
92         git checkout main^0 &&
93         test_must_fail git merge --no-commit side &&
94
95         # Manually resolve and record the resolution
96         test_write_lines 1 two 3 4 five six 7 >one &&
97         git rerere &&
98         cat one >expect &&
99
100         # should fail to apply
101         git reset --hard &&
102         git checkout main^0 &&
103         test_must_fail git apply --index --3way P.diff &&
104
105         # but rerere should have replayed the recorded resolution
106         test_cmp expect one
107 '
108
109 test_expect_success 'apply -3 with add/add conflict setup' '
110         git reset --hard &&
111
112         git checkout -b adder &&
113         test_write_lines 1 2 3 4 5 6 7 >three &&
114         test_write_lines 1 2 3 4 5 6 7 >four &&
115         git add three four &&
116         git commit -m "add three and four" &&
117
118         git checkout -b another adder^ &&
119         test_write_lines 1 2 3 4 5 6 7 >three &&
120         test_write_lines 1 2 3 four 5 6 7 >four &&
121         git add three four &&
122         git commit -m "add three and four" &&
123
124         # Merging another should be similar to applying this patch
125         git diff adder...another >P.diff &&
126
127         git checkout adder^0 &&
128         test_must_fail git merge --no-commit another &&
129         git ls-files -s >expect.ls &&
130         print_sanitized_conflicted_diff >expect.diff
131 '
132
133 test_expect_success 'apply -3 with add/add conflict' '
134         # should fail to apply ...
135         git reset --hard &&
136         git checkout adder^0 &&
137         test_must_fail git apply --index --3way P.diff &&
138         # ... and leave conflicts in the index and in the working tree
139         git ls-files -s >actual.ls &&
140         print_sanitized_conflicted_diff >actual.diff &&
141
142         # The result should resemble the corresponding merge
143         test_cmp expect.ls actual.ls &&
144         test_cmp expect.diff actual.diff
145 '
146
147 test_expect_success 'apply -3 with add/add conflict (dirty working tree)' '
148         # should fail to apply ...
149         git reset --hard &&
150         git checkout adder^0 &&
151         echo >>four &&
152         cat four >four.save &&
153         cat three >three.save &&
154         git ls-files -s >expect.ls &&
155         test_must_fail git apply --index --3way P.diff &&
156         # ... and should not touch anything
157         git ls-files -s >actual.ls &&
158         test_cmp expect.ls actual.ls &&
159         test_cmp four.save four &&
160         test_cmp three.save three
161 '
162
163 test_done