Blob


1 /*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <util.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <err.h>
27 #include <unistd.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_reference.h"
32 #include "got_repository.h"
33 #include "got_diff.h"
34 #include "got_opentemp.h"
36 #include "got_lib_path.h"
38 #ifndef nitems
39 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 #endif
42 #define GOT_REPO_PATH "../../../"
44 static int verbose;
46 void
47 test_printf(char *fmt, ...)
48 {
49 va_list ap;
51 if (!verbose)
52 return;
54 va_start(ap, fmt);
55 vprintf(fmt, ap);
56 va_end(ap);
57 }
59 static const struct got_error *
60 print_commit_object(struct got_object *, struct got_repository *);
62 static const struct got_error *
63 print_parent_commits(struct got_commit_object *commit,
64 struct got_repository *repo)
65 {
66 struct got_object_qid *qid;
67 const struct got_error *err = NULL;
68 struct got_object *obj;
70 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
71 err = got_object_open(&obj, repo, qid->id);
72 if (err != NULL)
73 return err;
74 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
75 err = got_error(GOT_ERR_OBJ_TYPE);
76 else
77 err = print_commit_object(obj, repo);
78 got_object_close(obj);
79 if (err)
80 break;
81 }
83 return err;
84 }
86 static const struct got_error *
87 print_tree_object(struct got_object *obj, char *parent,
88 struct got_repository *repo)
89 {
90 struct got_tree_object *tree;
91 struct got_tree_entry *te;
92 const struct got_error *err;
94 err = got_object_tree_open(&tree, repo, obj);
95 if (err != NULL)
96 return err;
98 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
99 struct got_object *treeobj;
100 char *next_parent;
101 char *hex;
103 err = got_object_id_str(&hex, te->id);
104 if (err)
105 break;
107 if (!S_ISDIR(te->mode)) {
108 test_printf("%s %s/%s\n", hex, parent, te->name);
109 free(hex);
110 continue;
112 test_printf("%s %s/%s\n", hex, parent, te->name);
113 free(hex);
115 err = got_object_open(&treeobj, repo, te->id);
116 if (err != NULL)
117 break;
119 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
120 err = got_error(GOT_ERR_OBJ_TYPE);
121 got_object_close(treeobj);
122 break;
125 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
126 err = got_error_from_errno();
127 got_object_close(treeobj);
128 break;
131 err = print_tree_object(treeobj, next_parent, repo);
132 free(next_parent);
133 if (err) {
134 got_object_close(treeobj);
135 break;
138 got_object_close(treeobj);
141 got_object_tree_close(tree);
142 return err;
145 static const struct got_error *
146 print_commit_object(struct got_object *obj, struct got_repository *repo)
148 struct got_commit_object *commit;
149 struct got_object_qid *qid;
150 char *buf;
151 const struct got_error *err;
152 struct got_object* treeobj;
154 err = got_object_commit_open(&commit, repo, obj);
155 if (err)
156 return err;
158 err = got_object_id_str(&buf, commit->tree_id);
159 if (err)
160 return err;
161 test_printf("tree: %s\n", buf);
162 free(buf);
163 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
164 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
165 err = got_object_id_str(&buf, qid->id);
166 if (err)
167 return err;
168 test_printf("%s\n", buf);
169 free(buf);
171 test_printf("author: %s\n", commit->author);
172 test_printf("committer: %s\n", commit->committer);
173 test_printf("log: %s\n", commit->logmsg);
175 err = got_object_open(&treeobj, repo, commit->tree_id);
176 if (err != NULL)
177 return err;
178 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
179 print_tree_object(treeobj, "", repo);
180 test_printf("\n");
182 got_object_close(treeobj);
184 err = print_parent_commits(commit, repo);
185 got_object_commit_close(commit);
187 return err;
190 static int
191 repo_read_log(const char *repo_path)
193 const struct got_error *err;
194 struct got_repository *repo;
195 struct got_reference *head_ref;
196 struct got_object_id *id;
197 struct got_object *obj;
198 char *buf;
200 err = got_repo_open(&repo, repo_path);
201 if (err != NULL || repo == NULL)
202 return 0;
203 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
204 if (err != NULL || head_ref == NULL)
205 return 0;
206 err = got_ref_resolve(&id, repo, head_ref);
207 if (err != NULL || head_ref == NULL)
208 return 0;
209 err = got_object_id_str(&buf, id);
210 if (err != NULL)
211 return 0;
212 test_printf("HEAD is at %s\n", buf);
213 free(buf);
214 err = got_object_open(&obj, repo, id);
215 if (err != NULL || obj == NULL)
216 return 0;
217 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
218 err = print_commit_object(obj, repo);
219 if (err)
220 return 0;
221 } else
222 return 0;
223 got_object_close(obj);
224 free(id);
225 got_ref_close(head_ref);
226 got_repo_close(repo);
227 return 1;
230 static int
231 repo_read_tree(const char *repo_path)
233 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
234 const struct got_error *err;
235 struct got_repository *repo;
236 struct got_object *obj;
238 err = got_repo_open(&repo, repo_path);
239 if (err != NULL || repo == NULL)
240 return 0;
241 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
242 if (err != NULL || obj == NULL)
243 return 0;
244 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
245 return 0;
247 print_tree_object(obj, "", repo);
248 test_printf("\n");
250 got_object_close(obj);
251 got_repo_close(repo);
252 return (err == NULL);
255 static int
256 repo_read_blob(const char *repo_path)
258 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
259 const struct got_error *err;
260 struct got_repository *repo;
261 struct got_object *obj;
262 struct got_blob_object *blob;
263 int i;
264 size_t len;
266 err = got_repo_open(&repo, repo_path);
267 if (err != NULL || repo == NULL)
268 return 0;
269 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
270 if (err != NULL || obj == NULL)
271 return 0;
272 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
273 return 0;
275 err = got_object_blob_open(&blob, repo, obj, 64);
276 if (err != NULL)
277 return 0;
279 test_printf("\n");
280 do {
281 const uint8_t *buf = got_object_blob_get_read_buf(blob);
282 err = got_object_blob_read_block(&len, blob);
283 if (err)
284 break;
285 for (i = 0; i < len; i++)
286 test_printf("%c", buf[i]);
287 } while (len != 0);
288 test_printf("\n");
290 got_object_blob_close(blob);
291 got_object_close(obj);
292 got_repo_close(repo);
293 return (err == NULL);
296 static int
297 repo_diff_blob(const char *repo_path)
299 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
300 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
301 const struct got_error *err;
302 struct got_repository *repo;
303 struct got_object *obj1;
304 struct got_object *obj2;
305 struct got_blob_object *blob1;
306 struct got_blob_object *blob2;
307 FILE *outfile;
308 int i;
309 char *line;
310 size_t len;
311 const char delim[3] = {'\0', '\0', '\0'};
312 const char *expected_output[] = {
313 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
314 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
315 "@@ -1,10 +1,10 @@",
316 " .PATH:${.CURDIR}/../../lib",
317 " ",
318 " PROG = repository_test",
319 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
320 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
321 " ",
322 " CPPFLAGS = -I${.CURDIR}/../../include",
323 "-LDADD = -lutil",
324 "+LDADD = -lutil -lz",
325 " ",
326 " NOMAN = yes"
327 };
329 err = got_repo_open(&repo, repo_path);
330 if (err != NULL || repo == NULL)
331 return 0;
333 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
334 if (err != NULL || obj1 == NULL)
335 return 0;
336 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
337 return 0;
338 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
339 if (err != NULL || obj2 == NULL)
340 return 0;
341 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
342 return 0;
344 err = got_object_blob_open(&blob1, repo, obj1, 512);
345 if (err != NULL)
346 return 0;
348 err = got_object_blob_open(&blob2, repo, obj2, 512);
349 if (err != NULL)
350 return 0;
352 test_printf("\n");
353 outfile = got_opentemp();
354 if (outfile == NULL)
355 return 0;
356 got_diff_blob(blob1, blob2, NULL, NULL, outfile);
357 rewind(outfile);
358 i = 0;
359 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
360 test_printf(line);
361 test_printf("\n");
362 if (i < nitems(expected_output) &&
363 strcmp(line, expected_output[i]) != 0) {
364 test_printf("diff output mismatch; expected: '%s'\n",
365 expected_output[i]);
367 i++;
369 fclose(outfile);
370 test_printf("\n");
371 if (i != nitems(expected_output) + 1) {
372 test_printf("number of lines expected: %d; actual: %d\n",
373 nitems(expected_output), i - 1);
374 return 0;
377 got_object_blob_close(blob1);
378 got_object_blob_close(blob2);
379 got_object_close(obj1);
380 got_object_close(obj2);
381 got_repo_close(repo);
382 return (err == NULL);
385 static int
386 repo_diff_tree(const char *repo_path)
388 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
389 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
390 const struct got_error *err;
391 struct got_repository *repo;
392 struct got_object *obj1;
393 struct got_object *obj2;
394 struct got_tree_object *tree1;
395 struct got_tree_object *tree2;
396 FILE *outfile;
398 err = got_repo_open(&repo, repo_path);
399 if (err != NULL || repo == NULL)
400 return 0;
402 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
403 if (err != NULL || obj1 == NULL)
404 return 0;
405 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
406 return 0;
407 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
408 if (err != NULL || obj2 == NULL)
409 return 0;
410 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
411 return 0;
413 err = got_object_tree_open(&tree1, repo, obj1);
414 if (err != NULL)
415 return 0;
417 err = got_object_tree_open(&tree2, repo, obj2);
418 if (err != NULL)
419 return 0;
421 if (!verbose) {
422 outfile = fopen("/dev/null", "w+");
423 if (outfile == NULL)
424 return 0;
425 } else
426 outfile = stdout;
427 test_printf("\n");
428 got_diff_tree(tree1, tree2, repo, outfile);
429 test_printf("\n");
431 got_object_tree_close(tree1);
432 got_object_tree_close(tree2);
433 got_object_close(obj1);
434 got_object_close(obj2);
435 got_repo_close(repo);
436 return (err == NULL);
439 #define RUN_TEST(expr, name) \
440 { test_ok = (expr); \
441 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
442 failure = (failure || !test_ok); }
445 void
446 usage(void)
448 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
451 int
452 main(int argc, char *argv[])
454 int test_ok = 0, failure = 0;
455 const char *repo_path;
456 int ch;
458 if (pledge("stdio rpath wpath cpath proc", NULL) == -1)
459 err(1, "pledge");
461 while ((ch = getopt(argc, argv, "v")) != -1) {
462 switch (ch) {
463 case 'v':
464 verbose = 1;
465 break;
466 default:
467 usage();
468 return 1;
471 argc -= optind;
472 argv += optind;
474 if (argc == 0)
475 repo_path = GOT_REPO_PATH;
476 else if (argc == 1)
477 repo_path = argv[0];
478 else {
479 usage();
480 return 1;
483 RUN_TEST(repo_read_tree(repo_path), "read_tree");
484 RUN_TEST(repo_read_log(repo_path), "read_log");
485 RUN_TEST(repo_read_blob(repo_path), "read_blob");
486 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
487 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
489 return failure ? 1 : 0;