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 "../../../"
33 static const struct got_error *
34 print_commit_object(struct got_object *, struct got_repository *);
36 static const struct got_error *
37 print_parent_commits(struct got_commit_object *commit,
38 struct got_repository *repo)
40 struct got_parent_id *pid;
41 const struct got_error *err;
42 struct got_object *obj;
44 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
45 err = got_object_open(&obj, repo, &pid->id);
48 if (obj->type != GOT_OBJ_TYPE_COMMIT)
49 return got_error(GOT_ERR_OBJ_TYPE);
50 print_commit_object(obj, repo);
51 got_object_close(obj);
57 static const struct got_error *
58 print_commit_object(struct got_object *obj, struct got_repository *repo)
60 struct got_commit_object *commit;
61 struct got_parent_id *pid;
62 char buf[SHA1_DIGEST_STRING_LENGTH];
63 const struct got_error *err;
65 err = got_object_commit_open(&commit, repo, obj);
70 got_object_id_str(&commit->tree_id, buf, sizeof(buf)));
71 printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
72 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
74 got_object_id_str(&pid->id, buf, sizeof(buf)));
76 printf("author: %s\n", commit->author);
77 printf("committer: %s\n", commit->committer);
78 printf("log: %s\n", commit->logmsg);
80 err = print_parent_commits(commit, repo);
81 got_object_commit_close(commit);
87 repo_read_log(const char *repo_path)
89 const struct got_error *err;
90 struct got_repository *repo;
91 struct got_reference *head_ref;
92 struct got_object_id *id;
93 struct got_object *obj;
94 char buf[SHA1_DIGEST_STRING_LENGTH];
97 err = got_repo_open(&repo, repo_path);
98 if (err != NULL || repo == NULL)
100 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
101 if (err != NULL || head_ref == NULL)
103 err = got_ref_resolve(&id, repo, head_ref);
104 if (err != NULL || head_ref == NULL)
106 printf("HEAD is at %s\n", got_object_id_str(id, buf, sizeof(buf)));
107 err = got_object_open(&obj, repo, id);
108 if (err != NULL || obj == NULL)
110 printf("object type=%d size=%lu\n", obj->type, obj->size);
111 if (obj->type == GOT_OBJ_TYPE_COMMIT)
112 print_commit_object(obj, repo);
113 got_object_close(obj);
115 got_ref_close(head_ref);
116 got_repo_close(repo);
121 main(int argc, const char *argv[])
124 const char *repo_path;
127 repo_path = GOT_REPO_PATH;
131 fprintf(stderr, "usage: repository_test [REPO_PATH]\n");
135 RUN_TEST(repo_read_log(repo_path), "read_log");
137 return failure ? 1 : 0;