Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/param.h>
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/stat.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <util.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_refs.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 "../../../"
41 static int verbose;
43 void
44 test_printf(char *fmt, ...)
45 {
46 va_list ap;
48 if (!verbose)
49 return;
51 va_start(ap, fmt);
52 vprintf(fmt, ap);
53 va_end(ap);
54 }
56 static int
57 remove_got_dir(const char *worktree_path)
58 {
59 char *path;
61 if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
62 return 0;
63 rmdir(path);
64 free(path);
65 return 1;
66 }
68 static int
69 remove_meta_file(const char *worktree_path, const char *name)
70 {
71 char *path;
73 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
74 name) == -1)
75 return 0;
76 unlink(path);
77 free(path);
78 return 1;
79 }
81 static int
82 remove_worktree(const char *worktree_path)
83 {
84 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD))
85 return 0;
86 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
87 return 0;
88 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
89 return 0;
90 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
91 return 0;
92 if (!remove_meta_file(worktree_path, GOT_WORKTREE_BASE_COMMIT))
93 return 0;
94 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
95 return 0;
96 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
97 return 0;
98 if (!remove_got_dir(worktree_path))
99 return 0;
100 if (rmdir(worktree_path) == -1)
101 return 0;
102 return 1;
105 static int
106 read_meta_file(char **content, const char *path)
108 FILE *f;
109 size_t n;
110 size_t len;
111 const char delim[3] = {'\0', '\0', '\0'};
112 int ret = 0;
114 f = fopen(path, "r");
115 if (f == NULL)
116 return errno;
118 *content = fparseln(f, &len, NULL, delim, 0);
119 if (*content == NULL)
120 ret = errno;
121 fclose(f);
122 return ret;
125 static int
126 check_meta_file_exists(const char *worktree_path, const char *name)
128 struct stat sb;
129 char *path;
130 int ret = 0;
132 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
133 name) == -1)
134 return 0;
135 if (stat(path, &sb) == 0)
136 ret = 1;
137 if (verbose) {
138 char *content;
139 if (read_meta_file(&content, path) == 0) {
140 test_printf("%s:\t%s\n", name, content);
141 free(content);
144 free(path);
145 return ret;
148 static int
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];
155 int ok = 0;
157 err = got_repo_open(&repo, repo_path);
158 if (err != NULL || repo == NULL)
159 goto done;
160 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
161 if (err != NULL || head_ref == NULL)
162 goto done;
164 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
165 if (mkdtemp(worktree_path) == NULL)
166 goto done;
168 err = got_worktree_init(worktree_path, head_ref, "/", repo);
169 if (err != NULL)
170 goto done;
172 /* Ensure required files were created. */
173 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD))
174 goto done;
175 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
176 goto done;
177 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
178 goto done;
179 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
180 goto done;
181 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
182 goto done;
183 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
184 goto done;
185 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
186 goto done;
188 if (!remove_worktree(worktree_path))
189 goto done;
190 ok = 1;
191 done:
192 if (head_ref)
193 got_ref_close(head_ref);
194 if (repo)
195 got_repo_close(repo);
196 return ok;
199 static int
200 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
202 FILE *f;
203 char *s = "This file should not be here\n";
204 int ret = 1;
206 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
207 name) == -1)
208 return 0;
209 f = fopen(*path, "w+");
210 if (f == NULL) {
211 free(*path);
212 return 0;
214 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
215 free(*path);
216 ret = 0;
218 fclose(f);
219 return ret;
222 static int
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;
227 char *path;
228 int ret = 0;
229 struct got_reference *head_ref = NULL;
231 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
232 return 0;
234 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
235 if (err != NULL || head_ref == NULL)
236 return 0;
238 err = got_worktree_init(worktree_path, head_ref, "/", repo);
239 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
240 (*ok)++;
241 ret = 1;
243 unlink(path);
244 free(path);
245 got_ref_close(head_ref);
246 return ret;
249 static int
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;
256 char *path;
257 int ok = 0;
258 FILE *f;
260 err = got_repo_open(&repo, repo_path);
261 if (err != NULL || repo == NULL)
262 goto done;
263 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
264 if (mkdtemp(worktree_path) == NULL)
265 goto done;
266 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
267 goto done;
269 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
270 == -1)
271 goto done;
272 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
273 goto done;
275 /* Create files which got_worktree_init() will try to create as well. */
276 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
277 GOT_WORKTREE_HEAD))
278 goto done;
279 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
280 GOT_WORKTREE_LOCK))
281 goto done;
282 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
283 GOT_WORKTREE_BASE_COMMIT))
284 goto done;
285 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
286 GOT_WORKTREE_FILE_INDEX))
287 goto done;
288 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
289 GOT_WORKTREE_REPOSITORY))
290 goto done;
291 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
292 GOT_WORKTREE_PATH_PREFIX))
293 goto done;
294 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
295 GOT_WORKTREE_FORMAT))
296 goto done;
298 done:
299 if (repo)
300 got_repo_close(repo);
301 free(gotpath);
302 if (ok == 7)
303 remove_worktree(worktree_path);
304 return (ok == 7);
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); }
313 void
314 usage(void)
316 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
319 int
320 main(int argc, char *argv[])
322 int test_ok = 0, failure = 0;
323 const char *repo_path;
324 int ch;
325 int vflag = 0;
327 while ((ch = getopt(argc, argv, "v")) != -1) {
328 switch (ch) {
329 case 'v':
330 verbose = 1;
331 break;
332 default:
333 usage();
334 return 1;
337 argc -= optind;
338 argv += optind;
340 if (argc == 0)
341 repo_path = GOT_REPO_PATH;
342 else if (argc == 1)
343 repo_path = argv[0];
344 else {
345 usage();
346 return 1;
349 RUN_TEST(worktree_init(repo_path), "init");
350 RUN_TEST(worktree_init_exists(repo_path), "init exists");
352 return failure ? 1 : 0;