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>
28 #include "got_error.h"
29 #include "got_object.h"
31 #include "got_repository.h"
34 #define GOT_REPO_PATH "../../../"
39 test_printf(char *fmt, ...)
51 static const struct got_error *
52 print_commit_object(struct got_object *, struct got_repository *);
54 static const struct got_error *
55 print_parent_commits(struct got_commit_object *commit,
56 struct got_repository *repo)
58 struct got_parent_id *pid;
59 const struct got_error *err = NULL;
60 struct got_object *obj;
62 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
63 err = got_object_open(&obj, repo, pid->id);
66 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
67 return got_error(GOT_ERR_OBJ_TYPE);
68 err = print_commit_object(obj, repo);
69 got_object_close(obj);
75 static const struct got_error *
76 print_tree_object(struct got_object *obj, char *parent,
77 struct got_repository *repo)
79 struct got_tree_object *tree;
80 struct got_tree_entry *te;
81 const struct got_error *err;
83 err = got_object_tree_open(&tree, repo, obj);
87 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
88 struct got_object *treeobj;
92 err = got_object_id_str(&hex, te->id);
96 if (!S_ISDIR(te->mode)) {
97 test_printf("%s %s/%s\n", hex, parent, te->name);
100 test_printf("%s %s/%s\n", hex, parent, te->name);
103 err = got_object_open(&treeobj, repo, te->id);
107 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
108 err = got_error(GOT_ERR_OBJ_TYPE);
109 got_object_close(treeobj);
113 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
114 err = got_error(GOT_ERR_NO_MEM);
115 got_object_close(treeobj);
119 err = print_tree_object(treeobj, next_parent, repo);
122 got_object_close(treeobj);
126 got_object_close(treeobj);
129 got_object_tree_close(tree);
133 static const struct got_error *
134 print_commit_object(struct got_object *obj, struct got_repository *repo)
136 struct got_commit_object *commit;
137 struct got_parent_id *pid;
139 const struct got_error *err;
140 struct got_object* treeobj;
142 err = got_object_commit_open(&commit, repo, obj);
146 err = got_object_id_str(&buf, commit->tree_id);
149 test_printf("tree: %s\n", buf);
151 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
152 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
153 err = got_object_id_str(&buf, pid->id);
156 test_printf("%s\n", buf);
159 test_printf("author: %s\n", commit->author);
160 test_printf("committer: %s\n", commit->committer);
161 test_printf("log: %s\n", commit->logmsg);
163 err = got_object_open(&treeobj, repo, commit->tree_id);
166 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
167 print_tree_object(treeobj, "", repo);
170 got_object_close(treeobj);
172 err = print_parent_commits(commit, repo);
173 got_object_commit_close(commit);
179 repo_read_log(const char *repo_path)
181 const struct got_error *err;
182 struct got_repository *repo;
183 struct got_reference *head_ref;
184 struct got_object_id *id;
185 struct got_object *obj;
188 err = got_repo_open(&repo, repo_path);
189 if (err != NULL || repo == NULL)
191 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
192 if (err != NULL || head_ref == NULL)
194 err = got_ref_resolve(&id, repo, head_ref);
195 if (err != NULL || head_ref == NULL)
197 err = got_object_id_str(&buf, id);
200 test_printf("HEAD is at %s\n", buf);
202 err = got_object_open(&obj, repo, id);
203 if (err != NULL || obj == NULL)
205 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
206 err = print_commit_object(obj, repo);
211 got_object_close(obj);
213 got_ref_close(head_ref);
214 got_repo_close(repo);
219 repo_read_tree(const char *repo_path)
221 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
222 const struct got_error *err;
223 struct got_repository *repo;
224 struct got_object *obj;
226 err = got_repo_open(&repo, repo_path);
227 if (err != NULL || repo == NULL)
229 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
230 if (err != NULL || obj == NULL)
232 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
235 print_tree_object(obj, "", repo);
238 got_object_close(obj);
239 got_repo_close(repo);
240 return (err == NULL);
243 repo_read_blob(const char *repo_path)
245 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
246 const struct got_error *err;
247 struct got_repository *repo;
248 struct got_object *obj;
249 struct got_blob_object *blob;
253 err = got_repo_open(&repo, repo_path);
254 if (err != NULL || repo == NULL)
256 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
257 if (err != NULL || obj == NULL)
259 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
262 err = got_object_blob_open(&blob, repo, obj, 64);
268 const uint8_t *buf = got_object_blob_get_read_buf(blob);
269 err = got_object_blob_read_block(&len, blob);
272 for (i = 0; i < len; i++)
273 test_printf("%c", buf[i]);
277 got_object_blob_close(blob);
278 got_object_close(obj);
279 got_repo_close(repo);
280 return (err == NULL);
284 repo_diff_blob(const char *repo_path)
286 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
287 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
288 const struct got_error *err;
289 struct got_repository *repo;
290 struct got_object *obj1;
291 struct got_object *obj2;
292 struct got_blob_object *blob1;
293 struct got_blob_object *blob2;
296 err = got_repo_open(&repo, repo_path);
297 if (err != NULL || repo == NULL)
300 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
301 if (err != NULL || obj1 == NULL)
303 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
305 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
306 if (err != NULL || obj2 == NULL)
308 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
311 err = got_object_blob_open(&blob1, repo, obj1, 512);
315 err = got_object_blob_open(&blob2, repo, obj2, 512);
321 outfile = fopen("/dev/null", "w+");
326 got_diff_blob(blob1, blob2, NULL, NULL, outfile);
329 got_object_blob_close(blob1);
330 got_object_blob_close(blob2);
331 got_object_close(obj1);
332 got_object_close(obj2);
333 got_repo_close(repo);
334 return (err == NULL);
338 repo_diff_tree(const char *repo_path)
340 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
341 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
342 const struct got_error *err;
343 struct got_repository *repo;
344 struct got_object *obj1;
345 struct got_object *obj2;
346 struct got_tree_object *tree1;
347 struct got_tree_object *tree2;
350 err = got_repo_open(&repo, repo_path);
351 if (err != NULL || repo == NULL)
354 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
355 if (err != NULL || obj1 == NULL)
357 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
359 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
360 if (err != NULL || obj2 == NULL)
362 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
365 err = got_object_tree_open(&tree1, repo, obj1);
369 err = got_object_tree_open(&tree2, repo, obj2);
374 outfile = fopen("/dev/null", "w+");
380 got_diff_tree(tree1, tree2, repo, outfile);
383 got_object_tree_close(tree1);
384 got_object_tree_close(tree2);
385 got_object_close(obj1);
386 got_object_close(obj2);
387 got_repo_close(repo);
388 return (err == NULL);
391 #define RUN_TEST(expr, name) \
392 { test_ok = (expr); \
393 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
394 failure = (failure || !test_ok); }
400 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
404 main(int argc, char *argv[])
406 int test_ok = 0, failure = 0;
407 const char *repo_path;
410 if (pledge("stdio rpath wpath cpath", NULL) == -1)
413 while ((ch = getopt(argc, argv, "v")) != -1) {
427 repo_path = GOT_REPO_PATH;
435 RUN_TEST(repo_read_tree(repo_path), "read_tree");
436 RUN_TEST(repo_read_log(repo_path), "read_log");
437 RUN_TEST(repo_read_blob(repo_path), "read_blob");
438 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
439 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
441 return failure ? 1 : 0;