diff conflicts: add tests for tree to index
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
diff --git a/tests/diff/diff_helpers.c b/tests/diff/diff_helpers.c
index 47e06f0..03862d6 100644
--- a/tests/diff/diff_helpers.c
+++ b/tests/diff/diff_helpers.c
@@ -68,7 +68,7 @@ int diff_file_cb(
if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
e->files_binary++;
- cl_assert(delta->status <= GIT_DELTA_TYPECHANGE);
+ cl_assert(delta->status <= GIT_DELTA_CONFLICTED);
e->file_status[delta->status] += 1;
diff --git a/tests/diff/diff_helpers.h b/tests/diff/diff_helpers.h
index bf21f4b..6bcd1e6 100644
--- a/tests/diff/diff_helpers.h
+++ b/tests/diff/diff_helpers.h
@@ -8,7 +8,7 @@ typedef struct {
int files;
int files_binary;
- int file_status[10]; /* indexed by git_delta_t value */
+ int file_status[11]; /* indexed by git_delta_t value */
int hunks;
int hunk_new_lines;
diff --git a/tests/diff/index.c b/tests/diff/index.c
index 21afe8d..a544b83 100644
--- a/tests/diff/index.c
+++ b/tests/diff/index.c
@@ -163,3 +163,79 @@ void test_diff_index__checks_options_version(void)
git_tree_free(a);
}
+static void do_conflicted_diff(diff_expects *exp, unsigned long flags)
+{
+ const char *a_commit = "26a125ee1bf"; /* the current HEAD */
+ git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
+ git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+ git_index_entry ancestor = {0}, ours = {0}, theirs = {0};
+ git_diff *diff = NULL;
+ git_index *index;
+
+ cl_assert(a);
+
+ opts.context_lines = 1;
+ opts.interhunk_lines = 1;
+ opts.flags |= flags;
+
+ memset(exp, 0, sizeof(diff_expects));
+
+ cl_git_pass(git_repository_index(&index, g_repo));
+
+ ancestor.path = ours.path = theirs.path = "staged_changes";
+ ancestor.mode = ours.mode = theirs.mode = 0100644;
+
+ git_oid_fromstr(&ancestor.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
+ git_oid_fromstr(&ours.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
+ git_oid_fromstr(&theirs.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
+
+ cl_git_pass(git_index_conflict_add(index, &ancestor, &ours, &theirs));
+ cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, index, &opts));
+
+ cl_git_pass(git_diff_foreach(
+ diff, diff_file_cb, diff_hunk_cb, diff_line_cb, exp));
+
+ git_diff_free(diff);
+ git_tree_free(a);
+ git_index_free(index);
+}
+
+void test_diff_index__reports_conflicts(void)
+{
+ diff_expects exp;
+
+ do_conflicted_diff(&exp, 0);
+
+ cl_assert_equal_i(8, exp.files);
+ cl_assert_equal_i(3, exp.file_status[GIT_DELTA_ADDED]);
+ cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
+ cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
+ cl_assert_equal_i(1, exp.file_status[GIT_DELTA_CONFLICTED]);
+
+ cl_assert_equal_i(7, exp.hunks);
+
+ cl_assert_equal_i(9, exp.lines);
+ cl_assert_equal_i(2, exp.line_ctxt);
+ cl_assert_equal_i(5, exp.line_adds);
+ cl_assert_equal_i(2, exp.line_dels);
+}
+
+void test_diff_index__reports_conflicts_when_reversed(void)
+{
+ diff_expects exp;
+
+ do_conflicted_diff(&exp, GIT_DIFF_REVERSE);
+
+ cl_assert_equal_i(8, exp.files);
+ cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]);
+ cl_assert_equal_i(3, exp.file_status[GIT_DELTA_DELETED]);
+ cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
+ cl_assert_equal_i(1, exp.file_status[GIT_DELTA_CONFLICTED]);
+
+ cl_assert_equal_i(7, exp.hunks);
+
+ cl_assert_equal_i(9, exp.lines);
+ cl_assert_equal_i(2, exp.line_ctxt);
+ cl_assert_equal_i(2, exp.line_adds);
+ cl_assert_equal_i(5, exp.line_dels);
+}