Skip to contentSkip to conversation

Fix spurious conflicts with pull --rebase

PATCH Superseded Elijah Newren 3 participants 7 messages Jun 15, 2016

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Fix spurious conflicts with pull --rebase

This patch series fixes spurious conflict issues with git pull --rebase for the case where the upstream repository is *not* rebased. (There is no change in the case where the upstream repository is rebased.)

In c85c79279d and d44e71261f, the call to git-rebase was modified in an effort to reduce the number of commits being reapplied, by trying to avoid commits that upstream already had or has. It was specifically designed with an upstream that is rebased in mind. Unfortunately, it had two side effects for the non-rebased upstream
case: (1) it prevented detecting if "local" patches are already upstream, and (2) it could in some cases cause more patches known to be upstream to be reapplied rather than less. This series fixes both of these issues for the non-rebased upstream case. See the commit message of the second patch for details.

It's worth noting that issue (1) above also affects the case where the upstream repository has been rebased, which this patch series does not address. As far as I can tell, fixing it would require changes (including new syntax) to format-patch to allow it to be told what 'upstream' is, and some changes to git-pull.sh/git-rebase.sh to pass it this information.

Changes since the last series:
* Address issues in t5520-pull.sh raised by Ævar Arnfjörð Bjarmason

Elijah Newren (2):
t5520-pull: Add testcases showing spurious conflicts from git pull
--rebase
pull --rebase: Avoid spurious conflicts and reapplying unnecessary
patches

 git-pull.sh     |   34 +++++++++++++++++++----------
 t/t5520-pull.sh |   63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 12 deletions(-)
Signature · 1 line
1.7.2.1
#1 lore raw

PATCH v3 1/2 t5520-pull: Add testcases showing spurious conflicts from git pull --rebase

---
 t/t5520-pull.sh |   63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 63 insertions(+), 0 deletions(-)
t/t5520-pull.sh +63 -0
index 319e389..9099e55 100755
@@ -4,6 +4,11 @@ test_description='pulling into void'
 
 . ./test-lib.sh
 
+modify () {
+	sed -e "$1" < "$2" > "$2".x &&
+	mv "$2".x "$2"
+}
+
 D=`pwd`
 
 test_expect_success setup '
@@ -160,4 +165,62 @@ test_expect_success 'pull --rebase works on branch yet to be born' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for detecting upstreamed changes' '
+	mkdir src &&
+	(cd src &&
+	 git init &&
+	 for i in $(seq 1 10); do echo $i; done > stuff &&
+	 git add stuff &&
+	 git commit -m "Initial revision"
+	) &&
+	git clone src dst &&
+	(cd src &&
+	 modify s/5/43/ stuff &&
+	 git commit -a -m "5->43" &&
+	 modify s/6/42/ stuff &&
+	 git commit -a -m "Make it bigger" &&
+	 correct=$(git rev-parse HEAD)
+	) &&
+	(cd dst &&
+	 modify s/5/43/ stuff &&
+	 git commit -a -m "Independent discovery of 5->43"
+	)
+'
+
+test_expect_failure 'git pull --rebase detects upstreamed changes' '
+	(cd dst &&
+	 git pull --rebase &&
+	 test -z "$(git ls-files -u)"
+	)
+'
+
+test_expect_success 'setup for avoiding reapplying old patches' '
+	(cd dst &&
+	 git rebase --abort;
+	 git reset --hard origin/master
+	) &&
+	git clone --bare src src-replace.git &&
+	rm -rf src &&
+	mv src-replace.git src &&
+	(cd dst &&
+	 modify s/2/22/ stuff &&
+	 git commit -a -m "Change 2" &&
+	 modify s/3/33/ stuff &&
+	 git commit -a -m "Change 3" &&
+	 modify s/4/44/ stuff &&
+	 git commit -a -m "Change 4" &&
+	 git push &&
+
+	 modify s/44/55/ stuff &&
+	 git commit --amend -a -m "Modified Change 4"
+	)
+'
+
+test_expect_failure 'git pull --rebase does not reapply old patches' '
+	(cd dst &&
+	 git pull --rebase;
+	 test 1 = $(find .git/rebase-apply -name "000*" | wc -l)
+	)
+'
+
 test_done
Signature · 1 line
1.7.2.1
#2 lore raw

Re: [PATCHv3 1/2] t5520-pull: Add testcases showing spurious conflicts from git pull --rebase

Quoted 10 lines · Elijah Newren
---
 t/t5520-pull.sh |   63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 63 insertions(+), 0 deletions(-)
t/t5520-pull.sh +0 -0
index 319e389..9099e55 100755

t/t5520-pull.sh test_description='pulling into void' · line 10

Quoted 7 lines
t/t5520-pull.sh +4 -0
@@ -4,6 +4,11 @@ test_description='pulling into void'
 
 . ./test-lib.sh
 
+modify () {
+	sed -e "$1" < "$2" > "$2".x &&
+	mv "$2".x "$2"
+}

Just a style thing but I'd prefer to see the above written like this:

        modify () {
                sed -e "$1" <"$2" >"$2.x" &&
                mv "$2.x" "$2"
        }

t/t5520-pull.sh test_expect_success 'pull --rebase works on branch yet to be born' ' · line 199

Quoted 3 lines
t/t5520-pull.sh +3 -0
+test_expect_success 'setup for avoiding reapplying old patches' '
+	(cd dst &&
+	 git rebase --abort;

This may be hypothetical but this discards error condition from failing to ch into dst (for whatever reason). Don't we expect "git rebase --abort" to exit with a non-zero status? Same comment for the last one in the patch below.

t/t5520-pull.sh test_expect_success 'pull --rebase works on branch yet to be born' ' · line 226

Quoted 8 lines
t/t5520-pull.sh +7 -0
+test_expect_failure 'git pull --rebase does not reapply old patches' '
+	(cd dst &&
+	 git pull --rebase;
+	 test 1 = $(find .git/rebase-apply -name "000*" | wc -l)
+	)
+'
+
 test_done

Thanks.

#3 lore raw

Re: [PATCHv3 1/2] t5520-pull: Add testcases showing spurious conflicts from git pull --rebase

Quoted 1 line · Junio C Hamano

Elijah Newren <[email protected]> writes:

<snip>

Quoted 11 lines

t/t5520-pull.sh test_description='pulling into void' · line 10

Quoted 4 lines
t/t5520-pull.sh +4 -0
+modify () {
+     sed -e "$1" < "$2" > "$2".x &&
+     mv "$2".x "$2"
+}

Just a style thing but I'd prefer to see the above written like this:

       modify () {
               sed -e "$1" <"$2" >"$2.x" &&
               mv "$2.x" "$2"
       }

I copied this function verbatim from t/t4127-apply-same-fn.sh. Would you like me to fix that one too?

Quoted 8 lines
Quoted 3 lines

+test_expect_success 'setup for avoiding reapplying old patches' '
+     (cd dst &&
+      git rebase --abort;

This may be hypothetical but this discards error condition from failing to ch into dst (for whatever reason).  Don't we expect "git rebase --abort" to exit with a non-zero status?  Same comment for the last one in the patch below.

Yes, Hannes pointed out the same issue. Does the follow-up interdiff I posted in response to my patch address this in a way you'd like?

#4 lore raw

Re: [PATCHv3 1/2] t5520-pull: Add testcases showing spurious conflicts from git pull --rebase

Quoted 10 lines · Elijah Newren
Quoted 1 line · Junio C Hamano

Elijah Newren <[email protected]> writes:

<snip>

Quoted 7 lines

t/t5520-pull.sh test_description='pulling into void' · line 10

Quoted 4 lines
t/t5520-pull.sh +4 -0
+modify () {
+     sed -e "$1" < "$2" > "$2".x &&
+     mv "$2".x "$2"
+}

Just a style thing but I'd prefer to see the above written like this:

Quoted 7 lines
Quoted 4 lines

       modify () {
               sed -e "$1" <"$2" >"$2.x" &&
               mv "$2.x" "$2"
       }

I copied this function verbatim from t/t4127-apply-same-fn.sh.  Would you like me to fix that one too?

Rather than copy-paste this around we should just turn it into a utility function.

It'd be very useful (if you're up to it) to just use sed in this patch, then submit another patch to include the modify() function in the test-lib.sh (and document it in t/README).

These could also use a modify():

    cd t && grep -A1 sed  *sh | grep -B1 mv | less
#5 lore raw

Re: [PATCHv3 1/2] t5520-pull: Add testcases showing spurious conflicts from git pull --rebase

In reply to Elijah Newren
Quoted 84 lines · Elijah Newren

---
 t/t5520-pull.sh |   63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++

 1 files changed, 63 insertions(+), 0 deletions(-)
t/t5520-pull.sh +0 -0
index 319e389..9099e55 100755
@@ -4,6 +4,11 @@ test_description='pulling into void'
 

 . ./test-lib.sh

t/t5520-pull.sh +5 -0
+modify () {
+       sed -e "$1" < "$2" > "$2".x &&
+       mv "$2".x "$2"
+}
+

 D=pwd

 test_expect_success setup '

t/t5520-pull.sh +0 -0
@@ -160,4 +165,62 @@ test_expect_success 'pull --rebase works on branch yet to be born' '

       test_cmp expect actual
 '

t/t5520-pull.sh +58 -0
+test_expect_success 'setup for detecting upstreamed changes' '
+       mkdir src &&
+       (cd src &&
+        git init &&
+        for i in $(seq 1 10); do echo $i; done > stuff &&
+        git add stuff &&
+        git commit -m "Initial revision"
+       ) &&
+       git clone src dst &&
+       (cd src &&
+        modify s/5/43/ stuff &&
+        git commit -a -m "5->43" &&
+        modify s/6/42/ stuff &&
+        git commit -a -m "Make it bigger" &&
+        correct=$(git rev-parse HEAD)
+       ) &&
+       (cd dst &&
+        modify s/5/43/ stuff &&
+        git commit -a -m "Independent discovery of 5->43"
+       )
+'
+
+test_expect_failure 'git pull --rebase detects upstreamed changes' '
+       (cd dst &&
+        git pull --rebase &&
+        test -z "$(git ls-files -u)"
+       )
+'
+
+test_expect_success 'setup for avoiding reapplying old patches' '
+       (cd dst &&
+        git rebase --abort;
+        git reset --hard origin/master
+       ) &&
+       git clone --bare src src-replace.git &&
+       rm -rf src &&
+       mv src-replace.git src &&
+       (cd dst &&
+        modify s/2/22/ stuff &&
+        git commit -a -m "Change 2" &&
+        modify s/3/33/ stuff &&
+        git commit -a -m "Change 3" &&
+        modify s/4/44/ stuff &&
+        git commit -a -m "Change 4" &&
+        git push &&
+
+        modify s/44/55/ stuff &&
+        git commit --amend -a -m "Modified Change 4"
+       )
+'
+
+test_expect_failure 'git pull --rebase does not reapply old patches' '
+       (cd dst &&
+        git pull --rebase;
+        test 1 = $(find .git/rebase-apply -name "000*" | wc -l)
+       )
+'
+
Quoted 2 lines

 test_done
--

Interdiff addressing Hannes' comments (given on previous series):

t/t5520-pull.sh +4 -5
diff -u b/t/t5520-pull.sh b/t/t5520-pull.sh
@@ -169,7 +169,7 @@
 	mkdir src &&
 	(cd src &&
 	 git init &&
-	 for i in $(seq 1 10); do echo $i; done > stuff &&
+	 printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
 	 git add stuff &&
 	 git commit -m "Initial revision"
 	) &&
@@ -178,8 +178,7 @@
 	 modify s/5/43/ stuff &&
 	 git commit -a -m "5->43" &&
 	 modify s/6/42/ stuff &&
-	 git commit -a -m "Make it bigger" &&
-	 correct=$(git rev-parse HEAD)
+	 git commit -a -m "Make it bigger"
 	) &&
 	(cd dst &&
 	 modify s/5/43/ stuff &&
@@ -196,7 +195,7 @@
 
 test_expect_success 'setup for avoiding reapplying old patches' '
 	(cd dst &&
-	 git rebase --abort;
+	 test_might_fail git rebase --abort &&
 	 git reset --hard origin/master
 	) &&
 	git clone --bare src src-replace.git &&
@@ -218,7 +217,7 @@
 
 test_expect_failure 'git pull --rebase does not reapply old patches' '
 	(cd dst &&
-	 git pull --rebase;
+	 test_must_fail git pull --rebase &&
 	 test 1 = $(find .git/rebase-apply -name "000*" | wc -l)
 	)
 '
#6 lore raw

PATCH v3 2/2 pull --rebase: Avoid spurious conflicts and reapplying unnecessary patches

In reply to Elijah Newren

Prior to c85c79279df2c8a583d95449d1029baba41f8660, pull --rebase would run
git rebase $merge_head
which resulted in a call to
git format-patch ... --ignore-if-in-upstream $merge_head..$cur_branch

This resulted in patches from $merge_head..$cur_branch being applied, as long as they did not already exist in $cur_branch..$merge_head. Unfortunately, when upstream is rebased, $merge_head..$cur_branch also refers to "old" commits that have already been rebased upstream, meaning that many patches that were already fixed upstream would be reapplied. This could result in many spurious conflicts, as well as reintroduce patches that were intentionally dropped upstream.

So the algorithm was changed in c85c79279df2c8a583d95449d1029baba41f8660 and d44e71261f91d3cc81293e0976bb40daa8abb583. Defining $old_remote_ref to be the most recent entry in the reflog for @{upstream} that is an ancestor of $cur_branch, pull --rebase was changed to run
git rebase --onto $merge_head $old_remote_ref
which results in a call to
git format-patch ... --ignore-if-in-upstream $old_remote_ref..$cur_branch

The whole point of this change was to reduce the number of commits being reapplied, by avoiding commits that upstream already has or had.

In the rebased upstream case, this change achieved that purpose. It is worth noting, though, that since $old_remote_ref is always an ancestor of $cur_branch (by its definition), format-patch will not know what upstream is and thus will not be able to determine if any patches are already upstream; they will all be reapplied.

In the non-rebased upstream case, this new form is usually the same as the original code but in some cases $old_remote_ref can be an ancestor of
$(git merge-base $merge_head $cur_branch)
meaning that instead of avoiding reapplying commits that upstream already has, it actually includes more such commits. Combined with the fact that format-patch can no longer detect commits that are already upstream (since it is no longer told what upstream is), results in lots of confusion for users (e.g. "git is giving me lots of conflicts in stuff I didn't even change since my last push.")

Fix the non-rebased upstream case by ignoring $old_remote_ref whenever it is contained in $(git merge-base $merge_head $cur_branch). This should have no affect on the rebased upstream case.

---
Note that 24 lines of the patch are simply due to moving 12 lines; I considered leaving those 12 lines where they were, but it'd split the code that handles the setting of oldremoteref, which I thought should be together. I couldn't place the new code earlier, because it depends on the definition of $merge_head. I'd be happy to reshuffle differently if anyone has any strong preferences.

 git-pull.sh     |   34 ++++++++++++++++++++++------------
 t/t5520-pull.sh |    4 ++--
 2 files changed, 24 insertions(+), 14 deletions(-)
git-pull.sh +22 -12
index a09a44e..54da07b 100755
@@ -206,18 +206,6 @@ test true = "$rebase" && {
 		git diff-index --ignore-submodules --cached --quiet HEAD -- ||
 		die "refusing to pull with rebase: your working tree is not up-to-date"
 	fi
-	oldremoteref= &&
-	. git-parse-remote &&
-	remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
-	oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
-	for reflog in $(git rev-list -g $remoteref 2>/dev/null)
-	do
-		if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
-		then
-			oldremoteref="$reflog"
-			break
-		fi
-	done
 }
 orig_head=$(git rev-parse -q --verify HEAD)
 git fetch $verbosity $progress $dry_run --update-head-ok "$@" || exit 1
@@ -273,6 +261,28 @@ then
 	exit
 fi
 
+if test true = "$rebase"
+then
+	oldremoteref= &&
+	. git-parse-remote &&
+	remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
+	oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
+	for reflog in $(git rev-list -g $remoteref 2>/dev/null)
+	do
+		if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
+		then
+			oldremoteref="$reflog"
+			break
+		fi
+	done
+
+	o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
+	if test "$oldremoteref" = "$o"
+	then
+		unset oldremoteref
+	fi
+fi
+
 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
 case "$rebase" in
 true)
t/t5520-pull.sh +2 -2
index 9099e55..0e5d097 100755
@@ -187,7 +187,7 @@ test_expect_success 'setup for detecting upstreamed changes' '
 	)
 '
 
-test_expect_failure 'git pull --rebase detects upstreamed changes' '
+test_expect_success 'git pull --rebase detects upstreamed changes' '
 	(cd dst &&
 	 git pull --rebase &&
 	 test -z "$(git ls-files -u)"
@@ -216,7 +216,7 @@ test_expect_success 'setup for avoiding reapplying old patches' '
 	)
 '
 
-test_expect_failure 'git pull --rebase does not reapply old patches' '
+test_expect_success 'git pull --rebase does not reapply old patches' '
 	(cd dst &&
 	 git pull --rebase;
 	 test 1 = $(find .git/rebase-apply -name "000*" | wc -l)
Signature · 1 line
1.7.2.1