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>
25 #include "got_error.h"
26 #include "got_object.h"
28 #include "got_repository.h"
31 #define RUN_TEST(expr, name) \
32 if (!(expr)) { printf("test %s failed", (name)); failure = 1; }
34 #define GOT_REPO_PATH "../../../"
36 static const struct got_error *
37 print_commit_object(struct got_object *, struct got_repository *);
39 static const struct got_error *
40 print_parent_commits(struct got_commit_object *commit,
41 struct got_repository *repo)
43 struct got_parent_id *pid;
44 const struct got_error *err;
45 struct got_object *obj;
47 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
48 err = got_object_open(&obj, repo, &pid->id);
51 if (obj->type != GOT_OBJ_TYPE_COMMIT)
52 return got_error(GOT_ERR_OBJ_TYPE);
53 print_commit_object(obj, repo);
54 got_object_close(obj);
60 static const struct got_error *
61 print_tree_object(struct got_object *obj, char *parent,
62 struct got_repository *repo)
64 struct got_tree_object *tree;
65 struct got_tree_entry *te;
66 const struct got_error *err;
67 char hex[SHA1_DIGEST_STRING_LENGTH];
69 err = got_object_tree_open(&tree, repo, obj);
73 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
74 struct got_object *treeobj;
77 if (!S_ISDIR(te->mode)) {
79 got_object_id_str(&te->id, hex, sizeof(hex)),
84 got_object_id_str(&te->id, hex, sizeof(hex)),
87 err = got_object_open(&treeobj, repo, &te->id);
91 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
92 err = got_error(GOT_ERR_OBJ_TYPE);
93 got_object_close(treeobj);
97 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
98 err = got_error(GOT_ERR_NO_MEM);
99 got_object_close(treeobj);
103 err = print_tree_object(treeobj, next_parent, repo);
106 got_object_close(treeobj);
110 got_object_close(treeobj);
113 got_object_tree_close(tree);
117 static const struct got_error *
118 print_commit_object(struct got_object *obj, struct got_repository *repo)
120 struct got_commit_object *commit;
121 struct got_parent_id *pid;
122 char buf[SHA1_DIGEST_STRING_LENGTH];
123 const struct got_error *err;
124 struct got_object* treeobj;
126 err = got_object_commit_open(&commit, repo, obj);
131 got_object_id_str(&commit->tree_id, buf, sizeof(buf)));
132 printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
133 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
135 got_object_id_str(&pid->id, buf, sizeof(buf)));
137 printf("author: %s\n", commit->author);
138 printf("committer: %s\n", commit->committer);
139 printf("log: %s\n", commit->logmsg);
141 err = got_object_open(&treeobj, repo, &commit->tree_id);
144 if (treeobj->type == GOT_OBJ_TYPE_TREE) {
145 print_tree_object(treeobj, "", repo);
148 got_object_close(treeobj);
150 err = print_parent_commits(commit, repo);
151 got_object_commit_close(commit);
157 repo_read_log(const char *repo_path)
159 const struct got_error *err;
160 struct got_repository *repo;
161 struct got_reference *head_ref;
162 struct got_object_id *id;
163 struct got_object *obj;
164 char buf[SHA1_DIGEST_STRING_LENGTH];
167 err = got_repo_open(&repo, repo_path);
168 if (err != NULL || repo == NULL)
170 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
171 if (err != NULL || head_ref == NULL)
173 err = got_ref_resolve(&id, repo, head_ref);
174 if (err != NULL || head_ref == NULL)
176 printf("HEAD is at %s\n", got_object_id_str(id, buf, sizeof(buf)));
177 err = got_object_open(&obj, repo, id);
178 if (err != NULL || obj == NULL)
180 if (obj->type == GOT_OBJ_TYPE_COMMIT)
181 print_commit_object(obj, repo);
182 got_object_close(obj);
184 got_ref_close(head_ref);
185 got_repo_close(repo);
190 repo_read_blob(const char *repo_path)
192 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
193 const struct got_error *err;
194 struct got_repository *repo;
195 struct got_object_id id;
196 struct got_object *obj;
197 struct got_blob_object *blob;
198 char hex[SHA1_DIGEST_STRING_LENGTH];
202 if (!got_parse_sha1_digest(id.sha1, blob_sha1))
205 err = got_repo_open(&repo, repo_path);
206 if (err != NULL || repo == NULL)
208 err = got_object_open(&obj, repo, &id);
209 if (err != NULL || obj == NULL)
211 if (obj->type != GOT_OBJ_TYPE_BLOB)
214 err = got_object_blob_open(&blob, repo, obj, 64);
220 err = got_object_blob_read_block(blob, &len);
223 for (i = 0; i < len; i++)
224 putchar(blob->zb.outbuf[i]);
228 got_object_blob_close(blob);
229 got_object_close(obj);
230 got_repo_close(repo);
231 return (err == NULL);
235 repo_diff_blob(const char *repo_path)
237 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
238 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
239 const struct got_error *err;
240 struct got_repository *repo;
241 struct got_object_id id1;
242 struct got_object_id id2;
243 struct got_object *obj1;
244 struct got_object *obj2;
245 struct got_blob_object *blob1;
246 struct got_blob_object *blob2;
247 char hex[SHA1_DIGEST_STRING_LENGTH];
251 if (!got_parse_sha1_digest(id1.sha1, blob1_sha1))
253 if (!got_parse_sha1_digest(id2.sha1, blob2_sha1))
256 err = got_repo_open(&repo, repo_path);
257 if (err != NULL || repo == NULL)
260 err = got_object_open(&obj1, repo, &id1);
261 if (err != NULL || obj1 == NULL)
263 if (obj1->type != GOT_OBJ_TYPE_BLOB)
265 err = got_object_open(&obj2, repo, &id2);
266 if (err != NULL || obj2 == NULL)
268 if (obj2->type != GOT_OBJ_TYPE_BLOB)
271 err = got_object_blob_open(&blob1, repo, obj1, 512);
275 err = got_object_blob_open(&blob2, repo, obj2, 512);
280 got_diff_blob(blob1, blob2, repo);
283 got_object_blob_close(blob1);
284 got_object_blob_close(blob2);
285 got_object_close(obj1);
286 got_object_close(obj2);
287 got_repo_close(repo);
288 return (err == NULL);
292 main(int argc, const char *argv[])
295 const char *repo_path;
298 repo_path = GOT_REPO_PATH;
302 fprintf(stderr, "usage: repository_test [REPO_PATH]\n");
306 RUN_TEST(repo_read_log(repo_path), "read_log");
307 RUN_TEST(repo_read_blob(repo_path), "read_blob");
308 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
310 return failure ? 1 : 0;