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_REF))
89 if (!remove_meta_file(worktree_path, GOT_WORKTREE_BASE_COMMIT))
91 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
93 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
95 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
97 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
99 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
101 if (!remove_got_dir(worktree_path))
103 if (rmdir(worktree_path) == -1)
109 read_meta_file(char **content, const char *path)
113 const char delim[3] = {'\0', '\0', '\0'};
116 f = fopen(path, "r");
120 *content = fparseln(f, &len, NULL, delim, 0);
121 if (*content == NULL)
128 check_meta_file_exists(const char *worktree_path, const char *name)
134 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
137 if (stat(path, &sb) == 0)
141 if (read_meta_file(&content, path) == 0) {
142 test_printf("%s:\t%s\n", name, content);
151 worktree_init(const char *repo_path)
153 const struct got_error *err;
154 struct got_repository *repo = NULL;
155 struct got_reference *head_ref = NULL;
156 char worktree_path[PATH_MAX];
159 err = got_repo_open(&repo, repo_path);
160 if (err != NULL || repo == NULL)
162 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
163 if (err != NULL || head_ref == NULL)
166 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
167 if (mkdtemp(worktree_path) == NULL)
170 err = got_worktree_init(worktree_path, head_ref, "/", repo);
174 /* Ensure required files were created. */
175 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD_REF))
177 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
179 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
181 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
183 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
185 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
187 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
190 if (!remove_worktree(worktree_path))
195 got_ref_close(head_ref);
197 got_repo_close(repo);
202 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
205 char *s = "This file should not be here\n";
208 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
211 f = fopen(*path, "w+");
216 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
225 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
226 const char *worktree_path, char *name)
228 const struct got_error *err;
231 struct got_reference *head_ref = NULL;
233 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
236 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
237 if (err != NULL || head_ref == NULL)
240 err = got_worktree_init(worktree_path, head_ref, "/", repo);
241 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
247 got_ref_close(head_ref);
252 worktree_init_exists(const char *repo_path)
254 const struct got_error *err;
255 struct got_repository *repo = NULL;
256 char worktree_path[PATH_MAX];
257 char *gotpath = NULL;
260 err = got_repo_open(&repo, repo_path);
261 if (err != NULL || repo == NULL)
263 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
264 if (mkdtemp(worktree_path) == NULL)
266 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
269 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
272 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
275 /* Create files which got_worktree_init() will try to create as well. */
276 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
277 GOT_WORKTREE_HEAD_REF))
279 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
280 GOT_WORKTREE_BASE_COMMIT))
282 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
285 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
286 GOT_WORKTREE_FILE_INDEX))
288 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
289 GOT_WORKTREE_REPOSITORY))
291 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
292 GOT_WORKTREE_PATH_PREFIX))
294 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
295 GOT_WORKTREE_FORMAT))
300 got_repo_close(repo);
303 remove_worktree(worktree_path);
308 progress_cb(void *arg, char status, const char *path)
313 worktree_checkout(const char *repo_path)
315 const struct got_error *err;
316 struct got_repository *repo = NULL;
317 struct got_reference *head_ref = NULL;
318 struct got_worktree *worktree = NULL;
319 char *makefile_path = NULL, *cfile_path = NULL;
320 char worktree_path[PATH_MAX];
324 err = got_repo_open(&repo, repo_path);
325 if (err != NULL || repo == NULL)
327 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
328 if (err != NULL || head_ref == NULL)
331 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
332 if (mkdtemp(worktree_path) == NULL)
335 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
340 err = got_worktree_open(&worktree, worktree_path);
344 err = got_worktree_checkout_files(worktree, repo, progress_cb, NULL,
349 test_printf("checked out %s\n", worktree_path);
351 /* The work tree should contain a Makefile and worktree_test.c. */
352 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
354 if (stat(makefile_path, &sb) != 0)
357 unlink(makefile_path);
358 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
360 if (stat(cfile_path, &sb) != 0)
365 if (!remove_worktree(worktree_path))
371 got_worktree_close(worktree);
373 got_ref_close(head_ref);
375 got_repo_close(repo);
381 #define RUN_TEST(expr, name) \
382 { test_ok = (expr); \
383 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
384 failure = (failure || !test_ok); }
390 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
394 main(int argc, char *argv[])
396 int test_ok = 0, failure = 0;
397 const char *repo_path;
401 if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
406 while ((ch = getopt(argc, argv, "v")) != -1) {
420 repo_path = GOT_REPO_PATH;
428 RUN_TEST(worktree_init(repo_path), "init");
429 RUN_TEST(worktree_init_exists(repo_path), "init exists");
430 RUN_TEST(worktree_checkout(repo_path), "checkout");
432 return failure ? 1 : 0;