Skip to contentSkip to conversation

rebase: Allow merge strategies to be used when rebasing

PATCH In review Eric Wong 2 participants 11 messages Jun 15, 2016 Documentation

lore
Share & tools
lore

Eric Wong

author
#0 lore raw

PATCH 1/1 rebase: Allow merge strategies to be used when rebasing

This solves the problem of rebasing local commits against an upstream that has renamed files.

---
 Documentation/git-rebase.txt |   20 +++++
 git-rebase.sh                |  176 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 186 insertions(+), 10 deletions(-)
Documentation/git-rebase.txt +19 -1
index 08ee4aa..c339c45 100644
@@ -7,7 +7,7 @@ git-rebase - Rebase local commits to a n
 
 SYNOPSIS
 --------
-'git-rebase' [--onto <newbase>] <upstream> [<branch>]
+'git-rebase' [--merge] [--onto <newbase>] <upstream> [<branch>]
 
 'git-rebase' --continue | --skip | --abort
 
@@ -106,6 +106,24 @@ OPTIONS
 --abort::
 	Restore the original branch and abort the rebase operation.
 
+--skip::
+	Restart the rebasing process by skipping the current patch.
+	This does not work with the --merge option.
+
+--merge::
+	Use merging strategies to rebase.  When the recursive (default) merge
+	strategy is used, this allows rebase to be aware of renames on the
+	upstream side.
+
+-s <strategy>, \--strategy=<strategy>::
+	Use the given merge strategy; can be supplied more than
+	once to specify them in the order they should be tried.
+	If there is no `-s` option, a built-in list of strategies
+	is used instead (`git-merge-recursive` when merging a single
+	head, `git-merge-octopus` otherwise).  This implies --merge.
+
+include::merge-strategies.txt[]
+
 NOTES
 -----
 When you rebase a branch, you are changing its history in a way that
git-rebase.sh +167 -9

219 lines not on this page · raw message

Signature · 1 line
1.4.0.ge24c
#1 lore raw
Quoted 2 lines · Eric Wong

This solves the problem of rebasing local commits against an upstream that has renamed files.

I think leveraging the merge strategy to perform rebase is sound, but the selection of merge base for this purpose is quite different from the regular merge, and I think unfortunately this patch is probably wrong in letting git-merge choose the merge base.

But let's mention other things as well.

- You kept the original "format-patch piped to am" workflow optionally working.

- You check if merge or patch was used for failed rebase and follow the appropriate codepath while resuming, which is good.

- The list of commits you generate with tac seem to include merge commit -- you may want to give --no-merges to rev-list.

- I do not think we use "tac" elsewhere -- is it portable enough?

- Exiting with success unconditionally after "git am" feels wrong. I would do "exit $?" instead of "exit 0" there.

Suppose you have this commit ancestry graph:

----------------------------------------------------------------
Example: git-rebase --onto master A topic

A---B---C topic B'--C' topic
/ --> /
D---E---F---G master D---E---F---G master
----------------------------------------------------------------

This is slightly different from the one at the beginning of the script. The idea is A turned out to be not so cool, and we would want to drop it.

git-rebase.sh When you have resolved this problem run · line 85

Quoted 6 lines
git-rebase.sh +6 -0
+call_merge () {
+	cmt="$(cat $dotest/`printf %0${prec}d $1`)"
+	echo "$cmt" > "$dotest/current"
+	git-merge $strategy_args "rebase-merge: $cmt" HEAD "$cmt" \
+			|| die "$MRESOLVEMSG"
+}

call_merge is first called with B in cmt, and HEAD is pointing at G. But the merge in this function makes a merge between B and G, taking the effect of E->A.

I think the three-way merge you would want here is not between B and G using E as the pivot, but between B and G using A as the pivot. That's how cherry-pick and revert works. I would leverage the interface that is one level lower for this -- the strategy modules themselves.

git-merge-$strategy $cmt^ -- HEAD $cmt

The strategy modules take merge base(s), double-dash as the separator, our head and the other head. They do not make commit themselves (instead they leave working tree and index in committable state) and signal the results with their exit status:

0 -- success
1 -- conflicts
2 -- did not handle the merge at all

author
#2 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 2 lines · Eric Wong

This solves the problem of rebasing local commits against an upstream that has renamed files.

I think leveraging the merge strategy to perform rebase is sound, but the selection of merge base for this purpose is quite different from the regular merge, and I think unfortunately this patch is probably wrong in letting git-merge choose the merge base.

Quoted 4 lines

But let's mention other things as well.

- You kept the original "format-patch piped to am" workflow optionally working.

I left it as the default, too. I figured that it's best not to change the default (and most likely faster) behavior of something people rely on.

Quoted 7 lines

- You check if merge or patch was used for failed rebase and follow the appropriate codepath while resuming, which is good.

- The list of commits you generate with tac seem to include merge commit -- you may want to give --no-merges to rev-list.

Good point, I'll change it.

Quoted 2 lines

- I do not think we use "tac" elsewhere -- is it portable enough?

Nope. perl -e 'print reverse <>' is equivalent and we already use plenty of perl.

Quoted 2 lines

- Exiting with success unconditionally after "git am" feels wrong. I would do "exit $?" instead of "exit 0" there.

Oops, I'll change that, too.

Quoted 25 lines

Suppose you have this commit ancestry graph:

----------------------------------------------------------------
Example: git-rebase --onto master A topic

A---B---C topic B'--C' topic
/ --> /
D---E---F---G master D---E---F---G master
----------------------------------------------------------------

This is slightly different from the one at the beginning of the script. The idea is A turned out to be not so cool, and we would want to drop it.

git-rebase.sh When you have resolved this problem run · line 85

Quoted 6 lines
git-rebase.sh +6 -0
+call_merge () {
+	cmt="$(cat $dotest/`printf %0${prec}d $1`)"
+	echo "$cmt" > "$dotest/current"
+	git-merge $strategy_args "rebase-merge: $cmt" HEAD "$cmt" \
+			|| die "$MRESOLVEMSG"
+}

call_merge is first called with B in cmt, and HEAD is pointing at G. But the merge in this function makes a merge between B and G, taking the effect of E->A.

Quoted 7 lines

I think the three-way merge you would want here is not between B and G using E as the pivot, but between B and G using A as the pivot. That's how cherry-pick and revert works. I would leverage the interface that is one level lower for this -- the strategy modules themselves.

git-merge-$strategy $cmt^ -- HEAD $cmt

Changing the 'git-merge $strategy_args "rebase-merge: $cmt" HEAD "$cmt"' line in call_merge() to this seems to have broken more tests.

I'm not an expert at merging strategies by any measure, I've just trusted merge-recursive to Do The Right Thing(TM) more often than not, and use rerere to avoid repeating work.

I'm not entirely sure I want (or fully understand how) to support cherry-picking/revert with this, as I've already dropped --skip when --merge was in use.

I'll think about this some more when I'm less distracted.

Quoted 9 lines

The strategy modules take merge base(s), double-dash as the separator, our head and the other head. They do not make commit themselves (instead they leave working tree and index in committable state) and signal the results with their exit status:

0 -- success
1 -- conflicts
2 -- did not handle the merge at all

Cool, that's good.

Signature · 1 line
Eric Wong
#3 lore raw
Quoted 8 lines · Eric Wong
Quoted 2 lines · Junio C Hamano

- You kept the original "format-patch piped to am" workflow optionally working.

I left it as the default, too. I figured that it's best not to change the default (and most likely faster) behavior of something people rely on.

I should have said: "You kept ... working, which is good".

Quoted 10 lines
Quoted 7 lines

I think the three-way merge you would want here is not between B and G using E as the pivot, but between B and G using A as the pivot. That's how cherry-pick and revert works. I would leverage the interface that is one level lower for this -- the strategy modules themselves.

git-merge-$strategy $cmt^ -- HEAD $cmt

Changing the 'git-merge $strategy_args "rebase-merge: $cmt" HEAD "$cmt"' line in call_merge() to this seems to have broken more tests.

Oh, that is to be expected if you changed git-merge -s recursive with git-merge-recursive without other changes. The former makes a commit (which your original patch later used to create a separate commit chain and discarded); the latter does not make a commit but expects the caller to create a commit out of the resulting index file.

Quoted 3 lines

I'm not an expert at merging strategies by any measure, I've just trusted merge-recursive to Do The Right Thing(TM) more often than not, and use rerere to avoid repeating work.

I was originally hoping that rebasing would just be a matter of listing sequence of commits to be ported onto a new base and running "git-cherry-pick" on each of them in sequence. Now cherry-pick does not use merge machinery (hence does not use git-merge-recursive), but if we change that then updating rebase would be pretty much straightforward. It just needs a UI layer to guide the user through recovery process when the merge does not resolve cleanly in the middle, no?

author
#4 lore raw
Quoted 24 lines · Junio C Hamano
Quoted 8 lines · Eric Wong
Quoted 2 lines · Junio C Hamano

- You kept the original "format-patch piped to am" workflow optionally working.

I left it as the default, too. I figured that it's best not to change the default (and most likely faster) behavior of something people rely on.

I should have said: "You kept ... working, which is good".

Quoted 10 lines
Quoted 7 lines

I think the three-way merge you would want here is not between B and G using E as the pivot, but between B and G using A as the pivot. That's how cherry-pick and revert works. I would leverage the interface that is one level lower for this -- the strategy modules themselves.

git-merge-$strategy $cmt^ -- HEAD $cmt

Changing the 'git-merge $strategy_args "rebase-merge: $cmt" HEAD "$cmt"' line in call_merge() to this seems to have broken more tests.

Quoted 6 lines

Oh, that is to be expected if you changed git-merge -s recursive with git-merge-recursive without other changes. The former makes a commit (which your original patch later used to create a separate commit chain and discarded); the latter does not make a commit but expects the caller to create a commit out of the resulting index file.

Oops, *smacks head*

Quoted 4 lines
Quoted 3 lines

I'm not an expert at merging strategies by any measure, I've just trusted merge-recursive to Do The Right Thing(TM) more often than not, and use rerere to avoid repeating work.

Quoted 8 lines

I was originally hoping that rebasing would just be a matter of listing sequence of commits to be ported onto a new base and running "git-cherry-pick" on each of them in sequence. Now cherry-pick does not use merge machinery (hence does not use git-merge-recursive), but if we change that then updating rebase would be pretty much straightforward. It just needs a UI layer to guide the user through recovery process when the merge does not resolve cleanly in the middle, no?

Sounds workable right to me. But then again, a cherry-pick is also a case of rebase on a single commit, so we could be using rebase (and its recovery code) in cherry-pick, too, right?

Signature · 1 line
Eric Wong
author
#5 lore raw

PATCH 1/1 rebase: Allow merge strategies to be used when rebasing

1 - is fixed for --onto usage.
2 - in the series is the test from Junio, so I'm not resending it:
Subject: [PATCH] Add renaming-rebase test.
3 - keeps the NO_PYTHON people happy

Signature · 1 line
Eric Wong
author
#6 lore raw

PATCH 1/3 rebase: Allow merge strategies to be used when rebasing

This solves the problem of rebasing local commits against an upstream that has renamed files.

---
 Documentation/git-rebase.txt |   20 ++++
 git-rebase.sh                |  192 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 202 insertions(+), 10 deletions(-)
Documentation/git-rebase.txt +19 -1
index 08ee4aa..c339c45 100644
@@ -7,7 +7,7 @@ git-rebase - Rebase local commits to a n
 
 SYNOPSIS
 --------
-'git-rebase' [--onto <newbase>] <upstream> [<branch>]
+'git-rebase' [--merge] [--onto <newbase>] <upstream> [<branch>]
 
 'git-rebase' --continue | --skip | --abort
 
@@ -106,6 +106,24 @@ OPTIONS
 --abort::
 	Restore the original branch and abort the rebase operation.
 
+--skip::
+	Restart the rebasing process by skipping the current patch.
+	This does not work with the --merge option.
+
+--merge::
+	Use merging strategies to rebase.  When the recursive (default) merge
+	strategy is used, this allows rebase to be aware of renames on the
+	upstream side.
+
+-s <strategy>, \--strategy=<strategy>::
+	Use the given merge strategy; can be supplied more than
+	once to specify them in the order they should be tried.
+	If there is no `-s` option, a built-in list of strategies
+	is used instead (`git-merge-recursive` when merging a single
+	head, `git-merge-octopus` otherwise).  This implies --merge.
+
+include::merge-strategies.txt[]
+
 NOTES
 -----
 When you rebase a branch, you are changing its history in a way that
git-rebase.sh +183 -9

235 lines not on this page · raw message

Signature · 1 line
1.4.0.g65f3
author
#7 lore raw

PATCH 3/3 rebase: error out for NO_PYTHON if they use recursive merge

recursive merge relies on Python, and we can't perform rename-aware merges without the recursive merge. So bail out before trying it.

The test won't work w/o recursive merge, either, so skip that, too.

---
 git-rebase.sh           |    9 +++++++++
 t/t3402-rebase-merge.sh |    6 ++++++
 2 files changed, 15 insertions(+), 0 deletions(-)
git-rebase.sh +9 -0
index bce7bf8..b9ce112 100755
@@ -300,6 +300,15 @@ then
 	exit $?
 fi
 
+if test "@@NO_PYTHON@@" && test "$strategy" = "recursive"
+then
+	die 'The recursive merge strategy currently relies on Python,
+which this installation of git was not configured with.  Please consider
+a different merge strategy (e.g. octopus, resolve, stupid, ours)
+or install Python and git with Python support.'
+
+fi
+
 # start doing a rebase with git-merge
 # this is rename-aware if the recursive (default) strategy is used
 
t/t3402-rebase-merge.sh +6 -0
index 8c7a519..f1c1f35 100755
@@ -7,6 +7,12 @@ test_description='git rebase --merge tes
 
 . ./test-lib.sh
 
+if test "$no_python"; then
+	echo "Skipping: no python => no recursive merge"
+	test_done
+	exit 0
+fi
+
 T="A quick brown fox
 jumps over the lazy dog."
 for i in 1 2 3 4 5 6 7 8 9 10
Signature · 1 line
1.4.0.g65f3
#8 lore raw
In reply to Eric Wong
Quoted 1 line · Eric Wong

1 - is fixed for --onto usage.

More importantly, it used to rebase "side edits further" commit incorrectly in the test sequence -- the commit should only touch "newfile", but it touched "renamed" as well. Your updated patch fixes this problem.

#9 lore raw
In reply to Eric Wong
Quoted 7 lines · Eric Wong
Quoted 6 lines · Junio C Hamano
Quoted 5 lines
Quoted 2 lines

git-merge-$strategy $cmt^ -- HEAD $cmt

Changing the 'git-merge $strategy_args "rebase-merge: $cmt" HEAD "$cmt"' line in call_merge() to this seems to have broken more tests.

Quoted 8 lines
Quoted 6 lines

Oh, that is to be expected if you changed git-merge -s recursive with git-merge-recursive without other changes. The former makes a commit (which your original patch later used to create a separate commit chain and discarded); the latter does not make a commit but expects the caller to create a commit out of the resulting index file.

Oops, *smacks head*

Well, but you used it to do the right thing after all ;-). The patch looks quite good.

Quoted 9 lines
Quoted 8 lines

I was originally hoping that rebasing would just be a matter of listing sequence of commits to be ported onto a new base and running "git-cherry-pick" on each of them in sequence. Now cherry-pick does not use merge machinery (hence does not use git-merge-recursive), but if we change that then updating rebase would be pretty much straightforward. It just needs a UI layer to guide the user through recovery process when the merge does not resolve cleanly in the middle, no?

Quoted 3 lines

Sounds workable right to me. But then again, a cherry-pick is also a case of rebase on a single commit, so we could be using rebase (and its recovery code) in cherry-pick, too, right?

Revert and cherry-pick are quite similar operation (the only difference is that you swap his and pivot when doing revert), so when you implement cherry-pick as an atomic operation you can have revert almost for free. If you have a rebase like you did, it would be a bit more involved to make it do revert as well.

#10 lore raw

PATCH 1/1 Add renaming-rebase test.

In reply to Eric Wong

This tests Eric's "renaming rebase" patch. It tests only very basic cases and most of the tests except the last one passes.

---
 t/t3402-rebase-merge.sh |   96 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 96 insertions(+), 0 deletions(-)
t/t3402-rebase-merge.sh new file +96 -0
new file mode 100755
index 0000000..8c7a519
@@ -0,0 +1,96 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='git rebase --merge test'
+
+. ./test-lib.sh
+
+T="A quick brown fox
+jumps over the lazy dog."
+for i in 1 2 3 4 5 6 7 8 9 10
+do
+	echo "$i $T"
+done >original
+
+test_expect_success setup '
+	git add original &&
+	git commit -m"initial" &&
+	git branch side &&
+	echo "11 $T" >>original &&
+	git commit -a -m"master updates a bit." &&
+
+	echo "12 $T" >>original &&
+	git commit -a -m"master updates a bit more." &&
+
+	git checkout side &&
+	(echo "0 $T" ; cat original) >renamed &&
+	git add renamed &&
+	git update-index --force-remove original &&
+	git commit -a -m"side renames and edits." &&
+
+	tr "[a-z]" "[A-Z]" <original >newfile &&
+	git add newfile &&
+	git commit -a -m"side edits further." &&
+
+	tr "[a-m]" "[A-M]" <original >newfile &&
+	rm -f original &&
+	git commit -a -m"side edits once again." &&
+
+	git branch test-rebase side &&
+	git branch test-rebase-pick side &&
+	git branch test-reference-pick side &&
+	git checkout -b test-merge side
+'
+
+test_expect_success 'reference merge' '
+	git merge -s recursive "reference merge" HEAD master
+'
+
+test_expect_success rebase '
+	git checkout test-rebase &&
+	git rebase --merge master
+'
+
+test_expect_success 'merge and rebase should match' '
+	git diff-tree -r test-rebase test-merge >difference &&
+	if test -s difference
+	then
+		cat difference
+		(exit 1)
+	else
+		echo happy
+	fi
+'
+
+test_expect_success 'rebase the other way' '
+	git reset --hard master &&
+	git rebase --merge side
+'
+
+test_expect_success 'merge and rebase should match' '
+	git diff-tree -r test-rebase test-merge >difference &&
+	if test -s difference
+	then
+		cat difference
+		(exit 1)
+	else
+		echo happy
+	fi
+'
+
+test_expect_success 'picking rebase' '
+	git reset --hard side &&
+	git rebase --merge --onto master side^^ &&
+	mb=$(git merge-base master HEAD) &&
+	if test "$mb" = "$(git rev-parse master)"
+	then
+		echo happy
+	else
+		git show-branch
+		(exit 1)
+	fi
+'
+
+test_done
Signature · 1 line
1.4.0.g1910f