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/queue.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sha1.h>
23 #include "got_error.h"
24 #include "got_object.h"
25 #include "got_refs.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)
39 {
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);
46 if (err != NULL)
47 return err;
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);
52 }
54 return NULL;
55 }
57 static const struct got_error *
58 print_commit_object(struct got_object *obj, struct got_repository *repo)
59 {
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);
66 if (err != NULL)
67 return err;
69 printf("tree: %s\n",
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) {
73 printf("%s\n",
74 got_object_id_str(&pid->id, buf, sizeof(buf)));
75 }
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);
83 return err;
84 }
86 static int
87 repo_read_log(const char *repo_path)
88 {
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];
95 int ret;
97 err = got_repo_open(&repo, repo_path);
98 if (err != NULL || repo == NULL)
99 return 0;
100 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
101 if (err != NULL || head_ref == NULL)
102 return 0;
103 err = got_ref_resolve(&id, repo, head_ref);
104 if (err != NULL || head_ref == NULL)
105 return 0;
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)
109 return 0;
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);
114 free(id);
115 got_ref_close(head_ref);
116 got_repo_close(repo);
117 return 1;
120 int
121 main(int argc, const char *argv[])
123 int failure = 0;
124 const char *repo_path;
126 if (argc == 1)
127 repo_path = GOT_REPO_PATH;
128 else if (argc == 2)
129 repo_path = argv[1];
130 else {
131 fprintf(stderr, "usage: repository_test [REPO_PATH]\n");
132 return 1;
135 RUN_TEST(repo_read_log(repo_path), "read_log");
137 return failure ? 1 : 0;