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 <sha1.h>
25 #include <zlib.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_sha1.h"
33 #include "got_diff.h"
35 #define GOT_REPO_PATH "../../../"
37 static int verbose;
39 void
40 test_printf(char *fmt, ...)
41 {
42 va_list ap;
44 if (!verbose)
45 return;
47 va_start(ap, fmt);
48 vprintf(fmt, ap);
49 va_end(ap);
50 }
52 static const struct got_error *
53 print_commit_object(struct got_object *, struct got_repository *);
55 static const struct got_error *
56 print_parent_commits(struct got_commit_object *commit,
57 struct got_repository *repo)
58 {
59 struct got_parent_id *pid;
60 const struct got_error *err = NULL;
61 struct got_object *obj;
63 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
64 err = got_object_open(&obj, repo, pid->id);
65 if (err != NULL)
66 return err;
67 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
68 return got_error(GOT_ERR_OBJ_TYPE);
69 err = print_commit_object(obj, repo);
70 got_object_close(obj);
71 }
73 return err;
74 }
76 static const struct got_error *
77 print_tree_object(struct got_object *obj, char *parent,
78 struct got_repository *repo)
79 {
80 struct got_tree_object *tree;
81 struct got_tree_entry *te;
82 const struct got_error *err;
84 err = got_object_tree_open(&tree, repo, obj);
85 if (err != NULL)
86 return err;
88 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
89 struct got_object *treeobj;
90 char *next_parent;
91 char *hex;
93 err = got_object_id_str(&hex, te->id);
94 if (err)
95 break;
97 if (!S_ISDIR(te->mode)) {
98 test_printf("%s %s/%s\n", hex, parent, te->name);
99 continue;
101 test_printf("%s %s/%s\n", hex, parent, te->name);
102 free(hex);
104 err = got_object_open(&treeobj, repo, te->id);
105 if (err != NULL)
106 break;
108 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
109 err = got_error(GOT_ERR_OBJ_TYPE);
110 got_object_close(treeobj);
111 break;
114 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
115 err = got_error(GOT_ERR_NO_MEM);
116 got_object_close(treeobj);
117 break;
120 err = print_tree_object(treeobj, next_parent, repo);
121 free(next_parent);
122 if (err) {
123 got_object_close(treeobj);
124 break;
127 got_object_close(treeobj);
130 got_object_tree_close(tree);
131 return err;
134 static const struct got_error *
135 print_commit_object(struct got_object *obj, struct got_repository *repo)
137 struct got_commit_object *commit;
138 struct got_parent_id *pid;
139 char *buf;
140 const struct got_error *err;
141 struct got_object* treeobj;
143 err = got_object_commit_open(&commit, repo, obj);
144 if (err)
145 return err;
147 err = got_object_id_str(&buf, commit->tree_id);
148 if (err)
149 return err;
150 test_printf("tree: %s\n", buf);
151 free(buf);
152 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
153 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
154 err = got_object_id_str(&buf, pid->id);
155 if (err)
156 return err;
157 test_printf("%s\n", buf);
158 free(buf);
160 test_printf("author: %s\n", commit->author);
161 test_printf("committer: %s\n", commit->committer);
162 test_printf("log: %s\n", commit->logmsg);
164 err = got_object_open(&treeobj, repo, commit->tree_id);
165 if (err != NULL)
166 return err;
167 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
168 print_tree_object(treeobj, "", repo);
169 test_printf("\n");
171 got_object_close(treeobj);
173 err = print_parent_commits(commit, repo);
174 got_object_commit_close(commit);
176 return err;
179 static int
180 repo_read_log(const char *repo_path)
182 const struct got_error *err;
183 struct got_repository *repo;
184 struct got_reference *head_ref;
185 struct got_object_id *id;
186 struct got_object *obj;
187 char *buf;
188 int ret;
190 err = got_repo_open(&repo, repo_path);
191 if (err != NULL || repo == NULL)
192 return 0;
193 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
194 if (err != NULL || head_ref == NULL)
195 return 0;
196 err = got_ref_resolve(&id, repo, head_ref);
197 if (err != NULL || head_ref == NULL)
198 return 0;
199 err = got_object_id_str(&buf, id);
200 if (err != NULL)
201 return 0;
202 test_printf("HEAD is at %s\n", buf);
203 free(buf);
204 err = got_object_open(&obj, repo, id);
205 if (err != NULL || obj == NULL)
206 return 0;
207 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
208 err = print_commit_object(obj, repo);
209 if (err)
210 return 0;
211 } else
212 return 0;
213 got_object_close(obj);
214 free(id);
215 got_ref_close(head_ref);
216 got_repo_close(repo);
217 return 1;
220 static int
221 repo_read_tree(const char *repo_path)
223 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
224 const struct got_error *err;
225 struct got_repository *repo;
226 struct got_object *obj;
227 char hex[SHA1_DIGEST_STRING_LENGTH];
228 int i;
229 size_t len;
231 err = got_repo_open(&repo, repo_path);
232 if (err != NULL || repo == NULL)
233 return 0;
234 err = got_object_open_by_id_str(&obj, repo, tree_sha1);
235 if (err != NULL || obj == NULL)
236 return 0;
237 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
238 return 0;
240 print_tree_object(obj, "", repo);
241 test_printf("\n");
243 got_object_close(obj);
244 got_repo_close(repo);
245 return (err == NULL);
247 static int
248 repo_read_blob(const char *repo_path)
250 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
251 const struct got_error *err;
252 struct got_repository *repo;
253 struct got_object *obj;
254 struct got_blob_object *blob;
255 char hex[SHA1_DIGEST_STRING_LENGTH];
256 int i;
257 size_t len;
259 err = got_repo_open(&repo, repo_path);
260 if (err != NULL || repo == NULL)
261 return 0;
262 err = got_object_open_by_id_str(&obj, repo, blob_sha1);
263 if (err != NULL || obj == NULL)
264 return 0;
265 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
266 return 0;
268 err = got_object_blob_open(&blob, repo, obj, 64);
269 if (err != NULL)
270 return 0;
272 test_printf("\n");
273 do {
274 const uint8_t *buf = got_object_blob_get_read_buf(blob);
275 err = got_object_blob_read_block(&len, blob);
276 if (err)
277 break;
278 for (i = 0; i < len; i++)
279 test_printf("%c", buf[i]);
280 } while (len != 0);
281 test_printf("\n");
283 got_object_blob_close(blob);
284 got_object_close(obj);
285 got_repo_close(repo);
286 return (err == NULL);
289 static int
290 repo_diff_blob(const char *repo_path)
292 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
293 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
294 const struct got_error *err;
295 struct got_repository *repo;
296 struct got_object *obj1;
297 struct got_object *obj2;
298 struct got_blob_object *blob1;
299 struct got_blob_object *blob2;
300 char hex[SHA1_DIGEST_STRING_LENGTH];
301 int i;
302 size_t len;
303 FILE *outfile;
305 err = got_repo_open(&repo, repo_path);
306 if (err != NULL || repo == NULL)
307 return 0;
309 err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
310 if (err != NULL || obj1 == NULL)
311 return 0;
312 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
313 return 0;
314 err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
315 if (err != NULL || obj2 == NULL)
316 return 0;
317 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
318 return 0;
320 err = got_object_blob_open(&blob1, repo, obj1, 512);
321 if (err != NULL)
322 return 0;
324 err = got_object_blob_open(&blob2, repo, obj2, 512);
325 if (err != NULL)
326 return 0;
328 test_printf("\n");
329 if (!verbose) {
330 outfile = fopen("/dev/null", "w+");
331 if (outfile == NULL)
332 return 0;
333 } else
334 outfile = stdout;
335 got_diff_blob(blob1, blob2, NULL, NULL, outfile);
336 test_printf("\n");
338 got_object_blob_close(blob1);
339 got_object_blob_close(blob2);
340 got_object_close(obj1);
341 got_object_close(obj2);
342 got_repo_close(repo);
343 return (err == NULL);
346 static int
347 repo_diff_tree(const char *repo_path)
349 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
350 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
351 const struct got_error *err;
352 struct got_repository *repo;
353 struct got_object *obj1;
354 struct got_object *obj2;
355 struct got_tree_object *tree1;
356 struct got_tree_object *tree2;
357 char hex[SHA1_DIGEST_STRING_LENGTH];
358 int i;
359 size_t len;
360 FILE *outfile;
362 err = got_repo_open(&repo, repo_path);
363 if (err != NULL || repo == NULL)
364 return 0;
366 err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
367 if (err != NULL || obj1 == NULL)
368 return 0;
369 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
370 return 0;
371 err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
372 if (err != NULL || obj2 == NULL)
373 return 0;
374 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
375 return 0;
377 err = got_object_tree_open(&tree1, repo, obj1);
378 if (err != NULL)
379 return 0;
381 err = got_object_tree_open(&tree2, repo, obj2);
382 if (err != NULL)
383 return 0;
385 if (!verbose) {
386 outfile = fopen("/dev/null", "w+");
387 if (outfile == NULL)
388 return 0;
389 } else
390 outfile = stdout;
391 test_printf("\n");
392 got_diff_tree(tree1, tree2, repo, outfile);
393 test_printf("\n");
395 got_object_tree_close(tree1);
396 got_object_tree_close(tree2);
397 got_object_close(obj1);
398 got_object_close(obj2);
399 got_repo_close(repo);
400 return (err == NULL);
403 #define RUN_TEST(expr, name) \
404 { test_ok = (expr); \
405 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
406 failure = (failure || !test_ok); }
409 void
410 usage(void)
412 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
415 int
416 main(int argc, char *argv[])
418 int test_ok = 0, failure = 0;
419 const char *repo_path;
420 int ch;
421 int vflag = 0;
423 while ((ch = getopt(argc, argv, "v")) != -1) {
424 switch (ch) {
425 case 'v':
426 verbose = 1;
427 break;
428 default:
429 usage();
430 return 1;
433 argc -= optind;
434 argv += optind;
436 if (argc == 0)
437 repo_path = GOT_REPO_PATH;
438 else if (argc == 1)
439 repo_path = argv[1];
440 else {
441 usage();
442 return 1;
445 RUN_TEST(repo_read_tree(repo_path), "read_tree");
446 RUN_TEST(repo_read_log(repo_path), "read_log");
447 RUN_TEST(repo_read_blob(repo_path), "read_blob");
448 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
449 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
451 return failure ? 1 : 0;