summaryrefslogtreecommitdiff
path: root/tests-clar/diff
diff options
context:
space:
mode:
authorRussell Belfer <arrbee@arrbee.com>2012-02-07 12:14:28 -0800
committerRussell Belfer <arrbee@arrbee.com>2012-03-02 15:49:29 -0800
commita2e895be820a2fd77285ef4576afe53f68c96ca2 (patch)
treea086aaaad07d11d17bec91f3660b22a96250df65 /tests-clar/diff
parent5a2f097fdc1408500cff9addf378f86046363665 (diff)
downloadlibgit2-a2e895be820a2fd77285ef4576afe53f68c96ca2.tar.gz
Continue implementation of git-diff
* Implemented git_diff_index_to_tree * Reworked git_diff_options structure to handle more options * Made most of the options in git_diff_options actually work * Reorganized code a bit to remove some redundancy * Added option parsing to examples/diff.c to test most options
Diffstat (limited to 'tests-clar/diff')
-rw-r--r--tests-clar/diff/blob.c5
-rw-r--r--tests-clar/diff/diff_helpers.c20
-rw-r--r--tests-clar/diff/diff_helpers.h1
-rw-r--r--tests-clar/diff/index.c96
-rw-r--r--tests-clar/diff/tree.c29
5 files changed, 124 insertions, 27 deletions
diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c
index 048b05c79..7aa8ceb22 100644
--- a/tests-clar/diff/blob.c
+++ b/tests-clar/diff/blob.c
@@ -22,7 +22,7 @@ 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;
+ git_diff_options opts = {0};
diff_expects exp;
/* tests/resources/attr/root_test1 */
@@ -44,8 +44,7 @@ void test_diff_blob__0(void)
/* Doing the equivalent of a `git diff -U1` on these files */
opts.context_lines = 1;
- opts.interhunk_lines = 0;
- opts.ignore_whitespace = 0;
+ opts.interhunk_lines = 1;
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_blobs(
diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c
index 3fcf45c10..b32c4bc2d 100644
--- a/tests-clar/diff/diff_helpers.c
+++ b/tests-clar/diff/diff_helpers.c
@@ -82,3 +82,23 @@ int diff_line_fn(
}
return 0;
}
+
+git_tree *resolve_commit_oid_to_tree(
+ git_repository *repo,
+ 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, 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;
+}
diff --git a/tests-clar/diff/diff_helpers.h b/tests-clar/diff/diff_helpers.h
index 4c3e7580e..035c000f5 100644
--- a/tests-clar/diff/diff_helpers.h
+++ b/tests-clar/diff/diff_helpers.h
@@ -38,3 +38,4 @@ extern int diff_line_fn(
char line_origin,
const char *content,
size_t content_len);
+
diff --git a/tests-clar/diff/index.c b/tests-clar/diff/index.c
new file mode 100644
index 000000000..0941c7c21
--- /dev/null
+++ b/tests-clar/diff/index.c
@@ -0,0 +1,96 @@
+#include "clar_libgit2.h"
+#include "diff_helpers.h"
+
+static git_repository *g_repo = NULL;
+
+void test_diff_index__initialize(void)
+{
+ cl_fixture_sandbox("status");
+ cl_git_pass(p_rename("status/.gitted", "status/.git"));
+ cl_git_pass(git_repository_open(&g_repo, "status/.git"));
+}
+
+void test_diff_index__cleanup(void)
+{
+ git_repository_free(g_repo);
+ g_repo = NULL;
+ cl_fixture_cleanup("status");
+}
+
+void test_diff_index__0(void)
+{
+ /* grabbed a couple of commit oids from the history of the attr repo */
+ const char *a_commit = "26a125ee1bf"; /* the current HEAD */
+ const char *b_commit = "0017bd4ab1ec3"; /* the start */
+ git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
+ git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit);
+ git_diff_options opts = {0};
+ git_diff_list *diff = NULL;
+ diff_expects exp;
+
+ cl_assert(a);
+ cl_assert(b);
+
+ opts.context_lines = 1;
+ opts.interhunk_lines = 1;
+
+ memset(&exp, 0, sizeof(exp));
+
+ cl_git_pass(git_diff_index_to_tree(g_repo, &opts, a, &diff));
+
+ cl_git_pass(git_diff_foreach(
+ diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn));
+
+ /* to generate these values:
+ * - cd to tests/resources/status,
+ * - mv .gitted .git
+ * - git diff --name-status --cached 26a125ee1bf
+ * - git diff -U1 --cached 26a125ee1bf
+ * - mv .git .gitted
+ */
+ cl_assert(exp.files == 8);
+ cl_assert(exp.file_adds == 3);
+ cl_assert(exp.file_dels == 2);
+ cl_assert(exp.file_mods == 3);
+
+ cl_assert(exp.hunks == 8);
+
+ cl_assert(exp.lines == 11);
+ cl_assert(exp.line_ctxt == 3);
+ cl_assert(exp.line_adds == 6);
+ cl_assert(exp.line_dels == 2);
+
+ git_diff_list_free(diff);
+ diff = NULL;
+ memset(&exp, 0, sizeof(exp));
+
+ cl_git_pass(git_diff_index_to_tree(g_repo, &opts, b, &diff));
+
+ cl_git_pass(git_diff_foreach(
+ diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn));
+
+ /* to generate these values:
+ * - cd to tests/resources/status,
+ * - mv .gitted .git
+ * - git diff --name-status --cached 0017bd4ab1ec3
+ * - git diff -U1 --cached 0017bd4ab1ec3
+ * - mv .git .gitted
+ */
+ cl_assert(exp.files == 12);
+ cl_assert(exp.file_adds == 7);
+ cl_assert(exp.file_dels == 2);
+ cl_assert(exp.file_mods == 3);
+
+ cl_assert(exp.hunks == 12);
+
+ cl_assert(exp.lines == 16);
+ cl_assert(exp.line_ctxt == 3);
+ cl_assert(exp.line_adds == 11);
+ cl_assert(exp.line_dels == 2);
+
+ git_diff_list_free(diff);
+ diff = NULL;
+
+ git_tree_free(a);
+ git_tree_free(b);
+}
diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c
index 3c5d2a9f2..836db5765 100644
--- a/tests-clar/diff/tree.c
+++ b/tests-clar/diff/tree.c
@@ -18,34 +18,16 @@ void test_diff_tree__cleanup(void)
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_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
+ git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit);
+ git_tree *c = resolve_commit_oid_to_tree(g_repo, c_commit);
+ git_diff_options opts = {0};
git_diff_list *diff = NULL;
diff_expects exp;
@@ -53,8 +35,7 @@ void test_diff_tree__0(void)
cl_assert(b);
opts.context_lines = 1;
- opts.interhunk_lines = 0;
- opts.ignore_whitespace = 0;
+ opts.interhunk_lines = 1;
memset(&exp, 0, sizeof(exp));