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