Skip to content

Commit 502d89c

Browse files
committed
Merge 'builtin-rebase--am'
This patch teaches the builtin rebase to avoid the scripted --am backend and call `git format-patch` and `git am` directly. Meaning: apart from the --merge and the --preserve-merges backends, `git rebase` is now implemented in pure C, with no need to ask the Unix shell interpreter for help. This brings us really close to a fully builtin `git rebase`: the --preserve-merges mode is about to be deprecated (as soon as the --rebase-merges mode has proven stable and robust enough), and there are plans to scrap the `git-rebase--merge` backend in favor of teaching the interactive rebase enough tricks to run the --merge mode, too. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 791d1af + 91eac90 commit 502d89c

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

builtin/rebase.c

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,37 @@ static int read_basic_state(struct rebase_options *opts)
241241
return 0;
242242
}
243243

244+
static int write_basic_state(struct rebase_options *opts)
245+
{
246+
write_file(state_dir_path("head-name", opts), "%s",
247+
opts->head_name ? opts->head_name : "detached HEAD");
248+
write_file(state_dir_path("onto", opts), "%s",
249+
opts->onto ? oid_to_hex(&opts->onto->object.oid) : "");
250+
write_file(state_dir_path("orig-head", opts), "%s",
251+
oid_to_hex(&opts->orig_head));
252+
write_file(state_dir_path("quiet", opts), "%s",
253+
opts->flags & REBASE_NO_QUIET ? "" : "t");
254+
if (opts->flags & REBASE_VERBOSE)
255+
write_file(state_dir_path("verbose", opts), "");
256+
if (opts->strategy)
257+
write_file(state_dir_path("strategy", opts), "%s",
258+
opts->strategy);
259+
if (opts->strategy_opts)
260+
write_file(state_dir_path("strategy_opts", opts), "%s",
261+
opts->strategy_opts);
262+
if (opts->allow_rerere_autoupdate >= 0)
263+
write_file(state_dir_path("allow_rerere_autoupdate", opts),
264+
"-%s-rerere-autoupdate",
265+
opts->allow_rerere_autoupdate ? "" : "-no");
266+
if (opts->gpg_sign_opt)
267+
write_file(state_dir_path("gpg_sign_opt", opts), "%s",
268+
opts->gpg_sign_opt);
269+
if (opts->signoff)
270+
write_file(state_dir_path("strategy", opts), "--signoff");
271+
272+
return 0;
273+
}
274+
244275
static int apply_autostash(struct rebase_options *opts)
245276
{
246277
const char *path = state_dir_path("autostash", opts);
@@ -333,6 +364,148 @@ N_("Resolve all conflicts manually, mark them as resolved with\n"
333364
"To abort and get back to the state before \"git rebase\", run "
334365
"\"git rebase --abort\".");
335366

367+
static int reset_head(struct object_id *oid, const char *action,
368+
const char *switch_to_branch, int detach_head,
369+
const char *reflog_orig_head, const char *reflog_head);
370+
371+
static int move_to_original_branch(struct rebase_options *opts)
372+
{
373+
struct strbuf buf = STRBUF_INIT;
374+
int ret;
375+
376+
if (opts->head_name && opts->onto)
377+
strbuf_addf(&buf, "rebase finished: %s onto %s",
378+
opts->head_name,
379+
oid_to_hex(&opts->onto->object.oid));
380+
ret = reset_head(NULL, "checkout", opts->head_name, 0,
381+
"HEAD", buf.buf);
382+
383+
strbuf_release(&buf);
384+
return ret;
385+
}
386+
387+
static int run_am(struct rebase_options *opts)
388+
{
389+
struct child_process am = CHILD_PROCESS_INIT;
390+
struct child_process format_patch = CHILD_PROCESS_INIT;
391+
struct strbuf revisions = STRBUF_INIT;
392+
int status;
393+
char *rebased_patches;
394+
395+
am.git_cmd = 1;
396+
argv_array_push(&am.args, "am");
397+
398+
if (opts->action && !strcmp("continue", opts->action)) {
399+
argv_array_push(&am.args, "--resolved");
400+
argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
401+
if (opts->gpg_sign_opt)
402+
argv_array_push(&am.args, opts->gpg_sign_opt);
403+
status = run_command(&am);
404+
if (status)
405+
return status;
406+
407+
discard_cache();
408+
return move_to_original_branch(opts);
409+
}
410+
if (opts->action && !strcmp("skip", opts->action)) {
411+
argv_array_push(&am.args, "--skip");
412+
argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
413+
status = run_command(&am);
414+
if (status)
415+
return status;
416+
417+
discard_cache();
418+
return move_to_original_branch(opts);
419+
}
420+
if (opts->action && !strcmp("show-current-patch", opts->action)) {
421+
argv_array_push(&am.args, "--show-current-patch");
422+
return run_command(&am);
423+
}
424+
425+
strbuf_addf(&revisions, "%s...%s",
426+
oid_to_hex(opts->root ?
427+
/* this is now equivalent to ! -z "$upstream" */
428+
&opts->onto->object.oid :
429+
&opts->upstream->object.oid),
430+
oid_to_hex(&opts->orig_head));
431+
432+
rebased_patches = xstrdup(git_path("rebased-patches"));
433+
format_patch.out = open(rebased_patches, O_WRONLY | O_CREAT, 0666);
434+
if (format_patch.out < 0) {
435+
status = error_errno(_("could not write '%s'"),
436+
rebased_patches);
437+
free(rebased_patches);
438+
argv_array_clear(&am.args);
439+
return status;
440+
}
441+
442+
format_patch.git_cmd = 1;
443+
argv_array_pushl(&format_patch.args, "format-patch", "-k", "--stdout",
444+
"--full-index", "--cherry-pick", "--right-only",
445+
"--src-prefix=a/", "--dst-prefix=b/", "--no-renames",
446+
"--no-cover-letter", "--pretty=mboxrd", NULL);
447+
if (opts->git_format_patch_opt.len)
448+
argv_array_split(&format_patch.args,
449+
opts->git_format_patch_opt.buf);
450+
argv_array_push(&format_patch.args, revisions.buf);
451+
if (opts->restrict_revision)
452+
argv_array_pushf(&format_patch.args, "^%s",
453+
oid_to_hex(&opts->restrict_revision->object.oid));
454+
455+
status = run_command(&format_patch);
456+
if (status) {
457+
unlink(rebased_patches);
458+
free(rebased_patches);
459+
argv_array_clear(&am.args);
460+
461+
reset_head(&opts->orig_head, "checkout", opts->head_name, 0,
462+
"HEAD", NULL);
463+
error(_("\ngit encountered an error while preparing the "
464+
"patches to replay\n"
465+
"these revisions:\n"
466+
"\n %s\n\n"
467+
"As a result, git cannot rebase them."),
468+
opts->revisions);
469+
470+
strbuf_release(&revisions);
471+
return status;
472+
}
473+
strbuf_release(&revisions);
474+
475+
am.in = open(rebased_patches, O_RDONLY);
476+
if (am.in < 0) {
477+
status = error_errno(_("could not read '%s'"),
478+
rebased_patches);
479+
free(rebased_patches);
480+
argv_array_clear(&am.args);
481+
return status;
482+
}
483+
484+
argv_array_split(&am.args, opts->git_am_opt.buf);
485+
argv_array_push(&am.args, "--rebasing");
486+
argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
487+
argv_array_push(&am.args, "--patch-format=mboxrd");
488+
if (opts->allow_rerere_autoupdate > 0)
489+
argv_array_push(&am.args, "--rerere-autoupdate");
490+
else if (opts->allow_rerere_autoupdate == 0)
491+
argv_array_push(&am.args, "--no-rerere-autoupdate");
492+
if (opts->gpg_sign_opt)
493+
argv_array_push(&am.args, opts->gpg_sign_opt);
494+
status = run_command(&am);
495+
unlink(rebased_patches);
496+
free(rebased_patches);
497+
498+
if (!status) {
499+
discard_cache();
500+
return move_to_original_branch(opts);
501+
}
502+
503+
if (is_directory(opts->state_dir))
504+
write_basic_state(opts);
505+
506+
return status;
507+
}
508+
336509
static int run_specific_rebase(struct rebase_options *opts)
337510
{
338511
const char *argv[] = { NULL, NULL };
@@ -413,6 +586,11 @@ static int run_specific_rebase(struct rebase_options *opts)
413586
goto finished_rebase;
414587
}
415588

589+
if (opts->type == REBASE_AM) {
590+
status = run_am(opts);
591+
goto finished_rebase;
592+
}
593+
416594
add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
417595
add_var(&script_snippet, "state_dir", opts->state_dir);
418596

0 commit comments

Comments
 (0)