2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/queue.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_reference.h"
32 #include "got_repository.h"
34 #include "got_opentemp.h"
36 #include "got_lib_path.h"
39 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 #define GOT_REPO_PATH "../../../"
47 test_printf(char *fmt, ...)
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)
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);
80 static const struct got_error *
81 print_tree_object(struct got_object_id *id, char *parent,
82 struct got_repository *repo)
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);
93 entries = got_object_tree_get_entries(tree);
94 SIMPLEQ_FOREACH(te, &entries->head, entry) {
98 err = got_object_id_str(&hex, te->id);
102 if (!S_ISDIR(te->mode)) {
103 test_printf("%s %s/%s\n", hex, parent, te->name);
107 test_printf("%s %s/%s\n", hex, parent, te->name);
110 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
111 err = got_error_from_errno();
115 err = print_tree_object(te->id, next_parent, repo);
121 got_object_tree_close(tree);
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;
132 const struct got_error *err;
135 err = got_object_open_as_commit(&commit, repo, id);
139 err = got_object_id_str(&buf, id);
141 got_object_commit_close(commit);
144 test_printf("tree: %s\n", 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);
152 got_object_commit_close(commit);
155 test_printf("%s\n", 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));
165 got_object_commit_close(commit);
168 if (obj_type == GOT_OBJ_TYPE_TREE)
171 err = print_parent_commits(commit, repo);
172 got_object_commit_close(commit);
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;
186 err = got_repo_open(&repo, repo_path);
187 if (err != NULL || repo == NULL)
189 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
190 if (err != NULL || head_ref == NULL)
192 err = got_ref_resolve(&id, repo, head_ref);
193 if (err != NULL || head_ref == NULL)
195 err = got_object_id_str(&buf, id);
198 test_printf("HEAD is at %s\n", buf);
200 err = print_commit_object(id, repo);
204 got_ref_close(head_ref);
205 got_repo_close(repo);
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)
220 err = got_object_resolve_id_str(&id, repo, tree_sha1);
224 print_tree_object(id, "", repo);
227 got_repo_close(repo);
228 return (err == NULL);
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;
242 err = got_repo_open(&repo, repo_path);
243 if (err != NULL || repo == NULL)
245 err = got_object_resolve_id_str(&id, repo, blob_sha1);
248 err = got_object_open_as_blob(&blob, repo, id, 64);
254 const uint8_t *buf = got_object_blob_get_read_buf(blob);
255 err = got_object_blob_read_block(&len, blob);
258 for (i = 0; i < len; i++)
259 test_printf("%c", buf[i]);
263 got_object_blob_close(blob);
264 got_repo_close(repo);
265 return (err == NULL);
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;
282 const char delim[3] = {'\0', '\0', '\0'};
283 const char *expected_output[] = {
284 "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
285 "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
287 " .PATH:${.CURDIR}/../../lib",
289 " PROG = repository_test",
290 "-SRCS = path.c repository.c error.c refs.c repository_test.c",
291 "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
293 " CPPFLAGS = -I${.CURDIR}/../../include",
295 "+LDADD = -lutil -lz",
300 err = got_repo_open(&repo, repo_path);
301 if (err != NULL || repo == NULL)
304 err = got_object_resolve_id_str(&id1, repo, blob1_sha1);
308 err = got_object_resolve_id_str(&id2, repo, blob2_sha1);
312 err = got_object_open_as_blob(&blob1, repo, id1, 512);
316 err = got_object_open_as_blob(&blob2, repo, id2, 512);
321 outfile = got_opentemp();
324 got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
327 while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
330 if (i < nitems(expected_output) &&
331 strcmp(line, expected_output[i]) != 0) {
332 test_printf("diff output mismatch; expected: '%s'\n",
340 if (i != nitems(expected_output) + 1) {
341 test_printf("number of lines expected: %d; actual: %d\n",
342 nitems(expected_output), i - 1);
346 got_object_blob_close(blob1);
347 got_object_blob_close(blob2);
348 got_repo_close(repo);
349 return (err == NULL);
353 repo_diff_tree(const char *repo_path)
355 const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
356 const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
357 const struct got_error *err;
358 struct got_repository *repo;
359 struct got_object_id *id1;
360 struct got_object_id *id2;
361 struct got_tree_object *tree1;
362 struct got_tree_object *tree2;
365 err = got_repo_open(&repo, repo_path);
366 if (err != NULL || repo == NULL)
369 err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
372 err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
376 err = got_object_open_as_tree(&tree1, repo, id1);
380 err = got_object_open_as_tree(&tree2, repo, id2);
385 outfile = fopen("/dev/null", "w+");
391 got_diff_tree(tree1, tree2, "", "", 3, repo, outfile);
394 got_object_tree_close(tree1);
395 got_object_tree_close(tree2);
396 got_repo_close(repo);
397 return (err == NULL);
400 #define RUN_TEST(expr, name) \
401 { test_ok = (expr); \
402 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
403 failure = (failure || !test_ok); }
409 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
413 main(int argc, char *argv[])
415 int test_ok = 0, failure = 0;
416 const char *repo_path;
420 if (pledge("stdio rpath wpath cpath proc exec sendfd", NULL) == -1)
424 while ((ch = getopt(argc, argv, "v")) != -1) {
438 repo_path = GOT_REPO_PATH;
446 RUN_TEST(repo_read_tree(repo_path), "read_tree");
447 RUN_TEST(repo_read_log(repo_path), "read_log");
448 RUN_TEST(repo_read_blob(repo_path), "read_blob");
449 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
450 RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
452 return failure ? 1 : 0;