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_refs.h"
35 #include "got_repository.h"
36 #include "got_worktree.h"
38 #include "got_worktree_lib.h"
39 #include "got_path_lib.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 int
300 worktree_checkout(const char *repo_path)
302 const struct got_error *err;
303 struct got_repository *repo = NULL;
304 struct got_reference *head_ref = NULL;
305 struct got_worktree *worktree = NULL;
306 char *makefile_path = NULL, *cfile_path = NULL;
307 char worktree_path[PATH_MAX];
308 int ok = 0;
309 struct stat sb;
311 err = got_repo_open(&repo, repo_path);
312 if (err != NULL || repo == NULL)
313 goto done;
314 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
315 if (err != NULL || head_ref == NULL)
316 goto done;
318 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
319 if (mkdtemp(worktree_path) == NULL)
320 goto done;
322 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
323 repo);
324 if (err != NULL)
325 goto done;
327 err = got_worktree_open(&worktree, worktree_path);
328 if (err != NULL)
329 goto done;
331 err = got_worktree_checkout_files(worktree, head_ref, repo);
332 if (err != NULL)
333 goto done;
335 test_printf("checked out %s\n", worktree_path);
337 /* The work tree should contain a Makefile and worktree_test.c. */
338 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
339 goto done;
340 if (stat(makefile_path, &sb) != 0)
341 goto done;
342 else
343 unlink(makefile_path);
344 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
345 goto done;
346 if (stat(cfile_path, &sb) != 0)
347 goto done;
348 else
349 unlink(cfile_path);
351 if (!remove_worktree(worktree_path))
352 goto done;
354 ok = 1;
355 done:
356 if (worktree)
357 got_worktree_close(worktree);
358 if (head_ref)
359 got_ref_close(head_ref);
360 if (repo)
361 got_repo_close(repo);
362 free(makefile_path);
363 free(cfile_path);
364 return ok;
367 #define RUN_TEST(expr, name) \
368 { test_ok = (expr); \
369 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
370 failure = (failure || !test_ok); }
373 void
374 usage(void)
376 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
379 int
380 main(int argc, char *argv[])
382 int test_ok = 0, failure = 0;
383 const char *repo_path;
384 int ch;
386 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
387 err(1, "pledge");
389 while ((ch = getopt(argc, argv, "v")) != -1) {
390 switch (ch) {
391 case 'v':
392 verbose = 1;
393 break;
394 default:
395 usage();
396 return 1;
399 argc -= optind;
400 argv += optind;
402 if (argc == 0)
403 repo_path = GOT_REPO_PATH;
404 else if (argc == 1)
405 repo_path = argv[0];
406 else {
407 usage();
408 return 1;
411 RUN_TEST(worktree_init(repo_path), "init");
412 RUN_TEST(worktree_init_exists(repo_path), "init exists");
413 RUN_TEST(worktree_checkout(repo_path), "checkout");
415 return failure ? 1 : 0;