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_priv.h"
37 #include "got_path_priv.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_BASE_COMMIT))
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)
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_BASE_COMMIT))
179 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
181 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
183 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
185 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
188 if (!remove_worktree(worktree_path))
193 got_ref_close(head_ref);
195 got_repo_close(repo);
200 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
203 char *s = "This file should not be here\n";
206 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
209 f = fopen(*path, "w+");
214 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
223 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
224 const char *worktree_path, char *name)
226 const struct got_error *err;
229 struct got_reference *head_ref = NULL;
231 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
234 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
235 if (err != NULL || head_ref == NULL)
238 err = got_worktree_init(worktree_path, head_ref, "/", repo);
239 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
245 got_ref_close(head_ref);
250 worktree_init_exists(const char *repo_path)
252 const struct got_error *err;
253 struct got_repository *repo = NULL;
254 char worktree_path[PATH_MAX];
255 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,
279 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
282 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
283 GOT_WORKTREE_BASE_COMMIT))
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);
307 #define RUN_TEST(expr, name) \
308 { test_ok = (expr); \
309 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
310 failure = (failure || !test_ok); }
316 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
320 main(int argc, char *argv[])
322 int test_ok = 0, failure = 0;
323 const char *repo_path;
327 while ((ch = getopt(argc, argv, "v")) != -1) {
341 repo_path = GOT_REPO_PATH;
349 RUN_TEST(worktree_init(repo_path), "init");
350 RUN_TEST(worktree_init_exists(repo_path), "init exists");
352 return failure ? 1 : 0;