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