Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/param.h>
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/stat.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <util.h>
29 #include <err.h>
30 #include <unistd.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_reference.h"
35 #include "got_repository.h"
36 #include "got_worktree.h"
37 #include "got_opentemp.h"
39 #include "got_lib_worktree.h"
40 #include "got_lib_path.h"
42 #define GOT_REPO_PATH "../../../"
44 static int verbose;
46 void
47 test_printf(char *fmt, ...)
48 {
49 va_list ap;
51 if (!verbose)
52 return;
54 va_start(ap, fmt);
55 vprintf(fmt, ap);
56 va_end(ap);
57 }
59 static int
60 remove_got_dir(const char *worktree_path)
61 {
62 char *path;
64 if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
65 return 0;
66 rmdir(path);
67 free(path);
68 return 1;
69 }
71 static int
72 remove_meta_file(const char *worktree_path, const char *name)
73 {
74 char *path;
76 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
77 name) == -1)
78 return 0;
79 unlink(path);
80 free(path);
81 return 1;
82 }
84 static int
85 remove_worktree(const char *worktree_path)
86 {
87 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD))
88 return 0;
89 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
90 return 0;
91 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
92 return 0;
93 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
94 return 0;
95 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
96 return 0;
97 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
98 return 0;
99 if (!remove_got_dir(worktree_path))
100 return 0;
101 if (rmdir(worktree_path) == -1)
102 return 0;
103 return 1;
106 static int
107 read_meta_file(char **content, const char *path)
109 FILE *f;
110 size_t len;
111 const char delim[3] = {'\0', '\0', '\0'};
112 int ret = 0;
114 f = fopen(path, "r");
115 if (f == NULL)
116 return errno;
118 *content = fparseln(f, &len, NULL, delim, 0);
119 if (*content == NULL)
120 ret = errno;
121 fclose(f);
122 return ret;
125 static int
126 check_meta_file_exists(const char *worktree_path, const char *name)
128 struct stat sb;
129 char *path;
130 int ret = 0;
132 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
133 name) == -1)
134 return 0;
135 if (stat(path, &sb) == 0)
136 ret = 1;
137 if (verbose) {
138 char *content;
139 if (read_meta_file(&content, path) == 0) {
140 test_printf("%s:\t%s\n", name, content);
141 free(content);
144 free(path);
145 return ret;
148 static int
149 worktree_init(const char *repo_path)
151 const struct got_error *err;
152 struct got_repository *repo = NULL;
153 struct got_reference *head_ref = NULL;
154 char worktree_path[PATH_MAX];
155 int ok = 0;
157 err = got_repo_open(&repo, repo_path);
158 if (err != NULL || repo == NULL)
159 goto done;
160 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
161 if (err != NULL || head_ref == NULL)
162 goto done;
164 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
165 if (mkdtemp(worktree_path) == NULL)
166 goto done;
168 err = got_worktree_init(worktree_path, head_ref, "/", repo);
169 if (err != NULL)
170 goto done;
172 /* Ensure required files were created. */
173 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD))
174 goto done;
175 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
176 goto done;
177 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
178 goto done;
179 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
180 goto done;
181 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
182 goto done;
183 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
184 goto done;
186 if (!remove_worktree(worktree_path))
187 goto done;
188 ok = 1;
189 done:
190 if (head_ref)
191 got_ref_close(head_ref);
192 if (repo)
193 got_repo_close(repo);
194 return ok;
197 static int
198 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
200 FILE *f;
201 char *s = "This file should not be here\n";
202 int ret = 1;
204 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
205 name) == -1)
206 return 0;
207 f = fopen(*path, "w+");
208 if (f == NULL) {
209 free(*path);
210 return 0;
212 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
213 free(*path);
214 ret = 0;
216 fclose(f);
217 return ret;
220 static int
221 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
222 const char *worktree_path, char *name)
224 const struct got_error *err;
225 char *path;
226 int ret = 0;
227 struct got_reference *head_ref = NULL;
229 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
230 return 0;
232 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
233 if (err != NULL || head_ref == NULL)
234 return 0;
236 err = got_worktree_init(worktree_path, head_ref, "/", repo);
237 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
238 (*ok)++;
239 ret = 1;
241 unlink(path);
242 free(path);
243 got_ref_close(head_ref);
244 return ret;
247 static int
248 worktree_init_exists(const char *repo_path)
250 const struct got_error *err;
251 struct got_repository *repo = NULL;
252 char worktree_path[PATH_MAX];
253 char *gotpath = NULL;
254 int ok = 0;
256 err = got_repo_open(&repo, repo_path);
257 if (err != NULL || repo == NULL)
258 goto done;
259 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
260 if (mkdtemp(worktree_path) == NULL)
261 goto done;
262 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
263 goto done;
265 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
266 == -1)
267 goto done;
268 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
269 goto done;
271 /* Create files which got_worktree_init() will try to create as well. */
272 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
273 GOT_WORKTREE_HEAD))
274 goto done;
275 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
276 GOT_WORKTREE_LOCK))
277 goto done;
278 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
279 GOT_WORKTREE_FILE_INDEX))
280 goto done;
281 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
282 GOT_WORKTREE_REPOSITORY))
283 goto done;
284 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
285 GOT_WORKTREE_PATH_PREFIX))
286 goto done;
287 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
288 GOT_WORKTREE_FORMAT))
289 goto done;
291 done:
292 if (repo)
293 got_repo_close(repo);
294 free(gotpath);
295 if (ok == 6)
296 remove_worktree(worktree_path);
297 return (ok == 6);
300 static void
301 process_cb(void *arg, const char *path)
305 static int
306 worktree_checkout(const char *repo_path)
308 const struct got_error *err;
309 struct got_repository *repo = NULL;
310 struct got_reference *head_ref = NULL;
311 struct got_worktree *worktree = NULL;
312 char *makefile_path = NULL, *cfile_path = NULL;
313 char worktree_path[PATH_MAX];
314 int ok = 0;
315 struct stat sb;
317 err = got_repo_open(&repo, repo_path);
318 if (err != NULL || repo == NULL)
319 goto done;
320 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
321 if (err != NULL || head_ref == NULL)
322 goto done;
324 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
325 if (mkdtemp(worktree_path) == NULL)
326 goto done;
328 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
329 repo);
330 if (err != NULL)
331 goto done;
333 err = got_worktree_open(&worktree, worktree_path);
334 if (err != NULL)
335 goto done;
337 err = got_worktree_checkout_files(worktree, head_ref, repo,
338 process_cb, NULL, NULL, NULL);
339 if (err != NULL)
340 goto done;
342 test_printf("checked out %s\n", worktree_path);
344 /* The work tree should contain a Makefile and worktree_test.c. */
345 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
346 goto done;
347 if (stat(makefile_path, &sb) != 0)
348 goto done;
349 else
350 unlink(makefile_path);
351 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
352 goto done;
353 if (stat(cfile_path, &sb) != 0)
354 goto done;
355 else
356 unlink(cfile_path);
358 if (!remove_worktree(worktree_path))
359 goto done;
361 ok = 1;
362 done:
363 if (worktree)
364 got_worktree_close(worktree);
365 if (head_ref)
366 got_ref_close(head_ref);
367 if (repo)
368 got_repo_close(repo);
369 free(makefile_path);
370 free(cfile_path);
371 return ok;
374 #define RUN_TEST(expr, name) \
375 { test_ok = (expr); \
376 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
377 failure = (failure || !test_ok); }
380 void
381 usage(void)
383 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
386 int
387 main(int argc, char *argv[])
389 int test_ok = 0, failure = 0;
390 const char *repo_path;
391 int ch;
393 #ifndef PROFILE
394 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
395 == -1)
396 err(1, "pledge");
397 #endif
399 while ((ch = getopt(argc, argv, "v")) != -1) {
400 switch (ch) {
401 case 'v':
402 verbose = 1;
403 break;
404 default:
405 usage();
406 return 1;
409 argc -= optind;
410 argv += optind;
412 if (argc == 0)
413 repo_path = GOT_REPO_PATH;
414 else if (argc == 1)
415 repo_path = argv[0];
416 else {
417 usage();
418 return 1;
421 RUN_TEST(worktree_init(repo_path), "init");
422 RUN_TEST(worktree_init_exists(repo_path), "init exists");
423 RUN_TEST(worktree_checkout(repo_path), "checkout");
425 return failure ? 1 : 0;