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