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