2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/param.h>
18 #include <sys/queue.h>
19 #include <sys/limits.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"
38 #include "got_privsep.h"
40 #include "got_lib_worktree.h"
41 #include "got_lib_path.h"
43 #define GOT_REPO_PATH "../../../"
48 test_printf(char *fmt, ...)
61 remove_got_dir(const char *worktree_path)
65 if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
73 remove_meta_file(const char *worktree_path, const char *name)
77 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
86 remove_worktree(const char *worktree_path)
88 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD_REF))
90 if (!remove_meta_file(worktree_path, GOT_WORKTREE_BASE_COMMIT))
92 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
94 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
96 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
98 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
100 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
102 if (!remove_got_dir(worktree_path))
104 if (rmdir(worktree_path) == -1)
110 read_meta_file(char **content, const char *path)
114 const char delim[3] = {'\0', '\0', '\0'};
117 f = fopen(path, "r");
121 *content = fparseln(f, &len, NULL, delim, 0);
122 if (*content == NULL)
129 check_meta_file_exists(const char *worktree_path, const char *name)
135 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
138 if (stat(path, &sb) == 0)
142 if (read_meta_file(&content, path) == 0) {
143 test_printf("%s:\t%s\n", name, content);
152 worktree_init(const char *repo_path)
154 const struct got_error *err;
155 struct got_repository *repo = NULL;
156 struct got_reference *head_ref = NULL;
157 char worktree_path[PATH_MAX];
160 err = got_repo_open(&repo, repo_path);
161 if (err != NULL || repo == NULL)
163 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
164 if (err != NULL || head_ref == NULL)
167 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
168 if (mkdtemp(worktree_path) == NULL)
171 err = got_worktree_init(worktree_path, head_ref, "/", repo);
175 /* Ensure required files were created. */
176 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD_REF))
178 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
180 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
182 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
184 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
186 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
188 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
191 if (!remove_worktree(worktree_path))
196 got_ref_close(head_ref);
198 got_repo_close(repo);
203 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
206 char *s = "This file should not be here\n";
209 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
212 f = fopen(*path, "w+");
217 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
226 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
227 const char *worktree_path, char *name)
229 const struct got_error *err;
232 struct got_reference *head_ref = NULL;
234 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
237 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
238 if (err != NULL || head_ref == NULL)
241 err = got_worktree_init(worktree_path, head_ref, "/", repo);
242 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
248 got_ref_close(head_ref);
253 worktree_init_exists(const char *repo_path)
255 const struct got_error *err;
256 struct got_repository *repo = NULL;
257 char worktree_path[PATH_MAX];
258 char *gotpath = NULL;
261 err = got_repo_open(&repo, repo_path);
262 if (err != NULL || repo == NULL)
264 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
265 if (mkdtemp(worktree_path) == NULL)
267 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
270 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
273 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
276 /* Create files which got_worktree_init() will try to create as well. */
277 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
278 GOT_WORKTREE_HEAD_REF))
280 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
281 GOT_WORKTREE_BASE_COMMIT))
283 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
286 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
287 GOT_WORKTREE_FILE_INDEX))
289 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
290 GOT_WORKTREE_REPOSITORY))
292 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
293 GOT_WORKTREE_PATH_PREFIX))
295 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
296 GOT_WORKTREE_FORMAT))
301 got_repo_close(repo);
304 remove_worktree(worktree_path);
309 progress_cb(void *arg, unsigned char status, const char *path)
314 worktree_checkout(const char *repo_path)
316 const struct got_error *err;
317 struct got_repository *repo = NULL;
318 struct got_reference *head_ref = NULL;
319 struct got_worktree *worktree = NULL;
320 char *makefile_path = NULL, *cfile_path = NULL;
321 char worktree_path[PATH_MAX];
325 err = got_repo_open(&repo, repo_path);
326 if (err != NULL || repo == NULL)
328 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
329 if (err != NULL || head_ref == NULL)
332 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
333 if (mkdtemp(worktree_path) == NULL)
336 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
341 err = got_worktree_open(&worktree, worktree_path);
345 err = got_worktree_checkout_files(worktree, repo, progress_cb, NULL,
350 test_printf("checked out %s\n", worktree_path);
352 /* The work tree should contain a Makefile and worktree_test.c. */
353 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
355 if (stat(makefile_path, &sb) != 0)
358 unlink(makefile_path);
359 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
361 if (stat(cfile_path, &sb) != 0)
366 if (!remove_worktree(worktree_path))
372 got_worktree_close(worktree);
374 got_ref_close(head_ref);
376 got_repo_close(repo);
382 #define RUN_TEST(expr, name) \
383 { test_ok = (expr); \
384 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
385 failure = (failure || !test_ok); }
391 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
395 main(int argc, char *argv[])
397 int test_ok = 0, failure = 0;
398 const char *repo_path;
403 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
408 while ((ch = getopt(argc, argv, "v")) != -1) {
422 repo_path = GOT_REPO_PATH;
430 cwd = getcwd(NULL, 0);
433 if (unveil(cwd, "rwc") != 0)
437 if (unveil("/tmp", "rwc") != 0)
440 if (unveil(repo_path, "r") != 0)
443 if (got_privsep_unveil_exec_helpers() != NULL)
446 if (unveil(NULL, NULL) != 0)
449 RUN_TEST(worktree_init(repo_path), "init");
450 RUN_TEST(worktree_init_exists(repo_path), "init exists");
451 RUN_TEST(worktree_checkout(repo_path), "checkout");
453 return failure ? 1 : 0;