Commit ac7012a81f0bdc472a3d22393291eb7d130705d1

Edward Thomson 2015-05-25T20:36:29

binary diff: test index->workdir binary diffs

diff --git a/tests/diff/binary.c b/tests/diff/binary.c
index cb574a5..136c42c 100644
--- a/tests/diff/binary.c
+++ b/tests/diff/binary.c
@@ -261,3 +261,102 @@ void test_diff_binary__delta_append(void)
 
 	git_index_free(index);
 }
+
+void test_diff_binary__index_to_workdir(void)
+{
+	git_index *index;
+	git_diff *diff;
+	git_patch *patch;
+	git_buf actual = GIT_BUF_INIT;
+	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+	const char *expected =
+		"diff --git a/untimely.txt b/untimely.txt\n" \
+		"index 9a69d960ae94b060f56c2a8702545e2bb1abb935..1111d4f11f4b35bf6759e0fb714fe09731ef0840 100644\n" \
+		"GIT binary patch\n" \
+		"delta 32\n" \
+		"nc%1vf+QYWt3zLL@hC)e3Vu?a>QDRl4f_G*?PG(-ZA}<#J$+QbW\n" \
+		"\n" \
+		"delta 7\n" \
+		"Oc%18D`@*{63ljhg(E~C7\n";
+
+	opts.flags = GIT_DIFF_SHOW_BINARY | GIT_DIFF_FORCE_BINARY;
+	opts.id_abbrev = GIT_OID_HEXSZ;
+
+	repo = cl_git_sandbox_init("renames");
+	cl_git_pass(git_repository_index(&index, repo));
+
+	cl_git_append2file("renames/untimely.txt", "Oh that crazy Kipling!\r\n");
+
+	cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, &opts));
+
+	cl_git_pass(git_patch_from_diff(&patch, diff, 0));
+	cl_git_pass(git_patch_to_buf(&actual, patch));
+
+	cl_assert_equal_s(expected, actual.ptr);
+
+	cl_git_pass(git_index_add_bypath(index, "untimely.txt"));
+	cl_git_pass(git_index_write(index));
+
+	test_patch(
+		"19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13",
+		NULL,
+		&opts,
+		expected);
+
+	git_buf_free(&actual);
+	git_patch_free(patch);
+	git_diff_free(diff);
+	git_index_free(index);
+}
+
+static int print_cb(
+	const git_diff_delta *delta,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
+	void *payload)
+{
+	git_buf *buf = (git_buf *)payload;
+
+	if (hunk)
+		git_buf_put(buf, hunk->header, hunk->header_len);
+
+	if (line)
+		git_buf_put(buf, line->content, line->content_len);
+
+	return git_buf_oom(buf) ? -1 : 0;
+}
+
+void test_diff_binary__print_patch_from_diff(void)
+{
+	git_index *index;
+	git_diff *diff;
+	git_buf actual = GIT_BUF_INIT;
+	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+	const char *expected =
+		"diff --git a/untimely.txt b/untimely.txt\n" \
+		"index 9a69d960ae94b060f56c2a8702545e2bb1abb935..1111d4f11f4b35bf6759e0fb714fe09731ef0840 100644\n" \
+		"GIT binary patch\n" \
+		"delta 32\n" \
+		"nc%1vf+QYWt3zLL@hC)e3Vu?a>QDRl4f_G*?PG(-ZA}<#J$+QbW\n" \
+		"\n" \
+		"delta 7\n" \
+		"Oc%18D`@*{63ljhg(E~C7\n";
+
+	opts.flags = GIT_DIFF_SHOW_BINARY | GIT_DIFF_FORCE_BINARY;
+	opts.id_abbrev = GIT_OID_HEXSZ;
+
+	repo = cl_git_sandbox_init("renames");
+	cl_git_pass(git_repository_index(&index, repo));
+
+	cl_git_append2file("renames/untimely.txt", "Oh that crazy Kipling!\r\n");
+
+	cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, &opts));
+
+	cl_git_pass(git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, print_cb, &actual));
+
+	cl_assert_equal_s(expected, actual.ptr);
+
+	git_buf_free(&actual);
+	git_diff_free(diff);
+	git_index_free(index);
+}