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 324d37e7 2019-05-11 stsp #include "got_path.h"
44 86c3caaf 2018-03-09 stsp
45 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.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 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
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 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("open", path);
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 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("dprintf");
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 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("close");
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 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
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 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("fprintf", tmppath);
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 230a42bd 2019-05-11 jcs err = got_error_prefix_errno3("rename", tmppath, path);
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 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
129 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("fclose", tmppath);
130 230a42bd 2019-05-11 jcs free(tmppath);
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 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
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 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("open", path);
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 230a42bd 2019-05-11 jcs : got_error_prefix_errno2("flock", path));
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 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("lstat", path);
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 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("calloc");
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 230a42bd 2019-05-11 jcs err = (n == -1 ? got_error_prefix_errno2("read", path) :
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 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("close", path_got);
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 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
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 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("mkdir", path);
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 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
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 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("mkdir", path_got);
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 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_ref_to_str");
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 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
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 6d9d28c3 2018-03-11 stsp int version, fd = -1;
334 6d9d28c3 2018-03-11 stsp const char *errstr;
335 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
336 c442a90d 2019-03-10 stsp uint32_t uuid_status;
337 6d9d28c3 2018-03-11 stsp
338 6d9d28c3 2018-03-11 stsp *worktree = NULL;
339 6d9d28c3 2018-03-11 stsp
340 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
341 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
342 7ac97322 2018-03-11 stsp path_got = NULL;
343 6d9d28c3 2018-03-11 stsp goto done;
344 6d9d28c3 2018-03-11 stsp }
345 6d9d28c3 2018-03-11 stsp
346 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
347 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
348 056e7441 2018-03-11 stsp path_lock = NULL;
349 6d9d28c3 2018-03-11 stsp goto done;
350 6d9d28c3 2018-03-11 stsp }
351 6d9d28c3 2018-03-11 stsp
352 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
353 6d9d28c3 2018-03-11 stsp if (fd == -1) {
354 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
355 230a42bd 2019-05-11 jcs : got_error_prefix_errno2("open", path_lock));
356 6d9d28c3 2018-03-11 stsp goto done;
357 6d9d28c3 2018-03-11 stsp }
358 6d9d28c3 2018-03-11 stsp
359 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
360 6d9d28c3 2018-03-11 stsp if (err)
361 6d9d28c3 2018-03-11 stsp goto done;
362 6d9d28c3 2018-03-11 stsp
363 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
364 6d9d28c3 2018-03-11 stsp if (errstr) {
365 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
366 6d9d28c3 2018-03-11 stsp goto done;
367 6d9d28c3 2018-03-11 stsp }
368 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 6d9d28c3 2018-03-11 stsp
373 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
374 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
375 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("calloc");
376 6d9d28c3 2018-03-11 stsp goto done;
377 6d9d28c3 2018-03-11 stsp }
378 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 6d9d28c3 2018-03-11 stsp
380 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
381 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
382 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("strdup");
383 6d9d28c3 2018-03-11 stsp goto done;
384 6d9d28c3 2018-03-11 stsp }
385 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
387 6d9d28c3 2018-03-11 stsp if (err)
388 6d9d28c3 2018-03-11 stsp goto done;
389 eaccb85f 2018-12-25 stsp
390 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
392 93a30277 2018-12-24 stsp if (err)
393 93a30277 2018-12-24 stsp goto done;
394 93a30277 2018-12-24 stsp
395 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
396 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
397 c442a90d 2019-03-10 stsp if (err)
398 c442a90d 2019-03-10 stsp goto done;
399 c442a90d 2019-03-10 stsp
400 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 eaccb85f 2018-12-25 stsp if (err)
402 c442a90d 2019-03-10 stsp goto done;
403 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
405 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
406 eaccb85f 2018-12-25 stsp goto done;
407 c442a90d 2019-03-10 stsp }
408 eaccb85f 2018-12-25 stsp
409 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
410 eaccb85f 2018-12-25 stsp if (err)
411 eaccb85f 2018-12-25 stsp goto done;
412 eaccb85f 2018-12-25 stsp
413 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 eaccb85f 2018-12-25 stsp base_commit_id_str);
415 f5baf295 2018-03-11 stsp if (err)
416 6d9d28c3 2018-03-11 stsp goto done;
417 6d9d28c3 2018-03-11 stsp
418 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
420 6d9d28c3 2018-03-11 stsp done:
421 eaccb85f 2018-12-25 stsp if (repo)
422 eaccb85f 2018-12-25 stsp got_repo_close(repo);
423 7ac97322 2018-03-11 stsp free(path_got);
424 056e7441 2018-03-11 stsp free(path_lock);
425 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
426 c442a90d 2019-03-10 stsp free(uuidstr);
427 bd165944 2019-03-10 stsp free(formatstr);
428 6d9d28c3 2018-03-11 stsp if (err) {
429 6d9d28c3 2018-03-11 stsp if (fd != -1)
430 6d9d28c3 2018-03-11 stsp close(fd);
431 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
432 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
433 6d9d28c3 2018-03-11 stsp *worktree = NULL;
434 6d9d28c3 2018-03-11 stsp } else
435 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
436 6d9d28c3 2018-03-11 stsp
437 6d9d28c3 2018-03-11 stsp return err;
438 86c3caaf 2018-03-09 stsp }
439 247140b2 2019-02-05 stsp
440 247140b2 2019-02-05 stsp const struct got_error *
441 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
442 247140b2 2019-02-05 stsp {
443 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
444 86c3caaf 2018-03-09 stsp
445 247140b2 2019-02-05 stsp do {
446 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
447 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
448 247140b2 2019-02-05 stsp return err;
449 247140b2 2019-02-05 stsp if (*worktree)
450 247140b2 2019-02-05 stsp return NULL;
451 247140b2 2019-02-05 stsp path = dirname(path);
452 d1542a27 2019-02-05 stsp if (path == NULL)
453 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("dirname", path);
454 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 247140b2 2019-02-05 stsp
456 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
457 247140b2 2019-02-05 stsp }
458 247140b2 2019-02-05 stsp
459 3a6ce05a 2019-02-11 stsp const struct got_error *
460 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
461 86c3caaf 2018-03-09 stsp {
462 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
463 c88eb298 2018-03-11 stsp free(worktree->root_path);
464 cde76477 2018-03-11 stsp free(worktree->repo_path);
465 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
466 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
467 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
468 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
469 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
470 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("close",
471 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
472 6d9d28c3 2018-03-11 stsp free(worktree);
473 3a6ce05a 2019-02-11 stsp return err;
474 86c3caaf 2018-03-09 stsp }
475 86c3caaf 2018-03-09 stsp
476 2fbdb5ae 2018-12-29 stsp const char *
477 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
478 c7f4312f 2019-02-05 stsp {
479 c7f4312f 2019-02-05 stsp return worktree->root_path;
480 c7f4312f 2019-02-05 stsp }
481 c7f4312f 2019-02-05 stsp
482 c7f4312f 2019-02-05 stsp const char *
483 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
484 86c3caaf 2018-03-09 stsp {
485 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
486 49520a32 2018-12-29 stsp }
487 49520a32 2018-12-29 stsp
488 49520a32 2018-12-29 stsp const char *
489 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
490 49520a32 2018-12-29 stsp {
491 f609be2e 2018-12-29 stsp return worktree->path_prefix;
492 e5dc7198 2018-12-29 stsp }
493 e5dc7198 2018-12-29 stsp
494 e5dc7198 2018-12-29 stsp const struct got_error *
495 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
496 e5dc7198 2018-12-29 stsp const char *path_prefix)
497 e5dc7198 2018-12-29 stsp {
498 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
499 e5dc7198 2018-12-29 stsp
500 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
501 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
502 48b8b0eb 2019-05-11 jcs return got_error_prefix_errno("asprintf");
503 e5dc7198 2018-12-29 stsp }
504 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
505 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
506 e5dc7198 2018-12-29 stsp free(absprefix);
507 e5dc7198 2018-12-29 stsp return NULL;
508 86c3caaf 2018-03-09 stsp }
509 86c3caaf 2018-03-09 stsp
510 bc70eb79 2019-05-09 stsp const char *
511 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 86c3caaf 2018-03-09 stsp {
513 36a38700 2019-05-10 stsp return worktree->head_ref_name;
514 507dc3bb 2018-12-29 stsp }
515 507dc3bb 2018-12-29 stsp
516 b72f483a 2019-02-05 stsp struct got_object_id *
517 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
518 507dc3bb 2018-12-29 stsp {
519 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
520 86c3caaf 2018-03-09 stsp }
521 86c3caaf 2018-03-09 stsp
522 507dc3bb 2018-12-29 stsp const struct got_error *
523 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
524 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
525 507dc3bb 2018-12-29 stsp {
526 507dc3bb 2018-12-29 stsp const struct got_error *err;
527 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
528 507dc3bb 2018-12-29 stsp char *id_str = NULL;
529 507dc3bb 2018-12-29 stsp char *path_got = NULL;
530 507dc3bb 2018-12-29 stsp
531 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
532 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
533 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
534 507dc3bb 2018-12-29 stsp path_got = NULL;
535 507dc3bb 2018-12-29 stsp goto done;
536 507dc3bb 2018-12-29 stsp }
537 507dc3bb 2018-12-29 stsp
538 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
539 507dc3bb 2018-12-29 stsp if (err)
540 507dc3bb 2018-12-29 stsp return err;
541 507dc3bb 2018-12-29 stsp
542 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
543 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
544 507dc3bb 2018-12-29 stsp goto done;
545 507dc3bb 2018-12-29 stsp }
546 507dc3bb 2018-12-29 stsp
547 507dc3bb 2018-12-29 stsp /* Record our base commit. */
548 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
549 507dc3bb 2018-12-29 stsp if (err)
550 507dc3bb 2018-12-29 stsp goto done;
551 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
552 507dc3bb 2018-12-29 stsp if (err)
553 507dc3bb 2018-12-29 stsp goto done;
554 507dc3bb 2018-12-29 stsp
555 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
556 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
557 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
558 48b8b0eb 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
559 507dc3bb 2018-12-29 stsp goto done;
560 507dc3bb 2018-12-29 stsp }
561 507dc3bb 2018-12-29 stsp done:
562 507dc3bb 2018-12-29 stsp if (obj)
563 507dc3bb 2018-12-29 stsp got_object_close(obj);
564 507dc3bb 2018-12-29 stsp free(id_str);
565 507dc3bb 2018-12-29 stsp free(path_got);
566 507dc3bb 2018-12-29 stsp return err;
567 507dc3bb 2018-12-29 stsp }
568 507dc3bb 2018-12-29 stsp
569 9d31a1d8 2018-03-11 stsp static const struct got_error *
570 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
571 86c3caaf 2018-03-09 stsp {
572 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
573 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
574 230a42bd 2019-05-11 jcs : got_error_prefix_errno2("flock",
575 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
576 86c3caaf 2018-03-09 stsp return NULL;
577 21908da4 2019-01-13 stsp }
578 21908da4 2019-01-13 stsp
579 21908da4 2019-01-13 stsp static const struct got_error *
580 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
581 4a1ddfc2 2019-01-12 stsp {
582 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
583 4a1ddfc2 2019-01-12 stsp char *abspath;
584 4a1ddfc2 2019-01-12 stsp
585 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
586 48b8b0eb 2019-05-11 jcs return got_error_prefix_errno("asprintf");
587 4a1ddfc2 2019-01-12 stsp
588 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
589 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
590 ddcd8544 2019-03-11 stsp struct stat sb;
591 ddcd8544 2019-03-11 stsp err = NULL;
592 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
593 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("lstat", abspath);
594 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
595 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
596 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
597 ddcd8544 2019-03-11 stsp }
598 ddcd8544 2019-03-11 stsp }
599 4a1ddfc2 2019-01-12 stsp free(abspath);
600 68c76935 2019-02-19 stsp return err;
601 68c76935 2019-02-19 stsp }
602 68c76935 2019-02-19 stsp
603 68c76935 2019-02-19 stsp static const struct got_error *
604 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
605 68c76935 2019-02-19 stsp {
606 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
607 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
608 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
609 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
610 68c76935 2019-02-19 stsp
611 68c76935 2019-02-19 stsp *same = 1;
612 68c76935 2019-02-19 stsp
613 230a42bd 2019-05-11 jcs for (;;) {
614 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
615 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
616 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fread");
617 68c76935 2019-02-19 stsp break;
618 68c76935 2019-02-19 stsp }
619 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
620 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
621 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fread");
622 68c76935 2019-02-19 stsp break;
623 68c76935 2019-02-19 stsp }
624 68c76935 2019-02-19 stsp if (flen1 == 0) {
625 68c76935 2019-02-19 stsp if (flen2 != 0)
626 68c76935 2019-02-19 stsp *same = 0;
627 68c76935 2019-02-19 stsp break;
628 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
629 68c76935 2019-02-19 stsp if (flen1 != 0)
630 68c76935 2019-02-19 stsp *same = 0;
631 68c76935 2019-02-19 stsp break;
632 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
633 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
634 68c76935 2019-02-19 stsp *same = 0;
635 68c76935 2019-02-19 stsp break;
636 68c76935 2019-02-19 stsp }
637 68c76935 2019-02-19 stsp } else {
638 68c76935 2019-02-19 stsp *same = 0;
639 68c76935 2019-02-19 stsp break;
640 68c76935 2019-02-19 stsp }
641 68c76935 2019-02-19 stsp }
642 68c76935 2019-02-19 stsp
643 68c76935 2019-02-19 stsp return err;
644 68c76935 2019-02-19 stsp }
645 68c76935 2019-02-19 stsp
646 68c76935 2019-02-19 stsp static const struct got_error *
647 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
648 68c76935 2019-02-19 stsp {
649 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
650 68c76935 2019-02-19 stsp struct stat sb;
651 68c76935 2019-02-19 stsp size_t size1, size2;
652 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
653 68c76935 2019-02-19 stsp
654 68c76935 2019-02-19 stsp *same = 1;
655 68c76935 2019-02-19 stsp
656 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
657 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("lstat", f1_path);
658 68c76935 2019-02-19 stsp goto done;
659 68c76935 2019-02-19 stsp }
660 68c76935 2019-02-19 stsp size1 = sb.st_size;
661 68c76935 2019-02-19 stsp
662 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
663 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("lstat", f2_path);
664 68c76935 2019-02-19 stsp goto done;
665 68c76935 2019-02-19 stsp }
666 68c76935 2019-02-19 stsp size2 = sb.st_size;
667 68c76935 2019-02-19 stsp
668 68c76935 2019-02-19 stsp if (size1 != size2) {
669 68c76935 2019-02-19 stsp *same = 0;
670 68c76935 2019-02-19 stsp return NULL;
671 68c76935 2019-02-19 stsp }
672 68c76935 2019-02-19 stsp
673 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
674 68c76935 2019-02-19 stsp if (f1 == NULL)
675 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("open", f1_path);
676 68c76935 2019-02-19 stsp
677 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
678 68c76935 2019-02-19 stsp if (f2 == NULL) {
679 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("open", f2_path);
680 68c76935 2019-02-19 stsp goto done;
681 68c76935 2019-02-19 stsp }
682 68c76935 2019-02-19 stsp
683 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
684 68c76935 2019-02-19 stsp done:
685 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
686 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
687 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
688 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
689 68c76935 2019-02-19 stsp
690 6353ad76 2019-02-08 stsp return err;
691 6353ad76 2019-02-08 stsp }
692 6353ad76 2019-02-08 stsp
693 6353ad76 2019-02-08 stsp /*
694 6353ad76 2019-02-08 stsp * Perform a 3-way merge where the file's version in the file index (blob2)
695 6353ad76 2019-02-08 stsp * acts as the common ancestor, the incoming blob (blob1) acts as the first
696 6353ad76 2019-02-08 stsp * derived version, and the file on disk acts as the second derived version.
697 6353ad76 2019-02-08 stsp */
698 6353ad76 2019-02-08 stsp static const struct got_error *
699 6353ad76 2019-02-08 stsp merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
700 6353ad76 2019-02-08 stsp struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
701 b8f41171 2019-02-10 stsp uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
702 b8f41171 2019-02-10 stsp struct got_repository *repo,
703 6353ad76 2019-02-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
704 6353ad76 2019-02-08 stsp {
705 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
706 6353ad76 2019-02-08 stsp int merged_fd = -1;
707 6353ad76 2019-02-08 stsp struct got_blob_object *blob2 = NULL;
708 6353ad76 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL;
709 6353ad76 2019-02-08 stsp char *blob1_path = NULL, *blob2_path = NULL;
710 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
711 6353ad76 2019-02-08 stsp char *id_str = NULL;
712 6353ad76 2019-02-08 stsp char *label1 = NULL;
713 68c76935 2019-02-19 stsp int overlapcnt = 0, update_timestamps = 0;
714 af54ae4a 2019-02-19 stsp char *parent;
715 6353ad76 2019-02-08 stsp
716 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
717 af54ae4a 2019-02-19 stsp if (parent == NULL)
718 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("dirname", ondisk_path);
719 af54ae4a 2019-02-19 stsp
720 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
721 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
722 af54ae4a 2019-02-19 stsp
723 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
724 6353ad76 2019-02-08 stsp if (err)
725 af54ae4a 2019-02-19 stsp goto done;
726 af54ae4a 2019-02-19 stsp
727 af54ae4a 2019-02-19 stsp free(base_path);
728 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
729 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
730 af54ae4a 2019-02-19 stsp base_path = NULL;
731 af54ae4a 2019-02-19 stsp goto done;
732 af54ae4a 2019-02-19 stsp }
733 af54ae4a 2019-02-19 stsp
734 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob1_path, &f1, base_path);
735 6353ad76 2019-02-08 stsp if (err)
736 6353ad76 2019-02-08 stsp goto done;
737 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
738 6353ad76 2019-02-08 stsp if (err)
739 6353ad76 2019-02-08 stsp goto done;
740 6353ad76 2019-02-08 stsp
741 af54ae4a 2019-02-19 stsp free(base_path);
742 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
743 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
744 af54ae4a 2019-02-19 stsp base_path = NULL;
745 af54ae4a 2019-02-19 stsp goto done;
746 af54ae4a 2019-02-19 stsp }
747 af54ae4a 2019-02-19 stsp
748 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob2_path, &f2, base_path);
749 6353ad76 2019-02-08 stsp if (err)
750 6353ad76 2019-02-08 stsp goto done;
751 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie)) {
752 1430b4e0 2019-03-27 stsp struct got_object_id id2;
753 1430b4e0 2019-03-27 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
754 1430b4e0 2019-03-27 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
755 1430b4e0 2019-03-27 stsp if (err)
756 1430b4e0 2019-03-27 stsp goto done;
757 1430b4e0 2019-03-27 stsp err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
758 1430b4e0 2019-03-27 stsp if (err)
759 1430b4e0 2019-03-27 stsp goto done;
760 1430b4e0 2019-03-27 stsp } else {
761 1430b4e0 2019-03-27 stsp /*
762 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
763 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
764 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
765 1430b4e0 2019-03-27 stsp */
766 1430b4e0 2019-03-27 stsp }
767 6353ad76 2019-02-08 stsp
768 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
769 6353ad76 2019-02-08 stsp if (err)
770 6353ad76 2019-02-08 stsp goto done;
771 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
772 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
773 6353ad76 2019-02-08 stsp goto done;
774 6353ad76 2019-02-08 stsp }
775 6353ad76 2019-02-08 stsp
776 6353ad76 2019-02-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
777 6353ad76 2019-02-08 stsp blob2_path, ondisk_path, label1, path);
778 6353ad76 2019-02-08 stsp if (err)
779 6353ad76 2019-02-08 stsp goto done;
780 6353ad76 2019-02-08 stsp
781 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
782 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
783 6353ad76 2019-02-08 stsp
784 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
785 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fsync");
786 816dc654 2019-02-16 stsp goto done;
787 68c76935 2019-02-19 stsp }
788 68c76935 2019-02-19 stsp
789 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
790 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
791 68c76935 2019-02-19 stsp err = check_files_equal(&update_timestamps, blob1_path,
792 68c76935 2019-02-19 stsp merged_path);
793 68c76935 2019-02-19 stsp if (err)
794 68c76935 2019-02-19 stsp goto done;
795 816dc654 2019-02-16 stsp }
796 6353ad76 2019-02-08 stsp
797 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
798 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("chmod", merged_path);
799 70a0c8ec 2019-02-20 stsp goto done;
800 70a0c8ec 2019-02-20 stsp }
801 70a0c8ec 2019-02-20 stsp
802 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
803 230a42bd 2019-05-11 jcs err = got_error_prefix_errno3("rename", merged_path,
804 230a42bd 2019-05-11 jcs ondisk_path);
805 2a57020b 2019-02-20 stsp unlink(merged_path);
806 6353ad76 2019-02-08 stsp goto done;
807 6353ad76 2019-02-08 stsp }
808 6353ad76 2019-02-08 stsp
809 02c07007 2019-02-10 stsp /*
810 02c07007 2019-02-10 stsp * Do not update timestamps of already modified files. Otherwise,
811 65ad4e61 2019-02-20 stsp * a future status walk would treat them as unmodified files again.
812 02c07007 2019-02-10 stsp */
813 6353ad76 2019-02-08 stsp err = got_fileindex_entry_update(ie, ondisk_path,
814 68c76935 2019-02-19 stsp blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
815 6353ad76 2019-02-08 stsp done:
816 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
817 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("close");
818 3a6ce05a 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
819 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
820 3a6ce05a 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
821 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
822 6353ad76 2019-02-08 stsp if (blob2)
823 6353ad76 2019-02-08 stsp got_object_blob_close(blob2);
824 6353ad76 2019-02-08 stsp free(merged_path);
825 af54ae4a 2019-02-19 stsp free(base_path);
826 6353ad76 2019-02-08 stsp if (blob1_path) {
827 6353ad76 2019-02-08 stsp unlink(blob1_path);
828 6353ad76 2019-02-08 stsp free(blob1_path);
829 6353ad76 2019-02-08 stsp }
830 6353ad76 2019-02-08 stsp if (blob2_path) {
831 6353ad76 2019-02-08 stsp unlink(blob2_path);
832 6353ad76 2019-02-08 stsp free(blob2_path);
833 6353ad76 2019-02-08 stsp }
834 6353ad76 2019-02-08 stsp free(id_str);
835 6353ad76 2019-02-08 stsp free(label1);
836 4a1ddfc2 2019-01-12 stsp return err;
837 4a1ddfc2 2019-01-12 stsp }
838 4a1ddfc2 2019-01-12 stsp
839 4a1ddfc2 2019-01-12 stsp static const struct got_error *
840 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
841 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
842 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
843 13d9040b 2019-03-27 stsp int update_timestamps)
844 13d9040b 2019-03-27 stsp {
845 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
846 13d9040b 2019-03-27 stsp
847 13d9040b 2019-03-27 stsp if (ie == NULL)
848 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
849 13d9040b 2019-03-27 stsp if (ie)
850 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
851 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
852 13d9040b 2019-03-27 stsp update_timestamps);
853 13d9040b 2019-03-27 stsp else {
854 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
855 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
856 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
857 13d9040b 2019-03-27 stsp if (!err)
858 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
859 13d9040b 2019-03-27 stsp }
860 13d9040b 2019-03-27 stsp return err;
861 13d9040b 2019-03-27 stsp }
862 13d9040b 2019-03-27 stsp
863 13d9040b 2019-03-27 stsp static const struct got_error *
864 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
865 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
866 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
867 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
868 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
869 9d31a1d8 2018-03-11 stsp {
870 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
871 507dc3bb 2018-12-29 stsp int fd = -1;
872 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
873 507dc3bb 2018-12-29 stsp int update = 0;
874 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
875 9d31a1d8 2018-03-11 stsp
876 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
877 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
878 9d31a1d8 2018-03-11 stsp if (fd == -1) {
879 21908da4 2019-01-13 stsp if (errno == ENOENT) {
880 21908da4 2019-01-13 stsp char *parent = dirname(path);
881 21908da4 2019-01-13 stsp if (parent == NULL)
882 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("dirname", path);
883 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
884 21908da4 2019-01-13 stsp if (err)
885 21908da4 2019-01-13 stsp return err;
886 21908da4 2019-01-13 stsp fd = open(ondisk_path,
887 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
888 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
889 21908da4 2019-01-13 stsp if (fd == -1)
890 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("open",
891 230a42bd 2019-05-11 jcs ondisk_path);
892 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
893 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
894 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
895 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
896 507dc3bb 2018-12-29 stsp goto done;
897 d70b8e30 2018-12-27 stsp } else {
898 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
899 507dc3bb 2018-12-29 stsp ondisk_path);
900 507dc3bb 2018-12-29 stsp if (err)
901 507dc3bb 2018-12-29 stsp goto done;
902 507dc3bb 2018-12-29 stsp update = 1;
903 9d31a1d8 2018-03-11 stsp }
904 507dc3bb 2018-12-29 stsp } else
905 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("open", ondisk_path);
906 9d31a1d8 2018-03-11 stsp }
907 9d31a1d8 2018-03-11 stsp
908 a378724f 2019-02-10 stsp if (restoring_missing_file)
909 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
910 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
911 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
912 a378724f 2019-02-10 stsp else
913 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
914 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
915 d7b62c98 2018-12-27 stsp
916 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
917 9d31a1d8 2018-03-11 stsp do {
918 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
919 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
920 9d31a1d8 2018-03-11 stsp if (err)
921 9d31a1d8 2018-03-11 stsp break;
922 9d31a1d8 2018-03-11 stsp if (len > 0) {
923 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
924 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
925 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
926 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("write");
927 b87c6f83 2018-12-24 stsp goto done;
928 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
929 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
930 b87c6f83 2018-12-24 stsp goto done;
931 9d31a1d8 2018-03-11 stsp }
932 61d6eaa3 2018-12-24 stsp hdrlen = 0;
933 9d31a1d8 2018-03-11 stsp }
934 9d31a1d8 2018-03-11 stsp } while (len != 0);
935 9d31a1d8 2018-03-11 stsp
936 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
937 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fsync");
938 816dc654 2019-02-16 stsp goto done;
939 816dc654 2019-02-16 stsp }
940 9d31a1d8 2018-03-11 stsp
941 507dc3bb 2018-12-29 stsp if (update) {
942 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
943 230a42bd 2019-05-11 jcs err = got_error_prefix_errno3("rename", tmppath,
944 230a42bd 2019-05-11 jcs ondisk_path);
945 2a57020b 2019-02-20 stsp unlink(tmppath);
946 68ed9ba5 2019-02-10 stsp goto done;
947 68ed9ba5 2019-02-10 stsp }
948 68ed9ba5 2019-02-10 stsp }
949 ba8a0d4d 2019-02-10 stsp
950 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
951 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
952 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("chmod", ondisk_path);
953 507dc3bb 2018-12-29 stsp goto done;
954 507dc3bb 2018-12-29 stsp }
955 ba8a0d4d 2019-02-10 stsp } else {
956 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
957 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("chmod", ondisk_path);
958 68ed9ba5 2019-02-10 stsp goto done;
959 68ed9ba5 2019-02-10 stsp }
960 507dc3bb 2018-12-29 stsp }
961 507dc3bb 2018-12-29 stsp
962 9d31a1d8 2018-03-11 stsp done:
963 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
964 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("close");
965 507dc3bb 2018-12-29 stsp free(tmppath);
966 6353ad76 2019-02-08 stsp return err;
967 6353ad76 2019-02-08 stsp }
968 6353ad76 2019-02-08 stsp
969 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
970 6353ad76 2019-02-08 stsp static const struct got_error *
971 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
972 7154f6ce 2019-03-27 stsp {
973 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
974 7154f6ce 2019-03-27 stsp const char *markers[3] = {
975 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
976 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
977 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
978 7154f6ce 2019-03-27 stsp };
979 7154f6ce 2019-03-27 stsp int i = 0;
980 7154f6ce 2019-03-27 stsp char *line;
981 7154f6ce 2019-03-27 stsp size_t len;
982 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
983 7154f6ce 2019-03-27 stsp
984 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
985 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
986 7154f6ce 2019-03-27 stsp if (line == NULL) {
987 7154f6ce 2019-03-27 stsp if (feof(f))
988 7154f6ce 2019-03-27 stsp break;
989 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
990 7154f6ce 2019-03-27 stsp break;
991 7154f6ce 2019-03-27 stsp }
992 7154f6ce 2019-03-27 stsp
993 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
994 7154f6ce 2019-03-27 stsp if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
995 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
996 7154f6ce 2019-03-27 stsp else
997 7154f6ce 2019-03-27 stsp i++;
998 7154f6ce 2019-03-27 stsp }
999 7154f6ce 2019-03-27 stsp }
1000 7154f6ce 2019-03-27 stsp
1001 7154f6ce 2019-03-27 stsp return err;
1002 7154f6ce 2019-03-27 stsp }
1003 7154f6ce 2019-03-27 stsp
1004 7154f6ce 2019-03-27 stsp static const struct got_error *
1005 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1006 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1007 b8f41171 2019-02-10 stsp struct got_repository *repo)
1008 6353ad76 2019-02-08 stsp {
1009 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1010 6353ad76 2019-02-08 stsp struct got_object_id id;
1011 6353ad76 2019-02-08 stsp size_t hdrlen;
1012 6353ad76 2019-02-08 stsp FILE *f = NULL;
1013 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1014 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1015 6353ad76 2019-02-08 stsp size_t flen, blen;
1016 6353ad76 2019-02-08 stsp
1017 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1018 6353ad76 2019-02-08 stsp
1019 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1020 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1021 b8f41171 2019-02-10 stsp if (ie) {
1022 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1023 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1024 2ec1f75b 2019-03-26 stsp else
1025 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1026 b8f41171 2019-02-10 stsp sb->st_mode =
1027 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1028 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1029 b8f41171 2019-02-10 stsp } else
1030 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1031 a378724f 2019-02-10 stsp return NULL;
1032 a378724f 2019-02-10 stsp }
1033 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("lstat", abspath);
1034 a378724f 2019-02-10 stsp }
1035 6353ad76 2019-02-08 stsp
1036 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1037 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1038 6353ad76 2019-02-08 stsp return NULL;
1039 6353ad76 2019-02-08 stsp }
1040 6353ad76 2019-02-08 stsp
1041 b8f41171 2019-02-10 stsp if (ie == NULL)
1042 d00136be 2019-03-26 stsp return NULL;
1043 d00136be 2019-03-26 stsp
1044 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1045 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1046 2ec1f75b 2019-03-26 stsp return NULL;
1047 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1048 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1049 b8f41171 2019-02-10 stsp return NULL;
1050 d00136be 2019-03-26 stsp }
1051 b8f41171 2019-02-10 stsp
1052 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1053 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1054 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1055 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1056 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1057 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1058 6353ad76 2019-02-08 stsp return NULL;
1059 6353ad76 2019-02-08 stsp
1060 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1061 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1062 6353ad76 2019-02-08 stsp if (err)
1063 6353ad76 2019-02-08 stsp return err;
1064 6353ad76 2019-02-08 stsp
1065 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1066 6353ad76 2019-02-08 stsp if (f == NULL) {
1067 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("fopen", abspath);
1068 6353ad76 2019-02-08 stsp goto done;
1069 6353ad76 2019-02-08 stsp }
1070 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1071 656b1f76 2019-05-11 jcs for (;;) {
1072 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1073 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1074 6353ad76 2019-02-08 stsp if (err)
1075 7154f6ce 2019-03-27 stsp goto done;
1076 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1077 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1078 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1079 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fread");
1080 7154f6ce 2019-03-27 stsp goto done;
1081 80c5c120 2019-02-19 stsp }
1082 6353ad76 2019-02-08 stsp if (blen == 0) {
1083 6353ad76 2019-02-08 stsp if (flen != 0)
1084 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1085 6353ad76 2019-02-08 stsp break;
1086 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1087 6353ad76 2019-02-08 stsp if (blen != 0)
1088 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1089 6353ad76 2019-02-08 stsp break;
1090 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1091 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1092 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1093 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1094 6353ad76 2019-02-08 stsp break;
1095 6353ad76 2019-02-08 stsp }
1096 6353ad76 2019-02-08 stsp } else {
1097 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1098 6353ad76 2019-02-08 stsp break;
1099 6353ad76 2019-02-08 stsp }
1100 6353ad76 2019-02-08 stsp hdrlen = 0;
1101 6353ad76 2019-02-08 stsp }
1102 7154f6ce 2019-03-27 stsp
1103 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1104 7154f6ce 2019-03-27 stsp rewind(f);
1105 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1106 7154f6ce 2019-03-27 stsp }
1107 6353ad76 2019-02-08 stsp done:
1108 6353ad76 2019-02-08 stsp if (blob)
1109 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1110 6353ad76 2019-02-08 stsp if (f)
1111 6353ad76 2019-02-08 stsp fclose(f);
1112 9d31a1d8 2018-03-11 stsp return err;
1113 9d31a1d8 2018-03-11 stsp }
1114 9d31a1d8 2018-03-11 stsp
1115 9d31a1d8 2018-03-11 stsp static const struct got_error *
1116 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1117 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1118 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1119 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1120 0584f854 2019-04-06 stsp void *progress_arg)
1121 9d31a1d8 2018-03-11 stsp {
1122 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1123 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1124 6353ad76 2019-02-08 stsp char *ondisk_path;
1125 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1126 b8f41171 2019-02-10 stsp struct stat sb;
1127 9d31a1d8 2018-03-11 stsp
1128 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1129 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
1130 6353ad76 2019-02-08 stsp
1131 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1132 b8f41171 2019-02-10 stsp if (err)
1133 b8f41171 2019-02-10 stsp goto done;
1134 6353ad76 2019-02-08 stsp
1135 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1136 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1137 b8f41171 2019-02-10 stsp goto done;
1138 b8f41171 2019-02-10 stsp }
1139 b8f41171 2019-02-10 stsp
1140 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1141 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1142 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1143 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1144 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1145 b8f41171 2019-02-10 stsp path);
1146 6353ad76 2019-02-08 stsp goto done;
1147 a378724f 2019-02-10 stsp }
1148 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1149 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1150 1430b4e0 2019-03-27 stsp SHA1_DIGEST_LENGTH) == 0)
1151 b8f41171 2019-02-10 stsp goto done;
1152 9d31a1d8 2018-03-11 stsp }
1153 9d31a1d8 2018-03-11 stsp
1154 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1155 8da9e5f4 2019-01-12 stsp if (err)
1156 6353ad76 2019-02-08 stsp goto done;
1157 512f0d0e 2019-01-02 stsp
1158 1430b4e0 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1159 6353ad76 2019-02-08 stsp err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1160 b8f41171 2019-02-10 stsp te->mode, sb.st_mode, blob, repo, progress_cb,
1161 b8f41171 2019-02-10 stsp progress_arg);
1162 13d9040b 2019-03-27 stsp else if (status == GOT_STATUS_DELETE) {
1163 13d9040b 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1164 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1165 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1166 13d9040b 2019-03-27 stsp if (err)
1167 13d9040b 2019-03-27 stsp goto done;
1168 13d9040b 2019-03-27 stsp } else {
1169 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1170 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1171 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1172 13d9040b 2019-03-27 stsp if (err)
1173 13d9040b 2019-03-27 stsp goto done;
1174 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1175 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1176 13d9040b 2019-03-27 stsp if (err)
1177 13d9040b 2019-03-27 stsp goto done;
1178 13d9040b 2019-03-27 stsp }
1179 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1180 6353ad76 2019-02-08 stsp done:
1181 6353ad76 2019-02-08 stsp free(ondisk_path);
1182 8da9e5f4 2019-01-12 stsp return err;
1183 90285c3b 2019-01-08 stsp }
1184 90285c3b 2019-01-08 stsp
1185 90285c3b 2019-01-08 stsp static const struct got_error *
1186 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1187 90285c3b 2019-01-08 stsp {
1188 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1189 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1190 90285c3b 2019-01-08 stsp
1191 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1192 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
1193 90285c3b 2019-01-08 stsp
1194 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1195 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1196 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("unlink", ondisk_path);
1197 c97665ae 2019-01-13 stsp } else {
1198 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1199 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1200 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1201 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1202 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("rmdir",
1203 230a42bd 2019-05-11 jcs parent);
1204 90285c3b 2019-01-08 stsp break;
1205 90285c3b 2019-01-08 stsp }
1206 90285c3b 2019-01-08 stsp parent = dirname(parent);
1207 90285c3b 2019-01-08 stsp }
1208 90285c3b 2019-01-08 stsp }
1209 90285c3b 2019-01-08 stsp free(ondisk_path);
1210 90285c3b 2019-01-08 stsp return err;
1211 512f0d0e 2019-01-02 stsp }
1212 512f0d0e 2019-01-02 stsp
1213 708d8e67 2019-03-27 stsp static const struct got_error *
1214 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1215 708d8e67 2019-03-27 stsp struct got_fileindex_entry *ie, const char *parent_path,
1216 708d8e67 2019-03-27 stsp struct got_repository *repo,
1217 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1218 708d8e67 2019-03-27 stsp {
1219 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1220 708d8e67 2019-03-27 stsp unsigned char status;
1221 708d8e67 2019-03-27 stsp struct stat sb;
1222 708d8e67 2019-03-27 stsp char *ondisk_path;
1223 708d8e67 2019-03-27 stsp
1224 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1225 708d8e67 2019-03-27 stsp == -1)
1226 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
1227 708d8e67 2019-03-27 stsp
1228 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1229 708d8e67 2019-03-27 stsp if (err)
1230 708d8e67 2019-03-27 stsp return err;
1231 708d8e67 2019-03-27 stsp
1232 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1233 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1234 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1235 fc6346c4 2019-03-27 stsp /*
1236 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1237 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1238 fc6346c4 2019-03-27 stsp */
1239 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1240 fc6346c4 2019-03-27 stsp 0);
1241 708d8e67 2019-03-27 stsp if (err)
1242 708d8e67 2019-03-27 stsp return err;
1243 fc6346c4 2019-03-27 stsp } else {
1244 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1245 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1246 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1247 fc6346c4 2019-03-27 stsp if (err)
1248 fc6346c4 2019-03-27 stsp return err;
1249 fc6346c4 2019-03-27 stsp }
1250 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1251 708d8e67 2019-03-27 stsp }
1252 708d8e67 2019-03-27 stsp
1253 fc6346c4 2019-03-27 stsp return err;
1254 708d8e67 2019-03-27 stsp }
1255 708d8e67 2019-03-27 stsp
1256 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1257 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1258 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1259 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1260 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1261 8da9e5f4 2019-01-12 stsp void *progress_arg;
1262 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1263 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1264 8da9e5f4 2019-01-12 stsp };
1265 8da9e5f4 2019-01-12 stsp
1266 512f0d0e 2019-01-02 stsp static const struct got_error *
1267 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1268 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1269 512f0d0e 2019-01-02 stsp {
1270 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1271 0584f854 2019-04-06 stsp
1272 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1273 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1274 512f0d0e 2019-01-02 stsp
1275 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1276 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1277 8da9e5f4 2019-01-12 stsp }
1278 512f0d0e 2019-01-02 stsp
1279 8da9e5f4 2019-01-12 stsp static const struct got_error *
1280 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1281 8da9e5f4 2019-01-12 stsp {
1282 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1283 7a9df742 2019-01-08 stsp
1284 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1285 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1286 0584f854 2019-04-06 stsp
1287 708d8e67 2019-03-27 stsp return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1288 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1289 9d31a1d8 2018-03-11 stsp }
1290 9d31a1d8 2018-03-11 stsp
1291 9d31a1d8 2018-03-11 stsp static const struct got_error *
1292 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1293 9d31a1d8 2018-03-11 stsp {
1294 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1295 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1296 8da9e5f4 2019-01-12 stsp char *path;
1297 9d31a1d8 2018-03-11 stsp
1298 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1299 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1300 0584f854 2019-04-06 stsp
1301 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1302 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1303 8da9e5f4 2019-01-12 stsp == -1)
1304 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
1305 9d31a1d8 2018-03-11 stsp
1306 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1307 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1308 8da9e5f4 2019-01-12 stsp else
1309 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1310 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1311 9d31a1d8 2018-03-11 stsp
1312 8da9e5f4 2019-01-12 stsp free(path);
1313 0cd1c46a 2019-03-11 stsp return err;
1314 0cd1c46a 2019-03-11 stsp }
1315 0cd1c46a 2019-03-11 stsp
1316 517bab73 2019-03-11 stsp const struct got_error *
1317 517bab73 2019-03-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1318 0cd1c46a 2019-03-11 stsp {
1319 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1320 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1321 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1322 0cd1c46a 2019-03-11 stsp
1323 517bab73 2019-03-11 stsp *refname = NULL;
1324 517bab73 2019-03-11 stsp
1325 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1326 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1327 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1328 0cd1c46a 2019-03-11 stsp
1329 0647c563 2019-03-11 stsp if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1330 0647c563 2019-03-11 stsp == -1) {
1331 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1332 0647c563 2019-03-11 stsp *refname = NULL;
1333 0cd1c46a 2019-03-11 stsp }
1334 517bab73 2019-03-11 stsp free(uuidstr);
1335 517bab73 2019-03-11 stsp return err;
1336 517bab73 2019-03-11 stsp }
1337 517bab73 2019-03-11 stsp
1338 517bab73 2019-03-11 stsp /*
1339 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1340 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1341 517bab73 2019-03-11 stsp */
1342 517bab73 2019-03-11 stsp static const struct got_error *
1343 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1344 517bab73 2019-03-11 stsp {
1345 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1346 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1347 517bab73 2019-03-11 stsp char *refname;
1348 517bab73 2019-03-11 stsp
1349 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1350 517bab73 2019-03-11 stsp if (err)
1351 517bab73 2019-03-11 stsp return err;
1352 0cd1c46a 2019-03-11 stsp
1353 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1354 0cd1c46a 2019-03-11 stsp if (err)
1355 0cd1c46a 2019-03-11 stsp goto done;
1356 0cd1c46a 2019-03-11 stsp
1357 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1358 0cd1c46a 2019-03-11 stsp done:
1359 0cd1c46a 2019-03-11 stsp free(refname);
1360 0cd1c46a 2019-03-11 stsp if (ref)
1361 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1362 9d31a1d8 2018-03-11 stsp return err;
1363 9d31a1d8 2018-03-11 stsp }
1364 9d31a1d8 2018-03-11 stsp
1365 ebf99748 2019-05-09 stsp static const struct got_error *
1366 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1367 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1368 ebf99748 2019-05-09 stsp {
1369 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1370 ebf99748 2019-05-09 stsp FILE *index = NULL;
1371 0cd1c46a 2019-03-11 stsp
1372 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1373 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1374 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1375 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_fileindex_alloc");
1376 ebf99748 2019-05-09 stsp
1377 ebf99748 2019-05-09 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1378 ebf99748 2019-05-09 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1379 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1380 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1381 ebf99748 2019-05-09 stsp goto done;
1382 ebf99748 2019-05-09 stsp }
1383 ebf99748 2019-05-09 stsp
1384 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1385 ebf99748 2019-05-09 stsp if (index == NULL) {
1386 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1387 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("fopen", *fileindex_path);
1388 ebf99748 2019-05-09 stsp } else {
1389 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1390 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1391 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
1392 ebf99748 2019-05-09 stsp }
1393 ebf99748 2019-05-09 stsp done:
1394 ebf99748 2019-05-09 stsp if (err) {
1395 ebf99748 2019-05-09 stsp free(*fileindex_path);
1396 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1397 ebf99748 2019-05-09 stsp free(*fileindex);
1398 ebf99748 2019-05-09 stsp *fileindex = NULL;
1399 ebf99748 2019-05-09 stsp }
1400 ebf99748 2019-05-09 stsp return err;
1401 ebf99748 2019-05-09 stsp }
1402 ebf99748 2019-05-09 stsp
1403 9d31a1d8 2018-03-11 stsp const struct got_error *
1404 c4cdcb68 2019-04-03 stsp got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1405 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1406 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1407 9d31a1d8 2018-03-11 stsp {
1408 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1409 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1410 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1411 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1412 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
1413 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1414 ebf99748 2019-05-09 stsp FILE *new_index = NULL;
1415 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1416 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1417 c4cdcb68 2019-04-03 stsp char *relpath = NULL, *entry_name = NULL;
1418 9d31a1d8 2018-03-11 stsp
1419 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1420 9d31a1d8 2018-03-11 stsp if (err)
1421 9d31a1d8 2018-03-11 stsp return err;
1422 93a30277 2018-12-24 stsp
1423 51514078 2018-12-25 stsp /*
1424 51514078 2018-12-25 stsp * Read the file index.
1425 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1426 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1427 51514078 2018-12-25 stsp */
1428 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1429 ebf99748 2019-05-09 stsp if (err)
1430 ebf99748 2019-05-09 stsp goto done;
1431 51514078 2018-12-25 stsp
1432 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1433 b8bdcc21 2018-12-24 stsp fileindex_path);
1434 51664889 2018-03-12 stsp if (err)
1435 51664889 2018-03-12 stsp goto done;
1436 51664889 2018-03-12 stsp
1437 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1438 0cd1c46a 2019-03-11 stsp if (err)
1439 0cd1c46a 2019-03-11 stsp goto done;
1440 0cd1c46a 2019-03-11 stsp
1441 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1442 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1443 9d31a1d8 2018-03-11 stsp if (err)
1444 9d31a1d8 2018-03-11 stsp goto done;
1445 9d31a1d8 2018-03-11 stsp
1446 c4cdcb68 2019-04-03 stsp if (path[0]) {
1447 c4cdcb68 2019-04-03 stsp char *tree_path;
1448 c4cdcb68 2019-04-03 stsp int obj_type;
1449 c4cdcb68 2019-04-03 stsp relpath = strdup(path);
1450 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1451 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
1452 c4cdcb68 2019-04-03 stsp goto done;
1453 c4cdcb68 2019-04-03 stsp }
1454 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1455 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1456 c4cdcb68 2019-04-03 stsp path) == -1) {
1457 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1458 c4cdcb68 2019-04-03 stsp goto done;
1459 c4cdcb68 2019-04-03 stsp }
1460 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1461 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1462 c4cdcb68 2019-04-03 stsp free(tree_path);
1463 c4cdcb68 2019-04-03 stsp if (err)
1464 c4cdcb68 2019-04-03 stsp goto done;
1465 c4cdcb68 2019-04-03 stsp err = got_object_get_type(&obj_type, repo, tree_id);
1466 c4cdcb68 2019-04-03 stsp if (err)
1467 c4cdcb68 2019-04-03 stsp goto done;
1468 c4cdcb68 2019-04-03 stsp if (obj_type == GOT_OBJ_TYPE_BLOB) {
1469 c4cdcb68 2019-04-03 stsp /* Split provided path into parent dir + entry name. */
1470 c4cdcb68 2019-04-03 stsp if (strchr(path, '/') == NULL) {
1471 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1472 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1473 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
1474 c4cdcb68 2019-04-03 stsp goto done;
1475 c4cdcb68 2019-04-03 stsp }
1476 c4cdcb68 2019-04-03 stsp tree_path = strdup(worktree->path_prefix);
1477 c4cdcb68 2019-04-03 stsp if (tree_path == NULL) {
1478 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
1479 c4cdcb68 2019-04-03 stsp goto done;
1480 c4cdcb68 2019-04-03 stsp }
1481 c4cdcb68 2019-04-03 stsp } else {
1482 c4cdcb68 2019-04-03 stsp err = got_path_dirname(&relpath, path);
1483 c4cdcb68 2019-04-03 stsp if (err)
1484 c4cdcb68 2019-04-03 stsp goto done;
1485 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s",
1486 c4cdcb68 2019-04-03 stsp worktree->path_prefix,
1487 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(
1488 c4cdcb68 2019-04-03 stsp worktree->path_prefix) ? "" : "/",
1489 c4cdcb68 2019-04-03 stsp relpath) == -1) {
1490 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1491 c4cdcb68 2019-04-03 stsp goto done;
1492 c4cdcb68 2019-04-03 stsp }
1493 c4cdcb68 2019-04-03 stsp }
1494 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1495 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1496 c4cdcb68 2019-04-03 stsp free(tree_path);
1497 c4cdcb68 2019-04-03 stsp if (err)
1498 c4cdcb68 2019-04-03 stsp goto done;
1499 c4cdcb68 2019-04-03 stsp entry_name = basename(path);
1500 c4cdcb68 2019-04-03 stsp if (entry_name == NULL) {
1501 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("basename", path);
1502 c4cdcb68 2019-04-03 stsp goto done;
1503 c4cdcb68 2019-04-03 stsp }
1504 c4cdcb68 2019-04-03 stsp }
1505 c4cdcb68 2019-04-03 stsp } else {
1506 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1507 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1508 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
1509 c4cdcb68 2019-04-03 stsp goto done;
1510 c4cdcb68 2019-04-03 stsp }
1511 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1512 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, worktree->path_prefix);
1513 c4cdcb68 2019-04-03 stsp if (err)
1514 c4cdcb68 2019-04-03 stsp goto done;
1515 c4cdcb68 2019-04-03 stsp }
1516 9d31a1d8 2018-03-11 stsp
1517 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1518 8da9e5f4 2019-01-12 stsp if (err)
1519 c4cdcb68 2019-04-03 stsp goto done;
1520 c4cdcb68 2019-04-03 stsp
1521 c4cdcb68 2019-04-03 stsp if (entry_name &&
1522 c4cdcb68 2019-04-03 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1523 c4cdcb68 2019-04-03 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1524 8da9e5f4 2019-01-12 stsp goto done;
1525 c4cdcb68 2019-04-03 stsp }
1526 9d31a1d8 2018-03-11 stsp
1527 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1528 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1529 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1530 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1531 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1532 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1533 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1534 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1535 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1536 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1537 c4cdcb68 2019-04-03 stsp checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1538 c4cdcb68 2019-04-03 stsp entry_name, repo, &diff_cb, &arg);
1539 8da9e5f4 2019-01-12 stsp
1540 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
1541 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
1542 9d31a1d8 2018-03-11 stsp if (err)
1543 9d31a1d8 2018-03-11 stsp goto done;
1544 9d31a1d8 2018-03-11 stsp
1545 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1546 230a42bd 2019-05-11 jcs err = got_error_prefix_errno3("rename", new_fileindex_path,
1547 230a42bd 2019-05-11 jcs fileindex_path);
1548 2a57020b 2019-02-20 stsp unlink(new_fileindex_path);
1549 9d31a1d8 2018-03-11 stsp goto done;
1550 9d31a1d8 2018-03-11 stsp }
1551 9d31a1d8 2018-03-11 stsp
1552 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1553 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
1554 9d31a1d8 2018-03-11 stsp
1555 9d31a1d8 2018-03-11 stsp done:
1556 c4cdcb68 2019-04-03 stsp free(relpath);
1557 edfa7d7f 2018-09-11 stsp if (tree)
1558 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1559 9d31a1d8 2018-03-11 stsp if (commit)
1560 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1561 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
1562 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
1563 b8bdcc21 2018-12-24 stsp if (new_index)
1564 b8bdcc21 2018-12-24 stsp fclose(new_index);
1565 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1566 9d31a1d8 2018-03-11 stsp free(fileindex_path);
1567 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1568 a143fb78 2018-12-25 stsp if (checkout_err)
1569 a143fb78 2018-12-25 stsp err = checkout_err;
1570 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1571 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1572 9d31a1d8 2018-03-11 stsp err = unlockerr;
1573 f8d1f275 2019-02-04 stsp return err;
1574 f8d1f275 2019-02-04 stsp }
1575 f8d1f275 2019-02-04 stsp
1576 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1577 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1578 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1579 927df6b7 2019-02-10 stsp const char *status_path;
1580 927df6b7 2019-02-10 stsp size_t status_path_len;
1581 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1582 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1583 f8d1f275 2019-02-04 stsp void *status_arg;
1584 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1585 f8d1f275 2019-02-04 stsp void *cancel_arg;
1586 f8d1f275 2019-02-04 stsp };
1587 f8d1f275 2019-02-04 stsp
1588 f8d1f275 2019-02-04 stsp static const struct got_error *
1589 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1590 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1591 927df6b7 2019-02-10 stsp struct got_repository *repo)
1592 927df6b7 2019-02-10 stsp {
1593 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1594 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1595 927df6b7 2019-02-10 stsp struct stat sb;
1596 927df6b7 2019-02-10 stsp struct got_object_id id;
1597 927df6b7 2019-02-10 stsp
1598 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1599 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1600 927df6b7 2019-02-10 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1601 927df6b7 2019-02-10 stsp err = (*status_cb)(status_arg, status, ie->path, &id);
1602 927df6b7 2019-02-10 stsp }
1603 927df6b7 2019-02-10 stsp return err;
1604 927df6b7 2019-02-10 stsp }
1605 927df6b7 2019-02-10 stsp
1606 927df6b7 2019-02-10 stsp static const struct got_error *
1607 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1608 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1609 f8d1f275 2019-02-04 stsp {
1610 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1611 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1612 f8d1f275 2019-02-04 stsp char *abspath;
1613 f8d1f275 2019-02-04 stsp
1614 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1615 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1616 0584f854 2019-04-06 stsp
1617 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1618 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1619 927df6b7 2019-02-10 stsp return NULL;
1620 927df6b7 2019-02-10 stsp
1621 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1622 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1623 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1624 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
1625 f8d1f275 2019-02-04 stsp } else {
1626 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1627 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1628 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
1629 f8d1f275 2019-02-04 stsp }
1630 f8d1f275 2019-02-04 stsp
1631 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1632 927df6b7 2019-02-10 stsp a->repo);
1633 f8d1f275 2019-02-04 stsp free(abspath);
1634 9d31a1d8 2018-03-11 stsp return err;
1635 9d31a1d8 2018-03-11 stsp }
1636 f8d1f275 2019-02-04 stsp
1637 f8d1f275 2019-02-04 stsp static const struct got_error *
1638 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1639 f8d1f275 2019-02-04 stsp {
1640 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1641 b72f483a 2019-02-05 stsp struct got_object_id id;
1642 2ec1f75b 2019-03-26 stsp unsigned char status;
1643 927df6b7 2019-02-10 stsp
1644 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1645 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1646 0584f854 2019-04-06 stsp
1647 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1648 927df6b7 2019-02-10 stsp return NULL;
1649 927df6b7 2019-02-10 stsp
1650 b72f483a 2019-02-05 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1651 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1652 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
1653 2ec1f75b 2019-03-26 stsp else
1654 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
1655 2ec1f75b 2019-03-26 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1656 f8d1f275 2019-02-04 stsp }
1657 f8d1f275 2019-02-04 stsp
1658 f8d1f275 2019-02-04 stsp static const struct got_error *
1659 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
1660 f8d1f275 2019-02-04 stsp {
1661 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1662 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1663 f8d1f275 2019-02-04 stsp char *path = NULL;
1664 f8d1f275 2019-02-04 stsp
1665 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1666 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1667 0584f854 2019-04-06 stsp
1668 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
1669 2c201a36 2019-02-10 stsp return NULL;
1670 2c201a36 2019-02-10 stsp
1671 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
1672 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
1673 f8d1f275 2019-02-04 stsp return NULL;
1674 f8d1f275 2019-02-04 stsp
1675 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1676 927df6b7 2019-02-10 stsp return NULL;
1677 927df6b7 2019-02-10 stsp
1678 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1679 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1680 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
1681 f8d1f275 2019-02-04 stsp } else {
1682 f8d1f275 2019-02-04 stsp path = de->d_name;
1683 f8d1f275 2019-02-04 stsp }
1684 f8d1f275 2019-02-04 stsp
1685 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1686 b72f483a 2019-02-05 stsp NULL);
1687 f8d1f275 2019-02-04 stsp if (parent_path[0])
1688 f8d1f275 2019-02-04 stsp free(path);
1689 b72f483a 2019-02-05 stsp return err;
1690 f8d1f275 2019-02-04 stsp }
1691 f8d1f275 2019-02-04 stsp
1692 f8d1f275 2019-02-04 stsp const struct got_error *
1693 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
1694 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
1695 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1696 f8d1f275 2019-02-04 stsp {
1697 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1698 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
1699 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
1700 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
1701 f8d1f275 2019-02-04 stsp FILE *index = NULL;
1702 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
1703 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
1704 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
1705 f8d1f275 2019-02-04 stsp
1706 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
1707 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
1708 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_fileindex_alloc");
1709 f8d1f275 2019-02-04 stsp goto done;
1710 f8d1f275 2019-02-04 stsp }
1711 f8d1f275 2019-02-04 stsp
1712 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1713 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1714 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1715 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
1716 f8d1f275 2019-02-04 stsp goto done;
1717 f8d1f275 2019-02-04 stsp }
1718 f8d1f275 2019-02-04 stsp
1719 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
1720 f8d1f275 2019-02-04 stsp if (index == NULL) {
1721 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
1722 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("fopen", fileindex_path);
1723 f8d1f275 2019-02-04 stsp goto done;
1724 f8d1f275 2019-02-04 stsp }
1725 f8d1f275 2019-02-04 stsp } else {
1726 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
1727 f8d1f275 2019-02-04 stsp fclose(index);
1728 f8d1f275 2019-02-04 stsp if (err)
1729 f8d1f275 2019-02-04 stsp goto done;
1730 f8d1f275 2019-02-04 stsp }
1731 f8d1f275 2019-02-04 stsp
1732 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
1733 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
1734 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1735 c513d110 2019-02-05 stsp goto done;
1736 c513d110 2019-02-05 stsp }
1737 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
1738 927df6b7 2019-02-10 stsp if (workdir == NULL) {
1739 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
1740 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
1741 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
1742 927df6b7 2019-02-10 stsp if (ie == NULL) {
1743 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
1744 927df6b7 2019-02-10 stsp goto done;
1745 927df6b7 2019-02-10 stsp }
1746 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
1747 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
1748 927df6b7 2019-02-10 stsp goto done;
1749 927df6b7 2019-02-10 stsp } else {
1750 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("opendir", ondisk_path);
1751 927df6b7 2019-02-10 stsp goto done;
1752 927df6b7 2019-02-10 stsp }
1753 927df6b7 2019-02-10 stsp }
1754 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
1755 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
1756 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
1757 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
1758 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
1759 927df6b7 2019-02-10 stsp arg.status_path = path;
1760 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
1761 f8d1f275 2019-02-04 stsp arg.repo = repo;
1762 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
1763 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
1764 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
1765 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
1766 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1767 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
1768 f8d1f275 2019-02-04 stsp done:
1769 f8d1f275 2019-02-04 stsp if (workdir)
1770 f8d1f275 2019-02-04 stsp closedir(workdir);
1771 927df6b7 2019-02-10 stsp free(ondisk_path);
1772 f8d1f275 2019-02-04 stsp free(fileindex_path);
1773 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
1774 f8d1f275 2019-02-04 stsp return err;
1775 f8d1f275 2019-02-04 stsp }
1776 6c7ab921 2019-03-18 stsp
1777 6c7ab921 2019-03-18 stsp const struct got_error *
1778 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1779 6c7ab921 2019-03-18 stsp const char *arg)
1780 6c7ab921 2019-03-18 stsp {
1781 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
1782 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
1783 6c7ab921 2019-03-18 stsp size_t len;
1784 6c7ab921 2019-03-18 stsp
1785 6c7ab921 2019-03-18 stsp *wt_path = NULL;
1786 6c7ab921 2019-03-18 stsp
1787 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
1788 6c7ab921 2019-03-18 stsp if (resolved == NULL)
1789 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("realpath", arg);
1790 6c7ab921 2019-03-18 stsp
1791 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
1792 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
1793 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
1794 6c7ab921 2019-03-18 stsp goto done;
1795 6c7ab921 2019-03-18 stsp }
1796 6c7ab921 2019-03-18 stsp
1797 c4cdcb68 2019-04-03 stsp path = strdup(resolved +
1798 c4cdcb68 2019-04-03 stsp strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1799 6c7ab921 2019-03-18 stsp if (path == NULL) {
1800 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
1801 6c7ab921 2019-03-18 stsp goto done;
1802 6c7ab921 2019-03-18 stsp }
1803 6c7ab921 2019-03-18 stsp
1804 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
1805 6c7ab921 2019-03-18 stsp len = strlen(path);
1806 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
1807 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
1808 6c7ab921 2019-03-18 stsp len--;
1809 6c7ab921 2019-03-18 stsp }
1810 6c7ab921 2019-03-18 stsp done:
1811 6c7ab921 2019-03-18 stsp free(resolved);
1812 6c7ab921 2019-03-18 stsp if (err == NULL)
1813 6c7ab921 2019-03-18 stsp *wt_path = path;
1814 6c7ab921 2019-03-18 stsp else
1815 6c7ab921 2019-03-18 stsp free(path);
1816 d00136be 2019-03-26 stsp return err;
1817 d00136be 2019-03-26 stsp }
1818 d00136be 2019-03-26 stsp
1819 d00136be 2019-03-26 stsp const struct got_error *
1820 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
1821 031a5338 2019-03-26 stsp const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1822 031a5338 2019-03-26 stsp struct got_repository *repo)
1823 d00136be 2019-03-26 stsp {
1824 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
1825 d00136be 2019-03-26 stsp struct got_fileindex_entry *ie = NULL;
1826 031a5338 2019-03-26 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1827 d00136be 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
1828 d00136be 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
1829 031a5338 2019-03-26 stsp int ie_added = 0;
1830 d00136be 2019-03-26 stsp
1831 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
1832 d00136be 2019-03-26 stsp if (err)
1833 d00136be 2019-03-26 stsp return err;
1834 d00136be 2019-03-26 stsp
1835 031a5338 2019-03-26 stsp err = got_path_skip_common_ancestor(&relpath,
1836 d00136be 2019-03-26 stsp got_worktree_get_root_path(worktree), ondisk_path);
1837 d00136be 2019-03-26 stsp if (err)
1838 d00136be 2019-03-26 stsp goto done;
1839 d00136be 2019-03-26 stsp
1840 d00136be 2019-03-26 stsp fileindex = got_fileindex_alloc();
1841 d00136be 2019-03-26 stsp if (fileindex == NULL) {
1842 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_fileindex_alloc");
1843 d00136be 2019-03-26 stsp goto done;
1844 d00136be 2019-03-26 stsp }
1845 d00136be 2019-03-26 stsp
1846 d00136be 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1847 d00136be 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1848 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1849 d00136be 2019-03-26 stsp fileindex_path = NULL;
1850 d00136be 2019-03-26 stsp goto done;
1851 d00136be 2019-03-26 stsp }
1852 d00136be 2019-03-26 stsp
1853 d00136be 2019-03-26 stsp index = fopen(fileindex_path, "rb");
1854 d00136be 2019-03-26 stsp if (index == NULL) {
1855 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("fopen", fileindex_path);
1856 d00136be 2019-03-26 stsp goto done;
1857 d00136be 2019-03-26 stsp }
1858 d00136be 2019-03-26 stsp
1859 d00136be 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
1860 d00136be 2019-03-26 stsp if (err)
1861 d00136be 2019-03-26 stsp goto done;
1862 d00136be 2019-03-26 stsp
1863 5c99ca9f 2019-03-27 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1864 2af4a041 2019-05-11 jcs err = got_error_set_errno(EEXIST, relpath);
1865 5c99ca9f 2019-03-27 stsp goto done;
1866 5c99ca9f 2019-03-27 stsp }
1867 5c99ca9f 2019-03-27 stsp
1868 5c99ca9f 2019-03-27 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1869 5c99ca9f 2019-03-27 stsp if (err)
1870 5c99ca9f 2019-03-27 stsp goto done;
1871 5c99ca9f 2019-03-27 stsp
1872 d00136be 2019-03-26 stsp err = got_fileindex_entry_add(fileindex, ie);
1873 d00136be 2019-03-26 stsp if (err)
1874 d00136be 2019-03-26 stsp goto done;
1875 031a5338 2019-03-26 stsp ie_added = 1; /* now owned by fileindex; don't free separately */
1876 d00136be 2019-03-26 stsp
1877 d00136be 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1878 d00136be 2019-03-26 stsp fileindex_path);
1879 d00136be 2019-03-26 stsp if (err)
1880 d00136be 2019-03-26 stsp goto done;
1881 d00136be 2019-03-26 stsp
1882 d00136be 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
1883 d00136be 2019-03-26 stsp if (err)
1884 d00136be 2019-03-26 stsp goto done;
1885 d00136be 2019-03-26 stsp
1886 d00136be 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1887 230a42bd 2019-05-11 jcs err = got_error_prefix_errno3("rename", new_fileindex_path,
1888 230a42bd 2019-05-11 jcs fileindex_path);
1889 d00136be 2019-03-26 stsp goto done;
1890 d00136be 2019-03-26 stsp }
1891 d00136be 2019-03-26 stsp
1892 d00136be 2019-03-26 stsp free(new_fileindex_path);
1893 d00136be 2019-03-26 stsp new_fileindex_path = NULL;
1894 031a5338 2019-03-26 stsp
1895 031a5338 2019-03-26 stsp err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1896 d00136be 2019-03-26 stsp done:
1897 d00136be 2019-03-26 stsp if (index) {
1898 d00136be 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
1899 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
1900 d00136be 2019-03-26 stsp }
1901 d00136be 2019-03-26 stsp if (new_fileindex_path) {
1902 d00136be 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
1903 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("unlink",
1904 230a42bd 2019-05-11 jcs new_fileindex_path);
1905 d00136be 2019-03-26 stsp free(new_fileindex_path);
1906 d00136be 2019-03-26 stsp }
1907 5c99ca9f 2019-03-27 stsp if (ie && !ie_added)
1908 d00136be 2019-03-26 stsp got_fileindex_entry_free(ie);
1909 d00136be 2019-03-26 stsp if (fileindex)
1910 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
1911 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1912 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
1913 d00136be 2019-03-26 stsp err = unlockerr;
1914 031a5338 2019-03-26 stsp free(relpath);
1915 6c7ab921 2019-03-18 stsp return err;
1916 6c7ab921 2019-03-18 stsp }
1917 2ec1f75b 2019-03-26 stsp
1918 2ec1f75b 2019-03-26 stsp const struct got_error *
1919 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
1920 2ec1f75b 2019-03-26 stsp const char *ondisk_path, int delete_local_mods,
1921 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
1922 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
1923 2ec1f75b 2019-03-26 stsp {
1924 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
1925 2ec1f75b 2019-03-26 stsp struct got_fileindex_entry *ie = NULL;
1926 2ec1f75b 2019-03-26 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1927 2ec1f75b 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
1928 2ec1f75b 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
1929 2ec1f75b 2019-03-26 stsp unsigned char status;
1930 2ec1f75b 2019-03-26 stsp struct stat sb;
1931 2ec1f75b 2019-03-26 stsp
1932 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
1933 2ec1f75b 2019-03-26 stsp if (err)
1934 2ec1f75b 2019-03-26 stsp return err;
1935 2ec1f75b 2019-03-26 stsp
1936 2ec1f75b 2019-03-26 stsp err = got_path_skip_common_ancestor(&relpath,
1937 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(worktree), ondisk_path);
1938 2ec1f75b 2019-03-26 stsp if (err)
1939 2ec1f75b 2019-03-26 stsp goto done;
1940 2ec1f75b 2019-03-26 stsp
1941 2ec1f75b 2019-03-26 stsp fileindex = got_fileindex_alloc();
1942 2ec1f75b 2019-03-26 stsp if (fileindex == NULL) {
1943 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_fileindex_alloc");
1944 2ec1f75b 2019-03-26 stsp goto done;
1945 2ec1f75b 2019-03-26 stsp }
1946 2ec1f75b 2019-03-26 stsp
1947 2ec1f75b 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1948 2ec1f75b 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1949 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1950 2ec1f75b 2019-03-26 stsp fileindex_path = NULL;
1951 2ec1f75b 2019-03-26 stsp goto done;
1952 2ec1f75b 2019-03-26 stsp }
1953 2ec1f75b 2019-03-26 stsp
1954 2ec1f75b 2019-03-26 stsp index = fopen(fileindex_path, "rb");
1955 2ec1f75b 2019-03-26 stsp if (index == NULL) {
1956 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("fopen", fileindex_path);
1957 2ec1f75b 2019-03-26 stsp goto done;
1958 2ec1f75b 2019-03-26 stsp }
1959 2ec1f75b 2019-03-26 stsp
1960 2ec1f75b 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
1961 2ec1f75b 2019-03-26 stsp if (err)
1962 2ec1f75b 2019-03-26 stsp goto done;
1963 2ec1f75b 2019-03-26 stsp
1964 2ec1f75b 2019-03-26 stsp ie = got_fileindex_entry_get(fileindex, relpath);
1965 2ec1f75b 2019-03-26 stsp if (ie == NULL) {
1966 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_BAD_PATH);
1967 2ec1f75b 2019-03-26 stsp goto done;
1968 2ec1f75b 2019-03-26 stsp }
1969 2ec1f75b 2019-03-26 stsp
1970 2ec1f75b 2019-03-26 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1971 2ec1f75b 2019-03-26 stsp if (err)
1972 2ec1f75b 2019-03-26 stsp goto done;
1973 2ec1f75b 2019-03-26 stsp
1974 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_NO_CHANGE) {
1975 71a29355 2019-03-27 stsp if (status == GOT_STATUS_DELETE) {
1976 2af4a041 2019-05-11 jcs err = got_error_set_errno(ENOENT, ondisk_path);
1977 71a29355 2019-03-27 stsp goto done;
1978 71a29355 2019-03-27 stsp }
1979 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY) {
1980 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_STATUS);
1981 2ec1f75b 2019-03-26 stsp goto done;
1982 2ec1f75b 2019-03-26 stsp }
1983 2ec1f75b 2019-03-26 stsp if (!delete_local_mods) {
1984 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_MODIFIED);
1985 2ec1f75b 2019-03-26 stsp goto done;
1986 2ec1f75b 2019-03-26 stsp }
1987 2ec1f75b 2019-03-26 stsp }
1988 2ec1f75b 2019-03-26 stsp
1989 2ec1f75b 2019-03-26 stsp if (unlink(ondisk_path) != 0) {
1990 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("unlink", ondisk_path);
1991 2ec1f75b 2019-03-26 stsp goto done;
1992 2ec1f75b 2019-03-26 stsp }
1993 2ec1f75b 2019-03-26 stsp
1994 2ec1f75b 2019-03-26 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1995 2ec1f75b 2019-03-26 stsp
1996 2ec1f75b 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1997 2ec1f75b 2019-03-26 stsp fileindex_path);
1998 2ec1f75b 2019-03-26 stsp if (err)
1999 2ec1f75b 2019-03-26 stsp goto done;
2000 2ec1f75b 2019-03-26 stsp
2001 2ec1f75b 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
2002 2ec1f75b 2019-03-26 stsp if (err)
2003 2ec1f75b 2019-03-26 stsp goto done;
2004 2ec1f75b 2019-03-26 stsp
2005 2ec1f75b 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2006 230a42bd 2019-05-11 jcs err = got_error_prefix_errno3("rename", new_fileindex_path,
2007 230a42bd 2019-05-11 jcs fileindex_path);
2008 2ec1f75b 2019-03-26 stsp goto done;
2009 2ec1f75b 2019-03-26 stsp }
2010 2ec1f75b 2019-03-26 stsp
2011 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
2012 2ec1f75b 2019-03-26 stsp new_fileindex_path = NULL;
2013 2ec1f75b 2019-03-26 stsp
2014 2ec1f75b 2019-03-26 stsp err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2015 2ec1f75b 2019-03-26 stsp done:
2016 2ec1f75b 2019-03-26 stsp free(relpath);
2017 2ec1f75b 2019-03-26 stsp if (index) {
2018 2ec1f75b 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2019 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
2020 2ec1f75b 2019-03-26 stsp }
2021 2ec1f75b 2019-03-26 stsp if (new_fileindex_path) {
2022 2ec1f75b 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
2023 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("unlink",
2024 230a42bd 2019-05-11 jcs new_fileindex_path);
2025 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
2026 2ec1f75b 2019-03-26 stsp }
2027 2ec1f75b 2019-03-26 stsp if (fileindex)
2028 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2029 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2030 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2031 2ec1f75b 2019-03-26 stsp err = unlockerr;
2032 2ec1f75b 2019-03-26 stsp return err;
2033 2ec1f75b 2019-03-26 stsp }
2034 a129376b 2019-03-28 stsp
2035 a129376b 2019-03-28 stsp const struct got_error *
2036 a129376b 2019-03-28 stsp got_worktree_revert(struct got_worktree *worktree,
2037 a129376b 2019-03-28 stsp const char *ondisk_path,
2038 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2039 a129376b 2019-03-28 stsp struct got_repository *repo)
2040 a129376b 2019-03-28 stsp {
2041 a129376b 2019-03-28 stsp struct got_fileindex *fileindex = NULL;
2042 a129376b 2019-03-28 stsp struct got_fileindex_entry *ie = NULL;
2043 a129376b 2019-03-28 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2044 a129376b 2019-03-28 stsp char *tree_path = NULL, *parent_path, *te_name;
2045 a129376b 2019-03-28 stsp FILE *index = NULL, *new_index = NULL;
2046 a129376b 2019-03-28 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2047 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2048 a129376b 2019-03-28 stsp struct got_object_id id, *tree_id = NULL;
2049 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2050 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2051 a129376b 2019-03-28 stsp unsigned char status;
2052 a129376b 2019-03-28 stsp struct stat sb;
2053 a129376b 2019-03-28 stsp
2054 a129376b 2019-03-28 stsp err = lock_worktree(worktree, LOCK_EX);
2055 a129376b 2019-03-28 stsp if (err)
2056 a129376b 2019-03-28 stsp return err;
2057 a129376b 2019-03-28 stsp
2058 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2059 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2060 a129376b 2019-03-28 stsp if (err)
2061 a129376b 2019-03-28 stsp goto done;
2062 a129376b 2019-03-28 stsp
2063 a129376b 2019-03-28 stsp fileindex = got_fileindex_alloc();
2064 a129376b 2019-03-28 stsp if (fileindex == NULL) {
2065 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_fileindex_alloc");
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 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2070 a129376b 2019-03-28 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2071 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
2072 a129376b 2019-03-28 stsp fileindex_path = NULL;
2073 a129376b 2019-03-28 stsp goto done;
2074 a129376b 2019-03-28 stsp }
2075 a129376b 2019-03-28 stsp
2076 a129376b 2019-03-28 stsp index = fopen(fileindex_path, "rb");
2077 a129376b 2019-03-28 stsp if (index == NULL) {
2078 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("fopen", fileindex_path);
2079 a129376b 2019-03-28 stsp goto done;
2080 a129376b 2019-03-28 stsp }
2081 a129376b 2019-03-28 stsp
2082 a129376b 2019-03-28 stsp err = got_fileindex_read(fileindex, index);
2083 a129376b 2019-03-28 stsp if (err)
2084 a129376b 2019-03-28 stsp goto done;
2085 a129376b 2019-03-28 stsp
2086 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2087 a129376b 2019-03-28 stsp if (ie == NULL) {
2088 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2089 a129376b 2019-03-28 stsp goto done;
2090 a129376b 2019-03-28 stsp }
2091 a129376b 2019-03-28 stsp
2092 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2093 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2094 a129376b 2019-03-28 stsp if (err) {
2095 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2096 a129376b 2019-03-28 stsp goto done;
2097 a129376b 2019-03-28 stsp parent_path = "/";
2098 a129376b 2019-03-28 stsp }
2099 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2100 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2101 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2102 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
2103 a129376b 2019-03-28 stsp goto done;
2104 a129376b 2019-03-28 stsp }
2105 a129376b 2019-03-28 stsp } else {
2106 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2107 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2108 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2109 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
2110 a129376b 2019-03-28 stsp goto done;
2111 a129376b 2019-03-28 stsp }
2112 a129376b 2019-03-28 stsp } else {
2113 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2114 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2115 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
2116 a129376b 2019-03-28 stsp goto done;
2117 a129376b 2019-03-28 stsp }
2118 a129376b 2019-03-28 stsp }
2119 a129376b 2019-03-28 stsp }
2120 a129376b 2019-03-28 stsp
2121 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2122 a129376b 2019-03-28 stsp tree_path);
2123 a129376b 2019-03-28 stsp if (err)
2124 a129376b 2019-03-28 stsp goto done;
2125 a129376b 2019-03-28 stsp
2126 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2127 a129376b 2019-03-28 stsp if (err)
2128 a129376b 2019-03-28 stsp goto done;
2129 a129376b 2019-03-28 stsp
2130 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2131 a129376b 2019-03-28 stsp if (te_name == NULL) {
2132 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("basename", ie->path);
2133 a129376b 2019-03-28 stsp goto done;
2134 a129376b 2019-03-28 stsp }
2135 a129376b 2019-03-28 stsp
2136 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2137 a129376b 2019-03-28 stsp if (err)
2138 a129376b 2019-03-28 stsp goto done;
2139 a129376b 2019-03-28 stsp
2140 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2141 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2142 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2143 a129376b 2019-03-28 stsp goto done;
2144 a129376b 2019-03-28 stsp }
2145 a129376b 2019-03-28 stsp
2146 a129376b 2019-03-28 stsp switch (status) {
2147 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2148 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2149 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2150 a129376b 2019-03-28 stsp break;
2151 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2152 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2153 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2154 a129376b 2019-03-28 stsp case GOT_STATUS_MISSING:
2155 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2156 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2157 a129376b 2019-03-28 stsp if (err)
2158 a129376b 2019-03-28 stsp goto done;
2159 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2160 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2161 a129376b 2019-03-28 stsp progress_arg);
2162 a129376b 2019-03-28 stsp if (err)
2163 a129376b 2019-03-28 stsp goto done;
2164 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2165 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2166 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2167 a129376b 2019-03-28 stsp if (err)
2168 a129376b 2019-03-28 stsp goto done;
2169 a129376b 2019-03-28 stsp }
2170 a129376b 2019-03-28 stsp break;
2171 a129376b 2019-03-28 stsp default:
2172 a129376b 2019-03-28 stsp goto done;
2173 a129376b 2019-03-28 stsp }
2174 a129376b 2019-03-28 stsp
2175 a129376b 2019-03-28 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2176 a129376b 2019-03-28 stsp fileindex_path);
2177 a129376b 2019-03-28 stsp if (err)
2178 a129376b 2019-03-28 stsp goto done;
2179 a129376b 2019-03-28 stsp
2180 a129376b 2019-03-28 stsp err = got_fileindex_write(fileindex, new_index);
2181 a129376b 2019-03-28 stsp if (err)
2182 a129376b 2019-03-28 stsp goto done;
2183 a129376b 2019-03-28 stsp
2184 a129376b 2019-03-28 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2185 230a42bd 2019-05-11 jcs err = got_error_prefix_errno3("rename", new_fileindex_path,
2186 230a42bd 2019-05-11 jcs fileindex_path);
2187 a129376b 2019-03-28 stsp goto done;
2188 a129376b 2019-03-28 stsp }
2189 a129376b 2019-03-28 stsp
2190 a129376b 2019-03-28 stsp free(new_fileindex_path);
2191 a129376b 2019-03-28 stsp new_fileindex_path = NULL;
2192 a129376b 2019-03-28 stsp done:
2193 a129376b 2019-03-28 stsp free(relpath);
2194 a129376b 2019-03-28 stsp free(tree_path);
2195 a129376b 2019-03-28 stsp if (blob)
2196 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2197 a129376b 2019-03-28 stsp if (tree)
2198 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2199 a129376b 2019-03-28 stsp free(tree_id);
2200 a129376b 2019-03-28 stsp if (index) {
2201 a129376b 2019-03-28 stsp if (fclose(index) != 0 && err == NULL)
2202 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
2203 a129376b 2019-03-28 stsp }
2204 a129376b 2019-03-28 stsp if (new_fileindex_path) {
2205 a129376b 2019-03-28 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
2206 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("unlink",
2207 230a42bd 2019-05-11 jcs new_fileindex_path);
2208 a129376b 2019-03-28 stsp free(new_fileindex_path);
2209 a129376b 2019-03-28 stsp }
2210 a129376b 2019-03-28 stsp if (fileindex)
2211 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2212 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2213 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2214 a129376b 2019-03-28 stsp err = unlockerr;
2215 c4296144 2019-05-09 stsp return err;
2216 c4296144 2019-05-09 stsp }
2217 cf066bf8 2019-05-09 stsp
2218 ed175427 2019-05-09 stsp struct commitable {
2219 24519714 2019-05-09 stsp char *path;
2220 44d03001 2019-05-09 stsp char *in_repo_path;
2221 768aea60 2019-05-09 stsp char *ondisk_path;
2222 cf066bf8 2019-05-09 stsp unsigned char status;
2223 e75eb4da 2019-05-10 stsp struct got_object_id *blob_id;
2224 ca2503ea 2019-05-09 stsp struct got_object_id *base_id;
2225 768aea60 2019-05-09 stsp mode_t mode;
2226 cf066bf8 2019-05-09 stsp };
2227 c4296144 2019-05-09 stsp
2228 cf066bf8 2019-05-09 stsp static void
2229 ed175427 2019-05-09 stsp free_commitable(struct commitable *ct)
2230 cf066bf8 2019-05-09 stsp {
2231 24519714 2019-05-09 stsp free(ct->path);
2232 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2233 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2234 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2235 ca2503ea 2019-05-09 stsp free(ct->base_id);
2236 cf066bf8 2019-05-09 stsp free(ct);
2237 cf066bf8 2019-05-09 stsp }
2238 24519714 2019-05-09 stsp
2239 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2240 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2241 24519714 2019-05-09 stsp struct got_repository *repo;
2242 24519714 2019-05-09 stsp struct got_worktree *worktree;
2243 24519714 2019-05-09 stsp };
2244 cf066bf8 2019-05-09 stsp
2245 c4296144 2019-05-09 stsp static const struct got_error *
2246 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2247 c4296144 2019-05-09 stsp struct got_object_id *id)
2248 c4296144 2019-05-09 stsp {
2249 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2250 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2251 ed175427 2019-05-09 stsp struct commitable *ct = NULL;
2252 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2253 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2254 768aea60 2019-05-09 stsp struct stat sb;
2255 c4296144 2019-05-09 stsp
2256 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2257 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2258 c4296144 2019-05-09 stsp
2259 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2260 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2261 c4296144 2019-05-09 stsp return NULL;
2262 0b5cc0d6 2019-05-09 stsp
2263 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2264 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
2265 036813ee 2019-05-09 stsp goto done;
2266 036813ee 2019-05-09 stsp }
2267 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2268 036813ee 2019-05-09 stsp parent_path = strdup("");
2269 69960a46 2019-05-09 stsp if (parent_path == NULL)
2270 230a42bd 2019-05-11 jcs return got_error_prefix_errno("strdup");
2271 69960a46 2019-05-09 stsp } else {
2272 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2273 69960a46 2019-05-09 stsp if (err)
2274 69960a46 2019-05-09 stsp return err;
2275 69960a46 2019-05-09 stsp }
2276 c4296144 2019-05-09 stsp
2277 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2278 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2279 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("calloc");
2280 c4296144 2019-05-09 stsp goto done;
2281 768aea60 2019-05-09 stsp }
2282 768aea60 2019-05-09 stsp
2283 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2284 768aea60 2019-05-09 stsp relpath) == -1) {
2285 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
2286 768aea60 2019-05-09 stsp goto done;
2287 768aea60 2019-05-09 stsp }
2288 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2289 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2290 768aea60 2019-05-09 stsp } else {
2291 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2292 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("lstat", ct->ondisk_path);
2293 768aea60 2019-05-09 stsp goto done;
2294 768aea60 2019-05-09 stsp }
2295 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2296 c4296144 2019-05-09 stsp }
2297 c4296144 2019-05-09 stsp
2298 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2299 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2300 44d03001 2019-05-09 stsp relpath) == -1) {
2301 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
2302 44d03001 2019-05-09 stsp goto done;
2303 44d03001 2019-05-09 stsp }
2304 44d03001 2019-05-09 stsp
2305 cf066bf8 2019-05-09 stsp ct->status = status;
2306 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2307 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2308 036813ee 2019-05-09 stsp ct->base_id = got_object_id_dup(id);
2309 036813ee 2019-05-09 stsp if (ct->base_id == NULL) {
2310 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
2311 036813ee 2019-05-09 stsp goto done;
2312 036813ee 2019-05-09 stsp }
2313 ca2503ea 2019-05-09 stsp }
2314 24519714 2019-05-09 stsp ct->path = strdup(path);
2315 24519714 2019-05-09 stsp if (ct->path == NULL) {
2316 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
2317 24519714 2019-05-09 stsp goto done;
2318 24519714 2019-05-09 stsp }
2319 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2320 c4296144 2019-05-09 stsp done:
2321 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2322 ed175427 2019-05-09 stsp free_commitable(ct);
2323 24519714 2019-05-09 stsp free(parent_path);
2324 036813ee 2019-05-09 stsp free(path);
2325 a129376b 2019-03-28 stsp return err;
2326 a129376b 2019-03-28 stsp }
2327 c4296144 2019-05-09 stsp
2328 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2329 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2330 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2331 036813ee 2019-05-09 stsp struct got_repository *);
2332 ed175427 2019-05-09 stsp
2333 ed175427 2019-05-09 stsp static const struct got_error *
2334 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2335 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2336 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2337 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2338 afa376bf 2019-05-09 stsp struct got_repository *repo)
2339 ed175427 2019-05-09 stsp {
2340 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2341 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2342 036813ee 2019-05-09 stsp char *subpath;
2343 ed175427 2019-05-09 stsp
2344 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2345 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2346 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
2347 ed175427 2019-05-09 stsp
2348 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2349 ed175427 2019-05-09 stsp if (err)
2350 ed175427 2019-05-09 stsp return err;
2351 ed175427 2019-05-09 stsp
2352 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2353 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2354 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2355 036813ee 2019-05-09 stsp free(subpath);
2356 036813ee 2019-05-09 stsp return err;
2357 036813ee 2019-05-09 stsp }
2358 ed175427 2019-05-09 stsp
2359 036813ee 2019-05-09 stsp static const struct got_error *
2360 036813ee 2019-05-09 stsp match_ct_parent_path(int *match, struct commitable *ct, const char *path)
2361 036813ee 2019-05-09 stsp {
2362 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2363 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2364 ed175427 2019-05-09 stsp
2365 036813ee 2019-05-09 stsp *match = 0;
2366 ed175427 2019-05-09 stsp
2367 036813ee 2019-05-09 stsp if (strchr(ct->path, '/') == NULL) {
2368 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2369 0f63689d 2019-05-10 stsp return NULL;
2370 036813ee 2019-05-09 stsp }
2371 0b5cc0d6 2019-05-09 stsp
2372 0f63689d 2019-05-10 stsp err = got_path_dirname(&ct_parent_path, ct->path);
2373 0f63689d 2019-05-10 stsp if (err)
2374 0f63689d 2019-05-10 stsp return err;
2375 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2376 036813ee 2019-05-09 stsp free(ct_parent_path);
2377 036813ee 2019-05-09 stsp return err;
2378 036813ee 2019-05-09 stsp }
2379 0b5cc0d6 2019-05-09 stsp
2380 768aea60 2019-05-09 stsp static mode_t
2381 768aea60 2019-05-09 stsp get_ct_file_mode(struct commitable *ct)
2382 768aea60 2019-05-09 stsp {
2383 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2384 768aea60 2019-05-09 stsp }
2385 768aea60 2019-05-09 stsp
2386 036813ee 2019-05-09 stsp static const struct got_error *
2387 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2388 036813ee 2019-05-09 stsp struct got_tree_entry *te, struct commitable *ct)
2389 036813ee 2019-05-09 stsp {
2390 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2391 ca2503ea 2019-05-09 stsp
2392 036813ee 2019-05-09 stsp *new_te = NULL;
2393 0b5cc0d6 2019-05-09 stsp
2394 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2395 036813ee 2019-05-09 stsp if (err)
2396 036813ee 2019-05-09 stsp goto done;
2397 0b5cc0d6 2019-05-09 stsp
2398 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2399 036813ee 2019-05-09 stsp
2400 036813ee 2019-05-09 stsp free((*new_te)->id);
2401 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2402 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2403 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
2404 036813ee 2019-05-09 stsp goto done;
2405 036813ee 2019-05-09 stsp }
2406 036813ee 2019-05-09 stsp done:
2407 036813ee 2019-05-09 stsp if (err && *new_te) {
2408 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2409 036813ee 2019-05-09 stsp *new_te = NULL;
2410 036813ee 2019-05-09 stsp }
2411 036813ee 2019-05-09 stsp return err;
2412 036813ee 2019-05-09 stsp }
2413 036813ee 2019-05-09 stsp
2414 036813ee 2019-05-09 stsp static const struct got_error *
2415 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2416 036813ee 2019-05-09 stsp struct commitable *ct)
2417 036813ee 2019-05-09 stsp {
2418 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2419 036813ee 2019-05-09 stsp char *ct_name;
2420 036813ee 2019-05-09 stsp
2421 036813ee 2019-05-09 stsp *new_te = NULL;
2422 036813ee 2019-05-09 stsp
2423 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2424 036813ee 2019-05-09 stsp if (*new_te == NULL)
2425 230a42bd 2019-05-11 jcs return got_error_prefix_errno("calloc");
2426 036813ee 2019-05-09 stsp
2427 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2428 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2429 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("basename", ct->path);
2430 036813ee 2019-05-09 stsp goto done;
2431 036813ee 2019-05-09 stsp }
2432 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2433 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2434 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
2435 036813ee 2019-05-09 stsp goto done;
2436 036813ee 2019-05-09 stsp }
2437 036813ee 2019-05-09 stsp
2438 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2439 036813ee 2019-05-09 stsp
2440 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2441 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2442 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
2443 036813ee 2019-05-09 stsp goto done;
2444 036813ee 2019-05-09 stsp }
2445 036813ee 2019-05-09 stsp done:
2446 036813ee 2019-05-09 stsp if (err && *new_te) {
2447 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2448 036813ee 2019-05-09 stsp *new_te = NULL;
2449 036813ee 2019-05-09 stsp }
2450 036813ee 2019-05-09 stsp return err;
2451 036813ee 2019-05-09 stsp }
2452 036813ee 2019-05-09 stsp
2453 036813ee 2019-05-09 stsp static const struct got_error *
2454 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
2455 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
2456 036813ee 2019-05-09 stsp {
2457 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2458 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
2459 036813ee 2019-05-09 stsp
2460 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2461 036813ee 2019-05-09 stsp if (err)
2462 036813ee 2019-05-09 stsp return err;
2463 036813ee 2019-05-09 stsp if (new_pe == NULL)
2464 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2465 036813ee 2019-05-09 stsp return NULL;
2466 afa376bf 2019-05-09 stsp }
2467 afa376bf 2019-05-09 stsp
2468 afa376bf 2019-05-09 stsp static const struct got_error *
2469 afa376bf 2019-05-09 stsp report_ct_status(struct commitable *ct,
2470 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
2471 afa376bf 2019-05-09 stsp {
2472 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
2473 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
2474 afa376bf 2019-05-09 stsp ct_path++;
2475 e75eb4da 2019-05-10 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id);
2476 036813ee 2019-05-09 stsp }
2477 036813ee 2019-05-09 stsp
2478 036813ee 2019-05-09 stsp static const struct got_error *
2479 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
2480 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2481 44d03001 2019-05-09 stsp {
2482 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
2483 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
2484 44d03001 2019-05-09 stsp char *te_path;
2485 44d03001 2019-05-09 stsp
2486 44d03001 2019-05-09 stsp *modified = 0;
2487 44d03001 2019-05-09 stsp
2488 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
2489 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
2490 44d03001 2019-05-09 stsp te->name) == -1)
2491 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
2492 44d03001 2019-05-09 stsp
2493 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2494 44d03001 2019-05-09 stsp struct commitable *ct = pe->data;
2495 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
2496 44d03001 2019-05-09 stsp strlen(te_path));
2497 44d03001 2019-05-09 stsp if (*modified)
2498 44d03001 2019-05-09 stsp break;
2499 44d03001 2019-05-09 stsp }
2500 44d03001 2019-05-09 stsp
2501 44d03001 2019-05-09 stsp free(te_path);
2502 44d03001 2019-05-09 stsp return err;
2503 44d03001 2019-05-09 stsp }
2504 44d03001 2019-05-09 stsp
2505 44d03001 2019-05-09 stsp static const struct got_error *
2506 036813ee 2019-05-09 stsp match_deleted_or_modified_ct(struct commitable **ctp,
2507 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
2508 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
2509 036813ee 2019-05-09 stsp {
2510 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2511 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2512 036813ee 2019-05-09 stsp
2513 036813ee 2019-05-09 stsp *ctp = NULL;
2514 036813ee 2019-05-09 stsp
2515 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2516 036813ee 2019-05-09 stsp struct commitable *ct = pe->data;
2517 036813ee 2019-05-09 stsp char *ct_name = NULL;
2518 036813ee 2019-05-09 stsp int path_matches;
2519 036813ee 2019-05-09 stsp
2520 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
2521 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
2522 036813ee 2019-05-09 stsp continue;
2523 036813ee 2019-05-09 stsp
2524 036813ee 2019-05-09 stsp if (got_object_id_cmp(ct->base_id, te->id) != 0)
2525 036813ee 2019-05-09 stsp continue;
2526 036813ee 2019-05-09 stsp
2527 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2528 036813ee 2019-05-09 stsp if (err)
2529 036813ee 2019-05-09 stsp return err;
2530 036813ee 2019-05-09 stsp if (!path_matches)
2531 036813ee 2019-05-09 stsp continue;
2532 036813ee 2019-05-09 stsp
2533 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
2534 036813ee 2019-05-09 stsp if (ct_name == NULL)
2535 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("basename", pe->path);
2536 036813ee 2019-05-09 stsp
2537 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
2538 036813ee 2019-05-09 stsp continue;
2539 036813ee 2019-05-09 stsp
2540 036813ee 2019-05-09 stsp *ctp = ct;
2541 036813ee 2019-05-09 stsp break;
2542 036813ee 2019-05-09 stsp }
2543 036813ee 2019-05-09 stsp
2544 036813ee 2019-05-09 stsp return err;
2545 036813ee 2019-05-09 stsp }
2546 036813ee 2019-05-09 stsp
2547 036813ee 2019-05-09 stsp static const struct got_error *
2548 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
2549 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
2550 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2551 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2552 036813ee 2019-05-09 stsp struct got_repository *repo)
2553 036813ee 2019-05-09 stsp {
2554 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2555 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
2556 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
2557 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
2558 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
2559 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2560 036813ee 2019-05-09 stsp
2561 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
2562 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
2563 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
2564 036813ee 2019-05-09 stsp
2565 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
2566 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2567 036813ee 2019-05-09 stsp struct commitable *ct = pe->data;
2568 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
2569 036813ee 2019-05-09 stsp
2570 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD)
2571 036813ee 2019-05-09 stsp continue;
2572 036813ee 2019-05-09 stsp
2573 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
2574 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
2575 036813ee 2019-05-09 stsp continue;
2576 036813ee 2019-05-09 stsp
2577 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2578 036813ee 2019-05-09 stsp pe->path);
2579 036813ee 2019-05-09 stsp if (err)
2580 036813ee 2019-05-09 stsp goto done;
2581 036813ee 2019-05-09 stsp
2582 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
2583 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
2584 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
2585 036813ee 2019-05-09 stsp if (err)
2586 036813ee 2019-05-09 stsp goto done;
2587 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
2588 afa376bf 2019-05-09 stsp if (err)
2589 afa376bf 2019-05-09 stsp goto done;
2590 036813ee 2019-05-09 stsp } else {
2591 2f51b5b3 2019-05-09 stsp char *subtree_path;
2592 baa7dcfa 2019-05-09 stsp struct got_pathlist_entry *pe2;
2593 baa7dcfa 2019-05-09 stsp int visited = 0;
2594 036813ee 2019-05-09 stsp
2595 2f51b5b3 2019-05-09 stsp *slash = '\0'; /* trim trailing path components */
2596 baa7dcfa 2019-05-09 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2597 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
2598 2f51b5b3 2019-05-09 stsp child_path) == -1) {
2599 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
2600 2f51b5b3 2019-05-09 stsp goto done;
2601 2f51b5b3 2019-05-09 stsp }
2602 baa7dcfa 2019-05-09 stsp TAILQ_FOREACH(pe2, &paths, entry) {
2603 baa7dcfa 2019-05-09 stsp if (got_path_cmp(subtree_path, pe2->path) != 0)
2604 baa7dcfa 2019-05-09 stsp continue;
2605 baa7dcfa 2019-05-09 stsp visited = 1;
2606 baa7dcfa 2019-05-09 stsp break;
2607 baa7dcfa 2019-05-09 stsp }
2608 ce0de6b6 2019-05-10 stsp if (visited) {
2609 ce0de6b6 2019-05-10 stsp free(subtree_path);
2610 baa7dcfa 2019-05-09 stsp continue;
2611 ce0de6b6 2019-05-10 stsp }
2612 baa7dcfa 2019-05-09 stsp
2613 9ba0479c 2019-05-10 stsp new_te = calloc(1, sizeof(*new_te));
2614 9ba0479c 2019-05-10 stsp new_te->mode = S_IFDIR;
2615 9ba0479c 2019-05-10 stsp new_te->name = strdup(child_path);
2616 9ba0479c 2019-05-10 stsp if (new_te->name == NULL) {
2617 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
2618 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2619 9ba0479c 2019-05-10 stsp new_te = NULL;
2620 9ba0479c 2019-05-10 stsp goto done;
2621 9ba0479c 2019-05-10 stsp }
2622 baa7dcfa 2019-05-09 stsp err = write_tree(&new_te->id, NULL, subtree_path,
2623 afa376bf 2019-05-09 stsp commitable_paths, status_cb, status_arg, repo);
2624 2f51b5b3 2019-05-09 stsp free(subtree_path);
2625 9ba0479c 2019-05-10 stsp if (err) {
2626 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2627 9ba0479c 2019-05-10 stsp new_te = NULL;
2628 036813ee 2019-05-09 stsp goto done;
2629 9ba0479c 2019-05-10 stsp }
2630 ed175427 2019-05-09 stsp }
2631 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2632 2f51b5b3 2019-05-09 stsp if (err)
2633 2f51b5b3 2019-05-09 stsp goto done;
2634 2f51b5b3 2019-05-09 stsp }
2635 2f51b5b3 2019-05-09 stsp
2636 2f51b5b3 2019-05-09 stsp if (base_tree) {
2637 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
2638 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
2639 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2640 2f51b5b3 2019-05-09 stsp struct commitable *ct = NULL;
2641 2f51b5b3 2019-05-09 stsp
2642 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
2643 44d03001 2019-05-09 stsp int modified;
2644 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2645 036813ee 2019-05-09 stsp if (err)
2646 036813ee 2019-05-09 stsp goto done;
2647 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
2648 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
2649 2f51b5b3 2019-05-09 stsp if (err)
2650 2f51b5b3 2019-05-09 stsp goto done;
2651 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
2652 44d03001 2019-05-09 stsp if (modified) {
2653 44d03001 2019-05-09 stsp free(new_te->id);
2654 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
2655 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
2656 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
2657 44d03001 2019-05-09 stsp if (err)
2658 44d03001 2019-05-09 stsp goto done;
2659 44d03001 2019-05-09 stsp }
2660 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2661 036813ee 2019-05-09 stsp if (err)
2662 036813ee 2019-05-09 stsp goto done;
2663 2f51b5b3 2019-05-09 stsp continue;
2664 036813ee 2019-05-09 stsp }
2665 2f51b5b3 2019-05-09 stsp
2666 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
2667 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
2668 2f51b5b3 2019-05-09 stsp if (ct) {
2669 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
2670 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
2671 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
2672 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
2673 2f51b5b3 2019-05-09 stsp if (err)
2674 2f51b5b3 2019-05-09 stsp goto done;
2675 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2676 2f51b5b3 2019-05-09 stsp if (err)
2677 2f51b5b3 2019-05-09 stsp goto done;
2678 2f51b5b3 2019-05-09 stsp }
2679 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
2680 afa376bf 2019-05-09 stsp if (err)
2681 afa376bf 2019-05-09 stsp goto done;
2682 2f51b5b3 2019-05-09 stsp } else {
2683 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
2684 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2685 2f51b5b3 2019-05-09 stsp if (err)
2686 2f51b5b3 2019-05-09 stsp goto done;
2687 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2688 2f51b5b3 2019-05-09 stsp if (err)
2689 2f51b5b3 2019-05-09 stsp goto done;
2690 2f51b5b3 2019-05-09 stsp }
2691 ca2503ea 2019-05-09 stsp }
2692 ed175427 2019-05-09 stsp }
2693 0b5cc0d6 2019-05-09 stsp
2694 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
2695 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
2696 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
2697 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
2698 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2699 0b5cc0d6 2019-05-09 stsp }
2700 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2701 ed175427 2019-05-09 stsp done:
2702 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
2703 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
2704 ed175427 2019-05-09 stsp return err;
2705 ed175427 2019-05-09 stsp }
2706 ed175427 2019-05-09 stsp
2707 ebf99748 2019-05-09 stsp static const struct got_error *
2708 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2709 ebf99748 2019-05-09 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2710 ebf99748 2019-05-09 stsp {
2711 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2712 ebf99748 2019-05-09 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
2713 ebf99748 2019-05-09 stsp struct got_fileindex *fileindex = NULL;
2714 ebf99748 2019-05-09 stsp FILE *new_index = NULL;
2715 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
2716 ebf99748 2019-05-09 stsp
2717 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2718 ebf99748 2019-05-09 stsp if (err)
2719 ebf99748 2019-05-09 stsp return err;
2720 ebf99748 2019-05-09 stsp
2721 ebf99748 2019-05-09 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2722 ebf99748 2019-05-09 stsp fileindex_path);
2723 ebf99748 2019-05-09 stsp if (err)
2724 ebf99748 2019-05-09 stsp goto done;
2725 ebf99748 2019-05-09 stsp
2726 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2727 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
2728 ebf99748 2019-05-09 stsp struct commitable *ct = pe->data;
2729 ebf99748 2019-05-09 stsp
2730 ebf99748 2019-05-09 stsp ie = got_fileindex_entry_get(fileindex, pe->path);
2731 ebf99748 2019-05-09 stsp if (ie) {
2732 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
2733 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
2734 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
2735 ebf99748 2019-05-09 stsp } else
2736 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
2737 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
2738 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
2739 ebf99748 2019-05-09 stsp } else {
2740 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
2741 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
2742 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
2743 ebf99748 2019-05-09 stsp if (err)
2744 ebf99748 2019-05-09 stsp goto done;
2745 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
2746 ebf99748 2019-05-09 stsp if (err)
2747 ebf99748 2019-05-09 stsp goto done;
2748 ebf99748 2019-05-09 stsp }
2749 ebf99748 2019-05-09 stsp }
2750 ebf99748 2019-05-09 stsp
2751 ebf99748 2019-05-09 stsp err = got_fileindex_write(fileindex, new_index);
2752 ebf99748 2019-05-09 stsp if (err)
2753 ebf99748 2019-05-09 stsp goto done;
2754 ebf99748 2019-05-09 stsp
2755 ebf99748 2019-05-09 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2756 230a42bd 2019-05-11 jcs err = got_error_prefix_errno3("rename", new_fileindex_path,
2757 230a42bd 2019-05-11 jcs fileindex_path);
2758 ebf99748 2019-05-09 stsp unlink(new_fileindex_path);
2759 ebf99748 2019-05-09 stsp goto done;
2760 ebf99748 2019-05-09 stsp }
2761 ebf99748 2019-05-09 stsp
2762 ebf99748 2019-05-09 stsp free(new_fileindex_path);
2763 ebf99748 2019-05-09 stsp new_fileindex_path = NULL;
2764 ebf99748 2019-05-09 stsp
2765 ebf99748 2019-05-09 stsp done:
2766 ebf99748 2019-05-09 stsp if (new_fileindex_path)
2767 ebf99748 2019-05-09 stsp unlink(new_fileindex_path);
2768 ebf99748 2019-05-09 stsp if (new_index)
2769 ebf99748 2019-05-09 stsp fclose(new_index);
2770 ebf99748 2019-05-09 stsp free(new_fileindex_path);
2771 ebf99748 2019-05-09 stsp free(fileindex_path);
2772 ebf99748 2019-05-09 stsp got_fileindex_free(fileindex);
2773 d56d26ce 2019-05-10 stsp return err;
2774 d56d26ce 2019-05-10 stsp }
2775 d56d26ce 2019-05-10 stsp
2776 d56d26ce 2019-05-10 stsp static const struct got_error *
2777 d56d26ce 2019-05-10 stsp check_ct_out_of_date(struct commitable *ct, struct got_repository *repo,
2778 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
2779 d56d26ce 2019-05-10 stsp {
2780 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
2781 d56d26ce 2019-05-10 stsp struct got_object_id *id_in_head;
2782 d56d26ce 2019-05-10 stsp
2783 1251a9e5 2019-05-10 stsp /*
2784 1251a9e5 2019-05-10 stsp * XXX This should probably be checking each commit from
2785 1251a9e5 2019-05-10 stsp * worktree's base_commit -> head and verify that the
2786 1251a9e5 2019-05-10 stsp * same blob exists in each of these commits.
2787 1251a9e5 2019-05-10 stsp * Removals+additions within this line of history could mean
2788 1251a9e5 2019-05-10 stsp * that renames have occured in which case we should really
2789 1251a9e5 2019-05-10 stsp * be forcing the user to run an update...
2790 1251a9e5 2019-05-10 stsp */
2791 d56d26ce 2019-05-10 stsp err = got_object_id_by_path(&id_in_head, repo,
2792 d56d26ce 2019-05-10 stsp head_commit_id, ct->in_repo_path);
2793 d56d26ce 2019-05-10 stsp if (err) {
2794 d56d26ce 2019-05-10 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
2795 d56d26ce 2019-05-10 stsp return err;
2796 d56d26ce 2019-05-10 stsp if (ct->status != GOT_STATUS_ADD)
2797 d56d26ce 2019-05-10 stsp return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2798 d56d26ce 2019-05-10 stsp err = NULL;
2799 d56d26ce 2019-05-10 stsp id_in_head = NULL;
2800 d56d26ce 2019-05-10 stsp }
2801 d56d26ce 2019-05-10 stsp
2802 d56d26ce 2019-05-10 stsp if (id_in_head && got_object_id_cmp(id_in_head, ct->base_id) != 0)
2803 d56d26ce 2019-05-10 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2804 d56d26ce 2019-05-10 stsp
2805 d56d26ce 2019-05-10 stsp free(id_in_head);
2806 ebf99748 2019-05-09 stsp return err;
2807 ebf99748 2019-05-09 stsp }
2808 ebf99748 2019-05-09 stsp
2809 c4296144 2019-05-09 stsp const struct got_error *
2810 c4296144 2019-05-09 stsp got_worktree_commit(struct got_object_id **new_commit_id,
2811 c4296144 2019-05-09 stsp struct got_worktree *worktree, const char *ondisk_path,
2812 afa376bf 2019-05-09 stsp const char *author, const char *committer, const char *logmsg,
2813 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2814 afa376bf 2019-05-09 stsp struct got_repository *repo)
2815 c4296144 2019-05-09 stsp {
2816 c4296144 2019-05-09 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2817 ed175427 2019-05-09 stsp struct collect_commitables_arg cc_arg;
2818 036813ee 2019-05-09 stsp struct got_pathlist_head commitable_paths;
2819 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
2820 bc70eb79 2019-05-09 stsp char *relpath = NULL;
2821 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
2822 36a38700 2019-05-10 stsp struct got_reference *head_ref = NULL;
2823 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
2824 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id = NULL;
2825 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
2826 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
2827 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
2828 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
2829 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
2830 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
2831 c4296144 2019-05-09 stsp
2832 c4296144 2019-05-09 stsp *new_commit_id = NULL;
2833 c4296144 2019-05-09 stsp
2834 036813ee 2019-05-09 stsp TAILQ_INIT(&commitable_paths);
2835 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
2836 c4296144 2019-05-09 stsp
2837 c4296144 2019-05-09 stsp if (ondisk_path) {
2838 c4296144 2019-05-09 stsp err = got_path_skip_common_ancestor(&relpath,
2839 c4296144 2019-05-09 stsp worktree->root_path, ondisk_path);
2840 c4296144 2019-05-09 stsp if (err)
2841 c4296144 2019-05-09 stsp return err;
2842 c4296144 2019-05-09 stsp }
2843 c4296144 2019-05-09 stsp
2844 c4296144 2019-05-09 stsp err = lock_worktree(worktree, LOCK_EX);
2845 c4296144 2019-05-09 stsp if (err)
2846 c4296144 2019-05-09 stsp goto done;
2847 675c7539 2019-05-09 stsp
2848 36a38700 2019-05-10 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name);
2849 36a38700 2019-05-10 stsp if (err)
2850 36a38700 2019-05-10 stsp goto done;
2851 36a38700 2019-05-10 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2852 09f5bd90 2019-05-09 stsp if (err)
2853 09f5bd90 2019-05-09 stsp goto done;
2854 09f5bd90 2019-05-09 stsp
2855 036813ee 2019-05-09 stsp cc_arg.commitable_paths = &commitable_paths;
2856 24519714 2019-05-09 stsp cc_arg.worktree = worktree;
2857 24519714 2019-05-09 stsp cc_arg.repo = repo;
2858 675c7539 2019-05-09 stsp err = got_worktree_status(worktree, relpath ? relpath : "",
2859 ed175427 2019-05-09 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
2860 675c7539 2019-05-09 stsp if (err)
2861 675c7539 2019-05-09 stsp goto done;
2862 675c7539 2019-05-09 stsp
2863 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2864 588edf97 2019-05-10 stsp if (err)
2865 588edf97 2019-05-10 stsp goto done;
2866 588edf97 2019-05-10 stsp
2867 819f385b 2019-05-10 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
2868 819f385b 2019-05-10 stsp struct commitable *ct = pe->data;
2869 d56d26ce 2019-05-10 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
2870 d56d26ce 2019-05-10 stsp if (err)
2871 819f385b 2019-05-10 stsp goto done;
2872 819f385b 2019-05-10 stsp }
2873 de18fc63 2019-05-09 stsp
2874 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2875 588edf97 2019-05-10 stsp if (err)
2876 588edf97 2019-05-10 stsp goto done;
2877 588edf97 2019-05-10 stsp
2878 675c7539 2019-05-09 stsp /* TODO: collect commit message if not specified */
2879 c4296144 2019-05-09 stsp
2880 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
2881 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
2882 ed175427 2019-05-09 stsp struct commitable *ct = pe->data;
2883 cf066bf8 2019-05-09 stsp char *ondisk_path;
2884 cf066bf8 2019-05-09 stsp
2885 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
2886 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
2887 cf066bf8 2019-05-09 stsp continue;
2888 cf066bf8 2019-05-09 stsp
2889 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
2890 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
2891 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
2892 cf066bf8 2019-05-09 stsp goto done;
2893 cf066bf8 2019-05-09 stsp }
2894 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2895 a7055788 2019-05-09 stsp free(ondisk_path);
2896 cf066bf8 2019-05-09 stsp if (err)
2897 cf066bf8 2019-05-09 stsp goto done;
2898 cf066bf8 2019-05-09 stsp }
2899 036813ee 2019-05-09 stsp
2900 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
2901 588edf97 2019-05-10 stsp err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
2902 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2903 036813ee 2019-05-09 stsp if (err)
2904 036813ee 2019-05-09 stsp goto done;
2905 036813ee 2019-05-09 stsp
2906 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2907 de18fc63 2019-05-09 stsp if (err)
2908 de18fc63 2019-05-09 stsp goto done;
2909 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2910 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2911 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2912 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
2913 9d40349a 2019-05-09 stsp if (err)
2914 9d40349a 2019-05-09 stsp goto done;
2915 9d40349a 2019-05-09 stsp
2916 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
2917 09f5bd90 2019-05-09 stsp /* XXX ideally we'd lock the reference file here to avoid a race */
2918 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
2919 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
2920 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_worktree_get_head_ref_name");
2921 09f5bd90 2019-05-09 stsp goto done;
2922 09f5bd90 2019-05-09 stsp }
2923 bc70eb79 2019-05-09 stsp err = got_ref_open(&head_ref2, repo, head_ref_name);
2924 09f5bd90 2019-05-09 stsp if (err)
2925 09f5bd90 2019-05-09 stsp goto done;
2926 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
2927 ebf99748 2019-05-09 stsp if (err)
2928 ebf99748 2019-05-09 stsp goto done;
2929 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
2930 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
2931 09f5bd90 2019-05-09 stsp goto done;
2932 09f5bd90 2019-05-09 stsp }
2933 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
2934 36a38700 2019-05-10 stsp err = got_ref_change_ref(head_ref, *new_commit_id);
2935 f2c16586 2019-05-09 stsp if (err)
2936 f2c16586 2019-05-09 stsp goto done;
2937 36a38700 2019-05-10 stsp err = got_ref_write(head_ref, repo);
2938 f2c16586 2019-05-09 stsp if (err)
2939 f2c16586 2019-05-09 stsp goto done;
2940 09f5bd90 2019-05-09 stsp /* XXX race has ended here */
2941 f2c16586 2019-05-09 stsp
2942 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
2943 09f5bd90 2019-05-09 stsp if (err)
2944 09f5bd90 2019-05-09 stsp goto done;
2945 09f5bd90 2019-05-09 stsp
2946 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
2947 ebf99748 2019-05-09 stsp if (err)
2948 ebf99748 2019-05-09 stsp goto done;
2949 ebf99748 2019-05-09 stsp
2950 ebf99748 2019-05-09 stsp err = update_fileindex_after_commit(&commitable_paths,
2951 ebf99748 2019-05-09 stsp *new_commit_id, worktree);
2952 ebf99748 2019-05-09 stsp if (err)
2953 ebf99748 2019-05-09 stsp goto done;
2954 c4296144 2019-05-09 stsp done:
2955 c4296144 2019-05-09 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2956 c4296144 2019-05-09 stsp if (unlockerr && err == NULL)
2957 c4296144 2019-05-09 stsp err = unlockerr;
2958 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
2959 ed175427 2019-05-09 stsp struct commitable *ct = pe->data;
2960 ed175427 2019-05-09 stsp free_commitable(ct);
2961 cf066bf8 2019-05-09 stsp }
2962 036813ee 2019-05-09 stsp got_pathlist_free(&commitable_paths);
2963 588edf97 2019-05-10 stsp if (head_tree)
2964 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
2965 588edf97 2019-05-10 stsp if (head_commit)
2966 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
2967 c4296144 2019-05-09 stsp free(relpath);
2968 09f5bd90 2019-05-09 stsp free(head_commit_id);
2969 09f5bd90 2019-05-09 stsp free(head_commit_id2);
2970 36a38700 2019-05-10 stsp if (head_ref)
2971 36a38700 2019-05-10 stsp got_ref_close(head_ref);
2972 09f5bd90 2019-05-09 stsp if (head_ref2)
2973 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
2974 c4296144 2019-05-09 stsp return err;
2975 c4296144 2019-05-09 stsp }