2 * Copyright (c) 2018, 2019 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)
124 if (fclose(f) != 0 && ret == 0)
130 check_meta_file_exists(const char *worktree_path, const char *name)
136 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
139 if (stat(path, &sb) == 0)
143 if (read_meta_file(&content, path) == 0) {
144 test_printf("%s:\t%s\n", name, content);
153 worktree_init(const char *repo_path)
155 const struct got_error *err;
156 struct got_repository *repo = NULL;
157 struct got_reference *head_ref = NULL;
158 char worktree_path[PATH_MAX];
161 err = got_repo_open(&repo, repo_path);
162 if (err != NULL || repo == NULL)
164 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
165 if (err != NULL || head_ref == NULL)
168 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
169 if (mkdtemp(worktree_path) == NULL)
172 err = got_worktree_init(worktree_path, head_ref, "/", repo);
176 /* Ensure required files were created. */
177 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD_REF))
179 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
181 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
183 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
185 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
187 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
189 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
192 if (!remove_worktree(worktree_path))
197 got_ref_close(head_ref);
199 got_repo_close(repo);
204 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
207 char *s = "This file should not be here\n";
210 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
213 f = fopen(*path, "w+");
218 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
228 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
229 const char *worktree_path, char *name)
231 const struct got_error *err;
234 struct got_reference *head_ref = NULL;
236 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
239 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
240 if (err != NULL || head_ref == NULL)
243 err = got_worktree_init(worktree_path, head_ref, "/", repo);
244 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
250 got_ref_close(head_ref);
255 worktree_init_exists(const char *repo_path)
257 const struct got_error *err;
258 struct got_repository *repo = NULL;
259 char worktree_path[PATH_MAX];
260 char *gotpath = NULL;
263 err = got_repo_open(&repo, repo_path);
264 if (err != NULL || repo == NULL)
266 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
267 if (mkdtemp(worktree_path) == NULL)
269 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
272 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
275 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
278 /* Create files which got_worktree_init() will try to create as well. */
279 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
280 GOT_WORKTREE_HEAD_REF))
282 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
283 GOT_WORKTREE_BASE_COMMIT))
285 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
288 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
289 GOT_WORKTREE_FILE_INDEX))
291 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
292 GOT_WORKTREE_REPOSITORY))
294 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
295 GOT_WORKTREE_PATH_PREFIX))
297 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
298 GOT_WORKTREE_FORMAT))
303 got_repo_close(repo);
306 remove_worktree(worktree_path);
311 progress_cb(void *arg, unsigned char status, const char *path)
316 worktree_checkout(const char *repo_path)
318 const struct got_error *err;
319 struct got_repository *repo = NULL;
320 struct got_reference *head_ref = NULL;
321 struct got_worktree *worktree = NULL;
322 char *makefile_path = NULL, *cfile_path = NULL;
323 char worktree_path[PATH_MAX];
327 err = got_repo_open(&repo, repo_path);
328 if (err != NULL || repo == NULL)
330 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
331 if (err != NULL || head_ref == NULL)
334 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
335 if (mkdtemp(worktree_path) == NULL)
338 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
343 err = got_worktree_open(&worktree, worktree_path);
347 err = got_worktree_checkout_files(worktree, repo, progress_cb, NULL,
352 test_printf("checked out %s\n", worktree_path);
354 /* The work tree should contain a Makefile and worktree_test.c. */
355 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
357 if (stat(makefile_path, &sb) != 0)
360 unlink(makefile_path);
361 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
363 if (stat(cfile_path, &sb) != 0)
368 if (!remove_worktree(worktree_path))
374 got_worktree_close(worktree);
376 got_ref_close(head_ref);
378 got_repo_close(repo);
384 #define RUN_TEST(expr, name) \
385 { test_ok = (expr); \
386 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
387 failure = (failure || !test_ok); }
393 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
397 main(int argc, char *argv[])
399 int test_ok = 0, failure = 0;
400 const char *repo_path;
405 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
406 "unveil", NULL) == -1)
410 while ((ch = getopt(argc, argv, "v")) != -1) {
424 repo_path = GOT_REPO_PATH;
432 cwd = getcwd(NULL, 0);
435 if (unveil(cwd, "rwc") != 0)
439 if (unveil("/tmp", "rwc") != 0)
442 if (unveil(repo_path, "r") != 0)
445 if (got_privsep_unveil_exec_helpers() != NULL)
448 if (unveil(NULL, NULL) != 0)
451 RUN_TEST(worktree_init(repo_path), "init");
452 RUN_TEST(worktree_init_exists(repo_path), "init exists");
453 RUN_TEST(worktree_checkout(repo_path), "checkout");
455 return failure ? 1 : 0;