Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cavaquinho/diff index to workdir zeroed oids 6296 #6297

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/libgit2/diff_generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ static int diff_delta__from_one(
delta->new_file.id_abbrev = (uint16_t)git_oid_hexsize(oid_type);
}

delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
if (!has_old || !git_oid_is_zero(&delta->old_file.id))
delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;

if (has_old || !git_oid_is_zero(&delta->new_file.id))
delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
Expand Down Expand Up @@ -264,8 +265,10 @@ static int diff_delta__from_two(
delta->old_file.mode = old_mode;
git_oid_cpy(&delta->old_file.id, old_id);
delta->old_file.id_abbrev = (uint16_t)git_oid_hexsize(oid_type);
delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID |
GIT_DIFF_FLAG_EXISTS;
delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;

if (!git_oid_is_zero(&old_entry->id))
delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
}

if (!git_index_entry_is_conflict(new_entry)) {
Expand Down
77 changes: 72 additions & 5 deletions tests/libgit2/diff/workdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void test_diff_workdir__to_index(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_diff *reverse_diff = NULL;
diff_expects exp;
int use_iterator;

Expand Down Expand Up @@ -59,6 +60,51 @@ void test_diff_workdir__to_index(void)
cl_assert_equal_i(5, exp.line_dels);
}

/* Let's try that once more with a reversed diff */
/* Free `diff` later. We want to compare it with the reverse diff first. */

opts.flags |= GIT_DIFF_REVERSE;

cl_git_pass(git_diff_index_to_workdir(&reverse_diff, g_repo, NULL, &opts));

memset(&exp, 0, sizeof(exp));

cl_git_pass(git_diff_foreach(
reverse_diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));

cl_assert_equal_i(13, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNTRACKED]);

cl_assert_equal_i(8, exp.hunks);

cl_assert_equal_i(14, exp.lines);
cl_assert_equal_i(5, exp.line_ctxt);
cl_assert_equal_i(4, exp.line_dels);
cl_assert_equal_i(5, exp.line_adds);

/* Verify if the reverse diff has the same OIDs as the direct diff. */
{
size_t delta_idx;

cl_assert_equal_i(git_diff_num_deltas(diff),
git_diff_num_deltas(reverse_diff));

for (delta_idx = 0; delta_idx < git_diff_num_deltas(diff); delta_idx++) {
const git_diff_delta *direct_delta = git_diff_get_delta(diff, delta_idx);
const git_diff_delta *reverse_delta = git_diff_get_delta(reverse_diff, delta_idx);

cl_assert_equal_oid(&direct_delta->old_file.id,
&reverse_delta->new_file.id);

cl_assert_equal_oid(&direct_delta->new_file.id,
&reverse_delta->old_file.id);
}
}

{
git_diff_perfdata perf = GIT_DIFF_PERFDATA_INIT;
cl_git_pass(git_diff_get_perfdata(&perf, diff));
Expand All @@ -68,6 +114,7 @@ void test_diff_workdir__to_index(void)
}

git_diff_free(diff);
git_diff_free(reverse_diff);
}

void test_diff_workdir__to_index_with_conflicts(void)
Expand Down Expand Up @@ -180,6 +227,7 @@ void test_diff_workdir__to_tree(void)
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_diff *diff2 = NULL;
git_diff *reverse_diff = NULL;
diff_expects exp;
int use_iterator;

Expand Down Expand Up @@ -302,21 +350,20 @@ void test_diff_workdir__to_tree(void)
cl_assert_equal_i(4, exp.line_dels);
}

git_diff_free(diff);

/* Let's try that once more with a reversed diff */
/* Free `diff` later. We want to compare it with the reverse diff first. */

opts.flags |= GIT_DIFF_REVERSE;

cl_git_pass(git_diff_tree_to_index(&diff, g_repo, b, NULL, &opts));
cl_git_pass(git_diff_tree_to_index(&reverse_diff, g_repo, b, NULL, &opts));
cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, NULL, &opts));
cl_git_pass(git_diff_merge(diff, diff2));
cl_git_pass(git_diff_merge(reverse_diff, diff2));
git_diff_free(diff2);

memset(&exp, 0, sizeof(exp));

cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
reverse_diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));

cl_assert_equal_i(16, exp.files);
cl_assert_equal_i(5, exp.file_status[GIT_DELTA_DELETED]);
Expand All @@ -332,7 +379,27 @@ void test_diff_workdir__to_tree(void)
cl_assert_equal_i(12, exp.line_dels);
cl_assert_equal_i(4, exp.line_adds);

/* Verify if the reverse diff has the same OIDs as the direct diff. */
{
size_t delta_idx;

cl_assert_equal_i(git_diff_num_deltas(diff),
git_diff_num_deltas(reverse_diff));

for (delta_idx = 0; delta_idx < git_diff_num_deltas(diff); delta_idx++) {
const git_diff_delta *direct_delta = git_diff_get_delta(diff, delta_idx);
const git_diff_delta *reverse_delta = git_diff_get_delta(reverse_diff, delta_idx);

cl_assert_equal_oid(&direct_delta->old_file.id,
&reverse_delta->new_file.id);

cl_assert_equal_oid(&direct_delta->new_file.id,
&reverse_delta->old_file.id);
}
}

git_diff_free(diff);
git_diff_free(reverse_diff);

/* all done now */

Expand Down