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_lib.h"
37 #include "got_path_lib.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_LOCK))
93 return 0;
94 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
95 return 0;
96 if (!remove_got_dir(worktree_path))
97 return 0;
98 if (rmdir(worktree_path) == -1)
99 return 0;
100 return 1;
103 static int
104 read_meta_file(char **content, const char *path)
106 FILE *f;
107 size_t n;
108 size_t len;
109 const char delim[3] = {'\0', '\0', '\0'};
110 int ret = 0;
112 f = fopen(path, "r");
113 if (f == NULL)
114 return errno;
116 *content = fparseln(f, &len, NULL, delim, 0);
117 if (*content == NULL)
118 ret = errno;
119 fclose(f);
120 return ret;
123 static int
124 check_meta_file_exists(const char *worktree_path, const char *name)
126 struct stat sb;
127 char *path;
128 int ret = 0;
130 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
131 name) == -1)
132 return 0;
133 if (stat(path, &sb) == 0)
134 ret = 1;
135 if (verbose) {
136 char *content;
137 if (read_meta_file(&content, path) == 0) {
138 test_printf("%s:\t%s\n", name, content);
139 free(content);
142 free(path);
143 return ret;
146 static int
147 worktree_init(const char *repo_path)
149 const struct got_error *err;
150 struct got_repository *repo = NULL;
151 struct got_reference *head_ref = NULL;
152 char worktree_path[PATH_MAX];
153 int ok = 0;
155 err = got_repo_open(&repo, repo_path);
156 if (err != NULL || repo == NULL)
157 goto done;
158 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
159 if (err != NULL || head_ref == NULL)
160 goto done;
162 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
163 if (mkdtemp(worktree_path) == NULL)
164 goto done;
166 err = got_worktree_init(worktree_path, head_ref, "/", repo);
167 if (err != NULL)
168 goto done;
170 /* Ensure required files were created. */
171 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD))
172 goto done;
173 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
174 goto done;
175 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
176 goto done;
177 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
178 goto done;
179 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
180 goto done;
181 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
182 goto done;
184 if (!remove_worktree(worktree_path))
185 goto done;
186 ok = 1;
187 done:
188 if (head_ref)
189 got_ref_close(head_ref);
190 if (repo)
191 got_repo_close(repo);
192 return ok;
195 static int
196 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
198 FILE *f;
199 char *s = "This file should not be here\n";
200 int ret = 1;
202 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
203 name) == -1)
204 return 0;
205 f = fopen(*path, "w+");
206 if (f == NULL) {
207 free(*path);
208 return 0;
210 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
211 free(*path);
212 ret = 0;
214 fclose(f);
215 return ret;
218 static int
219 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
220 const char *worktree_path, char *name)
222 const struct got_error *err;
223 char *path;
224 int ret = 0;
225 struct got_reference *head_ref = NULL;
227 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
228 return 0;
230 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
231 if (err != NULL || head_ref == NULL)
232 return 0;
234 err = got_worktree_init(worktree_path, head_ref, "/", repo);
235 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
236 (*ok)++;
237 ret = 1;
239 unlink(path);
240 free(path);
241 got_ref_close(head_ref);
242 return ret;
245 static int
246 worktree_init_exists(const char *repo_path)
248 const struct got_error *err;
249 struct got_repository *repo = NULL;
250 char worktree_path[PATH_MAX];
251 char *gotpath = NULL;
252 char *path;
253 int ok = 0;
254 FILE *f;
256 err = got_repo_open(&repo, repo_path);
257 if (err != NULL || repo == NULL)
258 goto done;
259 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
260 if (mkdtemp(worktree_path) == NULL)
261 goto done;
262 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
263 goto done;
265 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
266 == -1)
267 goto done;
268 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
269 goto done;
271 /* Create files which got_worktree_init() will try to create as well. */
272 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
273 GOT_WORKTREE_HEAD))
274 goto done;
275 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
276 GOT_WORKTREE_LOCK))
277 goto done;
278 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
279 GOT_WORKTREE_FILE_INDEX))
280 goto done;
281 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
282 GOT_WORKTREE_REPOSITORY))
283 goto done;
284 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
285 GOT_WORKTREE_PATH_PREFIX))
286 goto done;
287 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
288 GOT_WORKTREE_FORMAT))
289 goto done;
291 done:
292 if (repo)
293 got_repo_close(repo);
294 free(gotpath);
295 if (ok == 6)
296 remove_worktree(worktree_path);
297 return (ok == 6);
300 static int
301 worktree_checkout(const char *repo_path)
303 const struct got_error *err;
304 struct got_repository *repo = NULL;
305 struct got_reference *head_ref = NULL;
306 struct got_worktree *worktree = NULL;
307 char *makefile_path = NULL, *cfile_path = NULL;
308 char worktree_path[PATH_MAX];
309 int ok = 0;
310 struct stat sb;
312 err = got_repo_open(&repo, repo_path);
313 if (err != NULL || repo == NULL)
314 goto done;
315 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
316 if (err != NULL || head_ref == NULL)
317 goto done;
319 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
320 if (mkdtemp(worktree_path) == NULL)
321 goto done;
323 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
324 repo);
325 if (err != NULL)
326 goto done;
328 err = got_worktree_open(&worktree, worktree_path);
329 if (err != NULL)
330 goto done;
332 err = got_worktree_checkout_files(worktree, head_ref, repo);
333 if (err != NULL)
334 goto done;
336 test_printf("checked out %s\n", worktree_path);
338 /* The work tree should contain a Makefile and worktree_test.c. */
339 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
340 goto done;
341 if (stat(makefile_path, &sb) != 0)
342 goto done;
343 else
344 unlink(makefile_path);
345 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
346 goto done;
347 if (stat(cfile_path, &sb) != 0)
348 goto done;
349 else
350 unlink(cfile_path);
352 if (!remove_worktree(worktree_path))
353 goto done;
355 ok = 1;
356 done:
357 if (worktree)
358 got_worktree_close(worktree);
359 if (head_ref)
360 got_ref_close(head_ref);
361 if (repo)
362 got_repo_close(repo);
363 free(makefile_path);
364 free(cfile_path);
365 return ok;
368 #define RUN_TEST(expr, name) \
369 { test_ok = (expr); \
370 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
371 failure = (failure || !test_ok); }
374 void
375 usage(void)
377 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
380 int
381 main(int argc, char *argv[])
383 int test_ok = 0, failure = 0;
384 const char *repo_path;
385 int ch;
386 int vflag = 0;
388 while ((ch = getopt(argc, argv, "v")) != -1) {
389 switch (ch) {
390 case 'v':
391 verbose = 1;
392 break;
393 default:
394 usage();
395 return 1;
398 argc -= optind;
399 argv += optind;
401 if (argc == 0)
402 repo_path = GOT_REPO_PATH;
403 else if (argc == 1)
404 repo_path = argv[0];
405 else {
406 usage();
407 return 1;
410 RUN_TEST(worktree_init(repo_path), "init");
411 RUN_TEST(worktree_init_exists(repo_path), "init exists");
412 RUN_TEST(worktree_checkout(repo_path), "checkout");
414 return failure ? 1 : 0;