2 7b19e0f1 2017-11-05 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
18 d1cda826 2017-11-06 stsp #include <sys/queue.h>
20 82f2fb69 2018-01-26 stsp #include <stdarg.h>
21 4027f31a 2017-11-04 stsp #include <stdio.h>
22 4027f31a 2017-11-04 stsp #include <stdlib.h>
23 82f2fb69 2018-01-26 stsp #include <string.h>
24 f8352b2a 2018-03-12 stsp #include <unistd.h>
25 f8352b2a 2018-03-12 stsp #include <err.h>
26 59ece79d 2018-02-12 stsp #include <unistd.h>
28 4027f31a 2017-11-04 stsp #include "got_error.h"
29 11995603 2017-11-05 stsp #include "got_object.h"
30 4027f31a 2017-11-04 stsp #include "got_refs.h"
31 4027f31a 2017-11-04 stsp #include "got_repository.h"
32 f78b0693 2017-11-29 stsp #include "got_diff.h"
34 4027f31a 2017-11-04 stsp #define GOT_REPO_PATH "../../../"
36 14545512 2018-01-26 stsp static int verbose;
39 82f2fb69 2018-01-26 stsp test_printf(char *fmt, ...)
43 82f2fb69 2018-01-26 stsp if (!verbose)
46 82f2fb69 2018-01-26 stsp va_start(ap, fmt);
47 82f2fb69 2018-01-26 stsp vprintf(fmt, ap);
51 bfab4d9a 2017-11-12 stsp static const struct got_error *
52 bfab4d9a 2017-11-12 stsp print_commit_object(struct got_object *, struct got_repository *);
54 1c852fbe 2017-11-12 stsp static const struct got_error *
55 bfab4d9a 2017-11-12 stsp print_parent_commits(struct got_commit_object *commit,
56 bfab4d9a 2017-11-12 stsp struct got_repository *repo)
58 bfab4d9a 2017-11-12 stsp struct got_parent_id *pid;
59 a37d050f 2018-01-26 stsp const struct got_error *err = NULL;
60 bfab4d9a 2017-11-12 stsp struct got_object *obj;
62 bfab4d9a 2017-11-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
63 59ece79d 2018-02-12 stsp err = got_object_open(&obj, repo, pid->id);
64 bfab4d9a 2017-11-12 stsp if (err != NULL)
66 b107e67f 2018-01-19 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
67 bfab4d9a 2017-11-12 stsp return got_error(GOT_ERR_OBJ_TYPE);
68 a37d050f 2018-01-26 stsp err = print_commit_object(obj, repo);
69 bfab4d9a 2017-11-12 stsp got_object_close(obj);
75 bfab4d9a 2017-11-12 stsp static const struct got_error *
76 f715ca7f 2017-11-27 stsp print_tree_object(struct got_object *obj, char *parent,
77 f715ca7f 2017-11-27 stsp struct got_repository *repo)
79 0ffeb3c2 2017-11-26 stsp struct got_tree_object *tree;
80 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
81 0ffeb3c2 2017-11-26 stsp const struct got_error *err;
83 0ffeb3c2 2017-11-26 stsp err = got_object_tree_open(&tree, repo, obj);
84 0ffeb3c2 2017-11-26 stsp if (err != NULL)
87 f715ca7f 2017-11-27 stsp SIMPLEQ_FOREACH(te, &tree->entries, entry) {
88 f715ca7f 2017-11-27 stsp struct got_object *treeobj;
89 f715ca7f 2017-11-27 stsp char *next_parent;
92 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, te->id);
96 f715ca7f 2017-11-27 stsp if (!S_ISDIR(te->mode)) {
97 ef0981d5 2018-02-12 stsp test_printf("%s %s/%s\n", hex, parent, te->name);
100 ef0981d5 2018-02-12 stsp test_printf("%s %s/%s\n", hex, parent, te->name);
103 59ece79d 2018-02-12 stsp err = got_object_open(&treeobj, repo, te->id);
104 f715ca7f 2017-11-27 stsp if (err != NULL)
107 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
108 f715ca7f 2017-11-27 stsp err = got_error(GOT_ERR_OBJ_TYPE);
109 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
113 f715ca7f 2017-11-27 stsp if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
114 f715ca7f 2017-11-27 stsp err = got_error(GOT_ERR_NO_MEM);
115 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
119 f715ca7f 2017-11-27 stsp err = print_tree_object(treeobj, next_parent, repo);
120 f715ca7f 2017-11-27 stsp free(next_parent);
122 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
126 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
129 0ffeb3c2 2017-11-26 stsp got_object_tree_close(tree);
130 f715ca7f 2017-11-27 stsp return err;
133 0ffeb3c2 2017-11-26 stsp static const struct got_error *
134 1c852fbe 2017-11-12 stsp print_commit_object(struct got_object *obj, struct got_repository *repo)
136 1c852fbe 2017-11-12 stsp struct got_commit_object *commit;
137 1c852fbe 2017-11-12 stsp struct got_parent_id *pid;
139 1c852fbe 2017-11-12 stsp const struct got_error *err;
140 0ffeb3c2 2017-11-26 stsp struct got_object* treeobj;
142 1c852fbe 2017-11-12 stsp err = got_object_commit_open(&commit, repo, obj);
144 1c852fbe 2017-11-12 stsp return err;
146 ef0981d5 2018-02-12 stsp err = got_object_id_str(&buf, commit->tree_id);
148 ef0981d5 2018-02-12 stsp return err;
149 ef0981d5 2018-02-12 stsp test_printf("tree: %s\n", buf);
151 82f2fb69 2018-01-26 stsp test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
152 1c852fbe 2017-11-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
153 ef0981d5 2018-02-12 stsp err = got_object_id_str(&buf, pid->id);
155 ef0981d5 2018-02-12 stsp return err;
156 ef0981d5 2018-02-12 stsp test_printf("%s\n", buf);
159 82f2fb69 2018-01-26 stsp test_printf("author: %s\n", commit->author);
160 82f2fb69 2018-01-26 stsp test_printf("committer: %s\n", commit->committer);
161 82f2fb69 2018-01-26 stsp test_printf("log: %s\n", commit->logmsg);
163 59ece79d 2018-02-12 stsp err = got_object_open(&treeobj, repo, commit->tree_id);
164 0ffeb3c2 2017-11-26 stsp if (err != NULL)
165 0ffeb3c2 2017-11-26 stsp return err;
166 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
167 f715ca7f 2017-11-27 stsp print_tree_object(treeobj, "", repo);
168 82f2fb69 2018-01-26 stsp test_printf("\n");
170 0ffeb3c2 2017-11-26 stsp got_object_close(treeobj);
172 bfab4d9a 2017-11-12 stsp err = print_parent_commits(commit, repo);
173 1c852fbe 2017-11-12 stsp got_object_commit_close(commit);
175 bfab4d9a 2017-11-12 stsp return err;
179 bfab4d9a 2017-11-12 stsp repo_read_log(const char *repo_path)
181 11995603 2017-11-05 stsp const struct got_error *err;
182 11995603 2017-11-05 stsp struct got_repository *repo;
183 11995603 2017-11-05 stsp struct got_reference *head_ref;
184 11995603 2017-11-05 stsp struct got_object_id *id;
185 ab9a70b2 2017-11-06 stsp struct got_object *obj;
188 11995603 2017-11-05 stsp err = got_repo_open(&repo, repo_path);
189 11995603 2017-11-05 stsp if (err != NULL || repo == NULL)
191 11995603 2017-11-05 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
192 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
194 11995603 2017-11-05 stsp err = got_ref_resolve(&id, repo, head_ref);
195 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
197 ef0981d5 2018-02-12 stsp err = got_object_id_str(&buf, id);
198 ef0981d5 2018-02-12 stsp if (err != NULL)
200 ef0981d5 2018-02-12 stsp test_printf("HEAD is at %s\n", buf);
202 ab9a70b2 2017-11-06 stsp err = got_object_open(&obj, repo, id);
203 ab9a70b2 2017-11-06 stsp if (err != NULL || obj == NULL)
205 a37d050f 2018-01-26 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
206 a37d050f 2018-01-26 stsp err = print_commit_object(obj, repo);
211 ab9a70b2 2017-11-06 stsp got_object_close(obj);
213 11995603 2017-11-05 stsp got_ref_close(head_ref);
214 11995603 2017-11-05 stsp got_repo_close(repo);
219 044e7393 2018-02-11 stsp repo_read_tree(const char *repo_path)
221 044e7393 2018-02-11 stsp const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
222 044e7393 2018-02-11 stsp const struct got_error *err;
223 044e7393 2018-02-11 stsp struct got_repository *repo;
224 044e7393 2018-02-11 stsp struct got_object *obj;
226 044e7393 2018-02-11 stsp err = got_repo_open(&repo, repo_path);
227 044e7393 2018-02-11 stsp if (err != NULL || repo == NULL)
229 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj, repo, tree_sha1);
230 044e7393 2018-02-11 stsp if (err != NULL || obj == NULL)
232 044e7393 2018-02-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
235 044e7393 2018-02-11 stsp print_tree_object(obj, "", repo);
236 044e7393 2018-02-11 stsp test_printf("\n");
238 044e7393 2018-02-11 stsp got_object_close(obj);
239 044e7393 2018-02-11 stsp got_repo_close(repo);
240 044e7393 2018-02-11 stsp return (err == NULL);
243 68482ea3 2017-11-27 stsp repo_read_blob(const char *repo_path)
245 68482ea3 2017-11-27 stsp const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
246 68482ea3 2017-11-27 stsp const struct got_error *err;
247 68482ea3 2017-11-27 stsp struct got_repository *repo;
248 68482ea3 2017-11-27 stsp struct got_object *obj;
249 68482ea3 2017-11-27 stsp struct got_blob_object *blob;
251 68482ea3 2017-11-27 stsp size_t len;
253 68482ea3 2017-11-27 stsp err = got_repo_open(&repo, repo_path);
254 68482ea3 2017-11-27 stsp if (err != NULL || repo == NULL)
256 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj, repo, blob_sha1);
257 68482ea3 2017-11-27 stsp if (err != NULL || obj == NULL)
259 b107e67f 2018-01-19 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
262 68482ea3 2017-11-27 stsp err = got_object_blob_open(&blob, repo, obj, 64);
263 68482ea3 2017-11-27 stsp if (err != NULL)
266 82f2fb69 2018-01-26 stsp test_printf("\n");
268 f934cf2c 2018-02-12 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
269 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob);
272 68482ea3 2017-11-27 stsp for (i = 0; i < len; i++)
273 f934cf2c 2018-02-12 stsp test_printf("%c", buf[i]);
274 68482ea3 2017-11-27 stsp } while (len != 0);
275 82f2fb69 2018-01-26 stsp test_printf("\n");
277 68482ea3 2017-11-27 stsp got_object_blob_close(blob);
278 68482ea3 2017-11-27 stsp got_object_close(obj);
279 7d283eee 2017-11-29 stsp got_repo_close(repo);
280 7d283eee 2017-11-29 stsp return (err == NULL);
284 7d283eee 2017-11-29 stsp repo_diff_blob(const char *repo_path)
286 7d283eee 2017-11-29 stsp const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
287 7d283eee 2017-11-29 stsp const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
288 7d283eee 2017-11-29 stsp const struct got_error *err;
289 7d283eee 2017-11-29 stsp struct got_repository *repo;
290 7d283eee 2017-11-29 stsp struct got_object *obj1;
291 7d283eee 2017-11-29 stsp struct got_object *obj2;
292 7d283eee 2017-11-29 stsp struct got_blob_object *blob1;
293 7d283eee 2017-11-29 stsp struct got_blob_object *blob2;
294 354a7e12 2018-02-11 stsp FILE *outfile;
296 7d283eee 2017-11-29 stsp err = got_repo_open(&repo, repo_path);
297 7d283eee 2017-11-29 stsp if (err != NULL || repo == NULL)
300 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
301 7d283eee 2017-11-29 stsp if (err != NULL || obj1 == NULL)
303 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
305 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
306 7d283eee 2017-11-29 stsp if (err != NULL || obj2 == NULL)
308 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
311 7d283eee 2017-11-29 stsp err = got_object_blob_open(&blob1, repo, obj1, 512);
312 7d283eee 2017-11-29 stsp if (err != NULL)
315 7d283eee 2017-11-29 stsp err = got_object_blob_open(&blob2, repo, obj2, 512);
316 7d283eee 2017-11-29 stsp if (err != NULL)
319 82f2fb69 2018-01-26 stsp test_printf("\n");
320 354a7e12 2018-02-11 stsp if (!verbose) {
321 354a7e12 2018-02-11 stsp outfile = fopen("/dev/null", "w+");
322 354a7e12 2018-02-11 stsp if (outfile == NULL)
325 354a7e12 2018-02-11 stsp outfile = stdout;
326 354a7e12 2018-02-11 stsp got_diff_blob(blob1, blob2, NULL, NULL, outfile);
327 82f2fb69 2018-01-26 stsp test_printf("\n");
329 7d283eee 2017-11-29 stsp got_object_blob_close(blob1);
330 7d283eee 2017-11-29 stsp got_object_blob_close(blob2);
331 98abbc84 2017-11-30 stsp got_object_close(obj1);
332 98abbc84 2017-11-30 stsp got_object_close(obj2);
333 98abbc84 2017-11-30 stsp got_repo_close(repo);
334 98abbc84 2017-11-30 stsp return (err == NULL);
338 98abbc84 2017-11-30 stsp repo_diff_tree(const char *repo_path)
340 4a0235dd 2017-11-30 stsp const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
341 a3e2cbea 2017-12-01 stsp const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
342 98abbc84 2017-11-30 stsp const struct got_error *err;
343 98abbc84 2017-11-30 stsp struct got_repository *repo;
344 98abbc84 2017-11-30 stsp struct got_object *obj1;
345 98abbc84 2017-11-30 stsp struct got_object *obj2;
346 98abbc84 2017-11-30 stsp struct got_tree_object *tree1;
347 98abbc84 2017-11-30 stsp struct got_tree_object *tree2;
348 354a7e12 2018-02-11 stsp FILE *outfile;
350 98abbc84 2017-11-30 stsp err = got_repo_open(&repo, repo_path);
351 98abbc84 2017-11-30 stsp if (err != NULL || repo == NULL)
354 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
355 98abbc84 2017-11-30 stsp if (err != NULL || obj1 == NULL)
357 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
359 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
360 98abbc84 2017-11-30 stsp if (err != NULL || obj2 == NULL)
362 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
365 98abbc84 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, obj1);
366 98abbc84 2017-11-30 stsp if (err != NULL)
369 98abbc84 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, obj2);
370 98abbc84 2017-11-30 stsp if (err != NULL)
373 354a7e12 2018-02-11 stsp if (!verbose) {
374 354a7e12 2018-02-11 stsp outfile = fopen("/dev/null", "w+");
375 354a7e12 2018-02-11 stsp if (outfile == NULL)
378 354a7e12 2018-02-11 stsp outfile = stdout;
379 82f2fb69 2018-01-26 stsp test_printf("\n");
380 354a7e12 2018-02-11 stsp got_diff_tree(tree1, tree2, repo, outfile);
381 82f2fb69 2018-01-26 stsp test_printf("\n");
383 98abbc84 2017-11-30 stsp got_object_tree_close(tree1);
384 98abbc84 2017-11-30 stsp got_object_tree_close(tree2);
385 7d283eee 2017-11-29 stsp got_object_close(obj1);
386 7d283eee 2017-11-29 stsp got_object_close(obj2);
387 68482ea3 2017-11-27 stsp got_repo_close(repo);
388 68482ea3 2017-11-27 stsp return (err == NULL);
391 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
392 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
393 b08fe7be 2018-01-26 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
394 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
398 82f2fb69 2018-01-26 stsp usage(void)
400 82f2fb69 2018-01-26 stsp fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
404 82f2fb69 2018-01-26 stsp main(int argc, char *argv[])
406 b08fe7be 2018-01-26 stsp int test_ok = 0, failure = 0;
407 4027f31a 2017-11-04 stsp const char *repo_path;
410 f8352b2a 2018-03-12 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
411 f8352b2a 2018-03-12 stsp err(1, "pledge");
413 82f2fb69 2018-01-26 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
414 82f2fb69 2018-01-26 stsp switch (ch) {
416 82f2fb69 2018-01-26 stsp verbose = 1;
423 82f2fb69 2018-01-26 stsp argc -= optind;
424 82f2fb69 2018-01-26 stsp argv += optind;
426 82f2fb69 2018-01-26 stsp if (argc == 0)
427 4027f31a 2017-11-04 stsp repo_path = GOT_REPO_PATH;
428 82f2fb69 2018-01-26 stsp else if (argc == 1)
429 ff3eb0f2 2018-03-09 stsp repo_path = argv[0];
435 044e7393 2018-02-11 stsp RUN_TEST(repo_read_tree(repo_path), "read_tree");
436 bfab4d9a 2017-11-12 stsp RUN_TEST(repo_read_log(repo_path), "read_log");
437 68482ea3 2017-11-27 stsp RUN_TEST(repo_read_blob(repo_path), "read_blob");
438 7d283eee 2017-11-29 stsp RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
439 98abbc84 2017-11-30 stsp RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
441 4027f31a 2017-11-04 stsp return failure ? 1 : 0;