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 59ece79d 2018-02-12 stsp #include <unistd.h>
26 4027f31a 2017-11-04 stsp #include "got_error.h"
27 11995603 2017-11-05 stsp #include "got_object.h"
28 4027f31a 2017-11-04 stsp #include "got_refs.h"
29 4027f31a 2017-11-04 stsp #include "got_repository.h"
30 f78b0693 2017-11-29 stsp #include "got_diff.h"
32 4027f31a 2017-11-04 stsp #define GOT_REPO_PATH "../../../"
34 14545512 2018-01-26 stsp static int verbose;
37 82f2fb69 2018-01-26 stsp test_printf(char *fmt, ...)
41 82f2fb69 2018-01-26 stsp if (!verbose)
44 82f2fb69 2018-01-26 stsp va_start(ap, fmt);
45 82f2fb69 2018-01-26 stsp vprintf(fmt, ap);
49 bfab4d9a 2017-11-12 stsp static const struct got_error *
50 bfab4d9a 2017-11-12 stsp print_commit_object(struct got_object *, struct got_repository *);
52 1c852fbe 2017-11-12 stsp static const struct got_error *
53 bfab4d9a 2017-11-12 stsp print_parent_commits(struct got_commit_object *commit,
54 bfab4d9a 2017-11-12 stsp struct got_repository *repo)
56 bfab4d9a 2017-11-12 stsp struct got_parent_id *pid;
57 a37d050f 2018-01-26 stsp const struct got_error *err = NULL;
58 bfab4d9a 2017-11-12 stsp struct got_object *obj;
60 bfab4d9a 2017-11-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
61 59ece79d 2018-02-12 stsp err = got_object_open(&obj, repo, pid->id);
62 bfab4d9a 2017-11-12 stsp if (err != NULL)
64 b107e67f 2018-01-19 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
65 bfab4d9a 2017-11-12 stsp return got_error(GOT_ERR_OBJ_TYPE);
66 a37d050f 2018-01-26 stsp err = print_commit_object(obj, repo);
67 bfab4d9a 2017-11-12 stsp got_object_close(obj);
73 bfab4d9a 2017-11-12 stsp static const struct got_error *
74 f715ca7f 2017-11-27 stsp print_tree_object(struct got_object *obj, char *parent,
75 f715ca7f 2017-11-27 stsp struct got_repository *repo)
77 0ffeb3c2 2017-11-26 stsp struct got_tree_object *tree;
78 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
79 0ffeb3c2 2017-11-26 stsp const struct got_error *err;
81 0ffeb3c2 2017-11-26 stsp err = got_object_tree_open(&tree, repo, obj);
82 0ffeb3c2 2017-11-26 stsp if (err != NULL)
85 f715ca7f 2017-11-27 stsp SIMPLEQ_FOREACH(te, &tree->entries, entry) {
86 f715ca7f 2017-11-27 stsp struct got_object *treeobj;
87 f715ca7f 2017-11-27 stsp char *next_parent;
90 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, te->id);
94 f715ca7f 2017-11-27 stsp if (!S_ISDIR(te->mode)) {
95 ef0981d5 2018-02-12 stsp test_printf("%s %s/%s\n", hex, parent, te->name);
98 ef0981d5 2018-02-12 stsp test_printf("%s %s/%s\n", hex, parent, te->name);
101 59ece79d 2018-02-12 stsp err = got_object_open(&treeobj, repo, te->id);
102 f715ca7f 2017-11-27 stsp if (err != NULL)
105 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
106 f715ca7f 2017-11-27 stsp err = got_error(GOT_ERR_OBJ_TYPE);
107 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
111 f715ca7f 2017-11-27 stsp if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
112 f715ca7f 2017-11-27 stsp err = got_error(GOT_ERR_NO_MEM);
113 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
117 f715ca7f 2017-11-27 stsp err = print_tree_object(treeobj, next_parent, repo);
118 f715ca7f 2017-11-27 stsp free(next_parent);
120 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
124 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
127 0ffeb3c2 2017-11-26 stsp got_object_tree_close(tree);
128 f715ca7f 2017-11-27 stsp return err;
131 0ffeb3c2 2017-11-26 stsp static const struct got_error *
132 1c852fbe 2017-11-12 stsp print_commit_object(struct got_object *obj, struct got_repository *repo)
134 1c852fbe 2017-11-12 stsp struct got_commit_object *commit;
135 1c852fbe 2017-11-12 stsp struct got_parent_id *pid;
137 1c852fbe 2017-11-12 stsp const struct got_error *err;
138 0ffeb3c2 2017-11-26 stsp struct got_object* treeobj;
140 1c852fbe 2017-11-12 stsp err = got_object_commit_open(&commit, repo, obj);
142 1c852fbe 2017-11-12 stsp return err;
144 ef0981d5 2018-02-12 stsp err = got_object_id_str(&buf, commit->tree_id);
146 ef0981d5 2018-02-12 stsp return err;
147 ef0981d5 2018-02-12 stsp test_printf("tree: %s\n", buf);
149 82f2fb69 2018-01-26 stsp test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
150 1c852fbe 2017-11-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
151 ef0981d5 2018-02-12 stsp err = got_object_id_str(&buf, pid->id);
153 ef0981d5 2018-02-12 stsp return err;
154 ef0981d5 2018-02-12 stsp test_printf("%s\n", buf);
157 82f2fb69 2018-01-26 stsp test_printf("author: %s\n", commit->author);
158 82f2fb69 2018-01-26 stsp test_printf("committer: %s\n", commit->committer);
159 82f2fb69 2018-01-26 stsp test_printf("log: %s\n", commit->logmsg);
161 59ece79d 2018-02-12 stsp err = got_object_open(&treeobj, repo, commit->tree_id);
162 0ffeb3c2 2017-11-26 stsp if (err != NULL)
163 0ffeb3c2 2017-11-26 stsp return err;
164 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
165 f715ca7f 2017-11-27 stsp print_tree_object(treeobj, "", repo);
166 82f2fb69 2018-01-26 stsp test_printf("\n");
168 0ffeb3c2 2017-11-26 stsp got_object_close(treeobj);
170 bfab4d9a 2017-11-12 stsp err = print_parent_commits(commit, repo);
171 1c852fbe 2017-11-12 stsp got_object_commit_close(commit);
173 bfab4d9a 2017-11-12 stsp return err;
177 bfab4d9a 2017-11-12 stsp repo_read_log(const char *repo_path)
179 11995603 2017-11-05 stsp const struct got_error *err;
180 11995603 2017-11-05 stsp struct got_repository *repo;
181 11995603 2017-11-05 stsp struct got_reference *head_ref;
182 11995603 2017-11-05 stsp struct got_object_id *id;
183 ab9a70b2 2017-11-06 stsp struct got_object *obj;
187 11995603 2017-11-05 stsp err = got_repo_open(&repo, repo_path);
188 11995603 2017-11-05 stsp if (err != NULL || repo == NULL)
190 11995603 2017-11-05 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
191 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
193 11995603 2017-11-05 stsp err = got_ref_resolve(&id, repo, head_ref);
194 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
196 ef0981d5 2018-02-12 stsp err = got_object_id_str(&buf, id);
197 ef0981d5 2018-02-12 stsp if (err != NULL)
199 ef0981d5 2018-02-12 stsp test_printf("HEAD is at %s\n", buf);
201 ab9a70b2 2017-11-06 stsp err = got_object_open(&obj, repo, id);
202 ab9a70b2 2017-11-06 stsp if (err != NULL || obj == NULL)
204 a37d050f 2018-01-26 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
205 a37d050f 2018-01-26 stsp err = print_commit_object(obj, repo);
210 ab9a70b2 2017-11-06 stsp got_object_close(obj);
212 11995603 2017-11-05 stsp got_ref_close(head_ref);
213 11995603 2017-11-05 stsp got_repo_close(repo);
218 044e7393 2018-02-11 stsp repo_read_tree(const char *repo_path)
220 044e7393 2018-02-11 stsp const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
221 044e7393 2018-02-11 stsp const struct got_error *err;
222 044e7393 2018-02-11 stsp struct got_repository *repo;
223 044e7393 2018-02-11 stsp struct got_object *obj;
225 044e7393 2018-02-11 stsp size_t len;
227 044e7393 2018-02-11 stsp err = got_repo_open(&repo, repo_path);
228 044e7393 2018-02-11 stsp if (err != NULL || repo == NULL)
230 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj, repo, tree_sha1);
231 044e7393 2018-02-11 stsp if (err != NULL || obj == NULL)
233 044e7393 2018-02-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
236 044e7393 2018-02-11 stsp print_tree_object(obj, "", repo);
237 044e7393 2018-02-11 stsp test_printf("\n");
239 044e7393 2018-02-11 stsp got_object_close(obj);
240 044e7393 2018-02-11 stsp got_repo_close(repo);
241 044e7393 2018-02-11 stsp return (err == NULL);
244 68482ea3 2017-11-27 stsp repo_read_blob(const char *repo_path)
246 68482ea3 2017-11-27 stsp const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
247 68482ea3 2017-11-27 stsp const struct got_error *err;
248 68482ea3 2017-11-27 stsp struct got_repository *repo;
249 68482ea3 2017-11-27 stsp struct got_object *obj;
250 68482ea3 2017-11-27 stsp struct got_blob_object *blob;
252 68482ea3 2017-11-27 stsp size_t len;
254 68482ea3 2017-11-27 stsp err = got_repo_open(&repo, repo_path);
255 68482ea3 2017-11-27 stsp if (err != NULL || repo == NULL)
257 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj, repo, blob_sha1);
258 68482ea3 2017-11-27 stsp if (err != NULL || obj == NULL)
260 b107e67f 2018-01-19 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
263 68482ea3 2017-11-27 stsp err = got_object_blob_open(&blob, repo, obj, 64);
264 68482ea3 2017-11-27 stsp if (err != NULL)
267 82f2fb69 2018-01-26 stsp test_printf("\n");
269 f934cf2c 2018-02-12 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
270 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob);
273 68482ea3 2017-11-27 stsp for (i = 0; i < len; i++)
274 f934cf2c 2018-02-12 stsp test_printf("%c", buf[i]);
275 68482ea3 2017-11-27 stsp } while (len != 0);
276 82f2fb69 2018-01-26 stsp test_printf("\n");
278 68482ea3 2017-11-27 stsp got_object_blob_close(blob);
279 68482ea3 2017-11-27 stsp got_object_close(obj);
280 7d283eee 2017-11-29 stsp got_repo_close(repo);
281 7d283eee 2017-11-29 stsp return (err == NULL);
285 7d283eee 2017-11-29 stsp repo_diff_blob(const char *repo_path)
287 7d283eee 2017-11-29 stsp const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
288 7d283eee 2017-11-29 stsp const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
289 7d283eee 2017-11-29 stsp const struct got_error *err;
290 7d283eee 2017-11-29 stsp struct got_repository *repo;
291 7d283eee 2017-11-29 stsp struct got_object *obj1;
292 7d283eee 2017-11-29 stsp struct got_object *obj2;
293 7d283eee 2017-11-29 stsp struct got_blob_object *blob1;
294 7d283eee 2017-11-29 stsp struct got_blob_object *blob2;
296 7d283eee 2017-11-29 stsp size_t len;
297 354a7e12 2018-02-11 stsp FILE *outfile;
299 7d283eee 2017-11-29 stsp err = got_repo_open(&repo, repo_path);
300 7d283eee 2017-11-29 stsp if (err != NULL || repo == NULL)
303 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
304 7d283eee 2017-11-29 stsp if (err != NULL || obj1 == NULL)
306 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
308 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
309 7d283eee 2017-11-29 stsp if (err != NULL || obj2 == NULL)
311 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
314 7d283eee 2017-11-29 stsp err = got_object_blob_open(&blob1, repo, obj1, 512);
315 7d283eee 2017-11-29 stsp if (err != NULL)
318 7d283eee 2017-11-29 stsp err = got_object_blob_open(&blob2, repo, obj2, 512);
319 7d283eee 2017-11-29 stsp if (err != NULL)
322 82f2fb69 2018-01-26 stsp test_printf("\n");
323 354a7e12 2018-02-11 stsp if (!verbose) {
324 354a7e12 2018-02-11 stsp outfile = fopen("/dev/null", "w+");
325 354a7e12 2018-02-11 stsp if (outfile == NULL)
328 354a7e12 2018-02-11 stsp outfile = stdout;
329 354a7e12 2018-02-11 stsp got_diff_blob(blob1, blob2, NULL, NULL, outfile);
330 82f2fb69 2018-01-26 stsp test_printf("\n");
332 7d283eee 2017-11-29 stsp got_object_blob_close(blob1);
333 7d283eee 2017-11-29 stsp got_object_blob_close(blob2);
334 98abbc84 2017-11-30 stsp got_object_close(obj1);
335 98abbc84 2017-11-30 stsp got_object_close(obj2);
336 98abbc84 2017-11-30 stsp got_repo_close(repo);
337 98abbc84 2017-11-30 stsp return (err == NULL);
341 98abbc84 2017-11-30 stsp repo_diff_tree(const char *repo_path)
343 4a0235dd 2017-11-30 stsp const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
344 a3e2cbea 2017-12-01 stsp const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
345 98abbc84 2017-11-30 stsp const struct got_error *err;
346 98abbc84 2017-11-30 stsp struct got_repository *repo;
347 98abbc84 2017-11-30 stsp struct got_object *obj1;
348 98abbc84 2017-11-30 stsp struct got_object *obj2;
349 98abbc84 2017-11-30 stsp struct got_tree_object *tree1;
350 98abbc84 2017-11-30 stsp struct got_tree_object *tree2;
352 98abbc84 2017-11-30 stsp size_t len;
353 354a7e12 2018-02-11 stsp FILE *outfile;
355 98abbc84 2017-11-30 stsp err = got_repo_open(&repo, repo_path);
356 98abbc84 2017-11-30 stsp if (err != NULL || repo == NULL)
359 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
360 98abbc84 2017-11-30 stsp if (err != NULL || obj1 == NULL)
362 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
364 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
365 98abbc84 2017-11-30 stsp if (err != NULL || obj2 == NULL)
367 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
370 98abbc84 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, obj1);
371 98abbc84 2017-11-30 stsp if (err != NULL)
374 98abbc84 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, obj2);
375 98abbc84 2017-11-30 stsp if (err != NULL)
378 354a7e12 2018-02-11 stsp if (!verbose) {
379 354a7e12 2018-02-11 stsp outfile = fopen("/dev/null", "w+");
380 354a7e12 2018-02-11 stsp if (outfile == NULL)
383 354a7e12 2018-02-11 stsp outfile = stdout;
384 82f2fb69 2018-01-26 stsp test_printf("\n");
385 354a7e12 2018-02-11 stsp got_diff_tree(tree1, tree2, repo, outfile);
386 82f2fb69 2018-01-26 stsp test_printf("\n");
388 98abbc84 2017-11-30 stsp got_object_tree_close(tree1);
389 98abbc84 2017-11-30 stsp got_object_tree_close(tree2);
390 7d283eee 2017-11-29 stsp got_object_close(obj1);
391 7d283eee 2017-11-29 stsp got_object_close(obj2);
392 68482ea3 2017-11-27 stsp got_repo_close(repo);
393 68482ea3 2017-11-27 stsp return (err == NULL);
396 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
397 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
398 b08fe7be 2018-01-26 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
399 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
403 82f2fb69 2018-01-26 stsp usage(void)
405 82f2fb69 2018-01-26 stsp fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
409 82f2fb69 2018-01-26 stsp main(int argc, char *argv[])
411 b08fe7be 2018-01-26 stsp int test_ok = 0, failure = 0;
412 4027f31a 2017-11-04 stsp const char *repo_path;
414 82f2fb69 2018-01-26 stsp int vflag = 0;
416 82f2fb69 2018-01-26 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
417 82f2fb69 2018-01-26 stsp switch (ch) {
419 82f2fb69 2018-01-26 stsp verbose = 1;
426 82f2fb69 2018-01-26 stsp argc -= optind;
427 82f2fb69 2018-01-26 stsp argv += optind;
429 82f2fb69 2018-01-26 stsp if (argc == 0)
430 4027f31a 2017-11-04 stsp repo_path = GOT_REPO_PATH;
431 82f2fb69 2018-01-26 stsp else if (argc == 1)
432 ff3eb0f2 2018-03-09 stsp repo_path = argv[0];
438 044e7393 2018-02-11 stsp RUN_TEST(repo_read_tree(repo_path), "read_tree");
439 bfab4d9a 2017-11-12 stsp RUN_TEST(repo_read_log(repo_path), "read_log");
440 68482ea3 2017-11-27 stsp RUN_TEST(repo_read_blob(repo_path), "read_blob");
441 7d283eee 2017-11-29 stsp RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
442 98abbc84 2017-11-30 stsp RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
444 4027f31a 2017-11-04 stsp return failure ? 1 : 0;