Blame


1 86c3caaf 2018-03-09 stsp /*
2 86c3caaf 2018-03-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
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.
7 86c3caaf 2018-03-09 stsp *
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.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
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>
21 86c3caaf 2018-03-09 stsp
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 86c3caaf 2018-03-09 stsp
32 86c3caaf 2018-03-09 stsp #include "got_error.h"
33 86c3caaf 2018-03-09 stsp #include "got_object.h"
34 86c3caaf 2018-03-09 stsp #include "got_refs.h"
35 86c3caaf 2018-03-09 stsp #include "got_repository.h"
36 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
37 86c3caaf 2018-03-09 stsp
38 32cb896c 2018-03-11 stsp #include "got_worktree_lib.h"
39 32cb896c 2018-03-11 stsp #include "got_path_lib.h"
40 86c3caaf 2018-03-09 stsp
41 86c3caaf 2018-03-09 stsp #define GOT_REPO_PATH "../../../"
42 86c3caaf 2018-03-09 stsp
43 86c3caaf 2018-03-09 stsp static int verbose;
44 86c3caaf 2018-03-09 stsp
45 86c3caaf 2018-03-09 stsp void
46 86c3caaf 2018-03-09 stsp test_printf(char *fmt, ...)
47 86c3caaf 2018-03-09 stsp {
48 86c3caaf 2018-03-09 stsp va_list ap;
49 86c3caaf 2018-03-09 stsp
50 86c3caaf 2018-03-09 stsp if (!verbose)
51 86c3caaf 2018-03-09 stsp return;
52 86c3caaf 2018-03-09 stsp
53 86c3caaf 2018-03-09 stsp va_start(ap, fmt);
54 86c3caaf 2018-03-09 stsp vprintf(fmt, ap);
55 86c3caaf 2018-03-09 stsp va_end(ap);
56 86c3caaf 2018-03-09 stsp }
57 86c3caaf 2018-03-09 stsp
58 86c3caaf 2018-03-09 stsp static int
59 91c986ef 2018-03-09 stsp remove_got_dir(const char *worktree_path)
60 91c986ef 2018-03-09 stsp {
61 91c986ef 2018-03-09 stsp char *path;
62 91c986ef 2018-03-09 stsp
63 91c986ef 2018-03-09 stsp if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
64 91c986ef 2018-03-09 stsp return 0;
65 91c986ef 2018-03-09 stsp rmdir(path);
66 91c986ef 2018-03-09 stsp free(path);
67 91c986ef 2018-03-09 stsp return 1;
68 91c986ef 2018-03-09 stsp }
69 91c986ef 2018-03-09 stsp
70 91c986ef 2018-03-09 stsp static int
71 91c986ef 2018-03-09 stsp remove_meta_file(const char *worktree_path, const char *name)
72 91c986ef 2018-03-09 stsp {
73 91c986ef 2018-03-09 stsp char *path;
74 91c986ef 2018-03-09 stsp
75 91c986ef 2018-03-09 stsp if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
76 91c986ef 2018-03-09 stsp name) == -1)
77 91c986ef 2018-03-09 stsp return 0;
78 91c986ef 2018-03-09 stsp unlink(path);
79 91c986ef 2018-03-09 stsp free(path);
80 91c986ef 2018-03-09 stsp return 1;
81 91c986ef 2018-03-09 stsp }
82 91c986ef 2018-03-09 stsp
83 45d8e5fd 2018-03-11 stsp static int
84 b18d25df 2018-03-11 stsp remove_worktree(const char *worktree_path)
85 91c986ef 2018-03-09 stsp {
86 fdf001a7 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD))
87 45d8e5fd 2018-03-11 stsp return 0;
88 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
89 45d8e5fd 2018-03-11 stsp return 0;
90 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
91 45d8e5fd 2018-03-11 stsp return 0;
92 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
93 45d8e5fd 2018-03-11 stsp return 0;
94 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
95 45d8e5fd 2018-03-11 stsp return 0;
96 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
97 45d8e5fd 2018-03-11 stsp return 0;
98 45d8e5fd 2018-03-11 stsp if (!remove_got_dir(worktree_path))
99 45d8e5fd 2018-03-11 stsp return 0;
100 45d8e5fd 2018-03-11 stsp if (rmdir(worktree_path) == -1)
101 45d8e5fd 2018-03-11 stsp return 0;
102 45d8e5fd 2018-03-11 stsp return 1;
103 3962e86a 2018-03-11 stsp }
104 3962e86a 2018-03-11 stsp
105 3962e86a 2018-03-11 stsp static int
106 3962e86a 2018-03-11 stsp read_meta_file(char **content, const char *path)
107 3962e86a 2018-03-11 stsp {
108 3962e86a 2018-03-11 stsp FILE *f;
109 3962e86a 2018-03-11 stsp size_t len;
110 3962e86a 2018-03-11 stsp const char delim[3] = {'\0', '\0', '\0'};
111 3962e86a 2018-03-11 stsp int ret = 0;
112 3962e86a 2018-03-11 stsp
113 3962e86a 2018-03-11 stsp f = fopen(path, "r");
114 3962e86a 2018-03-11 stsp if (f == NULL)
115 3962e86a 2018-03-11 stsp return errno;
116 3962e86a 2018-03-11 stsp
117 3962e86a 2018-03-11 stsp *content = fparseln(f, &len, NULL, delim, 0);
118 3962e86a 2018-03-11 stsp if (*content == NULL)
119 3962e86a 2018-03-11 stsp ret = errno;
120 3962e86a 2018-03-11 stsp fclose(f);
121 3962e86a 2018-03-11 stsp return ret;
122 91c986ef 2018-03-09 stsp }
123 91c986ef 2018-03-09 stsp
124 91c986ef 2018-03-09 stsp static int
125 86c3caaf 2018-03-09 stsp check_meta_file_exists(const char *worktree_path, const char *name)
126 86c3caaf 2018-03-09 stsp {
127 07a7f8ad 2018-03-11 stsp struct stat sb;
128 86c3caaf 2018-03-09 stsp char *path;
129 5de261fe 2018-03-11 stsp int ret = 0;
130 86c3caaf 2018-03-09 stsp
131 86c3caaf 2018-03-09 stsp if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
132 86c3caaf 2018-03-09 stsp name) == -1)
133 86c3caaf 2018-03-09 stsp return 0;
134 5de261fe 2018-03-11 stsp if (stat(path, &sb) == 0)
135 5de261fe 2018-03-11 stsp ret = 1;
136 3962e86a 2018-03-11 stsp if (verbose) {
137 3962e86a 2018-03-11 stsp char *content;
138 3962e86a 2018-03-11 stsp if (read_meta_file(&content, path) == 0) {
139 3962e86a 2018-03-11 stsp test_printf("%s:\t%s\n", name, content);
140 3962e86a 2018-03-11 stsp free(content);
141 3962e86a 2018-03-11 stsp }
142 3962e86a 2018-03-11 stsp }
143 5de261fe 2018-03-11 stsp free(path);
144 5de261fe 2018-03-11 stsp return ret;
145 86c3caaf 2018-03-09 stsp }
146 86c3caaf 2018-03-09 stsp
147 86c3caaf 2018-03-09 stsp static int
148 86c3caaf 2018-03-09 stsp worktree_init(const char *repo_path)
149 86c3caaf 2018-03-09 stsp {
150 86c3caaf 2018-03-09 stsp const struct got_error *err;
151 86c3caaf 2018-03-09 stsp struct got_repository *repo = NULL;
152 86c3caaf 2018-03-09 stsp struct got_reference *head_ref = NULL;
153 86c3caaf 2018-03-09 stsp char worktree_path[PATH_MAX];
154 86c3caaf 2018-03-09 stsp int ok = 0;
155 86c3caaf 2018-03-09 stsp
156 86c3caaf 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
157 86c3caaf 2018-03-09 stsp if (err != NULL || repo == NULL)
158 86c3caaf 2018-03-09 stsp goto done;
159 86c3caaf 2018-03-09 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
160 86c3caaf 2018-03-09 stsp if (err != NULL || head_ref == NULL)
161 86c3caaf 2018-03-09 stsp goto done;
162 86c3caaf 2018-03-09 stsp
163 86c3caaf 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
164 86c3caaf 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
165 86c3caaf 2018-03-09 stsp goto done;
166 86c3caaf 2018-03-09 stsp
167 577ec78f 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/", repo);
168 86c3caaf 2018-03-09 stsp if (err != NULL)
169 86c3caaf 2018-03-09 stsp goto done;
170 86c3caaf 2018-03-09 stsp
171 86c3caaf 2018-03-09 stsp /* Ensure required files were created. */
172 fdf001a7 2018-03-11 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD))
173 86c3caaf 2018-03-09 stsp goto done;
174 056e7441 2018-03-11 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
175 056e7441 2018-03-11 stsp goto done;
176 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
179 86c3caaf 2018-03-09 stsp goto done;
180 577ec78f 2018-03-11 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
181 577ec78f 2018-03-11 stsp goto done;
182 1451e70d 2018-03-10 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
183 1451e70d 2018-03-10 stsp goto done;
184 45d8e5fd 2018-03-11 stsp
185 45d8e5fd 2018-03-11 stsp if (!remove_worktree(worktree_path))
186 45d8e5fd 2018-03-11 stsp goto done;
187 86c3caaf 2018-03-09 stsp ok = 1;
188 86c3caaf 2018-03-09 stsp done:
189 86c3caaf 2018-03-09 stsp if (head_ref)
190 86c3caaf 2018-03-09 stsp got_ref_close(head_ref);
191 86c3caaf 2018-03-09 stsp if (repo)
192 86c3caaf 2018-03-09 stsp got_repo_close(repo);
193 86c3caaf 2018-03-09 stsp return ok;
194 86c3caaf 2018-03-09 stsp }
195 86c3caaf 2018-03-09 stsp
196 0da17012 2018-03-09 stsp static int
197 0da17012 2018-03-09 stsp obstruct_meta_file(char **path, const char *worktree_path, const char *name)
198 0da17012 2018-03-09 stsp {
199 0da17012 2018-03-09 stsp FILE *f;
200 0da17012 2018-03-09 stsp char *s = "This file should not be here\n";
201 6b7476e9 2018-03-11 stsp int ret = 1;
202 0da17012 2018-03-09 stsp
203 0da17012 2018-03-09 stsp if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
204 0da17012 2018-03-09 stsp name) == -1)
205 0da17012 2018-03-09 stsp return 0;
206 0da17012 2018-03-09 stsp f = fopen(*path, "w+");
207 0da17012 2018-03-09 stsp if (f == NULL) {
208 0da17012 2018-03-09 stsp free(*path);
209 0da17012 2018-03-09 stsp return 0;
210 0da17012 2018-03-09 stsp }
211 0da17012 2018-03-09 stsp if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
212 0da17012 2018-03-09 stsp free(*path);
213 6b7476e9 2018-03-11 stsp ret = 0;
214 0da17012 2018-03-09 stsp }
215 0da17012 2018-03-09 stsp fclose(f);
216 6b7476e9 2018-03-11 stsp return ret;
217 0da17012 2018-03-09 stsp }
218 0da17012 2018-03-09 stsp
219 0da17012 2018-03-09 stsp static int
220 8eac252b 2018-03-11 stsp obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
221 8eac252b 2018-03-11 stsp const char *worktree_path, char *name)
222 8eac252b 2018-03-11 stsp {
223 8eac252b 2018-03-11 stsp const struct got_error *err;
224 8eac252b 2018-03-11 stsp char *path;
225 8eac252b 2018-03-11 stsp int ret = 0;
226 8eac252b 2018-03-11 stsp struct got_reference *head_ref = NULL;
227 8eac252b 2018-03-11 stsp
228 8eac252b 2018-03-11 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
229 8eac252b 2018-03-11 stsp return 0;
230 8eac252b 2018-03-11 stsp
231 8eac252b 2018-03-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
232 8eac252b 2018-03-11 stsp if (err != NULL || head_ref == NULL)
233 8eac252b 2018-03-11 stsp return 0;
234 8eac252b 2018-03-11 stsp
235 8eac252b 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/", repo);
236 8eac252b 2018-03-11 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
237 8eac252b 2018-03-11 stsp (*ok)++;
238 8eac252b 2018-03-11 stsp ret = 1;
239 8eac252b 2018-03-11 stsp }
240 8eac252b 2018-03-11 stsp unlink(path);
241 8eac252b 2018-03-11 stsp free(path);
242 8eac252b 2018-03-11 stsp got_ref_close(head_ref);
243 8eac252b 2018-03-11 stsp return ret;
244 8eac252b 2018-03-11 stsp }
245 8eac252b 2018-03-11 stsp
246 8eac252b 2018-03-11 stsp static int
247 0da17012 2018-03-09 stsp worktree_init_exists(const char *repo_path)
248 0da17012 2018-03-09 stsp {
249 0da17012 2018-03-09 stsp const struct got_error *err;
250 0da17012 2018-03-09 stsp struct got_repository *repo = NULL;
251 0da17012 2018-03-09 stsp char worktree_path[PATH_MAX];
252 91c986ef 2018-03-09 stsp char *gotpath = NULL;
253 0da17012 2018-03-09 stsp int ok = 0;
254 0da17012 2018-03-09 stsp
255 0da17012 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
256 0da17012 2018-03-09 stsp if (err != NULL || repo == NULL)
257 0da17012 2018-03-09 stsp goto done;
258 0da17012 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
259 0da17012 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
260 0da17012 2018-03-09 stsp goto done;
261 91c986ef 2018-03-09 stsp if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
262 91c986ef 2018-03-09 stsp goto done;
263 0da17012 2018-03-09 stsp
264 0da17012 2018-03-09 stsp if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
265 0da17012 2018-03-09 stsp == -1)
266 0da17012 2018-03-09 stsp goto done;
267 0da17012 2018-03-09 stsp if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
268 0da17012 2018-03-09 stsp goto done;
269 0da17012 2018-03-09 stsp
270 0da17012 2018-03-09 stsp /* Create files which got_worktree_init() will try to create as well. */
271 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
272 fdf001a7 2018-03-11 stsp GOT_WORKTREE_HEAD))
273 0da17012 2018-03-09 stsp goto done;
274 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
275 8eac252b 2018-03-11 stsp GOT_WORKTREE_LOCK))
276 056e7441 2018-03-11 stsp goto done;
277 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
278 8eac252b 2018-03-11 stsp GOT_WORKTREE_FILE_INDEX))
279 0da17012 2018-03-09 stsp goto done;
280 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
281 8eac252b 2018-03-11 stsp GOT_WORKTREE_REPOSITORY))
282 0da17012 2018-03-09 stsp goto done;
283 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
284 8eac252b 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX))
285 577ec78f 2018-03-11 stsp goto done;
286 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
287 8eac252b 2018-03-11 stsp GOT_WORKTREE_FORMAT))
288 1451e70d 2018-03-10 stsp goto done;
289 1451e70d 2018-03-10 stsp
290 0da17012 2018-03-09 stsp done:
291 0da17012 2018-03-09 stsp if (repo)
292 0da17012 2018-03-09 stsp got_repo_close(repo);
293 91c986ef 2018-03-09 stsp free(gotpath);
294 e77c4c9f 2018-03-12 stsp if (ok == 6)
295 b18d25df 2018-03-11 stsp remove_worktree(worktree_path);
296 e77c4c9f 2018-03-12 stsp return (ok == 6);
297 0da17012 2018-03-09 stsp }
298 9d31a1d8 2018-03-11 stsp
299 9d31a1d8 2018-03-11 stsp static int
300 9d31a1d8 2018-03-11 stsp worktree_checkout(const char *repo_path)
301 9d31a1d8 2018-03-11 stsp {
302 9d31a1d8 2018-03-11 stsp const struct got_error *err;
303 9d31a1d8 2018-03-11 stsp struct got_repository *repo = NULL;
304 9d31a1d8 2018-03-11 stsp struct got_reference *head_ref = NULL;
305 9d31a1d8 2018-03-11 stsp struct got_worktree *worktree = NULL;
306 9d31a1d8 2018-03-11 stsp char *makefile_path = NULL, *cfile_path = NULL;
307 9d31a1d8 2018-03-11 stsp char worktree_path[PATH_MAX];
308 9d31a1d8 2018-03-11 stsp int ok = 0;
309 9d31a1d8 2018-03-11 stsp struct stat sb;
310 9d31a1d8 2018-03-11 stsp
311 9d31a1d8 2018-03-11 stsp err = got_repo_open(&repo, repo_path);
312 9d31a1d8 2018-03-11 stsp if (err != NULL || repo == NULL)
313 9d31a1d8 2018-03-11 stsp goto done;
314 9d31a1d8 2018-03-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
315 9d31a1d8 2018-03-11 stsp if (err != NULL || head_ref == NULL)
316 9d31a1d8 2018-03-11 stsp goto done;
317 0da17012 2018-03-09 stsp
318 9d31a1d8 2018-03-11 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
319 9d31a1d8 2018-03-11 stsp if (mkdtemp(worktree_path) == NULL)
320 9d31a1d8 2018-03-11 stsp goto done;
321 9d31a1d8 2018-03-11 stsp
322 9d31a1d8 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
323 9d31a1d8 2018-03-11 stsp repo);
324 9d31a1d8 2018-03-11 stsp if (err != NULL)
325 9d31a1d8 2018-03-11 stsp goto done;
326 9d31a1d8 2018-03-11 stsp
327 9d31a1d8 2018-03-11 stsp err = got_worktree_open(&worktree, worktree_path);
328 9d31a1d8 2018-03-11 stsp if (err != NULL)
329 9d31a1d8 2018-03-11 stsp goto done;
330 9d31a1d8 2018-03-11 stsp
331 9d31a1d8 2018-03-11 stsp err = got_worktree_checkout_files(worktree, head_ref, repo);
332 9d31a1d8 2018-03-11 stsp if (err != NULL)
333 9d31a1d8 2018-03-11 stsp goto done;
334 9d31a1d8 2018-03-11 stsp
335 9d31a1d8 2018-03-11 stsp test_printf("checked out %s\n", worktree_path);
336 9d31a1d8 2018-03-11 stsp
337 9d31a1d8 2018-03-11 stsp /* The work tree should contain a Makefile and worktree_test.c. */
338 9d31a1d8 2018-03-11 stsp if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
339 9d31a1d8 2018-03-11 stsp goto done;
340 9d31a1d8 2018-03-11 stsp if (stat(makefile_path, &sb) != 0)
341 9d31a1d8 2018-03-11 stsp goto done;
342 9d31a1d8 2018-03-11 stsp else
343 9d31a1d8 2018-03-11 stsp unlink(makefile_path);
344 9d31a1d8 2018-03-11 stsp if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
345 9d31a1d8 2018-03-11 stsp goto done;
346 9d31a1d8 2018-03-11 stsp if (stat(cfile_path, &sb) != 0)
347 9d31a1d8 2018-03-11 stsp goto done;
348 9d31a1d8 2018-03-11 stsp else
349 9d31a1d8 2018-03-11 stsp unlink(cfile_path);
350 9d31a1d8 2018-03-11 stsp
351 9d31a1d8 2018-03-11 stsp if (!remove_worktree(worktree_path))
352 9d31a1d8 2018-03-11 stsp goto done;
353 9d31a1d8 2018-03-11 stsp
354 9d31a1d8 2018-03-11 stsp ok = 1;
355 9d31a1d8 2018-03-11 stsp done:
356 9d31a1d8 2018-03-11 stsp if (worktree)
357 9d31a1d8 2018-03-11 stsp got_worktree_close(worktree);
358 9d31a1d8 2018-03-11 stsp if (head_ref)
359 9d31a1d8 2018-03-11 stsp got_ref_close(head_ref);
360 9d31a1d8 2018-03-11 stsp if (repo)
361 9d31a1d8 2018-03-11 stsp got_repo_close(repo);
362 9d31a1d8 2018-03-11 stsp free(makefile_path);
363 9d31a1d8 2018-03-11 stsp free(cfile_path);
364 9d31a1d8 2018-03-11 stsp return ok;
365 9d31a1d8 2018-03-11 stsp }
366 9d31a1d8 2018-03-11 stsp
367 86c3caaf 2018-03-09 stsp #define RUN_TEST(expr, name) \
368 86c3caaf 2018-03-09 stsp { test_ok = (expr); \
369 86c3caaf 2018-03-09 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
370 86c3caaf 2018-03-09 stsp failure = (failure || !test_ok); }
371 86c3caaf 2018-03-09 stsp
372 86c3caaf 2018-03-09 stsp
373 86c3caaf 2018-03-09 stsp void
374 86c3caaf 2018-03-09 stsp usage(void)
375 86c3caaf 2018-03-09 stsp {
376 86c3caaf 2018-03-09 stsp fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
377 86c3caaf 2018-03-09 stsp }
378 86c3caaf 2018-03-09 stsp
379 86c3caaf 2018-03-09 stsp int
380 86c3caaf 2018-03-09 stsp main(int argc, char *argv[])
381 86c3caaf 2018-03-09 stsp {
382 86c3caaf 2018-03-09 stsp int test_ok = 0, failure = 0;
383 86c3caaf 2018-03-09 stsp const char *repo_path;
384 86c3caaf 2018-03-09 stsp int ch;
385 86c3caaf 2018-03-09 stsp
386 f8352b2a 2018-03-12 stsp if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
387 f8352b2a 2018-03-12 stsp err(1, "pledge");
388 f8352b2a 2018-03-12 stsp
389 86c3caaf 2018-03-09 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
390 86c3caaf 2018-03-09 stsp switch (ch) {
391 86c3caaf 2018-03-09 stsp case 'v':
392 86c3caaf 2018-03-09 stsp verbose = 1;
393 86c3caaf 2018-03-09 stsp break;
394 86c3caaf 2018-03-09 stsp default:
395 86c3caaf 2018-03-09 stsp usage();
396 86c3caaf 2018-03-09 stsp return 1;
397 86c3caaf 2018-03-09 stsp }
398 86c3caaf 2018-03-09 stsp }
399 86c3caaf 2018-03-09 stsp argc -= optind;
400 86c3caaf 2018-03-09 stsp argv += optind;
401 86c3caaf 2018-03-09 stsp
402 86c3caaf 2018-03-09 stsp if (argc == 0)
403 86c3caaf 2018-03-09 stsp repo_path = GOT_REPO_PATH;
404 86c3caaf 2018-03-09 stsp else if (argc == 1)
405 86c3caaf 2018-03-09 stsp repo_path = argv[0];
406 86c3caaf 2018-03-09 stsp else {
407 86c3caaf 2018-03-09 stsp usage();
408 86c3caaf 2018-03-09 stsp return 1;
409 86c3caaf 2018-03-09 stsp }
410 86c3caaf 2018-03-09 stsp
411 86c3caaf 2018-03-09 stsp RUN_TEST(worktree_init(repo_path), "init");
412 0da17012 2018-03-09 stsp RUN_TEST(worktree_init_exists(repo_path), "init exists");
413 9d31a1d8 2018-03-11 stsp RUN_TEST(worktree_checkout(repo_path), "checkout");
414 86c3caaf 2018-03-09 stsp
415 86c3caaf 2018-03-09 stsp return failure ? 1 : 0;
416 86c3caaf 2018-03-09 stsp }