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 7154f6ce 2019-03-27 stsp #include <util.h>
36 86c3caaf 2018-03-09 stsp
37 86c3caaf 2018-03-09 stsp #include "got_error.h"
38 86c3caaf 2018-03-09 stsp #include "got_repository.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 9d31a1d8 2018-03-11 stsp #include "got_object.h"
41 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
42 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 86c3caaf 2018-03-09 stsp
44 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
51 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
52 9d31a1d8 2018-03-11 stsp
53 9d31a1d8 2018-03-11 stsp #ifndef MIN
54 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 9d31a1d8 2018-03-11 stsp #endif
56 86c3caaf 2018-03-09 stsp
57 99724ed4 2018-03-10 stsp static const struct got_error *
58 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
59 99724ed4 2018-03-10 stsp {
60 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
61 99724ed4 2018-03-10 stsp char *path;
62 99724ed4 2018-03-10 stsp int fd = -1;
63 99724ed4 2018-03-10 stsp
64 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
65 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
66 99724ed4 2018-03-10 stsp path = NULL;
67 99724ed4 2018-03-10 stsp goto done;
68 99724ed4 2018-03-10 stsp }
69 99724ed4 2018-03-10 stsp
70 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
71 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
72 99724ed4 2018-03-10 stsp if (fd == -1) {
73 99724ed4 2018-03-10 stsp err = got_error_from_errno();
74 99724ed4 2018-03-10 stsp goto done;
75 99724ed4 2018-03-10 stsp }
76 99724ed4 2018-03-10 stsp
77 99724ed4 2018-03-10 stsp if (content) {
78 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
79 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
80 99724ed4 2018-03-10 stsp err = got_error_from_errno();
81 99724ed4 2018-03-10 stsp goto done;
82 99724ed4 2018-03-10 stsp }
83 99724ed4 2018-03-10 stsp }
84 99724ed4 2018-03-10 stsp
85 99724ed4 2018-03-10 stsp done:
86 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
87 99724ed4 2018-03-10 stsp err = got_error_from_errno();
88 99724ed4 2018-03-10 stsp free(path);
89 507dc3bb 2018-12-29 stsp return err;
90 507dc3bb 2018-12-29 stsp }
91 507dc3bb 2018-12-29 stsp
92 507dc3bb 2018-12-29 stsp static const struct got_error *
93 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
94 507dc3bb 2018-12-29 stsp {
95 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
96 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
97 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
98 507dc3bb 2018-12-29 stsp char *path = NULL;
99 507dc3bb 2018-12-29 stsp
100 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
102 507dc3bb 2018-12-29 stsp path = NULL;
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp
106 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
107 507dc3bb 2018-12-29 stsp if (err)
108 507dc3bb 2018-12-29 stsp goto done;
109 507dc3bb 2018-12-29 stsp
110 507dc3bb 2018-12-29 stsp if (content) {
111 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
112 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
113 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
114 507dc3bb 2018-12-29 stsp goto done;
115 507dc3bb 2018-12-29 stsp }
116 507dc3bb 2018-12-29 stsp }
117 507dc3bb 2018-12-29 stsp
118 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
119 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
120 2a57020b 2019-02-20 stsp unlink(tmppath);
121 507dc3bb 2018-12-29 stsp goto done;
122 507dc3bb 2018-12-29 stsp }
123 507dc3bb 2018-12-29 stsp
124 507dc3bb 2018-12-29 stsp done:
125 507dc3bb 2018-12-29 stsp free(tmppath);
126 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
127 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
128 99724ed4 2018-03-10 stsp return err;
129 99724ed4 2018-03-10 stsp }
130 99724ed4 2018-03-10 stsp
131 09fe317a 2018-03-11 stsp static const struct got_error *
132 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
133 09fe317a 2018-03-11 stsp {
134 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
135 09fe317a 2018-03-11 stsp char *path;
136 09fe317a 2018-03-11 stsp int fd = -1;
137 09fe317a 2018-03-11 stsp ssize_t n;
138 09fe317a 2018-03-11 stsp struct stat sb;
139 09fe317a 2018-03-11 stsp
140 09fe317a 2018-03-11 stsp *content = NULL;
141 09fe317a 2018-03-11 stsp
142 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
143 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
144 09fe317a 2018-03-11 stsp path = NULL;
145 09fe317a 2018-03-11 stsp goto done;
146 09fe317a 2018-03-11 stsp }
147 09fe317a 2018-03-11 stsp
148 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
149 09fe317a 2018-03-11 stsp if (fd == -1) {
150 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
151 f02eaa22 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
152 f02eaa22 2019-03-11 stsp else
153 f02eaa22 2019-03-11 stsp err = got_error_from_errno();
154 ef99fdb1 2018-03-11 stsp goto done;
155 ef99fdb1 2018-03-11 stsp }
156 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
157 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
158 73a5ef67 2018-03-11 stsp : got_error_from_errno());
159 09fe317a 2018-03-11 stsp goto done;
160 09fe317a 2018-03-11 stsp }
161 09fe317a 2018-03-11 stsp
162 d10c9b58 2019-02-19 stsp if (lstat(path, &sb) != 0) {
163 d10c9b58 2019-02-19 stsp err = got_error_from_errno();
164 d10c9b58 2019-02-19 stsp goto done;
165 d10c9b58 2019-02-19 stsp }
166 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
167 09fe317a 2018-03-11 stsp if (*content == NULL) {
168 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
169 09fe317a 2018-03-11 stsp goto done;
170 09fe317a 2018-03-11 stsp }
171 09fe317a 2018-03-11 stsp
172 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
173 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
174 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
175 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
176 09fe317a 2018-03-11 stsp goto done;
177 09fe317a 2018-03-11 stsp }
178 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
179 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
180 09fe317a 2018-03-11 stsp goto done;
181 09fe317a 2018-03-11 stsp }
182 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
183 09fe317a 2018-03-11 stsp
184 09fe317a 2018-03-11 stsp done:
185 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
186 09fe317a 2018-03-11 stsp err = got_error_from_errno();
187 09fe317a 2018-03-11 stsp free(path);
188 09fe317a 2018-03-11 stsp if (err) {
189 09fe317a 2018-03-11 stsp free(*content);
190 09fe317a 2018-03-11 stsp *content = NULL;
191 09fe317a 2018-03-11 stsp }
192 09fe317a 2018-03-11 stsp return err;
193 09fe317a 2018-03-11 stsp }
194 09fe317a 2018-03-11 stsp
195 86c3caaf 2018-03-09 stsp const struct got_error *
196 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
197 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
198 86c3caaf 2018-03-09 stsp {
199 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
200 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
201 ec22038e 2019-03-10 stsp uuid_t uuid;
202 ec22038e 2019-03-10 stsp uint32_t uuid_status;
203 65596e15 2018-12-24 stsp int obj_type;
204 7ac97322 2018-03-11 stsp char *path_got = NULL;
205 08d425ea 2018-12-24 stsp char *refstr = NULL;
206 1451e70d 2018-03-10 stsp char *formatstr = NULL;
207 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
208 65596e15 2018-12-24 stsp char *basestr = NULL;
209 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
210 65596e15 2018-12-24 stsp
211 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
212 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
213 0c48fee2 2019-03-11 stsp goto done;
214 0c48fee2 2019-03-11 stsp }
215 0c48fee2 2019-03-11 stsp
216 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
217 65596e15 2018-12-24 stsp if (err)
218 65596e15 2018-12-24 stsp return err;
219 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
220 65596e15 2018-12-24 stsp if (err)
221 65596e15 2018-12-24 stsp return err;
222 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
223 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
224 86c3caaf 2018-03-09 stsp
225 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
226 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
227 0a585a0d 2018-03-17 stsp return got_error_from_errno();
228 0bb8a95e 2018-03-12 stsp }
229 577ec78f 2018-03-11 stsp
230 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
231 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
232 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
233 86c3caaf 2018-03-09 stsp goto done;
234 86c3caaf 2018-03-09 stsp }
235 86c3caaf 2018-03-09 stsp
236 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
237 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
238 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
239 86c3caaf 2018-03-09 stsp goto done;
240 86c3caaf 2018-03-09 stsp }
241 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
242 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
243 86c3caaf 2018-03-09 stsp goto done;
244 86c3caaf 2018-03-09 stsp }
245 86c3caaf 2018-03-09 stsp
246 056e7441 2018-03-11 stsp /* Create an empty lock file. */
247 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
248 056e7441 2018-03-11 stsp if (err)
249 056e7441 2018-03-11 stsp goto done;
250 056e7441 2018-03-11 stsp
251 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
252 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
253 99724ed4 2018-03-10 stsp if (err)
254 86c3caaf 2018-03-09 stsp goto done;
255 86c3caaf 2018-03-09 stsp
256 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
257 08d425ea 2018-12-24 stsp refstr = got_ref_to_str(head_ref);
258 08d425ea 2018-12-24 stsp if (refstr == NULL) {
259 a1a7858a 2018-12-24 stsp err = got_error_from_errno();
260 a1a7858a 2018-12-24 stsp goto done;
261 a1a7858a 2018-12-24 stsp }
262 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
263 65596e15 2018-12-24 stsp if (err)
264 65596e15 2018-12-24 stsp goto done;
265 65596e15 2018-12-24 stsp
266 65596e15 2018-12-24 stsp /* Record our base commit. */
267 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
268 65596e15 2018-12-24 stsp if (err)
269 65596e15 2018-12-24 stsp goto done;
270 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
271 99724ed4 2018-03-10 stsp if (err)
272 86c3caaf 2018-03-09 stsp goto done;
273 86c3caaf 2018-03-09 stsp
274 1451e70d 2018-03-10 stsp /* Store path to repository. */
275 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
276 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
277 99724ed4 2018-03-10 stsp if (err)
278 86c3caaf 2018-03-09 stsp goto done;
279 86c3caaf 2018-03-09 stsp
280 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
281 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
282 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
283 ec22038e 2019-03-10 stsp if (err)
284 ec22038e 2019-03-10 stsp goto done;
285 ec22038e 2019-03-10 stsp
286 ec22038e 2019-03-10 stsp /* Generate UUID. */
287 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
288 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
289 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
290 ec22038e 2019-03-10 stsp goto done;
291 ec22038e 2019-03-10 stsp }
292 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
293 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
294 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
295 ec22038e 2019-03-10 stsp goto done;
296 ec22038e 2019-03-10 stsp }
297 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
298 577ec78f 2018-03-11 stsp if (err)
299 577ec78f 2018-03-11 stsp goto done;
300 577ec78f 2018-03-11 stsp
301 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
302 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
303 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
304 1451e70d 2018-03-10 stsp goto done;
305 1451e70d 2018-03-10 stsp }
306 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
307 99724ed4 2018-03-10 stsp if (err)
308 1451e70d 2018-03-10 stsp goto done;
309 1451e70d 2018-03-10 stsp
310 86c3caaf 2018-03-09 stsp done:
311 65596e15 2018-12-24 stsp free(commit_id);
312 7ac97322 2018-03-11 stsp free(path_got);
313 1451e70d 2018-03-10 stsp free(formatstr);
314 08d425ea 2018-12-24 stsp free(refstr);
315 0bb8a95e 2018-03-12 stsp free(absprefix);
316 65596e15 2018-12-24 stsp free(basestr);
317 ec22038e 2019-03-10 stsp free(uuidstr);
318 86c3caaf 2018-03-09 stsp return err;
319 86c3caaf 2018-03-09 stsp }
320 86c3caaf 2018-03-09 stsp
321 247140b2 2019-02-05 stsp static const struct got_error *
322 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
323 86c3caaf 2018-03-09 stsp {
324 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
325 7ac97322 2018-03-11 stsp char *path_got;
326 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
327 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
328 056e7441 2018-03-11 stsp char *path_lock = NULL;
329 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
330 271d2a38 2018-12-25 stsp char *head_ref_str = NULL;
331 6d9d28c3 2018-03-11 stsp int version, fd = -1;
332 6d9d28c3 2018-03-11 stsp const char *errstr;
333 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
334 c442a90d 2019-03-10 stsp uint32_t uuid_status;
335 6d9d28c3 2018-03-11 stsp
336 6d9d28c3 2018-03-11 stsp *worktree = NULL;
337 6d9d28c3 2018-03-11 stsp
338 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
340 7ac97322 2018-03-11 stsp path_got = NULL;
341 6d9d28c3 2018-03-11 stsp goto done;
342 6d9d28c3 2018-03-11 stsp }
343 6d9d28c3 2018-03-11 stsp
344 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
346 056e7441 2018-03-11 stsp path_lock = NULL;
347 6d9d28c3 2018-03-11 stsp goto done;
348 6d9d28c3 2018-03-11 stsp }
349 6d9d28c3 2018-03-11 stsp
350 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 6d9d28c3 2018-03-11 stsp if (fd == -1) {
352 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 73a5ef67 2018-03-11 stsp : got_error_from_errno());
354 6d9d28c3 2018-03-11 stsp goto done;
355 6d9d28c3 2018-03-11 stsp }
356 6d9d28c3 2018-03-11 stsp
357 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 6d9d28c3 2018-03-11 stsp if (err)
359 6d9d28c3 2018-03-11 stsp goto done;
360 6d9d28c3 2018-03-11 stsp
361 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 6d9d28c3 2018-03-11 stsp if (errstr) {
363 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
364 6d9d28c3 2018-03-11 stsp goto done;
365 6d9d28c3 2018-03-11 stsp }
366 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
367 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
368 6d9d28c3 2018-03-11 stsp goto done;
369 6d9d28c3 2018-03-11 stsp }
370 6d9d28c3 2018-03-11 stsp
371 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
372 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
373 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
374 6d9d28c3 2018-03-11 stsp goto done;
375 6d9d28c3 2018-03-11 stsp }
376 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
377 6d9d28c3 2018-03-11 stsp
378 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
379 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
380 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
381 6d9d28c3 2018-03-11 stsp goto done;
382 6d9d28c3 2018-03-11 stsp }
383 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
384 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
385 6d9d28c3 2018-03-11 stsp if (err)
386 6d9d28c3 2018-03-11 stsp goto done;
387 eaccb85f 2018-12-25 stsp
388 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
389 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
390 93a30277 2018-12-24 stsp if (err)
391 93a30277 2018-12-24 stsp goto done;
392 93a30277 2018-12-24 stsp
393 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
394 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
395 c442a90d 2019-03-10 stsp if (err)
396 c442a90d 2019-03-10 stsp goto done;
397 c442a90d 2019-03-10 stsp
398 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
399 eaccb85f 2018-12-25 stsp if (err)
400 c442a90d 2019-03-10 stsp goto done;
401 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
402 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
403 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
404 eaccb85f 2018-12-25 stsp goto done;
405 c442a90d 2019-03-10 stsp }
406 eaccb85f 2018-12-25 stsp
407 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
408 eaccb85f 2018-12-25 stsp if (err)
409 eaccb85f 2018-12-25 stsp goto done;
410 eaccb85f 2018-12-25 stsp
411 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
412 eaccb85f 2018-12-25 stsp base_commit_id_str);
413 f5baf295 2018-03-11 stsp if (err)
414 f5baf295 2018-03-11 stsp goto done;
415 f5baf295 2018-03-11 stsp
416 271d2a38 2018-12-25 stsp err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
417 f5baf295 2018-03-11 stsp if (err)
418 6d9d28c3 2018-03-11 stsp goto done;
419 6d9d28c3 2018-03-11 stsp
420 271d2a38 2018-12-25 stsp err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
421 6d9d28c3 2018-03-11 stsp done:
422 eaccb85f 2018-12-25 stsp if (repo)
423 eaccb85f 2018-12-25 stsp got_repo_close(repo);
424 7ac97322 2018-03-11 stsp free(path_got);
425 056e7441 2018-03-11 stsp free(path_lock);
426 271d2a38 2018-12-25 stsp free(head_ref_str);
427 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
428 c442a90d 2019-03-10 stsp free(uuidstr);
429 bd165944 2019-03-10 stsp free(formatstr);
430 6d9d28c3 2018-03-11 stsp if (err) {
431 6d9d28c3 2018-03-11 stsp if (fd != -1)
432 6d9d28c3 2018-03-11 stsp close(fd);
433 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
434 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
435 6d9d28c3 2018-03-11 stsp *worktree = NULL;
436 6d9d28c3 2018-03-11 stsp } else
437 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
438 6d9d28c3 2018-03-11 stsp
439 6d9d28c3 2018-03-11 stsp return err;
440 86c3caaf 2018-03-09 stsp }
441 247140b2 2019-02-05 stsp
442 247140b2 2019-02-05 stsp const struct got_error *
443 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
444 247140b2 2019-02-05 stsp {
445 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
446 86c3caaf 2018-03-09 stsp
447 247140b2 2019-02-05 stsp do {
448 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
449 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
450 247140b2 2019-02-05 stsp return err;
451 247140b2 2019-02-05 stsp if (*worktree)
452 247140b2 2019-02-05 stsp return NULL;
453 247140b2 2019-02-05 stsp path = dirname(path);
454 d1542a27 2019-02-05 stsp if (path == NULL)
455 d1542a27 2019-02-05 stsp return got_error_from_errno();
456 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
457 247140b2 2019-02-05 stsp
458 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
459 247140b2 2019-02-05 stsp }
460 247140b2 2019-02-05 stsp
461 3a6ce05a 2019-02-11 stsp const struct got_error *
462 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
463 86c3caaf 2018-03-09 stsp {
464 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
465 c88eb298 2018-03-11 stsp free(worktree->root_path);
466 cde76477 2018-03-11 stsp free(worktree->repo_path);
467 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
468 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
469 271d2a38 2018-12-25 stsp if (worktree->head_ref)
470 271d2a38 2018-12-25 stsp got_ref_close(worktree->head_ref);
471 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
472 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
473 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
474 6d9d28c3 2018-03-11 stsp free(worktree);
475 3a6ce05a 2019-02-11 stsp return err;
476 86c3caaf 2018-03-09 stsp }
477 86c3caaf 2018-03-09 stsp
478 2fbdb5ae 2018-12-29 stsp const char *
479 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
480 c7f4312f 2019-02-05 stsp {
481 c7f4312f 2019-02-05 stsp return worktree->root_path;
482 c7f4312f 2019-02-05 stsp }
483 c7f4312f 2019-02-05 stsp
484 c7f4312f 2019-02-05 stsp const char *
485 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
486 86c3caaf 2018-03-09 stsp {
487 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
488 49520a32 2018-12-29 stsp }
489 49520a32 2018-12-29 stsp
490 49520a32 2018-12-29 stsp const char *
491 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
492 49520a32 2018-12-29 stsp {
493 f609be2e 2018-12-29 stsp return worktree->path_prefix;
494 e5dc7198 2018-12-29 stsp }
495 e5dc7198 2018-12-29 stsp
496 e5dc7198 2018-12-29 stsp const struct got_error *
497 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
498 e5dc7198 2018-12-29 stsp const char *path_prefix)
499 e5dc7198 2018-12-29 stsp {
500 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
501 e5dc7198 2018-12-29 stsp
502 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
503 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
504 e5dc7198 2018-12-29 stsp return got_error_from_errno();
505 e5dc7198 2018-12-29 stsp }
506 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
507 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
508 e5dc7198 2018-12-29 stsp free(absprefix);
509 e5dc7198 2018-12-29 stsp return NULL;
510 86c3caaf 2018-03-09 stsp }
511 86c3caaf 2018-03-09 stsp
512 35be1456 2018-03-11 stsp char *
513 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
514 86c3caaf 2018-03-09 stsp {
515 271d2a38 2018-12-25 stsp return got_ref_to_str(worktree->head_ref);
516 be7061eb 2018-12-30 stsp }
517 be7061eb 2018-12-30 stsp
518 be7061eb 2018-12-30 stsp struct got_reference *
519 be7061eb 2018-12-30 stsp got_worktree_get_head_ref(struct got_worktree *worktree)
520 be7061eb 2018-12-30 stsp {
521 be7061eb 2018-12-30 stsp return got_ref_dup(worktree->head_ref);
522 507dc3bb 2018-12-29 stsp }
523 507dc3bb 2018-12-29 stsp
524 b72f483a 2019-02-05 stsp struct got_object_id *
525 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
526 507dc3bb 2018-12-29 stsp {
527 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
528 86c3caaf 2018-03-09 stsp }
529 86c3caaf 2018-03-09 stsp
530 507dc3bb 2018-12-29 stsp const struct got_error *
531 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
532 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
533 507dc3bb 2018-12-29 stsp {
534 507dc3bb 2018-12-29 stsp const struct got_error *err;
535 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
536 507dc3bb 2018-12-29 stsp char *id_str = NULL;
537 507dc3bb 2018-12-29 stsp char *path_got = NULL;
538 507dc3bb 2018-12-29 stsp
539 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
540 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
541 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
542 507dc3bb 2018-12-29 stsp path_got = NULL;
543 507dc3bb 2018-12-29 stsp goto done;
544 507dc3bb 2018-12-29 stsp }
545 507dc3bb 2018-12-29 stsp
546 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
547 507dc3bb 2018-12-29 stsp if (err)
548 507dc3bb 2018-12-29 stsp return err;
549 507dc3bb 2018-12-29 stsp
550 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
551 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
552 507dc3bb 2018-12-29 stsp goto done;
553 507dc3bb 2018-12-29 stsp }
554 507dc3bb 2018-12-29 stsp
555 507dc3bb 2018-12-29 stsp /* Record our base commit. */
556 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
557 507dc3bb 2018-12-29 stsp if (err)
558 507dc3bb 2018-12-29 stsp goto done;
559 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
560 507dc3bb 2018-12-29 stsp if (err)
561 507dc3bb 2018-12-29 stsp goto done;
562 507dc3bb 2018-12-29 stsp
563 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
564 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
565 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
566 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
567 507dc3bb 2018-12-29 stsp goto done;
568 507dc3bb 2018-12-29 stsp }
569 507dc3bb 2018-12-29 stsp done:
570 507dc3bb 2018-12-29 stsp if (obj)
571 507dc3bb 2018-12-29 stsp got_object_close(obj);
572 507dc3bb 2018-12-29 stsp free(id_str);
573 507dc3bb 2018-12-29 stsp free(path_got);
574 507dc3bb 2018-12-29 stsp return err;
575 507dc3bb 2018-12-29 stsp }
576 507dc3bb 2018-12-29 stsp
577 9d31a1d8 2018-03-11 stsp static const struct got_error *
578 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
579 86c3caaf 2018-03-09 stsp {
580 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
581 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
582 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
583 86c3caaf 2018-03-09 stsp return NULL;
584 21908da4 2019-01-13 stsp }
585 21908da4 2019-01-13 stsp
586 21908da4 2019-01-13 stsp static const struct got_error *
587 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
588 4a1ddfc2 2019-01-12 stsp {
589 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
590 4a1ddfc2 2019-01-12 stsp char *abspath;
591 4a1ddfc2 2019-01-12 stsp
592 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
593 4a1ddfc2 2019-01-12 stsp return got_error_from_errno();
594 4a1ddfc2 2019-01-12 stsp
595 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
596 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
597 ddcd8544 2019-03-11 stsp struct stat sb;
598 ddcd8544 2019-03-11 stsp err = NULL;
599 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
600 ddcd8544 2019-03-11 stsp err = got_error_from_errno();
601 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
602 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
603 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
604 ddcd8544 2019-03-11 stsp }
605 ddcd8544 2019-03-11 stsp }
606 4a1ddfc2 2019-01-12 stsp free(abspath);
607 68c76935 2019-02-19 stsp return err;
608 68c76935 2019-02-19 stsp }
609 68c76935 2019-02-19 stsp
610 68c76935 2019-02-19 stsp static const struct got_error *
611 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
612 68c76935 2019-02-19 stsp {
613 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
614 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
615 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
616 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
617 68c76935 2019-02-19 stsp
618 68c76935 2019-02-19 stsp *same = 1;
619 68c76935 2019-02-19 stsp
620 68c76935 2019-02-19 stsp while (1) {
621 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
622 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
623 68c76935 2019-02-19 stsp err = got_error_from_errno();
624 68c76935 2019-02-19 stsp break;
625 68c76935 2019-02-19 stsp }
626 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
627 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
628 68c76935 2019-02-19 stsp err = got_error_from_errno();
629 68c76935 2019-02-19 stsp break;
630 68c76935 2019-02-19 stsp }
631 68c76935 2019-02-19 stsp if (flen1 == 0) {
632 68c76935 2019-02-19 stsp if (flen2 != 0)
633 68c76935 2019-02-19 stsp *same = 0;
634 68c76935 2019-02-19 stsp break;
635 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
636 68c76935 2019-02-19 stsp if (flen1 != 0)
637 68c76935 2019-02-19 stsp *same = 0;
638 68c76935 2019-02-19 stsp break;
639 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
640 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
641 68c76935 2019-02-19 stsp *same = 0;
642 68c76935 2019-02-19 stsp break;
643 68c76935 2019-02-19 stsp }
644 68c76935 2019-02-19 stsp } else {
645 68c76935 2019-02-19 stsp *same = 0;
646 68c76935 2019-02-19 stsp break;
647 68c76935 2019-02-19 stsp }
648 68c76935 2019-02-19 stsp }
649 68c76935 2019-02-19 stsp
650 68c76935 2019-02-19 stsp return err;
651 68c76935 2019-02-19 stsp }
652 68c76935 2019-02-19 stsp
653 68c76935 2019-02-19 stsp static const struct got_error *
654 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
655 68c76935 2019-02-19 stsp {
656 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
657 68c76935 2019-02-19 stsp struct stat sb;
658 68c76935 2019-02-19 stsp size_t size1, size2;
659 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
660 68c76935 2019-02-19 stsp
661 68c76935 2019-02-19 stsp *same = 1;
662 68c76935 2019-02-19 stsp
663 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
664 68c76935 2019-02-19 stsp err = got_error_from_errno();
665 68c76935 2019-02-19 stsp goto done;
666 68c76935 2019-02-19 stsp }
667 68c76935 2019-02-19 stsp size1 = sb.st_size;
668 68c76935 2019-02-19 stsp
669 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
670 68c76935 2019-02-19 stsp err = got_error_from_errno();
671 68c76935 2019-02-19 stsp goto done;
672 68c76935 2019-02-19 stsp }
673 68c76935 2019-02-19 stsp size2 = sb.st_size;
674 68c76935 2019-02-19 stsp
675 68c76935 2019-02-19 stsp if (size1 != size2) {
676 68c76935 2019-02-19 stsp *same = 0;
677 68c76935 2019-02-19 stsp return NULL;
678 68c76935 2019-02-19 stsp }
679 68c76935 2019-02-19 stsp
680 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
681 68c76935 2019-02-19 stsp if (f1 == NULL)
682 68c76935 2019-02-19 stsp return got_error_from_errno();
683 68c76935 2019-02-19 stsp
684 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
685 68c76935 2019-02-19 stsp if (f2 == NULL) {
686 68c76935 2019-02-19 stsp err = got_error_from_errno();
687 68c76935 2019-02-19 stsp goto done;
688 68c76935 2019-02-19 stsp }
689 68c76935 2019-02-19 stsp
690 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
691 68c76935 2019-02-19 stsp done:
692 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
693 68c76935 2019-02-19 stsp err = got_error_from_errno();
694 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
695 68c76935 2019-02-19 stsp err = got_error_from_errno();
696 68c76935 2019-02-19 stsp
697 6353ad76 2019-02-08 stsp return err;
698 6353ad76 2019-02-08 stsp }
699 6353ad76 2019-02-08 stsp
700 6353ad76 2019-02-08 stsp /*
701 6353ad76 2019-02-08 stsp * Perform a 3-way merge where the file's version in the file index (blob2)
702 6353ad76 2019-02-08 stsp * acts as the common ancestor, the incoming blob (blob1) acts as the first
703 6353ad76 2019-02-08 stsp * derived version, and the file on disk acts as the second derived version.
704 6353ad76 2019-02-08 stsp */
705 6353ad76 2019-02-08 stsp static const struct got_error *
706 6353ad76 2019-02-08 stsp merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
707 6353ad76 2019-02-08 stsp struct got_fileindex_entry *ie, 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 *blob1,
709 b8f41171 2019-02-10 stsp struct got_repository *repo,
710 6353ad76 2019-02-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
711 6353ad76 2019-02-08 stsp {
712 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
713 6353ad76 2019-02-08 stsp int merged_fd = -1;
714 6353ad76 2019-02-08 stsp struct got_blob_object *blob2 = NULL;
715 6353ad76 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL;
716 6353ad76 2019-02-08 stsp char *blob1_path = NULL, *blob2_path = NULL;
717 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
718 6353ad76 2019-02-08 stsp char *id_str = NULL;
719 6353ad76 2019-02-08 stsp char *label1 = NULL;
720 68c76935 2019-02-19 stsp int overlapcnt = 0, update_timestamps = 0;
721 af54ae4a 2019-02-19 stsp char *parent;
722 6353ad76 2019-02-08 stsp
723 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
724 af54ae4a 2019-02-19 stsp if (parent == NULL)
725 af54ae4a 2019-02-19 stsp return got_error_from_errno();
726 af54ae4a 2019-02-19 stsp
727 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
728 af54ae4a 2019-02-19 stsp return got_error_from_errno();
729 af54ae4a 2019-02-19 stsp
730 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
731 6353ad76 2019-02-08 stsp if (err)
732 af54ae4a 2019-02-19 stsp goto done;
733 af54ae4a 2019-02-19 stsp
734 af54ae4a 2019-02-19 stsp free(base_path);
735 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
736 af54ae4a 2019-02-19 stsp err = got_error_from_errno();
737 af54ae4a 2019-02-19 stsp base_path = NULL;
738 af54ae4a 2019-02-19 stsp goto done;
739 af54ae4a 2019-02-19 stsp }
740 af54ae4a 2019-02-19 stsp
741 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob1_path, &f1, base_path);
742 6353ad76 2019-02-08 stsp if (err)
743 6353ad76 2019-02-08 stsp goto done;
744 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
745 6353ad76 2019-02-08 stsp if (err)
746 6353ad76 2019-02-08 stsp goto done;
747 6353ad76 2019-02-08 stsp
748 af54ae4a 2019-02-19 stsp free(base_path);
749 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
750 af54ae4a 2019-02-19 stsp err = got_error_from_errno();
751 af54ae4a 2019-02-19 stsp base_path = NULL;
752 af54ae4a 2019-02-19 stsp goto done;
753 af54ae4a 2019-02-19 stsp }
754 af54ae4a 2019-02-19 stsp
755 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob2_path, &f2, base_path);
756 6353ad76 2019-02-08 stsp if (err)
757 6353ad76 2019-02-08 stsp goto done;
758 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie)) {
759 1430b4e0 2019-03-27 stsp struct got_object_id id2;
760 1430b4e0 2019-03-27 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
761 1430b4e0 2019-03-27 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
762 1430b4e0 2019-03-27 stsp if (err)
763 1430b4e0 2019-03-27 stsp goto done;
764 1430b4e0 2019-03-27 stsp err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
765 1430b4e0 2019-03-27 stsp if (err)
766 1430b4e0 2019-03-27 stsp goto done;
767 1430b4e0 2019-03-27 stsp } else {
768 1430b4e0 2019-03-27 stsp /*
769 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
770 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
771 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
772 1430b4e0 2019-03-27 stsp */
773 1430b4e0 2019-03-27 stsp }
774 6353ad76 2019-02-08 stsp
775 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
776 6353ad76 2019-02-08 stsp if (err)
777 6353ad76 2019-02-08 stsp goto done;
778 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
779 6353ad76 2019-02-08 stsp err = got_error_from_errno();
780 6353ad76 2019-02-08 stsp goto done;
781 6353ad76 2019-02-08 stsp }
782 6353ad76 2019-02-08 stsp
783 6353ad76 2019-02-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
784 6353ad76 2019-02-08 stsp blob2_path, ondisk_path, label1, path);
785 6353ad76 2019-02-08 stsp if (err)
786 6353ad76 2019-02-08 stsp goto done;
787 6353ad76 2019-02-08 stsp
788 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
789 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
790 6353ad76 2019-02-08 stsp
791 6353ad76 2019-02-08 stsp
792 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
793 816dc654 2019-02-16 stsp err = got_error_from_errno();
794 816dc654 2019-02-16 stsp goto done;
795 68c76935 2019-02-19 stsp }
796 68c76935 2019-02-19 stsp
797 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
798 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
799 68c76935 2019-02-19 stsp err = check_files_equal(&update_timestamps, blob1_path,
800 68c76935 2019-02-19 stsp merged_path);
801 68c76935 2019-02-19 stsp if (err)
802 68c76935 2019-02-19 stsp goto done;
803 816dc654 2019-02-16 stsp }
804 6353ad76 2019-02-08 stsp
805 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
806 70a0c8ec 2019-02-20 stsp err = got_error_from_errno();
807 70a0c8ec 2019-02-20 stsp goto done;
808 70a0c8ec 2019-02-20 stsp }
809 70a0c8ec 2019-02-20 stsp
810 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
811 6353ad76 2019-02-08 stsp err = got_error_from_errno();
812 2a57020b 2019-02-20 stsp unlink(merged_path);
813 6353ad76 2019-02-08 stsp goto done;
814 6353ad76 2019-02-08 stsp }
815 6353ad76 2019-02-08 stsp
816 02c07007 2019-02-10 stsp /*
817 02c07007 2019-02-10 stsp * Do not update timestamps of already modified files. Otherwise,
818 65ad4e61 2019-02-20 stsp * a future status walk would treat them as unmodified files again.
819 02c07007 2019-02-10 stsp */
820 6353ad76 2019-02-08 stsp err = got_fileindex_entry_update(ie, ondisk_path,
821 68c76935 2019-02-19 stsp blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
822 6353ad76 2019-02-08 stsp done:
823 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
824 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
825 3a6ce05a 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
826 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
827 3a6ce05a 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
828 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
829 6353ad76 2019-02-08 stsp if (blob2)
830 6353ad76 2019-02-08 stsp got_object_blob_close(blob2);
831 6353ad76 2019-02-08 stsp free(merged_path);
832 af54ae4a 2019-02-19 stsp free(base_path);
833 6353ad76 2019-02-08 stsp if (blob1_path) {
834 6353ad76 2019-02-08 stsp unlink(blob1_path);
835 6353ad76 2019-02-08 stsp free(blob1_path);
836 6353ad76 2019-02-08 stsp }
837 6353ad76 2019-02-08 stsp if (blob2_path) {
838 6353ad76 2019-02-08 stsp unlink(blob2_path);
839 6353ad76 2019-02-08 stsp free(blob2_path);
840 6353ad76 2019-02-08 stsp }
841 6353ad76 2019-02-08 stsp free(id_str);
842 6353ad76 2019-02-08 stsp free(label1);
843 4a1ddfc2 2019-01-12 stsp return err;
844 4a1ddfc2 2019-01-12 stsp }
845 4a1ddfc2 2019-01-12 stsp
846 4a1ddfc2 2019-01-12 stsp static const struct got_error *
847 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
848 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
849 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
850 13d9040b 2019-03-27 stsp int update_timestamps)
851 13d9040b 2019-03-27 stsp {
852 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
853 13d9040b 2019-03-27 stsp
854 13d9040b 2019-03-27 stsp if (ie == NULL)
855 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
856 13d9040b 2019-03-27 stsp if (ie)
857 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
858 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
859 13d9040b 2019-03-27 stsp update_timestamps);
860 13d9040b 2019-03-27 stsp else {
861 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
862 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
863 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
864 13d9040b 2019-03-27 stsp if (!err)
865 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
866 13d9040b 2019-03-27 stsp }
867 13d9040b 2019-03-27 stsp return err;
868 13d9040b 2019-03-27 stsp }
869 13d9040b 2019-03-27 stsp
870 13d9040b 2019-03-27 stsp static const struct got_error *
871 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
872 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
873 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
874 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
875 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
876 9d31a1d8 2018-03-11 stsp {
877 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
878 507dc3bb 2018-12-29 stsp int fd = -1;
879 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
880 507dc3bb 2018-12-29 stsp int update = 0;
881 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
882 9d31a1d8 2018-03-11 stsp
883 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
884 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
885 9d31a1d8 2018-03-11 stsp if (fd == -1) {
886 21908da4 2019-01-13 stsp if (errno == ENOENT) {
887 21908da4 2019-01-13 stsp char *parent = dirname(path);
888 21908da4 2019-01-13 stsp if (parent == NULL)
889 21908da4 2019-01-13 stsp return got_error_from_errno();
890 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
891 21908da4 2019-01-13 stsp if (err)
892 21908da4 2019-01-13 stsp return err;
893 21908da4 2019-01-13 stsp fd = open(ondisk_path,
894 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
895 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
896 21908da4 2019-01-13 stsp if (fd == -1)
897 21908da4 2019-01-13 stsp return got_error_from_errno();
898 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
899 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
900 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
901 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
902 507dc3bb 2018-12-29 stsp goto done;
903 d70b8e30 2018-12-27 stsp } else {
904 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
905 507dc3bb 2018-12-29 stsp ondisk_path);
906 507dc3bb 2018-12-29 stsp if (err)
907 507dc3bb 2018-12-29 stsp goto done;
908 507dc3bb 2018-12-29 stsp update = 1;
909 9d31a1d8 2018-03-11 stsp }
910 507dc3bb 2018-12-29 stsp } else
911 7e7c1e4c 2019-01-12 stsp return got_error_from_errno();
912 9d31a1d8 2018-03-11 stsp }
913 9d31a1d8 2018-03-11 stsp
914 a378724f 2019-02-10 stsp if (restoring_missing_file)
915 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
916 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
917 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
918 a378724f 2019-02-10 stsp else
919 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
920 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
921 d7b62c98 2018-12-27 stsp
922 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
923 9d31a1d8 2018-03-11 stsp do {
924 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
925 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
926 9d31a1d8 2018-03-11 stsp if (err)
927 9d31a1d8 2018-03-11 stsp break;
928 9d31a1d8 2018-03-11 stsp if (len > 0) {
929 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
930 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
931 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
932 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
933 b87c6f83 2018-12-24 stsp goto done;
934 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
935 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
936 b87c6f83 2018-12-24 stsp goto done;
937 9d31a1d8 2018-03-11 stsp }
938 61d6eaa3 2018-12-24 stsp hdrlen = 0;
939 9d31a1d8 2018-03-11 stsp }
940 9d31a1d8 2018-03-11 stsp } while (len != 0);
941 9d31a1d8 2018-03-11 stsp
942 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
943 816dc654 2019-02-16 stsp err = got_error_from_errno();
944 816dc654 2019-02-16 stsp goto done;
945 816dc654 2019-02-16 stsp }
946 9d31a1d8 2018-03-11 stsp
947 507dc3bb 2018-12-29 stsp if (update) {
948 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
949 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
950 2a57020b 2019-02-20 stsp unlink(tmppath);
951 68ed9ba5 2019-02-10 stsp goto done;
952 68ed9ba5 2019-02-10 stsp }
953 68ed9ba5 2019-02-10 stsp }
954 ba8a0d4d 2019-02-10 stsp
955 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
956 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
957 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
958 507dc3bb 2018-12-29 stsp goto done;
959 507dc3bb 2018-12-29 stsp }
960 ba8a0d4d 2019-02-10 stsp } else {
961 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
962 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
963 68ed9ba5 2019-02-10 stsp goto done;
964 68ed9ba5 2019-02-10 stsp }
965 507dc3bb 2018-12-29 stsp }
966 507dc3bb 2018-12-29 stsp
967 9d31a1d8 2018-03-11 stsp done:
968 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
969 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
970 507dc3bb 2018-12-29 stsp free(tmppath);
971 6353ad76 2019-02-08 stsp return err;
972 6353ad76 2019-02-08 stsp }
973 6353ad76 2019-02-08 stsp
974 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
975 6353ad76 2019-02-08 stsp static const struct got_error *
976 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
977 7154f6ce 2019-03-27 stsp {
978 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
979 7154f6ce 2019-03-27 stsp const char *markers[3] = {
980 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
981 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
982 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
983 7154f6ce 2019-03-27 stsp };
984 7154f6ce 2019-03-27 stsp int i = 0;
985 7154f6ce 2019-03-27 stsp char *line;
986 7154f6ce 2019-03-27 stsp size_t len;
987 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
988 7154f6ce 2019-03-27 stsp
989 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
990 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
991 7154f6ce 2019-03-27 stsp if (line == NULL) {
992 7154f6ce 2019-03-27 stsp if (feof(f))
993 7154f6ce 2019-03-27 stsp break;
994 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
995 7154f6ce 2019-03-27 stsp break;
996 7154f6ce 2019-03-27 stsp }
997 7154f6ce 2019-03-27 stsp
998 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
999 7154f6ce 2019-03-27 stsp if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
1000 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1001 7154f6ce 2019-03-27 stsp else
1002 7154f6ce 2019-03-27 stsp i++;
1003 7154f6ce 2019-03-27 stsp }
1004 7154f6ce 2019-03-27 stsp }
1005 7154f6ce 2019-03-27 stsp
1006 7154f6ce 2019-03-27 stsp return err;
1007 7154f6ce 2019-03-27 stsp }
1008 7154f6ce 2019-03-27 stsp
1009 7154f6ce 2019-03-27 stsp static const struct got_error *
1010 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1011 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1012 b8f41171 2019-02-10 stsp struct got_repository *repo)
1013 6353ad76 2019-02-08 stsp {
1014 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1015 6353ad76 2019-02-08 stsp struct got_object_id id;
1016 6353ad76 2019-02-08 stsp size_t hdrlen;
1017 6353ad76 2019-02-08 stsp FILE *f = NULL;
1018 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1019 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1020 6353ad76 2019-02-08 stsp size_t flen, blen;
1021 6353ad76 2019-02-08 stsp
1022 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1023 6353ad76 2019-02-08 stsp
1024 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1025 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1026 b8f41171 2019-02-10 stsp if (ie) {
1027 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1028 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1029 2ec1f75b 2019-03-26 stsp else
1030 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1031 b8f41171 2019-02-10 stsp sb->st_mode =
1032 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1033 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1034 b8f41171 2019-02-10 stsp } else
1035 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1036 a378724f 2019-02-10 stsp return NULL;
1037 a378724f 2019-02-10 stsp }
1038 6353ad76 2019-02-08 stsp return got_error_from_errno();
1039 a378724f 2019-02-10 stsp }
1040 6353ad76 2019-02-08 stsp
1041 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1042 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1043 6353ad76 2019-02-08 stsp return NULL;
1044 6353ad76 2019-02-08 stsp }
1045 6353ad76 2019-02-08 stsp
1046 b8f41171 2019-02-10 stsp if (ie == NULL)
1047 d00136be 2019-03-26 stsp return NULL;
1048 d00136be 2019-03-26 stsp
1049 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1050 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1051 2ec1f75b 2019-03-26 stsp return NULL;
1052 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1053 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1054 b8f41171 2019-02-10 stsp return NULL;
1055 d00136be 2019-03-26 stsp }
1056 b8f41171 2019-02-10 stsp
1057 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1058 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1059 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1060 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1061 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1062 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1063 6353ad76 2019-02-08 stsp return NULL;
1064 6353ad76 2019-02-08 stsp
1065 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1066 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1067 6353ad76 2019-02-08 stsp if (err)
1068 6353ad76 2019-02-08 stsp return err;
1069 6353ad76 2019-02-08 stsp
1070 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1071 6353ad76 2019-02-08 stsp if (f == NULL) {
1072 6353ad76 2019-02-08 stsp err = got_error_from_errno();
1073 6353ad76 2019-02-08 stsp goto done;
1074 6353ad76 2019-02-08 stsp }
1075 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1076 6353ad76 2019-02-08 stsp while (1) {
1077 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1078 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1079 6353ad76 2019-02-08 stsp if (err)
1080 7154f6ce 2019-03-27 stsp goto done;
1081 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1082 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1083 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1084 80c5c120 2019-02-19 stsp err = got_error_from_errno();
1085 7154f6ce 2019-03-27 stsp goto done;
1086 80c5c120 2019-02-19 stsp }
1087 6353ad76 2019-02-08 stsp if (blen == 0) {
1088 6353ad76 2019-02-08 stsp if (flen != 0)
1089 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1090 6353ad76 2019-02-08 stsp break;
1091 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1092 6353ad76 2019-02-08 stsp if (blen != 0)
1093 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1094 6353ad76 2019-02-08 stsp break;
1095 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1096 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1097 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1098 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1099 6353ad76 2019-02-08 stsp break;
1100 6353ad76 2019-02-08 stsp }
1101 6353ad76 2019-02-08 stsp } else {
1102 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1103 6353ad76 2019-02-08 stsp break;
1104 6353ad76 2019-02-08 stsp }
1105 6353ad76 2019-02-08 stsp hdrlen = 0;
1106 6353ad76 2019-02-08 stsp }
1107 7154f6ce 2019-03-27 stsp
1108 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1109 7154f6ce 2019-03-27 stsp rewind(f);
1110 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1111 7154f6ce 2019-03-27 stsp }
1112 6353ad76 2019-02-08 stsp done:
1113 6353ad76 2019-02-08 stsp if (blob)
1114 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1115 6353ad76 2019-02-08 stsp if (f)
1116 6353ad76 2019-02-08 stsp fclose(f);
1117 9d31a1d8 2018-03-11 stsp return err;
1118 9d31a1d8 2018-03-11 stsp }
1119 9d31a1d8 2018-03-11 stsp
1120 9d31a1d8 2018-03-11 stsp static const struct got_error *
1121 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1122 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1123 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1124 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1125 8da9e5f4 2019-01-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1126 9d31a1d8 2018-03-11 stsp {
1127 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1128 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1129 6353ad76 2019-02-08 stsp char *ondisk_path;
1130 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1131 b8f41171 2019-02-10 stsp struct stat sb;
1132 9d31a1d8 2018-03-11 stsp
1133 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1134 6353ad76 2019-02-08 stsp return got_error_from_errno();
1135 6353ad76 2019-02-08 stsp
1136 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1137 b8f41171 2019-02-10 stsp if (err)
1138 b8f41171 2019-02-10 stsp goto done;
1139 6353ad76 2019-02-08 stsp
1140 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1141 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1142 b8f41171 2019-02-10 stsp goto done;
1143 b8f41171 2019-02-10 stsp }
1144 b8f41171 2019-02-10 stsp
1145 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1146 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1147 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1148 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1149 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1150 b8f41171 2019-02-10 stsp path);
1151 6353ad76 2019-02-08 stsp goto done;
1152 a378724f 2019-02-10 stsp }
1153 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1154 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1155 1430b4e0 2019-03-27 stsp SHA1_DIGEST_LENGTH) == 0)
1156 b8f41171 2019-02-10 stsp goto done;
1157 9d31a1d8 2018-03-11 stsp }
1158 9d31a1d8 2018-03-11 stsp
1159 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1160 8da9e5f4 2019-01-12 stsp if (err)
1161 6353ad76 2019-02-08 stsp goto done;
1162 512f0d0e 2019-01-02 stsp
1163 1430b4e0 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1164 6353ad76 2019-02-08 stsp err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1165 b8f41171 2019-02-10 stsp te->mode, sb.st_mode, blob, repo, progress_cb,
1166 b8f41171 2019-02-10 stsp progress_arg);
1167 13d9040b 2019-03-27 stsp else if (status == GOT_STATUS_DELETE) {
1168 13d9040b 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1169 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1170 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1171 13d9040b 2019-03-27 stsp if (err)
1172 13d9040b 2019-03-27 stsp goto done;
1173 13d9040b 2019-03-27 stsp } else {
1174 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1175 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1176 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1177 13d9040b 2019-03-27 stsp if (err)
1178 13d9040b 2019-03-27 stsp goto done;
1179 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1180 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1181 13d9040b 2019-03-27 stsp if (err)
1182 13d9040b 2019-03-27 stsp goto done;
1183 13d9040b 2019-03-27 stsp }
1184 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1185 6353ad76 2019-02-08 stsp done:
1186 6353ad76 2019-02-08 stsp free(ondisk_path);
1187 8da9e5f4 2019-01-12 stsp return err;
1188 90285c3b 2019-01-08 stsp }
1189 90285c3b 2019-01-08 stsp
1190 90285c3b 2019-01-08 stsp static const struct got_error *
1191 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1192 90285c3b 2019-01-08 stsp {
1193 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1194 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1195 90285c3b 2019-01-08 stsp
1196 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1197 90285c3b 2019-01-08 stsp return got_error_from_errno();
1198 90285c3b 2019-01-08 stsp
1199 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1200 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1201 c97665ae 2019-01-13 stsp err = got_error_from_errno();
1202 c97665ae 2019-01-13 stsp } else {
1203 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1204 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1205 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1206 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1207 26c4ac4d 2019-01-08 stsp err = got_error_from_errno();
1208 90285c3b 2019-01-08 stsp break;
1209 90285c3b 2019-01-08 stsp }
1210 90285c3b 2019-01-08 stsp parent = dirname(parent);
1211 90285c3b 2019-01-08 stsp }
1212 90285c3b 2019-01-08 stsp }
1213 90285c3b 2019-01-08 stsp free(ondisk_path);
1214 90285c3b 2019-01-08 stsp return err;
1215 512f0d0e 2019-01-02 stsp }
1216 512f0d0e 2019-01-02 stsp
1217 708d8e67 2019-03-27 stsp static const struct got_error *
1218 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1219 708d8e67 2019-03-27 stsp struct got_fileindex_entry *ie, const char *parent_path,
1220 708d8e67 2019-03-27 stsp struct got_repository *repo,
1221 708d8e67 2019-03-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1222 708d8e67 2019-03-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1223 708d8e67 2019-03-27 stsp {
1224 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1225 708d8e67 2019-03-27 stsp unsigned char status;
1226 708d8e67 2019-03-27 stsp struct stat sb;
1227 708d8e67 2019-03-27 stsp char *ondisk_path;
1228 708d8e67 2019-03-27 stsp
1229 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1230 708d8e67 2019-03-27 stsp == -1)
1231 708d8e67 2019-03-27 stsp return got_error_from_errno();
1232 708d8e67 2019-03-27 stsp
1233 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1234 708d8e67 2019-03-27 stsp if (err)
1235 708d8e67 2019-03-27 stsp return err;
1236 708d8e67 2019-03-27 stsp
1237 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1238 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1239 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1240 fc6346c4 2019-03-27 stsp /*
1241 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1242 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1243 fc6346c4 2019-03-27 stsp */
1244 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1245 fc6346c4 2019-03-27 stsp 0);
1246 708d8e67 2019-03-27 stsp if (err)
1247 708d8e67 2019-03-27 stsp return err;
1248 fc6346c4 2019-03-27 stsp } else {
1249 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1250 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1251 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1252 fc6346c4 2019-03-27 stsp if (err)
1253 fc6346c4 2019-03-27 stsp return err;
1254 fc6346c4 2019-03-27 stsp }
1255 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1256 708d8e67 2019-03-27 stsp }
1257 708d8e67 2019-03-27 stsp
1258 fc6346c4 2019-03-27 stsp return err;
1259 708d8e67 2019-03-27 stsp }
1260 708d8e67 2019-03-27 stsp
1261 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1262 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1263 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1264 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1265 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1266 8da9e5f4 2019-01-12 stsp void *progress_arg;
1267 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1268 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1269 8da9e5f4 2019-01-12 stsp };
1270 8da9e5f4 2019-01-12 stsp
1271 512f0d0e 2019-01-02 stsp static const struct got_error *
1272 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1273 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1274 512f0d0e 2019-01-02 stsp {
1275 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1276 512f0d0e 2019-01-02 stsp
1277 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1278 8da9e5f4 2019-01-12 stsp ie->path, a->repo, a->progress_cb, a->progress_arg,
1279 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1280 8da9e5f4 2019-01-12 stsp }
1281 512f0d0e 2019-01-02 stsp
1282 8da9e5f4 2019-01-12 stsp static const struct got_error *
1283 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1284 8da9e5f4 2019-01-12 stsp {
1285 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1286 7a9df742 2019-01-08 stsp
1287 708d8e67 2019-03-27 stsp return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1288 708d8e67 2019-03-27 stsp a->repo, a->progress_cb, a->progress_arg,
1289 708d8e67 2019-03-27 stsp a->cancel_cb, a->cancel_arg);
1290 9d31a1d8 2018-03-11 stsp }
1291 9d31a1d8 2018-03-11 stsp
1292 9d31a1d8 2018-03-11 stsp static const struct got_error *
1293 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1294 9d31a1d8 2018-03-11 stsp {
1295 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1296 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1297 8da9e5f4 2019-01-12 stsp char *path;
1298 9d31a1d8 2018-03-11 stsp
1299 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1300 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1301 8da9e5f4 2019-01-12 stsp == -1)
1302 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
1303 9d31a1d8 2018-03-11 stsp
1304 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1305 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1306 8da9e5f4 2019-01-12 stsp else
1307 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1308 8da9e5f4 2019-01-12 stsp a->repo, a->progress_cb, a->progress_arg,
1309 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1310 9d31a1d8 2018-03-11 stsp
1311 8da9e5f4 2019-01-12 stsp free(path);
1312 0cd1c46a 2019-03-11 stsp return err;
1313 0cd1c46a 2019-03-11 stsp }
1314 0cd1c46a 2019-03-11 stsp
1315 517bab73 2019-03-11 stsp const struct got_error *
1316 517bab73 2019-03-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1317 0cd1c46a 2019-03-11 stsp {
1318 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1319 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1320 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1321 0cd1c46a 2019-03-11 stsp
1322 517bab73 2019-03-11 stsp *refname = NULL;
1323 517bab73 2019-03-11 stsp
1324 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1325 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1326 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1327 0cd1c46a 2019-03-11 stsp
1328 0647c563 2019-03-11 stsp if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1329 0647c563 2019-03-11 stsp == -1) {
1330 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
1331 0647c563 2019-03-11 stsp *refname = NULL;
1332 0cd1c46a 2019-03-11 stsp }
1333 517bab73 2019-03-11 stsp free(uuidstr);
1334 517bab73 2019-03-11 stsp return err;
1335 517bab73 2019-03-11 stsp }
1336 517bab73 2019-03-11 stsp
1337 517bab73 2019-03-11 stsp /*
1338 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1339 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1340 517bab73 2019-03-11 stsp */
1341 517bab73 2019-03-11 stsp static const struct got_error *
1342 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1343 517bab73 2019-03-11 stsp {
1344 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1345 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1346 517bab73 2019-03-11 stsp char *refname;
1347 517bab73 2019-03-11 stsp
1348 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1349 517bab73 2019-03-11 stsp if (err)
1350 517bab73 2019-03-11 stsp return err;
1351 0cd1c46a 2019-03-11 stsp
1352 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1353 0cd1c46a 2019-03-11 stsp if (err)
1354 0cd1c46a 2019-03-11 stsp goto done;
1355 0cd1c46a 2019-03-11 stsp
1356 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1357 0cd1c46a 2019-03-11 stsp done:
1358 0cd1c46a 2019-03-11 stsp free(refname);
1359 0cd1c46a 2019-03-11 stsp if (ref)
1360 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1361 9d31a1d8 2018-03-11 stsp return err;
1362 9d31a1d8 2018-03-11 stsp }
1363 9d31a1d8 2018-03-11 stsp
1364 0cd1c46a 2019-03-11 stsp
1365 9d31a1d8 2018-03-11 stsp const struct got_error *
1366 c4cdcb68 2019-04-03 stsp got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1367 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1368 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1369 9d31a1d8 2018-03-11 stsp {
1370 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1371 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1372 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1373 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1374 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
1375 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1376 51514078 2018-12-25 stsp FILE *index = NULL, *new_index = NULL;
1377 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1378 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1379 c4cdcb68 2019-04-03 stsp char *relpath = NULL, *entry_name = NULL;
1380 9d31a1d8 2018-03-11 stsp
1381 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1382 9d31a1d8 2018-03-11 stsp if (err)
1383 9d31a1d8 2018-03-11 stsp return err;
1384 93a30277 2018-12-24 stsp
1385 133d2798 2019-01-08 stsp fileindex = got_fileindex_alloc();
1386 133d2798 2019-01-08 stsp if (fileindex == NULL) {
1387 133d2798 2019-01-08 stsp err = got_error_from_errno();
1388 9d31a1d8 2018-03-11 stsp goto done;
1389 133d2798 2019-01-08 stsp }
1390 9d31a1d8 2018-03-11 stsp
1391 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1392 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1393 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1394 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
1395 9d31a1d8 2018-03-11 stsp goto done;
1396 9d31a1d8 2018-03-11 stsp }
1397 9d31a1d8 2018-03-11 stsp
1398 51514078 2018-12-25 stsp /*
1399 51514078 2018-12-25 stsp * Read the file index.
1400 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1401 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1402 51514078 2018-12-25 stsp */
1403 51514078 2018-12-25 stsp index = fopen(fileindex_path, "rb");
1404 51514078 2018-12-25 stsp if (index == NULL) {
1405 cf61d818 2019-01-12 stsp if (errno != ENOENT) {
1406 cf61d818 2019-01-12 stsp err = got_error_from_errno();
1407 cf61d818 2019-01-12 stsp goto done;
1408 cf61d818 2019-01-12 stsp }
1409 cf61d818 2019-01-12 stsp } else {
1410 cf61d818 2019-01-12 stsp err = got_fileindex_read(fileindex, index);
1411 cf61d818 2019-01-12 stsp fclose(index);
1412 cf61d818 2019-01-12 stsp if (err)
1413 cf61d818 2019-01-12 stsp goto done;
1414 51514078 2018-12-25 stsp }
1415 51514078 2018-12-25 stsp
1416 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1417 b8bdcc21 2018-12-24 stsp fileindex_path);
1418 51664889 2018-03-12 stsp if (err)
1419 51664889 2018-03-12 stsp goto done;
1420 51664889 2018-03-12 stsp
1421 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1422 0cd1c46a 2019-03-11 stsp if (err)
1423 0cd1c46a 2019-03-11 stsp goto done;
1424 0cd1c46a 2019-03-11 stsp
1425 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1426 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1427 9d31a1d8 2018-03-11 stsp if (err)
1428 9d31a1d8 2018-03-11 stsp goto done;
1429 9d31a1d8 2018-03-11 stsp
1430 c4cdcb68 2019-04-03 stsp if (path[0]) {
1431 c4cdcb68 2019-04-03 stsp char *tree_path;
1432 c4cdcb68 2019-04-03 stsp int obj_type;
1433 c4cdcb68 2019-04-03 stsp relpath = strdup(path);
1434 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1435 c4cdcb68 2019-04-03 stsp err = got_error_from_errno();
1436 c4cdcb68 2019-04-03 stsp goto done;
1437 c4cdcb68 2019-04-03 stsp }
1438 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1439 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1440 c4cdcb68 2019-04-03 stsp path) == -1) {
1441 c4cdcb68 2019-04-03 stsp err = got_error_from_errno();
1442 c4cdcb68 2019-04-03 stsp goto done;
1443 c4cdcb68 2019-04-03 stsp }
1444 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1445 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1446 c4cdcb68 2019-04-03 stsp free(tree_path);
1447 c4cdcb68 2019-04-03 stsp if (err)
1448 c4cdcb68 2019-04-03 stsp goto done;
1449 c4cdcb68 2019-04-03 stsp err = got_object_get_type(&obj_type, repo, tree_id);
1450 c4cdcb68 2019-04-03 stsp if (err)
1451 c4cdcb68 2019-04-03 stsp goto done;
1452 c4cdcb68 2019-04-03 stsp if (obj_type == GOT_OBJ_TYPE_BLOB) {
1453 c4cdcb68 2019-04-03 stsp /* Split provided path into parent dir + entry name. */
1454 c4cdcb68 2019-04-03 stsp if (strchr(path, '/') == NULL) {
1455 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1456 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1457 c4cdcb68 2019-04-03 stsp err = got_error_from_errno();
1458 c4cdcb68 2019-04-03 stsp goto done;
1459 c4cdcb68 2019-04-03 stsp }
1460 c4cdcb68 2019-04-03 stsp tree_path = strdup(worktree->path_prefix);
1461 c4cdcb68 2019-04-03 stsp if (tree_path == NULL) {
1462 c4cdcb68 2019-04-03 stsp err = got_error_from_errno();
1463 c4cdcb68 2019-04-03 stsp goto done;
1464 c4cdcb68 2019-04-03 stsp }
1465 c4cdcb68 2019-04-03 stsp } else {
1466 c4cdcb68 2019-04-03 stsp err = got_path_dirname(&relpath, path);
1467 c4cdcb68 2019-04-03 stsp if (err)
1468 c4cdcb68 2019-04-03 stsp goto done;
1469 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s",
1470 c4cdcb68 2019-04-03 stsp worktree->path_prefix,
1471 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(
1472 c4cdcb68 2019-04-03 stsp worktree->path_prefix) ? "" : "/",
1473 c4cdcb68 2019-04-03 stsp relpath) == -1) {
1474 c4cdcb68 2019-04-03 stsp err = got_error_from_errno();
1475 c4cdcb68 2019-04-03 stsp goto done;
1476 c4cdcb68 2019-04-03 stsp }
1477 c4cdcb68 2019-04-03 stsp }
1478 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1479 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1480 c4cdcb68 2019-04-03 stsp free(tree_path);
1481 c4cdcb68 2019-04-03 stsp if (err)
1482 c4cdcb68 2019-04-03 stsp goto done;
1483 c4cdcb68 2019-04-03 stsp entry_name = basename(path);
1484 c4cdcb68 2019-04-03 stsp if (entry_name == NULL) {
1485 c4cdcb68 2019-04-03 stsp err = got_error_from_errno();
1486 c4cdcb68 2019-04-03 stsp goto done;
1487 c4cdcb68 2019-04-03 stsp }
1488 c4cdcb68 2019-04-03 stsp }
1489 c4cdcb68 2019-04-03 stsp } else {
1490 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1491 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1492 c4cdcb68 2019-04-03 stsp err = got_error_from_errno();
1493 c4cdcb68 2019-04-03 stsp goto done;
1494 c4cdcb68 2019-04-03 stsp }
1495 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1496 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, worktree->path_prefix);
1497 c4cdcb68 2019-04-03 stsp if (err)
1498 c4cdcb68 2019-04-03 stsp goto done;
1499 c4cdcb68 2019-04-03 stsp }
1500 9d31a1d8 2018-03-11 stsp
1501 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1502 8da9e5f4 2019-01-12 stsp if (err)
1503 c4cdcb68 2019-04-03 stsp goto done;
1504 c4cdcb68 2019-04-03 stsp
1505 c4cdcb68 2019-04-03 stsp if (entry_name &&
1506 c4cdcb68 2019-04-03 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1507 c4cdcb68 2019-04-03 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1508 8da9e5f4 2019-01-12 stsp goto done;
1509 c4cdcb68 2019-04-03 stsp }
1510 9d31a1d8 2018-03-11 stsp
1511 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1512 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1513 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1514 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1515 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1516 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1517 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1518 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1519 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1520 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1521 c4cdcb68 2019-04-03 stsp checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1522 c4cdcb68 2019-04-03 stsp entry_name, repo, &diff_cb, &arg);
1523 8da9e5f4 2019-01-12 stsp
1524 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
1525 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
1526 9d31a1d8 2018-03-11 stsp if (err)
1527 9d31a1d8 2018-03-11 stsp goto done;
1528 9d31a1d8 2018-03-11 stsp
1529 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1530 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
1531 2a57020b 2019-02-20 stsp unlink(new_fileindex_path);
1532 9d31a1d8 2018-03-11 stsp goto done;
1533 9d31a1d8 2018-03-11 stsp }
1534 9d31a1d8 2018-03-11 stsp
1535 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1536 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
1537 9d31a1d8 2018-03-11 stsp
1538 9d31a1d8 2018-03-11 stsp done:
1539 c4cdcb68 2019-04-03 stsp free(relpath);
1540 edfa7d7f 2018-09-11 stsp if (tree)
1541 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1542 9d31a1d8 2018-03-11 stsp if (commit)
1543 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1544 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
1545 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
1546 b8bdcc21 2018-12-24 stsp if (new_index)
1547 b8bdcc21 2018-12-24 stsp fclose(new_index);
1548 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1549 9d31a1d8 2018-03-11 stsp free(fileindex_path);
1550 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1551 a143fb78 2018-12-25 stsp if (checkout_err)
1552 a143fb78 2018-12-25 stsp err = checkout_err;
1553 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1554 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1555 9d31a1d8 2018-03-11 stsp err = unlockerr;
1556 f8d1f275 2019-02-04 stsp return err;
1557 f8d1f275 2019-02-04 stsp }
1558 f8d1f275 2019-02-04 stsp
1559 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1560 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1561 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1562 927df6b7 2019-02-10 stsp const char *status_path;
1563 927df6b7 2019-02-10 stsp size_t status_path_len;
1564 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1565 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1566 f8d1f275 2019-02-04 stsp void *status_arg;
1567 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1568 f8d1f275 2019-02-04 stsp void *cancel_arg;
1569 f8d1f275 2019-02-04 stsp };
1570 f8d1f275 2019-02-04 stsp
1571 f8d1f275 2019-02-04 stsp static const struct got_error *
1572 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1573 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1574 927df6b7 2019-02-10 stsp struct got_repository *repo)
1575 927df6b7 2019-02-10 stsp {
1576 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1577 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1578 927df6b7 2019-02-10 stsp struct stat sb;
1579 927df6b7 2019-02-10 stsp struct got_object_id id;
1580 927df6b7 2019-02-10 stsp
1581 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1582 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1583 927df6b7 2019-02-10 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1584 927df6b7 2019-02-10 stsp err = (*status_cb)(status_arg, status, ie->path, &id);
1585 927df6b7 2019-02-10 stsp }
1586 927df6b7 2019-02-10 stsp return err;
1587 927df6b7 2019-02-10 stsp }
1588 927df6b7 2019-02-10 stsp
1589 927df6b7 2019-02-10 stsp static const struct got_error *
1590 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1591 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1592 f8d1f275 2019-02-04 stsp {
1593 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1594 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1595 f8d1f275 2019-02-04 stsp char *abspath;
1596 f8d1f275 2019-02-04 stsp
1597 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1598 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1599 927df6b7 2019-02-10 stsp return NULL;
1600 927df6b7 2019-02-10 stsp
1601 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1602 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1603 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1604 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1605 f8d1f275 2019-02-04 stsp } else {
1606 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1607 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1608 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1609 f8d1f275 2019-02-04 stsp }
1610 f8d1f275 2019-02-04 stsp
1611 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1612 927df6b7 2019-02-10 stsp a->repo);
1613 f8d1f275 2019-02-04 stsp free(abspath);
1614 9d31a1d8 2018-03-11 stsp return err;
1615 9d31a1d8 2018-03-11 stsp }
1616 f8d1f275 2019-02-04 stsp
1617 f8d1f275 2019-02-04 stsp static const struct got_error *
1618 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1619 f8d1f275 2019-02-04 stsp {
1620 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1621 b72f483a 2019-02-05 stsp struct got_object_id id;
1622 2ec1f75b 2019-03-26 stsp unsigned char status;
1623 927df6b7 2019-02-10 stsp
1624 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1625 927df6b7 2019-02-10 stsp return NULL;
1626 927df6b7 2019-02-10 stsp
1627 b72f483a 2019-02-05 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1628 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1629 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
1630 2ec1f75b 2019-03-26 stsp else
1631 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
1632 2ec1f75b 2019-03-26 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1633 f8d1f275 2019-02-04 stsp }
1634 f8d1f275 2019-02-04 stsp
1635 f8d1f275 2019-02-04 stsp static const struct got_error *
1636 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
1637 f8d1f275 2019-02-04 stsp {
1638 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1639 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1640 f8d1f275 2019-02-04 stsp char *path = NULL;
1641 f8d1f275 2019-02-04 stsp
1642 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
1643 2c201a36 2019-02-10 stsp return NULL;
1644 2c201a36 2019-02-10 stsp
1645 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
1646 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
1647 f8d1f275 2019-02-04 stsp return NULL;
1648 f8d1f275 2019-02-04 stsp
1649 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1650 927df6b7 2019-02-10 stsp return NULL;
1651 927df6b7 2019-02-10 stsp
1652 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1653 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1654 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1655 f8d1f275 2019-02-04 stsp } else {
1656 f8d1f275 2019-02-04 stsp path = de->d_name;
1657 f8d1f275 2019-02-04 stsp }
1658 f8d1f275 2019-02-04 stsp
1659 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1660 b72f483a 2019-02-05 stsp NULL);
1661 f8d1f275 2019-02-04 stsp if (parent_path[0])
1662 f8d1f275 2019-02-04 stsp free(path);
1663 b72f483a 2019-02-05 stsp return err;
1664 f8d1f275 2019-02-04 stsp }
1665 f8d1f275 2019-02-04 stsp
1666 f8d1f275 2019-02-04 stsp const struct got_error *
1667 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
1668 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
1669 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1670 f8d1f275 2019-02-04 stsp {
1671 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1672 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
1673 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
1674 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
1675 f8d1f275 2019-02-04 stsp FILE *index = NULL;
1676 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
1677 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
1678 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
1679 f8d1f275 2019-02-04 stsp
1680 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
1681 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
1682 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1683 f8d1f275 2019-02-04 stsp goto done;
1684 f8d1f275 2019-02-04 stsp }
1685 f8d1f275 2019-02-04 stsp
1686 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1687 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1688 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1689 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
1690 f8d1f275 2019-02-04 stsp goto done;
1691 f8d1f275 2019-02-04 stsp }
1692 f8d1f275 2019-02-04 stsp
1693 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
1694 f8d1f275 2019-02-04 stsp if (index == NULL) {
1695 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
1696 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1697 f8d1f275 2019-02-04 stsp goto done;
1698 f8d1f275 2019-02-04 stsp }
1699 f8d1f275 2019-02-04 stsp } else {
1700 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
1701 f8d1f275 2019-02-04 stsp fclose(index);
1702 f8d1f275 2019-02-04 stsp if (err)
1703 f8d1f275 2019-02-04 stsp goto done;
1704 f8d1f275 2019-02-04 stsp }
1705 f8d1f275 2019-02-04 stsp
1706 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
1707 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
1708 c513d110 2019-02-05 stsp err = got_error_from_errno();
1709 c513d110 2019-02-05 stsp goto done;
1710 c513d110 2019-02-05 stsp }
1711 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
1712 927df6b7 2019-02-10 stsp if (workdir == NULL) {
1713 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
1714 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
1715 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
1716 927df6b7 2019-02-10 stsp if (ie == NULL) {
1717 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
1718 927df6b7 2019-02-10 stsp goto done;
1719 927df6b7 2019-02-10 stsp }
1720 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
1721 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
1722 927df6b7 2019-02-10 stsp goto done;
1723 927df6b7 2019-02-10 stsp } else {
1724 927df6b7 2019-02-10 stsp err = got_error_from_errno();
1725 927df6b7 2019-02-10 stsp goto done;
1726 927df6b7 2019-02-10 stsp }
1727 927df6b7 2019-02-10 stsp }
1728 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
1729 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
1730 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
1731 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
1732 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
1733 927df6b7 2019-02-10 stsp arg.status_path = path;
1734 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
1735 f8d1f275 2019-02-04 stsp arg.repo = repo;
1736 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
1737 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
1738 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
1739 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
1740 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1741 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
1742 f8d1f275 2019-02-04 stsp done:
1743 f8d1f275 2019-02-04 stsp if (workdir)
1744 f8d1f275 2019-02-04 stsp closedir(workdir);
1745 927df6b7 2019-02-10 stsp free(ondisk_path);
1746 f8d1f275 2019-02-04 stsp free(fileindex_path);
1747 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
1748 f8d1f275 2019-02-04 stsp return err;
1749 f8d1f275 2019-02-04 stsp }
1750 6c7ab921 2019-03-18 stsp
1751 6c7ab921 2019-03-18 stsp const struct got_error *
1752 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1753 6c7ab921 2019-03-18 stsp const char *arg)
1754 6c7ab921 2019-03-18 stsp {
1755 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
1756 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
1757 6c7ab921 2019-03-18 stsp size_t len;
1758 6c7ab921 2019-03-18 stsp
1759 6c7ab921 2019-03-18 stsp *wt_path = NULL;
1760 6c7ab921 2019-03-18 stsp
1761 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
1762 6c7ab921 2019-03-18 stsp if (resolved == NULL)
1763 6c7ab921 2019-03-18 stsp return got_error_from_errno();
1764 6c7ab921 2019-03-18 stsp
1765 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
1766 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
1767 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
1768 6c7ab921 2019-03-18 stsp goto done;
1769 6c7ab921 2019-03-18 stsp }
1770 6c7ab921 2019-03-18 stsp
1771 c4cdcb68 2019-04-03 stsp path = strdup(resolved +
1772 c4cdcb68 2019-04-03 stsp strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1773 6c7ab921 2019-03-18 stsp if (path == NULL) {
1774 6c7ab921 2019-03-18 stsp err = got_error_from_errno();
1775 6c7ab921 2019-03-18 stsp goto done;
1776 6c7ab921 2019-03-18 stsp }
1777 6c7ab921 2019-03-18 stsp
1778 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
1779 6c7ab921 2019-03-18 stsp len = strlen(path);
1780 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
1781 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
1782 6c7ab921 2019-03-18 stsp len--;
1783 6c7ab921 2019-03-18 stsp }
1784 6c7ab921 2019-03-18 stsp done:
1785 6c7ab921 2019-03-18 stsp free(resolved);
1786 6c7ab921 2019-03-18 stsp if (err == NULL)
1787 6c7ab921 2019-03-18 stsp *wt_path = path;
1788 6c7ab921 2019-03-18 stsp else
1789 6c7ab921 2019-03-18 stsp free(path);
1790 d00136be 2019-03-26 stsp return err;
1791 d00136be 2019-03-26 stsp }
1792 d00136be 2019-03-26 stsp
1793 d00136be 2019-03-26 stsp const struct got_error *
1794 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
1795 031a5338 2019-03-26 stsp const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1796 031a5338 2019-03-26 stsp struct got_repository *repo)
1797 d00136be 2019-03-26 stsp {
1798 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
1799 d00136be 2019-03-26 stsp struct got_fileindex_entry *ie = NULL;
1800 031a5338 2019-03-26 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1801 d00136be 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
1802 d00136be 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
1803 031a5338 2019-03-26 stsp int ie_added = 0;
1804 d00136be 2019-03-26 stsp
1805 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
1806 d00136be 2019-03-26 stsp if (err)
1807 d00136be 2019-03-26 stsp return err;
1808 d00136be 2019-03-26 stsp
1809 031a5338 2019-03-26 stsp err = got_path_skip_common_ancestor(&relpath,
1810 d00136be 2019-03-26 stsp got_worktree_get_root_path(worktree), ondisk_path);
1811 d00136be 2019-03-26 stsp if (err)
1812 d00136be 2019-03-26 stsp goto done;
1813 d00136be 2019-03-26 stsp
1814 d00136be 2019-03-26 stsp fileindex = got_fileindex_alloc();
1815 d00136be 2019-03-26 stsp if (fileindex == NULL) {
1816 d00136be 2019-03-26 stsp err = got_error_from_errno();
1817 d00136be 2019-03-26 stsp goto done;
1818 d00136be 2019-03-26 stsp }
1819 d00136be 2019-03-26 stsp
1820 d00136be 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1821 d00136be 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1822 d00136be 2019-03-26 stsp err = got_error_from_errno();
1823 d00136be 2019-03-26 stsp fileindex_path = NULL;
1824 d00136be 2019-03-26 stsp goto done;
1825 d00136be 2019-03-26 stsp }
1826 d00136be 2019-03-26 stsp
1827 d00136be 2019-03-26 stsp index = fopen(fileindex_path, "rb");
1828 d00136be 2019-03-26 stsp if (index == NULL) {
1829 d00136be 2019-03-26 stsp err = got_error_from_errno();
1830 d00136be 2019-03-26 stsp goto done;
1831 d00136be 2019-03-26 stsp }
1832 d00136be 2019-03-26 stsp
1833 d00136be 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
1834 d00136be 2019-03-26 stsp if (err)
1835 d00136be 2019-03-26 stsp goto done;
1836 d00136be 2019-03-26 stsp
1837 5c99ca9f 2019-03-27 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1838 5c99ca9f 2019-03-27 stsp err = got_error_set_errno(EEXIST);
1839 5c99ca9f 2019-03-27 stsp goto done;
1840 5c99ca9f 2019-03-27 stsp }
1841 5c99ca9f 2019-03-27 stsp
1842 5c99ca9f 2019-03-27 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1843 5c99ca9f 2019-03-27 stsp if (err)
1844 5c99ca9f 2019-03-27 stsp goto done;
1845 5c99ca9f 2019-03-27 stsp
1846 d00136be 2019-03-26 stsp err = got_fileindex_entry_add(fileindex, ie);
1847 d00136be 2019-03-26 stsp if (err)
1848 d00136be 2019-03-26 stsp goto done;
1849 031a5338 2019-03-26 stsp ie_added = 1; /* now owned by fileindex; don't free separately */
1850 d00136be 2019-03-26 stsp
1851 d00136be 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1852 d00136be 2019-03-26 stsp fileindex_path);
1853 d00136be 2019-03-26 stsp if (err)
1854 d00136be 2019-03-26 stsp goto done;
1855 d00136be 2019-03-26 stsp
1856 d00136be 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
1857 d00136be 2019-03-26 stsp if (err)
1858 d00136be 2019-03-26 stsp goto done;
1859 d00136be 2019-03-26 stsp
1860 d00136be 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1861 d00136be 2019-03-26 stsp err = got_error_from_errno();
1862 d00136be 2019-03-26 stsp goto done;
1863 d00136be 2019-03-26 stsp }
1864 d00136be 2019-03-26 stsp
1865 d00136be 2019-03-26 stsp free(new_fileindex_path);
1866 d00136be 2019-03-26 stsp new_fileindex_path = NULL;
1867 031a5338 2019-03-26 stsp
1868 031a5338 2019-03-26 stsp err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1869 d00136be 2019-03-26 stsp done:
1870 d00136be 2019-03-26 stsp if (index) {
1871 d00136be 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
1872 d00136be 2019-03-26 stsp err = got_error_from_errno();
1873 d00136be 2019-03-26 stsp }
1874 d00136be 2019-03-26 stsp if (new_fileindex_path) {
1875 d00136be 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
1876 d00136be 2019-03-26 stsp err = got_error_from_errno();
1877 d00136be 2019-03-26 stsp free(new_fileindex_path);
1878 d00136be 2019-03-26 stsp }
1879 5c99ca9f 2019-03-27 stsp if (ie && !ie_added)
1880 d00136be 2019-03-26 stsp got_fileindex_entry_free(ie);
1881 d00136be 2019-03-26 stsp if (fileindex)
1882 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
1883 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1884 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
1885 d00136be 2019-03-26 stsp err = unlockerr;
1886 031a5338 2019-03-26 stsp free(relpath);
1887 6c7ab921 2019-03-18 stsp return err;
1888 6c7ab921 2019-03-18 stsp }
1889 2ec1f75b 2019-03-26 stsp
1890 2ec1f75b 2019-03-26 stsp const struct got_error *
1891 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
1892 2ec1f75b 2019-03-26 stsp const char *ondisk_path, int delete_local_mods,
1893 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
1894 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
1895 2ec1f75b 2019-03-26 stsp {
1896 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
1897 2ec1f75b 2019-03-26 stsp struct got_fileindex_entry *ie = NULL;
1898 2ec1f75b 2019-03-26 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1899 2ec1f75b 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
1900 2ec1f75b 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
1901 2ec1f75b 2019-03-26 stsp unsigned char status;
1902 2ec1f75b 2019-03-26 stsp struct stat sb;
1903 2ec1f75b 2019-03-26 stsp
1904 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
1905 2ec1f75b 2019-03-26 stsp if (err)
1906 2ec1f75b 2019-03-26 stsp return err;
1907 2ec1f75b 2019-03-26 stsp
1908 2ec1f75b 2019-03-26 stsp err = got_path_skip_common_ancestor(&relpath,
1909 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(worktree), ondisk_path);
1910 2ec1f75b 2019-03-26 stsp if (err)
1911 2ec1f75b 2019-03-26 stsp goto done;
1912 2ec1f75b 2019-03-26 stsp
1913 2ec1f75b 2019-03-26 stsp fileindex = got_fileindex_alloc();
1914 2ec1f75b 2019-03-26 stsp if (fileindex == NULL) {
1915 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1916 2ec1f75b 2019-03-26 stsp goto done;
1917 2ec1f75b 2019-03-26 stsp }
1918 2ec1f75b 2019-03-26 stsp
1919 2ec1f75b 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1920 2ec1f75b 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1921 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1922 2ec1f75b 2019-03-26 stsp fileindex_path = NULL;
1923 2ec1f75b 2019-03-26 stsp goto done;
1924 2ec1f75b 2019-03-26 stsp }
1925 2ec1f75b 2019-03-26 stsp
1926 2ec1f75b 2019-03-26 stsp index = fopen(fileindex_path, "rb");
1927 2ec1f75b 2019-03-26 stsp if (index == NULL) {
1928 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1929 2ec1f75b 2019-03-26 stsp goto done;
1930 2ec1f75b 2019-03-26 stsp }
1931 2ec1f75b 2019-03-26 stsp
1932 2ec1f75b 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
1933 2ec1f75b 2019-03-26 stsp if (err)
1934 2ec1f75b 2019-03-26 stsp goto done;
1935 2ec1f75b 2019-03-26 stsp
1936 2ec1f75b 2019-03-26 stsp ie = got_fileindex_entry_get(fileindex, relpath);
1937 2ec1f75b 2019-03-26 stsp if (ie == NULL) {
1938 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_BAD_PATH);
1939 2ec1f75b 2019-03-26 stsp goto done;
1940 2ec1f75b 2019-03-26 stsp }
1941 2ec1f75b 2019-03-26 stsp
1942 2ec1f75b 2019-03-26 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1943 2ec1f75b 2019-03-26 stsp if (err)
1944 2ec1f75b 2019-03-26 stsp goto done;
1945 2ec1f75b 2019-03-26 stsp
1946 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_NO_CHANGE) {
1947 71a29355 2019-03-27 stsp if (status == GOT_STATUS_DELETE) {
1948 71a29355 2019-03-27 stsp err = got_error_set_errno(ENOENT);
1949 71a29355 2019-03-27 stsp goto done;
1950 71a29355 2019-03-27 stsp }
1951 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY) {
1952 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_STATUS);
1953 2ec1f75b 2019-03-26 stsp goto done;
1954 2ec1f75b 2019-03-26 stsp }
1955 2ec1f75b 2019-03-26 stsp if (!delete_local_mods) {
1956 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_MODIFIED);
1957 2ec1f75b 2019-03-26 stsp goto done;
1958 2ec1f75b 2019-03-26 stsp }
1959 2ec1f75b 2019-03-26 stsp }
1960 2ec1f75b 2019-03-26 stsp
1961 2ec1f75b 2019-03-26 stsp if (unlink(ondisk_path) != 0) {
1962 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1963 2ec1f75b 2019-03-26 stsp goto done;
1964 2ec1f75b 2019-03-26 stsp }
1965 2ec1f75b 2019-03-26 stsp
1966 2ec1f75b 2019-03-26 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1967 2ec1f75b 2019-03-26 stsp
1968 2ec1f75b 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1969 2ec1f75b 2019-03-26 stsp fileindex_path);
1970 2ec1f75b 2019-03-26 stsp if (err)
1971 2ec1f75b 2019-03-26 stsp goto done;
1972 2ec1f75b 2019-03-26 stsp
1973 2ec1f75b 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
1974 2ec1f75b 2019-03-26 stsp if (err)
1975 2ec1f75b 2019-03-26 stsp goto done;
1976 2ec1f75b 2019-03-26 stsp
1977 2ec1f75b 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1978 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1979 2ec1f75b 2019-03-26 stsp goto done;
1980 2ec1f75b 2019-03-26 stsp }
1981 2ec1f75b 2019-03-26 stsp
1982 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
1983 2ec1f75b 2019-03-26 stsp new_fileindex_path = NULL;
1984 2ec1f75b 2019-03-26 stsp
1985 2ec1f75b 2019-03-26 stsp err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1986 2ec1f75b 2019-03-26 stsp done:
1987 2ec1f75b 2019-03-26 stsp free(relpath);
1988 2ec1f75b 2019-03-26 stsp if (index) {
1989 2ec1f75b 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
1990 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1991 2ec1f75b 2019-03-26 stsp }
1992 2ec1f75b 2019-03-26 stsp if (new_fileindex_path) {
1993 2ec1f75b 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
1994 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1995 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
1996 2ec1f75b 2019-03-26 stsp }
1997 2ec1f75b 2019-03-26 stsp if (fileindex)
1998 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
1999 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2000 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2001 2ec1f75b 2019-03-26 stsp err = unlockerr;
2002 2ec1f75b 2019-03-26 stsp return err;
2003 2ec1f75b 2019-03-26 stsp }
2004 a129376b 2019-03-28 stsp
2005 a129376b 2019-03-28 stsp const struct got_error *
2006 a129376b 2019-03-28 stsp got_worktree_revert(struct got_worktree *worktree,
2007 a129376b 2019-03-28 stsp const char *ondisk_path,
2008 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2009 a129376b 2019-03-28 stsp struct got_repository *repo)
2010 a129376b 2019-03-28 stsp {
2011 a129376b 2019-03-28 stsp struct got_fileindex *fileindex = NULL;
2012 a129376b 2019-03-28 stsp struct got_fileindex_entry *ie = NULL;
2013 a129376b 2019-03-28 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2014 a129376b 2019-03-28 stsp char *tree_path = NULL, *parent_path, *te_name;
2015 a129376b 2019-03-28 stsp FILE *index = NULL, *new_index = NULL;
2016 a129376b 2019-03-28 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2017 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2018 a129376b 2019-03-28 stsp struct got_object_id id, *tree_id = NULL;
2019 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2020 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2021 a129376b 2019-03-28 stsp unsigned char status;
2022 a129376b 2019-03-28 stsp struct stat sb;
2023 a129376b 2019-03-28 stsp
2024 a129376b 2019-03-28 stsp err = lock_worktree(worktree, LOCK_EX);
2025 a129376b 2019-03-28 stsp if (err)
2026 a129376b 2019-03-28 stsp return err;
2027 a129376b 2019-03-28 stsp
2028 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2029 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2030 a129376b 2019-03-28 stsp if (err)
2031 a129376b 2019-03-28 stsp goto done;
2032 a129376b 2019-03-28 stsp
2033 a129376b 2019-03-28 stsp fileindex = got_fileindex_alloc();
2034 a129376b 2019-03-28 stsp if (fileindex == NULL) {
2035 a129376b 2019-03-28 stsp err = got_error_from_errno();
2036 a129376b 2019-03-28 stsp goto done;
2037 a129376b 2019-03-28 stsp }
2038 a129376b 2019-03-28 stsp
2039 a129376b 2019-03-28 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2040 a129376b 2019-03-28 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2041 a129376b 2019-03-28 stsp err = got_error_from_errno();
2042 a129376b 2019-03-28 stsp fileindex_path = NULL;
2043 a129376b 2019-03-28 stsp goto done;
2044 a129376b 2019-03-28 stsp }
2045 a129376b 2019-03-28 stsp
2046 a129376b 2019-03-28 stsp index = fopen(fileindex_path, "rb");
2047 a129376b 2019-03-28 stsp if (index == NULL) {
2048 a129376b 2019-03-28 stsp err = got_error_from_errno();
2049 a129376b 2019-03-28 stsp goto done;
2050 a129376b 2019-03-28 stsp }
2051 a129376b 2019-03-28 stsp
2052 a129376b 2019-03-28 stsp err = got_fileindex_read(fileindex, index);
2053 a129376b 2019-03-28 stsp if (err)
2054 a129376b 2019-03-28 stsp goto done;
2055 a129376b 2019-03-28 stsp
2056 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2057 a129376b 2019-03-28 stsp if (ie == NULL) {
2058 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2059 a129376b 2019-03-28 stsp goto done;
2060 a129376b 2019-03-28 stsp }
2061 a129376b 2019-03-28 stsp
2062 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2063 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2064 a129376b 2019-03-28 stsp if (err) {
2065 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2066 a129376b 2019-03-28 stsp goto done;
2067 a129376b 2019-03-28 stsp parent_path = "/";
2068 a129376b 2019-03-28 stsp }
2069 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2070 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2071 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2072 a129376b 2019-03-28 stsp err = got_error_from_errno();
2073 a129376b 2019-03-28 stsp goto done;
2074 a129376b 2019-03-28 stsp }
2075 a129376b 2019-03-28 stsp } else {
2076 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2077 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2078 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2079 a129376b 2019-03-28 stsp err = got_error_from_errno();
2080 a129376b 2019-03-28 stsp goto done;
2081 a129376b 2019-03-28 stsp }
2082 a129376b 2019-03-28 stsp } else {
2083 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2084 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2085 a129376b 2019-03-28 stsp err = got_error_from_errno();
2086 a129376b 2019-03-28 stsp goto done;
2087 a129376b 2019-03-28 stsp }
2088 a129376b 2019-03-28 stsp }
2089 a129376b 2019-03-28 stsp }
2090 a129376b 2019-03-28 stsp
2091 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2092 a129376b 2019-03-28 stsp tree_path);
2093 a129376b 2019-03-28 stsp if (err)
2094 a129376b 2019-03-28 stsp goto done;
2095 a129376b 2019-03-28 stsp
2096 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2097 a129376b 2019-03-28 stsp if (err)
2098 a129376b 2019-03-28 stsp goto done;
2099 a129376b 2019-03-28 stsp
2100 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2101 a129376b 2019-03-28 stsp if (te_name == NULL) {
2102 a129376b 2019-03-28 stsp err = got_error_from_errno();
2103 a129376b 2019-03-28 stsp goto done;
2104 a129376b 2019-03-28 stsp }
2105 a129376b 2019-03-28 stsp
2106 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2107 a129376b 2019-03-28 stsp if (err)
2108 a129376b 2019-03-28 stsp goto done;
2109 a129376b 2019-03-28 stsp
2110 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2111 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2112 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2113 a129376b 2019-03-28 stsp goto done;
2114 a129376b 2019-03-28 stsp }
2115 a129376b 2019-03-28 stsp
2116 a129376b 2019-03-28 stsp switch (status) {
2117 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2118 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2119 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2120 a129376b 2019-03-28 stsp break;
2121 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2122 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2123 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2124 a129376b 2019-03-28 stsp case GOT_STATUS_MISSING:
2125 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2126 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2127 a129376b 2019-03-28 stsp if (err)
2128 a129376b 2019-03-28 stsp goto done;
2129 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2130 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2131 a129376b 2019-03-28 stsp progress_arg);
2132 a129376b 2019-03-28 stsp if (err)
2133 a129376b 2019-03-28 stsp goto done;
2134 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2135 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2136 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2137 a129376b 2019-03-28 stsp if (err)
2138 a129376b 2019-03-28 stsp goto done;
2139 a129376b 2019-03-28 stsp }
2140 a129376b 2019-03-28 stsp break;
2141 a129376b 2019-03-28 stsp default:
2142 a129376b 2019-03-28 stsp goto done;
2143 a129376b 2019-03-28 stsp }
2144 a129376b 2019-03-28 stsp
2145 a129376b 2019-03-28 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2146 a129376b 2019-03-28 stsp fileindex_path);
2147 a129376b 2019-03-28 stsp if (err)
2148 a129376b 2019-03-28 stsp goto done;
2149 a129376b 2019-03-28 stsp
2150 a129376b 2019-03-28 stsp err = got_fileindex_write(fileindex, new_index);
2151 a129376b 2019-03-28 stsp if (err)
2152 a129376b 2019-03-28 stsp goto done;
2153 a129376b 2019-03-28 stsp
2154 a129376b 2019-03-28 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2155 a129376b 2019-03-28 stsp err = got_error_from_errno();
2156 a129376b 2019-03-28 stsp goto done;
2157 a129376b 2019-03-28 stsp }
2158 a129376b 2019-03-28 stsp
2159 a129376b 2019-03-28 stsp free(new_fileindex_path);
2160 a129376b 2019-03-28 stsp new_fileindex_path = NULL;
2161 a129376b 2019-03-28 stsp done:
2162 a129376b 2019-03-28 stsp free(relpath);
2163 a129376b 2019-03-28 stsp free(tree_path);
2164 a129376b 2019-03-28 stsp if (blob)
2165 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2166 a129376b 2019-03-28 stsp if (tree)
2167 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2168 a129376b 2019-03-28 stsp free(tree_id);
2169 a129376b 2019-03-28 stsp if (index) {
2170 a129376b 2019-03-28 stsp if (fclose(index) != 0 && err == NULL)
2171 a129376b 2019-03-28 stsp err = got_error_from_errno();
2172 a129376b 2019-03-28 stsp }
2173 a129376b 2019-03-28 stsp if (new_fileindex_path) {
2174 a129376b 2019-03-28 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
2175 a129376b 2019-03-28 stsp err = got_error_from_errno();
2176 a129376b 2019-03-28 stsp free(new_fileindex_path);
2177 a129376b 2019-03-28 stsp }
2178 a129376b 2019-03-28 stsp if (fileindex)
2179 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2180 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2181 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2182 a129376b 2019-03-28 stsp err = unlockerr;
2183 a129376b 2019-03-28 stsp return err;
2184 a129376b 2019-03-28 stsp }