summaryrefslogtreecommitdiff
path: root/tests/libgit2/grafts
diff options
context:
space:
mode:
Diffstat (limited to 'tests/libgit2/grafts')
-rw-r--r--tests/libgit2/grafts/basic.c123
-rw-r--r--tests/libgit2/grafts/parse.c149
-rw-r--r--tests/libgit2/grafts/shallow.c146
3 files changed, 418 insertions, 0 deletions
diff --git a/tests/libgit2/grafts/basic.c b/tests/libgit2/grafts/basic.c
new file mode 100644
index 000000000..5ad437b19
--- /dev/null
+++ b/tests/libgit2/grafts/basic.c
@@ -0,0 +1,123 @@
+#include "clar_libgit2.h"
+
+#include "futils.h"
+#include "grafts.h"
+
+static git_repository *g_repo;
+
+void test_grafts_basic__initialize(void)
+{
+ git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 1);
+ g_repo = cl_git_sandbox_init("grafted.git");
+}
+
+void test_grafts_basic__cleanup(void)
+{
+ git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 0);
+ cl_git_sandbox_cleanup();
+}
+
+void test_grafts_basic__graft_add(void)
+{
+ git_array_oid_t parents = GIT_ARRAY_INIT;
+ git_oid oid_src, *oid1;
+ git_commit_graft *graft;
+ git_grafts *grafts;
+
+ cl_git_pass(git_grafts_new(&grafts));
+
+ cl_assert(oid1 = git_array_alloc(parents));
+ cl_git_pass(git_oid__fromstr(&oid_src, "2f3053cbff8a4ca2f0666de364ddb734a28a31a9", GIT_OID_SHA1));
+ git_oid_cpy(oid1, &oid_src);
+
+ git_oid__fromstr(&oid_src, "f503807ffa920e407a600cfaee96b7152259acc7", GIT_OID_SHA1);
+ cl_git_pass(git_grafts_add(grafts, &oid_src, parents));
+ git_array_clear(parents);
+
+ cl_assert_equal_i(1, git_grafts_size(grafts));
+ cl_git_pass(git_grafts_get(&graft, grafts, &oid_src));
+ cl_assert_equal_s("f503807ffa920e407a600cfaee96b7152259acc7", git_oid_tostr_s(&graft->oid));
+ cl_assert_equal_i(1, git_array_size(graft->parents));
+ cl_assert_equal_s("2f3053cbff8a4ca2f0666de364ddb734a28a31a9", git_oid_tostr_s(git_array_get(graft->parents, 0)));
+
+ git_grafts_free(grafts);
+}
+
+void test_grafts_basic__grafted_revwalk(void)
+{
+ git_revwalk *w;
+ git_oid oids[10];
+ size_t i = 0;
+ git_commit *commit;
+
+ cl_git_pass(git_revwalk_new(&w, g_repo));
+ cl_git_pass(git_revwalk_push_ref(w, "refs/heads/branch"));
+
+ cl_git_pass(git_revwalk_next(&oids[i++], w));
+ cl_assert_equal_s(git_oid_tostr_s(&oids[0]), "8a00e91619098618be97c0d2ceabb05a2c58edd9");
+ cl_git_pass(git_revwalk_next(&oids[i++], w));
+ cl_assert_equal_s(git_oid_tostr_s(&oids[1]), "f503807ffa920e407a600cfaee96b7152259acc7");
+ cl_git_pass(git_revwalk_next(&oids[i++], w));
+ cl_assert_equal_s(git_oid_tostr_s(&oids[2]), "2f3053cbff8a4ca2f0666de364ddb734a28a31a9");
+
+ cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oids[i++], w));
+
+ cl_git_pass(git_commit_lookup(&commit, g_repo, &oids[0]));
+
+ cl_assert_equal_i(1, git_commit_parentcount(commit));
+
+ git_commit_free(commit);
+ git_revwalk_free(w);
+}
+
+void test_grafts_basic__grafted_objects(void)
+{
+ git_oid oid;
+ git_commit *commit;
+
+ cl_git_pass(git_oid__fromstr(&oid, "f503807ffa920e407a600cfaee96b7152259acc7", GIT_OID_SHA1));
+ cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
+ cl_assert_equal_i(1, git_commit_parentcount(commit));
+ git_commit_free(commit);
+
+ cl_git_pass(git_oid__fromstr(&oid, "0512adebd3782157f0d5c9b22b043f87b4aaff9e", GIT_OID_SHA1));
+ cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
+ cl_assert_equal_i(1, git_commit_parentcount(commit));
+ git_commit_free(commit);
+
+ cl_git_pass(git_oid__fromstr(&oid, "66cc22a015f6ca75b34c82d28f78ba663876bade", GIT_OID_SHA1));
+ cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
+ cl_assert_equal_i(4, git_commit_parentcount(commit));
+ git_commit_free(commit);
+}
+
+void test_grafts_basic__grafted_merge_revwalk(void)
+{
+ git_revwalk *w;
+ git_oid oids[10];
+ size_t i = 0;
+
+ cl_git_pass(git_revwalk_new(&w, g_repo));
+ cl_git_pass(git_revwalk_push_ref(w, "refs/heads/bottom"));
+
+ cl_git_pass(git_revwalk_next(&oids[i++], w));
+ cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "66cc22a015f6ca75b34c82d28f78ba663876bade");
+ cl_git_pass(git_revwalk_next(&oids[i++], w));
+ cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "e414f42f4e6bc6934563a2349a8600f0ab68618e");
+ cl_git_pass(git_revwalk_next(&oids[i++], w));
+ cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "8a00e91619098618be97c0d2ceabb05a2c58edd9");
+ cl_git_pass(git_revwalk_next(&oids[i++], w));
+ cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "1c18e80a276611bb9b146590616bbc5aebdf2945");
+ cl_git_pass(git_revwalk_next(&oids[i++], w));
+ cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "d7224d49d6d5aff6ade596ed74f4bcd4f77b29e2");
+ cl_git_pass(git_revwalk_next(&oids[i++], w));
+ cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "0512adebd3782157f0d5c9b22b043f87b4aaff9e");
+ cl_git_pass(git_revwalk_next(&oids[i++], w));
+ cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "f503807ffa920e407a600cfaee96b7152259acc7");
+ cl_git_pass(git_revwalk_next(&oids[i++], w));
+ cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "2f3053cbff8a4ca2f0666de364ddb734a28a31a9");
+
+ cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oids[i++], w));
+
+ git_revwalk_free(w);
+}
diff --git a/tests/libgit2/grafts/parse.c b/tests/libgit2/grafts/parse.c
new file mode 100644
index 000000000..149b01c8c
--- /dev/null
+++ b/tests/libgit2/grafts/parse.c
@@ -0,0 +1,149 @@
+#include "clar_libgit2.h"
+
+#include "grafts.h"
+
+#define OID0 "c0368f9f9743e950e6cfe1f45a649f8a9dfcd97e"
+#define OID1 "cfc50a0db87ce908fb8a8c5b8f7b4ab96eee8643"
+#define OID2 "6914d97cd08b9edf5e855fca211c750fa82fd80a"
+#define OID3 "516521937d0e9ce9d0d836149a0702671f326b4a"
+#define OID4 "e2c29d67ef2f217650196f94c796f0532b8caad6"
+#define OID5 "79bcb936596cb50353fe7be28b7444e66e4a2842"
+#define OID6 "b9c54107d57c17dbcaf646c4d52f66eb9e69d23d"
+#define OID7 "9f8a746e9ad7b58cc840016bc3944d5ad262acb5"
+#define OID8 "392f4beef7d0d15b2bc5b1abe1a754eba0ec36da"
+
+#define OID_TRUNCATED "392f4beef7d0d15b2bc5b1abe1a754eba0ec36d"
+#define OID_NONHEX "9f8a746e9ax7b58cc840016bc3944d5ad262acb5"
+
+static git_grafts *grafts;
+
+void test_grafts_parse__initialize(void)
+{
+ cl_git_pass(git_grafts_new(&grafts));
+}
+
+void test_grafts_parse__cleanup(void)
+{
+ git_grafts_free(grafts);
+ grafts = NULL;
+}
+
+static void assert_parse_succeeds(git_grafts *grafts, const char *string, size_t n)
+{
+ cl_git_pass(git_grafts_parse(grafts, string, strlen(string)));
+ cl_assert_equal_i(git_grafts_size(grafts), n);
+}
+
+static void assert_parse_fails(git_grafts *grafts, const char *string)
+{
+ cl_git_fail(git_grafts_parse(grafts, string, strlen(string)));
+}
+
+static void assert_graft_contains(git_grafts *grafts, const char *graft, size_t n, ...)
+{
+ git_commit_graft *commit;
+ git_oid oid;
+ va_list ap;
+ size_t i = 0;
+
+ cl_git_pass(git_oid__fromstr(&oid, graft, GIT_OID_SHA1));
+ cl_git_pass(git_grafts_get(&commit, grafts, &oid));
+ cl_assert_equal_oid(&commit->oid, &oid);
+ cl_assert_equal_i(commit->parents.size, n);
+
+ va_start(ap, n);
+ while (i < n) {
+ cl_git_pass(git_oid__fromstr(&oid, va_arg(ap, const char *), GIT_OID_SHA1));
+ cl_assert_equal_oid(&commit->parents.ptr[i], &oid);
+ i++;
+ }
+ va_end(ap);
+}
+
+void test_grafts_parse__single_oid(void)
+{
+ assert_parse_succeeds(grafts, OID1, 1);
+ assert_graft_contains(grafts, OID1, 0);
+}
+
+void test_grafts_parse__single_oid_with_newline(void)
+{
+ assert_parse_succeeds(grafts, OID1 "\n", 1);
+ assert_graft_contains(grafts, OID1, 0);
+}
+
+void test_grafts_parse__multiple_oids(void)
+{
+ assert_parse_succeeds(grafts, OID1 "\n" OID2 "\n" OID3, 3);
+ assert_graft_contains(grafts, OID1, 0);
+ assert_graft_contains(grafts, OID2, 0);
+ assert_graft_contains(grafts, OID3, 0);
+}
+
+void test_grafts_parse__same_oid(void)
+{
+ assert_parse_succeeds(grafts, OID1 "\n" OID1, 1);
+ assert_graft_contains(grafts, OID1, 0);
+}
+
+void test_grafts_parse__oid_with_parent(void)
+{
+ assert_parse_succeeds(grafts, OID1 " " OID2, 1);
+ assert_graft_contains(grafts, OID1, 1, OID2);
+}
+
+void test_grafts_parse__oid_with_parent_and_newline(void)
+{
+ assert_parse_succeeds(grafts, OID1 " " OID2 "\n", 1);
+ assert_graft_contains(grafts, OID1, 1, OID2);
+}
+
+void test_grafts_parse__oid_with_multiple_parents(void)
+{
+ assert_parse_succeeds(grafts, OID1 " " OID2 " " OID3 " " OID4 " " OID5, 1);
+ assert_graft_contains(grafts, OID1, 4, OID2, OID3, OID4, OID5);
+}
+
+void test_grafts_parse__multiple_oids_with_multiple_parents(void)
+{
+ assert_parse_succeeds(grafts,
+ OID1 " " OID2 " " OID3 " " OID4 " " OID5 "\n"
+ OID6 " " OID7 " " OID8 "\n" , 2);
+ assert_graft_contains(grafts, OID1, 4, OID2, OID3, OID4, OID5);
+ assert_graft_contains(grafts, OID6, 2, OID7, OID8);
+}
+
+void test_grafts_parse__multiple_spaces_fails(void)
+{
+ assert_parse_fails(grafts, OID1 " " OID2);
+}
+
+void test_grafts_parse__trailing_space_fails(void)
+{
+ assert_parse_fails(grafts, OID1 " " OID2 " ");
+}
+
+void test_grafts_parse__invalid_character_inbetween_fails(void)
+{
+ assert_parse_fails(grafts, OID1 " x " OID2);
+}
+
+void test_grafts_parse__truncated_oid_fails(void)
+{
+ assert_parse_fails(grafts, OID_TRUNCATED);
+}
+
+void test_grafts_parse__truncated_parent_fails(void)
+{
+ assert_parse_fails(grafts, OID1 " " OID_TRUNCATED);
+}
+
+void test_grafts_parse__invalid_oid_fails(void)
+{
+ assert_parse_fails(grafts, OID_NONHEX);
+}
+
+void test_grafts_parse__invalid_parent_fails(void)
+{
+ assert_parse_fails(grafts, OID1 " " OID_NONHEX);
+}
diff --git a/tests/libgit2/grafts/shallow.c b/tests/libgit2/grafts/shallow.c
new file mode 100644
index 000000000..8c2723922
--- /dev/null
+++ b/tests/libgit2/grafts/shallow.c
@@ -0,0 +1,146 @@
+#include "clar_libgit2.h"
+
+#include "futils.h"
+#include "grafts.h"
+#include "repository.h"
+
+static git_repository *g_repo;
+static git_oid g_shallow_oid;
+
+void test_grafts_shallow__set_feature_flag(void)
+{
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 1));
+}
+
+void test_grafts_shallow__unset_feature_flag(void)
+{
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 0));
+}
+
+void test_grafts_shallow__initialize(void)
+{
+ git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 1);
+ cl_git_pass(git_oid__fromstr(&g_shallow_oid, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1));
+}
+
+void test_grafts_shallow__cleanup(void)
+{
+ git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 0);
+ cl_git_sandbox_cleanup();
+}
+
+void test_grafts_shallow__no_shallow_file(void)
+{
+ g_repo = cl_git_sandbox_init("testrepo.git");
+ cl_assert_equal_i(0, git_repository_is_shallow(g_repo));
+}
+
+void test_grafts_shallow__empty_shallow_file(void)
+{
+ g_repo = cl_git_sandbox_init("testrepo.git");
+ cl_git_mkfile("testrepo.git/shallow", "");
+ cl_assert_equal_i(0, git_repository_is_shallow(g_repo));
+}
+
+void test_grafts_shallow__shallow_repo(void)
+{
+ g_repo = cl_git_sandbox_init("shallow.git");
+ cl_assert_equal_i(1, git_repository_is_shallow(g_repo));
+}
+
+void test_grafts_shallow__clears_errors(void)
+{
+ g_repo = cl_git_sandbox_init("testrepo.git");
+ cl_assert_equal_i(0, git_repository_is_shallow(g_repo));
+ cl_assert_equal_p(NULL, git_error_last());
+}
+
+void test_grafts_shallow__shallow_oids(void)
+{
+ git_commit_graft *graft;
+ git_grafts *grafts;
+
+ g_repo = cl_git_sandbox_init("shallow.git");
+
+ cl_git_pass(git_repository_shallow_grafts__weakptr(&grafts, g_repo));
+ cl_assert_equal_i(1, git_grafts_size(grafts));
+ cl_git_pass(git_grafts_get(&graft, grafts, &g_shallow_oid));
+}
+
+void test_grafts_shallow__cache_clearing(void)
+{
+ git_commit_graft *graft;
+ git_grafts *grafts;
+ git_oid tmp_oid;
+
+ cl_git_pass(git_oid__fromstr(&tmp_oid, "0000000000000000000000000000000000000000", GIT_OID_SHA1));
+ g_repo = cl_git_sandbox_init("shallow.git");
+ cl_git_pass(git_repository_shallow_grafts__weakptr(&grafts, g_repo));
+
+ cl_assert_equal_i(1, git_grafts_size(grafts));
+ cl_git_pass(git_grafts_get(&graft, grafts, &g_shallow_oid));
+
+ cl_git_mkfile("shallow.git/shallow",
+ "be3563ae3f795b2b4353bcce3a527ad0a4f7f644\n"
+ "0000000000000000000000000000000000000000\n"
+ );
+
+ cl_git_pass(git_grafts_refresh(grafts));
+ cl_assert_equal_i(2, git_grafts_size(grafts));
+ cl_git_pass(git_grafts_get(&graft, grafts, &g_shallow_oid));
+ cl_git_pass(git_grafts_get(&graft, grafts, &tmp_oid));
+
+ cl_git_pass(p_unlink("shallow.git/shallow"));
+ cl_git_pass(git_grafts_refresh(grafts));
+ cl_assert_equal_i(0, git_grafts_size(grafts));
+}
+
+void test_grafts_shallow__errors_on_borked(void)
+{
+ git_grafts *grafts;
+
+ g_repo = cl_git_sandbox_init("shallow.git");
+
+ cl_git_mkfile("shallow.git/shallow", "lolno");
+ cl_git_pass(git_repository_shallow_grafts__weakptr(&grafts, g_repo));
+ cl_git_fail(git_grafts_refresh(grafts));
+ cl_assert_equal_i(0, git_grafts_size(grafts));
+
+ cl_git_mkfile("shallow.git/shallow", "lolno\n");
+ cl_git_pass(git_repository_shallow_grafts__weakptr(&grafts, g_repo));
+ cl_git_fail(git_grafts_refresh(grafts));
+ cl_assert_equal_i(0, git_grafts_size(grafts));
+}
+
+void test_grafts_shallow__revwalk_behavior(void)
+{
+ git_revwalk *w;
+ git_oid oid_1, oid_2, oid_3;
+
+ g_repo = cl_git_sandbox_init("shallow.git");
+
+ cl_git_pass(git_revwalk_new(&w, g_repo));
+ cl_git_pass(git_revwalk_push_head(w));
+
+ cl_git_pass(git_revwalk_next(&oid_1, w)); // a65fedf39aefe402d3bb6e24df4d4f5fe4547750
+ cl_git_pass(git_revwalk_next(&oid_2, w)); // be3563ae3f795b2b4353bcce3a527ad0a4f7f644
+ cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oid_3, w));
+
+ cl_assert_equal_s(git_oid_tostr_s(&oid_1), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
+ cl_assert_equal_s(git_oid_tostr_s(&oid_2), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
+
+ git_revwalk_free(w);
+}
+
+void test_grafts_shallow__grafted_object(void)
+{
+ git_commit *commit;
+
+ g_repo = cl_git_sandbox_init("shallow.git");
+
+ cl_git_pass(git_commit_lookup(&commit, g_repo, &g_shallow_oid));
+
+ cl_assert_equal_i(0, git_commit_parentcount(commit));
+
+ git_commit_free(commit);
+}