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.
17 #include <sys/queue.h>
23 #include "got_error.h"
24 #include "got_object.h"
26 #include "got_repository.h"
28 #define RUN_TEST(expr, name) \
29 if (!(expr)) { printf("test %s failed", (name)); failure = 1; }
31 #define GOT_REPO_PATH "../../../"
34 repo_read_object_header(const char *repo_path)
36 const struct got_error *err;
37 struct got_repository *repo;
38 struct got_reference *head_ref;
39 struct got_object_id *id;
40 struct got_object *obj;
41 char buf[SHA1_DIGEST_STRING_LENGTH];
44 err = got_repo_open(&repo, repo_path);
45 if (err != NULL || repo == NULL)
47 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
48 if (err != NULL || head_ref == NULL)
50 err = got_ref_resolve(&id, repo, head_ref);
51 if (err != NULL || head_ref == NULL)
53 printf("HEAD is at %s\n", got_object_id_str(id, buf, sizeof(buf)));
54 err = got_object_open(&obj, repo, id);
55 if (err != NULL || obj == NULL)
57 printf("object type=%d size=%lu\n", obj->type, obj->size);
58 if (obj->type == GOT_OBJ_TYPE_COMMIT) {
59 struct got_commit_object *commit;
60 struct got_parent_id *pid;
62 err = got_object_commit_open(&commit, repo, obj);
63 if (err != NULL || commit == NULL)
66 got_object_id_str(&commit->tree_id, buf, sizeof(buf)));
67 printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
68 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
70 got_object_id_str(&pid->id, buf, sizeof(buf)));
72 printf("author: %s\n", commit->author);
73 printf("committer: %s\n", commit->committer);
74 printf("log: %s\n", commit->logmsg);
76 got_object_close(obj);
78 got_ref_close(head_ref);
84 main(int argc, const char *argv[])
87 const char *repo_path;
90 repo_path = GOT_REPO_PATH;
94 fprintf(stderr, "usage: repository_test [REPO_PATH]\n");
98 RUN_TEST(repo_read_object_header(repo_path), "read_object_header");
100 return failure ? 1 : 0;