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"
35 #define GOT_REPO_PATH "../../../"
40 test_printf(char *fmt, ...)
52 static const struct got_error *
53 print_commit_object(struct got_object *, struct got_repository *);
55 static const struct got_error *
56 print_parent_commits(struct got_commit_object *commit,
57 struct got_repository *repo)
59 struct got_parent_id *pid;
60 const struct got_error *err = NULL;
61 struct got_object *obj;
63 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
64 err = got_object_open(&obj, repo, pid->id);
67 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
68 return got_error(GOT_ERR_OBJ_TYPE);
69 err = print_commit_object(obj, repo);
70 got_object_close(obj);
76 static const struct got_error *
77 print_tree_object(struct got_object *obj, char *parent,
78 struct got_repository *repo)
80 struct got_tree_object *tree;
81 struct got_tree_entry *te;
82 const struct got_error *err;
84 err = got_object_tree_open(&tree, repo, obj);
88 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
89 struct got_object *treeobj;
93 err = got_object_id_str(&hex, te->id);
97 if (!S_ISDIR(te->mode)) {
98 test_printf("%s %s/%s\n", hex, parent, te->name);
101 test_printf("%s %s/%s\n", hex, parent, te->name);
104 err = got_object_open(&treeobj, repo, te->id);
108 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
109 err = got_error(GOT_ERR_OBJ_TYPE);
110 got_object_close(treeobj);
114 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
115 err = got_error(GOT_ERR_NO_MEM);
116 got_object_close(treeobj);
120 err = print_tree_object(treeobj, next_parent, repo);
123 got_object_close(treeobj);
127 got_object_close(treeobj);
130 got_object_tree_close(tree);
134 static const struct got_error *
135 print_commit_object(struct got_object *obj, struct got_repository *repo)
137 struct got_commit_object *commit;
138 struct got_parent_id *pid;
140 const struct got_error *err;
141 struct got_object* treeobj;
143 err = got_object_commit_open(&commit, repo, obj);
147 err = got_object_id_str(&buf, commit->tree_id);
150 test_printf("tree: %s\n", buf);
152 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
153 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
154 err = got_object_id_str(&buf, pid->id);
157 test_printf("%s\n", buf);
160 test_printf("author: %s\n", commit->author);
161 test_printf("committer: %s\n", commit->committer);
162 test_printf("log: %s\n", commit->logmsg);
164 err = got_object_open(&treeobj, repo, commit->tree_id);
167 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
168 print_tree_object(treeobj, "", repo);
171 got_object_close(treeobj);
173 err = print_parent_commits(commit, repo);
174 got_object_commit_close(commit);
180 repo_read_log(const char *repo_path)
182 const struct got_error *err;
183 struct got_repository *repo;
184 struct got_reference *head_ref;
185 struct got_object_id *id;
186 struct got_object *obj;
190 err = got_repo_open(&repo, repo_path);
191 if (err != NULL || repo == NULL)
193 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
194 if (err != NULL || head_ref == NULL)
196 err = got_ref_resolve(&id, repo, head_ref);
197 if (err != NULL || head_ref == NULL)
199 err = got_object_id_str(&buf, id);
202 test_printf("HEAD is at %s\n", buf);
204 err = got_object_open(&obj, repo, id);
205 if (err != NULL || obj == NULL)
207 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
208 err = print_commit_object(obj, repo);
213 got_object_close(obj);
215 got_ref_close(head_ref);
216 got_repo_close(repo);
221 repo_read_tree(const char *repo_path)
223 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
224 const struct got_error *err;
225 struct got_repository *repo;
226 struct got_object *obj;
227 char hex[SHA1_DIGEST_STRING_LENGTH];
231 err = got_repo_open(&repo, repo_path);
232 if (err != NULL || repo == NULL)
234 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
235 if (err != NULL || obj == NULL)
237 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
240 print_tree_object(obj, "", repo);
243 got_object_close(obj);
244 got_repo_close(repo);
245 return (err == NULL);
248 repo_read_blob(const char *repo_path)
250 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
251 const struct got_error *err;
252 struct got_repository *repo;
253 struct got_object *obj;
254 struct got_blob_object *blob;
255 char hex[SHA1_DIGEST_STRING_LENGTH];
259 err = got_repo_open(&repo, repo_path);
260 if (err != NULL || repo == NULL)
262 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
263 if (err != NULL || obj == NULL)
265 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
268 err = got_object_blob_open(&blob, repo, obj, 64);
274 const uint8_t *buf = got_object_blob_get_read_buf(blob);
275 err = got_object_blob_read_block(&len, blob);
278 for (i = 0; i < len; i++)
279 test_printf("%c", buf[i]);
283 got_object_blob_close(blob);
284 got_object_close(obj);
285 got_repo_close(repo);
286 return (err == NULL);
290 repo_diff_blob(const char *repo_path)
292 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
293 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
294 const struct got_error *err;
295 struct got_repository *repo;
296 struct got_object *obj1;
297 struct got_object *obj2;
298 struct got_blob_object *blob1;
299 struct got_blob_object *blob2;
300 char hex[SHA1_DIGEST_STRING_LENGTH];
305 err = got_repo_open(&repo, repo_path);
306 if (err != NULL || repo == NULL)
309 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
310 if (err != NULL || obj1 == NULL)
312 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
314 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
315 if (err != NULL || obj2 == NULL)
317 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
320 err = got_object_blob_open(&blob1, repo, obj1, 512);
324 err = got_object_blob_open(&blob2, repo, obj2, 512);
330 outfile = fopen("/dev/null", "w+");
335 got_diff_blob(blob1, blob2, NULL, NULL, outfile);
338 got_object_blob_close(blob1);
339 got_object_blob_close(blob2);
340 got_object_close(obj1);
341 got_object_close(obj2);
342 got_repo_close(repo);
343 return (err == NULL);
347 repo_diff_tree(const char *repo_path)
349 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
350 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
351 const struct got_error *err;
352 struct got_repository *repo;
353 struct got_object *obj1;
354 struct got_object *obj2;
355 struct got_tree_object *tree1;
356 struct got_tree_object *tree2;
357 char hex[SHA1_DIGEST_STRING_LENGTH];
362 err = got_repo_open(&repo, repo_path);
363 if (err != NULL || repo == NULL)
366 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
367 if (err != NULL || obj1 == NULL)
369 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
371 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
372 if (err != NULL || obj2 == NULL)
374 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
377 err = got_object_tree_open(&tree1, repo, obj1);
381 err = got_object_tree_open(&tree2, repo, obj2);
386 outfile = fopen("/dev/null", "w+");
392 got_diff_tree(tree1, tree2, repo, outfile);
395 got_object_tree_close(tree1);
396 got_object_tree_close(tree2);
397 got_object_close(obj1);
398 got_object_close(obj2);
399 got_repo_close(repo);
400 return (err == NULL);
403 #define RUN_TEST(expr, name) \
404 { test_ok = (expr); \
405 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
406 failure = (failure || !test_ok); }
412 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
416 main(int argc, char *argv[])
418 int test_ok = 0, failure = 0;
419 const char *repo_path;
423 while ((ch = getopt(argc, argv, "v")) != -1) {
437 repo_path = GOT_REPO_PATH;
445 RUN_TEST(repo_read_tree(repo_path), "read_tree");
446 RUN_TEST(repo_read_log(repo_path), "read_log");
447 RUN_TEST(repo_read_blob(repo_path), "read_blob");
448 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
449 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
451 return failure ? 1 : 0;