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"
38 #include "got_lib_worktree.h"
39 #include "got_lib_path.h"
41 #define GOT_REPO_PATH "../../../"
46 test_printf(char *fmt, ...)
59 remove_got_dir(const char *worktree_path)
63 if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
71 remove_meta_file(const char *worktree_path, const char *name)
75 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
84 remove_worktree(const char *worktree_path)
86 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD))
88 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
90 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
92 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
94 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
96 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
98 if (!remove_got_dir(worktree_path))
100 if (rmdir(worktree_path) == -1)
106 read_meta_file(char **content, const char *path)
110 const char delim[3] = {'\0', '\0', '\0'};
113 f = fopen(path, "r");
117 *content = fparseln(f, &len, NULL, delim, 0);
118 if (*content == NULL)
125 check_meta_file_exists(const char *worktree_path, const char *name)
131 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
134 if (stat(path, &sb) == 0)
138 if (read_meta_file(&content, path) == 0) {
139 test_printf("%s:\t%s\n", name, content);
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];
156 err = got_repo_open(&repo, repo_path);
157 if (err != NULL || repo == NULL)
159 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
160 if (err != NULL || head_ref == NULL)
163 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
164 if (mkdtemp(worktree_path) == NULL)
167 err = got_worktree_init(worktree_path, head_ref, "/", repo);
171 /* Ensure required files were created. */
172 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD))
174 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
176 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
178 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
180 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
182 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
185 if (!remove_worktree(worktree_path))
190 got_ref_close(head_ref);
192 got_repo_close(repo);
197 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
200 char *s = "This file should not be here\n";
203 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
206 f = fopen(*path, "w+");
211 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
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;
226 struct got_reference *head_ref = NULL;
228 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
231 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
232 if (err != NULL || head_ref == NULL)
235 err = got_worktree_init(worktree_path, head_ref, "/", repo);
236 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
242 got_ref_close(head_ref);
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;
255 err = got_repo_open(&repo, repo_path);
256 if (err != NULL || repo == NULL)
258 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
259 if (mkdtemp(worktree_path) == NULL)
261 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
264 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
267 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
270 /* Create files which got_worktree_init() will try to create as well. */
271 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
274 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
277 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
278 GOT_WORKTREE_FILE_INDEX))
280 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
281 GOT_WORKTREE_REPOSITORY))
283 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
284 GOT_WORKTREE_PATH_PREFIX))
286 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
287 GOT_WORKTREE_FORMAT))
292 got_repo_close(repo);
295 remove_worktree(worktree_path);
300 process_cb(void *arg, const char *path)
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];
316 err = got_repo_open(&repo, repo_path);
317 if (err != NULL || repo == NULL)
319 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
320 if (err != NULL || head_ref == NULL)
323 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
324 if (mkdtemp(worktree_path) == NULL)
327 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
332 err = got_worktree_open(&worktree, worktree_path);
336 err = got_worktree_checkout_files(worktree, head_ref, repo,
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)
346 if (stat(makefile_path, &sb) != 0)
349 unlink(makefile_path);
350 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
352 if (stat(cfile_path, &sb) != 0)
357 if (!remove_worktree(worktree_path))
363 got_worktree_close(worktree);
365 got_ref_close(head_ref);
367 got_repo_close(repo);
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); }
382 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
386 main(int argc, char *argv[])
388 int test_ok = 0, failure = 0;
389 const char *repo_path;
392 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
395 while ((ch = getopt(argc, argv, "v")) != -1) {
409 repo_path = GOT_REPO_PATH;
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;