Commit 0ff723cc90c258f2b78285e7e4b55352e1bc05cd

Edward Thomson 2015-09-25T12:09:50

apply: test postimages that grow/shrink original Test with some postimages that actually grow/shrink from the original, adding new lines or removing them. (Also do so without context to ensure that we can add/remove from a non-zero part of the line vector.)

diff --git a/tests/apply/fromfile.c b/tests/apply/fromfile.c
index 88a2f45..ec2b889 100644
--- a/tests/apply/fromfile.c
+++ b/tests/apply/fromfile.c
@@ -125,6 +125,48 @@ void test_apply_fromfile__lastline(void)
 		"file.txt", 0100644));
 }
 
+void test_apply_fromfile__change_middle_shrink(void)
+{
+	cl_git_pass(validate_and_apply_patchfile(
+		FILE_ORIGINAL, strlen(FILE_ORIGINAL),
+		FILE_CHANGE_MIDDLE_SHRINK, strlen(FILE_CHANGE_MIDDLE_SHRINK),
+		PATCH_ORIGINAL_TO_CHANGE_MIDDLE_SHRINK, NULL,
+		"file.txt", 0100644));
+}
+
+void test_apply_fromfile__change_middle_shrink_nocontext(void)
+{
+	git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+	diff_opts.context_lines = 0;
+
+	cl_git_pass(validate_and_apply_patchfile(
+		FILE_ORIGINAL, strlen(FILE_ORIGINAL),
+		FILE_CHANGE_MIDDLE_SHRINK, strlen(FILE_CHANGE_MIDDLE_SHRINK),
+		PATCH_ORIGINAL_TO_MIDDLE_SHRINK_NOCONTEXT, &diff_opts,
+		"file.txt", 0100644));
+}
+
+void test_apply_fromfile__change_middle_grow(void)
+{
+	cl_git_pass(validate_and_apply_patchfile(
+		FILE_ORIGINAL, strlen(FILE_ORIGINAL),
+		FILE_CHANGE_MIDDLE_GROW, strlen(FILE_CHANGE_MIDDLE_GROW),
+		PATCH_ORIGINAL_TO_CHANGE_MIDDLE_GROW, NULL,
+		"file.txt", 0100644));
+}
+
+void test_apply_fromfile__change_middle_grow_nocontext(void)
+{
+	git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+	diff_opts.context_lines = 0;
+
+	cl_git_pass(validate_and_apply_patchfile(
+		FILE_ORIGINAL, strlen(FILE_ORIGINAL),
+		FILE_CHANGE_MIDDLE_GROW, strlen(FILE_CHANGE_MIDDLE_GROW),
+		PATCH_ORIGINAL_TO_MIDDLE_GROW_NOCONTEXT, &diff_opts,
+		"file.txt", 0100644));
+}
+
 void test_apply_fromfile__prepend(void)
 {
 	cl_git_pass(validate_and_apply_patchfile(
diff --git a/tests/patch/patch_common.h b/tests/patch/patch_common.h
index f4cb2ff..e097062 100644
--- a/tests/patch/patch_common.h
+++ b/tests/patch/patch_common.h
@@ -98,6 +98,95 @@
 	"-below it!\n" \
 	"+change to the last line.\n"
 
+/* A change of the middle where we remove many lines */
+
+#define FILE_CHANGE_MIDDLE_SHRINK \
+	"hey!\n" \
+	"i've changed a lot, but left the line\n" \
+	"below it!\n"
+
+#define PATCH_ORIGINAL_TO_CHANGE_MIDDLE_SHRINK \
+	"diff --git a/file.txt b/file.txt\n" \
+	"index 9432026..629cd35 100644\n" \
+	"--- a/file.txt\n" \
+	"+++ b/file.txt\n" \
+	"@@ -1,9 +1,3 @@\n" \
+	" hey!\n" \
+	"-this is some context!\n" \
+	"-around some lines\n" \
+	"-that will change\n" \
+	"-yes it is!\n" \
+	"-(this line is changed)\n" \
+	"-and this\n" \
+	"-is additional context\n" \
+	"+i've changed a lot, but left the line\n" \
+	" below it!\n"
+
+#define PATCH_ORIGINAL_TO_MIDDLE_SHRINK_NOCONTEXT \
+	"diff --git a/file.txt b/file.txt\n" \
+	"index 9432026..629cd35 100644\n" \
+	"--- a/file.txt\n" \
+	"+++ b/file.txt\n" \
+	"@@ -2,7 +2 @@ hey!\n" \
+	"-this is some context!\n" \
+	"-around some lines\n" \
+	"-that will change\n" \
+	"-yes it is!\n" \
+	"-(this line is changed)\n" \
+	"-and this\n" \
+	"-is additional context\n" \
+	"+i've changed a lot, but left the line\n"
+
+/* A change to the middle where we grow many lines */
+
+#define FILE_CHANGE_MIDDLE_GROW \
+	"hey!\n" \
+	"this is some context!\n" \
+	"around some lines\n" \
+	"that will change\n" \
+	"yes it is!\n" \
+	"this line is changed\n" \
+	"and this line is added\n" \
+	"so is this\n" \
+	"(this too)\n" \
+	"whee...\n" \
+	"and this\n" \
+	"is additional context\n" \
+	"below it!\n"
+
+#define PATCH_ORIGINAL_TO_CHANGE_MIDDLE_GROW \
+	"diff --git a/file.txt b/file.txt\n" \
+	"index 9432026..207ebca 100644\n" \
+	"--- a/file.txt\n" \
+	"+++ b/file.txt\n" \
+	"@@ -3,7 +3,11 @@ this is some context!\n" \
+	" around some lines\n" \
+	" that will change\n" \
+	" yes it is!\n" \
+	"-(this line is changed)\n" \
+	"+this line is changed\n" \
+	"+and this line is added\n" \
+	"+so is this\n" \
+	"+(this too)\n" \
+	"+whee...\n" \
+	" and this\n" \
+	" is additional context\n" \
+	" below it!\n"
+
+
+#define PATCH_ORIGINAL_TO_MIDDLE_GROW_NOCONTEXT \
+	"diff --git a/file.txt b/file.txt\n" \
+	"index 9432026..207ebca 100644\n" \
+	"--- a/file.txt\n" \
+	"+++ b/file.txt\n" \
+	"@@ -6 +6,5 @@ yes it is!\n" \
+	"-(this line is changed)\n" \
+	"+this line is changed\n" \
+	"+and this line is added\n" \
+	"+so is this\n" \
+	"+(this too)\n" \
+	"+whee...\n"
+
 /* An insertion at the beginning of the file (and the resultant patch) */
 
 #define FILE_PREPEND \