Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c34fa5

Browse files
committed
Merge pull request libgit2#3228 from git-up/diff_merge
Explicitly handle GIT_DELTA_CONFLICTED in git_diff_merge()
2 parents 91c1833 + cb63e7e commit 0c34fa5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/diff_tform.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ static git_diff_delta *diff_delta__merge_like_cgit(
6565
* f3 = b->new_file
6666
*/
6767

68+
/* If one of the diffs is a conflict, just dup it */
69+
if (b->status == GIT_DELTA_CONFLICTED)
70+
return diff_delta__dup(b, pool);
71+
if (a->status == GIT_DELTA_CONFLICTED)
72+
return diff_delta__dup(a, pool);
73+
6874
/* if f2 == f3 or f2 is deleted, then just dup the 'a' diff */
6975
if (b->status == GIT_DELTA_UNMODIFIED || a->status == GIT_DELTA_DELETED)
7076
return diff_delta__dup(a, pool);
@@ -111,6 +117,11 @@ static git_diff_delta *diff_delta__merge_like_cgit_reversed(
111117

112118
/* reversed version of above logic */
113119

120+
if (a->status == GIT_DELTA_CONFLICTED)
121+
return diff_delta__dup(a, pool);
122+
if (b->status == GIT_DELTA_CONFLICTED)
123+
return diff_delta__dup(b, pool);
124+
114125
if (a->status == GIT_DELTA_UNMODIFIED)
115126
return diff_delta__dup(b, pool);
116127

tests/diff/workdir.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,3 +1745,39 @@ void test_diff_workdir__binary_detection(void)
17451745
git_index_free(idx);
17461746
git_buf_free(&b);
17471747
}
1748+
1749+
void test_diff_workdir__to_index_conflicted(void) {
1750+
const char *a_commit = "26a125ee1bf"; /* the current HEAD */
1751+
git_index_entry ancestor = {{0}}, ours = {{0}}, theirs = {{0}};
1752+
git_tree *a;
1753+
git_index *index;
1754+
git_diff *diff1, *diff2;
1755+
const git_diff_delta *delta;
1756+
1757+
g_repo = cl_git_sandbox_init("status");
1758+
a = resolve_commit_oid_to_tree(g_repo, a_commit);
1759+
1760+
cl_git_pass(git_repository_index(&index, g_repo));
1761+
1762+
ancestor.path = ours.path = theirs.path = "_file";
1763+
ancestor.mode = ours.mode = theirs.mode = 0100644;
1764+
git_oid_fromstr(&ancestor.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
1765+
git_oid_fromstr(&ours.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
1766+
git_oid_fromstr(&theirs.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
1767+
cl_git_pass(git_index_conflict_add(index, &ancestor, &ours, &theirs));
1768+
1769+
cl_git_pass(git_diff_tree_to_index(&diff1, g_repo, a, index, NULL));
1770+
cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, index, NULL));
1771+
cl_git_pass(git_diff_merge(diff1, diff2));
1772+
1773+
cl_assert_equal_i(git_diff_num_deltas(diff1), 12);
1774+
delta = git_diff_get_delta(diff1, 0);
1775+
cl_assert_equal_s(delta->old_file.path, "_file");
1776+
cl_assert_equal_i(delta->nfiles, 1);
1777+
cl_assert_equal_i(delta->status, GIT_DELTA_CONFLICTED);
1778+
1779+
git_diff_free(diff2);
1780+
git_diff_free(diff1);
1781+
git_index_free(index);
1782+
git_tree_free(a);
1783+
}

0 commit comments

Comments
 (0)