Commit bd682f3ee4bc6593470d65f69a0e734fd0ae88eb

Edward Thomson 2018-11-04T19:01:57

apply: test that we can't rename a file after modifying it Multiple deltas can exist in a diff, and can be applied in-order. However if there exists a delta that renames a file, it must be first, so that other deltas can reference the resulting target file. git enforces this (`error: already exists in index`), so ensure that we do, too.

diff --git a/tests/apply/apply_helpers.h b/tests/apply/apply_helpers.h
index 982e731..90c1942 100644
--- a/tests/apply/apply_helpers.h
+++ b/tests/apply/apply_helpers.h
@@ -319,6 +319,31 @@
 	" Put into a pot three quarts of water, three onions cut small, one\n" \
 	" spoonful of black pepper pounded, and two of salt, with two or three\n"
 
+#define DIFF_RENAME_AFTER_MODIFY \
+	"diff --git a/veal.txt b/veal.txt\n" \
+	"index 292cb60..61c686b 100644\n" \
+	"--- a/veal.txt\n" \
+	"+++ b/veal.txt\n" \
+	"@@ -1,4 +1,4 @@\n" \
+	"-VEAL SOUP!\n" \
+	"+VEAL SOUP\n" \
+	"\n" \
+	" Put into a pot three quarts of water, three onions cut small, one\n" \
+	" spoonful of black pepper pounded, and two of salt, with two or three\n" \
+	"diff --git a/veal.txt b/beef.txt\n" \
+	"similarity index 96%\n" \
+	"rename from veal.txt\n" \
+	"rename to beef.txt\n" \
+	"index 94d2c01..292cb60 100644\n" \
+	"--- a/veal.txt\n" \
+	"+++ b/beef.txt\n" \
+	"@@ -15,4 +15,4 @@ will curdle in the soup. For a change you may put a dozen ripe tomatos\n" \
+	" in, first taking off their skins, by letting them stand a few minutes in\n" \
+	" hot water, when they may be easily peeled. When made in this way you\n" \
+	" must thicken it with the flour only. Any part of the veal may be used,\n" \
+	"-but the shin or knuckle is the nicest.\n" \
+	"+but the shin or knuckle is the nicest!\n"
+
 struct iterator_compare_data {
 	struct merge_index_entry *expected;
 	size_t cnt;
diff --git a/tests/apply/both.c b/tests/apply/both.c
index 7ad5bd8..91b9b42 100644
--- a/tests/apply/both.c
+++ b/tests/apply/both.c
@@ -650,3 +650,14 @@ void test_apply_both__rename_and_modify_deltas(void)
 
 	git_diff_free(diff);
 }
+
+void test_apply_both__cant_rename_after_modify(void)
+{
+	git_diff *diff;
+
+	cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_AFTER_MODIFY,
+		strlen(DIFF_RENAME_AFTER_MODIFY)));
+	cl_git_fail(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+	git_diff_free(diff);
+}