2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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>
31 c442a90d 2019-03-10 stsp #include <uuid.h>
33 86c3caaf 2018-03-09 stsp #include "got_error.h"
34 86c3caaf 2018-03-09 stsp #include "got_object.h"
35 5261c201 2018-04-01 stsp #include "got_reference.h"
36 86c3caaf 2018-03-09 stsp #include "got_repository.h"
37 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
38 511a516b 2018-05-19 stsp #include "got_opentemp.h"
39 eea47b7e 2019-01-04 stsp #include "got_privsep.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
44 86c3caaf 2018-03-09 stsp #define GOT_REPO_PATH "../../../"
46 86c3caaf 2018-03-09 stsp static int verbose;
49 86c3caaf 2018-03-09 stsp test_printf(char *fmt, ...)
53 86c3caaf 2018-03-09 stsp if (!verbose)
56 86c3caaf 2018-03-09 stsp va_start(ap, fmt);
57 86c3caaf 2018-03-09 stsp vprintf(fmt, ap);
62 91c986ef 2018-03-09 stsp remove_got_dir(const char *worktree_path)
66 91c986ef 2018-03-09 stsp if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
68 91c986ef 2018-03-09 stsp rmdir(path);
74 91c986ef 2018-03-09 stsp remove_meta_file(const char *worktree_path, const char *name)
78 91c986ef 2018-03-09 stsp if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
79 91c986ef 2018-03-09 stsp name) == -1)
81 91c986ef 2018-03-09 stsp unlink(path);
86 0cd1c46a 2019-03-11 stsp static const struct got_error *
87 0cd1c46a 2019-03-11 stsp remove_worktree_base_ref(struct got_worktree *worktree,
88 0cd1c46a 2019-03-11 stsp struct got_repository *repo)
90 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
91 517bab73 2019-03-11 stsp struct got_reference *base_ref = NULL;
92 517bab73 2019-03-11 stsp char *refname = NULL, *absrefname = NULL;
94 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
98 517bab73 2019-03-11 stsp if (asprintf(&absrefname, "refs/%s", refname) == -1) {
99 517bab73 2019-03-11 stsp err = got_error_from_errno();
102 517bab73 2019-03-11 stsp err = got_ref_open(&base_ref, repo, absrefname);
106 0cd1c46a 2019-03-11 stsp err = got_ref_delete(base_ref, repo);
108 0cd1c46a 2019-03-11 stsp if (base_ref)
109 0cd1c46a 2019-03-11 stsp got_ref_close(base_ref);
110 0cd1c46a 2019-03-11 stsp free(refname);
111 517bab73 2019-03-11 stsp free(absrefname);
112 0cd1c46a 2019-03-11 stsp return err;
117 b18d25df 2018-03-11 stsp remove_worktree(const char *worktree_path)
119 0f92850e 2018-12-25 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD_REF))
121 0f92850e 2018-12-25 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_BASE_COMMIT))
123 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
125 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
127 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
129 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
131 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
133 ec22038e 2019-03-10 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_UUID))
135 45d8e5fd 2018-03-11 stsp if (!remove_got_dir(worktree_path))
137 45d8e5fd 2018-03-11 stsp if (rmdir(worktree_path) == -1)
143 3962e86a 2018-03-11 stsp read_meta_file(char **content, const char *path)
146 3962e86a 2018-03-11 stsp size_t len;
147 3962e86a 2018-03-11 stsp const char delim[3] = {'\0', '\0', '\0'};
148 3962e86a 2018-03-11 stsp int ret = 0;
150 3962e86a 2018-03-11 stsp f = fopen(path, "r");
151 3962e86a 2018-03-11 stsp if (f == NULL)
152 3962e86a 2018-03-11 stsp return errno;
154 3962e86a 2018-03-11 stsp *content = fparseln(f, &len, NULL, delim, 0);
155 3962e86a 2018-03-11 stsp if (*content == NULL)
156 3962e86a 2018-03-11 stsp ret = errno;
157 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0 && ret == 0)
158 fb43ecf1 2019-02-11 stsp ret = errno;
159 3962e86a 2018-03-11 stsp return ret;
163 86c3caaf 2018-03-09 stsp check_meta_file_exists(const char *worktree_path, const char *name)
165 07a7f8ad 2018-03-11 stsp struct stat sb;
166 86c3caaf 2018-03-09 stsp char *path;
167 5de261fe 2018-03-11 stsp int ret = 0;
169 86c3caaf 2018-03-09 stsp if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
170 86c3caaf 2018-03-09 stsp name) == -1)
172 5de261fe 2018-03-11 stsp if (stat(path, &sb) == 0)
174 3962e86a 2018-03-11 stsp if (verbose) {
175 3962e86a 2018-03-11 stsp char *content;
176 3962e86a 2018-03-11 stsp if (read_meta_file(&content, path) == 0) {
177 3962e86a 2018-03-11 stsp test_printf("%s:\t%s\n", name, content);
178 3962e86a 2018-03-11 stsp free(content);
181 5de261fe 2018-03-11 stsp free(path);
182 5de261fe 2018-03-11 stsp return ret;
186 86c3caaf 2018-03-09 stsp worktree_init(const char *repo_path)
188 86c3caaf 2018-03-09 stsp const struct got_error *err;
189 86c3caaf 2018-03-09 stsp struct got_repository *repo = NULL;
190 86c3caaf 2018-03-09 stsp struct got_reference *head_ref = NULL;
191 86c3caaf 2018-03-09 stsp char worktree_path[PATH_MAX];
192 86c3caaf 2018-03-09 stsp int ok = 0;
194 86c3caaf 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
195 86c3caaf 2018-03-09 stsp if (err != NULL || repo == NULL)
197 86c3caaf 2018-03-09 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
198 86c3caaf 2018-03-09 stsp if (err != NULL || head_ref == NULL)
201 86c3caaf 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
202 86c3caaf 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
205 577ec78f 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/", repo);
206 86c3caaf 2018-03-09 stsp if (err != NULL)
209 86c3caaf 2018-03-09 stsp /* Ensure required files were created. */
210 0f92850e 2018-12-25 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD_REF))
212 0f92850e 2018-12-25 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
214 056e7441 2018-03-11 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
216 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
218 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
220 577ec78f 2018-03-11 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
222 1451e70d 2018-03-10 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
224 ec22038e 2019-03-10 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_UUID))
227 45d8e5fd 2018-03-11 stsp if (!remove_worktree(worktree_path))
231 86c3caaf 2018-03-09 stsp if (head_ref)
232 86c3caaf 2018-03-09 stsp got_ref_close(head_ref);
234 86c3caaf 2018-03-09 stsp got_repo_close(repo);
239 0da17012 2018-03-09 stsp obstruct_meta_file(char **path, const char *worktree_path, const char *name)
242 0da17012 2018-03-09 stsp char *s = "This file should not be here\n";
243 6b7476e9 2018-03-11 stsp int ret = 1;
245 0da17012 2018-03-09 stsp if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
246 0da17012 2018-03-09 stsp name) == -1)
248 0da17012 2018-03-09 stsp f = fopen(*path, "w+");
249 0da17012 2018-03-09 stsp if (f == NULL) {
250 0da17012 2018-03-09 stsp free(*path);
253 0da17012 2018-03-09 stsp if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
254 0da17012 2018-03-09 stsp free(*path);
257 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0)
259 6b7476e9 2018-03-11 stsp return ret;
263 8eac252b 2018-03-11 stsp obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
264 8eac252b 2018-03-11 stsp const char *worktree_path, char *name)
266 8eac252b 2018-03-11 stsp const struct got_error *err;
267 8eac252b 2018-03-11 stsp char *path;
268 8eac252b 2018-03-11 stsp int ret = 0;
269 8eac252b 2018-03-11 stsp struct got_reference *head_ref = NULL;
271 8eac252b 2018-03-11 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
274 8eac252b 2018-03-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
275 8eac252b 2018-03-11 stsp if (err != NULL || head_ref == NULL)
278 8eac252b 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/", repo);
279 8eac252b 2018-03-11 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
283 8eac252b 2018-03-11 stsp unlink(path);
284 8eac252b 2018-03-11 stsp free(path);
285 8eac252b 2018-03-11 stsp got_ref_close(head_ref);
286 8eac252b 2018-03-11 stsp return ret;
290 0da17012 2018-03-09 stsp worktree_init_exists(const char *repo_path)
292 0da17012 2018-03-09 stsp const struct got_error *err;
293 0da17012 2018-03-09 stsp struct got_repository *repo = NULL;
294 0da17012 2018-03-09 stsp char worktree_path[PATH_MAX];
295 91c986ef 2018-03-09 stsp char *gotpath = NULL;
296 0da17012 2018-03-09 stsp int ok = 0;
298 0da17012 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
299 0da17012 2018-03-09 stsp if (err != NULL || repo == NULL)
301 0da17012 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
302 0da17012 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
304 91c986ef 2018-03-09 stsp if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
307 0da17012 2018-03-09 stsp if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
310 0da17012 2018-03-09 stsp if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
313 0da17012 2018-03-09 stsp /* Create files which got_worktree_init() will try to create as well. */
314 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
315 0f92850e 2018-12-25 stsp GOT_WORKTREE_HEAD_REF))
317 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
318 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT))
320 65596e15 2018-12-24 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
321 8eac252b 2018-03-11 stsp GOT_WORKTREE_LOCK))
323 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
324 8eac252b 2018-03-11 stsp GOT_WORKTREE_FILE_INDEX))
326 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
327 8eac252b 2018-03-11 stsp GOT_WORKTREE_REPOSITORY))
329 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
330 8eac252b 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX))
332 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
333 8eac252b 2018-03-11 stsp GOT_WORKTREE_FORMAT))
338 0da17012 2018-03-09 stsp got_repo_close(repo);
339 91c986ef 2018-03-09 stsp free(gotpath);
340 65596e15 2018-12-24 stsp if (ok == 7)
341 b18d25df 2018-03-11 stsp remove_worktree(worktree_path);
342 65596e15 2018-12-24 stsp return (ok == 7);
345 291c6f03 2018-03-12 stsp static void
346 a0eb853d 2018-12-29 stsp progress_cb(void *arg, unsigned char status, const char *path)
351 9d31a1d8 2018-03-11 stsp worktree_checkout(const char *repo_path)
353 9d31a1d8 2018-03-11 stsp const struct got_error *err;
354 9d31a1d8 2018-03-11 stsp struct got_repository *repo = NULL;
355 9d31a1d8 2018-03-11 stsp struct got_reference *head_ref = NULL;
356 9d31a1d8 2018-03-11 stsp struct got_worktree *worktree = NULL;
357 9d31a1d8 2018-03-11 stsp char *makefile_path = NULL, *cfile_path = NULL;
358 9d31a1d8 2018-03-11 stsp char worktree_path[PATH_MAX];
359 9d31a1d8 2018-03-11 stsp int ok = 0;
360 9d31a1d8 2018-03-11 stsp struct stat sb;
362 9d31a1d8 2018-03-11 stsp err = got_repo_open(&repo, repo_path);
363 9d31a1d8 2018-03-11 stsp if (err != NULL || repo == NULL)
365 9d31a1d8 2018-03-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
366 9d31a1d8 2018-03-11 stsp if (err != NULL || head_ref == NULL)
369 9d31a1d8 2018-03-11 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
370 9d31a1d8 2018-03-11 stsp if (mkdtemp(worktree_path) == NULL)
373 9d31a1d8 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
375 9d31a1d8 2018-03-11 stsp if (err != NULL)
378 9d31a1d8 2018-03-11 stsp err = got_worktree_open(&worktree, worktree_path);
379 9d31a1d8 2018-03-11 stsp if (err != NULL)
382 c4cdcb68 2019-04-03 stsp err = got_worktree_checkout_files(worktree, "", repo, progress_cb, NULL,
383 93a30277 2018-12-24 stsp NULL, NULL);
384 9d31a1d8 2018-03-11 stsp if (err != NULL)
387 9d31a1d8 2018-03-11 stsp test_printf("checked out %s\n", worktree_path);
389 9d31a1d8 2018-03-11 stsp /* The work tree should contain a Makefile and worktree_test.c. */
390 9d31a1d8 2018-03-11 stsp if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
392 9d31a1d8 2018-03-11 stsp if (stat(makefile_path, &sb) != 0)
395 9d31a1d8 2018-03-11 stsp unlink(makefile_path);
396 9d31a1d8 2018-03-11 stsp if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
398 9d31a1d8 2018-03-11 stsp if (stat(cfile_path, &sb) != 0)
401 9d31a1d8 2018-03-11 stsp unlink(cfile_path);
403 0cd1c46a 2019-03-11 stsp err = remove_worktree_base_ref(worktree, repo);
406 9d31a1d8 2018-03-11 stsp if (!remove_worktree(worktree_path))
411 9d31a1d8 2018-03-11 stsp if (worktree)
412 9d31a1d8 2018-03-11 stsp got_worktree_close(worktree);
413 9d31a1d8 2018-03-11 stsp if (head_ref)
414 9d31a1d8 2018-03-11 stsp got_ref_close(head_ref);
416 9d31a1d8 2018-03-11 stsp got_repo_close(repo);
417 9d31a1d8 2018-03-11 stsp free(makefile_path);
418 9d31a1d8 2018-03-11 stsp free(cfile_path);
422 86c3caaf 2018-03-09 stsp #define RUN_TEST(expr, name) \
423 86c3caaf 2018-03-09 stsp { test_ok = (expr); \
424 9465d522 2019-01-03 stsp printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
425 86c3caaf 2018-03-09 stsp failure = (failure || !test_ok); }
429 86c3caaf 2018-03-09 stsp usage(void)
431 86c3caaf 2018-03-09 stsp fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
435 86c3caaf 2018-03-09 stsp main(int argc, char *argv[])
437 86c3caaf 2018-03-09 stsp int test_ok = 0, failure = 0;
438 86c3caaf 2018-03-09 stsp const char *repo_path;
439 eea47b7e 2019-01-04 stsp char *cwd = NULL;
442 2ff12563 2018-09-15 stsp #ifndef PROFILE
443 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
444 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
445 f8352b2a 2018-03-12 stsp err(1, "pledge");
448 86c3caaf 2018-03-09 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
449 86c3caaf 2018-03-09 stsp switch (ch) {
451 86c3caaf 2018-03-09 stsp verbose = 1;
458 86c3caaf 2018-03-09 stsp argc -= optind;
459 86c3caaf 2018-03-09 stsp argv += optind;
461 86c3caaf 2018-03-09 stsp if (argc == 0)
462 86c3caaf 2018-03-09 stsp repo_path = GOT_REPO_PATH;
463 86c3caaf 2018-03-09 stsp else if (argc == 1)
464 86c3caaf 2018-03-09 stsp repo_path = argv[0];
470 eea47b7e 2019-01-04 stsp cwd = getcwd(NULL, 0);
471 eea47b7e 2019-01-04 stsp if (cwd == NULL)
472 eea47b7e 2019-01-04 stsp err(1, "getcwd");
473 eea47b7e 2019-01-04 stsp if (unveil(cwd, "rwc") != 0)
474 eea47b7e 2019-01-04 stsp err(1, "unvail");
477 eea47b7e 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
478 eea47b7e 2019-01-04 stsp err(1, "unveil");
480 0cd1c46a 2019-03-11 stsp if (unveil(repo_path, "rwc") != 0)
481 eea47b7e 2019-01-04 stsp err(1, "unveil");
483 eea47b7e 2019-01-04 stsp if (got_privsep_unveil_exec_helpers() != NULL)
486 eea47b7e 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
487 eea47b7e 2019-01-04 stsp err(1, "unveil");
489 86c3caaf 2018-03-09 stsp RUN_TEST(worktree_init(repo_path), "init");
490 0da17012 2018-03-09 stsp RUN_TEST(worktree_init_exists(repo_path), "init exists");
491 9d31a1d8 2018-03-11 stsp RUN_TEST(worktree_checkout(repo_path), "checkout");
493 86c3caaf 2018-03-09 stsp return failure ? 1 : 0;