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