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>
26 #include "got_error.h"
27 #include "got_object.h"
29 #include "got_repository.h"
32 #define GOT_REPO_PATH "../../../"
37 test_printf(char *fmt, ...)
49 static const struct got_error *
50 print_commit_object(struct got_object *, struct got_repository *);
52 static const struct got_error *
53 print_parent_commits(struct got_commit_object *commit,
54 struct got_repository *repo)
56 struct got_parent_id *pid;
57 const struct got_error *err = NULL;
58 struct got_object *obj;
60 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
61 err = got_object_open(&obj, repo, pid->id);
64 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
65 return got_error(GOT_ERR_OBJ_TYPE);
66 err = print_commit_object(obj, repo);
67 got_object_close(obj);
73 static const struct got_error *
74 print_tree_object(struct got_object *obj, char *parent,
75 struct got_repository *repo)
77 struct got_tree_object *tree;
78 struct got_tree_entry *te;
79 const struct got_error *err;
81 err = got_object_tree_open(&tree, repo, obj);
85 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
86 struct got_object *treeobj;
90 err = got_object_id_str(&hex, te->id);
94 if (!S_ISDIR(te->mode)) {
95 test_printf("%s %s/%s\n", hex, parent, te->name);
98 test_printf("%s %s/%s\n", hex, parent, te->name);
101 err = got_object_open(&treeobj, repo, te->id);
105 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
106 err = got_error(GOT_ERR_OBJ_TYPE);
107 got_object_close(treeobj);
111 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
112 err = got_error(GOT_ERR_NO_MEM);
113 got_object_close(treeobj);
117 err = print_tree_object(treeobj, next_parent, repo);
120 got_object_close(treeobj);
124 got_object_close(treeobj);
127 got_object_tree_close(tree);
131 static const struct got_error *
132 print_commit_object(struct got_object *obj, struct got_repository *repo)
134 struct got_commit_object *commit;
135 struct got_parent_id *pid;
137 const struct got_error *err;
138 struct got_object* treeobj;
140 err = got_object_commit_open(&commit, repo, obj);
144 err = got_object_id_str(&buf, commit->tree_id);
147 test_printf("tree: %s\n", buf);
149 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
150 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
151 err = got_object_id_str(&buf, pid->id);
154 test_printf("%s\n", buf);
157 test_printf("author: %s\n", commit->author);
158 test_printf("committer: %s\n", commit->committer);
159 test_printf("log: %s\n", commit->logmsg);
161 err = got_object_open(&treeobj, repo, commit->tree_id);
164 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
165 print_tree_object(treeobj, "", repo);
168 got_object_close(treeobj);
170 err = print_parent_commits(commit, repo);
171 got_object_commit_close(commit);
177 repo_read_log(const char *repo_path)
179 const struct got_error *err;
180 struct got_repository *repo;
181 struct got_reference *head_ref;
182 struct got_object_id *id;
183 struct got_object *obj;
187 err = got_repo_open(&repo, repo_path);
188 if (err != NULL || repo == NULL)
190 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
191 if (err != NULL || head_ref == NULL)
193 err = got_ref_resolve(&id, repo, head_ref);
194 if (err != NULL || head_ref == NULL)
196 err = got_object_id_str(&buf, id);
199 test_printf("HEAD is at %s\n", buf);
201 err = got_object_open(&obj, repo, id);
202 if (err != NULL || obj == NULL)
204 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
205 err = print_commit_object(obj, repo);
210 got_object_close(obj);
212 got_ref_close(head_ref);
213 got_repo_close(repo);
218 repo_read_tree(const char *repo_path)
220 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
221 const struct got_error *err;
222 struct got_repository *repo;
223 struct got_object *obj;
227 err = got_repo_open(&repo, repo_path);
228 if (err != NULL || repo == NULL)
230 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
231 if (err != NULL || obj == NULL)
233 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
236 print_tree_object(obj, "", repo);
239 got_object_close(obj);
240 got_repo_close(repo);
241 return (err == NULL);
244 repo_read_blob(const char *repo_path)
246 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
247 const struct got_error *err;
248 struct got_repository *repo;
249 struct got_object *obj;
250 struct got_blob_object *blob;
254 err = got_repo_open(&repo, repo_path);
255 if (err != NULL || repo == NULL)
257 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
258 if (err != NULL || obj == NULL)
260 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
263 err = got_object_blob_open(&blob, repo, obj, 64);
269 const uint8_t *buf = got_object_blob_get_read_buf(blob);
270 err = got_object_blob_read_block(&len, blob);
273 for (i = 0; i < len; i++)
274 test_printf("%c", buf[i]);
278 got_object_blob_close(blob);
279 got_object_close(obj);
280 got_repo_close(repo);
281 return (err == NULL);
285 repo_diff_blob(const char *repo_path)
287 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
288 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
289 const struct got_error *err;
290 struct got_repository *repo;
291 struct got_object *obj1;
292 struct got_object *obj2;
293 struct got_blob_object *blob1;
294 struct got_blob_object *blob2;
299 err = got_repo_open(&repo, repo_path);
300 if (err != NULL || repo == NULL)
303 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
304 if (err != NULL || obj1 == NULL)
306 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
308 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
309 if (err != NULL || obj2 == NULL)
311 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
314 err = got_object_blob_open(&blob1, repo, obj1, 512);
318 err = got_object_blob_open(&blob2, repo, obj2, 512);
324 outfile = fopen("/dev/null", "w+");
329 got_diff_blob(blob1, blob2, NULL, NULL, outfile);
332 got_object_blob_close(blob1);
333 got_object_blob_close(blob2);
334 got_object_close(obj1);
335 got_object_close(obj2);
336 got_repo_close(repo);
337 return (err == NULL);
341 repo_diff_tree(const char *repo_path)
343 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
344 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
345 const struct got_error *err;
346 struct got_repository *repo;
347 struct got_object *obj1;
348 struct got_object *obj2;
349 struct got_tree_object *tree1;
350 struct got_tree_object *tree2;
355 err = got_repo_open(&repo, repo_path);
356 if (err != NULL || repo == NULL)
359 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
360 if (err != NULL || obj1 == NULL)
362 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
364 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
365 if (err != NULL || obj2 == NULL)
367 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
370 err = got_object_tree_open(&tree1, repo, obj1);
374 err = got_object_tree_open(&tree2, repo, obj2);
379 outfile = fopen("/dev/null", "w+");
385 got_diff_tree(tree1, tree2, repo, outfile);
388 got_object_tree_close(tree1);
389 got_object_tree_close(tree2);
390 got_object_close(obj1);
391 got_object_close(obj2);
392 got_repo_close(repo);
393 return (err == NULL);
396 #define RUN_TEST(expr, name) \
397 { test_ok = (expr); \
398 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
399 failure = (failure || !test_ok); }
405 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
409 main(int argc, char *argv[])
411 int test_ok = 0, failure = 0;
412 const char *repo_path;
416 while ((ch = getopt(argc, argv, "v")) != -1) {
430 repo_path = GOT_REPO_PATH;
438 RUN_TEST(repo_read_tree(repo_path), "read_tree");
439 RUN_TEST(repo_read_log(repo_path), "read_log");
440 RUN_TEST(repo_read_blob(repo_path), "read_blob");
441 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
442 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
444 return failure ? 1 : 0;