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 <util.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <err.h>
27 #include <unistd.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_reference.h"
32 #include "got_repository.h"
33 #include "got_diff.h"
34 #include "got_opentemp.h"
36 #include "got_lib_path.h"
38 #ifndef nitems
39 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 #endif
42 #define GOT_REPO_PATH "../../../"
44 static int verbose;
46 void
47 test_printf(char *fmt, ...)
48 {
49 va_list ap;
51 if (!verbose)
52 return;
54 va_start(ap, fmt);
55 vprintf(fmt, ap);
56 va_end(ap);
57 }
59 static const struct got_error *
60 print_commit_object(struct got_object_id *, struct got_repository *);
62 static const struct got_error *
63 print_parent_commits(struct got_commit_object *commit,
64 struct got_repository *repo)
65 {
66 const struct got_object_id_queue *parent_ids;
67 struct got_object_qid *qid;
68 const struct got_error *err = NULL;
70 parent_ids = got_object_commit_get_parent_ids(commit);
71 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
72 err = print_commit_object(qid->id, repo);
73 if (err)
74 break;
75 }
77 return err;
78 }
80 static const struct got_error *
81 print_tree_object(struct got_object_id *id, char *parent,
82 struct got_repository *repo)
83 {
84 struct got_tree_object *tree;
85 const struct got_tree_entries *entries;
86 struct got_tree_entry *te;
87 const struct got_error *err;
89 err = got_object_open_as_tree(&tree, repo, id);
90 if (err != NULL)
91 return err;
93 entries = got_object_tree_get_entries(tree);
94 SIMPLEQ_FOREACH(te, &entries->head, entry) {
95 char *next_parent;
96 char *hex;
98 err = got_object_id_str(&hex, te->id);
99 if (err)
100 break;
102 if (!S_ISDIR(te->mode)) {
103 test_printf("%s %s/%s\n", hex, parent, te->name);
104 free(hex);
105 continue;
107 test_printf("%s %s/%s\n", hex, parent, te->name);
108 free(hex);
110 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
111 err = got_error_from_errno();
112 break;
115 err = print_tree_object(te->id, next_parent, repo);
116 free(next_parent);
117 if (err)
118 break;
121 got_object_tree_close(tree);
122 return err;
125 static const struct got_error *
126 print_commit_object(struct got_object_id *id, struct got_repository *repo)
128 struct got_commit_object *commit;
129 const struct got_object_id_queue *parent_ids;
130 struct got_object_qid *qid;
131 char *buf;
132 const struct got_error *err;
133 int obj_type;
135 err = got_object_open_as_commit(&commit, repo, id);
136 if (err)
137 return err;
139 err = got_object_id_str(&buf, id);
140 if (err) {
141 got_object_commit_close(commit);
142 return err;
144 test_printf("tree: %s\n", buf);
145 free(buf);
146 test_printf("parent%s: ",
147 (got_object_commit_get_nparents(commit) == 1) ? "" : "s");
148 parent_ids = got_object_commit_get_parent_ids(commit);
149 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
150 err = got_object_id_str(&buf, qid->id);
151 if (err) {
152 got_object_commit_close(commit);
153 return err;
155 test_printf("%s\n", buf);
156 free(buf);
158 test_printf("author: %s\n", got_object_commit_get_author(commit));
159 test_printf("committer: %s\n", got_object_commit_get_committer(commit));
160 test_printf("log: %s\n", got_object_commit_get_logmsg(commit));
162 err = got_object_get_type(&obj_type, repo,
163 got_object_commit_get_tree_id(commit));
164 if (err != NULL) {
165 got_object_commit_close(commit);
166 return err;
168 if (obj_type == GOT_OBJ_TYPE_TREE)
169 test_printf("\n");
171 err = print_parent_commits(commit, repo);
172 got_object_commit_close(commit);
174 return err;
177 static int
178 repo_read_log(const char *repo_path)
180 const struct got_error *err;
181 struct got_repository *repo;
182 struct got_reference *head_ref;
183 struct got_object_id *id;
184 char *buf;
186 err = got_repo_open(&repo, repo_path);
187 if (err != NULL || repo == NULL)
188 return 0;
189 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
190 if (err != NULL || head_ref == NULL)
191 return 0;
192 err = got_ref_resolve(&id, repo, head_ref);
193 if (err != NULL || head_ref == NULL)
194 return 0;
195 err = got_object_id_str(&buf, id);
196 if (err != NULL)
197 return 0;
198 test_printf("HEAD is at %s\n", buf);
199 free(buf);
200 err = print_commit_object(id, repo);
201 if (err)
202 return 0;
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;
217 err = got_repo_open(&repo, repo_path);
218 if (err != NULL || repo == NULL)
219 return 0;
220 err = got_object_resolve_id_str(&id, repo, tree_sha1);
221 if (err != NULL)
222 return 0;
224 print_tree_object(id, "", repo);
225 test_printf("\n");
227 got_repo_close(repo);
228 return (err == NULL);
231 static int
232 repo_read_blob(const char *repo_path)
234 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
235 const struct got_error *err;
236 struct got_repository *repo;
237 struct got_object_id *id;
238 struct got_blob_object *blob;
239 int i;
240 size_t len;
242 err = got_repo_open(&repo, repo_path);
243 if (err != NULL || repo == NULL)
244 return 0;
245 err = got_object_resolve_id_str(&id, repo, blob_sha1);
246 if (err != NULL)
247 return 0;
248 err = got_object_open_as_blob(&blob, repo, id, 64);
249 if (err != NULL)
250 return 0;
252 test_printf("\n");
253 do {
254 const uint8_t *buf = got_object_blob_get_read_buf(blob);
255 err = got_object_blob_read_block(&len, blob);
256 if (err)
257 break;
258 for (i = 0; i < len; i++)
259 test_printf("%c", buf[i]);
260 } while (len != 0);
261 test_printf("\n");
263 got_object_blob_close(blob);
264 got_repo_close(repo);
265 return (err == NULL);
268 static int
269 repo_diff_blob(const char *repo_path)
271 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
272 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
273 const struct got_error *err;
274 struct got_repository *repo;
275 struct got_object_id *id1, *id2;
276 struct got_blob_object *blob1;
277 struct got_blob_object *blob2;
278 FILE *outfile;
279 int i;
280 char *line;
281 size_t len;
282 const char delim[3] = {'\0', '\0', '\0'};
283 const char *expected_output[] = {
284 "blob - 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
285 "blob + de7eb21b21c7823a753261aadf7cba35c9580fbf",
286 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
287 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
288 "@@ -1,10 +1,10 @@",
289 " .PATH:${.CURDIR}/../../lib",
290 " ",
291 " PROG = repository_test",
292 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
293 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
294 " ",
295 " CPPFLAGS = -I${.CURDIR}/../../include",
296 "-LDADD = -lutil",
297 "+LDADD = -lutil -lz",
298 " ",
299 " NOMAN = yes"
300 };
302 err = got_repo_open(&repo, repo_path);
303 if (err != NULL || repo == NULL)
304 return 0;
306 err = got_object_resolve_id_str(&id1, repo, blob1_sha1);
307 if (err != NULL)
308 return 0;
310 err = got_object_resolve_id_str(&id2, repo, blob2_sha1);
311 if (err != NULL)
312 return 0;
314 err = got_object_open_as_blob(&blob1, repo, id1, 512);
315 if (err != NULL)
316 return 0;
318 err = got_object_open_as_blob(&blob2, repo, id2, 512);
319 if (err != NULL)
320 return 0;
322 test_printf("\n");
323 outfile = got_opentemp();
324 if (outfile == NULL)
325 return 0;
326 got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
327 rewind(outfile);
328 i = 0;
329 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
330 test_printf(line);
331 test_printf("\n");
332 if (i < nitems(expected_output) &&
333 strcmp(line, expected_output[i]) != 0) {
334 test_printf("diff output mismatch; expected: '%s'\n",
335 expected_output[i]);
336 return 0;
338 i++;
340 fclose(outfile);
341 test_printf("\n");
342 if (i != nitems(expected_output) + 1) {
343 test_printf("number of lines expected: %d; actual: %d\n",
344 nitems(expected_output), i - 1);
345 return 0;
348 got_object_blob_close(blob1);
349 got_object_blob_close(blob2);
350 got_repo_close(repo);
351 return (err == NULL);
354 static int
355 repo_diff_tree(const char *repo_path)
357 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
358 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
359 const struct got_error *err;
360 struct got_repository *repo;
361 struct got_object_id *id1;
362 struct got_object_id *id2;
363 struct got_tree_object *tree1;
364 struct got_tree_object *tree2;
365 FILE *outfile;
367 err = got_repo_open(&repo, repo_path);
368 if (err != NULL || repo == NULL)
369 return 0;
371 err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
372 if (err != NULL)
373 return 0;
374 err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
375 if (err != NULL)
376 return 0;
378 err = got_object_open_as_tree(&tree1, repo, id1);
379 if (err != NULL)
380 return 0;
382 err = got_object_open_as_tree(&tree2, repo, id2);
383 if (err != NULL)
384 return 0;
386 if (!verbose) {
387 outfile = fopen("/dev/null", "w+");
388 if (outfile == NULL)
389 return 0;
390 } else
391 outfile = stdout;
392 test_printf("\n");
393 got_diff_tree(tree1, tree2, "", "", 3, repo, outfile);
394 test_printf("\n");
396 got_object_tree_close(tree1);
397 got_object_tree_close(tree2);
398 got_repo_close(repo);
399 return (err == NULL);
402 #define RUN_TEST(expr, name) \
403 { test_ok = (expr); \
404 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
405 failure = (failure || !test_ok); }
408 void
409 usage(void)
411 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
414 int
415 main(int argc, char *argv[])
417 int test_ok = 0, failure = 0;
418 const char *repo_path;
419 int ch;
421 #ifndef PROFILE
422 if (pledge("stdio rpath wpath cpath proc exec sendfd", NULL) == -1)
423 err(1, "pledge");
424 #endif
426 while ((ch = getopt(argc, argv, "v")) != -1) {
427 switch (ch) {
428 case 'v':
429 verbose = 1;
430 break;
431 default:
432 usage();
433 return 1;
436 argc -= optind;
437 argv += optind;
439 if (argc == 0)
440 repo_path = GOT_REPO_PATH;
441 else if (argc == 1)
442 repo_path = argv[0];
443 else {
444 usage();
445 return 1;
448 RUN_TEST(repo_read_tree(repo_path), "read_tree");
449 RUN_TEST(repo_read_log(repo_path), "read_log");
450 RUN_TEST(repo_read_blob(repo_path), "read_blob");
451 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
452 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
454 return failure ? 1 : 0;