Commit 553395dc5c677991ddf26b6133e52976a5dabb5b

Edward Thomson 2018-06-25T20:21:01

apply: test that the index is not modified Ensure that by default, when using GIT_APPLY_LOCATION_WORKDIR, that patch application does not update the index, only the working directory.

diff --git a/tests/apply/workdir.c b/tests/apply/workdir.c
index 1287d27..2b4ff56 100644
--- a/tests/apply/workdir.c
+++ b/tests/apply/workdir.c
@@ -116,6 +116,25 @@ static void validate_apply_workdir(
 	git_index_free(index);
 }
 
+static void validate_apply_index(
+	git_repository *repo,
+	struct merge_index_entry *index_entries,
+	size_t index_cnt)
+{
+	git_index *index;
+	git_iterator *iterator;
+	struct iterator_compare_data data = { index_entries, index_cnt };
+
+	cl_git_pass(git_repository_index(&index, repo));
+	cl_git_pass(git_iterator_for_index(&iterator, repo, index, NULL));
+
+	cl_git_pass(git_iterator_foreach(iterator, iterator_compare, &data));
+	cl_assert_equal_i(data.idx, data.cnt);
+
+	git_iterator_free(iterator);
+	git_index_free(index);
+}
+
 static int iterator_eq(const git_index_entry **entry, void *_data)
 {
 	GIT_UNUSED(_data);
@@ -269,3 +288,60 @@ void test_apply_workdir__adds_file(void)
 
 	git_diff_free(diff);
 }
+
+void test_apply_workdir__leaves_index_unchanged(void)
+{
+	git_index *index;
+	git_diff *diff;
+
+	const char *diff_file =
+		DIFF_MODIFY_TWO_FILES
+		DIFF_DELETE_FILE
+		DIFF_ADD_FILE;
+
+	struct merge_index_entry index_expected[] = {
+		{ 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+		{ 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+		{ 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+		{ 0100644, "750aa35f6336925a627f1366d2b6b533100c38a0", 0, "new_in_index.txt" },
+		{ 0100644, "b558b69bdceadc9b50a6a785e4800172d74ab863", 0, "oyster.txt" },
+		{ 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" }
+	};
+	size_t index_expected_cnt = sizeof(index_expected) /
+		sizeof(struct merge_index_entry);
+
+	struct merge_index_entry workdir_expected[] = {
+		{ 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+		{ 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+		{ 0100644, "750aa35f6336925a627f1366d2b6b533100c38a0", 0, "new_in_index.txt" },
+		{ 0100644, "6370543fcfedb3e6516ec53b06158f3687dc1447", 0, "newfile.txt" },
+		{ 0100644, "b558b69bdceadc9b50a6a785e4800172d74ab863", 0, "oyster.txt" },
+		{ 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+	};
+	size_t workdir_expected_cnt = sizeof(workdir_expected) /
+		sizeof(struct merge_index_entry);
+
+	/* mutate the index and workdir */
+	git_repository_index(&index, repo);
+
+	cl_git_rmfile("merge-recursive/beef.txt");
+	cl_git_pass(git_index_remove_bypath(index, "beef.txt"));
+
+	cl_git_mkfile("merge-recursive/new_in_index.txt",
+	    "Hello; this is new in the index.\n");
+	cl_git_pass(git_index_add_bypath(index, "new_in_index.txt"));
+
+	cl_git_rewritefile("merge-recursive/oyster.txt",
+	    "Hello; this is now changed in the index.\n");
+	cl_git_pass(git_index_add_bypath(index, "oyster.txt"));
+	cl_git_pass(git_index_write(index));
+
+	cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+	cl_git_pass(git_apply(repo, diff, NULL));
+
+	validate_apply_index(repo, index_expected, index_expected_cnt);
+	validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+	git_index_free(index);
+	git_diff_free(diff);
+}