aboutsummaryrefslogtreecommitdiff
path: root/t/t3650-replay-basics.sh
diff options
context:
space:
mode:
Diffstat (limited to 't/t3650-replay-basics.sh')
-rwxr-xr-xt/t3650-replay-basics.sh364
1 files changed, 356 insertions, 8 deletions
diff --git a/t/t3650-replay-basics.sh b/t/t3650-replay-basics.sh
index 58b3759935..3353bc4a4d 100755
--- a/t/t3650-replay-basics.sh
+++ b/t/t3650-replay-basics.sh
@@ -25,6 +25,8 @@ test_expect_success 'setup' '
git switch -c topic3 &&
test_commit G &&
test_commit H &&
+ git switch -c empty &&
+ git commit --allow-empty -m empty &&
git switch -c topic4 main &&
test_commit I &&
test_commit J &&
@@ -43,6 +45,13 @@ test_expect_success 'setup' '
test_commit L &&
test_commit M &&
+ git switch --detach topic4 &&
+ test_commit N &&
+ test_commit O &&
+ git switch -c topic-with-merge topic4 &&
+ test_merge P O --no-ff &&
+ git switch main &&
+
git switch -c conflict B &&
test_commit C.conflict C.t conflict
'
@@ -51,8 +60,57 @@ test_expect_success 'setup bare' '
git clone --bare . bare
'
+test_expect_success 'argument to --advance must be a reference' '
+ echo "fatal: argument to --advance must be a reference" >expect &&
+ oid=$(git rev-parse main) &&
+ test_must_fail git replay --advance=$oid topic1..topic2 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--onto with invalid commit-ish' '
+ printf "fatal: ${SQ}refs/not-valid${SQ} is not " >expect &&
+ printf "a valid commit-ish for --onto\n" >>expect &&
+ test_must_fail git replay --onto=refs/not-valid topic1..topic2 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'exactly one of --onto, --advance, or --revert is required' '
+ echo "error: exactly one of --onto, --advance, or --revert is required" >expect &&
+ test_might_fail git replay -h >>expect &&
+ test_must_fail git replay topic1..topic2 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'replay down to root onto another branch' '
+ git replay --ref-action=print --onto main topic2 >result &&
+
+ test_line_count = 1 result &&
+
+ git log --format=%s $(cut -f 3 -d " " result) >actual &&
+ test_write_lines E D C M L B A >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--advance and --contained cannot be used together' '
+ test_must_fail git replay --advance=main --contained \
+ topic1..topic2 2>actual &&
+ test_grep "cannot be used together" actual
+'
+
+test_expect_success 'cannot advance target ... ordering would be ill-defined' '
+ echo "fatal: ${SQ}--advance${SQ} cannot be used with multiple revision ranges because the ordering would be ill-defined" >expect &&
+ test_must_fail git replay --advance=main main topic1 topic2 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'replaying merge commits is not supported yet' '
+ echo "fatal: replaying merge commits is not supported yet!" >expect &&
+ test_must_fail git replay --advance=main main..topic-with-merge 2>actual &&
+ test_cmp expect actual
+'
+
test_expect_success 'using replay to rebase two branches, one on top of other' '
- git replay --onto main topic1..topic2 >result &&
+ git replay --ref-action=print --onto main topic1..topic2 >result &&
test_line_count = 1 result &&
@@ -68,7 +126,7 @@ test_expect_success 'using replay to rebase two branches, one on top of other' '
'
test_expect_success 'using replay on bare repo to rebase two branches, one on top of other' '
- git -C bare replay --onto main topic1..topic2 >result-bare &&
+ git -C bare replay --ref-action=print --onto main topic1..topic2 >result-bare &&
test_cmp expect result-bare
'
@@ -86,7 +144,7 @@ test_expect_success 'using replay to perform basic cherry-pick' '
# 2nd field of result is refs/heads/main vs. refs/heads/topic2
# 4th field of result is hash for main instead of hash for topic2
- git replay --advance main topic1..topic2 >result &&
+ git replay --ref-action=print --advance main topic1..topic2 >result &&
test_line_count = 1 result &&
@@ -102,10 +160,29 @@ test_expect_success 'using replay to perform basic cherry-pick' '
'
test_expect_success 'using replay on bare repo to perform basic cherry-pick' '
- git -C bare replay --advance main topic1..topic2 >result-bare &&
+ git -C bare replay --ref-action=print --advance main topic1..topic2 >result-bare &&
test_cmp expect result-bare
'
+test_expect_success 'commits that become empty are dropped' '
+ # Save original branches
+ git for-each-ref --format="update %(refname) %(objectname)" \
+ refs/heads/ >original-branches &&
+ test_when_finished "git update-ref --stdin <original-branches &&
+ rm original-branches" &&
+ # Cherry-pick tip of topic1 ("F"), from the middle of A..empty, to main
+ git replay --advance main topic1^! &&
+
+ # Replay all of A..empty onto main (which includes topic1 & thus F
+ # in the middle)
+ git replay --onto main --branches --ancestry-path=empty ^A \
+ >result &&
+ git log --format="%s%d" L..empty >actual &&
+ test_write_lines >expect \
+ "empty (empty)" "H (topic3)" G "C (topic1)" "F (main)" "M (tag: M)" &&
+ test_cmp expect actual
+'
+
test_expect_success 'replay on bare repo fails with both --advance and --onto' '
test_must_fail git -C bare replay --advance main --onto main topic1..topic2 >result-bare
'
@@ -115,7 +192,7 @@ test_expect_success 'replay fails when both --advance and --onto are omitted' '
'
test_expect_success 'using replay to also rebase a contained branch' '
- git replay --contained --onto main main..topic3 >result &&
+ git replay --ref-action=print --contained --onto main main..topic3 >result &&
test_line_count = 2 result &&
cut -f 3 -d " " result >new-branch-tips &&
@@ -139,12 +216,12 @@ test_expect_success 'using replay to also rebase a contained branch' '
'
test_expect_success 'using replay on bare repo to also rebase a contained branch' '
- git -C bare replay --contained --onto main main..topic3 >result-bare &&
+ git -C bare replay --ref-action=print --contained --onto main main..topic3 >result-bare &&
test_cmp expect result-bare
'
test_expect_success 'using replay to rebase multiple divergent branches' '
- git replay --onto main ^topic1 topic2 topic4 >result &&
+ git replay --ref-action=print --onto main ^topic1 topic2 topic4 >result &&
test_line_count = 2 result &&
cut -f 3 -d " " result >new-branch-tips &&
@@ -168,7 +245,7 @@ test_expect_success 'using replay to rebase multiple divergent branches' '
'
test_expect_success 'using replay on bare repo to rebase multiple divergent branches, including contained ones' '
- git -C bare replay --contained --onto main ^main topic2 topic3 topic4 >result &&
+ git -C bare replay --ref-action=print --contained --onto main ^main topic2 topic3 topic4 >result &&
test_line_count = 4 result &&
cut -f 3 -d " " result >new-branch-tips &&
@@ -195,6 +272,15 @@ test_expect_success 'using replay on bare repo to rebase multiple divergent bran
done
'
+test_expect_success 'using replay to update detached HEAD' '
+ current_head=$(git branch --show-current) &&
+ test_when_finished git switch "$current_head" &&
+ git switch --detach &&
+ test_commit something &&
+ git replay --ref-action=print --onto HEAD~2 --ref-action=print HEAD~..HEAD >updates &&
+ test_grep "update HEAD " updates
+'
+
test_expect_success 'merge.directoryRenames=false' '
# create a test case that stress-tests the rename caching
git switch -c rename-onto &&
@@ -217,4 +303,266 @@ test_expect_success 'merge.directoryRenames=false' '
--onto rename-onto rename-onto..rename-from
'
+test_expect_success 'default atomic behavior updates refs directly' '
+ # Use a separate branch to avoid contaminating topic2 for later tests
+ git branch test-atomic topic2 &&
+ test_when_finished "git branch -D test-atomic" &&
+
+ # Test default atomic behavior (no output, refs updated)
+ git replay --onto main topic1..test-atomic >output &&
+ test_must_be_empty output &&
+
+ # Verify ref was updated
+ git log --format=%s test-atomic >actual &&
+ test_write_lines E D M L B A >expect &&
+ test_cmp expect actual &&
+
+ # Verify reflog message includes SHA of onto commit
+ git reflog test-atomic -1 --format=%gs >reflog-msg &&
+ ONTO_SHA=$(git rev-parse main) &&
+ echo "replay --onto $ONTO_SHA" >expect-reflog &&
+ test_cmp expect-reflog reflog-msg
+'
+
+test_expect_success 'atomic behavior in bare repository' '
+ # Store original state for cleanup
+ START=$(git -C bare rev-parse topic2) &&
+ test_when_finished "git -C bare update-ref refs/heads/topic2 $START" &&
+
+ # Test atomic updates work in bare repo
+ git -C bare replay --onto main topic1..topic2 >output &&
+ test_must_be_empty output &&
+
+ # Verify ref was updated in bare repo
+ git -C bare log --format=%s topic2 >actual &&
+ test_write_lines E D M L B A >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reflog message for --advance mode' '
+ # Store original state
+ START=$(git rev-parse main) &&
+ test_when_finished "git update-ref refs/heads/main $START" &&
+
+ # Test --advance mode reflog message
+ git replay --advance main topic1..topic2 >output &&
+ test_must_be_empty output &&
+
+ # Verify reflog message includes --advance and branch name
+ git reflog main -1 --format=%gs >reflog-msg &&
+ echo "replay --advance main" >expect-reflog &&
+ test_cmp expect-reflog reflog-msg
+'
+
+test_expect_success 'replay.refAction=print config option' '
+ # Store original state
+ START=$(git rev-parse topic2) &&
+ test_when_finished "git branch -f topic2 $START" &&
+
+ # Test with config set to print
+ test_config replay.refAction print &&
+ git replay --onto main topic1..topic2 >output &&
+ test_line_count = 1 output &&
+ test_grep "^update refs/heads/topic2 " output
+'
+
+test_expect_success 'replay.refAction=update config option' '
+ # Store original state
+ START=$(git rev-parse topic2) &&
+ test_when_finished "git branch -f topic2 $START" &&
+
+ # Test with config set to update
+ test_config replay.refAction update &&
+ git replay --onto main topic1..topic2 >output &&
+ test_must_be_empty output &&
+
+ # Verify ref was updated
+ git log --format=%s topic2 >actual &&
+ test_write_lines E D M L B A >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'command-line --ref-action overrides config' '
+ # Store original state
+ START=$(git rev-parse topic2) &&
+ test_when_finished "git branch -f topic2 $START" &&
+
+ # Set config to update but use --ref-action=print
+ test_config replay.refAction update &&
+ git replay --ref-action=print --onto main topic1..topic2 >output &&
+ test_line_count = 1 output &&
+ test_grep "^update refs/heads/topic2 " output
+'
+
+test_expect_success 'invalid replay.refAction value' '
+ test_config replay.refAction invalid &&
+ test_must_fail git replay --onto main topic1..topic2 2>error &&
+ test_grep "invalid.*replay.refAction.*value" error
+'
+
+test_expect_success 'argument to --revert must be a reference' '
+ echo "fatal: argument to --revert must be a reference" >expect &&
+ oid=$(git rev-parse main) &&
+ test_must_fail git replay --revert=$oid topic1..topic2 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cannot revert with multiple sources' '
+ echo "fatal: ${SQ}--revert${SQ} cannot be used with multiple revision ranges because the ordering would be ill-defined" >expect &&
+ test_must_fail git replay --revert main main topic1 topic2 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'using replay --revert to revert commits' '
+ # Reuse existing topic4 branch (has commits I and J on top of main)
+ START=$(git rev-parse topic4) &&
+ test_when_finished "git branch -f topic4 $START" &&
+
+ # Revert commits I and J
+ git replay --revert topic4 topic4~2..topic4 &&
+
+ # Verify the revert commits were created (newest-first ordering
+ # means J is reverted first, then I on top)
+ git log --format=%s -4 topic4 >actual &&
+ cat >expect <<-\EOF &&
+ Revert "I"
+ Revert "J"
+ J
+ I
+ EOF
+ test_cmp expect actual &&
+
+ # Verify commit message format includes hash (tip is Revert "I")
+ test_commit_message topic4 <<-EOF &&
+ Revert "I"
+
+ This reverts commit $(git rev-parse I).
+ EOF
+
+ # Verify reflog message
+ git reflog topic4 -1 --format=%gs >reflog-msg &&
+ echo "replay --revert topic4" >expect-reflog &&
+ test_cmp expect-reflog reflog-msg
+'
+
+test_expect_success 'using replay --revert in bare repo' '
+ # Reuse existing topic4 in bare repo
+ START=$(git -C bare rev-parse topic4) &&
+ test_when_finished "git -C bare update-ref refs/heads/topic4 $START" &&
+
+ # Revert commit J in bare repo
+ git -C bare replay --revert topic4 topic4~1..topic4 &&
+
+ # Verify revert was created
+ git -C bare log -1 --format=%s topic4 >actual &&
+ echo "Revert \"J\"" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'revert of revert uses Reapply' '
+ # Use topic4 and first revert J, then revert the revert
+ START=$(git rev-parse topic4) &&
+ test_when_finished "git branch -f topic4 $START" &&
+
+ # First revert J
+ git replay --revert topic4 topic4~1..topic4 &&
+ REVERT_J=$(git rev-parse topic4) &&
+
+ # Now revert the revert - should become Reapply
+ git replay --revert topic4 topic4~1..topic4 &&
+
+ # Verify Reapply prefix and message format
+ test_commit_message topic4 <<-EOF
+ Reapply "J"
+
+ This reverts commit $REVERT_J.
+ EOF
+'
+
+test_expect_success 'git replay --revert with conflict' '
+ # conflict branch has C.conflict which conflicts with topic1s C
+ test_expect_code 1 git replay --revert conflict B..topic1
+'
+
+test_expect_success 'git replay --revert incompatible with --contained' '
+ test_must_fail git replay --revert topic4 --contained topic4~1..topic4 2>error &&
+ test_grep "cannot be used together" error
+'
+
+test_expect_success 'git replay --revert incompatible with --onto' '
+ test_must_fail git replay --revert topic4 --onto main topic4~1..topic4 2>error &&
+ test_grep "cannot be used together" error
+'
+
+test_expect_success 'git replay --revert incompatible with --advance' '
+ test_must_fail git replay --revert topic4 --advance main topic4~1..topic4 2>error &&
+ test_grep "cannot be used together" error
+'
+
+test_expect_success 'using --onto with --ref' '
+ git branch test-ref-onto topic2 &&
+ test_when_finished "git branch -D test-ref-onto" &&
+
+ git replay --ref-action=print --onto=main --ref=refs/heads/test-ref-onto topic1..topic2 >result &&
+
+ test_line_count = 1 result &&
+ test_grep "^update refs/heads/test-ref-onto " result &&
+
+ git log --format=%s $(cut -f 3 -d " " result) >actual &&
+ test_write_lines E D M L B A >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'using --advance with --ref' '
+ git branch test-ref-advance main &&
+ git branch test-ref-target main &&
+ test_when_finished "git branch -D test-ref-advance test-ref-target" &&
+
+ git replay --ref-action=print --advance=test-ref-advance --ref=refs/heads/test-ref-target topic1..topic2 >result &&
+
+ test_line_count = 1 result &&
+ test_grep "^update refs/heads/test-ref-target " result
+'
+
+test_expect_success 'using --revert with --ref' '
+ git branch test-ref-revert topic4 &&
+ git branch test-ref-revert-target topic4 &&
+ test_when_finished "git branch -D test-ref-revert test-ref-revert-target" &&
+
+ git replay --ref-action=print --revert=test-ref-revert --ref=refs/heads/test-ref-revert-target topic4~1..topic4 >result &&
+
+ test_line_count = 1 result &&
+ test_grep "^update refs/heads/test-ref-revert-target " result
+'
+
+test_expect_success '--ref is incompatible with --contained' '
+ test_must_fail git replay --onto=main --ref=refs/heads/main --contained topic1..topic2 2>err &&
+ test_grep "cannot be used together" err
+'
+
+test_expect_success '--ref with nonexistent fully-qualified ref' '
+ test_when_finished "git update-ref -d refs/heads/new-branch" &&
+
+ git replay --onto=main --ref=refs/heads/new-branch topic1..topic2 &&
+
+ git log --format=%s -2 new-branch >actual &&
+ test_write_lines E D >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--ref must be a valid refname' '
+ test_must_fail git replay --onto=main --ref="refs/heads/bad..ref" topic1..topic2 2>err &&
+ test_grep "is not a valid refname" err
+'
+
+test_expect_success '--ref requires fully qualified ref' '
+ test_must_fail git replay --onto=main --ref=main topic1..topic2 2>err &&
+ test_grep "is not a valid refname" err
+'
+
+test_expect_success '--onto with --ref rejects multiple revision ranges' '
+ test_must_fail git replay --onto=main --ref=refs/heads/topic2 ^topic1 topic2 topic4 2>err &&
+ test_grep "cannot be used with multiple revision ranges" err
+'
+
test_done