rebase: store orig_head as a commit

Using a struct commit rather than a struct oid to hold orig_head means that we error out straight away if the branch being rebased does not point to a commit. It also simplifies the code that handles finding the merge base and fork point as it no longer has to convert from an oid to a commit. To avoid changing the behavior of "git rebase <upstream> <branch>" we keep the existing call to read_ref() and use lookup_commit_object() on the oid returned by that rather than calling lookup_commit_reference_by_name() which applies the ref dwim rules to its argument. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Oct 17, 2022 at 13:17 UTC f21becdd94d17ae792b951cf784898324bedfda5
1 file changed +31 -35
builtin/rebase.c
+31 -35
@@ -68,7 +68,7 @@ struct rebase_options {
68 const char *upstream_name;
69 const char *upstream_arg;
70 char *head_name;
71 - struct object_id orig_head;
71 + struct commit *orig_head;
72 struct commit *onto;
73 const char *onto_name;
74 const char *revisions;
@@ -260,13 +260,13 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
260 struct replay_opts replay = get_replay_opts(opts);
261 struct string_list commands = STRING_LIST_INIT_DUP;
262
263 - if (get_revision_ranges(opts->upstream, opts->onto, &opts->orig_head,
263 + if (get_revision_ranges(opts->upstream, opts->onto, &opts->orig_head->object.oid,
264 &revisions, &shortrevisions))
265 return -1;
266
267 if (init_basic_state(&replay,
268 opts->head_name ? opts->head_name : "detached HEAD",
269 - opts->onto, &opts->orig_head)) {
269 + opts->onto, &opts->orig_head->object.oid)) {
270 free(revisions);
271 free(shortrevisions);
272
@@ -297,8 +297,8 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
297 split_exec_commands(opts->cmd, &commands);
298 ret = complete_action(the_repository, &replay, flags,
299 shortrevisions, opts->onto_name, opts->onto,
300 - &opts->orig_head, &commands, opts->autosquash,
301 - &todo_list);
300 + &opts->orig_head->object.oid, &commands,
301 + opts->autosquash, &todo_list);
302 }
303
304 string_list_clear(&commands, 0);
@@ -446,7 +446,8 @@ static int read_basic_state(struct rebase_options *opts)
446 } else if (!read_oneliner(&buf, state_dir_path("head", opts),
447 READ_ONELINER_WARN_MISSING))
448 return -1;
449 - if (get_oid_hex(buf.buf, &opts->orig_head))
449 + if (get_oid_hex(buf.buf, &oid) ||
450 + !(opts->orig_head = lookup_commit_object(the_repository, &oid)))
451 return error(_("invalid orig-head: '%s'"), buf.buf);
452
453 if (file_exists(state_dir_path("quiet", opts)))
@@ -515,7 +516,7 @@ static int rebase_write_basic_state(struct rebase_options *opts)
516 write_file(state_dir_path("onto", opts), "%s",
517 opts->onto ? oid_to_hex(&opts->onto->object.oid) : "");
518 write_file(state_dir_path("orig-head", opts), "%s",
518 - oid_to_hex(&opts->orig_head));
519 + oid_to_hex(&opts->orig_head->object.oid));
520 if (!(opts->flags & REBASE_NO_QUIET))
521 write_file(state_dir_path("quiet", opts), "%s", "");
522 if (opts->flags & REBASE_VERBOSE)
@@ -644,7 +645,7 @@ static int run_am(struct rebase_options *opts)
645 /* this is now equivalent to !opts->upstream */
646 &opts->onto->object.oid :
647 &opts->upstream->object.oid),
647 - oid_to_hex(&opts->orig_head));
648 + oid_to_hex(&opts->orig_head->object.oid));
649
650 rebased_patches = xstrdup(git_path("rebased-patches"));
651 format_patch.out = open(rebased_patches,
@@ -678,7 +679,7 @@ static int run_am(struct rebase_options *opts)
679 free(rebased_patches);
680 strvec_clear(&am.args);
681
681 - ropts.oid = &opts->orig_head;
682 + ropts.oid = &opts->orig_head->object.oid;
683 ropts.branch = opts->head_name;
684 ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
685 reset_head(the_repository, &ropts);
@@ -826,7 +827,7 @@ static int checkout_up_to_date(struct rebase_options *options)
827 strbuf_addf(&buf, "%s: checkout %s",
828 getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
829 options->switch_to);
829 - ropts.oid = &options->orig_head;
830 + ropts.oid = &options->orig_head->object.oid;
831 ropts.branch = options->head_name;
832 ropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
833 if (!ropts.branch)
@@ -859,15 +860,11 @@ static int is_linear_history(struct commit *from, struct commit *to)
860
861 static int can_fast_forward(struct commit *onto, struct commit *upstream,
862 struct commit *restrict_revision,
862 - struct object_id *head_oid, struct object_id *merge_base)
863 + struct commit *head, struct object_id *merge_base)
864 {
864 - struct commit *head = lookup_commit(the_repository, head_oid);
865 struct commit_list *merge_bases = NULL;
866 int res = 0;
867
868 - if (!head)
869 - goto done;
870 -
868 merge_bases = get_merge_bases(onto, head);
869 if (!merge_bases || merge_bases->next) {
870 oidcpy(merge_base, null_oid());
@@ -1302,13 +1299,13 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1299
1300 if (read_basic_state(&options))
1301 exit(1);
1305 - ropts.oid = &options.orig_head;
1302 + ropts.oid = &options.orig_head->object.oid;
1303 ropts.branch = options.head_name;
1304 ropts.flags = RESET_HEAD_HARD;
1305 ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
1306 if (reset_head(the_repository, &ropts) < 0)
1307 die(_("could not move back to %s"),
1311 - oid_to_hex(&options.orig_head));
1308 + oid_to_hex(&options.orig_head->object.oid));
1309 remove_branch_state(the_repository, 0);
1310 ret = finish_rebase(&options);
1311 goto cleanup;
@@ -1594,25 +1591,27 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1591 */
1592 if (argc == 1) {
1593 /* Is it "rebase other branchname" or "rebase other commit"? */
1594 + struct object_id branch_oid;
1595 branch_name = argv[0];
1596 options.switch_to = argv[0];
1597
1598 /* Is it a local branch? */
1599 strbuf_reset(&buf);
1600 strbuf_addf(&buf, "refs/heads/%s", branch_name);
1603 - if (!read_ref(buf.buf, &options.orig_head)) {
1601 + if (!read_ref(buf.buf, &branch_oid)) {
1602 die_if_checked_out(buf.buf, 1);
1603 options.head_name = xstrdup(buf.buf);
1604 + options.orig_head =
1605 + lookup_commit_object(the_repository,
1606 + &branch_oid);
1607 /* If not is it a valid ref (branch or commit)? */
1608 } else {
1608 - struct commit *commit =
1609 + options.orig_head =
1610 lookup_commit_reference_by_name(branch_name);
1610 - if (!commit)
1611 - die(_("no such branch/commit '%s'"),
1612 - branch_name);
1613 - oidcpy(&options.orig_head, &commit->object.oid);
1611 options.head_name = NULL;
1612 }
1613 + if (!options.orig_head)
1614 + die(_("no such branch/commit '%s'"), branch_name);
1615 } else if (argc == 0) {
1616 /* Do not need to switch branches, we are already on it. */
1617 options.head_name =
@@ -1629,8 +1628,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1628 FREE_AND_NULL(options.head_name);
1629 branch_name = "HEAD";
1630 }
1632 - if (get_oid("HEAD", &options.orig_head))
1633 - die(_("Could not resolve HEAD to a revision"));
1631 + options.orig_head = lookup_commit_reference_by_name("HEAD");
1632 + if (!options.orig_head)
1633 + die(_("Could not resolve HEAD to a commit"));
1634 } else
1635 BUG("unexpected number of arguments left to parse");
1636
@@ -1662,13 +1662,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1662 options.onto_name);
1663 }
1664
1665 - if (options.fork_point > 0) {
1666 - struct commit *head =
1667 - lookup_commit_reference(the_repository,
1668 - &options.orig_head);
1665 + if (options.fork_point > 0)
1666 options.restrict_revision =
1670 - get_fork_point(options.upstream_name, head);
1671 - }
1667 + get_fork_point(options.upstream_name, options.orig_head);
1668
1669 if (repo_read_index(the_repository) < 0)
1670 die(_("could not read index"));
@@ -1698,7 +1694,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1694 * call it before checking allow_preemptive_ff.
1695 */
1696 if (can_fast_forward(options.onto, options.upstream, options.restrict_revision,
1701 - &options.orig_head, &merge_base) &&
1697 + options.orig_head, &merge_base) &&
1698 allow_preemptive_ff) {
1699 int flag;
1700
@@ -1775,7 +1771,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1771 strbuf_addf(&msg, "%s: checkout %s",
1772 getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
1773 ropts.oid = &options.onto->object.oid;
1778 - ropts.orig_head = &options.orig_head,
1774 + ropts.orig_head = &options.orig_head->object.oid,
1775 ropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |
1776 RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
1777 ropts.head_msg = msg.buf;
@@ -1789,7 +1785,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1785 * we just fast-forwarded.
1786 */
1787 strbuf_reset(&msg);
1792 - if (oideq(&merge_base, &options.orig_head)) {
1788 + if (oideq(&merge_base, &options.orig_head->object.oid)) {
1789 printf(_("Fast-forwarded %s to %s.\n"),
1790 branch_name, options.onto_name);
1791 strbuf_addf(&msg, "rebase finished: %s onto %s",
@@ -1810,7 +1806,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1806 (options.restrict_revision ?
1807 oid_to_hex(&options.restrict_revision->object.oid) :
1808 oid_to_hex(&options.upstream->object.oid)),
1813 - oid_to_hex(&options.orig_head));
1809 + oid_to_hex(&options.orig_head->object.oid));
1810
1811 options.revisions = revisions.buf;
1812