Blame


1 86c3caaf 2018-03-09 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 c442a90d 2019-03-10 stsp #include <uuid.h>
32 86c3caaf 2018-03-09 stsp
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 1dd54920 2019-05-11 stsp #include "got_path.h"
38 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
39 511a516b 2018-05-19 stsp #include "got_opentemp.h"
40 eea47b7e 2019-01-04 stsp #include "got_privsep.h"
41 86c3caaf 2018-03-09 stsp
42 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
43 86c3caaf 2018-03-09 stsp
44 86c3caaf 2018-03-09 stsp #define GOT_REPO_PATH "../../../"
45 86c3caaf 2018-03-09 stsp
46 86c3caaf 2018-03-09 stsp static int verbose;
47 86c3caaf 2018-03-09 stsp
48 86c3caaf 2018-03-09 stsp void
49 86c3caaf 2018-03-09 stsp test_printf(char *fmt, ...)
50 86c3caaf 2018-03-09 stsp {
51 86c3caaf 2018-03-09 stsp va_list ap;
52 86c3caaf 2018-03-09 stsp
53 86c3caaf 2018-03-09 stsp if (!verbose)
54 86c3caaf 2018-03-09 stsp return;
55 86c3caaf 2018-03-09 stsp
56 86c3caaf 2018-03-09 stsp va_start(ap, fmt);
57 86c3caaf 2018-03-09 stsp vprintf(fmt, ap);
58 86c3caaf 2018-03-09 stsp va_end(ap);
59 86c3caaf 2018-03-09 stsp }
60 86c3caaf 2018-03-09 stsp
61 86c3caaf 2018-03-09 stsp static int
62 91c986ef 2018-03-09 stsp remove_got_dir(const char *worktree_path)
63 91c986ef 2018-03-09 stsp {
64 91c986ef 2018-03-09 stsp char *path;
65 91c986ef 2018-03-09 stsp
66 91c986ef 2018-03-09 stsp if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
67 91c986ef 2018-03-09 stsp return 0;
68 91c986ef 2018-03-09 stsp rmdir(path);
69 91c986ef 2018-03-09 stsp free(path);
70 91c986ef 2018-03-09 stsp return 1;
71 91c986ef 2018-03-09 stsp }
72 91c986ef 2018-03-09 stsp
73 91c986ef 2018-03-09 stsp static int
74 91c986ef 2018-03-09 stsp remove_meta_file(const char *worktree_path, const char *name)
75 91c986ef 2018-03-09 stsp {
76 91c986ef 2018-03-09 stsp char *path;
77 91c986ef 2018-03-09 stsp
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)
80 91c986ef 2018-03-09 stsp return 0;
81 91c986ef 2018-03-09 stsp unlink(path);
82 91c986ef 2018-03-09 stsp free(path);
83 91c986ef 2018-03-09 stsp return 1;
84 91c986ef 2018-03-09 stsp }
85 91c986ef 2018-03-09 stsp
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)
89 0cd1c46a 2019-03-11 stsp {
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 1ab61ced 2019-07-10 stsp char *refname = NULL;
93 0cd1c46a 2019-03-11 stsp
94 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
95 517bab73 2019-03-11 stsp if (err)
96 517bab73 2019-03-11 stsp return err;
97 0cd1c46a 2019-03-11 stsp
98 1ab61ced 2019-07-10 stsp err = got_ref_open(&base_ref, repo, refname, 0);
99 0cd1c46a 2019-03-11 stsp if (err)
100 0cd1c46a 2019-03-11 stsp goto done;
101 0cd1c46a 2019-03-11 stsp
102 0cd1c46a 2019-03-11 stsp err = got_ref_delete(base_ref, repo);
103 0cd1c46a 2019-03-11 stsp done:
104 0cd1c46a 2019-03-11 stsp if (base_ref)
105 0cd1c46a 2019-03-11 stsp got_ref_close(base_ref);
106 0cd1c46a 2019-03-11 stsp free(refname);
107 0cd1c46a 2019-03-11 stsp return err;
108 0cd1c46a 2019-03-11 stsp
109 0cd1c46a 2019-03-11 stsp }
110 0cd1c46a 2019-03-11 stsp
111 45d8e5fd 2018-03-11 stsp static int
112 b18d25df 2018-03-11 stsp remove_worktree(const char *worktree_path)
113 91c986ef 2018-03-09 stsp {
114 0f92850e 2018-12-25 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD_REF))
115 45d8e5fd 2018-03-11 stsp return 0;
116 0f92850e 2018-12-25 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_BASE_COMMIT))
117 65596e15 2018-12-24 stsp return 0;
118 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
119 45d8e5fd 2018-03-11 stsp return 0;
120 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
121 45d8e5fd 2018-03-11 stsp return 0;
122 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
123 45d8e5fd 2018-03-11 stsp return 0;
124 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
125 45d8e5fd 2018-03-11 stsp return 0;
126 45d8e5fd 2018-03-11 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
127 ec22038e 2019-03-10 stsp return 0;
128 ec22038e 2019-03-10 stsp if (!remove_meta_file(worktree_path, GOT_WORKTREE_UUID))
129 45d8e5fd 2018-03-11 stsp return 0;
130 45d8e5fd 2018-03-11 stsp if (!remove_got_dir(worktree_path))
131 45d8e5fd 2018-03-11 stsp return 0;
132 45d8e5fd 2018-03-11 stsp if (rmdir(worktree_path) == -1)
133 45d8e5fd 2018-03-11 stsp return 0;
134 45d8e5fd 2018-03-11 stsp return 1;
135 3962e86a 2018-03-11 stsp }
136 3962e86a 2018-03-11 stsp
137 3962e86a 2018-03-11 stsp static int
138 3962e86a 2018-03-11 stsp read_meta_file(char **content, const char *path)
139 3962e86a 2018-03-11 stsp {
140 3962e86a 2018-03-11 stsp FILE *f;
141 3962e86a 2018-03-11 stsp size_t len;
142 3962e86a 2018-03-11 stsp const char delim[3] = {'\0', '\0', '\0'};
143 3962e86a 2018-03-11 stsp int ret = 0;
144 3962e86a 2018-03-11 stsp
145 3962e86a 2018-03-11 stsp f = fopen(path, "r");
146 3962e86a 2018-03-11 stsp if (f == NULL)
147 3962e86a 2018-03-11 stsp return errno;
148 3962e86a 2018-03-11 stsp
149 3962e86a 2018-03-11 stsp *content = fparseln(f, &len, NULL, delim, 0);
150 3962e86a 2018-03-11 stsp if (*content == NULL)
151 3962e86a 2018-03-11 stsp ret = errno;
152 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0 && ret == 0)
153 fb43ecf1 2019-02-11 stsp ret = errno;
154 3962e86a 2018-03-11 stsp return ret;
155 91c986ef 2018-03-09 stsp }
156 91c986ef 2018-03-09 stsp
157 91c986ef 2018-03-09 stsp static int
158 86c3caaf 2018-03-09 stsp check_meta_file_exists(const char *worktree_path, const char *name)
159 86c3caaf 2018-03-09 stsp {
160 07a7f8ad 2018-03-11 stsp struct stat sb;
161 86c3caaf 2018-03-09 stsp char *path;
162 5de261fe 2018-03-11 stsp int ret = 0;
163 86c3caaf 2018-03-09 stsp
164 86c3caaf 2018-03-09 stsp if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
165 86c3caaf 2018-03-09 stsp name) == -1)
166 86c3caaf 2018-03-09 stsp return 0;
167 5de261fe 2018-03-11 stsp if (stat(path, &sb) == 0)
168 5de261fe 2018-03-11 stsp ret = 1;
169 3962e86a 2018-03-11 stsp if (verbose) {
170 3962e86a 2018-03-11 stsp char *content;
171 3962e86a 2018-03-11 stsp if (read_meta_file(&content, path) == 0) {
172 3962e86a 2018-03-11 stsp test_printf("%s:\t%s\n", name, content);
173 3962e86a 2018-03-11 stsp free(content);
174 3962e86a 2018-03-11 stsp }
175 3962e86a 2018-03-11 stsp }
176 5de261fe 2018-03-11 stsp free(path);
177 5de261fe 2018-03-11 stsp return ret;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp static int
181 86c3caaf 2018-03-09 stsp worktree_init(const char *repo_path)
182 86c3caaf 2018-03-09 stsp {
183 86c3caaf 2018-03-09 stsp const struct got_error *err;
184 86c3caaf 2018-03-09 stsp struct got_repository *repo = NULL;
185 86c3caaf 2018-03-09 stsp struct got_reference *head_ref = NULL;
186 86c3caaf 2018-03-09 stsp char worktree_path[PATH_MAX];
187 86c3caaf 2018-03-09 stsp int ok = 0;
188 86c3caaf 2018-03-09 stsp
189 86c3caaf 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
190 86c3caaf 2018-03-09 stsp if (err != NULL || repo == NULL)
191 86c3caaf 2018-03-09 stsp goto done;
192 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
193 86c3caaf 2018-03-09 stsp if (err != NULL || head_ref == NULL)
194 86c3caaf 2018-03-09 stsp goto done;
195 86c3caaf 2018-03-09 stsp
196 86c3caaf 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
197 86c3caaf 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp
200 577ec78f 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/", repo);
201 86c3caaf 2018-03-09 stsp if (err != NULL)
202 86c3caaf 2018-03-09 stsp goto done;
203 86c3caaf 2018-03-09 stsp
204 86c3caaf 2018-03-09 stsp /* Ensure required files were created. */
205 0f92850e 2018-12-25 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD_REF))
206 86c3caaf 2018-03-09 stsp goto done;
207 0f92850e 2018-12-25 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
208 65596e15 2018-12-24 stsp goto done;
209 056e7441 2018-03-11 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
210 056e7441 2018-03-11 stsp goto done;
211 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
212 86c3caaf 2018-03-09 stsp goto done;
213 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
214 86c3caaf 2018-03-09 stsp goto done;
215 577ec78f 2018-03-11 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
216 577ec78f 2018-03-11 stsp goto done;
217 1451e70d 2018-03-10 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
218 ec22038e 2019-03-10 stsp goto done;
219 ec22038e 2019-03-10 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_UUID))
220 1451e70d 2018-03-10 stsp goto done;
221 45d8e5fd 2018-03-11 stsp
222 45d8e5fd 2018-03-11 stsp if (!remove_worktree(worktree_path))
223 45d8e5fd 2018-03-11 stsp goto done;
224 86c3caaf 2018-03-09 stsp ok = 1;
225 86c3caaf 2018-03-09 stsp done:
226 86c3caaf 2018-03-09 stsp if (head_ref)
227 86c3caaf 2018-03-09 stsp got_ref_close(head_ref);
228 86c3caaf 2018-03-09 stsp if (repo)
229 86c3caaf 2018-03-09 stsp got_repo_close(repo);
230 86c3caaf 2018-03-09 stsp return ok;
231 86c3caaf 2018-03-09 stsp }
232 86c3caaf 2018-03-09 stsp
233 0da17012 2018-03-09 stsp static int
234 0da17012 2018-03-09 stsp obstruct_meta_file(char **path, const char *worktree_path, const char *name)
235 0da17012 2018-03-09 stsp {
236 0da17012 2018-03-09 stsp FILE *f;
237 0da17012 2018-03-09 stsp char *s = "This file should not be here\n";
238 6b7476e9 2018-03-11 stsp int ret = 1;
239 0da17012 2018-03-09 stsp
240 0da17012 2018-03-09 stsp if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
241 0da17012 2018-03-09 stsp name) == -1)
242 0da17012 2018-03-09 stsp return 0;
243 0da17012 2018-03-09 stsp f = fopen(*path, "w+");
244 0da17012 2018-03-09 stsp if (f == NULL) {
245 0da17012 2018-03-09 stsp free(*path);
246 0da17012 2018-03-09 stsp return 0;
247 0da17012 2018-03-09 stsp }
248 0da17012 2018-03-09 stsp if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
249 0da17012 2018-03-09 stsp free(*path);
250 6b7476e9 2018-03-11 stsp ret = 0;
251 0da17012 2018-03-09 stsp }
252 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0)
253 fb43ecf1 2019-02-11 stsp ret = 0;
254 6b7476e9 2018-03-11 stsp return ret;
255 0da17012 2018-03-09 stsp }
256 0da17012 2018-03-09 stsp
257 0da17012 2018-03-09 stsp static int
258 8eac252b 2018-03-11 stsp obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
259 8eac252b 2018-03-11 stsp const char *worktree_path, char *name)
260 8eac252b 2018-03-11 stsp {
261 8eac252b 2018-03-11 stsp const struct got_error *err;
262 8eac252b 2018-03-11 stsp char *path;
263 8eac252b 2018-03-11 stsp int ret = 0;
264 8eac252b 2018-03-11 stsp struct got_reference *head_ref = NULL;
265 8eac252b 2018-03-11 stsp
266 8eac252b 2018-03-11 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
267 8eac252b 2018-03-11 stsp return 0;
268 8eac252b 2018-03-11 stsp
269 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
270 8eac252b 2018-03-11 stsp if (err != NULL || head_ref == NULL)
271 8eac252b 2018-03-11 stsp return 0;
272 8eac252b 2018-03-11 stsp
273 8eac252b 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/", repo);
274 8eac252b 2018-03-11 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
275 8eac252b 2018-03-11 stsp (*ok)++;
276 8eac252b 2018-03-11 stsp ret = 1;
277 8eac252b 2018-03-11 stsp }
278 8eac252b 2018-03-11 stsp unlink(path);
279 8eac252b 2018-03-11 stsp free(path);
280 8eac252b 2018-03-11 stsp got_ref_close(head_ref);
281 8eac252b 2018-03-11 stsp return ret;
282 8eac252b 2018-03-11 stsp }
283 8eac252b 2018-03-11 stsp
284 8eac252b 2018-03-11 stsp static int
285 0da17012 2018-03-09 stsp worktree_init_exists(const char *repo_path)
286 0da17012 2018-03-09 stsp {
287 0da17012 2018-03-09 stsp const struct got_error *err;
288 0da17012 2018-03-09 stsp struct got_repository *repo = NULL;
289 0da17012 2018-03-09 stsp char worktree_path[PATH_MAX];
290 91c986ef 2018-03-09 stsp char *gotpath = NULL;
291 0da17012 2018-03-09 stsp int ok = 0;
292 0da17012 2018-03-09 stsp
293 0da17012 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
294 0da17012 2018-03-09 stsp if (err != NULL || repo == NULL)
295 0da17012 2018-03-09 stsp goto done;
296 0da17012 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
297 0da17012 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
298 0da17012 2018-03-09 stsp goto done;
299 91c986ef 2018-03-09 stsp if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
300 91c986ef 2018-03-09 stsp goto done;
301 0da17012 2018-03-09 stsp
302 0da17012 2018-03-09 stsp if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
303 0da17012 2018-03-09 stsp == -1)
304 0da17012 2018-03-09 stsp goto done;
305 0da17012 2018-03-09 stsp if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
306 0da17012 2018-03-09 stsp goto done;
307 0da17012 2018-03-09 stsp
308 0da17012 2018-03-09 stsp /* Create files which got_worktree_init() will try to create as well. */
309 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
310 0f92850e 2018-12-25 stsp GOT_WORKTREE_HEAD_REF))
311 0da17012 2018-03-09 stsp goto done;
312 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
313 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT))
314 65596e15 2018-12-24 stsp goto done;
315 65596e15 2018-12-24 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
316 8eac252b 2018-03-11 stsp GOT_WORKTREE_LOCK))
317 056e7441 2018-03-11 stsp goto done;
318 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
319 8eac252b 2018-03-11 stsp GOT_WORKTREE_FILE_INDEX))
320 0da17012 2018-03-09 stsp goto done;
321 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
322 8eac252b 2018-03-11 stsp GOT_WORKTREE_REPOSITORY))
323 0da17012 2018-03-09 stsp goto done;
324 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
325 8eac252b 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX))
326 577ec78f 2018-03-11 stsp goto done;
327 8eac252b 2018-03-11 stsp if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
328 8eac252b 2018-03-11 stsp GOT_WORKTREE_FORMAT))
329 1451e70d 2018-03-10 stsp goto done;
330 1451e70d 2018-03-10 stsp
331 0da17012 2018-03-09 stsp done:
332 0da17012 2018-03-09 stsp if (repo)
333 0da17012 2018-03-09 stsp got_repo_close(repo);
334 91c986ef 2018-03-09 stsp free(gotpath);
335 65596e15 2018-12-24 stsp if (ok == 7)
336 b18d25df 2018-03-11 stsp remove_worktree(worktree_path);
337 65596e15 2018-12-24 stsp return (ok == 7);
338 291c6f03 2018-03-12 stsp }
339 291c6f03 2018-03-12 stsp
340 1ee397ad 2019-07-12 stsp static const struct got_error *
341 a0eb853d 2018-12-29 stsp progress_cb(void *arg, unsigned char status, const char *path)
342 291c6f03 2018-03-12 stsp {
343 1ee397ad 2019-07-12 stsp return NULL;
344 0da17012 2018-03-09 stsp }
345 9d31a1d8 2018-03-11 stsp
346 9d31a1d8 2018-03-11 stsp static int
347 9d31a1d8 2018-03-11 stsp worktree_checkout(const char *repo_path)
348 9d31a1d8 2018-03-11 stsp {
349 9d31a1d8 2018-03-11 stsp const struct got_error *err;
350 9d31a1d8 2018-03-11 stsp struct got_repository *repo = NULL;
351 9d31a1d8 2018-03-11 stsp struct got_reference *head_ref = NULL;
352 9d31a1d8 2018-03-11 stsp struct got_worktree *worktree = NULL;
353 9d31a1d8 2018-03-11 stsp char *makefile_path = NULL, *cfile_path = NULL;
354 9d31a1d8 2018-03-11 stsp char worktree_path[PATH_MAX];
355 9d31a1d8 2018-03-11 stsp int ok = 0;
356 9d31a1d8 2018-03-11 stsp struct stat sb;
357 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
358 9d31a1d8 2018-03-11 stsp
359 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
360 f2ea84fa 2019-07-27 stsp
361 9d31a1d8 2018-03-11 stsp err = got_repo_open(&repo, repo_path);
362 9d31a1d8 2018-03-11 stsp if (err != NULL || repo == NULL)
363 9d31a1d8 2018-03-11 stsp goto done;
364 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
365 9d31a1d8 2018-03-11 stsp if (err != NULL || head_ref == NULL)
366 9d31a1d8 2018-03-11 stsp goto done;
367 0da17012 2018-03-09 stsp
368 9d31a1d8 2018-03-11 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
369 9d31a1d8 2018-03-11 stsp if (mkdtemp(worktree_path) == NULL)
370 9d31a1d8 2018-03-11 stsp goto done;
371 9d31a1d8 2018-03-11 stsp
372 9d31a1d8 2018-03-11 stsp err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
373 9d31a1d8 2018-03-11 stsp repo);
374 9d31a1d8 2018-03-11 stsp if (err != NULL)
375 9d31a1d8 2018-03-11 stsp goto done;
376 9d31a1d8 2018-03-11 stsp
377 9d31a1d8 2018-03-11 stsp err = got_worktree_open(&worktree, worktree_path);
378 9d31a1d8 2018-03-11 stsp if (err != NULL)
379 9d31a1d8 2018-03-11 stsp goto done;
380 9d31a1d8 2018-03-11 stsp
381 f2ea84fa 2019-07-27 stsp err = got_pathlist_append(NULL, &paths, "", NULL);
382 f2ea84fa 2019-07-27 stsp if (err)
383 f2ea84fa 2019-07-27 stsp goto done;
384 f2ea84fa 2019-07-27 stsp err = got_worktree_checkout_files(worktree, &paths, repo, progress_cb,
385 f2ea84fa 2019-07-27 stsp NULL, NULL, NULL);
386 9d31a1d8 2018-03-11 stsp if (err != NULL)
387 9d31a1d8 2018-03-11 stsp goto done;
388 9d31a1d8 2018-03-11 stsp
389 9d31a1d8 2018-03-11 stsp test_printf("checked out %s\n", worktree_path);
390 9d31a1d8 2018-03-11 stsp
391 9d31a1d8 2018-03-11 stsp /* The work tree should contain a Makefile and worktree_test.c. */
392 9d31a1d8 2018-03-11 stsp if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
393 9d31a1d8 2018-03-11 stsp goto done;
394 9d31a1d8 2018-03-11 stsp if (stat(makefile_path, &sb) != 0)
395 9d31a1d8 2018-03-11 stsp goto done;
396 9d31a1d8 2018-03-11 stsp else
397 9d31a1d8 2018-03-11 stsp unlink(makefile_path);
398 9d31a1d8 2018-03-11 stsp if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
399 9d31a1d8 2018-03-11 stsp goto done;
400 9d31a1d8 2018-03-11 stsp if (stat(cfile_path, &sb) != 0)
401 9d31a1d8 2018-03-11 stsp goto done;
402 9d31a1d8 2018-03-11 stsp else
403 9d31a1d8 2018-03-11 stsp unlink(cfile_path);
404 9d31a1d8 2018-03-11 stsp
405 0cd1c46a 2019-03-11 stsp err = remove_worktree_base_ref(worktree, repo);
406 0cd1c46a 2019-03-11 stsp if (err)
407 0cd1c46a 2019-03-11 stsp goto done;
408 9d31a1d8 2018-03-11 stsp if (!remove_worktree(worktree_path))
409 9d31a1d8 2018-03-11 stsp goto done;
410 9d31a1d8 2018-03-11 stsp
411 9d31a1d8 2018-03-11 stsp ok = 1;
412 9d31a1d8 2018-03-11 stsp done:
413 9d31a1d8 2018-03-11 stsp if (worktree)
414 9d31a1d8 2018-03-11 stsp got_worktree_close(worktree);
415 9d31a1d8 2018-03-11 stsp if (head_ref)
416 9d31a1d8 2018-03-11 stsp got_ref_close(head_ref);
417 9d31a1d8 2018-03-11 stsp if (repo)
418 9d31a1d8 2018-03-11 stsp got_repo_close(repo);
419 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
420 9d31a1d8 2018-03-11 stsp free(makefile_path);
421 9d31a1d8 2018-03-11 stsp free(cfile_path);
422 9d31a1d8 2018-03-11 stsp return ok;
423 9d31a1d8 2018-03-11 stsp }
424 9d31a1d8 2018-03-11 stsp
425 86c3caaf 2018-03-09 stsp #define RUN_TEST(expr, name) \
426 86c3caaf 2018-03-09 stsp { test_ok = (expr); \
427 9465d522 2019-01-03 stsp printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
428 86c3caaf 2018-03-09 stsp failure = (failure || !test_ok); }
429 86c3caaf 2018-03-09 stsp
430 86c3caaf 2018-03-09 stsp
431 86c3caaf 2018-03-09 stsp void
432 86c3caaf 2018-03-09 stsp usage(void)
433 86c3caaf 2018-03-09 stsp {
434 86c3caaf 2018-03-09 stsp fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
435 86c3caaf 2018-03-09 stsp }
436 86c3caaf 2018-03-09 stsp
437 86c3caaf 2018-03-09 stsp int
438 86c3caaf 2018-03-09 stsp main(int argc, char *argv[])
439 86c3caaf 2018-03-09 stsp {
440 86c3caaf 2018-03-09 stsp int test_ok = 0, failure = 0;
441 86c3caaf 2018-03-09 stsp const char *repo_path;
442 eea47b7e 2019-01-04 stsp char *cwd = NULL;
443 86c3caaf 2018-03-09 stsp int ch;
444 86c3caaf 2018-03-09 stsp
445 2ff12563 2018-09-15 stsp #ifndef PROFILE
446 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
447 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
448 f8352b2a 2018-03-12 stsp err(1, "pledge");
449 2ff12563 2018-09-15 stsp #endif
450 f8352b2a 2018-03-12 stsp
451 86c3caaf 2018-03-09 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
452 86c3caaf 2018-03-09 stsp switch (ch) {
453 86c3caaf 2018-03-09 stsp case 'v':
454 86c3caaf 2018-03-09 stsp verbose = 1;
455 86c3caaf 2018-03-09 stsp break;
456 86c3caaf 2018-03-09 stsp default:
457 86c3caaf 2018-03-09 stsp usage();
458 86c3caaf 2018-03-09 stsp return 1;
459 86c3caaf 2018-03-09 stsp }
460 86c3caaf 2018-03-09 stsp }
461 86c3caaf 2018-03-09 stsp argc -= optind;
462 86c3caaf 2018-03-09 stsp argv += optind;
463 86c3caaf 2018-03-09 stsp
464 86c3caaf 2018-03-09 stsp if (argc == 0)
465 86c3caaf 2018-03-09 stsp repo_path = GOT_REPO_PATH;
466 86c3caaf 2018-03-09 stsp else if (argc == 1)
467 86c3caaf 2018-03-09 stsp repo_path = argv[0];
468 86c3caaf 2018-03-09 stsp else {
469 86c3caaf 2018-03-09 stsp usage();
470 86c3caaf 2018-03-09 stsp return 1;
471 86c3caaf 2018-03-09 stsp }
472 86c3caaf 2018-03-09 stsp
473 eea47b7e 2019-01-04 stsp cwd = getcwd(NULL, 0);
474 eea47b7e 2019-01-04 stsp if (cwd == NULL)
475 eea47b7e 2019-01-04 stsp err(1, "getcwd");
476 eea47b7e 2019-01-04 stsp if (unveil(cwd, "rwc") != 0)
477 eea47b7e 2019-01-04 stsp err(1, "unvail");
478 eea47b7e 2019-01-04 stsp free(cwd);
479 eea47b7e 2019-01-04 stsp
480 eea47b7e 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
481 eea47b7e 2019-01-04 stsp err(1, "unveil");
482 eea47b7e 2019-01-04 stsp
483 0cd1c46a 2019-03-11 stsp if (unveil(repo_path, "rwc") != 0)
484 eea47b7e 2019-01-04 stsp err(1, "unveil");
485 eea47b7e 2019-01-04 stsp
486 eea47b7e 2019-01-04 stsp if (got_privsep_unveil_exec_helpers() != NULL)
487 eea47b7e 2019-01-04 stsp return 1;
488 eea47b7e 2019-01-04 stsp
489 eea47b7e 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
490 eea47b7e 2019-01-04 stsp err(1, "unveil");
491 eea47b7e 2019-01-04 stsp
492 86c3caaf 2018-03-09 stsp RUN_TEST(worktree_init(repo_path), "init");
493 0da17012 2018-03-09 stsp RUN_TEST(worktree_init_exists(repo_path), "init exists");
494 9d31a1d8 2018-03-11 stsp RUN_TEST(worktree_checkout(repo_path), "checkout");
495 86c3caaf 2018-03-09 stsp
496 86c3caaf 2018-03-09 stsp return failure ? 1 : 0;
497 86c3caaf 2018-03-09 stsp }