Blame


1 7b19e0f1 2017-11-05 stsp /*
2 7b19e0f1 2017-11-05 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
18 d1cda826 2017-11-06 stsp #include <sys/queue.h>
19 d1cda826 2017-11-06 stsp
20 82f2fb69 2018-01-26 stsp #include <stdarg.h>
21 4027f31a 2017-11-04 stsp #include <stdio.h>
22 4027f31a 2017-11-04 stsp #include <stdlib.h>
23 82f2fb69 2018-01-26 stsp #include <string.h>
24 4027f31a 2017-11-04 stsp #include <sha1.h>
25 68482ea3 2017-11-27 stsp #include <zlib.h>
26 4027f31a 2017-11-04 stsp
27 4027f31a 2017-11-04 stsp #include "got_error.h"
28 11995603 2017-11-05 stsp #include "got_object.h"
29 4027f31a 2017-11-04 stsp #include "got_refs.h"
30 4027f31a 2017-11-04 stsp #include "got_repository.h"
31 68482ea3 2017-11-27 stsp #include "got_sha1.h"
32 f78b0693 2017-11-29 stsp #include "got_diff.h"
33 82f2fb69 2018-01-26 stsp #include "unistd.h"
34 4027f31a 2017-11-04 stsp
35 4027f31a 2017-11-04 stsp #define GOT_REPO_PATH "../../../"
36 4027f31a 2017-11-04 stsp
37 14545512 2018-01-26 stsp static int verbose;
38 82f2fb69 2018-01-26 stsp
39 82f2fb69 2018-01-26 stsp void
40 82f2fb69 2018-01-26 stsp test_printf(char *fmt, ...)
41 82f2fb69 2018-01-26 stsp {
42 82f2fb69 2018-01-26 stsp va_list ap;
43 82f2fb69 2018-01-26 stsp
44 82f2fb69 2018-01-26 stsp if (!verbose)
45 82f2fb69 2018-01-26 stsp return;
46 82f2fb69 2018-01-26 stsp
47 82f2fb69 2018-01-26 stsp va_start(ap, fmt);
48 82f2fb69 2018-01-26 stsp vprintf(fmt, ap);
49 82f2fb69 2018-01-26 stsp va_end(ap);
50 82f2fb69 2018-01-26 stsp }
51 82f2fb69 2018-01-26 stsp
52 bfab4d9a 2017-11-12 stsp static const struct got_error *
53 bfab4d9a 2017-11-12 stsp print_commit_object(struct got_object *, struct got_repository *);
54 1c852fbe 2017-11-12 stsp
55 1c852fbe 2017-11-12 stsp static const struct got_error *
56 bfab4d9a 2017-11-12 stsp print_parent_commits(struct got_commit_object *commit,
57 bfab4d9a 2017-11-12 stsp struct got_repository *repo)
58 bfab4d9a 2017-11-12 stsp {
59 bfab4d9a 2017-11-12 stsp struct got_parent_id *pid;
60 a37d050f 2018-01-26 stsp const struct got_error *err = NULL;
61 bfab4d9a 2017-11-12 stsp struct got_object *obj;
62 bfab4d9a 2017-11-12 stsp
63 bfab4d9a 2017-11-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
64 bfab4d9a 2017-11-12 stsp err = got_object_open(&obj, repo, &pid->id);
65 bfab4d9a 2017-11-12 stsp if (err != NULL)
66 bfab4d9a 2017-11-12 stsp return err;
67 b107e67f 2018-01-19 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
68 bfab4d9a 2017-11-12 stsp return got_error(GOT_ERR_OBJ_TYPE);
69 a37d050f 2018-01-26 stsp err = print_commit_object(obj, repo);
70 bfab4d9a 2017-11-12 stsp got_object_close(obj);
71 bfab4d9a 2017-11-12 stsp }
72 bfab4d9a 2017-11-12 stsp
73 a37d050f 2018-01-26 stsp return err;
74 bfab4d9a 2017-11-12 stsp }
75 bfab4d9a 2017-11-12 stsp
76 bfab4d9a 2017-11-12 stsp static const struct got_error *
77 f715ca7f 2017-11-27 stsp print_tree_object(struct got_object *obj, char *parent,
78 f715ca7f 2017-11-27 stsp struct got_repository *repo)
79 0ffeb3c2 2017-11-26 stsp {
80 0ffeb3c2 2017-11-26 stsp struct got_tree_object *tree;
81 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
82 0ffeb3c2 2017-11-26 stsp const struct got_error *err;
83 f715ca7f 2017-11-27 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
84 0ffeb3c2 2017-11-26 stsp
85 0ffeb3c2 2017-11-26 stsp err = got_object_tree_open(&tree, repo, obj);
86 0ffeb3c2 2017-11-26 stsp if (err != NULL)
87 0ffeb3c2 2017-11-26 stsp return err;
88 0ffeb3c2 2017-11-26 stsp
89 f715ca7f 2017-11-27 stsp SIMPLEQ_FOREACH(te, &tree->entries, entry) {
90 f715ca7f 2017-11-27 stsp struct got_object *treeobj;
91 f715ca7f 2017-11-27 stsp char *next_parent;
92 f715ca7f 2017-11-27 stsp
93 f715ca7f 2017-11-27 stsp if (!S_ISDIR(te->mode)) {
94 82f2fb69 2018-01-26 stsp test_printf("%s %s/%s\n",
95 f715ca7f 2017-11-27 stsp got_object_id_str(&te->id, hex, sizeof(hex)),
96 f715ca7f 2017-11-27 stsp parent, te->name);
97 f715ca7f 2017-11-27 stsp continue;
98 f715ca7f 2017-11-27 stsp }
99 82f2fb69 2018-01-26 stsp test_printf("%s %s/%s\n",
100 f715ca7f 2017-11-27 stsp got_object_id_str(&te->id, hex, sizeof(hex)),
101 f715ca7f 2017-11-27 stsp parent, te->name);
102 f715ca7f 2017-11-27 stsp
103 f715ca7f 2017-11-27 stsp err = got_object_open(&treeobj, repo, &te->id);
104 f715ca7f 2017-11-27 stsp if (err != NULL)
105 f715ca7f 2017-11-27 stsp break;
106 f715ca7f 2017-11-27 stsp
107 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
108 f715ca7f 2017-11-27 stsp err = got_error(GOT_ERR_OBJ_TYPE);
109 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
110 f715ca7f 2017-11-27 stsp break;
111 f715ca7f 2017-11-27 stsp }
112 f715ca7f 2017-11-27 stsp
113 f715ca7f 2017-11-27 stsp if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
114 f715ca7f 2017-11-27 stsp err = got_error(GOT_ERR_NO_MEM);
115 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
116 f715ca7f 2017-11-27 stsp break;
117 f715ca7f 2017-11-27 stsp }
118 f715ca7f 2017-11-27 stsp
119 f715ca7f 2017-11-27 stsp err = print_tree_object(treeobj, next_parent, repo);
120 f715ca7f 2017-11-27 stsp free(next_parent);
121 f715ca7f 2017-11-27 stsp if (err) {
122 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
123 f715ca7f 2017-11-27 stsp break;
124 f715ca7f 2017-11-27 stsp }
125 f715ca7f 2017-11-27 stsp
126 f715ca7f 2017-11-27 stsp got_object_close(treeobj);
127 f715ca7f 2017-11-27 stsp }
128 f715ca7f 2017-11-27 stsp
129 0ffeb3c2 2017-11-26 stsp got_object_tree_close(tree);
130 f715ca7f 2017-11-27 stsp return err;
131 0ffeb3c2 2017-11-26 stsp }
132 0ffeb3c2 2017-11-26 stsp
133 0ffeb3c2 2017-11-26 stsp static const struct got_error *
134 1c852fbe 2017-11-12 stsp print_commit_object(struct got_object *obj, struct got_repository *repo)
135 1c852fbe 2017-11-12 stsp {
136 1c852fbe 2017-11-12 stsp struct got_commit_object *commit;
137 1c852fbe 2017-11-12 stsp struct got_parent_id *pid;
138 1c852fbe 2017-11-12 stsp char buf[SHA1_DIGEST_STRING_LENGTH];
139 1c852fbe 2017-11-12 stsp const struct got_error *err;
140 0ffeb3c2 2017-11-26 stsp struct got_object* treeobj;
141 1c852fbe 2017-11-12 stsp
142 1c852fbe 2017-11-12 stsp err = got_object_commit_open(&commit, repo, obj);
143 1c852fbe 2017-11-12 stsp if (err != NULL)
144 1c852fbe 2017-11-12 stsp return err;
145 1c852fbe 2017-11-12 stsp
146 82f2fb69 2018-01-26 stsp test_printf("tree: %s\n",
147 1c852fbe 2017-11-12 stsp got_object_id_str(&commit->tree_id, buf, sizeof(buf)));
148 82f2fb69 2018-01-26 stsp test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
149 1c852fbe 2017-11-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
150 82f2fb69 2018-01-26 stsp test_printf("%s\n",
151 1c852fbe 2017-11-12 stsp got_object_id_str(&pid->id, buf, sizeof(buf)));
152 1c852fbe 2017-11-12 stsp }
153 82f2fb69 2018-01-26 stsp test_printf("author: %s\n", commit->author);
154 82f2fb69 2018-01-26 stsp test_printf("committer: %s\n", commit->committer);
155 82f2fb69 2018-01-26 stsp test_printf("log: %s\n", commit->logmsg);
156 bfab4d9a 2017-11-12 stsp
157 0ffeb3c2 2017-11-26 stsp err = got_object_open(&treeobj, repo, &commit->tree_id);
158 0ffeb3c2 2017-11-26 stsp if (err != NULL)
159 0ffeb3c2 2017-11-26 stsp return err;
160 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
161 f715ca7f 2017-11-27 stsp print_tree_object(treeobj, "", repo);
162 82f2fb69 2018-01-26 stsp test_printf("\n");
163 f715ca7f 2017-11-27 stsp }
164 0ffeb3c2 2017-11-26 stsp got_object_close(treeobj);
165 0ffeb3c2 2017-11-26 stsp
166 bfab4d9a 2017-11-12 stsp err = print_parent_commits(commit, repo);
167 1c852fbe 2017-11-12 stsp got_object_commit_close(commit);
168 1c852fbe 2017-11-12 stsp
169 bfab4d9a 2017-11-12 stsp return err;
170 1c852fbe 2017-11-12 stsp }
171 1c852fbe 2017-11-12 stsp
172 4027f31a 2017-11-04 stsp static int
173 bfab4d9a 2017-11-12 stsp repo_read_log(const char *repo_path)
174 11995603 2017-11-05 stsp {
175 11995603 2017-11-05 stsp const struct got_error *err;
176 11995603 2017-11-05 stsp struct got_repository *repo;
177 11995603 2017-11-05 stsp struct got_reference *head_ref;
178 11995603 2017-11-05 stsp struct got_object_id *id;
179 ab9a70b2 2017-11-06 stsp struct got_object *obj;
180 d71d75ad 2017-11-05 stsp char buf[SHA1_DIGEST_STRING_LENGTH];
181 11995603 2017-11-05 stsp int ret;
182 11995603 2017-11-05 stsp
183 11995603 2017-11-05 stsp err = got_repo_open(&repo, repo_path);
184 11995603 2017-11-05 stsp if (err != NULL || repo == NULL)
185 11995603 2017-11-05 stsp return 0;
186 11995603 2017-11-05 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
187 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
188 11995603 2017-11-05 stsp return 0;
189 11995603 2017-11-05 stsp err = got_ref_resolve(&id, repo, head_ref);
190 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
191 11995603 2017-11-05 stsp return 0;
192 82f2fb69 2018-01-26 stsp test_printf("HEAD is at %s\n", got_object_id_str(id, buf, sizeof(buf)));
193 ab9a70b2 2017-11-06 stsp err = got_object_open(&obj, repo, id);
194 ab9a70b2 2017-11-06 stsp if (err != NULL || obj == NULL)
195 ab9a70b2 2017-11-06 stsp return 0;
196 a37d050f 2018-01-26 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
197 a37d050f 2018-01-26 stsp err = print_commit_object(obj, repo);
198 a37d050f 2018-01-26 stsp if (err)
199 a37d050f 2018-01-26 stsp return 0;
200 a37d050f 2018-01-26 stsp } else
201 a37d050f 2018-01-26 stsp return 0;
202 ab9a70b2 2017-11-06 stsp got_object_close(obj);
203 11995603 2017-11-05 stsp free(id);
204 11995603 2017-11-05 stsp got_ref_close(head_ref);
205 11995603 2017-11-05 stsp got_repo_close(repo);
206 11995603 2017-11-05 stsp return 1;
207 11995603 2017-11-05 stsp }
208 11995603 2017-11-05 stsp
209 68482ea3 2017-11-27 stsp static int
210 68482ea3 2017-11-27 stsp repo_read_blob(const char *repo_path)
211 68482ea3 2017-11-27 stsp {
212 68482ea3 2017-11-27 stsp const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
213 68482ea3 2017-11-27 stsp const struct got_error *err;
214 68482ea3 2017-11-27 stsp struct got_repository *repo;
215 68482ea3 2017-11-27 stsp struct got_object_id id;
216 68482ea3 2017-11-27 stsp struct got_object *obj;
217 68482ea3 2017-11-27 stsp struct got_blob_object *blob;
218 68482ea3 2017-11-27 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
219 68482ea3 2017-11-27 stsp int i;
220 68482ea3 2017-11-27 stsp size_t len;
221 68482ea3 2017-11-27 stsp
222 68482ea3 2017-11-27 stsp if (!got_parse_sha1_digest(id.sha1, blob_sha1))
223 68482ea3 2017-11-27 stsp return 0;
224 68482ea3 2017-11-27 stsp
225 68482ea3 2017-11-27 stsp err = got_repo_open(&repo, repo_path);
226 68482ea3 2017-11-27 stsp if (err != NULL || repo == NULL)
227 68482ea3 2017-11-27 stsp return 0;
228 68482ea3 2017-11-27 stsp err = got_object_open(&obj, repo, &id);
229 68482ea3 2017-11-27 stsp if (err != NULL || obj == NULL)
230 68482ea3 2017-11-27 stsp return 0;
231 b107e67f 2018-01-19 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
232 68482ea3 2017-11-27 stsp return 0;
233 68482ea3 2017-11-27 stsp
234 68482ea3 2017-11-27 stsp err = got_object_blob_open(&blob, repo, obj, 64);
235 68482ea3 2017-11-27 stsp if (err != NULL)
236 68482ea3 2017-11-27 stsp return 0;
237 68482ea3 2017-11-27 stsp
238 82f2fb69 2018-01-26 stsp test_printf("\n");
239 68482ea3 2017-11-27 stsp do {
240 68482ea3 2017-11-27 stsp err = got_object_blob_read_block(blob, &len);
241 68482ea3 2017-11-27 stsp if (err)
242 68482ea3 2017-11-27 stsp break;
243 68482ea3 2017-11-27 stsp for (i = 0; i < len; i++)
244 82f2fb69 2018-01-26 stsp test_printf("%c", blob->zb.outbuf[i]);
245 68482ea3 2017-11-27 stsp } while (len != 0);
246 82f2fb69 2018-01-26 stsp test_printf("\n");
247 68482ea3 2017-11-27 stsp
248 68482ea3 2017-11-27 stsp got_object_blob_close(blob);
249 68482ea3 2017-11-27 stsp got_object_close(obj);
250 7d283eee 2017-11-29 stsp got_repo_close(repo);
251 7d283eee 2017-11-29 stsp return (err == NULL);
252 7d283eee 2017-11-29 stsp }
253 7d283eee 2017-11-29 stsp
254 7d283eee 2017-11-29 stsp static int
255 7d283eee 2017-11-29 stsp repo_diff_blob(const char *repo_path)
256 7d283eee 2017-11-29 stsp {
257 7d283eee 2017-11-29 stsp const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
258 7d283eee 2017-11-29 stsp const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
259 7d283eee 2017-11-29 stsp const struct got_error *err;
260 7d283eee 2017-11-29 stsp struct got_repository *repo;
261 7d283eee 2017-11-29 stsp struct got_object_id id1;
262 7d283eee 2017-11-29 stsp struct got_object_id id2;
263 7d283eee 2017-11-29 stsp struct got_object *obj1;
264 7d283eee 2017-11-29 stsp struct got_object *obj2;
265 7d283eee 2017-11-29 stsp struct got_blob_object *blob1;
266 7d283eee 2017-11-29 stsp struct got_blob_object *blob2;
267 7d283eee 2017-11-29 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
268 7d283eee 2017-11-29 stsp int i;
269 7d283eee 2017-11-29 stsp size_t len;
270 7d283eee 2017-11-29 stsp
271 7d283eee 2017-11-29 stsp if (!got_parse_sha1_digest(id1.sha1, blob1_sha1))
272 7d283eee 2017-11-29 stsp return 0;
273 7d283eee 2017-11-29 stsp if (!got_parse_sha1_digest(id2.sha1, blob2_sha1))
274 7d283eee 2017-11-29 stsp return 0;
275 7d283eee 2017-11-29 stsp
276 7d283eee 2017-11-29 stsp err = got_repo_open(&repo, repo_path);
277 7d283eee 2017-11-29 stsp if (err != NULL || repo == NULL)
278 7d283eee 2017-11-29 stsp return 0;
279 7d283eee 2017-11-29 stsp
280 7d283eee 2017-11-29 stsp err = got_object_open(&obj1, repo, &id1);
281 7d283eee 2017-11-29 stsp if (err != NULL || obj1 == NULL)
282 7d283eee 2017-11-29 stsp return 0;
283 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
284 7d283eee 2017-11-29 stsp return 0;
285 7d283eee 2017-11-29 stsp err = got_object_open(&obj2, repo, &id2);
286 7d283eee 2017-11-29 stsp if (err != NULL || obj2 == NULL)
287 7d283eee 2017-11-29 stsp return 0;
288 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
289 7d283eee 2017-11-29 stsp return 0;
290 7d283eee 2017-11-29 stsp
291 7d283eee 2017-11-29 stsp err = got_object_blob_open(&blob1, repo, obj1, 512);
292 7d283eee 2017-11-29 stsp if (err != NULL)
293 7d283eee 2017-11-29 stsp return 0;
294 7d283eee 2017-11-29 stsp
295 7d283eee 2017-11-29 stsp err = got_object_blob_open(&blob2, repo, obj2, 512);
296 7d283eee 2017-11-29 stsp if (err != NULL)
297 7d283eee 2017-11-29 stsp return 0;
298 7d283eee 2017-11-29 stsp
299 82f2fb69 2018-01-26 stsp test_printf("\n");
300 62136d3a 2017-11-29 stsp got_diff_blob(blob1, blob2, NULL, NULL, stdout);
301 82f2fb69 2018-01-26 stsp test_printf("\n");
302 7d283eee 2017-11-29 stsp
303 7d283eee 2017-11-29 stsp got_object_blob_close(blob1);
304 7d283eee 2017-11-29 stsp got_object_blob_close(blob2);
305 98abbc84 2017-11-30 stsp got_object_close(obj1);
306 98abbc84 2017-11-30 stsp got_object_close(obj2);
307 98abbc84 2017-11-30 stsp got_repo_close(repo);
308 98abbc84 2017-11-30 stsp return (err == NULL);
309 98abbc84 2017-11-30 stsp }
310 98abbc84 2017-11-30 stsp
311 98abbc84 2017-11-30 stsp static int
312 98abbc84 2017-11-30 stsp repo_diff_tree(const char *repo_path)
313 98abbc84 2017-11-30 stsp {
314 4a0235dd 2017-11-30 stsp const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
315 a3e2cbea 2017-12-01 stsp const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
316 98abbc84 2017-11-30 stsp const struct got_error *err;
317 98abbc84 2017-11-30 stsp struct got_repository *repo;
318 98abbc84 2017-11-30 stsp struct got_object_id id1;
319 98abbc84 2017-11-30 stsp struct got_object_id id2;
320 98abbc84 2017-11-30 stsp struct got_object *obj1;
321 98abbc84 2017-11-30 stsp struct got_object *obj2;
322 98abbc84 2017-11-30 stsp struct got_tree_object *tree1;
323 98abbc84 2017-11-30 stsp struct got_tree_object *tree2;
324 98abbc84 2017-11-30 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
325 98abbc84 2017-11-30 stsp int i;
326 98abbc84 2017-11-30 stsp size_t len;
327 98abbc84 2017-11-30 stsp
328 4a0235dd 2017-11-30 stsp if (!got_parse_sha1_digest(id1.sha1, tree1_sha1))
329 98abbc84 2017-11-30 stsp return 0;
330 4a0235dd 2017-11-30 stsp if (!got_parse_sha1_digest(id2.sha1, tree2_sha1))
331 98abbc84 2017-11-30 stsp return 0;
332 98abbc84 2017-11-30 stsp
333 98abbc84 2017-11-30 stsp err = got_repo_open(&repo, repo_path);
334 98abbc84 2017-11-30 stsp if (err != NULL || repo == NULL)
335 98abbc84 2017-11-30 stsp return 0;
336 98abbc84 2017-11-30 stsp
337 98abbc84 2017-11-30 stsp err = got_object_open(&obj1, repo, &id1);
338 98abbc84 2017-11-30 stsp if (err != NULL || obj1 == NULL)
339 98abbc84 2017-11-30 stsp return 0;
340 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
341 98abbc84 2017-11-30 stsp return 0;
342 98abbc84 2017-11-30 stsp err = got_object_open(&obj2, repo, &id2);
343 98abbc84 2017-11-30 stsp if (err != NULL || obj2 == NULL)
344 98abbc84 2017-11-30 stsp return 0;
345 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
346 98abbc84 2017-11-30 stsp return 0;
347 98abbc84 2017-11-30 stsp
348 98abbc84 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, obj1);
349 98abbc84 2017-11-30 stsp if (err != NULL)
350 98abbc84 2017-11-30 stsp return 0;
351 98abbc84 2017-11-30 stsp
352 98abbc84 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, obj2);
353 98abbc84 2017-11-30 stsp if (err != NULL)
354 98abbc84 2017-11-30 stsp return 0;
355 98abbc84 2017-11-30 stsp
356 82f2fb69 2018-01-26 stsp test_printf("\n");
357 98abbc84 2017-11-30 stsp got_diff_tree(tree1, tree2, repo);
358 82f2fb69 2018-01-26 stsp test_printf("\n");
359 98abbc84 2017-11-30 stsp
360 98abbc84 2017-11-30 stsp got_object_tree_close(tree1);
361 98abbc84 2017-11-30 stsp got_object_tree_close(tree2);
362 7d283eee 2017-11-29 stsp got_object_close(obj1);
363 7d283eee 2017-11-29 stsp got_object_close(obj2);
364 68482ea3 2017-11-27 stsp got_repo_close(repo);
365 68482ea3 2017-11-27 stsp return (err == NULL);
366 68482ea3 2017-11-27 stsp }
367 b08fe7be 2018-01-26 stsp
368 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
369 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
370 b08fe7be 2018-01-26 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
371 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
372 68482ea3 2017-11-27 stsp
373 82f2fb69 2018-01-26 stsp
374 82f2fb69 2018-01-26 stsp void
375 82f2fb69 2018-01-26 stsp usage(void)
376 82f2fb69 2018-01-26 stsp {
377 82f2fb69 2018-01-26 stsp fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
378 82f2fb69 2018-01-26 stsp }
379 82f2fb69 2018-01-26 stsp
380 4027f31a 2017-11-04 stsp int
381 82f2fb69 2018-01-26 stsp main(int argc, char *argv[])
382 4027f31a 2017-11-04 stsp {
383 b08fe7be 2018-01-26 stsp int test_ok = 0, failure = 0;
384 4027f31a 2017-11-04 stsp const char *repo_path;
385 82f2fb69 2018-01-26 stsp int ch;
386 82f2fb69 2018-01-26 stsp int vflag = 0;
387 4027f31a 2017-11-04 stsp
388 82f2fb69 2018-01-26 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
389 82f2fb69 2018-01-26 stsp switch (ch) {
390 82f2fb69 2018-01-26 stsp case 'v':
391 82f2fb69 2018-01-26 stsp verbose = 1;
392 82f2fb69 2018-01-26 stsp break;
393 82f2fb69 2018-01-26 stsp default:
394 82f2fb69 2018-01-26 stsp usage();
395 82f2fb69 2018-01-26 stsp return 1;
396 82f2fb69 2018-01-26 stsp }
397 82f2fb69 2018-01-26 stsp }
398 82f2fb69 2018-01-26 stsp argc -= optind;
399 82f2fb69 2018-01-26 stsp argv += optind;
400 82f2fb69 2018-01-26 stsp
401 82f2fb69 2018-01-26 stsp if (argc == 0)
402 4027f31a 2017-11-04 stsp repo_path = GOT_REPO_PATH;
403 82f2fb69 2018-01-26 stsp else if (argc == 1)
404 4027f31a 2017-11-04 stsp repo_path = argv[1];
405 4027f31a 2017-11-04 stsp else {
406 82f2fb69 2018-01-26 stsp usage();
407 4027f31a 2017-11-04 stsp return 1;
408 4027f31a 2017-11-04 stsp }
409 4027f31a 2017-11-04 stsp
410 bfab4d9a 2017-11-12 stsp RUN_TEST(repo_read_log(repo_path), "read_log");
411 68482ea3 2017-11-27 stsp RUN_TEST(repo_read_blob(repo_path), "read_blob");
412 7d283eee 2017-11-29 stsp RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
413 98abbc84 2017-11-30 stsp RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
414 4027f31a 2017-11-04 stsp
415 4027f31a 2017-11-04 stsp return failure ? 1 : 0;
416 4027f31a 2017-11-04 stsp }