Skip to content

Commit b1ab78b

Browse files
committed
Merge 'pk/rebase-in-c-5-test'
This fifth batch of builtin rebase patches concludes the conversion: the builtin rebase is now feature-complete. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents d54a687 + 17f09d1 commit b1ab78b

File tree

1 file changed

+135
-15
lines changed

1 file changed

+135
-15
lines changed

builtin/rebase.c

Lines changed: 135 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ struct rebase_options {
9898
int allow_empty_message;
9999
int rebase_merges, rebase_cousins;
100100
char *strategy, *strategy_opts;
101+
struct strbuf git_format_patch_opt;
101102
};
102103

103104
static int is_interactive(struct rebase_options *opts)
@@ -379,6 +380,15 @@ static int run_specific_rebase(struct rebase_options *opts)
379380
add_var(&script_snippet, "rebase_root", opts->root ? "t" : "");
380381
add_var(&script_snippet, "squash_onto",
381382
opts->squash_onto ? oid_to_hex(opts->squash_onto) : "");
383+
add_var(&script_snippet, "git_format_patch_opt",
384+
opts->git_format_patch_opt.buf);
385+
386+
if (is_interactive(opts) &&
387+
!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
388+
strbuf_addstr(&script_snippet,
389+
"GIT_EDITOR=:; export GIT_EDITOR; ");
390+
opts->autosquash = 0;
391+
}
382392

383393
switch (opts->type) {
384394
case REBASE_AM:
@@ -431,7 +441,8 @@ static int run_specific_rebase(struct rebase_options *opts)
431441
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
432442

433443
static int reset_head(struct object_id *oid, const char *action,
434-
const char *switch_to_branch, int detach_head)
444+
const char *switch_to_branch, int detach_head,
445+
const char *reflog_orig_head, const char *reflog_head)
435446
{
436447
struct object_id head_oid;
437448
struct tree_desc desc;
@@ -506,20 +517,26 @@ static int reset_head(struct object_id *oid, const char *action,
506517
old_orig = &oid_old_orig;
507518
if (!get_oid("HEAD", &oid_orig)) {
508519
orig = &oid_orig;
509-
strbuf_addstr(&msg, "updating ORIG_HEAD");
510-
update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
520+
if (!reflog_orig_head) {
521+
strbuf_addstr(&msg, "updating ORIG_HEAD");
522+
reflog_orig_head = msg.buf;
523+
}
524+
update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
511525
UPDATE_REFS_MSG_ON_ERR);
512526
} else if (old_orig)
513527
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
514-
strbuf_setlen(&msg, prefix_len);
515-
strbuf_addstr(&msg, "updating HEAD");
528+
if (!reflog_head) {
529+
strbuf_setlen(&msg, prefix_len);
530+
strbuf_addstr(&msg, "updating HEAD");
531+
reflog_head = msg.buf;
532+
}
516533
if (!switch_to_branch)
517-
ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
534+
ret = update_ref(reflog_head, "HEAD", oid, orig, REF_NO_DEREF,
518535
UPDATE_REFS_MSG_ON_ERR);
519536
else {
520537
ret = create_symref("HEAD", switch_to_branch, msg.buf);
521538
if (!ret)
522-
ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
539+
ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
523540
UPDATE_REFS_MSG_ON_ERR);
524541
}
525542

@@ -622,6 +639,36 @@ static int parse_opt_interactive(const struct option *opt, const char *arg,
622639
return 0;
623640
}
624641

642+
static void NORETURN error_on_missing_default_upstream(void)
643+
{
644+
struct branch *current_branch = branch_get(NULL);
645+
646+
printf(_("%s\n"
647+
"Please specify which branch you want to rebase against.\n"
648+
"See git-rebase(1) for details.\n"
649+
"\n"
650+
" git rebase '<branch>'\n"
651+
"\n"),
652+
current_branch ? _("There is no tracking information for "
653+
"the current branch.") :
654+
_("You are not currently on a branch."));
655+
656+
if (current_branch) {
657+
const char *remote = current_branch->remote_name;
658+
659+
if (!remote)
660+
remote = _("<remote>");
661+
662+
printf(_("If you wish to set tracking information for this "
663+
"branch you can do so with:\n"
664+
"\n"
665+
" git branch --set-upstream-to=%s/<branch> %s\n"
666+
"\n"),
667+
remote, current_branch->name);
668+
}
669+
exit(1);
670+
}
671+
625672
int cmd_rebase(int argc, const char **argv, const char *prefix)
626673
{
627674
struct rebase_options options = {
@@ -630,6 +677,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
630677
.git_am_opt = STRBUF_INIT,
631678
.allow_rerere_autoupdate = -1,
632679
.allow_empty_message = 1,
680+
.git_format_patch_opt = STRBUF_INIT,
633681
};
634682
const char *branch_name;
635683
int ret, flags, total_argc, in_progress = 0;
@@ -870,7 +918,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
870918
rerere_clear(&merge_rr);
871919
string_list_clear(&merge_rr, 1);
872920

873-
if (reset_head(NULL, "reset", NULL, 0) < 0)
921+
if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0)
874922
die(_("could not discard worktree changes"));
875923
if (read_basic_state(&options))
876924
exit(1);
@@ -886,7 +934,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
886934
if (read_basic_state(&options))
887935
exit(1);
888936
if (reset_head(&options.orig_head, "reset",
889-
options.head_name, 0) < 0)
937+
options.head_name, 0, NULL, NULL) < 0)
890938
die(_("could not move back to %s"),
891939
oid_to_hex(&options.orig_head));
892940
ret = finish_rebase(&options);
@@ -1032,6 +1080,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10321080
if (options.root && !options.onto_name)
10331081
imply_interactive(&options, "--root without --onto");
10341082

1083+
if (isatty(2) && options.flags & REBASE_NO_QUIET)
1084+
strbuf_addstr(&options.git_format_patch_opt, " --progress");
1085+
10351086
switch (options.type) {
10361087
case REBASE_MERGE:
10371088
case REBASE_INTERACTIVE:
@@ -1048,6 +1099,28 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10481099
break;
10491100
}
10501101

1102+
if (options.git_am_opt.len) {
1103+
const char *p;
1104+
1105+
/* all am options except -q are compatible only with --am */
1106+
strbuf_reset(&buf);
1107+
strbuf_addbuf(&buf, &options.git_am_opt);
1108+
strbuf_addch(&buf, ' ');
1109+
while ((p = strstr(buf.buf, " -q ")))
1110+
strbuf_splice(&buf, p - buf.buf, 4, " ", 1);
1111+
strbuf_trim(&buf);
1112+
1113+
if (is_interactive(&options) && buf.len)
1114+
die(_("error: cannot combine interactive options "
1115+
"(--interactive, --exec, --rebase-merges, "
1116+
"--preserve-merges, --keep-empty, --root + "
1117+
"--onto) with am options (%s)"), buf.buf);
1118+
if (options.type == REBASE_MERGE && buf.len)
1119+
die(_("error: cannot combine merge options (--merge, "
1120+
"--strategy, --strategy-option) with am options "
1121+
"(%s)"), buf.buf);
1122+
}
1123+
10511124
if (options.signoff) {
10521125
if (options.type == REBASE_PRESERVE_MERGES)
10531126
die("cannot combine '--signoff' with "
@@ -1056,10 +1129,37 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10561129
options.flags |= REBASE_FORCE;
10571130
}
10581131

1132+
if (options.type == REBASE_PRESERVE_MERGES)
1133+
/*
1134+
* Note: incompatibility with --signoff handled in signoff block above
1135+
* Note: incompatibility with --interactive is just a strong warning;
1136+
* git-rebase.txt caveats with "unless you know what you are doing"
1137+
*/
1138+
if (options.rebase_merges)
1139+
die(_("error: cannot combine '--preserve_merges' with "
1140+
"'--rebase-merges'"));
1141+
1142+
if (options.rebase_merges) {
1143+
if (strategy_options.nr)
1144+
die(_("error: cannot combine '--rebase_merges' with "
1145+
"'--strategy-option'"));
1146+
if (options.strategy)
1147+
die(_("error: cannot combine '--rebase_merges' with "
1148+
"'--strategy'"));
1149+
}
1150+
10591151
if (!options.root) {
1060-
if (argc < 1)
1061-
die("TODO: handle @{upstream}");
1062-
else {
1152+
if (argc < 1) {
1153+
struct branch *branch;
1154+
1155+
branch = branch_get(NULL);
1156+
options.upstream_name = branch_get_upstream(branch,
1157+
NULL);
1158+
if (!options.upstream_name)
1159+
error_on_missing_default_upstream();
1160+
if (fork_point < 0)
1161+
fork_point = 1;
1162+
} else {
10631163
options.upstream_name = argv[0];
10641164
argc--;
10651165
argv++;
@@ -1198,7 +1298,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
11981298
write_file(autostash, "%s", buf.buf);
11991299
printf(_("Created autostash: %s\n"), buf.buf);
12001300
if (reset_head(&head->object.oid, "reset --hard",
1201-
NULL, 0) < 0)
1301+
NULL, 0, NULL, NULL) < 0)
12021302
die(_("could not reset --hard"));
12031303
printf(_("HEAD is now at %s"),
12041304
find_unique_abbrev(&head->object.oid,
@@ -1252,7 +1352,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
12521352
strbuf_addf(&buf, "rebase: checkout %s",
12531353
options.switch_to);
12541354
if (reset_head(&oid, "checkout",
1255-
options.head_name, 0) < 0) {
1355+
options.head_name, 0,
1356+
NULL, NULL) < 0) {
12561357
ret = !!error(_("could not switch to "
12571358
"%s"),
12581359
options.switch_to);
@@ -1317,10 +1418,29 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
13171418
"it...\n"));
13181419

13191420
strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1320-
if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
1421+
if (reset_head(&options.onto->object.oid, "checkout", NULL, 1,
1422+
NULL, msg.buf))
13211423
die(_("Could not detach HEAD"));
13221424
strbuf_release(&msg);
13231425

1426+
/*
1427+
* If the onto is a proper descendant of the tip of the branch, then
1428+
* we just fast-forwarded.
1429+
*/
1430+
strbuf_reset(&msg);
1431+
if (!oidcmp(&merge_base, &options.orig_head)) {
1432+
printf(_("Fast-forwarded %s to %s. \n"),
1433+
branch_name, options.onto_name);
1434+
strbuf_addf(&msg, "rebase finished: %s onto %s",
1435+
options.head_name ? options.head_name : "detached HEAD",
1436+
oid_to_hex(&options.onto->object.oid));
1437+
reset_head(NULL, "Fast-forwarded", options.head_name, 0,
1438+
"HEAD", msg.buf);
1439+
strbuf_release(&msg);
1440+
ret = !!finish_rebase(&options);
1441+
goto cleanup;
1442+
}
1443+
13241444
strbuf_addf(&revisions, "%s..%s",
13251445
options.root ? oid_to_hex(&options.onto->object.oid) :
13261446
(options.restrict_revision ?

0 commit comments

Comments
 (0)