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