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/stat.h>
18 #include <sys/queue.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sha1.h>
23 #include <zlib.h>
25 #include "got_error.h"
26 #include "got_object.h"
27 #include "got_refs.h"
28 #include "got_repository.h"
29 #include "got_sha1.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)
42 {
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);
49 if (err != NULL)
50 return err;
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);
55 }
57 return NULL;
58 }
60 static const struct got_error *
61 print_tree_object(struct got_object *obj, char *parent,
62 struct got_repository *repo)
63 {
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);
70 if (err != NULL)
71 return err;
73 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
74 struct got_object *treeobj;
75 char *next_parent;
77 if (!S_ISDIR(te->mode)) {
78 printf("%s %s/%s\n",
79 got_object_id_str(&te->id, hex, sizeof(hex)),
80 parent, te->name);
81 continue;
82 }
83 printf("%s %s/%s\n",
84 got_object_id_str(&te->id, hex, sizeof(hex)),
85 parent, te->name);
87 err = got_object_open(&treeobj, repo, &te->id);
88 if (err != NULL)
89 break;
91 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
92 err = got_error(GOT_ERR_OBJ_TYPE);
93 got_object_close(treeobj);
94 break;
95 }
97 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
98 err = got_error(GOT_ERR_NO_MEM);
99 got_object_close(treeobj);
100 break;
103 err = print_tree_object(treeobj, next_parent, repo);
104 free(next_parent);
105 if (err) {
106 got_object_close(treeobj);
107 break;
110 got_object_close(treeobj);
113 got_object_tree_close(tree);
114 return err;
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);
127 if (err != NULL)
128 return err;
130 printf("tree: %s\n",
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) {
134 printf("%s\n",
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);
142 if (err != NULL)
143 return err;
144 if (treeobj->type == GOT_OBJ_TYPE_TREE) {
145 print_tree_object(treeobj, "", repo);
146 printf("\n");
148 got_object_close(treeobj);
150 err = print_parent_commits(commit, repo);
151 got_object_commit_close(commit);
153 return err;
156 static int
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];
165 int ret;
167 err = got_repo_open(&repo, repo_path);
168 if (err != NULL || repo == NULL)
169 return 0;
170 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
171 if (err != NULL || head_ref == NULL)
172 return 0;
173 err = got_ref_resolve(&id, repo, head_ref);
174 if (err != NULL || head_ref == NULL)
175 return 0;
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)
179 return 0;
180 if (obj->type == GOT_OBJ_TYPE_COMMIT)
181 print_commit_object(obj, repo);
182 got_object_close(obj);
183 free(id);
184 got_ref_close(head_ref);
185 got_repo_close(repo);
186 return 1;
189 static int
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];
199 int i;
200 size_t len;
202 if (!got_parse_sha1_digest(id.sha1, blob_sha1))
203 return 0;
205 err = got_repo_open(&repo, repo_path);
206 if (err != NULL || repo == NULL)
207 return 0;
208 err = got_object_open(&obj, repo, &id);
209 if (err != NULL || obj == NULL)
210 return 0;
211 if (obj->type != GOT_OBJ_TYPE_BLOB)
212 return 0;
214 err = got_object_blob_open(&blob, repo, obj, 64);
215 if (err != NULL)
216 return 0;
218 putchar('\n');
219 do {
220 err = got_object_blob_read_block(blob, &len);
221 if (err)
222 break;
223 for (i = 0; i < len; i++)
224 putchar(blob->zb.outbuf[i]);
225 } while (len != 0);
226 putchar('\n');
228 got_object_blob_close(blob);
229 got_object_close(obj);
230 got_repo_close(repo);
231 return (err == NULL);
234 static int
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];
248 int i;
249 size_t len;
251 if (!got_parse_sha1_digest(id1.sha1, blob1_sha1))
252 return 0;
253 if (!got_parse_sha1_digest(id2.sha1, blob2_sha1))
254 return 0;
256 err = got_repo_open(&repo, repo_path);
257 if (err != NULL || repo == NULL)
258 return 0;
260 err = got_object_open(&obj1, repo, &id1);
261 if (err != NULL || obj1 == NULL)
262 return 0;
263 if (obj1->type != GOT_OBJ_TYPE_BLOB)
264 return 0;
265 err = got_object_open(&obj2, repo, &id2);
266 if (err != NULL || obj2 == NULL)
267 return 0;
268 if (obj2->type != GOT_OBJ_TYPE_BLOB)
269 return 0;
271 err = got_object_blob_open(&blob1, repo, obj1, 512);
272 if (err != NULL)
273 return 0;
275 err = got_object_blob_open(&blob2, repo, obj2, 512);
276 if (err != NULL)
277 return 0;
279 putchar('\n');
280 got_diff_blob(blob1, blob2, repo);
281 putchar('\n');
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);
291 int
292 main(int argc, const char *argv[])
294 int failure = 0;
295 const char *repo_path;
297 if (argc == 1)
298 repo_path = GOT_REPO_PATH;
299 else if (argc == 2)
300 repo_path = argv[1];
301 else {
302 fprintf(stderr, "usage: repository_test [REPO_PATH]\n");
303 return 1;
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;