@matheusbsbs / matheus-bagliano / commits / f09c9b8c5f
Fork 0

Teach rebase -i about --preserve-merges

The option "-p" (or long "--preserve-merges") makes it possible to rebase side branches including merges, without straightening the history. Example: X \ A---M---B / ---o---O---P---Q When the current HEAD is "B", "git rebase -i -p --onto Q O" will yield X \ ---o---O---P---Q---A'---M'---B' Note that this will - _not_ touch X [*1*], it does - _not_ work without the --interactive flag [*2*], it does - _not_ guess the type of the merge, but blindly uses recursive or whatever strategy you provided with "-s <strategy>" for all merges it has to redo, and it does - _not_ make use of the original merge commit via git-rerere. *1*: only commits which reach a merge base between <upstream> and HEAD are reapplied. The others are kept as-are. *2*: git-rebase without --interactive is inherently patch based (at least at the moment), and therefore merges cannot be preserved. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 25, 2007 at 18:59 UTC f09c9b8c5ff9d8a15499b09ccd6c3e7b3c76af77
3 files changed +151 -4
Documentation/git-rebase.txt
+22 -1
@@ -9,7 +9,7 @@ SYNOPSIS
9 --------
10 [verse]
11 'git-rebase' [-i | --interactive] [-v | --verbose] [--merge] [-C<n>]
12 - [--onto <newbase>] <upstream> [<branch>]
12 + [-p | --preserve-merges] [--onto <newbase>] <upstream> [<branch>]
13 'git-rebase' --continue | --skip | --abort
14
15 DESCRIPTION
@@ -213,6 +213,10 @@ OPTIONS
213 Make a list of the commits which are about to be rebased. Let the
214 user edit that list before rebasing.
215
216 +-p, \--preserve-merges::
217 + Instead of ignoring merges, try to recreate them. This option
218 + only works in interactive mode.
219 +
220 include::merge-strategies.txt[]
221
222 NOTES
@@ -304,6 +308,23 @@ $ git rebase -i HEAD~5
308
309 And move the first patch to the end of the list.
310
311 +You might want to preserve merges, if you have a history like this:
312 +
313 +------------------
314 + X
315 + \
316 + A---M---B
317 + /
318 +---o---O---P---Q
319 +------------------
320 +
321 +Suppose you want to rebase the side branch starting at "A" to "Q". Make
322 +sure that the current HEAD is "B", and call
323 +
324 +-----------------------------
325 +$ git rebase -i -p --onto Q O
326 +-----------------------------
327 +
328 Authors
329 ------
330 Written by Junio C Hamano <junkio@cox.net> and
git-rebase--interactive.sh
+107 -3
@@ -10,7 +10,8 @@
10 # The original idea comes from Eric W. Biederman, in
11 # http://article.gmane.org/gmane.comp.version-control.git/22407
12
13 -USAGE='(--continue | --abort | --skip | [--onto <branch>] <upstream> [<branch>])'
13 +USAGE='(--continue | --abort | --skip | [--preserve-merges] [--verbose]
14 + [--onto <branch>] <upstream> [<branch>])'
15
16 . git-sh-setup
17 require_work_tree
@@ -18,6 +19,8 @@ require_work_tree
19 DOTEST="$GIT_DIR/.dotest-merge"
20 TODO="$DOTEST"/todo
21 DONE="$DOTEST"/done
22 +REWRITTEN="$DOTEST"/rewritten
23 +PRESERVE_MERGES=
24 STRATEGY=
25 VERBOSE=
26
@@ -68,6 +71,8 @@ die_abort () {
71 pick_one () {
72 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
73 git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
74 + test -d "$REWRITTEN" &&
75 + pick_one_preserving_merges "$@" && return
76 parent_sha1=$(git rev-parse --verify $sha1^ 2>/dev/null)
77 current_sha1=$(git rev-parse --verify HEAD)
78 if [ $current_sha1 = $parent_sha1 ]; then
@@ -79,6 +84,75 @@ pick_one () {
84 fi
85 }
86
87 +pick_one_preserving_merges () {
88 + case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
89 + sha1=$(git rev-parse $sha1)
90 +
91 + if [ -f "$DOTEST"/current-commit ]
92 + then
93 + current_commit=$(cat "$DOTEST"/current-commit) &&
94 + git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
95 + rm "$DOTEST"/current-commit ||
96 + die "Cannot write current commit's replacement sha1"
97 + fi
98 +
99 + # rewrite parents; if none were rewritten, we can fast-forward.
100 + fast_forward=t
101 + preserve=t
102 + new_parents=
103 + for p in $(git rev-list --parents -1 $sha1 | cut -d\ -f2-)
104 + do
105 + if [ -f "$REWRITTEN"/$p ]
106 + then
107 + preserve=f
108 + new_p=$(cat "$REWRITTEN"/$p)
109 + test $p != $new_p && fast_forward=f
110 + case "$new_parents" in
111 + *$new_p*)
112 + ;; # do nothing; that parent is already there
113 + *)
114 + new_parents="$new_parents $new_p"
115 + esac
116 + fi
117 + done
118 + case $fast_forward in
119 + t)
120 + echo "Fast forward to $sha1"
121 + test $preserve=f && echo $sha1 > "$REWRITTEN"/$sha1
122 + ;;
123 + f)
124 + test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
125 +
126 + first_parent=$(expr "$new_parents" : " \([^ ]*\)")
127 + # detach HEAD to current parent
128 + git checkout $first_parent 2> /dev/null ||
129 + die "Cannot move HEAD to $first_parent"
130 +
131 + echo $sha1 > "$DOTEST"/current-commit
132 + case "$new_parents" in
133 + \ *\ *)
134 + # redo merge
135 + author_script=$(get_author_ident_from_commit $sha1)
136 + eval "$author_script"
137 + msg="$(git cat-file commit $sha1 | \
138 + sed -e '1,/^$/d' -e "s/[\"\\]/\\\\&/g")"
139 + # NEEDSWORK: give rerere a chance
140 + if ! git merge $STRATEGY -m "$msg" $new_parents
141 + then
142 + echo "$msg" > "$GIT_DIR"/MERGE_MSG
143 + warn Error redoing merge $sha1
144 + warn
145 + warn After fixup, please use
146 + die "$author_script git commit"
147 + fi
148 + ;;
149 + *)
150 + git cherry-pick $STRATEGY "$@" ||
151 + die_with_patch $sha1 "Could not pick $sha1"
152 + esac
153 + esac
154 +}
155 +
156 do_next () {
157 read command sha1 rest < "$TODO"
158 case "$command" in
@@ -155,7 +229,15 @@ do_next () {
229 HEADNAME=$(cat "$DOTEST"/head-name) &&
230 OLDHEAD=$(cat "$DOTEST"/head) &&
231 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
158 - NEWHEAD=$(git rev-parse HEAD) &&
232 + if [ -d "$REWRITTEN" ]
233 + then
234 + test -f "$DOTEST"/current-commit &&
235 + current_commit=$(cat "$DOTEST"/current-commit) &&
236 + git rev-parse HEAD > "$REWRITTEN"/$current_commit
237 + NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
238 + else
239 + NEWHEAD=$(git rev-parse HEAD)
240 + fi &&
241 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
242 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
243 git symbolic-ref HEAD $HEADNAME &&
@@ -226,6 +308,9 @@ do
308 -v|--verbose)
309 VERBOSE=t
310 ;;
311 + -p|--preserve-merges)
312 + PRESERVE_MERGES=t
313 + ;;
314 -i|--interactive)
315 # yeah, we know
316 ;;
@@ -274,6 +359,25 @@ do
359 echo $UPSTREAM > "$DOTEST"/upstream
360 echo $ONTO > "$DOTEST"/onto
361 test t = "$VERBOSE" && : > "$DOTEST"/verbose
362 + if [ t = "$PRESERVE_MERGES" ]
363 + then
364 + # $REWRITTEN contains files for each commit that is
365 + # reachable by at least one merge base of $HEAD and
366 + # $UPSTREAM. They are not necessarily rewritten, but
367 + # their children might be.
368 + # This ensures that commits on merged, but otherwise
369 + # unrelated side branches are left alone. (Think "X"
370 + # in the man page's example.)
371 + mkdir "$REWRITTEN" &&
372 + for c in $(git merge-base --all $HEAD $UPSTREAM)
373 + do
374 + echo $ONTO > "$REWRITTEN"/$c ||
375 + die "Could not init rewritten commits"
376 + done
377 + MERGES_OPTION=
378 + else
379 + MERGES_OPTION=--no-merges
380 + fi
381
382 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
383 SHORTHEAD=$(git rev-parse --short $HEAD)
@@ -286,7 +390,7 @@ do
390 # edit = use commit, but stop for amending
391 # squash = use commit, but meld into previous commit
392 EOF
289 - git rev-list --no-merges --pretty=oneline --abbrev-commit \
393 + git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
394 --abbrev=7 --reverse $UPSTREAM..$HEAD | \
395 sed "s/^/pick /" >> "$TODO"
396
t/t3404-rebase-interactive.sh
+22
@@ -166,4 +166,26 @@ test_expect_success 'retain authorship when squashing' '
166 git show HEAD | grep "^Author: Nitfol"
167 '
168
169 +test_expect_success 'preserve merges with -p' '
170 + git checkout -b to-be-preserved master^ &&
171 + : > unrelated-file &&
172 + git add unrelated-file &&
173 + test_tick &&
174 + git commit -m "unrelated" &&
175 + git checkout -b to-be-rebased master &&
176 + echo B > file1 &&
177 + test_tick &&
178 + git commit -m J file1 &&
179 + test_tick &&
180 + git merge to-be-preserved &&
181 + echo C > file1 &&
182 + test_tick &&
183 + git commit -m K file1 &&
184 + git rebase -i -p --onto branch1 master &&
185 + test $(git rev-parse HEAD^^2) = $(git rev-parse to-be-preserved) &&
186 + test $(git rev-parse HEAD~3) = $(git rev-parse branch1) &&
187 + test $(git show HEAD:file1) = C &&
188 + test $(git show HEAD~2:file1) = B
189 +'
190 +
191 test_done