Skip to content

Commit a68561d

Browse files
committed
Merge branch 'pk/rebase-in-c-5-test' into pu
* pk/rebase-in-c-5-test: builtin rebase: error out on incompatible option/mode combinations builtin rebase: use no-op editor when interactive is "implied" builtin rebase: show progress when connected to a terminal builtin rebase: fast-forward to onto if it is a proper descendant builtin rebase: optionally pass custom reflogs to reset_head() builtin rebase: optionally auto-detect the upstream
2 parents dd833ce + bb6316a commit a68561d

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
@@ -99,6 +99,7 @@ struct rebase_options {
9999
int allow_empty_message;
100100
int rebase_merges, rebase_cousins;
101101
char *strategy, *strategy_opts;
102+
struct strbuf git_format_patch_opt;
102103
};
103104

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

384394
switch (opts->type) {
385395
case REBASE_AM:
@@ -432,7 +442,8 @@ static int run_specific_rebase(struct rebase_options *opts)
432442
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
433443

434444
static int reset_head(struct object_id *oid, const char *action,
435-
const char *switch_to_branch, int detach_head)
445+
const char *switch_to_branch, int detach_head,
446+
const char *reflog_orig_head, const char *reflog_head)
436447
{
437448
struct object_id head_oid;
438449
struct tree_desc desc;
@@ -507,20 +518,26 @@ static int reset_head(struct object_id *oid, const char *action,
507518
old_orig = &oid_old_orig;
508519
if (!get_oid("HEAD", &oid_orig)) {
509520
orig = &oid_orig;
510-
strbuf_addstr(&msg, "updating ORIG_HEAD");
511-
update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
521+
if (!reflog_orig_head) {
522+
strbuf_addstr(&msg, "updating ORIG_HEAD");
523+
reflog_orig_head = msg.buf;
524+
}
525+
update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
512526
UPDATE_REFS_MSG_ON_ERR);
513527
} else if (old_orig)
514528
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
515-
strbuf_setlen(&msg, prefix_len);
516-
strbuf_addstr(&msg, "updating HEAD");
529+
if (!reflog_head) {
530+
strbuf_setlen(&msg, prefix_len);
531+
strbuf_addstr(&msg, "updating HEAD");
532+
reflog_head = msg.buf;
533+
}
517534
if (!switch_to_branch)
518-
ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
535+
ret = update_ref(reflog_head, "HEAD", oid, orig, REF_NO_DEREF,
519536
UPDATE_REFS_MSG_ON_ERR);
520537
else {
521538
ret = create_symref("HEAD", switch_to_branch, msg.buf);
522539
if (!ret)
523-
ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
540+
ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
524541
UPDATE_REFS_MSG_ON_ERR);
525542
}
526543

@@ -623,6 +640,36 @@ static int parse_opt_interactive(const struct option *opt, const char *arg,
623640
return 0;
624641
}
625642

643+
static void NORETURN error_on_missing_default_upstream(void)
644+
{
645+
struct branch *current_branch = branch_get(NULL);
646+
647+
printf(_("%s\n"
648+
"Please specify which branch you want to rebase against.\n"
649+
"See git-rebase(1) for details.\n"
650+
"\n"
651+
" git rebase '<branch>'\n"
652+
"\n"),
653+
current_branch ? _("There is no tracking information for "
654+
"the current branch.") :
655+
_("You are not currently on a branch."));
656+
657+
if (current_branch) {
658+
const char *remote = current_branch->remote_name;
659+
660+
if (!remote)
661+
remote = _("<remote>");
662+
663+
printf(_("If you wish to set tracking information for this "
664+
"branch you can do so with:\n"
665+
"\n"
666+
" git branch --set-upstream-to=%s/<branch> %s\n"
667+
"\n"),
668+
remote, current_branch->name);
669+
}
670+
exit(1);
671+
}
672+
626673
int cmd_rebase(int argc, const char **argv, const char *prefix)
627674
{
628675
struct rebase_options options = {
@@ -631,6 +678,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
631678
.git_am_opt = STRBUF_INIT,
632679
.allow_rerere_autoupdate = -1,
633680
.allow_empty_message = 1,
681+
.git_format_patch_opt = STRBUF_INIT,
634682
};
635683
const char *branch_name;
636684
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)