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"
39 #include "got_lib_worktree.h"
40 #include "got_lib_path.h"
42 #define GOT_REPO_PATH "../../../"
47 test_printf(char *fmt, ...)
60 remove_got_dir(const char *worktree_path)
64 if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
72 remove_meta_file(const char *worktree_path, const char *name)
76 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
85 remove_worktree(const char *worktree_path)
87 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD))
89 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
91 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
93 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
95 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
97 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
99 if (!remove_got_dir(worktree_path))
101 if (rmdir(worktree_path) == -1)
107 read_meta_file(char **content, const char *path)
111 const char delim[3] = {'\0', '\0', '\0'};
114 f = fopen(path, "r");
118 *content = fparseln(f, &len, NULL, delim, 0);
119 if (*content == NULL)
126 check_meta_file_exists(const char *worktree_path, const char *name)
132 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
135 if (stat(path, &sb) == 0)
139 if (read_meta_file(&content, path) == 0) {
140 test_printf("%s:\t%s\n", name, content);
149 worktree_init(const char *repo_path)
151 const struct got_error *err;
152 struct got_repository *repo = NULL;
153 struct got_reference *head_ref = NULL;
154 char worktree_path[PATH_MAX];
157 err = got_repo_open(&repo, repo_path);
158 if (err != NULL || repo == NULL)
160 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
161 if (err != NULL || head_ref == NULL)
164 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
165 if (mkdtemp(worktree_path) == NULL)
168 err = got_worktree_init(worktree_path, head_ref, "/", repo);
172 /* Ensure required files were created. */
173 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD))
175 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
177 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
179 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
181 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
183 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
186 if (!remove_worktree(worktree_path))
191 got_ref_close(head_ref);
193 got_repo_close(repo);
198 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
201 char *s = "This file should not be here\n";
204 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
207 f = fopen(*path, "w+");
212 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
221 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
222 const char *worktree_path, char *name)
224 const struct got_error *err;
227 struct got_reference *head_ref = NULL;
229 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
232 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
233 if (err != NULL || head_ref == NULL)
236 err = got_worktree_init(worktree_path, head_ref, "/", repo);
237 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
243 got_ref_close(head_ref);
248 worktree_init_exists(const char *repo_path)
250 const struct got_error *err;
251 struct got_repository *repo = NULL;
252 char worktree_path[PATH_MAX];
253 char *gotpath = NULL;
256 err = got_repo_open(&repo, repo_path);
257 if (err != NULL || repo == NULL)
259 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
260 if (mkdtemp(worktree_path) == NULL)
262 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
265 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
268 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
271 /* Create files which got_worktree_init() will try to create as well. */
272 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
275 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
278 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
279 GOT_WORKTREE_FILE_INDEX))
281 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
282 GOT_WORKTREE_REPOSITORY))
284 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
285 GOT_WORKTREE_PATH_PREFIX))
287 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
288 GOT_WORKTREE_FORMAT))
293 got_repo_close(repo);
296 remove_worktree(worktree_path);
301 process_cb(void *arg, const char *path)
306 worktree_checkout(const char *repo_path)
308 const struct got_error *err;
309 struct got_repository *repo = NULL;
310 struct got_reference *head_ref = NULL;
311 struct got_worktree *worktree = NULL;
312 char *makefile_path = NULL, *cfile_path = NULL;
313 char worktree_path[PATH_MAX];
317 err = got_repo_open(&repo, repo_path);
318 if (err != NULL || repo == NULL)
320 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
321 if (err != NULL || head_ref == NULL)
324 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
325 if (mkdtemp(worktree_path) == NULL)
328 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
333 err = got_worktree_open(&worktree, worktree_path);
337 err = got_worktree_checkout_files(worktree, head_ref, repo,
342 test_printf("checked out %s\n", worktree_path);
344 /* The work tree should contain a Makefile and worktree_test.c. */
345 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
347 if (stat(makefile_path, &sb) != 0)
350 unlink(makefile_path);
351 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
353 if (stat(cfile_path, &sb) != 0)
358 if (!remove_worktree(worktree_path))
364 got_worktree_close(worktree);
366 got_ref_close(head_ref);
368 got_repo_close(repo);
374 #define RUN_TEST(expr, name) \
375 { test_ok = (expr); \
376 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
377 failure = (failure || !test_ok); }
383 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
387 main(int argc, char *argv[])
389 int test_ok = 0, failure = 0;
390 const char *repo_path;
393 if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
396 while ((ch = getopt(argc, argv, "v")) != -1) {
410 repo_path = GOT_REPO_PATH;
418 RUN_TEST(worktree_init(repo_path), "init");
419 RUN_TEST(worktree_init_exists(repo_path), "init exists");
420 RUN_TEST(worktree_checkout(repo_path), "checkout");
422 return failure ? 1 : 0;