Skip to content

Commit 7543f6f

Browse files
dschogitster
authored andcommitted
rebase -i: introduce --rebase-merges=[no-]rebase-cousins
When running `git rebase --rebase-merges` non-interactively with an ancestor of HEAD as <upstream> (or leaving the todo list unmodified), we would ideally recreate the exact same commits as before the rebase. However, if there are commits in the commit range <upstream>.. that do not have <upstream> as direct ancestor (i.e. if `git log <upstream>..` would show commits that are omitted by `git log --ancestry-path <upstream>..`), this is currently not the case: we would turn them into commits that have <upstream> as direct ancestor. Let's illustrate that with a diagram: C / \ A - B - E - F \ / D Currently, after running `git rebase -i --rebase-merges B`, the new branch structure would be (pay particular attention to the commit `D`): --- C' -- / \ A - B ------ E' - F' \ / D' This is not really preserving the branch topology from before! The reason is that the commit `D` does not have `B` as ancestor, and therefore it gets rebased onto `B`. This is unintuitive behavior. Even worse, when recreating branch structure, most use cases would appear to want cousins *not* to be rebased onto the new base commit. For example, Git for Windows (the heaviest user of the Git garden shears, which served as the blueprint for --rebase-merges) frequently merges branches from `next` early, and these branches certainly do *not* want to be rebased. In the example above, the desired outcome would look like this: --- C' -- / \ A - B ------ E' - F' \ / -- D' -- Let's introduce the term "cousins" for such commits ("D" in the example), and let's not rebase them by default. For hypothetical use cases where cousins *do* need to be rebased, `git rebase --rebase=merges=rebase-cousins` needs to be used. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1131ec9 commit 7543f6f

File tree

7 files changed

+59
-6
lines changed

7 files changed

+59
-6
lines changed

Documentation/git-rebase.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ rebase.instructionFormat. A customized instruction format will automatically
380380
have the long commit hash prepended to the format.
381381

382382
-r::
383-
--rebase-merges::
383+
--rebase-merges[=(rebase-cousins|no-rebase-cousins)]::
384384
By default, a rebase will simply drop merge commits from the todo
385385
list, and put the rebased commits into a single, linear branch.
386386
With `--rebase-merges`, the rebase will instead try to preserve
@@ -389,9 +389,16 @@ have the long commit hash prepended to the format.
389389
manual amendments in these merge commits will have to be
390390
resolved/re-applied manually.
391391
+
392-
This mode is similar in spirit to `--preserve-merges`, but in contrast to
393-
that option works well in interactive rebases: commits can be reordered,
394-
inserted and dropped at will.
392+
By default, or when `no-rebase-cousins` was specified, commits which do not
393+
have `<upstream>` as direct ancestor will keep their original branch point,
394+
i.e. commits that would be excluded by gitlink:git-log[1]'s
395+
`--ancestry-path` option will keep their original ancestry by default. If
396+
the `rebase-cousins` mode is turned on, such commits are instead rebased
397+
onto `<upstream>` (or `<onto>`, if specified).
398+
+
399+
The `--rebase-merges` mode is similar in spirit to `--preserve-merges`, but
400+
in contrast to that option works well in interactive rebases: commits can be
401+
reordered, inserted and dropped at will.
395402
+
396403
It is currently only possible to recreate the merge commits using the
397404
`recursive` merge strategy; Different merge strategies can be used only via

builtin/rebase--helper.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
1313
{
1414
struct replay_opts opts = REPLAY_OPTS_INIT;
1515
unsigned flags = 0, keep_empty = 0, rebase_merges = 0;
16-
int abbreviate_commands = 0;
16+
int abbreviate_commands = 0, rebase_cousins = -1;
1717
enum {
1818
CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
1919
CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH,
@@ -25,6 +25,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
2525
OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message,
2626
N_("allow commits with empty messages")),
2727
OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
28+
OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
29+
N_("keep original branch points of cousins")),
2830
OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
2931
CONTINUE),
3032
OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),
@@ -59,8 +61,13 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
5961
flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
6062
flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
6163
flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
64+
flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
6265
flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0;
6366

67+
if (rebase_cousins >= 0 && !rebase_merges)
68+
warning(_("--[no-]rebase-cousins has no effect without "
69+
"--rebase-merges"));
70+
6471
if (command == CONTINUE && argc == 1)
6572
return !!sequencer_continue(&opts);
6673
if (command == ABORT && argc == 1)

git-rebase--interactive.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ git_rebase__interactive () {
971971

972972
git rebase--helper --make-script ${keep_empty:+--keep-empty} \
973973
${rebase_merges:+--rebase-merges} \
974+
${rebase_cousins:+--rebase-cousins} \
974975
$revisions ${restrict_revision+^$restrict_revision} >"$todo" ||
975976
die "$(gettext "Could not generate todo list")"
976977

git-rebase.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ q,quiet! be quiet. implies --no-stat
1717
autostash automatically stash/stash pop before and after
1818
fork-point use 'merge-base --fork-point' to refine upstream
1919
onto=! rebase onto given branch instead of upstream
20-
r,rebase-merges! try to rebase merges instead of skipping them
20+
r,rebase-merges? try to rebase merges instead of skipping them
2121
p,preserve-merges! try to recreate merges instead of ignoring them
2222
s,strategy=! use the given merge strategy
2323
no-ff! cherry-pick all commits, even if unchanged
@@ -91,6 +91,7 @@ state_dir=
9191
# One of {'', continue, skip, abort}, as parsed from command line
9292
action=
9393
rebase_merges=
94+
rebase_cousins=
9495
preserve_merges=
9596
autosquash=
9697
keep_empty=
@@ -286,6 +287,15 @@ do
286287
rebase_merges=t
287288
test -z "$interactive_rebase" && interactive_rebase=implied
288289
;;
290+
--rebase-merges=*)
291+
rebase_merges=t
292+
case "${1#*=}" in
293+
rebase-cousins) rebase_cousins=t;;
294+
no-rebase-cousins) rebase_cousins=;;
295+
*) die "Unknown mode: $1";;
296+
esac
297+
test -z "$interactive_rebase" && interactive_rebase=implied
298+
;;
289299
--preserve-merges)
290300
preserve_merges=t
291301
test -z "$interactive_rebase" && interactive_rebase=implied

sequencer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3578,6 +3578,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
35783578
unsigned flags)
35793579
{
35803580
int keep_empty = flags & TODO_LIST_KEEP_EMPTY;
3581+
int rebase_cousins = flags & TODO_LIST_REBASE_COUSINS;
35813582
struct strbuf buf = STRBUF_INIT, oneline = STRBUF_INIT;
35823583
struct strbuf label = STRBUF_INIT;
35833584
struct commit_list *commits = NULL, **tail = &commits, *iter;
@@ -3755,6 +3756,9 @@ static int make_script_with_merges(struct pretty_print_context *pp,
37553756
&commit->object.oid);
37563757
if (entry)
37573758
to = entry->string;
3759+
else if (!rebase_cousins)
3760+
to = label_oid(&commit->object.oid, NULL,
3761+
&state);
37583762

37593763
if (!to || !strcmp(to, "onto"))
37603764
fprintf(out, "%s onto\n", cmd_reset);

sequencer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ int sequencer_remove_state(struct replay_opts *opts);
6060
#define TODO_LIST_SHORTEN_IDS (1U << 1)
6161
#define TODO_LIST_ABBREVIATE_CMDS (1U << 2)
6262
#define TODO_LIST_REBASE_MERGES (1U << 3)
63+
/*
64+
* When rebasing merges, commits that do have the base commit as ancestor
65+
* ("cousins") are *not* rebased onto the new base by default. If those
66+
* commits should be rebased onto the new base, this flag needs to be passed.
67+
*/
68+
#define TODO_LIST_REBASE_COUSINS (1U << 4)
6369
int sequencer_make_script(FILE *out, int argc, const char **argv,
6470
unsigned flags);
6571

t/t3430-rebase-merges.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,24 @@ test_expect_success 'with a branch tip that was cherry-picked already' '
176176
EOF
177177
'
178178

179+
test_expect_success 'do not rebase cousins unless asked for' '
180+
git checkout -b cousins master &&
181+
before="$(git rev-parse --verify HEAD)" &&
182+
test_tick &&
183+
git rebase -r HEAD^ &&
184+
test_cmp_rev HEAD $before &&
185+
test_tick &&
186+
git rebase --rebase-merges=rebase-cousins HEAD^ &&
187+
test_cmp_graph HEAD^.. <<-\EOF
188+
* Merge the topic branch '\''onebranch'\''
189+
|\
190+
| * D
191+
| * G
192+
|/
193+
o H
194+
EOF
195+
'
196+
179197
test_expect_success 'refs/rewritten/* is worktree-local' '
180198
git worktree add wt &&
181199
cat >wt/script-from-scratch <<-\EOF &&

0 commit comments

Comments
 (0)