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>
27 #include "got_error.h"
28 #include "got_object.h"
30 #include "got_repository.h"
35 #define GOT_REPO_PATH "../../../"
40 test_printf(char *fmt, ...)
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)
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);
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);
76 static const struct got_error *
77 print_tree_object(struct got_object *obj, char *parent,
78 struct got_repository *repo)
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);
89 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
90 struct got_object *treeobj;
93 if (!S_ISDIR(te->mode)) {
94 test_printf("%s %s/%s\n",
95 got_object_id_str(&te->id, hex, sizeof(hex)),
99 test_printf("%s %s/%s\n",
100 got_object_id_str(&te->id, hex, sizeof(hex)),
103 err = got_object_open(&treeobj, repo, &te->id);
107 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
108 err = got_error(GOT_ERR_OBJ_TYPE);
109 got_object_close(treeobj);
113 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
114 err = got_error(GOT_ERR_NO_MEM);
115 got_object_close(treeobj);
119 err = print_tree_object(treeobj, next_parent, repo);
122 got_object_close(treeobj);
126 got_object_close(treeobj);
129 got_object_tree_close(tree);
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);
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) {
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);
160 if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
161 print_tree_object(treeobj, "", repo);
164 got_object_close(treeobj);
166 err = print_parent_commits(commit, repo);
167 got_object_commit_close(commit);
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];
183 err = got_repo_open(&repo, repo_path);
184 if (err != NULL || repo == NULL)
186 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
187 if (err != NULL || head_ref == NULL)
189 err = got_ref_resolve(&id, repo, head_ref);
190 if (err != NULL || head_ref == NULL)
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)
196 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
197 err = print_commit_object(obj, repo);
202 got_object_close(obj);
204 got_ref_close(head_ref);
205 got_repo_close(repo);
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];
222 if (!got_parse_sha1_digest(id.sha1, blob_sha1))
225 err = got_repo_open(&repo, repo_path);
226 if (err != NULL || repo == NULL)
228 err = got_object_open(&obj, repo, &id);
229 if (err != NULL || obj == NULL)
231 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
234 err = got_object_blob_open(&blob, repo, obj, 64);
240 err = got_object_blob_read_block(blob, &len);
243 for (i = 0; i < len; i++)
244 test_printf("%c", blob->zb.outbuf[i]);
248 got_object_blob_close(blob);
249 got_object_close(obj);
250 got_repo_close(repo);
251 return (err == NULL);
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];
271 if (!got_parse_sha1_digest(id1.sha1, blob1_sha1))
273 if (!got_parse_sha1_digest(id2.sha1, blob2_sha1))
276 err = got_repo_open(&repo, repo_path);
277 if (err != NULL || repo == NULL)
280 err = got_object_open(&obj1, repo, &id1);
281 if (err != NULL || obj1 == NULL)
283 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
285 err = got_object_open(&obj2, repo, &id2);
286 if (err != NULL || obj2 == NULL)
288 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
291 err = got_object_blob_open(&blob1, repo, obj1, 512);
295 err = got_object_blob_open(&blob2, repo, obj2, 512);
300 got_diff_blob(blob1, blob2, NULL, NULL, stdout);
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);
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];
328 if (!got_parse_sha1_digest(id1.sha1, tree1_sha1))
330 if (!got_parse_sha1_digest(id2.sha1, tree2_sha1))
333 err = got_repo_open(&repo, repo_path);
334 if (err != NULL || repo == NULL)
337 err = got_object_open(&obj1, repo, &id1);
338 if (err != NULL || obj1 == NULL)
340 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
342 err = got_object_open(&obj2, repo, &id2);
343 if (err != NULL || obj2 == NULL)
345 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
348 err = got_object_tree_open(&tree1, repo, obj1);
352 err = got_object_tree_open(&tree2, repo, obj2);
357 got_diff_tree(tree1, tree2, repo);
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); }
377 fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
381 main(int argc, char *argv[])
383 int test_ok = 0, failure = 0;
384 const char *repo_path;
388 while ((ch = getopt(argc, argv, "v")) != -1) {
402 repo_path = GOT_REPO_PATH;
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;