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 1dd54920 2019-05-11 stsp #include "got_path.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 9d31a1d8 2018-03-11 stsp
57 9d31a1d8 2018-03-11 stsp #ifndef MIN
58 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 9d31a1d8 2018-03-11 stsp #endif
60 86c3caaf 2018-03-09 stsp
61 99724ed4 2018-03-10 stsp static const struct got_error *
62 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
63 99724ed4 2018-03-10 stsp {
64 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
65 99724ed4 2018-03-10 stsp char *path;
66 99724ed4 2018-03-10 stsp
67 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
69 99724ed4 2018-03-10 stsp
70 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
71 99724ed4 2018-03-10 stsp free(path);
72 507dc3bb 2018-12-29 stsp return err;
73 507dc3bb 2018-12-29 stsp }
74 507dc3bb 2018-12-29 stsp
75 507dc3bb 2018-12-29 stsp static const struct got_error *
76 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
77 507dc3bb 2018-12-29 stsp {
78 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
79 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
80 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
81 507dc3bb 2018-12-29 stsp char *path = NULL;
82 507dc3bb 2018-12-29 stsp
83 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
85 507dc3bb 2018-12-29 stsp path = NULL;
86 507dc3bb 2018-12-29 stsp goto done;
87 507dc3bb 2018-12-29 stsp }
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
90 507dc3bb 2018-12-29 stsp if (err)
91 507dc3bb 2018-12-29 stsp goto done;
92 507dc3bb 2018-12-29 stsp
93 507dc3bb 2018-12-29 stsp if (content) {
94 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
95 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
96 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp }
99 507dc3bb 2018-12-29 stsp }
100 507dc3bb 2018-12-29 stsp
101 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
103 2a57020b 2019-02-20 stsp unlink(tmppath);
104 507dc3bb 2018-12-29 stsp goto done;
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp done:
108 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
109 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
110 230a42bd 2019-05-11 jcs free(tmppath);
111 99724ed4 2018-03-10 stsp return err;
112 99724ed4 2018-03-10 stsp }
113 99724ed4 2018-03-10 stsp
114 09fe317a 2018-03-11 stsp static const struct got_error *
115 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
116 09fe317a 2018-03-11 stsp {
117 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
118 09fe317a 2018-03-11 stsp char *path;
119 09fe317a 2018-03-11 stsp int fd = -1;
120 09fe317a 2018-03-11 stsp ssize_t n;
121 09fe317a 2018-03-11 stsp struct stat sb;
122 09fe317a 2018-03-11 stsp
123 09fe317a 2018-03-11 stsp *content = NULL;
124 09fe317a 2018-03-11 stsp
125 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
127 09fe317a 2018-03-11 stsp path = NULL;
128 09fe317a 2018-03-11 stsp goto done;
129 09fe317a 2018-03-11 stsp }
130 09fe317a 2018-03-11 stsp
131 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
132 09fe317a 2018-03-11 stsp if (fd == -1) {
133 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
134 f02eaa22 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
135 f02eaa22 2019-03-11 stsp else
136 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
137 ef99fdb1 2018-03-11 stsp goto done;
138 ef99fdb1 2018-03-11 stsp }
139 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
142 09fe317a 2018-03-11 stsp goto done;
143 09fe317a 2018-03-11 stsp }
144 09fe317a 2018-03-11 stsp
145 d10c9b58 2019-02-19 stsp if (lstat(path, &sb) != 0) {
146 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", path);
147 d10c9b58 2019-02-19 stsp goto done;
148 d10c9b58 2019-02-19 stsp }
149 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
150 09fe317a 2018-03-11 stsp if (*content == NULL) {
151 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
152 09fe317a 2018-03-11 stsp goto done;
153 09fe317a 2018-03-11 stsp }
154 09fe317a 2018-03-11 stsp
155 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
156 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
157 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
158 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
159 09fe317a 2018-03-11 stsp goto done;
160 09fe317a 2018-03-11 stsp }
161 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
162 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
163 09fe317a 2018-03-11 stsp goto done;
164 09fe317a 2018-03-11 stsp }
165 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
166 09fe317a 2018-03-11 stsp
167 09fe317a 2018-03-11 stsp done:
168 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
169 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
170 09fe317a 2018-03-11 stsp free(path);
171 09fe317a 2018-03-11 stsp if (err) {
172 09fe317a 2018-03-11 stsp free(*content);
173 09fe317a 2018-03-11 stsp *content = NULL;
174 09fe317a 2018-03-11 stsp }
175 09fe317a 2018-03-11 stsp return err;
176 09fe317a 2018-03-11 stsp }
177 09fe317a 2018-03-11 stsp
178 024e9686 2019-05-14 stsp static const struct got_error *
179 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
180 024e9686 2019-05-14 stsp {
181 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
182 024e9686 2019-05-14 stsp char *refstr = NULL;
183 024e9686 2019-05-14 stsp
184 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
185 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
186 024e9686 2019-05-14 stsp if (refstr == NULL)
187 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
188 024e9686 2019-05-14 stsp } else {
189 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
190 024e9686 2019-05-14 stsp if (refstr == NULL)
191 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
192 024e9686 2019-05-14 stsp }
193 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
194 024e9686 2019-05-14 stsp free(refstr);
195 024e9686 2019-05-14 stsp return err;
196 024e9686 2019-05-14 stsp }
197 024e9686 2019-05-14 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 1451e70d 2018-03-10 stsp char *formatstr = NULL;
209 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
210 65596e15 2018-12-24 stsp char *basestr = NULL;
211 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
212 65596e15 2018-12-24 stsp
213 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
215 0c48fee2 2019-03-11 stsp goto done;
216 0c48fee2 2019-03-11 stsp }
217 0c48fee2 2019-03-11 stsp
218 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
219 65596e15 2018-12-24 stsp if (err)
220 65596e15 2018-12-24 stsp return err;
221 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
222 65596e15 2018-12-24 stsp if (err)
223 65596e15 2018-12-24 stsp return err;
224 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
226 86c3caaf 2018-03-09 stsp
227 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
228 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
229 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
230 0bb8a95e 2018-03-12 stsp }
231 577ec78f 2018-03-11 stsp
232 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
233 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
235 86c3caaf 2018-03-09 stsp goto done;
236 86c3caaf 2018-03-09 stsp }
237 86c3caaf 2018-03-09 stsp
238 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
239 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp }
243 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
245 86c3caaf 2018-03-09 stsp goto done;
246 86c3caaf 2018-03-09 stsp }
247 86c3caaf 2018-03-09 stsp
248 056e7441 2018-03-11 stsp /* Create an empty lock file. */
249 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
250 056e7441 2018-03-11 stsp if (err)
251 056e7441 2018-03-11 stsp goto done;
252 056e7441 2018-03-11 stsp
253 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
255 99724ed4 2018-03-10 stsp if (err)
256 86c3caaf 2018-03-09 stsp goto done;
257 86c3caaf 2018-03-09 stsp
258 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
259 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
260 65596e15 2018-12-24 stsp if (err)
261 65596e15 2018-12-24 stsp goto done;
262 65596e15 2018-12-24 stsp
263 65596e15 2018-12-24 stsp /* Record our base commit. */
264 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
265 65596e15 2018-12-24 stsp if (err)
266 65596e15 2018-12-24 stsp goto done;
267 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
268 99724ed4 2018-03-10 stsp if (err)
269 86c3caaf 2018-03-09 stsp goto done;
270 86c3caaf 2018-03-09 stsp
271 1451e70d 2018-03-10 stsp /* Store path to repository. */
272 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
274 99724ed4 2018-03-10 stsp if (err)
275 86c3caaf 2018-03-09 stsp goto done;
276 86c3caaf 2018-03-09 stsp
277 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
278 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
280 ec22038e 2019-03-10 stsp if (err)
281 ec22038e 2019-03-10 stsp goto done;
282 ec22038e 2019-03-10 stsp
283 ec22038e 2019-03-10 stsp /* Generate UUID. */
284 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
285 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
286 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
287 ec22038e 2019-03-10 stsp goto done;
288 ec22038e 2019-03-10 stsp }
289 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
292 ec22038e 2019-03-10 stsp goto done;
293 ec22038e 2019-03-10 stsp }
294 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
295 577ec78f 2018-03-11 stsp if (err)
296 577ec78f 2018-03-11 stsp goto done;
297 577ec78f 2018-03-11 stsp
298 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
299 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
301 1451e70d 2018-03-10 stsp goto done;
302 1451e70d 2018-03-10 stsp }
303 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
304 99724ed4 2018-03-10 stsp if (err)
305 1451e70d 2018-03-10 stsp goto done;
306 1451e70d 2018-03-10 stsp
307 86c3caaf 2018-03-09 stsp done:
308 65596e15 2018-12-24 stsp free(commit_id);
309 7ac97322 2018-03-11 stsp free(path_got);
310 1451e70d 2018-03-10 stsp free(formatstr);
311 0bb8a95e 2018-03-12 stsp free(absprefix);
312 65596e15 2018-12-24 stsp free(basestr);
313 ec22038e 2019-03-10 stsp free(uuidstr);
314 86c3caaf 2018-03-09 stsp return err;
315 86c3caaf 2018-03-09 stsp }
316 86c3caaf 2018-03-09 stsp
317 247140b2 2019-02-05 stsp static const struct got_error *
318 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
319 86c3caaf 2018-03-09 stsp {
320 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
321 7ac97322 2018-03-11 stsp char *path_got;
322 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
323 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
324 056e7441 2018-03-11 stsp char *path_lock = NULL;
325 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
326 6d9d28c3 2018-03-11 stsp int version, fd = -1;
327 6d9d28c3 2018-03-11 stsp const char *errstr;
328 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
329 c442a90d 2019-03-10 stsp uint32_t uuid_status;
330 6d9d28c3 2018-03-11 stsp
331 6d9d28c3 2018-03-11 stsp *worktree = NULL;
332 6d9d28c3 2018-03-11 stsp
333 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
335 7ac97322 2018-03-11 stsp path_got = NULL;
336 6d9d28c3 2018-03-11 stsp goto done;
337 6d9d28c3 2018-03-11 stsp }
338 6d9d28c3 2018-03-11 stsp
339 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 056e7441 2018-03-11 stsp path_lock = NULL;
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 6d9d28c3 2018-03-11 stsp
345 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
346 6d9d28c3 2018-03-11 stsp if (fd == -1) {
347 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
349 6d9d28c3 2018-03-11 stsp goto done;
350 6d9d28c3 2018-03-11 stsp }
351 6d9d28c3 2018-03-11 stsp
352 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
353 6d9d28c3 2018-03-11 stsp if (err)
354 6d9d28c3 2018-03-11 stsp goto done;
355 6d9d28c3 2018-03-11 stsp
356 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
357 6d9d28c3 2018-03-11 stsp if (errstr) {
358 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
359 6d9d28c3 2018-03-11 stsp goto done;
360 6d9d28c3 2018-03-11 stsp }
361 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
362 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
363 6d9d28c3 2018-03-11 stsp goto done;
364 6d9d28c3 2018-03-11 stsp }
365 6d9d28c3 2018-03-11 stsp
366 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
367 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
368 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
369 6d9d28c3 2018-03-11 stsp goto done;
370 6d9d28c3 2018-03-11 stsp }
371 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
372 6d9d28c3 2018-03-11 stsp
373 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
374 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
376 6d9d28c3 2018-03-11 stsp goto done;
377 6d9d28c3 2018-03-11 stsp }
378 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
379 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
380 6d9d28c3 2018-03-11 stsp if (err)
381 6d9d28c3 2018-03-11 stsp goto done;
382 eaccb85f 2018-12-25 stsp
383 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
384 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
385 93a30277 2018-12-24 stsp if (err)
386 93a30277 2018-12-24 stsp goto done;
387 93a30277 2018-12-24 stsp
388 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
389 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
390 c442a90d 2019-03-10 stsp if (err)
391 c442a90d 2019-03-10 stsp goto done;
392 c442a90d 2019-03-10 stsp
393 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
394 eaccb85f 2018-12-25 stsp if (err)
395 c442a90d 2019-03-10 stsp goto done;
396 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
397 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
398 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
399 eaccb85f 2018-12-25 stsp goto done;
400 c442a90d 2019-03-10 stsp }
401 eaccb85f 2018-12-25 stsp
402 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
403 eaccb85f 2018-12-25 stsp if (err)
404 eaccb85f 2018-12-25 stsp goto done;
405 eaccb85f 2018-12-25 stsp
406 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
407 eaccb85f 2018-12-25 stsp base_commit_id_str);
408 f5baf295 2018-03-11 stsp if (err)
409 6d9d28c3 2018-03-11 stsp goto done;
410 6d9d28c3 2018-03-11 stsp
411 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
412 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
413 6d9d28c3 2018-03-11 stsp done:
414 eaccb85f 2018-12-25 stsp if (repo)
415 eaccb85f 2018-12-25 stsp got_repo_close(repo);
416 7ac97322 2018-03-11 stsp free(path_got);
417 056e7441 2018-03-11 stsp free(path_lock);
418 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
419 c442a90d 2019-03-10 stsp free(uuidstr);
420 bd165944 2019-03-10 stsp free(formatstr);
421 6d9d28c3 2018-03-11 stsp if (err) {
422 6d9d28c3 2018-03-11 stsp if (fd != -1)
423 6d9d28c3 2018-03-11 stsp close(fd);
424 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
425 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
426 6d9d28c3 2018-03-11 stsp *worktree = NULL;
427 6d9d28c3 2018-03-11 stsp } else
428 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
429 6d9d28c3 2018-03-11 stsp
430 6d9d28c3 2018-03-11 stsp return err;
431 86c3caaf 2018-03-09 stsp }
432 247140b2 2019-02-05 stsp
433 247140b2 2019-02-05 stsp const struct got_error *
434 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
435 247140b2 2019-02-05 stsp {
436 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
437 86c3caaf 2018-03-09 stsp
438 247140b2 2019-02-05 stsp do {
439 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
440 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
441 247140b2 2019-02-05 stsp return err;
442 247140b2 2019-02-05 stsp if (*worktree)
443 247140b2 2019-02-05 stsp return NULL;
444 247140b2 2019-02-05 stsp path = dirname(path);
445 d1542a27 2019-02-05 stsp if (path == NULL)
446 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
447 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
448 247140b2 2019-02-05 stsp
449 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
450 247140b2 2019-02-05 stsp }
451 247140b2 2019-02-05 stsp
452 3a6ce05a 2019-02-11 stsp const struct got_error *
453 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
454 86c3caaf 2018-03-09 stsp {
455 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
456 c88eb298 2018-03-11 stsp free(worktree->root_path);
457 cde76477 2018-03-11 stsp free(worktree->repo_path);
458 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
459 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
460 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
461 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
462 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
463 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
464 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
465 6d9d28c3 2018-03-11 stsp free(worktree);
466 3a6ce05a 2019-02-11 stsp return err;
467 86c3caaf 2018-03-09 stsp }
468 86c3caaf 2018-03-09 stsp
469 2fbdb5ae 2018-12-29 stsp const char *
470 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
471 c7f4312f 2019-02-05 stsp {
472 c7f4312f 2019-02-05 stsp return worktree->root_path;
473 c7f4312f 2019-02-05 stsp }
474 c7f4312f 2019-02-05 stsp
475 c7f4312f 2019-02-05 stsp const char *
476 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
477 86c3caaf 2018-03-09 stsp {
478 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
479 49520a32 2018-12-29 stsp }
480 49520a32 2018-12-29 stsp
481 49520a32 2018-12-29 stsp const char *
482 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
483 49520a32 2018-12-29 stsp {
484 f609be2e 2018-12-29 stsp return worktree->path_prefix;
485 e5dc7198 2018-12-29 stsp }
486 e5dc7198 2018-12-29 stsp
487 e5dc7198 2018-12-29 stsp const struct got_error *
488 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
489 e5dc7198 2018-12-29 stsp const char *path_prefix)
490 e5dc7198 2018-12-29 stsp {
491 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
492 e5dc7198 2018-12-29 stsp
493 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
494 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
495 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
496 e5dc7198 2018-12-29 stsp }
497 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
498 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
499 e5dc7198 2018-12-29 stsp free(absprefix);
500 e5dc7198 2018-12-29 stsp return NULL;
501 86c3caaf 2018-03-09 stsp }
502 86c3caaf 2018-03-09 stsp
503 bc70eb79 2019-05-09 stsp const char *
504 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
505 86c3caaf 2018-03-09 stsp {
506 36a38700 2019-05-10 stsp return worktree->head_ref_name;
507 024e9686 2019-05-14 stsp }
508 024e9686 2019-05-14 stsp
509 024e9686 2019-05-14 stsp const struct got_error *
510 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
511 024e9686 2019-05-14 stsp struct got_reference *head_ref)
512 024e9686 2019-05-14 stsp {
513 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
514 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
515 024e9686 2019-05-14 stsp
516 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
517 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
518 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
519 024e9686 2019-05-14 stsp path_got = NULL;
520 024e9686 2019-05-14 stsp goto done;
521 024e9686 2019-05-14 stsp }
522 024e9686 2019-05-14 stsp
523 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
524 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
525 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
526 024e9686 2019-05-14 stsp goto done;
527 024e9686 2019-05-14 stsp }
528 024e9686 2019-05-14 stsp
529 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
530 024e9686 2019-05-14 stsp if (err)
531 024e9686 2019-05-14 stsp goto done;
532 024e9686 2019-05-14 stsp
533 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
534 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
535 024e9686 2019-05-14 stsp done:
536 024e9686 2019-05-14 stsp free(path_got);
537 024e9686 2019-05-14 stsp if (err)
538 024e9686 2019-05-14 stsp free(head_ref_name);
539 024e9686 2019-05-14 stsp return err;
540 507dc3bb 2018-12-29 stsp }
541 507dc3bb 2018-12-29 stsp
542 b72f483a 2019-02-05 stsp struct got_object_id *
543 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
544 507dc3bb 2018-12-29 stsp {
545 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
546 86c3caaf 2018-03-09 stsp }
547 86c3caaf 2018-03-09 stsp
548 507dc3bb 2018-12-29 stsp const struct got_error *
549 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
550 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
551 507dc3bb 2018-12-29 stsp {
552 507dc3bb 2018-12-29 stsp const struct got_error *err;
553 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
554 507dc3bb 2018-12-29 stsp char *id_str = NULL;
555 507dc3bb 2018-12-29 stsp char *path_got = NULL;
556 507dc3bb 2018-12-29 stsp
557 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
559 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
560 507dc3bb 2018-12-29 stsp path_got = NULL;
561 507dc3bb 2018-12-29 stsp goto done;
562 507dc3bb 2018-12-29 stsp }
563 507dc3bb 2018-12-29 stsp
564 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
565 507dc3bb 2018-12-29 stsp if (err)
566 507dc3bb 2018-12-29 stsp return err;
567 507dc3bb 2018-12-29 stsp
568 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
569 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
570 507dc3bb 2018-12-29 stsp goto done;
571 507dc3bb 2018-12-29 stsp }
572 507dc3bb 2018-12-29 stsp
573 507dc3bb 2018-12-29 stsp /* Record our base commit. */
574 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
575 507dc3bb 2018-12-29 stsp if (err)
576 507dc3bb 2018-12-29 stsp goto done;
577 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
578 507dc3bb 2018-12-29 stsp if (err)
579 507dc3bb 2018-12-29 stsp goto done;
580 507dc3bb 2018-12-29 stsp
581 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
582 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
583 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
584 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
585 507dc3bb 2018-12-29 stsp goto done;
586 507dc3bb 2018-12-29 stsp }
587 507dc3bb 2018-12-29 stsp done:
588 507dc3bb 2018-12-29 stsp if (obj)
589 507dc3bb 2018-12-29 stsp got_object_close(obj);
590 507dc3bb 2018-12-29 stsp free(id_str);
591 507dc3bb 2018-12-29 stsp free(path_got);
592 507dc3bb 2018-12-29 stsp return err;
593 507dc3bb 2018-12-29 stsp }
594 507dc3bb 2018-12-29 stsp
595 9d31a1d8 2018-03-11 stsp static const struct got_error *
596 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
597 86c3caaf 2018-03-09 stsp {
598 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
599 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
600 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
601 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
602 86c3caaf 2018-03-09 stsp return NULL;
603 21908da4 2019-01-13 stsp }
604 21908da4 2019-01-13 stsp
605 21908da4 2019-01-13 stsp static const struct got_error *
606 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
607 4a1ddfc2 2019-01-12 stsp {
608 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
609 4a1ddfc2 2019-01-12 stsp char *abspath;
610 4a1ddfc2 2019-01-12 stsp
611 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
612 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
613 4a1ddfc2 2019-01-12 stsp
614 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
615 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
616 ddcd8544 2019-03-11 stsp struct stat sb;
617 ddcd8544 2019-03-11 stsp err = NULL;
618 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
619 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
620 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
621 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
622 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
623 ddcd8544 2019-03-11 stsp }
624 ddcd8544 2019-03-11 stsp }
625 4a1ddfc2 2019-01-12 stsp free(abspath);
626 68c76935 2019-02-19 stsp return err;
627 68c76935 2019-02-19 stsp }
628 68c76935 2019-02-19 stsp
629 68c76935 2019-02-19 stsp static const struct got_error *
630 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
631 68c76935 2019-02-19 stsp {
632 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
633 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
634 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
635 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
636 68c76935 2019-02-19 stsp
637 68c76935 2019-02-19 stsp *same = 1;
638 68c76935 2019-02-19 stsp
639 230a42bd 2019-05-11 jcs for (;;) {
640 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
641 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
642 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
643 68c76935 2019-02-19 stsp break;
644 68c76935 2019-02-19 stsp }
645 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
646 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
647 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
648 68c76935 2019-02-19 stsp break;
649 68c76935 2019-02-19 stsp }
650 68c76935 2019-02-19 stsp if (flen1 == 0) {
651 68c76935 2019-02-19 stsp if (flen2 != 0)
652 68c76935 2019-02-19 stsp *same = 0;
653 68c76935 2019-02-19 stsp break;
654 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
655 68c76935 2019-02-19 stsp if (flen1 != 0)
656 68c76935 2019-02-19 stsp *same = 0;
657 68c76935 2019-02-19 stsp break;
658 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
659 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
660 68c76935 2019-02-19 stsp *same = 0;
661 68c76935 2019-02-19 stsp break;
662 68c76935 2019-02-19 stsp }
663 68c76935 2019-02-19 stsp } else {
664 68c76935 2019-02-19 stsp *same = 0;
665 68c76935 2019-02-19 stsp break;
666 68c76935 2019-02-19 stsp }
667 68c76935 2019-02-19 stsp }
668 68c76935 2019-02-19 stsp
669 68c76935 2019-02-19 stsp return err;
670 68c76935 2019-02-19 stsp }
671 68c76935 2019-02-19 stsp
672 68c76935 2019-02-19 stsp static const struct got_error *
673 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
674 68c76935 2019-02-19 stsp {
675 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
676 68c76935 2019-02-19 stsp struct stat sb;
677 68c76935 2019-02-19 stsp size_t size1, size2;
678 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
679 68c76935 2019-02-19 stsp
680 68c76935 2019-02-19 stsp *same = 1;
681 68c76935 2019-02-19 stsp
682 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
683 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
684 68c76935 2019-02-19 stsp goto done;
685 68c76935 2019-02-19 stsp }
686 68c76935 2019-02-19 stsp size1 = sb.st_size;
687 68c76935 2019-02-19 stsp
688 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
690 68c76935 2019-02-19 stsp goto done;
691 68c76935 2019-02-19 stsp }
692 68c76935 2019-02-19 stsp size2 = sb.st_size;
693 68c76935 2019-02-19 stsp
694 68c76935 2019-02-19 stsp if (size1 != size2) {
695 68c76935 2019-02-19 stsp *same = 0;
696 68c76935 2019-02-19 stsp return NULL;
697 68c76935 2019-02-19 stsp }
698 68c76935 2019-02-19 stsp
699 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
700 68c76935 2019-02-19 stsp if (f1 == NULL)
701 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
702 68c76935 2019-02-19 stsp
703 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
704 68c76935 2019-02-19 stsp if (f2 == NULL) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
706 68c76935 2019-02-19 stsp goto done;
707 68c76935 2019-02-19 stsp }
708 68c76935 2019-02-19 stsp
709 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
710 68c76935 2019-02-19 stsp done:
711 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
712 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
713 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
714 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
715 68c76935 2019-02-19 stsp
716 6353ad76 2019-02-08 stsp return err;
717 6353ad76 2019-02-08 stsp }
718 6353ad76 2019-02-08 stsp
719 6353ad76 2019-02-08 stsp /*
720 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
721 46b6ee73 2019-06-04 stsp * blob_deriv acts as the first derived version, and the file on disk
722 234035bc 2019-06-01 stsp * acts as the second derived version.
723 6353ad76 2019-02-08 stsp */
724 6353ad76 2019-02-08 stsp static const struct got_error *
725 234035bc 2019-06-01 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
726 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
727 46b6ee73 2019-06-04 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
728 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
729 234035bc 2019-06-01 stsp void *progress_arg)
730 6353ad76 2019-02-08 stsp {
731 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
732 6353ad76 2019-02-08 stsp int merged_fd = -1;
733 46b6ee73 2019-06-04 stsp FILE *f_deriv = NULL, *f_orig = NULL;
734 46b6ee73 2019-06-04 stsp char *blob_deriv_path = NULL, *blob_orig_path = NULL;
735 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
736 6353ad76 2019-02-08 stsp char *id_str = NULL;
737 6353ad76 2019-02-08 stsp char *label1 = NULL;
738 234035bc 2019-06-01 stsp int overlapcnt = 0;
739 af54ae4a 2019-02-19 stsp char *parent;
740 6353ad76 2019-02-08 stsp
741 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
742 234035bc 2019-06-01 stsp
743 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
744 af54ae4a 2019-02-19 stsp if (parent == NULL)
745 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
746 af54ae4a 2019-02-19 stsp
747 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
748 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
749 af54ae4a 2019-02-19 stsp
750 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
751 6353ad76 2019-02-08 stsp if (err)
752 af54ae4a 2019-02-19 stsp goto done;
753 af54ae4a 2019-02-19 stsp
754 af54ae4a 2019-02-19 stsp free(base_path);
755 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
756 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
757 af54ae4a 2019-02-19 stsp base_path = NULL;
758 af54ae4a 2019-02-19 stsp goto done;
759 af54ae4a 2019-02-19 stsp }
760 af54ae4a 2019-02-19 stsp
761 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
762 6353ad76 2019-02-08 stsp if (err)
763 6353ad76 2019-02-08 stsp goto done;
764 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
765 6c4c42e0 2019-06-24 stsp blob_deriv);
766 6353ad76 2019-02-08 stsp if (err)
767 6353ad76 2019-02-08 stsp goto done;
768 6353ad76 2019-02-08 stsp
769 af54ae4a 2019-02-19 stsp free(base_path);
770 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
771 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
772 af54ae4a 2019-02-19 stsp base_path = NULL;
773 af54ae4a 2019-02-19 stsp goto done;
774 af54ae4a 2019-02-19 stsp }
775 af54ae4a 2019-02-19 stsp
776 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
777 6353ad76 2019-02-08 stsp if (err)
778 6353ad76 2019-02-08 stsp goto done;
779 46b6ee73 2019-06-04 stsp if (blob_orig) {
780 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
781 46b6ee73 2019-06-04 stsp blob_orig);
782 1430b4e0 2019-03-27 stsp if (err)
783 1430b4e0 2019-03-27 stsp goto done;
784 1430b4e0 2019-03-27 stsp } else {
785 1430b4e0 2019-03-27 stsp /*
786 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
787 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
788 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
789 1430b4e0 2019-03-27 stsp */
790 1430b4e0 2019-03-27 stsp }
791 6353ad76 2019-02-08 stsp
792 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
793 6353ad76 2019-02-08 stsp if (err)
794 6353ad76 2019-02-08 stsp goto done;
795 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
796 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
797 6353ad76 2019-02-08 stsp goto done;
798 6353ad76 2019-02-08 stsp }
799 6353ad76 2019-02-08 stsp
800 46b6ee73 2019-06-04 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
801 46b6ee73 2019-06-04 stsp blob_orig_path, ondisk_path, label1, path);
802 6353ad76 2019-02-08 stsp if (err)
803 6353ad76 2019-02-08 stsp goto done;
804 6353ad76 2019-02-08 stsp
805 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
806 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
807 6353ad76 2019-02-08 stsp
808 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
809 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
810 816dc654 2019-02-16 stsp goto done;
811 68c76935 2019-02-19 stsp }
812 68c76935 2019-02-19 stsp
813 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
814 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
815 46b6ee73 2019-06-04 stsp err = check_files_equal(local_changes_subsumed, blob_deriv_path,
816 68c76935 2019-02-19 stsp merged_path);
817 68c76935 2019-02-19 stsp if (err)
818 68c76935 2019-02-19 stsp goto done;
819 816dc654 2019-02-16 stsp }
820 6353ad76 2019-02-08 stsp
821 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
822 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
823 70a0c8ec 2019-02-20 stsp goto done;
824 70a0c8ec 2019-02-20 stsp }
825 70a0c8ec 2019-02-20 stsp
826 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
827 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
828 230a42bd 2019-05-11 jcs ondisk_path);
829 2a57020b 2019-02-20 stsp unlink(merged_path);
830 6353ad76 2019-02-08 stsp goto done;
831 6353ad76 2019-02-08 stsp }
832 6353ad76 2019-02-08 stsp
833 6353ad76 2019-02-08 stsp done:
834 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
835 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
836 46b6ee73 2019-06-04 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
837 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
838 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
839 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
840 6353ad76 2019-02-08 stsp free(merged_path);
841 af54ae4a 2019-02-19 stsp free(base_path);
842 46b6ee73 2019-06-04 stsp if (blob_deriv_path) {
843 46b6ee73 2019-06-04 stsp unlink(blob_deriv_path);
844 46b6ee73 2019-06-04 stsp free(blob_deriv_path);
845 6353ad76 2019-02-08 stsp }
846 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
847 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
848 46b6ee73 2019-06-04 stsp free(blob_orig_path);
849 6353ad76 2019-02-08 stsp }
850 6353ad76 2019-02-08 stsp free(id_str);
851 6353ad76 2019-02-08 stsp free(label1);
852 4a1ddfc2 2019-01-12 stsp return err;
853 4a1ddfc2 2019-01-12 stsp }
854 4a1ddfc2 2019-01-12 stsp
855 4a1ddfc2 2019-01-12 stsp static const struct got_error *
856 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
857 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
858 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
859 13d9040b 2019-03-27 stsp int update_timestamps)
860 13d9040b 2019-03-27 stsp {
861 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
862 13d9040b 2019-03-27 stsp
863 13d9040b 2019-03-27 stsp if (ie == NULL)
864 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
865 13d9040b 2019-03-27 stsp if (ie)
866 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
867 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
868 13d9040b 2019-03-27 stsp update_timestamps);
869 13d9040b 2019-03-27 stsp else {
870 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
871 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
872 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
873 13d9040b 2019-03-27 stsp if (!err)
874 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
875 13d9040b 2019-03-27 stsp }
876 13d9040b 2019-03-27 stsp return err;
877 13d9040b 2019-03-27 stsp }
878 13d9040b 2019-03-27 stsp
879 13d9040b 2019-03-27 stsp static const struct got_error *
880 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
881 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
882 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
883 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
884 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
885 9d31a1d8 2018-03-11 stsp {
886 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
887 507dc3bb 2018-12-29 stsp int fd = -1;
888 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
889 507dc3bb 2018-12-29 stsp int update = 0;
890 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
891 9d31a1d8 2018-03-11 stsp
892 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
893 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
894 9d31a1d8 2018-03-11 stsp if (fd == -1) {
895 21908da4 2019-01-13 stsp if (errno == ENOENT) {
896 21908da4 2019-01-13 stsp char *parent = dirname(path);
897 21908da4 2019-01-13 stsp if (parent == NULL)
898 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
899 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
900 21908da4 2019-01-13 stsp if (err)
901 21908da4 2019-01-13 stsp return err;
902 21908da4 2019-01-13 stsp fd = open(ondisk_path,
903 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
904 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
905 21908da4 2019-01-13 stsp if (fd == -1)
906 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
907 230a42bd 2019-05-11 jcs ondisk_path);
908 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
909 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
910 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
911 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
912 507dc3bb 2018-12-29 stsp goto done;
913 d70b8e30 2018-12-27 stsp } else {
914 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
915 507dc3bb 2018-12-29 stsp ondisk_path);
916 507dc3bb 2018-12-29 stsp if (err)
917 507dc3bb 2018-12-29 stsp goto done;
918 507dc3bb 2018-12-29 stsp update = 1;
919 9d31a1d8 2018-03-11 stsp }
920 507dc3bb 2018-12-29 stsp } else
921 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
922 9d31a1d8 2018-03-11 stsp }
923 9d31a1d8 2018-03-11 stsp
924 a378724f 2019-02-10 stsp if (restoring_missing_file)
925 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
926 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
927 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
928 a378724f 2019-02-10 stsp else
929 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
930 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
931 d7b62c98 2018-12-27 stsp
932 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
933 9d31a1d8 2018-03-11 stsp do {
934 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
935 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
936 9d31a1d8 2018-03-11 stsp if (err)
937 9d31a1d8 2018-03-11 stsp break;
938 9d31a1d8 2018-03-11 stsp if (len > 0) {
939 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
940 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
941 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
942 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
943 b87c6f83 2018-12-24 stsp goto done;
944 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
945 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
946 b87c6f83 2018-12-24 stsp goto done;
947 9d31a1d8 2018-03-11 stsp }
948 61d6eaa3 2018-12-24 stsp hdrlen = 0;
949 9d31a1d8 2018-03-11 stsp }
950 9d31a1d8 2018-03-11 stsp } while (len != 0);
951 9d31a1d8 2018-03-11 stsp
952 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
953 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
954 816dc654 2019-02-16 stsp goto done;
955 816dc654 2019-02-16 stsp }
956 9d31a1d8 2018-03-11 stsp
957 507dc3bb 2018-12-29 stsp if (update) {
958 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
959 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
960 230a42bd 2019-05-11 jcs ondisk_path);
961 2a57020b 2019-02-20 stsp unlink(tmppath);
962 68ed9ba5 2019-02-10 stsp goto done;
963 68ed9ba5 2019-02-10 stsp }
964 68ed9ba5 2019-02-10 stsp }
965 ba8a0d4d 2019-02-10 stsp
966 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
967 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
968 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
969 507dc3bb 2018-12-29 stsp goto done;
970 507dc3bb 2018-12-29 stsp }
971 ba8a0d4d 2019-02-10 stsp } else {
972 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
973 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
974 68ed9ba5 2019-02-10 stsp goto done;
975 68ed9ba5 2019-02-10 stsp }
976 507dc3bb 2018-12-29 stsp }
977 507dc3bb 2018-12-29 stsp
978 9d31a1d8 2018-03-11 stsp done:
979 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
980 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
981 507dc3bb 2018-12-29 stsp free(tmppath);
982 6353ad76 2019-02-08 stsp return err;
983 6353ad76 2019-02-08 stsp }
984 6353ad76 2019-02-08 stsp
985 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
986 6353ad76 2019-02-08 stsp static const struct got_error *
987 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
988 7154f6ce 2019-03-27 stsp {
989 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
990 7154f6ce 2019-03-27 stsp const char *markers[3] = {
991 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
992 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
993 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
994 7154f6ce 2019-03-27 stsp };
995 7154f6ce 2019-03-27 stsp int i = 0;
996 7154f6ce 2019-03-27 stsp char *line;
997 7154f6ce 2019-03-27 stsp size_t len;
998 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
999 7154f6ce 2019-03-27 stsp
1000 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1001 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1002 7154f6ce 2019-03-27 stsp if (line == NULL) {
1003 7154f6ce 2019-03-27 stsp if (feof(f))
1004 7154f6ce 2019-03-27 stsp break;
1005 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1006 7154f6ce 2019-03-27 stsp break;
1007 7154f6ce 2019-03-27 stsp }
1008 7154f6ce 2019-03-27 stsp
1009 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1010 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1011 19332e6d 2019-05-13 stsp == 0)
1012 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1013 7154f6ce 2019-03-27 stsp else
1014 7154f6ce 2019-03-27 stsp i++;
1015 7154f6ce 2019-03-27 stsp }
1016 7154f6ce 2019-03-27 stsp }
1017 7154f6ce 2019-03-27 stsp
1018 7154f6ce 2019-03-27 stsp return err;
1019 7154f6ce 2019-03-27 stsp }
1020 7154f6ce 2019-03-27 stsp
1021 7154f6ce 2019-03-27 stsp static const struct got_error *
1022 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1023 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1024 b8f41171 2019-02-10 stsp struct got_repository *repo)
1025 6353ad76 2019-02-08 stsp {
1026 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1027 6353ad76 2019-02-08 stsp struct got_object_id id;
1028 6353ad76 2019-02-08 stsp size_t hdrlen;
1029 6353ad76 2019-02-08 stsp FILE *f = NULL;
1030 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1031 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1032 6353ad76 2019-02-08 stsp size_t flen, blen;
1033 6353ad76 2019-02-08 stsp
1034 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1035 6353ad76 2019-02-08 stsp
1036 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1037 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1038 b8f41171 2019-02-10 stsp if (ie) {
1039 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1040 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1041 2ec1f75b 2019-03-26 stsp else
1042 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1043 b8f41171 2019-02-10 stsp sb->st_mode =
1044 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1045 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1046 b8f41171 2019-02-10 stsp } else
1047 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1048 a378724f 2019-02-10 stsp return NULL;
1049 a378724f 2019-02-10 stsp }
1050 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", abspath);
1051 a378724f 2019-02-10 stsp }
1052 6353ad76 2019-02-08 stsp
1053 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1054 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1055 6353ad76 2019-02-08 stsp return NULL;
1056 6353ad76 2019-02-08 stsp }
1057 6353ad76 2019-02-08 stsp
1058 b8f41171 2019-02-10 stsp if (ie == NULL)
1059 d00136be 2019-03-26 stsp return NULL;
1060 d00136be 2019-03-26 stsp
1061 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1062 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1063 2ec1f75b 2019-03-26 stsp return NULL;
1064 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1065 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1066 b8f41171 2019-02-10 stsp return NULL;
1067 d00136be 2019-03-26 stsp }
1068 b8f41171 2019-02-10 stsp
1069 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1070 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1071 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1072 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1073 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1074 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1075 6353ad76 2019-02-08 stsp return NULL;
1076 6353ad76 2019-02-08 stsp
1077 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1078 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1079 6353ad76 2019-02-08 stsp if (err)
1080 6353ad76 2019-02-08 stsp return err;
1081 6353ad76 2019-02-08 stsp
1082 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1083 6353ad76 2019-02-08 stsp if (f == NULL) {
1084 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1085 6353ad76 2019-02-08 stsp goto done;
1086 6353ad76 2019-02-08 stsp }
1087 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1088 656b1f76 2019-05-11 jcs for (;;) {
1089 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1090 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1091 6353ad76 2019-02-08 stsp if (err)
1092 7154f6ce 2019-03-27 stsp goto done;
1093 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1094 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1095 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1096 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1097 7154f6ce 2019-03-27 stsp goto done;
1098 80c5c120 2019-02-19 stsp }
1099 6353ad76 2019-02-08 stsp if (blen == 0) {
1100 6353ad76 2019-02-08 stsp if (flen != 0)
1101 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1102 6353ad76 2019-02-08 stsp break;
1103 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1104 6353ad76 2019-02-08 stsp if (blen != 0)
1105 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1106 6353ad76 2019-02-08 stsp break;
1107 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1108 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1109 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1110 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1111 6353ad76 2019-02-08 stsp break;
1112 6353ad76 2019-02-08 stsp }
1113 6353ad76 2019-02-08 stsp } else {
1114 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1115 6353ad76 2019-02-08 stsp break;
1116 6353ad76 2019-02-08 stsp }
1117 6353ad76 2019-02-08 stsp hdrlen = 0;
1118 6353ad76 2019-02-08 stsp }
1119 7154f6ce 2019-03-27 stsp
1120 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1121 7154f6ce 2019-03-27 stsp rewind(f);
1122 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1123 7154f6ce 2019-03-27 stsp }
1124 6353ad76 2019-02-08 stsp done:
1125 6353ad76 2019-02-08 stsp if (blob)
1126 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1127 6353ad76 2019-02-08 stsp if (f)
1128 6353ad76 2019-02-08 stsp fclose(f);
1129 9d31a1d8 2018-03-11 stsp return err;
1130 9d31a1d8 2018-03-11 stsp }
1131 9d31a1d8 2018-03-11 stsp
1132 9d31a1d8 2018-03-11 stsp static const struct got_error *
1133 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1134 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1135 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1136 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1137 0584f854 2019-04-06 stsp void *progress_arg)
1138 9d31a1d8 2018-03-11 stsp {
1139 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1140 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1141 6353ad76 2019-02-08 stsp char *ondisk_path;
1142 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1143 b8f41171 2019-02-10 stsp struct stat sb;
1144 9d31a1d8 2018-03-11 stsp
1145 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1146 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1147 6353ad76 2019-02-08 stsp
1148 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1149 b8f41171 2019-02-10 stsp if (err)
1150 b8f41171 2019-02-10 stsp goto done;
1151 6353ad76 2019-02-08 stsp
1152 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1153 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1154 b8f41171 2019-02-10 stsp goto done;
1155 b8f41171 2019-02-10 stsp }
1156 b8f41171 2019-02-10 stsp
1157 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1158 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1159 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1160 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1161 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1162 b8f41171 2019-02-10 stsp path);
1163 6353ad76 2019-02-08 stsp goto done;
1164 a378724f 2019-02-10 stsp }
1165 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1166 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1167 1430b4e0 2019-03-27 stsp SHA1_DIGEST_LENGTH) == 0)
1168 b8f41171 2019-02-10 stsp goto done;
1169 9d31a1d8 2018-03-11 stsp }
1170 9d31a1d8 2018-03-11 stsp
1171 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1172 8da9e5f4 2019-01-12 stsp if (err)
1173 6353ad76 2019-02-08 stsp goto done;
1174 512f0d0e 2019-01-02 stsp
1175 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1176 234035bc 2019-06-01 stsp int update_timestamps;
1177 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1178 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1179 234035bc 2019-06-01 stsp struct got_object_id id2;
1180 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1181 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1182 234035bc 2019-06-01 stsp if (err)
1183 234035bc 2019-06-01 stsp goto done;
1184 234035bc 2019-06-01 stsp }
1185 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1186 234035bc 2019-06-01 stsp ondisk_path, path, sb.st_mode, blob, repo,
1187 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1188 234035bc 2019-06-01 stsp if (blob2)
1189 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1190 234035bc 2019-06-01 stsp /*
1191 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1192 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1193 234035bc 2019-06-01 stsp * unmodified files again.
1194 234035bc 2019-06-01 stsp */
1195 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1196 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1197 234035bc 2019-06-01 stsp update_timestamps);
1198 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1199 13d9040b 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1200 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1201 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1202 13d9040b 2019-03-27 stsp if (err)
1203 13d9040b 2019-03-27 stsp goto done;
1204 13d9040b 2019-03-27 stsp } else {
1205 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1206 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1207 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1208 13d9040b 2019-03-27 stsp if (err)
1209 13d9040b 2019-03-27 stsp goto done;
1210 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1211 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1212 13d9040b 2019-03-27 stsp if (err)
1213 13d9040b 2019-03-27 stsp goto done;
1214 13d9040b 2019-03-27 stsp }
1215 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1216 6353ad76 2019-02-08 stsp done:
1217 6353ad76 2019-02-08 stsp free(ondisk_path);
1218 8da9e5f4 2019-01-12 stsp return err;
1219 90285c3b 2019-01-08 stsp }
1220 90285c3b 2019-01-08 stsp
1221 90285c3b 2019-01-08 stsp static const struct got_error *
1222 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1223 90285c3b 2019-01-08 stsp {
1224 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1225 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1226 90285c3b 2019-01-08 stsp
1227 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1228 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1229 90285c3b 2019-01-08 stsp
1230 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1231 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1232 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1233 c97665ae 2019-01-13 stsp } else {
1234 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1235 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1236 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1237 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1238 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1239 230a42bd 2019-05-11 jcs parent);
1240 90285c3b 2019-01-08 stsp break;
1241 90285c3b 2019-01-08 stsp }
1242 90285c3b 2019-01-08 stsp parent = dirname(parent);
1243 90285c3b 2019-01-08 stsp }
1244 90285c3b 2019-01-08 stsp }
1245 90285c3b 2019-01-08 stsp free(ondisk_path);
1246 90285c3b 2019-01-08 stsp return err;
1247 512f0d0e 2019-01-02 stsp }
1248 512f0d0e 2019-01-02 stsp
1249 708d8e67 2019-03-27 stsp static const struct got_error *
1250 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1251 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1252 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 708d8e67 2019-03-27 stsp {
1254 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1255 708d8e67 2019-03-27 stsp unsigned char status;
1256 708d8e67 2019-03-27 stsp struct stat sb;
1257 708d8e67 2019-03-27 stsp char *ondisk_path;
1258 708d8e67 2019-03-27 stsp
1259 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1260 708d8e67 2019-03-27 stsp == -1)
1261 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1262 708d8e67 2019-03-27 stsp
1263 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1264 708d8e67 2019-03-27 stsp if (err)
1265 708d8e67 2019-03-27 stsp return err;
1266 708d8e67 2019-03-27 stsp
1267 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1268 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1269 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1270 fc6346c4 2019-03-27 stsp /*
1271 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1272 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1273 fc6346c4 2019-03-27 stsp */
1274 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1275 fc6346c4 2019-03-27 stsp 0);
1276 708d8e67 2019-03-27 stsp if (err)
1277 708d8e67 2019-03-27 stsp return err;
1278 fc6346c4 2019-03-27 stsp } else {
1279 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1280 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1281 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1282 fc6346c4 2019-03-27 stsp if (err)
1283 fc6346c4 2019-03-27 stsp return err;
1284 fc6346c4 2019-03-27 stsp }
1285 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1286 708d8e67 2019-03-27 stsp }
1287 708d8e67 2019-03-27 stsp
1288 fc6346c4 2019-03-27 stsp return err;
1289 708d8e67 2019-03-27 stsp }
1290 708d8e67 2019-03-27 stsp
1291 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1292 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1293 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1294 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1295 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1296 8da9e5f4 2019-01-12 stsp void *progress_arg;
1297 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1298 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1299 8da9e5f4 2019-01-12 stsp };
1300 8da9e5f4 2019-01-12 stsp
1301 512f0d0e 2019-01-02 stsp static const struct got_error *
1302 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1303 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1304 512f0d0e 2019-01-02 stsp {
1305 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1306 0584f854 2019-04-06 stsp
1307 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1308 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1309 512f0d0e 2019-01-02 stsp
1310 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1311 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1312 8da9e5f4 2019-01-12 stsp }
1313 512f0d0e 2019-01-02 stsp
1314 8da9e5f4 2019-01-12 stsp static const struct got_error *
1315 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1316 8da9e5f4 2019-01-12 stsp {
1317 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1318 7a9df742 2019-01-08 stsp
1319 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1320 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1321 0584f854 2019-04-06 stsp
1322 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1323 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1324 9d31a1d8 2018-03-11 stsp }
1325 9d31a1d8 2018-03-11 stsp
1326 9d31a1d8 2018-03-11 stsp static const struct got_error *
1327 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1328 9d31a1d8 2018-03-11 stsp {
1329 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1330 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1331 8da9e5f4 2019-01-12 stsp char *path;
1332 9d31a1d8 2018-03-11 stsp
1333 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1334 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1335 0584f854 2019-04-06 stsp
1336 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1337 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1338 8da9e5f4 2019-01-12 stsp == -1)
1339 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1340 9d31a1d8 2018-03-11 stsp
1341 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1342 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1343 8da9e5f4 2019-01-12 stsp else
1344 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1345 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1346 9d31a1d8 2018-03-11 stsp
1347 8da9e5f4 2019-01-12 stsp free(path);
1348 0cd1c46a 2019-03-11 stsp return err;
1349 0cd1c46a 2019-03-11 stsp }
1350 0cd1c46a 2019-03-11 stsp
1351 517bab73 2019-03-11 stsp const struct got_error *
1352 517bab73 2019-03-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1353 0cd1c46a 2019-03-11 stsp {
1354 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1355 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1356 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1357 0cd1c46a 2019-03-11 stsp
1358 517bab73 2019-03-11 stsp *refname = NULL;
1359 517bab73 2019-03-11 stsp
1360 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1361 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1362 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1363 0cd1c46a 2019-03-11 stsp
1364 0647c563 2019-03-11 stsp if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1365 0647c563 2019-03-11 stsp == -1) {
1366 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1367 0647c563 2019-03-11 stsp *refname = NULL;
1368 0cd1c46a 2019-03-11 stsp }
1369 517bab73 2019-03-11 stsp free(uuidstr);
1370 517bab73 2019-03-11 stsp return err;
1371 517bab73 2019-03-11 stsp }
1372 517bab73 2019-03-11 stsp
1373 517bab73 2019-03-11 stsp /*
1374 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1375 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1376 517bab73 2019-03-11 stsp */
1377 517bab73 2019-03-11 stsp static const struct got_error *
1378 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1379 517bab73 2019-03-11 stsp {
1380 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1381 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1382 517bab73 2019-03-11 stsp char *refname;
1383 517bab73 2019-03-11 stsp
1384 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1385 517bab73 2019-03-11 stsp if (err)
1386 517bab73 2019-03-11 stsp return err;
1387 0cd1c46a 2019-03-11 stsp
1388 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1389 0cd1c46a 2019-03-11 stsp if (err)
1390 0cd1c46a 2019-03-11 stsp goto done;
1391 0cd1c46a 2019-03-11 stsp
1392 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1393 0cd1c46a 2019-03-11 stsp done:
1394 0cd1c46a 2019-03-11 stsp free(refname);
1395 0cd1c46a 2019-03-11 stsp if (ref)
1396 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1397 9d31a1d8 2018-03-11 stsp return err;
1398 9d31a1d8 2018-03-11 stsp }
1399 9d31a1d8 2018-03-11 stsp
1400 ebf99748 2019-05-09 stsp static const struct got_error *
1401 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1402 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1403 ebf99748 2019-05-09 stsp {
1404 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1405 ebf99748 2019-05-09 stsp FILE *index = NULL;
1406 0cd1c46a 2019-03-11 stsp
1407 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1408 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1409 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1410 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1411 ebf99748 2019-05-09 stsp
1412 ebf99748 2019-05-09 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1413 ebf99748 2019-05-09 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1414 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1415 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1416 ebf99748 2019-05-09 stsp goto done;
1417 ebf99748 2019-05-09 stsp }
1418 ebf99748 2019-05-09 stsp
1419 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1420 ebf99748 2019-05-09 stsp if (index == NULL) {
1421 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1422 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1423 ebf99748 2019-05-09 stsp } else {
1424 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1425 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1426 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1427 ebf99748 2019-05-09 stsp }
1428 ebf99748 2019-05-09 stsp done:
1429 ebf99748 2019-05-09 stsp if (err) {
1430 ebf99748 2019-05-09 stsp free(*fileindex_path);
1431 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1432 ebf99748 2019-05-09 stsp free(*fileindex);
1433 ebf99748 2019-05-09 stsp *fileindex = NULL;
1434 ebf99748 2019-05-09 stsp }
1435 ebf99748 2019-05-09 stsp return err;
1436 ebf99748 2019-05-09 stsp }
1437 c932eeeb 2019-05-22 stsp
1438 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1439 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1440 c932eeeb 2019-05-22 stsp const char *path;
1441 c932eeeb 2019-05-22 stsp size_t path_len;
1442 c932eeeb 2019-05-22 stsp const char *entry_name;
1443 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1444 a484d721 2019-06-10 stsp void *progress_arg;
1445 c932eeeb 2019-05-22 stsp };
1446 ebf99748 2019-05-09 stsp
1447 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1448 c932eeeb 2019-05-22 stsp static const struct got_error *
1449 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1450 c932eeeb 2019-05-22 stsp {
1451 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1452 c932eeeb 2019-05-22 stsp
1453 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1454 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1455 c932eeeb 2019-05-22 stsp return NULL;
1456 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1457 102ff934 2019-06-11 stsp return NULL;
1458 102ff934 2019-06-11 stsp
1459 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1460 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1461 c932eeeb 2019-05-22 stsp return NULL;
1462 c932eeeb 2019-05-22 stsp
1463 a484d721 2019-06-10 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE, ie->path);
1464 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1465 c932eeeb 2019-05-22 stsp return NULL;
1466 9c6338c4 2019-06-02 stsp }
1467 9c6338c4 2019-06-02 stsp
1468 9c6338c4 2019-06-02 stsp static const struct got_error *
1469 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1470 9c6338c4 2019-06-02 stsp {
1471 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1472 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1473 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1474 9c6338c4 2019-06-02 stsp
1475 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1476 9c6338c4 2019-06-02 stsp fileindex_path);
1477 9c6338c4 2019-06-02 stsp if (err)
1478 9c6338c4 2019-06-02 stsp goto done;
1479 9c6338c4 2019-06-02 stsp
1480 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1481 9c6338c4 2019-06-02 stsp if (err)
1482 9c6338c4 2019-06-02 stsp goto done;
1483 9c6338c4 2019-06-02 stsp
1484 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1485 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1486 9c6338c4 2019-06-02 stsp fileindex_path);
1487 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1488 9c6338c4 2019-06-02 stsp }
1489 9c6338c4 2019-06-02 stsp done:
1490 9c6338c4 2019-06-02 stsp if (new_index)
1491 9c6338c4 2019-06-02 stsp fclose(new_index);
1492 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1493 9c6338c4 2019-06-02 stsp return err;
1494 c932eeeb 2019-05-22 stsp }
1495 c932eeeb 2019-05-22 stsp
1496 9d31a1d8 2018-03-11 stsp const struct got_error *
1497 c4cdcb68 2019-04-03 stsp got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1498 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1499 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1500 9d31a1d8 2018-03-11 stsp {
1501 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1502 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1503 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1504 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1505 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1506 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1507 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1508 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1509 c4cdcb68 2019-04-03 stsp char *relpath = NULL, *entry_name = NULL;
1510 af12c6b9 2019-06-04 stsp struct bump_base_commit_id_arg bbc_arg;
1511 9d31a1d8 2018-03-11 stsp
1512 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1513 9d31a1d8 2018-03-11 stsp if (err)
1514 9d31a1d8 2018-03-11 stsp return err;
1515 93a30277 2018-12-24 stsp
1516 51514078 2018-12-25 stsp /*
1517 51514078 2018-12-25 stsp * Read the file index.
1518 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1519 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1520 51514078 2018-12-25 stsp */
1521 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1522 ebf99748 2019-05-09 stsp if (err)
1523 ebf99748 2019-05-09 stsp goto done;
1524 51514078 2018-12-25 stsp
1525 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1526 0cd1c46a 2019-03-11 stsp if (err)
1527 0cd1c46a 2019-03-11 stsp goto done;
1528 0cd1c46a 2019-03-11 stsp
1529 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1530 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1531 9d31a1d8 2018-03-11 stsp if (err)
1532 9d31a1d8 2018-03-11 stsp goto done;
1533 9d31a1d8 2018-03-11 stsp
1534 c4cdcb68 2019-04-03 stsp if (path[0]) {
1535 c4cdcb68 2019-04-03 stsp char *tree_path;
1536 c4cdcb68 2019-04-03 stsp int obj_type;
1537 c4cdcb68 2019-04-03 stsp relpath = strdup(path);
1538 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1539 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1540 c4cdcb68 2019-04-03 stsp goto done;
1541 c4cdcb68 2019-04-03 stsp }
1542 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1543 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1544 c4cdcb68 2019-04-03 stsp path) == -1) {
1545 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1546 c4cdcb68 2019-04-03 stsp goto done;
1547 c4cdcb68 2019-04-03 stsp }
1548 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1549 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1550 c4cdcb68 2019-04-03 stsp free(tree_path);
1551 c4cdcb68 2019-04-03 stsp if (err)
1552 c4cdcb68 2019-04-03 stsp goto done;
1553 c4cdcb68 2019-04-03 stsp err = got_object_get_type(&obj_type, repo, tree_id);
1554 c4cdcb68 2019-04-03 stsp if (err)
1555 c4cdcb68 2019-04-03 stsp goto done;
1556 c4cdcb68 2019-04-03 stsp if (obj_type == GOT_OBJ_TYPE_BLOB) {
1557 c4cdcb68 2019-04-03 stsp /* Split provided path into parent dir + entry name. */
1558 c4cdcb68 2019-04-03 stsp if (strchr(path, '/') == NULL) {
1559 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1560 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1561 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1562 c4cdcb68 2019-04-03 stsp goto done;
1563 c4cdcb68 2019-04-03 stsp }
1564 c4cdcb68 2019-04-03 stsp tree_path = strdup(worktree->path_prefix);
1565 c4cdcb68 2019-04-03 stsp if (tree_path == NULL) {
1566 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1567 c4cdcb68 2019-04-03 stsp goto done;
1568 c4cdcb68 2019-04-03 stsp }
1569 c4cdcb68 2019-04-03 stsp } else {
1570 c4cdcb68 2019-04-03 stsp err = got_path_dirname(&relpath, path);
1571 c4cdcb68 2019-04-03 stsp if (err)
1572 c4cdcb68 2019-04-03 stsp goto done;
1573 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s",
1574 c4cdcb68 2019-04-03 stsp worktree->path_prefix,
1575 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(
1576 c4cdcb68 2019-04-03 stsp worktree->path_prefix) ? "" : "/",
1577 c4cdcb68 2019-04-03 stsp relpath) == -1) {
1578 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1579 c4cdcb68 2019-04-03 stsp goto done;
1580 c4cdcb68 2019-04-03 stsp }
1581 c4cdcb68 2019-04-03 stsp }
1582 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1583 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1584 c4cdcb68 2019-04-03 stsp free(tree_path);
1585 c4cdcb68 2019-04-03 stsp if (err)
1586 c4cdcb68 2019-04-03 stsp goto done;
1587 c4cdcb68 2019-04-03 stsp entry_name = basename(path);
1588 c4cdcb68 2019-04-03 stsp if (entry_name == NULL) {
1589 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", path);
1590 c4cdcb68 2019-04-03 stsp goto done;
1591 c4cdcb68 2019-04-03 stsp }
1592 c4cdcb68 2019-04-03 stsp }
1593 c4cdcb68 2019-04-03 stsp } else {
1594 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1595 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1596 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1597 c4cdcb68 2019-04-03 stsp goto done;
1598 c4cdcb68 2019-04-03 stsp }
1599 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1600 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, worktree->path_prefix);
1601 c4cdcb68 2019-04-03 stsp if (err)
1602 c4cdcb68 2019-04-03 stsp goto done;
1603 c4cdcb68 2019-04-03 stsp }
1604 9d31a1d8 2018-03-11 stsp
1605 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1606 8da9e5f4 2019-01-12 stsp if (err)
1607 c4cdcb68 2019-04-03 stsp goto done;
1608 c4cdcb68 2019-04-03 stsp
1609 c4cdcb68 2019-04-03 stsp if (entry_name &&
1610 c4cdcb68 2019-04-03 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1611 c4cdcb68 2019-04-03 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1612 8da9e5f4 2019-01-12 stsp goto done;
1613 c4cdcb68 2019-04-03 stsp }
1614 9d31a1d8 2018-03-11 stsp
1615 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1616 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1617 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1618 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1619 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1620 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1621 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1622 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1623 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1624 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1625 af12c6b9 2019-06-04 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1626 c4cdcb68 2019-04-03 stsp entry_name, repo, &diff_cb, &arg);
1627 af12c6b9 2019-06-04 stsp if (err)
1628 af12c6b9 2019-06-04 stsp goto sync;
1629 8da9e5f4 2019-01-12 stsp
1630 af12c6b9 2019-06-04 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1631 af12c6b9 2019-06-04 stsp bbc_arg.entry_name = entry_name;
1632 af12c6b9 2019-06-04 stsp bbc_arg.path = path;
1633 af12c6b9 2019-06-04 stsp bbc_arg.path_len = strlen(path);
1634 a484d721 2019-06-10 stsp bbc_arg.progress_cb = progress_cb;
1635 a484d721 2019-06-10 stsp bbc_arg.progress_arg = progress_arg;
1636 af12c6b9 2019-06-04 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1637 af12c6b9 2019-06-04 stsp bump_base_commit_id, &bbc_arg);
1638 af12c6b9 2019-06-04 stsp sync:
1639 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1640 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1641 af12c6b9 2019-06-04 stsp err = sync_err;
1642 9d31a1d8 2018-03-11 stsp done:
1643 9c6338c4 2019-06-02 stsp free(fileindex_path);
1644 c4cdcb68 2019-04-03 stsp free(relpath);
1645 edfa7d7f 2018-09-11 stsp if (tree)
1646 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1647 9d31a1d8 2018-03-11 stsp if (commit)
1648 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1649 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1650 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1651 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1652 9d31a1d8 2018-03-11 stsp err = unlockerr;
1653 f8d1f275 2019-02-04 stsp return err;
1654 f8d1f275 2019-02-04 stsp }
1655 f8d1f275 2019-02-04 stsp
1656 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1657 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1658 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1659 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1660 234035bc 2019-06-01 stsp void *progress_arg;
1661 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1662 234035bc 2019-06-01 stsp void *cancel_arg;
1663 234035bc 2019-06-01 stsp };
1664 234035bc 2019-06-01 stsp
1665 234035bc 2019-06-01 stsp static const struct got_error *
1666 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1667 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1668 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1669 234035bc 2019-06-01 stsp struct got_repository *repo)
1670 234035bc 2019-06-01 stsp {
1671 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1672 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1673 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1674 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1675 234035bc 2019-06-01 stsp struct stat sb;
1676 234035bc 2019-06-01 stsp unsigned char status;
1677 234035bc 2019-06-01 stsp int local_changes_subsumed;
1678 234035bc 2019-06-01 stsp
1679 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1680 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1681 234035bc 2019-06-01 stsp if (ie == NULL) {
1682 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1683 234035bc 2019-06-01 stsp path2);
1684 234035bc 2019-06-01 stsp return NULL;
1685 234035bc 2019-06-01 stsp }
1686 234035bc 2019-06-01 stsp
1687 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1688 234035bc 2019-06-01 stsp path2) == -1)
1689 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1690 234035bc 2019-06-01 stsp
1691 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1692 234035bc 2019-06-01 stsp if (err)
1693 234035bc 2019-06-01 stsp goto done;
1694 234035bc 2019-06-01 stsp
1695 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1696 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1697 234035bc 2019-06-01 stsp path2);
1698 234035bc 2019-06-01 stsp goto done;
1699 234035bc 2019-06-01 stsp }
1700 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1701 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1702 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1703 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1704 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status, path2);
1705 234035bc 2019-06-01 stsp goto done;
1706 234035bc 2019-06-01 stsp }
1707 234035bc 2019-06-01 stsp
1708 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1709 234035bc 2019-06-01 stsp ondisk_path, path2, sb.st_mode, blob2, repo,
1710 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1711 234035bc 2019-06-01 stsp } else if (blob1) {
1712 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path1);
1713 234035bc 2019-06-01 stsp if (ie == NULL) {
1714 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1715 234035bc 2019-06-01 stsp path2);
1716 234035bc 2019-06-01 stsp return NULL;
1717 234035bc 2019-06-01 stsp }
1718 2b92fad7 2019-06-02 stsp
1719 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1720 2b92fad7 2019-06-02 stsp path1) == -1)
1721 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
1722 2b92fad7 2019-06-02 stsp
1723 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1724 2b92fad7 2019-06-02 stsp if (err)
1725 2b92fad7 2019-06-02 stsp goto done;
1726 2b92fad7 2019-06-02 stsp
1727 2b92fad7 2019-06-02 stsp switch (status) {
1728 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
1729 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1730 2b92fad7 2019-06-02 stsp path1);
1731 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
1732 2b92fad7 2019-06-02 stsp if (err)
1733 2b92fad7 2019-06-02 stsp goto done;
1734 2b92fad7 2019-06-02 stsp if (ie)
1735 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1736 2b92fad7 2019-06-02 stsp break;
1737 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
1738 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
1739 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1740 2b92fad7 2019-06-02 stsp path1);
1741 2b92fad7 2019-06-02 stsp if (ie)
1742 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1743 2b92fad7 2019-06-02 stsp break;
1744 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
1745 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
1746 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
1747 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg,
1748 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
1749 2b92fad7 2019-06-02 stsp break;
1750 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
1751 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, status, path1);
1752 2b92fad7 2019-06-02 stsp break;
1753 2b92fad7 2019-06-02 stsp default:
1754 2b92fad7 2019-06-02 stsp break;
1755 2b92fad7 2019-06-02 stsp }
1756 234035bc 2019-06-01 stsp } else if (blob2) {
1757 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1758 234035bc 2019-06-01 stsp path2) == -1)
1759 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1760 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1761 234035bc 2019-06-01 stsp if (ie) {
1762 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
1763 234035bc 2019-06-01 stsp repo);
1764 234035bc 2019-06-01 stsp if (err)
1765 234035bc 2019-06-01 stsp goto done;
1766 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1767 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1768 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1769 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1770 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status,
1771 234035bc 2019-06-01 stsp path2);
1772 234035bc 2019-06-01 stsp goto done;
1773 234035bc 2019-06-01 stsp }
1774 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
1775 234035bc 2019-06-01 stsp NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1776 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1777 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1778 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
1779 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
1780 234035bc 2019-06-01 stsp blob2, 0);
1781 234035bc 2019-06-01 stsp if (err)
1782 234035bc 2019-06-01 stsp goto done;
1783 234035bc 2019-06-01 stsp }
1784 234035bc 2019-06-01 stsp } else {
1785 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1786 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
1787 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
1788 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
1789 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
1790 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1791 234035bc 2019-06-01 stsp if (err)
1792 234035bc 2019-06-01 stsp goto done;
1793 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
1794 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
1795 234035bc 2019-06-01 stsp if (err)
1796 234035bc 2019-06-01 stsp goto done;
1797 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
1798 2b92fad7 2019-06-02 stsp if (err) {
1799 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
1800 2b92fad7 2019-06-02 stsp goto done;
1801 2b92fad7 2019-06-02 stsp }
1802 234035bc 2019-06-01 stsp }
1803 234035bc 2019-06-01 stsp }
1804 234035bc 2019-06-01 stsp done:
1805 234035bc 2019-06-01 stsp free(ondisk_path);
1806 234035bc 2019-06-01 stsp return err;
1807 234035bc 2019-06-01 stsp }
1808 234035bc 2019-06-01 stsp
1809 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
1810 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1811 234035bc 2019-06-01 stsp struct got_repository *repo;
1812 234035bc 2019-06-01 stsp };
1813 234035bc 2019-06-01 stsp
1814 234035bc 2019-06-01 stsp static const struct got_error *
1815 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1816 234035bc 2019-06-01 stsp {
1817 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
1818 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
1819 234035bc 2019-06-01 stsp unsigned char status;
1820 234035bc 2019-06-01 stsp struct stat sb;
1821 234035bc 2019-06-01 stsp char *ondisk_path;
1822 234035bc 2019-06-01 stsp
1823 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
1824 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1825 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
1826 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
1827 234035bc 2019-06-01 stsp
1828 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1829 234035bc 2019-06-01 stsp == -1)
1830 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1831 234035bc 2019-06-01 stsp
1832 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
1833 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1834 234035bc 2019-06-01 stsp if (err)
1835 234035bc 2019-06-01 stsp return err;
1836 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
1837 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
1838 234035bc 2019-06-01 stsp
1839 234035bc 2019-06-01 stsp return NULL;
1840 234035bc 2019-06-01 stsp }
1841 234035bc 2019-06-01 stsp
1842 234035bc 2019-06-01 stsp const struct got_error *
1843 234035bc 2019-06-01 stsp got_worktree_merge_files(struct got_worktree *worktree,
1844 234035bc 2019-06-01 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1845 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1846 234035bc 2019-06-01 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1847 234035bc 2019-06-01 stsp {
1848 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1849 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1850 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1851 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
1852 234035bc 2019-06-01 stsp char *fileindex_path = NULL;
1853 234035bc 2019-06-01 stsp struct got_fileindex *fileindex = NULL;
1854 234035bc 2019-06-01 stsp struct check_merge_ok_arg mok_arg;
1855 234035bc 2019-06-01 stsp
1856 234035bc 2019-06-01 stsp err = lock_worktree(worktree, LOCK_EX);
1857 234035bc 2019-06-01 stsp if (err)
1858 234035bc 2019-06-01 stsp return err;
1859 234035bc 2019-06-01 stsp
1860 234035bc 2019-06-01 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1861 234035bc 2019-06-01 stsp if (err)
1862 234035bc 2019-06-01 stsp goto done;
1863 234035bc 2019-06-01 stsp
1864 234035bc 2019-06-01 stsp mok_arg.worktree = worktree;
1865 234035bc 2019-06-01 stsp mok_arg.repo = repo;
1866 234035bc 2019-06-01 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1867 234035bc 2019-06-01 stsp &mok_arg);
1868 234035bc 2019-06-01 stsp if (err)
1869 234035bc 2019-06-01 stsp goto done;
1870 234035bc 2019-06-01 stsp
1871 03415a1a 2019-06-02 stsp if (commit_id1) {
1872 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1873 03415a1a 2019-06-02 stsp worktree->path_prefix);
1874 03415a1a 2019-06-02 stsp if (err)
1875 03415a1a 2019-06-02 stsp goto done;
1876 03415a1a 2019-06-02 stsp
1877 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1878 03415a1a 2019-06-02 stsp if (err)
1879 03415a1a 2019-06-02 stsp goto done;
1880 03415a1a 2019-06-02 stsp }
1881 234035bc 2019-06-01 stsp
1882 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1883 234035bc 2019-06-01 stsp worktree->path_prefix);
1884 234035bc 2019-06-01 stsp if (err)
1885 234035bc 2019-06-01 stsp goto done;
1886 234035bc 2019-06-01 stsp
1887 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1888 234035bc 2019-06-01 stsp if (err)
1889 234035bc 2019-06-01 stsp goto done;
1890 234035bc 2019-06-01 stsp
1891 234035bc 2019-06-01 stsp arg.worktree = worktree;
1892 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
1893 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
1894 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
1895 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
1896 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
1897 234035bc 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1898 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1899 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1900 af12c6b9 2019-06-04 stsp err = sync_err;
1901 234035bc 2019-06-01 stsp done:
1902 234035bc 2019-06-01 stsp got_fileindex_free(fileindex);
1903 234035bc 2019-06-01 stsp if (tree1)
1904 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
1905 234035bc 2019-06-01 stsp if (tree2)
1906 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
1907 234035bc 2019-06-01 stsp
1908 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1909 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
1910 234035bc 2019-06-01 stsp err = unlockerr;
1911 234035bc 2019-06-01 stsp return err;
1912 234035bc 2019-06-01 stsp }
1913 234035bc 2019-06-01 stsp
1914 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1915 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1916 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1917 927df6b7 2019-02-10 stsp const char *status_path;
1918 927df6b7 2019-02-10 stsp size_t status_path_len;
1919 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1920 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1921 f8d1f275 2019-02-04 stsp void *status_arg;
1922 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1923 f8d1f275 2019-02-04 stsp void *cancel_arg;
1924 f8d1f275 2019-02-04 stsp };
1925 f8d1f275 2019-02-04 stsp
1926 f8d1f275 2019-02-04 stsp static const struct got_error *
1927 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1928 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1929 927df6b7 2019-02-10 stsp struct got_repository *repo)
1930 927df6b7 2019-02-10 stsp {
1931 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1932 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1933 927df6b7 2019-02-10 stsp struct stat sb;
1934 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1935 927df6b7 2019-02-10 stsp
1936 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1937 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1938 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1939 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1940 016a88dd 2019-05-13 stsp err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1941 016a88dd 2019-05-13 stsp &commit_id);
1942 927df6b7 2019-02-10 stsp }
1943 927df6b7 2019-02-10 stsp return err;
1944 927df6b7 2019-02-10 stsp }
1945 927df6b7 2019-02-10 stsp
1946 927df6b7 2019-02-10 stsp static const struct got_error *
1947 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1948 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1949 f8d1f275 2019-02-04 stsp {
1950 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1951 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1952 f8d1f275 2019-02-04 stsp char *abspath;
1953 f8d1f275 2019-02-04 stsp
1954 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1955 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1956 0584f854 2019-04-06 stsp
1957 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1958 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1959 927df6b7 2019-02-10 stsp return NULL;
1960 927df6b7 2019-02-10 stsp
1961 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1962 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1963 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1964 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1965 f8d1f275 2019-02-04 stsp } else {
1966 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1967 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1968 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1969 f8d1f275 2019-02-04 stsp }
1970 f8d1f275 2019-02-04 stsp
1971 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1972 927df6b7 2019-02-10 stsp a->repo);
1973 f8d1f275 2019-02-04 stsp free(abspath);
1974 9d31a1d8 2018-03-11 stsp return err;
1975 9d31a1d8 2018-03-11 stsp }
1976 f8d1f275 2019-02-04 stsp
1977 f8d1f275 2019-02-04 stsp static const struct got_error *
1978 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1979 f8d1f275 2019-02-04 stsp {
1980 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1981 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1982 2ec1f75b 2019-03-26 stsp unsigned char status;
1983 927df6b7 2019-02-10 stsp
1984 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1985 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1986 0584f854 2019-04-06 stsp
1987 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1988 927df6b7 2019-02-10 stsp return NULL;
1989 927df6b7 2019-02-10 stsp
1990 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1991 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1992 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1993 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
1994 2ec1f75b 2019-03-26 stsp else
1995 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
1996 016a88dd 2019-05-13 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1997 016a88dd 2019-05-13 stsp &commit_id);
1998 f8d1f275 2019-02-04 stsp }
1999 f8d1f275 2019-02-04 stsp
2000 f8d1f275 2019-02-04 stsp static const struct got_error *
2001 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2002 f8d1f275 2019-02-04 stsp {
2003 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2004 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2005 f8d1f275 2019-02-04 stsp char *path = NULL;
2006 f8d1f275 2019-02-04 stsp
2007 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2008 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2009 0584f854 2019-04-06 stsp
2010 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2011 2c201a36 2019-02-10 stsp return NULL;
2012 2c201a36 2019-02-10 stsp
2013 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2014 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2015 f8d1f275 2019-02-04 stsp return NULL;
2016 f8d1f275 2019-02-04 stsp
2017 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2018 927df6b7 2019-02-10 stsp return NULL;
2019 927df6b7 2019-02-10 stsp
2020 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2021 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2022 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2023 f8d1f275 2019-02-04 stsp } else {
2024 f8d1f275 2019-02-04 stsp path = de->d_name;
2025 f8d1f275 2019-02-04 stsp }
2026 f8d1f275 2019-02-04 stsp
2027 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2028 016a88dd 2019-05-13 stsp NULL, NULL);
2029 f8d1f275 2019-02-04 stsp if (parent_path[0])
2030 f8d1f275 2019-02-04 stsp free(path);
2031 b72f483a 2019-02-05 stsp return err;
2032 f8d1f275 2019-02-04 stsp }
2033 f8d1f275 2019-02-04 stsp
2034 f8d1f275 2019-02-04 stsp const struct got_error *
2035 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
2036 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
2037 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2038 f8d1f275 2019-02-04 stsp {
2039 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2040 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2041 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
2042 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
2043 f8d1f275 2019-02-04 stsp FILE *index = NULL;
2044 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2045 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2046 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2047 f8d1f275 2019-02-04 stsp
2048 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
2049 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
2050 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2051 f8d1f275 2019-02-04 stsp goto done;
2052 f8d1f275 2019-02-04 stsp }
2053 f8d1f275 2019-02-04 stsp
2054 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2055 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2056 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2057 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
2058 f8d1f275 2019-02-04 stsp goto done;
2059 f8d1f275 2019-02-04 stsp }
2060 f8d1f275 2019-02-04 stsp
2061 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
2062 f8d1f275 2019-02-04 stsp if (index == NULL) {
2063 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
2064 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2065 f8d1f275 2019-02-04 stsp goto done;
2066 f8d1f275 2019-02-04 stsp }
2067 f8d1f275 2019-02-04 stsp } else {
2068 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
2069 f8d1f275 2019-02-04 stsp fclose(index);
2070 f8d1f275 2019-02-04 stsp if (err)
2071 f8d1f275 2019-02-04 stsp goto done;
2072 f8d1f275 2019-02-04 stsp }
2073 f8d1f275 2019-02-04 stsp
2074 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2075 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
2076 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2077 c513d110 2019-02-05 stsp goto done;
2078 c513d110 2019-02-05 stsp }
2079 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2080 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2081 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
2082 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
2083 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
2084 927df6b7 2019-02-10 stsp if (ie == NULL) {
2085 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
2086 927df6b7 2019-02-10 stsp goto done;
2087 927df6b7 2019-02-10 stsp }
2088 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
2089 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
2090 927df6b7 2019-02-10 stsp goto done;
2091 927df6b7 2019-02-10 stsp } else {
2092 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2093 927df6b7 2019-02-10 stsp goto done;
2094 927df6b7 2019-02-10 stsp }
2095 927df6b7 2019-02-10 stsp }
2096 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
2097 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
2098 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
2099 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
2100 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
2101 927df6b7 2019-02-10 stsp arg.status_path = path;
2102 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
2103 f8d1f275 2019-02-04 stsp arg.repo = repo;
2104 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
2105 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
2106 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
2107 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
2108 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2109 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
2110 f8d1f275 2019-02-04 stsp done:
2111 f8d1f275 2019-02-04 stsp if (workdir)
2112 f8d1f275 2019-02-04 stsp closedir(workdir);
2113 927df6b7 2019-02-10 stsp free(ondisk_path);
2114 f8d1f275 2019-02-04 stsp free(fileindex_path);
2115 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2116 f8d1f275 2019-02-04 stsp return err;
2117 f8d1f275 2019-02-04 stsp }
2118 6c7ab921 2019-03-18 stsp
2119 6c7ab921 2019-03-18 stsp const struct got_error *
2120 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2121 6c7ab921 2019-03-18 stsp const char *arg)
2122 6c7ab921 2019-03-18 stsp {
2123 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2124 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
2125 6c7ab921 2019-03-18 stsp size_t len;
2126 6c7ab921 2019-03-18 stsp
2127 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2128 6c7ab921 2019-03-18 stsp
2129 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2130 6c7ab921 2019-03-18 stsp if (resolved == NULL)
2131 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", arg);
2132 6c7ab921 2019-03-18 stsp
2133 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2134 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2135 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2136 6c7ab921 2019-03-18 stsp goto done;
2137 6c7ab921 2019-03-18 stsp }
2138 6c7ab921 2019-03-18 stsp
2139 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2140 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2141 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2142 f6d88e1a 2019-05-29 stsp if (err)
2143 f6d88e1a 2019-05-29 stsp goto done;
2144 f6d88e1a 2019-05-29 stsp } else {
2145 f6d88e1a 2019-05-29 stsp path = strdup("");
2146 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2147 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2148 f6d88e1a 2019-05-29 stsp goto done;
2149 f6d88e1a 2019-05-29 stsp }
2150 6c7ab921 2019-03-18 stsp }
2151 6c7ab921 2019-03-18 stsp
2152 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2153 6c7ab921 2019-03-18 stsp len = strlen(path);
2154 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
2155 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2156 6c7ab921 2019-03-18 stsp len--;
2157 6c7ab921 2019-03-18 stsp }
2158 6c7ab921 2019-03-18 stsp done:
2159 6c7ab921 2019-03-18 stsp free(resolved);
2160 6c7ab921 2019-03-18 stsp if (err == NULL)
2161 6c7ab921 2019-03-18 stsp *wt_path = path;
2162 6c7ab921 2019-03-18 stsp else
2163 6c7ab921 2019-03-18 stsp free(path);
2164 d00136be 2019-03-26 stsp return err;
2165 d00136be 2019-03-26 stsp }
2166 d00136be 2019-03-26 stsp
2167 07f5b47a 2019-06-02 stsp static const struct got_error *
2168 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2169 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2170 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2171 07f5b47a 2019-06-02 stsp {
2172 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2173 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2174 07f5b47a 2019-06-02 stsp
2175 07f5b47a 2019-06-02 stsp /* Re-adding an existing entry is a no-op. */
2176 07f5b47a 2019-06-02 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2177 07f5b47a 2019-06-02 stsp return NULL;
2178 07f5b47a 2019-06-02 stsp
2179 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2180 07f5b47a 2019-06-02 stsp if (err)
2181 07f5b47a 2019-06-02 stsp return err;
2182 07f5b47a 2019-06-02 stsp
2183 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2184 07f5b47a 2019-06-02 stsp if (err) {
2185 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2186 07f5b47a 2019-06-02 stsp return err;
2187 07f5b47a 2019-06-02 stsp }
2188 07f5b47a 2019-06-02 stsp
2189 07f5b47a 2019-06-02 stsp return report_file_status(ie, relpath, status_cb, status_arg, repo);
2190 07f5b47a 2019-06-02 stsp }
2191 07f5b47a 2019-06-02 stsp
2192 d00136be 2019-03-26 stsp const struct got_error *
2193 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2194 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
2195 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2196 031a5338 2019-03-26 stsp struct got_repository *repo)
2197 d00136be 2019-03-26 stsp {
2198 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2199 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2200 9c6338c4 2019-06-02 stsp FILE *index = NULL;
2201 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2202 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2203 d00136be 2019-03-26 stsp
2204 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2205 d00136be 2019-03-26 stsp if (err)
2206 d00136be 2019-03-26 stsp return err;
2207 d00136be 2019-03-26 stsp
2208 d00136be 2019-03-26 stsp
2209 d00136be 2019-03-26 stsp fileindex = got_fileindex_alloc();
2210 d00136be 2019-03-26 stsp if (fileindex == NULL) {
2211 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2212 d00136be 2019-03-26 stsp goto done;
2213 d00136be 2019-03-26 stsp }
2214 d00136be 2019-03-26 stsp
2215 d00136be 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2216 d00136be 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2217 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2218 d00136be 2019-03-26 stsp fileindex_path = NULL;
2219 d00136be 2019-03-26 stsp goto done;
2220 d00136be 2019-03-26 stsp }
2221 d00136be 2019-03-26 stsp
2222 d00136be 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2223 d00136be 2019-03-26 stsp if (index == NULL) {
2224 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2225 d00136be 2019-03-26 stsp goto done;
2226 d00136be 2019-03-26 stsp }
2227 d00136be 2019-03-26 stsp
2228 d00136be 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2229 d00136be 2019-03-26 stsp if (err)
2230 d00136be 2019-03-26 stsp goto done;
2231 d00136be 2019-03-26 stsp
2232 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2233 1dd54920 2019-05-11 stsp char *relpath;
2234 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
2235 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
2236 1dd54920 2019-05-11 stsp if (err)
2237 af12c6b9 2019-06-04 stsp break;
2238 07f5b47a 2019-06-02 stsp err = schedule_addition(pe->path, fileindex, relpath,
2239 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2240 1dd54920 2019-05-11 stsp free(relpath);
2241 1dd54920 2019-05-11 stsp if (err)
2242 af12c6b9 2019-06-04 stsp break;
2243 1dd54920 2019-05-11 stsp }
2244 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2245 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2246 af12c6b9 2019-06-04 stsp err = sync_err;
2247 d00136be 2019-03-26 stsp done:
2248 d00136be 2019-03-26 stsp if (index) {
2249 d00136be 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2250 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2251 d00136be 2019-03-26 stsp }
2252 d00136be 2019-03-26 stsp if (fileindex)
2253 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2254 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2255 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2256 d00136be 2019-03-26 stsp err = unlockerr;
2257 6c7ab921 2019-03-18 stsp return err;
2258 6c7ab921 2019-03-18 stsp }
2259 17ed4618 2019-06-02 stsp
2260 17ed4618 2019-06-02 stsp static const struct got_error *
2261 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2262 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2263 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2264 17ed4618 2019-06-02 stsp struct got_repository *repo)
2265 17ed4618 2019-06-02 stsp {
2266 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2267 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2268 17ed4618 2019-06-02 stsp unsigned char status;
2269 17ed4618 2019-06-02 stsp struct stat sb;
2270 17ed4618 2019-06-02 stsp
2271 17ed4618 2019-06-02 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2272 17ed4618 2019-06-02 stsp if (ie == NULL)
2273 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2274 2ec1f75b 2019-03-26 stsp
2275 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2276 17ed4618 2019-06-02 stsp if (err)
2277 17ed4618 2019-06-02 stsp return err;
2278 17ed4618 2019-06-02 stsp
2279 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2280 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2281 17ed4618 2019-06-02 stsp return got_error_set_errno(ENOENT, ondisk_path);
2282 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_MODIFY)
2283 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_STATUS);
2284 17ed4618 2019-06-02 stsp if (!delete_local_mods)
2285 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_MODIFIED);
2286 17ed4618 2019-06-02 stsp }
2287 17ed4618 2019-06-02 stsp
2288 17ed4618 2019-06-02 stsp if (unlink(ondisk_path) != 0)
2289 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2290 17ed4618 2019-06-02 stsp
2291 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2292 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2293 17ed4618 2019-06-02 stsp }
2294 17ed4618 2019-06-02 stsp
2295 2ec1f75b 2019-03-26 stsp const struct got_error *
2296 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2297 17ed4618 2019-06-02 stsp struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2298 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2299 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2300 2ec1f75b 2019-03-26 stsp {
2301 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2302 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2303 9c6338c4 2019-06-02 stsp FILE *index = NULL;
2304 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2305 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2306 2ec1f75b 2019-03-26 stsp
2307 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2308 2ec1f75b 2019-03-26 stsp if (err)
2309 2ec1f75b 2019-03-26 stsp return err;
2310 2ec1f75b 2019-03-26 stsp
2311 2ec1f75b 2019-03-26 stsp fileindex = got_fileindex_alloc();
2312 2ec1f75b 2019-03-26 stsp if (fileindex == NULL) {
2313 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2314 2ec1f75b 2019-03-26 stsp goto done;
2315 2ec1f75b 2019-03-26 stsp }
2316 2ec1f75b 2019-03-26 stsp
2317 2ec1f75b 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2318 2ec1f75b 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2319 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2320 2ec1f75b 2019-03-26 stsp fileindex_path = NULL;
2321 2ec1f75b 2019-03-26 stsp goto done;
2322 2ec1f75b 2019-03-26 stsp }
2323 2ec1f75b 2019-03-26 stsp
2324 2ec1f75b 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2325 2ec1f75b 2019-03-26 stsp if (index == NULL) {
2326 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2327 2ec1f75b 2019-03-26 stsp goto done;
2328 2ec1f75b 2019-03-26 stsp }
2329 2ec1f75b 2019-03-26 stsp
2330 2ec1f75b 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2331 2ec1f75b 2019-03-26 stsp if (err)
2332 2ec1f75b 2019-03-26 stsp goto done;
2333 2ec1f75b 2019-03-26 stsp
2334 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2335 17ed4618 2019-06-02 stsp char *relpath;
2336 17ed4618 2019-06-02 stsp err = got_path_skip_common_ancestor(&relpath,
2337 17ed4618 2019-06-02 stsp got_worktree_get_root_path(worktree), pe->path);
2338 17ed4618 2019-06-02 stsp if (err)
2339 af12c6b9 2019-06-04 stsp break;
2340 17ed4618 2019-06-02 stsp err = schedule_for_deletion(pe->path, fileindex, relpath,
2341 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2342 17ed4618 2019-06-02 stsp free(relpath);
2343 17ed4618 2019-06-02 stsp if (err)
2344 af12c6b9 2019-06-04 stsp break;
2345 2ec1f75b 2019-03-26 stsp }
2346 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2347 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2348 af12c6b9 2019-06-04 stsp err = sync_err;
2349 2ec1f75b 2019-03-26 stsp done:
2350 2ec1f75b 2019-03-26 stsp if (index) {
2351 2ec1f75b 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2352 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2353 2ec1f75b 2019-03-26 stsp }
2354 2ec1f75b 2019-03-26 stsp if (fileindex)
2355 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2356 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2357 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2358 2ec1f75b 2019-03-26 stsp err = unlockerr;
2359 2ec1f75b 2019-03-26 stsp return err;
2360 2ec1f75b 2019-03-26 stsp }
2361 a129376b 2019-03-28 stsp
2362 e20a8b6f 2019-06-04 stsp static const struct got_error *
2363 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2364 a129376b 2019-03-28 stsp const char *ondisk_path,
2365 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2366 a129376b 2019-03-28 stsp struct got_repository *repo)
2367 a129376b 2019-03-28 stsp {
2368 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2369 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2370 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2371 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2372 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2373 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2374 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2375 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2376 a129376b 2019-03-28 stsp unsigned char status;
2377 a129376b 2019-03-28 stsp struct stat sb;
2378 a129376b 2019-03-28 stsp
2379 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2380 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2381 a129376b 2019-03-28 stsp if (err)
2382 a129376b 2019-03-28 stsp goto done;
2383 a129376b 2019-03-28 stsp
2384 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2385 a129376b 2019-03-28 stsp if (ie == NULL) {
2386 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2387 a129376b 2019-03-28 stsp goto done;
2388 a129376b 2019-03-28 stsp }
2389 a129376b 2019-03-28 stsp
2390 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2391 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2392 a129376b 2019-03-28 stsp if (err) {
2393 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2394 a129376b 2019-03-28 stsp goto done;
2395 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2396 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2397 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2398 e20a8b6f 2019-06-04 stsp goto done;
2399 e20a8b6f 2019-06-04 stsp }
2400 a129376b 2019-03-28 stsp }
2401 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2402 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2403 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2404 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2405 a129376b 2019-03-28 stsp goto done;
2406 a129376b 2019-03-28 stsp }
2407 a129376b 2019-03-28 stsp } else {
2408 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2409 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2410 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2411 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2412 a129376b 2019-03-28 stsp goto done;
2413 a129376b 2019-03-28 stsp }
2414 a129376b 2019-03-28 stsp } else {
2415 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2416 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2417 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2418 a129376b 2019-03-28 stsp goto done;
2419 a129376b 2019-03-28 stsp }
2420 a129376b 2019-03-28 stsp }
2421 a129376b 2019-03-28 stsp }
2422 a129376b 2019-03-28 stsp
2423 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2424 a129376b 2019-03-28 stsp tree_path);
2425 a129376b 2019-03-28 stsp if (err)
2426 a129376b 2019-03-28 stsp goto done;
2427 a129376b 2019-03-28 stsp
2428 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2429 a129376b 2019-03-28 stsp if (err)
2430 a129376b 2019-03-28 stsp goto done;
2431 a129376b 2019-03-28 stsp
2432 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2433 a129376b 2019-03-28 stsp if (te_name == NULL) {
2434 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ie->path);
2435 a129376b 2019-03-28 stsp goto done;
2436 a129376b 2019-03-28 stsp }
2437 a129376b 2019-03-28 stsp
2438 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2439 a129376b 2019-03-28 stsp if (err)
2440 a129376b 2019-03-28 stsp goto done;
2441 a129376b 2019-03-28 stsp
2442 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2443 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2444 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2445 a129376b 2019-03-28 stsp goto done;
2446 a129376b 2019-03-28 stsp }
2447 a129376b 2019-03-28 stsp
2448 a129376b 2019-03-28 stsp switch (status) {
2449 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2450 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2451 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2452 a129376b 2019-03-28 stsp break;
2453 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2454 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2455 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2456 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2457 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2458 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2459 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2460 a129376b 2019-03-28 stsp if (err)
2461 a129376b 2019-03-28 stsp goto done;
2462 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2463 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2464 a129376b 2019-03-28 stsp progress_arg);
2465 a129376b 2019-03-28 stsp if (err)
2466 a129376b 2019-03-28 stsp goto done;
2467 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2468 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2469 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2470 a129376b 2019-03-28 stsp if (err)
2471 a129376b 2019-03-28 stsp goto done;
2472 a129376b 2019-03-28 stsp }
2473 a129376b 2019-03-28 stsp break;
2474 e20a8b6f 2019-06-04 stsp }
2475 a129376b 2019-03-28 stsp default:
2476 a129376b 2019-03-28 stsp goto done;
2477 a129376b 2019-03-28 stsp }
2478 a129376b 2019-03-28 stsp done:
2479 a129376b 2019-03-28 stsp free(relpath);
2480 e20a8b6f 2019-06-04 stsp free(parent_path);
2481 a129376b 2019-03-28 stsp free(tree_path);
2482 a129376b 2019-03-28 stsp if (blob)
2483 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2484 a129376b 2019-03-28 stsp if (tree)
2485 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2486 a129376b 2019-03-28 stsp free(tree_id);
2487 e20a8b6f 2019-06-04 stsp return err;
2488 e20a8b6f 2019-06-04 stsp }
2489 e20a8b6f 2019-06-04 stsp
2490 e20a8b6f 2019-06-04 stsp const struct got_error *
2491 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2492 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2493 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2494 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2495 e20a8b6f 2019-06-04 stsp {
2496 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2497 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2498 e20a8b6f 2019-06-04 stsp FILE *index = NULL;
2499 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2500 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2501 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2502 e20a8b6f 2019-06-04 stsp
2503 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2504 e20a8b6f 2019-06-04 stsp if (err)
2505 e20a8b6f 2019-06-04 stsp return err;
2506 e20a8b6f 2019-06-04 stsp
2507 e20a8b6f 2019-06-04 stsp fileindex = got_fileindex_alloc();
2508 e20a8b6f 2019-06-04 stsp if (fileindex == NULL) {
2509 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("got_fileindex_alloc");
2510 e20a8b6f 2019-06-04 stsp goto done;
2511 e20a8b6f 2019-06-04 stsp }
2512 e20a8b6f 2019-06-04 stsp
2513 e20a8b6f 2019-06-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2514 e20a8b6f 2019-06-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2515 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("asprintf");
2516 e20a8b6f 2019-06-04 stsp fileindex_path = NULL;
2517 e20a8b6f 2019-06-04 stsp goto done;
2518 e20a8b6f 2019-06-04 stsp }
2519 e20a8b6f 2019-06-04 stsp
2520 e20a8b6f 2019-06-04 stsp index = fopen(fileindex_path, "rb");
2521 e20a8b6f 2019-06-04 stsp if (index == NULL) {
2522 e20a8b6f 2019-06-04 stsp err = got_error_from_errno2("fopen", fileindex_path);
2523 e20a8b6f 2019-06-04 stsp goto done;
2524 e20a8b6f 2019-06-04 stsp }
2525 e20a8b6f 2019-06-04 stsp
2526 e20a8b6f 2019-06-04 stsp err = got_fileindex_read(fileindex, index);
2527 e20a8b6f 2019-06-04 stsp if (err)
2528 e20a8b6f 2019-06-04 stsp goto done;
2529 e20a8b6f 2019-06-04 stsp
2530 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2531 e20a8b6f 2019-06-04 stsp err = revert_file(worktree, fileindex, pe->path,
2532 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2533 e20a8b6f 2019-06-04 stsp if (err)
2534 af12c6b9 2019-06-04 stsp break;
2535 e20a8b6f 2019-06-04 stsp }
2536 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2537 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2538 e20a8b6f 2019-06-04 stsp err = sync_err;
2539 af12c6b9 2019-06-04 stsp done:
2540 a129376b 2019-03-28 stsp if (index) {
2541 a129376b 2019-03-28 stsp if (fclose(index) != 0 && err == NULL)
2542 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2543 a129376b 2019-03-28 stsp }
2544 a129376b 2019-03-28 stsp if (fileindex)
2545 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2546 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2547 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2548 a129376b 2019-03-28 stsp err = unlockerr;
2549 c4296144 2019-05-09 stsp return err;
2550 c4296144 2019-05-09 stsp }
2551 c4296144 2019-05-09 stsp
2552 cf066bf8 2019-05-09 stsp static void
2553 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2554 cf066bf8 2019-05-09 stsp {
2555 24519714 2019-05-09 stsp free(ct->path);
2556 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2557 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2558 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2559 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2560 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2561 cf066bf8 2019-05-09 stsp free(ct);
2562 cf066bf8 2019-05-09 stsp }
2563 24519714 2019-05-09 stsp
2564 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2565 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2566 24519714 2019-05-09 stsp struct got_repository *repo;
2567 24519714 2019-05-09 stsp struct got_worktree *worktree;
2568 24519714 2019-05-09 stsp };
2569 cf066bf8 2019-05-09 stsp
2570 c4296144 2019-05-09 stsp static const struct got_error *
2571 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2572 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2573 c4296144 2019-05-09 stsp {
2574 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2575 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2576 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2577 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2578 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2579 768aea60 2019-05-09 stsp struct stat sb;
2580 c4296144 2019-05-09 stsp
2581 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2582 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2583 c4296144 2019-05-09 stsp
2584 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2585 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2586 c4296144 2019-05-09 stsp return NULL;
2587 0b5cc0d6 2019-05-09 stsp
2588 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2589 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2590 036813ee 2019-05-09 stsp goto done;
2591 036813ee 2019-05-09 stsp }
2592 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2593 036813ee 2019-05-09 stsp parent_path = strdup("");
2594 69960a46 2019-05-09 stsp if (parent_path == NULL)
2595 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2596 69960a46 2019-05-09 stsp } else {
2597 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2598 69960a46 2019-05-09 stsp if (err)
2599 69960a46 2019-05-09 stsp return err;
2600 69960a46 2019-05-09 stsp }
2601 c4296144 2019-05-09 stsp
2602 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2603 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2604 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2605 c4296144 2019-05-09 stsp goto done;
2606 768aea60 2019-05-09 stsp }
2607 768aea60 2019-05-09 stsp
2608 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2609 768aea60 2019-05-09 stsp relpath) == -1) {
2610 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2611 768aea60 2019-05-09 stsp goto done;
2612 768aea60 2019-05-09 stsp }
2613 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2614 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2615 768aea60 2019-05-09 stsp } else {
2616 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2617 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2618 768aea60 2019-05-09 stsp goto done;
2619 768aea60 2019-05-09 stsp }
2620 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2621 c4296144 2019-05-09 stsp }
2622 c4296144 2019-05-09 stsp
2623 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2624 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2625 44d03001 2019-05-09 stsp relpath) == -1) {
2626 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2627 44d03001 2019-05-09 stsp goto done;
2628 44d03001 2019-05-09 stsp }
2629 44d03001 2019-05-09 stsp
2630 cf066bf8 2019-05-09 stsp ct->status = status;
2631 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2632 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2633 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2634 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2635 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2636 b416585c 2019-05-13 stsp goto done;
2637 b416585c 2019-05-13 stsp }
2638 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2639 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2640 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2641 036813ee 2019-05-09 stsp goto done;
2642 036813ee 2019-05-09 stsp }
2643 ca2503ea 2019-05-09 stsp }
2644 24519714 2019-05-09 stsp ct->path = strdup(path);
2645 24519714 2019-05-09 stsp if (ct->path == NULL) {
2646 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2647 24519714 2019-05-09 stsp goto done;
2648 24519714 2019-05-09 stsp }
2649 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2650 c4296144 2019-05-09 stsp done:
2651 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2652 ed175427 2019-05-09 stsp free_commitable(ct);
2653 24519714 2019-05-09 stsp free(parent_path);
2654 036813ee 2019-05-09 stsp free(path);
2655 a129376b 2019-03-28 stsp return err;
2656 a129376b 2019-03-28 stsp }
2657 c4296144 2019-05-09 stsp
2658 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2659 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2660 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2661 036813ee 2019-05-09 stsp struct got_repository *);
2662 ed175427 2019-05-09 stsp
2663 ed175427 2019-05-09 stsp static const struct got_error *
2664 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2665 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2666 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2667 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2668 afa376bf 2019-05-09 stsp struct got_repository *repo)
2669 ed175427 2019-05-09 stsp {
2670 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2671 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2672 036813ee 2019-05-09 stsp char *subpath;
2673 ed175427 2019-05-09 stsp
2674 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2675 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2676 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2677 ed175427 2019-05-09 stsp
2678 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2679 ed175427 2019-05-09 stsp if (err)
2680 ed175427 2019-05-09 stsp return err;
2681 ed175427 2019-05-09 stsp
2682 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2683 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2684 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2685 036813ee 2019-05-09 stsp free(subpath);
2686 036813ee 2019-05-09 stsp return err;
2687 036813ee 2019-05-09 stsp }
2688 ed175427 2019-05-09 stsp
2689 036813ee 2019-05-09 stsp static const struct got_error *
2690 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2691 036813ee 2019-05-09 stsp {
2692 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2693 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2694 ed175427 2019-05-09 stsp
2695 036813ee 2019-05-09 stsp *match = 0;
2696 ed175427 2019-05-09 stsp
2697 036813ee 2019-05-09 stsp if (strchr(ct->path, '/') == NULL) {
2698 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2699 0f63689d 2019-05-10 stsp return NULL;
2700 036813ee 2019-05-09 stsp }
2701 0b5cc0d6 2019-05-09 stsp
2702 0f63689d 2019-05-10 stsp err = got_path_dirname(&ct_parent_path, ct->path);
2703 0f63689d 2019-05-10 stsp if (err)
2704 0f63689d 2019-05-10 stsp return err;
2705 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2706 036813ee 2019-05-09 stsp free(ct_parent_path);
2707 036813ee 2019-05-09 stsp return err;
2708 036813ee 2019-05-09 stsp }
2709 0b5cc0d6 2019-05-09 stsp
2710 768aea60 2019-05-09 stsp static mode_t
2711 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2712 768aea60 2019-05-09 stsp {
2713 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2714 768aea60 2019-05-09 stsp }
2715 768aea60 2019-05-09 stsp
2716 036813ee 2019-05-09 stsp static const struct got_error *
2717 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2718 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
2719 036813ee 2019-05-09 stsp {
2720 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2721 ca2503ea 2019-05-09 stsp
2722 036813ee 2019-05-09 stsp *new_te = NULL;
2723 0b5cc0d6 2019-05-09 stsp
2724 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2725 036813ee 2019-05-09 stsp if (err)
2726 036813ee 2019-05-09 stsp goto done;
2727 0b5cc0d6 2019-05-09 stsp
2728 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2729 036813ee 2019-05-09 stsp
2730 036813ee 2019-05-09 stsp free((*new_te)->id);
2731 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2732 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2733 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2734 036813ee 2019-05-09 stsp goto done;
2735 036813ee 2019-05-09 stsp }
2736 036813ee 2019-05-09 stsp done:
2737 036813ee 2019-05-09 stsp if (err && *new_te) {
2738 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2739 036813ee 2019-05-09 stsp *new_te = NULL;
2740 036813ee 2019-05-09 stsp }
2741 036813ee 2019-05-09 stsp return err;
2742 036813ee 2019-05-09 stsp }
2743 036813ee 2019-05-09 stsp
2744 036813ee 2019-05-09 stsp static const struct got_error *
2745 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2746 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
2747 036813ee 2019-05-09 stsp {
2748 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2749 036813ee 2019-05-09 stsp char *ct_name;
2750 036813ee 2019-05-09 stsp
2751 036813ee 2019-05-09 stsp *new_te = NULL;
2752 036813ee 2019-05-09 stsp
2753 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2754 036813ee 2019-05-09 stsp if (*new_te == NULL)
2755 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2756 036813ee 2019-05-09 stsp
2757 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2758 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2759 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
2760 036813ee 2019-05-09 stsp goto done;
2761 036813ee 2019-05-09 stsp }
2762 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2763 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2764 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2765 036813ee 2019-05-09 stsp goto done;
2766 036813ee 2019-05-09 stsp }
2767 036813ee 2019-05-09 stsp
2768 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2769 036813ee 2019-05-09 stsp
2770 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2771 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2772 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2773 036813ee 2019-05-09 stsp goto done;
2774 036813ee 2019-05-09 stsp }
2775 036813ee 2019-05-09 stsp done:
2776 036813ee 2019-05-09 stsp if (err && *new_te) {
2777 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2778 036813ee 2019-05-09 stsp *new_te = NULL;
2779 036813ee 2019-05-09 stsp }
2780 036813ee 2019-05-09 stsp return err;
2781 036813ee 2019-05-09 stsp }
2782 036813ee 2019-05-09 stsp
2783 036813ee 2019-05-09 stsp static const struct got_error *
2784 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
2785 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
2786 036813ee 2019-05-09 stsp {
2787 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2788 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
2789 036813ee 2019-05-09 stsp
2790 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2791 036813ee 2019-05-09 stsp if (err)
2792 036813ee 2019-05-09 stsp return err;
2793 036813ee 2019-05-09 stsp if (new_pe == NULL)
2794 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2795 036813ee 2019-05-09 stsp return NULL;
2796 afa376bf 2019-05-09 stsp }
2797 afa376bf 2019-05-09 stsp
2798 afa376bf 2019-05-09 stsp static const struct got_error *
2799 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
2800 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
2801 afa376bf 2019-05-09 stsp {
2802 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
2803 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
2804 afa376bf 2019-05-09 stsp ct_path++;
2805 016a88dd 2019-05-13 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2806 036813ee 2019-05-09 stsp }
2807 036813ee 2019-05-09 stsp
2808 036813ee 2019-05-09 stsp static const struct got_error *
2809 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
2810 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2811 44d03001 2019-05-09 stsp {
2812 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
2813 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
2814 44d03001 2019-05-09 stsp char *te_path;
2815 44d03001 2019-05-09 stsp
2816 44d03001 2019-05-09 stsp *modified = 0;
2817 44d03001 2019-05-09 stsp
2818 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
2819 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
2820 44d03001 2019-05-09 stsp te->name) == -1)
2821 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2822 44d03001 2019-05-09 stsp
2823 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2824 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2825 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
2826 44d03001 2019-05-09 stsp strlen(te_path));
2827 44d03001 2019-05-09 stsp if (*modified)
2828 44d03001 2019-05-09 stsp break;
2829 44d03001 2019-05-09 stsp }
2830 44d03001 2019-05-09 stsp
2831 44d03001 2019-05-09 stsp free(te_path);
2832 44d03001 2019-05-09 stsp return err;
2833 44d03001 2019-05-09 stsp }
2834 44d03001 2019-05-09 stsp
2835 44d03001 2019-05-09 stsp static const struct got_error *
2836 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
2837 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
2838 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
2839 036813ee 2019-05-09 stsp {
2840 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2841 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2842 036813ee 2019-05-09 stsp
2843 036813ee 2019-05-09 stsp *ctp = NULL;
2844 036813ee 2019-05-09 stsp
2845 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2846 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2847 036813ee 2019-05-09 stsp char *ct_name = NULL;
2848 036813ee 2019-05-09 stsp int path_matches;
2849 036813ee 2019-05-09 stsp
2850 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
2851 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
2852 036813ee 2019-05-09 stsp continue;
2853 036813ee 2019-05-09 stsp
2854 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2855 036813ee 2019-05-09 stsp continue;
2856 036813ee 2019-05-09 stsp
2857 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2858 036813ee 2019-05-09 stsp if (err)
2859 036813ee 2019-05-09 stsp return err;
2860 036813ee 2019-05-09 stsp if (!path_matches)
2861 036813ee 2019-05-09 stsp continue;
2862 036813ee 2019-05-09 stsp
2863 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
2864 036813ee 2019-05-09 stsp if (ct_name == NULL)
2865 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
2866 036813ee 2019-05-09 stsp
2867 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
2868 036813ee 2019-05-09 stsp continue;
2869 036813ee 2019-05-09 stsp
2870 036813ee 2019-05-09 stsp *ctp = ct;
2871 036813ee 2019-05-09 stsp break;
2872 036813ee 2019-05-09 stsp }
2873 036813ee 2019-05-09 stsp
2874 036813ee 2019-05-09 stsp return err;
2875 036813ee 2019-05-09 stsp }
2876 036813ee 2019-05-09 stsp
2877 036813ee 2019-05-09 stsp static const struct got_error *
2878 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
2879 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
2880 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2881 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2882 036813ee 2019-05-09 stsp struct got_repository *repo)
2883 036813ee 2019-05-09 stsp {
2884 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2885 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
2886 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
2887 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
2888 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
2889 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2890 036813ee 2019-05-09 stsp
2891 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
2892 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
2893 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
2894 036813ee 2019-05-09 stsp
2895 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
2896 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2897 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2898 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
2899 036813ee 2019-05-09 stsp
2900 a3df2849 2019-05-20 stsp if (ct->status != GOT_STATUS_ADD ||
2901 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
2902 036813ee 2019-05-09 stsp continue;
2903 036813ee 2019-05-09 stsp
2904 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
2905 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
2906 036813ee 2019-05-09 stsp continue;
2907 036813ee 2019-05-09 stsp
2908 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2909 036813ee 2019-05-09 stsp pe->path);
2910 036813ee 2019-05-09 stsp if (err)
2911 036813ee 2019-05-09 stsp goto done;
2912 036813ee 2019-05-09 stsp
2913 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
2914 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
2915 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
2916 036813ee 2019-05-09 stsp if (err)
2917 036813ee 2019-05-09 stsp goto done;
2918 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
2919 afa376bf 2019-05-09 stsp if (err)
2920 afa376bf 2019-05-09 stsp goto done;
2921 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
2922 036813ee 2019-05-09 stsp } else {
2923 2f51b5b3 2019-05-09 stsp char *subtree_path;
2924 036813ee 2019-05-09 stsp
2925 2f51b5b3 2019-05-09 stsp *slash = '\0'; /* trim trailing path components */
2926 baa7dcfa 2019-05-09 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2927 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
2928 2f51b5b3 2019-05-09 stsp child_path) == -1) {
2929 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2930 2f51b5b3 2019-05-09 stsp goto done;
2931 baa7dcfa 2019-05-09 stsp }
2932 baa7dcfa 2019-05-09 stsp
2933 9ba0479c 2019-05-10 stsp new_te = calloc(1, sizeof(*new_te));
2934 9ba0479c 2019-05-10 stsp new_te->mode = S_IFDIR;
2935 9ba0479c 2019-05-10 stsp new_te->name = strdup(child_path);
2936 9ba0479c 2019-05-10 stsp if (new_te->name == NULL) {
2937 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2938 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2939 9ba0479c 2019-05-10 stsp new_te = NULL;
2940 9ba0479c 2019-05-10 stsp goto done;
2941 9ba0479c 2019-05-10 stsp }
2942 baa7dcfa 2019-05-09 stsp err = write_tree(&new_te->id, NULL, subtree_path,
2943 afa376bf 2019-05-09 stsp commitable_paths, status_cb, status_arg, repo);
2944 2f51b5b3 2019-05-09 stsp free(subtree_path);
2945 9ba0479c 2019-05-10 stsp if (err) {
2946 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2947 9ba0479c 2019-05-10 stsp new_te = NULL;
2948 036813ee 2019-05-09 stsp goto done;
2949 9ba0479c 2019-05-10 stsp }
2950 ed175427 2019-05-09 stsp }
2951 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2952 2f51b5b3 2019-05-09 stsp if (err)
2953 2f51b5b3 2019-05-09 stsp goto done;
2954 2f51b5b3 2019-05-09 stsp }
2955 2f51b5b3 2019-05-09 stsp
2956 2f51b5b3 2019-05-09 stsp if (base_tree) {
2957 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
2958 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
2959 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2960 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2961 2f51b5b3 2019-05-09 stsp
2962 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
2963 44d03001 2019-05-09 stsp int modified;
2964 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2965 036813ee 2019-05-09 stsp if (err)
2966 036813ee 2019-05-09 stsp goto done;
2967 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
2968 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
2969 2f51b5b3 2019-05-09 stsp if (err)
2970 2f51b5b3 2019-05-09 stsp goto done;
2971 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
2972 44d03001 2019-05-09 stsp if (modified) {
2973 44d03001 2019-05-09 stsp free(new_te->id);
2974 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
2975 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
2976 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
2977 44d03001 2019-05-09 stsp if (err)
2978 44d03001 2019-05-09 stsp goto done;
2979 44d03001 2019-05-09 stsp }
2980 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2981 036813ee 2019-05-09 stsp if (err)
2982 036813ee 2019-05-09 stsp goto done;
2983 2f51b5b3 2019-05-09 stsp continue;
2984 036813ee 2019-05-09 stsp }
2985 2f51b5b3 2019-05-09 stsp
2986 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
2987 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
2988 2f51b5b3 2019-05-09 stsp if (ct) {
2989 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
2990 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
2991 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
2992 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
2993 2f51b5b3 2019-05-09 stsp if (err)
2994 2f51b5b3 2019-05-09 stsp goto done;
2995 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2996 2f51b5b3 2019-05-09 stsp if (err)
2997 2f51b5b3 2019-05-09 stsp goto done;
2998 2f51b5b3 2019-05-09 stsp }
2999 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3000 b416585c 2019-05-13 stsp status_arg);
3001 afa376bf 2019-05-09 stsp if (err)
3002 afa376bf 2019-05-09 stsp goto done;
3003 2f51b5b3 2019-05-09 stsp } else {
3004 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3005 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3006 2f51b5b3 2019-05-09 stsp if (err)
3007 2f51b5b3 2019-05-09 stsp goto done;
3008 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3009 2f51b5b3 2019-05-09 stsp if (err)
3010 2f51b5b3 2019-05-09 stsp goto done;
3011 2f51b5b3 2019-05-09 stsp }
3012 ca2503ea 2019-05-09 stsp }
3013 ed175427 2019-05-09 stsp }
3014 0b5cc0d6 2019-05-09 stsp
3015 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3016 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3017 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3018 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3019 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3020 0b5cc0d6 2019-05-09 stsp }
3021 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3022 ed175427 2019-05-09 stsp done:
3023 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3024 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3025 ed175427 2019-05-09 stsp return err;
3026 ed175427 2019-05-09 stsp }
3027 ed175427 2019-05-09 stsp
3028 ebf99748 2019-05-09 stsp static const struct got_error *
3029 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3030 ebf99748 2019-05-09 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3031 ebf99748 2019-05-09 stsp {
3032 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err;
3033 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3034 ebf99748 2019-05-09 stsp struct got_fileindex *fileindex = NULL;
3035 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3036 ebf99748 2019-05-09 stsp
3037 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3038 ebf99748 2019-05-09 stsp if (err)
3039 ebf99748 2019-05-09 stsp return err;
3040 ebf99748 2019-05-09 stsp
3041 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3042 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3043 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3044 ebf99748 2019-05-09 stsp
3045 ebf99748 2019-05-09 stsp ie = got_fileindex_entry_get(fileindex, pe->path);
3046 ebf99748 2019-05-09 stsp if (ie) {
3047 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
3048 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3049 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3050 ebf99748 2019-05-09 stsp } else
3051 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3052 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3053 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3054 ebf99748 2019-05-09 stsp } else {
3055 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3056 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3057 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3058 ebf99748 2019-05-09 stsp if (err)
3059 af12c6b9 2019-06-04 stsp break;
3060 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3061 ebf99748 2019-05-09 stsp if (err)
3062 af12c6b9 2019-06-04 stsp break;
3063 ebf99748 2019-05-09 stsp }
3064 ebf99748 2019-05-09 stsp }
3065 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3066 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3067 af12c6b9 2019-06-04 stsp err = sync_err;
3068 ebf99748 2019-05-09 stsp free(fileindex_path);
3069 ebf99748 2019-05-09 stsp got_fileindex_free(fileindex);
3070 d56d26ce 2019-05-10 stsp return err;
3071 d56d26ce 2019-05-10 stsp }
3072 d56d26ce 2019-05-10 stsp
3073 d56d26ce 2019-05-10 stsp static const struct got_error *
3074 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3075 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
3076 d56d26ce 2019-05-10 stsp {
3077 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3078 1a36436d 2019-06-10 stsp struct got_object_id *id_in_head = NULL, *id = NULL;
3079 1a36436d 2019-06-10 stsp struct got_commit_object *commit = NULL;
3080 1a36436d 2019-06-10 stsp char *path = NULL;
3081 1a36436d 2019-06-10 stsp const char *ct_path = ct->in_repo_path;
3082 1a36436d 2019-06-10 stsp
3083 1a36436d 2019-06-10 stsp while (ct_path[0] == '/')
3084 1a36436d 2019-06-10 stsp ct_path++;
3085 d56d26ce 2019-05-10 stsp
3086 1251a9e5 2019-05-10 stsp /*
3087 1a36436d 2019-06-10 stsp * Ensure that no modifications were made to files *and their parents*
3088 1a36436d 2019-06-10 stsp * in commits between the file's base commit and the branch head.
3089 b416585c 2019-05-13 stsp *
3090 1a36436d 2019-06-10 stsp * Checking the parents is important for detecting conflicting tree
3091 1a36436d 2019-06-10 stsp * configurations (files or parent folders might have been moved,
3092 1a36436d 2019-06-10 stsp * deleted, added again, etc.). Such changes need to be merged with
3093 1a36436d 2019-06-10 stsp * local changes before a commit can occur.
3094 1a36436d 2019-06-10 stsp *
3095 1a36436d 2019-06-10 stsp * The implication is that the file's (parent) entry in the root
3096 1a36436d 2019-06-10 stsp * directory must have the same ID in all relevant commits.
3097 b416585c 2019-05-13 stsp */
3098 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
3099 1a36436d 2019-06-10 stsp struct got_object_qid *pid;
3100 1a36436d 2019-06-10 stsp char *slash;
3101 1a36436d 2019-06-10 stsp struct got_object_id *root_entry_id = NULL;
3102 d56d26ce 2019-05-10 stsp
3103 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3104 1a36436d 2019-06-10 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3105 1a36436d 2019-06-10 stsp return NULL;
3106 d56d26ce 2019-05-10 stsp
3107 1a36436d 2019-06-10 stsp /* Compute the path to the root directory's entry. */
3108 1a36436d 2019-06-10 stsp path = strdup(ct_path);
3109 1a36436d 2019-06-10 stsp if (path == NULL) {
3110 1a36436d 2019-06-10 stsp err = got_error_from_errno("strdup");
3111 1a36436d 2019-06-10 stsp goto done;
3112 1a36436d 2019-06-10 stsp }
3113 1a36436d 2019-06-10 stsp slash = strchr(path, '/');
3114 1a36436d 2019-06-10 stsp if (slash)
3115 1a36436d 2019-06-10 stsp *slash = '\0';
3116 1a36436d 2019-06-10 stsp
3117 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
3118 1a36436d 2019-06-10 stsp if (err)
3119 1a36436d 2019-06-10 stsp goto done;
3120 1a36436d 2019-06-10 stsp
3121 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&root_entry_id, repo,
3122 1a36436d 2019-06-10 stsp head_commit_id, path);
3123 1a36436d 2019-06-10 stsp if (err)
3124 1a36436d 2019-06-10 stsp goto done;
3125 1a36436d 2019-06-10 stsp
3126 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3127 1a36436d 2019-06-10 stsp while (pid) {
3128 1a36436d 2019-06-10 stsp struct got_commit_object *pcommit;
3129 1a36436d 2019-06-10 stsp
3130 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3131 1a36436d 2019-06-10 stsp if (err) {
3132 1a36436d 2019-06-10 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
3133 1a36436d 2019-06-10 stsp goto done;
3134 1a36436d 2019-06-10 stsp err = NULL;
3135 1a36436d 2019-06-10 stsp break;
3136 1a36436d 2019-06-10 stsp }
3137 1a36436d 2019-06-10 stsp
3138 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id, repo, pid->id, path);
3139 1a36436d 2019-06-10 stsp if (err)
3140 1a36436d 2019-06-10 stsp goto done;
3141 1a36436d 2019-06-10 stsp
3142 1a36436d 2019-06-10 stsp if (got_object_id_cmp(id, root_entry_id) != 0) {
3143 1a36436d 2019-06-10 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3144 1a36436d 2019-06-10 stsp break;
3145 1a36436d 2019-06-10 stsp }
3146 1a36436d 2019-06-10 stsp
3147 1a36436d 2019-06-10 stsp if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3148 1a36436d 2019-06-10 stsp break; /* all relevant commits scanned */
3149 1a36436d 2019-06-10 stsp
3150 1a36436d 2019-06-10 stsp err = got_object_open_as_commit(&pcommit, repo,
3151 1a36436d 2019-06-10 stsp pid->id);
3152 1a36436d 2019-06-10 stsp if (err)
3153 1a36436d 2019-06-10 stsp goto done;
3154 1a36436d 2019-06-10 stsp
3155 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3156 1a36436d 2019-06-10 stsp commit = pcommit;
3157 1a36436d 2019-06-10 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3158 1a36436d 2019-06-10 stsp commit));
3159 1a36436d 2019-06-10 stsp }
3160 1a36436d 2019-06-10 stsp } else {
3161 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3162 1a36436d 2019-06-10 stsp err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3163 1a36436d 2019-06-10 stsp ct_path);
3164 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3165 1a36436d 2019-06-10 stsp goto done;
3166 1a36436d 2019-06-10 stsp err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3167 1a36436d 2019-06-10 stsp }
3168 1a36436d 2019-06-10 stsp done:
3169 1a36436d 2019-06-10 stsp if (commit)
3170 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3171 d56d26ce 2019-05-10 stsp free(id_in_head);
3172 1a36436d 2019-06-10 stsp free(id);
3173 1a36436d 2019-06-10 stsp free(path);
3174 1a36436d 2019-06-10 stsp return err;
3175 ebf99748 2019-05-09 stsp }
3176 ebf99748 2019-05-09 stsp
3177 c4296144 2019-05-09 stsp const struct got_error *
3178 c4296144 2019-05-09 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3179 c4296144 2019-05-09 stsp struct got_worktree *worktree, const char *ondisk_path,
3180 33ad4cbe 2019-05-12 jcs const char *author, const char *committer,
3181 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3182 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3183 afa376bf 2019-05-09 stsp struct got_repository *repo)
3184 c4296144 2019-05-09 stsp {
3185 c4296144 2019-05-09 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3186 ed175427 2019-05-09 stsp struct collect_commitables_arg cc_arg;
3187 036813ee 2019-05-09 stsp struct got_pathlist_head commitable_paths;
3188 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3189 bc70eb79 2019-05-09 stsp char *relpath = NULL;
3190 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3191 36a38700 2019-05-10 stsp struct got_reference *head_ref = NULL;
3192 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3193 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id = NULL;
3194 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3195 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3196 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3197 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3198 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3199 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3200 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3201 c4296144 2019-05-09 stsp
3202 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3203 c4296144 2019-05-09 stsp
3204 036813ee 2019-05-09 stsp TAILQ_INIT(&commitable_paths);
3205 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3206 c4296144 2019-05-09 stsp
3207 c4296144 2019-05-09 stsp if (ondisk_path) {
3208 c4296144 2019-05-09 stsp err = got_path_skip_common_ancestor(&relpath,
3209 c4296144 2019-05-09 stsp worktree->root_path, ondisk_path);
3210 c4296144 2019-05-09 stsp if (err)
3211 c4296144 2019-05-09 stsp return err;
3212 c4296144 2019-05-09 stsp }
3213 c4296144 2019-05-09 stsp
3214 c4296144 2019-05-09 stsp err = lock_worktree(worktree, LOCK_EX);
3215 c4296144 2019-05-09 stsp if (err)
3216 c4296144 2019-05-09 stsp goto done;
3217 675c7539 2019-05-09 stsp
3218 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3219 36a38700 2019-05-10 stsp if (err)
3220 36a38700 2019-05-10 stsp goto done;
3221 36a38700 2019-05-10 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3222 09f5bd90 2019-05-09 stsp if (err)
3223 09f5bd90 2019-05-09 stsp goto done;
3224 09f5bd90 2019-05-09 stsp
3225 036813ee 2019-05-09 stsp cc_arg.commitable_paths = &commitable_paths;
3226 24519714 2019-05-09 stsp cc_arg.worktree = worktree;
3227 24519714 2019-05-09 stsp cc_arg.repo = repo;
3228 675c7539 2019-05-09 stsp err = got_worktree_status(worktree, relpath ? relpath : "",
3229 ed175427 2019-05-09 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
3230 675c7539 2019-05-09 stsp if (err)
3231 675c7539 2019-05-09 stsp goto done;
3232 675c7539 2019-05-09 stsp
3233 33ad4cbe 2019-05-12 jcs if (TAILQ_EMPTY(&commitable_paths)) {
3234 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3235 33ad4cbe 2019-05-12 jcs goto done;
3236 33ad4cbe 2019-05-12 jcs }
3237 33ad4cbe 2019-05-12 jcs
3238 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3239 588edf97 2019-05-10 stsp if (err)
3240 588edf97 2019-05-10 stsp goto done;
3241 588edf97 2019-05-10 stsp
3242 819f385b 2019-05-10 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3243 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3244 d56d26ce 2019-05-10 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
3245 d56d26ce 2019-05-10 stsp if (err)
3246 819f385b 2019-05-10 stsp goto done;
3247 819f385b 2019-05-10 stsp }
3248 de18fc63 2019-05-09 stsp
3249 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3250 588edf97 2019-05-10 stsp if (err)
3251 588edf97 2019-05-10 stsp goto done;
3252 588edf97 2019-05-10 stsp
3253 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3254 33ad4cbe 2019-05-12 jcs err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3255 33ad4cbe 2019-05-12 jcs if (err)
3256 33ad4cbe 2019-05-12 jcs goto done;
3257 33ad4cbe 2019-05-12 jcs }
3258 c4296144 2019-05-09 stsp
3259 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3260 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3261 33ad4cbe 2019-05-12 jcs goto done;
3262 33ad4cbe 2019-05-12 jcs }
3263 33ad4cbe 2019-05-12 jcs
3264 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3265 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3266 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3267 cf066bf8 2019-05-09 stsp char *ondisk_path;
3268 cf066bf8 2019-05-09 stsp
3269 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3270 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3271 cf066bf8 2019-05-09 stsp continue;
3272 cf066bf8 2019-05-09 stsp
3273 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3274 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3275 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3276 cf066bf8 2019-05-09 stsp goto done;
3277 cf066bf8 2019-05-09 stsp }
3278 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3279 a7055788 2019-05-09 stsp free(ondisk_path);
3280 cf066bf8 2019-05-09 stsp if (err)
3281 cf066bf8 2019-05-09 stsp goto done;
3282 cf066bf8 2019-05-09 stsp }
3283 036813ee 2019-05-09 stsp
3284 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3285 588edf97 2019-05-10 stsp err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3286 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3287 036813ee 2019-05-09 stsp if (err)
3288 036813ee 2019-05-09 stsp goto done;
3289 036813ee 2019-05-09 stsp
3290 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3291 de18fc63 2019-05-09 stsp if (err)
3292 de18fc63 2019-05-09 stsp goto done;
3293 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3294 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3295 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3296 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3297 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3298 33ad4cbe 2019-05-12 jcs free(logmsg);
3299 9d40349a 2019-05-09 stsp if (err)
3300 9d40349a 2019-05-09 stsp goto done;
3301 9d40349a 2019-05-09 stsp
3302 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3303 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3304 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3305 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3306 09f5bd90 2019-05-09 stsp goto done;
3307 09f5bd90 2019-05-09 stsp }
3308 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3309 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3310 09f5bd90 2019-05-09 stsp if (err)
3311 09f5bd90 2019-05-09 stsp goto done;
3312 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3313 ebf99748 2019-05-09 stsp if (err)
3314 ebf99748 2019-05-09 stsp goto done;
3315 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3316 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3317 09f5bd90 2019-05-09 stsp goto done;
3318 09f5bd90 2019-05-09 stsp }
3319 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3320 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3321 f2c16586 2019-05-09 stsp if (err)
3322 f2c16586 2019-05-09 stsp goto done;
3323 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3324 f2c16586 2019-05-09 stsp if (err)
3325 f2c16586 2019-05-09 stsp goto done;
3326 f2c16586 2019-05-09 stsp
3327 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3328 09f5bd90 2019-05-09 stsp if (err)
3329 09f5bd90 2019-05-09 stsp goto done;
3330 09f5bd90 2019-05-09 stsp
3331 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3332 ebf99748 2019-05-09 stsp if (err)
3333 ebf99748 2019-05-09 stsp goto done;
3334 ebf99748 2019-05-09 stsp
3335 ebf99748 2019-05-09 stsp err = update_fileindex_after_commit(&commitable_paths,
3336 ebf99748 2019-05-09 stsp *new_commit_id, worktree);
3337 ebf99748 2019-05-09 stsp if (err)
3338 ebf99748 2019-05-09 stsp goto done;
3339 c4296144 2019-05-09 stsp done:
3340 c4296144 2019-05-09 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3341 c4296144 2019-05-09 stsp if (unlockerr && err == NULL)
3342 c4296144 2019-05-09 stsp err = unlockerr;
3343 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3344 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3345 ed175427 2019-05-09 stsp free_commitable(ct);
3346 cf066bf8 2019-05-09 stsp }
3347 036813ee 2019-05-09 stsp got_pathlist_free(&commitable_paths);
3348 588edf97 2019-05-10 stsp if (head_tree)
3349 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3350 588edf97 2019-05-10 stsp if (head_commit)
3351 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3352 c4296144 2019-05-09 stsp free(relpath);
3353 09f5bd90 2019-05-09 stsp free(head_commit_id);
3354 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3355 36a38700 2019-05-10 stsp if (head_ref)
3356 36a38700 2019-05-10 stsp got_ref_close(head_ref);
3357 2f17228e 2019-05-12 stsp if (head_ref2) {
3358 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3359 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3360 2f17228e 2019-05-12 stsp err = unlockerr;
3361 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3362 2f17228e 2019-05-12 stsp }
3363 c4296144 2019-05-09 stsp return err;
3364 8656d6c4 2019-05-20 stsp }
3365 8656d6c4 2019-05-20 stsp
3366 8656d6c4 2019-05-20 stsp const char *
3367 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3368 8656d6c4 2019-05-20 stsp {
3369 8656d6c4 2019-05-20 stsp return ct->path;
3370 8656d6c4 2019-05-20 stsp }
3371 8656d6c4 2019-05-20 stsp
3372 8656d6c4 2019-05-20 stsp unsigned int
3373 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3374 8656d6c4 2019-05-20 stsp {
3375 8656d6c4 2019-05-20 stsp return ct->status;
3376 c4296144 2019-05-09 stsp }