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