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>
29 86c3caaf 2018-03-09 stsp #include "got_error.h"
30 86c3caaf 2018-03-09 stsp #include "got_object.h"
31 86c3caaf 2018-03-09 stsp #include "got_refs.h"
32 86c3caaf 2018-03-09 stsp #include "got_repository.h"
33 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
35 86c3caaf 2018-03-09 stsp #include "got_worktree_priv.h"
36 0da17012 2018-03-09 stsp #include "got_path_priv.h"
38 86c3caaf 2018-03-09 stsp #define GOT_REPO_PATH "../../../"
40 86c3caaf 2018-03-09 stsp static int verbose;
43 86c3caaf 2018-03-09 stsp test_printf(char *fmt, ...)
47 86c3caaf 2018-03-09 stsp if (!verbose)
50 86c3caaf 2018-03-09 stsp va_start(ap, fmt);
51 86c3caaf 2018-03-09 stsp vprintf(fmt, ap);
56 91c986ef 2018-03-09 stsp remove_got_dir(const char *worktree_path)
60 91c986ef 2018-03-09 stsp if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
62 91c986ef 2018-03-09 stsp rmdir(path);
68 91c986ef 2018-03-09 stsp remove_meta_file(const char *worktree_path, const char *name)
72 91c986ef 2018-03-09 stsp if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
73 91c986ef 2018-03-09 stsp name) == -1)
75 91c986ef 2018-03-09 stsp unlink(path);
81 91c986ef 2018-03-09 stsp remove_workdir(const char *worktree_path)
83 91c986ef 2018-03-09 stsp remove_meta_file(worktree_path, GOT_REF_HEAD);
84 91c986ef 2018-03-09 stsp remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX);
85 91c986ef 2018-03-09 stsp remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY);
86 1451e70d 2018-03-10 stsp remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT);
87 91c986ef 2018-03-09 stsp remove_got_dir(worktree_path);
88 91c986ef 2018-03-09 stsp rmdir(worktree_path);
92 86c3caaf 2018-03-09 stsp check_meta_file_exists(const char *worktree_path, const char *name)
97 86c3caaf 2018-03-09 stsp if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
98 86c3caaf 2018-03-09 stsp name) == -1)
100 86c3caaf 2018-03-09 stsp f = fopen(path, "r");
101 0da17012 2018-03-09 stsp free(path);
102 86c3caaf 2018-03-09 stsp if (f == NULL)
109 86c3caaf 2018-03-09 stsp worktree_init(const char *repo_path)
111 86c3caaf 2018-03-09 stsp const struct got_error *err;
112 86c3caaf 2018-03-09 stsp struct got_repository *repo = NULL;
113 86c3caaf 2018-03-09 stsp struct got_reference *head_ref = NULL;
114 86c3caaf 2018-03-09 stsp char worktree_path[PATH_MAX];
115 86c3caaf 2018-03-09 stsp int ok = 0;
117 86c3caaf 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
118 86c3caaf 2018-03-09 stsp if (err != NULL || repo == NULL)
120 86c3caaf 2018-03-09 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
121 86c3caaf 2018-03-09 stsp if (err != NULL || head_ref == NULL)
124 86c3caaf 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
125 86c3caaf 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
128 86c3caaf 2018-03-09 stsp err = got_worktree_init(worktree_path, head_ref, repo);
129 86c3caaf 2018-03-09 stsp if (err != NULL)
132 86c3caaf 2018-03-09 stsp /* Ensure required files were created. */
133 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_REF_HEAD))
135 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
137 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
139 1451e70d 2018-03-10 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
142 91c986ef 2018-03-09 stsp remove_workdir(worktree_path);
144 86c3caaf 2018-03-09 stsp if (head_ref)
145 86c3caaf 2018-03-09 stsp got_ref_close(head_ref);
147 86c3caaf 2018-03-09 stsp got_repo_close(repo);
152 0da17012 2018-03-09 stsp obstruct_meta_file(char **path, const char *worktree_path, const char *name)
155 0da17012 2018-03-09 stsp char *s = "This file should not be here\n";
157 0da17012 2018-03-09 stsp if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
158 0da17012 2018-03-09 stsp name) == -1)
160 0da17012 2018-03-09 stsp f = fopen(*path, "w+");
161 0da17012 2018-03-09 stsp if (f == NULL) {
162 0da17012 2018-03-09 stsp free(*path);
165 0da17012 2018-03-09 stsp if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
166 0da17012 2018-03-09 stsp free(*path);
174 0da17012 2018-03-09 stsp worktree_init_exists(const char *repo_path)
176 0da17012 2018-03-09 stsp const struct got_error *err;
177 0da17012 2018-03-09 stsp struct got_repository *repo = NULL;
178 0da17012 2018-03-09 stsp struct got_reference *head_ref = NULL;
179 0da17012 2018-03-09 stsp char worktree_path[PATH_MAX];
180 91c986ef 2018-03-09 stsp char *gotpath = NULL;
181 0da17012 2018-03-09 stsp char *path;
182 0da17012 2018-03-09 stsp int ok = 0;
185 0da17012 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
186 0da17012 2018-03-09 stsp if (err != NULL || repo == NULL)
188 0da17012 2018-03-09 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
189 0da17012 2018-03-09 stsp if (err != NULL || head_ref == NULL)
192 0da17012 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
193 0da17012 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
195 91c986ef 2018-03-09 stsp if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
198 0da17012 2018-03-09 stsp if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
201 0da17012 2018-03-09 stsp if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
204 0da17012 2018-03-09 stsp /* Create files which got_worktree_init() will try to create as well. */
206 0da17012 2018-03-09 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_REF_HEAD))
208 0da17012 2018-03-09 stsp err = got_worktree_init(worktree_path, head_ref, repo);
209 0da17012 2018-03-09 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST)
211 0da17012 2018-03-09 stsp unlink(path);
212 0da17012 2018-03-09 stsp free(path);
214 0da17012 2018-03-09 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
216 0da17012 2018-03-09 stsp err = got_worktree_init(worktree_path, head_ref, repo);
217 0da17012 2018-03-09 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST)
219 0da17012 2018-03-09 stsp unlink(path);
220 0da17012 2018-03-09 stsp free(path);
222 0da17012 2018-03-09 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_REPOSITORY))
224 0da17012 2018-03-09 stsp err = got_worktree_init(worktree_path, head_ref, repo);
225 0da17012 2018-03-09 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST)
227 0da17012 2018-03-09 stsp unlink(path);
228 0da17012 2018-03-09 stsp free(path);
230 1451e70d 2018-03-10 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FORMAT))
232 1451e70d 2018-03-10 stsp err = got_worktree_init(worktree_path, head_ref, repo);
233 1451e70d 2018-03-10 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST)
235 1451e70d 2018-03-10 stsp unlink(path);
236 1451e70d 2018-03-10 stsp free(path);
239 0da17012 2018-03-09 stsp if (head_ref)
240 0da17012 2018-03-09 stsp got_ref_close(head_ref);
242 0da17012 2018-03-09 stsp got_repo_close(repo);
243 91c986ef 2018-03-09 stsp free(gotpath);
244 1451e70d 2018-03-10 stsp if (ok == 4)
245 91c986ef 2018-03-09 stsp remove_workdir(worktree_path);
246 1451e70d 2018-03-10 stsp return (ok == 4);
249 86c3caaf 2018-03-09 stsp #define RUN_TEST(expr, name) \
250 86c3caaf 2018-03-09 stsp { test_ok = (expr); \
251 86c3caaf 2018-03-09 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
252 86c3caaf 2018-03-09 stsp failure = (failure || !test_ok); }
256 86c3caaf 2018-03-09 stsp usage(void)
258 86c3caaf 2018-03-09 stsp fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
262 86c3caaf 2018-03-09 stsp main(int argc, char *argv[])
264 86c3caaf 2018-03-09 stsp int test_ok = 0, failure = 0;
265 86c3caaf 2018-03-09 stsp const char *repo_path;
267 86c3caaf 2018-03-09 stsp int vflag = 0;
269 86c3caaf 2018-03-09 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
270 86c3caaf 2018-03-09 stsp switch (ch) {
272 86c3caaf 2018-03-09 stsp verbose = 1;
279 86c3caaf 2018-03-09 stsp argc -= optind;
280 86c3caaf 2018-03-09 stsp argv += optind;
282 86c3caaf 2018-03-09 stsp if (argc == 0)
283 86c3caaf 2018-03-09 stsp repo_path = GOT_REPO_PATH;
284 86c3caaf 2018-03-09 stsp else if (argc == 1)
285 86c3caaf 2018-03-09 stsp repo_path = argv[0];
291 86c3caaf 2018-03-09 stsp RUN_TEST(worktree_init(repo_path), "init");
292 0da17012 2018-03-09 stsp RUN_TEST(worktree_init_exists(repo_path), "init exists");
294 86c3caaf 2018-03-09 stsp return failure ? 1 : 0;