Blame tests/diff/blob.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "diff_helpers.h"
Packit Service 20376f
Packit Service 20376f
#define BLOB_DIFF \
Packit Service 20376f
    "diff --git a/file b/file\n" \
Packit Service 20376f
    "index 45141a7..4d713dc 100644\n" \
Packit Service 20376f
    "--- a/file\n" \
Packit Service 20376f
    "+++ b/file\n" \
Packit Service 20376f
    "@@ -1 +1,6 @@\n" \
Packit Service 20376f
    " Hello from the root\n" \
Packit Service 20376f
    "+\n" \
Packit Service 20376f
    "+Some additional lines\n" \
Packit Service 20376f
    "+\n" \
Packit Service 20376f
    "+Down here below\n" \
Packit Service 20376f
    "+\n"
Packit Service 20376f
Packit Service 20376f
static git_repository *g_repo = NULL;
Packit Service 20376f
static diff_expects expected;
Packit Service 20376f
static git_diff_options opts;
Packit Service 20376f
static git_blob *d, *alien;
Packit Service 20376f
Packit Service 20376f
static void quick_diff_blob_to_str(
Packit Service 20376f
	const git_blob *blob, const char *blob_path,
Packit Service 20376f
	const char *str, size_t len, const char *str_path)
Packit Service 20376f
{
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
Packit Service 20376f
	if (str && !len)
Packit Service 20376f
		len = strlen(str);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_blob_to_buffer(
Packit Service 20376f
		blob, blob_path, str, len, str_path,
Packit Service 20376f
		&opts, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
Packit Service 20376f
	g_repo = cl_git_sandbox_init("attr");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION));
Packit Service 20376f
	opts.context_lines = 1;
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
Packit Service 20376f
	/* tests/resources/attr/root_test4.txt */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&oid, "a0f7217a", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &oid, 8));
Packit Service 20376f
Packit Service 20376f
	/* alien.png */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&oid, "edf3dcee", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&alien, g_repo, &oid, 8));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	git_blob_free(d);
Packit Service 20376f
	d = NULL;
Packit Service 20376f
Packit Service 20376f
	git_blob_free(alien);
Packit Service 20376f
	alien = NULL;
Packit Service 20376f
Packit Service 20376f
	cl_git_sandbox_cleanup();
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_one_modified(
Packit Service 20376f
	int hunks, int lines, int ctxt, int adds, int dels, diff_expects *exp)
Packit Service 20376f
{
Packit Service 20376f
	cl_assert_equal_i(1, exp->files);
Packit Service 20376f
	cl_assert_equal_i(1, exp->file_status[GIT_DELTA_MODIFIED]);
Packit Service 20376f
	cl_assert_equal_i(0, exp->files_binary);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(hunks, exp->hunks);
Packit Service 20376f
	cl_assert_equal_i(lines, exp->lines);
Packit Service 20376f
	cl_assert_equal_i(ctxt,  exp->line_ctxt);
Packit Service 20376f
	cl_assert_equal_i(adds,  exp->line_adds);
Packit Service 20376f
	cl_assert_equal_i(dels,  exp->line_dels);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__patch_with_freed_blobs(void)
Packit Service 20376f
{
Packit Service 20376f
	git_oid a_oid, b_oid;
Packit Service 20376f
	git_blob *a, *b;
Packit Service 20376f
	git_patch *p;
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	/* tests/resources/attr/root_test1 */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4));
Packit Service 20376f
	/* tests/resources/attr/root_test2 */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, a, NULL, b, NULL, NULL));
Packit Service 20376f
Packit Service 20376f
	git_blob_free(a);
Packit Service 20376f
	git_blob_free(b);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_to_buf(&buf, p));
Packit Service 20376f
	cl_assert_equal_s(buf.ptr, BLOB_DIFF);
Packit Service 20376f
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_compare_text_blobs(void)
Packit Service 20376f
{
Packit Service 20376f
	git_blob *a, *b, *c;
Packit Service 20376f
	git_oid a_oid, b_oid, c_oid;
Packit Service 20376f
Packit Service 20376f
	/* tests/resources/attr/root_test1 */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4));
Packit Service 20376f
Packit Service 20376f
	/* tests/resources/attr/root_test2 */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4));
Packit Service 20376f
Packit Service 20376f
	/* tests/resources/attr/root_test3 */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 16));
Packit Service 20376f
Packit Service 20376f
	/* Doing the equivalent of a `git diff -U1` on these files */
Packit Service 20376f
Packit Service 20376f
	/* diff on tests/resources/attr/root_test1 */
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		a, NULL, b, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
	assert_one_modified(1, 6, 1, 5, 0, &expected);
Packit Service 20376f
Packit Service 20376f
	/* same diff but use direct buffers */
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	cl_git_pass(git_diff_buffers(
Packit Service 20376f
		git_blob_rawcontent(a), (size_t)git_blob_rawsize(a), NULL,
Packit Service 20376f
		git_blob_rawcontent(b), (size_t)git_blob_rawsize(b), NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
	assert_one_modified(1, 6, 1, 5, 0, &expected);
Packit Service 20376f
Packit Service 20376f
	/* diff on tests/resources/attr/root_test2 */
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		b, NULL, c, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
	assert_one_modified(1, 15, 3, 9, 3, &expected);
Packit Service 20376f
Packit Service 20376f
	/* diff on tests/resources/attr/root_test3 */
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		a, NULL, c, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
	assert_one_modified(1, 13, 0, 12, 1, &expected);
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		c, NULL, d, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
	assert_one_modified(2, 14, 4, 6, 4, &expected);
Packit Service 20376f
Packit Service 20376f
	git_blob_free(a);
Packit Service 20376f
	git_blob_free(b);
Packit Service 20376f
	git_blob_free(c);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_patch_matches_blobs(
Packit Service 20376f
	git_patch *p, git_blob *a, git_blob *b,
Packit Service 20376f
	int hunks, int l0, int l1, int ctxt, int adds, int dels)
Packit Service 20376f
{
Packit Service 20376f
	const git_diff_delta *delta;
Packit Service 20376f
	size_t tc, ta, td;
Packit Service 20376f
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
Packit Service 20376f
	delta = git_patch_get_delta(p);
Packit Service 20376f
	cl_assert(delta != NULL);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status);
Packit Service 20376f
	cl_assert_equal_oid(git_blob_id(a), &delta->old_file.id);
Packit Service 20376f
	cl_assert_equal_sz(git_blob_rawsize(a), delta->old_file.size);
Packit Service 20376f
	cl_assert_equal_oid(git_blob_id(b), &delta->new_file.id);
Packit Service 20376f
	cl_assert_equal_sz(git_blob_rawsize(b), delta->new_file.size);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(hunks, (int)git_patch_num_hunks(p));
Packit Service 20376f
Packit Service 20376f
	if (hunks > 0)
Packit Service 20376f
		cl_assert_equal_i(l0, git_patch_num_lines_in_hunk(p, 0));
Packit Service 20376f
	if (hunks > 1)
Packit Service 20376f
		cl_assert_equal_i(l1, git_patch_num_lines_in_hunk(p, 1));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p));
Packit Service 20376f
	cl_assert_equal_i(ctxt, (int)tc);
Packit Service 20376f
	cl_assert_equal_i(adds, (int)ta);
Packit Service 20376f
	cl_assert_equal_i(dels, (int)td);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_compare_text_blobs_with_patch(void)
Packit Service 20376f
{
Packit Service 20376f
	git_blob *a, *b, *c;
Packit Service 20376f
	git_oid a_oid, b_oid, c_oid;
Packit Service 20376f
	git_patch *p;
Packit Service 20376f
Packit Service 20376f
	/* tests/resources/attr/root_test1 */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 8));
Packit Service 20376f
Packit Service 20376f
	/* tests/resources/attr/root_test2 */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 8));
Packit Service 20376f
Packit Service 20376f
	/* tests/resources/attr/root_test3 */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 16));
Packit Service 20376f
Packit Service 20376f
	/* Doing the equivalent of a `git diff -U1` on these files */
Packit Service 20376f
Packit Service 20376f
	/* diff on tests/resources/attr/root_test1 */
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, a, NULL, b, NULL, &opts));
Packit Service 20376f
	assert_patch_matches_blobs(p, a, b, 1, 6, 0, 1, 5, 0);
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	/* diff on tests/resources/attr/root_test2 */
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, b, NULL, c, NULL, &opts));
Packit Service 20376f
	assert_patch_matches_blobs(p, b, c, 1, 15, 0, 3, 9, 3);
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	/* diff on tests/resources/attr/root_test3 */
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, a, NULL, c, NULL, &opts));
Packit Service 20376f
	assert_patch_matches_blobs(p, a, c, 1, 13, 0, 0, 12, 1);
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	/* one more */
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, c, NULL, d, NULL, &opts));
Packit Service 20376f
	assert_patch_matches_blobs(p, c, d, 2, 5, 9, 4, 6, 4);
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	git_blob_free(a);
Packit Service 20376f
	git_blob_free(b);
Packit Service 20376f
	git_blob_free(c);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_compare_against_null_blobs(void)
Packit Service 20376f
{
Packit Service 20376f
	git_blob *e = NULL;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		d, NULL, e, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1, expected.files);
Packit Service 20376f
	cl_assert_equal_i(1, expected.file_status[GIT_DELTA_DELETED]);
Packit Service 20376f
	cl_assert_equal_i(0, expected.files_binary);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1, expected.hunks);
Packit Service 20376f
	cl_assert_equal_i(14, expected.hunk_old_lines);
Packit Service 20376f
	cl_assert_equal_i(14, expected.lines);
Packit Service 20376f
	cl_assert_equal_i(14, expected.line_dels);
Packit Service 20376f
Packit Service 20376f
	opts.flags |= GIT_DIFF_REVERSE;
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		d, NULL, e, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1, expected.files);
Packit Service 20376f
	cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]);
Packit Service 20376f
	cl_assert_equal_i(0, expected.files_binary);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1, expected.hunks);
Packit Service 20376f
	cl_assert_equal_i(14, expected.hunk_new_lines);
Packit Service 20376f
	cl_assert_equal_i(14, expected.lines);
Packit Service 20376f
	cl_assert_equal_i(14, expected.line_adds);
Packit Service 20376f
Packit Service 20376f
	opts.flags ^= GIT_DIFF_REVERSE;
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		alien, NULL, NULL, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1, expected.files);
Packit Service 20376f
	cl_assert_equal_i(1, expected.files_binary);
Packit Service 20376f
	cl_assert_equal_i(1, expected.file_status[GIT_DELTA_DELETED]);
Packit Service 20376f
	cl_assert_equal_i(0, expected.hunks);
Packit Service 20376f
	cl_assert_equal_i(0, expected.lines);
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		NULL, NULL, alien, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1, expected.files);
Packit Service 20376f
	cl_assert_equal_i(1, expected.files_binary);
Packit Service 20376f
	cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]);
Packit Service 20376f
	cl_assert_equal_i(0, expected.hunks);
Packit Service 20376f
	cl_assert_equal_i(0, expected.lines);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_compare_against_null_blobs_with_patch(void)
Packit Service 20376f
{
Packit Service 20376f
	git_blob *e = NULL;
Packit Service 20376f
	git_patch *p;
Packit Service 20376f
	const git_diff_delta *delta;
Packit Service 20376f
	const git_diff_line *line;
Packit Service 20376f
	int l, max_l;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, d, NULL, e, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
Packit Service 20376f
	delta = git_patch_get_delta(p);
Packit Service 20376f
	cl_assert(delta != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_DELETED, delta->status);
Packit Service 20376f
	cl_assert_equal_oid(git_blob_id(d), &delta->old_file.id);
Packit Service 20376f
	cl_assert_equal_sz(git_blob_rawsize(d), delta->old_file.size);
Packit Service 20376f
	cl_assert(git_oid_iszero(&delta->new_file.id));
Packit Service 20376f
	cl_assert_equal_sz(0, delta->new_file.size);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
Packit Service 20376f
	cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0));
Packit Service 20376f
Packit Service 20376f
	max_l = git_patch_num_lines_in_hunk(p, 0);
Packit Service 20376f
	for (l = 0; l < max_l; ++l) {
Packit Service 20376f
		cl_git_pass(git_patch_get_line_in_hunk(&line, p, 0, l));
Packit Service 20376f
		cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	opts.flags |= GIT_DIFF_REVERSE;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, d, NULL, e, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
Packit Service 20376f
	delta = git_patch_get_delta(p);
Packit Service 20376f
	cl_assert(delta != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_ADDED, delta->status);
Packit Service 20376f
	cl_assert(git_oid_iszero(&delta->old_file.id));
Packit Service 20376f
	cl_assert_equal_sz(0, delta->old_file.size);
Packit Service 20376f
	cl_assert_equal_oid(git_blob_id(d), &delta->new_file.id);
Packit Service 20376f
	cl_assert_equal_sz(git_blob_rawsize(d), delta->new_file.size);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
Packit Service 20376f
	cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0));
Packit Service 20376f
Packit Service 20376f
	max_l = git_patch_num_lines_in_hunk(p, 0);
Packit Service 20376f
	for (l = 0; l < max_l; ++l) {
Packit Service 20376f
		cl_git_pass(git_patch_get_line_in_hunk(&line, p, 0, l));
Packit Service 20376f
		cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	opts.flags ^= GIT_DIFF_REVERSE;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, alien, NULL, NULL, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
Packit Service 20376f
	delta = git_patch_get_delta(p);
Packit Service 20376f
	cl_assert(delta != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_DELETED, delta->status);
Packit Service 20376f
	cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
Packit Service 20376f
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, NULL, NULL, alien, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
Packit Service 20376f
	delta = git_patch_get_delta(p);
Packit Service 20376f
	cl_assert(delta != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_ADDED, delta->status);
Packit Service 20376f
	cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
Packit Service 20376f
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_identical_blobs_comparison(diff_expects *expected)
Packit Service 20376f
{
Packit Service 20376f
	cl_assert_equal_i(1, expected->files);
Packit Service 20376f
	cl_assert_equal_i(1, expected->file_status[GIT_DELTA_UNMODIFIED]);
Packit Service 20376f
	cl_assert_equal_i(0, expected->hunks);
Packit Service 20376f
	cl_assert_equal_i(0, expected->lines);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_compare_identical_blobs(void)
Packit Service 20376f
{
Packit Service 20376f
	opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		d, NULL, d, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	assert_identical_blobs_comparison(&expected);
Packit Service 20376f
	cl_assert_equal_i(0, expected.files_binary);
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		NULL, NULL, NULL, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	assert_identical_blobs_comparison(&expected);
Packit Service 20376f
	cl_assert_equal_i(0, expected.files_binary);
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		alien, NULL, alien, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	assert_identical_blobs_comparison(&expected);
Packit Service 20376f
	cl_assert(expected.files_binary > 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_compare_identical_blobs_with_patch(void)
Packit Service 20376f
{
Packit Service 20376f
	git_patch *p;
Packit Service 20376f
	const git_diff_delta *delta;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, d, NULL, d, NULL, &opts));
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
Packit Service 20376f
	delta = git_patch_get_delta(p);
Packit Service 20376f
	cl_assert(delta != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_UNMODIFIED, delta->status);
Packit Service 20376f
	cl_assert_equal_sz(delta->old_file.size, git_blob_rawsize(d));
Packit Service 20376f
	cl_assert_equal_oid(git_blob_id(d), &delta->old_file.id);
Packit Service 20376f
	cl_assert_equal_sz(delta->new_file.size, git_blob_rawsize(d));
Packit Service 20376f
	cl_assert_equal_oid(git_blob_id(d), &delta->new_file.id);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, NULL, NULL, NULL, NULL, &opts));
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
Packit Service 20376f
	delta = git_patch_get_delta(p);
Packit Service 20376f
	cl_assert(delta != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_UNMODIFIED, delta->status);
Packit Service 20376f
	cl_assert_equal_sz(0, delta->old_file.size);
Packit Service 20376f
	cl_assert(git_oid_iszero(&delta->old_file.id));
Packit Service 20376f
	cl_assert_equal_sz(0, delta->new_file.size);
Packit Service 20376f
	cl_assert(git_oid_iszero(&delta->new_file.id));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blobs(&p, alien, NULL, alien, NULL, &opts));
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_get_delta(p)->status);
Packit Service 20376f
	cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_binary_blobs_comparison(diff_expects *expected)
Packit Service 20376f
{
Packit Service 20376f
	cl_assert(expected->files_binary > 0);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1, expected->files);
Packit Service 20376f
	cl_assert_equal_i(1, expected->file_status[GIT_DELTA_MODIFIED]);
Packit Service 20376f
	cl_assert_equal_i(0, expected->hunks);
Packit Service 20376f
	cl_assert_equal_i(0, expected->lines);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_compare_two_binary_blobs(void)
Packit Service 20376f
{
Packit Service 20376f
	git_blob *heart;
Packit Service 20376f
	git_oid h_oid;
Packit Service 20376f
Packit Service 20376f
	/* heart.png */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&h_oid, "de863bff", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&heart, g_repo, &h_oid, 8));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		alien, NULL, heart, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	assert_binary_blobs_comparison(&expected);
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		heart, NULL, alien, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	assert_binary_blobs_comparison(&expected);
Packit Service 20376f
Packit Service 20376f
	git_blob_free(heart);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_compare_a_binary_blob_and_a_text_blob(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		alien, NULL, d, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	assert_binary_blobs_comparison(&expected);
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		d, NULL, alien, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	assert_binary_blobs_comparison(&expected);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * $ git diff fe773770 a0f7217
Packit Service 20376f
 * diff --git a/fe773770 b/a0f7217
Packit Service 20376f
 * index fe77377..a0f7217 100644
Packit Service 20376f
 * --- a/fe773770
Packit Service 20376f
 * +++ b/a0f7217
Packit Service 20376f
 * @@ -1,6 +1,6 @@
Packit Service 20376f
 *  Here is some stuff at the start
Packit Service 20376f
 * 
Packit Service 20376f
 * -This should go in one hunk
Packit Service 20376f
 * +This should go in one hunk (first)
Packit Service 20376f
 * 
Packit Service 20376f
 *  Some additional lines
Packit Service 20376f
 * 
Packit Service 20376f
 * @@ -8,7 +8,7 @@ Down here below the other lines
Packit Service 20376f
 * 
Packit Service 20376f
 *  With even more at the end
Packit Service 20376f
 * 
Packit Service 20376f
 * -Followed by a second hunk of stuff
Packit Service 20376f
 * +Followed by a second hunk of stuff (second)
Packit Service 20376f
 * 
Packit Service 20376f
 *  That happens down here
Packit Service 20376f
 */
Packit Service 20376f
void test_diff_blob__comparing_two_text_blobs_honors_interhunkcontext(void)
Packit Service 20376f
{
Packit Service 20376f
	git_blob *old_d;
Packit Service 20376f
	git_oid old_d_oid;
Packit Service 20376f
Packit Service 20376f
	opts.context_lines = 3;
Packit Service 20376f
Packit Service 20376f
	/* tests/resources/attr/root_test1 from commit f5b0af1 */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&old_d_oid, "fe773770", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&old_d, g_repo, &old_d_oid, 8));
Packit Service 20376f
Packit Service 20376f
	/* Test with default inter-hunk-context (not set) => default is 0 */
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		old_d, NULL, d, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(2, expected.hunks);
Packit Service 20376f
Packit Service 20376f
	/* Test with inter-hunk-context explicitly set to 0 */
Packit Service 20376f
	opts.interhunk_lines = 0;
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		old_d, NULL, d, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(2, expected.hunks);
Packit Service 20376f
Packit Service 20376f
	/* Test with inter-hunk-context explicitly set to 1 */
Packit Service 20376f
	opts.interhunk_lines = 1;
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		old_d, NULL, d, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1, expected.hunks);
Packit Service 20376f
Packit Service 20376f
	git_blob_free(old_d);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__checks_options_version_too_low(void)
Packit Service 20376f
{
Packit Service 20376f
	const git_error *err;
Packit Service 20376f
Packit Service 20376f
	opts.version = 0;
Packit Service 20376f
	cl_git_fail(git_diff_blobs(
Packit Service 20376f
		d, NULL, alien, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
	err = giterr_last();
Packit Service 20376f
	cl_assert_equal_i(GITERR_INVALID, err->klass);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__checks_options_version_too_high(void)
Packit Service 20376f
{
Packit Service 20376f
	const git_error *err;
Packit Service 20376f
Packit Service 20376f
	opts.version = 1024;
Packit Service 20376f
	cl_git_fail(git_diff_blobs(
Packit Service 20376f
		d, NULL, alien, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
	err = giterr_last();
Packit Service 20376f
	cl_assert_equal_i(GITERR_INVALID, err->klass);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_correctly_detect_a_binary_blob_as_binary(void)
Packit Service 20376f
{
Packit Service 20376f
	/* alien.png */
Packit Service 20376f
	cl_assert_equal_i(true, git_blob_is_binary(alien));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_correctly_detect_a_textual_blob_as_non_binary(void)
Packit Service 20376f
{
Packit Service 20376f
	/* tests/resources/attr/root_test4.txt */
Packit Service 20376f
	cl_assert_equal_i(false, git_blob_is_binary(d));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * git_diff_blob_to_buffer tests
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
static void assert_changed_single_one_line_file(
Packit Service 20376f
	diff_expects *expected, git_delta_t mod)
Packit Service 20376f
{
Packit Service 20376f
	cl_assert_equal_i(1, expected->files);
Packit Service 20376f
	cl_assert_equal_i(1, expected->file_status[mod]);
Packit Service 20376f
	cl_assert_equal_i(1, expected->hunks);
Packit Service 20376f
	cl_assert_equal_i(1, expected->lines);
Packit Service 20376f
Packit Service 20376f
	if (mod == GIT_DELTA_ADDED)
Packit Service 20376f
		cl_assert_equal_i(1, expected->line_adds);
Packit Service 20376f
	else if (mod == GIT_DELTA_DELETED)
Packit Service 20376f
		cl_assert_equal_i(1, expected->line_dels);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_compare_blob_to_buffer(void)
Packit Service 20376f
{
Packit Service 20376f
	git_blob *a;
Packit Service 20376f
	git_oid a_oid;
Packit Service 20376f
	const char *a_content = "Hello from the root\n";
Packit Service 20376f
	const char *b_content = "Hello from the root\n\nSome additional lines\n\nDown here below\n\n";
Packit Service 20376f
Packit Service 20376f
	/* tests/resources/attr/root_test1 */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 8));
Packit Service 20376f
Packit Service 20376f
	/* diff from blob a to content of b */
Packit Service 20376f
	quick_diff_blob_to_str(a, NULL, b_content, 0, NULL);
Packit Service 20376f
	assert_one_modified(1, 6, 1, 5, 0, &expected);
Packit Service 20376f
Packit Service 20376f
	/* diff from blob a to content of a */
Packit Service 20376f
	opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
Packit Service 20376f
	quick_diff_blob_to_str(a, NULL, a_content, 0, NULL);
Packit Service 20376f
	assert_identical_blobs_comparison(&expected);
Packit Service 20376f
Packit Service 20376f
	/* diff from NULL blob to content of a */
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	quick_diff_blob_to_str(NULL, NULL, a_content, 0, NULL);
Packit Service 20376f
	assert_changed_single_one_line_file(&expected, GIT_DELTA_ADDED);
Packit Service 20376f
Packit Service 20376f
	/* diff from blob a to NULL buffer */
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	quick_diff_blob_to_str(a, NULL, NULL, 0, NULL);
Packit Service 20376f
	assert_changed_single_one_line_file(&expected, GIT_DELTA_DELETED);
Packit Service 20376f
Packit Service 20376f
	/* diff with reverse */
Packit Service 20376f
	opts.flags ^= GIT_DIFF_REVERSE;
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	quick_diff_blob_to_str(a, NULL, NULL, 0, NULL);
Packit Service 20376f
	assert_changed_single_one_line_file(&expected, GIT_DELTA_ADDED);
Packit Service 20376f
Packit Service 20376f
	git_blob_free(a);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_compare_blob_to_buffer_with_patch(void)
Packit Service 20376f
{
Packit Service 20376f
	git_patch *p;
Packit Service 20376f
	git_blob *a;
Packit Service 20376f
	git_oid a_oid;
Packit Service 20376f
	const char *a_content = "Hello from the root\n";
Packit Service 20376f
	const char *b_content = "Hello from the root\n\nSome additional lines\n\nDown here below\n\n";
Packit Service 20376f
	size_t tc, ta, td;
Packit Service 20376f
Packit Service 20376f
	/* tests/resources/attr/root_test1 */
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 8));
Packit Service 20376f
Packit Service 20376f
	/* diff from blob a to content of b */
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, a, NULL, b_content, strlen(b_content), NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_MODIFIED, git_patch_get_delta(p)->status);
Packit Service 20376f
	cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
Packit Service 20376f
	cl_assert_equal_i(6, git_patch_num_lines_in_hunk(p, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p));
Packit Service 20376f
	cl_assert_equal_i(1, (int)tc);
Packit Service 20376f
	cl_assert_equal_i(5, (int)ta);
Packit Service 20376f
	cl_assert_equal_i(0, (int)td);
Packit Service 20376f
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	/* diff from blob a to content of a */
Packit Service 20376f
	opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, a, NULL, a_content, strlen(a_content), NULL, &opts));
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_get_delta(p)->status);
Packit Service 20376f
	cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	/* diff from NULL blob to content of a */
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, NULL, NULL, a_content, strlen(a_content), NULL, &opts));
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_get_delta(p)->status);
Packit Service 20376f
	cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
Packit Service 20376f
	cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0));
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	/* diff from blob a to NULL buffer */
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, a, NULL, NULL, 0, NULL, &opts));
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_DELETED, git_patch_get_delta(p)->status);
Packit Service 20376f
	cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
Packit Service 20376f
	cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0));
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	/* diff with reverse */
Packit Service 20376f
	opts.flags ^= GIT_DIFF_REVERSE;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, a, NULL, NULL, 0, NULL, &opts));
Packit Service 20376f
	cl_assert(p != NULL);
Packit Service 20376f
	cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_get_delta(p)->status);
Packit Service 20376f
	cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
Packit Service 20376f
	cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0));
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	git_blob_free(a);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_one_modified_with_lines(diff_expects *expected, int lines)
Packit Service 20376f
{
Packit Service 20376f
	cl_assert_equal_i(1, expected->files);
Packit Service 20376f
	cl_assert_equal_i(1, expected->file_status[GIT_DELTA_MODIFIED]);
Packit Service 20376f
	cl_assert_equal_i(0, expected->files_binary);
Packit Service 20376f
	cl_assert_equal_i(lines, expected->lines);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__binary_data_comparisons(void)
Packit Service 20376f
{
Packit Service 20376f
	git_blob *bin, *nonbin;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	const char *nonbin_content = "Hello from the root\n";
Packit Service 20376f
	size_t nonbin_len = 20;
Packit Service 20376f
	const char *bin_content = "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n";
Packit Service 20376f
	size_t bin_len = 33;
Packit Service 20376f
Packit Service 20376f
	opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&oid, "45141a79", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&nonbin, g_repo, &oid, 8));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&oid, "b435cd56", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&bin, g_repo, &oid, 8));
Packit Service 20376f
Packit Service 20376f
	/* non-binary to reference content */
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(nonbin, NULL, nonbin_content, nonbin_len, NULL);
Packit Service 20376f
	assert_identical_blobs_comparison(&expected);
Packit Service 20376f
	cl_assert_equal_i(0, expected.files_binary);
Packit Service 20376f
Packit Service 20376f
	/* binary to reference content */
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(bin, NULL, bin_content, bin_len, NULL);
Packit Service 20376f
	assert_identical_blobs_comparison(&expected);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(1, expected.files_binary);
Packit Service 20376f
Packit Service 20376f
	/* non-binary to binary content */
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(nonbin, NULL, bin_content, bin_len, NULL);
Packit Service 20376f
	assert_binary_blobs_comparison(&expected);
Packit Service 20376f
Packit Service 20376f
	/* binary to non-binary content */
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(bin, NULL, nonbin_content, nonbin_len, NULL);
Packit Service 20376f
	assert_binary_blobs_comparison(&expected);
Packit Service 20376f
Packit Service 20376f
	/* non-binary to binary blob */
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		bin, NULL, nonbin, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
	assert_binary_blobs_comparison(&expected);
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * repeat with FORCE_TEXT
Packit Service 20376f
	 */
Packit Service 20376f
Packit Service 20376f
	opts.flags |= GIT_DIFF_FORCE_TEXT;
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(bin, NULL, bin_content, bin_len, NULL);
Packit Service 20376f
	assert_identical_blobs_comparison(&expected);
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(nonbin, NULL, bin_content, bin_len, NULL);
Packit Service 20376f
	assert_one_modified_with_lines(&expected, 4);
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(bin, NULL, nonbin_content, nonbin_len, NULL);
Packit Service 20376f
	assert_one_modified_with_lines(&expected, 4);
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
	cl_git_pass(git_diff_blobs(
Packit Service 20376f
		bin, NULL, nonbin, NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
	assert_one_modified_with_lines(&expected, 4);
Packit Service 20376f
Packit Service 20376f
	/* cleanup */
Packit Service 20376f
	git_blob_free(bin);
Packit Service 20376f
	git_blob_free(nonbin);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__using_path_and_attributes(void)
Packit Service 20376f
{
Packit Service 20376f
	git_config *cfg;
Packit Service 20376f
	git_blob *bin, *nonbin;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	const char *nonbin_content = "Hello from the root\n";
Packit Service 20376f
	const char *bin_content =
Packit Service 20376f
		"0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n";
Packit Service 20376f
	size_t bin_len = 33;
Packit Service 20376f
	const char *changed;
Packit Service 20376f
	git_patch *p;
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	/* set up custom diff drivers and 'diff' attribute mappings for them */
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_config(&cfg, g_repo));
Packit Service 20376f
	cl_git_pass(git_config_set_bool(cfg, "diff.iam_binary.binary", 1));
Packit Service 20376f
	cl_git_pass(git_config_set_bool(cfg, "diff.iam_text.binary", 0));
Packit Service 20376f
	cl_git_pass(git_config_set_string(
Packit Service 20376f
		cfg, "diff.iam_alphactx.xfuncname", "^[A-Za-z].*$"));
Packit Service 20376f
	cl_git_pass(git_config_set_bool(cfg, "diff.iam_textalpha.binary", 0));
Packit Service 20376f
	cl_git_pass(git_config_set_string(
Packit Service 20376f
		cfg, "diff.iam_textalpha.xfuncname", "^[A-Za-z].*$"));
Packit Service 20376f
	cl_git_pass(git_config_set_string(
Packit Service 20376f
		cfg, "diff.iam_numctx.funcname", "^[0-9][0-9]*"));
Packit Service 20376f
	cl_git_pass(git_config_set_bool(cfg, "diff.iam_textnum.binary", 0));
Packit Service 20376f
	cl_git_pass(git_config_set_string(
Packit Service 20376f
		cfg, "diff.iam_textnum.funcname", "^[0-9][0-9]*"));
Packit Service 20376f
	git_config_free(cfg);
Packit Service 20376f
Packit Service 20376f
	cl_git_append2file(
Packit Service 20376f
		"attr/.gitattributes",
Packit Service 20376f
		"\n\n# test_diff_blob__using_path_and_attributes extra\n\n"
Packit Service 20376f
		"*.binary  diff=iam_binary\n"
Packit Service 20376f
		"*.textary diff=iam_text\n"
Packit Service 20376f
		"*.alphary diff=iam_alphactx\n"
Packit Service 20376f
		"*.textalphary diff=iam_textalpha\n"
Packit Service 20376f
		"*.textnumary diff=iam_textnum\n"
Packit Service 20376f
		"*.numary  diff=iam_numctx\n\n");
Packit Service 20376f
Packit Service 20376f
	opts.context_lines = 0;
Packit Service 20376f
	opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&oid, "45141a79", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&nonbin, g_repo, &oid, 8));
Packit Service 20376f
	/* 20b: "Hello from the root\n" */
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&oid, "b435cd56", 8));
Packit Service 20376f
	cl_git_pass(git_blob_lookup_prefix(&bin, g_repo, &oid, 8));
Packit Service 20376f
	/* 33b: "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\n0123456789\n" */
Packit Service 20376f
Packit Service 20376f
	/* non-binary to reference content */
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(nonbin, NULL, nonbin_content, 0, NULL);
Packit Service 20376f
	assert_identical_blobs_comparison(&expected);
Packit Service 20376f
	cl_assert_equal_i(0, expected.files_binary);
Packit Service 20376f
Packit Service 20376f
	/* binary to reference content */
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(bin, NULL, bin_content, bin_len, NULL);
Packit Service 20376f
	assert_identical_blobs_comparison(&expected);
Packit Service 20376f
	cl_assert_equal_i(1, expected.files_binary);
Packit Service 20376f
Packit Service 20376f
	/* add some text */
Packit Service 20376f
Packit Service 20376f
	changed = "Hello from the root\nMore lines\nAnd more\nGo here\n";
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(nonbin, NULL, changed, 0, NULL);
Packit Service 20376f
	assert_one_modified(1, 3, 0, 3, 0, &expected);
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(nonbin, "foo/bar.binary", changed, 0, NULL);
Packit Service 20376f
	cl_assert_equal_i(1, expected.files);
Packit Service 20376f
	cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
Packit Service 20376f
	cl_assert_equal_i(1, expected.files_binary);
Packit Service 20376f
	cl_assert_equal_i(0, expected.hunks);
Packit Service 20376f
	cl_assert_equal_i(0, expected.lines);
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(nonbin, "foo/bar.textary", changed, 0, NULL);
Packit Service 20376f
	assert_one_modified(1, 3, 0, 3, 0, &expected);
Packit Service 20376f
Packit Service 20376f
	quick_diff_blob_to_str(nonbin, "foo/bar.alphary", changed, 0, NULL);
Packit Service 20376f
	assert_one_modified(1, 3, 0, 3, 0, &expected);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, nonbin, "zzz.normal", changed, strlen(changed), NULL, &opts));
Packit Service 20376f
	cl_git_pass(git_patch_to_buf(&buf, p));
Packit Service 20376f
	cl_assert_equal_s(
Packit Service 20376f
		"diff --git a/zzz.normal b/zzz.normal\n"
Packit Service 20376f
		"index 45141a7..75b0dbb 100644\n"
Packit Service 20376f
		"--- a/zzz.normal\n"
Packit Service 20376f
		"+++ b/zzz.normal\n"
Packit Service 20376f
		"@@ -1,0 +2,3 @@ Hello from the root\n"
Packit Service 20376f
		"+More lines\n"
Packit Service 20376f
		"+And more\n"
Packit Service 20376f
		"+Go here\n", buf.ptr);
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, nonbin, "zzz.binary", changed, strlen(changed), NULL, &opts));
Packit Service 20376f
	cl_git_pass(git_patch_to_buf(&buf, p));
Packit Service 20376f
	cl_assert_equal_s(
Packit Service 20376f
		"diff --git a/zzz.binary b/zzz.binary\n"
Packit Service 20376f
		"index 45141a7..75b0dbb 100644\n"
Packit Service 20376f
		"Binary files a/zzz.binary and b/zzz.binary differ\n", buf.ptr);
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, nonbin, "zzz.alphary", changed, strlen(changed), NULL, &opts));
Packit Service 20376f
	cl_git_pass(git_patch_to_buf(&buf, p));
Packit Service 20376f
	cl_assert_equal_s(
Packit Service 20376f
		"diff --git a/zzz.alphary b/zzz.alphary\n"
Packit Service 20376f
		"index 45141a7..75b0dbb 100644\n"
Packit Service 20376f
		"--- a/zzz.alphary\n"
Packit Service 20376f
		"+++ b/zzz.alphary\n"
Packit Service 20376f
		"@@ -1,0 +2,3 @@ Hello from the root\n"
Packit Service 20376f
		"+More lines\n"
Packit Service 20376f
		"+And more\n"
Packit Service 20376f
		"+Go here\n", buf.ptr);
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, nonbin, "zzz.numary", changed, strlen(changed), NULL, &opts));
Packit Service 20376f
	cl_git_pass(git_patch_to_buf(&buf, p));
Packit Service 20376f
	cl_assert_equal_s(
Packit Service 20376f
		"diff --git a/zzz.numary b/zzz.numary\n"
Packit Service 20376f
		"index 45141a7..75b0dbb 100644\n"
Packit Service 20376f
		"--- a/zzz.numary\n"
Packit Service 20376f
		"+++ b/zzz.numary\n"
Packit Service 20376f
		"@@ -1,0 +2,3 @@\n"
Packit Service 20376f
		"+More lines\n"
Packit Service 20376f
		"+And more\n"
Packit Service 20376f
		"+Go here\n", buf.ptr);
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	/* "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n"
Packit Service 20376f
	 * 33 bytes
Packit Service 20376f
	 */
Packit Service 20376f
Packit Service 20376f
	changed = "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\nreplace a line\n";
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, bin, "zzz.normal", changed, 37, NULL, &opts));
Packit Service 20376f
	cl_git_pass(git_patch_to_buf(&buf, p));
Packit Service 20376f
	cl_assert_equal_s(
Packit Service 20376f
		"diff --git a/zzz.normal b/zzz.normal\n"
Packit Service 20376f
		"index b435cd5..1604519 100644\n"
Packit Service 20376f
		"Binary files a/zzz.normal and b/zzz.normal differ\n", buf.ptr);
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, bin, "zzz.textary", changed, 37, NULL, &opts));
Packit Service 20376f
	cl_git_pass(git_patch_to_buf(&buf, p));
Packit Service 20376f
	cl_assert_equal_s(
Packit Service 20376f
		"diff --git a/zzz.textary b/zzz.textary\n"
Packit Service 20376f
		"index b435cd5..1604519 100644\n"
Packit Service 20376f
		"--- a/zzz.textary\n"
Packit Service 20376f
		"+++ b/zzz.textary\n"
Packit Service 20376f
		"@@ -3 +3 @@\n"
Packit Service 20376f
		"-0123456789\n"
Packit Service 20376f
		"+replace a line\n", buf.ptr);
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, bin, "zzz.textalphary", changed, 37, NULL, &opts));
Packit Service 20376f
	cl_git_pass(git_patch_to_buf(&buf, p));
Packit Service 20376f
	cl_assert_equal_s(
Packit Service 20376f
		"diff --git a/zzz.textalphary b/zzz.textalphary\n"
Packit Service 20376f
		"index b435cd5..1604519 100644\n"
Packit Service 20376f
		"--- a/zzz.textalphary\n"
Packit Service 20376f
		"+++ b/zzz.textalphary\n"
Packit Service 20376f
		"@@ -3 +3 @@\n"
Packit Service 20376f
		"-0123456789\n"
Packit Service 20376f
		"+replace a line\n", buf.ptr);
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_patch_from_blob_and_buffer(
Packit Service 20376f
		&p, bin, "zzz.textnumary", changed, 37, NULL, &opts));
Packit Service 20376f
	cl_git_pass(git_patch_to_buf(&buf, p));
Packit Service 20376f
	cl_assert_equal_s(
Packit Service 20376f
		"diff --git a/zzz.textnumary b/zzz.textnumary\n"
Packit Service 20376f
		"index b435cd5..1604519 100644\n"
Packit Service 20376f
		"--- a/zzz.textnumary\n"
Packit Service 20376f
		"+++ b/zzz.textnumary\n"
Packit Service 20376f
		"@@ -3 +3 @@ 0123456789\n"
Packit Service 20376f
		"-0123456789\n"
Packit Service 20376f
		"+replace a line\n", buf.ptr);
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
	git_patch_free(p);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
	git_blob_free(nonbin);
Packit Service 20376f
	git_blob_free(bin);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_diff_blob__can_compare_buffer_to_buffer(void)
Packit Service 20376f
{
Packit Service 20376f
	const char *a = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n";
Packit Service 20376f
	const char *b = "a\nB\nc\nd\nE\nF\nh\nj\nk\n";
Packit Service 20376f
Packit Service 20376f
	opts.interhunk_lines = 0;
Packit Service 20376f
	opts.context_lines = 0;
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_buffers(
Packit Service 20376f
		a, strlen(a), NULL, b, strlen(b), NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
	assert_one_modified(4, 9, 0, 4, 5, &expected);
Packit Service 20376f
Packit Service 20376f
	opts.flags ^= GIT_DIFF_REVERSE;
Packit Service 20376f
Packit Service 20376f
	memset(&expected, 0, sizeof(expected));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_diff_buffers(
Packit Service 20376f
		a, strlen(a), NULL, b, strlen(b), NULL, &opts,
Packit Service 20376f
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
Packit Service 20376f
	assert_one_modified(4, 9, 0, 5, 4, &expected);
Packit Service 20376f
}