sequencer: allow introducing new root commits
In the context of the new --rebase-merges mode, which was designed specifically to allow for changing the existing branch topology liberally, a user may want to extract commits into a completely fresh branch that starts with a newly-created root commit. This is now possible by inserting the command `reset [new root]` before `pick`ing the commit that wants to become a root commit. Example: reset [new root] pick 012345 a commit that is about to become a root commit pick 234567 this commit will have the previous one as parent This does not conflict with other uses of the `reset` command because `[new root]` is not (part of) a valid ref name: both the opening bracket as well as the space are illegal in ref names. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
21d0764c82
commit
ebddf39396
16
sequencer.c
16
sequencer.c
@ -2747,6 +2747,21 @@ static int do_reset(const char *name, int len, struct replay_opts *opts)
|
|||||||
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
|
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
if (len == 10 && !strncmp("[new root]", name, len)) {
|
||||||
|
if (!opts->have_squash_onto) {
|
||||||
|
const char *hex;
|
||||||
|
if (commit_tree("", 0, the_hash_algo->empty_tree,
|
||||||
|
NULL, &opts->squash_onto,
|
||||||
|
NULL, NULL))
|
||||||
|
return error(_("writing fake root commit"));
|
||||||
|
opts->have_squash_onto = 1;
|
||||||
|
hex = oid_to_hex(&opts->squash_onto);
|
||||||
|
if (write_message(hex, strlen(hex),
|
||||||
|
rebase_path_squash_onto(), 0))
|
||||||
|
return error(_("writing squash-onto"));
|
||||||
|
}
|
||||||
|
oidcpy(&oid, &opts->squash_onto);
|
||||||
|
} else {
|
||||||
/* Determine the length of the label */
|
/* Determine the length of the label */
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
if (isspace(name[i]))
|
if (isspace(name[i]))
|
||||||
@ -2760,6 +2775,7 @@ static int do_reset(const char *name, int len, struct replay_opts *opts)
|
|||||||
strbuf_release(&ref_name);
|
strbuf_release(&ref_name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
|
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
|
||||||
setup_unpack_trees_porcelain(&unpack_tree_opts, "reset");
|
setup_unpack_trees_porcelain(&unpack_tree_opts, "reset");
|
||||||
|
@ -241,4 +241,38 @@ test_expect_success 'refuse to merge ancestors of HEAD' '
|
|||||||
test_cmp_rev HEAD $before
|
test_cmp_rev HEAD $before
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success 'root commits' '
|
||||||
|
git checkout --orphan unrelated &&
|
||||||
|
(GIT_AUTHOR_NAME="Parsnip" GIT_AUTHOR_EMAIL="root@example.com" \
|
||||||
|
test_commit second-root) &&
|
||||||
|
test_commit third-root &&
|
||||||
|
cat >script-from-scratch <<-\EOF &&
|
||||||
|
pick third-root
|
||||||
|
label first-branch
|
||||||
|
reset [new root]
|
||||||
|
pick second-root
|
||||||
|
merge first-branch # Merge the 3rd root
|
||||||
|
EOF
|
||||||
|
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
|
||||||
|
test_tick &&
|
||||||
|
git rebase -i --force --root -r &&
|
||||||
|
test "Parsnip" = "$(git show -s --format=%an HEAD^)" &&
|
||||||
|
test $(git rev-parse second-root^0) != $(git rev-parse HEAD^) &&
|
||||||
|
test $(git rev-parse second-root:second-root.t) = \
|
||||||
|
$(git rev-parse HEAD^:second-root.t) &&
|
||||||
|
test_cmp_graph HEAD <<-\EOF &&
|
||||||
|
* Merge the 3rd root
|
||||||
|
|\
|
||||||
|
| * third-root
|
||||||
|
* second-root
|
||||||
|
EOF
|
||||||
|
|
||||||
|
: fast forward if possible &&
|
||||||
|
before="$(git rev-parse --verify HEAD)" &&
|
||||||
|
test_might_fail git config --unset sequence.editor &&
|
||||||
|
test_tick &&
|
||||||
|
git rebase -i --root -r &&
|
||||||
|
test_cmp_rev HEAD $before
|
||||||
|
'
|
||||||
|
|
||||||
test_done
|
test_done
|
||||||
|
Reference in New Issue
Block a user