2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_reference.h"
32 #include "got_repository.h"
34 #include "got_opentemp.h"
36 #include "got_lib_path.h"
39 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 #define GOT_REPO_PATH "../../../"
47 test_printf(char *fmt, ...)
59 static const struct got_error *
60 print_commit_object(struct got_object_id *, struct got_repository *);
62 static const struct got_error *
63 print_parent_commits(struct got_commit_object *commit,
64 struct got_repository *repo)
66 const struct got_object_id_queue *parent_ids;
67 struct got_object_qid *qid;
68 const struct got_error *err = NULL;
70 parent_ids = got_object_commit_get_parent_ids(commit);
71 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
72 err = print_commit_object(qid->id, repo);
80 static const struct got_error *
81 print_tree_object(struct got_object_id *id, char *parent,
82 struct got_repository *repo)
84 struct got_tree_object *tree;
85 const struct got_tree_entries *entries;
86 struct got_tree_entry *te;
87 const struct got_error *err;
89 err = got_object_open_as_tree(&tree, repo, id);
93 entries = got_object_tree_get_entries(tree);
94 SIMPLEQ_FOREACH(te, &entries->head, entry) {
98 err = got_object_id_str(&hex, te->id);
102 if (!S_ISDIR(te->mode)) {
103 test_printf("%s %s/%s\n", hex, parent, te->name);
107 test_printf("%s %s/%s\n", hex, parent, te->name);
110 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
111 err = got_error_from_errno();
115 err = print_tree_object(te->id, next_parent, repo);
121 got_object_tree_close(tree);
125 static const struct got_error *
126 print_commit_object(struct got_object_id *id, struct got_repository *repo)
128 struct got_commit_object *commit;
129 const struct got_object_id_queue *parent_ids;
130 struct got_object_qid *qid;
132 const struct got_error *err;
135 err = got_object_open_as_commit(&commit, repo, id);
139 err = got_object_id_str(&buf, id);
141 got_object_commit_close(commit);
144 test_printf("tree: %s\n", buf);
146 test_printf("parent%s: ",
147 (got_object_commit_get_nparents(commit) == 1) ? "" : "s");
148 parent_ids = got_object_commit_get_parent_ids(commit);
149 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
150 err = got_object_id_str(&buf, qid->id);
152 got_object_commit_close(commit);
155 test_printf("%s\n", buf);
158 test_printf("author: %s\n", got_object_commit_get_author(commit));
159 test_printf("committer: %s\n", got_object_commit_get_committer(commit));
160 test_printf("log: %s\n", got_object_commit_get_logmsg(commit));
162 err = got_object_get_type(&obj_type, repo,
163 got_object_commit_get_tree_id(commit));
165 got_object_commit_close(commit);
168 if (obj_type == GOT_OBJ_TYPE_TREE)
171 err = print_parent_commits(commit, repo);
172 got_object_commit_close(commit);
178 repo_read_log(const char *repo_path)
180 const struct got_error *err;
181 struct got_repository *repo;
182 struct got_reference *head_ref;
183 struct got_object_id *id;
186 err = got_repo_open(&repo, repo_path);
187 if (err != NULL || repo == NULL)
189 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
190 if (err != NULL || head_ref == NULL)
192 err = got_ref_resolve(&id, repo, head_ref);
193 if (err != NULL || head_ref == NULL)
195 err = got_object_id_str(&buf, id);
198 test_printf("HEAD is at %s\n", buf);
200 err = print_commit_object(id, repo);
204 got_ref_close(head_ref);
205 got_repo_close(repo);
210 repo_read_tree(const char *repo_path)
212 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
213 const struct got_error *err;
214 struct got_repository *repo;
215 struct got_object_id *id;
217 err = got_repo_open(&repo, repo_path);
218 if (err != NULL || repo == NULL)
220 err = got_object_resolve_id_str(&id, repo, tree_sha1);
224 print_tree_object(id, "", repo);
227 got_repo_close(repo);
228 return (err == NULL);
232 repo_read_blob(const char *repo_path)
234 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
235 const struct got_error *err;
236 struct got_repository *repo;
237 struct got_object_id *id;
238 struct got_blob_object *blob;
242 err = got_repo_open(&repo, repo_path);
243 if (err != NULL || repo == NULL)
245 err = got_object_resolve_id_str(&id, repo, blob_sha1);
248 err = got_object_open_as_blob(&blob, repo, id, 64);
254 const uint8_t *buf = got_object_blob_get_read_buf(blob);
255 err = got_object_blob_read_block(&len, blob);
258 for (i = 0; i < len; i++)
259 test_printf("%c", buf[i]);
263 got_object_blob_close(blob);
264 got_repo_close(repo);
265 return (err == NULL);
269 repo_diff_blob(const char *repo_path)
271 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
272 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
273 const struct got_error *err;
274 struct got_repository *repo;
275 struct got_object_id *id1, *id2;
276 struct got_blob_object *blob1;
277 struct got_blob_object *blob2;
282 const char delim[3] = {'\0', '\0', '\0'};
283 const char *expected_output[] = {
284 "blob - 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
285 "blob + de7eb21b21c7823a753261aadf7cba35c9580fbf",
286 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
287 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
289 " .PATH:${.CURDIR}/../../lib",
291 " PROG = repository_test",
292 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
293 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
295 " CPPFLAGS = -I${.CURDIR}/../../include",
297 "+LDADD = -lutil -lz",
302 err = got_repo_open(&repo, repo_path);
303 if (err != NULL || repo == NULL)
306 err = got_object_resolve_id_str(&id1, repo, blob1_sha1);
310 err = got_object_resolve_id_str(&id2, repo, blob2_sha1);
314 err = got_object_open_as_blob(&blob1, repo, id1, 512);
318 err = got_object_open_as_blob(&blob2, repo, id2, 512);
323 outfile = got_opentemp();
326 got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
329 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
332 if (i < nitems(expected_output) &&
333 strcmp(line, expected_output[i]) != 0) {
334 test_printf("diff output mismatch; expected: '%s'\n",
342 if (i != nitems(expected_output) + 1) {
343 test_printf("number of lines expected: %d; actual: %d\n",
344 nitems(expected_output), i - 1);
348 got_object_blob_close(blob1);
349 got_object_blob_close(blob2);
350 got_repo_close(repo);
351 return (err == NULL);
355 repo_diff_tree(const char *repo_path)
357 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
358 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
359 const struct got_error *err;
360 struct got_repository *repo;
361 struct got_object_id *id1;
362 struct got_object_id *id2;
363 struct got_tree_object *tree1;
364 struct got_tree_object *tree2;
367 err = got_repo_open(&repo, repo_path);
368 if (err != NULL || repo == NULL)
371 err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
374 err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
378 err = got_object_open_as_tree(&tree1, repo, id1);
382 err = got_object_open_as_tree(&tree2, repo, id2);
387 outfile = fopen("/dev/null", "w+");
393 got_diff_tree(tree1, tree2, "", "", 3, repo, outfile);
396 got_object_tree_close(tree1);
397 got_object_tree_close(tree2);
398 got_repo_close(repo);
399 return (err == NULL);
402 #define RUN_TEST(expr, name) \
403 { test_ok = (expr); \
404 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
405 failure = (failure || !test_ok); }
411 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
415 main(int argc, char *argv[])
417 int test_ok = 0, failure = 0;
418 const char *repo_path;
422 if (pledge("stdio rpath wpath cpath proc exec sendfd", NULL) == -1)
426 while ((ch = getopt(argc, argv, "v")) != -1) {
440 repo_path = GOT_REPO_PATH;
448 RUN_TEST(repo_read_tree(repo_path), "read_tree");
449 RUN_TEST(repo_read_log(repo_path), "read_log");
450 RUN_TEST(repo_read_blob(repo_path), "read_blob");
451 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
452 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
454 return failure ? 1 : 0;