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"
38 #include "got_lib_worktree.h"
39 #include "got_lib_path.h"
41 #define GOT_REPO_PATH "../../../"
43 static int verbose;
45 void
46 test_printf(char *fmt, ...)
47 {
48 va_list ap;
50 if (!verbose)
51 return;
53 va_start(ap, fmt);
54 vprintf(fmt, ap);
55 va_end(ap);
56 }
58 static int
59 remove_got_dir(const char *worktree_path)
60 {
61 char *path;
63 if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
64 return 0;
65 rmdir(path);
66 free(path);
67 return 1;
68 }
70 static int
71 remove_meta_file(const char *worktree_path, const char *name)
72 {
73 char *path;
75 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
76 name) == -1)
77 return 0;
78 unlink(path);
79 free(path);
80 return 1;
81 }
83 static int
84 remove_worktree(const char *worktree_path)
85 {
86 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD))
87 return 0;
88 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
89 return 0;
90 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
91 return 0;
92 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
93 return 0;
94 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
95 return 0;
96 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
97 return 0;
98 if (!remove_got_dir(worktree_path))
99 return 0;
100 if (rmdir(worktree_path) == -1)
101 return 0;
102 return 1;
105 static int
106 read_meta_file(char **content, const char *path)
108 FILE *f;
109 size_t len;
110 const char delim[3] = {'\0', '\0', '\0'};
111 int ret = 0;
113 f = fopen(path, "r");
114 if (f == NULL)
115 return errno;
117 *content = fparseln(f, &len, NULL, delim, 0);
118 if (*content == NULL)
119 ret = errno;
120 fclose(f);
121 return ret;
124 static int
125 check_meta_file_exists(const char *worktree_path, const char *name)
127 struct stat sb;
128 char *path;
129 int ret = 0;
131 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
132 name) == -1)
133 return 0;
134 if (stat(path, &sb) == 0)
135 ret = 1;
136 if (verbose) {
137 char *content;
138 if (read_meta_file(&content, path) == 0) {
139 test_printf("%s:\t%s\n", name, content);
140 free(content);
143 free(path);
144 return ret;
147 static int
148 worktree_init(const char *repo_path)
150 const struct got_error *err;
151 struct got_repository *repo = NULL;
152 struct got_reference *head_ref = NULL;
153 char worktree_path[PATH_MAX];
154 int ok = 0;
156 err = got_repo_open(&repo, repo_path);
157 if (err != NULL || repo == NULL)
158 goto done;
159 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
160 if (err != NULL || head_ref == NULL)
161 goto done;
163 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
164 if (mkdtemp(worktree_path) == NULL)
165 goto done;
167 err = got_worktree_init(worktree_path, head_ref, "/", repo);
168 if (err != NULL)
169 goto done;
171 /* Ensure required files were created. */
172 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD))
173 goto done;
174 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
175 goto done;
176 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
177 goto done;
178 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
179 goto done;
180 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
181 goto done;
182 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
183 goto done;
185 if (!remove_worktree(worktree_path))
186 goto done;
187 ok = 1;
188 done:
189 if (head_ref)
190 got_ref_close(head_ref);
191 if (repo)
192 got_repo_close(repo);
193 return ok;
196 static int
197 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
199 FILE *f;
200 char *s = "This file should not be here\n";
201 int ret = 1;
203 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
204 name) == -1)
205 return 0;
206 f = fopen(*path, "w+");
207 if (f == NULL) {
208 free(*path);
209 return 0;
211 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
212 free(*path);
213 ret = 0;
215 fclose(f);
216 return ret;
219 static int
220 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
221 const char *worktree_path, char *name)
223 const struct got_error *err;
224 char *path;
225 int ret = 0;
226 struct got_reference *head_ref = NULL;
228 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
229 return 0;
231 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
232 if (err != NULL || head_ref == NULL)
233 return 0;
235 err = got_worktree_init(worktree_path, head_ref, "/", repo);
236 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
237 (*ok)++;
238 ret = 1;
240 unlink(path);
241 free(path);
242 got_ref_close(head_ref);
243 return ret;
246 static int
247 worktree_init_exists(const char *repo_path)
249 const struct got_error *err;
250 struct got_repository *repo = NULL;
251 char worktree_path[PATH_MAX];
252 char *gotpath = NULL;
253 int ok = 0;
255 err = got_repo_open(&repo, repo_path);
256 if (err != NULL || repo == NULL)
257 goto done;
258 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
259 if (mkdtemp(worktree_path) == NULL)
260 goto done;
261 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
262 goto done;
264 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
265 == -1)
266 goto done;
267 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
268 goto done;
270 /* Create files which got_worktree_init() will try to create as well. */
271 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
272 GOT_WORKTREE_HEAD))
273 goto done;
274 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
275 GOT_WORKTREE_LOCK))
276 goto done;
277 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
278 GOT_WORKTREE_FILE_INDEX))
279 goto done;
280 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
281 GOT_WORKTREE_REPOSITORY))
282 goto done;
283 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
284 GOT_WORKTREE_PATH_PREFIX))
285 goto done;
286 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
287 GOT_WORKTREE_FORMAT))
288 goto done;
290 done:
291 if (repo)
292 got_repo_close(repo);
293 free(gotpath);
294 if (ok == 6)
295 remove_worktree(worktree_path);
296 return (ok == 6);
299 static void
300 process_cb(void *arg, const char *path)
304 static int
305 worktree_checkout(const char *repo_path)
307 const struct got_error *err;
308 struct got_repository *repo = NULL;
309 struct got_reference *head_ref = NULL;
310 struct got_worktree *worktree = NULL;
311 char *makefile_path = NULL, *cfile_path = NULL;
312 char worktree_path[PATH_MAX];
313 int ok = 0;
314 struct stat sb;
316 err = got_repo_open(&repo, repo_path);
317 if (err != NULL || repo == NULL)
318 goto done;
319 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
320 if (err != NULL || head_ref == NULL)
321 goto done;
323 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
324 if (mkdtemp(worktree_path) == NULL)
325 goto done;
327 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
328 repo);
329 if (err != NULL)
330 goto done;
332 err = got_worktree_open(&worktree, worktree_path);
333 if (err != NULL)
334 goto done;
336 err = got_worktree_checkout_files(worktree, head_ref, repo,
337 process_cb, NULL);
338 if (err != NULL)
339 goto done;
341 test_printf("checked out %s\n", worktree_path);
343 /* The work tree should contain a Makefile and worktree_test.c. */
344 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
345 goto done;
346 if (stat(makefile_path, &sb) != 0)
347 goto done;
348 else
349 unlink(makefile_path);
350 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
351 goto done;
352 if (stat(cfile_path, &sb) != 0)
353 goto done;
354 else
355 unlink(cfile_path);
357 if (!remove_worktree(worktree_path))
358 goto done;
360 ok = 1;
361 done:
362 if (worktree)
363 got_worktree_close(worktree);
364 if (head_ref)
365 got_ref_close(head_ref);
366 if (repo)
367 got_repo_close(repo);
368 free(makefile_path);
369 free(cfile_path);
370 return ok;
373 #define RUN_TEST(expr, name) \
374 { test_ok = (expr); \
375 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
376 failure = (failure || !test_ok); }
379 void
380 usage(void)
382 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
385 int
386 main(int argc, char *argv[])
388 int test_ok = 0, failure = 0;
389 const char *repo_path;
390 int ch;
392 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
393 err(1, "pledge");
395 while ((ch = getopt(argc, argv, "v")) != -1) {
396 switch (ch) {
397 case 'v':
398 verbose = 1;
399 break;
400 default:
401 usage();
402 return 1;
405 argc -= optind;
406 argv += optind;
408 if (argc == 0)
409 repo_path = GOT_REPO_PATH;
410 else if (argc == 1)
411 repo_path = argv[0];
412 else {
413 usage();
414 return 1;
417 RUN_TEST(worktree_init(repo_path), "init");
418 RUN_TEST(worktree_init_exists(repo_path), "init exists");
419 RUN_TEST(worktree_checkout(repo_path), "checkout");
421 return failure ? 1 : 0;