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 const struct got_tree_entries *entries;
92 struct got_tree_entry *te;
93 const struct got_error *err;
95 err = got_object_tree_open(&tree, repo, obj);
96 if (err != NULL)
97 return err;
99 entries = got_object_tree_get_entries(tree);
100 SIMPLEQ_FOREACH(te, &entries->head, entry) {
101 struct got_object *treeobj;
102 char *next_parent;
103 char *hex;
105 err = got_object_id_str(&hex, te->id);
106 if (err)
107 break;
109 if (!S_ISDIR(te->mode)) {
110 test_printf("%s %s/%s\n", hex, parent, te->name);
111 free(hex);
112 continue;
114 test_printf("%s %s/%s\n", hex, parent, te->name);
115 free(hex);
117 err = got_object_open(&treeobj, repo, te->id);
118 if (err != NULL)
119 break;
121 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
122 err = got_error(GOT_ERR_OBJ_TYPE);
123 got_object_close(treeobj);
124 break;
127 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
128 err = got_error_from_errno();
129 got_object_close(treeobj);
130 break;
133 err = print_tree_object(treeobj, next_parent, repo);
134 free(next_parent);
135 if (err) {
136 got_object_close(treeobj);
137 break;
140 got_object_close(treeobj);
143 got_object_tree_close(tree);
144 return err;
147 static const struct got_error *
148 print_commit_object(struct got_object *obj, struct got_repository *repo)
150 struct got_commit_object *commit;
151 struct got_object_qid *qid;
152 char *buf;
153 const struct got_error *err;
154 struct got_object* treeobj;
156 err = got_object_commit_open(&commit, repo, obj);
157 if (err)
158 return err;
160 err = got_object_id_str(&buf, commit->tree_id);
161 if (err)
162 return err;
163 test_printf("tree: %s\n", buf);
164 free(buf);
165 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
166 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
167 err = got_object_id_str(&buf, qid->id);
168 if (err)
169 return err;
170 test_printf("%s\n", buf);
171 free(buf);
173 test_printf("author: %s\n", commit->author);
174 test_printf("committer: %s\n", commit->committer);
175 test_printf("log: %s\n", commit->logmsg);
177 err = got_object_open(&treeobj, repo, commit->tree_id);
178 if (err != NULL)
179 return err;
180 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
181 print_tree_object(treeobj, "", repo);
182 test_printf("\n");
184 got_object_close(treeobj);
186 err = print_parent_commits(commit, repo);
187 got_object_commit_close(commit);
189 return err;
192 static int
193 repo_read_log(const char *repo_path)
195 const struct got_error *err;
196 struct got_repository *repo;
197 struct got_reference *head_ref;
198 struct got_object_id *id;
199 struct got_object *obj;
200 char *buf;
202 err = got_repo_open(&repo, repo_path);
203 if (err != NULL || repo == NULL)
204 return 0;
205 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
206 if (err != NULL || head_ref == NULL)
207 return 0;
208 err = got_ref_resolve(&id, repo, head_ref);
209 if (err != NULL || head_ref == NULL)
210 return 0;
211 err = got_object_id_str(&buf, id);
212 if (err != NULL)
213 return 0;
214 test_printf("HEAD is at %s\n", buf);
215 free(buf);
216 err = got_object_open(&obj, repo, id);
217 if (err != NULL || obj == NULL)
218 return 0;
219 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
220 err = print_commit_object(obj, repo);
221 if (err)
222 return 0;
223 } else
224 return 0;
225 got_object_close(obj);
226 free(id);
227 got_ref_close(head_ref);
228 got_repo_close(repo);
229 return 1;
232 static int
233 repo_read_tree(const char *repo_path)
235 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
236 const struct got_error *err;
237 struct got_repository *repo;
238 struct got_object *obj;
240 err = got_repo_open(&repo, repo_path);
241 if (err != NULL || repo == NULL)
242 return 0;
243 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
244 if (err != NULL || obj == NULL)
245 return 0;
246 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
247 return 0;
249 print_tree_object(obj, "", repo);
250 test_printf("\n");
252 got_object_close(obj);
253 got_repo_close(repo);
254 return (err == NULL);
257 static int
258 repo_read_blob(const char *repo_path)
260 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
261 const struct got_error *err;
262 struct got_repository *repo;
263 struct got_object *obj;
264 struct got_blob_object *blob;
265 int i;
266 size_t len;
268 err = got_repo_open(&repo, repo_path);
269 if (err != NULL || repo == NULL)
270 return 0;
271 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
272 if (err != NULL || obj == NULL)
273 return 0;
274 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
275 return 0;
277 err = got_object_blob_open(&blob, repo, obj, 64);
278 if (err != NULL)
279 return 0;
281 test_printf("\n");
282 do {
283 const uint8_t *buf = got_object_blob_get_read_buf(blob);
284 err = got_object_blob_read_block(&len, blob);
285 if (err)
286 break;
287 for (i = 0; i < len; i++)
288 test_printf("%c", buf[i]);
289 } while (len != 0);
290 test_printf("\n");
292 got_object_blob_close(blob);
293 got_object_close(obj);
294 got_repo_close(repo);
295 return (err == NULL);
298 static int
299 repo_diff_blob(const char *repo_path)
301 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
302 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
303 const struct got_error *err;
304 struct got_repository *repo;
305 struct got_object *obj1;
306 struct got_object *obj2;
307 struct got_blob_object *blob1;
308 struct got_blob_object *blob2;
309 FILE *outfile;
310 int i;
311 char *line;
312 size_t len;
313 const char delim[3] = {'\0', '\0', '\0'};
314 const char *expected_output[] = {
315 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
316 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
317 "@@ -1,10 +1,10 @@",
318 " .PATH:${.CURDIR}/../../lib",
319 " ",
320 " PROG = repository_test",
321 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
322 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
323 " ",
324 " CPPFLAGS = -I${.CURDIR}/../../include",
325 "-LDADD = -lutil",
326 "+LDADD = -lutil -lz",
327 " ",
328 " NOMAN = yes"
329 };
331 err = got_repo_open(&repo, repo_path);
332 if (err != NULL || repo == NULL)
333 return 0;
335 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
336 if (err != NULL || obj1 == NULL)
337 return 0;
338 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
339 return 0;
340 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
341 if (err != NULL || obj2 == NULL)
342 return 0;
343 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
344 return 0;
346 err = got_object_blob_open(&blob1, repo, obj1, 512);
347 if (err != NULL)
348 return 0;
350 err = got_object_blob_open(&blob2, repo, obj2, 512);
351 if (err != NULL)
352 return 0;
354 test_printf("\n");
355 outfile = got_opentemp();
356 if (outfile == NULL)
357 return 0;
358 got_diff_blob(blob1, blob2, NULL, NULL, outfile);
359 rewind(outfile);
360 i = 0;
361 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
362 test_printf(line);
363 test_printf("\n");
364 if (i < nitems(expected_output) &&
365 strcmp(line, expected_output[i]) != 0) {
366 test_printf("diff output mismatch; expected: '%s'\n",
367 expected_output[i]);
369 i++;
371 fclose(outfile);
372 test_printf("\n");
373 if (i != nitems(expected_output) + 1) {
374 test_printf("number of lines expected: %d; actual: %d\n",
375 nitems(expected_output), i - 1);
376 return 0;
379 got_object_blob_close(blob1);
380 got_object_blob_close(blob2);
381 got_object_close(obj1);
382 got_object_close(obj2);
383 got_repo_close(repo);
384 return (err == NULL);
387 static int
388 repo_diff_tree(const char *repo_path)
390 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
391 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
392 const struct got_error *err;
393 struct got_repository *repo;
394 struct got_object *obj1;
395 struct got_object *obj2;
396 struct got_tree_object *tree1;
397 struct got_tree_object *tree2;
398 FILE *outfile;
400 err = got_repo_open(&repo, repo_path);
401 if (err != NULL || repo == NULL)
402 return 0;
404 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
405 if (err != NULL || obj1 == NULL)
406 return 0;
407 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
408 return 0;
409 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
410 if (err != NULL || obj2 == NULL)
411 return 0;
412 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
413 return 0;
415 err = got_object_tree_open(&tree1, repo, obj1);
416 if (err != NULL)
417 return 0;
419 err = got_object_tree_open(&tree2, repo, obj2);
420 if (err != NULL)
421 return 0;
423 if (!verbose) {
424 outfile = fopen("/dev/null", "w+");
425 if (outfile == NULL)
426 return 0;
427 } else
428 outfile = stdout;
429 test_printf("\n");
430 got_diff_tree(tree1, tree2, repo, outfile);
431 test_printf("\n");
433 got_object_tree_close(tree1);
434 got_object_tree_close(tree2);
435 got_object_close(obj1);
436 got_object_close(obj2);
437 got_repo_close(repo);
438 return (err == NULL);
441 #define RUN_TEST(expr, name) \
442 { test_ok = (expr); \
443 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
444 failure = (failure || !test_ok); }
447 void
448 usage(void)
450 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
453 int
454 main(int argc, char *argv[])
456 int test_ok = 0, failure = 0;
457 const char *repo_path;
458 int ch;
460 if (pledge("stdio rpath wpath cpath proc exec sendfd", NULL) == -1)
461 err(1, "pledge");
463 while ((ch = getopt(argc, argv, "v")) != -1) {
464 switch (ch) {
465 case 'v':
466 verbose = 1;
467 break;
468 default:
469 usage();
470 return 1;
473 argc -= optind;
474 argv += optind;
476 if (argc == 0)
477 repo_path = GOT_REPO_PATH;
478 else if (argc == 1)
479 repo_path = argv[0];
480 else {
481 usage();
482 return 1;
485 RUN_TEST(repo_read_tree(repo_path), "read_tree");
486 RUN_TEST(repo_read_log(repo_path), "read_log");
487 RUN_TEST(repo_read_blob(repo_path), "read_blob");
488 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
489 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
491 return failure ? 1 : 0;