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 f8352b2a 2018-03-12 stsp #include <unistd.h>
25 f8352b2a 2018-03-12 stsp #include <err.h>
26 59ece79d 2018-02-12 stsp #include <unistd.h>
27 4027f31a 2017-11-04 stsp
28 4027f31a 2017-11-04 stsp #include "got_error.h"
29 11995603 2017-11-05 stsp #include "got_object.h"
30 4027f31a 2017-11-04 stsp #include "got_refs.h"
31 4027f31a 2017-11-04 stsp #include "got_repository.h"
32 f78b0693 2017-11-29 stsp #include "got_diff.h"
33 4027f31a 2017-11-04 stsp
34 4027f31a 2017-11-04 stsp #define GOT_REPO_PATH "../../../"
35 4027f31a 2017-11-04 stsp
36 14545512 2018-01-26 stsp static int verbose;
37 82f2fb69 2018-01-26 stsp
38 82f2fb69 2018-01-26 stsp void
39 82f2fb69 2018-01-26 stsp test_printf(char *fmt, ...)
40 82f2fb69 2018-01-26 stsp {
41 82f2fb69 2018-01-26 stsp va_list ap;
42 82f2fb69 2018-01-26 stsp
43 82f2fb69 2018-01-26 stsp if (!verbose)
44 82f2fb69 2018-01-26 stsp return;
45 82f2fb69 2018-01-26 stsp
46 82f2fb69 2018-01-26 stsp va_start(ap, fmt);
47 82f2fb69 2018-01-26 stsp vprintf(fmt, ap);
48 82f2fb69 2018-01-26 stsp va_end(ap);
49 82f2fb69 2018-01-26 stsp }
50 82f2fb69 2018-01-26 stsp
51 bfab4d9a 2017-11-12 stsp static const struct got_error *
52 bfab4d9a 2017-11-12 stsp print_commit_object(struct got_object *, struct got_repository *);
53 1c852fbe 2017-11-12 stsp
54 1c852fbe 2017-11-12 stsp static const struct got_error *
55 bfab4d9a 2017-11-12 stsp print_parent_commits(struct got_commit_object *commit,
56 bfab4d9a 2017-11-12 stsp struct got_repository *repo)
57 bfab4d9a 2017-11-12 stsp {
58 bfab4d9a 2017-11-12 stsp struct got_parent_id *pid;
59 a37d050f 2018-01-26 stsp const struct got_error *err = NULL;
60 bfab4d9a 2017-11-12 stsp struct got_object *obj;
61 bfab4d9a 2017-11-12 stsp
62 bfab4d9a 2017-11-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
63 59ece79d 2018-02-12 stsp err = got_object_open(&obj, repo, pid->id);
64 bfab4d9a 2017-11-12 stsp if (err != NULL)
65 bfab4d9a 2017-11-12 stsp return err;
66 b107e67f 2018-01-19 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
67 bfab4d9a 2017-11-12 stsp return got_error(GOT_ERR_OBJ_TYPE);
68 a37d050f 2018-01-26 stsp err = print_commit_object(obj, repo);
69 bfab4d9a 2017-11-12 stsp got_object_close(obj);
70 bfab4d9a 2017-11-12 stsp }
71 bfab4d9a 2017-11-12 stsp
72 a37d050f 2018-01-26 stsp return err;
73 bfab4d9a 2017-11-12 stsp }
74 bfab4d9a 2017-11-12 stsp
75 bfab4d9a 2017-11-12 stsp static const struct got_error *
76 f715ca7f 2017-11-27 stsp print_tree_object(struct got_object *obj, char *parent,
77 f715ca7f 2017-11-27 stsp struct got_repository *repo)
78 0ffeb3c2 2017-11-26 stsp {
79 0ffeb3c2 2017-11-26 stsp struct got_tree_object *tree;
80 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
81 0ffeb3c2 2017-11-26 stsp const struct got_error *err;
82 0ffeb3c2 2017-11-26 stsp
83 0ffeb3c2 2017-11-26 stsp err = got_object_tree_open(&tree, repo, obj);
84 0ffeb3c2 2017-11-26 stsp if (err != NULL)
85 0ffeb3c2 2017-11-26 stsp return err;
86 0ffeb3c2 2017-11-26 stsp
87 f715ca7f 2017-11-27 stsp SIMPLEQ_FOREACH(te, &tree->entries, entry) {
88 f715ca7f 2017-11-27 stsp struct got_object *treeobj;
89 f715ca7f 2017-11-27 stsp char *next_parent;
90 ef0981d5 2018-02-12 stsp char *hex;
91 f715ca7f 2017-11-27 stsp
92 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, te->id);
93 ef0981d5 2018-02-12 stsp if (err)
94 ef0981d5 2018-02-12 stsp break;
95 ef0981d5 2018-02-12 stsp
96 f715ca7f 2017-11-27 stsp if (!S_ISDIR(te->mode)) {
97 ef0981d5 2018-02-12 stsp test_printf("%s %s/%s\n", hex, parent, te->name);
98 f715ca7f 2017-11-27 stsp continue;
99 f715ca7f 2017-11-27 stsp }
100 ef0981d5 2018-02-12 stsp test_printf("%s %s/%s\n", hex, parent, te->name);
101 ef0981d5 2018-02-12 stsp free(hex);
102 f715ca7f 2017-11-27 stsp
103 59ece79d 2018-02-12 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 ef0981d5 2018-02-12 stsp char *buf;
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 ef0981d5 2018-02-12 stsp if (err)
144 1c852fbe 2017-11-12 stsp return err;
145 1c852fbe 2017-11-12 stsp
146 ef0981d5 2018-02-12 stsp err = got_object_id_str(&buf, commit->tree_id);
147 ef0981d5 2018-02-12 stsp if (err)
148 ef0981d5 2018-02-12 stsp return err;
149 ef0981d5 2018-02-12 stsp test_printf("tree: %s\n", buf);
150 ef0981d5 2018-02-12 stsp free(buf);
151 82f2fb69 2018-01-26 stsp test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
152 1c852fbe 2017-11-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
153 ef0981d5 2018-02-12 stsp err = got_object_id_str(&buf, pid->id);
154 ef0981d5 2018-02-12 stsp if (err)
155 ef0981d5 2018-02-12 stsp return err;
156 ef0981d5 2018-02-12 stsp test_printf("%s\n", buf);
157 ef0981d5 2018-02-12 stsp free(buf);
158 1c852fbe 2017-11-12 stsp }
159 82f2fb69 2018-01-26 stsp test_printf("author: %s\n", commit->author);
160 82f2fb69 2018-01-26 stsp test_printf("committer: %s\n", commit->committer);
161 82f2fb69 2018-01-26 stsp test_printf("log: %s\n", commit->logmsg);
162 bfab4d9a 2017-11-12 stsp
163 59ece79d 2018-02-12 stsp err = got_object_open(&treeobj, repo, commit->tree_id);
164 0ffeb3c2 2017-11-26 stsp if (err != NULL)
165 0ffeb3c2 2017-11-26 stsp return err;
166 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
167 f715ca7f 2017-11-27 stsp print_tree_object(treeobj, "", repo);
168 82f2fb69 2018-01-26 stsp test_printf("\n");
169 f715ca7f 2017-11-27 stsp }
170 0ffeb3c2 2017-11-26 stsp got_object_close(treeobj);
171 0ffeb3c2 2017-11-26 stsp
172 bfab4d9a 2017-11-12 stsp err = print_parent_commits(commit, repo);
173 1c852fbe 2017-11-12 stsp got_object_commit_close(commit);
174 1c852fbe 2017-11-12 stsp
175 bfab4d9a 2017-11-12 stsp return err;
176 1c852fbe 2017-11-12 stsp }
177 1c852fbe 2017-11-12 stsp
178 4027f31a 2017-11-04 stsp static int
179 bfab4d9a 2017-11-12 stsp repo_read_log(const char *repo_path)
180 11995603 2017-11-05 stsp {
181 11995603 2017-11-05 stsp const struct got_error *err;
182 11995603 2017-11-05 stsp struct got_repository *repo;
183 11995603 2017-11-05 stsp struct got_reference *head_ref;
184 11995603 2017-11-05 stsp struct got_object_id *id;
185 ab9a70b2 2017-11-06 stsp struct got_object *obj;
186 ef0981d5 2018-02-12 stsp char *buf;
187 11995603 2017-11-05 stsp
188 11995603 2017-11-05 stsp err = got_repo_open(&repo, repo_path);
189 11995603 2017-11-05 stsp if (err != NULL || repo == NULL)
190 11995603 2017-11-05 stsp return 0;
191 11995603 2017-11-05 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
192 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
193 11995603 2017-11-05 stsp return 0;
194 11995603 2017-11-05 stsp err = got_ref_resolve(&id, repo, head_ref);
195 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
196 11995603 2017-11-05 stsp return 0;
197 ef0981d5 2018-02-12 stsp err = got_object_id_str(&buf, id);
198 ef0981d5 2018-02-12 stsp if (err != NULL)
199 ef0981d5 2018-02-12 stsp return 0;
200 ef0981d5 2018-02-12 stsp test_printf("HEAD is at %s\n", buf);
201 ef0981d5 2018-02-12 stsp free(buf);
202 ab9a70b2 2017-11-06 stsp err = got_object_open(&obj, repo, id);
203 ab9a70b2 2017-11-06 stsp if (err != NULL || obj == NULL)
204 ab9a70b2 2017-11-06 stsp return 0;
205 a37d050f 2018-01-26 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
206 a37d050f 2018-01-26 stsp err = print_commit_object(obj, repo);
207 a37d050f 2018-01-26 stsp if (err)
208 a37d050f 2018-01-26 stsp return 0;
209 a37d050f 2018-01-26 stsp } else
210 a37d050f 2018-01-26 stsp return 0;
211 ab9a70b2 2017-11-06 stsp got_object_close(obj);
212 11995603 2017-11-05 stsp free(id);
213 11995603 2017-11-05 stsp got_ref_close(head_ref);
214 11995603 2017-11-05 stsp got_repo_close(repo);
215 11995603 2017-11-05 stsp return 1;
216 11995603 2017-11-05 stsp }
217 044e7393 2018-02-11 stsp
218 044e7393 2018-02-11 stsp static int
219 044e7393 2018-02-11 stsp repo_read_tree(const char *repo_path)
220 044e7393 2018-02-11 stsp {
221 044e7393 2018-02-11 stsp const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
222 044e7393 2018-02-11 stsp const struct got_error *err;
223 044e7393 2018-02-11 stsp struct got_repository *repo;
224 044e7393 2018-02-11 stsp struct got_object *obj;
225 044e7393 2018-02-11 stsp
226 044e7393 2018-02-11 stsp err = got_repo_open(&repo, repo_path);
227 044e7393 2018-02-11 stsp if (err != NULL || repo == NULL)
228 044e7393 2018-02-11 stsp return 0;
229 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj, repo, tree_sha1);
230 044e7393 2018-02-11 stsp if (err != NULL || obj == NULL)
231 044e7393 2018-02-11 stsp return 0;
232 044e7393 2018-02-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
233 044e7393 2018-02-11 stsp return 0;
234 11995603 2017-11-05 stsp
235 044e7393 2018-02-11 stsp print_tree_object(obj, "", repo);
236 044e7393 2018-02-11 stsp test_printf("\n");
237 044e7393 2018-02-11 stsp
238 044e7393 2018-02-11 stsp got_object_close(obj);
239 044e7393 2018-02-11 stsp got_repo_close(repo);
240 044e7393 2018-02-11 stsp return (err == NULL);
241 044e7393 2018-02-11 stsp }
242 68482ea3 2017-11-27 stsp static int
243 68482ea3 2017-11-27 stsp repo_read_blob(const char *repo_path)
244 68482ea3 2017-11-27 stsp {
245 68482ea3 2017-11-27 stsp const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
246 68482ea3 2017-11-27 stsp const struct got_error *err;
247 68482ea3 2017-11-27 stsp struct got_repository *repo;
248 68482ea3 2017-11-27 stsp struct got_object *obj;
249 68482ea3 2017-11-27 stsp struct got_blob_object *blob;
250 68482ea3 2017-11-27 stsp int i;
251 68482ea3 2017-11-27 stsp size_t len;
252 68482ea3 2017-11-27 stsp
253 68482ea3 2017-11-27 stsp err = got_repo_open(&repo, repo_path);
254 68482ea3 2017-11-27 stsp if (err != NULL || repo == NULL)
255 68482ea3 2017-11-27 stsp return 0;
256 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj, repo, blob_sha1);
257 68482ea3 2017-11-27 stsp if (err != NULL || obj == NULL)
258 68482ea3 2017-11-27 stsp return 0;
259 b107e67f 2018-01-19 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
260 68482ea3 2017-11-27 stsp return 0;
261 68482ea3 2017-11-27 stsp
262 68482ea3 2017-11-27 stsp err = got_object_blob_open(&blob, repo, obj, 64);
263 68482ea3 2017-11-27 stsp if (err != NULL)
264 68482ea3 2017-11-27 stsp return 0;
265 68482ea3 2017-11-27 stsp
266 82f2fb69 2018-01-26 stsp test_printf("\n");
267 68482ea3 2017-11-27 stsp do {
268 f934cf2c 2018-02-12 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
269 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob);
270 68482ea3 2017-11-27 stsp if (err)
271 68482ea3 2017-11-27 stsp break;
272 68482ea3 2017-11-27 stsp for (i = 0; i < len; i++)
273 f934cf2c 2018-02-12 stsp test_printf("%c", buf[i]);
274 68482ea3 2017-11-27 stsp } while (len != 0);
275 82f2fb69 2018-01-26 stsp test_printf("\n");
276 68482ea3 2017-11-27 stsp
277 68482ea3 2017-11-27 stsp got_object_blob_close(blob);
278 68482ea3 2017-11-27 stsp got_object_close(obj);
279 7d283eee 2017-11-29 stsp got_repo_close(repo);
280 7d283eee 2017-11-29 stsp return (err == NULL);
281 7d283eee 2017-11-29 stsp }
282 7d283eee 2017-11-29 stsp
283 7d283eee 2017-11-29 stsp static int
284 7d283eee 2017-11-29 stsp repo_diff_blob(const char *repo_path)
285 7d283eee 2017-11-29 stsp {
286 7d283eee 2017-11-29 stsp const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
287 7d283eee 2017-11-29 stsp const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
288 7d283eee 2017-11-29 stsp const struct got_error *err;
289 7d283eee 2017-11-29 stsp struct got_repository *repo;
290 7d283eee 2017-11-29 stsp struct got_object *obj1;
291 7d283eee 2017-11-29 stsp struct got_object *obj2;
292 7d283eee 2017-11-29 stsp struct got_blob_object *blob1;
293 7d283eee 2017-11-29 stsp struct got_blob_object *blob2;
294 354a7e12 2018-02-11 stsp FILE *outfile;
295 7d283eee 2017-11-29 stsp
296 7d283eee 2017-11-29 stsp err = got_repo_open(&repo, repo_path);
297 7d283eee 2017-11-29 stsp if (err != NULL || repo == NULL)
298 7d283eee 2017-11-29 stsp return 0;
299 7d283eee 2017-11-29 stsp
300 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj1, repo, blob1_sha1);
301 7d283eee 2017-11-29 stsp if (err != NULL || obj1 == NULL)
302 7d283eee 2017-11-29 stsp return 0;
303 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
304 7d283eee 2017-11-29 stsp return 0;
305 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj2, repo, blob2_sha1);
306 7d283eee 2017-11-29 stsp if (err != NULL || obj2 == NULL)
307 7d283eee 2017-11-29 stsp return 0;
308 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
309 7d283eee 2017-11-29 stsp return 0;
310 7d283eee 2017-11-29 stsp
311 7d283eee 2017-11-29 stsp err = got_object_blob_open(&blob1, repo, obj1, 512);
312 7d283eee 2017-11-29 stsp if (err != NULL)
313 7d283eee 2017-11-29 stsp return 0;
314 7d283eee 2017-11-29 stsp
315 7d283eee 2017-11-29 stsp err = got_object_blob_open(&blob2, repo, obj2, 512);
316 7d283eee 2017-11-29 stsp if (err != NULL)
317 7d283eee 2017-11-29 stsp return 0;
318 7d283eee 2017-11-29 stsp
319 82f2fb69 2018-01-26 stsp test_printf("\n");
320 354a7e12 2018-02-11 stsp if (!verbose) {
321 354a7e12 2018-02-11 stsp outfile = fopen("/dev/null", "w+");
322 354a7e12 2018-02-11 stsp if (outfile == NULL)
323 354a7e12 2018-02-11 stsp return 0;
324 354a7e12 2018-02-11 stsp } else
325 354a7e12 2018-02-11 stsp outfile = stdout;
326 354a7e12 2018-02-11 stsp got_diff_blob(blob1, blob2, NULL, NULL, outfile);
327 82f2fb69 2018-01-26 stsp test_printf("\n");
328 7d283eee 2017-11-29 stsp
329 7d283eee 2017-11-29 stsp got_object_blob_close(blob1);
330 7d283eee 2017-11-29 stsp got_object_blob_close(blob2);
331 98abbc84 2017-11-30 stsp got_object_close(obj1);
332 98abbc84 2017-11-30 stsp got_object_close(obj2);
333 98abbc84 2017-11-30 stsp got_repo_close(repo);
334 98abbc84 2017-11-30 stsp return (err == NULL);
335 98abbc84 2017-11-30 stsp }
336 98abbc84 2017-11-30 stsp
337 98abbc84 2017-11-30 stsp static int
338 98abbc84 2017-11-30 stsp repo_diff_tree(const char *repo_path)
339 98abbc84 2017-11-30 stsp {
340 4a0235dd 2017-11-30 stsp const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
341 a3e2cbea 2017-12-01 stsp const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
342 98abbc84 2017-11-30 stsp const struct got_error *err;
343 98abbc84 2017-11-30 stsp struct got_repository *repo;
344 98abbc84 2017-11-30 stsp struct got_object *obj1;
345 98abbc84 2017-11-30 stsp struct got_object *obj2;
346 98abbc84 2017-11-30 stsp struct got_tree_object *tree1;
347 98abbc84 2017-11-30 stsp struct got_tree_object *tree2;
348 354a7e12 2018-02-11 stsp FILE *outfile;
349 98abbc84 2017-11-30 stsp
350 98abbc84 2017-11-30 stsp err = got_repo_open(&repo, repo_path);
351 98abbc84 2017-11-30 stsp if (err != NULL || repo == NULL)
352 98abbc84 2017-11-30 stsp return 0;
353 98abbc84 2017-11-30 stsp
354 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj1, repo, tree1_sha1);
355 98abbc84 2017-11-30 stsp if (err != NULL || obj1 == NULL)
356 98abbc84 2017-11-30 stsp return 0;
357 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
358 98abbc84 2017-11-30 stsp return 0;
359 6dfa2fd3 2018-02-12 stsp err = got_object_open_by_id_str(&obj2, repo, tree2_sha1);
360 98abbc84 2017-11-30 stsp if (err != NULL || obj2 == NULL)
361 98abbc84 2017-11-30 stsp return 0;
362 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)
363 98abbc84 2017-11-30 stsp return 0;
364 98abbc84 2017-11-30 stsp
365 98abbc84 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, obj1);
366 98abbc84 2017-11-30 stsp if (err != NULL)
367 98abbc84 2017-11-30 stsp return 0;
368 98abbc84 2017-11-30 stsp
369 98abbc84 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, obj2);
370 98abbc84 2017-11-30 stsp if (err != NULL)
371 98abbc84 2017-11-30 stsp return 0;
372 98abbc84 2017-11-30 stsp
373 354a7e12 2018-02-11 stsp if (!verbose) {
374 354a7e12 2018-02-11 stsp outfile = fopen("/dev/null", "w+");
375 354a7e12 2018-02-11 stsp if (outfile == NULL)
376 354a7e12 2018-02-11 stsp return 0;
377 354a7e12 2018-02-11 stsp } else
378 354a7e12 2018-02-11 stsp outfile = stdout;
379 82f2fb69 2018-01-26 stsp test_printf("\n");
380 354a7e12 2018-02-11 stsp got_diff_tree(tree1, tree2, repo, outfile);
381 82f2fb69 2018-01-26 stsp test_printf("\n");
382 98abbc84 2017-11-30 stsp
383 98abbc84 2017-11-30 stsp got_object_tree_close(tree1);
384 98abbc84 2017-11-30 stsp got_object_tree_close(tree2);
385 7d283eee 2017-11-29 stsp got_object_close(obj1);
386 7d283eee 2017-11-29 stsp got_object_close(obj2);
387 68482ea3 2017-11-27 stsp got_repo_close(repo);
388 68482ea3 2017-11-27 stsp return (err == NULL);
389 68482ea3 2017-11-27 stsp }
390 b08fe7be 2018-01-26 stsp
391 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
392 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
393 b08fe7be 2018-01-26 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
394 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
395 68482ea3 2017-11-27 stsp
396 82f2fb69 2018-01-26 stsp
397 82f2fb69 2018-01-26 stsp void
398 82f2fb69 2018-01-26 stsp usage(void)
399 82f2fb69 2018-01-26 stsp {
400 82f2fb69 2018-01-26 stsp fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
401 82f2fb69 2018-01-26 stsp }
402 82f2fb69 2018-01-26 stsp
403 4027f31a 2017-11-04 stsp int
404 82f2fb69 2018-01-26 stsp main(int argc, char *argv[])
405 4027f31a 2017-11-04 stsp {
406 b08fe7be 2018-01-26 stsp int test_ok = 0, failure = 0;
407 4027f31a 2017-11-04 stsp const char *repo_path;
408 82f2fb69 2018-01-26 stsp int ch;
409 4027f31a 2017-11-04 stsp
410 f8352b2a 2018-03-12 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
411 f8352b2a 2018-03-12 stsp err(1, "pledge");
412 f8352b2a 2018-03-12 stsp
413 82f2fb69 2018-01-26 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
414 82f2fb69 2018-01-26 stsp switch (ch) {
415 82f2fb69 2018-01-26 stsp case 'v':
416 82f2fb69 2018-01-26 stsp verbose = 1;
417 82f2fb69 2018-01-26 stsp break;
418 82f2fb69 2018-01-26 stsp default:
419 82f2fb69 2018-01-26 stsp usage();
420 82f2fb69 2018-01-26 stsp return 1;
421 82f2fb69 2018-01-26 stsp }
422 82f2fb69 2018-01-26 stsp }
423 82f2fb69 2018-01-26 stsp argc -= optind;
424 82f2fb69 2018-01-26 stsp argv += optind;
425 82f2fb69 2018-01-26 stsp
426 82f2fb69 2018-01-26 stsp if (argc == 0)
427 4027f31a 2017-11-04 stsp repo_path = GOT_REPO_PATH;
428 82f2fb69 2018-01-26 stsp else if (argc == 1)
429 ff3eb0f2 2018-03-09 stsp repo_path = argv[0];
430 4027f31a 2017-11-04 stsp else {
431 82f2fb69 2018-01-26 stsp usage();
432 4027f31a 2017-11-04 stsp return 1;
433 4027f31a 2017-11-04 stsp }
434 4027f31a 2017-11-04 stsp
435 044e7393 2018-02-11 stsp RUN_TEST(repo_read_tree(repo_path), "read_tree");
436 bfab4d9a 2017-11-12 stsp RUN_TEST(repo_read_log(repo_path), "read_log");
437 68482ea3 2017-11-27 stsp RUN_TEST(repo_read_blob(repo_path), "read_blob");
438 7d283eee 2017-11-29 stsp RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
439 98abbc84 2017-11-30 stsp RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
440 4027f31a 2017-11-04 stsp
441 4027f31a 2017-11-04 stsp return failure ? 1 : 0;
442 4027f31a 2017-11-04 stsp }