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>
26 #include "got_error.h"
27 #include "got_object.h"
28 #include "got_refs.h"
29 #include "got_repository.h"
30 #include "got_diff.h"
32 #define GOT_REPO_PATH "../../../"
34 static int verbose;
36 void
37 test_printf(char *fmt, ...)
38 {
39 va_list ap;
41 if (!verbose)
42 return;
44 va_start(ap, fmt);
45 vprintf(fmt, ap);
46 va_end(ap);
47 }
49 static const struct got_error *
50 print_commit_object(struct got_object *, struct got_repository *);
52 static const struct got_error *
53 print_parent_commits(struct got_commit_object *commit,
54 struct got_repository *repo)
55 {
56 struct got_parent_id *pid;
57 const struct got_error *err = NULL;
58 struct got_object *obj;
60 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
61 err = got_object_open(&obj, repo, pid->id);
62 if (err != NULL)
63 return err;
64 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
65 return got_error(GOT_ERR_OBJ_TYPE);
66 err = print_commit_object(obj, repo);
67 got_object_close(obj);
68 }
70 return err;
71 }
73 static const struct got_error *
74 print_tree_object(struct got_object *obj, char *parent,
75 struct got_repository *repo)
76 {
77 struct got_tree_object *tree;
78 struct got_tree_entry *te;
79 const struct got_error *err;
81 err = got_object_tree_open(&tree, repo, obj);
82 if (err != NULL)
83 return err;
85 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
86 struct got_object *treeobj;
87 char *next_parent;
88 char *hex;
90 err = got_object_id_str(&hex, te->id);
91 if (err)
92 break;
94 if (!S_ISDIR(te->mode)) {
95 test_printf("%s %s/%s\n", hex, parent, te->name);
96 continue;
97 }
98 test_printf("%s %s/%s\n", hex, parent, te->name);
99 free(hex);
101 err = got_object_open(&treeobj, repo, te->id);
102 if (err != NULL)
103 break;
105 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
106 err = got_error(GOT_ERR_OBJ_TYPE);
107 got_object_close(treeobj);
108 break;
111 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
112 err = got_error(GOT_ERR_NO_MEM);
113 got_object_close(treeobj);
114 break;
117 err = print_tree_object(treeobj, next_parent, repo);
118 free(next_parent);
119 if (err) {
120 got_object_close(treeobj);
121 break;
124 got_object_close(treeobj);
127 got_object_tree_close(tree);
128 return err;
131 static const struct got_error *
132 print_commit_object(struct got_object *obj, struct got_repository *repo)
134 struct got_commit_object *commit;
135 struct got_parent_id *pid;
136 char *buf;
137 const struct got_error *err;
138 struct got_object* treeobj;
140 err = got_object_commit_open(&commit, repo, obj);
141 if (err)
142 return err;
144 err = got_object_id_str(&buf, commit->tree_id);
145 if (err)
146 return err;
147 test_printf("tree: %s\n", buf);
148 free(buf);
149 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
150 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
151 err = got_object_id_str(&buf, pid->id);
152 if (err)
153 return err;
154 test_printf("%s\n", buf);
155 free(buf);
157 test_printf("author: %s\n", commit->author);
158 test_printf("committer: %s\n", commit->committer);
159 test_printf("log: %s\n", commit->logmsg);
161 err = got_object_open(&treeobj, repo, commit->tree_id);
162 if (err != NULL)
163 return err;
164 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
165 print_tree_object(treeobj, "", repo);
166 test_printf("\n");
168 got_object_close(treeobj);
170 err = print_parent_commits(commit, repo);
171 got_object_commit_close(commit);
173 return err;
176 static int
177 repo_read_log(const char *repo_path)
179 const struct got_error *err;
180 struct got_repository *repo;
181 struct got_reference *head_ref;
182 struct got_object_id *id;
183 struct got_object *obj;
184 char *buf;
185 int ret;
187 err = got_repo_open(&repo, repo_path);
188 if (err != NULL || repo == NULL)
189 return 0;
190 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
191 if (err != NULL || head_ref == NULL)
192 return 0;
193 err = got_ref_resolve(&id, repo, head_ref);
194 if (err != NULL || head_ref == NULL)
195 return 0;
196 err = got_object_id_str(&buf, id);
197 if (err != NULL)
198 return 0;
199 test_printf("HEAD is at %s\n", buf);
200 free(buf);
201 err = got_object_open(&obj, repo, id);
202 if (err != NULL || obj == NULL)
203 return 0;
204 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
205 err = print_commit_object(obj, repo);
206 if (err)
207 return 0;
208 } else
209 return 0;
210 got_object_close(obj);
211 free(id);
212 got_ref_close(head_ref);
213 got_repo_close(repo);
214 return 1;
217 static int
218 repo_read_tree(const char *repo_path)
220 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
221 const struct got_error *err;
222 struct got_repository *repo;
223 struct got_object *obj;
224 int i;
225 size_t len;
227 err = got_repo_open(&repo, repo_path);
228 if (err != NULL || repo == NULL)
229 return 0;
230 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
231 if (err != NULL || obj == NULL)
232 return 0;
233 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
234 return 0;
236 print_tree_object(obj, "", repo);
237 test_printf("\n");
239 got_object_close(obj);
240 got_repo_close(repo);
241 return (err == NULL);
243 static int
244 repo_read_blob(const char *repo_path)
246 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
247 const struct got_error *err;
248 struct got_repository *repo;
249 struct got_object *obj;
250 struct got_blob_object *blob;
251 int i;
252 size_t len;
254 err = got_repo_open(&repo, repo_path);
255 if (err != NULL || repo == NULL)
256 return 0;
257 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
258 if (err != NULL || obj == NULL)
259 return 0;
260 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
261 return 0;
263 err = got_object_blob_open(&blob, repo, obj, 64);
264 if (err != NULL)
265 return 0;
267 test_printf("\n");
268 do {
269 const uint8_t *buf = got_object_blob_get_read_buf(blob);
270 err = got_object_blob_read_block(&len, blob);
271 if (err)
272 break;
273 for (i = 0; i < len; i++)
274 test_printf("%c", buf[i]);
275 } while (len != 0);
276 test_printf("\n");
278 got_object_blob_close(blob);
279 got_object_close(obj);
280 got_repo_close(repo);
281 return (err == NULL);
284 static int
285 repo_diff_blob(const char *repo_path)
287 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
288 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
289 const struct got_error *err;
290 struct got_repository *repo;
291 struct got_object *obj1;
292 struct got_object *obj2;
293 struct got_blob_object *blob1;
294 struct got_blob_object *blob2;
295 int i;
296 size_t len;
297 FILE *outfile;
299 err = got_repo_open(&repo, repo_path);
300 if (err != NULL || repo == NULL)
301 return 0;
303 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
304 if (err != NULL || obj1 == NULL)
305 return 0;
306 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
307 return 0;
308 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
309 if (err != NULL || obj2 == NULL)
310 return 0;
311 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
312 return 0;
314 err = got_object_blob_open(&blob1, repo, obj1, 512);
315 if (err != NULL)
316 return 0;
318 err = got_object_blob_open(&blob2, repo, obj2, 512);
319 if (err != NULL)
320 return 0;
322 test_printf("\n");
323 if (!verbose) {
324 outfile = fopen("/dev/null", "w+");
325 if (outfile == NULL)
326 return 0;
327 } else
328 outfile = stdout;
329 got_diff_blob(blob1, blob2, NULL, NULL, outfile);
330 test_printf("\n");
332 got_object_blob_close(blob1);
333 got_object_blob_close(blob2);
334 got_object_close(obj1);
335 got_object_close(obj2);
336 got_repo_close(repo);
337 return (err == NULL);
340 static int
341 repo_diff_tree(const char *repo_path)
343 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
344 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
345 const struct got_error *err;
346 struct got_repository *repo;
347 struct got_object *obj1;
348 struct got_object *obj2;
349 struct got_tree_object *tree1;
350 struct got_tree_object *tree2;
351 int i;
352 size_t len;
353 FILE *outfile;
355 err = got_repo_open(&repo, repo_path);
356 if (err != NULL || repo == NULL)
357 return 0;
359 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
360 if (err != NULL || obj1 == NULL)
361 return 0;
362 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
363 return 0;
364 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
365 if (err != NULL || obj2 == NULL)
366 return 0;
367 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
368 return 0;
370 err = got_object_tree_open(&tree1, repo, obj1);
371 if (err != NULL)
372 return 0;
374 err = got_object_tree_open(&tree2, repo, obj2);
375 if (err != NULL)
376 return 0;
378 if (!verbose) {
379 outfile = fopen("/dev/null", "w+");
380 if (outfile == NULL)
381 return 0;
382 } else
383 outfile = stdout;
384 test_printf("\n");
385 got_diff_tree(tree1, tree2, repo, outfile);
386 test_printf("\n");
388 got_object_tree_close(tree1);
389 got_object_tree_close(tree2);
390 got_object_close(obj1);
391 got_object_close(obj2);
392 got_repo_close(repo);
393 return (err == NULL);
396 #define RUN_TEST(expr, name) \
397 { test_ok = (expr); \
398 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
399 failure = (failure || !test_ok); }
402 void
403 usage(void)
405 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
408 int
409 main(int argc, char *argv[])
411 int test_ok = 0, failure = 0;
412 const char *repo_path;
413 int ch;
414 int vflag = 0;
416 while ((ch = getopt(argc, argv, "v")) != -1) {
417 switch (ch) {
418 case 'v':
419 verbose = 1;
420 break;
421 default:
422 usage();
423 return 1;
426 argc -= optind;
427 argv += optind;
429 if (argc == 0)
430 repo_path = GOT_REPO_PATH;
431 else if (argc == 1)
432 repo_path = argv[0];
433 else {
434 usage();
435 return 1;
438 RUN_TEST(repo_read_tree(repo_path), "read_tree");
439 RUN_TEST(repo_read_log(repo_path), "read_log");
440 RUN_TEST(repo_read_blob(repo_path), "read_blob");
441 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
442 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
444 return failure ? 1 : 0;