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