binary diff: test index->workdir binary diffs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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);
+}