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>
27 #include "got_error.h"
28 #include "got_object.h"
29 #include "got_refs.h"
30 #include "got_repository.h"
31 #include "got_sha1.h"
32 #include "got_diff.h"
33 #include "unistd.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;
83 char hex[SHA1_DIGEST_STRING_LENGTH];
85 err = got_object_tree_open(&tree, repo, obj);
86 if (err != NULL)
87 return err;
89 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
90 struct got_object *treeobj;
91 char *next_parent;
93 if (!S_ISDIR(te->mode)) {
94 test_printf("%s %s/%s\n",
95 got_object_id_str(&te->id, hex, sizeof(hex)),
96 parent, te->name);
97 continue;
98 }
99 test_printf("%s %s/%s\n",
100 got_object_id_str(&te->id, hex, sizeof(hex)),
101 parent, te->name);
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[SHA1_DIGEST_STRING_LENGTH];
139 const struct got_error *err;
140 struct got_object* treeobj;
142 err = got_object_commit_open(&commit, repo, obj);
143 if (err != NULL)
144 return err;
146 test_printf("tree: %s\n",
147 got_object_id_str(&commit->tree_id, buf, sizeof(buf)));
148 test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
149 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
150 test_printf("%s\n",
151 got_object_id_str(&pid->id, buf, sizeof(buf)));
153 test_printf("author: %s\n", commit->author);
154 test_printf("committer: %s\n", commit->committer);
155 test_printf("log: %s\n", commit->logmsg);
157 err = got_object_open(&treeobj, repo, &commit->tree_id);
158 if (err != NULL)
159 return err;
160 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
161 print_tree_object(treeobj, "", repo);
162 test_printf("\n");
164 got_object_close(treeobj);
166 err = print_parent_commits(commit, repo);
167 got_object_commit_close(commit);
169 return err;
172 static int
173 repo_read_log(const char *repo_path)
175 const struct got_error *err;
176 struct got_repository *repo;
177 struct got_reference *head_ref;
178 struct got_object_id *id;
179 struct got_object *obj;
180 char buf[SHA1_DIGEST_STRING_LENGTH];
181 int ret;
183 err = got_repo_open(&repo, repo_path);
184 if (err != NULL || repo == NULL)
185 return 0;
186 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
187 if (err != NULL || head_ref == NULL)
188 return 0;
189 err = got_ref_resolve(&id, repo, head_ref);
190 if (err != NULL || head_ref == NULL)
191 return 0;
192 test_printf("HEAD is at %s\n", got_object_id_str(id, buf, sizeof(buf)));
193 err = got_object_open(&obj, repo, id);
194 if (err != NULL || obj == NULL)
195 return 0;
196 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
197 err = print_commit_object(obj, repo);
198 if (err)
199 return 0;
200 } else
201 return 0;
202 got_object_close(obj);
203 free(id);
204 got_ref_close(head_ref);
205 got_repo_close(repo);
206 return 1;
209 static int
210 repo_read_tree(const char *repo_path)
212 const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
213 const struct got_error *err;
214 struct got_repository *repo;
215 struct got_object_id id;
216 struct got_object *obj;
217 char hex[SHA1_DIGEST_STRING_LENGTH];
218 int i;
219 size_t len;
221 if (!got_parse_sha1_digest(id.sha1, tree_sha1))
222 return 0;
224 err = got_repo_open(&repo, repo_path);
225 if (err != NULL || repo == NULL)
226 return 0;
227 err = got_object_open(&obj, repo, &id);
228 if (err != NULL || obj == NULL)
229 return 0;
230 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
231 return 0;
233 print_tree_object(obj, "", repo);
234 test_printf("\n");
236 got_object_close(obj);
237 got_repo_close(repo);
238 return (err == NULL);
240 static int
241 repo_read_blob(const char *repo_path)
243 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
244 const struct got_error *err;
245 struct got_repository *repo;
246 struct got_object_id id;
247 struct got_object *obj;
248 struct got_blob_object *blob;
249 char hex[SHA1_DIGEST_STRING_LENGTH];
250 int i;
251 size_t len;
253 if (!got_parse_sha1_digest(id.sha1, blob_sha1))
254 return 0;
256 err = got_repo_open(&repo, repo_path);
257 if (err != NULL || repo == NULL)
258 return 0;
259 err = got_object_open(&obj, repo, &id);
260 if (err != NULL || obj == NULL)
261 return 0;
262 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
263 return 0;
265 err = got_object_blob_open(&blob, repo, obj, 64);
266 if (err != NULL)
267 return 0;
269 test_printf("\n");
270 do {
271 err = got_object_blob_read_block(&len, blob);
272 if (err)
273 break;
274 for (i = 0; i < len; i++)
275 test_printf("%c", blob->read_buf[i]);
276 } while (len != 0);
277 test_printf("\n");
279 got_object_blob_close(blob);
280 got_object_close(obj);
281 got_repo_close(repo);
282 return (err == NULL);
285 static int
286 repo_diff_blob(const char *repo_path)
288 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
289 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
290 const struct got_error *err;
291 struct got_repository *repo;
292 struct got_object_id id1;
293 struct got_object_id id2;
294 struct got_object *obj1;
295 struct got_object *obj2;
296 struct got_blob_object *blob1;
297 struct got_blob_object *blob2;
298 char hex[SHA1_DIGEST_STRING_LENGTH];
299 int i;
300 size_t len;
302 if (!got_parse_sha1_digest(id1.sha1, blob1_sha1))
303 return 0;
304 if (!got_parse_sha1_digest(id2.sha1, blob2_sha1))
305 return 0;
307 err = got_repo_open(&repo, repo_path);
308 if (err != NULL || repo == NULL)
309 return 0;
311 err = got_object_open(&obj1, repo, &id1);
312 if (err != NULL || obj1 == NULL)
313 return 0;
314 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
315 return 0;
316 err = got_object_open(&obj2, repo, &id2);
317 if (err != NULL || obj2 == NULL)
318 return 0;
319 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
320 return 0;
322 err = got_object_blob_open(&blob1, repo, obj1, 512);
323 if (err != NULL)
324 return 0;
326 err = got_object_blob_open(&blob2, repo, obj2, 512);
327 if (err != NULL)
328 return 0;
330 test_printf("\n");
331 got_diff_blob(blob1, blob2, NULL, NULL, stdout);
332 test_printf("\n");
334 got_object_blob_close(blob1);
335 got_object_blob_close(blob2);
336 got_object_close(obj1);
337 got_object_close(obj2);
338 got_repo_close(repo);
339 return (err == NULL);
342 static int
343 repo_diff_tree(const char *repo_path)
345 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
346 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
347 const struct got_error *err;
348 struct got_repository *repo;
349 struct got_object_id id1;
350 struct got_object_id id2;
351 struct got_object *obj1;
352 struct got_object *obj2;
353 struct got_tree_object *tree1;
354 struct got_tree_object *tree2;
355 char hex[SHA1_DIGEST_STRING_LENGTH];
356 int i;
357 size_t len;
359 if (!got_parse_sha1_digest(id1.sha1, tree1_sha1))
360 return 0;
361 if (!got_parse_sha1_digest(id2.sha1, tree2_sha1))
362 return 0;
364 err = got_repo_open(&repo, repo_path);
365 if (err != NULL || repo == NULL)
366 return 0;
368 err = got_object_open(&obj1, repo, &id1);
369 if (err != NULL || obj1 == NULL)
370 return 0;
371 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
372 return 0;
373 err = got_object_open(&obj2, repo, &id2);
374 if (err != NULL || obj2 == NULL)
375 return 0;
376 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
377 return 0;
379 err = got_object_tree_open(&tree1, repo, obj1);
380 if (err != NULL)
381 return 0;
383 err = got_object_tree_open(&tree2, repo, obj2);
384 if (err != NULL)
385 return 0;
387 test_printf("\n");
388 got_diff_tree(tree1, tree2, repo);
389 test_printf("\n");
391 got_object_tree_close(tree1);
392 got_object_tree_close(tree2);
393 got_object_close(obj1);
394 got_object_close(obj2);
395 got_repo_close(repo);
396 return (err == NULL);
399 #define RUN_TEST(expr, name) \
400 { test_ok = (expr); \
401 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
402 failure = (failure || !test_ok); }
405 void
406 usage(void)
408 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
411 int
412 main(int argc, char *argv[])
414 int test_ok = 0, failure = 0;
415 const char *repo_path;
416 int ch;
417 int vflag = 0;
419 while ((ch = getopt(argc, argv, "v")) != -1) {
420 switch (ch) {
421 case 'v':
422 verbose = 1;
423 break;
424 default:
425 usage();
426 return 1;
429 argc -= optind;
430 argv += optind;
432 if (argc == 0)
433 repo_path = GOT_REPO_PATH;
434 else if (argc == 1)
435 repo_path = argv[1];
436 else {
437 usage();
438 return 1;
441 RUN_TEST(repo_read_tree(repo_path), "read_tree");
442 RUN_TEST(repo_read_log(repo_path), "read_log");
443 RUN_TEST(repo_read_blob(repo_path), "read_blob");
444 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
445 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
447 return failure ? 1 : 0;