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 86c3caaf 2018-03-09 stsp
29 86c3caaf 2018-03-09 stsp #include "got_error.h"
30 86c3caaf 2018-03-09 stsp #include "got_object.h"
31 86c3caaf 2018-03-09 stsp #include "got_refs.h"
32 86c3caaf 2018-03-09 stsp #include "got_repository.h"
33 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
34 86c3caaf 2018-03-09 stsp
35 86c3caaf 2018-03-09 stsp #include "got_worktree_priv.h"
36 0da17012 2018-03-09 stsp #include "got_path_priv.h"
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #define GOT_REPO_PATH "../../../"
39 86c3caaf 2018-03-09 stsp
40 86c3caaf 2018-03-09 stsp static int verbose;
41 86c3caaf 2018-03-09 stsp
42 86c3caaf 2018-03-09 stsp void
43 86c3caaf 2018-03-09 stsp test_printf(char *fmt, ...)
44 86c3caaf 2018-03-09 stsp {
45 86c3caaf 2018-03-09 stsp va_list ap;
46 86c3caaf 2018-03-09 stsp
47 86c3caaf 2018-03-09 stsp if (!verbose)
48 86c3caaf 2018-03-09 stsp return;
49 86c3caaf 2018-03-09 stsp
50 86c3caaf 2018-03-09 stsp va_start(ap, fmt);
51 86c3caaf 2018-03-09 stsp vprintf(fmt, ap);
52 86c3caaf 2018-03-09 stsp va_end(ap);
53 86c3caaf 2018-03-09 stsp }
54 86c3caaf 2018-03-09 stsp
55 86c3caaf 2018-03-09 stsp static int
56 91c986ef 2018-03-09 stsp remove_got_dir(const char *worktree_path)
57 91c986ef 2018-03-09 stsp {
58 91c986ef 2018-03-09 stsp char *path;
59 91c986ef 2018-03-09 stsp
60 91c986ef 2018-03-09 stsp if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
61 91c986ef 2018-03-09 stsp return 0;
62 91c986ef 2018-03-09 stsp rmdir(path);
63 91c986ef 2018-03-09 stsp free(path);
64 91c986ef 2018-03-09 stsp return 1;
65 91c986ef 2018-03-09 stsp }
66 91c986ef 2018-03-09 stsp
67 91c986ef 2018-03-09 stsp static int
68 91c986ef 2018-03-09 stsp remove_meta_file(const char *worktree_path, const char *name)
69 91c986ef 2018-03-09 stsp {
70 91c986ef 2018-03-09 stsp char *path;
71 91c986ef 2018-03-09 stsp
72 91c986ef 2018-03-09 stsp if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
73 91c986ef 2018-03-09 stsp name) == -1)
74 91c986ef 2018-03-09 stsp return 0;
75 91c986ef 2018-03-09 stsp unlink(path);
76 91c986ef 2018-03-09 stsp free(path);
77 91c986ef 2018-03-09 stsp return 1;
78 91c986ef 2018-03-09 stsp }
79 91c986ef 2018-03-09 stsp
80 91c986ef 2018-03-09 stsp static void
81 91c986ef 2018-03-09 stsp remove_workdir(const char *worktree_path)
82 91c986ef 2018-03-09 stsp {
83 91c986ef 2018-03-09 stsp remove_meta_file(worktree_path, GOT_REF_HEAD);
84 91c986ef 2018-03-09 stsp remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX);
85 91c986ef 2018-03-09 stsp remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY);
86 1451e70d 2018-03-10 stsp remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT);
87 91c986ef 2018-03-09 stsp remove_got_dir(worktree_path);
88 91c986ef 2018-03-09 stsp rmdir(worktree_path);
89 91c986ef 2018-03-09 stsp }
90 91c986ef 2018-03-09 stsp
91 91c986ef 2018-03-09 stsp static int
92 86c3caaf 2018-03-09 stsp check_meta_file_exists(const char *worktree_path, const char *name)
93 86c3caaf 2018-03-09 stsp {
94 86c3caaf 2018-03-09 stsp FILE *f;
95 86c3caaf 2018-03-09 stsp char *path;
96 86c3caaf 2018-03-09 stsp
97 86c3caaf 2018-03-09 stsp if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
98 86c3caaf 2018-03-09 stsp name) == -1)
99 86c3caaf 2018-03-09 stsp return 0;
100 86c3caaf 2018-03-09 stsp f = fopen(path, "r");
101 0da17012 2018-03-09 stsp free(path);
102 86c3caaf 2018-03-09 stsp if (f == NULL)
103 86c3caaf 2018-03-09 stsp return 0;
104 86c3caaf 2018-03-09 stsp fclose(f);
105 86c3caaf 2018-03-09 stsp return 1;
106 86c3caaf 2018-03-09 stsp }
107 86c3caaf 2018-03-09 stsp
108 86c3caaf 2018-03-09 stsp static int
109 86c3caaf 2018-03-09 stsp worktree_init(const char *repo_path)
110 86c3caaf 2018-03-09 stsp {
111 86c3caaf 2018-03-09 stsp const struct got_error *err;
112 86c3caaf 2018-03-09 stsp struct got_repository *repo = NULL;
113 86c3caaf 2018-03-09 stsp struct got_reference *head_ref = NULL;
114 86c3caaf 2018-03-09 stsp char worktree_path[PATH_MAX];
115 86c3caaf 2018-03-09 stsp int ok = 0;
116 86c3caaf 2018-03-09 stsp
117 86c3caaf 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
118 86c3caaf 2018-03-09 stsp if (err != NULL || repo == NULL)
119 86c3caaf 2018-03-09 stsp goto done;
120 86c3caaf 2018-03-09 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
121 86c3caaf 2018-03-09 stsp if (err != NULL || head_ref == NULL)
122 86c3caaf 2018-03-09 stsp goto done;
123 86c3caaf 2018-03-09 stsp
124 86c3caaf 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
125 86c3caaf 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
126 86c3caaf 2018-03-09 stsp goto done;
127 86c3caaf 2018-03-09 stsp
128 86c3caaf 2018-03-09 stsp err = got_worktree_init(worktree_path, head_ref, repo);
129 86c3caaf 2018-03-09 stsp if (err != NULL)
130 86c3caaf 2018-03-09 stsp goto done;
131 86c3caaf 2018-03-09 stsp
132 86c3caaf 2018-03-09 stsp /* Ensure required files were created. */
133 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_REF_HEAD))
134 86c3caaf 2018-03-09 stsp goto done;
135 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
136 86c3caaf 2018-03-09 stsp goto done;
137 86c3caaf 2018-03-09 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
138 86c3caaf 2018-03-09 stsp goto done;
139 1451e70d 2018-03-10 stsp if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
140 1451e70d 2018-03-10 stsp goto done;
141 86c3caaf 2018-03-09 stsp ok = 1;
142 91c986ef 2018-03-09 stsp remove_workdir(worktree_path);
143 86c3caaf 2018-03-09 stsp done:
144 86c3caaf 2018-03-09 stsp if (head_ref)
145 86c3caaf 2018-03-09 stsp got_ref_close(head_ref);
146 86c3caaf 2018-03-09 stsp if (repo)
147 86c3caaf 2018-03-09 stsp got_repo_close(repo);
148 86c3caaf 2018-03-09 stsp return ok;
149 86c3caaf 2018-03-09 stsp }
150 86c3caaf 2018-03-09 stsp
151 0da17012 2018-03-09 stsp static int
152 0da17012 2018-03-09 stsp obstruct_meta_file(char **path, const char *worktree_path, const char *name)
153 0da17012 2018-03-09 stsp {
154 0da17012 2018-03-09 stsp FILE *f;
155 0da17012 2018-03-09 stsp char *s = "This file should not be here\n";
156 0da17012 2018-03-09 stsp
157 0da17012 2018-03-09 stsp if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
158 0da17012 2018-03-09 stsp name) == -1)
159 0da17012 2018-03-09 stsp return 0;
160 0da17012 2018-03-09 stsp f = fopen(*path, "w+");
161 0da17012 2018-03-09 stsp if (f == NULL) {
162 0da17012 2018-03-09 stsp free(*path);
163 0da17012 2018-03-09 stsp return 0;
164 0da17012 2018-03-09 stsp }
165 0da17012 2018-03-09 stsp if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
166 0da17012 2018-03-09 stsp free(*path);
167 0da17012 2018-03-09 stsp return 0;
168 0da17012 2018-03-09 stsp }
169 0da17012 2018-03-09 stsp fclose(f);
170 0da17012 2018-03-09 stsp return 1;
171 0da17012 2018-03-09 stsp }
172 0da17012 2018-03-09 stsp
173 0da17012 2018-03-09 stsp static int
174 0da17012 2018-03-09 stsp worktree_init_exists(const char *repo_path)
175 0da17012 2018-03-09 stsp {
176 0da17012 2018-03-09 stsp const struct got_error *err;
177 0da17012 2018-03-09 stsp struct got_repository *repo = NULL;
178 0da17012 2018-03-09 stsp struct got_reference *head_ref = NULL;
179 0da17012 2018-03-09 stsp char worktree_path[PATH_MAX];
180 91c986ef 2018-03-09 stsp char *gotpath = NULL;
181 0da17012 2018-03-09 stsp char *path;
182 0da17012 2018-03-09 stsp int ok = 0;
183 0da17012 2018-03-09 stsp FILE *f;
184 0da17012 2018-03-09 stsp
185 0da17012 2018-03-09 stsp err = got_repo_open(&repo, repo_path);
186 0da17012 2018-03-09 stsp if (err != NULL || repo == NULL)
187 0da17012 2018-03-09 stsp goto done;
188 0da17012 2018-03-09 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
189 0da17012 2018-03-09 stsp if (err != NULL || head_ref == NULL)
190 0da17012 2018-03-09 stsp goto done;
191 0da17012 2018-03-09 stsp
192 0da17012 2018-03-09 stsp strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
193 0da17012 2018-03-09 stsp if (mkdtemp(worktree_path) == NULL)
194 0da17012 2018-03-09 stsp goto done;
195 91c986ef 2018-03-09 stsp if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
196 91c986ef 2018-03-09 stsp goto done;
197 0da17012 2018-03-09 stsp
198 0da17012 2018-03-09 stsp if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
199 0da17012 2018-03-09 stsp == -1)
200 0da17012 2018-03-09 stsp goto done;
201 0da17012 2018-03-09 stsp if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
202 0da17012 2018-03-09 stsp goto done;
203 0da17012 2018-03-09 stsp
204 0da17012 2018-03-09 stsp /* Create files which got_worktree_init() will try to create as well. */
205 0da17012 2018-03-09 stsp
206 0da17012 2018-03-09 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_REF_HEAD))
207 0da17012 2018-03-09 stsp goto done;
208 0da17012 2018-03-09 stsp err = got_worktree_init(worktree_path, head_ref, repo);
209 0da17012 2018-03-09 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST)
210 0da17012 2018-03-09 stsp ok++;
211 0da17012 2018-03-09 stsp unlink(path);
212 0da17012 2018-03-09 stsp free(path);
213 0da17012 2018-03-09 stsp
214 0da17012 2018-03-09 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
215 0da17012 2018-03-09 stsp goto done;
216 0da17012 2018-03-09 stsp err = got_worktree_init(worktree_path, head_ref, repo);
217 0da17012 2018-03-09 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST)
218 0da17012 2018-03-09 stsp ok++;
219 0da17012 2018-03-09 stsp unlink(path);
220 0da17012 2018-03-09 stsp free(path);
221 0da17012 2018-03-09 stsp
222 0da17012 2018-03-09 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_REPOSITORY))
223 0da17012 2018-03-09 stsp goto done;
224 0da17012 2018-03-09 stsp err = got_worktree_init(worktree_path, head_ref, repo);
225 0da17012 2018-03-09 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST)
226 0da17012 2018-03-09 stsp ok++;
227 0da17012 2018-03-09 stsp unlink(path);
228 0da17012 2018-03-09 stsp free(path);
229 0da17012 2018-03-09 stsp
230 1451e70d 2018-03-10 stsp if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FORMAT))
231 1451e70d 2018-03-10 stsp goto done;
232 1451e70d 2018-03-10 stsp err = got_worktree_init(worktree_path, head_ref, repo);
233 1451e70d 2018-03-10 stsp if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST)
234 1451e70d 2018-03-10 stsp ok++;
235 1451e70d 2018-03-10 stsp unlink(path);
236 1451e70d 2018-03-10 stsp free(path);
237 1451e70d 2018-03-10 stsp
238 0da17012 2018-03-09 stsp done:
239 0da17012 2018-03-09 stsp if (head_ref)
240 0da17012 2018-03-09 stsp got_ref_close(head_ref);
241 0da17012 2018-03-09 stsp if (repo)
242 0da17012 2018-03-09 stsp got_repo_close(repo);
243 91c986ef 2018-03-09 stsp free(gotpath);
244 1451e70d 2018-03-10 stsp if (ok == 4)
245 91c986ef 2018-03-09 stsp remove_workdir(worktree_path);
246 1451e70d 2018-03-10 stsp return (ok == 4);
247 0da17012 2018-03-09 stsp }
248 0da17012 2018-03-09 stsp
249 86c3caaf 2018-03-09 stsp #define RUN_TEST(expr, name) \
250 86c3caaf 2018-03-09 stsp { test_ok = (expr); \
251 86c3caaf 2018-03-09 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
252 86c3caaf 2018-03-09 stsp failure = (failure || !test_ok); }
253 86c3caaf 2018-03-09 stsp
254 86c3caaf 2018-03-09 stsp
255 86c3caaf 2018-03-09 stsp void
256 86c3caaf 2018-03-09 stsp usage(void)
257 86c3caaf 2018-03-09 stsp {
258 86c3caaf 2018-03-09 stsp fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
259 86c3caaf 2018-03-09 stsp }
260 86c3caaf 2018-03-09 stsp
261 86c3caaf 2018-03-09 stsp int
262 86c3caaf 2018-03-09 stsp main(int argc, char *argv[])
263 86c3caaf 2018-03-09 stsp {
264 86c3caaf 2018-03-09 stsp int test_ok = 0, failure = 0;
265 86c3caaf 2018-03-09 stsp const char *repo_path;
266 86c3caaf 2018-03-09 stsp int ch;
267 86c3caaf 2018-03-09 stsp int vflag = 0;
268 86c3caaf 2018-03-09 stsp
269 86c3caaf 2018-03-09 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
270 86c3caaf 2018-03-09 stsp switch (ch) {
271 86c3caaf 2018-03-09 stsp case 'v':
272 86c3caaf 2018-03-09 stsp verbose = 1;
273 86c3caaf 2018-03-09 stsp break;
274 86c3caaf 2018-03-09 stsp default:
275 86c3caaf 2018-03-09 stsp usage();
276 86c3caaf 2018-03-09 stsp return 1;
277 86c3caaf 2018-03-09 stsp }
278 86c3caaf 2018-03-09 stsp }
279 86c3caaf 2018-03-09 stsp argc -= optind;
280 86c3caaf 2018-03-09 stsp argv += optind;
281 86c3caaf 2018-03-09 stsp
282 86c3caaf 2018-03-09 stsp if (argc == 0)
283 86c3caaf 2018-03-09 stsp repo_path = GOT_REPO_PATH;
284 86c3caaf 2018-03-09 stsp else if (argc == 1)
285 86c3caaf 2018-03-09 stsp repo_path = argv[0];
286 86c3caaf 2018-03-09 stsp else {
287 86c3caaf 2018-03-09 stsp usage();
288 86c3caaf 2018-03-09 stsp return 1;
289 86c3caaf 2018-03-09 stsp }
290 86c3caaf 2018-03-09 stsp
291 86c3caaf 2018-03-09 stsp RUN_TEST(worktree_init(repo_path), "init");
292 0da17012 2018-03-09 stsp RUN_TEST(worktree_init_exists(repo_path), "init exists");
293 86c3caaf 2018-03-09 stsp
294 86c3caaf 2018-03-09 stsp return failure ? 1 : 0;
295 86c3caaf 2018-03-09 stsp }