summaryrefslogtreecommitdiff
path: root/tests/libgit2
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2023-05-09 20:38:04 +0100
committerGitHub <noreply@github.com>2023-05-09 20:38:04 +0100
commit2bbcdee6b666f34e83d1cf9ca9badc66258202d6 (patch)
tree26a9d405c75564de8e1a3dc8ac4dc1c8ec7421f7 /tests/libgit2
parent251408cfd4ff8112efaa5c82ec81c9574c4bf022 (diff)
parent437c5f5a0b6ae6068168081ac6422dba44cff31d (diff)
downloadlibgit2-2bbcdee6b666f34e83d1cf9ca9badc66258202d6.tar.gz
Merge pull request #6557 from libgit2/ethomson/shallow
Shallow (#6396) with some fixes from review
Diffstat (limited to 'tests/libgit2')
-rw-r--r--tests/libgit2/core/oidarray.c98
-rw-r--r--tests/libgit2/grafts/basic.c121
-rw-r--r--tests/libgit2/grafts/parse.c149
-rw-r--r--tests/libgit2/grafts/shallow.c134
-rw-r--r--tests/libgit2/online/shallow.c166
-rw-r--r--tests/libgit2/transports/smart/packet.c4
6 files changed, 670 insertions, 2 deletions
diff --git a/tests/libgit2/core/oidarray.c b/tests/libgit2/core/oidarray.c
new file mode 100644
index 000000000..4a9e47c70
--- /dev/null
+++ b/tests/libgit2/core/oidarray.c
@@ -0,0 +1,98 @@
+#include "clar_libgit2.h"
+
+#include "git2/oid.h"
+#include "git2/transport.h"
+
+#include "common.h"
+#include "transports/smart.h"
+#include "oid.h"
+#include "oidarray.h"
+
+#include <assert.h>
+
+#define oid_0 "c070ad8c08840c8116da865b2d65593a6bb9cd2a"
+#define oid_1 "0966a434eb1a025db6b71485ab63a3bfbea520b6"
+#define oid_2 "83834a7afdaa1a1260568567f6ad90020389f664"
+#define oid_3 "746fb4c91a7b6190bc4761adf7410afc4b59812c"
+
+void test_core_oidarray__add_and_remove_oid_from_shallowarray(void)
+{
+ git_oid oid_0_obj, oid_1_obj, oid_2_obj, oid_3_obj;
+ git_array_oid_t array = GIT_ARRAY_INIT;
+
+ git_oid__fromstr(&oid_0_obj, oid_0, GIT_OID_SHA1);
+ git_oid__fromstr(&oid_1_obj, oid_1, GIT_OID_SHA1);
+ git_oid__fromstr(&oid_2_obj, oid_2, GIT_OID_SHA1);
+ git_oid__fromstr(&oid_3_obj, oid_3, GIT_OID_SHA1);
+
+ /* add some initial ids */
+ git_oidarray__add(&array, &oid_0_obj);
+ git_oidarray__add(&array, &oid_1_obj);
+ git_oidarray__add(&array, &oid_2_obj);
+
+ cl_assert_equal_i(3, array.size);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[1]));
+ cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&array.ptr[2]));
+
+ /* don't duplicate existing ids */
+ git_oidarray__add(&array, &oid_1_obj);
+
+ cl_assert_equal_i(3, array.size);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[1]));
+ cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&array.ptr[2]));
+
+ /* remove the last id */
+ cl_assert_equal_i(1, git_oidarray__remove(&array, &oid_2_obj));
+
+ cl_assert_equal_i(2, array.size);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[1]));
+
+ /* add another id */
+ git_oidarray__add(&array, &oid_3_obj);
+
+ cl_assert_equal_i(3, array.size);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[1]));
+ cl_assert_equal_s("746fb4c91a7b6190bc4761adf7410afc4b59812c", git_oid_tostr_s(&array.ptr[2]));
+
+ /* remove the first id */
+ cl_assert_equal_i(1, git_oidarray__remove(&array, &oid_0_obj));
+
+ cl_assert_equal_i(2, array.size);
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("746fb4c91a7b6190bc4761adf7410afc4b59812c", git_oid_tostr_s(&array.ptr[1]));
+
+ /* removing a nonexistent oid does nothing */
+ cl_assert_equal_i(0, git_oidarray__remove(&array, &oid_2_obj));
+
+ /* add another id */
+ git_oidarray__add(&array, &oid_0_obj);
+
+ cl_assert_equal_i(3, array.size);
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("746fb4c91a7b6190bc4761adf7410afc4b59812c", git_oid_tostr_s(&array.ptr[1]));
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[2]));
+
+ /* remove another id */
+ cl_assert_equal_i(1, git_oidarray__remove(&array, &oid_3_obj));
+
+ cl_assert_equal_i(2, array.size);
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&array.ptr[0]));
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[1]));
+
+ /* remove another id */
+ cl_assert_equal_i(1, git_oidarray__remove(&array, &oid_1_obj));
+
+ cl_assert_equal_i(1, array.size);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&array.ptr[0]));
+
+ /* remove the final id */
+ cl_assert_equal_i(1, git_oidarray__remove(&array, &oid_0_obj));
+
+ cl_assert_equal_i(0, array.size);
+
+ git_array_clear(array);
+}
diff --git a/tests/libgit2/grafts/basic.c b/tests/libgit2/grafts/basic.c
new file mode 100644
index 000000000..30c87f908
--- /dev/null
+++ b/tests/libgit2/grafts/basic.c
@@ -0,0 +1,121 @@
+#include "clar_libgit2.h"
+
+#include "futils.h"
+#include "grafts.h"
+
+static git_repository *g_repo;
+
+void test_grafts_basic__initialize(void)
+{
+ g_repo = cl_git_sandbox_init("grafted.git");
+}
+
+void test_grafts_basic__cleanup(void)
+{
+ 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, GIT_OID_SHA1));
+
+ 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..3b0618a1d
--- /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, GIT_OID_SHA1));
+}
+
+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..5911a26aa
--- /dev/null
+++ b/tests/libgit2/grafts/shallow.c
@@ -0,0 +1,134 @@
+#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__initialize(void)
+{
+ cl_git_pass(git_oid__fromstr(&g_shallow_oid, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1));
+}
+
+void test_grafts_shallow__cleanup(void)
+{
+ 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);
+}
diff --git a/tests/libgit2/online/shallow.c b/tests/libgit2/online/shallow.c
new file mode 100644
index 000000000..5c0e6565b
--- /dev/null
+++ b/tests/libgit2/online/shallow.c
@@ -0,0 +1,166 @@
+#include "clar_libgit2.h"
+#include "futils.h"
+#include "repository.h"
+
+static int remote_single_branch(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload)
+{
+ GIT_UNUSED(payload);
+
+ cl_git_pass(git_remote_create_with_fetchspec(out, repo, name, url, "+refs/heads/master:refs/remotes/origin/master"));
+
+ return 0;
+}
+
+void test_online_shallow__clone_depth_zero(void)
+{
+ git_str path = GIT_STR_INIT;
+ git_repository *repo;
+ git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
+ git_oid *roots;
+ size_t roots_len;
+
+ clone_opts.fetch_opts.depth = 0;
+ clone_opts.remote_cb = remote_single_branch;
+
+ git_str_joinpath(&path, clar_sandbox_path(), "shallowclone_0");
+
+ cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_str_cstr(&path), &clone_opts));
+
+ /* cloning with depth 0 results in a full clone. */
+ cl_assert_equal_b(false, git_repository_is_shallow(repo));
+
+ /* full clones do not have shallow roots. */
+ cl_git_pass(git_repository__shallow_roots(&roots, &roots_len, repo));
+ cl_assert_equal_i(0, roots_len);
+
+ git__free(roots);
+ git_str_dispose(&path);
+ git_repository_free(repo);
+}
+
+void test_online_shallow__clone_depth_one(void)
+{
+ git_str path = GIT_STR_INIT;
+ git_repository *repo;
+ git_revwalk *walk;
+ git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
+ git_oid oid;
+ git_oid *roots;
+ size_t roots_len;
+ size_t num_commits = 0;
+ int error = 0;
+
+ clone_opts.fetch_opts.depth = 1;
+ clone_opts.remote_cb = remote_single_branch;
+
+ git_str_joinpath(&path, clar_sandbox_path(), "shallowclone_1");
+
+ cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_str_cstr(&path), &clone_opts));
+
+ cl_assert_equal_b(true, git_repository_is_shallow(repo));
+
+ cl_git_pass(git_repository__shallow_roots(&roots, &roots_len, repo));
+ cl_assert_equal_i(1, roots_len);
+ cl_assert_equal_s("49322bb17d3acc9146f98c97d078513228bbf3c0", git_oid_tostr_s(&roots[0]));
+
+ git_revwalk_new(&walk, repo);
+
+ git_revwalk_push_head(walk);
+
+ while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) {
+ num_commits++;
+ }
+
+ cl_assert_equal_i(num_commits, 1);
+ cl_assert_equal_i(error, GIT_ITEROVER);
+
+ git__free(roots);
+ git_str_dispose(&path);
+ git_revwalk_free(walk);
+ git_repository_free(repo);
+}
+
+void test_online_shallow__clone_depth_five(void)
+{
+ git_str path = GIT_STR_INIT;
+ git_repository *repo;
+ git_revwalk *walk;
+ git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
+ git_oid oid;
+ git_oid *roots;
+ size_t roots_len;
+ size_t num_commits = 0;
+ int error = 0;
+
+ clone_opts.fetch_opts.depth = 5;
+ clone_opts.remote_cb = remote_single_branch;
+
+ git_str_joinpath(&path, clar_sandbox_path(), "shallowclone_5");
+
+ cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_str_cstr(&path), &clone_opts));
+
+ cl_assert_equal_b(true, git_repository_is_shallow(repo));
+
+ cl_git_pass(git_repository__shallow_roots(&roots, &roots_len, repo));
+ cl_assert_equal_i(3, roots_len);
+ cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&roots[0]));
+ cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&roots[1]));
+ cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots[2]));
+
+ git_revwalk_new(&walk, repo);
+
+ git_revwalk_push_head(walk);
+
+ while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) {
+ num_commits++;
+ }
+
+ cl_assert_equal_i(num_commits, 13);
+ cl_assert_equal_i(error, GIT_ITEROVER);
+
+ git__free(roots);
+ git_str_dispose(&path);
+ git_revwalk_free(walk);
+ git_repository_free(repo);
+}
+
+void test_online_shallow__unshallow(void)
+{
+ git_str path = GIT_STR_INIT;
+ git_repository *repo;
+ git_revwalk *walk;
+ git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
+ git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
+ git_remote *origin = NULL;
+ git_oid oid;
+ size_t num_commits = 0;
+ int error = 0;
+
+ clone_opts.fetch_opts.depth = 5;
+ clone_opts.remote_cb = remote_single_branch;
+
+ git_str_joinpath(&path, clar_sandbox_path(), "unshallow");
+ cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_str_cstr(&path), &clone_opts));
+ cl_assert_equal_b(true, git_repository_is_shallow(repo));
+
+ fetch_opts.depth = GIT_FETCH_DEPTH_UNSHALLOW;
+ cl_git_pass(git_remote_lookup(&origin, repo, "origin"));
+
+ cl_git_pass(git_remote_fetch(origin, NULL, &fetch_opts, NULL));
+ cl_assert_equal_b(false, git_repository_is_shallow(repo));
+
+ git_revwalk_new(&walk, repo);
+ git_revwalk_push_head(walk);
+
+ while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) {
+ num_commits++;
+ }
+
+ cl_assert_equal_i(num_commits, 21);
+ cl_assert_equal_i(error, GIT_ITEROVER);
+
+ git_remote_free(origin);
+ git_str_dispose(&path);
+ git_revwalk_free(walk);
+ git_repository_free(repo);
+}
diff --git a/tests/libgit2/transports/smart/packet.c b/tests/libgit2/transports/smart/packet.c
index 2035e3b65..a775a4cfa 100644
--- a/tests/libgit2/transports/smart/packet.c
+++ b/tests/libgit2/transports/smart/packet.c
@@ -25,7 +25,7 @@ static void assert_data_pkt_parses(const char *line, const char *expected_data,
size_t linelen = strlen(line) + 1;
const char *endptr;
git_pkt_data *pkt;
- git_pkt_parse_data pkt_parse_data = { 0 };
+ git_pkt_parse_data pkt_parse_data = { 1, GIT_OID_SHA1 };
cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen, &pkt_parse_data));
cl_assert_equal_i(pkt->type, GIT_PKT_DATA);
@@ -71,7 +71,7 @@ static void assert_ack_parses(const char *line, const char *expected_oid, enum g
const char *endptr;
git_pkt_ack *pkt;
git_oid oid;
- git_pkt_parse_data pkt_parse_data = { 0 };
+ git_pkt_parse_data pkt_parse_data = { 1, GIT_OID_SHA1 };
cl_git_pass(git_oid__fromstr(&oid, expected_oid, GIT_OID_SHA1));