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>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_reference.h"
36 #include "got_repository.h"
38 #include "got_worktree.h"
39 #include "got_opentemp.h"
40 #include "got_privsep.h"
42 #include "got_lib_worktree.h"
44 #define GOT_REPO_PATH "../../../"
49 test_printf(char *fmt, ...)
62 remove_got_dir(const char *worktree_path)
66 if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
74 remove_meta_file(const char *worktree_path, const char *name)
78 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
86 static const struct got_error *
87 remove_worktree_base_ref(struct got_worktree *worktree,
88 struct got_repository *repo)
90 const struct got_error *err = NULL;
91 struct got_reference *base_ref = NULL;
94 err = got_worktree_get_base_ref_name(&refname, worktree);
98 err = got_ref_open(&base_ref, repo, refname, 0);
102 err = got_ref_delete(base_ref, repo);
105 got_ref_close(base_ref);
112 remove_worktree(const char *worktree_path)
114 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD_REF))
116 if (!remove_meta_file(worktree_path, GOT_WORKTREE_BASE_COMMIT))
118 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
120 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
122 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
124 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
126 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
128 if (!remove_meta_file(worktree_path, GOT_WORKTREE_UUID))
130 if (!remove_got_dir(worktree_path))
132 if (rmdir(worktree_path) == -1)
138 read_meta_file(char **content, const char *path)
142 const char delim[3] = {'\0', '\0', '\0'};
145 f = fopen(path, "r");
149 *content = fparseln(f, &len, NULL, delim, 0);
150 if (*content == NULL)
152 if (fclose(f) != 0 && ret == 0)
158 check_meta_file_exists(const char *worktree_path, const char *name)
164 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
167 if (stat(path, &sb) == 0)
171 if (read_meta_file(&content, path) == 0) {
172 test_printf("%s:\t%s\n", name, content);
181 worktree_init(const char *repo_path)
183 const struct got_error *err;
184 struct got_repository *repo = NULL;
185 struct got_reference *head_ref = NULL;
186 char worktree_path[PATH_MAX];
189 err = got_repo_open(&repo, repo_path);
190 if (err != NULL || repo == NULL)
192 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
193 if (err != NULL || head_ref == NULL)
196 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
197 if (mkdtemp(worktree_path) == NULL)
200 err = got_worktree_init(worktree_path, head_ref, "/", repo);
204 /* Ensure required files were created. */
205 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD_REF))
207 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
209 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
211 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
213 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
215 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
217 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
219 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_UUID))
222 if (!remove_worktree(worktree_path))
227 got_ref_close(head_ref);
229 got_repo_close(repo);
234 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
237 char *s = "This file should not be here\n";
240 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
243 f = fopen(*path, "w+");
248 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
258 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
259 const char *worktree_path, char *name)
261 const struct got_error *err;
264 struct got_reference *head_ref = NULL;
266 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
269 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
270 if (err != NULL || head_ref == NULL)
273 err = got_worktree_init(worktree_path, head_ref, "/", repo);
274 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
280 got_ref_close(head_ref);
285 worktree_init_exists(const char *repo_path)
287 const struct got_error *err;
288 struct got_repository *repo = NULL;
289 char worktree_path[PATH_MAX];
290 char *gotpath = NULL;
293 err = got_repo_open(&repo, repo_path);
294 if (err != NULL || repo == NULL)
296 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
297 if (mkdtemp(worktree_path) == NULL)
299 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
302 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
305 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
308 /* Create files which got_worktree_init() will try to create as well. */
309 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
310 GOT_WORKTREE_HEAD_REF))
312 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
313 GOT_WORKTREE_BASE_COMMIT))
315 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
318 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
319 GOT_WORKTREE_FILE_INDEX))
321 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
322 GOT_WORKTREE_REPOSITORY))
324 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
325 GOT_WORKTREE_PATH_PREFIX))
327 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
328 GOT_WORKTREE_FORMAT))
333 got_repo_close(repo);
336 remove_worktree(worktree_path);
340 static const struct got_error *
341 progress_cb(void *arg, unsigned char status, const char *path)
347 worktree_checkout(const char *repo_path)
349 const struct got_error *err;
350 struct got_repository *repo = NULL;
351 struct got_reference *head_ref = NULL;
352 struct got_worktree *worktree = NULL;
353 char *makefile_path = NULL, *cfile_path = NULL;
354 char worktree_path[PATH_MAX];
358 err = got_repo_open(&repo, repo_path);
359 if (err != NULL || repo == NULL)
361 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
362 if (err != NULL || head_ref == NULL)
365 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
366 if (mkdtemp(worktree_path) == NULL)
369 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
374 err = got_worktree_open(&worktree, worktree_path);
378 err = got_worktree_checkout_files(worktree, "", repo, progress_cb, NULL,
383 test_printf("checked out %s\n", worktree_path);
385 /* The work tree should contain a Makefile and worktree_test.c. */
386 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
388 if (stat(makefile_path, &sb) != 0)
391 unlink(makefile_path);
392 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
394 if (stat(cfile_path, &sb) != 0)
399 err = remove_worktree_base_ref(worktree, repo);
402 if (!remove_worktree(worktree_path))
408 got_worktree_close(worktree);
410 got_ref_close(head_ref);
412 got_repo_close(repo);
418 #define RUN_TEST(expr, name) \
419 { test_ok = (expr); \
420 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
421 failure = (failure || !test_ok); }
427 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
431 main(int argc, char *argv[])
433 int test_ok = 0, failure = 0;
434 const char *repo_path;
439 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
440 "unveil", NULL) == -1)
444 while ((ch = getopt(argc, argv, "v")) != -1) {
458 repo_path = GOT_REPO_PATH;
466 cwd = getcwd(NULL, 0);
469 if (unveil(cwd, "rwc") != 0)
473 if (unveil("/tmp", "rwc") != 0)
476 if (unveil(repo_path, "rwc") != 0)
479 if (got_privsep_unveil_exec_helpers() != NULL)
482 if (unveil(NULL, NULL) != 0)
485 RUN_TEST(worktree_init(repo_path), "init");
486 RUN_TEST(worktree_init_exists(repo_path), "init exists");
487 RUN_TEST(worktree_checkout(repo_path), "checkout");
489 return failure ? 1 : 0;