1 #include "clar_libgit2.h"
4 #include <git2/sys/commit_graph.h>
6 #include "commit_graph.h"
9 void test_graph_commitgraph__parse(void)
12 struct git_commit_graph_file
*file
;
13 struct git_commit_graph_entry e
, parent
;
15 git_str commit_graph_path
= GIT_STR_INIT
;
17 cl_git_pass(git_repository_open(&repo
, cl_fixture("testrepo.git")));
18 cl_git_pass(git_str_joinpath(&commit_graph_path
, git_repository_path(repo
), "objects/info/commit-graph"));
19 cl_git_pass(git_commit_graph_file_open(&file
, git_str_cstr(&commit_graph_path
)));
20 cl_assert_equal_i(git_commit_graph_file_needs_refresh(file
, git_str_cstr(&commit_graph_path
)), 0);
22 cl_git_pass(git_oid_fromstr(&id
, "5001298e0c09ad9c34e4249bc5801c75e9754fa5"));
23 cl_git_pass(git_commit_graph_entry_find(&e
, file
, &id
, GIT_OID_HEXSZ
));
24 cl_assert_equal_oid(&e
.sha1
, &id
);
25 cl_git_pass(git_oid_fromstr(&id
, "418382dff1ffb8bdfba833f4d8bbcde58b1e7f47"));
26 cl_assert_equal_oid(&e
.tree_oid
, &id
);
27 cl_assert_equal_i(e
.generation
, 1);
28 cl_assert_equal_i(e
.commit_time
, UINT64_C(1273610423));
29 cl_assert_equal_i(e
.parent_count
, 0);
31 cl_git_pass(git_oid_fromstr(&id
, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
32 cl_git_pass(git_commit_graph_entry_find(&e
, file
, &id
, GIT_OID_HEXSZ
));
33 cl_assert_equal_oid(&e
.sha1
, &id
);
34 cl_assert_equal_i(e
.generation
, 5);
35 cl_assert_equal_i(e
.commit_time
, UINT64_C(1274813907));
36 cl_assert_equal_i(e
.parent_count
, 2);
38 cl_git_pass(git_oid_fromstr(&id
, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
39 cl_git_pass(git_commit_graph_entry_parent(&parent
, file
, &e
, 0));
40 cl_assert_equal_oid(&parent
.sha1
, &id
);
41 cl_assert_equal_i(parent
.generation
, 4);
43 cl_git_pass(git_oid_fromstr(&id
, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
44 cl_git_pass(git_commit_graph_entry_parent(&parent
, file
, &e
, 1));
45 cl_assert_equal_oid(&parent
.sha1
, &id
);
46 cl_assert_equal_i(parent
.generation
, 3);
48 git_commit_graph_file_free(file
);
49 git_repository_free(repo
);
50 git_str_dispose(&commit_graph_path
);
53 void test_graph_commitgraph__parse_octopus_merge(void)
56 struct git_commit_graph_file
*file
;
57 struct git_commit_graph_entry e
, parent
;
59 git_str commit_graph_path
= GIT_STR_INIT
;
61 cl_git_pass(git_repository_open(&repo
, cl_fixture("merge-recursive/.gitted")));
62 cl_git_pass(git_str_joinpath(&commit_graph_path
, git_repository_path(repo
), "objects/info/commit-graph"));
63 cl_git_pass(git_commit_graph_file_open(&file
, git_str_cstr(&commit_graph_path
)));
65 cl_git_pass(git_oid_fromstr(&id
, "d71c24b3b113fd1d1909998c5bfe33b86a65ee03"));
66 cl_git_pass(git_commit_graph_entry_find(&e
, file
, &id
, GIT_OID_HEXSZ
));
67 cl_assert_equal_oid(&e
.sha1
, &id
);
68 cl_git_pass(git_oid_fromstr(&id
, "348f16ffaeb73f319a75cec5b16a0a47d2d5e27c"));
69 cl_assert_equal_oid(&e
.tree_oid
, &id
);
70 cl_assert_equal_i(e
.generation
, 7);
71 cl_assert_equal_i(e
.commit_time
, UINT64_C(1447083009));
72 cl_assert_equal_i(e
.parent_count
, 3);
74 cl_git_pass(git_oid_fromstr(&id
, "ad2ace9e15f66b3d1138922e6ffdc3ea3f967fa6"));
75 cl_git_pass(git_commit_graph_entry_parent(&parent
, file
, &e
, 0));
76 cl_assert_equal_oid(&parent
.sha1
, &id
);
77 cl_assert_equal_i(parent
.generation
, 6);
79 cl_git_pass(git_oid_fromstr(&id
, "483065df53c0f4a02cdc6b2910b05d388fc17ffb"));
80 cl_git_pass(git_commit_graph_entry_parent(&parent
, file
, &e
, 1));
81 cl_assert_equal_oid(&parent
.sha1
, &id
);
82 cl_assert_equal_i(parent
.generation
, 2);
84 cl_git_pass(git_oid_fromstr(&id
, "815b5a1c80ca749d705c7aa0cb294a00cbedd340"));
85 cl_git_pass(git_commit_graph_entry_parent(&parent
, file
, &e
, 2));
86 cl_assert_equal_oid(&parent
.sha1
, &id
);
87 cl_assert_equal_i(parent
.generation
, 6);
89 git_commit_graph_file_free(file
);
90 git_repository_free(repo
);
91 git_str_dispose(&commit_graph_path
);
94 void test_graph_commitgraph__writer(void)
97 git_commit_graph_writer
*w
= NULL
;
99 git_commit_graph_writer_options opts
= GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT
;
100 git_buf cgraph
= GIT_BUF_INIT
;
101 git_str expected_cgraph
= GIT_STR_INIT
, path
= GIT_STR_INIT
;
103 cl_git_pass(git_repository_open(&repo
, cl_fixture("testrepo.git")));
105 cl_git_pass(git_str_joinpath(&path
, git_repository_path(repo
), "objects/info"));
106 cl_git_pass(git_commit_graph_writer_new(&w
, git_str_cstr(&path
)));
108 /* This is equivalent to `git commit-graph write --reachable`. */
109 cl_git_pass(git_revwalk_new(&walk
, repo
));
110 cl_git_pass(git_revwalk_push_glob(walk
, "refs/*"));
111 cl_git_pass(git_commit_graph_writer_add_revwalk(w
, walk
));
112 git_revwalk_free(walk
);
114 cl_git_pass(git_commit_graph_writer_dump(&cgraph
, w
, &opts
));
115 cl_git_pass(git_str_joinpath(&path
, git_repository_path(repo
), "objects/info/commit-graph"));
116 cl_git_pass(git_futils_readbuffer(&expected_cgraph
, git_str_cstr(&path
)));
118 cl_assert_equal_i(cgraph
.size
, git_str_len(&expected_cgraph
));
119 cl_assert_equal_i(memcmp(cgraph
.ptr
, git_str_cstr(&expected_cgraph
), cgraph
.size
), 0);
121 git_buf_dispose(&cgraph
);
122 git_str_dispose(&expected_cgraph
);
123 git_str_dispose(&path
);
124 git_commit_graph_writer_free(w
);
125 git_repository_free(repo
);