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 4027f31a 2017-11-04 stsp #include <sha1.h>
25 68482ea3 2017-11-27 stsp #include <zlib.h>
27 4027f31a 2017-11-04 stsp #include "got_error.h"
28 11995603 2017-11-05 stsp #include "got_object.h"
29 4027f31a 2017-11-04 stsp #include "got_refs.h"
30 4027f31a 2017-11-04 stsp #include "got_repository.h"
31 68482ea3 2017-11-27 stsp #include "got_sha1.h"
32 f78b0693 2017-11-29 stsp #include "got_diff.h"
33 82f2fb69 2018-01-26 stsp #include "unistd.h"
35 4027f31a 2017-11-04 stsp #define GOT_REPO_PATH "../../../"
37 14545512 2018-01-26 stsp static int verbose;
40 82f2fb69 2018-01-26 stsp test_printf(char *fmt, ...)
44 82f2fb69 2018-01-26 stsp if (!verbose)
47 82f2fb69 2018-01-26 stsp va_start(ap, fmt);
48 82f2fb69 2018-01-26 stsp vprintf(fmt, ap);
52 bfab4d9a 2017-11-12 stsp static const struct got_error *
53 bfab4d9a 2017-11-12 stsp print_commit_object(struct got_object *, struct got_repository *);
55 1c852fbe 2017-11-12 stsp static const struct got_error *
56 bfab4d9a 2017-11-12 stsp print_parent_commits(struct got_commit_object *commit,
57 bfab4d9a 2017-11-12 stsp struct got_repository *repo)
59 bfab4d9a 2017-11-12 stsp struct got_parent_id *pid;
60 a37d050f 2018-01-26 stsp const struct got_error *err = NULL;
61 bfab4d9a 2017-11-12 stsp struct got_object *obj;
63 bfab4d9a 2017-11-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
64 bfab4d9a 2017-11-12 stsp err = got_object_open(&obj, repo, &pid->id);
65 bfab4d9a 2017-11-12 stsp if (err != NULL)
67 b107e67f 2018-01-19 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
68 bfab4d9a 2017-11-12 stsp return got_error(GOT_ERR_OBJ_TYPE);
69 a37d050f 2018-01-26 stsp err = print_commit_object(obj, repo);
70 bfab4d9a 2017-11-12 stsp got_object_close(obj);
76 bfab4d9a 2017-11-12 stsp static const struct got_error *
77 f715ca7f 2017-11-27 stsp print_tree_object(struct got_object *obj, char *parent,
78 f715ca7f 2017-11-27 stsp struct got_repository *repo)
80 0ffeb3c2 2017-11-26 stsp struct got_tree_object *tree;
81 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
82 0ffeb3c2 2017-11-26 stsp const struct got_error *err;
83 f715ca7f 2017-11-27 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
85 0ffeb3c2 2017-11-26 stsp err = got_object_tree_open(&tree, repo, obj);
86 0ffeb3c2 2017-11-26 stsp if (err != NULL)
89 f715ca7f 2017-11-27 stsp SIMPLEQ_FOREACH(te, &tree->entries, entry) {
90 f715ca7f 2017-11-27 stsp struct got_object *treeobj;
91 f715ca7f 2017-11-27 stsp char *next_parent;
93 f715ca7f 2017-11-27 stsp if (!S_ISDIR(te->mode)) {
94 82f2fb69 2018-01-26 stsp test_printf("%s %s/%s\n",
95 f715ca7f 2017-11-27 stsp got_object_id_str(&te->id, hex, sizeof(hex)),
96 f715ca7f 2017-11-27 stsp parent, te->name);
99 82f2fb69 2018-01-26 stsp test_printf("%s %s/%s\n",
100 f715ca7f 2017-11-27 stsp got_object_id_str(&te->id, hex, sizeof(hex)),
101 f715ca7f 2017-11-27 stsp parent, te->name);
103 f715ca7f 2017-11-27 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;
138 1c852fbe 2017-11-12 stsp char buf[SHA1_DIGEST_STRING_LENGTH];
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);
143 1c852fbe 2017-11-12 stsp if (err != NULL)
144 1c852fbe 2017-11-12 stsp return err;
146 82f2fb69 2018-01-26 stsp test_printf("tree: %s\n",
147 1c852fbe 2017-11-12 stsp got_object_id_str(&commit->tree_id, buf, sizeof(buf)));
148 82f2fb69 2018-01-26 stsp test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
149 1c852fbe 2017-11-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
150 82f2fb69 2018-01-26 stsp test_printf("%s\n",
151 1c852fbe 2017-11-12 stsp got_object_id_str(&pid->id, buf, sizeof(buf)));
153 82f2fb69 2018-01-26 stsp test_printf("author: %s\n", commit->author);
154 82f2fb69 2018-01-26 stsp test_printf("committer: %s\n", commit->committer);
155 82f2fb69 2018-01-26 stsp test_printf("log: %s\n", commit->logmsg);
157 0ffeb3c2 2017-11-26 stsp err = got_object_open(&treeobj, repo, &commit->tree_id);
158 0ffeb3c2 2017-11-26 stsp if (err != NULL)
159 0ffeb3c2 2017-11-26 stsp return err;
160 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
161 f715ca7f 2017-11-27 stsp print_tree_object(treeobj, "", repo);
162 82f2fb69 2018-01-26 stsp test_printf("\n");
164 0ffeb3c2 2017-11-26 stsp got_object_close(treeobj);
166 bfab4d9a 2017-11-12 stsp err = print_parent_commits(commit, repo);
167 1c852fbe 2017-11-12 stsp got_object_commit_close(commit);
169 bfab4d9a 2017-11-12 stsp return err;
173 bfab4d9a 2017-11-12 stsp repo_read_log(const char *repo_path)
175 11995603 2017-11-05 stsp const struct got_error *err;
176 11995603 2017-11-05 stsp struct got_repository *repo;
177 11995603 2017-11-05 stsp struct got_reference *head_ref;
178 11995603 2017-11-05 stsp struct got_object_id *id;
179 ab9a70b2 2017-11-06 stsp struct got_object *obj;
180 d71d75ad 2017-11-05 stsp char buf[SHA1_DIGEST_STRING_LENGTH];
183 11995603 2017-11-05 stsp err = got_repo_open(&repo, repo_path);
184 11995603 2017-11-05 stsp if (err != NULL || repo == NULL)
186 11995603 2017-11-05 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
187 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
189 11995603 2017-11-05 stsp err = got_ref_resolve(&id, repo, head_ref);
190 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
192 82f2fb69 2018-01-26 stsp test_printf("HEAD is at %s\n", got_object_id_str(id, buf, sizeof(buf)));
193 ab9a70b2 2017-11-06 stsp err = got_object_open(&obj, repo, id);
194 ab9a70b2 2017-11-06 stsp if (err != NULL || obj == NULL)
196 a37d050f 2018-01-26 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
197 a37d050f 2018-01-26 stsp err = print_commit_object(obj, repo);
202 ab9a70b2 2017-11-06 stsp got_object_close(obj);
204 11995603 2017-11-05 stsp got_ref_close(head_ref);
205 11995603 2017-11-05 stsp got_repo_close(repo);
210 68482ea3 2017-11-27 stsp repo_read_blob(const char *repo_path)
212 68482ea3 2017-11-27 stsp const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
213 68482ea3 2017-11-27 stsp const struct got_error *err;
214 68482ea3 2017-11-27 stsp struct got_repository *repo;
215 68482ea3 2017-11-27 stsp struct got_object_id id;
216 68482ea3 2017-11-27 stsp struct got_object *obj;
217 68482ea3 2017-11-27 stsp struct got_blob_object *blob;
218 68482ea3 2017-11-27 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
220 68482ea3 2017-11-27 stsp size_t len;
222 68482ea3 2017-11-27 stsp if (!got_parse_sha1_digest(id.sha1, blob_sha1))
225 68482ea3 2017-11-27 stsp err = got_repo_open(&repo, repo_path);
226 68482ea3 2017-11-27 stsp if (err != NULL || repo == NULL)
228 68482ea3 2017-11-27 stsp err = got_object_open(&obj, repo, &id);
229 68482ea3 2017-11-27 stsp if (err != NULL || obj == NULL)
231 b107e67f 2018-01-19 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
234 68482ea3 2017-11-27 stsp err = got_object_blob_open(&blob, repo, obj, 64);
235 68482ea3 2017-11-27 stsp if (err != NULL)
238 82f2fb69 2018-01-26 stsp test_printf("\n");
240 68482ea3 2017-11-27 stsp err = got_object_blob_read_block(blob, &len);
243 68482ea3 2017-11-27 stsp for (i = 0; i < len; i++)
244 82f2fb69 2018-01-26 stsp test_printf("%c", blob->zb.outbuf[i]);
245 68482ea3 2017-11-27 stsp } while (len != 0);
246 82f2fb69 2018-01-26 stsp test_printf("\n");
248 68482ea3 2017-11-27 stsp got_object_blob_close(blob);
249 68482ea3 2017-11-27 stsp got_object_close(obj);
250 7d283eee 2017-11-29 stsp got_repo_close(repo);
251 7d283eee 2017-11-29 stsp return (err == NULL);
255 7d283eee 2017-11-29 stsp repo_diff_blob(const char *repo_path)
257 7d283eee 2017-11-29 stsp const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
258 7d283eee 2017-11-29 stsp const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
259 7d283eee 2017-11-29 stsp const struct got_error *err;
260 7d283eee 2017-11-29 stsp struct got_repository *repo;
261 7d283eee 2017-11-29 stsp struct got_object_id id1;
262 7d283eee 2017-11-29 stsp struct got_object_id id2;
263 7d283eee 2017-11-29 stsp struct got_object *obj1;
264 7d283eee 2017-11-29 stsp struct got_object *obj2;
265 7d283eee 2017-11-29 stsp struct got_blob_object *blob1;
266 7d283eee 2017-11-29 stsp struct got_blob_object *blob2;
267 7d283eee 2017-11-29 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
269 7d283eee 2017-11-29 stsp size_t len;
271 7d283eee 2017-11-29 stsp if (!got_parse_sha1_digest(id1.sha1, blob1_sha1))
273 7d283eee 2017-11-29 stsp if (!got_parse_sha1_digest(id2.sha1, blob2_sha1))
276 7d283eee 2017-11-29 stsp err = got_repo_open(&repo, repo_path);
277 7d283eee 2017-11-29 stsp if (err != NULL || repo == NULL)
280 7d283eee 2017-11-29 stsp err = got_object_open(&obj1, repo, &id1);
281 7d283eee 2017-11-29 stsp if (err != NULL || obj1 == NULL)
283 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
285 7d283eee 2017-11-29 stsp err = got_object_open(&obj2, repo, &id2);
286 7d283eee 2017-11-29 stsp if (err != NULL || obj2 == NULL)
288 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
291 7d283eee 2017-11-29 stsp err = got_object_blob_open(&blob1, repo, obj1, 512);
292 7d283eee 2017-11-29 stsp if (err != NULL)
295 7d283eee 2017-11-29 stsp err = got_object_blob_open(&blob2, repo, obj2, 512);
296 7d283eee 2017-11-29 stsp if (err != NULL)
299 82f2fb69 2018-01-26 stsp test_printf("\n");
300 62136d3a 2017-11-29 stsp got_diff_blob(blob1, blob2, NULL, NULL, stdout);
301 82f2fb69 2018-01-26 stsp test_printf("\n");
303 7d283eee 2017-11-29 stsp got_object_blob_close(blob1);
304 7d283eee 2017-11-29 stsp got_object_blob_close(blob2);
305 98abbc84 2017-11-30 stsp got_object_close(obj1);
306 98abbc84 2017-11-30 stsp got_object_close(obj2);
307 98abbc84 2017-11-30 stsp got_repo_close(repo);
308 98abbc84 2017-11-30 stsp return (err == NULL);
312 98abbc84 2017-11-30 stsp repo_diff_tree(const char *repo_path)
314 4a0235dd 2017-11-30 stsp const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
315 a3e2cbea 2017-12-01 stsp const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
316 98abbc84 2017-11-30 stsp const struct got_error *err;
317 98abbc84 2017-11-30 stsp struct got_repository *repo;
318 98abbc84 2017-11-30 stsp struct got_object_id id1;
319 98abbc84 2017-11-30 stsp struct got_object_id id2;
320 98abbc84 2017-11-30 stsp struct got_object *obj1;
321 98abbc84 2017-11-30 stsp struct got_object *obj2;
322 98abbc84 2017-11-30 stsp struct got_tree_object *tree1;
323 98abbc84 2017-11-30 stsp struct got_tree_object *tree2;
324 98abbc84 2017-11-30 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
326 98abbc84 2017-11-30 stsp size_t len;
328 4a0235dd 2017-11-30 stsp if (!got_parse_sha1_digest(id1.sha1, tree1_sha1))
330 4a0235dd 2017-11-30 stsp if (!got_parse_sha1_digest(id2.sha1, tree2_sha1))
333 98abbc84 2017-11-30 stsp err = got_repo_open(&repo, repo_path);
334 98abbc84 2017-11-30 stsp if (err != NULL || repo == NULL)
337 98abbc84 2017-11-30 stsp err = got_object_open(&obj1, repo, &id1);
338 98abbc84 2017-11-30 stsp if (err != NULL || obj1 == NULL)
340 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
342 98abbc84 2017-11-30 stsp err = got_object_open(&obj2, repo, &id2);
343 98abbc84 2017-11-30 stsp if (err != NULL || obj2 == NULL)
345 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
348 98abbc84 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, obj1);
349 98abbc84 2017-11-30 stsp if (err != NULL)
352 98abbc84 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, obj2);
353 98abbc84 2017-11-30 stsp if (err != NULL)
356 82f2fb69 2018-01-26 stsp test_printf("\n");
357 98abbc84 2017-11-30 stsp got_diff_tree(tree1, tree2, repo);
358 82f2fb69 2018-01-26 stsp test_printf("\n");
360 98abbc84 2017-11-30 stsp got_object_tree_close(tree1);
361 98abbc84 2017-11-30 stsp got_object_tree_close(tree2);
362 7d283eee 2017-11-29 stsp got_object_close(obj1);
363 7d283eee 2017-11-29 stsp got_object_close(obj2);
364 68482ea3 2017-11-27 stsp got_repo_close(repo);
365 68482ea3 2017-11-27 stsp return (err == NULL);
368 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
369 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
370 b08fe7be 2018-01-26 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
371 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
375 82f2fb69 2018-01-26 stsp usage(void)
377 82f2fb69 2018-01-26 stsp fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
381 82f2fb69 2018-01-26 stsp main(int argc, char *argv[])
383 b08fe7be 2018-01-26 stsp int test_ok = 0, failure = 0;
384 4027f31a 2017-11-04 stsp const char *repo_path;
386 82f2fb69 2018-01-26 stsp int vflag = 0;
388 82f2fb69 2018-01-26 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
389 82f2fb69 2018-01-26 stsp switch (ch) {
391 82f2fb69 2018-01-26 stsp verbose = 1;
398 82f2fb69 2018-01-26 stsp argc -= optind;
399 82f2fb69 2018-01-26 stsp argv += optind;
401 82f2fb69 2018-01-26 stsp if (argc == 0)
402 4027f31a 2017-11-04 stsp repo_path = GOT_REPO_PATH;
403 82f2fb69 2018-01-26 stsp else if (argc == 1)
404 4027f31a 2017-11-04 stsp repo_path = argv[1];
410 bfab4d9a 2017-11-12 stsp RUN_TEST(repo_read_log(repo_path), "read_log");
411 68482ea3 2017-11-27 stsp RUN_TEST(repo_read_blob(repo_path), "read_blob");
412 7d283eee 2017-11-29 stsp RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
413 98abbc84 2017-11-30 stsp RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
415 4027f31a 2017-11-04 stsp return failure ? 1 : 0;