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