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