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>
30 #include "got_error.h"
31 #include "got_object.h"
33 #include "got_repository.h"
34 #include "got_worktree.h"
36 #include "got_worktree_lib.h"
37 #include "got_path_lib.h"
39 #define GOT_REPO_PATH "../../../"
44 test_printf(char *fmt, ...)
57 remove_got_dir(const char *worktree_path)
61 if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
69 remove_meta_file(const char *worktree_path, const char *name)
73 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
82 remove_worktree(const char *worktree_path)
84 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD))
86 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
88 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
90 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
92 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
94 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
96 if (!remove_got_dir(worktree_path))
98 if (rmdir(worktree_path) == -1)
104 read_meta_file(char **content, const char *path)
109 const char delim[3] = {'\0', '\0', '\0'};
112 f = fopen(path, "r");
116 *content = fparseln(f, &len, NULL, delim, 0);
117 if (*content == NULL)
124 check_meta_file_exists(const char *worktree_path, const char *name)
130 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
133 if (stat(path, &sb) == 0)
137 if (read_meta_file(&content, path) == 0) {
138 test_printf("%s:\t%s\n", name, content);
147 worktree_init(const char *repo_path)
149 const struct got_error *err;
150 struct got_repository *repo = NULL;
151 struct got_reference *head_ref = NULL;
152 char worktree_path[PATH_MAX];
155 err = got_repo_open(&repo, repo_path);
156 if (err != NULL || repo == NULL)
158 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
159 if (err != NULL || head_ref == NULL)
162 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
163 if (mkdtemp(worktree_path) == NULL)
166 err = got_worktree_init(worktree_path, head_ref, "/", repo);
170 /* Ensure required files were created. */
171 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD))
173 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
175 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
177 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
179 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
181 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
184 if (!remove_worktree(worktree_path))
189 got_ref_close(head_ref);
191 got_repo_close(repo);
196 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
199 char *s = "This file should not be here\n";
202 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
205 f = fopen(*path, "w+");
210 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
219 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
220 const char *worktree_path, char *name)
222 const struct got_error *err;
225 struct got_reference *head_ref = NULL;
227 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
230 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
231 if (err != NULL || head_ref == NULL)
234 err = got_worktree_init(worktree_path, head_ref, "/", repo);
235 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
241 got_ref_close(head_ref);
246 worktree_init_exists(const char *repo_path)
248 const struct got_error *err;
249 struct got_repository *repo = NULL;
250 char worktree_path[PATH_MAX];
251 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 worktree_checkout(const char *repo_path)
303 const struct got_error *err;
304 struct got_repository *repo = NULL;
305 struct got_reference *head_ref = NULL;
306 struct got_worktree *worktree = NULL;
307 char *makefile_path = NULL, *cfile_path = NULL;
308 char worktree_path[PATH_MAX];
312 err = got_repo_open(&repo, repo_path);
313 if (err != NULL || repo == NULL)
315 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
316 if (err != NULL || head_ref == NULL)
319 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
320 if (mkdtemp(worktree_path) == NULL)
323 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
328 err = got_worktree_open(&worktree, worktree_path);
332 err = got_worktree_checkout_files(worktree, head_ref, repo);
336 test_printf("checked out %s\n", worktree_path);
338 /* The work tree should contain a Makefile and worktree_test.c. */
339 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
341 if (stat(makefile_path, &sb) != 0)
344 unlink(makefile_path);
345 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
347 if (stat(cfile_path, &sb) != 0)
352 if (!remove_worktree(worktree_path))
358 got_worktree_close(worktree);
360 got_ref_close(head_ref);
362 got_repo_close(repo);
368 #define RUN_TEST(expr, name) \
369 { test_ok = (expr); \
370 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
371 failure = (failure || !test_ok); }
377 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
381 main(int argc, char *argv[])
383 int test_ok = 0, failure = 0;
384 const char *repo_path;
388 while ((ch = getopt(argc, argv, "v")) != -1) {
402 repo_path = GOT_REPO_PATH;
410 RUN_TEST(worktree_init(repo_path), "init");
411 RUN_TEST(worktree_init_exists(repo_path), "init exists");
412 RUN_TEST(worktree_checkout(repo_path), "checkout");
414 return failure ? 1 : 0;