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"
37 #include "got_worktree.h"
38 #include "got_opentemp.h"
39 #include "got_privsep.h"
41 #include "got_lib_worktree.h"
42 #include "got_lib_path.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,
87 remove_worktree(const char *worktree_path)
89 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD_REF))
91 if (!remove_meta_file(worktree_path, GOT_WORKTREE_BASE_COMMIT))
93 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
95 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
97 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
99 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
101 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
103 if (!remove_meta_file(worktree_path, GOT_WORKTREE_UUID))
105 if (!remove_got_dir(worktree_path))
107 if (rmdir(worktree_path) == -1)
113 read_meta_file(char **content, const char *path)
117 const char delim[3] = {'\0', '\0', '\0'};
120 f = fopen(path, "r");
124 *content = fparseln(f, &len, NULL, delim, 0);
125 if (*content == NULL)
127 if (fclose(f) != 0 && ret == 0)
133 check_meta_file_exists(const char *worktree_path, const char *name)
139 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
142 if (stat(path, &sb) == 0)
146 if (read_meta_file(&content, path) == 0) {
147 test_printf("%s:\t%s\n", name, content);
156 worktree_init(const char *repo_path)
158 const struct got_error *err;
159 struct got_repository *repo = NULL;
160 struct got_reference *head_ref = NULL;
161 char worktree_path[PATH_MAX];
164 err = got_repo_open(&repo, repo_path);
165 if (err != NULL || repo == NULL)
167 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
168 if (err != NULL || head_ref == NULL)
171 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
172 if (mkdtemp(worktree_path) == NULL)
175 err = got_worktree_init(worktree_path, head_ref, "/", repo);
179 /* Ensure required files were created. */
180 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD_REF))
182 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
184 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
186 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
188 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
190 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
192 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
194 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_UUID))
197 if (!remove_worktree(worktree_path))
202 got_ref_close(head_ref);
204 got_repo_close(repo);
209 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
212 char *s = "This file should not be here\n";
215 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
218 f = fopen(*path, "w+");
223 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
233 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
234 const char *worktree_path, char *name)
236 const struct got_error *err;
239 struct got_reference *head_ref = NULL;
241 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
244 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
245 if (err != NULL || head_ref == NULL)
248 err = got_worktree_init(worktree_path, head_ref, "/", repo);
249 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
255 got_ref_close(head_ref);
260 worktree_init_exists(const char *repo_path)
262 const struct got_error *err;
263 struct got_repository *repo = NULL;
264 char worktree_path[PATH_MAX];
265 char *gotpath = NULL;
268 err = got_repo_open(&repo, repo_path);
269 if (err != NULL || repo == NULL)
271 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
272 if (mkdtemp(worktree_path) == NULL)
274 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
277 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
280 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
283 /* Create files which got_worktree_init() will try to create as well. */
284 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
285 GOT_WORKTREE_HEAD_REF))
287 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
288 GOT_WORKTREE_BASE_COMMIT))
290 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
293 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
294 GOT_WORKTREE_FILE_INDEX))
296 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
297 GOT_WORKTREE_REPOSITORY))
299 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
300 GOT_WORKTREE_PATH_PREFIX))
302 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
303 GOT_WORKTREE_FORMAT))
308 got_repo_close(repo);
311 remove_worktree(worktree_path);
316 progress_cb(void *arg, unsigned char status, const char *path)
321 worktree_checkout(const char *repo_path)
323 const struct got_error *err;
324 struct got_repository *repo = NULL;
325 struct got_reference *head_ref = NULL;
326 struct got_worktree *worktree = NULL;
327 char *makefile_path = NULL, *cfile_path = NULL;
328 char worktree_path[PATH_MAX];
332 err = got_repo_open(&repo, repo_path);
333 if (err != NULL || repo == NULL)
335 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
336 if (err != NULL || head_ref == NULL)
339 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
340 if (mkdtemp(worktree_path) == NULL)
343 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
348 err = got_worktree_open(&worktree, worktree_path);
352 err = got_worktree_checkout_files(worktree, repo, progress_cb, NULL,
357 test_printf("checked out %s\n", worktree_path);
359 /* The work tree should contain a Makefile and worktree_test.c. */
360 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
362 if (stat(makefile_path, &sb) != 0)
365 unlink(makefile_path);
366 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
368 if (stat(cfile_path, &sb) != 0)
373 if (!remove_worktree(worktree_path))
379 got_worktree_close(worktree);
381 got_ref_close(head_ref);
383 got_repo_close(repo);
389 #define RUN_TEST(expr, name) \
390 { test_ok = (expr); \
391 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
392 failure = (failure || !test_ok); }
398 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
402 main(int argc, char *argv[])
404 int test_ok = 0, failure = 0;
405 const char *repo_path;
410 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
411 "unveil", NULL) == -1)
415 while ((ch = getopt(argc, argv, "v")) != -1) {
429 repo_path = GOT_REPO_PATH;
437 cwd = getcwd(NULL, 0);
440 if (unveil(cwd, "rwc") != 0)
444 if (unveil("/tmp", "rwc") != 0)
447 if (unveil(repo_path, "r") != 0)
450 if (got_privsep_unveil_exec_helpers() != NULL)
453 if (unveil(NULL, NULL) != 0)
456 RUN_TEST(worktree_init(repo_path), "init");
457 RUN_TEST(worktree_init_exists(repo_path), "init exists");
458 RUN_TEST(worktree_checkout(repo_path), "checkout");
460 return failure ? 1 : 0;