2 86c3caaf 2018-03-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/param.h>
18 86c3caaf 2018-03-09 stsp #include <sys/queue.h>
19 86c3caaf 2018-03-09 stsp #include <sys/limits.h>
20 0da17012 2018-03-09 stsp #include <sys/stat.h>
22 86c3caaf 2018-03-09 stsp #include <stdarg.h>
23 86c3caaf 2018-03-09 stsp #include <stdio.h>
24 86c3caaf 2018-03-09 stsp #include <stdlib.h>
25 86c3caaf 2018-03-09 stsp #include <string.h>
26 86c3caaf 2018-03-09 stsp #include <unistd.h>
27 0da17012 2018-03-09 stsp #include <errno.h>
28 3962e86a 2018-03-11 stsp #include <util.h>
29 f8352b2a 2018-03-12 stsp #include <err.h>
30 f8352b2a 2018-03-12 stsp #include <unistd.h>
32 86c3caaf 2018-03-09 stsp #include "got_error.h"
33 86c3caaf 2018-03-09 stsp #include "got_object.h"
34 5261c201 2018-04-01 stsp #include "got_reference.h"
35 86c3caaf 2018-03-09 stsp #include "got_repository.h"
36 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
37 511a516b 2018-05-19 stsp #include "got_opentemp.h"
39 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
42 86c3caaf 2018-03-09 stsp #define GOT_REPO_PATH "../../../"
44 86c3caaf 2018-03-09 stsp static int verbose;
47 86c3caaf 2018-03-09 stsp test_printf(char *fmt, ...)
51 86c3caaf 2018-03-09 stsp if (!verbose)
54 86c3caaf 2018-03-09 stsp va_start(ap, fmt);
55 86c3caaf 2018-03-09 stsp vprintf(fmt, ap);
60 91c986ef 2018-03-09 stsp remove_got_dir(const char *worktree_path)
64 91c986ef 2018-03-09 stsp if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
66 91c986ef 2018-03-09 stsp rmdir(path);
72 91c986ef 2018-03-09 stsp remove_meta_file(const char *worktree_path, const char *name)
76 91c986ef 2018-03-09 stsp if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
77 91c986ef 2018-03-09 stsp name) == -1)
79 91c986ef 2018-03-09 stsp unlink(path);
85 b18d25df 2018-03-11 stsp remove_worktree(const char *worktree_path)
87 fdf001a7 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD))
89 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
91 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
93 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
95 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
97 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
99 45d8e5fd 2018-03-11 stsp if (!remove_got_dir(worktree_path))
101 45d8e5fd 2018-03-11 stsp if (rmdir(worktree_path) == -1)
107 3962e86a 2018-03-11 stsp read_meta_file(char **content, const char *path)
110 3962e86a 2018-03-11 stsp size_t len;
111 3962e86a 2018-03-11 stsp const char delim[3] = {'\0', '\0', '\0'};
112 3962e86a 2018-03-11 stsp int ret = 0;
114 3962e86a 2018-03-11 stsp f = fopen(path, "r");
115 3962e86a 2018-03-11 stsp if (f == NULL)
116 3962e86a 2018-03-11 stsp return errno;
118 3962e86a 2018-03-11 stsp *content = fparseln(f, &len, NULL, delim, 0);
119 3962e86a 2018-03-11 stsp if (*content == NULL)
120 3962e86a 2018-03-11 stsp ret = errno;
122 3962e86a 2018-03-11 stsp return ret;
126 86c3caaf 2018-03-09 stsp check_meta_file_exists(const char *worktree_path, const char *name)
128 07a7f8ad 2018-03-11 stsp struct stat sb;
129 86c3caaf 2018-03-09 stsp char *path;
130 5de261fe 2018-03-11 stsp int ret = 0;
132 86c3caaf 2018-03-09 stsp if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
133 86c3caaf 2018-03-09 stsp name) == -1)
135 5de261fe 2018-03-11 stsp if (stat(path, &sb) == 0)
137 3962e86a 2018-03-11 stsp if (verbose) {
138 3962e86a 2018-03-11 stsp char *content;
139 3962e86a 2018-03-11 stsp if (read_meta_file(&content, path) == 0) {
140 3962e86a 2018-03-11 stsp test_printf("%s:\t%s\n", name, content);
141 3962e86a 2018-03-11 stsp free(content);
144 5de261fe 2018-03-11 stsp free(path);
145 5de261fe 2018-03-11 stsp return ret;
149 86c3caaf 2018-03-09 stsp worktree_init(const char *repo_path)
151 86c3caaf 2018-03-09 stsp const struct got_error *err;
152 86c3caaf 2018-03-09 stsp struct got_repository *repo = NULL;
153 86c3caaf 2018-03-09 stsp struct got_reference *head_ref = NULL;
154 86c3caaf 2018-03-09 stsp char worktree_path[PATH_MAX];
155 86c3caaf 2018-03-09 stsp int ok = 0;
157 86c3caaf 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
158 86c3caaf 2018-03-09 stsp if (err != NULL || repo == NULL)
160 86c3caaf 2018-03-09 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
161 86c3caaf 2018-03-09 stsp if (err != NULL || head_ref == NULL)
164 86c3caaf 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
165 86c3caaf 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
168 577ec78f 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/", repo);
169 86c3caaf 2018-03-09 stsp if (err != NULL)
172 86c3caaf 2018-03-09 stsp /* Ensure required files were created. */
173 fdf001a7 2018-03-11 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD))
175 056e7441 2018-03-11 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
177 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
179 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
181 577ec78f 2018-03-11 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
183 1451e70d 2018-03-10 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
186 45d8e5fd 2018-03-11 stsp if (!remove_worktree(worktree_path))
190 86c3caaf 2018-03-09 stsp if (head_ref)
191 86c3caaf 2018-03-09 stsp got_ref_close(head_ref);
193 86c3caaf 2018-03-09 stsp got_repo_close(repo);
198 0da17012 2018-03-09 stsp obstruct_meta_file(char **path, const char *worktree_path, const char *name)
201 0da17012 2018-03-09 stsp char *s = "This file should not be here\n";
202 6b7476e9 2018-03-11 stsp int ret = 1;
204 0da17012 2018-03-09 stsp if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
205 0da17012 2018-03-09 stsp name) == -1)
207 0da17012 2018-03-09 stsp f = fopen(*path, "w+");
208 0da17012 2018-03-09 stsp if (f == NULL) {
209 0da17012 2018-03-09 stsp free(*path);
212 0da17012 2018-03-09 stsp if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
213 0da17012 2018-03-09 stsp free(*path);
217 6b7476e9 2018-03-11 stsp return ret;
221 8eac252b 2018-03-11 stsp obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
222 8eac252b 2018-03-11 stsp const char *worktree_path, char *name)
224 8eac252b 2018-03-11 stsp const struct got_error *err;
225 8eac252b 2018-03-11 stsp char *path;
226 8eac252b 2018-03-11 stsp int ret = 0;
227 8eac252b 2018-03-11 stsp struct got_reference *head_ref = NULL;
229 8eac252b 2018-03-11 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
232 8eac252b 2018-03-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
233 8eac252b 2018-03-11 stsp if (err != NULL || head_ref == NULL)
236 8eac252b 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/", repo);
237 8eac252b 2018-03-11 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
241 8eac252b 2018-03-11 stsp unlink(path);
242 8eac252b 2018-03-11 stsp free(path);
243 8eac252b 2018-03-11 stsp got_ref_close(head_ref);
244 8eac252b 2018-03-11 stsp return ret;
248 0da17012 2018-03-09 stsp worktree_init_exists(const char *repo_path)
250 0da17012 2018-03-09 stsp const struct got_error *err;
251 0da17012 2018-03-09 stsp struct got_repository *repo = NULL;
252 0da17012 2018-03-09 stsp char worktree_path[PATH_MAX];
253 91c986ef 2018-03-09 stsp char *gotpath = NULL;
254 0da17012 2018-03-09 stsp int ok = 0;
256 0da17012 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
257 0da17012 2018-03-09 stsp if (err != NULL || repo == NULL)
259 0da17012 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
260 0da17012 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
262 91c986ef 2018-03-09 stsp if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
265 0da17012 2018-03-09 stsp if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
268 0da17012 2018-03-09 stsp if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
271 0da17012 2018-03-09 stsp /* Create files which got_worktree_init() will try to create as well. */
272 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
273 fdf001a7 2018-03-11 stsp GOT_WORKTREE_HEAD))
275 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
276 8eac252b 2018-03-11 stsp GOT_WORKTREE_LOCK))
278 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
279 8eac252b 2018-03-11 stsp GOT_WORKTREE_FILE_INDEX))
281 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
282 8eac252b 2018-03-11 stsp GOT_WORKTREE_REPOSITORY))
284 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
285 8eac252b 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX))
287 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
288 8eac252b 2018-03-11 stsp GOT_WORKTREE_FORMAT))
293 0da17012 2018-03-09 stsp got_repo_close(repo);
294 91c986ef 2018-03-09 stsp free(gotpath);
295 e77c4c9f 2018-03-12 stsp if (ok == 6)
296 b18d25df 2018-03-11 stsp remove_worktree(worktree_path);
297 e77c4c9f 2018-03-12 stsp return (ok == 6);
300 291c6f03 2018-03-12 stsp static void
301 291c6f03 2018-03-12 stsp process_cb(void *arg, const char *path)
306 9d31a1d8 2018-03-11 stsp worktree_checkout(const char *repo_path)
308 9d31a1d8 2018-03-11 stsp const struct got_error *err;
309 9d31a1d8 2018-03-11 stsp struct got_repository *repo = NULL;
310 9d31a1d8 2018-03-11 stsp struct got_reference *head_ref = NULL;
311 9d31a1d8 2018-03-11 stsp struct got_worktree *worktree = NULL;
312 9d31a1d8 2018-03-11 stsp char *makefile_path = NULL, *cfile_path = NULL;
313 9d31a1d8 2018-03-11 stsp char worktree_path[PATH_MAX];
314 9d31a1d8 2018-03-11 stsp int ok = 0;
315 9d31a1d8 2018-03-11 stsp struct stat sb;
317 9d31a1d8 2018-03-11 stsp err = got_repo_open(&repo, repo_path);
318 9d31a1d8 2018-03-11 stsp if (err != NULL || repo == NULL)
320 9d31a1d8 2018-03-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
321 9d31a1d8 2018-03-11 stsp if (err != NULL || head_ref == NULL)
324 9d31a1d8 2018-03-11 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
325 9d31a1d8 2018-03-11 stsp if (mkdtemp(worktree_path) == NULL)
328 9d31a1d8 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
330 9d31a1d8 2018-03-11 stsp if (err != NULL)
333 9d31a1d8 2018-03-11 stsp err = got_worktree_open(&worktree, worktree_path);
334 9d31a1d8 2018-03-11 stsp if (err != NULL)
337 291c6f03 2018-03-12 stsp err = got_worktree_checkout_files(worktree, head_ref, repo,
338 99437157 2018-11-11 stsp process_cb, NULL, NULL, NULL);
339 9d31a1d8 2018-03-11 stsp if (err != NULL)
342 9d31a1d8 2018-03-11 stsp test_printf("checked out %s\n", worktree_path);
344 9d31a1d8 2018-03-11 stsp /* The work tree should contain a Makefile and worktree_test.c. */
345 9d31a1d8 2018-03-11 stsp if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
347 9d31a1d8 2018-03-11 stsp if (stat(makefile_path, &sb) != 0)
350 9d31a1d8 2018-03-11 stsp unlink(makefile_path);
351 9d31a1d8 2018-03-11 stsp if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
353 9d31a1d8 2018-03-11 stsp if (stat(cfile_path, &sb) != 0)
356 9d31a1d8 2018-03-11 stsp unlink(cfile_path);
358 9d31a1d8 2018-03-11 stsp if (!remove_worktree(worktree_path))
363 9d31a1d8 2018-03-11 stsp if (worktree)
364 9d31a1d8 2018-03-11 stsp got_worktree_close(worktree);
365 9d31a1d8 2018-03-11 stsp if (head_ref)
366 9d31a1d8 2018-03-11 stsp got_ref_close(head_ref);
368 9d31a1d8 2018-03-11 stsp got_repo_close(repo);
369 9d31a1d8 2018-03-11 stsp free(makefile_path);
370 9d31a1d8 2018-03-11 stsp free(cfile_path);
374 86c3caaf 2018-03-09 stsp #define RUN_TEST(expr, name) \
375 86c3caaf 2018-03-09 stsp { test_ok = (expr); \
376 86c3caaf 2018-03-09 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
377 86c3caaf 2018-03-09 stsp failure = (failure || !test_ok); }
381 86c3caaf 2018-03-09 stsp usage(void)
383 86c3caaf 2018-03-09 stsp fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
387 86c3caaf 2018-03-09 stsp main(int argc, char *argv[])
389 86c3caaf 2018-03-09 stsp int test_ok = 0, failure = 0;
390 86c3caaf 2018-03-09 stsp const char *repo_path;
393 2ff12563 2018-09-15 stsp #ifndef PROFILE
394 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
396 f8352b2a 2018-03-12 stsp err(1, "pledge");
399 86c3caaf 2018-03-09 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
400 86c3caaf 2018-03-09 stsp switch (ch) {
402 86c3caaf 2018-03-09 stsp verbose = 1;
409 86c3caaf 2018-03-09 stsp argc -= optind;
410 86c3caaf 2018-03-09 stsp argv += optind;
412 86c3caaf 2018-03-09 stsp if (argc == 0)
413 86c3caaf 2018-03-09 stsp repo_path = GOT_REPO_PATH;
414 86c3caaf 2018-03-09 stsp else if (argc == 1)
415 86c3caaf 2018-03-09 stsp repo_path = argv[0];
421 86c3caaf 2018-03-09 stsp RUN_TEST(worktree_init(repo_path), "init");
422 0da17012 2018-03-09 stsp RUN_TEST(worktree_init_exists(repo_path), "init exists");
423 9d31a1d8 2018-03-11 stsp RUN_TEST(worktree_checkout(repo_path), "checkout");
425 86c3caaf 2018-03-09 stsp return failure ? 1 : 0;