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 5a83d54e 2018-04-01 stsp #include <util.h>
23 4027f31a 2017-11-04 stsp #include <stdlib.h>
24 82f2fb69 2018-01-26 stsp #include <string.h>
25 f8352b2a 2018-03-12 stsp #include <unistd.h>
26 f8352b2a 2018-03-12 stsp #include <err.h>
27 59ece79d 2018-02-12 stsp #include <unistd.h>
28 4027f31a 2017-11-04 stsp
29 4027f31a 2017-11-04 stsp #include "got_error.h"
30 11995603 2017-11-05 stsp #include "got_object.h"
31 5261c201 2018-04-01 stsp #include "got_reference.h"
32 4027f31a 2017-11-04 stsp #include "got_repository.h"
33 f78b0693 2017-11-29 stsp #include "got_diff.h"
34 511a516b 2018-05-19 stsp #include "got_opentemp.h"
35 4027f31a 2017-11-04 stsp
36 5a83d54e 2018-04-01 stsp #include "got_lib_path.h"
37 5a83d54e 2018-04-01 stsp
38 5a83d54e 2018-04-01 stsp #ifndef nitems
39 5a83d54e 2018-04-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 5a83d54e 2018-04-01 stsp #endif
41 5a83d54e 2018-04-01 stsp
42 4027f31a 2017-11-04 stsp #define GOT_REPO_PATH "../../../"
43 4027f31a 2017-11-04 stsp
44 14545512 2018-01-26 stsp static int verbose;
45 82f2fb69 2018-01-26 stsp
46 82f2fb69 2018-01-26 stsp void
47 82f2fb69 2018-01-26 stsp test_printf(char *fmt, ...)
48 82f2fb69 2018-01-26 stsp {
49 82f2fb69 2018-01-26 stsp va_list ap;
50 82f2fb69 2018-01-26 stsp
51 82f2fb69 2018-01-26 stsp if (!verbose)
52 82f2fb69 2018-01-26 stsp return;
53 82f2fb69 2018-01-26 stsp
54 82f2fb69 2018-01-26 stsp va_start(ap, fmt);
55 82f2fb69 2018-01-26 stsp vprintf(fmt, ap);
56 82f2fb69 2018-01-26 stsp va_end(ap);
57 82f2fb69 2018-01-26 stsp }
58 82f2fb69 2018-01-26 stsp
59 bfab4d9a 2017-11-12 stsp static const struct got_error *
60 15a94983 2018-12-23 stsp print_commit_object(struct got_object_id *, struct got_repository *);
61 1c852fbe 2017-11-12 stsp
62 1c852fbe 2017-11-12 stsp static const struct got_error *
63 bfab4d9a 2017-11-12 stsp print_parent_commits(struct got_commit_object *commit,
64 bfab4d9a 2017-11-12 stsp struct got_repository *repo)
65 bfab4d9a 2017-11-12 stsp {
66 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
67 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
68 a37d050f 2018-01-26 stsp const struct got_error *err = NULL;
69 bfab4d9a 2017-11-12 stsp
70 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
71 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
72 15a94983 2018-12-23 stsp err = print_commit_object(qid->id, repo);
73 2178c42e 2018-04-22 stsp if (err)
74 2178c42e 2018-04-22 stsp break;
75 bfab4d9a 2017-11-12 stsp }
76 bfab4d9a 2017-11-12 stsp
77 a37d050f 2018-01-26 stsp return err;
78 bfab4d9a 2017-11-12 stsp }
79 bfab4d9a 2017-11-12 stsp
80 bfab4d9a 2017-11-12 stsp static const struct got_error *
81 15a94983 2018-12-23 stsp print_tree_object(struct got_object_id *id, char *parent,
82 f715ca7f 2017-11-27 stsp struct got_repository *repo)
83 0ffeb3c2 2017-11-26 stsp {
84 0ffeb3c2 2017-11-26 stsp struct got_tree_object *tree;
85 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
86 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
87 0ffeb3c2 2017-11-26 stsp const struct got_error *err;
88 0ffeb3c2 2017-11-26 stsp
89 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo, id);
90 0ffeb3c2 2017-11-26 stsp if (err != NULL)
91 0ffeb3c2 2017-11-26 stsp return err;
92 0ffeb3c2 2017-11-26 stsp
93 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
94 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
95 f715ca7f 2017-11-27 stsp char *next_parent;
96 ef0981d5 2018-02-12 stsp char *hex;
97 f715ca7f 2017-11-27 stsp
98 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, te->id);
99 ef0981d5 2018-02-12 stsp if (err)
100 ef0981d5 2018-02-12 stsp break;
101 ef0981d5 2018-02-12 stsp
102 f715ca7f 2017-11-27 stsp if (!S_ISDIR(te->mode)) {
103 ef0981d5 2018-02-12 stsp test_printf("%s %s/%s\n", hex, parent, te->name);
104 f78ec441 2018-03-17 stsp free(hex);
105 f715ca7f 2017-11-27 stsp continue;
106 f715ca7f 2017-11-27 stsp }
107 ef0981d5 2018-02-12 stsp test_printf("%s %s/%s\n", hex, parent, te->name);
108 ef0981d5 2018-02-12 stsp free(hex);
109 f715ca7f 2017-11-27 stsp
110 f715ca7f 2017-11-27 stsp if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
111 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
112 f715ca7f 2017-11-27 stsp break;
113 f715ca7f 2017-11-27 stsp }
114 f715ca7f 2017-11-27 stsp
115 15a94983 2018-12-23 stsp err = print_tree_object(te->id, next_parent, repo);
116 f715ca7f 2017-11-27 stsp free(next_parent);
117 15a94983 2018-12-23 stsp if (err)
118 f715ca7f 2017-11-27 stsp break;
119 f715ca7f 2017-11-27 stsp }
120 f715ca7f 2017-11-27 stsp
121 0ffeb3c2 2017-11-26 stsp got_object_tree_close(tree);
122 f715ca7f 2017-11-27 stsp return err;
123 0ffeb3c2 2017-11-26 stsp }
124 0ffeb3c2 2017-11-26 stsp
125 0ffeb3c2 2017-11-26 stsp static const struct got_error *
126 15a94983 2018-12-23 stsp print_commit_object(struct got_object_id *id, struct got_repository *repo)
127 1c852fbe 2017-11-12 stsp {
128 1c852fbe 2017-11-12 stsp struct got_commit_object *commit;
129 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
130 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
131 ef0981d5 2018-02-12 stsp char *buf;
132 1c852fbe 2017-11-12 stsp const struct got_error *err;
133 15a94983 2018-12-23 stsp int obj_type;
134 1c852fbe 2017-11-12 stsp
135 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, id);
136 ef0981d5 2018-02-12 stsp if (err)
137 1c852fbe 2017-11-12 stsp return err;
138 1c852fbe 2017-11-12 stsp
139 15a94983 2018-12-23 stsp err = got_object_id_str(&buf, id);
140 15a94983 2018-12-23 stsp if (err) {
141 15a94983 2018-12-23 stsp got_object_commit_close(commit);
142 ef0981d5 2018-02-12 stsp return err;
143 15a94983 2018-12-23 stsp }
144 ef0981d5 2018-02-12 stsp test_printf("tree: %s\n", buf);
145 ef0981d5 2018-02-12 stsp free(buf);
146 45d799e2 2018-12-23 stsp test_printf("parent%s: ",
147 45d799e2 2018-12-23 stsp (got_object_commit_get_nparents(commit) == 1) ? "" : "s");
148 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
149 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
150 79f35eb3 2018-06-11 stsp err = got_object_id_str(&buf, qid->id);
151 15a94983 2018-12-23 stsp if (err) {
152 15a94983 2018-12-23 stsp got_object_commit_close(commit);
153 ef0981d5 2018-02-12 stsp return err;
154 15a94983 2018-12-23 stsp }
155 ef0981d5 2018-02-12 stsp test_printf("%s\n", buf);
156 ef0981d5 2018-02-12 stsp free(buf);
157 1c852fbe 2017-11-12 stsp }
158 45d799e2 2018-12-23 stsp test_printf("author: %s\n", got_object_commit_get_author(commit));
159 45d799e2 2018-12-23 stsp test_printf("committer: %s\n", got_object_commit_get_committer(commit));
160 45d799e2 2018-12-23 stsp test_printf("log: %s\n", got_object_commit_get_logmsg(commit));
161 bfab4d9a 2017-11-12 stsp
162 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo,
163 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
164 15a94983 2018-12-23 stsp if (err != NULL) {
165 15a94983 2018-12-23 stsp got_object_commit_close(commit);
166 0ffeb3c2 2017-11-26 stsp return err;
167 f715ca7f 2017-11-27 stsp }
168 15a94983 2018-12-23 stsp if (obj_type == GOT_OBJ_TYPE_TREE)
169 15a94983 2018-12-23 stsp test_printf("\n");
170 0ffeb3c2 2017-11-26 stsp
171 bfab4d9a 2017-11-12 stsp err = print_parent_commits(commit, repo);
172 1c852fbe 2017-11-12 stsp got_object_commit_close(commit);
173 1c852fbe 2017-11-12 stsp
174 bfab4d9a 2017-11-12 stsp return err;
175 1c852fbe 2017-11-12 stsp }
176 1c852fbe 2017-11-12 stsp
177 4027f31a 2017-11-04 stsp static int
178 bfab4d9a 2017-11-12 stsp repo_read_log(const char *repo_path)
179 11995603 2017-11-05 stsp {
180 11995603 2017-11-05 stsp const struct got_error *err;
181 11995603 2017-11-05 stsp struct got_repository *repo;
182 11995603 2017-11-05 stsp struct got_reference *head_ref;
183 11995603 2017-11-05 stsp struct got_object_id *id;
184 ef0981d5 2018-02-12 stsp char *buf;
185 11995603 2017-11-05 stsp
186 11995603 2017-11-05 stsp err = got_repo_open(&repo, repo_path);
187 11995603 2017-11-05 stsp if (err != NULL || repo == NULL)
188 11995603 2017-11-05 stsp return 0;
189 11995603 2017-11-05 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
190 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
191 11995603 2017-11-05 stsp return 0;
192 11995603 2017-11-05 stsp err = got_ref_resolve(&id, repo, head_ref);
193 11995603 2017-11-05 stsp if (err != NULL || head_ref == NULL)
194 11995603 2017-11-05 stsp return 0;
195 ef0981d5 2018-02-12 stsp err = got_object_id_str(&buf, id);
196 ef0981d5 2018-02-12 stsp if (err != NULL)
197 ef0981d5 2018-02-12 stsp return 0;
198 ef0981d5 2018-02-12 stsp test_printf("HEAD is at %s\n", buf);
199 ef0981d5 2018-02-12 stsp free(buf);
200 15a94983 2018-12-23 stsp err = print_commit_object(id, repo);
201 15a94983 2018-12-23 stsp if (err)
202 a37d050f 2018-01-26 stsp return 0;
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 044e7393 2018-02-11 stsp
209 044e7393 2018-02-11 stsp static int
210 044e7393 2018-02-11 stsp repo_read_tree(const char *repo_path)
211 044e7393 2018-02-11 stsp {
212 044e7393 2018-02-11 stsp const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
213 044e7393 2018-02-11 stsp const struct got_error *err;
214 044e7393 2018-02-11 stsp struct got_repository *repo;
215 15a94983 2018-12-23 stsp struct got_object_id *id;
216 044e7393 2018-02-11 stsp
217 044e7393 2018-02-11 stsp err = got_repo_open(&repo, repo_path);
218 044e7393 2018-02-11 stsp if (err != NULL || repo == NULL)
219 044e7393 2018-02-11 stsp return 0;
220 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id, repo, tree_sha1);
221 15a94983 2018-12-23 stsp if (err != NULL)
222 044e7393 2018-02-11 stsp return 0;
223 11995603 2017-11-05 stsp
224 15a94983 2018-12-23 stsp print_tree_object(id, "", repo);
225 044e7393 2018-02-11 stsp test_printf("\n");
226 044e7393 2018-02-11 stsp
227 044e7393 2018-02-11 stsp got_repo_close(repo);
228 044e7393 2018-02-11 stsp return (err == NULL);
229 044e7393 2018-02-11 stsp }
230 f78ec441 2018-03-17 stsp
231 68482ea3 2017-11-27 stsp static int
232 68482ea3 2017-11-27 stsp repo_read_blob(const char *repo_path)
233 68482ea3 2017-11-27 stsp {
234 68482ea3 2017-11-27 stsp const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
235 68482ea3 2017-11-27 stsp const struct got_error *err;
236 68482ea3 2017-11-27 stsp struct got_repository *repo;
237 15a94983 2018-12-23 stsp struct got_object_id *id;
238 68482ea3 2017-11-27 stsp struct got_blob_object *blob;
239 68482ea3 2017-11-27 stsp int i;
240 68482ea3 2017-11-27 stsp size_t len;
241 68482ea3 2017-11-27 stsp
242 68482ea3 2017-11-27 stsp err = got_repo_open(&repo, repo_path);
243 68482ea3 2017-11-27 stsp if (err != NULL || repo == NULL)
244 68482ea3 2017-11-27 stsp return 0;
245 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id, repo, blob_sha1);
246 15a94983 2018-12-23 stsp if (err != NULL)
247 68482ea3 2017-11-27 stsp return 0;
248 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, id, 64);
249 68482ea3 2017-11-27 stsp if (err != NULL)
250 68482ea3 2017-11-27 stsp return 0;
251 68482ea3 2017-11-27 stsp
252 82f2fb69 2018-01-26 stsp test_printf("\n");
253 68482ea3 2017-11-27 stsp do {
254 f934cf2c 2018-02-12 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
255 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob);
256 68482ea3 2017-11-27 stsp if (err)
257 68482ea3 2017-11-27 stsp break;
258 68482ea3 2017-11-27 stsp for (i = 0; i < len; i++)
259 f934cf2c 2018-02-12 stsp test_printf("%c", buf[i]);
260 68482ea3 2017-11-27 stsp } while (len != 0);
261 82f2fb69 2018-01-26 stsp test_printf("\n");
262 68482ea3 2017-11-27 stsp
263 68482ea3 2017-11-27 stsp got_object_blob_close(blob);
264 7d283eee 2017-11-29 stsp got_repo_close(repo);
265 7d283eee 2017-11-29 stsp return (err == NULL);
266 7d283eee 2017-11-29 stsp }
267 7d283eee 2017-11-29 stsp
268 7d283eee 2017-11-29 stsp static int
269 7d283eee 2017-11-29 stsp repo_diff_blob(const char *repo_path)
270 7d283eee 2017-11-29 stsp {
271 7d283eee 2017-11-29 stsp const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
272 7d283eee 2017-11-29 stsp const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
273 7d283eee 2017-11-29 stsp const struct got_error *err;
274 7d283eee 2017-11-29 stsp struct got_repository *repo;
275 15a94983 2018-12-23 stsp struct got_object_id *id1, *id2;
276 7d283eee 2017-11-29 stsp struct got_blob_object *blob1;
277 7d283eee 2017-11-29 stsp struct got_blob_object *blob2;
278 354a7e12 2018-02-11 stsp FILE *outfile;
279 5a83d54e 2018-04-01 stsp int i;
280 5a83d54e 2018-04-01 stsp char *line;
281 5a83d54e 2018-04-01 stsp size_t len;
282 5a83d54e 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
283 5a83d54e 2018-04-01 stsp const char *expected_output[] = {
284 0de8fe28 2018-12-24 stsp "blob - 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
285 0de8fe28 2018-12-24 stsp "blob + de7eb21b21c7823a753261aadf7cba35c9580fbf",
286 3b8ef1a8 2018-09-13 stsp "--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
287 3b8ef1a8 2018-09-13 stsp "+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
288 5a83d54e 2018-04-01 stsp "@@ -1,10 +1,10 @@",
289 5a83d54e 2018-04-01 stsp " .PATH:${.CURDIR}/../../lib",
290 5a83d54e 2018-04-01 stsp " ",
291 5a83d54e 2018-04-01 stsp " PROG = repository_test",
292 5a83d54e 2018-04-01 stsp "-SRCS = path.c repository.c error.c refs.c repository_test.c",
293 5a83d54e 2018-04-01 stsp "+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
294 5a83d54e 2018-04-01 stsp " ",
295 5a83d54e 2018-04-01 stsp " CPPFLAGS = -I${.CURDIR}/../../include",
296 5a83d54e 2018-04-01 stsp "-LDADD = -lutil",
297 5a83d54e 2018-04-01 stsp "+LDADD = -lutil -lz",
298 5a83d54e 2018-04-01 stsp " ",
299 5a83d54e 2018-04-01 stsp " NOMAN = yes"
300 5a83d54e 2018-04-01 stsp };
301 7d283eee 2017-11-29 stsp
302 7d283eee 2017-11-29 stsp err = got_repo_open(&repo, repo_path);
303 7d283eee 2017-11-29 stsp if (err != NULL || repo == NULL)
304 7d283eee 2017-11-29 stsp return 0;
305 7d283eee 2017-11-29 stsp
306 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id1, repo, blob1_sha1);
307 15a94983 2018-12-23 stsp if (err != NULL)
308 7d283eee 2017-11-29 stsp return 0;
309 15a94983 2018-12-23 stsp
310 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id2, repo, blob2_sha1);
311 15a94983 2018-12-23 stsp if (err != NULL)
312 7d283eee 2017-11-29 stsp return 0;
313 7d283eee 2017-11-29 stsp
314 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 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 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 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 5a83d54e 2018-04-01 stsp outfile = got_opentemp();
324 5a83d54e 2018-04-01 stsp if (outfile == NULL)
325 5a83d54e 2018-04-01 stsp return 0;
326 54156555 2018-12-24 stsp got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
327 5a83d54e 2018-04-01 stsp rewind(outfile);
328 5a83d54e 2018-04-01 stsp i = 0;
329 5a83d54e 2018-04-01 stsp while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
330 5a83d54e 2018-04-01 stsp test_printf(line);
331 5a83d54e 2018-04-01 stsp test_printf("\n");
332 5a83d54e 2018-04-01 stsp if (i < nitems(expected_output) &&
333 5a83d54e 2018-04-01 stsp strcmp(line, expected_output[i]) != 0) {
334 5a83d54e 2018-04-01 stsp test_printf("diff output mismatch; expected: '%s'\n",
335 5a83d54e 2018-04-01 stsp expected_output[i]);
336 3b8ef1a8 2018-09-13 stsp return 0;
337 5a83d54e 2018-04-01 stsp }
338 5a83d54e 2018-04-01 stsp i++;
339 5a83d54e 2018-04-01 stsp }
340 5a83d54e 2018-04-01 stsp fclose(outfile);
341 82f2fb69 2018-01-26 stsp test_printf("\n");
342 5a83d54e 2018-04-01 stsp if (i != nitems(expected_output) + 1) {
343 5a83d54e 2018-04-01 stsp test_printf("number of lines expected: %d; actual: %d\n",
344 5a83d54e 2018-04-01 stsp nitems(expected_output), i - 1);
345 5a83d54e 2018-04-01 stsp return 0;
346 5a83d54e 2018-04-01 stsp }
347 7d283eee 2017-11-29 stsp
348 7d283eee 2017-11-29 stsp got_object_blob_close(blob1);
349 7d283eee 2017-11-29 stsp got_object_blob_close(blob2);
350 98abbc84 2017-11-30 stsp got_repo_close(repo);
351 98abbc84 2017-11-30 stsp return (err == NULL);
352 98abbc84 2017-11-30 stsp }
353 98abbc84 2017-11-30 stsp
354 98abbc84 2017-11-30 stsp static int
355 98abbc84 2017-11-30 stsp repo_diff_tree(const char *repo_path)
356 98abbc84 2017-11-30 stsp {
357 4a0235dd 2017-11-30 stsp const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
358 a3e2cbea 2017-12-01 stsp const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
359 98abbc84 2017-11-30 stsp const struct got_error *err;
360 98abbc84 2017-11-30 stsp struct got_repository *repo;
361 15a94983 2018-12-23 stsp struct got_object_id *id1;
362 15a94983 2018-12-23 stsp struct got_object_id *id2;
363 98abbc84 2017-11-30 stsp struct got_tree_object *tree1;
364 98abbc84 2017-11-30 stsp struct got_tree_object *tree2;
365 354a7e12 2018-02-11 stsp FILE *outfile;
366 98abbc84 2017-11-30 stsp
367 98abbc84 2017-11-30 stsp err = got_repo_open(&repo, repo_path);
368 98abbc84 2017-11-30 stsp if (err != NULL || repo == NULL)
369 98abbc84 2017-11-30 stsp return 0;
370 98abbc84 2017-11-30 stsp
371 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
372 15a94983 2018-12-23 stsp if (err != NULL)
373 98abbc84 2017-11-30 stsp return 0;
374 15a94983 2018-12-23 stsp err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
375 15a94983 2018-12-23 stsp if (err != NULL)
376 98abbc84 2017-11-30 stsp return 0;
377 98abbc84 2017-11-30 stsp
378 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
379 98abbc84 2017-11-30 stsp if (err != NULL)
380 98abbc84 2017-11-30 stsp return 0;
381 98abbc84 2017-11-30 stsp
382 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
383 98abbc84 2017-11-30 stsp if (err != NULL)
384 98abbc84 2017-11-30 stsp return 0;
385 98abbc84 2017-11-30 stsp
386 354a7e12 2018-02-11 stsp if (!verbose) {
387 354a7e12 2018-02-11 stsp outfile = fopen("/dev/null", "w+");
388 354a7e12 2018-02-11 stsp if (outfile == NULL)
389 354a7e12 2018-02-11 stsp return 0;
390 354a7e12 2018-02-11 stsp } else
391 354a7e12 2018-02-11 stsp outfile = stdout;
392 82f2fb69 2018-01-26 stsp test_printf("\n");
393 54156555 2018-12-24 stsp got_diff_tree(tree1, tree2, "", "", 3, repo, outfile);
394 82f2fb69 2018-01-26 stsp test_printf("\n");
395 98abbc84 2017-11-30 stsp
396 98abbc84 2017-11-30 stsp got_object_tree_close(tree1);
397 98abbc84 2017-11-30 stsp got_object_tree_close(tree2);
398 68482ea3 2017-11-27 stsp got_repo_close(repo);
399 68482ea3 2017-11-27 stsp return (err == NULL);
400 68482ea3 2017-11-27 stsp }
401 b08fe7be 2018-01-26 stsp
402 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
403 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
404 b08fe7be 2018-01-26 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
405 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
406 68482ea3 2017-11-27 stsp
407 82f2fb69 2018-01-26 stsp
408 82f2fb69 2018-01-26 stsp void
409 82f2fb69 2018-01-26 stsp usage(void)
410 82f2fb69 2018-01-26 stsp {
411 82f2fb69 2018-01-26 stsp fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
412 82f2fb69 2018-01-26 stsp }
413 82f2fb69 2018-01-26 stsp
414 4027f31a 2017-11-04 stsp int
415 82f2fb69 2018-01-26 stsp main(int argc, char *argv[])
416 4027f31a 2017-11-04 stsp {
417 b08fe7be 2018-01-26 stsp int test_ok = 0, failure = 0;
418 4027f31a 2017-11-04 stsp const char *repo_path;
419 82f2fb69 2018-01-26 stsp int ch;
420 4027f31a 2017-11-04 stsp
421 2ff12563 2018-09-15 stsp #ifndef PROFILE
422 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath proc exec sendfd", NULL) == -1)
423 f8352b2a 2018-03-12 stsp err(1, "pledge");
424 2ff12563 2018-09-15 stsp #endif
425 f8352b2a 2018-03-12 stsp
426 82f2fb69 2018-01-26 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
427 82f2fb69 2018-01-26 stsp switch (ch) {
428 82f2fb69 2018-01-26 stsp case 'v':
429 82f2fb69 2018-01-26 stsp verbose = 1;
430 82f2fb69 2018-01-26 stsp break;
431 82f2fb69 2018-01-26 stsp default:
432 82f2fb69 2018-01-26 stsp usage();
433 82f2fb69 2018-01-26 stsp return 1;
434 82f2fb69 2018-01-26 stsp }
435 82f2fb69 2018-01-26 stsp }
436 82f2fb69 2018-01-26 stsp argc -= optind;
437 82f2fb69 2018-01-26 stsp argv += optind;
438 82f2fb69 2018-01-26 stsp
439 82f2fb69 2018-01-26 stsp if (argc == 0)
440 4027f31a 2017-11-04 stsp repo_path = GOT_REPO_PATH;
441 82f2fb69 2018-01-26 stsp else if (argc == 1)
442 ff3eb0f2 2018-03-09 stsp repo_path = argv[0];
443 4027f31a 2017-11-04 stsp else {
444 82f2fb69 2018-01-26 stsp usage();
445 4027f31a 2017-11-04 stsp return 1;
446 4027f31a 2017-11-04 stsp }
447 4027f31a 2017-11-04 stsp
448 044e7393 2018-02-11 stsp RUN_TEST(repo_read_tree(repo_path), "read_tree");
449 bfab4d9a 2017-11-12 stsp RUN_TEST(repo_read_log(repo_path), "read_log");
450 68482ea3 2017-11-27 stsp RUN_TEST(repo_read_blob(repo_path), "read_blob");
451 7d283eee 2017-11-29 stsp RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
452 98abbc84 2017-11-30 stsp RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
453 4027f31a 2017-11-04 stsp
454 4027f31a 2017-11-04 stsp return failure ? 1 : 0;
455 4027f31a 2017-11-04 stsp }