Skip to content

Commit 4912066

Browse files
committed
built-in rebase: call git am directly
While the scripted `git rebase` still has to rely on the `git-rebase--am.sh` script to implement the glue between the `rebase` and the `am` commands, we can go a more direct route in the built-in rebase and avoid using a shell script altogether. This patch represents a straight-forward port of `git-rebase--am.sh` to C, along with the glue code to call it directly from within `builtin/rebase.c`. This reduces the chances of Git for Windows running into trouble due to problems with the POSIX emulation layer (known as "MSYS2 runtime", itself a derivative of the Cygwin runtime): when no shell script is called, the POSIX emulation layer is avoided altogether. Note: we pass an empty action to `reset_head()` here when moving back to the original branch, as no other action is applicable, really. This parameter is used to initialize `unpack_trees()`' messages. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 31bb293 commit 4912066

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

builtin/rebase.c

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,37 @@ static int read_basic_state(struct rebase_options *opts)
246246
return 0;
247247
}
248248

249+
static int write_basic_state(struct rebase_options *opts)
250+
{
251+
write_file(state_dir_path("head-name", opts), "%s",
252+
opts->head_name ? opts->head_name : "detached HEAD");
253+
write_file(state_dir_path("onto", opts), "%s",
254+
opts->onto ? oid_to_hex(&opts->onto->object.oid) : "");
255+
write_file(state_dir_path("orig-head", opts), "%s",
256+
oid_to_hex(&opts->orig_head));
257+
write_file(state_dir_path("quiet", opts), "%s",
258+
opts->flags & REBASE_NO_QUIET ? "" : "t");
259+
if (opts->flags & REBASE_VERBOSE)
260+
write_file(state_dir_path("verbose", opts), "%s", "");
261+
if (opts->strategy)
262+
write_file(state_dir_path("strategy", opts), "%s",
263+
opts->strategy);
264+
if (opts->strategy_opts)
265+
write_file(state_dir_path("strategy_opts", opts), "%s",
266+
opts->strategy_opts);
267+
if (opts->allow_rerere_autoupdate >= 0)
268+
write_file(state_dir_path("allow_rerere_autoupdate", opts),
269+
"-%s-rerere-autoupdate",
270+
opts->allow_rerere_autoupdate ? "" : "-no");
271+
if (opts->gpg_sign_opt)
272+
write_file(state_dir_path("gpg_sign_opt", opts), "%s",
273+
opts->gpg_sign_opt);
274+
if (opts->signoff)
275+
write_file(state_dir_path("strategy", opts), "--signoff");
276+
277+
return 0;
278+
}
279+
249280
static int apply_autostash(struct rebase_options *opts)
250281
{
251282
const char *path = state_dir_path("autostash", opts);
@@ -465,13 +496,156 @@ static int reset_head(struct object_id *oid, const char *action,
465496
return ret;
466497
}
467498

499+
static int move_to_original_branch(struct rebase_options *opts)
500+
{
501+
struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
502+
int ret;
503+
504+
if (!opts->head_name)
505+
return 0; /* nothing to move back to */
506+
507+
if (!opts->onto)
508+
BUG("move_to_original_branch without onto");
509+
510+
strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s",
511+
opts->head_name, oid_to_hex(&opts->onto->object.oid));
512+
strbuf_addf(&head_reflog, "rebase finished: returning to %s",
513+
opts->head_name);
514+
ret = reset_head(NULL, "", opts->head_name, RESET_HEAD_REFS_ONLY,
515+
orig_head_reflog.buf, head_reflog.buf);
516+
517+
strbuf_release(&orig_head_reflog);
518+
strbuf_release(&head_reflog);
519+
return ret;
520+
}
521+
468522
static const char *resolvemsg =
469523
N_("Resolve all conflicts manually, mark them as resolved with\n"
470524
"\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
471525
"You can instead skip this commit: run \"git rebase --skip\".\n"
472526
"To abort and get back to the state before \"git rebase\", run "
473527
"\"git rebase --abort\".");
474528

529+
static int run_am(struct rebase_options *opts)
530+
{
531+
struct child_process am = CHILD_PROCESS_INIT;
532+
struct child_process format_patch = CHILD_PROCESS_INIT;
533+
struct strbuf revisions = STRBUF_INIT;
534+
int status;
535+
char *rebased_patches;
536+
537+
am.git_cmd = 1;
538+
argv_array_push(&am.args, "am");
539+
540+
if (opts->action && !strcmp("continue", opts->action)) {
541+
argv_array_push(&am.args, "--resolved");
542+
argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
543+
if (opts->gpg_sign_opt)
544+
argv_array_push(&am.args, opts->gpg_sign_opt);
545+
status = run_command(&am);
546+
if (status)
547+
return status;
548+
549+
return move_to_original_branch(opts);
550+
}
551+
if (opts->action && !strcmp("skip", opts->action)) {
552+
argv_array_push(&am.args, "--skip");
553+
argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
554+
status = run_command(&am);
555+
if (status)
556+
return status;
557+
558+
return move_to_original_branch(opts);
559+
}
560+
if (opts->action && !strcmp("show-current-patch", opts->action)) {
561+
argv_array_push(&am.args, "--show-current-patch");
562+
return run_command(&am);
563+
}
564+
565+
strbuf_addf(&revisions, "%s...%s",
566+
oid_to_hex(opts->root ?
567+
/* this is now equivalent to !opts->upstream */
568+
&opts->onto->object.oid :
569+
&opts->upstream->object.oid),
570+
oid_to_hex(&opts->orig_head));
571+
572+
rebased_patches = xstrdup(git_path("rebased-patches"));
573+
format_patch.out = open(rebased_patches,
574+
O_WRONLY | O_CREAT | O_TRUNC, 0666);
575+
if (format_patch.out < 0) {
576+
status = error_errno(_("could not open '%s' for writing"),
577+
rebased_patches);
578+
free(rebased_patches);
579+
argv_array_clear(&am.args);
580+
return status;
581+
}
582+
583+
format_patch.git_cmd = 1;
584+
argv_array_pushl(&format_patch.args, "format-patch", "-k", "--stdout",
585+
"--full-index", "--cherry-pick", "--right-only",
586+
"--src-prefix=a/", "--dst-prefix=b/", "--no-renames",
587+
"--no-cover-letter", "--pretty=mboxrd", NULL);
588+
if (opts->git_format_patch_opt.len)
589+
argv_array_split(&format_patch.args,
590+
opts->git_format_patch_opt.buf);
591+
argv_array_push(&format_patch.args, revisions.buf);
592+
if (opts->restrict_revision)
593+
argv_array_pushf(&format_patch.args, "^%s",
594+
oid_to_hex(&opts->restrict_revision->object.oid));
595+
596+
status = run_command(&format_patch);
597+
if (status) {
598+
unlink(rebased_patches);
599+
free(rebased_patches);
600+
argv_array_clear(&am.args);
601+
602+
reset_head(&opts->orig_head, "checkout", opts->head_name, 0,
603+
"HEAD", NULL);
604+
error(_("\ngit encountered an error while preparing the "
605+
"patches to replay\n"
606+
"these revisions:\n"
607+
"\n %s\n\n"
608+
"As a result, git cannot rebase them."),
609+
opts->revisions);
610+
611+
strbuf_release(&revisions);
612+
return status;
613+
}
614+
strbuf_release(&revisions);
615+
616+
am.in = open(rebased_patches, O_RDONLY);
617+
if (am.in < 0) {
618+
status = error_errno(_("could not open '%s' for reading"),
619+
rebased_patches);
620+
free(rebased_patches);
621+
argv_array_clear(&am.args);
622+
return status;
623+
}
624+
625+
argv_array_pushv(&am.args, opts->git_am_opts.argv);
626+
argv_array_push(&am.args, "--rebasing");
627+
argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
628+
argv_array_push(&am.args, "--patch-format=mboxrd");
629+
if (opts->allow_rerere_autoupdate > 0)
630+
argv_array_push(&am.args, "--rerere-autoupdate");
631+
else if (opts->allow_rerere_autoupdate == 0)
632+
argv_array_push(&am.args, "--no-rerere-autoupdate");
633+
if (opts->gpg_sign_opt)
634+
argv_array_push(&am.args, opts->gpg_sign_opt);
635+
status = run_command(&am);
636+
unlink(rebased_patches);
637+
free(rebased_patches);
638+
639+
if (!status) {
640+
return move_to_original_branch(opts);
641+
}
642+
643+
if (is_directory(opts->state_dir))
644+
write_basic_state(opts);
645+
646+
return status;
647+
}
648+
475649
static int run_specific_rebase(struct rebase_options *opts)
476650
{
477651
const char *argv[] = { NULL, NULL };
@@ -552,6 +726,11 @@ static int run_specific_rebase(struct rebase_options *opts)
552726
goto finished_rebase;
553727
}
554728

729+
if (opts->type == REBASE_AM) {
730+
status = run_am(opts);
731+
goto finished_rebase;
732+
}
733+
555734
add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
556735
add_var(&script_snippet, "state_dir", opts->state_dir);
557736

0 commit comments

Comments
 (0)