Skip to content

Commit e7e0098

Browse files
committed
Merge branch 'js/rebase-merge-octopus' into pu
* js/rebase-merge-octopus: rebase --rebase-merges: adjust man page for octopus support rebase --rebase-merges: add support for octopus merges merge: allow reading the merge commit message from a file
2 parents 547adad + caafecf commit e7e0098

File tree

5 files changed

+204
-47
lines changed

5 files changed

+204
-47
lines changed

Documentation/git-merge.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SYNOPSIS
1212
'git merge' [-n] [--stat] [--no-commit] [--squash] [--[no-]edit]
1313
[-s <strategy>] [-X <strategy-option>] [-S[<keyid>]]
1414
[--[no-]allow-unrelated-histories]
15-
[--[no-]rerere-autoupdate] [-m <msg>] [<commit>...]
15+
[--[no-]rerere-autoupdate] [-m <msg>] [-F <file>] [<commit>...]
1616
'git merge' --abort
1717
'git merge' --continue
1818

@@ -75,6 +75,14 @@ The 'git fmt-merge-msg' command can be
7575
used to give a good default for automated 'git merge'
7676
invocations. The automated message can include the branch description.
7777

78+
-F <file>::
79+
--file=<file>::
80+
Read the commit message to be used for the merge commit (in
81+
case one is created).
82+
+
83+
If `--log` is specified, a shortlog of the commits being merged
84+
will be appended to the specified message.
85+
7886
--[no-]rerere-autoupdate::
7987
Allow the rerere mechanism to update the index with the
8088
result of auto-conflict resolution if possible.

Documentation/git-rebase.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -960,8 +960,8 @@ rescheduled immediately, with a helpful message how to edit the todo list
960960
(this typically happens when a `reset` command was inserted into the todo
961961
list manually and contains a typo).
962962

963-
The `merge` command will merge the specified revision into whatever is
964-
HEAD at that time. With `-C <original-commit>`, the commit message of
963+
The `merge` command will merge the specified revision(s) into whatever
964+
is HEAD at that time. With `-C <original-commit>`, the commit message of
965965
the specified merge commit will be used. When the `-C` is changed to
966966
a lower-case `-c`, the message will be opened in an editor after a
967967
successful merge so that the user can edit the message.
@@ -970,7 +970,8 @@ If a `merge` command fails for any reason other than merge conflicts (i.e.
970970
when the merge operation did not even start), it is rescheduled immediately.
971971

972972
At this time, the `merge` command will *always* use the `recursive`
973-
merge strategy, with no way to choose a different one. To work around
973+
merge strategy for regular merges, and `octopus` for octopus merges,
974+
strategy, with no way to choose a different one. To work around
974975
this, an `exec` command can be used to call `git merge` explicitly,
975976
using the fact that the labels are worktree-local refs (the ref
976977
`refs/rewritten/onto` would correspond to the label `onto`, for example).

builtin/merge.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,35 @@ static int option_parse_message(const struct option *opt,
111111
return 0;
112112
}
113113

114+
static int option_read_message(struct parse_opt_ctx_t *ctx,
115+
const struct option *opt, int unset)
116+
{
117+
struct strbuf *buf = opt->value;
118+
const char *arg;
119+
120+
if (unset)
121+
BUG("-F cannot be negated");
122+
123+
if (ctx->opt) {
124+
arg = ctx->opt;
125+
ctx->opt = NULL;
126+
} else if (ctx->argc > 1) {
127+
ctx->argc--;
128+
arg = *++ctx->argv;
129+
} else
130+
return opterror(opt, "requires a value", 0);
131+
132+
if (buf->len)
133+
strbuf_addch(buf, '\n');
134+
if (ctx->prefix && !is_absolute_path(arg))
135+
arg = prefix_filename(ctx->prefix, arg);
136+
if (strbuf_read_file(buf, arg, 0) < 0)
137+
return error(_("could not read file '%s'"), arg);
138+
have_message = 1;
139+
140+
return 0;
141+
}
142+
114143
static struct strategy *get_strategy(const char *name)
115144
{
116145
int i;
@@ -228,6 +257,9 @@ static struct option builtin_merge_options[] = {
228257
OPT_CALLBACK('m', "message", &merge_msg, N_("message"),
229258
N_("merge commit message (for a non-fast-forward merge)"),
230259
option_parse_message),
260+
{ OPTION_LOWLEVEL_CALLBACK, 'F', "file", &merge_msg, N_("path"),
261+
N_("read message from file"), PARSE_OPT_NONEG,
262+
(parse_opt_cb *) option_read_message },
231263
OPT__VERBOSITY(&verbosity),
232264
OPT_BOOL(0, "abort", &abort_current_merge,
233265
N_("abort the current in-progress merge")),

sequencer.c

Lines changed: 125 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2806,6 +2806,26 @@ static int do_reset(const char *name, int len, struct replay_opts *opts)
28062806
len, name));
28072807
}
28082808

2809+
static struct commit *lookup_label(const char *label, int len,
2810+
struct strbuf *buf)
2811+
{
2812+
struct commit *commit;
2813+
2814+
strbuf_reset(buf);
2815+
strbuf_addf(buf, "refs/rewritten/%.*s", len, label);
2816+
commit = lookup_commit_reference_by_name(buf->buf);
2817+
if (!commit) {
2818+
/* fall back to non-rewritten ref or commit */
2819+
strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0);
2820+
commit = lookup_commit_reference_by_name(buf->buf);
2821+
}
2822+
2823+
if (!commit)
2824+
error(_("could not resolve '%s'"), buf->buf);
2825+
2826+
return commit;
2827+
}
2828+
28092829
static int do_merge(struct commit *commit, const char *arg, int arg_len,
28102830
int flags, struct replay_opts *opts)
28112831
{
@@ -2814,8 +2834,9 @@ static int do_merge(struct commit *commit, const char *arg, int arg_len,
28142834
struct strbuf ref_name = STRBUF_INIT;
28152835
struct commit *head_commit, *merge_commit, *i;
28162836
struct commit_list *bases, *j, *reversed = NULL;
2837+
struct commit_list *to_merge = NULL, **tail = &to_merge;
28172838
struct merge_options o;
2818-
int merge_arg_len, oneline_offset, can_fast_forward, ret;
2839+
int merge_arg_len, oneline_offset, can_fast_forward, ret, k;
28192840
static struct lock_file lock;
28202841
const char *p;
28212842

@@ -2830,26 +2851,34 @@ static int do_merge(struct commit *commit, const char *arg, int arg_len,
28302851
goto leave_merge;
28312852
}
28322853

2833-
oneline_offset = arg_len;
2834-
merge_arg_len = strcspn(arg, " \t\n");
2835-
p = arg + merge_arg_len;
2836-
p += strspn(p, " \t\n");
2837-
if (*p == '#' && (!p[1] || isspace(p[1]))) {
2838-
p += 1 + strspn(p + 1, " \t\n");
2839-
oneline_offset = p - arg;
2840-
} else if (p - arg < arg_len)
2841-
BUG("octopus merges are not supported yet: '%s'", p);
2842-
2843-
strbuf_addf(&ref_name, "refs/rewritten/%.*s", merge_arg_len, arg);
2844-
merge_commit = lookup_commit_reference_by_name(ref_name.buf);
2845-
if (!merge_commit) {
2846-
/* fall back to non-rewritten ref or commit */
2847-
strbuf_splice(&ref_name, 0, strlen("refs/rewritten/"), "", 0);
2848-
merge_commit = lookup_commit_reference_by_name(ref_name.buf);
2854+
/*
2855+
* For octopus merges, the arg starts with the list of revisions to be
2856+
* merged. The list is optionally followed by '#' and the oneline.
2857+
*/
2858+
merge_arg_len = oneline_offset = arg_len;
2859+
for (p = arg; p - arg < arg_len; p += strspn(p, " \t\n")) {
2860+
if (!*p)
2861+
break;
2862+
if (*p == '#' && (!p[1] || isspace(p[1]))) {
2863+
p += 1 + strspn(p + 1, " \t\n");
2864+
oneline_offset = p - arg;
2865+
break;
2866+
}
2867+
k = strcspn(p, " \t\n");
2868+
if (!k)
2869+
continue;
2870+
merge_commit = lookup_label(p, k, &ref_name);
2871+
if (!merge_commit) {
2872+
ret = error(_("unable to parse '%.*s'"), k, p);
2873+
goto leave_merge;
2874+
}
2875+
tail = &commit_list_insert(merge_commit, tail)->next;
2876+
p += k;
2877+
merge_arg_len = p - arg;
28492878
}
28502879

2851-
if (!merge_commit) {
2852-
ret = error(_("could not resolve '%s'"), ref_name.buf);
2880+
if (!to_merge) {
2881+
ret = error(_("nothing to merge: '%.*s'"), arg_len, arg);
28532882
goto leave_merge;
28542883
}
28552884

@@ -2860,8 +2889,13 @@ static int do_merge(struct commit *commit, const char *arg, int arg_len,
28602889
* "[new root]", let's simply fast-forward to the merge head.
28612890
*/
28622891
rollback_lock_file(&lock);
2863-
ret = fast_forward_to(&merge_commit->object.oid,
2864-
&head_commit->object.oid, 0, opts);
2892+
if (to_merge->next)
2893+
ret = error(_("octopus merge cannot be executed on "
2894+
"top of a [new root]"));
2895+
else
2896+
ret = fast_forward_to(&to_merge->item->object.oid,
2897+
&head_commit->object.oid, 0,
2898+
opts);
28652899
goto leave_merge;
28662900
}
28672901

@@ -2897,7 +2931,8 @@ static int do_merge(struct commit *commit, const char *arg, int arg_len,
28972931
p = arg + oneline_offset;
28982932
len = arg_len - oneline_offset;
28992933
} else {
2900-
strbuf_addf(&buf, "Merge branch '%.*s'",
2934+
strbuf_addf(&buf, "Merge %s '%.*s'",
2935+
to_merge->next ? "branches" : "branch",
29012936
merge_arg_len, arg);
29022937
p = buf.buf;
29032938
len = buf.len;
@@ -2921,28 +2956,76 @@ static int do_merge(struct commit *commit, const char *arg, int arg_len,
29212956
&head_commit->object.oid);
29222957

29232958
/*
2924-
* If the merge head is different from the original one, we cannot
2959+
* If any merge head is different from the original one, we cannot
29252960
* fast-forward.
29262961
*/
29272962
if (can_fast_forward) {
2928-
struct commit_list *second_parent = commit->parents->next;
2963+
struct commit_list *p = commit->parents->next;
29292964

2930-
if (second_parent && !second_parent->next &&
2931-
oidcmp(&merge_commit->object.oid,
2932-
&second_parent->item->object.oid))
2965+
for (j = to_merge; j && p; j = j->next, p = p->next)
2966+
if (oidcmp(&j->item->object.oid,
2967+
&p->item->object.oid)) {
2968+
can_fast_forward = 0;
2969+
break;
2970+
}
2971+
/*
2972+
* If the number of merge heads differs from the original merge
2973+
* commit, we cannot fast-forward.
2974+
*/
2975+
if (j || p)
29332976
can_fast_forward = 0;
29342977
}
29352978

2936-
if (can_fast_forward && commit->parents->next &&
2937-
!commit->parents->next->next &&
2938-
!oidcmp(&commit->parents->next->item->object.oid,
2939-
&merge_commit->object.oid)) {
2979+
if (can_fast_forward) {
29402980
rollback_lock_file(&lock);
29412981
ret = fast_forward_to(&commit->object.oid,
29422982
&head_commit->object.oid, 0, opts);
29432983
goto leave_merge;
29442984
}
29452985

2986+
if (to_merge->next) {
2987+
/* Octopus merge */
2988+
struct child_process cmd = CHILD_PROCESS_INIT;
2989+
2990+
if (read_env_script(&cmd.env_array)) {
2991+
const char *gpg_opt = gpg_sign_opt_quoted(opts);
2992+
2993+
ret = error(_(staged_changes_advice), gpg_opt, gpg_opt);
2994+
goto leave_merge;
2995+
}
2996+
2997+
cmd.git_cmd = 1;
2998+
argv_array_push(&cmd.args, "merge");
2999+
argv_array_push(&cmd.args, "-s");
3000+
argv_array_push(&cmd.args, "octopus");
3001+
argv_array_push(&cmd.args, "--no-edit");
3002+
argv_array_push(&cmd.args, "--no-ff");
3003+
argv_array_push(&cmd.args, "--no-log");
3004+
argv_array_push(&cmd.args, "--no-stat");
3005+
argv_array_push(&cmd.args, "-F");
3006+
argv_array_push(&cmd.args, git_path_merge_msg(the_repository));
3007+
if (opts->gpg_sign)
3008+
argv_array_push(&cmd.args, opts->gpg_sign);
3009+
3010+
/* Add the tips to be merged */
3011+
for (j = to_merge; j; j = j->next)
3012+
argv_array_push(&cmd.args,
3013+
oid_to_hex(&j->item->object.oid));
3014+
3015+
strbuf_release(&ref_name);
3016+
unlink(git_path_cherry_pick_head(the_repository));
3017+
rollback_lock_file(&lock);
3018+
3019+
rollback_lock_file(&lock);
3020+
ret = run_command(&cmd);
3021+
3022+
/* force re-reading of the cache */
3023+
if (!ret && (discard_cache() < 0 || read_cache() < 0))
3024+
ret = error(_("could not read index"));
3025+
goto leave_merge;
3026+
}
3027+
3028+
merge_commit = to_merge->item;
29463029
write_message(oid_to_hex(&merge_commit->object.oid), GIT_SHA1_HEXSZ,
29473030
git_path_merge_head(the_repository), 0);
29483031
write_message("no-ff", 5, git_path_merge_mode(the_repository), 0);
@@ -3005,6 +3088,7 @@ static int do_merge(struct commit *commit, const char *arg, int arg_len,
30053088
leave_merge:
30063089
strbuf_release(&ref_name);
30073090
rollback_lock_file(&lock);
3091+
free_commit_list(to_merge);
30083092
return ret;
30093093
}
30103094

@@ -3910,7 +3994,6 @@ static int make_script_with_merges(struct pretty_print_context *pp,
39103994
*/
39113995
while ((commit = get_revision(revs))) {
39123996
struct commit_list *to_merge;
3913-
int is_octopus;
39143997
const char *p1, *p2;
39153998
struct object_id *oid;
39163999
int is_empty;
@@ -3942,11 +4025,6 @@ static int make_script_with_merges(struct pretty_print_context *pp,
39424025
continue;
39434026
}
39444027

3945-
is_octopus = to_merge && to_merge->next;
3946-
3947-
if (is_octopus)
3948-
BUG("Octopus merges not yet supported");
3949-
39504028
/* Create a label */
39514029
strbuf_reset(&label);
39524030
if (skip_prefix(oneline.buf, "Merge ", &p1) &&
@@ -3968,13 +4046,17 @@ static int make_script_with_merges(struct pretty_print_context *pp,
39684046
strbuf_addf(&buf, "%s -C %s",
39694047
cmd_merge, oid_to_hex(&commit->object.oid));
39704048

3971-
/* label the tip of merged branch */
3972-
oid = &to_merge->item->object.oid;
3973-
strbuf_addch(&buf, ' ');
4049+
/* label the tips of merged branches */
4050+
for (; to_merge; to_merge = to_merge->next) {
4051+
oid = &to_merge->item->object.oid;
4052+
strbuf_addch(&buf, ' ');
4053+
4054+
if (!oidset_contains(&interesting, oid)) {
4055+
strbuf_addstr(&buf, label_oid(oid, NULL,
4056+
&state));
4057+
continue;
4058+
}
39744059

3975-
if (!oidset_contains(&interesting, oid))
3976-
strbuf_addstr(&buf, label_oid(oid, NULL, &state));
3977-
else {
39784060
tips_tail = &commit_list_insert(to_merge->item,
39794061
tips_tail)->next;
39804062

t/t3430-rebase-merges.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,38 @@ test_expect_success 'labels that are object IDs are rewritten' '
329329
! grep "^label $third$" .git/ORIGINAL-TODO
330330
'
331331

332+
test_expect_success 'octopus merges' '
333+
git checkout -b three &&
334+
test_commit before-octopus &&
335+
test_commit three &&
336+
git checkout -b two HEAD^ &&
337+
test_commit two &&
338+
git checkout -b one HEAD^ &&
339+
test_commit one &&
340+
test_tick &&
341+
(GIT_AUTHOR_NAME="Hank" GIT_AUTHOR_EMAIL="[email protected]" \
342+
git merge -m "Tüntenfüsch" two three) &&
343+
344+
: fast forward if possible &&
345+
before="$(git rev-parse --verify HEAD)" &&
346+
test_tick &&
347+
git rebase -i -r HEAD^^ &&
348+
test_cmp_rev HEAD $before &&
349+
350+
test_tick &&
351+
git rebase -i --force -r HEAD^^ &&
352+
test "Hank" = "$(git show -s --format=%an HEAD)" &&
353+
test "$before" != $(git rev-parse HEAD) &&
354+
test_cmp_graph HEAD^^.. <<-\EOF
355+
*-. Tüntenfüsch
356+
|\ \
357+
| | * three
358+
| * | two
359+
| |/
360+
* | one
361+
|/
362+
o before-octopus
363+
EOF
364+
'
365+
332366
test_done

0 commit comments

Comments
 (0)