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/stat.h>
18 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 86c3caaf 2018-03-09 stsp
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <sha1.h>
31 9d31a1d8 2018-03-11 stsp #include <zlib.h>
32 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
33 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 86c3caaf 2018-03-09 stsp
35 86c3caaf 2018-03-09 stsp #include "got_error.h"
36 86c3caaf 2018-03-09 stsp #include "got_repository.h"
37 5261c201 2018-04-01 stsp #include "got_reference.h"
38 9d31a1d8 2018-03-11 stsp #include "got_object.h"
39 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
40 511a516b 2018-05-19 stsp #include "got_opentemp.h"
41 86c3caaf 2018-03-09 stsp
42 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
46 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
49 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
50 9d31a1d8 2018-03-11 stsp
51 9d31a1d8 2018-03-11 stsp #ifndef MIN
52 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 9d31a1d8 2018-03-11 stsp #endif
54 86c3caaf 2018-03-09 stsp
55 99724ed4 2018-03-10 stsp static const struct got_error *
56 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
57 99724ed4 2018-03-10 stsp {
58 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
59 99724ed4 2018-03-10 stsp char *path;
60 99724ed4 2018-03-10 stsp int fd = -1;
61 99724ed4 2018-03-10 stsp
62 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
63 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
64 99724ed4 2018-03-10 stsp path = NULL;
65 99724ed4 2018-03-10 stsp goto done;
66 99724ed4 2018-03-10 stsp }
67 99724ed4 2018-03-10 stsp
68 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
69 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
70 99724ed4 2018-03-10 stsp if (fd == -1) {
71 99724ed4 2018-03-10 stsp err = got_error_from_errno();
72 99724ed4 2018-03-10 stsp goto done;
73 99724ed4 2018-03-10 stsp }
74 99724ed4 2018-03-10 stsp
75 99724ed4 2018-03-10 stsp if (content) {
76 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
77 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
78 99724ed4 2018-03-10 stsp err = got_error_from_errno();
79 99724ed4 2018-03-10 stsp goto done;
80 99724ed4 2018-03-10 stsp }
81 99724ed4 2018-03-10 stsp }
82 99724ed4 2018-03-10 stsp
83 99724ed4 2018-03-10 stsp done:
84 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
85 99724ed4 2018-03-10 stsp err = got_error_from_errno();
86 99724ed4 2018-03-10 stsp free(path);
87 507dc3bb 2018-12-29 stsp return err;
88 507dc3bb 2018-12-29 stsp }
89 507dc3bb 2018-12-29 stsp
90 507dc3bb 2018-12-29 stsp static const struct got_error *
91 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
92 507dc3bb 2018-12-29 stsp {
93 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
94 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
95 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
96 507dc3bb 2018-12-29 stsp char *path = NULL;
97 507dc3bb 2018-12-29 stsp
98 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
99 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
100 507dc3bb 2018-12-29 stsp path = NULL;
101 507dc3bb 2018-12-29 stsp goto done;
102 507dc3bb 2018-12-29 stsp }
103 507dc3bb 2018-12-29 stsp
104 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
105 507dc3bb 2018-12-29 stsp if (err)
106 507dc3bb 2018-12-29 stsp goto done;
107 507dc3bb 2018-12-29 stsp
108 507dc3bb 2018-12-29 stsp if (content) {
109 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
110 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
111 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
112 507dc3bb 2018-12-29 stsp goto done;
113 507dc3bb 2018-12-29 stsp }
114 507dc3bb 2018-12-29 stsp }
115 507dc3bb 2018-12-29 stsp
116 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
117 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
118 507dc3bb 2018-12-29 stsp goto done;
119 507dc3bb 2018-12-29 stsp }
120 507dc3bb 2018-12-29 stsp
121 507dc3bb 2018-12-29 stsp done:
122 507dc3bb 2018-12-29 stsp free(tmppath);
123 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
124 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
125 99724ed4 2018-03-10 stsp return err;
126 99724ed4 2018-03-10 stsp }
127 99724ed4 2018-03-10 stsp
128 09fe317a 2018-03-11 stsp static const struct got_error *
129 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
130 09fe317a 2018-03-11 stsp {
131 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
132 09fe317a 2018-03-11 stsp char *path;
133 09fe317a 2018-03-11 stsp int fd = -1;
134 09fe317a 2018-03-11 stsp ssize_t n;
135 09fe317a 2018-03-11 stsp struct stat sb;
136 09fe317a 2018-03-11 stsp
137 09fe317a 2018-03-11 stsp *content = NULL;
138 09fe317a 2018-03-11 stsp
139 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
140 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
141 09fe317a 2018-03-11 stsp path = NULL;
142 09fe317a 2018-03-11 stsp goto done;
143 09fe317a 2018-03-11 stsp }
144 09fe317a 2018-03-11 stsp
145 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
146 09fe317a 2018-03-11 stsp if (fd == -1) {
147 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
148 ef99fdb1 2018-03-11 stsp goto done;
149 ef99fdb1 2018-03-11 stsp }
150 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
151 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
152 73a5ef67 2018-03-11 stsp : got_error_from_errno());
153 09fe317a 2018-03-11 stsp goto done;
154 09fe317a 2018-03-11 stsp }
155 09fe317a 2018-03-11 stsp
156 09fe317a 2018-03-11 stsp stat(path, &sb);
157 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
158 09fe317a 2018-03-11 stsp if (*content == NULL) {
159 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
160 09fe317a 2018-03-11 stsp goto done;
161 09fe317a 2018-03-11 stsp }
162 09fe317a 2018-03-11 stsp
163 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
164 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
165 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
166 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
167 09fe317a 2018-03-11 stsp goto done;
168 09fe317a 2018-03-11 stsp }
169 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
170 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
171 09fe317a 2018-03-11 stsp goto done;
172 09fe317a 2018-03-11 stsp }
173 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
174 09fe317a 2018-03-11 stsp
175 09fe317a 2018-03-11 stsp done:
176 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
177 09fe317a 2018-03-11 stsp err = got_error_from_errno();
178 09fe317a 2018-03-11 stsp free(path);
179 09fe317a 2018-03-11 stsp if (err) {
180 09fe317a 2018-03-11 stsp free(*content);
181 09fe317a 2018-03-11 stsp *content = NULL;
182 09fe317a 2018-03-11 stsp }
183 09fe317a 2018-03-11 stsp return err;
184 09fe317a 2018-03-11 stsp }
185 09fe317a 2018-03-11 stsp
186 86c3caaf 2018-03-09 stsp const struct got_error *
187 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
188 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
189 86c3caaf 2018-03-09 stsp {
190 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
191 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
192 65596e15 2018-12-24 stsp int obj_type;
193 7ac97322 2018-03-11 stsp char *path_got = NULL;
194 08d425ea 2018-12-24 stsp char *refstr = NULL;
195 1451e70d 2018-03-10 stsp char *formatstr = NULL;
196 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
197 65596e15 2018-12-24 stsp char *basestr = NULL;
198 65596e15 2018-12-24 stsp
199 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
200 65596e15 2018-12-24 stsp if (err)
201 65596e15 2018-12-24 stsp return err;
202 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
203 65596e15 2018-12-24 stsp if (err)
204 65596e15 2018-12-24 stsp return err;
205 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
206 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
207 86c3caaf 2018-03-09 stsp
208 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
209 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
210 0a585a0d 2018-03-17 stsp return got_error_from_errno();
211 0bb8a95e 2018-03-12 stsp }
212 577ec78f 2018-03-11 stsp
213 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
214 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
215 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
216 86c3caaf 2018-03-09 stsp goto done;
217 86c3caaf 2018-03-09 stsp }
218 86c3caaf 2018-03-09 stsp
219 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
220 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
221 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
222 86c3caaf 2018-03-09 stsp goto done;
223 86c3caaf 2018-03-09 stsp }
224 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
225 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
226 86c3caaf 2018-03-09 stsp goto done;
227 86c3caaf 2018-03-09 stsp }
228 86c3caaf 2018-03-09 stsp
229 056e7441 2018-03-11 stsp /* Create an empty lock file. */
230 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
231 056e7441 2018-03-11 stsp if (err)
232 056e7441 2018-03-11 stsp goto done;
233 056e7441 2018-03-11 stsp
234 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
235 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
236 99724ed4 2018-03-10 stsp if (err)
237 86c3caaf 2018-03-09 stsp goto done;
238 86c3caaf 2018-03-09 stsp
239 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
240 08d425ea 2018-12-24 stsp refstr = got_ref_to_str(head_ref);
241 08d425ea 2018-12-24 stsp if (refstr == NULL) {
242 a1a7858a 2018-12-24 stsp err = got_error_from_errno();
243 a1a7858a 2018-12-24 stsp goto done;
244 a1a7858a 2018-12-24 stsp }
245 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
246 65596e15 2018-12-24 stsp if (err)
247 65596e15 2018-12-24 stsp goto done;
248 65596e15 2018-12-24 stsp
249 65596e15 2018-12-24 stsp /* Record our base commit. */
250 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
251 65596e15 2018-12-24 stsp if (err)
252 65596e15 2018-12-24 stsp goto done;
253 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
254 99724ed4 2018-03-10 stsp if (err)
255 86c3caaf 2018-03-09 stsp goto done;
256 86c3caaf 2018-03-09 stsp
257 1451e70d 2018-03-10 stsp /* Store path to repository. */
258 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
259 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
260 99724ed4 2018-03-10 stsp if (err)
261 86c3caaf 2018-03-09 stsp goto done;
262 86c3caaf 2018-03-09 stsp
263 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
264 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
265 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
266 577ec78f 2018-03-11 stsp if (err)
267 577ec78f 2018-03-11 stsp goto done;
268 577ec78f 2018-03-11 stsp
269 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
270 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
271 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
272 1451e70d 2018-03-10 stsp goto done;
273 1451e70d 2018-03-10 stsp }
274 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
275 99724ed4 2018-03-10 stsp if (err)
276 1451e70d 2018-03-10 stsp goto done;
277 1451e70d 2018-03-10 stsp
278 86c3caaf 2018-03-09 stsp done:
279 65596e15 2018-12-24 stsp free(commit_id);
280 7ac97322 2018-03-11 stsp free(path_got);
281 1451e70d 2018-03-10 stsp free(formatstr);
282 08d425ea 2018-12-24 stsp free(refstr);
283 0bb8a95e 2018-03-12 stsp free(absprefix);
284 65596e15 2018-12-24 stsp free(basestr);
285 86c3caaf 2018-03-09 stsp return err;
286 86c3caaf 2018-03-09 stsp }
287 86c3caaf 2018-03-09 stsp
288 247140b2 2019-02-05 stsp static const struct got_error *
289 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
290 86c3caaf 2018-03-09 stsp {
291 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
292 7ac97322 2018-03-11 stsp char *path_got;
293 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
294 056e7441 2018-03-11 stsp char *path_lock = NULL;
295 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
296 271d2a38 2018-12-25 stsp char *head_ref_str = NULL;
297 6d9d28c3 2018-03-11 stsp int version, fd = -1;
298 6d9d28c3 2018-03-11 stsp const char *errstr;
299 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
300 6d9d28c3 2018-03-11 stsp
301 6d9d28c3 2018-03-11 stsp *worktree = NULL;
302 6d9d28c3 2018-03-11 stsp
303 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
304 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
305 7ac97322 2018-03-11 stsp path_got = NULL;
306 6d9d28c3 2018-03-11 stsp goto done;
307 6d9d28c3 2018-03-11 stsp }
308 6d9d28c3 2018-03-11 stsp
309 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
310 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
311 056e7441 2018-03-11 stsp path_lock = NULL;
312 6d9d28c3 2018-03-11 stsp goto done;
313 6d9d28c3 2018-03-11 stsp }
314 6d9d28c3 2018-03-11 stsp
315 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
316 6d9d28c3 2018-03-11 stsp if (fd == -1) {
317 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
318 73a5ef67 2018-03-11 stsp : got_error_from_errno());
319 6d9d28c3 2018-03-11 stsp goto done;
320 6d9d28c3 2018-03-11 stsp }
321 6d9d28c3 2018-03-11 stsp
322 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
323 6d9d28c3 2018-03-11 stsp if (err)
324 6d9d28c3 2018-03-11 stsp goto done;
325 6d9d28c3 2018-03-11 stsp
326 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
327 6d9d28c3 2018-03-11 stsp if (errstr) {
328 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
329 6d9d28c3 2018-03-11 stsp goto done;
330 6d9d28c3 2018-03-11 stsp }
331 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
332 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
333 6d9d28c3 2018-03-11 stsp goto done;
334 6d9d28c3 2018-03-11 stsp }
335 6d9d28c3 2018-03-11 stsp
336 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
337 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
338 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
339 6d9d28c3 2018-03-11 stsp goto done;
340 6d9d28c3 2018-03-11 stsp }
341 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
342 6d9d28c3 2018-03-11 stsp
343 c88eb298 2018-03-11 stsp (*worktree)->root_path = strdup(path);
344 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
345 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
346 6d9d28c3 2018-03-11 stsp goto done;
347 6d9d28c3 2018-03-11 stsp }
348 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
349 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
350 6d9d28c3 2018-03-11 stsp if (err)
351 6d9d28c3 2018-03-11 stsp goto done;
352 eaccb85f 2018-12-25 stsp
353 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
354 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
355 93a30277 2018-12-24 stsp if (err)
356 93a30277 2018-12-24 stsp goto done;
357 93a30277 2018-12-24 stsp
358 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
359 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
360 eaccb85f 2018-12-25 stsp if (err)
361 eaccb85f 2018-12-25 stsp goto done;
362 eaccb85f 2018-12-25 stsp
363 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
364 eaccb85f 2018-12-25 stsp if (err)
365 eaccb85f 2018-12-25 stsp goto done;
366 eaccb85f 2018-12-25 stsp
367 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
368 eaccb85f 2018-12-25 stsp base_commit_id_str);
369 f5baf295 2018-03-11 stsp if (err)
370 f5baf295 2018-03-11 stsp goto done;
371 f5baf295 2018-03-11 stsp
372 271d2a38 2018-12-25 stsp err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
373 f5baf295 2018-03-11 stsp if (err)
374 6d9d28c3 2018-03-11 stsp goto done;
375 6d9d28c3 2018-03-11 stsp
376 271d2a38 2018-12-25 stsp err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
377 6d9d28c3 2018-03-11 stsp done:
378 eaccb85f 2018-12-25 stsp if (repo)
379 eaccb85f 2018-12-25 stsp got_repo_close(repo);
380 7ac97322 2018-03-11 stsp free(path_got);
381 056e7441 2018-03-11 stsp free(path_lock);
382 271d2a38 2018-12-25 stsp free(head_ref_str);
383 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
384 6d9d28c3 2018-03-11 stsp if (err) {
385 6d9d28c3 2018-03-11 stsp if (fd != -1)
386 6d9d28c3 2018-03-11 stsp close(fd);
387 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
388 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
389 6d9d28c3 2018-03-11 stsp *worktree = NULL;
390 6d9d28c3 2018-03-11 stsp } else
391 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
392 6d9d28c3 2018-03-11 stsp
393 6d9d28c3 2018-03-11 stsp return err;
394 86c3caaf 2018-03-09 stsp }
395 247140b2 2019-02-05 stsp
396 247140b2 2019-02-05 stsp const struct got_error *
397 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
398 247140b2 2019-02-05 stsp {
399 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
400 86c3caaf 2018-03-09 stsp
401 247140b2 2019-02-05 stsp do {
402 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
403 247140b2 2019-02-05 stsp if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
404 247140b2 2019-02-05 stsp return err;
405 247140b2 2019-02-05 stsp if (*worktree)
406 247140b2 2019-02-05 stsp return NULL;
407 247140b2 2019-02-05 stsp path = dirname(path);
408 d1542a27 2019-02-05 stsp if (path == NULL)
409 d1542a27 2019-02-05 stsp return got_error_from_errno();
410 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
411 247140b2 2019-02-05 stsp
412 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
413 247140b2 2019-02-05 stsp }
414 247140b2 2019-02-05 stsp
415 3a6ce05a 2019-02-11 stsp const struct got_error *
416 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
417 86c3caaf 2018-03-09 stsp {
418 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
419 c88eb298 2018-03-11 stsp free(worktree->root_path);
420 cde76477 2018-03-11 stsp free(worktree->repo_path);
421 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
422 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
423 271d2a38 2018-12-25 stsp if (worktree->head_ref)
424 271d2a38 2018-12-25 stsp got_ref_close(worktree->head_ref);
425 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
426 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
427 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
428 6d9d28c3 2018-03-11 stsp free(worktree);
429 3a6ce05a 2019-02-11 stsp return err;
430 86c3caaf 2018-03-09 stsp }
431 86c3caaf 2018-03-09 stsp
432 2fbdb5ae 2018-12-29 stsp const char *
433 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
434 c7f4312f 2019-02-05 stsp {
435 c7f4312f 2019-02-05 stsp return worktree->root_path;
436 c7f4312f 2019-02-05 stsp }
437 c7f4312f 2019-02-05 stsp
438 c7f4312f 2019-02-05 stsp const char *
439 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
440 86c3caaf 2018-03-09 stsp {
441 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
442 49520a32 2018-12-29 stsp }
443 49520a32 2018-12-29 stsp
444 49520a32 2018-12-29 stsp const char *
445 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
446 49520a32 2018-12-29 stsp {
447 f609be2e 2018-12-29 stsp return worktree->path_prefix;
448 e5dc7198 2018-12-29 stsp }
449 e5dc7198 2018-12-29 stsp
450 e5dc7198 2018-12-29 stsp const struct got_error *
451 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
452 e5dc7198 2018-12-29 stsp const char *path_prefix)
453 e5dc7198 2018-12-29 stsp {
454 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
455 e5dc7198 2018-12-29 stsp
456 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
457 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
458 e5dc7198 2018-12-29 stsp return got_error_from_errno();
459 e5dc7198 2018-12-29 stsp }
460 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
461 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
462 e5dc7198 2018-12-29 stsp free(absprefix);
463 e5dc7198 2018-12-29 stsp return NULL;
464 86c3caaf 2018-03-09 stsp }
465 86c3caaf 2018-03-09 stsp
466 35be1456 2018-03-11 stsp char *
467 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
468 86c3caaf 2018-03-09 stsp {
469 271d2a38 2018-12-25 stsp return got_ref_to_str(worktree->head_ref);
470 be7061eb 2018-12-30 stsp }
471 be7061eb 2018-12-30 stsp
472 be7061eb 2018-12-30 stsp struct got_reference *
473 be7061eb 2018-12-30 stsp got_worktree_get_head_ref(struct got_worktree *worktree)
474 be7061eb 2018-12-30 stsp {
475 be7061eb 2018-12-30 stsp return got_ref_dup(worktree->head_ref);
476 507dc3bb 2018-12-29 stsp }
477 507dc3bb 2018-12-29 stsp
478 b72f483a 2019-02-05 stsp struct got_object_id *
479 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
480 507dc3bb 2018-12-29 stsp {
481 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
482 86c3caaf 2018-03-09 stsp }
483 86c3caaf 2018-03-09 stsp
484 507dc3bb 2018-12-29 stsp const struct got_error *
485 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
486 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
487 507dc3bb 2018-12-29 stsp {
488 507dc3bb 2018-12-29 stsp const struct got_error *err;
489 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
490 507dc3bb 2018-12-29 stsp char *id_str = NULL;
491 507dc3bb 2018-12-29 stsp char *path_got = NULL;
492 507dc3bb 2018-12-29 stsp
493 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
494 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
495 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
496 507dc3bb 2018-12-29 stsp path_got = NULL;
497 507dc3bb 2018-12-29 stsp goto done;
498 507dc3bb 2018-12-29 stsp }
499 507dc3bb 2018-12-29 stsp
500 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
501 507dc3bb 2018-12-29 stsp if (err)
502 507dc3bb 2018-12-29 stsp return err;
503 507dc3bb 2018-12-29 stsp
504 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
505 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
506 507dc3bb 2018-12-29 stsp goto done;
507 507dc3bb 2018-12-29 stsp }
508 507dc3bb 2018-12-29 stsp
509 507dc3bb 2018-12-29 stsp /* Record our base commit. */
510 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
511 507dc3bb 2018-12-29 stsp if (err)
512 507dc3bb 2018-12-29 stsp goto done;
513 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
514 507dc3bb 2018-12-29 stsp if (err)
515 507dc3bb 2018-12-29 stsp goto done;
516 507dc3bb 2018-12-29 stsp
517 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
518 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
519 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
520 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
521 507dc3bb 2018-12-29 stsp goto done;
522 507dc3bb 2018-12-29 stsp }
523 507dc3bb 2018-12-29 stsp done:
524 507dc3bb 2018-12-29 stsp if (obj)
525 507dc3bb 2018-12-29 stsp got_object_close(obj);
526 507dc3bb 2018-12-29 stsp free(id_str);
527 507dc3bb 2018-12-29 stsp free(path_got);
528 507dc3bb 2018-12-29 stsp return err;
529 507dc3bb 2018-12-29 stsp }
530 507dc3bb 2018-12-29 stsp
531 9d31a1d8 2018-03-11 stsp static const struct got_error *
532 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
533 86c3caaf 2018-03-09 stsp {
534 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
535 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
536 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
537 86c3caaf 2018-03-09 stsp return NULL;
538 9d31a1d8 2018-03-11 stsp }
539 9d31a1d8 2018-03-11 stsp
540 9d31a1d8 2018-03-11 stsp static const struct got_error *
541 21908da4 2019-01-13 stsp make_parent_dirs(const char *abspath)
542 21908da4 2019-01-13 stsp {
543 21908da4 2019-01-13 stsp const struct got_error *err = NULL;
544 21908da4 2019-01-13 stsp
545 21908da4 2019-01-13 stsp char *parent = dirname(abspath);
546 21908da4 2019-01-13 stsp if (parent == NULL)
547 21908da4 2019-01-13 stsp return NULL;
548 21908da4 2019-01-13 stsp
549 21908da4 2019-01-13 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
550 21908da4 2019-01-13 stsp if (errno == ENOENT) {
551 21908da4 2019-01-13 stsp err = make_parent_dirs(parent);
552 21908da4 2019-01-13 stsp if (err)
553 21908da4 2019-01-13 stsp return err;
554 c65fcef2 2019-01-13 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
555 c65fcef2 2019-01-13 stsp return got_error_from_errno();
556 21908da4 2019-01-13 stsp } else
557 c65fcef2 2019-01-13 stsp err = got_error_from_errno();
558 21908da4 2019-01-13 stsp }
559 21908da4 2019-01-13 stsp
560 c65fcef2 2019-01-13 stsp return err;
561 21908da4 2019-01-13 stsp }
562 21908da4 2019-01-13 stsp
563 21908da4 2019-01-13 stsp static const struct got_error *
564 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
565 4a1ddfc2 2019-01-12 stsp {
566 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
567 4a1ddfc2 2019-01-12 stsp char *abspath;
568 4a1ddfc2 2019-01-12 stsp
569 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
570 4a1ddfc2 2019-01-12 stsp return got_error_from_errno();
571 4a1ddfc2 2019-01-12 stsp
572 4a1ddfc2 2019-01-12 stsp /* XXX queue work rather than editing disk directly? */
573 4a1ddfc2 2019-01-12 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
574 4a1ddfc2 2019-01-12 stsp struct stat sb;
575 4a1ddfc2 2019-01-12 stsp
576 21908da4 2019-01-13 stsp if (errno == EEXIST) {
577 21908da4 2019-01-13 stsp if (lstat(abspath, &sb) == -1) {
578 21908da4 2019-01-13 stsp err = got_error_from_errno();
579 21908da4 2019-01-13 stsp goto done;
580 21908da4 2019-01-13 stsp }
581 21908da4 2019-01-13 stsp
582 21908da4 2019-01-13 stsp if (!S_ISDIR(sb.st_mode)) {
583 21908da4 2019-01-13 stsp /* TODO directory is obstructed; do something */
584 c65fcef2 2019-01-13 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
585 c65fcef2 2019-01-13 stsp goto done;
586 21908da4 2019-01-13 stsp }
587 21908da4 2019-01-13 stsp
588 21908da4 2019-01-13 stsp return NULL;
589 c65fcef2 2019-01-13 stsp } else if (errno == ENOENT) {
590 21908da4 2019-01-13 stsp err = make_parent_dirs(abspath);
591 21908da4 2019-01-13 stsp if (err)
592 c65fcef2 2019-01-13 stsp goto done;
593 c65fcef2 2019-01-13 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
594 c65fcef2 2019-01-13 stsp err = got_error_from_errno();
595 c65fcef2 2019-01-13 stsp } else
596 4a1ddfc2 2019-01-12 stsp err = got_error_from_errno();
597 4a1ddfc2 2019-01-12 stsp }
598 4a1ddfc2 2019-01-12 stsp
599 4a1ddfc2 2019-01-12 stsp done:
600 4a1ddfc2 2019-01-12 stsp free(abspath);
601 6353ad76 2019-02-08 stsp return err;
602 6353ad76 2019-02-08 stsp }
603 6353ad76 2019-02-08 stsp
604 6353ad76 2019-02-08 stsp /*
605 6353ad76 2019-02-08 stsp * Perform a 3-way merge where the file's version in the file index (blob2)
606 6353ad76 2019-02-08 stsp * acts as the common ancestor, the incoming blob (blob1) acts as the first
607 6353ad76 2019-02-08 stsp * derived version, and the file on disk acts as the second derived version.
608 6353ad76 2019-02-08 stsp */
609 6353ad76 2019-02-08 stsp static const struct got_error *
610 6353ad76 2019-02-08 stsp merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
611 6353ad76 2019-02-08 stsp struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
612 b8f41171 2019-02-10 stsp uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
613 b8f41171 2019-02-10 stsp struct got_repository *repo,
614 6353ad76 2019-02-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
615 6353ad76 2019-02-08 stsp {
616 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
617 6353ad76 2019-02-08 stsp int merged_fd = -1;
618 6353ad76 2019-02-08 stsp struct got_blob_object *blob2 = NULL;
619 6353ad76 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL;
620 6353ad76 2019-02-08 stsp char *blob1_path = NULL, *blob2_path = NULL;
621 6353ad76 2019-02-08 stsp char *merged_path = NULL;
622 6353ad76 2019-02-08 stsp struct got_object_id id2;
623 6353ad76 2019-02-08 stsp char *id_str = NULL;
624 6353ad76 2019-02-08 stsp char *label1 = NULL;
625 6353ad76 2019-02-08 stsp int overlapcnt = 0;
626 6353ad76 2019-02-08 stsp
627 6353ad76 2019-02-08 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, "/tmp/got-merged");
628 6353ad76 2019-02-08 stsp if (err)
629 6353ad76 2019-02-08 stsp return err;
630 6353ad76 2019-02-08 stsp err = got_opentemp_named(&blob1_path, &f1, "/tmp/got-merge-blob1");
631 6353ad76 2019-02-08 stsp if (err)
632 6353ad76 2019-02-08 stsp goto done;
633 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
634 6353ad76 2019-02-08 stsp if (err)
635 6353ad76 2019-02-08 stsp goto done;
636 6353ad76 2019-02-08 stsp
637 6353ad76 2019-02-08 stsp err = got_opentemp_named(&blob2_path, &f2, "/tmp/got-merge-blob2");
638 6353ad76 2019-02-08 stsp if (err)
639 6353ad76 2019-02-08 stsp goto done;
640 6353ad76 2019-02-08 stsp
641 6353ad76 2019-02-08 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
642 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
643 6353ad76 2019-02-08 stsp if (err)
644 6353ad76 2019-02-08 stsp goto done;
645 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
646 6353ad76 2019-02-08 stsp if (err)
647 6353ad76 2019-02-08 stsp goto done;
648 6353ad76 2019-02-08 stsp
649 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
650 6353ad76 2019-02-08 stsp if (err)
651 6353ad76 2019-02-08 stsp goto done;
652 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
653 6353ad76 2019-02-08 stsp err = got_error_from_errno();
654 6353ad76 2019-02-08 stsp goto done;
655 6353ad76 2019-02-08 stsp }
656 6353ad76 2019-02-08 stsp
657 6353ad76 2019-02-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
658 6353ad76 2019-02-08 stsp blob2_path, ondisk_path, label1, path);
659 6353ad76 2019-02-08 stsp if (err)
660 6353ad76 2019-02-08 stsp goto done;
661 6353ad76 2019-02-08 stsp
662 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
663 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
664 6353ad76 2019-02-08 stsp
665 6353ad76 2019-02-08 stsp
666 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
667 816dc654 2019-02-16 stsp err = got_error_from_errno();
668 816dc654 2019-02-16 stsp goto done;
669 816dc654 2019-02-16 stsp }
670 6353ad76 2019-02-08 stsp
671 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
672 6353ad76 2019-02-08 stsp err = got_error_from_errno();
673 6353ad76 2019-02-08 stsp goto done;
674 6353ad76 2019-02-08 stsp }
675 6353ad76 2019-02-08 stsp
676 02c07007 2019-02-10 stsp /*
677 02c07007 2019-02-10 stsp * Do not update timestamps of already modified files. Otherwise,
678 02c07007 2019-02-10 stsp * the status walk would treat them as unmodified files again.
679 02c07007 2019-02-10 stsp */
680 6353ad76 2019-02-08 stsp err = got_fileindex_entry_update(ie, ondisk_path,
681 02c07007 2019-02-10 stsp blob1->id.sha1, worktree->base_commit_id->sha1, 0);
682 6353ad76 2019-02-08 stsp done:
683 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
684 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
685 3a6ce05a 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
686 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
687 3a6ce05a 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
688 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
689 6353ad76 2019-02-08 stsp if (blob2)
690 6353ad76 2019-02-08 stsp got_object_blob_close(blob2);
691 6353ad76 2019-02-08 stsp free(merged_path);
692 6353ad76 2019-02-08 stsp if (blob1_path) {
693 6353ad76 2019-02-08 stsp unlink(blob1_path);
694 6353ad76 2019-02-08 stsp free(blob1_path);
695 6353ad76 2019-02-08 stsp }
696 6353ad76 2019-02-08 stsp if (blob2_path) {
697 6353ad76 2019-02-08 stsp unlink(blob2_path);
698 6353ad76 2019-02-08 stsp free(blob2_path);
699 6353ad76 2019-02-08 stsp }
700 6353ad76 2019-02-08 stsp free(id_str);
701 6353ad76 2019-02-08 stsp free(label1);
702 4a1ddfc2 2019-01-12 stsp return err;
703 4a1ddfc2 2019-01-12 stsp }
704 4a1ddfc2 2019-01-12 stsp
705 4a1ddfc2 2019-01-12 stsp static const struct got_error *
706 8da9e5f4 2019-01-12 stsp install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
707 6353ad76 2019-02-08 stsp struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
708 b8f41171 2019-02-10 stsp uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
709 b8f41171 2019-02-10 stsp int restoring_missing_file, struct got_repository *repo,
710 b8f41171 2019-02-10 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
711 9d31a1d8 2018-03-11 stsp {
712 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
713 507dc3bb 2018-12-29 stsp int fd = -1;
714 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
715 507dc3bb 2018-12-29 stsp int update = 0;
716 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
717 9d31a1d8 2018-03-11 stsp
718 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
719 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
720 9d31a1d8 2018-03-11 stsp if (fd == -1) {
721 21908da4 2019-01-13 stsp if (errno == ENOENT) {
722 21908da4 2019-01-13 stsp char *parent = dirname(path);
723 21908da4 2019-01-13 stsp if (parent == NULL)
724 21908da4 2019-01-13 stsp return got_error_from_errno();
725 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
726 21908da4 2019-01-13 stsp if (err)
727 21908da4 2019-01-13 stsp return err;
728 21908da4 2019-01-13 stsp fd = open(ondisk_path,
729 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
730 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
731 21908da4 2019-01-13 stsp if (fd == -1)
732 21908da4 2019-01-13 stsp return got_error_from_errno();
733 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
734 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
735 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
736 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
737 507dc3bb 2018-12-29 stsp goto done;
738 d70b8e30 2018-12-27 stsp } else {
739 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
740 507dc3bb 2018-12-29 stsp ondisk_path);
741 507dc3bb 2018-12-29 stsp if (err)
742 507dc3bb 2018-12-29 stsp goto done;
743 507dc3bb 2018-12-29 stsp update = 1;
744 9d31a1d8 2018-03-11 stsp }
745 507dc3bb 2018-12-29 stsp } else
746 7e7c1e4c 2019-01-12 stsp return got_error_from_errno();
747 9d31a1d8 2018-03-11 stsp }
748 9d31a1d8 2018-03-11 stsp
749 a378724f 2019-02-10 stsp if (restoring_missing_file)
750 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
751 a378724f 2019-02-10 stsp else
752 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
753 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
754 d7b62c98 2018-12-27 stsp
755 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
756 9d31a1d8 2018-03-11 stsp do {
757 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
758 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
759 9d31a1d8 2018-03-11 stsp if (err)
760 9d31a1d8 2018-03-11 stsp break;
761 9d31a1d8 2018-03-11 stsp if (len > 0) {
762 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
763 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
764 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
765 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
766 b87c6f83 2018-12-24 stsp goto done;
767 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
768 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
769 b87c6f83 2018-12-24 stsp goto done;
770 9d31a1d8 2018-03-11 stsp }
771 61d6eaa3 2018-12-24 stsp hdrlen = 0;
772 9d31a1d8 2018-03-11 stsp }
773 9d31a1d8 2018-03-11 stsp } while (len != 0);
774 9d31a1d8 2018-03-11 stsp
775 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
776 816dc654 2019-02-16 stsp err = got_error_from_errno();
777 816dc654 2019-02-16 stsp goto done;
778 816dc654 2019-02-16 stsp }
779 9d31a1d8 2018-03-11 stsp
780 507dc3bb 2018-12-29 stsp if (update) {
781 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
782 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
783 68ed9ba5 2019-02-10 stsp goto done;
784 68ed9ba5 2019-02-10 stsp }
785 68ed9ba5 2019-02-10 stsp }
786 ba8a0d4d 2019-02-10 stsp
787 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
788 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
789 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
790 507dc3bb 2018-12-29 stsp goto done;
791 507dc3bb 2018-12-29 stsp }
792 ba8a0d4d 2019-02-10 stsp } else {
793 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
794 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
795 68ed9ba5 2019-02-10 stsp goto done;
796 68ed9ba5 2019-02-10 stsp }
797 507dc3bb 2018-12-29 stsp }
798 507dc3bb 2018-12-29 stsp
799 5f9fef37 2019-01-13 stsp if (entry == NULL)
800 5f9fef37 2019-01-13 stsp entry = got_fileindex_entry_get(fileindex, path);
801 51514078 2018-12-25 stsp if (entry)
802 51514078 2018-12-25 stsp err = got_fileindex_entry_update(entry, ondisk_path,
803 02c07007 2019-02-10 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
804 51514078 2018-12-25 stsp else {
805 51514078 2018-12-25 stsp err = got_fileindex_entry_alloc(&entry, ondisk_path,
806 8da9e5f4 2019-01-12 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
807 51514078 2018-12-25 stsp if (err)
808 51514078 2018-12-25 stsp goto done;
809 51514078 2018-12-25 stsp err = got_fileindex_entry_add(fileindex, entry);
810 51514078 2018-12-25 stsp }
811 9d31a1d8 2018-03-11 stsp done:
812 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
813 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
814 507dc3bb 2018-12-29 stsp free(tmppath);
815 6353ad76 2019-02-08 stsp return err;
816 6353ad76 2019-02-08 stsp }
817 6353ad76 2019-02-08 stsp
818 6353ad76 2019-02-08 stsp static const struct got_error *
819 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
820 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
821 b8f41171 2019-02-10 stsp struct got_repository *repo)
822 6353ad76 2019-02-08 stsp {
823 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
824 6353ad76 2019-02-08 stsp struct got_object_id id;
825 6353ad76 2019-02-08 stsp size_t hdrlen;
826 6353ad76 2019-02-08 stsp FILE *f = NULL;
827 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
828 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
829 6353ad76 2019-02-08 stsp size_t flen, blen;
830 6353ad76 2019-02-08 stsp
831 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
832 6353ad76 2019-02-08 stsp
833 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
834 a378724f 2019-02-10 stsp if (errno == ENOENT) {
835 b8f41171 2019-02-10 stsp if (ie) {
836 b8f41171 2019-02-10 stsp *status = GOT_STATUS_MISSING;
837 b8f41171 2019-02-10 stsp sb->st_mode =
838 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
839 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
840 b8f41171 2019-02-10 stsp } else
841 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
842 a378724f 2019-02-10 stsp return NULL;
843 a378724f 2019-02-10 stsp }
844 6353ad76 2019-02-08 stsp return got_error_from_errno();
845 a378724f 2019-02-10 stsp }
846 6353ad76 2019-02-08 stsp
847 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
848 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
849 6353ad76 2019-02-08 stsp return NULL;
850 6353ad76 2019-02-08 stsp }
851 6353ad76 2019-02-08 stsp
852 b8f41171 2019-02-10 stsp if (ie == NULL)
853 b8f41171 2019-02-10 stsp return NULL;
854 b8f41171 2019-02-10 stsp
855 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
856 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
857 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
858 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
859 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
860 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
861 6353ad76 2019-02-08 stsp return NULL;
862 6353ad76 2019-02-08 stsp
863 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
864 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
865 6353ad76 2019-02-08 stsp if (err)
866 6353ad76 2019-02-08 stsp return err;
867 6353ad76 2019-02-08 stsp
868 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
869 6353ad76 2019-02-08 stsp if (f == NULL) {
870 6353ad76 2019-02-08 stsp err = got_error_from_errno();
871 6353ad76 2019-02-08 stsp goto done;
872 6353ad76 2019-02-08 stsp }
873 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
874 6353ad76 2019-02-08 stsp while (1) {
875 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
876 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
877 6353ad76 2019-02-08 stsp if (err)
878 6353ad76 2019-02-08 stsp break;
879 6353ad76 2019-02-08 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
880 6353ad76 2019-02-08 stsp if (blen == 0) {
881 6353ad76 2019-02-08 stsp if (flen != 0)
882 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
883 6353ad76 2019-02-08 stsp break;
884 6353ad76 2019-02-08 stsp } else if (flen == 0) {
885 6353ad76 2019-02-08 stsp if (blen != 0)
886 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
887 6353ad76 2019-02-08 stsp break;
888 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
889 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
890 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
891 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
892 6353ad76 2019-02-08 stsp break;
893 6353ad76 2019-02-08 stsp }
894 6353ad76 2019-02-08 stsp } else {
895 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
896 6353ad76 2019-02-08 stsp break;
897 6353ad76 2019-02-08 stsp }
898 6353ad76 2019-02-08 stsp hdrlen = 0;
899 6353ad76 2019-02-08 stsp }
900 6353ad76 2019-02-08 stsp done:
901 6353ad76 2019-02-08 stsp if (blob)
902 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
903 6353ad76 2019-02-08 stsp if (f)
904 6353ad76 2019-02-08 stsp fclose(f);
905 9d31a1d8 2018-03-11 stsp return err;
906 9d31a1d8 2018-03-11 stsp }
907 9d31a1d8 2018-03-11 stsp
908 9d31a1d8 2018-03-11 stsp static const struct got_error *
909 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
910 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
911 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
912 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
913 8da9e5f4 2019-01-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
914 9d31a1d8 2018-03-11 stsp {
915 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
916 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
917 6353ad76 2019-02-08 stsp char *ondisk_path;
918 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
919 b8f41171 2019-02-10 stsp struct stat sb;
920 9d31a1d8 2018-03-11 stsp
921 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
922 6353ad76 2019-02-08 stsp return got_error_from_errno();
923 6353ad76 2019-02-08 stsp
924 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
925 b8f41171 2019-02-10 stsp if (err)
926 b8f41171 2019-02-10 stsp goto done;
927 6353ad76 2019-02-08 stsp
928 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
929 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
930 b8f41171 2019-02-10 stsp goto done;
931 b8f41171 2019-02-10 stsp }
932 b8f41171 2019-02-10 stsp
933 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
934 b8f41171 2019-02-10 stsp if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
935 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
936 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
937 b8f41171 2019-02-10 stsp path);
938 6353ad76 2019-02-08 stsp goto done;
939 a378724f 2019-02-10 stsp }
940 b8f41171 2019-02-10 stsp if (memcmp(ie->blob_sha1,
941 b8f41171 2019-02-10 stsp te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
942 b8f41171 2019-02-10 stsp goto done;
943 9d31a1d8 2018-03-11 stsp }
944 9d31a1d8 2018-03-11 stsp
945 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
946 8da9e5f4 2019-01-12 stsp if (err)
947 6353ad76 2019-02-08 stsp goto done;
948 512f0d0e 2019-01-02 stsp
949 276262e8 2019-02-08 stsp if (status == GOT_STATUS_MODIFY)
950 6353ad76 2019-02-08 stsp err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
951 b8f41171 2019-02-10 stsp te->mode, sb.st_mode, blob, repo, progress_cb,
952 b8f41171 2019-02-10 stsp progress_arg);
953 6353ad76 2019-02-08 stsp else
954 6353ad76 2019-02-08 stsp err = install_blob(worktree, fileindex, ie, ondisk_path, path,
955 b8f41171 2019-02-10 stsp te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
956 b8f41171 2019-02-10 stsp repo, progress_cb, progress_arg);
957 6353ad76 2019-02-08 stsp
958 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
959 6353ad76 2019-02-08 stsp done:
960 6353ad76 2019-02-08 stsp free(ondisk_path);
961 8da9e5f4 2019-01-12 stsp return err;
962 90285c3b 2019-01-08 stsp }
963 90285c3b 2019-01-08 stsp
964 90285c3b 2019-01-08 stsp static const struct got_error *
965 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
966 90285c3b 2019-01-08 stsp {
967 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
968 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
969 90285c3b 2019-01-08 stsp
970 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
971 90285c3b 2019-01-08 stsp return got_error_from_errno();
972 90285c3b 2019-01-08 stsp
973 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
974 c97665ae 2019-01-13 stsp if (errno != ENOENT)
975 c97665ae 2019-01-13 stsp err = got_error_from_errno();
976 c97665ae 2019-01-13 stsp } else {
977 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
978 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
979 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
980 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
981 26c4ac4d 2019-01-08 stsp err = got_error_from_errno();
982 90285c3b 2019-01-08 stsp break;
983 90285c3b 2019-01-08 stsp }
984 90285c3b 2019-01-08 stsp parent = dirname(parent);
985 90285c3b 2019-01-08 stsp }
986 90285c3b 2019-01-08 stsp }
987 90285c3b 2019-01-08 stsp free(ondisk_path);
988 90285c3b 2019-01-08 stsp return err;
989 512f0d0e 2019-01-02 stsp }
990 512f0d0e 2019-01-02 stsp
991 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
992 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
993 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
994 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
995 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
996 8da9e5f4 2019-01-12 stsp void *progress_arg;
997 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
998 8da9e5f4 2019-01-12 stsp void *cancel_arg;
999 8da9e5f4 2019-01-12 stsp };
1000 8da9e5f4 2019-01-12 stsp
1001 512f0d0e 2019-01-02 stsp static const struct got_error *
1002 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1003 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1004 512f0d0e 2019-01-02 stsp {
1005 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1006 512f0d0e 2019-01-02 stsp
1007 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1008 8da9e5f4 2019-01-12 stsp ie->path, a->repo, a->progress_cb, a->progress_arg,
1009 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1010 8da9e5f4 2019-01-12 stsp }
1011 512f0d0e 2019-01-02 stsp
1012 8da9e5f4 2019-01-12 stsp static const struct got_error *
1013 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1014 8da9e5f4 2019-01-12 stsp {
1015 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1016 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1017 7a9df742 2019-01-08 stsp
1018 8da9e5f4 2019-01-12 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1019 7a9df742 2019-01-08 stsp
1020 8da9e5f4 2019-01-12 stsp err = remove_ondisk_file(a->worktree->root_path, ie->path);
1021 8da9e5f4 2019-01-12 stsp if (err)
1022 8da9e5f4 2019-01-12 stsp return err;
1023 8da9e5f4 2019-01-12 stsp got_fileindex_entry_remove(a->fileindex, ie);
1024 8da9e5f4 2019-01-12 stsp return NULL;
1025 9d31a1d8 2018-03-11 stsp }
1026 9d31a1d8 2018-03-11 stsp
1027 9d31a1d8 2018-03-11 stsp static const struct got_error *
1028 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1029 9d31a1d8 2018-03-11 stsp {
1030 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1031 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1032 8da9e5f4 2019-01-12 stsp char *path;
1033 9d31a1d8 2018-03-11 stsp
1034 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1035 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1036 8da9e5f4 2019-01-12 stsp == -1)
1037 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
1038 9d31a1d8 2018-03-11 stsp
1039 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1040 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1041 8da9e5f4 2019-01-12 stsp else
1042 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1043 8da9e5f4 2019-01-12 stsp a->repo, a->progress_cb, a->progress_arg,
1044 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1045 9d31a1d8 2018-03-11 stsp
1046 8da9e5f4 2019-01-12 stsp free(path);
1047 9d31a1d8 2018-03-11 stsp return err;
1048 9d31a1d8 2018-03-11 stsp }
1049 9d31a1d8 2018-03-11 stsp
1050 9d31a1d8 2018-03-11 stsp const struct got_error *
1051 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1052 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1053 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1054 9d31a1d8 2018-03-11 stsp {
1055 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1056 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1057 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1058 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1059 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
1060 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1061 51514078 2018-12-25 stsp FILE *index = NULL, *new_index = NULL;
1062 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1063 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1064 9d31a1d8 2018-03-11 stsp
1065 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1066 9d31a1d8 2018-03-11 stsp if (err)
1067 9d31a1d8 2018-03-11 stsp return err;
1068 93a30277 2018-12-24 stsp
1069 133d2798 2019-01-08 stsp fileindex = got_fileindex_alloc();
1070 133d2798 2019-01-08 stsp if (fileindex == NULL) {
1071 133d2798 2019-01-08 stsp err = got_error_from_errno();
1072 9d31a1d8 2018-03-11 stsp goto done;
1073 133d2798 2019-01-08 stsp }
1074 9d31a1d8 2018-03-11 stsp
1075 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1076 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1077 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1078 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
1079 9d31a1d8 2018-03-11 stsp goto done;
1080 9d31a1d8 2018-03-11 stsp }
1081 9d31a1d8 2018-03-11 stsp
1082 51514078 2018-12-25 stsp /*
1083 51514078 2018-12-25 stsp * Read the file index.
1084 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1085 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1086 51514078 2018-12-25 stsp */
1087 51514078 2018-12-25 stsp index = fopen(fileindex_path, "rb");
1088 51514078 2018-12-25 stsp if (index == NULL) {
1089 cf61d818 2019-01-12 stsp if (errno != ENOENT) {
1090 cf61d818 2019-01-12 stsp err = got_error_from_errno();
1091 cf61d818 2019-01-12 stsp goto done;
1092 cf61d818 2019-01-12 stsp }
1093 cf61d818 2019-01-12 stsp } else {
1094 cf61d818 2019-01-12 stsp err = got_fileindex_read(fileindex, index);
1095 cf61d818 2019-01-12 stsp fclose(index);
1096 cf61d818 2019-01-12 stsp if (err)
1097 cf61d818 2019-01-12 stsp goto done;
1098 51514078 2018-12-25 stsp }
1099 51514078 2018-12-25 stsp
1100 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1101 b8bdcc21 2018-12-24 stsp fileindex_path);
1102 51664889 2018-03-12 stsp if (err)
1103 51664889 2018-03-12 stsp goto done;
1104 51664889 2018-03-12 stsp
1105 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1106 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1107 9d31a1d8 2018-03-11 stsp if (err)
1108 9d31a1d8 2018-03-11 stsp goto done;
1109 9d31a1d8 2018-03-11 stsp
1110 8da9e5f4 2019-01-12 stsp err = got_object_id_by_path(&tree_id, repo,
1111 8da9e5f4 2019-01-12 stsp worktree->base_commit_id, worktree->path_prefix);
1112 9d31a1d8 2018-03-11 stsp if (err)
1113 9d31a1d8 2018-03-11 stsp goto done;
1114 9d31a1d8 2018-03-11 stsp
1115 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1116 8da9e5f4 2019-01-12 stsp if (err)
1117 8da9e5f4 2019-01-12 stsp goto done;
1118 9d31a1d8 2018-03-11 stsp
1119 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1120 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1121 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1122 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1123 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1124 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1125 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1126 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1127 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1128 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1129 8da9e5f4 2019-01-12 stsp checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1130 8da9e5f4 2019-01-12 stsp &diff_cb, &arg);
1131 8da9e5f4 2019-01-12 stsp
1132 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
1133 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
1134 9d31a1d8 2018-03-11 stsp if (err)
1135 9d31a1d8 2018-03-11 stsp goto done;
1136 9d31a1d8 2018-03-11 stsp
1137 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1138 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
1139 9d31a1d8 2018-03-11 stsp goto done;
1140 9d31a1d8 2018-03-11 stsp }
1141 9d31a1d8 2018-03-11 stsp
1142 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1143 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
1144 9d31a1d8 2018-03-11 stsp
1145 9d31a1d8 2018-03-11 stsp done:
1146 edfa7d7f 2018-09-11 stsp if (tree)
1147 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1148 9d31a1d8 2018-03-11 stsp if (commit)
1149 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1150 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
1151 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
1152 b8bdcc21 2018-12-24 stsp if (new_index)
1153 b8bdcc21 2018-12-24 stsp fclose(new_index);
1154 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1155 9d31a1d8 2018-03-11 stsp free(fileindex_path);
1156 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1157 a143fb78 2018-12-25 stsp if (checkout_err)
1158 a143fb78 2018-12-25 stsp err = checkout_err;
1159 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1160 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1161 9d31a1d8 2018-03-11 stsp err = unlockerr;
1162 f8d1f275 2019-02-04 stsp return err;
1163 f8d1f275 2019-02-04 stsp }
1164 f8d1f275 2019-02-04 stsp
1165 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1166 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1167 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1168 927df6b7 2019-02-10 stsp const char *status_path;
1169 927df6b7 2019-02-10 stsp size_t status_path_len;
1170 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1171 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1172 f8d1f275 2019-02-04 stsp void *status_arg;
1173 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1174 f8d1f275 2019-02-04 stsp void *cancel_arg;
1175 f8d1f275 2019-02-04 stsp };
1176 f8d1f275 2019-02-04 stsp
1177 f8d1f275 2019-02-04 stsp static const struct got_error *
1178 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1179 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1180 927df6b7 2019-02-10 stsp struct got_repository *repo)
1181 927df6b7 2019-02-10 stsp {
1182 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1183 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1184 927df6b7 2019-02-10 stsp struct stat sb;
1185 927df6b7 2019-02-10 stsp struct got_object_id id;
1186 927df6b7 2019-02-10 stsp
1187 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1188 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1189 927df6b7 2019-02-10 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1190 927df6b7 2019-02-10 stsp err = (*status_cb)(status_arg, status, ie->path, &id);
1191 927df6b7 2019-02-10 stsp }
1192 927df6b7 2019-02-10 stsp return err;
1193 927df6b7 2019-02-10 stsp }
1194 927df6b7 2019-02-10 stsp
1195 927df6b7 2019-02-10 stsp static const struct got_error *
1196 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1197 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1198 f8d1f275 2019-02-04 stsp {
1199 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1200 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1201 f8d1f275 2019-02-04 stsp char *abspath;
1202 f8d1f275 2019-02-04 stsp
1203 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1204 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1205 927df6b7 2019-02-10 stsp return NULL;
1206 927df6b7 2019-02-10 stsp
1207 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1208 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1209 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1210 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1211 f8d1f275 2019-02-04 stsp } else {
1212 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1213 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1214 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1215 f8d1f275 2019-02-04 stsp }
1216 f8d1f275 2019-02-04 stsp
1217 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1218 927df6b7 2019-02-10 stsp a->repo);
1219 f8d1f275 2019-02-04 stsp free(abspath);
1220 9d31a1d8 2018-03-11 stsp return err;
1221 9d31a1d8 2018-03-11 stsp }
1222 f8d1f275 2019-02-04 stsp
1223 f8d1f275 2019-02-04 stsp static const struct got_error *
1224 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1225 f8d1f275 2019-02-04 stsp {
1226 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1227 b72f483a 2019-02-05 stsp struct got_object_id id;
1228 927df6b7 2019-02-10 stsp
1229 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1230 927df6b7 2019-02-10 stsp return NULL;
1231 927df6b7 2019-02-10 stsp
1232 b72f483a 2019-02-05 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1233 b72f483a 2019-02-05 stsp return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1234 b72f483a 2019-02-05 stsp &id);
1235 f8d1f275 2019-02-04 stsp }
1236 f8d1f275 2019-02-04 stsp
1237 f8d1f275 2019-02-04 stsp static const struct got_error *
1238 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
1239 f8d1f275 2019-02-04 stsp {
1240 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1241 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1242 f8d1f275 2019-02-04 stsp char *path = NULL;
1243 f8d1f275 2019-02-04 stsp
1244 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
1245 2c201a36 2019-02-10 stsp return NULL;
1246 2c201a36 2019-02-10 stsp
1247 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
1248 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
1249 f8d1f275 2019-02-04 stsp return NULL;
1250 f8d1f275 2019-02-04 stsp
1251 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1252 927df6b7 2019-02-10 stsp return NULL;
1253 927df6b7 2019-02-10 stsp
1254 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1255 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1256 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1257 f8d1f275 2019-02-04 stsp } else {
1258 f8d1f275 2019-02-04 stsp path = de->d_name;
1259 f8d1f275 2019-02-04 stsp }
1260 f8d1f275 2019-02-04 stsp
1261 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1262 b72f483a 2019-02-05 stsp NULL);
1263 f8d1f275 2019-02-04 stsp if (parent_path[0])
1264 f8d1f275 2019-02-04 stsp free(path);
1265 b72f483a 2019-02-05 stsp return err;
1266 f8d1f275 2019-02-04 stsp }
1267 f8d1f275 2019-02-04 stsp
1268 f8d1f275 2019-02-04 stsp const struct got_error *
1269 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
1270 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
1271 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1272 f8d1f275 2019-02-04 stsp {
1273 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1274 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
1275 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
1276 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
1277 f8d1f275 2019-02-04 stsp FILE *index = NULL;
1278 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
1279 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
1280 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
1281 f8d1f275 2019-02-04 stsp
1282 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
1283 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
1284 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1285 f8d1f275 2019-02-04 stsp goto done;
1286 f8d1f275 2019-02-04 stsp }
1287 f8d1f275 2019-02-04 stsp
1288 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1289 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1290 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1291 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
1292 f8d1f275 2019-02-04 stsp goto done;
1293 f8d1f275 2019-02-04 stsp }
1294 f8d1f275 2019-02-04 stsp
1295 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
1296 f8d1f275 2019-02-04 stsp if (index == NULL) {
1297 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
1298 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1299 f8d1f275 2019-02-04 stsp goto done;
1300 f8d1f275 2019-02-04 stsp }
1301 f8d1f275 2019-02-04 stsp } else {
1302 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
1303 f8d1f275 2019-02-04 stsp fclose(index);
1304 f8d1f275 2019-02-04 stsp if (err)
1305 f8d1f275 2019-02-04 stsp goto done;
1306 f8d1f275 2019-02-04 stsp }
1307 f8d1f275 2019-02-04 stsp
1308 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
1309 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
1310 c513d110 2019-02-05 stsp err = got_error_from_errno();
1311 c513d110 2019-02-05 stsp goto done;
1312 c513d110 2019-02-05 stsp }
1313 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
1314 927df6b7 2019-02-10 stsp if (workdir == NULL) {
1315 927df6b7 2019-02-10 stsp if (errno == ENOTDIR) {
1316 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
1317 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
1318 927df6b7 2019-02-10 stsp if (ie == NULL) {
1319 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
1320 927df6b7 2019-02-10 stsp goto done;
1321 927df6b7 2019-02-10 stsp }
1322 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
1323 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
1324 927df6b7 2019-02-10 stsp goto done;
1325 927df6b7 2019-02-10 stsp } else {
1326 927df6b7 2019-02-10 stsp err = got_error_from_errno();
1327 927df6b7 2019-02-10 stsp goto done;
1328 927df6b7 2019-02-10 stsp }
1329 927df6b7 2019-02-10 stsp }
1330 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
1331 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
1332 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
1333 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
1334 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
1335 927df6b7 2019-02-10 stsp arg.status_path = path;
1336 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
1337 f8d1f275 2019-02-04 stsp arg.repo = repo;
1338 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
1339 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
1340 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
1341 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
1342 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1343 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
1344 f8d1f275 2019-02-04 stsp done:
1345 f8d1f275 2019-02-04 stsp if (workdir)
1346 f8d1f275 2019-02-04 stsp closedir(workdir);
1347 927df6b7 2019-02-10 stsp free(ondisk_path);
1348 f8d1f275 2019-02-04 stsp free(fileindex_path);
1349 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
1350 f8d1f275 2019-02-04 stsp return err;
1351 f8d1f275 2019-02-04 stsp }