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