]>
Commit | Line | Data |
---|---|---|
acb4441e JH |
1 | #!/bin/sh |
2 | ||
8bb19c14 | 3 | test_description='miscellaneous basic tests for cherry-pick and revert' |
acb4441e | 4 | |
cbc75a12 | 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
7 | ||
9ff2f060 | 8 | TEST_PASSES_SANITIZE_LEAK=true |
acb4441e JH |
9 | . ./test-lib.sh |
10 | ||
11 | test_expect_success setup ' | |
12 | ||
13 | for l in a b c d e f g h i j k l m n o | |
14 | do | |
db5875aa | 15 | echo $l$l$l$l$l$l$l$l$l || return 1 |
acb4441e JH |
16 | done >oops && |
17 | ||
18 | test_tick && | |
19 | git add oops && | |
20 | git commit -m initial && | |
21 | git tag initial && | |
22 | ||
23 | test_tick && | |
24 | echo "Add extra line at the end" >>oops && | |
25 | git commit -a -m added && | |
26 | git tag added && | |
27 | ||
28 | test_tick && | |
29 | git mv oops spoo && | |
30 | git commit -m rename1 && | |
31 | git tag rename1 && | |
32 | ||
33 | test_tick && | |
34 | git checkout -b side initial && | |
35 | git mv oops opos && | |
36 | git commit -m rename2 && | |
37 | git tag rename2 | |
38 | ' | |
39 | ||
e0ef8495 JN |
40 | test_expect_success 'cherry-pick --nonsense' ' |
41 | ||
42 | pos=$(git rev-parse HEAD) && | |
43 | git diff --exit-code HEAD && | |
44 | test_must_fail git cherry-pick --nonsense 2>msg && | |
45 | git diff --exit-code HEAD "$pos" && | |
6789275d | 46 | test_grep "[Uu]sage:" msg |
e0ef8495 JN |
47 | ' |
48 | ||
49 | test_expect_success 'revert --nonsense' ' | |
50 | ||
51 | pos=$(git rev-parse HEAD) && | |
52 | git diff --exit-code HEAD && | |
53 | test_must_fail git revert --nonsense 2>msg && | |
54 | git diff --exit-code HEAD "$pos" && | |
6789275d | 55 | test_grep "[Uu]sage:" msg |
e0ef8495 JN |
56 | ' |
57 | ||
8bb19c14 OB |
58 | # the following two test cherry-pick and revert with renames |
59 | # | |
60 | # -- | |
61 | # + rename2: renames oops to opos | |
62 | # + rename1: renames oops to spoo | |
63 | # + added: adds extra line to oops | |
64 | # ++ initial: has lines in oops | |
65 | ||
acb4441e JH |
66 | test_expect_success 'cherry-pick after renaming branch' ' |
67 | ||
68 | git checkout rename2 && | |
9e54dc6c | 69 | git cherry-pick added && |
82b28c4e | 70 | test_cmp_rev rename2 HEAD^ && |
643cb5f7 CC |
71 | grep "Add extra line at the end" opos && |
72 | git reflog -1 | grep cherry-pick | |
acb4441e JH |
73 | |
74 | ' | |
75 | ||
76 | test_expect_success 'revert after renaming branch' ' | |
77 | ||
78 | git checkout rename1 && | |
9e54dc6c | 79 | git revert added && |
82b28c4e KM |
80 | test_cmp_rev rename1 HEAD^ && |
81 | test_path_is_file spoo && | |
82 | test_cmp_rev initial:oops HEAD:spoo && | |
643cb5f7 | 83 | git reflog -1 | grep revert |
acb4441e JH |
84 | |
85 | ' | |
86 | ||
f6ce1f25 JN |
87 | test_expect_success 'cherry-pick on stat-dirty working tree' ' |
88 | git clone . copy && | |
89 | ( | |
90 | cd copy && | |
91 | git checkout initial && | |
0e496492 | 92 | test-tool chmtime +40 oops && |
f6ce1f25 JN |
93 | git cherry-pick added |
94 | ) | |
95 | ' | |
96 | ||
fff1bb3a | 97 | test_expect_success 'revert forbidden on dirty working tree' ' |
0f2d4476 JK |
98 | |
99 | echo content >extra_file && | |
100 | git add extra_file && | |
101 | test_must_fail git revert HEAD 2>errors && | |
6789275d | 102 | test_grep "your local changes would be overwritten by " errors |
0f2d4476 JK |
103 | |
104 | ' | |
105 | ||
7a96c386 | 106 | test_expect_success 'cherry-pick on unborn branch' ' |
1b90588d | 107 | git switch --orphan unborn && |
334ae397 | 108 | git rm --cached -r . && |
334ae397 | 109 | git cherry-pick initial && |
1b90588d BL |
110 | git diff --exit-code initial && |
111 | test_cmp_rev ! initial HEAD | |
112 | ' | |
113 | ||
114 | test_expect_success 'cherry-pick on unborn branch with --allow-empty' ' | |
115 | git checkout --detach && | |
116 | git branch -D unborn && | |
117 | git switch --orphan unborn && | |
118 | git cherry-pick initial --allow-empty && | |
119 | git diff --exit-code initial && | |
2c9e125b | 120 | test_cmp_rev ! initial HEAD |
334ae397 MZ |
121 | ' |
122 | ||
182d7dc4 HU |
123 | test_expect_success 'cherry-pick "-" to pick from previous branch' ' |
124 | git checkout unborn && | |
125 | test_commit to-pick actual content && | |
cbc75a12 | 126 | git checkout main && |
182d7dc4 HU |
127 | git cherry-pick - && |
128 | echo content >expect && | |
129 | test_cmp expect actual | |
130 | ' | |
131 | ||
132 | test_expect_success 'cherry-pick "-" is meaningless without checkout' ' | |
133 | test_create_repo afresh && | |
134 | ( | |
135 | cd afresh && | |
136 | test_commit one && | |
137 | test_commit two && | |
138 | test_commit three && | |
139 | test_must_fail git cherry-pick - | |
140 | ) | |
141 | ' | |
142 | ||
d644c550 JK |
143 | test_expect_success 'cherry-pick "-" works with arguments' ' |
144 | git checkout -b side-branch && | |
145 | test_commit change actual change && | |
cbc75a12 | 146 | git checkout main && |
d644c550 JK |
147 | git cherry-pick -s - && |
148 | echo "Signed-off-by: C O Mitter <committer@example.com>" >expect && | |
149 | git cat-file commit HEAD | grep ^Signed-off-by: >signoff && | |
150 | test_cmp expect signoff && | |
151 | echo change >expect && | |
152 | test_cmp expect actual | |
153 | ' | |
154 | ||
2f682e21 | 155 | test_expect_success 'cherry-pick works with dirty renamed file' ' |
05f2dfb9 JS |
156 | test_commit to-rename && |
157 | git checkout -b unrelated && | |
158 | test_commit unrelated && | |
159 | git checkout @{-1} && | |
160 | git mv to-rename.t renamed && | |
161 | test_tick && | |
162 | git commit -m renamed && | |
163 | echo modified >renamed && | |
f670adb4 | 164 | git cherry-pick refs/heads/unrelated && |
fd53b7ff | 165 | test $(git rev-parse :0:renamed) = $(git rev-parse HEAD~2:to-rename.t) && |
7c5585ff | 166 | grep -q "^modified$" renamed |
05f2dfb9 JS |
167 | ' |
168 | ||
f172556b | 169 | test_expect_success 'advice from failed revert' ' |
43966ab3 | 170 | test_when_finished "git reset --hard" && |
f172556b ZH |
171 | test_commit --no-tag "add dream" dream dream && |
172 | dream_oid=$(git rev-parse --short HEAD) && | |
173 | cat <<-EOF >expected && | |
174 | error: could not revert $dream_oid... add dream | |
175 | hint: After resolving the conflicts, mark them with | |
176 | hint: "git add/rm <pathspec>", then run | |
177 | hint: "git revert --continue". | |
178 | hint: You can instead skip this commit with "git revert --skip". | |
179 | hint: To abort and get back to the state before "git revert", | |
180 | hint: run "git revert --abort". | |
ec030091 | 181 | hint: Disable this message with "git config advice.mergeConflict false" |
f172556b ZH |
182 | EOF |
183 | test_commit --append --no-tag "double-add dream" dream dream && | |
184 | test_must_fail git revert HEAD^ 2>actual && | |
185 | test_cmp expected actual | |
186 | ' | |
43966ab3 | 187 | |
883cb1b8 OB |
188 | test_expect_subject () { |
189 | echo "$1" >expect && | |
190 | git log -1 --pretty=%s >actual && | |
191 | test_cmp expect actual | |
192 | } | |
193 | ||
194 | test_expect_success 'titles of fresh reverts' ' | |
195 | test_commit --no-tag A file1 && | |
196 | test_commit --no-tag B file1 && | |
197 | git revert --no-edit HEAD && | |
198 | test_expect_subject "Revert \"B\"" && | |
199 | git revert --no-edit HEAD && | |
200 | test_expect_subject "Reapply \"B\"" && | |
201 | git revert --no-edit HEAD && | |
202 | test_expect_subject "Revert \"Reapply \"B\"\"" | |
203 | ' | |
204 | ||
205 | test_expect_success 'title of legacy double revert' ' | |
206 | test_commit --no-tag "Revert \"Revert \"B\"\"" file1 && | |
207 | git revert --no-edit HEAD && | |
208 | test_expect_subject "Revert \"Revert \"Revert \"B\"\"\"" | |
209 | ' | |
210 | ||
43966ab3 JH |
211 | test_expect_success 'identification of reverted commit (default)' ' |
212 | test_commit to-ident && | |
213 | test_when_finished "git reset --hard to-ident" && | |
214 | git checkout --detach to-ident && | |
215 | git revert --no-edit HEAD && | |
216 | git cat-file commit HEAD >actual.raw && | |
217 | grep "^This reverts " actual.raw >actual && | |
218 | echo "This reverts commit $(git rev-parse HEAD^)." >expect && | |
219 | test_cmp expect actual | |
220 | ' | |
221 | ||
222 | test_expect_success 'identification of reverted commit (--reference)' ' | |
223 | git checkout --detach to-ident && | |
224 | git revert --reference --no-edit HEAD && | |
225 | git cat-file commit HEAD >actual.raw && | |
226 | grep "^This reverts " actual.raw >actual && | |
227 | echo "This reverts commit $(git show -s --pretty=reference HEAD^)." >expect && | |
228 | test_cmp expect actual | |
229 | ' | |
230 | ||
231 | test_expect_success 'identification of reverted commit (revert.reference)' ' | |
232 | git checkout --detach to-ident && | |
233 | git -c revert.reference=true revert --no-edit HEAD && | |
234 | git cat-file commit HEAD >actual.raw && | |
235 | grep "^This reverts " actual.raw >actual && | |
236 | echo "This reverts commit $(git show -s --pretty=reference HEAD^)." >expect && | |
237 | test_cmp expect actual | |
238 | ' | |
239 | ||
191faaf7 JH |
240 | test_expect_success 'cherry-pick is unaware of --reference (for now)' ' |
241 | test_when_finished "git reset --hard" && | |
242 | test_must_fail git cherry-pick --reference HEAD 2>actual && | |
243 | grep "^usage: git cherry-pick" actual | |
244 | ' | |
245 | ||
acb4441e | 246 | test_done |