/** * ggit_patch_new_from_blobs: * @old_blob: (allow-none): a #GgitBlob to diff from. * @old_as_path: (allow-none): treat @old_blob as if it had this filename, or %NULL, * @new_blob: (allow-none): a #GgitBlob to diff to. * @new_as_path: (allow-none): treat @new_blob as if it had this filename, or %NULL, * @diff_options: (allow-none): a #GgitDiffOptions, or %NULL. * @error: a #GError for error reporting, or %NULL. * * Directly generate a patch from the difference between two blobs. * * This is just like ggit_diff_blobs() except it generates a patch object * for the difference instead of directly making callbacks. You can use the * standard ggit_patch accessor functions to read the patch data, and * you must call ggit_patch_unref on the patch when done. */ GgitPatch * ggit_patch_new_from_blobs (GgitBlob *old_blob, const gchar *old_as_path, GgitBlob *new_blob, const gchar *new_as_path, GgitDiffOptions *diff_options, GError **error) { gint ret; const git_diff_options *gdiff_options; git_patch *patch; g_return_val_if_fail (error == NULL || *error == NULL, NULL); gdiff_options = _ggit_diff_options_get_diff_options (diff_options); ret = git_patch_from_blobs (&patch, old_blob ? _ggit_native_get (old_blob) : NULL, old_as_path, new_blob ? _ggit_native_get (new_blob) : NULL, new_as_path, (git_diff_options *) gdiff_options); if (ret != GIT_OK) { _ggit_error_set (error, ret); return NULL; } return _ggit_patch_wrap (patch); }
void test_diff_blob__patch_with_freed_blobs(void) { git_oid a_oid, b_oid; git_blob *a, *b; git_patch *p; git_buf buf = GIT_BUF_INIT; /* tests/resources/attr/root_test1 */ cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8)); cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4)); /* tests/resources/attr/root_test2 */ cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8)); cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4)); cl_git_pass(git_patch_from_blobs(&p, a, NULL, b, NULL, NULL)); git_blob_free(a); git_blob_free(b); cl_git_pass(git_patch_to_buf(&buf, p)); cl_assert_equal_s(buf.ptr, BLOB_DIFF); git_patch_free(p); git_buf_free(&buf); }
void test_diff_blob__can_compare_text_blobs_with_patch(void) { git_blob *a, *b, *c; git_oid a_oid, b_oid, c_oid; git_patch *p; /* tests/resources/attr/root_test1 */ cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8)); cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 8)); /* tests/resources/attr/root_test2 */ cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8)); cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 8)); /* tests/resources/attr/root_test3 */ cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16)); cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 16)); /* Doing the equivalent of a `git diff -U1` on these files */ /* diff on tests/resources/attr/root_test1 */ cl_git_pass(git_patch_from_blobs(&p, a, NULL, b, NULL, &opts)); assert_patch_matches_blobs(p, a, b, 1, 6, 0, 1, 5, 0); git_patch_free(p); /* diff on tests/resources/attr/root_test2 */ cl_git_pass(git_patch_from_blobs(&p, b, NULL, c, NULL, &opts)); assert_patch_matches_blobs(p, b, c, 1, 15, 0, 3, 9, 3); git_patch_free(p); /* diff on tests/resources/attr/root_test3 */ cl_git_pass(git_patch_from_blobs(&p, a, NULL, c, NULL, &opts)); assert_patch_matches_blobs(p, a, c, 1, 13, 0, 0, 12, 1); git_patch_free(p); /* one more */ cl_git_pass(git_patch_from_blobs(&p, c, NULL, d, NULL, &opts)); assert_patch_matches_blobs(p, c, d, 2, 5, 9, 4, 6, 4); git_patch_free(p); git_blob_free(a); git_blob_free(b); git_blob_free(c); }
void test_diff_blob__can_compare_identical_blobs_with_patch(void) { git_patch *p; const git_diff_delta *delta; cl_git_pass(git_patch_from_blobs(&p, d, NULL, d, NULL, &opts)); cl_assert(p != NULL); delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_UNMODIFIED, delta->status); cl_assert_equal_sz(delta->old_file.size, git_blob_rawsize(d)); cl_assert_equal_oid(git_blob_id(d), &delta->old_file.id); cl_assert_equal_sz(delta->new_file.size, git_blob_rawsize(d)); cl_assert_equal_oid(git_blob_id(d), &delta->new_file.id); cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); git_patch_free(p); cl_git_pass(git_patch_from_blobs(&p, NULL, NULL, NULL, NULL, &opts)); cl_assert(p != NULL); delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_UNMODIFIED, delta->status); cl_assert_equal_sz(0, delta->old_file.size); cl_assert(git_oid_iszero(&delta->old_file.id)); cl_assert_equal_sz(0, delta->new_file.size); cl_assert(git_oid_iszero(&delta->new_file.id)); cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); git_patch_free(p); cl_git_pass(git_patch_from_blobs(&p, alien, NULL, alien, NULL, &opts)); cl_assert(p != NULL); cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_get_delta(p)->status); cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); git_patch_free(p); }
PyObject * Blob_diff(Blob *self, PyObject *args, PyObject *kwds) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_patch *patch; char *old_as_path = NULL, *new_as_path = NULL; Blob *py_blob = NULL; int err; char *keywords[] = {"blob", "flag", "old_as_path", "new_as_path", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O!ssI", keywords, &BlobType, &py_blob, &opts.flags, &old_as_path, &new_as_path)) return NULL; err = git_patch_from_blobs(&patch, self->blob, old_as_path, py_blob ? py_blob->blob : NULL, new_as_path, &opts); if (err < 0) return Error_set(err); return wrap_patch(patch); }
void test_diff_blob__can_compare_against_null_blobs_with_patch(void) { git_blob *e = NULL; git_patch *p; const git_diff_delta *delta; const git_diff_line *line; int l, max_l; cl_git_pass(git_patch_from_blobs(&p, d, NULL, e, NULL, &opts)); cl_assert(p != NULL); delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); cl_assert_equal_oid(git_blob_id(d), &delta->old_file.id); cl_assert_equal_sz(git_blob_rawsize(d), delta->old_file.size); cl_assert(git_oid_iszero(&delta->new_file.id)); cl_assert_equal_sz(0, delta->new_file.size); cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0)); max_l = git_patch_num_lines_in_hunk(p, 0); for (l = 0; l < max_l; ++l) { cl_git_pass(git_patch_get_line_in_hunk(&line, p, 0, l)); cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin); } git_patch_free(p); opts.flags |= GIT_DIFF_REVERSE; cl_git_pass(git_patch_from_blobs(&p, d, NULL, e, NULL, &opts)); cl_assert(p != NULL); delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_ADDED, delta->status); cl_assert(git_oid_iszero(&delta->old_file.id)); cl_assert_equal_sz(0, delta->old_file.size); cl_assert_equal_oid(git_blob_id(d), &delta->new_file.id); cl_assert_equal_sz(git_blob_rawsize(d), delta->new_file.size); cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0)); max_l = git_patch_num_lines_in_hunk(p, 0); for (l = 0; l < max_l; ++l) { cl_git_pass(git_patch_get_line_in_hunk(&line, p, 0, l)); cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin); } git_patch_free(p); opts.flags ^= GIT_DIFF_REVERSE; cl_git_pass(git_patch_from_blobs(&p, alien, NULL, NULL, NULL, &opts)); cl_assert(p != NULL); delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0); cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); git_patch_free(p); cl_git_pass(git_patch_from_blobs(&p, NULL, NULL, alien, NULL, &opts)); cl_assert(p != NULL); delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_ADDED, delta->status); cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0); cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); git_patch_free(p); }