Skip to content

Commit 8f6aed7

Browse files
dschogitster
authored andcommitted
rebase: introduce the --rebase-merges option
Once upon a time, this here developer thought: wouldn't it be nice if, say, Git for Windows' patches on top of core Git could be represented as a thicket of branches, and be rebased on top of core Git in order to maintain a cherry-pick'able set of patch series? The original attempt to answer this was: git rebase --preserve-merges. However, that experiment was never intended as an interactive option, and it only piggy-backed on git rebase --interactive because that command's implementation looked already very, very familiar: it was designed by the same person who designed --preserve-merges: yours truly. Some time later, some other developer (I am looking at you, Andreas! ;-)) decided that it would be a good idea to allow --preserve-merges to be combined with --interactive (with caveats!) and the Git maintainer (well, the interim Git maintainer during Junio's absence, that is) agreed, and that is when the glamor of the --preserve-merges design started to fall apart rather quickly and unglamorously. The reason? In --preserve-merges mode, the parents of a merge commit (or for that matter, of *any* commit) were not stated explicitly, but were *implied* by the commit name passed to the `pick` command. This made it impossible, for example, to reorder commits. Not to mention to move commits between branches or, deity forbid, to split topic branches into two. Alas, these shortcomings also prevented that mode (whose original purpose was to serve Git for Windows' needs, with the additional hope that it may be useful to others, too) from serving Git for Windows' needs. Five years later, when it became really untenable to have one unwieldy, big hodge-podge patch series of partly related, partly unrelated patches in Git for Windows that was rebased onto core Git's tags from time to time (earning the undeserved wrath of the developer of the ill-fated git-remote-hg series that first obsoleted Git for Windows' competing approach, only to be abandoned without maintainer later) was really untenable, the "Git garden shears" were born [*1*/*2*]: a script, piggy-backing on top of the interactive rebase, that would first determine the branch topology of the patches to be rebased, create a pseudo todo list for further editing, transform the result into a real todo list (making heavy use of the `exec` command to "implement" the missing todo list commands) and finally recreate the patch series on top of the new base commit. That was in 2013. And it took about three weeks to come up with the design and implement it as an out-of-tree script. Needless to say, the implementation needed quite a few years to stabilize, all the while the design itself proved itself sound. With this patch, the goodness of the Git garden shears comes to `git rebase -i` itself. Passing the `--rebase-merges` option will generate a todo list that can be understood readily, and where it is obvious how to reorder commits. New branches can be introduced by inserting `label` commands and calling `merge <label>`. And once this mode will have become stable and universally accepted, we can deprecate the design mistake that was `--preserve-merges`. Link *1*: https://github.com/msysgit/msysgit/blob/master/share/msysGit/shears.sh Link *2*: https://github.com/git-for-windows/build-extra/blob/master/shears.sh Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1644c73 commit 8f6aed7

File tree

5 files changed

+207
-2
lines changed

5 files changed

+207
-2
lines changed

Documentation/git-rebase.txt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,24 @@ The commit list format can be changed by setting the configuration option
379379
rebase.instructionFormat. A customized instruction format will automatically
380380
have the long commit hash prepended to the format.
381381

382+
-r::
383+
--rebase-merges::
384+
By default, a rebase will simply drop merge commits from the todo
385+
list, and put the rebased commits into a single, linear branch.
386+
With `--rebase-merges`, the rebase will instead try to preserve
387+
the branching structure within the commits that are to be rebased,
388+
by recreating the merge commits. Any resolved merge conflicts or
389+
manual amendments in these merge commits will have to be
390+
resolved/re-applied manually.
391+
+
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.
395+
+
396+
It is currently only possible to recreate the merge commits using the
397+
`recursive` merge strategy; Different merge strategies can be used only via
398+
explicit `exec git merge -s <strategy> [...]` commands.
399+
382400
-p::
383401
--preserve-merges::
384402
Recreate merge commits instead of flattening the history by replaying
@@ -781,7 +799,8 @@ BUGS
781799
The todo list presented by `--preserve-merges --interactive` does not
782800
represent the topology of the revision graph. Editing commits and
783801
rewording their commit messages should work fine, but attempts to
784-
reorder commits tend to produce counterintuitive results.
802+
reorder commits tend to produce counterintuitive results. Use
803+
`--rebase-merges` in such scenarios instead.
785804

786805
For example, an attempt to rearrange
787806
------------

contrib/completion/git-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@ _git_rebase ()
19441944
--*)
19451945
__gitcomp "
19461946
--onto --merge --strategy --interactive
1947-
--preserve-merges --stat --no-stat
1947+
--rebase-merges --preserve-merges --stat --no-stat
19481948
--committer-date-is-author-date --ignore-date
19491949
--ignore-whitespace --whitespace=
19501950
--autosquash --no-autosquash

git-rebase--interactive.sh

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

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

git-rebase.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +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
2021
p,preserve-merges! try to recreate merges instead of ignoring them
2122
s,strategy=! use the given merge strategy
2223
no-ff! cherry-pick all commits, even if unchanged
@@ -89,6 +90,7 @@ type=
8990
state_dir=
9091
# One of {'', continue, skip, abort}, as parsed from command line
9192
action=
93+
rebase_merges=
9294
preserve_merges=
9395
autosquash=
9496
keep_empty=
@@ -280,6 +282,10 @@ do
280282
--no-keep-empty)
281283
keep_empty=
282284
;;
285+
--rebase-merges)
286+
rebase_merges=t
287+
test -z "$interactive_rebase" && interactive_rebase=implied
288+
;;
283289
--preserve-merges)
284290
preserve_merges=t
285291
test -z "$interactive_rebase" && interactive_rebase=implied

t/t3430-rebase-merges.sh

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2018 Johannes E. Schindelin
4+
#
5+
6+
test_description='git rebase -i --rebase-merges
7+
8+
This test runs git rebase "interactively", retaining the branch structure by
9+
recreating merge commits.
10+
11+
Initial setup:
12+
13+
-- B -- (first)
14+
/ \
15+
A - C - D - E - H (master)
16+
\ /
17+
F - G (second)
18+
'
19+
. ./test-lib.sh
20+
. "$TEST_DIRECTORY"/lib-rebase.sh
21+
22+
test_cmp_graph () {
23+
cat >expect &&
24+
git log --graph --boundary --format=%s "$@" >output &&
25+
sed "s/ *$//" <output >output.trimmed &&
26+
test_cmp expect output.trimmed
27+
}
28+
29+
test_expect_success 'setup' '
30+
write_script replace-editor.sh <<-\EOF &&
31+
mv "$1" "$(git rev-parse --git-path ORIGINAL-TODO)"
32+
cp script-from-scratch "$1"
33+
EOF
34+
35+
test_commit A &&
36+
git checkout -b first &&
37+
test_commit B &&
38+
git checkout master &&
39+
test_commit C &&
40+
test_commit D &&
41+
git merge --no-commit B &&
42+
test_tick &&
43+
git commit -m E &&
44+
git tag -m E E &&
45+
git checkout -b second C &&
46+
test_commit F &&
47+
test_commit G &&
48+
git checkout master &&
49+
git merge --no-commit G &&
50+
test_tick &&
51+
git commit -m H &&
52+
git tag -m H H
53+
'
54+
55+
test_expect_success 'create completely different structure' '
56+
cat >script-from-scratch <<-\EOF &&
57+
label onto
58+
59+
# onebranch
60+
pick G
61+
pick D
62+
label onebranch
63+
64+
# second
65+
reset onto
66+
pick B
67+
label second
68+
69+
reset onto
70+
merge -C H second
71+
merge onebranch # Merge the topic branch '\''onebranch'\''
72+
EOF
73+
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
74+
test_tick &&
75+
git rebase -i -r A &&
76+
test_cmp_graph <<-\EOF
77+
* Merge the topic branch '\''onebranch'\''
78+
|\
79+
| * D
80+
| * G
81+
* | H
82+
|\ \
83+
| |/
84+
|/|
85+
| * B
86+
|/
87+
* A
88+
EOF
89+
'
90+
91+
test_expect_success 'generate correct todo list' '
92+
cat >expect <<-\EOF &&
93+
label onto
94+
95+
reset onto
96+
pick d9df450 B
97+
label E
98+
99+
reset onto
100+
pick 5dee784 C
101+
label branch-point
102+
pick ca2c861 F
103+
pick 088b00a G
104+
label H
105+
106+
reset branch-point # C
107+
pick 12bd07b D
108+
merge -C 2051b56 E # E
109+
merge -C 233d48a H # H
110+
111+
EOF
112+
113+
grep -v "^#" <.git/ORIGINAL-TODO >output &&
114+
test_cmp expect output
115+
'
116+
117+
test_expect_success '`reset` refuses to overwrite untracked files' '
118+
git checkout -b refuse-to-reset &&
119+
test_commit dont-overwrite-untracked &&
120+
git checkout @{-1} &&
121+
: >dont-overwrite-untracked.t &&
122+
echo "reset refs/tags/dont-overwrite-untracked" >script-from-scratch &&
123+
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
124+
test_must_fail git rebase -r HEAD &&
125+
git rebase --abort
126+
'
127+
128+
test_expect_success 'failed `merge` writes patch (may be rescheduled, too)' '
129+
test_when_finished "test_might_fail git rebase --abort" &&
130+
git checkout -b conflicting-merge A &&
131+
132+
: fail because of conflicting untracked file &&
133+
>G.t &&
134+
echo "merge -C H G" >script-from-scratch &&
135+
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
136+
test_tick &&
137+
test_must_fail git rebase -ir HEAD &&
138+
grep "^merge -C .* G$" .git/rebase-merge/done &&
139+
grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
140+
test_path_is_file .git/rebase-merge/patch &&
141+
142+
: fail because of merge conflict &&
143+
rm G.t .git/rebase-merge/patch &&
144+
git reset --hard &&
145+
test_commit conflicting-G G.t not-G conflicting-G &&
146+
test_must_fail git rebase --continue &&
147+
! grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
148+
test_path_is_file .git/rebase-merge/patch
149+
'
150+
151+
test_expect_success 'with a branch tip that was cherry-picked already' '
152+
git checkout -b already-upstream master &&
153+
base="$(git rev-parse --verify HEAD)" &&
154+
155+
test_commit A1 &&
156+
test_commit A2 &&
157+
git reset --hard $base &&
158+
test_commit B1 &&
159+
test_tick &&
160+
git merge -m "Merge branch A" A2 &&
161+
162+
git checkout -b upstream-with-a2 $base &&
163+
test_tick &&
164+
git cherry-pick A2 &&
165+
166+
git checkout already-upstream &&
167+
test_tick &&
168+
git rebase -i -r upstream-with-a2 &&
169+
test_cmp_graph upstream-with-a2.. <<-\EOF
170+
* Merge branch A
171+
|\
172+
| * A1
173+
* | B1
174+
|/
175+
o A2
176+
EOF
177+
'
178+
179+
test_done

0 commit comments

Comments
 (0)