Skip to content

Commit e65123a

Browse files
prertikgitster
authored andcommitted
builtin rebase: support git rebase <upstream> <switch-to>
This commit adds support for `switch-to` which is used to switch to the target branch if needed. The equivalent codes found in shell script `git-legacy-rebase.sh` is converted to builtin `rebase.c`. Signed-off-by: Pratik Karki <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d4c569f commit e65123a

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

builtin/rebase.c

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct rebase_options {
7979
struct commit *onto;
8080
const char *onto_name;
8181
const char *revisions;
82+
const char *switch_to;
8283
int root;
8384
struct commit *restrict_revision;
8485
int dont_finish_rebase;
@@ -186,6 +187,8 @@ static int run_specific_rebase(struct rebase_options *opts)
186187
opts->flags & REBASE_DIFFSTAT ? "t" : "");
187188
add_var(&script_snippet, "force_rebase",
188189
opts->flags & REBASE_FORCE ? "t" : "");
190+
if (opts->switch_to)
191+
add_var(&script_snippet, "switch_to", opts->switch_to);
189192

190193
switch (opts->type) {
191194
case REBASE_AM:
@@ -564,9 +567,23 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
564567
* orig_head -- commit object name of tip of the branch before rebasing
565568
* head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
566569
*/
567-
if (argc > 0)
568-
die("TODO: handle switch_to");
569-
else {
570+
if (argc == 1) {
571+
/* Is it "rebase other branchname" or "rebase other commit"? */
572+
branch_name = argv[0];
573+
options.switch_to = argv[0];
574+
575+
/* Is it a local branch? */
576+
strbuf_reset(&buf);
577+
strbuf_addf(&buf, "refs/heads/%s", branch_name);
578+
if (!read_ref(buf.buf, &options.orig_head))
579+
options.head_name = xstrdup(buf.buf);
580+
/* If not is it a valid ref (branch or commit)? */
581+
else if (!get_oid(branch_name, &options.orig_head))
582+
options.head_name = NULL;
583+
else
584+
die(_("fatal: no such branch/commit '%s'"),
585+
branch_name);
586+
} else if (argc == 0) {
570587
/* Do not need to switch branches, we are already on it. */
571588
options.head_name =
572589
xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
@@ -585,7 +602,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
585602
}
586603
if (get_oid("HEAD", &options.orig_head))
587604
die(_("Could not resolve HEAD to a revision"));
588-
}
605+
} else
606+
BUG("unexpected number of arguments left to parse");
589607

590608
if (read_index(the_repository->index) < 0)
591609
die(_("could not read index"));
@@ -612,6 +630,28 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
612630
int flag;
613631

614632
if (!(options.flags & REBASE_FORCE)) {
633+
/* Lazily switch to the target branch if needed... */
634+
if (options.switch_to) {
635+
struct object_id oid;
636+
637+
if (get_oid(options.switch_to, &oid) < 0) {
638+
ret = !!error(_("could not parse '%s'"),
639+
options.switch_to);
640+
goto cleanup;
641+
}
642+
643+
strbuf_reset(&buf);
644+
strbuf_addf(&buf, "rebase: checkout %s",
645+
options.switch_to);
646+
if (reset_head(&oid, "checkout",
647+
options.head_name, 0) < 0) {
648+
ret = !!error(_("could not switch to "
649+
"%s"),
650+
options.switch_to);
651+
goto cleanup;
652+
}
653+
}
654+
615655
if (!(options.flags & REBASE_NO_QUIET))
616656
; /* be quiet */
617657
else if (!strcmp(branch_name, "HEAD") &&

0 commit comments

Comments
 (0)