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 <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <err.h>
26 #include <unistd.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_refs.h"
31 #include "got_repository.h"
32 #include "got_diff.h"
34 #define GOT_REPO_PATH "../../../"
36 static int verbose;
38 void
39 test_printf(char *fmt, ...)
40 {
41 va_list ap;
43 if (!verbose)
44 return;
46 va_start(ap, fmt);
47 vprintf(fmt, ap);
48 va_end(ap);
49 }
51 static const struct got_error *
52 print_commit_object(struct got_object *, struct got_repository *);
54 static const struct got_error *
55 print_parent_commits(struct got_commit_object *commit,
56 struct got_repository *repo)
57 {
58 struct got_parent_id *pid;
59 const struct got_error *err = NULL;
60 struct got_object *obj;
62 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
63 err = got_object_open(&obj, repo, pid->id);
64 if (err != NULL)
65 return err;
66 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
67 return got_error(GOT_ERR_OBJ_TYPE);
68 err = print_commit_object(obj, repo);
69 got_object_close(obj);
70 }
72 return err;
73 }
75 static const struct got_error *
76 print_tree_object(struct got_object *obj, char *parent,
77 struct got_repository *repo)
78 {
79 struct got_tree_object *tree;
80 struct got_tree_entry *te;
81 const struct got_error *err;
83 err = got_object_tree_open(&tree, repo, obj);
84 if (err != NULL)
85 return err;
87 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
88 struct got_object *treeobj;
89 char *next_parent;
90 char *hex;
92 err = got_object_id_str(&hex, te->id);
93 if (err)
94 break;
96 if (!S_ISDIR(te->mode)) {
97 test_printf("%s %s/%s\n", hex, parent, te->name);
98 continue;
99 }
100 test_printf("%s %s/%s\n", hex, parent, te->name);
101 free(hex);
103 err = got_object_open(&treeobj, repo, te->id);
104 if (err != NULL)
105 break;
107 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
108 err = got_error(GOT_ERR_OBJ_TYPE);
109 got_object_close(treeobj);
110 break;
113 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
114 err = got_error(GOT_ERR_NO_MEM);
115 got_object_close(treeobj);
116 break;
119 err = print_tree_object(treeobj, next_parent, repo);
120 free(next_parent);
121 if (err) {
122 got_object_close(treeobj);
123 break;
126 got_object_close(treeobj);
129 got_object_tree_close(tree);
130 return err;
133 static const struct got_error *
134 print_commit_object(struct got_object *obj, struct got_repository *repo)
136 struct got_commit_object *commit;
137 struct got_parent_id *pid;
138 char *buf;
139 const struct got_error *err;
140 struct got_object* treeobj;
142 err = got_object_commit_open(&commit, repo, obj);
143 if (err)
144 return err;
146 err = got_object_id_str(&buf, commit->tree_id);
147 if (err)
148 return err;
149 test_printf("tree: %s\n", buf);
150 free(buf);
151 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
152 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
153 err = got_object_id_str(&buf, pid->id);
154 if (err)
155 return err;
156 test_printf("%s\n", buf);
157 free(buf);
159 test_printf("author: %s\n", commit->author);
160 test_printf("committer: %s\n", commit->committer);
161 test_printf("log: %s\n", commit->logmsg);
163 err = got_object_open(&treeobj, repo, commit->tree_id);
164 if (err != NULL)
165 return err;
166 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
167 print_tree_object(treeobj, "", repo);
168 test_printf("\n");
170 got_object_close(treeobj);
172 err = print_parent_commits(commit, repo);
173 got_object_commit_close(commit);
175 return err;
178 static int
179 repo_read_log(const char *repo_path)
181 const struct got_error *err;
182 struct got_repository *repo;
183 struct got_reference *head_ref;
184 struct got_object_id *id;
185 struct got_object *obj;
186 char *buf;
188 err = got_repo_open(&repo, repo_path);
189 if (err != NULL || repo == NULL)
190 return 0;
191 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
192 if (err != NULL || head_ref == NULL)
193 return 0;
194 err = got_ref_resolve(&id, repo, head_ref);
195 if (err != NULL || head_ref == NULL)
196 return 0;
197 err = got_object_id_str(&buf, id);
198 if (err != NULL)
199 return 0;
200 test_printf("HEAD is at %s\n", buf);
201 free(buf);
202 err = got_object_open(&obj, repo, id);
203 if (err != NULL || obj == NULL)
204 return 0;
205 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
206 err = print_commit_object(obj, repo);
207 if (err)
208 return 0;
209 } else
210 return 0;
211 got_object_close(obj);
212 free(id);
213 got_ref_close(head_ref);
214 got_repo_close(repo);
215 return 1;
218 static int
219 repo_read_tree(const char *repo_path)
221 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
222 const struct got_error *err;
223 struct got_repository *repo;
224 struct got_object *obj;
226 err = got_repo_open(&repo, repo_path);
227 if (err != NULL || repo == NULL)
228 return 0;
229 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
230 if (err != NULL || obj == NULL)
231 return 0;
232 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
233 return 0;
235 print_tree_object(obj, "", repo);
236 test_printf("\n");
238 got_object_close(obj);
239 got_repo_close(repo);
240 return (err == NULL);
242 static int
243 repo_read_blob(const char *repo_path)
245 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
246 const struct got_error *err;
247 struct got_repository *repo;
248 struct got_object *obj;
249 struct got_blob_object *blob;
250 int i;
251 size_t len;
253 err = got_repo_open(&repo, repo_path);
254 if (err != NULL || repo == NULL)
255 return 0;
256 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
257 if (err != NULL || obj == NULL)
258 return 0;
259 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
260 return 0;
262 err = got_object_blob_open(&blob, repo, obj, 64);
263 if (err != NULL)
264 return 0;
266 test_printf("\n");
267 do {
268 const uint8_t *buf = got_object_blob_get_read_buf(blob);
269 err = got_object_blob_read_block(&len, blob);
270 if (err)
271 break;
272 for (i = 0; i < len; i++)
273 test_printf("%c", buf[i]);
274 } while (len != 0);
275 test_printf("\n");
277 got_object_blob_close(blob);
278 got_object_close(obj);
279 got_repo_close(repo);
280 return (err == NULL);
283 static int
284 repo_diff_blob(const char *repo_path)
286 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
287 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
288 const struct got_error *err;
289 struct got_repository *repo;
290 struct got_object *obj1;
291 struct got_object *obj2;
292 struct got_blob_object *blob1;
293 struct got_blob_object *blob2;
294 FILE *outfile;
296 err = got_repo_open(&repo, repo_path);
297 if (err != NULL || repo == NULL)
298 return 0;
300 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
301 if (err != NULL || obj1 == NULL)
302 return 0;
303 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
304 return 0;
305 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
306 if (err != NULL || obj2 == NULL)
307 return 0;
308 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
309 return 0;
311 err = got_object_blob_open(&blob1, repo, obj1, 512);
312 if (err != NULL)
313 return 0;
315 err = got_object_blob_open(&blob2, repo, obj2, 512);
316 if (err != NULL)
317 return 0;
319 test_printf("\n");
320 if (!verbose) {
321 outfile = fopen("/dev/null", "w+");
322 if (outfile == NULL)
323 return 0;
324 } else
325 outfile = stdout;
326 got_diff_blob(blob1, blob2, NULL, NULL, outfile);
327 test_printf("\n");
329 got_object_blob_close(blob1);
330 got_object_blob_close(blob2);
331 got_object_close(obj1);
332 got_object_close(obj2);
333 got_repo_close(repo);
334 return (err == NULL);
337 static int
338 repo_diff_tree(const char *repo_path)
340 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
341 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
342 const struct got_error *err;
343 struct got_repository *repo;
344 struct got_object *obj1;
345 struct got_object *obj2;
346 struct got_tree_object *tree1;
347 struct got_tree_object *tree2;
348 FILE *outfile;
350 err = got_repo_open(&repo, repo_path);
351 if (err != NULL || repo == NULL)
352 return 0;
354 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
355 if (err != NULL || obj1 == NULL)
356 return 0;
357 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
358 return 0;
359 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
360 if (err != NULL || obj2 == NULL)
361 return 0;
362 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
363 return 0;
365 err = got_object_tree_open(&tree1, repo, obj1);
366 if (err != NULL)
367 return 0;
369 err = got_object_tree_open(&tree2, repo, obj2);
370 if (err != NULL)
371 return 0;
373 if (!verbose) {
374 outfile = fopen("/dev/null", "w+");
375 if (outfile == NULL)
376 return 0;
377 } else
378 outfile = stdout;
379 test_printf("\n");
380 got_diff_tree(tree1, tree2, repo, outfile);
381 test_printf("\n");
383 got_object_tree_close(tree1);
384 got_object_tree_close(tree2);
385 got_object_close(obj1);
386 got_object_close(obj2);
387 got_repo_close(repo);
388 return (err == NULL);
391 #define RUN_TEST(expr, name) \
392 { test_ok = (expr); \
393 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
394 failure = (failure || !test_ok); }
397 void
398 usage(void)
400 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
403 int
404 main(int argc, char *argv[])
406 int test_ok = 0, failure = 0;
407 const char *repo_path;
408 int ch;
410 if (pledge("stdio rpath wpath cpath", NULL) == -1)
411 err(1, "pledge");
413 while ((ch = getopt(argc, argv, "v")) != -1) {
414 switch (ch) {
415 case 'v':
416 verbose = 1;
417 break;
418 default:
419 usage();
420 return 1;
423 argc -= optind;
424 argv += optind;
426 if (argc == 0)
427 repo_path = GOT_REPO_PATH;
428 else if (argc == 1)
429 repo_path = argv[0];
430 else {
431 usage();
432 return 1;
435 RUN_TEST(repo_read_tree(repo_path), "read_tree");
436 RUN_TEST(repo_read_log(repo_path), "read_log");
437 RUN_TEST(repo_read_blob(repo_path), "read_blob");
438 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
439 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
441 return failure ? 1 : 0;