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"
32 #include "got_repository.h"
35 #include "got_lib_path.h"
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #define GOT_REPO_PATH "../../../"
46 test_printf(char *fmt, ...)
58 static const struct got_error *
59 print_commit_object(struct got_object *, struct got_repository *);
61 static const struct got_error *
62 print_parent_commits(struct got_commit_object *commit,
63 struct got_repository *repo)
65 struct got_parent_id *pid;
66 const struct got_error *err = NULL;
67 struct got_object *obj;
69 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
70 err = got_object_open(&obj, repo, pid->id);
73 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
74 return got_error(GOT_ERR_OBJ_TYPE);
75 err = print_commit_object(obj, repo);
76 got_object_close(obj);
82 static const struct got_error *
83 print_tree_object(struct got_object *obj, char *parent,
84 struct got_repository *repo)
86 struct got_tree_object *tree;
87 struct got_tree_entry *te;
88 const struct got_error *err;
90 err = got_object_tree_open(&tree, repo, obj);
94 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
95 struct got_object *treeobj;
99 err = got_object_id_str(&hex, te->id);
103 if (!S_ISDIR(te->mode)) {
104 test_printf("%s %s/%s\n", hex, parent, te->name);
108 test_printf("%s %s/%s\n", hex, parent, te->name);
111 err = got_object_open(&treeobj, repo, te->id);
115 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
116 err = got_error(GOT_ERR_OBJ_TYPE);
117 got_object_close(treeobj);
121 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
122 err = got_error_from_errno();
123 got_object_close(treeobj);
127 err = print_tree_object(treeobj, next_parent, repo);
130 got_object_close(treeobj);
134 got_object_close(treeobj);
137 got_object_tree_close(tree);
141 static const struct got_error *
142 print_commit_object(struct got_object *obj, struct got_repository *repo)
144 struct got_commit_object *commit;
145 struct got_parent_id *pid;
147 const struct got_error *err;
148 struct got_object* treeobj;
150 err = got_object_commit_open(&commit, repo, obj);
154 err = got_object_id_str(&buf, commit->tree_id);
157 test_printf("tree: %s\n", buf);
159 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
160 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
161 err = got_object_id_str(&buf, pid->id);
164 test_printf("%s\n", buf);
167 test_printf("author: %s\n", commit->author);
168 test_printf("committer: %s\n", commit->committer);
169 test_printf("log: %s\n", commit->logmsg);
171 err = got_object_open(&treeobj, repo, commit->tree_id);
174 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
175 print_tree_object(treeobj, "", repo);
178 got_object_close(treeobj);
180 err = print_parent_commits(commit, repo);
181 got_object_commit_close(commit);
187 repo_read_log(const char *repo_path)
189 const struct got_error *err;
190 struct got_repository *repo;
191 struct got_reference *head_ref;
192 struct got_object_id *id;
193 struct got_object *obj;
196 err = got_repo_open(&repo, repo_path);
197 if (err != NULL || repo == NULL)
199 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
200 if (err != NULL || head_ref == NULL)
202 err = got_ref_resolve(&id, repo, head_ref);
203 if (err != NULL || head_ref == NULL)
205 err = got_object_id_str(&buf, id);
208 test_printf("HEAD is at %s\n", buf);
210 err = got_object_open(&obj, repo, id);
211 if (err != NULL || obj == NULL)
213 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
214 err = print_commit_object(obj, repo);
219 got_object_close(obj);
221 got_ref_close(head_ref);
222 got_repo_close(repo);
227 repo_read_tree(const char *repo_path)
229 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
230 const struct got_error *err;
231 struct got_repository *repo;
232 struct got_object *obj;
234 err = got_repo_open(&repo, repo_path);
235 if (err != NULL || repo == NULL)
237 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
238 if (err != NULL || obj == NULL)
240 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
243 print_tree_object(obj, "", repo);
246 got_object_close(obj);
247 got_repo_close(repo);
248 return (err == NULL);
252 repo_read_blob(const char *repo_path)
254 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
255 const struct got_error *err;
256 struct got_repository *repo;
257 struct got_object *obj;
258 struct got_blob_object *blob;
262 err = got_repo_open(&repo, repo_path);
263 if (err != NULL || repo == NULL)
265 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
266 if (err != NULL || obj == NULL)
268 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
271 err = got_object_blob_open(&blob, repo, obj, 64);
277 const uint8_t *buf = got_object_blob_get_read_buf(blob);
278 err = got_object_blob_read_block(&len, blob);
281 for (i = 0; i < len; i++)
282 test_printf("%c", buf[i]);
286 got_object_blob_close(blob);
287 got_object_close(obj);
288 got_repo_close(repo);
289 return (err == NULL);
293 repo_diff_blob(const char *repo_path)
295 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
296 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
297 const struct got_error *err;
298 struct got_repository *repo;
299 struct got_object *obj1;
300 struct got_object *obj2;
301 struct got_blob_object *blob1;
302 struct got_blob_object *blob2;
307 const char delim[3] = {'\0', '\0', '\0'};
308 const char *expected_output[] = {
309 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
310 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
312 " .PATH:${.CURDIR}/../../lib",
314 " PROG = repository_test",
315 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
316 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
318 " CPPFLAGS = -I${.CURDIR}/../../include",
320 "+LDADD = -lutil -lz",
325 err = got_repo_open(&repo, repo_path);
326 if (err != NULL || repo == NULL)
329 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
330 if (err != NULL || obj1 == NULL)
332 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
334 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
335 if (err != NULL || obj2 == NULL)
337 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
340 err = got_object_blob_open(&blob1, repo, obj1, 512);
344 err = got_object_blob_open(&blob2, repo, obj2, 512);
349 outfile = got_opentemp();
352 got_diff_blob(blob1, blob2, NULL, NULL, outfile);
355 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
358 if (i < nitems(expected_output) &&
359 strcmp(line, expected_output[i]) != 0) {
360 test_printf("diff output mismatch; expected: '%s'\n",
367 if (i != nitems(expected_output) + 1) {
368 test_printf("number of lines expected: %d; actual: %d\n",
369 nitems(expected_output), i - 1);
373 got_object_blob_close(blob1);
374 got_object_blob_close(blob2);
375 got_object_close(obj1);
376 got_object_close(obj2);
377 got_repo_close(repo);
378 return (err == NULL);
382 repo_diff_tree(const char *repo_path)
384 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
385 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
386 const struct got_error *err;
387 struct got_repository *repo;
388 struct got_object *obj1;
389 struct got_object *obj2;
390 struct got_tree_object *tree1;
391 struct got_tree_object *tree2;
394 err = got_repo_open(&repo, repo_path);
395 if (err != NULL || repo == NULL)
398 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
399 if (err != NULL || obj1 == NULL)
401 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
403 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
404 if (err != NULL || obj2 == NULL)
406 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
409 err = got_object_tree_open(&tree1, repo, obj1);
413 err = got_object_tree_open(&tree2, repo, obj2);
418 outfile = fopen("/dev/null", "w+");
424 got_diff_tree(tree1, tree2, repo, outfile);
427 got_object_tree_close(tree1);
428 got_object_tree_close(tree2);
429 got_object_close(obj1);
430 got_object_close(obj2);
431 got_repo_close(repo);
432 return (err == NULL);
435 #define RUN_TEST(expr, name) \
436 { test_ok = (expr); \
437 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
438 failure = (failure || !test_ok); }
444 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
448 main(int argc, char *argv[])
450 int test_ok = 0, failure = 0;
451 const char *repo_path;
454 if (pledge("stdio rpath wpath cpath", NULL) == -1)
457 while ((ch = getopt(argc, argv, "v")) != -1) {
471 repo_path = GOT_REPO_PATH;
479 RUN_TEST(repo_read_tree(repo_path), "read_tree");
480 RUN_TEST(repo_read_log(repo_path), "read_log");
481 RUN_TEST(repo_read_blob(repo_path), "read_blob");
482 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
483 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
485 return failure ? 1 : 0;