1 #include "clar_libgit2.h"
3 #include "git2/sys/diff.h"
8 #include "repository.h"
10 static git_repository
*repo
;
12 void test_diff_binary__initialize(void)
16 void test_diff_binary__cleanup(void)
18 cl_git_sandbox_cleanup();
24 const git_diff_options
*opts
,
27 git_oid id_one
, id_two
;
28 git_index
*index
= NULL
;
29 git_commit
*commit_one
, *commit_two
= NULL
;
30 git_tree
*tree_one
, *tree_two
;
33 git_buf actual
= GIT_BUF_INIT
;
35 cl_git_pass(git_oid_fromstr(&id_one
, one
));
36 cl_git_pass(git_commit_lookup(&commit_one
, repo
, &id_one
));
37 cl_git_pass(git_commit_tree(&tree_one
, commit_one
));
40 cl_git_pass(git_oid_fromstr(&id_two
, two
));
41 cl_git_pass(git_commit_lookup(&commit_two
, repo
, &id_two
));
42 cl_git_pass(git_commit_tree(&tree_two
, commit_two
));
44 cl_git_pass(git_repository_index(&index
, repo
));
45 cl_git_pass(git_index_write_tree(&id_two
, index
));
46 cl_git_pass(git_tree_lookup(&tree_two
, repo
, &id_two
));
49 cl_git_pass(git_diff_tree_to_tree(&diff
, repo
, tree_one
, tree_two
, opts
));
51 cl_git_pass(git_patch_from_diff(&patch
, diff
, 0));
52 cl_git_pass(git_patch_to_buf(&actual
, patch
));
54 cl_assert_equal_s(expected
, actual
.ptr
);
56 git_buf_clear(&actual
);
57 cl_git_pass(git_diff_print(diff
, GIT_DIFF_FORMAT_PATCH
, git_diff_print_callback__to_buf
, &actual
));
59 cl_assert_equal_s(expected
, actual
.ptr
);
61 git_buf_dispose(&actual
);
62 git_patch_free(patch
);
64 git_tree_free(tree_one
);
65 git_tree_free(tree_two
);
66 git_commit_free(commit_one
);
67 git_commit_free(commit_two
);
68 git_index_free(index
);
71 void test_diff_binary__add_normal(void)
73 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
74 const char *expected
=
75 "diff --git a/binary.bin b/binary.bin\n" \
76 "new file mode 100644\n" \
77 "index 0000000..bd474b2\n" \
78 "Binary files /dev/null and b/binary.bin differ\n";
80 repo
= cl_git_sandbox_init("diff_format_email");
82 "873806f6f27e631eb0b23e4b56bea2bfac14a373",
83 "897d3af16ca9e420cd071b1c4541bd2b91d04c8c",
88 void test_diff_binary__add(void)
90 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
91 const char *expected
=
92 "diff --git a/binary.bin b/binary.bin\n" \
93 "new file mode 100644\n" \
94 "index 0000000000000000000000000000000000000000..bd474b2519cc15eab801ff851cc7d50f0dee49a1\n" \
95 "GIT binary patch\n" \
103 opts
.flags
= GIT_DIFF_SHOW_BINARY
;
104 opts
.id_abbrev
= GIT_OID_HEXSZ
;
106 repo
= cl_git_sandbox_init("diff_format_email");
108 "873806f6f27e631eb0b23e4b56bea2bfac14a373",
109 "897d3af16ca9e420cd071b1c4541bd2b91d04c8c",
114 void test_diff_binary__modify_normal(void)
116 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
117 const char *expected
=
118 "diff --git a/binary.bin b/binary.bin\n" \
119 "index bd474b2..9ac35ff 100644\n" \
120 "Binary files a/binary.bin and b/binary.bin differ\n";
122 repo
= cl_git_sandbox_init("diff_format_email");
124 "897d3af16ca9e420cd071b1c4541bd2b91d04c8c",
125 "8d7523f6fcb2404257889abe0d96f093d9f524f9",
130 void test_diff_binary__modify(void)
132 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
133 const char *expected
=
134 "diff --git a/binary.bin b/binary.bin\n" \
135 "index bd474b2519cc15eab801ff851cc7d50f0dee49a1..9ac35ff15cd8864aeafd889e4826a3150f0b06c4 100644\n" \
136 "GIT binary patch\n" \
138 "Mc${NkU}WL~000&M4gdfE\n" \
141 "Kc${Nk-~s>u4FC%O\n" \
144 opts
.flags
= GIT_DIFF_SHOW_BINARY
;
146 repo
= cl_git_sandbox_init("diff_format_email");
148 "897d3af16ca9e420cd071b1c4541bd2b91d04c8c",
149 "8d7523f6fcb2404257889abe0d96f093d9f524f9",
154 void test_diff_binary__delete_normal(void)
156 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
157 const char *expected
=
158 "diff --git a/binary.bin b/binary.bin\n" \
159 "deleted file mode 100644\n" \
160 "index bd474b2..0000000\n" \
161 "Binary files a/binary.bin and /dev/null differ\n";
163 repo
= cl_git_sandbox_init("diff_format_email");
165 "897d3af16ca9e420cd071b1c4541bd2b91d04c8c",
166 "873806f6f27e631eb0b23e4b56bea2bfac14a373",
171 void test_diff_binary__delete(void)
173 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
174 const char *expected
=
175 "diff --git a/binary.bin b/binary.bin\n" \
176 "deleted file mode 100644\n" \
177 "index bd474b2519cc15eab801ff851cc7d50f0dee49a1..0000000000000000000000000000000000000000\n" \
178 "GIT binary patch\n" \
183 "Kc${Nk-~s>u4FC%O\n" \
186 opts
.flags
= GIT_DIFF_SHOW_BINARY
;
187 opts
.id_abbrev
= GIT_OID_HEXSZ
;
189 repo
= cl_git_sandbox_init("diff_format_email");
191 "897d3af16ca9e420cd071b1c4541bd2b91d04c8c",
192 "873806f6f27e631eb0b23e4b56bea2bfac14a373",
197 void test_diff_binary__delta(void)
200 git_buf contents
= GIT_BUF_INIT
;
202 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
203 const char *expected
=
204 "diff --git a/songof7cities.txt b/songof7cities.txt\n" \
205 "index 4210ffd5c390b21dd5483375e75288dea9ede512..cc84ec183351c9944ed90a619ca08911924055b5 100644\n" \
206 "GIT binary patch\n" \
208 "zc$}LmI8{(0BqLQJI6p64AwNwaIJGP_Pa)Ye#M3o+qJ$<Jl;sX*mF<MGCYv&*L7AHu\n" \
209 "zGA1*^gt?gYVN82wTbPO_W)+x<&1+cP;HrPHR>PQ;Y(X&QMK*C5^Br3bjG4d=XI^5@\n" \
210 "JfH567LIG)KJdFSV\n" \
213 "zc$}LmI8{(0BqLQJI6p64AwNwaIJGP_Pr*5}Br~;mqJ$<Jl;sX*mF<MGCYv&*L7AHu\n" \
214 "zGA1*^gt?gYVN82wTbPO_W)+x<&1+cP;HrPHR>PQ;Y(X&QMK*C5^Br3bjG4d=XI^5@\n" \
215 "JfH567LIF3FM2!Fd\n" \
218 opts
.flags
= GIT_DIFF_SHOW_BINARY
| GIT_DIFF_FORCE_BINARY
;
219 opts
.id_abbrev
= GIT_OID_HEXSZ
;
221 repo
= cl_git_sandbox_init("renames");
222 cl_git_pass(git_repository_index(&index
, repo
));
224 cl_git_pass(git_futils_readbuffer(&contents
, "renames/songof7cities.txt"));
226 for (i
= 0; i
< contents
.size
- 6; i
++) {
227 if (strncmp(&contents
.ptr
[i
], "Cities", 6) == 0)
228 memcpy(&contents
.ptr
[i
], "cITIES", 6);
231 cl_git_rewritefile("renames/songof7cities.txt", contents
.ptr
);
232 cl_git_pass(git_index_add_bypath(index
, "songof7cities.txt"));
233 cl_git_pass(git_index_write(index
));
236 "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13",
241 git_index_free(index
);
242 git_buf_dispose(&contents
);
245 void test_diff_binary__delta_append(void)
248 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
249 const char *expected
=
250 "diff --git a/untimely.txt b/untimely.txt\n" \
251 "index 9a69d960ae94b060f56c2a8702545e2bb1abb935..1111d4f11f4b35bf6759e0fb714fe09731ef0840 100644\n" \
252 "GIT binary patch\n" \
254 "nc%1vf+QYWt3zLL@hC)e3Vu?a>QDRl4f_G*?PG(-ZA}<#J$+QbW\n" \
257 "Oc%18D`@*{63ljhg(E~C7\n" \
260 opts
.flags
= GIT_DIFF_SHOW_BINARY
| GIT_DIFF_FORCE_BINARY
;
261 opts
.id_abbrev
= GIT_OID_HEXSZ
;
263 repo
= cl_git_sandbox_init("renames");
264 cl_git_pass(git_repository_index(&index
, repo
));
266 cl_git_append2file("renames/untimely.txt", "Oh that crazy Kipling!\r\n");
267 cl_git_pass(git_index_add_bypath(index
, "untimely.txt"));
268 cl_git_pass(git_index_write(index
));
271 "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13",
276 git_index_free(index
);
279 void test_diff_binary__empty_for_no_diff(void)
281 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
286 git_buf actual
= GIT_BUF_INIT
;
288 opts
.flags
= GIT_DIFF_SHOW_BINARY
| GIT_DIFF_FORCE_BINARY
;
289 opts
.id_abbrev
= GIT_OID_HEXSZ
;
291 repo
= cl_git_sandbox_init("renames");
293 cl_git_pass(git_oid_fromstr(&id
, "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13"));
294 cl_git_pass(git_commit_lookup(&commit
, repo
, &id
));
295 cl_git_pass(git_commit_tree(&tree
, commit
));
297 cl_git_pass(git_diff_tree_to_tree(&diff
, repo
, tree
, tree
, &opts
));
298 cl_git_pass(git_diff_print(diff
, GIT_DIFF_FORMAT_PATCH
, git_diff_print_callback__to_buf
, &actual
));
300 cl_assert_equal_s("", actual
.ptr
);
302 git_buf_dispose(&actual
);
304 git_commit_free(commit
);
308 void test_diff_binary__index_to_workdir(void)
313 git_buf actual
= GIT_BUF_INIT
;
314 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
315 const char *expected
=
316 "diff --git a/untimely.txt b/untimely.txt\n" \
317 "index 9a69d960ae94b060f56c2a8702545e2bb1abb935..1111d4f11f4b35bf6759e0fb714fe09731ef0840 100644\n" \
318 "GIT binary patch\n" \
320 "nc%1vf+QYWt3zLL@hC)e3Vu?a>QDRl4f_G*?PG(-ZA}<#J$+QbW\n" \
323 "Oc%18D`@*{63ljhg(E~C7\n" \
326 opts
.flags
= GIT_DIFF_SHOW_BINARY
| GIT_DIFF_FORCE_BINARY
;
327 opts
.id_abbrev
= GIT_OID_HEXSZ
;
329 repo
= cl_git_sandbox_init("renames");
330 cl_git_pass(git_repository_index(&index
, repo
));
332 cl_git_append2file("renames/untimely.txt", "Oh that crazy Kipling!\r\n");
334 cl_git_pass(git_diff_index_to_workdir(&diff
, repo
, index
, &opts
));
336 cl_git_pass(git_patch_from_diff(&patch
, diff
, 0));
337 cl_git_pass(git_patch_to_buf(&actual
, patch
));
339 cl_assert_equal_s(expected
, actual
.ptr
);
341 cl_git_pass(git_index_add_bypath(index
, "untimely.txt"));
342 cl_git_pass(git_index_write(index
));
345 "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13",
350 git_buf_dispose(&actual
);
351 git_patch_free(patch
);
353 git_index_free(index
);
357 const git_diff_delta
*delta
,
358 const git_diff_hunk
*hunk
,
359 const git_diff_line
*line
,
362 git_buf
*buf
= (git_buf
*)payload
;
367 git_buf_put(buf
, hunk
->header
, hunk
->header_len
);
370 git_buf_put(buf
, line
->content
, line
->content_len
);
372 return git_buf_oom(buf
) ? -1 : 0;
375 void test_diff_binary__print_patch_from_diff(void)
379 git_buf actual
= GIT_BUF_INIT
;
380 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
381 const char *expected
=
382 "diff --git a/untimely.txt b/untimely.txt\n" \
383 "index 9a69d960ae94b060f56c2a8702545e2bb1abb935..1111d4f11f4b35bf6759e0fb714fe09731ef0840 100644\n" \
384 "GIT binary patch\n" \
386 "nc%1vf+QYWt3zLL@hC)e3Vu?a>QDRl4f_G*?PG(-ZA}<#J$+QbW\n" \
389 "Oc%18D`@*{63ljhg(E~C7\n" \
392 opts
.flags
= GIT_DIFF_SHOW_BINARY
| GIT_DIFF_FORCE_BINARY
;
393 opts
.id_abbrev
= GIT_OID_HEXSZ
;
395 repo
= cl_git_sandbox_init("renames");
396 cl_git_pass(git_repository_index(&index
, repo
));
398 cl_git_append2file("renames/untimely.txt", "Oh that crazy Kipling!\r\n");
400 cl_git_pass(git_diff_index_to_workdir(&diff
, repo
, index
, &opts
));
402 cl_git_pass(git_diff_print(diff
, GIT_DIFF_FORMAT_PATCH
, print_cb
, &actual
));
404 cl_assert_equal_s(expected
, actual
.ptr
);
406 git_buf_dispose(&actual
);
408 git_index_free(index
);
414 git_buf old_binary_base85
;
415 size_t old_binary_inflatedlen
;
416 git_diff_binary_t old_binary_type
;
420 git_buf new_binary_base85
;
421 size_t new_binary_inflatedlen
;
422 git_diff_binary_t new_binary_type
;
426 const git_diff_delta
*delta
,
430 struct diff_data
*diff_data
= payload
;
432 GIT_UNUSED(progress
);
434 if (delta
->old_file
.path
)
435 diff_data
->old_path
= git__strdup(delta
->old_file
.path
);
437 if (delta
->new_file
.path
)
438 diff_data
->new_path
= git__strdup(delta
->new_file
.path
);
440 git_oid_cpy(&diff_data
->old_id
, &delta
->old_file
.id
);
441 git_oid_cpy(&diff_data
->new_id
, &delta
->new_file
.id
);
446 static int binary_cb(
447 const git_diff_delta
*delta
,
448 const git_diff_binary
*binary
,
451 struct diff_data
*diff_data
= payload
;
455 git_buf_encode_base85(&diff_data
->old_binary_base85
,
456 binary
->old_file
.data
, binary
->old_file
.datalen
);
457 diff_data
->old_binary_inflatedlen
= binary
->old_file
.inflatedlen
;
458 diff_data
->old_binary_type
= binary
->old_file
.type
;
460 git_buf_encode_base85(&diff_data
->new_binary_base85
,
461 binary
->new_file
.data
, binary
->new_file
.datalen
);
462 diff_data
->new_binary_inflatedlen
= binary
->new_file
.inflatedlen
;
463 diff_data
->new_binary_type
= binary
->new_file
.type
;
469 const git_diff_delta
*delta
,
470 const git_diff_hunk
*hunk
,
477 cl_fail("did not expect hunk callback");
482 const git_diff_delta
*delta
,
483 const git_diff_hunk
*hunk
,
484 const git_diff_line
*line
,
492 cl_fail("did not expect line callback");
496 void test_diff_binary__blob_to_blob(void)
499 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
500 git_blob
*old_blob
, *new_blob
;
501 git_oid old_id
, new_id
;
502 struct diff_data diff_data
= {0};
504 opts
.flags
= GIT_DIFF_SHOW_BINARY
| GIT_DIFF_FORCE_BINARY
;
505 opts
.id_abbrev
= GIT_OID_HEXSZ
;
507 repo
= cl_git_sandbox_init("renames");
508 cl_git_pass(git_repository_index__weakptr(&index
, repo
));
510 cl_git_append2file("renames/untimely.txt", "Oh that crazy Kipling!\r\n");
511 cl_git_pass(git_index_add_bypath(index
, "untimely.txt"));
512 cl_git_pass(git_index_write(index
));
514 git_oid_fromstr(&old_id
, "9a69d960ae94b060f56c2a8702545e2bb1abb935");
515 git_oid_fromstr(&new_id
, "1111d4f11f4b35bf6759e0fb714fe09731ef0840");
517 cl_git_pass(git_blob_lookup(&old_blob
, repo
, &old_id
));
518 cl_git_pass(git_blob_lookup(&new_blob
, repo
, &new_id
));
520 cl_git_pass(git_diff_blobs(old_blob
,
521 "untimely.txt", new_blob
, "untimely.txt", &opts
,
522 file_cb
, binary_cb
, hunk_cb
, line_cb
, &diff_data
));
524 cl_assert_equal_s("untimely.txt", diff_data
.old_path
);
525 cl_assert_equal_oid(&old_id
, &diff_data
.old_id
);
526 cl_assert_equal_i(GIT_DIFF_BINARY_DELTA
, diff_data
.old_binary_type
);
527 cl_assert_equal_i(7, diff_data
.old_binary_inflatedlen
);
528 cl_assert_equal_s("c%18D`@*{63ljhg(E~C7",
529 diff_data
.old_binary_base85
.ptr
);
531 cl_assert_equal_s("untimely.txt", diff_data
.new_path
);
532 cl_assert_equal_oid(&new_id
, &diff_data
.new_id
);
533 cl_assert_equal_i(GIT_DIFF_BINARY_DELTA
, diff_data
.new_binary_type
);
534 cl_assert_equal_i(32, diff_data
.new_binary_inflatedlen
);
535 cl_assert_equal_s("c%1vf+QYWt3zLL@hC)e3Vu?a>QDRl4f_G*?PG(-ZA}<#J$+QbW",
536 diff_data
.new_binary_base85
.ptr
);
538 git_blob_free(old_blob
);
539 git_blob_free(new_blob
);
541 git__free(diff_data
.old_path
);
542 git__free(diff_data
.new_path
);
544 git_buf_dispose(&diff_data
.old_binary_base85
);
545 git_buf_dispose(&diff_data
.new_binary_base85
);