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"
30 #include "got_diff.h"
32 #define RUN_TEST(expr, name) \
33 if (!(expr)) { printf("test %s failed", (name)); failure = 1; }
35 #define GOT_REPO_PATH "../../../"
37 static const struct got_error *
38 print_commit_object(struct got_object *, struct got_repository *);
40 static const struct got_error *
41 print_parent_commits(struct got_commit_object *commit,
42 struct got_repository *repo)
43 {
44 struct got_parent_id *pid;
45 const struct got_error *err;
46 struct got_object *obj;
48 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
49 err = got_object_open(&obj, repo, &pid->id);
50 if (err != NULL)
51 return err;
52 if (obj->type != GOT_OBJ_TYPE_COMMIT)
53 return got_error(GOT_ERR_OBJ_TYPE);
54 print_commit_object(obj, repo);
55 got_object_close(obj);
56 }
58 return NULL;
59 }
61 static const struct got_error *
62 print_tree_object(struct got_object *obj, char *parent,
63 struct got_repository *repo)
64 {
65 struct got_tree_object *tree;
66 struct got_tree_entry *te;
67 const struct got_error *err;
68 char hex[SHA1_DIGEST_STRING_LENGTH];
70 err = got_object_tree_open(&tree, repo, obj);
71 if (err != NULL)
72 return err;
74 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
75 struct got_object *treeobj;
76 char *next_parent;
78 if (!S_ISDIR(te->mode)) {
79 printf("%s %s/%s\n",
80 got_object_id_str(&te->id, hex, sizeof(hex)),
81 parent, te->name);
82 continue;
83 }
84 printf("%s %s/%s\n",
85 got_object_id_str(&te->id, hex, sizeof(hex)),
86 parent, te->name);
88 err = got_object_open(&treeobj, repo, &te->id);
89 if (err != NULL)
90 break;
92 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
93 err = got_error(GOT_ERR_OBJ_TYPE);
94 got_object_close(treeobj);
95 break;
96 }
98 if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
99 err = got_error(GOT_ERR_NO_MEM);
100 got_object_close(treeobj);
101 break;
104 err = print_tree_object(treeobj, next_parent, repo);
105 free(next_parent);
106 if (err) {
107 got_object_close(treeobj);
108 break;
111 got_object_close(treeobj);
114 got_object_tree_close(tree);
115 return err;
118 static const struct got_error *
119 print_commit_object(struct got_object *obj, struct got_repository *repo)
121 struct got_commit_object *commit;
122 struct got_parent_id *pid;
123 char buf[SHA1_DIGEST_STRING_LENGTH];
124 const struct got_error *err;
125 struct got_object* treeobj;
127 err = got_object_commit_open(&commit, repo, obj);
128 if (err != NULL)
129 return err;
131 printf("tree: %s\n",
132 got_object_id_str(&commit->tree_id, buf, sizeof(buf)));
133 printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
134 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
135 printf("%s\n",
136 got_object_id_str(&pid->id, buf, sizeof(buf)));
138 printf("author: %s\n", commit->author);
139 printf("committer: %s\n", commit->committer);
140 printf("log: %s\n", commit->logmsg);
142 err = got_object_open(&treeobj, repo, &commit->tree_id);
143 if (err != NULL)
144 return err;
145 if (treeobj->type == GOT_OBJ_TYPE_TREE) {
146 print_tree_object(treeobj, "", repo);
147 printf("\n");
149 got_object_close(treeobj);
151 err = print_parent_commits(commit, repo);
152 got_object_commit_close(commit);
154 return err;
157 static int
158 repo_read_log(const char *repo_path)
160 const struct got_error *err;
161 struct got_repository *repo;
162 struct got_reference *head_ref;
163 struct got_object_id *id;
164 struct got_object *obj;
165 char buf[SHA1_DIGEST_STRING_LENGTH];
166 int ret;
168 err = got_repo_open(&repo, repo_path);
169 if (err != NULL || repo == NULL)
170 return 0;
171 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
172 if (err != NULL || head_ref == NULL)
173 return 0;
174 err = got_ref_resolve(&id, repo, head_ref);
175 if (err != NULL || head_ref == NULL)
176 return 0;
177 printf("HEAD is at %s\n", got_object_id_str(id, buf, sizeof(buf)));
178 err = got_object_open(&obj, repo, id);
179 if (err != NULL || obj == NULL)
180 return 0;
181 if (obj->type == GOT_OBJ_TYPE_COMMIT)
182 print_commit_object(obj, repo);
183 got_object_close(obj);
184 free(id);
185 got_ref_close(head_ref);
186 got_repo_close(repo);
187 return 1;
190 static int
191 repo_read_blob(const char *repo_path)
193 const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
194 const struct got_error *err;
195 struct got_repository *repo;
196 struct got_object_id id;
197 struct got_object *obj;
198 struct got_blob_object *blob;
199 char hex[SHA1_DIGEST_STRING_LENGTH];
200 int i;
201 size_t len;
203 if (!got_parse_sha1_digest(id.sha1, blob_sha1))
204 return 0;
206 err = got_repo_open(&repo, repo_path);
207 if (err != NULL || repo == NULL)
208 return 0;
209 err = got_object_open(&obj, repo, &id);
210 if (err != NULL || obj == NULL)
211 return 0;
212 if (obj->type != GOT_OBJ_TYPE_BLOB)
213 return 0;
215 err = got_object_blob_open(&blob, repo, obj, 64);
216 if (err != NULL)
217 return 0;
219 putchar('\n');
220 do {
221 err = got_object_blob_read_block(blob, &len);
222 if (err)
223 break;
224 for (i = 0; i < len; i++)
225 putchar(blob->zb.outbuf[i]);
226 } while (len != 0);
227 putchar('\n');
229 got_object_blob_close(blob);
230 got_object_close(obj);
231 got_repo_close(repo);
232 return (err == NULL);
235 static int
236 repo_diff_blob(const char *repo_path)
238 const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
239 const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
240 const struct got_error *err;
241 struct got_repository *repo;
242 struct got_object_id id1;
243 struct got_object_id id2;
244 struct got_object *obj1;
245 struct got_object *obj2;
246 struct got_blob_object *blob1;
247 struct got_blob_object *blob2;
248 char hex[SHA1_DIGEST_STRING_LENGTH];
249 int i;
250 size_t len;
252 if (!got_parse_sha1_digest(id1.sha1, blob1_sha1))
253 return 0;
254 if (!got_parse_sha1_digest(id2.sha1, blob2_sha1))
255 return 0;
257 err = got_repo_open(&repo, repo_path);
258 if (err != NULL || repo == NULL)
259 return 0;
261 err = got_object_open(&obj1, repo, &id1);
262 if (err != NULL || obj1 == NULL)
263 return 0;
264 if (obj1->type != GOT_OBJ_TYPE_BLOB)
265 return 0;
266 err = got_object_open(&obj2, repo, &id2);
267 if (err != NULL || obj2 == NULL)
268 return 0;
269 if (obj2->type != GOT_OBJ_TYPE_BLOB)
270 return 0;
272 err = got_object_blob_open(&blob1, repo, obj1, 512);
273 if (err != NULL)
274 return 0;
276 err = got_object_blob_open(&blob2, repo, obj2, 512);
277 if (err != NULL)
278 return 0;
280 putchar('\n');
281 got_diff_blob(blob1, blob2, NULL, NULL, stdout);
282 putchar('\n');
284 got_object_blob_close(blob1);
285 got_object_blob_close(blob2);
286 got_object_close(obj1);
287 got_object_close(obj2);
288 got_repo_close(repo);
289 return (err == NULL);
292 int
293 main(int argc, const char *argv[])
295 int failure = 0;
296 const char *repo_path;
298 if (argc == 1)
299 repo_path = GOT_REPO_PATH;
300 else if (argc == 2)
301 repo_path = argv[1];
302 else {
303 fprintf(stderr, "usage: repository_test [REPO_PATH]\n");
304 return 1;
307 RUN_TEST(repo_read_log(repo_path), "read_log");
308 RUN_TEST(repo_read_blob(repo_path), "read_blob");
309 RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
311 return failure ? 1 : 0;