Commit 4f806761 by nulltoken

diff: fix the diffing of a concrete blob against a null one

parent 245c5eae
...@@ -686,7 +686,6 @@ int git_diff_print_patch( ...@@ -686,7 +686,6 @@ int git_diff_print_patch(
return error; return error;
} }
int git_diff_blobs( int git_diff_blobs(
git_blob *old_blob, git_blob *old_blob,
git_blob *new_blob, git_blob *new_blob,
...@@ -701,44 +700,43 @@ int git_diff_blobs( ...@@ -701,44 +700,43 @@ int git_diff_blobs(
xpparam_t xdiff_params; xpparam_t xdiff_params;
xdemitconf_t xdiff_config; xdemitconf_t xdiff_config;
xdemitcb_t xdiff_callback; xdemitcb_t xdiff_callback;
git_blob *new, *old;
memset(&delta, 0, sizeof(delta));
new = new_blob;
old = old_blob;
if (options && (options->flags & GIT_DIFF_REVERSE)) { if (options && (options->flags & GIT_DIFF_REVERSE)) {
git_blob *swap = old_blob; git_blob *swap = old;
old_blob = new_blob; old = new;
new_blob = swap; new = swap;
} }
if (old_blob) { if (old) {
old_data.ptr = (char *)git_blob_rawcontent(old_blob); old_data.ptr = (char *)git_blob_rawcontent(old);
old_data.size = git_blob_rawsize(old_blob); old_data.size = git_blob_rawsize(old);
git_oid_cpy(&delta.old_file.oid, git_object_id((const git_object *)old));
} else { } else {
old_data.ptr = ""; old_data.ptr = "";
old_data.size = 0; old_data.size = 0;
} }
if (new_blob) { if (new) {
new_data.ptr = (char *)git_blob_rawcontent(new_blob); new_data.ptr = (char *)git_blob_rawcontent(new);
new_data.size = git_blob_rawsize(new_blob); new_data.size = git_blob_rawsize(new);
git_oid_cpy(&delta.new_file.oid, git_object_id((const git_object *)new));
} else { } else {
new_data.ptr = ""; new_data.ptr = "";
new_data.size = 0; new_data.size = 0;
} }
/* populate a "fake" delta record */ /* populate a "fake" delta record */
delta.status = old_data.ptr ? delta.status = new ?
(new_data.ptr ? GIT_DELTA_MODIFIED : GIT_DELTA_DELETED) : (old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
(new_data.ptr ? GIT_DELTA_ADDED : GIT_DELTA_UNTRACKED); (old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
delta.old_file.mode = 0000000; /* can't know the truth from a blob alone */
delta.new_file.mode = 0000000;
git_oid_cpy(&delta.old_file.oid, git_object_id((const git_object *)old_blob));
git_oid_cpy(&delta.new_file.oid, git_object_id((const git_object *)new_blob));
delta.old_file.path = NULL;
delta.new_file.path = NULL;
delta.old_file.size = old_data.size; delta.old_file.size = old_data.size;
delta.new_file.size = new_data.size; delta.new_file.size = new_data.size;
delta.old_file.flags = 0;
delta.new_file.flags = 0;
delta.similarity = 0;
info.diff = NULL; info.diff = NULL;
info.delta = δ info.delta = δ
......
...@@ -2,23 +2,38 @@ ...@@ -2,23 +2,38 @@
#include "diff_helpers.h" #include "diff_helpers.h"
static git_repository *g_repo = NULL; static git_repository *g_repo = NULL;
static diff_expects exp;
static git_diff_options opts;
static git_blob *d;
void test_diff_blob__initialize(void) void test_diff_blob__initialize(void)
{ {
git_oid d_oid;
g_repo = cl_git_sandbox_init("attr"); g_repo = cl_git_sandbox_init("attr");
memset(&opts, 0, sizeof(opts));
opts.context_lines = 1;
opts.interhunk_lines = 1;
memset(&exp, 0, sizeof(exp));
/* tests/resources/attr/root_test4.txt */
cl_git_pass(git_oid_fromstrn(&d_oid, "fe773770c5a6", 12));
cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &d_oid, 6));
} }
void test_diff_blob__cleanup(void) void test_diff_blob__cleanup(void)
{ {
git_blob_free(d);
cl_git_sandbox_cleanup(); cl_git_sandbox_cleanup();
} }
void test_diff_blob__can_compare_text_blobs(void) void test_diff_blob__can_compare_text_blobs(void)
{ {
git_blob *a, *b, *c, *d; git_blob *a, *b, *c;
git_oid a_oid, b_oid, c_oid, d_oid; git_oid a_oid, b_oid, c_oid;
git_diff_options opts = {0};
diff_expects exp;
/* tests/resources/attr/root_test1 */ /* tests/resources/attr/root_test1 */
cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8)); cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
...@@ -32,16 +47,8 @@ void test_diff_blob__can_compare_text_blobs(void) ...@@ -32,16 +47,8 @@ void test_diff_blob__can_compare_text_blobs(void)
cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16)); cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16));
cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 8)); cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 8));
/* tests/resources/attr/root_test4.txt */
cl_git_pass(git_oid_fromstrn(&d_oid, "fe773770c5a6", 12));
cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &d_oid, 6));
/* Doing the equivalent of a `git diff -U1` on these files */ /* Doing the equivalent of a `git diff -U1` on these files */
opts.context_lines = 1;
opts.interhunk_lines = 1;
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_blobs( cl_git_pass(git_diff_blobs(
a, b, &opts, &exp, diff_hunk_fn, diff_line_fn)); a, b, &opts, &exp, diff_hunk_fn, diff_line_fn));
...@@ -86,6 +93,28 @@ void test_diff_blob__can_compare_text_blobs(void) ...@@ -86,6 +93,28 @@ void test_diff_blob__can_compare_text_blobs(void)
git_blob_free(a); git_blob_free(a);
git_blob_free(b); git_blob_free(b);
git_blob_free(c); git_blob_free(c);
git_blob_free(d);
} }
void test_diff_blob__can_compare_against_null_blobs(void)
{
git_blob *e = NULL;
cl_git_pass(git_diff_blobs(
d, e, &opts, &exp, diff_hunk_fn, diff_line_fn));
cl_assert(exp.hunks == 1);
cl_assert(exp.hunk_old_lines == 14);
cl_assert(exp.lines == 14);
cl_assert(exp.line_dels == 14);
opts.flags |= GIT_DIFF_REVERSE;
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_blobs(
d, e, &opts, &exp, diff_hunk_fn, diff_line_fn));
cl_assert(exp.hunks == 1);
cl_assert(exp.hunk_new_lines == 14);
cl_assert(exp.lines == 14);
cl_assert(exp.line_adds == 14);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment