interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t3451-history-reword.sh

462 lines · 9503 bytes Raw Blame History
1#!/bin/sh
2
3test_description='tests for git-history reword subcommand'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY/lib-log-graph.sh"
7. "$TEST_DIRECTORY/lib-gpg.sh"
8
9reword_with_message () {
10 cat >message &&
11 write_script fake-editor.sh <<-\EOF &&
12 cp message "$1"
13 EOF
14 test_set_editor "$(pwd)"/fake-editor.sh &&
15 git history reword "$@" &&
16 rm fake-editor.sh message
17}
18
19expect_graph () {
20 cat >expect &&
21 lib_test_cmp_graph --graph --format=%s "$@"
22}
23
24expect_log () {
25 git log --format="%s" "$@" >actual &&
26 cat >expect &&
27 test_cmp expect actual
28}
29
30test_reword_gpg_sign () {
31 must_fail= will=will
32 if test "x$1" = "x!"
33 then
34 must_fail=test_must_fail
35 will="will not"
36 shift
37 fi
38 conf=$1
39 shift
40
41 test_expect_success GPG "reword $* with commit.gpgsign=$conf $will sign rewritten history" "
42 test_when_finished 'rm -rf repo' &&
43 git init repo &&
44 (
45 cd repo &&
46 test_commit first &&
47 test_commit second &&
48 test_commit third &&
49
50 git config commit.gpgsign $conf &&
51 reword_with_message $* HEAD~ <<-EOF &&
52 second reworded
53 EOF
54
55 $must_fail git verify-commit HEAD~ &&
56 $must_fail git verify-commit HEAD
57 )
58 "
59}
60
61test_expect_success 'can reword tip of a branch' '
62 test_when_finished "rm -rf repo" &&
63 git init repo &&
64 (
65 cd repo &&
66 test_commit first &&
67 test_commit second &&
68 test_commit third &&
69
70 git symbolic-ref HEAD >expect &&
71 reword_with_message HEAD <<-EOF &&
72 third reworded
73 EOF
74 git symbolic-ref HEAD >actual &&
75 test_cmp expect actual &&
76
77 expect_log <<-\EOF &&
78 third reworded
79 second
80 first
81 EOF
82
83 git reflog >reflog &&
84 test_grep "reword: updating HEAD" reflog
85 )
86'
87
88test_expect_success 'can reword commit in the middle' '
89 test_when_finished "rm -rf repo" &&
90 git init repo &&
91 (
92 cd repo &&
93 test_commit first &&
94 test_commit second &&
95 test_commit third &&
96
97 git symbolic-ref HEAD >expect &&
98 reword_with_message HEAD~ <<-EOF &&
99 second reworded
100 EOF
101 git symbolic-ref HEAD >actual &&
102 test_cmp expect actual &&
103
104 expect_log <<-\EOF
105 third
106 second reworded
107 first
108 EOF
109 )
110'
111
112test_reword_gpg_sign ! false
113test_reword_gpg_sign true
114test_reword_gpg_sign false --gpg-sign
115test_reword_gpg_sign ! true --no-gpg-sign
116test_reword_gpg_sign ! true --gpg-sign --no-gpg-sign
117test_reword_gpg_sign false --no-gpg-sign --gpg-sign
118
119test_expect_success GPG 'reword uses an explicit signing key for rewritten history' '
120 test_when_finished "rm -rf repo" &&
121 git init repo &&
122 (
123 cd repo &&
124 test_commit first &&
125 test_commit second &&
126 test_commit third &&
127
128 reword_with_message -SB7227189 HEAD~ <<-EOF &&
129 second reworded
130 EOF
131
132 git verify-commit HEAD~ &&
133 git verify-commit HEAD &&
134 git log -2 --format=%GK >actual &&
135 cat >expect <<-\EOF &&
136 65A0EEA02E30CAD7
137 65A0EEA02E30CAD7
138 EOF
139 test_cmp expect actual
140 )
141'
142
143test_expect_success 'can reword commit in the middle even on detached head' '
144 test_when_finished "rm -rf repo" &&
145 git init repo &&
146 (
147 cd repo &&
148 test_commit first &&
149 test_commit second &&
150 test_commit third_on_main &&
151 git checkout --detach HEAD^ &&
152 test_commit third_on_head &&
153
154 reword_with_message HEAD~ <<-EOF &&
155 second reworded
156 EOF
157
158 expect_graph HEAD --branches <<-\EOF
159 * third_on_head
160 | * third_on_main
161 |/
162 * second reworded
163 * first
164 EOF
165 )
166'
167
168test_expect_success 'can reword the detached head' '
169 test_when_finished "rm -rf repo" &&
170 git init repo &&
171 (
172 cd repo &&
173 test_commit first &&
174 test_commit second &&
175 git checkout --detach HEAD &&
176 test_commit third &&
177
178 reword_with_message HEAD <<-EOF &&
179 third reworded
180 EOF
181
182 expect_log <<-\EOF
183 third reworded
184 second
185 first
186 EOF
187 )
188'
189
190test_expect_success 'can reword root commit' '
191 test_when_finished "rm -rf repo" &&
192 git init repo &&
193 (
194 cd repo &&
195 test_commit first &&
196 test_commit second &&
197 test_commit third &&
198 reword_with_message HEAD~2 <<-EOF &&
199 first reworded
200 EOF
201
202 expect_log <<-\EOF
203 third
204 second
205 first reworded
206 EOF
207 )
208'
209
210test_expect_success 'can reword in a bare repo' '
211 test_when_finished "rm -rf repo repo.git" &&
212 git init repo &&
213 test_commit -C repo first &&
214 git clone --bare repo repo.git &&
215 (
216 cd repo.git &&
217 reword_with_message HEAD <<-EOF &&
218 reworded
219 EOF
220
221 expect_log <<-\EOF
222 reworded
223 EOF
224 )
225'
226
227test_expect_success 'can reword a commit on a different branch' '
228 test_when_finished "rm -rf repo" &&
229 git init repo &&
230 (
231 cd repo &&
232 test_commit base &&
233 git branch theirs &&
234 test_commit ours &&
235 git switch theirs &&
236 test_commit theirs &&
237
238 git rev-parse ours >ours-before &&
239 reword_with_message theirs <<-EOF &&
240 Reworded theirs
241 EOF
242 git rev-parse ours >ours-after &&
243 test_cmp ours-before ours-after &&
244
245 expect_graph --branches <<-\EOF
246 * Reworded theirs
247 | * ours
248 |/
249 * base
250 EOF
251 )
252'
253
254test_expect_success 'can reword a merge commit' '
255 test_when_finished "rm -rf repo" &&
256 git init repo &&
257 (
258 cd repo &&
259 test_commit base &&
260 git branch branch &&
261 test_commit ours &&
262 git switch branch &&
263 test_commit theirs &&
264 git switch - &&
265 git merge theirs &&
266
267 # It is not possible to replay merge commits embedded in the
268 # history (yet).
269 test_must_fail git -c core.editor=false history reword HEAD~ 2>err &&
270 test_grep "replaying merge commits is not supported yet" err &&
271
272 # But it is possible to reword a merge commit directly.
273 reword_with_message HEAD <<-EOF &&
274 Reworded merge commit
275 EOF
276 expect_graph <<-\EOF
277 * Reworded merge commit
278 |\
279 | * theirs
280 * | ours
281 |/
282 * base
283 EOF
284 )
285'
286
287test_expect_success '--dry-run prints ref updates without modifying repo' '
288 test_when_finished "rm -rf repo" &&
289 git init repo --initial-branch=main &&
290 (
291 cd repo &&
292 test_commit base &&
293 git branch branch &&
294 test_commit ours &&
295 git switch branch &&
296 test_commit theirs &&
297
298 git refs list >refs-expect &&
299 reword_with_message --dry-run --update-refs=head base >updates <<-\EOF &&
300 reworded commit
301 EOF
302 git refs list >refs-actual &&
303 test_cmp refs-expect refs-actual &&
304 test_grep "update refs/heads/branch" updates &&
305 test_grep ! "update refs/heads/main" updates &&
306
307 reword_with_message --dry-run base >updates <<-\EOF &&
308 reworded commit
309 EOF
310 git refs list >refs-actual &&
311 test_cmp refs-expect refs-actual &&
312
313 test_grep "update refs/heads/branch" updates &&
314 test_grep "update refs/heads/main" updates &&
315 git update-ref --stdin <updates &&
316 expect_log --branches <<-\EOF
317 theirs
318 ours
319 reworded commit
320 EOF
321 )
322'
323
324test_expect_success '--update-refs=head updates only HEAD' '
325 test_when_finished "rm -rf repo" &&
326 git init repo --initial-branch=main &&
327 (
328 cd repo &&
329 test_commit base &&
330 git branch branch &&
331 test_commit theirs &&
332 git switch branch &&
333 test_commit ours &&
334
335 # When told to update HEAD, only, the command will refuse to
336 # rewrite commits that are not an ancestor of HEAD.
337 test_must_fail git -c core.editor=false history reword --update-refs=head theirs 2>err &&
338 test_grep "rewritten commit must be an ancestor of HEAD" err &&
339
340 reword_with_message --update-refs=head base >updates <<-\EOF &&
341 reworded base
342 EOF
343 expect_log HEAD <<-\EOF &&
344 ours
345 reworded base
346 EOF
347 expect_log main <<-\EOF
348 theirs
349 base
350 EOF
351 )
352'
353
354test_expect_success 'editor shows proper status' '
355 test_when_finished "rm -rf repo" &&
356 git init repo &&
357 (
358 cd repo &&
359 test_commit first &&
360
361 write_script fake-editor.sh <<-\EOF &&
362 cp "$1" . &&
363 printf "\namend a comment\n" >>"$1"
364 EOF
365 test_set_editor "$(pwd)"/fake-editor.sh &&
366 git history reword HEAD &&
367
368 cat >expect <<-EOF &&
369 first
370
371 # Please enter the commit message for the reworded changes. Lines starting
372 # with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit.
373 # Changes to be committed:
374 # new file: first.t
375 #
376 EOF
377 test_cmp expect COMMIT_EDITMSG &&
378
379 test_commit_message HEAD <<-\EOF
380 first
381
382 amend a comment
383 EOF
384 )
385'
386
387# For now, git-history(1) does not yet execute any hooks. This is subject to
388# change in the future, and if it does this test here is expected to start
389# failing. In other words, this test is not an endorsement of the current
390# status quo.
391test_expect_success 'hooks are not executed for rewritten commits' '
392 test_when_finished "rm -rf repo" &&
393 git init repo &&
394 (
395 cd repo &&
396 test_commit first &&
397 test_commit second &&
398 test_commit third &&
399
400 ORIG_PATH="$(pwd)" &&
401 export ORIG_PATH &&
402 for hook in prepare-commit-msg pre-commit post-commit post-rewrite commit-msg
403 do
404 write_script .git/hooks/$hook <<-\EOF || exit 1
405 touch "$ORIG_PATH/hooks.log
406 EOF
407 done &&
408
409 reword_with_message HEAD~ <<-EOF &&
410 second reworded
411 EOF
412
413 cat >expect <<-EOF &&
414 third
415 second reworded
416 first
417 EOF
418 git log --format=%s >actual &&
419 test_cmp expect actual &&
420
421 test_path_is_missing hooks.log
422 )
423'
424
425test_expect_success 'aborts with empty commit message' '
426 test_when_finished "rm -rf repo" &&
427 git init repo &&
428 (
429 cd repo &&
430 test_commit first &&
431
432 ! reword_with_message HEAD 2>err </dev/null &&
433 test_grep "Aborting commit due to empty commit message." err
434 )
435'
436
437test_expect_success 'retains changes in the worktree and index' '
438 test_when_finished "rm -rf repo" &&
439 git init repo &&
440 (
441 cd repo &&
442 touch a b &&
443 git add . &&
444 git commit -m "initial commit" &&
445 echo foo >a &&
446 echo bar >b &&
447 git add b &&
448 reword_with_message HEAD <<-EOF &&
449 message
450 EOF
451 cat >expect <<-\EOF &&
452 M a
453 M b
454 ?? actual
455 ?? expect
456 EOF
457 git status --porcelain >actual &&
458 test_cmp expect actual
459 )
460'
461
462test_done