t/README: add missing value for GIT_TEST_DEFAULT_REF_FORMAT
[git/gitster.git] / t / t3501-revert-cherry-pick.sh
blob411027fb58c7dfb657409980970e71005f2c79c5
1 #!/bin/sh
3 test_description='miscellaneous basic tests for cherry-pick and revert'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 test_expect_success setup '
13 for l in a b c d e f g h i j k l m n o
15 echo $l$l$l$l$l$l$l$l$l || return 1
16 done >oops &&
18 test_tick &&
19 git add oops &&
20 git commit -m initial &&
21 git tag initial &&
23 test_tick &&
24 echo "Add extra line at the end" >>oops &&
25 git commit -a -m added &&
26 git tag added &&
28 test_tick &&
29 git mv oops spoo &&
30 git commit -m rename1 &&
31 git tag rename1 &&
33 test_tick &&
34 git checkout -b side initial &&
35 git mv oops opos &&
36 git commit -m rename2 &&
37 git tag rename2
40 test_expect_success 'cherry-pick --nonsense' '
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" &&
46 test_grep "[Uu]sage:" msg
49 test_expect_success 'revert --nonsense' '
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" &&
55 test_grep "[Uu]sage:" msg
58 # the following two test cherry-pick and revert with renames
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
66 test_expect_success 'cherry-pick after renaming branch' '
68 git checkout rename2 &&
69 git cherry-pick added &&
70 test_cmp_rev rename2 HEAD^ &&
71 grep "Add extra line at the end" opos &&
72 git reflog -1 | grep cherry-pick
76 test_expect_success 'revert after renaming branch' '
78 git checkout rename1 &&
79 git revert added &&
80 test_cmp_rev rename1 HEAD^ &&
81 test_path_is_file spoo &&
82 test_cmp_rev initial:oops HEAD:spoo &&
83 git reflog -1 | grep revert
87 test_expect_success 'cherry-pick on stat-dirty working tree' '
88 git clone . copy &&
90 cd copy &&
91 git checkout initial &&
92 test-tool chmtime +40 oops &&
93 git cherry-pick added
97 test_expect_success 'revert forbidden on dirty working tree' '
99 echo content >extra_file &&
100 git add extra_file &&
101 test_must_fail git revert HEAD 2>errors &&
102 test_grep "your local changes would be overwritten by " errors
106 test_expect_success 'cherry-pick on unborn branch' '
107 git switch --orphan unborn &&
108 git rm --cached -r . &&
109 git cherry-pick initial &&
110 git diff --exit-code initial &&
111 test_cmp_rev ! initial HEAD
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 &&
120 test_cmp_rev ! initial HEAD
123 test_expect_success 'cherry-pick "-" to pick from previous branch' '
124 git checkout unborn &&
125 test_commit to-pick actual content &&
126 git checkout main &&
127 git cherry-pick - &&
128 echo content >expect &&
129 test_cmp expect actual
132 test_expect_success 'cherry-pick "-" is meaningless without checkout' '
133 test_create_repo afresh &&
135 cd afresh &&
136 test_commit one &&
137 test_commit two &&
138 test_commit three &&
139 test_must_fail git cherry-pick -
143 test_expect_success 'cherry-pick "-" works with arguments' '
144 git checkout -b side-branch &&
145 test_commit change actual change &&
146 git checkout main &&
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
155 test_expect_success 'cherry-pick works with dirty renamed file' '
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 &&
164 git cherry-pick refs/heads/unrelated &&
165 test $(git rev-parse :0:renamed) = $(git rev-parse HEAD~2:to-rename.t) &&
166 grep -q "^modified$" renamed
169 test_expect_success 'advice from failed revert' '
170 test_when_finished "git reset --hard" &&
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".
181 hint: Disable this message with "git config advice.mergeConflict false"
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
188 test_expect_subject () {
189 echo "$1" >expect &&
190 git log -1 --pretty=%s >actual &&
191 test_cmp expect actual
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\"\""
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\"\"\""
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
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
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
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
246 test_done