summaryrefslogtreecommitdiff
path: root/tests-clar/diff
diff options
context:
space:
mode:
Diffstat (limited to 'tests-clar/diff')
-rw-r--r--tests-clar/diff/blob.c97
-rw-r--r--tests-clar/diff/diff_helpers.c62
-rw-r--r--tests-clar/diff/diff_helpers.h36
-rw-r--r--tests-clar/diff/tree.c105
4 files changed, 300 insertions, 0 deletions
diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c
new file mode 100644
index 000000000..048b05c79
--- /dev/null
+++ b/tests-clar/diff/blob.c
@@ -0,0 +1,97 @@
+#include "clar_libgit2.h"
+#include "diff_helpers.h"
+
+static git_repository *g_repo = NULL;
+
+void test_diff_blob__initialize(void)
+{
+ cl_fixture_sandbox("attr");
+ cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
+ cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes"));
+ cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
+}
+
+void test_diff_blob__cleanup(void)
+{
+ git_repository_free(g_repo);
+ g_repo = NULL;
+ cl_fixture_cleanup("attr");
+}
+
+void test_diff_blob__0(void)
+{
+ git_blob *a, *b, *c, *d;
+ git_oid a_oid, b_oid, c_oid, d_oid;
+ git_diff_options opts;
+ diff_expects exp;
+
+ /* 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));
+
+ /* 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, 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 */
+
+ opts.context_lines = 1;
+ opts.interhunk_lines = 0;
+ opts.ignore_whitespace = 0;
+
+ memset(&exp, 0, sizeof(exp));
+ cl_git_pass(git_diff_blobs(
+ g_repo, a, b, &opts, &exp, diff_hunk_fn, diff_line_fn));
+
+ cl_assert(exp.hunks == 1);
+ cl_assert(exp.lines == 6);
+ cl_assert(exp.line_ctxt == 1);
+ cl_assert(exp.line_adds == 5);
+ cl_assert(exp.line_dels == 0);
+
+ memset(&exp, 0, sizeof(exp));
+ cl_git_pass(git_diff_blobs(
+ g_repo, b, c, &opts, &exp, diff_hunk_fn, diff_line_fn));
+
+ cl_assert(exp.hunks == 1);
+ cl_assert(exp.lines == 15);
+ cl_assert(exp.line_ctxt == 3);
+ cl_assert(exp.line_adds == 9);
+ cl_assert(exp.line_dels == 3);
+
+ memset(&exp, 0, sizeof(exp));
+ cl_git_pass(git_diff_blobs(
+ g_repo, a, c, &opts, &exp, diff_hunk_fn, diff_line_fn));
+
+ cl_assert(exp.hunks == 1);
+ cl_assert(exp.lines == 13);
+ cl_assert(exp.line_ctxt == 0);
+ cl_assert(exp.line_adds == 12);
+ cl_assert(exp.line_dels == 1);
+
+ opts.context_lines = 1;
+
+ memset(&exp, 0, sizeof(exp));
+ cl_git_pass(git_diff_blobs(
+ g_repo, c, d, &opts, &exp, diff_hunk_fn, diff_line_fn));
+
+ cl_assert(exp.hunks == 2);
+ cl_assert(exp.lines == 14);
+ cl_assert(exp.line_ctxt == 4);
+ cl_assert(exp.line_adds == 6);
+ cl_assert(exp.line_dels == 4);
+
+ git_blob_free(a);
+ git_blob_free(b);
+ git_blob_free(c);
+ git_blob_free(d);
+}
+
diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c
index b2dbe9ee7..3fcf45c10 100644
--- a/tests-clar/diff/diff_helpers.c
+++ b/tests-clar/diff/diff_helpers.c
@@ -20,3 +20,65 @@ git_tree *resolve_commit_oid_to_tree(
git_object_free(obj);
return tree;
}
+
+int diff_file_fn(
+ void *cb_data,
+ git_diff_delta *delta,
+ float progress)
+{
+ diff_expects *e = cb_data;
+ (void)progress;
+ e->files++;
+ if (delta->old_attr == 0)
+ e->file_adds++;
+ else if (delta->new_attr == 0)
+ e->file_dels++;
+ else
+ e->file_mods++;
+ return 0;
+}
+
+int diff_hunk_fn(
+ void *cb_data,
+ git_diff_delta *delta,
+ git_diff_range *range,
+ const char *header,
+ size_t header_len)
+{
+ diff_expects *e = cb_data;
+ (void)delta;
+ (void)header;
+ (void)header_len;
+ e->hunks++;
+ e->hunk_old_lines += range->old_lines;
+ e->hunk_new_lines += range->new_lines;
+ return 0;
+}
+
+int diff_line_fn(
+ void *cb_data,
+ git_diff_delta *delta,
+ char line_origin,
+ const char *content,
+ size_t content_len)
+{
+ diff_expects *e = cb_data;
+ (void)delta;
+ (void)content;
+ (void)content_len;
+ e->lines++;
+ switch (line_origin) {
+ case GIT_DIFF_LINE_CONTEXT:
+ e->line_ctxt++;
+ break;
+ case GIT_DIFF_LINE_ADDITION:
+ e->line_adds++;
+ break;
+ case GIT_DIFF_LINE_DELETION:
+ e->line_dels++;
+ break;
+ default:
+ break;
+ }
+ return 0;
+}
diff --git a/tests-clar/diff/diff_helpers.h b/tests-clar/diff/diff_helpers.h
index a75dd912c..4c3e7580e 100644
--- a/tests-clar/diff/diff_helpers.h
+++ b/tests-clar/diff/diff_helpers.h
@@ -1,4 +1,40 @@
#include "fileops.h"
+#include "git2/diff.h"
extern git_tree *resolve_commit_oid_to_tree(
git_repository *repo, const char *partial_oid);
+
+typedef struct {
+ int files;
+ int file_adds;
+ int file_dels;
+ int file_mods;
+
+ int hunks;
+ int hunk_new_lines;
+ int hunk_old_lines;
+
+ int lines;
+ int line_ctxt;
+ int line_adds;
+ int line_dels;
+} diff_expects;
+
+extern int diff_file_fn(
+ void *cb_data,
+ git_diff_delta *delta,
+ float progress);
+
+extern int diff_hunk_fn(
+ void *cb_data,
+ git_diff_delta *delta,
+ git_diff_range *range,
+ const char *header,
+ size_t header_len);
+
+extern int diff_line_fn(
+ void *cb_data,
+ git_diff_delta *delta,
+ char line_origin,
+ const char *content,
+ size_t content_len);
diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c
new file mode 100644
index 000000000..3c5d2a9f2
--- /dev/null
+++ b/tests-clar/diff/tree.c
@@ -0,0 +1,105 @@
+#include "clar_libgit2.h"
+#include "diff_helpers.h"
+
+static git_repository *g_repo = NULL;
+
+void test_diff_tree__initialize(void)
+{
+ cl_fixture_sandbox("attr");
+ cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
+ cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes"));
+ cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
+}
+
+void test_diff_tree__cleanup(void)
+{
+ git_repository_free(g_repo);
+ g_repo = NULL;
+ cl_fixture_cleanup("attr");
+}
+
+static git_tree *resolve_commit_oid_to_tree(const char *partial_oid)
+{
+ size_t len = strlen(partial_oid);
+ git_oid oid;
+ git_object *obj;
+ git_tree *tree;
+
+ if (git_oid_fromstrn(&oid, partial_oid, len) == 0)
+ git_object_lookup_prefix(&obj, g_repo, &oid, len, GIT_OBJ_ANY);
+ cl_assert(obj);
+ if (git_object_type(obj) == GIT_OBJ_TREE)
+ return (git_tree *)obj;
+ cl_assert(git_object_type(obj) == GIT_OBJ_COMMIT);
+ cl_git_pass(git_commit_tree(&tree, (git_commit *)obj));
+ git_object_free(obj);
+ return tree;
+}
+
+void test_diff_tree__0(void)
+{
+ /* grabbed a couple of commit oids from the history of the attr repo */
+ const char *a_commit = "605812a";
+ const char *b_commit = "370fe9ec22";
+ const char *c_commit = "f5b0af1fb4f5c";
+ git_tree *a = resolve_commit_oid_to_tree(a_commit);
+ git_tree *b = resolve_commit_oid_to_tree(b_commit);
+ git_tree *c = resolve_commit_oid_to_tree(c_commit);
+ git_diff_options opts;
+ git_diff_list *diff = NULL;
+ diff_expects exp;
+
+ cl_assert(a);
+ cl_assert(b);
+
+ opts.context_lines = 1;
+ opts.interhunk_lines = 0;
+ opts.ignore_whitespace = 0;
+
+ memset(&exp, 0, sizeof(exp));
+
+ cl_git_pass(git_diff_tree_to_tree(g_repo, &opts, a, b, &diff));
+
+ cl_git_pass(git_diff_foreach(
+ diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn));
+
+ cl_assert(exp.files == 5);
+ cl_assert(exp.file_adds == 2);
+ cl_assert(exp.file_dels == 1);
+ cl_assert(exp.file_mods == 2);
+
+ cl_assert(exp.hunks == 5);
+
+ cl_assert(exp.lines == 7 + 24 + 1 + 6 + 6);
+ cl_assert(exp.line_ctxt == 1);
+ cl_assert(exp.line_adds == 24 + 1 + 5 + 5);
+ cl_assert(exp.line_dels == 7 + 1);
+
+ git_diff_list_free(diff);
+ diff = NULL;
+
+ memset(&exp, 0, sizeof(exp));
+
+ cl_git_pass(git_diff_tree_to_tree(g_repo, &opts, c, b, &diff));
+
+ cl_git_pass(git_diff_foreach(
+ diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn));
+
+ cl_assert(exp.files == 2);
+ cl_assert(exp.file_adds == 0);
+ cl_assert(exp.file_dels == 0);
+ cl_assert(exp.file_mods == 2);
+
+ cl_assert(exp.hunks == 2);
+
+ cl_assert(exp.lines == 8 + 15);
+ cl_assert(exp.line_ctxt == 1);
+ cl_assert(exp.line_adds == 1);
+ cl_assert(exp.line_dels == 7 + 14);
+
+ git_diff_list_free(diff);
+
+ git_tree_free(a);
+ git_tree_free(b);
+ git_tree_free(c);
+}