rebase: add --update-refs option

When working on a large feature, it can be helpful to break that feature
into multiple smaller parts that become reviewed in sequence. During
development or during review, a change to one part of the feature could
affect multiple of these parts. An interactive rebase can help adjust
the multi-part "story" of the branch.

However, if there are branches tracking the different parts of the
feature, then rebasing the entire list of commits can create commits not
reachable from those "sub branches". It can take a manual step to update
those branches.

Add a new --update-refs option to 'git rebase -i' that adds 'update-ref
<ref>' steps to the todo file whenever a commit that is being rebased is
decorated with that <ref>. At the very end, the rebase process updates
all of the listed refs to the values stored during the rebase operation.

Be sure to iterate after any squashing or fixups are placed. Update the
branch only after those squashes and fixups are complete. This allows a
--fixup commit at the tip of the feature to apply correctly to the sub
branch, even if it is fixing up the most-recent commit in that part.

This change update the documentation and builtin to accept the
--update-refs option as well as updating the todo file with the
'update-ref' commands. Tests are added to ensure that these todo
commands are added in the correct locations.

This change does _not_ include the actual behavior of tracking the
updated refs and writing the new ref values at the end of the rebase
process. That is deferred to a later change.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Derrick Stolee
2022-07-19 18:33:39 +00:00
committed by Junio C Hamano
parent a97d79163e
commit 900b50c242
6 changed files with 216 additions and 0 deletions

View File

@ -1743,6 +1743,80 @@ test_expect_success 'ORIG_HEAD is updated correctly' '
test_cmp_rev ORIG_HEAD test-orig-head@{1}
'
test_expect_success '--update-refs adds label and update-ref commands' '
git checkout -b update-refs no-conflict-branch &&
git branch -f base HEAD~4 &&
git branch -f first HEAD~3 &&
git branch -f second HEAD~3 &&
git branch -f third HEAD~1 &&
git commit --allow-empty --fixup=third &&
git branch -f is-not-reordered &&
git commit --allow-empty --fixup=HEAD~4 &&
git branch -f shared-tip &&
(
set_cat_todo_editor &&
cat >expect <<-EOF &&
pick $(git log -1 --format=%h J) J
fixup $(git log -1 --format=%h update-refs) fixup! J # empty
update-ref refs/heads/second
update-ref refs/heads/first
pick $(git log -1 --format=%h K) K
pick $(git log -1 --format=%h L) L
fixup $(git log -1 --format=%h is-not-reordered) fixup! L # empty
update-ref refs/heads/third
pick $(git log -1 --format=%h M) M
update-ref refs/heads/no-conflict-branch
update-ref refs/heads/is-not-reordered
update-ref refs/heads/shared-tip
EOF
test_must_fail git rebase -i --autosquash --update-refs primary >todo &&
test_cmp expect todo
)
'
test_expect_success '--update-refs adds commands with --rebase-merges' '
git checkout -b update-refs-with-merge no-conflict-branch &&
git branch -f base HEAD~4 &&
git branch -f first HEAD~3 &&
git branch -f second HEAD~3 &&
git branch -f third HEAD~1 &&
git merge -m merge branch2 &&
git branch -f merge-branch &&
git commit --fixup=third --allow-empty &&
(
set_cat_todo_editor &&
cat >expect <<-EOF &&
label onto
reset onto
pick $(git log -1 --format=%h branch2~1) F
pick $(git log -1 --format=%h branch2) I
update-ref refs/heads/branch2
label merge
reset onto
pick $(git log -1 --format=%h refs/heads/second) J
update-ref refs/heads/second
update-ref refs/heads/first
pick $(git log -1 --format=%h refs/heads/third~1) K
pick $(git log -1 --format=%h refs/heads/third) L
fixup $(git log -1 --format=%h update-refs-with-merge) fixup! L # empty
update-ref refs/heads/third
pick $(git log -1 --format=%h HEAD~2) M
update-ref refs/heads/no-conflict-branch
merge -C $(git log -1 --format=%h HEAD~1) merge # merge
update-ref refs/heads/merge-branch
EOF
test_must_fail git rebase -i --autosquash \
--rebase-merges=rebase-cousins \
--update-refs primary >todo &&
test_cmp expect todo
)
'
# This must be the last test in this file
test_expect_success '$EDITOR and friends are unchanged' '
test_editor_unchanged