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"
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 err = got_error(GOT_ERR_OBJ_TYPE);
75 else
76 err = print_commit_object(obj, repo);
77 got_object_close(obj);
78 if (err)
79 break;
80 }
82 return err;
83 }
85 static const struct got_error *
86 print_tree_object(struct got_object *obj, char *parent,
87 struct got_repository *repo)
88 {
89 struct got_tree_object *tree;
90 struct got_tree_entry *te;
91 const struct got_error *err;
93 err = got_object_tree_open(&tree, repo, obj);
94 if (err != NULL)
95 return err;
97 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
98 struct got_object *treeobj;
99 char *next_parent;
100 char *hex;
102 err = got_object_id_str(&hex, te->id);
103 if (err)
104 break;
106 if (!S_ISDIR(te->mode)) {
107 test_printf("%s %s/%s\n", hex, parent, te->name);
108 free(hex);
109 continue;
111 test_printf("%s %s/%s\n", hex, parent, te->name);
112 free(hex);
114 err = got_object_open(&treeobj, repo, te->id);
115 if (err != NULL)
116 break;
118 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
119 err = got_error(GOT_ERR_OBJ_TYPE);
120 got_object_close(treeobj);
121 break;
124 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
125 err = got_error_from_errno();
126 got_object_close(treeobj);
127 break;
130 err = print_tree_object(treeobj, next_parent, repo);
131 free(next_parent);
132 if (err) {
133 got_object_close(treeobj);
134 break;
137 got_object_close(treeobj);
140 got_object_tree_close(tree);
141 return err;
144 static const struct got_error *
145 print_commit_object(struct got_object *obj, struct got_repository *repo)
147 struct got_commit_object *commit;
148 struct got_parent_id *pid;
149 char *buf;
150 const struct got_error *err;
151 struct got_object* treeobj;
153 err = got_object_commit_open(&commit, repo, obj);
154 if (err)
155 return err;
157 err = got_object_id_str(&buf, commit->tree_id);
158 if (err)
159 return err;
160 test_printf("tree: %s\n", buf);
161 free(buf);
162 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
163 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
164 err = got_object_id_str(&buf, pid->id);
165 if (err)
166 return err;
167 test_printf("%s\n", buf);
168 free(buf);
170 test_printf("author: %s\n", commit->author);
171 test_printf("committer: %s\n", commit->committer);
172 test_printf("log: %s\n", commit->logmsg);
174 err = got_object_open(&treeobj, repo, commit->tree_id);
175 if (err != NULL)
176 return err;
177 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
178 print_tree_object(treeobj, "", repo);
179 test_printf("\n");
181 got_object_close(treeobj);
183 err = print_parent_commits(commit, repo);
184 got_object_commit_close(commit);
186 return err;
189 static int
190 repo_read_log(const char *repo_path)
192 const struct got_error *err;
193 struct got_repository *repo;
194 struct got_reference *head_ref;
195 struct got_object_id *id;
196 struct got_object *obj;
197 char *buf;
199 err = got_repo_open(&repo, repo_path);
200 if (err != NULL || repo == NULL)
201 return 0;
202 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
203 if (err != NULL || head_ref == NULL)
204 return 0;
205 err = got_ref_resolve(&id, repo, head_ref);
206 if (err != NULL || head_ref == NULL)
207 return 0;
208 err = got_object_id_str(&buf, id);
209 if (err != NULL)
210 return 0;
211 test_printf("HEAD is at %s\n", buf);
212 free(buf);
213 err = got_object_open(&obj, repo, id);
214 if (err != NULL || obj == NULL)
215 return 0;
216 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
217 err = print_commit_object(obj, repo);
218 if (err)
219 return 0;
220 } else
221 return 0;
222 got_object_close(obj);
223 free(id);
224 got_ref_close(head_ref);
225 got_repo_close(repo);
226 return 1;
229 static int
230 repo_read_tree(const char *repo_path)
232 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
233 const struct got_error *err;
234 struct got_repository *repo;
235 struct got_object *obj;
237 err = got_repo_open(&repo, repo_path);
238 if (err != NULL || repo == NULL)
239 return 0;
240 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
241 if (err != NULL || obj == NULL)
242 return 0;
243 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
244 return 0;
246 print_tree_object(obj, "", repo);
247 test_printf("\n");
249 got_object_close(obj);
250 got_repo_close(repo);
251 return (err == NULL);
254 static int
255 repo_read_blob(const char *repo_path)
257 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
258 const struct got_error *err;
259 struct got_repository *repo;
260 struct got_object *obj;
261 struct got_blob_object *blob;
262 int i;
263 size_t len;
265 err = got_repo_open(&repo, repo_path);
266 if (err != NULL || repo == NULL)
267 return 0;
268 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
269 if (err != NULL || obj == NULL)
270 return 0;
271 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
272 return 0;
274 err = got_object_blob_open(&blob, repo, obj, 64);
275 if (err != NULL)
276 return 0;
278 test_printf("\n");
279 do {
280 const uint8_t *buf = got_object_blob_get_read_buf(blob);
281 err = got_object_blob_read_block(&len, blob);
282 if (err)
283 break;
284 for (i = 0; i < len; i++)
285 test_printf("%c", buf[i]);
286 } while (len != 0);
287 test_printf("\n");
289 got_object_blob_close(blob);
290 got_object_close(obj);
291 got_repo_close(repo);
292 return (err == NULL);
295 static int
296 repo_diff_blob(const char *repo_path)
298 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
299 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
300 const struct got_error *err;
301 struct got_repository *repo;
302 struct got_object *obj1;
303 struct got_object *obj2;
304 struct got_blob_object *blob1;
305 struct got_blob_object *blob2;
306 FILE *outfile;
307 int i;
308 char *line;
309 size_t len;
310 const char delim[3] = {'\0', '\0', '\0'};
311 const char *expected_output[] = {
312 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
313 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
314 "@@ -1,10 +1,10 @@",
315 " .PATH:${.CURDIR}/../../lib",
316 " ",
317 " PROG = repository_test",
318 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
319 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
320 " ",
321 " CPPFLAGS = -I${.CURDIR}/../../include",
322 "-LDADD = -lutil",
323 "+LDADD = -lutil -lz",
324 " ",
325 " NOMAN = yes"
326 };
328 err = got_repo_open(&repo, repo_path);
329 if (err != NULL || repo == NULL)
330 return 0;
332 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
333 if (err != NULL || obj1 == NULL)
334 return 0;
335 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
336 return 0;
337 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
338 if (err != NULL || obj2 == NULL)
339 return 0;
340 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
341 return 0;
343 err = got_object_blob_open(&blob1, repo, obj1, 512);
344 if (err != NULL)
345 return 0;
347 err = got_object_blob_open(&blob2, repo, obj2, 512);
348 if (err != NULL)
349 return 0;
351 test_printf("\n");
352 outfile = got_opentemp();
353 if (outfile == NULL)
354 return 0;
355 got_diff_blob(blob1, blob2, NULL, NULL, outfile);
356 rewind(outfile);
357 i = 0;
358 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
359 test_printf(line);
360 test_printf("\n");
361 if (i < nitems(expected_output) &&
362 strcmp(line, expected_output[i]) != 0) {
363 test_printf("diff output mismatch; expected: '%s'\n",
364 expected_output[i]);
366 i++;
368 fclose(outfile);
369 test_printf("\n");
370 if (i != nitems(expected_output) + 1) {
371 test_printf("number of lines expected: %d; actual: %d\n",
372 nitems(expected_output), i - 1);
373 return 0;
376 got_object_blob_close(blob1);
377 got_object_blob_close(blob2);
378 got_object_close(obj1);
379 got_object_close(obj2);
380 got_repo_close(repo);
381 return (err == NULL);
384 static int
385 repo_diff_tree(const char *repo_path)
387 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
388 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
389 const struct got_error *err;
390 struct got_repository *repo;
391 struct got_object *obj1;
392 struct got_object *obj2;
393 struct got_tree_object *tree1;
394 struct got_tree_object *tree2;
395 FILE *outfile;
397 err = got_repo_open(&repo, repo_path);
398 if (err != NULL || repo == NULL)
399 return 0;
401 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
402 if (err != NULL || obj1 == NULL)
403 return 0;
404 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
405 return 0;
406 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
407 if (err != NULL || obj2 == NULL)
408 return 0;
409 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
410 return 0;
412 err = got_object_tree_open(&tree1, repo, obj1);
413 if (err != NULL)
414 return 0;
416 err = got_object_tree_open(&tree2, repo, obj2);
417 if (err != NULL)
418 return 0;
420 if (!verbose) {
421 outfile = fopen("/dev/null", "w+");
422 if (outfile == NULL)
423 return 0;
424 } else
425 outfile = stdout;
426 test_printf("\n");
427 got_diff_tree(tree1, tree2, repo, outfile);
428 test_printf("\n");
430 got_object_tree_close(tree1);
431 got_object_tree_close(tree2);
432 got_object_close(obj1);
433 got_object_close(obj2);
434 got_repo_close(repo);
435 return (err == NULL);
438 #define RUN_TEST(expr, name) \
439 { test_ok = (expr); \
440 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
441 failure = (failure || !test_ok); }
444 void
445 usage(void)
447 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
450 int
451 main(int argc, char *argv[])
453 int test_ok = 0, failure = 0;
454 const char *repo_path;
455 int ch;
457 if (pledge("stdio rpath wpath cpath proc", NULL) == -1)
458 err(1, "pledge");
460 while ((ch = getopt(argc, argv, "v")) != -1) {
461 switch (ch) {
462 case 'v':
463 verbose = 1;
464 break;
465 default:
466 usage();
467 return 1;
470 argc -= optind;
471 argv += optind;
473 if (argc == 0)
474 repo_path = GOT_REPO_PATH;
475 else if (argc == 1)
476 repo_path = argv[0];
477 else {
478 usage();
479 return 1;
482 RUN_TEST(repo_read_tree(repo_path), "read_tree");
483 RUN_TEST(repo_read_log(repo_path), "read_log");
484 RUN_TEST(repo_read_blob(repo_path), "read_blob");
485 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
486 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
488 return failure ? 1 : 0;