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