threads / patch / 25162

patch, 37 partsAudit and fix corner case bugs in recursive merge

Subject: [PATCH 00/37] Audit and fix corner case bugs in recursive merge

## tl;dr

46 messages between Sep 20, 2010 and Sep 22, 2010. Diffs are folded; open one to read it.

replies: 45people: 4as markdown or json

Elijah Newren· Sep 20, 2010, 08:28 UTC · lore

I may want to add another test or two, and tweak some things here or there, but I finally have this series working enough that others could test or comment on it now...

This patch series serves as an audit and fix of a number of corner case bugs in the recursive merge algorithm. The initial thrust for the audit came from this exchange (from http://thread.gmane.org/gmane.comp.version-control.git/155770/focus=155917):

On Thu, Sep 9, 2010 at 6:26 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 13 quoted lines
> Elijah Newren <newren@gmail.com> writes:
>> ... git can't know that there's a conflict
>> until after it's tried resolving paths involving newsub/newfile to see if
>> they are still in the way at the end (and if newsub/newfile is not in the
>> way at the end, there should be no conflict at all, which did not hold with
>> git previously).
>
> I'll queue this patch to 'pu', but anybody who wrote the above to
> correctly arriave at the crux of the issue in his analysis would know that
> this is another band-aid on top of band-aid.  The approach merge-recursive
> takes to first grab the set of directories and files fundamentally is
> wrong---it should be resolving the paths in-core first and then look at
> the result to ignore a directory that has become empty.

Junio is right, of course -- on all counts. Inspection of the current band-aids shows that there are still a number of bugs in the recursive merge algorithm in corner cases with renames and directory/file (D/F) conflicts. My investigation also turned up a bug not involving renames but dealing with D/F conflicts in combination with modify/delete conflicts. I also happened to find a bug not involving D/F conflicts where git could incorrectly resolve a merge involving prior criss-cross merges (in some cases silently accepting one side of the merge as the final resolution without detecting a relevant conflict).

This patch series is predominantly about doing exactly as Junio suggests: resolving paths in-core first and then looking at the result to see if the D/F conflicts still have paths in the way of each other at the end. However, it also includes the creation of several testcases showing how and where the current algorithm is insufficient, and addresses the undetected merge conflict found as well.

This series is based on next as it modifies/builds-on en/rename-d-f; it cannot be played directly on top of that series as it also depends on ks/recursive-rename-add-identical from master.

This series also includes three patches previously posted on the list but which have not yet been picked up in pu or elsewhere: One from Junio to allow creation of new process_*() functions and modifying process_df_entry() to work with such chaining, one from Ken Schalk to add a testcase for resolving a rename/add conflict involving symlinks, and a previous submission of mine to rename/clarify the 'stage' variable in process_renames().

The series ended up being bigger than I realized when I started. Also, since it's whole purpose is to handle corner cases, I know that it may be harder to review than other series. I've tried to break it down to address that, but I'm not sure if I have split the patches too finely or too coarsely, or whether I've added sufficient explanation. Let me know.

Schalk, Ken (1):
  t3030: Add a testcase for resolvable rename/add conflict with
    symlinks
Junio C Hamano (1):
  merge-recursive: Restructure showing how to chain more process_*
    functions
Elijah Newren (35):
  t6032: Add a test checking for excessive output from merge
  t6022: Add test combinations of {content conflict?, D/F conflict
    remains?}
  t6022: Add tests for reversing order of merges when D/F conflicts
    present
  t6022: Add tests with both rename source & dest involved in D/F
    conflicts
  t6022: Add paired rename+D/F conflict: (two/file, one/file) -> (one,
    two)
  t6022: Add tests for rename/rename combined with D/F conflicts
  t6020: Modernize style a bit
  t6020: Add a testcase for modify/delete + directory/file conflict
  t6036: Test index and worktree state, not just that merge fails
  t6036: Add a second testcase similar to the first but with content
    changes
  t6036: Add testcase for undetected conflict

The above patches are all about adding testcases both to extend our coverage of different cases, and to expose corner case bugs.

  merge-recursive: Small code clarification -- variable name and
    comments
  merge-recursive: Rename conflict_rename_rename*() for clarity
  merge-recursive: Nuke rename/directory conflict detection
These three patches are just small code cleanups.  No bug fixes yet.
  merge-recursive: Move rename/delete handling into dedicated function
  merge-recursive: Move delete/modify handling into dedicated function
  merge-recursive: Move process_entry's content merging into a function

These three patches are just moving code into functions which will later be called from multiple places. Still no bug fixes.

  merge-recursive: New data structures for deferring of D/F conflicts
  merge-recursive: New function to assist resolving renames in-core
    only
  merge-recursive: Have process_entry() skip D/F or rename entries
  merge-recursive: Structure process_df_entry() to handle more cases
  merge-recursive: Update conflict_rename_rename_1to2() call signature
  merge-recursive: Update merge_content() call signature

These patches introduce new data structures and update the call signatures of various functions to make it so I can pass additional D/F conflict information to them. Still no bug fixes yet.

  merge-recursive: Avoid doubly merging rename/add conflict contents

This patch is for what I belive to be the most concerning of the corner case bugs I found (since git will under some circumstances create and report a clean merge when it should have detected a conflict), and the only one that doesn't involve D/F conflicts.

  merge-recursive: Move handling of double rename of one file to two
  merge-recursive: Move handling of double rename of one file to other
    file
  merge-recursive: Delay handling of rename/delete conflicts
  merge-recursive: Delay content merging for renames
  merge-recursive: Delay modify/delete conflicts if D/F conflict
    present

These patches simply move anything related to D/F conflicts from process_renames() and process_entry() into process_df_entry(). Just moving the code without further changes already fixes a couple bugs.

  conflict_rename_delete(): Check whether D/F conflicts are still
    present
  conflict_rename_rename_1to2(): Fix checks for presence of D/F
    conflicts
  merge_content(): Check whether D/F conflicts are still present
  handle_delete_modify(): Check whether D/F conflicts are still present
  merge-recursive: Make room for directories in D/F conflicts

These patches are where the majority of the corner case bugs involving D/F conflicts get fixed, now that the appropriate structure is in place.

  merge-recursive: Remove redundant path clearing for D/F conflicts

...and one final clean up patch, due to the fact that keeping all the tests passing in intermediate commits meant using ad-hoc methods to make room for directories in D/F conflicts.

 merge-recursive.c                 |  649 ++++++++++++++++++++++++-------------
 t/t3030-merge-recursive.sh        |   37 ++-
 t/t6020-merge-df.sh               |   82 ++++-
 t/t6022-merge-rename.sh           |  366 +++++++++++++++++++++
 t/t6032-merge-large-rename.sh     |   30 ++
 t/t6036-recursive-corner-cases.sh |  185 +++++++++++-
 6 files changed, 1108 insertions(+), 241 deletions(-)
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 01/37] t3030: Add a testcase for resolvable rename/add conflict with symlinks

From: Schalk, Ken <ken.schalk@intel.com>

d5af510 (RE: [PATCH] Avoid rename/add conflict when contents are identical 2010-09-01) avoided erroring out in a rename/add conflict when the contents were identical. A simpler fix could have handled that particular testcase, but it would not correctly handle the case where a symlink is involved. Add another testcase using symlinks, to avoid breaking that case.

Signed-off-by: Ken Schalk <ken.schalk@intel.com>
Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t3030-merge-recursive.sh |   37 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 36 insertions(+), 1 deletions(-)
Show changes to t/t3030-merge-recursive.sh +36 −1
diff --git a/t/t3030-merge-recursive.sh b/t/t3030-merge-recursive.sh
index e66e550..3935c4b 100755
--- a/t/t3030-merge-recursive.sh
+++ b/t/t3030-merge-recursive.sh
@@ -25,6 +25,10 @@ test_expect_success 'setup 1' '
 	git branch submod &&
 	git branch copy &&
 	git branch rename &&
+	if test_have_prereq SYMLINKS
+	then
+		git branch rename-ln
+	fi &&
 
 	echo hello >>a &&
 	cp a d/e &&
@@ -255,7 +259,16 @@ test_expect_success 'setup 8' '
 	git mv a e &&
 	git add e &&
 	test_tick &&
-	git commit -m "rename a->e"
+	git commit -m "rename a->e" &&
+	if test_have_prereq SYMLINKS
+	then
+		git checkout rename-ln &&
+		git mv a e &&
+		ln -s e a &&
+		git add a e &&
+		test_tick &&
+		git commit -m "rename a->e, symlink a->e"
+	fi
 '
 
 test_expect_success 'setup 9' '
@@ -615,4 +628,26 @@ test_expect_success 'merge-recursive copy vs. rename' '
 	test_cmp expected actual
 '
 
+if test_have_prereq SYMLINKS
+then
+	test_expect_success 'merge-recursive rename vs. rename/symlink' '
+
+		git checkout -f rename &&
+		git merge rename-ln &&
+		( git ls-tree -r HEAD ; git ls-files -s ) >actual &&
+		(
+			echo "100644 blob $o0	b"
+			echo "100644 blob $o0	c"
+			echo "100644 blob $o0	d/e"
+			echo "100644 blob $o0	e"
+			echo "100644 $o0 0	b"
+			echo "100644 $o0 0	c"
+			echo "100644 $o0 0	d/e"
+			echo "100644 $o0 0	e"
+		) >expected &&
+		test_cmp expected actual
+	'
+fi
+
+
 test_done
-- 
1.7.3.271.g16009
Johannes Sixt· Sep 20, 2010, 09:15 UTC · re: Elijah Newren · lore

Re: [PATCH 01/37] t3030: Add a testcase for resolvable rename/add conflict with symlinks

Am 9/20/2010 10:28, schrieb Elijah Newren:
Show 16 quoted lines
> @@ -255,7 +259,16 @@ test_expect_success 'setup 8' '
>  	git mv a e &&
>  	git add e &&
>  	test_tick &&
> -	git commit -m "rename a->e"
> +	git commit -m "rename a->e" &&
> +	if test_have_prereq SYMLINKS
> +	then
> +		git checkout rename-ln &&
> +		git mv a e &&
> +		ln -s e a &&
> +		git add a e &&
> +		test_tick &&
> +		git commit -m "rename a->e, symlink a->e"
> +	fi
>  '
You don't need the SYMLINKS prerequisite anywhere. Write this without ln -s:
	git commit -m "rename a->e" &&
	git checkout rename-ln &&
	git mv a e &&
	sha1e=$(printf e | git hash-object -w --stdin) &&
	git update-index --add --cacheinfo 120000 $sha1e a &&
	test_tick &&
	git commit -m "rename a->e, symlink a->e"
and the new test case passes even when SYMLINKS are not available!
-- Hannes
Jakub Narebski· Sep 20, 2010, 09:34 UTC · re: Elijah Newren · lore

Re: [PATCH 01/37] t3030: Add a testcase for resolvable rename/add conflict with symlinks

Elijah Newren <newren@gmail.com> writes:
Just an addition to what Johannes Sixt wrote.
Show 15 quoted lines
> @@ -255,7 +259,16 @@ test_expect_success 'setup 8' '
>  	git mv a e &&
>  	git add e &&
>  	test_tick &&
> -	git commit -m "rename a->e"
> +	git commit -m "rename a->e" &&
> +	if test_have_prereq SYMLINKS
> +	then
> +		git checkout rename-ln &&
> +		git mv a e &&
> +		ln -s e a &&
> +		git add a e &&
> +		test_tick &&
> +		git commit -m "rename a->e, symlink a->e"
> +	fi

Wouldn't it be better to move it to separate test_expect_success entry... if SYMLINK prerequisite would be really needed (Johannes says it isn't).

> +if test_have_prereq SYMLINKS
> +then
> +	test_expect_success 'merge-recursive rename vs. rename/symlink' '
[...]
> +fi
It is better to use
  +test_expect_success SYMLINKS 'merge-recursive rename vs. rename/symlink' '
form, see t/README about three-argument form of test_expect_success.
-- 
Jakub Narebski
Poland
ShadeHawk on #git
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 02/37] merge-recursive: Restructure showing how to chain more process_* functions

From: Junio C Hamano <gitster@pobox.com>

In 3734893 (merge-recursive: Fix D/F conflicts 2010-07-09), process_df_entry() was added to process_renames() and process_entry() but in a somewhat restrictive manner. Modify the code slightly to make it clearer how we could chain more such functions if necessary, and alter process_df_entry() to handle such chaining.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)
Show changes to merge-recursive.c +8 −3
diff --git a/merge-recursive.c b/merge-recursive.c
index bf611ae..4d9c165 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1260,9 +1260,8 @@ static int process_df_entry(struct merge_options *o,
 	const char *conf;
 	struct stat st;
 
-	/* We currently only handle D->F cases */
-	assert((!o_sha && a_sha && !b_sha) ||
-	       (!o_sha && !a_sha && b_sha));
+	if (! ((!o_sha && a_sha && !b_sha) || (!o_sha && !a_sha && b_sha)))
+		return 1; /* we don't handle non D-F cases */
 
 	entry->processed = 1;
 
@@ -1351,6 +1350,12 @@ int merge_trees(struct merge_options *o,
 				&& !process_df_entry(o, path, e))
 				clean = 0;
 		}
+		for (i = 0; i < entries->nr; i++) {
+			struct stage_data *e = entries->items[i].util;
+			if (!e->processed)
+				die("Unprocessed path??? %s",
+				    entries->items[i].string);
+		}
 
 		string_list_clear(re_merge, 0);
 		string_list_clear(re_head, 0);
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 03/37] t6032: Add a test checking for excessive output from merge

Previous D/F fixes I submitted (5a2580d and ae74548) had caused merge to become excessively spammy, which was fixed in 96ecac6 (merge-recursive: Avoid excessive output for and reprocessing of renames 2010-08-20). Add a new test to avoid repeating that mistake with my several upcoming changes.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6032-merge-large-rename.sh |   30 ++++++++++++++++++++++++++++++
 1 files changed, 30 insertions(+), 0 deletions(-)
Show changes to t/t6032-merge-large-rename.sh +30 −0
diff --git a/t/t6032-merge-large-rename.sh b/t/t6032-merge-large-rename.sh
index eac5eba..fdb6c25 100755
--- a/t/t6032-merge-large-rename.sh
+++ b/t/t6032-merge-large-rename.sh
@@ -70,4 +70,34 @@ test_expect_success 'set merge.renamelimit to 5' '
 test_rename 5 ok
 test_rename 6 fail
 
+test_expect_success 'setup large simple rename' '
+	git config --unset merge.renamelimit &&
+	git config --unset diff.renamelimit &&
+
+	git reset --hard initial &&
+	for i in $(count 200); do
+		make_text foo bar baz >$i
+	done &&
+	git add . &&
+	git commit -m create-files &&
+
+	git branch simple-change &&
+	git checkout -b simple-rename &&
+
+	mkdir builtin &&
+	git mv [0-9]* builtin/ &&
+	git commit -m renamed &&
+
+	git checkout simple-change &&
+	>unrelated-change &&
+	git add unrelated-change &&
+	git commit -m unrelated-change
+'
+
+test_expect_success 'massive simple rename does not spam added files' '
+	unset GIT_MERGE_VERBOSITY &&
+	git merge --no-stat simple-rename | grep -v Removing >output &&
+	test 5 -gt "$(wc -l < output)"
+'
+
 test_done
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 04/37] t6022: Add test combinations of {content conflict?, D/F conflict remains?}

Add testing of the various ways that a renamed file to a path involved in a directory/file conflict may be involved in. This includes whether or not there are conflicts of the contents of the renamed file (if the file was modified on both sides of history), and whether the directory from the other side of the merge will disappear as a result of the merge or not.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6022-merge-rename.sh |  128 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 128 insertions(+), 0 deletions(-)
Show changes to t/t6022-merge-rename.sh +128 −0
diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh
index b66544b..a992206 100755
--- a/t/t6022-merge-rename.sh
+++ b/t/t6022-merge-rename.sh
@@ -3,6 +3,11 @@
 test_description='Merge-recursive merging renames'
 . ./test-lib.sh
 
+modify () {
+	sed -e "$1" <"$2" >"$2.x" &&
+	mv "$2.x" "$2"
+}
+
 test_expect_success setup \
 '
 cat >A <<\EOF &&
@@ -341,4 +346,127 @@ test_expect_success 'merge of identical changes in a renamed file' '
 	GIT_MERGE_VERBOSITY=3 git merge change+rename | grep "^Skipped B"
 '
 
+test_expect_success 'setup for rename + d/f conflicts' '
+	git reset --hard &&
+	git checkout --orphan dir-in-way &&
+	git rm -rf . &&
+
+	mkdir sub &&
+	mkdir dir &&
+	printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" >sub/file &&
+	echo foo >dir/file-in-the-way &&
+	git add -A &&
+	git commit -m "Common commmit" &&
+
+	echo 11 >>sub/file &&
+	echo more >>dir/file-in-the-way &&
+	git add -u &&
+	git commit -m "Commit to merge, with dir in the way" &&
+
+	git checkout -b dir-not-in-way &&
+	git reset --soft HEAD^ &&
+	git rm -rf dir &&
+	git commit -m "Commit to merge, with dir removed" -- dir sub/file &&
+
+	git checkout -b renamed-file-has-no-conflicts dir-in-way~1 &&
+	git rm -rf dir &&
+	git rm sub/file &&
+	printf "1\n2\n3\n4\n5555\n6\n7\n8\n9\n10\n" >dir &&
+	git add dir &&
+	git commit -m "Independent change" &&
+
+	git checkout -b renamed-file-has-conflicts dir-in-way~1 &&
+	git rm -rf dir &&
+	git mv sub/file dir &&
+	echo 12 >>dir &&
+	git add dir &&
+	git commit -m "Conflicting change"
+'
+
+printf "1\n2\n3\n4\n5555\n6\n7\n8\n9\n10\n11\n" >expected
+
+test_expect_success 'Rename+D/F conflict; renamed file merges + dir not in way' '
+	git reset --hard &&
+	git checkout -q renamed-file-has-no-conflicts^0 &&
+	git merge --strategy=recursive dir-not-in-way &&
+	git diff --quiet &&
+	test -f dir &&
+	test_cmp expected dir
+'
+
+test_expect_failure 'Rename+D/F conflict; renamed file merges but dir in way' '
+	git reset --hard &&
+	rm -rf dir~* &&
+	git checkout -q renamed-file-has-no-conflicts^0 &&
+	test_must_fail git merge --strategy=recursive dir-in-way >output &&
+
+	grep "CONFLICT (delete/modify): dir/file-in-the-way" output &&
+	grep "Auto-merging dir" output &&
+	grep "Adding as dir~HEAD instead" output &&
+
+	test 2 = "$(git ls-files -u | wc -l)" &&
+	test 2 = "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
+
+	test_must_fail git diff --quiet &&
+	test_must_fail git diff --cached --quiet &&
+
+	test -f dir/file-in-the-way &&
+	test -f dir~HEAD &&
+	test_cmp expected dir~HEAD
+'
+
+cat >expected <<\EOF &&
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+<<<<<<< HEAD
+12
+=======
+11
+>>>>>>> dir-not-in-way
+EOF
+
+test_expect_failure 'Rename+D/F conflict; renamed file cannot merge, dir not in way' '
+	git reset --hard &&
+	rm -rf dir~* &&
+	git checkout -q renamed-file-has-conflicts^0 &&
+	test_must_fail git merge --strategy=recursive dir-not-in-way &&
+
+	test 3 = "$(git ls-files -u | wc -l)" &&
+	test 3 = "$(git ls-files -u dir | wc -l)" &&
+
+	test_must_fail git diff --quiet &&
+	test_must_fail git diff --cached --quiet &&
+
+	test -f dir &&
+	test_cmp expected dir
+'
+
+test_expect_failure 'Rename+D/F conflict; renamed file cannot merge and dir in the way' '
+	modify s/dir-not-in-way/dir-in-way/ expected &&
+
+	git reset --hard &&
+	rm -rf dir~* &&
+	git checkout -q renamed-file-has-conflicts^0 &&
+	test_must_fail git merge --strategy=recursive dir-in-way &&
+
+	test 5 = "$(git ls-files -u | wc -l)" &&
+	test 3 = "$(git ls-files -u dir | grep -v file-in-the-way | wc -l)" &&
+	test 2 = "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
+
+	test_must_fail git diff --quiet &&
+	test_must_fail git diff --cached --quiet &&
+
+	test -f dir/file-in-the-way &&
+	test -f dir~HEAD &&
+	test_cmp expected dir~HEAD
+'
+
 test_done
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 05/37] t6022: Add tests for reversing order of merges when D/F conflicts present

When merging two branches with some path involved in a D/F conflict, the choice of which branch to merge into the other matters for (at least) two reasons: (1) whether the working copy has a directory full of files that is in the way of a file, or a file exists that is in the way of a directory of files, (2) when the directory full of files does not disappear due to the merge, what files at the same paths should be renamed to (e.g. filename~HEAD vs. filename~otherbranch).

Add some tests that reverse the merge order of two other tests, and which verify the contents are as expected (namely, that the results are identical other than modified-for-uniqueness filenames involving branch names).

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6022-merge-rename.sh |   58 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)
Show changes to t/t6022-merge-rename.sh +58 −0
diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh
index a992206..2839dfb 100755
--- a/t/t6022-merge-rename.sh
+++ b/t/t6022-merge-rename.sh
@@ -415,6 +415,28 @@ test_expect_failure 'Rename+D/F conflict; renamed file merges but dir in way' '
 	test_cmp expected dir~HEAD
 '
 
+test_expect_failure 'Same as previous, but merged other way' '
+	git reset --hard &&
+	rm -rf dir~* &&
+	git checkout -q dir-in-way^0 &&
+	test_must_fail git merge --strategy=recursive renamed-file-has-no-conflicts >output 2>errors &&
+
+	! grep "error: refusing to lose untracked file at" errors &&
+	grep "CONFLICT (delete/modify): dir/file-in-the-way" output &&
+	grep "Auto-merging dir" output &&
+	grep "Adding as dir~renamed-file-has-no-conflicts instead" output &&
+
+	test 2 = "$(git ls-files -u | wc -l)" &&
+	test 2 = "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
+
+	test_must_fail git diff --quiet &&
+	test_must_fail git diff --cached --quiet &&
+
+	test -f dir/file-in-the-way &&
+	test -f dir~renamed-file-has-no-conflicts &&
+	test_cmp expected dir~renamed-file-has-no-conflicts
+'
+
 cat >expected <<\EOF &&
 1
 2
@@ -469,4 +491,40 @@ test_expect_failure 'Rename+D/F conflict; renamed file cannot merge and dir in t
 	test_cmp expected dir~HEAD
 '
 
+cat >expected <<\EOF &&
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+<<<<<<< HEAD
+11
+=======
+12
+>>>>>>> renamed-file-has-conflicts
+EOF
+
+test_expect_failure 'Same as previous, but merged other way' '
+	git reset --hard &&
+	rm -rf dir~* &&
+	git checkout -q dir-in-way^0 &&
+	test_must_fail git merge --strategy=recursive renamed-file-has-conflicts &&
+
+	test 5 = "$(git ls-files -u | wc -l)" &&
+	test 3 = "$(git ls-files -u dir | grep -v file-in-the-way | wc -l)" &&
+	test 2 = "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
+
+	test_must_fail git diff --quiet &&
+	test_must_fail git diff --cached --quiet &&
+
+	test -f dir/file-in-the-way &&
+	test -f dir~renamed-file-has-conflicts &&
+	test_cmp expected dir~renamed-file-has-conflicts
+'
+
 test_done
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 06/37] t6022: Add tests with both rename source & dest involved in D/F conflicts

Having the source of a rename be involved in a directory/file conflict does not currently pose any difficulties to the current merge-recursive algorithm (in contrast to destinations of renames and D/F conflicts). However, combining the two seemed like good testcases to include for completeness.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6022-merge-rename.sh |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)
Show changes to t/t6022-merge-rename.sh +38 −0
diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh
index 2839dfb..2af863c 100755
--- a/t/t6022-merge-rename.sh
+++ b/t/t6022-merge-rename.sh
@@ -527,4 +527,42 @@ test_expect_failure 'Same as previous, but merged other way' '
 	test_cmp expected dir~renamed-file-has-conflicts
 '
 
+test_expect_success 'setup both rename source and destination involved in D/F conflict' '
+	git reset --hard &&
+	git checkout --orphan rename-dest &&
+	git rm -rf . &&
+	git clean -fdqx &&
+
+	mkdir one &&
+	echo stuff >one/file &&
+	git add -A &&
+	git commit -m "Common commmit" &&
+
+	git mv one/file destdir &&
+	git commit -m "Renamed to destdir" &&
+
+	git checkout -b source-conflict HEAD~1 &&
+	git rm -rf one &&
+	mkdir destdir &&
+	touch one destdir/foo &&
+	git add -A &&
+	git commit -m "Conflicts in the way"
+'
+
+test_expect_failure 'both rename source and destination involved in D/F conflict' '
+	git reset --hard &&
+	rm -rf dir~* &&
+	git checkout -q rename-dest^0 &&
+	test_must_fail git merge --strategy=recursive source-conflict &&
+
+	test 1 = "$(git ls-files -u | wc -l)" &&
+
+	test_must_fail git diff --quiet &&
+
+	test -f destdir/foo &&
+	test -f one &&
+	test -f destdir~HEAD &&
+	test "stuff" = "$(cat destdir~HEAD)"
+'
+
 test_done
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 07/37] t6022: Add paired rename+D/F conflict: (two/file, one/file) -> (one, two)

An interesting testcase is having two files each in their own subdirectory getting renamed to the toplevel at the directory pathname of the other. Questions arise as to whether the order of operations matters and whether the directories can correctly get out of the way and make room for the new files.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6022-merge-rename.sh |   63 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 63 insertions(+), 0 deletions(-)
Show changes to t/t6022-merge-rename.sh +63 −0
diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh
index 2af863c..a38b383 100755
--- a/t/t6022-merge-rename.sh
+++ b/t/t6022-merge-rename.sh
@@ -565,4 +565,67 @@ test_expect_failure 'both rename source and destination involved in D/F conflict
 	test "stuff" = "$(cat destdir~HEAD)"
 '
 
+test_expect_success 'setup pair rename to parent of other (D/F conflicts)' '
+	git reset --hard &&
+	git checkout --orphan rename-two &&
+	git rm -rf . &&
+	git clean -fdqx &&
+
+	mkdir one &&
+	mkdir two &&
+	echo stuff >one/file &&
+	echo other >two/file &&
+	git add -A &&
+	git commit -m "Common commmit" &&
+
+	git rm -rf one &&
+	git mv two/file one &&
+	git commit -m "Rename two/file -> one" &&
+
+	git checkout -b rename-one HEAD~1 &&
+	git rm -rf two &&
+	git mv one/file two &&
+	rm -r one &&
+	git commit -m "Rename one/file -> two"
+'
+
+test_expect_failure 'pair rename to parent of other (D/F conflicts) w/ untracked dir' '
+	git checkout -q rename-one^0 &&
+	mkdir one &&
+	test_must_fail git merge --strategy=recursive rename-two &&
+
+	test 2 = "$(git ls-files -u | wc -l)" &&
+	test 1 = "$(git ls-files -u one | wc -l)" &&
+	test 1 = "$(git ls-files -u two | wc -l)" &&
+
+	test_must_fail git diff --quiet &&
+
+	test 4 = $(find . | grep -v .git | wc -l) &&
+
+	test -d one &&
+	test -f one~rename-two &&
+	test -f two &&
+	test "other" = $(cat one~rename-two) &&
+	test "stuff" = $(cat two)
+'
+
+test_expect_success 'pair rename to parent of other (D/F conflicts) w/ clean start' '
+	git reset --hard &&
+	git clean -fdqx &&
+	test_must_fail git merge --strategy=recursive rename-two &&
+
+	test 2 = "$(git ls-files -u | wc -l)" &&
+	test 1 = "$(git ls-files -u one | wc -l)" &&
+	test 1 = "$(git ls-files -u two | wc -l)" &&
+
+	test_must_fail git diff --quiet &&
+
+	test 3 = $(find . | grep -v .git | wc -l) &&
+
+	test -f one &&
+	test -f two &&
+	test "other" = $(cat one) &&
+	test "stuff" = $(cat two)
+'
+
 test_done
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 08/37] t6022: Add tests for rename/rename combined with D/F conflicts

Add tests where one file is renamed to two different paths in different sides of history, and where each of the new files matches the name of a directory from the opposite side of history. Include tests for both the case where the merge results in those directories not being cleanly removed, and where those directories are cleanly removed during the merge.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6022-merge-rename.sh |   79 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 79 insertions(+), 0 deletions(-)
Show changes to t/t6022-merge-rename.sh +79 −0
diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh
index a38b383..02dea16 100755
--- a/t/t6022-merge-rename.sh
+++ b/t/t6022-merge-rename.sh
@@ -628,4 +628,83 @@ test_expect_success 'pair rename to parent of other (D/F conflicts) w/ clean sta
 	test "stuff" = $(cat two)
 '
 
+test_expect_success 'setup rename of one file to two, with directories in the way' '
+	git reset --hard &&
+	git checkout --orphan first-rename &&
+	git rm -rf . &&
+	git clean -fdqx &&
+
+	echo stuff >original &&
+	git add -A &&
+	git commit -m "Common commmit" &&
+
+	mkdir two &&
+	>two/file &&
+	git add two/file &&
+	git mv original one &&
+	git commit -m "Put two/file in the way, rename to one" &&
+
+	git checkout -b second-rename HEAD~1 &&
+	mkdir one &&
+	>one/file &&
+	git add one/file &&
+	git mv original two &&
+	git commit -m "Put one/file in the way, rename to two"
+'
+
+test_expect_failure 'check handling of differently renamed file with D/F conflicts' '
+	git checkout -q first-rename^0 &&
+	test_must_fail git merge --strategy=recursive second-rename &&
+
+	test 5 = "$(git ls-files -s | wc -l)" &&
+	test 3 = "$(git ls-files -u | wc -l)" &&
+	test 1 = "$(git ls-files -u one | wc -l)" &&
+	test 1 = "$(git ls-files -u two | wc -l)" &&
+	test 1 = "$(git ls-files -u original | wc -l)" &&
+	test 2 = "$(git ls-files -o | wc -l)" &&
+
+	test -f one/file &&
+	test -f two/file &&
+	test -f one~HEAD &&
+	test -f two~second-rename &&
+	! test -f original
+'
+
+test_expect_success 'setup rename one file to two; directories moving out of the way' '
+	git reset --hard &&
+	git checkout --orphan first-rename-redo &&
+	git rm -rf . &&
+	git clean -fdqx &&
+
+	echo stuff >original &&
+	mkdir one two &&
+	touch one/file two/file &&
+	git add -A &&
+	git commit -m "Common commmit" &&
+
+	git rm -rf one &&
+	git mv original one &&
+	git commit -m "Rename to one" &&
+
+	git checkout -b second-rename-redo HEAD~1 &&
+	git rm -rf two &&
+	git mv original two &&
+	git commit -m "Rename to two"
+'
+
+test_expect_failure 'check handling of differently renamed file with D/F conflicts' '
+	git checkout -q first-rename-redo^0 &&
+	test_must_fail git merge --strategy=recursive second-rename-redo &&
+
+	test 3 = "$(git ls-files -u | wc -l)" &&
+	test 1 = "$(git ls-files -u one | wc -l)" &&
+	test 1 = "$(git ls-files -u two | wc -l)" &&
+	test 1 = "$(git ls-files -u original | wc -l)" &&
+	test 0 = "$(git ls-files -o | wc -l)" &&
+
+	test -f one &&
+	test -f two &&
+	! test -f original
+'
+
 test_done
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 09/37] t6020: Modernize style a bit

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6020-merge-df.sh |   35 ++++++++++++++++++++---------------
 1 files changed, 20 insertions(+), 15 deletions(-)
Show changes to t/t6020-merge-df.sh +20 −15
diff --git a/t/t6020-merge-df.sh b/t/t6020-merge-df.sh
index 490d397..8662207 100755
--- a/t/t6020-merge-df.sh
+++ b/t/t6020-merge-df.sh
@@ -6,21 +6,26 @@
 test_description='Test merge with directory/file conflicts'
 . ./test-lib.sh
 
-test_expect_success 'prepare repository' \
-'echo "Hello" > init &&
-git add init &&
-git commit -m "Initial commit" &&
-git branch B &&
-mkdir dir &&
-echo "foo" > dir/foo &&
-git add dir/foo &&
-git commit -m "File: dir/foo" &&
-git checkout B &&
-echo "file dir" > dir &&
-git add dir &&
-git commit -m "File: dir"'
-
-test_expect_code 1 'Merge with d/f conflicts' 'git merge "merge msg" B master'
+test_expect_success 'prepare repository' '
+	echo Hello >init &&
+	git add init &&
+	git commit -m initial &&
+
+	git branch B &&
+	mkdir dir &&
+	echo foo >dir/foo &&
+	git add dir/foo &&
+	git commit -m "File: dir/foo" &&
+
+	git checkout B &&
+	echo file dir >dir &&
+	git add dir &&
+	git commit -m "File: dir"
+'
+
+test_expect_success 'Merge with d/f conflicts' '
+	test_must_fail git merge master
+'
 
 test_expect_success 'F/D conflict' '
 	git reset --hard &&
-- 
1.7.3.271.g16009
Johannes Sixt· Sep 20, 2010, 09:24 UTC · re: Elijah Newren · lore

Re: [PATCH 09/37] t6020: Modernize style a bit

Am 9/20/2010 10:28, schrieb Elijah Newren:
> -test_expect_code 1 'Merge with d/f conflicts' 'git merge "merge msg" B master'
> +test_expect_success 'Merge with d/f conflicts' '
> +	test_must_fail git merge master
> +'

The old version requested a particular kind of failure. Are you saying that with modern 'git merge' all non-zero exit codes mean the same kind of failure?

-- Hannes
Elijah Newren· Sep 20, 2010, 16:03 UTC · re: Johannes Sixt · lore

Re: [PATCH 09/37] t6020: Modernize style a bit

On Mon, Sep 20, 2010 at 3:24 AM, Johannes Sixt <j.sixt@viscovery.net> wrote:
Show 9 quoted lines
> Am 9/20/2010 10:28, schrieb Elijah Newren:
>> -test_expect_code 1 'Merge with d/f conflicts' 'git merge "merge msg" B master'
>> +test_expect_success 'Merge with d/f conflicts' '
>> +     test_must_fail git merge master
>> +'
>
> The old version requested a particular kind of failure. Are you saying
> that with modern 'git merge' all non-zero exit codes mean the same kind of
> failure?

No, I'm saying that I don't think the test meant to try to distinguish between exit codes. I think the test only used test_expect_code because that's all that existed in 2005 (test_must_fail postdates this test by over 2 years). There's really only one exit code from git merge[1], so there's really not much point in trying to distinguish between kinds of failure.

[1] Okay, technically you can get code 128 if you hit a die(), but we don't in general try to differentiate between failure due to hitting die() and failure for other reasons when using test_must fail, so I didn't think it was worth checking here either.

Junio C Hamano· Sep 22, 2010, 01:44 UTC · re: Elijah Newren · lore

Re: [PATCH 09/37] t6020: Modernize style a bit

Elijah Newren <newren@gmail.com> writes:
Show 6 quoted lines
>> The old version requested a particular kind of failure. Are you saying
>> that with modern 'git merge' all non-zero exit codes mean the same kind of
>> failure?
>
> No, I'm saying that I don't think the test meant to try to distinguish
> between exit codes.

The check for status 1 comes from 72d1216 (New test case: merge with directory/file conflicts, 2005-12-03), and if you read git-merge.sh from that era, you would notice that at least we do not want to see status 2 (merge wrapper failed in a bad way) from this test. git-merge was designed to exit with status 1 when (and only when) the merge request was reasonable, it did its best effort and left conflict to be resolved in the working tree.

Elijah Newren· Sep 22, 2010, 04:41 UTC · re: Junio C Hamano · lore

Re: [PATCH 09/37] t6020: Modernize style a bit

On Tue, Sep 21, 2010 at 7:44 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 16 quoted lines
> Elijah Newren <newren@gmail.com> writes:
>
>>> The old version requested a particular kind of failure. Are you saying
>>> that with modern 'git merge' all non-zero exit codes mean the same kind of
>>> failure?
>>
>> No, I'm saying that I don't think the test meant to try to distinguish
>> between exit codes.
>
> The check for status 1 comes from 72d1216 (New test case: merge with
> directory/file conflicts, 2005-12-03), and if you read git-merge.sh from
> that era, you would notice that at least we do not want to see status 2
> (merge wrapper failed in a bad way) from this test.  git-merge was
> designed to exit with status 1 when (and only when) the merge request was
> reasonable, it did its best effort and left conflict to be resolved in the
> working tree.
Indeed; looks like I didn't check closely enough.
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 10/37] t6020: Add a testcase for modify/delete + directory/file conflict

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6020-merge-df.sh |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 47 insertions(+), 0 deletions(-)
Show changes to t/t6020-merge-df.sh +47 −0
diff --git a/t/t6020-merge-df.sh b/t/t6020-merge-df.sh
index 8662207..bc9db1a 100755
--- a/t/t6020-merge-df.sh
+++ b/t/t6020-merge-df.sh
@@ -50,4 +50,51 @@ test_expect_success 'F/D conflict' '
 	git merge master
 '
 
+test_expect_success 'setup modify/delete + directory/file conflict' '
+	git checkout --orphan modify &&
+	git rm -rf . &&
+	git clean -fdqx &&
+
+	printf "a\nb\nc\nd\ne\nf\ng\nh\n" >letters &&
+	git add letters &&
+	git commit -m initial &&
+
+	echo i >>letters &&
+	git add letters &&
+	git commit -m modified &&
+
+	git checkout -b delete HEAD^ &&
+	git rm letters &&
+	mkdir letters &&
+	>letters/file &&
+	git add letters &&
+	git commit -m deleted
+'
+
+test_expect_failure 'modify/delete + directory/file conflict' '
+	git checkout delete^0 &&
+	test_must_fail git merge modify &&
+
+	test 3 = $(git ls-files -s | wc -l) &&
+	test 2 = $(git ls-files -u | wc -l) &&
+	test 1 = $(git ls-files -o | wc -l) &&
+
+	test -f letters/file &&
+	test -f letters~modify
+'
+
+test_expect_failure 'modify/delete + directory/file conflict; other way' '
+	git reset --hard &&
+	git clean -f &&
+	git checkout modify^0 &&
+	test_must_fail git merge delete &&
+
+	test 3 = $(git ls-files -s | wc -l) &&
+	test 2 = $(git ls-files -u | wc -l) &&
+	test 1 = $(git ls-files -o | wc -l) &&
+
+	test -f letters/file &&
+	test -f letters~HEAD
+'
+
 test_done
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 11/37] t6036: Test index and worktree state, not just that merge fails

c94736a (merge-recursive: don't segfault while handling rename clashes 2009-07-30) added this testcase with an interesting corner case test, which previously had cased git to segfault. This test ensures that the segfault does not return and that the merge correctly fails; just add some checks that verify the state of the index and worktree after the merge are correct.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6036-recursive-corner-cases.sh |   24 +++++++++++++++++++++---
 1 files changed, 21 insertions(+), 3 deletions(-)
Show changes to t/t6036-recursive-corner-cases.sh +21 −3
diff --git a/t/t6036-recursive-corner-cases.sh b/t/t6036-recursive-corner-cases.sh
index b874141..1755073 100755
--- a/t/t6036-recursive-corner-cases.sh
+++ b/t/t6036-recursive-corner-cases.sh
@@ -14,7 +14,7 @@ test_description='recursive merge corner cases'
 #  R1  R2
 #
 
-test_expect_success setup '
+test_expect_success 'setup basic criss-cross + rename with no modifications' '
 	ten="0 1 2 3 4 5 6 7 8 9"
 	for i in $ten
 	do
@@ -45,11 +45,29 @@ test_expect_success setup '
 	git tag R2
 '
 
-test_expect_success merge '
+test_expect_success 'merge simple rename+criss-cross with no modifications' '
 	git reset --hard &&
 	git checkout L2^0 &&
 
-	test_must_fail git merge -s recursive R2^0
+	test_must_fail git merge -s recursive R2^0 &&
+
+	test 5 = $(git ls-files -s | wc -l) &&
+	test 3 = $(git ls-files -u | wc -l) &&
+	test 0 = $(git ls-files -o | wc -l) &&
+
+	test $(git rev-parse :0:one) = $(git rev-parse L2:one) &&
+	test $(git rev-parse :0:two) = $(git rev-parse R2:two) &&
+	test $(git rev-parse :2:three) = $(git rev-parse L2:three) &&
+	test $(git rev-parse :3:three) = $(git rev-parse R2:three) &&
+
+	cp two merged &&
+	>empty &&
+	test_must_fail git merge-file \
+		-L "Temporary merge branch 2" \
+		-L "" \
+		-L "Temporary merge branch 1" \
+		merged empty one &&
+	test $(git rev-parse :1:three) = $(git hash-object merged)
 '
 
 test_done
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 12/37] t6036: Add a second testcase similar to the first but with content changes

c94736a (merge-recursive: don't segfault while handling rename clashes 2009-07-30) added t6036 with a testcase that involved dual renames and a criss-cross merge. Add a test that is nearly identical, but which also involves content modification -- a case git currently does not merge correctly.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6036-recursive-corner-cases.sh |   76 +++++++++++++++++++++++++++++++++++++
 1 files changed, 76 insertions(+), 0 deletions(-)
Show changes to t/t6036-recursive-corner-cases.sh +76 −0
diff --git a/t/t6036-recursive-corner-cases.sh b/t/t6036-recursive-corner-cases.sh
index 1755073..9206c22 100755
--- a/t/t6036-recursive-corner-cases.sh
+++ b/t/t6036-recursive-corner-cases.sh
@@ -70,4 +70,80 @@ test_expect_success 'merge simple rename+criss-cross with no modifications' '
 	test $(git rev-parse :1:three) = $(git hash-object merged)
 '
 
+#
+# Same as before, but modify L1 slightly:
+#
+#  L1m L2
+#   o---o
+#  / \ / \
+# o   X   ?
+#  \ / \ /
+#   o---o
+#  R1  R2
+#
+
+test_expect_success 'setup criss-cross + rename merges with basic modification' '
+	git rm -rf . &&
+	git clean -fdqx &&
+	rm -rf .git &&
+	git init &&
+
+	ten="0 1 2 3 4 5 6 7 8 9"
+	for i in $ten
+	do
+		echo line $i in a sample file
+	done >one &&
+	for i in $ten
+	do
+		echo line $i in another sample file
+	done >two &&
+	git add one two &&
+	test_tick && git commit -m initial &&
+
+	git branch L1 &&
+	git checkout -b R1 &&
+	git mv one three &&
+	echo more >>two &&
+	git add two &&
+	test_tick && git commit -m R1 &&
+
+	git checkout L1 &&
+	git mv two three &&
+	test_tick && git commit -m L1 &&
+
+	git checkout L1^0 &&
+	test_tick && git merge -s ours R1 &&
+	git tag L2 &&
+
+	git checkout R1^0 &&
+	test_tick && git merge -s ours L1 &&
+	git tag R2
+'
+
+test_expect_failure 'merge criss-cross + rename merges with basic modification' '
+	git reset --hard &&
+	git checkout L2^0 &&
+
+	test_must_fail git merge -s recursive R2^0 &&
+
+	test 5 = $(git ls-files -s | wc -l) &&
+	test 3 = $(git ls-files -u | wc -l) &&
+	test 0 = $(git ls-files -o | wc -l) &&
+
+	test $(git rev-parse :0:one) = $(git rev-parse L2:one) &&
+	test $(git rev-parse :0:two) = $(git rev-parse R2:two) &&
+	test $(git rev-parse :2:three) = $(git rev-parse L2:three) &&
+	test $(git rev-parse :3:three) = $(git rev-parse R2:three) &&
+
+	head -n 10 two >merged &&
+	cp one merge-me &&
+	>empty &&
+	test_must_fail git merge-file \
+		-L "Temporary merge branch 2" \
+		-L "" \
+		-L "Temporary merge branch 1" \
+		merged empty merge-me &&
+	test $(git rev-parse :1:three) = $(git hash-object merged)
+'
+
 test_done
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 13/37] t6036: Add testcase for undetected conflict

If merging two lines of development involves a rename/add conflict, and two different people make such a merge but resolve it differently, and then someone tries to merge the resulting two merges, then they should clearly get a conflict due to the different resolutions from the previous developers. However, in some such cases the conflict would not be detected and git would silently accept one of the two versions being merged as the final merge resolution.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6036-recursive-corner-cases.sh |   85 +++++++++++++++++++++++++++++++++++++
 1 files changed, 85 insertions(+), 0 deletions(-)
Show changes to t/t6036-recursive-corner-cases.sh +85 −0
diff --git a/t/t6036-recursive-corner-cases.sh b/t/t6036-recursive-corner-cases.sh
index 9206c22..6c2b2bf 100755
--- a/t/t6036-recursive-corner-cases.sh
+++ b/t/t6036-recursive-corner-cases.sh
@@ -146,4 +146,89 @@ test_expect_failure 'merge criss-cross + rename merges with basic modification'
 	test $(git rev-parse :1:three) = $(git hash-object merged)
 '
 
+#
+# For the next test, we start with three commits in two lines of development
+# which setup a rename/add conflict:
+#   Commit A: File 'a' exists
+#   Commit B: Rename 'a' -> 'new_a'
+#   Commit C: Modify 'a', create different 'new_a'
+# Later, two different people merge and resolve differently:
+#   Commit D: Merge B & C, ignoring separately created 'new_a'
+#   Commit E: Merge B & C making use of some piece of secondary 'new_a'
+# Finally, someone goes to merge D & E.  Does git detect the conflict?
+#
+#      B   D
+#      o---o
+#     / \ / \
+#  A o   X   ? F
+#     \ / \ /
+#      o---o
+#      C   E
+#
+
+test_expect_success 'setup differently handled merges of rename/add conflict' '
+	git rm -rf . &&
+	git clean -fdqx &&
+	rm -rf .git &&
+	git init &&
+
+	printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
+	git add a &&
+	test_tick && git commit -m A &&
+
+	git branch B &&
+	git checkout -b C &&
+	echo 10 >>a &&
+	echo "other content" >>new_a &&
+	git add a new_a &&
+	test_tick && git commit -m C &&
+
+	git checkout B &&
+	git mv a new_a &&
+	test_tick && git commit -m B &&
+
+	git checkout B^0 &&
+	test_must_fail git merge C &&
+	git clean -f &&
+	test_tick && git commit -m D &&
+	git tag D &&
+
+	git checkout C^0 &&
+	test_must_fail git merge B &&
+	rm new_a~HEAD new_a &&
+	printf "Incorrectly merged content" >>new_a &&
+	git add -u &&
+	test_tick && git commit -m E &&
+	git tag E
+'
+
+test_expect_failure 'git detects differently handled merges conflict' '
+	git reset --hard &&
+	git checkout D^0 &&
+
+	git merge -s recursive E^0 && {
+		echo "BAD: should have conflicted"
+		test "Incorrectly merged content" = "$(cat new_a)" &&
+			echo "BAD: Silently accepted wrong content"
+		return 1
+	}
+
+	test 3 = $(git ls-files -s | wc -l) &&
+	test 3 = $(git ls-files -u | wc -l) &&
+	test 0 = $(git ls-files -o | wc -l) &&
+
+	test $(git rev-parse :2:new_a) = $(git rev-parse D:new_a) &&
+	test $(git rev-parse :3:new_a) = $(git rev-parse E:new_a) &&
+
+	git cat-file -p B:new_a >>merged &&
+	git cat-file -p C:new_a >>merge-me &&
+	>empty &&
+	test_must_fail git merge-file \
+		-L "Temporary merge branch 2" \
+		-L "" \
+		-L "Temporary merge branch 1" \
+		merged empty merge-me &&
+	test $(git rev-parse :1:new_a) = $(git hash-object merged)
+'
+
 test_done
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 14/37] merge-recursive: Small code clarification -- variable name and comments

process_renames() had a variable named "stage" and derived variables src_other and dst_other whose purpose was not immediately obvious; also, I want to extend the scope of this variable and use it later, so it should have a more descriptive name. Do so, and add a brief comment explaining how it is used and what it relates to.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   20 ++++++++++++++------
 1 files changed, 14 insertions(+), 6 deletions(-)
Show changes to merge-recursive.c +14 −6
diff --git a/merge-recursive.c b/merge-recursive.c
index 4d9c165..8f45cec 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -925,15 +925,23 @@ static int process_renames(struct merge_options *o,
 			struct string_list_item *item;
 			/* we only use sha1 and mode of these */
 			struct diff_filespec src_other, dst_other;
-			int try_merge, stage = a_renames == renames1 ? 3: 2;
+			int try_merge;
 
-			remove_file(o, 1, ren1_src, o->call_depth || stage == 3);
+			/*
+			 * unpack_trees loads entries from common-commit
+			 * into stage 1, from head-commit into stage 2, and
+			 * from merge-commit into stage 3.  We keep track
+			 * of which side corresponds to the rename.
+			 */
+			int renamed_stage = a_renames == renames1 ? 2 : 3;
+			int other_stage =   a_renames == renames1 ? 3 : 2;
 
-			hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
-			src_other.mode = ren1->src_entry->stages[stage].mode;
-			hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
-			dst_other.mode = ren1->dst_entry->stages[stage].mode;
+			remove_file(o, 1, ren1_src, o->call_depth || renamed_stage == 2);
 
+			hashcpy(src_other.sha1, ren1->src_entry->stages[other_stage].sha);
+			src_other.mode = ren1->src_entry->stages[other_stage].mode;
+			hashcpy(dst_other.sha1, ren1->dst_entry->stages[other_stage].sha);
+			dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
 			try_merge = 0;
 
 			if (sha_eq(src_other.sha1, null_sha1)) {
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 15/37] merge-recursive: Rename conflict_rename_rename*() for clarity

The names conflict_rename_rename and conflict_rename_rename_2 did not make it clear what they were handling. Since the first of these handles one file being renamed in both branches to different files, while the latter handles two different files being renamed to the same thing, add a little '1to2' and '2to1' suffix on these and an explanatory comment to make their intent clearer.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   26 ++++++++++++++------------
 1 files changed, 14 insertions(+), 12 deletions(-)
Show changes to merge-recursive.c +14 −12
diff --git a/merge-recursive.c b/merge-recursive.c
index 8f45cec..cc1ab92 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -731,12 +731,13 @@ static struct merge_file_info merge_file(struct merge_options *o,
 	return result;
 }
 
-static void conflict_rename_rename(struct merge_options *o,
-				   struct rename *ren1,
-				   const char *branch1,
-				   struct rename *ren2,
-				   const char *branch2)
+static void conflict_rename_rename_1to2(struct merge_options *o,
+					struct rename *ren1,
+					const char *branch1,
+					struct rename *ren2,
+					const char *branch2)
 {
+	/* One file was renamed in both branches, but to different names. */
 	char *del[2];
 	int delp = 0;
 	const char *ren1_dst = ren1->pair->two->path;
@@ -783,12 +784,13 @@ static void conflict_rename_dir(struct merge_options *o,
 	free(new_path);
 }
 
-static void conflict_rename_rename_2(struct merge_options *o,
-				     struct rename *ren1,
-				     const char *branch1,
-				     struct rename *ren2,
-				     const char *branch2)
+static void conflict_rename_rename_2to1(struct merge_options *o,
+					struct rename *ren1,
+					const char *branch1,
+					struct rename *ren2,
+					const char *branch2)
 {
+	/* Two files were renamed to the same thing. */
 	char *new_path1 = unique_path(o, ren1->pair->two->path, branch1);
 	char *new_path2 = unique_path(o, ren2->pair->two->path, branch2);
 	output(o, 1, "Renaming %s to %s and %s to %s instead",
@@ -890,7 +892,7 @@ static int process_renames(struct merge_options *o,
 					update_file(o, 0, ren1->pair->one->sha1,
 						    ren1->pair->one->mode, src);
 				}
-				conflict_rename_rename(o, ren1, branch1, ren2, branch2);
+				conflict_rename_rename_1to2(o, ren1, branch1, ren2, branch2);
 			} else {
 				struct merge_file_info mfi;
 				remove_file(o, 1, ren1_src, 1);
@@ -1005,7 +1007,7 @@ static int process_renames(struct merge_options *o,
 				       "Rename %s->%s in %s",
 				       ren1_src, ren1_dst, branch1,
 				       ren2->pair->one->path, ren2->pair->two->path, branch2);
-				conflict_rename_rename_2(o, ren1, branch1, ren2, branch2);
+				conflict_rename_rename_2to1(o, ren1, branch1, ren2, branch2);
 			} else
 				try_merge = 1;
 
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 16/37] merge-recursive: Nuke rename/directory conflict detection

Since we want to resolve merges in-core and then detect at the end whether D/F conflicts remain in the way, we should just apply renames in-core and let logic elsewhere check for D/F conflicts.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   18 ------------------
 1 files changed, 0 insertions(+), 18 deletions(-)
Show changes to merge-recursive.c +0 −18
diff --git a/merge-recursive.c b/merge-recursive.c
index cc1ab92..f7591a3 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -773,17 +773,6 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
 		free(del[delp]);
 }
 
-static void conflict_rename_dir(struct merge_options *o,
-				struct rename *ren1,
-				const char *branch1)
-{
-	char *new_path = unique_path(o, ren1->pair->two->path, branch1);
-	output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path);
-	remove_file(o, 0, ren1->pair->two->path, 0);
-	update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
-	free(new_path);
-}
-
 static void conflict_rename_rename_2to1(struct merge_options *o,
 					struct rename *ren1,
 					const char *branch1,
@@ -1044,13 +1033,6 @@ static int process_renames(struct merge_options *o,
 					if (!ren1->dst_entry->stages[2].mode !=
 					    !ren1->dst_entry->stages[3].mode)
 						ren1->dst_entry->processed = 0;
-				} else if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
-					clean_merge = 0;
-					output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s "
-					       " directory %s added in %s",
-					       ren1_src, ren1_dst, branch1,
-					       ren1_dst, branch2);
-					conflict_rename_dir(o, ren1, branch1);
 				} else {
 					if (mfi.merge || !mfi.clean)
 						output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 17/37] merge-recursive: Move rename/delete handling into dedicated function

This move is in preparation for the function growing and being called from multiple places in order to handle D/F conflicts.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   31 ++++++++++++++++++++-----------
 1 files changed, 20 insertions(+), 11 deletions(-)
Show changes to merge-recursive.c +20 −11
diff --git a/merge-recursive.c b/merge-recursive.c
index f7591a3..87be24c 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -731,6 +731,25 @@ static struct merge_file_info merge_file(struct merge_options *o,
 	return result;
 }
 
+static void conflict_rename_delete(struct merge_options *o,
+				   struct diff_filepair *pair,
+				   const char *rename_branch,
+				   const char *other_branch)
+{
+	char *dest_name = pair->two->path;
+
+	output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
+	       "and deleted in %s",
+	       pair->one->path, pair->two->path, rename_branch,
+	       other_branch);
+	if (!o->call_depth)
+		update_stages(dest_name, NULL,
+			      rename_branch == o->branch1 ? pair->two : NULL,
+			      rename_branch == o->branch1 ? NULL : pair->two,
+			      1);
+	update_file(o, 0, pair->two->sha1, pair->two->mode, dest_name);
+}
+
 static void conflict_rename_rename_1to2(struct merge_options *o,
 					struct rename *ren1,
 					const char *branch1,
@@ -937,17 +956,7 @@ static int process_renames(struct merge_options *o,
 
 			if (sha_eq(src_other.sha1, null_sha1)) {
 				clean_merge = 0;
-				output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
-				       "and deleted in %s",
-				       ren1_src, ren1_dst, branch1,
-				       branch2);
-				update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
-				if (!o->call_depth)
-					update_stages(ren1_dst, NULL,
-							branch1 == o->branch1 ?
-							ren1->pair->two : NULL,
-							branch1 == o->branch1 ?
-							NULL : ren1->pair->two, 1);
+				conflict_rename_delete(o, ren1->pair, branch1, branch2);
 			} else if ((dst_other.mode == ren1->pair->two->mode) &&
 				   sha_eq(dst_other.sha1, ren1->pair->two->sha1)) {
 				/* Added file on the other side
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 18/37] merge-recursive: Move delete/modify handling into dedicated function

This move is in preparation for the function being called from multiple places in order to handle D/F conflicts.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   35 ++++++++++++++++++++++-------------
 1 files changed, 22 insertions(+), 13 deletions(-)
Show changes to merge-recursive.c +22 −13
diff --git a/merge-recursive.c b/merge-recursive.c
index 87be24c..a6da2cc 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1119,6 +1119,26 @@ error_return:
 	return ret;
 }
 
+static void handle_delete_modify(struct merge_options *o,
+				 const char *path,
+				 unsigned char *a_sha, int a_mode,
+				 unsigned char *b_sha, int b_mode)
+{
+	if (!a_sha) {
+		output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
+		       "and modified in %s. Version %s of %s left in tree.",
+		       path, o->branch1,
+		       o->branch2, o->branch2, path);
+		update_file(o, 0, b_sha, b_mode, path);
+	} else {
+		output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
+		       "and modified in %s. Version %s of %s left in tree.",
+		       path, o->branch2,
+		       o->branch1, o->branch1, path);
+		update_file(o, 0, a_sha, a_mode, path);
+	}
+}
+
 /* Per entry merge function */
 static int process_entry(struct merge_options *o,
 			 const char *path, struct stage_data *entry)
@@ -1151,19 +1171,8 @@ static int process_entry(struct merge_options *o,
 		} else {
 			/* Deleted in one and changed in the other */
 			clean_merge = 0;
-			if (!a_sha) {
-				output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
-				       "and modified in %s. Version %s of %s left in tree.",
-				       path, o->branch1,
-				       o->branch2, o->branch2, path);
-				update_file(o, 0, b_sha, b_mode, path);
-			} else {
-				output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
-				       "and modified in %s. Version %s of %s left in tree.",
-				       path, o->branch2,
-				       o->branch1, o->branch1, path);
-				update_file(o, 0, a_sha, a_mode, path);
-			}
+			handle_delete_modify(o, path,
+					     a_sha, a_mode, b_sha, b_mode);
 		}
 
 	} else if ((!o_sha && a_sha && !b_sha) ||
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 19/37] merge-recursive: Move process_entry's content merging into a function

This move is in preparation for merge_content growing and being called from multiple places in order to handle D/F conflicts.

I also snuck in a small change to the output in the case that the merged content for the file matches the current file contents, to make it better match (and thus more able to take over) how other merge_file() calls in process_renames() are handled.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   71 ++++++++++++++++++++++++++++++++---------------------
 1 files changed, 43 insertions(+), 28 deletions(-)
Show changes to merge-recursive.c +43 −28
diff --git a/merge-recursive.c b/merge-recursive.c
index a6da2cc..1f8b2d5 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1139,6 +1139,47 @@ static void handle_delete_modify(struct merge_options *o,
 	}
 }
 
+
+static int merge_content(struct merge_options *o,
+			 const char *path,
+			 unsigned char *o_sha, int o_mode,
+			 unsigned char *a_sha, int a_mode,
+			 unsigned char *b_sha, int b_mode)
+{
+	const char *reason = "content";
+	struct merge_file_info mfi;
+	struct diff_filespec one, a, b;
+
+	if (!o_sha) {
+		reason = "add/add";
+		o_sha = (unsigned char *)null_sha1;
+	}
+	one.path = a.path = b.path = (char *)path;
+	hashcpy(one.sha1, o_sha);
+	one.mode = o_mode;
+	hashcpy(a.sha1, a_sha);
+	a.mode = a_mode;
+	hashcpy(b.sha1, b_sha);
+	b.mode = b_mode;
+
+	mfi = merge_file(o, &one, &a, &b, o->branch1, o->branch2);
+	if (mfi.clean && sha_eq(mfi.sha, a_sha) && mfi.mode == a.mode)
+		output(o, 3, "Skipped %s (merged same as existing)", path);
+	else
+		output(o, 2, "Auto-merging %s", path);
+
+	if (!mfi.clean) {
+		if (S_ISGITLINK(mfi.mode))
+			reason = "submodule";
+		output(o, 1, "CONFLICT (%s): Merge conflict in %s",
+				reason, path);
+	}
+
+	update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
+	return mfi.clean;
+
+}
+
 /* Per entry merge function */
 static int process_entry(struct merge_options *o,
 			 const char *path, struct stage_data *entry)
@@ -1207,34 +1248,8 @@ static int process_entry(struct merge_options *o,
 	} else if (a_sha && b_sha) {
 		/* Case C: Added in both (check for same permissions) and */
 		/* case D: Modified in both, but differently. */
-		const char *reason = "content";
-		struct merge_file_info mfi;
-		struct diff_filespec one, a, b;
-
-		if (!o_sha) {
-			reason = "add/add";
-			o_sha = (unsigned char *)null_sha1;
-		}
-		output(o, 2, "Auto-merging %s", path);
-		one.path = a.path = b.path = (char *)path;
-		hashcpy(one.sha1, o_sha);
-		one.mode = o_mode;
-		hashcpy(a.sha1, a_sha);
-		a.mode = a_mode;
-		hashcpy(b.sha1, b_sha);
-		b.mode = b_mode;
-
-		mfi = merge_file(o, &one, &a, &b,
-				 o->branch1, o->branch2);
-
-		clean_merge = mfi.clean;
-		if (!mfi.clean) {
-			if (S_ISGITLINK(mfi.mode))
-				reason = "submodule";
-			output(o, 1, "CONFLICT (%s): Merge conflict in %s",
-					reason, path);
-		}
-		update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
+		clean_merge = merge_content(o, path,
+					    o_sha, o_mode, a_sha, a_mode, b_sha, b_mode);
 	} else if (!o_sha && !a_sha && !b_sha) {
 		/*
 		 * this entry was deleted altogether. a_mode == 0 means
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 20/37] merge-recursive: New data structures for deferring of D/F conflicts

Since we need to resolve paths (including renames) in-core first and defer checking of D/F conflicts (namely waiting to see if directories are still in the way after all paths are resolved) before updating files involved in D/F conflicts, we will need to first process_renames, then record some information about the rename needed at D/F resolution time, and then make use of that information when resolving D/F conflicts at the end.

This commit adds some relevant data structures for storing the necessary information.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 45 insertions(+), 0 deletions(-)
Show changes to merge-recursive.c +45 −0
diff --git a/merge-recursive.c b/merge-recursive.c
index 1f8b2d5..f15291e 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -63,6 +63,23 @@ static int sha_eq(const unsigned char *a, const unsigned char *b)
 	return a && b && hashcmp(a, b) == 0;
 }
 
+enum rename_type {
+	RENAME_NORMAL = 0,
+	RENAME_DELETE,
+	RENAME_ONE_FILE_TO_TWO
+};
+
+struct rename_df_conflict_info
+{
+	enum rename_type rename_type;
+	struct diff_filepair *pair1;
+	struct diff_filepair *pair2;
+	const char *branch1;
+	const char *branch2;
+	struct stage_data *dst_entry1;
+	struct stage_data *dst_entry2;
+};
+
 /*
  * Since we want to write the index eventually, we cannot reuse the index
  * for these (temporary) data.
@@ -74,9 +91,37 @@ struct stage_data
 		unsigned mode;
 		unsigned char sha[20];
 	} stages[4];
+	struct rename_df_conflict_info *rename_df_conflict_info;
 	unsigned processed:1;
 };
 
+static inline void setup_rename_df_conflict_info(enum rename_type rename_type,
+						 struct diff_filepair *pair1,
+						 struct diff_filepair *pair2,
+						 const char *branch1,
+						 const char *branch2,
+						 struct stage_data *dst_entry1,
+						 struct stage_data *dst_entry2)
+{
+	struct rename_df_conflict_info *ci = xcalloc(1, sizeof(struct rename_df_conflict_info));
+	ci->rename_type = rename_type;
+	ci->pair1 = pair1;
+	ci->branch1 = branch1;
+	ci->branch2 = branch2;
+
+	ci->dst_entry1 = dst_entry1;
+	dst_entry1->rename_df_conflict_info = ci;
+	dst_entry1->processed = 0;
+
+	assert(!pair2 == !dst_entry2);
+	if (dst_entry2) {
+		ci->dst_entry2 = dst_entry2;
+		ci->pair2 = pair2;
+		dst_entry2->rename_df_conflict_info = ci;
+		dst_entry2->processed = 0;
+	}
+}
+
 static int show(struct merge_options *o, int v)
 {
 	return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 21/37] merge-recursive: New function to assist resolving renames in-core only

process_renames() and process_entry() have nearly identical code for doing three-way file merging to resolve content changes. Since we are already deferring some of the current rename handling in order to better handle D/F conflicts, it seems to make sense to defer content merging as well and remove the (nearly) duplicated code sections for handling this merging.

To facilitate this process, add a new update_stages_and_entry() function which will map the higher stage index entries from two files involved in a rename into the resulting rename destination's index entries, and update the associated stage_data structure.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   31 ++++++++++++++++++++++++++++---
 1 files changed, 28 insertions(+), 3 deletions(-)
Show changes to merge-recursive.c +28 −3
diff --git a/merge-recursive.c b/merge-recursive.c
index f15291e..ec47d56 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -418,11 +418,10 @@ static struct string_list *get_renames(struct merge_options *o,
 	return renames;
 }
 
-static int update_stages(const char *path, struct diff_filespec *o,
+static int update_stages_options(const char *path, struct diff_filespec *o,
 			 struct diff_filespec *a, struct diff_filespec *b,
-			 int clear)
+			 int clear, int options)
 {
-	int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
 	if (clear)
 		if (remove_file_from_cache(path))
 			return -1;
@@ -438,6 +437,32 @@ static int update_stages(const char *path, struct diff_filespec *o,
 	return 0;
 }
 
+static int update_stages(const char *path, struct diff_filespec *o,
+			 struct diff_filespec *a, struct diff_filespec *b,
+			 int clear)
+{
+	int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
+	return update_stages_options(path, o, a, b, clear, options);
+}
+
+static int update_stages_and_entry(const char *path,
+				   struct stage_data *entry,
+				   struct diff_filespec *o,
+				   struct diff_filespec *a,
+				   struct diff_filespec *b,
+				   int clear)
+{
+	entry->processed = 0;
+	entry->stages[1].mode = o->mode;
+	entry->stages[2].mode = a->mode;
+	entry->stages[3].mode = b->mode;
+	hashcpy(entry->stages[1].sha, o->sha1);
+	hashcpy(entry->stages[2].sha, a->sha1);
+	hashcpy(entry->stages[3].sha, b->sha1);
+	int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
+	return update_stages_options(path, o, a, b, clear, options);
+}
+
 static int remove_file(struct merge_options *o, int clean,
 		       const char *path, int no_wd)
 {
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 22/37] merge-recursive: Have process_entry() skip D/F or rename entries

If an entry has an associated rename_df_conflict_info, skip it and allow it to be processed by process_df_entry().

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
Show changes to merge-recursive.c +3 −0
diff --git a/merge-recursive.c b/merge-recursive.c
index ec47d56..5326f5e 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1267,6 +1267,9 @@ static int process_entry(struct merge_options *o,
 	unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
 	unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
 
+	if (entry->rename_df_conflict_info)
+		return 1; /* Such cases are handled elsewhere. */
+
 	entry->processed = 1;
 	if (o_sha && (!a_sha || !b_sha)) {
 		/* Case A: Deleted in one */
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 23/37] merge-recursive: Structure process_df_entry() to handle more cases

Modify process_df_entry() (mostly just indentation level changes) to get it ready for handling more D/F conflict type cases.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   83 ++++++++++++++++++++++++++++++-----------------------
 1 files changed, 47 insertions(+), 36 deletions(-)
Show changes to merge-recursive.c +47 −36
diff --git a/merge-recursive.c b/merge-recursive.c
index 5326f5e..b8222ad 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1336,13 +1336,19 @@ static int process_entry(struct merge_options *o,
 }
 
 /*
- * Per entry merge function for D/F conflicts, to be called only after
- * all files below dir have been processed.  We do this because in the
- * cases we can cleanly resolve D/F conflicts, process_entry() can clean
- * out all the files below the directory for us.
+ * Per entry merge function for D/F (and/or rename) conflicts.  In the
+ * cases we can cleanly resolve D/F conflicts, process_entry() can
+ * clean out all the files below the directory for us.  All D/F
+ * conflict cases must be handled here at the end to make sure any
+ * directories that can be cleaned out, are.
+ *
+ * Some rename conflicts may also be handled here that don't necessarily
+ * involve D/F conflicts, since the code to handle them is generic enough
+ * to handle those rename conflicts with or without D/F conflicts also
+ * being involved.
  */
 static int process_df_entry(struct merge_options *o,
-			 const char *path, struct stage_data *entry)
+			    const char *path, struct stage_data *entry)
 {
 	int clean_merge = 1;
 	unsigned o_mode = entry->stages[1].mode;
@@ -1351,42 +1357,47 @@ static int process_df_entry(struct merge_options *o,
 	unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
 	unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
 	unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
-	const char *add_branch;
-	const char *other_branch;
-	unsigned mode;
-	const unsigned char *sha;
-	const char *conf;
 	struct stat st;
 
-	if (! ((!o_sha && a_sha && !b_sha) || (!o_sha && !a_sha && b_sha)))
-		return 1; /* we don't handle non D-F cases */
-
 	entry->processed = 1;
+	if (entry->rename_df_conflict_info) {
+		die ("Not yet implemented.");
+	} else if (!o_sha && !!a_sha != !!b_sha) {
+		/* directory -> (directory, file) */
+		const char *add_branch;
+		const char *other_branch;
+		unsigned mode;
+		const unsigned char *sha;
+		const char *conf;
 
-	if (a_sha) {
-		add_branch = o->branch1;
-		other_branch = o->branch2;
-		mode = a_mode;
-		sha = a_sha;
-		conf = "file/directory";
-	} else {
-		add_branch = o->branch2;
-		other_branch = o->branch1;
-		mode = b_mode;
-		sha = b_sha;
-		conf = "directory/file";
-	}
-	if (lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
-		const char *new_path = unique_path(o, path, add_branch);
-		clean_merge = 0;
-		output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
-		       "Adding %s as %s",
-		       conf, path, other_branch, path, new_path);
-		remove_file(o, 0, path, 0);
-		update_file(o, 0, sha, mode, new_path);
+		if (a_sha) {
+			add_branch = o->branch1;
+			other_branch = o->branch2;
+			mode = a_mode;
+			sha = a_sha;
+			conf = "file/directory";
+		} else {
+			add_branch = o->branch2;
+			other_branch = o->branch1;
+			mode = b_mode;
+			sha = b_sha;
+			conf = "directory/file";
+		}
+		if (lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
+			const char *new_path = unique_path(o, path, add_branch);
+			clean_merge = 0;
+			output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
+			       "Adding %s as %s",
+			       conf, path, other_branch, path, new_path);
+			remove_file(o, 0, path, 0);
+			update_file(o, 0, sha, mode, new_path);
+		} else {
+			output(o, 2, "Adding %s", path);
+			update_file(o, 1, sha, mode, path);
+		}
 	} else {
-		output(o, 2, "Adding %s", path);
-		update_file(o, 1, sha, mode, path);
+		entry->processed = 0;
+		return 1; /* not handled; assume clean until processed */
 	}
 
 	return clean_merge;
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 24/37] merge-recursive: Update conflict_rename_rename_1to2() call signature

To facilitate having this function called later using information stored in a rename_df_conflict_info struct, accept a diff_filepair instead of a rename.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)
Show changes to merge-recursive.c +9 −9
diff --git a/merge-recursive.c b/merge-recursive.c
index b8222ad..237d1ac 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -821,16 +821,16 @@ static void conflict_rename_delete(struct merge_options *o,
 }
 
 static void conflict_rename_rename_1to2(struct merge_options *o,
-					struct rename *ren1,
+					struct diff_filepair *pair1,
 					const char *branch1,
-					struct rename *ren2,
+					struct diff_filepair *pair2,
 					const char *branch2)
 {
 	/* One file was renamed in both branches, but to different names. */
 	char *del[2];
 	int delp = 0;
-	const char *ren1_dst = ren1->pair->two->path;
-	const char *ren2_dst = ren2->pair->two->path;
+	const char *ren1_dst = pair1->two->path;
+	const char *ren2_dst = pair2->two->path;
 	const char *dst_name1 = ren1_dst;
 	const char *dst_name2 = ren2_dst;
 	if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
@@ -851,12 +851,12 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
 		/*
 		 * Uncomment to leave the conflicting names in the resulting tree
 		 *
-		 * update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
-		 * update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
+		 * update_file(o, 0, pair1->two->sha1, pair1->two->mode, dst_name1);
+		 * update_file(o, 0, pair2->two->sha1, pair2->two->mode, dst_name2);
 		 */
 	} else {
-		update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
-		update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
+		update_stages(dst_name1, NULL, pair1->two, NULL, 1);
+		update_stages(dst_name2, NULL, NULL, pair2->two, 1);
 	}
 	while (delp--)
 		free(del[delp]);
@@ -970,7 +970,7 @@ static int process_renames(struct merge_options *o,
 					update_file(o, 0, ren1->pair->one->sha1,
 						    ren1->pair->one->mode, src);
 				}
-				conflict_rename_rename_1to2(o, ren1, branch1, ren2, branch2);
+				conflict_rename_rename_1to2(o, ren1->pair, branch1, ren2->pair, branch2);
 			} else {
 				struct merge_file_info mfi;
 				remove_file(o, 1, ren1_src, 1);
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 25/37] merge-recursive: Update merge_content() call signature

Enable calling merge_content() and providing more information about renames and D/F conflicts (which we will want to do from process_df_entry()).

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
Show changes to merge-recursive.c +4 −2
diff --git a/merge-recursive.c b/merge-recursive.c
index 237d1ac..5f528c1 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1214,7 +1214,8 @@ static int merge_content(struct merge_options *o,
 			 const char *path,
 			 unsigned char *o_sha, int o_mode,
 			 unsigned char *a_sha, int a_mode,
-			 unsigned char *b_sha, int b_mode)
+			 unsigned char *b_sha, int b_mode,
+			 const char *df_rename_conflict_branch)
 {
 	const char *reason = "content";
 	struct merge_file_info mfi;
@@ -1322,7 +1323,8 @@ static int process_entry(struct merge_options *o,
 		/* Case C: Added in both (check for same permissions) and */
 		/* case D: Modified in both, but differently. */
 		clean_merge = merge_content(o, path,
-					    o_sha, o_mode, a_sha, a_mode, b_sha, b_mode);
+					    o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
+					    NULL);
 	} else if (!o_sha && !a_sha && !b_sha) {
 		/*
 		 * this entry was deleted altogether. a_mode == 0 means
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:28 UTC · re: Elijah Newren · lore

[PATCH 26/37] merge-recursive: Avoid doubly merging rename/add conflict contents

When a commit moves A to B while another commit created B (or moved C to B), and these two different commits serve as different merge-bases for a later merge, c94736a (merge-recursive: don't segfault while handling rename clashes 2009-07-30) added some special code to avoid segfaults. Since that commit, the two versions of B are merged in place (which could be potentially conflicting) and the intermediate result is used as the virtual ancestor.

However, right before this special merge, try_merge was turned on, meaning that process_renames() would try an alternative merge that ignores the 'add' part of the conflict, and, if the merge is clean, store that as the new virtual ancestor. This could cause incorrect merging of criss-cross merges; it would typically result in just recording a slightly confusing merge base, but in some cases it could cause silent acceptance of one side of a merge as the final resolution when a conflict should have been flagged.

When we do a special merge for such a rename/add conflict between merge-bases, turn try_merge off to avoid an inappropriate second merge.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c                 |    1 +
 t/t6036-recursive-corner-cases.sh |    4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)
Show changes to 2 files +3 −2

merge-recursive.c, t/t6036-recursive-corner-cases.sh

diff --git a/merge-recursive.c b/merge-recursive.c
index 5f528c1..178bbd8 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1061,6 +1061,7 @@ static int process_renames(struct merge_options *o,
 						    mfi.sha,
 						    mfi.mode,
 						    ren1_dst);
+					try_merge = 0;
 				} else {
 					new_path = unique_path(o, ren1_dst, branch2);
 					output(o, 1, "Adding as %s instead", new_path);
diff --git a/t/t6036-recursive-corner-cases.sh b/t/t6036-recursive-corner-cases.sh
index 6c2b2bf..a2e5c5c 100755
--- a/t/t6036-recursive-corner-cases.sh
+++ b/t/t6036-recursive-corner-cases.sh
@@ -120,7 +120,7 @@ test_expect_success 'setup criss-cross + rename merges with basic modification'
 	git tag R2
 '
 
-test_expect_failure 'merge criss-cross + rename merges with basic modification' '
+test_expect_success 'merge criss-cross + rename merges with basic modification' '
 	git reset --hard &&
 	git checkout L2^0 &&
 
@@ -202,7 +202,7 @@ test_expect_success 'setup differently handled merges of rename/add conflict' '
 	git tag E
 '
 
-test_expect_failure 'git detects differently handled merges conflict' '
+test_expect_success 'git detects differently handled merges conflict' '
 	git reset --hard &&
 	git checkout D^0 &&
 
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:29 UTC · re: Elijah Newren · lore

[PATCH 27/37] merge-recursive: Move handling of double rename of one file to two

Move the handling of rename/rename conflicts where one file is renamed to two different files, from process_renames() to process_df_entry().

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c       |   57 +++++++++++++++++++++++++++++++++-------------
 t/t6022-merge-rename.sh |    2 +-
 2 files changed, 42 insertions(+), 17 deletions(-)
Show changes to 2 files +42 −17

merge-recursive.c, t/t6022-merge-rename.sh

diff --git a/merge-recursive.c b/merge-recursive.c
index 178bbd8..e219d62 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -855,8 +855,11 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
 		 * update_file(o, 0, pair2->two->sha1, pair2->two->mode, dst_name2);
 		 */
 	} else {
-		update_stages(dst_name1, NULL, pair1->two, NULL, 1);
-		update_stages(dst_name2, NULL, NULL, pair2->two, 1);
+		update_stages(ren1_dst, NULL, pair1->two, NULL, 1);
+		update_stages(ren2_dst, NULL, NULL, pair2->two, 1);
+
+		update_file(o, 0, pair1->two->sha1, pair1->two->mode, dst_name1);
+		update_file(o, 0, pair2->two->sha1, pair2->two->mode, dst_name2);
 	}
 	while (delp--)
 		free(del[delp]);
@@ -958,19 +961,15 @@ static int process_renames(struct merge_options *o,
 			ren2->dst_entry->processed = 1;
 			ren2->processed = 1;
 			if (strcmp(ren1_dst, ren2_dst) != 0) {
-				clean_merge = 0;
-				output(o, 1, "CONFLICT (rename/rename): "
-				       "Rename \"%s\"->\"%s\" in branch \"%s\" "
-				       "rename \"%s\"->\"%s\" in \"%s\"%s",
-				       src, ren1_dst, branch1,
-				       src, ren2_dst, branch2,
-				       o->call_depth ? " (left unresolved)": "");
-				if (o->call_depth) {
-					remove_file_from_cache(src);
-					update_file(o, 0, ren1->pair->one->sha1,
-						    ren1->pair->one->mode, src);
-				}
-				conflict_rename_rename_1to2(o, ren1->pair, branch1, ren2->pair, branch2);
+				setup_rename_df_conflict_info(RENAME_ONE_FILE_TO_TWO,
+							      ren1->pair,
+							      ren2->pair,
+							      branch1,
+							      branch2,
+							      ren1->dst_entry,
+							      ren2->dst_entry);
+				remove_file(o, 0, ren1_dst, 0);
+				/* ren2_dst not in head, so no need to delete */
 			} else {
 				struct merge_file_info mfi;
 				remove_file(o, 1, ren1_src, 1);
@@ -1364,7 +1363,33 @@ static int process_df_entry(struct merge_options *o,
 
 	entry->processed = 1;
 	if (entry->rename_df_conflict_info) {
-		die ("Not yet implemented.");
+		struct rename_df_conflict_info *conflict_info = entry->rename_df_conflict_info;
+		char *src;
+		switch (conflict_info->rename_type) {
+		case RENAME_ONE_FILE_TO_TWO:
+			src = conflict_info->pair1->one->path;
+			clean_merge = 0;
+			output(o, 1, "CONFLICT (rename/rename): "
+			       "Rename \"%s\"->\"%s\" in branch \"%s\" "
+			       "rename \"%s\"->\"%s\" in \"%s\"%s",
+			       src, conflict_info->pair1->two->path, conflict_info->branch1,
+			       src, conflict_info->pair2->two->path, conflict_info->branch2,
+			       o->call_depth ? " (left unresolved)": "");
+			if (o->call_depth) {
+				remove_file_from_cache(src);
+				update_file(o, 0, conflict_info->pair1->one->sha1,
+					    conflict_info->pair1->one->mode, src);
+			}
+			conflict_rename_rename_1to2(o, conflict_info->pair1,
+						    conflict_info->branch1,
+						    conflict_info->pair2,
+						    conflict_info->branch2);
+			conflict_info->dst_entry2->processed = 1;
+			break;
+		default:
+			entry->processed = 0;
+			break;
+		}
 	} else if (!o_sha && !!a_sha != !!b_sha) {
 		/* directory -> (directory, file) */
 		const char *add_branch;
diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh
index 02dea16..1f29c0a 100755
--- a/t/t6022-merge-rename.sh
+++ b/t/t6022-merge-rename.sh
@@ -652,7 +652,7 @@ test_expect_success 'setup rename of one file to two, with directories in the wa
 	git commit -m "Put one/file in the way, rename to two"
 '
 
-test_expect_failure 'check handling of differently renamed file with D/F conflicts' '
+test_expect_success 'check handling of differently renamed file with D/F conflicts' '
 	git checkout -q first-rename^0 &&
 	test_must_fail git merge --strategy=recursive second-rename &&
 
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:29 UTC · re: Elijah Newren · lore

[PATCH 28/37] merge-recursive: Move handling of double rename of one file to other file

Move the handling of rename/rename conflicts where one file is renamed on both sides to the same file, from process_renames() to process_entry(). Here we avoid the three way merge logic by just using update_stages_and_entry() to move the higher stage entries in the index from the rename source to the rename destination, and then allow process_entry() to do its magic.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   32 ++++++--------------------------
 1 files changed, 6 insertions(+), 26 deletions(-)
Show changes to merge-recursive.c +6 −26
diff --git a/merge-recursive.c b/merge-recursive.c
index e219d62..21b52d4 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -971,33 +971,13 @@ static int process_renames(struct merge_options *o,
 				remove_file(o, 0, ren1_dst, 0);
 				/* ren2_dst not in head, so no need to delete */
 			} else {
-				struct merge_file_info mfi;
 				remove_file(o, 1, ren1_src, 1);
-				mfi = merge_file(o,
-						 ren1->pair->one,
-						 ren1->pair->two,
-						 ren2->pair->two,
-						 branch1,
-						 branch2);
-				if (mfi.merge || !mfi.clean)
-					output(o, 1, "Renaming %s->%s", src, ren1_dst);
-
-				if (mfi.merge)
-					output(o, 2, "Auto-merging %s", ren1_dst);
-
-				if (!mfi.clean) {
-					output(o, 1, "CONFLICT (content): merge conflict in %s",
-					       ren1_dst);
-					clean_merge = 0;
-
-					if (!o->call_depth)
-						update_stages(ren1_dst,
-							      ren1->pair->one,
-							      ren1->pair->two,
-							      ren2->pair->two,
-							      1 /* clear */);
-				}
-				update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
+				update_stages_and_entry(ren1_dst,
+							ren1->dst_entry,
+							ren1->pair->one,
+							ren1->pair->two,
+							ren2->pair->two,
+							1 /* clear */);
 			}
 		} else {
 			/* Renamed in 1, maybe changed in 2 */
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:29 UTC · re: Elijah Newren · lore

[PATCH 29/37] merge-recursive: Delay handling of rename/delete conflicts

Move the handling of rename/delete conflicts from process_renames() to process_df_entry().

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   22 ++++++++++++++++++++--
 1 files changed, 20 insertions(+), 2 deletions(-)
Show changes to merge-recursive.c +20 −2
diff --git a/merge-recursive.c b/merge-recursive.c
index 21b52d4..81508b3 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1004,8 +1004,20 @@ static int process_renames(struct merge_options *o,
 			try_merge = 0;
 
 			if (sha_eq(src_other.sha1, null_sha1)) {
-				clean_merge = 0;
-				conflict_rename_delete(o, ren1->pair, branch1, branch2);
+				if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
+					ren1->dst_entry->processed = 0;
+					setup_rename_df_conflict_info(RENAME_DELETE,
+								      ren1->pair,
+								      NULL,
+								      branch1,
+								      branch2,
+								      ren1->dst_entry,
+								      NULL);
+					remove_file(o, 0, ren1_dst, 0);
+				} else {
+					clean_merge = 0;
+					conflict_rename_delete(o, ren1->pair, branch1, branch2);
+				}
 			} else if ((dst_other.mode == ren1->pair->two->mode) &&
 				   sha_eq(dst_other.sha1, ren1->pair->two->sha1)) {
 				/* Added file on the other side
@@ -1346,6 +1358,12 @@ static int process_df_entry(struct merge_options *o,
 		struct rename_df_conflict_info *conflict_info = entry->rename_df_conflict_info;
 		char *src;
 		switch (conflict_info->rename_type) {
+		case RENAME_DELETE:
+			clean_merge = 0;
+			conflict_rename_delete(o, conflict_info->pair1,
+					       conflict_info->branch1,
+					       conflict_info->branch2);
+			break;
 		case RENAME_ONE_FILE_TO_TWO:
 			src = conflict_info->pair1->one->path;
 			clean_merge = 0;
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:29 UTC · re: Elijah Newren · lore

[PATCH 30/37] merge-recursive: Delay content merging for renames

Move the handling of content merging for renames from process_renames() to process_df_entry().

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c       |   51 +++++++++++++---------------------------------
 t/t6022-merge-rename.sh |    2 +-
 2 files changed, 16 insertions(+), 37 deletions(-)
Show changes to 2 files +16 −37

merge-recursive.c, t/t6022-merge-rename.sh

diff --git a/merge-recursive.c b/merge-recursive.c
index 81508b3..008cacb 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1073,7 +1073,6 @@ static int process_renames(struct merge_options *o,
 
 			if (try_merge) {
 				struct diff_filespec *one, *a, *b;
-				struct merge_file_info mfi;
 				src_other.path = (char *)ren1_src;
 
 				one = ren1->pair->one;
@@ -1084,41 +1083,16 @@ static int process_renames(struct merge_options *o,
 					b = ren1->pair->two;
 					a = &src_other;
 				}
-				mfi = merge_file(o, one, a, b,
-						o->branch1, o->branch2);
-
-				if (mfi.clean &&
-				    sha_eq(mfi.sha, ren1->pair->two->sha1) &&
-				    mfi.mode == ren1->pair->two->mode) {
-					/*
-					 * This message is part of
-					 * t6022 test. If you change
-					 * it update the test too.
-					 */
-					output(o, 3, "Skipped %s (merged same as existing)", ren1_dst);
-
-					/* There may be higher stage entries left
-					 * in the index (e.g. due to a D/F
-					 * conflict) that need to be resolved.
-					 */
-					if (!ren1->dst_entry->stages[2].mode !=
-					    !ren1->dst_entry->stages[3].mode)
-						ren1->dst_entry->processed = 0;
-				} else {
-					if (mfi.merge || !mfi.clean)
-						output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
-					if (mfi.merge)
-						output(o, 2, "Auto-merging %s", ren1_dst);
-					if (!mfi.clean) {
-						output(o, 1, "CONFLICT (rename/modify): Merge conflict in %s",
-						       ren1_dst);
-						clean_merge = 0;
-
-						if (!o->call_depth)
-							update_stages(ren1_dst,
-								      one, a, b, 1);
-					}
-					update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
+				update_stages_and_entry(ren1_dst, ren1->dst_entry, one, a, b, 1);
+				if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
+					setup_rename_df_conflict_info(RENAME_NORMAL,
+								      ren1->pair,
+								      NULL,
+								      branch1,
+								      NULL,
+								      ren1->dst_entry,
+								      NULL);
+					remove_file(o, 0, ren1_dst, 0);
 				}
 			}
 		}
@@ -1358,6 +1332,11 @@ static int process_df_entry(struct merge_options *o,
 		struct rename_df_conflict_info *conflict_info = entry->rename_df_conflict_info;
 		char *src;
 		switch (conflict_info->rename_type) {
+		case RENAME_NORMAL:
+			clean_merge = merge_content(o, path,
+						    o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
+						    conflict_info->branch1);
+			break;
 		case RENAME_DELETE:
 			clean_merge = 0;
 			conflict_rename_delete(o, conflict_info->pair1,
diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh
index 1f29c0a..edbfa47 100755
--- a/t/t6022-merge-rename.sh
+++ b/t/t6022-merge-rename.sh
@@ -455,7 +455,7 @@ cat >expected <<\EOF &&
 >>>>>>> dir-not-in-way
 EOF
 
-test_expect_failure 'Rename+D/F conflict; renamed file cannot merge, dir not in way' '
+test_expect_success 'Rename+D/F conflict; renamed file cannot merge, dir not in way' '
 	git reset --hard &&
 	rm -rf dir~* &&
 	git checkout -q renamed-file-has-conflicts^0 &&
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:29 UTC · re: Elijah Newren · lore

[PATCH 31/37] merge-recursive: Delay modify/delete conflicts if D/F conflict present

When handling merges with modify/delete conflicts, if the modified path is involved in a D/F conflict, handle the issue in process_df_entry() rather than process_entry().

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
Show changes to merge-recursive.c +9 −0
diff --git a/merge-recursive.c b/merge-recursive.c
index 008cacb..d9fcd6d 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1249,6 +1249,10 @@ static int process_entry(struct merge_options *o,
 				output(o, 2, "Removing %s", path);
 			/* do not touch working file if it did not exist */
 			remove_file(o, 1, path, !a_sha);
+		} else if (string_list_has_string(&o->current_directory_set,
+						  path)) {
+			entry->processed = 0;
+			return 1; /* Assume clean till processed */
 		} else {
 			/* Deleted in one and changed in the other */
 			clean_merge = 0;
@@ -1367,6 +1371,11 @@ static int process_df_entry(struct merge_options *o,
 			entry->processed = 0;
 			break;
 		}
+	} else if (o_sha && (!a_sha || !b_sha)) {
+		/* Modify/delete; deleted side may have put a directory in the way */
+		clean_merge = 0;
+		handle_delete_modify(o, path,
+				     a_sha, a_mode, b_sha, b_mode);
 	} else if (!o_sha && !!a_sha != !!b_sha) {
 		/* directory -> (directory, file) */
 		const char *add_branch;
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:29 UTC · re: Elijah Newren · lore

[PATCH 32/37] conflict_rename_delete(): Check whether D/F conflicts are still present

If all the paths below some directory involved in a D/F conflict were not removed during the rest of the merge, then the contents of the file whose path conflicted needs to be recorded in file with an alternative filename.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c       |    8 ++++++++
 t/t6022-merge-rename.sh |    4 ++--
 2 files changed, 10 insertions(+), 2 deletions(-)
Show changes to 2 files +10 −2

merge-recursive.c, t/t6022-merge-rename.sh

diff --git a/merge-recursive.c b/merge-recursive.c
index d9fcd6d..c6a3465 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -807,6 +807,8 @@ static void conflict_rename_delete(struct merge_options *o,
 				   const char *other_branch)
 {
 	char *dest_name = pair->two->path;
+	int df_conflict = 0;
+	struct stat st;
 
 	output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
 	       "and deleted in %s",
@@ -817,7 +819,13 @@ static void conflict_rename_delete(struct merge_options *o,
 			      rename_branch == o->branch1 ? pair->two : NULL,
 			      rename_branch == o->branch1 ? NULL : pair->two,
 			      1);
+	if (lstat(dest_name, &st) == 0 && S_ISDIR(st.st_mode)) {
+		dest_name = unique_path(o, dest_name, rename_branch);
+		df_conflict = 1;
+	}
 	update_file(o, 0, pair->two->sha1, pair->two->mode, dest_name);
+	if (df_conflict)
+		free(dest_name);
 }
 
 static void conflict_rename_rename_1to2(struct merge_options *o,
diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh
index edbfa47..9bf190e 100755
--- a/t/t6022-merge-rename.sh
+++ b/t/t6022-merge-rename.sh
@@ -549,7 +549,7 @@ test_expect_success 'setup both rename source and destination involved in D/F co
 	git commit -m "Conflicts in the way"
 '
 
-test_expect_failure 'both rename source and destination involved in D/F conflict' '
+test_expect_success 'both rename source and destination involved in D/F conflict' '
 	git reset --hard &&
 	rm -rf dir~* &&
 	git checkout -q rename-dest^0 &&
@@ -589,7 +589,7 @@ test_expect_success 'setup pair rename to parent of other (D/F conflicts)' '
 	git commit -m "Rename one/file -> two"
 '
 
-test_expect_failure 'pair rename to parent of other (D/F conflicts) w/ untracked dir' '
+test_expect_success 'pair rename to parent of other (D/F conflicts) w/ untracked dir' '
 	git checkout -q rename-one^0 &&
 	mkdir one &&
 	test_must_fail git merge --strategy=recursive rename-two &&
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:29 UTC · re: Elijah Newren · lore

[PATCH 33/37] conflict_rename_rename_1to2(): Fix checks for presence of D/F conflicts

This function is called from process_df_entry(), near the end of the merge. Rather than just checking whether one of the sides of the merge had a directory at the same path as one of our files, check whether that directory is still present by this point of our merge.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c       |    7 +++----
 t/t6022-merge-rename.sh |    2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)
Show changes to 2 files +4 −5

merge-recursive.c, t/t6022-merge-rename.sh

diff --git a/merge-recursive.c b/merge-recursive.c
index c6a3465..3cc5886 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -841,17 +841,16 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
 	const char *ren2_dst = pair2->two->path;
 	const char *dst_name1 = ren1_dst;
 	const char *dst_name2 = ren2_dst;
-	if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
+	struct stat st;
+	if (lstat(ren1_dst, &st) == 0 && S_ISDIR(st.st_mode)) {
 		dst_name1 = del[delp++] = unique_path(o, ren1_dst, branch1);
 		output(o, 1, "%s is a directory in %s adding as %s instead",
 		       ren1_dst, branch2, dst_name1);
-		remove_file(o, 0, ren1_dst, 0);
 	}
-	if (string_list_has_string(&o->current_directory_set, ren2_dst)) {
+	if (lstat(ren2_dst, &st) == 0 && S_ISDIR(st.st_mode)) {
 		dst_name2 = del[delp++] = unique_path(o, ren2_dst, branch2);
 		output(o, 1, "%s is a directory in %s adding as %s instead",
 		       ren2_dst, branch1, dst_name2);
-		remove_file(o, 0, ren2_dst, 0);
 	}
 	if (o->call_depth) {
 		remove_file_from_cache(dst_name1);
diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh
index 9bf190e..0b67002 100755
--- a/t/t6022-merge-rename.sh
+++ b/t/t6022-merge-rename.sh
@@ -692,7 +692,7 @@ test_expect_success 'setup rename one file to two; directories moving out of the
 	git commit -m "Rename to two"
 '
 
-test_expect_failure 'check handling of differently renamed file with D/F conflicts' '
+test_expect_success 'check handling of differently renamed file with D/F conflicts' '
 	git checkout -q first-rename-redo^0 &&
 	test_must_fail git merge --strategy=recursive second-rename-redo &&
 
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:29 UTC · re: Elijah Newren · lore

[PATCH 34/37] merge_content(): Check whether D/F conflicts are still present

If all the paths below some directory involved in a D/F conflict were not removed during the rest of the merge, then the contents of the file whose path conflicted needs to be recorded in file with an alternative filename.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c       |   22 ++++++++++++++++++++--
 t/t6022-merge-rename.sh |    8 ++++----
 2 files changed, 24 insertions(+), 6 deletions(-)
Show changes to 2 files +24 −6

merge-recursive.c, t/t6022-merge-rename.sh

diff --git a/merge-recursive.c b/merge-recursive.c
index 3cc5886..a971d59 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1193,6 +1193,8 @@ static int merge_content(struct merge_options *o,
 	const char *reason = "content";
 	struct merge_file_info mfi;
 	struct diff_filespec one, a, b;
+	struct stat st;
+	unsigned df_conflict_remains = 0;
 
 	if (!o_sha) {
 		reason = "add/add";
@@ -1207,7 +1209,13 @@ static int merge_content(struct merge_options *o,
 	b.mode = b_mode;
 
 	mfi = merge_file(o, &one, &a, &b, o->branch1, o->branch2);
-	if (mfi.clean && sha_eq(mfi.sha, a_sha) && mfi.mode == a.mode)
+	if (df_rename_conflict_branch &&
+	    lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
+		df_conflict_remains = 1;
+	}
+
+	if (mfi.clean && !df_conflict_remains &&
+	    sha_eq(mfi.sha, a_sha) && mfi.mode == a.mode)
 		output(o, 3, "Skipped %s (merged same as existing)", path);
 	else
 		output(o, 2, "Auto-merging %s", path);
@@ -1219,7 +1227,17 @@ static int merge_content(struct merge_options *o,
 				reason, path);
 	}
 
-	update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
+	if (df_conflict_remains) {
+		const char *new_path;
+		update_file_flags(o, mfi.sha, mfi.mode, path,
+				  o->call_depth || mfi.clean, 0);
+		new_path = unique_path(o, path, df_rename_conflict_branch);
+		mfi.clean = 0;
+		output(o, 1, "Adding as %s instead", new_path);
+		update_file_flags(o, mfi.sha, mfi.mode, new_path, 0, 1);
+	} else {
+		update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
+	}
 	return mfi.clean;
 
 }
diff --git a/t/t6022-merge-rename.sh b/t/t6022-merge-rename.sh
index 0b67002..422092e 100755
--- a/t/t6022-merge-rename.sh
+++ b/t/t6022-merge-rename.sh
@@ -394,7 +394,7 @@ test_expect_success 'Rename+D/F conflict; renamed file merges + dir not in way'
 	test_cmp expected dir
 '
 
-test_expect_failure 'Rename+D/F conflict; renamed file merges but dir in way' '
+test_expect_success 'Rename+D/F conflict; renamed file merges but dir in way' '
 	git reset --hard &&
 	rm -rf dir~* &&
 	git checkout -q renamed-file-has-no-conflicts^0 &&
@@ -415,7 +415,7 @@ test_expect_failure 'Rename+D/F conflict; renamed file merges but dir in way' '
 	test_cmp expected dir~HEAD
 '
 
-test_expect_failure 'Same as previous, but merged other way' '
+test_expect_success 'Same as previous, but merged other way' '
 	git reset --hard &&
 	rm -rf dir~* &&
 	git checkout -q dir-in-way^0 &&
@@ -471,7 +471,7 @@ test_expect_success 'Rename+D/F conflict; renamed file cannot merge, dir not in
 	test_cmp expected dir
 '
 
-test_expect_failure 'Rename+D/F conflict; renamed file cannot merge and dir in the way' '
+test_expect_success 'Rename+D/F conflict; renamed file cannot merge and dir in the way' '
 	modify s/dir-not-in-way/dir-in-way/ expected &&
 
 	git reset --hard &&
@@ -509,7 +509,7 @@ cat >expected <<\EOF &&
 >>>>>>> renamed-file-has-conflicts
 EOF
 
-test_expect_failure 'Same as previous, but merged other way' '
+test_expect_success 'Same as previous, but merged other way' '
 	git reset --hard &&
 	rm -rf dir~* &&
 	git checkout -q dir-in-way^0 &&
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:29 UTC · re: Elijah Newren · lore

[PATCH 35/37] handle_delete_modify(): Check whether D/F conflicts are still present

If all the paths below some directory involved in a D/F conflict were not removed during the rest of the merge, then the contents of the file whose path conflicted needs to be recorded in file with an alternative filename.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c   |   25 ++++++++++++++++---------
 t/t6020-merge-df.sh |    2 +-
 2 files changed, 17 insertions(+), 10 deletions(-)
Show changes to 2 files +17 −10

merge-recursive.c, t/t6020-merge-df.sh

diff --git a/merge-recursive.c b/merge-recursive.c
index a971d59..05da47c 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1164,25 +1164,29 @@ error_return:
 
 static void handle_delete_modify(struct merge_options *o,
 				 const char *path,
+				 const char *new_path,
 				 unsigned char *a_sha, int a_mode,
 				 unsigned char *b_sha, int b_mode)
 {
 	if (!a_sha) {
 		output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
-		       "and modified in %s. Version %s of %s left in tree.",
+		       "and modified in %s. Version %s of %s left in tree%s%s.",
 		       path, o->branch1,
-		       o->branch2, o->branch2, path);
-		update_file(o, 0, b_sha, b_mode, path);
+		       o->branch2, o->branch2, path,
+		       path == new_path ? "" : " at ",
+		       path == new_path ? "" : new_path);
+		update_file(o, 0, b_sha, b_mode, new_path);
 	} else {
 		output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
-		       "and modified in %s. Version %s of %s left in tree.",
+		       "and modified in %s. Version %s of %s left in tree%s%s.",
 		       path, o->branch2,
-		       o->branch1, o->branch1, path);
-		update_file(o, 0, a_sha, a_mode, path);
+		       o->branch1, o->branch1, path,
+		       path == new_path ? "" : " at ",
+		       path == new_path ? "" : new_path);
+		update_file(o, 0, a_sha, a_mode, new_path);
 	}
 }
 
-
 static int merge_content(struct merge_options *o,
 			 const char *path,
 			 unsigned char *o_sha, int o_mode,
@@ -1281,7 +1285,7 @@ static int process_entry(struct merge_options *o,
 		} else {
 			/* Deleted in one and changed in the other */
 			clean_merge = 0;
-			handle_delete_modify(o, path,
+			handle_delete_modify(o, path, path,
 					     a_sha, a_mode, b_sha, b_mode);
 		}
 
@@ -1398,8 +1402,11 @@ static int process_df_entry(struct merge_options *o,
 		}
 	} else if (o_sha && (!a_sha || !b_sha)) {
 		/* Modify/delete; deleted side may have put a directory in the way */
+		const char *new_path = path;
+		if (lstat(path, &st) == 0 && S_ISDIR(st.st_mode))
+			new_path = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
 		clean_merge = 0;
-		handle_delete_modify(o, path,
+		handle_delete_modify(o, path, new_path,
 				     a_sha, a_mode, b_sha, b_mode);
 	} else if (!o_sha && !!a_sha != !!b_sha) {
 		/* directory -> (directory, file) */
diff --git a/t/t6020-merge-df.sh b/t/t6020-merge-df.sh
index bc9db1a..7b1ce82 100755
--- a/t/t6020-merge-df.sh
+++ b/t/t6020-merge-df.sh
@@ -71,7 +71,7 @@ test_expect_success 'setup modify/delete + directory/file conflict' '
 	git commit -m deleted
 '
 
-test_expect_failure 'modify/delete + directory/file conflict' '
+test_expect_success 'modify/delete + directory/file conflict' '
 	git checkout delete^0 &&
 	test_must_fail git merge modify &&
 
-- 
1.7.3.271.g16009
Elijah Newren· Sep 20, 2010, 08:29 UTC · re: Elijah Newren · lore

[PATCH 36/37] merge-recursive: Make room for directories in D/F conflicts

When there are unmerged entries present, make sure to check for D/F conflicts first and remove any files present in HEAD that would be in the way of creating files below the correspondingly named directory. Such files will be processed again at the end of the merge in process_df_entry(); at that time we will be able to tell if we need to and can reinstate the file, whether we need to place its contents in a different file due to the directory still being present, etc.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c   |   58 +++++++++++++++++++++++++++++++++++++++++++++++++++
 t/t6020-merge-df.sh |    2 +-
 2 files changed, 59 insertions(+), 1 deletions(-)
Show changes to 2 files +59 −1

merge-recursive.c, t/t6020-merge-df.sh

diff --git a/merge-recursive.c b/merge-recursive.c
index 05da47c..821bed8 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -347,6 +347,63 @@ static struct string_list *get_unmerged(void)
 	return unmerged;
 }
 
+static void make_room_for_directories_of_df_conflicts(struct merge_options *o,
+						      struct string_list *entries)
+{
+	/* If there are D/F conflicts, and the paths currently exist
+	 * in the working copy as a file, we want to remove them to
+	 * make room for the corresponding directory.  Such paths will
+	 * later be processed in process_df_entry() at the end.  If
+	 * the corresponding directory ends up being removed by the
+	 * merge, then the file will be reinstated at that time;
+	 * otherwise, if the file is not supposed to be removed by the
+	 * merge, the contents of the file will be placed in another
+	 * unique filename.
+	 *
+	 * NOTE: This function relies on the fact that entries for a
+	 * D/F conflict will appear adjacent in the index, with the
+	 * entries for the file appearing before entries for paths
+	 * below the corresponding directory.
+	 */
+	const char *last_file = NULL;
+	int last_len;
+	struct stage_data *last_e;
+	int i;
+
+	for (i = 0; i < entries->nr; i++) {
+		const char *path = entries->items[i].string;
+		int len = strlen(path);
+		struct stage_data *e = entries->items[i].util;
+
+		/*
+		 * Check if last_file & path correspond to a D/F conflict;
+		 * i.e. whether path is last_file+'/'+<something>.
+		 * If so, remove last_file to make room for path and friends.
+		 */
+		if (last_file &&
+		    len > last_len &&
+		    memcmp(path, last_file, last_len) == 0 &&
+		    path[last_len] == '/') {
+			output(o, 3, "Removing %s to make room for subdirectory; may re-add later.", last_file);
+			unlink(last_file);
+		}
+
+		/*
+		 * Determine whether path could exist as a file in the
+		 * working directory as a possible D/F conflict.  This
+		 * will only occur when it exists in stage 2 as a
+		 * file.
+		 */
+		if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
+			last_file = path;
+			last_len = len;
+			last_e = e;
+		} else {
+			last_file = NULL;
+		}
+	}
+}
+
 struct rename
 {
 	struct diff_filepair *pair;
@@ -1488,6 +1545,7 @@ int merge_trees(struct merge_options *o,
 		get_files_dirs(o, merge);
 
 		entries = get_unmerged();
+		make_room_for_directories_of_df_conflicts(o, entries);
 		re_head  = get_renames(o, head, common, head, merge, entries);
 		re_merge = get_renames(o, merge, common, head, merge, entries);
 		clean = process_renames(o, re_head, re_merge);
diff --git a/t/t6020-merge-df.sh b/t/t6020-merge-df.sh
index 7b1ce82..b129f1d 100755
--- a/t/t6020-merge-df.sh
+++ b/t/t6020-merge-df.sh
@@ -83,7 +83,7 @@ test_expect_success 'modify/delete + directory/file conflict' '
 	test -f letters~modify
 '
 
-test_expect_failure 'modify/delete + directory/file conflict; other way' '
+test_expect_success 'modify/delete + directory/file conflict; other way' '
 	git reset --hard &&
 	git clean -f &&
 	git checkout modify^0 &&
-- 
1.7.3.271.g16009
Johannes Sixt· Sep 20, 2010, 11:40 UTC · re: Elijah Newren · lore

Re: [PATCH 36/37] merge-recursive: Make room for directories in D/F conflicts

Am 9/20/2010 10:29, schrieb Elijah Newren:
> +	 * NOTE: This function relies on the fact that entries for a
> +	 * D/F conflict will appear adjacent in the index, with the
> +	 * entries for the file appearing before entries for paths
> +	 * below the corresponding directory.

I don't think that this is a generally valid assumption. There can be other entries in between:

   this
   this.txt
   this/file
(where your focus is on "this").
-- Hannes
Elijah Newren· Sep 20, 2010, 16:06 UTC · re: Johannes Sixt · lore

Re: [PATCH 36/37] merge-recursive: Make room for directories in D/F conflicts

On Mon, Sep 20, 2010 at 5:40 AM, Johannes Sixt <j.sixt@viscovery.net> wrote:
Show 14 quoted lines
> Am 9/20/2010 10:29, schrieb Elijah Newren:
>> +      * NOTE: This function relies on the fact that entries for a
>> +      * D/F conflict will appear adjacent in the index, with the
>> +      * entries for the file appearing before entries for paths
>> +      * below the corresponding directory.
>
> I don't think that this is a generally valid assumption. There can be
> other entries in between:
>
>   this
>   this.txt
>   this/file
>
> (where your focus is on "this").

Ah, this was one of my two big remaining questions about this series (the other being whether all the o->call_depth > 0 cases have been fixed). Thanks for the clarification.

Would a sort of the entries string_list using df_name_compare() give me what I need?

Elijah
Elijah Newren· Sep 20, 2010, 08:29 UTC · re: Elijah Newren · lore

[PATCH 37/37] merge-recursive: Remove redundant path clearing for D/F conflicts

The code had several places where individual checks were done to remove files that could be in the way of directories in D/F conflicts. Not all D/F conflicts could have a path cleared for them in such a manner, however, leading to the need to create make_room_for_directories_of_df_conflicts() as done in the previous patch. That new function could not have been incorporated into the code sooner, since not all relevant code paths had been deferred to process_df_entry() yet, leading to the creation of even more of these now-redundant path removals.

Clean out all of these extra D/F path clearing cases.
Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-recursive.c |   17 ++---------------
 1 files changed, 2 insertions(+), 15 deletions(-)
Show changes to merge-recursive.c +2 −15
diff --git a/merge-recursive.c b/merge-recursive.c
index 821bed8..494e15a 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1032,8 +1032,6 @@ static int process_renames(struct merge_options *o,
 							      branch2,
 							      ren1->dst_entry,
 							      ren2->dst_entry);
-				remove_file(o, 0, ren1_dst, 0);
-				/* ren2_dst not in head, so no need to delete */
 			} else {
 				remove_file(o, 1, ren1_src, 1);
 				update_stages_and_entry(ren1_dst,
@@ -1077,7 +1075,6 @@ static int process_renames(struct merge_options *o,
 								      branch2,
 								      ren1->dst_entry,
 								      NULL);
-					remove_file(o, 0, ren1_dst, 0);
 				} else {
 					clean_merge = 0;
 					conflict_rename_delete(o, ren1->pair, branch1, branch2);
@@ -1156,7 +1153,6 @@ static int process_renames(struct merge_options *o,
 								      NULL,
 								      ren1->dst_entry,
 								      NULL);
-					remove_file(o, 0, ren1_dst, 0);
 				}
 			}
 		}
@@ -1338,7 +1334,7 @@ static int process_entry(struct merge_options *o,
 		} else if (string_list_has_string(&o->current_directory_set,
 						  path)) {
 			entry->processed = 0;
-			return 1; /* Assume clean till processed */
+			return 1; /* Assume clean until processed */
 		} else {
 			/* Deleted in one and changed in the other */
 			clean_merge = 0;
@@ -1362,15 +1358,7 @@ static int process_entry(struct merge_options *o,
 		if (string_list_has_string(&o->current_directory_set, path)) {
 			/* Handle D->F conflicts after all subfiles */
 			entry->processed = 0;
-			/* But get any file out of the way now, so conflicted
-			 * entries below the directory of the same name can
-			 * be put in the working directory.
-			 */
-			if (a_sha)
-				output(o, 2, "Removing %s", path);
-			/* do not touch working file if it did not exist */
-			remove_file(o, 0, path, !a_sha);
-			return 1; /* Assume clean till processed */
+			return 1; /* Assume clean until processed */
 		} else {
 			output(o, 2, "Adding %s", path);
 			update_file(o, 1, sha, mode, path);
@@ -1492,7 +1480,6 @@ static int process_df_entry(struct merge_options *o,
 			output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
 			       "Adding %s as %s",
 			       conf, path, other_branch, path, new_path);
-			remove_file(o, 0, path, 0);
 			update_file(o, 0, sha, mode, new_path);
 		} else {
 			output(o, 2, "Adding %s", path);
-- 
1.7.3.271.g16009

← back to recent threads