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_blob(const char *repo_path)
212 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
213 const struct got_error *err;
214 struct got_repository *repo;
215 struct got_object_id id;
216 struct got_object *obj;
217 struct got_blob_object *blob;
218 char hex[SHA1_DIGEST_STRING_LENGTH];
219 int i;
220 size_t len;
222 if (!got_parse_sha1_digest(id.sha1, blob_sha1))
223 return 0;
225 err = got_repo_open(&repo, repo_path);
226 if (err != NULL || repo == NULL)
227 return 0;
228 err = got_object_open(&obj, repo, &id);
229 if (err != NULL || obj == NULL)
230 return 0;
231 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
232 return 0;
234 err = got_object_blob_open(&blob, repo, obj, 64);
235 if (err != NULL)
236 return 0;
238 test_printf("\n");
239 do {
240 err = got_object_blob_read_block(blob, &len);
241 if (err)
242 break;
243 for (i = 0; i < len; i++)
244 test_printf("%c", blob->zb.outbuf[i]);
245 } while (len != 0);
246 test_printf("\n");
248 got_object_blob_close(blob);
249 got_object_close(obj);
250 got_repo_close(repo);
251 return (err == NULL);
254 static int
255 repo_diff_blob(const char *repo_path)
257 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
258 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
259 const struct got_error *err;
260 struct got_repository *repo;
261 struct got_object_id id1;
262 struct got_object_id id2;
263 struct got_object *obj1;
264 struct got_object *obj2;
265 struct got_blob_object *blob1;
266 struct got_blob_object *blob2;
267 char hex[SHA1_DIGEST_STRING_LENGTH];
268 int i;
269 size_t len;
271 if (!got_parse_sha1_digest(id1.sha1, blob1_sha1))
272 return 0;
273 if (!got_parse_sha1_digest(id2.sha1, blob2_sha1))
274 return 0;
276 err = got_repo_open(&repo, repo_path);
277 if (err != NULL || repo == NULL)
278 return 0;
280 err = got_object_open(&obj1, repo, &id1);
281 if (err != NULL || obj1 == NULL)
282 return 0;
283 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
284 return 0;
285 err = got_object_open(&obj2, repo, &id2);
286 if (err != NULL || obj2 == NULL)
287 return 0;
288 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
289 return 0;
291 err = got_object_blob_open(&blob1, repo, obj1, 512);
292 if (err != NULL)
293 return 0;
295 err = got_object_blob_open(&blob2, repo, obj2, 512);
296 if (err != NULL)
297 return 0;
299 test_printf("\n");
300 got_diff_blob(blob1, blob2, NULL, NULL, stdout);
301 test_printf("\n");
303 got_object_blob_close(blob1);
304 got_object_blob_close(blob2);
305 got_object_close(obj1);
306 got_object_close(obj2);
307 got_repo_close(repo);
308 return (err == NULL);
311 static int
312 repo_diff_tree(const char *repo_path)
314 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
315 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
316 const struct got_error *err;
317 struct got_repository *repo;
318 struct got_object_id id1;
319 struct got_object_id id2;
320 struct got_object *obj1;
321 struct got_object *obj2;
322 struct got_tree_object *tree1;
323 struct got_tree_object *tree2;
324 char hex[SHA1_DIGEST_STRING_LENGTH];
325 int i;
326 size_t len;
328 if (!got_parse_sha1_digest(id1.sha1, tree1_sha1))
329 return 0;
330 if (!got_parse_sha1_digest(id2.sha1, tree2_sha1))
331 return 0;
333 err = got_repo_open(&repo, repo_path);
334 if (err != NULL || repo == NULL)
335 return 0;
337 err = got_object_open(&obj1, repo, &id1);
338 if (err != NULL || obj1 == NULL)
339 return 0;
340 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
341 return 0;
342 err = got_object_open(&obj2, repo, &id2);
343 if (err != NULL || obj2 == NULL)
344 return 0;
345 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
346 return 0;
348 err = got_object_tree_open(&tree1, repo, obj1);
349 if (err != NULL)
350 return 0;
352 err = got_object_tree_open(&tree2, repo, obj2);
353 if (err != NULL)
354 return 0;
356 test_printf("\n");
357 got_diff_tree(tree1, tree2, repo);
358 test_printf("\n");
360 got_object_tree_close(tree1);
361 got_object_tree_close(tree2);
362 got_object_close(obj1);
363 got_object_close(obj2);
364 got_repo_close(repo);
365 return (err == NULL);
368 #define RUN_TEST(expr, name) \
369 { test_ok = (expr); \
370 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
371 failure = (failure || !test_ok); }
374 void
375 usage(void)
377 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
380 int
381 main(int argc, char *argv[])
383 int test_ok = 0, failure = 0;
384 const char *repo_path;
385 int ch;
386 int vflag = 0;
388 while ((ch = getopt(argc, argv, "v")) != -1) {
389 switch (ch) {
390 case 'v':
391 verbose = 1;
392 break;
393 default:
394 usage();
395 return 1;
398 argc -= optind;
399 argv += optind;
401 if (argc == 0)
402 repo_path = GOT_REPO_PATH;
403 else if (argc == 1)
404 repo_path = argv[1];
405 else {
406 usage();
407 return 1;
410 RUN_TEST(repo_read_log(repo_path), "read_log");
411 RUN_TEST(repo_read_blob(repo_path), "read_blob");
412 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
413 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
415 return failure ? 1 : 0;