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_refs.h"
32 #include "got_repository.h"
33 #include "got_diff.h"
35 #include "got_lib_path.h"
37 #ifndef nitems
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 #endif
41 #define GOT_REPO_PATH "../../../"
43 static int verbose;
45 void
46 test_printf(char *fmt, ...)
47 {
48 va_list ap;
50 if (!verbose)
51 return;
53 va_start(ap, fmt);
54 vprintf(fmt, ap);
55 va_end(ap);
56 }
58 static const struct got_error *
59 print_commit_object(struct got_object *, struct got_repository *);
61 static const struct got_error *
62 print_parent_commits(struct got_commit_object *commit,
63 struct got_repository *repo)
64 {
65 struct got_parent_id *pid;
66 const struct got_error *err = NULL;
67 struct got_object *obj;
69 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
70 err = got_object_open(&obj, repo, pid->id);
71 if (err != NULL)
72 return err;
73 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
74 return got_error(GOT_ERR_OBJ_TYPE);
75 err = print_commit_object(obj, repo);
76 got_object_close(obj);
77 }
79 return err;
80 }
82 static const struct got_error *
83 print_tree_object(struct got_object *obj, char *parent,
84 struct got_repository *repo)
85 {
86 struct got_tree_object *tree;
87 struct got_tree_entry *te;
88 const struct got_error *err;
90 err = got_object_tree_open(&tree, repo, obj);
91 if (err != NULL)
92 return err;
94 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
95 struct got_object *treeobj;
96 char *next_parent;
97 char *hex;
99 err = got_object_id_str(&hex, te->id);
100 if (err)
101 break;
103 if (!S_ISDIR(te->mode)) {
104 test_printf("%s %s/%s\n", hex, parent, te->name);
105 free(hex);
106 continue;
108 test_printf("%s %s/%s\n", hex, parent, te->name);
109 free(hex);
111 err = got_object_open(&treeobj, repo, te->id);
112 if (err != NULL)
113 break;
115 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
116 err = got_error(GOT_ERR_OBJ_TYPE);
117 got_object_close(treeobj);
118 break;
121 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
122 err = got_error_from_errno();
123 got_object_close(treeobj);
124 break;
127 err = print_tree_object(treeobj, next_parent, repo);
128 free(next_parent);
129 if (err) {
130 got_object_close(treeobj);
131 break;
134 got_object_close(treeobj);
137 got_object_tree_close(tree);
138 return err;
141 static const struct got_error *
142 print_commit_object(struct got_object *obj, struct got_repository *repo)
144 struct got_commit_object *commit;
145 struct got_parent_id *pid;
146 char *buf;
147 const struct got_error *err;
148 struct got_object* treeobj;
150 err = got_object_commit_open(&commit, repo, obj);
151 if (err)
152 return err;
154 err = got_object_id_str(&buf, commit->tree_id);
155 if (err)
156 return err;
157 test_printf("tree: %s\n", buf);
158 free(buf);
159 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
160 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
161 err = got_object_id_str(&buf, pid->id);
162 if (err)
163 return err;
164 test_printf("%s\n", buf);
165 free(buf);
167 test_printf("author: %s\n", commit->author);
168 test_printf("committer: %s\n", commit->committer);
169 test_printf("log: %s\n", commit->logmsg);
171 err = got_object_open(&treeobj, repo, commit->tree_id);
172 if (err != NULL)
173 return err;
174 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
175 print_tree_object(treeobj, "", repo);
176 test_printf("\n");
178 got_object_close(treeobj);
180 err = print_parent_commits(commit, repo);
181 got_object_commit_close(commit);
183 return err;
186 static int
187 repo_read_log(const char *repo_path)
189 const struct got_error *err;
190 struct got_repository *repo;
191 struct got_reference *head_ref;
192 struct got_object_id *id;
193 struct got_object *obj;
194 char *buf;
196 err = got_repo_open(&repo, repo_path);
197 if (err != NULL || repo == NULL)
198 return 0;
199 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
200 if (err != NULL || head_ref == NULL)
201 return 0;
202 err = got_ref_resolve(&id, repo, head_ref);
203 if (err != NULL || head_ref == NULL)
204 return 0;
205 err = got_object_id_str(&buf, id);
206 if (err != NULL)
207 return 0;
208 test_printf("HEAD is at %s\n", buf);
209 free(buf);
210 err = got_object_open(&obj, repo, id);
211 if (err != NULL || obj == NULL)
212 return 0;
213 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
214 err = print_commit_object(obj, repo);
215 if (err)
216 return 0;
217 } else
218 return 0;
219 got_object_close(obj);
220 free(id);
221 got_ref_close(head_ref);
222 got_repo_close(repo);
223 return 1;
226 static int
227 repo_read_tree(const char *repo_path)
229 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
230 const struct got_error *err;
231 struct got_repository *repo;
232 struct got_object *obj;
234 err = got_repo_open(&repo, repo_path);
235 if (err != NULL || repo == NULL)
236 return 0;
237 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
238 if (err != NULL || obj == NULL)
239 return 0;
240 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
241 return 0;
243 print_tree_object(obj, "", repo);
244 test_printf("\n");
246 got_object_close(obj);
247 got_repo_close(repo);
248 return (err == NULL);
251 static int
252 repo_read_blob(const char *repo_path)
254 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
255 const struct got_error *err;
256 struct got_repository *repo;
257 struct got_object *obj;
258 struct got_blob_object *blob;
259 int i;
260 size_t len;
262 err = got_repo_open(&repo, repo_path);
263 if (err != NULL || repo == NULL)
264 return 0;
265 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
266 if (err != NULL || obj == NULL)
267 return 0;
268 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
269 return 0;
271 err = got_object_blob_open(&blob, repo, obj, 64);
272 if (err != NULL)
273 return 0;
275 test_printf("\n");
276 do {
277 const uint8_t *buf = got_object_blob_get_read_buf(blob);
278 err = got_object_blob_read_block(&len, blob);
279 if (err)
280 break;
281 for (i = 0; i < len; i++)
282 test_printf("%c", buf[i]);
283 } while (len != 0);
284 test_printf("\n");
286 got_object_blob_close(blob);
287 got_object_close(obj);
288 got_repo_close(repo);
289 return (err == NULL);
292 static int
293 repo_diff_blob(const char *repo_path)
295 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
296 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
297 const struct got_error *err;
298 struct got_repository *repo;
299 struct got_object *obj1;
300 struct got_object *obj2;
301 struct got_blob_object *blob1;
302 struct got_blob_object *blob2;
303 FILE *outfile;
304 int i;
305 char *line;
306 size_t len;
307 const char delim[3] = {'\0', '\0', '\0'};
308 const char *expected_output[] = {
309 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
310 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
311 "@@ -1,10 +1,10 @@",
312 " .PATH:${.CURDIR}/../../lib",
313 " ",
314 " PROG = repository_test",
315 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
316 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
317 " ",
318 " CPPFLAGS = -I${.CURDIR}/../../include",
319 "-LDADD = -lutil",
320 "+LDADD = -lutil -lz",
321 " ",
322 " NOMAN = yes"
323 };
325 err = got_repo_open(&repo, repo_path);
326 if (err != NULL || repo == NULL)
327 return 0;
329 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
330 if (err != NULL || obj1 == NULL)
331 return 0;
332 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
333 return 0;
334 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
335 if (err != NULL || obj2 == NULL)
336 return 0;
337 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
338 return 0;
340 err = got_object_blob_open(&blob1, repo, obj1, 512);
341 if (err != NULL)
342 return 0;
344 err = got_object_blob_open(&blob2, repo, obj2, 512);
345 if (err != NULL)
346 return 0;
348 test_printf("\n");
349 outfile = got_opentemp();
350 if (outfile == NULL)
351 return 0;
352 got_diff_blob(blob1, blob2, NULL, NULL, outfile);
353 rewind(outfile);
354 i = 0;
355 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
356 test_printf(line);
357 test_printf("\n");
358 if (i < nitems(expected_output) &&
359 strcmp(line, expected_output[i]) != 0) {
360 test_printf("diff output mismatch; expected: '%s'\n",
361 expected_output[i]);
363 i++;
365 fclose(outfile);
366 test_printf("\n");
367 if (i != nitems(expected_output) + 1) {
368 test_printf("number of lines expected: %d; actual: %d\n",
369 nitems(expected_output), i - 1);
370 return 0;
373 got_object_blob_close(blob1);
374 got_object_blob_close(blob2);
375 got_object_close(obj1);
376 got_object_close(obj2);
377 got_repo_close(repo);
378 return (err == NULL);
381 static int
382 repo_diff_tree(const char *repo_path)
384 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
385 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
386 const struct got_error *err;
387 struct got_repository *repo;
388 struct got_object *obj1;
389 struct got_object *obj2;
390 struct got_tree_object *tree1;
391 struct got_tree_object *tree2;
392 FILE *outfile;
394 err = got_repo_open(&repo, repo_path);
395 if (err != NULL || repo == NULL)
396 return 0;
398 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
399 if (err != NULL || obj1 == NULL)
400 return 0;
401 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
402 return 0;
403 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
404 if (err != NULL || obj2 == NULL)
405 return 0;
406 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
407 return 0;
409 err = got_object_tree_open(&tree1, repo, obj1);
410 if (err != NULL)
411 return 0;
413 err = got_object_tree_open(&tree2, repo, obj2);
414 if (err != NULL)
415 return 0;
417 if (!verbose) {
418 outfile = fopen("/dev/null", "w+");
419 if (outfile == NULL)
420 return 0;
421 } else
422 outfile = stdout;
423 test_printf("\n");
424 got_diff_tree(tree1, tree2, repo, outfile);
425 test_printf("\n");
427 got_object_tree_close(tree1);
428 got_object_tree_close(tree2);
429 got_object_close(obj1);
430 got_object_close(obj2);
431 got_repo_close(repo);
432 return (err == NULL);
435 #define RUN_TEST(expr, name) \
436 { test_ok = (expr); \
437 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
438 failure = (failure || !test_ok); }
441 void
442 usage(void)
444 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
447 int
448 main(int argc, char *argv[])
450 int test_ok = 0, failure = 0;
451 const char *repo_path;
452 int ch;
454 if (pledge("stdio rpath wpath cpath", NULL) == -1)
455 err(1, "pledge");
457 while ((ch = getopt(argc, argv, "v")) != -1) {
458 switch (ch) {
459 case 'v':
460 verbose = 1;
461 break;
462 default:
463 usage();
464 return 1;
467 argc -= optind;
468 argv += optind;
470 if (argc == 0)
471 repo_path = GOT_REPO_PATH;
472 else if (argc == 1)
473 repo_path = argv[0];
474 else {
475 usage();
476 return 1;
479 RUN_TEST(repo_read_tree(repo_path), "read_tree");
480 RUN_TEST(repo_read_log(repo_path), "read_log");
481 RUN_TEST(repo_read_blob(repo_path), "read_blob");
482 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
483 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
485 return failure ? 1 : 0;