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