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 a2e6d162 2019-07-27 stsp err = got_error_path(path, 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 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
146 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", 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 a2e6d162 2019-07-27 stsp got_error_path(path, 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 a2e6d162 2019-07-27 stsp err = got_error_path(path, 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 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
359 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
360 6d9d28c3 2018-03-11 stsp goto done;
361 6d9d28c3 2018-03-11 stsp }
362 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
363 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
364 6d9d28c3 2018-03-11 stsp goto done;
365 6d9d28c3 2018-03-11 stsp }
366 6d9d28c3 2018-03-11 stsp
367 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
368 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
369 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
373 6d9d28c3 2018-03-11 stsp
374 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
375 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
376 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
377 6d9d28c3 2018-03-11 stsp goto done;
378 6d9d28c3 2018-03-11 stsp }
379 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
380 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
381 6d9d28c3 2018-03-11 stsp if (err)
382 6d9d28c3 2018-03-11 stsp goto done;
383 eaccb85f 2018-12-25 stsp
384 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
386 93a30277 2018-12-24 stsp if (err)
387 93a30277 2018-12-24 stsp goto done;
388 93a30277 2018-12-24 stsp
389 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
390 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
391 c442a90d 2019-03-10 stsp if (err)
392 c442a90d 2019-03-10 stsp goto done;
393 c442a90d 2019-03-10 stsp
394 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
395 eaccb85f 2018-12-25 stsp if (err)
396 c442a90d 2019-03-10 stsp goto done;
397 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
398 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
399 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
400 eaccb85f 2018-12-25 stsp goto done;
401 c442a90d 2019-03-10 stsp }
402 eaccb85f 2018-12-25 stsp
403 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
404 eaccb85f 2018-12-25 stsp if (err)
405 eaccb85f 2018-12-25 stsp goto done;
406 eaccb85f 2018-12-25 stsp
407 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
408 eaccb85f 2018-12-25 stsp base_commit_id_str);
409 f5baf295 2018-03-11 stsp if (err)
410 6d9d28c3 2018-03-11 stsp goto done;
411 6d9d28c3 2018-03-11 stsp
412 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
413 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
414 6d9d28c3 2018-03-11 stsp done:
415 eaccb85f 2018-12-25 stsp if (repo)
416 eaccb85f 2018-12-25 stsp got_repo_close(repo);
417 7ac97322 2018-03-11 stsp free(path_got);
418 056e7441 2018-03-11 stsp free(path_lock);
419 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
420 c442a90d 2019-03-10 stsp free(uuidstr);
421 bd165944 2019-03-10 stsp free(formatstr);
422 6d9d28c3 2018-03-11 stsp if (err) {
423 6d9d28c3 2018-03-11 stsp if (fd != -1)
424 6d9d28c3 2018-03-11 stsp close(fd);
425 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
426 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
427 6d9d28c3 2018-03-11 stsp *worktree = NULL;
428 6d9d28c3 2018-03-11 stsp } else
429 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
430 6d9d28c3 2018-03-11 stsp
431 6d9d28c3 2018-03-11 stsp return err;
432 86c3caaf 2018-03-09 stsp }
433 247140b2 2019-02-05 stsp
434 247140b2 2019-02-05 stsp const struct got_error *
435 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
436 247140b2 2019-02-05 stsp {
437 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
438 86c3caaf 2018-03-09 stsp
439 247140b2 2019-02-05 stsp do {
440 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
441 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
442 247140b2 2019-02-05 stsp return err;
443 247140b2 2019-02-05 stsp if (*worktree)
444 247140b2 2019-02-05 stsp return NULL;
445 247140b2 2019-02-05 stsp path = dirname(path);
446 d1542a27 2019-02-05 stsp if (path == NULL)
447 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
448 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 247140b2 2019-02-05 stsp
450 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
451 247140b2 2019-02-05 stsp }
452 247140b2 2019-02-05 stsp
453 3a6ce05a 2019-02-11 stsp const struct got_error *
454 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
455 86c3caaf 2018-03-09 stsp {
456 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
457 c88eb298 2018-03-11 stsp free(worktree->root_path);
458 cde76477 2018-03-11 stsp free(worktree->repo_path);
459 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
460 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
461 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
462 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
463 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
464 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
465 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
466 6d9d28c3 2018-03-11 stsp free(worktree);
467 3a6ce05a 2019-02-11 stsp return err;
468 86c3caaf 2018-03-09 stsp }
469 86c3caaf 2018-03-09 stsp
470 2fbdb5ae 2018-12-29 stsp const char *
471 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
472 c7f4312f 2019-02-05 stsp {
473 c7f4312f 2019-02-05 stsp return worktree->root_path;
474 c7f4312f 2019-02-05 stsp }
475 c7f4312f 2019-02-05 stsp
476 c7f4312f 2019-02-05 stsp const char *
477 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
478 86c3caaf 2018-03-09 stsp {
479 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
480 49520a32 2018-12-29 stsp }
481 49520a32 2018-12-29 stsp
482 49520a32 2018-12-29 stsp const char *
483 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
484 49520a32 2018-12-29 stsp {
485 f609be2e 2018-12-29 stsp return worktree->path_prefix;
486 e5dc7198 2018-12-29 stsp }
487 e5dc7198 2018-12-29 stsp
488 e5dc7198 2018-12-29 stsp const struct got_error *
489 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
490 e5dc7198 2018-12-29 stsp const char *path_prefix)
491 e5dc7198 2018-12-29 stsp {
492 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
493 e5dc7198 2018-12-29 stsp
494 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
495 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
496 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
497 e5dc7198 2018-12-29 stsp }
498 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
499 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
500 e5dc7198 2018-12-29 stsp free(absprefix);
501 e5dc7198 2018-12-29 stsp return NULL;
502 86c3caaf 2018-03-09 stsp }
503 86c3caaf 2018-03-09 stsp
504 bc70eb79 2019-05-09 stsp const char *
505 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 86c3caaf 2018-03-09 stsp {
507 36a38700 2019-05-10 stsp return worktree->head_ref_name;
508 024e9686 2019-05-14 stsp }
509 024e9686 2019-05-14 stsp
510 024e9686 2019-05-14 stsp const struct got_error *
511 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
512 024e9686 2019-05-14 stsp struct got_reference *head_ref)
513 024e9686 2019-05-14 stsp {
514 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
515 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
516 024e9686 2019-05-14 stsp
517 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
518 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
519 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
520 024e9686 2019-05-14 stsp path_got = NULL;
521 024e9686 2019-05-14 stsp goto done;
522 024e9686 2019-05-14 stsp }
523 024e9686 2019-05-14 stsp
524 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
525 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
526 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
527 024e9686 2019-05-14 stsp goto done;
528 024e9686 2019-05-14 stsp }
529 024e9686 2019-05-14 stsp
530 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
531 024e9686 2019-05-14 stsp if (err)
532 024e9686 2019-05-14 stsp goto done;
533 024e9686 2019-05-14 stsp
534 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
535 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
536 024e9686 2019-05-14 stsp done:
537 024e9686 2019-05-14 stsp free(path_got);
538 024e9686 2019-05-14 stsp if (err)
539 024e9686 2019-05-14 stsp free(head_ref_name);
540 024e9686 2019-05-14 stsp return err;
541 507dc3bb 2018-12-29 stsp }
542 507dc3bb 2018-12-29 stsp
543 b72f483a 2019-02-05 stsp struct got_object_id *
544 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 507dc3bb 2018-12-29 stsp {
546 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
547 86c3caaf 2018-03-09 stsp }
548 86c3caaf 2018-03-09 stsp
549 507dc3bb 2018-12-29 stsp const struct got_error *
550 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
551 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
552 507dc3bb 2018-12-29 stsp {
553 507dc3bb 2018-12-29 stsp const struct got_error *err;
554 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
555 507dc3bb 2018-12-29 stsp char *id_str = NULL;
556 507dc3bb 2018-12-29 stsp char *path_got = NULL;
557 507dc3bb 2018-12-29 stsp
558 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
559 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
560 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
561 507dc3bb 2018-12-29 stsp path_got = NULL;
562 507dc3bb 2018-12-29 stsp goto done;
563 507dc3bb 2018-12-29 stsp }
564 507dc3bb 2018-12-29 stsp
565 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
566 507dc3bb 2018-12-29 stsp if (err)
567 507dc3bb 2018-12-29 stsp return err;
568 507dc3bb 2018-12-29 stsp
569 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
570 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
571 507dc3bb 2018-12-29 stsp goto done;
572 507dc3bb 2018-12-29 stsp }
573 507dc3bb 2018-12-29 stsp
574 507dc3bb 2018-12-29 stsp /* Record our base commit. */
575 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
576 507dc3bb 2018-12-29 stsp if (err)
577 507dc3bb 2018-12-29 stsp goto done;
578 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
579 507dc3bb 2018-12-29 stsp if (err)
580 507dc3bb 2018-12-29 stsp goto done;
581 507dc3bb 2018-12-29 stsp
582 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
583 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
584 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
585 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
586 507dc3bb 2018-12-29 stsp goto done;
587 507dc3bb 2018-12-29 stsp }
588 507dc3bb 2018-12-29 stsp done:
589 507dc3bb 2018-12-29 stsp if (obj)
590 507dc3bb 2018-12-29 stsp got_object_close(obj);
591 507dc3bb 2018-12-29 stsp free(id_str);
592 507dc3bb 2018-12-29 stsp free(path_got);
593 507dc3bb 2018-12-29 stsp return err;
594 507dc3bb 2018-12-29 stsp }
595 507dc3bb 2018-12-29 stsp
596 9d31a1d8 2018-03-11 stsp static const struct got_error *
597 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
598 86c3caaf 2018-03-09 stsp {
599 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
600 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
601 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
602 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
603 86c3caaf 2018-03-09 stsp return NULL;
604 21908da4 2019-01-13 stsp }
605 21908da4 2019-01-13 stsp
606 21908da4 2019-01-13 stsp static const struct got_error *
607 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 4a1ddfc2 2019-01-12 stsp {
609 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
610 4a1ddfc2 2019-01-12 stsp char *abspath;
611 4a1ddfc2 2019-01-12 stsp
612 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
613 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
614 4a1ddfc2 2019-01-12 stsp
615 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
616 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
617 ddcd8544 2019-03-11 stsp struct stat sb;
618 ddcd8544 2019-03-11 stsp err = NULL;
619 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
620 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
621 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
622 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
623 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
624 ddcd8544 2019-03-11 stsp }
625 ddcd8544 2019-03-11 stsp }
626 4a1ddfc2 2019-01-12 stsp free(abspath);
627 68c76935 2019-02-19 stsp return err;
628 68c76935 2019-02-19 stsp }
629 68c76935 2019-02-19 stsp
630 68c76935 2019-02-19 stsp static const struct got_error *
631 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 68c76935 2019-02-19 stsp {
633 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
634 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
635 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
636 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
637 68c76935 2019-02-19 stsp
638 68c76935 2019-02-19 stsp *same = 1;
639 68c76935 2019-02-19 stsp
640 230a42bd 2019-05-11 jcs for (;;) {
641 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
642 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
643 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
644 68c76935 2019-02-19 stsp break;
645 68c76935 2019-02-19 stsp }
646 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
647 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
649 68c76935 2019-02-19 stsp break;
650 68c76935 2019-02-19 stsp }
651 68c76935 2019-02-19 stsp if (flen1 == 0) {
652 68c76935 2019-02-19 stsp if (flen2 != 0)
653 68c76935 2019-02-19 stsp *same = 0;
654 68c76935 2019-02-19 stsp break;
655 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
656 68c76935 2019-02-19 stsp if (flen1 != 0)
657 68c76935 2019-02-19 stsp *same = 0;
658 68c76935 2019-02-19 stsp break;
659 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
660 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
661 68c76935 2019-02-19 stsp *same = 0;
662 68c76935 2019-02-19 stsp break;
663 68c76935 2019-02-19 stsp }
664 68c76935 2019-02-19 stsp } else {
665 68c76935 2019-02-19 stsp *same = 0;
666 68c76935 2019-02-19 stsp break;
667 68c76935 2019-02-19 stsp }
668 68c76935 2019-02-19 stsp }
669 68c76935 2019-02-19 stsp
670 68c76935 2019-02-19 stsp return err;
671 68c76935 2019-02-19 stsp }
672 68c76935 2019-02-19 stsp
673 68c76935 2019-02-19 stsp static const struct got_error *
674 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 68c76935 2019-02-19 stsp {
676 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
677 68c76935 2019-02-19 stsp struct stat sb;
678 68c76935 2019-02-19 stsp size_t size1, size2;
679 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
680 68c76935 2019-02-19 stsp
681 68c76935 2019-02-19 stsp *same = 1;
682 68c76935 2019-02-19 stsp
683 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
684 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
685 68c76935 2019-02-19 stsp goto done;
686 68c76935 2019-02-19 stsp }
687 68c76935 2019-02-19 stsp size1 = sb.st_size;
688 68c76935 2019-02-19 stsp
689 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
690 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
691 68c76935 2019-02-19 stsp goto done;
692 68c76935 2019-02-19 stsp }
693 68c76935 2019-02-19 stsp size2 = sb.st_size;
694 68c76935 2019-02-19 stsp
695 68c76935 2019-02-19 stsp if (size1 != size2) {
696 68c76935 2019-02-19 stsp *same = 0;
697 68c76935 2019-02-19 stsp return NULL;
698 68c76935 2019-02-19 stsp }
699 68c76935 2019-02-19 stsp
700 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
701 68c76935 2019-02-19 stsp if (f1 == NULL)
702 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
703 68c76935 2019-02-19 stsp
704 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
705 68c76935 2019-02-19 stsp if (f2 == NULL) {
706 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
707 68c76935 2019-02-19 stsp goto done;
708 68c76935 2019-02-19 stsp }
709 68c76935 2019-02-19 stsp
710 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
711 68c76935 2019-02-19 stsp done:
712 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
713 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
714 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
715 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
716 68c76935 2019-02-19 stsp
717 6353ad76 2019-02-08 stsp return err;
718 6353ad76 2019-02-08 stsp }
719 6353ad76 2019-02-08 stsp
720 6353ad76 2019-02-08 stsp /*
721 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
722 46b6ee73 2019-06-04 stsp * blob_deriv acts as the first derived version, and the file on disk
723 234035bc 2019-06-01 stsp * acts as the second derived version.
724 6353ad76 2019-02-08 stsp */
725 6353ad76 2019-02-08 stsp static const struct got_error *
726 234035bc 2019-06-01 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
727 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
728 46b6ee73 2019-06-04 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
729 818c7501 2019-07-11 stsp struct got_object_id *deriv_base_commit_id,
730 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
731 234035bc 2019-06-01 stsp void *progress_arg)
732 6353ad76 2019-02-08 stsp {
733 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
734 6353ad76 2019-02-08 stsp int merged_fd = -1;
735 46b6ee73 2019-06-04 stsp FILE *f_deriv = NULL, *f_orig = NULL;
736 46b6ee73 2019-06-04 stsp char *blob_deriv_path = NULL, *blob_orig_path = NULL;
737 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
738 6353ad76 2019-02-08 stsp char *id_str = NULL;
739 818c7501 2019-07-11 stsp char *label_deriv = NULL;
740 234035bc 2019-06-01 stsp int overlapcnt = 0;
741 af54ae4a 2019-02-19 stsp char *parent;
742 6353ad76 2019-02-08 stsp
743 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
744 234035bc 2019-06-01 stsp
745 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
746 af54ae4a 2019-02-19 stsp if (parent == NULL)
747 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
748 af54ae4a 2019-02-19 stsp
749 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
750 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
751 af54ae4a 2019-02-19 stsp
752 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
753 6353ad76 2019-02-08 stsp if (err)
754 af54ae4a 2019-02-19 stsp goto done;
755 af54ae4a 2019-02-19 stsp
756 af54ae4a 2019-02-19 stsp free(base_path);
757 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
758 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
759 af54ae4a 2019-02-19 stsp base_path = NULL;
760 af54ae4a 2019-02-19 stsp goto done;
761 af54ae4a 2019-02-19 stsp }
762 af54ae4a 2019-02-19 stsp
763 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
764 6353ad76 2019-02-08 stsp if (err)
765 6353ad76 2019-02-08 stsp goto done;
766 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
767 6c4c42e0 2019-06-24 stsp blob_deriv);
768 6353ad76 2019-02-08 stsp if (err)
769 6353ad76 2019-02-08 stsp goto done;
770 6353ad76 2019-02-08 stsp
771 af54ae4a 2019-02-19 stsp free(base_path);
772 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
773 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
774 af54ae4a 2019-02-19 stsp base_path = NULL;
775 af54ae4a 2019-02-19 stsp goto done;
776 af54ae4a 2019-02-19 stsp }
777 af54ae4a 2019-02-19 stsp
778 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
779 6353ad76 2019-02-08 stsp if (err)
780 6353ad76 2019-02-08 stsp goto done;
781 46b6ee73 2019-06-04 stsp if (blob_orig) {
782 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
783 46b6ee73 2019-06-04 stsp blob_orig);
784 1430b4e0 2019-03-27 stsp if (err)
785 1430b4e0 2019-03-27 stsp goto done;
786 1430b4e0 2019-03-27 stsp } else {
787 1430b4e0 2019-03-27 stsp /*
788 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
789 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
790 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
791 1430b4e0 2019-03-27 stsp */
792 1430b4e0 2019-03-27 stsp }
793 6353ad76 2019-02-08 stsp
794 818c7501 2019-07-11 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
795 6353ad76 2019-02-08 stsp if (err)
796 6353ad76 2019-02-08 stsp goto done;
797 818c7501 2019-07-11 stsp if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
798 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
799 6353ad76 2019-02-08 stsp goto done;
800 6353ad76 2019-02-08 stsp }
801 6353ad76 2019-02-08 stsp
802 46b6ee73 2019-06-04 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
803 818c7501 2019-07-11 stsp blob_orig_path, ondisk_path, label_deriv, path);
804 6353ad76 2019-02-08 stsp if (err)
805 6353ad76 2019-02-08 stsp goto done;
806 6353ad76 2019-02-08 stsp
807 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
808 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 1ee397ad 2019-07-12 stsp if (err)
810 1ee397ad 2019-07-12 stsp goto done;
811 6353ad76 2019-02-08 stsp
812 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
813 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
814 816dc654 2019-02-16 stsp goto done;
815 68c76935 2019-02-19 stsp }
816 68c76935 2019-02-19 stsp
817 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
818 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
819 46b6ee73 2019-06-04 stsp err = check_files_equal(local_changes_subsumed, blob_deriv_path,
820 68c76935 2019-02-19 stsp merged_path);
821 68c76935 2019-02-19 stsp if (err)
822 68c76935 2019-02-19 stsp goto done;
823 816dc654 2019-02-16 stsp }
824 6353ad76 2019-02-08 stsp
825 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
826 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
827 70a0c8ec 2019-02-20 stsp goto done;
828 70a0c8ec 2019-02-20 stsp }
829 70a0c8ec 2019-02-20 stsp
830 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
831 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
832 230a42bd 2019-05-11 jcs ondisk_path);
833 2a57020b 2019-02-20 stsp unlink(merged_path);
834 6353ad76 2019-02-08 stsp goto done;
835 6353ad76 2019-02-08 stsp }
836 6353ad76 2019-02-08 stsp
837 6353ad76 2019-02-08 stsp done:
838 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
839 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
840 46b6ee73 2019-06-04 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
841 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
842 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
843 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
844 6353ad76 2019-02-08 stsp free(merged_path);
845 af54ae4a 2019-02-19 stsp free(base_path);
846 46b6ee73 2019-06-04 stsp if (blob_deriv_path) {
847 46b6ee73 2019-06-04 stsp unlink(blob_deriv_path);
848 46b6ee73 2019-06-04 stsp free(blob_deriv_path);
849 6353ad76 2019-02-08 stsp }
850 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
851 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
852 46b6ee73 2019-06-04 stsp free(blob_orig_path);
853 6353ad76 2019-02-08 stsp }
854 6353ad76 2019-02-08 stsp free(id_str);
855 818c7501 2019-07-11 stsp free(label_deriv);
856 4a1ddfc2 2019-01-12 stsp return err;
857 4a1ddfc2 2019-01-12 stsp }
858 4a1ddfc2 2019-01-12 stsp
859 4a1ddfc2 2019-01-12 stsp static const struct got_error *
860 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
861 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
862 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
863 13d9040b 2019-03-27 stsp int update_timestamps)
864 13d9040b 2019-03-27 stsp {
865 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
866 13d9040b 2019-03-27 stsp
867 13d9040b 2019-03-27 stsp if (ie == NULL)
868 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
869 13d9040b 2019-03-27 stsp if (ie)
870 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
871 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
872 13d9040b 2019-03-27 stsp update_timestamps);
873 13d9040b 2019-03-27 stsp else {
874 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
875 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
876 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
877 13d9040b 2019-03-27 stsp if (!err)
878 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
879 13d9040b 2019-03-27 stsp }
880 13d9040b 2019-03-27 stsp return err;
881 13d9040b 2019-03-27 stsp }
882 13d9040b 2019-03-27 stsp
883 13d9040b 2019-03-27 stsp static const struct got_error *
884 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
885 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
886 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
887 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
888 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
889 9d31a1d8 2018-03-11 stsp {
890 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
891 507dc3bb 2018-12-29 stsp int fd = -1;
892 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
893 507dc3bb 2018-12-29 stsp int update = 0;
894 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
895 9d31a1d8 2018-03-11 stsp
896 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
897 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
898 9d31a1d8 2018-03-11 stsp if (fd == -1) {
899 21908da4 2019-01-13 stsp if (errno == ENOENT) {
900 21908da4 2019-01-13 stsp char *parent = dirname(path);
901 21908da4 2019-01-13 stsp if (parent == NULL)
902 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
903 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
904 21908da4 2019-01-13 stsp if (err)
905 21908da4 2019-01-13 stsp return err;
906 21908da4 2019-01-13 stsp fd = open(ondisk_path,
907 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
908 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
909 21908da4 2019-01-13 stsp if (fd == -1)
910 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
911 230a42bd 2019-05-11 jcs ondisk_path);
912 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
913 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
914 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
915 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
916 507dc3bb 2018-12-29 stsp goto done;
917 d70b8e30 2018-12-27 stsp } else {
918 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
919 507dc3bb 2018-12-29 stsp ondisk_path);
920 507dc3bb 2018-12-29 stsp if (err)
921 507dc3bb 2018-12-29 stsp goto done;
922 507dc3bb 2018-12-29 stsp update = 1;
923 9d31a1d8 2018-03-11 stsp }
924 507dc3bb 2018-12-29 stsp } else
925 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
926 9d31a1d8 2018-03-11 stsp }
927 9d31a1d8 2018-03-11 stsp
928 a378724f 2019-02-10 stsp if (restoring_missing_file)
929 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
930 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
931 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
932 a378724f 2019-02-10 stsp else
933 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
934 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
935 1ee397ad 2019-07-12 stsp if (err)
936 1ee397ad 2019-07-12 stsp goto done;
937 d7b62c98 2018-12-27 stsp
938 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
939 9d31a1d8 2018-03-11 stsp do {
940 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
941 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
942 9d31a1d8 2018-03-11 stsp if (err)
943 9d31a1d8 2018-03-11 stsp break;
944 9d31a1d8 2018-03-11 stsp if (len > 0) {
945 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
946 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
947 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
948 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
949 b87c6f83 2018-12-24 stsp goto done;
950 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
951 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
952 b87c6f83 2018-12-24 stsp goto done;
953 9d31a1d8 2018-03-11 stsp }
954 61d6eaa3 2018-12-24 stsp hdrlen = 0;
955 9d31a1d8 2018-03-11 stsp }
956 9d31a1d8 2018-03-11 stsp } while (len != 0);
957 9d31a1d8 2018-03-11 stsp
958 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
959 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
960 816dc654 2019-02-16 stsp goto done;
961 816dc654 2019-02-16 stsp }
962 9d31a1d8 2018-03-11 stsp
963 507dc3bb 2018-12-29 stsp if (update) {
964 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
965 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
966 230a42bd 2019-05-11 jcs ondisk_path);
967 2a57020b 2019-02-20 stsp unlink(tmppath);
968 68ed9ba5 2019-02-10 stsp goto done;
969 68ed9ba5 2019-02-10 stsp }
970 68ed9ba5 2019-02-10 stsp }
971 ba8a0d4d 2019-02-10 stsp
972 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
973 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
974 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
975 507dc3bb 2018-12-29 stsp goto done;
976 507dc3bb 2018-12-29 stsp }
977 ba8a0d4d 2019-02-10 stsp } else {
978 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
979 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
980 68ed9ba5 2019-02-10 stsp goto done;
981 68ed9ba5 2019-02-10 stsp }
982 507dc3bb 2018-12-29 stsp }
983 507dc3bb 2018-12-29 stsp
984 9d31a1d8 2018-03-11 stsp done:
985 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
986 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
987 507dc3bb 2018-12-29 stsp free(tmppath);
988 6353ad76 2019-02-08 stsp return err;
989 6353ad76 2019-02-08 stsp }
990 6353ad76 2019-02-08 stsp
991 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
992 6353ad76 2019-02-08 stsp static const struct got_error *
993 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
994 7154f6ce 2019-03-27 stsp {
995 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
996 7154f6ce 2019-03-27 stsp const char *markers[3] = {
997 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
998 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
999 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1000 7154f6ce 2019-03-27 stsp };
1001 7154f6ce 2019-03-27 stsp int i = 0;
1002 7154f6ce 2019-03-27 stsp char *line;
1003 7154f6ce 2019-03-27 stsp size_t len;
1004 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1005 7154f6ce 2019-03-27 stsp
1006 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1007 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1008 7154f6ce 2019-03-27 stsp if (line == NULL) {
1009 7154f6ce 2019-03-27 stsp if (feof(f))
1010 7154f6ce 2019-03-27 stsp break;
1011 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1012 7154f6ce 2019-03-27 stsp break;
1013 7154f6ce 2019-03-27 stsp }
1014 7154f6ce 2019-03-27 stsp
1015 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1016 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1017 19332e6d 2019-05-13 stsp == 0)
1018 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1019 7154f6ce 2019-03-27 stsp else
1020 7154f6ce 2019-03-27 stsp i++;
1021 7154f6ce 2019-03-27 stsp }
1022 7154f6ce 2019-03-27 stsp }
1023 7154f6ce 2019-03-27 stsp
1024 7154f6ce 2019-03-27 stsp return err;
1025 e2b1e152 2019-07-13 stsp }
1026 e2b1e152 2019-07-13 stsp
1027 e2b1e152 2019-07-13 stsp static int
1028 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1029 e2b1e152 2019-07-13 stsp {
1030 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1031 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1032 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1033 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1034 e2b1e152 2019-07-13 stsp ie->size == (sb->st_size & 0xffffffff));
1035 c363b2c1 2019-08-03 stsp }
1036 c363b2c1 2019-08-03 stsp
1037 c363b2c1 2019-08-03 stsp static unsigned char
1038 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1039 c363b2c1 2019-08-03 stsp {
1040 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1041 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1042 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1043 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1044 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1045 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1046 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1047 c363b2c1 2019-08-03 stsp default:
1048 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1049 c363b2c1 2019-08-03 stsp }
1050 7154f6ce 2019-03-27 stsp }
1051 7154f6ce 2019-03-27 stsp
1052 7154f6ce 2019-03-27 stsp static const struct got_error *
1053 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1054 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1055 b8f41171 2019-02-10 stsp struct got_repository *repo)
1056 6353ad76 2019-02-08 stsp {
1057 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1058 6353ad76 2019-02-08 stsp struct got_object_id id;
1059 6353ad76 2019-02-08 stsp size_t hdrlen;
1060 6353ad76 2019-02-08 stsp FILE *f = NULL;
1061 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1062 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1063 6353ad76 2019-02-08 stsp size_t flen, blen;
1064 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1065 6353ad76 2019-02-08 stsp
1066 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1067 6353ad76 2019-02-08 stsp
1068 339c298e 2019-07-27 stsp if (lstat(abspath, sb) == -1) {
1069 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1070 abb4604f 2019-07-27 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1071 abb4604f 2019-07-27 stsp *status = GOT_STATUS_MISSING;
1072 abb4604f 2019-07-27 stsp else
1073 abb4604f 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1074 a378724f 2019-02-10 stsp return NULL;
1075 a378724f 2019-02-10 stsp }
1076 339c298e 2019-07-27 stsp return got_error_from_errno2("lstat", abspath);
1077 a378724f 2019-02-10 stsp }
1078 6353ad76 2019-02-08 stsp
1079 339c298e 2019-07-27 stsp if (!S_ISREG(sb->st_mode)) {
1080 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1081 339c298e 2019-07-27 stsp return NULL;
1082 3f148bc6 2019-07-27 stsp }
1083 efdd40df 2019-07-27 stsp
1084 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1085 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1086 339c298e 2019-07-27 stsp return NULL;
1087 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1088 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1089 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1090 339c298e 2019-07-27 stsp return NULL;
1091 d00136be 2019-03-26 stsp }
1092 b8f41171 2019-02-10 stsp
1093 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1094 339c298e 2019-07-27 stsp return NULL;
1095 6353ad76 2019-02-08 stsp
1096 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1097 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1098 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1099 c363b2c1 2019-08-03 stsp else
1100 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1101 c363b2c1 2019-08-03 stsp
1102 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1103 6353ad76 2019-02-08 stsp if (err)
1104 339c298e 2019-07-27 stsp return err;
1105 6353ad76 2019-02-08 stsp
1106 339c298e 2019-07-27 stsp f = fopen(abspath, "r");
1107 6353ad76 2019-02-08 stsp if (f == NULL) {
1108 339c298e 2019-07-27 stsp err = got_error_from_errno2("fopen", abspath);
1109 6353ad76 2019-02-08 stsp goto done;
1110 6353ad76 2019-02-08 stsp }
1111 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1112 656b1f76 2019-05-11 jcs for (;;) {
1113 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1114 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1115 6353ad76 2019-02-08 stsp if (err)
1116 7154f6ce 2019-03-27 stsp goto done;
1117 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1118 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1119 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1120 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1121 7154f6ce 2019-03-27 stsp goto done;
1122 80c5c120 2019-02-19 stsp }
1123 6353ad76 2019-02-08 stsp if (blen == 0) {
1124 6353ad76 2019-02-08 stsp if (flen != 0)
1125 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1126 6353ad76 2019-02-08 stsp break;
1127 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1128 6353ad76 2019-02-08 stsp if (blen != 0)
1129 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1130 6353ad76 2019-02-08 stsp break;
1131 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1132 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1133 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1134 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1135 6353ad76 2019-02-08 stsp break;
1136 6353ad76 2019-02-08 stsp }
1137 6353ad76 2019-02-08 stsp } else {
1138 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1139 6353ad76 2019-02-08 stsp break;
1140 6353ad76 2019-02-08 stsp }
1141 6353ad76 2019-02-08 stsp hdrlen = 0;
1142 6353ad76 2019-02-08 stsp }
1143 7154f6ce 2019-03-27 stsp
1144 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1145 7154f6ce 2019-03-27 stsp rewind(f);
1146 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1147 7154f6ce 2019-03-27 stsp }
1148 6353ad76 2019-02-08 stsp done:
1149 6353ad76 2019-02-08 stsp if (blob)
1150 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1151 6353ad76 2019-02-08 stsp if (f)
1152 6353ad76 2019-02-08 stsp fclose(f);
1153 9d31a1d8 2018-03-11 stsp return err;
1154 e2b1e152 2019-07-13 stsp }
1155 e2b1e152 2019-07-13 stsp
1156 e2b1e152 2019-07-13 stsp /*
1157 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1158 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1159 e2b1e152 2019-07-13 stsp */
1160 e2b1e152 2019-07-13 stsp static const struct got_error *
1161 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1162 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1163 e2b1e152 2019-07-13 stsp {
1164 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1165 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1166 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1167 e2b1e152 2019-07-13 stsp
1168 e2b1e152 2019-07-13 stsp return NULL;
1169 9d31a1d8 2018-03-11 stsp }
1170 9d31a1d8 2018-03-11 stsp
1171 9d31a1d8 2018-03-11 stsp static const struct got_error *
1172 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1173 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1174 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1175 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1176 0584f854 2019-04-06 stsp void *progress_arg)
1177 9d31a1d8 2018-03-11 stsp {
1178 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1179 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1180 6353ad76 2019-02-08 stsp char *ondisk_path;
1181 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1182 b8f41171 2019-02-10 stsp struct stat sb;
1183 9d31a1d8 2018-03-11 stsp
1184 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1185 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1186 6353ad76 2019-02-08 stsp
1187 abb4604f 2019-07-27 stsp if (ie) {
1188 abb4604f 2019-07-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1189 abb4604f 2019-07-27 stsp if (err)
1190 abb4604f 2019-07-27 stsp goto done;
1191 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1192 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1193 abb4604f 2019-07-27 stsp } else
1194 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1195 6353ad76 2019-02-08 stsp
1196 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1197 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1198 b8f41171 2019-02-10 stsp goto done;
1199 b8f41171 2019-02-10 stsp }
1200 b8f41171 2019-02-10 stsp
1201 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1202 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1203 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1204 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1205 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1206 e2b1e152 2019-07-13 stsp if (err)
1207 e2b1e152 2019-07-13 stsp goto done;
1208 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1209 b8f41171 2019-02-10 stsp path);
1210 6353ad76 2019-02-08 stsp goto done;
1211 a378724f 2019-02-10 stsp }
1212 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1213 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1214 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1215 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1216 b8f41171 2019-02-10 stsp goto done;
1217 e2b1e152 2019-07-13 stsp }
1218 9d31a1d8 2018-03-11 stsp }
1219 9d31a1d8 2018-03-11 stsp
1220 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1221 8da9e5f4 2019-01-12 stsp if (err)
1222 6353ad76 2019-02-08 stsp goto done;
1223 512f0d0e 2019-01-02 stsp
1224 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1225 234035bc 2019-06-01 stsp int update_timestamps;
1226 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1227 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1228 234035bc 2019-06-01 stsp struct got_object_id id2;
1229 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1230 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1231 234035bc 2019-06-01 stsp if (err)
1232 234035bc 2019-06-01 stsp goto done;
1233 234035bc 2019-06-01 stsp }
1234 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1235 818c7501 2019-07-11 stsp ondisk_path, path, sb.st_mode, blob,
1236 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1237 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1238 234035bc 2019-06-01 stsp if (blob2)
1239 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1240 234035bc 2019-06-01 stsp /*
1241 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1242 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1243 234035bc 2019-06-01 stsp * unmodified files again.
1244 234035bc 2019-06-01 stsp */
1245 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1246 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1247 234035bc 2019-06-01 stsp update_timestamps);
1248 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1249 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1250 1ee397ad 2019-07-12 stsp if (err)
1251 1ee397ad 2019-07-12 stsp goto done;
1252 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1253 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1254 13d9040b 2019-03-27 stsp if (err)
1255 13d9040b 2019-03-27 stsp goto done;
1256 13d9040b 2019-03-27 stsp } else {
1257 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1258 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1259 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1260 13d9040b 2019-03-27 stsp if (err)
1261 13d9040b 2019-03-27 stsp goto done;
1262 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1263 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1264 13d9040b 2019-03-27 stsp if (err)
1265 13d9040b 2019-03-27 stsp goto done;
1266 13d9040b 2019-03-27 stsp }
1267 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1268 6353ad76 2019-02-08 stsp done:
1269 6353ad76 2019-02-08 stsp free(ondisk_path);
1270 8da9e5f4 2019-01-12 stsp return err;
1271 90285c3b 2019-01-08 stsp }
1272 90285c3b 2019-01-08 stsp
1273 90285c3b 2019-01-08 stsp static const struct got_error *
1274 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1275 90285c3b 2019-01-08 stsp {
1276 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1277 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1278 90285c3b 2019-01-08 stsp
1279 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1280 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1281 90285c3b 2019-01-08 stsp
1282 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1283 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1284 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1285 c97665ae 2019-01-13 stsp } else {
1286 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1287 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1288 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1289 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1290 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1291 230a42bd 2019-05-11 jcs parent);
1292 90285c3b 2019-01-08 stsp break;
1293 90285c3b 2019-01-08 stsp }
1294 90285c3b 2019-01-08 stsp parent = dirname(parent);
1295 90285c3b 2019-01-08 stsp }
1296 90285c3b 2019-01-08 stsp }
1297 90285c3b 2019-01-08 stsp free(ondisk_path);
1298 90285c3b 2019-01-08 stsp return err;
1299 512f0d0e 2019-01-02 stsp }
1300 512f0d0e 2019-01-02 stsp
1301 708d8e67 2019-03-27 stsp static const struct got_error *
1302 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1303 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1304 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1305 708d8e67 2019-03-27 stsp {
1306 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1307 708d8e67 2019-03-27 stsp unsigned char status;
1308 708d8e67 2019-03-27 stsp struct stat sb;
1309 708d8e67 2019-03-27 stsp char *ondisk_path;
1310 708d8e67 2019-03-27 stsp
1311 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1312 708d8e67 2019-03-27 stsp == -1)
1313 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1314 708d8e67 2019-03-27 stsp
1315 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1316 708d8e67 2019-03-27 stsp if (err)
1317 708d8e67 2019-03-27 stsp return err;
1318 708d8e67 2019-03-27 stsp
1319 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1320 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1321 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1322 1ee397ad 2019-07-12 stsp if (err)
1323 1ee397ad 2019-07-12 stsp return err;
1324 fc6346c4 2019-03-27 stsp /*
1325 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1326 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1327 fc6346c4 2019-03-27 stsp */
1328 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1329 fc6346c4 2019-03-27 stsp 0);
1330 708d8e67 2019-03-27 stsp if (err)
1331 708d8e67 2019-03-27 stsp return err;
1332 fc6346c4 2019-03-27 stsp } else {
1333 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1334 1ee397ad 2019-07-12 stsp if (err)
1335 1ee397ad 2019-07-12 stsp return err;
1336 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1337 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1338 fc6346c4 2019-03-27 stsp if (err)
1339 fc6346c4 2019-03-27 stsp return err;
1340 fc6346c4 2019-03-27 stsp }
1341 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1342 708d8e67 2019-03-27 stsp }
1343 708d8e67 2019-03-27 stsp
1344 fc6346c4 2019-03-27 stsp return err;
1345 708d8e67 2019-03-27 stsp }
1346 708d8e67 2019-03-27 stsp
1347 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1348 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1349 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1350 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1351 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1352 8da9e5f4 2019-01-12 stsp void *progress_arg;
1353 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1354 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1355 8da9e5f4 2019-01-12 stsp };
1356 8da9e5f4 2019-01-12 stsp
1357 512f0d0e 2019-01-02 stsp static const struct got_error *
1358 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1359 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1360 512f0d0e 2019-01-02 stsp {
1361 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1362 0584f854 2019-04-06 stsp
1363 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1364 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1365 512f0d0e 2019-01-02 stsp
1366 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1367 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1368 8da9e5f4 2019-01-12 stsp }
1369 512f0d0e 2019-01-02 stsp
1370 8da9e5f4 2019-01-12 stsp static const struct got_error *
1371 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1372 8da9e5f4 2019-01-12 stsp {
1373 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1374 7a9df742 2019-01-08 stsp
1375 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1376 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1377 0584f854 2019-04-06 stsp
1378 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1379 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1380 9d31a1d8 2018-03-11 stsp }
1381 9d31a1d8 2018-03-11 stsp
1382 9d31a1d8 2018-03-11 stsp static const struct got_error *
1383 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1384 9d31a1d8 2018-03-11 stsp {
1385 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1386 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1387 8da9e5f4 2019-01-12 stsp char *path;
1388 9d31a1d8 2018-03-11 stsp
1389 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1390 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1391 0584f854 2019-04-06 stsp
1392 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1393 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1394 8da9e5f4 2019-01-12 stsp == -1)
1395 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1396 9d31a1d8 2018-03-11 stsp
1397 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1398 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1399 8da9e5f4 2019-01-12 stsp else
1400 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1401 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1402 9d31a1d8 2018-03-11 stsp
1403 8da9e5f4 2019-01-12 stsp free(path);
1404 0cd1c46a 2019-03-11 stsp return err;
1405 0cd1c46a 2019-03-11 stsp }
1406 0cd1c46a 2019-03-11 stsp
1407 818c7501 2019-07-11 stsp static const struct got_error *
1408 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1409 0cd1c46a 2019-03-11 stsp {
1410 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1411 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1412 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1413 0cd1c46a 2019-03-11 stsp
1414 517bab73 2019-03-11 stsp *refname = NULL;
1415 517bab73 2019-03-11 stsp
1416 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1417 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1418 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1419 0cd1c46a 2019-03-11 stsp
1420 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1421 0647c563 2019-03-11 stsp == -1) {
1422 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1423 0647c563 2019-03-11 stsp *refname = NULL;
1424 0cd1c46a 2019-03-11 stsp }
1425 517bab73 2019-03-11 stsp free(uuidstr);
1426 517bab73 2019-03-11 stsp return err;
1427 517bab73 2019-03-11 stsp }
1428 517bab73 2019-03-11 stsp
1429 818c7501 2019-07-11 stsp const struct got_error *
1430 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1431 818c7501 2019-07-11 stsp {
1432 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1433 818c7501 2019-07-11 stsp }
1434 818c7501 2019-07-11 stsp
1435 818c7501 2019-07-11 stsp static const struct got_error *
1436 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1437 818c7501 2019-07-11 stsp {
1438 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1439 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1440 818c7501 2019-07-11 stsp }
1441 818c7501 2019-07-11 stsp
1442 818c7501 2019-07-11 stsp static const struct got_error *
1443 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1444 818c7501 2019-07-11 stsp {
1445 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1446 818c7501 2019-07-11 stsp }
1447 818c7501 2019-07-11 stsp
1448 818c7501 2019-07-11 stsp static const struct got_error *
1449 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1450 818c7501 2019-07-11 stsp {
1451 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1452 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1453 818c7501 2019-07-11 stsp }
1454 818c7501 2019-07-11 stsp
1455 818c7501 2019-07-11 stsp static const struct got_error *
1456 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1457 818c7501 2019-07-11 stsp {
1458 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1459 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1460 0ebf8283 2019-07-24 stsp }
1461 0ebf8283 2019-07-24 stsp
1462 0ebf8283 2019-07-24 stsp static const struct got_error *
1463 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1464 0ebf8283 2019-07-24 stsp {
1465 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1466 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1467 0ebf8283 2019-07-24 stsp }
1468 0ebf8283 2019-07-24 stsp
1469 0ebf8283 2019-07-24 stsp static const struct got_error *
1470 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1471 0ebf8283 2019-07-24 stsp {
1472 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1473 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1474 0ebf8283 2019-07-24 stsp }
1475 0ebf8283 2019-07-24 stsp
1476 0ebf8283 2019-07-24 stsp static const struct got_error *
1477 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1478 0ebf8283 2019-07-24 stsp {
1479 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1480 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1481 818c7501 2019-07-11 stsp }
1482 818c7501 2019-07-11 stsp
1483 0ebf8283 2019-07-24 stsp static const struct got_error *
1484 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1485 0ebf8283 2019-07-24 stsp {
1486 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1487 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1488 0ebf8283 2019-07-24 stsp }
1489 818c7501 2019-07-11 stsp
1490 0ebf8283 2019-07-24 stsp const struct got_error *
1491 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
1492 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
1493 0ebf8283 2019-07-24 stsp {
1494 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
1495 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1496 0ebf8283 2019-07-24 stsp *path = NULL;
1497 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
1498 0ebf8283 2019-07-24 stsp }
1499 0ebf8283 2019-07-24 stsp return NULL;
1500 0ebf8283 2019-07-24 stsp }
1501 0ebf8283 2019-07-24 stsp
1502 517bab73 2019-03-11 stsp /*
1503 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1504 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1505 517bab73 2019-03-11 stsp */
1506 517bab73 2019-03-11 stsp static const struct got_error *
1507 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1508 517bab73 2019-03-11 stsp {
1509 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1510 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1511 517bab73 2019-03-11 stsp char *refname;
1512 517bab73 2019-03-11 stsp
1513 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1514 517bab73 2019-03-11 stsp if (err)
1515 517bab73 2019-03-11 stsp return err;
1516 0cd1c46a 2019-03-11 stsp
1517 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1518 0cd1c46a 2019-03-11 stsp if (err)
1519 0cd1c46a 2019-03-11 stsp goto done;
1520 0cd1c46a 2019-03-11 stsp
1521 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1522 0cd1c46a 2019-03-11 stsp done:
1523 0cd1c46a 2019-03-11 stsp free(refname);
1524 0cd1c46a 2019-03-11 stsp if (ref)
1525 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1526 3e3a69f1 2019-07-25 stsp return err;
1527 3e3a69f1 2019-07-25 stsp }
1528 3e3a69f1 2019-07-25 stsp
1529 3e3a69f1 2019-07-25 stsp static const struct got_error *
1530 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1531 3e3a69f1 2019-07-25 stsp {
1532 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
1533 3e3a69f1 2019-07-25 stsp
1534 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1535 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1536 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
1537 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
1538 3e3a69f1 2019-07-25 stsp }
1539 9d31a1d8 2018-03-11 stsp return err;
1540 9d31a1d8 2018-03-11 stsp }
1541 9d31a1d8 2018-03-11 stsp
1542 3e3a69f1 2019-07-25 stsp
1543 ebf99748 2019-05-09 stsp static const struct got_error *
1544 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1545 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1546 ebf99748 2019-05-09 stsp {
1547 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1548 ebf99748 2019-05-09 stsp FILE *index = NULL;
1549 0cd1c46a 2019-03-11 stsp
1550 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1551 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1552 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1553 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1554 ebf99748 2019-05-09 stsp
1555 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
1556 3e3a69f1 2019-07-25 stsp if (err)
1557 ebf99748 2019-05-09 stsp goto done;
1558 ebf99748 2019-05-09 stsp
1559 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1560 ebf99748 2019-05-09 stsp if (index == NULL) {
1561 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1562 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1563 ebf99748 2019-05-09 stsp } else {
1564 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1565 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1566 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1567 ebf99748 2019-05-09 stsp }
1568 ebf99748 2019-05-09 stsp done:
1569 ebf99748 2019-05-09 stsp if (err) {
1570 ebf99748 2019-05-09 stsp free(*fileindex_path);
1571 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1572 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1573 ebf99748 2019-05-09 stsp *fileindex = NULL;
1574 ebf99748 2019-05-09 stsp }
1575 ebf99748 2019-05-09 stsp return err;
1576 ebf99748 2019-05-09 stsp }
1577 c932eeeb 2019-05-22 stsp
1578 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1579 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1580 c932eeeb 2019-05-22 stsp const char *path;
1581 c932eeeb 2019-05-22 stsp size_t path_len;
1582 c932eeeb 2019-05-22 stsp const char *entry_name;
1583 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1584 a484d721 2019-06-10 stsp void *progress_arg;
1585 c932eeeb 2019-05-22 stsp };
1586 ebf99748 2019-05-09 stsp
1587 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1588 c932eeeb 2019-05-22 stsp static const struct got_error *
1589 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1590 c932eeeb 2019-05-22 stsp {
1591 1ee397ad 2019-07-12 stsp const struct got_error *err;
1592 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1593 c932eeeb 2019-05-22 stsp
1594 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1595 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1596 c932eeeb 2019-05-22 stsp return NULL;
1597 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1598 102ff934 2019-06-11 stsp return NULL;
1599 102ff934 2019-06-11 stsp
1600 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1601 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1602 c932eeeb 2019-05-22 stsp return NULL;
1603 c932eeeb 2019-05-22 stsp
1604 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
1605 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1606 edd02c5e 2019-07-11 stsp ie->path);
1607 1ee397ad 2019-07-12 stsp if (err)
1608 1ee397ad 2019-07-12 stsp return err;
1609 1ee397ad 2019-07-12 stsp }
1610 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1611 c932eeeb 2019-05-22 stsp return NULL;
1612 9c6338c4 2019-06-02 stsp }
1613 9c6338c4 2019-06-02 stsp
1614 9c6338c4 2019-06-02 stsp static const struct got_error *
1615 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1616 9c6338c4 2019-06-02 stsp {
1617 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1618 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1619 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1620 9c6338c4 2019-06-02 stsp
1621 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1622 9c6338c4 2019-06-02 stsp fileindex_path);
1623 9c6338c4 2019-06-02 stsp if (err)
1624 9c6338c4 2019-06-02 stsp goto done;
1625 9c6338c4 2019-06-02 stsp
1626 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1627 9c6338c4 2019-06-02 stsp if (err)
1628 9c6338c4 2019-06-02 stsp goto done;
1629 9c6338c4 2019-06-02 stsp
1630 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1631 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1632 9c6338c4 2019-06-02 stsp fileindex_path);
1633 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1634 9c6338c4 2019-06-02 stsp }
1635 9c6338c4 2019-06-02 stsp done:
1636 9c6338c4 2019-06-02 stsp if (new_index)
1637 9c6338c4 2019-06-02 stsp fclose(new_index);
1638 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1639 9c6338c4 2019-06-02 stsp return err;
1640 c932eeeb 2019-05-22 stsp }
1641 6ced4176 2019-07-12 stsp
1642 6ced4176 2019-07-12 stsp static const struct got_error *
1643 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1644 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
1645 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
1646 6ced4176 2019-07-12 stsp {
1647 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
1648 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
1649 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
1650 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1651 6ced4176 2019-07-12 stsp
1652 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1653 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1654 6ced4176 2019-07-12 stsp *tree_id = NULL;
1655 6ced4176 2019-07-12 stsp
1656 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
1657 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
1658 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
1659 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1660 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1661 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1662 6ced4176 2019-07-12 stsp goto done;
1663 6ced4176 2019-07-12 stsp }
1664 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1665 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
1666 6ced4176 2019-07-12 stsp if (err)
1667 6ced4176 2019-07-12 stsp goto done;
1668 6ced4176 2019-07-12 stsp return NULL;
1669 6ced4176 2019-07-12 stsp }
1670 6ced4176 2019-07-12 stsp
1671 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
1672 6ced4176 2019-07-12 stsp
1673 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1674 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
1675 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1676 6ced4176 2019-07-12 stsp goto done;
1677 6ced4176 2019-07-12 stsp }
1678 6ced4176 2019-07-12 stsp
1679 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1680 6ced4176 2019-07-12 stsp in_repo_path);
1681 6ced4176 2019-07-12 stsp if (err)
1682 6ced4176 2019-07-12 stsp goto done;
1683 6ced4176 2019-07-12 stsp
1684 6ced4176 2019-07-12 stsp free(in_repo_path);
1685 6ced4176 2019-07-12 stsp in_repo_path = NULL;
1686 6ced4176 2019-07-12 stsp
1687 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
1688 6ced4176 2019-07-12 stsp if (err)
1689 6ced4176 2019-07-12 stsp goto done;
1690 c932eeeb 2019-05-22 stsp
1691 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1692 6ced4176 2019-07-12 stsp /* Check out a single file. */
1693 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
1694 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
1695 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
1696 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
1697 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1698 6ced4176 2019-07-12 stsp goto done;
1699 6ced4176 2019-07-12 stsp }
1700 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1701 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1702 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1703 6ced4176 2019-07-12 stsp goto done;
1704 6ced4176 2019-07-12 stsp }
1705 6ced4176 2019-07-12 stsp } else {
1706 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
1707 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
1708 6ced4176 2019-07-12 stsp if (err)
1709 6ced4176 2019-07-12 stsp return err;
1710 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
1711 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
1712 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
1713 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1714 6ced4176 2019-07-12 stsp goto done;
1715 6ced4176 2019-07-12 stsp }
1716 6ced4176 2019-07-12 stsp }
1717 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1718 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
1719 6ced4176 2019-07-12 stsp } else {
1720 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
1721 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
1722 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
1723 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
1724 6ced4176 2019-07-12 stsp goto done;
1725 6ced4176 2019-07-12 stsp }
1726 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
1727 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1728 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1729 6ced4176 2019-07-12 stsp goto done;
1730 6ced4176 2019-07-12 stsp }
1731 6ced4176 2019-07-12 stsp }
1732 6ced4176 2019-07-12 stsp done:
1733 6ced4176 2019-07-12 stsp free(id);
1734 6ced4176 2019-07-12 stsp free(in_repo_path);
1735 6ced4176 2019-07-12 stsp if (err) {
1736 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1737 6ced4176 2019-07-12 stsp free(*tree_relpath);
1738 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1739 6ced4176 2019-07-12 stsp free(*tree_id);
1740 6ced4176 2019-07-12 stsp *tree_id = NULL;
1741 6ced4176 2019-07-12 stsp }
1742 07ed472d 2019-07-12 stsp return err;
1743 07ed472d 2019-07-12 stsp }
1744 07ed472d 2019-07-12 stsp
1745 07ed472d 2019-07-12 stsp static const struct got_error *
1746 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1747 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1748 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1749 07ed472d 2019-07-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1750 07ed472d 2019-07-12 stsp {
1751 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
1752 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
1753 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
1754 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
1755 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
1756 07ed472d 2019-07-12 stsp
1757 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
1758 07ed472d 2019-07-12 stsp if (err)
1759 07ed472d 2019-07-12 stsp goto done;
1760 07ed472d 2019-07-12 stsp
1761 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
1762 07ed472d 2019-07-12 stsp worktree->base_commit_id);
1763 07ed472d 2019-07-12 stsp if (err)
1764 07ed472d 2019-07-12 stsp goto done;
1765 07ed472d 2019-07-12 stsp
1766 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1767 07ed472d 2019-07-12 stsp if (err)
1768 07ed472d 2019-07-12 stsp goto done;
1769 07ed472d 2019-07-12 stsp
1770 07ed472d 2019-07-12 stsp if (entry_name &&
1771 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1772 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1773 07ed472d 2019-07-12 stsp goto done;
1774 07ed472d 2019-07-12 stsp }
1775 07ed472d 2019-07-12 stsp
1776 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
1777 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
1778 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
1779 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
1780 07ed472d 2019-07-12 stsp arg.worktree = worktree;
1781 07ed472d 2019-07-12 stsp arg.repo = repo;
1782 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
1783 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
1784 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
1785 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
1786 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1787 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
1788 07ed472d 2019-07-12 stsp done:
1789 07ed472d 2019-07-12 stsp if (tree)
1790 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
1791 07ed472d 2019-07-12 stsp if (commit)
1792 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
1793 6ced4176 2019-07-12 stsp return err;
1794 6ced4176 2019-07-12 stsp }
1795 6ced4176 2019-07-12 stsp
1796 9d31a1d8 2018-03-11 stsp const struct got_error *
1797 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1798 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
1799 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1800 f2ea84fa 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1801 9d31a1d8 2018-03-11 stsp {
1802 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1803 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1804 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1805 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1806 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1807 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1808 f2ea84fa 2019-07-27 stsp struct tree_path_data {
1809 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
1810 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
1811 f2ea84fa 2019-07-27 stsp int entry_type;
1812 f2ea84fa 2019-07-27 stsp char *relpath;
1813 f2ea84fa 2019-07-27 stsp char *entry_name;
1814 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
1815 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1816 9d31a1d8 2018-03-11 stsp
1817 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
1818 f2ea84fa 2019-07-27 stsp
1819 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1820 9d31a1d8 2018-03-11 stsp if (err)
1821 9d31a1d8 2018-03-11 stsp return err;
1822 93a30277 2018-12-24 stsp
1823 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
1824 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1825 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
1826 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
1827 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
1828 f2ea84fa 2019-07-27 stsp goto done;
1829 f2ea84fa 2019-07-27 stsp }
1830 6ced4176 2019-07-12 stsp
1831 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
1832 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1833 f2ea84fa 2019-07-27 stsp if (err) {
1834 f2ea84fa 2019-07-27 stsp free(tpd);
1835 6ced4176 2019-07-12 stsp goto done;
1836 6ced4176 2019-07-12 stsp }
1837 f2ea84fa 2019-07-27 stsp
1838 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1839 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
1840 f2ea84fa 2019-07-27 stsp if (err) {
1841 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1842 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1843 f2ea84fa 2019-07-27 stsp free(tpd);
1844 f2ea84fa 2019-07-27 stsp goto done;
1845 f2ea84fa 2019-07-27 stsp }
1846 f2ea84fa 2019-07-27 stsp } else
1847 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
1848 f2ea84fa 2019-07-27 stsp
1849 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1850 6ced4176 2019-07-12 stsp }
1851 6ced4176 2019-07-12 stsp
1852 51514078 2018-12-25 stsp /*
1853 51514078 2018-12-25 stsp * Read the file index.
1854 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1855 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1856 51514078 2018-12-25 stsp */
1857 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1858 8da9e5f4 2019-01-12 stsp if (err)
1859 c4cdcb68 2019-04-03 stsp goto done;
1860 c4cdcb68 2019-04-03 stsp
1861 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1862 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1863 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
1864 f2ea84fa 2019-07-27 stsp
1865 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
1866 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
1867 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1868 f2ea84fa 2019-07-27 stsp if (err)
1869 f2ea84fa 2019-07-27 stsp break;
1870 f2ea84fa 2019-07-27 stsp
1871 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1872 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
1873 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
1874 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
1875 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
1876 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
1877 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1878 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
1879 f2ea84fa 2019-07-27 stsp if (err)
1880 f2ea84fa 2019-07-27 stsp break;
1881 f2ea84fa 2019-07-27 stsp
1882 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
1883 711d9cd9 2019-07-12 stsp }
1884 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1885 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1886 af12c6b9 2019-06-04 stsp err = sync_err;
1887 9d31a1d8 2018-03-11 stsp done:
1888 9c6338c4 2019-06-02 stsp free(fileindex_path);
1889 edfa7d7f 2018-09-11 stsp if (tree)
1890 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1891 9d31a1d8 2018-03-11 stsp if (commit)
1892 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1893 5ade8233 2019-07-12 stsp if (fileindex)
1894 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
1895 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
1896 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1897 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1898 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1899 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1900 f2ea84fa 2019-07-27 stsp free(tpd);
1901 f2ea84fa 2019-07-27 stsp }
1902 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1903 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1904 9d31a1d8 2018-03-11 stsp err = unlockerr;
1905 f8d1f275 2019-02-04 stsp return err;
1906 f8d1f275 2019-02-04 stsp }
1907 f8d1f275 2019-02-04 stsp
1908 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1909 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1910 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1911 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1912 234035bc 2019-06-01 stsp void *progress_arg;
1913 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1914 234035bc 2019-06-01 stsp void *cancel_arg;
1915 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
1916 234035bc 2019-06-01 stsp };
1917 234035bc 2019-06-01 stsp
1918 234035bc 2019-06-01 stsp static const struct got_error *
1919 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1920 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1921 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1922 234035bc 2019-06-01 stsp struct got_repository *repo)
1923 234035bc 2019-06-01 stsp {
1924 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1925 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1926 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1927 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1928 234035bc 2019-06-01 stsp struct stat sb;
1929 234035bc 2019-06-01 stsp unsigned char status;
1930 234035bc 2019-06-01 stsp int local_changes_subsumed;
1931 234035bc 2019-06-01 stsp
1932 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1933 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
1934 d6c87207 2019-08-02 stsp strlen(path2));
1935 1ee397ad 2019-07-12 stsp if (ie == NULL)
1936 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1937 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1938 234035bc 2019-06-01 stsp
1939 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1940 234035bc 2019-06-01 stsp path2) == -1)
1941 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1942 234035bc 2019-06-01 stsp
1943 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1944 234035bc 2019-06-01 stsp if (err)
1945 234035bc 2019-06-01 stsp goto done;
1946 234035bc 2019-06-01 stsp
1947 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1948 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1949 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
1950 234035bc 2019-06-01 stsp goto done;
1951 234035bc 2019-06-01 stsp }
1952 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1953 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1954 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1955 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1956 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
1957 234035bc 2019-06-01 stsp goto done;
1958 234035bc 2019-06-01 stsp }
1959 234035bc 2019-06-01 stsp
1960 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1961 818c7501 2019-07-11 stsp ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1962 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1963 234035bc 2019-06-01 stsp } else if (blob1) {
1964 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
1965 d6c87207 2019-08-02 stsp strlen(path1));
1966 1ee397ad 2019-07-12 stsp if (ie == NULL)
1967 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1968 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1969 2b92fad7 2019-06-02 stsp
1970 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1971 2b92fad7 2019-06-02 stsp path1) == -1)
1972 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
1973 2b92fad7 2019-06-02 stsp
1974 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1975 2b92fad7 2019-06-02 stsp if (err)
1976 2b92fad7 2019-06-02 stsp goto done;
1977 2b92fad7 2019-06-02 stsp
1978 2b92fad7 2019-06-02 stsp switch (status) {
1979 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
1980 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1981 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
1982 1ee397ad 2019-07-12 stsp if (err)
1983 1ee397ad 2019-07-12 stsp goto done;
1984 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
1985 2b92fad7 2019-06-02 stsp if (err)
1986 2b92fad7 2019-06-02 stsp goto done;
1987 2b92fad7 2019-06-02 stsp if (ie)
1988 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1989 2b92fad7 2019-06-02 stsp break;
1990 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
1991 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
1992 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1993 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
1994 1ee397ad 2019-07-12 stsp if (err)
1995 1ee397ad 2019-07-12 stsp goto done;
1996 2b92fad7 2019-06-02 stsp if (ie)
1997 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1998 2b92fad7 2019-06-02 stsp break;
1999 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2000 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2001 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2002 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2003 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2004 1ee397ad 2019-07-12 stsp if (err)
2005 1ee397ad 2019-07-12 stsp goto done;
2006 2b92fad7 2019-06-02 stsp break;
2007 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2008 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2009 1ee397ad 2019-07-12 stsp if (err)
2010 1ee397ad 2019-07-12 stsp goto done;
2011 2b92fad7 2019-06-02 stsp break;
2012 2b92fad7 2019-06-02 stsp default:
2013 2b92fad7 2019-06-02 stsp break;
2014 2b92fad7 2019-06-02 stsp }
2015 234035bc 2019-06-01 stsp } else if (blob2) {
2016 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2017 234035bc 2019-06-01 stsp path2) == -1)
2018 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2019 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2020 d6c87207 2019-08-02 stsp strlen(path2));
2021 234035bc 2019-06-01 stsp if (ie) {
2022 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2023 234035bc 2019-06-01 stsp repo);
2024 234035bc 2019-06-01 stsp if (err)
2025 234035bc 2019-06-01 stsp goto done;
2026 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2027 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2028 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2029 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2030 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2031 1ee397ad 2019-07-12 stsp status, path2);
2032 234035bc 2019-06-01 stsp goto done;
2033 234035bc 2019-06-01 stsp }
2034 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2035 818c7501 2019-07-11 stsp NULL, ondisk_path, path2, sb.st_mode, blob2,
2036 818c7501 2019-07-11 stsp a->commit_id2, repo,
2037 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2038 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2039 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
2040 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
2041 234035bc 2019-06-01 stsp blob2, 0);
2042 234035bc 2019-06-01 stsp if (err)
2043 234035bc 2019-06-01 stsp goto done;
2044 234035bc 2019-06-01 stsp }
2045 234035bc 2019-06-01 stsp } else {
2046 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2047 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2048 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
2049 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
2050 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
2051 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2052 234035bc 2019-06-01 stsp if (err)
2053 234035bc 2019-06-01 stsp goto done;
2054 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
2055 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
2056 234035bc 2019-06-01 stsp if (err)
2057 234035bc 2019-06-01 stsp goto done;
2058 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2059 2b92fad7 2019-06-02 stsp if (err) {
2060 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2061 2b92fad7 2019-06-02 stsp goto done;
2062 2b92fad7 2019-06-02 stsp }
2063 234035bc 2019-06-01 stsp }
2064 234035bc 2019-06-01 stsp }
2065 234035bc 2019-06-01 stsp done:
2066 234035bc 2019-06-01 stsp free(ondisk_path);
2067 234035bc 2019-06-01 stsp return err;
2068 234035bc 2019-06-01 stsp }
2069 234035bc 2019-06-01 stsp
2070 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2071 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2072 234035bc 2019-06-01 stsp struct got_repository *repo;
2073 234035bc 2019-06-01 stsp };
2074 234035bc 2019-06-01 stsp
2075 234035bc 2019-06-01 stsp static const struct got_error *
2076 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2077 234035bc 2019-06-01 stsp {
2078 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2079 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2080 234035bc 2019-06-01 stsp unsigned char status;
2081 234035bc 2019-06-01 stsp struct stat sb;
2082 234035bc 2019-06-01 stsp char *ondisk_path;
2083 234035bc 2019-06-01 stsp
2084 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2085 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2086 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2087 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2088 234035bc 2019-06-01 stsp
2089 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2090 234035bc 2019-06-01 stsp == -1)
2091 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2092 234035bc 2019-06-01 stsp
2093 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2094 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2095 234035bc 2019-06-01 stsp if (err)
2096 234035bc 2019-06-01 stsp return err;
2097 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2098 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2099 234035bc 2019-06-01 stsp
2100 234035bc 2019-06-01 stsp return NULL;
2101 234035bc 2019-06-01 stsp }
2102 234035bc 2019-06-01 stsp
2103 818c7501 2019-07-11 stsp static const struct got_error *
2104 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2105 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2106 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2107 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2108 818c7501 2019-07-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2109 234035bc 2019-06-01 stsp {
2110 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2111 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2112 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2113 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2114 234035bc 2019-06-01 stsp
2115 03415a1a 2019-06-02 stsp if (commit_id1) {
2116 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2117 03415a1a 2019-06-02 stsp worktree->path_prefix);
2118 03415a1a 2019-06-02 stsp if (err)
2119 03415a1a 2019-06-02 stsp goto done;
2120 03415a1a 2019-06-02 stsp
2121 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2122 03415a1a 2019-06-02 stsp if (err)
2123 03415a1a 2019-06-02 stsp goto done;
2124 03415a1a 2019-06-02 stsp }
2125 234035bc 2019-06-01 stsp
2126 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2127 234035bc 2019-06-01 stsp worktree->path_prefix);
2128 234035bc 2019-06-01 stsp if (err)
2129 234035bc 2019-06-01 stsp goto done;
2130 234035bc 2019-06-01 stsp
2131 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2132 234035bc 2019-06-01 stsp if (err)
2133 234035bc 2019-06-01 stsp goto done;
2134 234035bc 2019-06-01 stsp
2135 234035bc 2019-06-01 stsp arg.worktree = worktree;
2136 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2137 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2138 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2139 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2140 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2141 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2142 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2143 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2144 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2145 af12c6b9 2019-06-04 stsp err = sync_err;
2146 234035bc 2019-06-01 stsp done:
2147 234035bc 2019-06-01 stsp if (tree1)
2148 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2149 234035bc 2019-06-01 stsp if (tree2)
2150 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2151 818c7501 2019-07-11 stsp return err;
2152 818c7501 2019-07-11 stsp }
2153 234035bc 2019-06-01 stsp
2154 818c7501 2019-07-11 stsp const struct got_error *
2155 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2156 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2157 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2158 818c7501 2019-07-11 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2159 818c7501 2019-07-11 stsp {
2160 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2161 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2162 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2163 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2164 818c7501 2019-07-11 stsp
2165 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2166 818c7501 2019-07-11 stsp if (err)
2167 818c7501 2019-07-11 stsp return err;
2168 818c7501 2019-07-11 stsp
2169 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2170 818c7501 2019-07-11 stsp if (err)
2171 818c7501 2019-07-11 stsp goto done;
2172 818c7501 2019-07-11 stsp
2173 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2174 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2175 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2176 818c7501 2019-07-11 stsp &mok_arg);
2177 818c7501 2019-07-11 stsp if (err)
2178 818c7501 2019-07-11 stsp goto done;
2179 818c7501 2019-07-11 stsp
2180 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2181 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2182 818c7501 2019-07-11 stsp done:
2183 818c7501 2019-07-11 stsp if (fileindex)
2184 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2185 818c7501 2019-07-11 stsp free(fileindex_path);
2186 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2187 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2188 234035bc 2019-06-01 stsp err = unlockerr;
2189 234035bc 2019-06-01 stsp return err;
2190 234035bc 2019-06-01 stsp }
2191 234035bc 2019-06-01 stsp
2192 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2193 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2194 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2195 927df6b7 2019-02-10 stsp const char *status_path;
2196 927df6b7 2019-02-10 stsp size_t status_path_len;
2197 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2198 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2199 f8d1f275 2019-02-04 stsp void *status_arg;
2200 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
2201 f8d1f275 2019-02-04 stsp void *cancel_arg;
2202 f8d1f275 2019-02-04 stsp };
2203 88d0e355 2019-08-03 stsp
2204 f8d1f275 2019-02-04 stsp static const struct got_error *
2205 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2206 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2207 927df6b7 2019-02-10 stsp struct got_repository *repo)
2208 927df6b7 2019-02-10 stsp {
2209 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2210 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2211 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2212 927df6b7 2019-02-10 stsp struct stat sb;
2213 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2214 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2215 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2216 927df6b7 2019-02-10 stsp
2217 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
2218 98eaaa12 2019-08-03 stsp if (err)
2219 98eaaa12 2019-08-03 stsp return err;
2220 98eaaa12 2019-08-03 stsp
2221 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2222 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_NO_CHANGE)
2223 98eaaa12 2019-08-03 stsp return NULL;
2224 98eaaa12 2019-08-03 stsp
2225 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2226 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2227 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2228 98eaaa12 2019-08-03 stsp }
2229 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2230 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2231 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2232 927df6b7 2019-02-10 stsp }
2233 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2234 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2235 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2236 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2237 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2238 98eaaa12 2019-08-03 stsp }
2239 98eaaa12 2019-08-03 stsp
2240 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2241 98eaaa12 2019-08-03 stsp ie->path, blob_idp, staged_blob_idp, commit_idp);
2242 927df6b7 2019-02-10 stsp }
2243 927df6b7 2019-02-10 stsp
2244 927df6b7 2019-02-10 stsp static const struct got_error *
2245 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2246 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
2247 f8d1f275 2019-02-04 stsp {
2248 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2249 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2250 f8d1f275 2019-02-04 stsp char *abspath;
2251 f8d1f275 2019-02-04 stsp
2252 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2253 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2254 0584f854 2019-04-06 stsp
2255 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2256 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2257 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2258 927df6b7 2019-02-10 stsp return NULL;
2259 927df6b7 2019-02-10 stsp
2260 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2261 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2262 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2263 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2264 f8d1f275 2019-02-04 stsp } else {
2265 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2266 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2268 f8d1f275 2019-02-04 stsp }
2269 f8d1f275 2019-02-04 stsp
2270 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2271 927df6b7 2019-02-10 stsp a->repo);
2272 f8d1f275 2019-02-04 stsp free(abspath);
2273 9d31a1d8 2018-03-11 stsp return err;
2274 9d31a1d8 2018-03-11 stsp }
2275 f8d1f275 2019-02-04 stsp
2276 f8d1f275 2019-02-04 stsp static const struct got_error *
2277 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2278 f8d1f275 2019-02-04 stsp {
2279 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2280 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2281 2ec1f75b 2019-03-26 stsp unsigned char status;
2282 927df6b7 2019-02-10 stsp
2283 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2284 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2285 0584f854 2019-04-06 stsp
2286 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2287 927df6b7 2019-02-10 stsp return NULL;
2288 927df6b7 2019-02-10 stsp
2289 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2290 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2291 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2292 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2293 2ec1f75b 2019-03-26 stsp else
2294 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2295 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2296 537ac44b 2019-08-03 stsp ie->path, &blob_id, NULL, &commit_id);
2297 f8d1f275 2019-02-04 stsp }
2298 f8d1f275 2019-02-04 stsp
2299 f8d1f275 2019-02-04 stsp static const struct got_error *
2300 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2301 f8d1f275 2019-02-04 stsp {
2302 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2303 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2304 f8d1f275 2019-02-04 stsp char *path = NULL;
2305 f8d1f275 2019-02-04 stsp
2306 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2307 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2308 0584f854 2019-04-06 stsp
2309 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2310 2c201a36 2019-02-10 stsp return NULL;
2311 2c201a36 2019-02-10 stsp
2312 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2313 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2314 927df6b7 2019-02-10 stsp return NULL;
2315 927df6b7 2019-02-10 stsp
2316 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2317 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2318 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2319 f8d1f275 2019-02-04 stsp } else {
2320 f8d1f275 2019-02-04 stsp path = de->d_name;
2321 f8d1f275 2019-02-04 stsp }
2322 f8d1f275 2019-02-04 stsp
2323 c577a9ce 2019-07-27 stsp if (got_path_is_child(path, a->status_path, a->status_path_len))
2324 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2325 537ac44b 2019-08-03 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2326 f8d1f275 2019-02-04 stsp if (parent_path[0])
2327 f8d1f275 2019-02-04 stsp free(path);
2328 b72f483a 2019-02-05 stsp return err;
2329 f8d1f275 2019-02-04 stsp }
2330 f8d1f275 2019-02-04 stsp
2331 347d1d3e 2019-07-12 stsp static const struct got_error *
2332 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
2333 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2334 abb4604f 2019-07-27 stsp void *status_arg, struct got_repository *repo)
2335 abb4604f 2019-07-27 stsp {
2336 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
2337 abb4604f 2019-07-27 stsp struct stat sb;
2338 abb4604f 2019-07-27 stsp
2339 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2340 abb4604f 2019-07-27 stsp if (ie)
2341 abb4604f 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb,
2342 abb4604f 2019-07-27 stsp status_arg, repo);
2343 abb4604f 2019-07-27 stsp
2344 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
2345 abb4604f 2019-07-27 stsp if (errno != ENOENT)
2346 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
2347 abb4604f 2019-07-27 stsp return NULL;
2348 abb4604f 2019-07-27 stsp }
2349 abb4604f 2019-07-27 stsp
2350 abb4604f 2019-07-27 stsp if (S_ISREG(sb.st_mode))
2351 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2352 537ac44b 2019-08-03 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2353 abb4604f 2019-07-27 stsp
2354 abb4604f 2019-07-27 stsp return NULL;
2355 abb4604f 2019-07-27 stsp }
2356 abb4604f 2019-07-27 stsp
2357 abb4604f 2019-07-27 stsp static const struct got_error *
2358 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
2359 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
2360 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
2361 347d1d3e 2019-07-12 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2362 f8d1f275 2019-02-04 stsp {
2363 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2364 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2365 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2366 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2367 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2368 f8d1f275 2019-02-04 stsp
2369 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2370 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
2371 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
2372 8dc303cc 2019-07-27 stsp
2373 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2374 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2375 abb4604f 2019-07-27 stsp if (errno != ENOTDIR && errno != ENOENT)
2376 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2377 abb4604f 2019-07-27 stsp else
2378 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
2379 abb4604f 2019-07-27 stsp fileindex, status_cb, status_arg, repo);
2380 abb4604f 2019-07-27 stsp } else {
2381 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
2382 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
2383 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
2384 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
2385 abb4604f 2019-07-27 stsp arg.worktree = worktree;
2386 abb4604f 2019-07-27 stsp arg.status_path = path;
2387 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
2388 abb4604f 2019-07-27 stsp arg.repo = repo;
2389 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
2390 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
2391 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
2392 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
2393 abb4604f 2019-07-27 stsp err = got_fileindex_diff_dir(fileindex, workdir,
2394 abb4604f 2019-07-27 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
2395 927df6b7 2019-02-10 stsp }
2396 8dc303cc 2019-07-27 stsp
2397 f8d1f275 2019-02-04 stsp if (workdir)
2398 f8d1f275 2019-02-04 stsp closedir(workdir);
2399 927df6b7 2019-02-10 stsp free(ondisk_path);
2400 347d1d3e 2019-07-12 stsp return err;
2401 347d1d3e 2019-07-12 stsp }
2402 347d1d3e 2019-07-12 stsp
2403 347d1d3e 2019-07-12 stsp const struct got_error *
2404 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
2405 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2406 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
2407 72ea6654 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2408 347d1d3e 2019-07-12 stsp {
2409 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
2410 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
2411 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
2412 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
2413 347d1d3e 2019-07-12 stsp
2414 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2415 347d1d3e 2019-07-12 stsp if (err)
2416 347d1d3e 2019-07-12 stsp return err;
2417 347d1d3e 2019-07-12 stsp
2418 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2419 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
2420 72ea6654 2019-07-27 stsp status_cb, status_arg, cancel_cb, cancel_arg);
2421 72ea6654 2019-07-27 stsp if (err)
2422 72ea6654 2019-07-27 stsp break;
2423 72ea6654 2019-07-27 stsp }
2424 f8d1f275 2019-02-04 stsp free(fileindex_path);
2425 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2426 f8d1f275 2019-02-04 stsp return err;
2427 f8d1f275 2019-02-04 stsp }
2428 6c7ab921 2019-03-18 stsp
2429 6c7ab921 2019-03-18 stsp const struct got_error *
2430 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2431 6c7ab921 2019-03-18 stsp const char *arg)
2432 6c7ab921 2019-03-18 stsp {
2433 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2434 d0710d08 2019-07-22 stsp char *resolved, *cwd = NULL, *path = NULL;
2435 6c7ab921 2019-03-18 stsp size_t len;
2436 6c7ab921 2019-03-18 stsp
2437 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2438 6c7ab921 2019-03-18 stsp
2439 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2440 d0710d08 2019-07-22 stsp if (resolved == NULL) {
2441 d0710d08 2019-07-22 stsp if (errno != ENOENT)
2442 d0710d08 2019-07-22 stsp return got_error_from_errno2("realpath", arg);
2443 d0710d08 2019-07-22 stsp cwd = getcwd(NULL, 0);
2444 d0710d08 2019-07-22 stsp if (cwd == NULL)
2445 d0710d08 2019-07-22 stsp return got_error_from_errno("getcwd");
2446 d0710d08 2019-07-22 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2447 d0710d08 2019-07-22 stsp err = got_error_from_errno("asprintf");
2448 d0710d08 2019-07-22 stsp goto done;
2449 d0710d08 2019-07-22 stsp }
2450 d0710d08 2019-07-22 stsp }
2451 6c7ab921 2019-03-18 stsp
2452 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2453 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2454 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2455 6c7ab921 2019-03-18 stsp goto done;
2456 6c7ab921 2019-03-18 stsp }
2457 6c7ab921 2019-03-18 stsp
2458 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2459 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2460 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2461 f6d88e1a 2019-05-29 stsp if (err)
2462 f6d88e1a 2019-05-29 stsp goto done;
2463 f6d88e1a 2019-05-29 stsp } else {
2464 f6d88e1a 2019-05-29 stsp path = strdup("");
2465 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2466 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2467 f6d88e1a 2019-05-29 stsp goto done;
2468 f6d88e1a 2019-05-29 stsp }
2469 6c7ab921 2019-03-18 stsp }
2470 6c7ab921 2019-03-18 stsp
2471 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2472 6c7ab921 2019-03-18 stsp len = strlen(path);
2473 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
2474 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2475 6c7ab921 2019-03-18 stsp len--;
2476 6c7ab921 2019-03-18 stsp }
2477 6c7ab921 2019-03-18 stsp done:
2478 6c7ab921 2019-03-18 stsp free(resolved);
2479 d0710d08 2019-07-22 stsp free(cwd);
2480 6c7ab921 2019-03-18 stsp if (err == NULL)
2481 6c7ab921 2019-03-18 stsp *wt_path = path;
2482 6c7ab921 2019-03-18 stsp else
2483 6c7ab921 2019-03-18 stsp free(path);
2484 d00136be 2019-03-26 stsp return err;
2485 d00136be 2019-03-26 stsp }
2486 d00136be 2019-03-26 stsp
2487 07f5b47a 2019-06-02 stsp static const struct got_error *
2488 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2489 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2490 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2491 07f5b47a 2019-06-02 stsp {
2492 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2493 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2494 07f5b47a 2019-06-02 stsp
2495 07f5b47a 2019-06-02 stsp /* Re-adding an existing entry is a no-op. */
2496 d6c87207 2019-08-02 stsp if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
2497 07f5b47a 2019-06-02 stsp return NULL;
2498 07f5b47a 2019-06-02 stsp
2499 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2500 07f5b47a 2019-06-02 stsp if (err)
2501 07f5b47a 2019-06-02 stsp return err;
2502 07f5b47a 2019-06-02 stsp
2503 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2504 07f5b47a 2019-06-02 stsp if (err) {
2505 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2506 07f5b47a 2019-06-02 stsp return err;
2507 07f5b47a 2019-06-02 stsp }
2508 07f5b47a 2019-06-02 stsp
2509 c02b99b6 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2510 07f5b47a 2019-06-02 stsp }
2511 07f5b47a 2019-06-02 stsp
2512 d00136be 2019-03-26 stsp const struct got_error *
2513 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2514 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
2515 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2516 031a5338 2019-03-26 stsp struct got_repository *repo)
2517 d00136be 2019-03-26 stsp {
2518 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2519 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2520 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2521 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2522 d00136be 2019-03-26 stsp
2523 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2524 d00136be 2019-03-26 stsp if (err)
2525 d00136be 2019-03-26 stsp return err;
2526 d00136be 2019-03-26 stsp
2527 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2528 d00136be 2019-03-26 stsp if (err)
2529 d00136be 2019-03-26 stsp goto done;
2530 d00136be 2019-03-26 stsp
2531 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2532 1dd54920 2019-05-11 stsp char *relpath;
2533 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
2534 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
2535 1dd54920 2019-05-11 stsp if (err)
2536 af12c6b9 2019-06-04 stsp break;
2537 07f5b47a 2019-06-02 stsp err = schedule_addition(pe->path, fileindex, relpath,
2538 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2539 1dd54920 2019-05-11 stsp free(relpath);
2540 1dd54920 2019-05-11 stsp if (err)
2541 af12c6b9 2019-06-04 stsp break;
2542 1dd54920 2019-05-11 stsp }
2543 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2544 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2545 af12c6b9 2019-06-04 stsp err = sync_err;
2546 d00136be 2019-03-26 stsp done:
2547 fb399478 2019-07-12 stsp free(fileindex_path);
2548 d00136be 2019-03-26 stsp if (fileindex)
2549 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2550 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2551 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2552 d00136be 2019-03-26 stsp err = unlockerr;
2553 6c7ab921 2019-03-18 stsp return err;
2554 6c7ab921 2019-03-18 stsp }
2555 17ed4618 2019-06-02 stsp
2556 17ed4618 2019-06-02 stsp static const struct got_error *
2557 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2558 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2559 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2560 17ed4618 2019-06-02 stsp struct got_repository *repo)
2561 17ed4618 2019-06-02 stsp {
2562 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2563 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2564 9acbc4fa 2019-08-03 stsp unsigned char status, staged_status;
2565 17ed4618 2019-06-02 stsp struct stat sb;
2566 17ed4618 2019-06-02 stsp
2567 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2568 17ed4618 2019-06-02 stsp if (ie == NULL)
2569 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2570 2ec1f75b 2019-03-26 stsp
2571 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
2572 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
2573 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2574 9acbc4fa 2019-08-03 stsp return NULL;
2575 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2576 9acbc4fa 2019-08-03 stsp }
2577 9acbc4fa 2019-08-03 stsp
2578 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2579 17ed4618 2019-06-02 stsp if (err)
2580 17ed4618 2019-06-02 stsp return err;
2581 17ed4618 2019-06-02 stsp
2582 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2583 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2584 de426ea6 2019-08-03 stsp return NULL;
2585 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_MODIFY)
2586 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_STATUS);
2587 17ed4618 2019-06-02 stsp if (!delete_local_mods)
2588 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_MODIFIED);
2589 17ed4618 2019-06-02 stsp }
2590 17ed4618 2019-06-02 stsp
2591 17ed4618 2019-06-02 stsp if (unlink(ondisk_path) != 0)
2592 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2593 17ed4618 2019-06-02 stsp
2594 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2595 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2596 17ed4618 2019-06-02 stsp }
2597 17ed4618 2019-06-02 stsp
2598 2ec1f75b 2019-03-26 stsp const struct got_error *
2599 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2600 17ed4618 2019-06-02 stsp struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2601 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2602 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2603 2ec1f75b 2019-03-26 stsp {
2604 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2605 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2606 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2607 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2608 2ec1f75b 2019-03-26 stsp
2609 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2610 2ec1f75b 2019-03-26 stsp if (err)
2611 2ec1f75b 2019-03-26 stsp return err;
2612 2ec1f75b 2019-03-26 stsp
2613 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2614 2ec1f75b 2019-03-26 stsp if (err)
2615 2ec1f75b 2019-03-26 stsp goto done;
2616 2ec1f75b 2019-03-26 stsp
2617 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2618 17ed4618 2019-06-02 stsp char *relpath;
2619 17ed4618 2019-06-02 stsp err = got_path_skip_common_ancestor(&relpath,
2620 17ed4618 2019-06-02 stsp got_worktree_get_root_path(worktree), pe->path);
2621 17ed4618 2019-06-02 stsp if (err)
2622 af12c6b9 2019-06-04 stsp break;
2623 17ed4618 2019-06-02 stsp err = schedule_for_deletion(pe->path, fileindex, relpath,
2624 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2625 17ed4618 2019-06-02 stsp free(relpath);
2626 17ed4618 2019-06-02 stsp if (err)
2627 af12c6b9 2019-06-04 stsp break;
2628 2ec1f75b 2019-03-26 stsp }
2629 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2630 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2631 af12c6b9 2019-06-04 stsp err = sync_err;
2632 2ec1f75b 2019-03-26 stsp done:
2633 fb399478 2019-07-12 stsp free(fileindex_path);
2634 2ec1f75b 2019-03-26 stsp if (fileindex)
2635 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2636 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2637 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2638 2ec1f75b 2019-03-26 stsp err = unlockerr;
2639 2ec1f75b 2019-03-26 stsp return err;
2640 2ec1f75b 2019-03-26 stsp }
2641 a129376b 2019-03-28 stsp
2642 e20a8b6f 2019-06-04 stsp static const struct got_error *
2643 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2644 a129376b 2019-03-28 stsp const char *ondisk_path,
2645 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2646 a129376b 2019-03-28 stsp struct got_repository *repo)
2647 a129376b 2019-03-28 stsp {
2648 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2649 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2650 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2651 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2652 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2653 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
2654 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2655 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2656 24278f30 2019-08-03 stsp unsigned char status, staged_status;
2657 a129376b 2019-03-28 stsp struct stat sb;
2658 a129376b 2019-03-28 stsp
2659 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2660 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2661 a129376b 2019-03-28 stsp if (err)
2662 a129376b 2019-03-28 stsp goto done;
2663 a129376b 2019-03-28 stsp
2664 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2665 a129376b 2019-03-28 stsp if (ie == NULL) {
2666 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2667 a129376b 2019-03-28 stsp goto done;
2668 a129376b 2019-03-28 stsp }
2669 a129376b 2019-03-28 stsp
2670 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2671 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2672 a129376b 2019-03-28 stsp if (err) {
2673 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2674 a129376b 2019-03-28 stsp goto done;
2675 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2676 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2677 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2678 e20a8b6f 2019-06-04 stsp goto done;
2679 e20a8b6f 2019-06-04 stsp }
2680 a129376b 2019-03-28 stsp }
2681 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2682 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2683 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2684 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2685 a129376b 2019-03-28 stsp goto done;
2686 a129376b 2019-03-28 stsp }
2687 a129376b 2019-03-28 stsp } else {
2688 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2689 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2690 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2691 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2692 a129376b 2019-03-28 stsp goto done;
2693 a129376b 2019-03-28 stsp }
2694 a129376b 2019-03-28 stsp } else {
2695 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2696 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2697 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2698 a129376b 2019-03-28 stsp goto done;
2699 a129376b 2019-03-28 stsp }
2700 a129376b 2019-03-28 stsp }
2701 a129376b 2019-03-28 stsp }
2702 a129376b 2019-03-28 stsp
2703 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2704 a129376b 2019-03-28 stsp if (err)
2705 a129376b 2019-03-28 stsp goto done;
2706 7fa0d182 2019-08-03 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2707 7fa0d182 2019-08-03 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
2708 24278f30 2019-08-03 stsp
2709 24278f30 2019-08-03 stsp staged_status = get_staged_status(ie);
2710 24278f30 2019-08-03 stsp if (status == GOT_STATUS_DELETE &&
2711 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_NO_CHANGE) {
2712 24278f30 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2713 24278f30 2019-08-03 stsp goto done;
2714 24278f30 2019-08-03 stsp }
2715 a129376b 2019-03-28 stsp
2716 a9fa2909 2019-07-27 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2717 a9fa2909 2019-07-27 stsp tree_path);
2718 a9fa2909 2019-07-27 stsp if (err) {
2719 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2720 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
2721 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
2722 a9fa2909 2019-07-27 stsp goto done;
2723 a9fa2909 2019-07-27 stsp } else {
2724 a9fa2909 2019-07-27 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2725 a9fa2909 2019-07-27 stsp if (err)
2726 a9fa2909 2019-07-27 stsp goto done;
2727 a9fa2909 2019-07-27 stsp
2728 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
2729 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
2730 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
2731 a9fa2909 2019-07-27 stsp goto done;
2732 a9fa2909 2019-07-27 stsp }
2733 a9fa2909 2019-07-27 stsp
2734 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
2735 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
2736 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
2737 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2738 a9fa2909 2019-07-27 stsp goto done;
2739 a9fa2909 2019-07-27 stsp }
2740 a129376b 2019-03-28 stsp }
2741 a129376b 2019-03-28 stsp
2742 a129376b 2019-03-28 stsp switch (status) {
2743 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2744 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2745 1ee397ad 2019-07-12 stsp if (err)
2746 1ee397ad 2019-07-12 stsp goto done;
2747 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2748 a129376b 2019-03-28 stsp break;
2749 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2750 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2751 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2752 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2753 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2754 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2755 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2756 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
2757 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2758 24278f30 2019-08-03 stsp } else
2759 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
2760 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2761 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2762 a129376b 2019-03-28 stsp if (err)
2763 a129376b 2019-03-28 stsp goto done;
2764 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2765 24278f30 2019-08-03 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE, sb.st_mode,
2766 24278f30 2019-08-03 stsp blob, 0, 1, repo, progress_cb, progress_arg);
2767 a129376b 2019-03-28 stsp if (err)
2768 a129376b 2019-03-28 stsp goto done;
2769 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2770 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2771 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2772 a129376b 2019-03-28 stsp if (err)
2773 a129376b 2019-03-28 stsp goto done;
2774 a129376b 2019-03-28 stsp }
2775 a129376b 2019-03-28 stsp break;
2776 e20a8b6f 2019-06-04 stsp }
2777 a129376b 2019-03-28 stsp default:
2778 a129376b 2019-03-28 stsp goto done;
2779 a129376b 2019-03-28 stsp }
2780 a129376b 2019-03-28 stsp done:
2781 a129376b 2019-03-28 stsp free(relpath);
2782 e20a8b6f 2019-06-04 stsp free(parent_path);
2783 a129376b 2019-03-28 stsp free(tree_path);
2784 a129376b 2019-03-28 stsp if (blob)
2785 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2786 a129376b 2019-03-28 stsp if (tree)
2787 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2788 a129376b 2019-03-28 stsp free(tree_id);
2789 e20a8b6f 2019-06-04 stsp return err;
2790 e20a8b6f 2019-06-04 stsp }
2791 e20a8b6f 2019-06-04 stsp
2792 e20a8b6f 2019-06-04 stsp const struct got_error *
2793 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2794 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2795 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2796 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2797 e20a8b6f 2019-06-04 stsp {
2798 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2799 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2800 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2801 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2802 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2803 e20a8b6f 2019-06-04 stsp
2804 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2805 e20a8b6f 2019-06-04 stsp if (err)
2806 e20a8b6f 2019-06-04 stsp return err;
2807 e20a8b6f 2019-06-04 stsp
2808 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2809 e20a8b6f 2019-06-04 stsp if (err)
2810 e20a8b6f 2019-06-04 stsp goto done;
2811 e20a8b6f 2019-06-04 stsp
2812 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2813 e20a8b6f 2019-06-04 stsp err = revert_file(worktree, fileindex, pe->path,
2814 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2815 e20a8b6f 2019-06-04 stsp if (err)
2816 af12c6b9 2019-06-04 stsp break;
2817 e20a8b6f 2019-06-04 stsp }
2818 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2819 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2820 e20a8b6f 2019-06-04 stsp err = sync_err;
2821 af12c6b9 2019-06-04 stsp done:
2822 fb399478 2019-07-12 stsp free(fileindex_path);
2823 a129376b 2019-03-28 stsp if (fileindex)
2824 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2825 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2826 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2827 a129376b 2019-03-28 stsp err = unlockerr;
2828 c4296144 2019-05-09 stsp return err;
2829 c4296144 2019-05-09 stsp }
2830 c4296144 2019-05-09 stsp
2831 cf066bf8 2019-05-09 stsp static void
2832 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2833 cf066bf8 2019-05-09 stsp {
2834 24519714 2019-05-09 stsp free(ct->path);
2835 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2836 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2837 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2838 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2839 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2840 cf066bf8 2019-05-09 stsp free(ct);
2841 cf066bf8 2019-05-09 stsp }
2842 24519714 2019-05-09 stsp
2843 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2844 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2845 24519714 2019-05-09 stsp struct got_repository *repo;
2846 24519714 2019-05-09 stsp struct got_worktree *worktree;
2847 24519714 2019-05-09 stsp };
2848 cf066bf8 2019-05-09 stsp
2849 c4296144 2019-05-09 stsp static const struct got_error *
2850 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
2851 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
2852 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2853 537ac44b 2019-08-03 stsp struct got_object_id *commit_id)
2854 c4296144 2019-05-09 stsp {
2855 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2856 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2857 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2858 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2859 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2860 768aea60 2019-05-09 stsp struct stat sb;
2861 c4296144 2019-05-09 stsp
2862 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2863 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2864 c4296144 2019-05-09 stsp
2865 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2866 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2867 c4296144 2019-05-09 stsp return NULL;
2868 0b5cc0d6 2019-05-09 stsp
2869 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2870 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2871 036813ee 2019-05-09 stsp goto done;
2872 036813ee 2019-05-09 stsp }
2873 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2874 036813ee 2019-05-09 stsp parent_path = strdup("");
2875 69960a46 2019-05-09 stsp if (parent_path == NULL)
2876 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2877 69960a46 2019-05-09 stsp } else {
2878 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2879 69960a46 2019-05-09 stsp if (err)
2880 69960a46 2019-05-09 stsp return err;
2881 69960a46 2019-05-09 stsp }
2882 c4296144 2019-05-09 stsp
2883 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2884 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2885 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2886 c4296144 2019-05-09 stsp goto done;
2887 768aea60 2019-05-09 stsp }
2888 768aea60 2019-05-09 stsp
2889 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2890 768aea60 2019-05-09 stsp relpath) == -1) {
2891 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2892 768aea60 2019-05-09 stsp goto done;
2893 768aea60 2019-05-09 stsp }
2894 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2895 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2896 768aea60 2019-05-09 stsp } else {
2897 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2898 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2899 768aea60 2019-05-09 stsp goto done;
2900 768aea60 2019-05-09 stsp }
2901 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2902 c4296144 2019-05-09 stsp }
2903 c4296144 2019-05-09 stsp
2904 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2905 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2906 44d03001 2019-05-09 stsp relpath) == -1) {
2907 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2908 44d03001 2019-05-09 stsp goto done;
2909 44d03001 2019-05-09 stsp }
2910 44d03001 2019-05-09 stsp
2911 cf066bf8 2019-05-09 stsp ct->status = status;
2912 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2913 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2914 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2915 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2916 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2917 b416585c 2019-05-13 stsp goto done;
2918 b416585c 2019-05-13 stsp }
2919 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2920 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2921 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2922 036813ee 2019-05-09 stsp goto done;
2923 036813ee 2019-05-09 stsp }
2924 ca2503ea 2019-05-09 stsp }
2925 24519714 2019-05-09 stsp ct->path = strdup(path);
2926 24519714 2019-05-09 stsp if (ct->path == NULL) {
2927 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2928 24519714 2019-05-09 stsp goto done;
2929 24519714 2019-05-09 stsp }
2930 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2931 c4296144 2019-05-09 stsp done:
2932 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2933 ed175427 2019-05-09 stsp free_commitable(ct);
2934 24519714 2019-05-09 stsp free(parent_path);
2935 036813ee 2019-05-09 stsp free(path);
2936 a129376b 2019-03-28 stsp return err;
2937 a129376b 2019-03-28 stsp }
2938 c4296144 2019-05-09 stsp
2939 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2940 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2941 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2942 036813ee 2019-05-09 stsp struct got_repository *);
2943 ed175427 2019-05-09 stsp
2944 ed175427 2019-05-09 stsp static const struct got_error *
2945 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2946 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2947 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2948 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2949 afa376bf 2019-05-09 stsp struct got_repository *repo)
2950 ed175427 2019-05-09 stsp {
2951 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2952 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2953 036813ee 2019-05-09 stsp char *subpath;
2954 ed175427 2019-05-09 stsp
2955 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2956 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2957 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2958 ed175427 2019-05-09 stsp
2959 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2960 ed175427 2019-05-09 stsp if (err)
2961 ed175427 2019-05-09 stsp return err;
2962 ed175427 2019-05-09 stsp
2963 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2964 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2965 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2966 036813ee 2019-05-09 stsp free(subpath);
2967 036813ee 2019-05-09 stsp return err;
2968 036813ee 2019-05-09 stsp }
2969 ed175427 2019-05-09 stsp
2970 036813ee 2019-05-09 stsp static const struct got_error *
2971 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2972 036813ee 2019-05-09 stsp {
2973 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2974 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2975 ed175427 2019-05-09 stsp
2976 036813ee 2019-05-09 stsp *match = 0;
2977 ed175427 2019-05-09 stsp
2978 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
2979 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2980 0f63689d 2019-05-10 stsp return NULL;
2981 036813ee 2019-05-09 stsp }
2982 0b5cc0d6 2019-05-09 stsp
2983 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2984 0f63689d 2019-05-10 stsp if (err)
2985 0f63689d 2019-05-10 stsp return err;
2986 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2987 036813ee 2019-05-09 stsp free(ct_parent_path);
2988 036813ee 2019-05-09 stsp return err;
2989 036813ee 2019-05-09 stsp }
2990 0b5cc0d6 2019-05-09 stsp
2991 768aea60 2019-05-09 stsp static mode_t
2992 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2993 768aea60 2019-05-09 stsp {
2994 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2995 768aea60 2019-05-09 stsp }
2996 768aea60 2019-05-09 stsp
2997 036813ee 2019-05-09 stsp static const struct got_error *
2998 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2999 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
3000 036813ee 2019-05-09 stsp {
3001 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3002 ca2503ea 2019-05-09 stsp
3003 036813ee 2019-05-09 stsp *new_te = NULL;
3004 0b5cc0d6 2019-05-09 stsp
3005 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
3006 036813ee 2019-05-09 stsp if (err)
3007 036813ee 2019-05-09 stsp goto done;
3008 0b5cc0d6 2019-05-09 stsp
3009 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3010 036813ee 2019-05-09 stsp
3011 036813ee 2019-05-09 stsp free((*new_te)->id);
3012 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
3013 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
3014 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3015 036813ee 2019-05-09 stsp goto done;
3016 036813ee 2019-05-09 stsp }
3017 036813ee 2019-05-09 stsp done:
3018 036813ee 2019-05-09 stsp if (err && *new_te) {
3019 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
3020 036813ee 2019-05-09 stsp *new_te = NULL;
3021 036813ee 2019-05-09 stsp }
3022 036813ee 2019-05-09 stsp return err;
3023 036813ee 2019-05-09 stsp }
3024 036813ee 2019-05-09 stsp
3025 036813ee 2019-05-09 stsp static const struct got_error *
3026 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3027 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
3028 036813ee 2019-05-09 stsp {
3029 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3030 036813ee 2019-05-09 stsp char *ct_name;
3031 036813ee 2019-05-09 stsp
3032 036813ee 2019-05-09 stsp *new_te = NULL;
3033 036813ee 2019-05-09 stsp
3034 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
3035 036813ee 2019-05-09 stsp if (*new_te == NULL)
3036 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3037 036813ee 2019-05-09 stsp
3038 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
3039 036813ee 2019-05-09 stsp if (ct_name == NULL) {
3040 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
3041 036813ee 2019-05-09 stsp goto done;
3042 036813ee 2019-05-09 stsp }
3043 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
3044 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
3045 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3046 036813ee 2019-05-09 stsp goto done;
3047 036813ee 2019-05-09 stsp }
3048 036813ee 2019-05-09 stsp
3049 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
3050 036813ee 2019-05-09 stsp
3051 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
3052 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
3053 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3054 036813ee 2019-05-09 stsp goto done;
3055 036813ee 2019-05-09 stsp }
3056 036813ee 2019-05-09 stsp done:
3057 036813ee 2019-05-09 stsp if (err && *new_te) {
3058 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
3059 036813ee 2019-05-09 stsp *new_te = NULL;
3060 036813ee 2019-05-09 stsp }
3061 036813ee 2019-05-09 stsp return err;
3062 036813ee 2019-05-09 stsp }
3063 036813ee 2019-05-09 stsp
3064 036813ee 2019-05-09 stsp static const struct got_error *
3065 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
3066 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
3067 036813ee 2019-05-09 stsp {
3068 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3069 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
3070 036813ee 2019-05-09 stsp
3071 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3072 036813ee 2019-05-09 stsp if (err)
3073 036813ee 2019-05-09 stsp return err;
3074 036813ee 2019-05-09 stsp if (new_pe == NULL)
3075 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
3076 036813ee 2019-05-09 stsp return NULL;
3077 afa376bf 2019-05-09 stsp }
3078 afa376bf 2019-05-09 stsp
3079 afa376bf 2019-05-09 stsp static const struct got_error *
3080 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
3081 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
3082 afa376bf 2019-05-09 stsp {
3083 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
3084 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
3085 afa376bf 2019-05-09 stsp ct_path++;
3086 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, ct->status, GOT_STATUS_NO_CHANGE,
3087 537ac44b 2019-08-03 stsp ct_path, ct->blob_id, NULL, NULL);
3088 036813ee 2019-05-09 stsp }
3089 036813ee 2019-05-09 stsp
3090 036813ee 2019-05-09 stsp static const struct got_error *
3091 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
3092 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3093 44d03001 2019-05-09 stsp {
3094 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
3095 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
3096 44d03001 2019-05-09 stsp char *te_path;
3097 44d03001 2019-05-09 stsp
3098 44d03001 2019-05-09 stsp *modified = 0;
3099 44d03001 2019-05-09 stsp
3100 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
3101 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
3102 44d03001 2019-05-09 stsp te->name) == -1)
3103 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3104 44d03001 2019-05-09 stsp
3105 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3106 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3107 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
3108 44d03001 2019-05-09 stsp strlen(te_path));
3109 44d03001 2019-05-09 stsp if (*modified)
3110 44d03001 2019-05-09 stsp break;
3111 44d03001 2019-05-09 stsp }
3112 44d03001 2019-05-09 stsp
3113 44d03001 2019-05-09 stsp free(te_path);
3114 44d03001 2019-05-09 stsp return err;
3115 44d03001 2019-05-09 stsp }
3116 44d03001 2019-05-09 stsp
3117 44d03001 2019-05-09 stsp static const struct got_error *
3118 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
3119 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
3120 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
3121 036813ee 2019-05-09 stsp {
3122 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3123 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3124 036813ee 2019-05-09 stsp
3125 036813ee 2019-05-09 stsp *ctp = NULL;
3126 036813ee 2019-05-09 stsp
3127 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3128 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3129 036813ee 2019-05-09 stsp char *ct_name = NULL;
3130 036813ee 2019-05-09 stsp int path_matches;
3131 036813ee 2019-05-09 stsp
3132 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
3133 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
3134 036813ee 2019-05-09 stsp continue;
3135 036813ee 2019-05-09 stsp
3136 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3137 036813ee 2019-05-09 stsp continue;
3138 036813ee 2019-05-09 stsp
3139 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3140 036813ee 2019-05-09 stsp if (err)
3141 036813ee 2019-05-09 stsp return err;
3142 036813ee 2019-05-09 stsp if (!path_matches)
3143 036813ee 2019-05-09 stsp continue;
3144 036813ee 2019-05-09 stsp
3145 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
3146 036813ee 2019-05-09 stsp if (ct_name == NULL)
3147 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
3148 036813ee 2019-05-09 stsp
3149 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
3150 036813ee 2019-05-09 stsp continue;
3151 036813ee 2019-05-09 stsp
3152 036813ee 2019-05-09 stsp *ctp = ct;
3153 036813ee 2019-05-09 stsp break;
3154 036813ee 2019-05-09 stsp }
3155 2b496619 2019-07-10 stsp
3156 2b496619 2019-07-10 stsp return err;
3157 2b496619 2019-07-10 stsp }
3158 2b496619 2019-07-10 stsp
3159 2b496619 2019-07-10 stsp static const struct got_error *
3160 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3161 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
3162 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
3163 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3164 2b496619 2019-07-10 stsp struct got_repository *repo)
3165 2b496619 2019-07-10 stsp {
3166 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
3167 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
3168 2b496619 2019-07-10 stsp char *subtree_path;
3169 2b496619 2019-07-10 stsp
3170 2b496619 2019-07-10 stsp *new_tep = NULL;
3171 2b496619 2019-07-10 stsp
3172 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3173 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
3174 2b496619 2019-07-10 stsp child_path) == -1)
3175 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
3176 036813ee 2019-05-09 stsp
3177 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
3178 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
3179 2b496619 2019-07-10 stsp new_te->name = strdup(child_path);
3180 2b496619 2019-07-10 stsp if (new_te->name == NULL) {
3181 2b496619 2019-07-10 stsp err = got_error_from_errno("strdup");
3182 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3183 2b496619 2019-07-10 stsp goto done;
3184 2b496619 2019-07-10 stsp }
3185 2b496619 2019-07-10 stsp err = write_tree(&new_te->id, NULL, subtree_path,
3186 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
3187 2b496619 2019-07-10 stsp if (err) {
3188 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3189 2b496619 2019-07-10 stsp goto done;
3190 2b496619 2019-07-10 stsp }
3191 2b496619 2019-07-10 stsp done:
3192 2b496619 2019-07-10 stsp free(subtree_path);
3193 2b496619 2019-07-10 stsp if (err == NULL)
3194 2b496619 2019-07-10 stsp *new_tep = new_te;
3195 036813ee 2019-05-09 stsp return err;
3196 036813ee 2019-05-09 stsp }
3197 036813ee 2019-05-09 stsp
3198 036813ee 2019-05-09 stsp static const struct got_error *
3199 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
3200 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
3201 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3202 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3203 036813ee 2019-05-09 stsp struct got_repository *repo)
3204 036813ee 2019-05-09 stsp {
3205 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3206 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
3207 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
3208 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
3209 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
3210 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3211 036813ee 2019-05-09 stsp
3212 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
3213 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
3214 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
3215 036813ee 2019-05-09 stsp
3216 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
3217 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3218 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3219 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
3220 036813ee 2019-05-09 stsp
3221 a3df2849 2019-05-20 stsp if (ct->status != GOT_STATUS_ADD ||
3222 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
3223 036813ee 2019-05-09 stsp continue;
3224 036813ee 2019-05-09 stsp
3225 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
3226 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
3227 036813ee 2019-05-09 stsp continue;
3228 036813ee 2019-05-09 stsp
3229 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3230 036813ee 2019-05-09 stsp pe->path);
3231 036813ee 2019-05-09 stsp if (err)
3232 036813ee 2019-05-09 stsp goto done;
3233 036813ee 2019-05-09 stsp
3234 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
3235 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
3236 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
3237 036813ee 2019-05-09 stsp if (err)
3238 036813ee 2019-05-09 stsp goto done;
3239 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
3240 afa376bf 2019-05-09 stsp if (err)
3241 afa376bf 2019-05-09 stsp goto done;
3242 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
3243 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3244 2b496619 2019-07-10 stsp if (err)
3245 2f51b5b3 2019-05-09 stsp goto done;
3246 2b496619 2019-07-10 stsp } else {
3247 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
3248 2b496619 2019-07-10 stsp if (base_tree == NULL ||
3249 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
3250 2b496619 2019-07-10 stsp == NULL) {
3251 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
3252 2b496619 2019-07-10 stsp child_path, path_base_tree,
3253 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
3254 2b496619 2019-07-10 stsp repo);
3255 2b496619 2019-07-10 stsp if (err)
3256 2b496619 2019-07-10 stsp goto done;
3257 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3258 2b496619 2019-07-10 stsp if (err)
3259 2b496619 2019-07-10 stsp goto done;
3260 9ba0479c 2019-05-10 stsp }
3261 ed175427 2019-05-09 stsp }
3262 2f51b5b3 2019-05-09 stsp }
3263 2f51b5b3 2019-05-09 stsp
3264 2f51b5b3 2019-05-09 stsp if (base_tree) {
3265 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
3266 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
3267 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3268 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3269 2f51b5b3 2019-05-09 stsp
3270 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
3271 44d03001 2019-05-09 stsp int modified;
3272 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3273 036813ee 2019-05-09 stsp if (err)
3274 036813ee 2019-05-09 stsp goto done;
3275 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
3276 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
3277 2f51b5b3 2019-05-09 stsp if (err)
3278 2f51b5b3 2019-05-09 stsp goto done;
3279 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
3280 44d03001 2019-05-09 stsp if (modified) {
3281 44d03001 2019-05-09 stsp free(new_te->id);
3282 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
3283 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
3284 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
3285 44d03001 2019-05-09 stsp if (err)
3286 44d03001 2019-05-09 stsp goto done;
3287 44d03001 2019-05-09 stsp }
3288 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3289 036813ee 2019-05-09 stsp if (err)
3290 036813ee 2019-05-09 stsp goto done;
3291 2f51b5b3 2019-05-09 stsp continue;
3292 036813ee 2019-05-09 stsp }
3293 2f51b5b3 2019-05-09 stsp
3294 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
3295 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
3296 2f51b5b3 2019-05-09 stsp if (ct) {
3297 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
3298 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
3299 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
3300 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
3301 2f51b5b3 2019-05-09 stsp if (err)
3302 2f51b5b3 2019-05-09 stsp goto done;
3303 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3304 2f51b5b3 2019-05-09 stsp if (err)
3305 2f51b5b3 2019-05-09 stsp goto done;
3306 2f51b5b3 2019-05-09 stsp }
3307 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3308 b416585c 2019-05-13 stsp status_arg);
3309 afa376bf 2019-05-09 stsp if (err)
3310 afa376bf 2019-05-09 stsp goto done;
3311 2f51b5b3 2019-05-09 stsp } else {
3312 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3313 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3314 2f51b5b3 2019-05-09 stsp if (err)
3315 2f51b5b3 2019-05-09 stsp goto done;
3316 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3317 2f51b5b3 2019-05-09 stsp if (err)
3318 2f51b5b3 2019-05-09 stsp goto done;
3319 2f51b5b3 2019-05-09 stsp }
3320 ca2503ea 2019-05-09 stsp }
3321 ed175427 2019-05-09 stsp }
3322 0b5cc0d6 2019-05-09 stsp
3323 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3324 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3325 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3326 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3327 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3328 0b5cc0d6 2019-05-09 stsp }
3329 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3330 ed175427 2019-05-09 stsp done:
3331 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3332 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3333 ed175427 2019-05-09 stsp return err;
3334 ed175427 2019-05-09 stsp }
3335 ed175427 2019-05-09 stsp
3336 ebf99748 2019-05-09 stsp static const struct got_error *
3337 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3338 93ec1b5f 2019-07-12 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3339 ebf99748 2019-05-09 stsp {
3340 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
3341 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3342 ebf99748 2019-05-09 stsp
3343 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3344 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3345 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3346 ebf99748 2019-05-09 stsp
3347 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3348 ebf99748 2019-05-09 stsp if (ie) {
3349 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
3350 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3351 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3352 ebf99748 2019-05-09 stsp } else
3353 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3354 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3355 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3356 ebf99748 2019-05-09 stsp } else {
3357 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3358 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3359 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3360 ebf99748 2019-05-09 stsp if (err)
3361 af12c6b9 2019-06-04 stsp break;
3362 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3363 ebf99748 2019-05-09 stsp if (err)
3364 af12c6b9 2019-06-04 stsp break;
3365 ebf99748 2019-05-09 stsp }
3366 ebf99748 2019-05-09 stsp }
3367 d56d26ce 2019-05-10 stsp return err;
3368 d56d26ce 2019-05-10 stsp }
3369 d56d26ce 2019-05-10 stsp
3370 d56d26ce 2019-05-10 stsp static const struct got_error *
3371 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3372 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
3373 d56d26ce 2019-05-10 stsp {
3374 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3375 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
3376 1a36436d 2019-06-10 stsp struct got_commit_object *commit = NULL;
3377 1a36436d 2019-06-10 stsp const char *ct_path = ct->in_repo_path;
3378 1a36436d 2019-06-10 stsp
3379 1a36436d 2019-06-10 stsp while (ct_path[0] == '/')
3380 1a36436d 2019-06-10 stsp ct_path++;
3381 d56d26ce 2019-05-10 stsp
3382 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
3383 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3384 1a36436d 2019-06-10 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3385 1a36436d 2019-06-10 stsp return NULL;
3386 9bead371 2019-07-28 stsp /*
3387 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
3388 9bead371 2019-07-28 stsp * on matches file content in the branch head.
3389 9bead371 2019-07-28 stsp */
3390 9bead371 2019-07-28 stsp err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3391 9bead371 2019-07-28 stsp if (err) {
3392 9bead371 2019-07-28 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
3393 9bead371 2019-07-28 stsp goto done;
3394 9bead371 2019-07-28 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3395 1a36436d 2019-06-10 stsp goto done;
3396 9bead371 2019-07-28 stsp } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3397 9bead371 2019-07-28 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3398 1a36436d 2019-06-10 stsp } else {
3399 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3400 9bead371 2019-07-28 stsp err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3401 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3402 1a36436d 2019-06-10 stsp goto done;
3403 9bead371 2019-07-28 stsp err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3404 1a36436d 2019-06-10 stsp }
3405 1a36436d 2019-06-10 stsp done:
3406 1a36436d 2019-06-10 stsp if (commit)
3407 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3408 1a36436d 2019-06-10 stsp free(id);
3409 1a36436d 2019-06-10 stsp return err;
3410 ebf99748 2019-05-09 stsp }
3411 ebf99748 2019-05-09 stsp
3412 c4296144 2019-05-09 stsp const struct got_error *
3413 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
3414 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
3415 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
3416 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
3417 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3418 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3419 afa376bf 2019-05-09 stsp struct got_repository *repo)
3420 c4296144 2019-05-09 stsp {
3421 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3422 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3423 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3424 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3425 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3426 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3427 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3428 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3429 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3430 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3431 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3432 c4296144 2019-05-09 stsp
3433 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3434 c4296144 2019-05-09 stsp
3435 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3436 675c7539 2019-05-09 stsp
3437 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3438 588edf97 2019-05-10 stsp if (err)
3439 588edf97 2019-05-10 stsp goto done;
3440 de18fc63 2019-05-09 stsp
3441 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3442 588edf97 2019-05-10 stsp if (err)
3443 588edf97 2019-05-10 stsp goto done;
3444 588edf97 2019-05-10 stsp
3445 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3446 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3447 33ad4cbe 2019-05-12 jcs if (err)
3448 33ad4cbe 2019-05-12 jcs goto done;
3449 33ad4cbe 2019-05-12 jcs }
3450 c4296144 2019-05-09 stsp
3451 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3452 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3453 33ad4cbe 2019-05-12 jcs goto done;
3454 33ad4cbe 2019-05-12 jcs }
3455 33ad4cbe 2019-05-12 jcs
3456 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3457 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3458 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3459 cf066bf8 2019-05-09 stsp char *ondisk_path;
3460 cf066bf8 2019-05-09 stsp
3461 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3462 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3463 cf066bf8 2019-05-09 stsp continue;
3464 cf066bf8 2019-05-09 stsp
3465 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3466 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3467 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3468 cf066bf8 2019-05-09 stsp goto done;
3469 cf066bf8 2019-05-09 stsp }
3470 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3471 a7055788 2019-05-09 stsp free(ondisk_path);
3472 cf066bf8 2019-05-09 stsp if (err)
3473 cf066bf8 2019-05-09 stsp goto done;
3474 cf066bf8 2019-05-09 stsp }
3475 036813ee 2019-05-09 stsp
3476 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3477 39cd0ff6 2019-07-12 stsp err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3478 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3479 036813ee 2019-05-09 stsp if (err)
3480 036813ee 2019-05-09 stsp goto done;
3481 036813ee 2019-05-09 stsp
3482 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3483 de18fc63 2019-05-09 stsp if (err)
3484 de18fc63 2019-05-09 stsp goto done;
3485 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3486 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3487 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3488 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3489 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3490 33ad4cbe 2019-05-12 jcs free(logmsg);
3491 9d40349a 2019-05-09 stsp if (err)
3492 9d40349a 2019-05-09 stsp goto done;
3493 9d40349a 2019-05-09 stsp
3494 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3495 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3496 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3497 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3498 09f5bd90 2019-05-09 stsp goto done;
3499 09f5bd90 2019-05-09 stsp }
3500 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3501 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3502 09f5bd90 2019-05-09 stsp if (err)
3503 09f5bd90 2019-05-09 stsp goto done;
3504 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3505 ebf99748 2019-05-09 stsp if (err)
3506 ebf99748 2019-05-09 stsp goto done;
3507 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3508 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3509 09f5bd90 2019-05-09 stsp goto done;
3510 09f5bd90 2019-05-09 stsp }
3511 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3512 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3513 f2c16586 2019-05-09 stsp if (err)
3514 f2c16586 2019-05-09 stsp goto done;
3515 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3516 f2c16586 2019-05-09 stsp if (err)
3517 f2c16586 2019-05-09 stsp goto done;
3518 f2c16586 2019-05-09 stsp
3519 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3520 09f5bd90 2019-05-09 stsp if (err)
3521 09f5bd90 2019-05-09 stsp goto done;
3522 09f5bd90 2019-05-09 stsp
3523 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3524 ebf99748 2019-05-09 stsp if (err)
3525 ebf99748 2019-05-09 stsp goto done;
3526 c4296144 2019-05-09 stsp done:
3527 588edf97 2019-05-10 stsp if (head_tree)
3528 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3529 588edf97 2019-05-10 stsp if (head_commit)
3530 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3531 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3532 2f17228e 2019-05-12 stsp if (head_ref2) {
3533 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3534 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3535 2f17228e 2019-05-12 stsp err = unlockerr;
3536 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3537 39cd0ff6 2019-07-12 stsp }
3538 39cd0ff6 2019-07-12 stsp return err;
3539 39cd0ff6 2019-07-12 stsp }
3540 5c1e53bc 2019-07-28 stsp
3541 5c1e53bc 2019-07-28 stsp static const struct got_error *
3542 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
3543 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
3544 5c1e53bc 2019-07-28 stsp {
3545 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
3546 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
3547 39cd0ff6 2019-07-12 stsp
3548 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
3549 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
3550 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
3551 5c1e53bc 2019-07-28 stsp
3552 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
3553 5c1e53bc 2019-07-28 stsp ct_path++;
3554 5c1e53bc 2019-07-28 stsp
3555 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
3556 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
3557 5c1e53bc 2019-07-28 stsp break;
3558 5c1e53bc 2019-07-28 stsp }
3559 5c1e53bc 2019-07-28 stsp
3560 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
3561 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
3562 5c1e53bc 2019-07-28 stsp
3563 5c1e53bc 2019-07-28 stsp return NULL;
3564 5c1e53bc 2019-07-28 stsp }
3565 5c1e53bc 2019-07-28 stsp
3566 39cd0ff6 2019-07-12 stsp const struct got_error *
3567 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3568 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
3569 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
3570 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3571 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3572 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
3573 39cd0ff6 2019-07-12 stsp {
3574 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3575 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3576 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
3577 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
3578 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
3579 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
3580 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
3581 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
3582 39cd0ff6 2019-07-12 stsp
3583 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
3584 39cd0ff6 2019-07-12 stsp
3585 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
3586 39cd0ff6 2019-07-12 stsp
3587 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
3588 39cd0ff6 2019-07-12 stsp if (err)
3589 39cd0ff6 2019-07-12 stsp goto done;
3590 39cd0ff6 2019-07-12 stsp
3591 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3592 39cd0ff6 2019-07-12 stsp if (err)
3593 39cd0ff6 2019-07-12 stsp goto done;
3594 39cd0ff6 2019-07-12 stsp
3595 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3596 39cd0ff6 2019-07-12 stsp if (err)
3597 39cd0ff6 2019-07-12 stsp goto done;
3598 39cd0ff6 2019-07-12 stsp
3599 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3600 39cd0ff6 2019-07-12 stsp if (err)
3601 39cd0ff6 2019-07-12 stsp goto done;
3602 39cd0ff6 2019-07-12 stsp
3603 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
3604 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
3605 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
3606 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
3607 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3608 5c1e53bc 2019-07-28 stsp collect_commitables, &cc_arg, NULL, NULL);
3609 5c1e53bc 2019-07-28 stsp if (err)
3610 5c1e53bc 2019-07-28 stsp goto done;
3611 5c1e53bc 2019-07-28 stsp }
3612 39cd0ff6 2019-07-12 stsp
3613 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
3614 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3615 39cd0ff6 2019-07-12 stsp goto done;
3616 5c1e53bc 2019-07-28 stsp }
3617 5c1e53bc 2019-07-28 stsp
3618 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
3619 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
3620 5c1e53bc 2019-07-28 stsp if (err)
3621 5c1e53bc 2019-07-28 stsp goto done;
3622 39cd0ff6 2019-07-12 stsp }
3623 39cd0ff6 2019-07-12 stsp
3624 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3625 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
3626 b50cabdf 2019-07-12 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
3627 b50cabdf 2019-07-12 stsp if (err)
3628 b50cabdf 2019-07-12 stsp goto done;
3629 b50cabdf 2019-07-12 stsp }
3630 b50cabdf 2019-07-12 stsp
3631 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
3632 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
3633 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3634 39cd0ff6 2019-07-12 stsp if (err)
3635 39cd0ff6 2019-07-12 stsp goto done;
3636 39cd0ff6 2019-07-12 stsp
3637 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3638 93ec1b5f 2019-07-12 stsp fileindex);
3639 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3640 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
3641 39cd0ff6 2019-07-12 stsp err = sync_err;
3642 39cd0ff6 2019-07-12 stsp done:
3643 39cd0ff6 2019-07-12 stsp if (fileindex)
3644 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
3645 39cd0ff6 2019-07-12 stsp free(fileindex_path);
3646 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3647 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
3648 39cd0ff6 2019-07-12 stsp err = unlockerr;
3649 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3650 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
3651 39cd0ff6 2019-07-12 stsp free_commitable(ct);
3652 39cd0ff6 2019-07-12 stsp }
3653 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
3654 c4296144 2019-05-09 stsp return err;
3655 8656d6c4 2019-05-20 stsp }
3656 8656d6c4 2019-05-20 stsp
3657 8656d6c4 2019-05-20 stsp const char *
3658 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3659 8656d6c4 2019-05-20 stsp {
3660 8656d6c4 2019-05-20 stsp return ct->path;
3661 8656d6c4 2019-05-20 stsp }
3662 8656d6c4 2019-05-20 stsp
3663 8656d6c4 2019-05-20 stsp unsigned int
3664 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3665 8656d6c4 2019-05-20 stsp {
3666 8656d6c4 2019-05-20 stsp return ct->status;
3667 818c7501 2019-07-11 stsp }
3668 818c7501 2019-07-11 stsp
3669 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
3670 818c7501 2019-07-11 stsp struct got_worktree *worktree;
3671 818c7501 2019-07-11 stsp struct got_repository *repo;
3672 818c7501 2019-07-11 stsp };
3673 818c7501 2019-07-11 stsp
3674 818c7501 2019-07-11 stsp static const struct got_error *
3675 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3676 818c7501 2019-07-11 stsp {
3677 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3678 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
3679 818c7501 2019-07-11 stsp unsigned char status;
3680 818c7501 2019-07-11 stsp struct stat sb;
3681 818c7501 2019-07-11 stsp char *ondisk_path;
3682 818c7501 2019-07-11 stsp
3683 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
3684 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3685 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
3686 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3687 818c7501 2019-07-11 stsp
3688 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3689 818c7501 2019-07-11 stsp == -1)
3690 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
3691 818c7501 2019-07-11 stsp
3692 818c7501 2019-07-11 stsp /* Reject rebase of a work tree with modified or conflicted files. */
3693 818c7501 2019-07-11 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3694 818c7501 2019-07-11 stsp free(ondisk_path);
3695 818c7501 2019-07-11 stsp if (err)
3696 818c7501 2019-07-11 stsp return err;
3697 818c7501 2019-07-11 stsp
3698 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
3699 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
3700 818c7501 2019-07-11 stsp
3701 818c7501 2019-07-11 stsp return NULL;
3702 818c7501 2019-07-11 stsp }
3703 818c7501 2019-07-11 stsp
3704 818c7501 2019-07-11 stsp const struct got_error *
3705 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3706 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3707 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
3708 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
3709 818c7501 2019-07-11 stsp {
3710 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3711 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3712 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
3713 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3714 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
3715 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3716 818c7501 2019-07-11 stsp
3717 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3718 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3719 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3720 818c7501 2019-07-11 stsp
3721 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3722 818c7501 2019-07-11 stsp if (err)
3723 818c7501 2019-07-11 stsp return err;
3724 818c7501 2019-07-11 stsp
3725 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
3726 818c7501 2019-07-11 stsp if (err)
3727 818c7501 2019-07-11 stsp goto done;
3728 818c7501 2019-07-11 stsp
3729 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
3730 818c7501 2019-07-11 stsp ok_arg.repo = repo;
3731 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3732 818c7501 2019-07-11 stsp &ok_arg);
3733 818c7501 2019-07-11 stsp if (err)
3734 818c7501 2019-07-11 stsp goto done;
3735 818c7501 2019-07-11 stsp
3736 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3737 818c7501 2019-07-11 stsp if (err)
3738 818c7501 2019-07-11 stsp goto done;
3739 818c7501 2019-07-11 stsp
3740 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3741 818c7501 2019-07-11 stsp if (err)
3742 818c7501 2019-07-11 stsp goto done;
3743 818c7501 2019-07-11 stsp
3744 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3745 818c7501 2019-07-11 stsp if (err)
3746 818c7501 2019-07-11 stsp goto done;
3747 818c7501 2019-07-11 stsp
3748 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3749 818c7501 2019-07-11 stsp 0);
3750 818c7501 2019-07-11 stsp if (err)
3751 818c7501 2019-07-11 stsp goto done;
3752 818c7501 2019-07-11 stsp
3753 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
3754 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
3755 818c7501 2019-07-11 stsp if (err)
3756 818c7501 2019-07-11 stsp goto done;
3757 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
3758 818c7501 2019-07-11 stsp if (err)
3759 818c7501 2019-07-11 stsp goto done;
3760 818c7501 2019-07-11 stsp
3761 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
3762 818c7501 2019-07-11 stsp
3763 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3764 818c7501 2019-07-11 stsp if (err)
3765 818c7501 2019-07-11 stsp goto done;
3766 818c7501 2019-07-11 stsp
3767 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
3768 818c7501 2019-07-11 stsp if (err)
3769 818c7501 2019-07-11 stsp goto done;
3770 818c7501 2019-07-11 stsp
3771 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
3772 818c7501 2019-07-11 stsp worktree->base_commit_id);
3773 818c7501 2019-07-11 stsp if (err)
3774 818c7501 2019-07-11 stsp goto done;
3775 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
3776 818c7501 2019-07-11 stsp if (err)
3777 818c7501 2019-07-11 stsp goto done;
3778 818c7501 2019-07-11 stsp
3779 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
3780 818c7501 2019-07-11 stsp if (err)
3781 818c7501 2019-07-11 stsp goto done;
3782 818c7501 2019-07-11 stsp done:
3783 818c7501 2019-07-11 stsp free(fileindex_path);
3784 818c7501 2019-07-11 stsp free(tmp_branch_name);
3785 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
3786 818c7501 2019-07-11 stsp free(branch_ref_name);
3787 818c7501 2019-07-11 stsp if (branch_ref)
3788 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3789 818c7501 2019-07-11 stsp if (wt_branch)
3790 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
3791 818c7501 2019-07-11 stsp if (err) {
3792 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
3793 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
3794 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3795 818c7501 2019-07-11 stsp }
3796 818c7501 2019-07-11 stsp if (*tmp_branch) {
3797 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3798 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3799 818c7501 2019-07-11 stsp }
3800 3e3a69f1 2019-07-25 stsp if (*fileindex) {
3801 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
3802 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3803 3e3a69f1 2019-07-25 stsp }
3804 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
3805 818c7501 2019-07-11 stsp }
3806 818c7501 2019-07-11 stsp return err;
3807 818c7501 2019-07-11 stsp }
3808 818c7501 2019-07-11 stsp
3809 818c7501 2019-07-11 stsp const struct got_error *
3810 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
3811 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3812 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
3813 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
3814 818c7501 2019-07-11 stsp {
3815 818c7501 2019-07-11 stsp const struct got_error *err;
3816 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3817 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3818 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3819 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
3820 818c7501 2019-07-11 stsp
3821 818c7501 2019-07-11 stsp *commit_id = NULL;
3822 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
3823 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
3824 3e3a69f1 2019-07-25 stsp *branch = NULL;
3825 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3826 3e3a69f1 2019-07-25 stsp
3827 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
3828 3e3a69f1 2019-07-25 stsp if (err)
3829 3e3a69f1 2019-07-25 stsp return err;
3830 818c7501 2019-07-11 stsp
3831 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
3832 3e3a69f1 2019-07-25 stsp if (err)
3833 3e3a69f1 2019-07-25 stsp goto done;
3834 3e3a69f1 2019-07-25 stsp
3835 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3836 818c7501 2019-07-11 stsp if (err)
3837 818c7501 2019-07-11 stsp return err;
3838 818c7501 2019-07-11 stsp
3839 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3840 818c7501 2019-07-11 stsp if (err)
3841 818c7501 2019-07-11 stsp goto done;
3842 818c7501 2019-07-11 stsp
3843 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3844 818c7501 2019-07-11 stsp if (err)
3845 818c7501 2019-07-11 stsp goto done;
3846 818c7501 2019-07-11 stsp
3847 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3848 818c7501 2019-07-11 stsp if (err)
3849 818c7501 2019-07-11 stsp goto done;
3850 818c7501 2019-07-11 stsp
3851 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3852 818c7501 2019-07-11 stsp if (err)
3853 818c7501 2019-07-11 stsp goto done;
3854 818c7501 2019-07-11 stsp
3855 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
3856 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
3857 818c7501 2019-07-11 stsp if (err)
3858 818c7501 2019-07-11 stsp goto done;
3859 818c7501 2019-07-11 stsp
3860 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3861 818c7501 2019-07-11 stsp if (err)
3862 818c7501 2019-07-11 stsp goto done;
3863 818c7501 2019-07-11 stsp
3864 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
3865 818c7501 2019-07-11 stsp if (err)
3866 818c7501 2019-07-11 stsp goto done;
3867 818c7501 2019-07-11 stsp
3868 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
3869 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
3870 818c7501 2019-07-11 stsp if (err)
3871 818c7501 2019-07-11 stsp goto done;
3872 818c7501 2019-07-11 stsp
3873 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3874 818c7501 2019-07-11 stsp if (err)
3875 818c7501 2019-07-11 stsp goto done;
3876 818c7501 2019-07-11 stsp done:
3877 818c7501 2019-07-11 stsp free(commit_ref_name);
3878 818c7501 2019-07-11 stsp free(branch_ref_name);
3879 3e3a69f1 2019-07-25 stsp free(fileindex_path);
3880 818c7501 2019-07-11 stsp if (commit_ref)
3881 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3882 818c7501 2019-07-11 stsp if (branch_ref)
3883 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3884 818c7501 2019-07-11 stsp if (err) {
3885 818c7501 2019-07-11 stsp free(*commit_id);
3886 818c7501 2019-07-11 stsp *commit_id = NULL;
3887 818c7501 2019-07-11 stsp if (*tmp_branch) {
3888 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3889 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3890 818c7501 2019-07-11 stsp }
3891 818c7501 2019-07-11 stsp if (*new_base_branch) {
3892 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
3893 818c7501 2019-07-11 stsp *new_base_branch = NULL;
3894 818c7501 2019-07-11 stsp }
3895 818c7501 2019-07-11 stsp if (*branch) {
3896 818c7501 2019-07-11 stsp got_ref_close(*branch);
3897 818c7501 2019-07-11 stsp *branch = NULL;
3898 818c7501 2019-07-11 stsp }
3899 3e3a69f1 2019-07-25 stsp if (*fileindex) {
3900 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
3901 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3902 3e3a69f1 2019-07-25 stsp }
3903 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
3904 818c7501 2019-07-11 stsp }
3905 818c7501 2019-07-11 stsp return err;
3906 c4296144 2019-05-09 stsp }
3907 818c7501 2019-07-11 stsp
3908 818c7501 2019-07-11 stsp const struct got_error *
3909 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3910 818c7501 2019-07-11 stsp {
3911 818c7501 2019-07-11 stsp const struct got_error *err;
3912 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
3913 818c7501 2019-07-11 stsp
3914 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3915 818c7501 2019-07-11 stsp if (err)
3916 818c7501 2019-07-11 stsp return err;
3917 818c7501 2019-07-11 stsp
3918 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3919 818c7501 2019-07-11 stsp free(tmp_branch_name);
3920 818c7501 2019-07-11 stsp return NULL;
3921 818c7501 2019-07-11 stsp }
3922 818c7501 2019-07-11 stsp
3923 818c7501 2019-07-11 stsp static const struct got_error *
3924 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3925 818c7501 2019-07-11 stsp char **logmsg, void *arg)
3926 818c7501 2019-07-11 stsp {
3927 0ebf8283 2019-07-24 stsp *logmsg = arg;
3928 818c7501 2019-07-11 stsp return NULL;
3929 818c7501 2019-07-11 stsp }
3930 818c7501 2019-07-11 stsp
3931 818c7501 2019-07-11 stsp static const struct got_error *
3932 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
3933 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3934 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3935 818c7501 2019-07-11 stsp {
3936 818c7501 2019-07-11 stsp return NULL;
3937 01757395 2019-07-12 stsp }
3938 01757395 2019-07-12 stsp
3939 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
3940 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
3941 01757395 2019-07-12 stsp void *progress_arg;
3942 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
3943 01757395 2019-07-12 stsp };
3944 01757395 2019-07-12 stsp
3945 01757395 2019-07-12 stsp static const struct got_error *
3946 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
3947 01757395 2019-07-12 stsp {
3948 01757395 2019-07-12 stsp const struct got_error *err;
3949 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
3950 01757395 2019-07-12 stsp char *p;
3951 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
3952 01757395 2019-07-12 stsp
3953 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
3954 01757395 2019-07-12 stsp if (err)
3955 01757395 2019-07-12 stsp return err;
3956 01757395 2019-07-12 stsp
3957 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
3958 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
3959 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
3960 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
3961 01757395 2019-07-12 stsp return NULL;
3962 01757395 2019-07-12 stsp
3963 01757395 2019-07-12 stsp p = strdup(path);
3964 01757395 2019-07-12 stsp if (p == NULL)
3965 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
3966 01757395 2019-07-12 stsp
3967 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3968 01757395 2019-07-12 stsp if (err || new == NULL)
3969 01757395 2019-07-12 stsp free(p);
3970 01757395 2019-07-12 stsp return err;
3971 01757395 2019-07-12 stsp }
3972 01757395 2019-07-12 stsp
3973 01757395 2019-07-12 stsp void
3974 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3975 01757395 2019-07-12 stsp {
3976 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
3977 01757395 2019-07-12 stsp
3978 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
3979 01757395 2019-07-12 stsp free((char *)pe->path);
3980 01757395 2019-07-12 stsp
3981 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
3982 818c7501 2019-07-11 stsp }
3983 818c7501 2019-07-11 stsp
3984 0ebf8283 2019-07-24 stsp static const struct got_error *
3985 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3986 0ebf8283 2019-07-24 stsp struct got_repository *repo)
3987 818c7501 2019-07-11 stsp {
3988 818c7501 2019-07-11 stsp const struct got_error *err;
3989 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
3990 818c7501 2019-07-11 stsp
3991 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3992 818c7501 2019-07-11 stsp if (err) {
3993 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
3994 818c7501 2019-07-11 stsp goto done;
3995 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3996 818c7501 2019-07-11 stsp if (err)
3997 818c7501 2019-07-11 stsp goto done;
3998 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
3999 818c7501 2019-07-11 stsp if (err)
4000 818c7501 2019-07-11 stsp goto done;
4001 818c7501 2019-07-11 stsp } else {
4002 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
4003 818c7501 2019-07-11 stsp int cmp;
4004 818c7501 2019-07-11 stsp
4005 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
4006 818c7501 2019-07-11 stsp if (err)
4007 818c7501 2019-07-11 stsp goto done;
4008 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
4009 818c7501 2019-07-11 stsp free(stored_id);
4010 818c7501 2019-07-11 stsp if (cmp != 0) {
4011 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4012 818c7501 2019-07-11 stsp goto done;
4013 818c7501 2019-07-11 stsp }
4014 818c7501 2019-07-11 stsp }
4015 0ebf8283 2019-07-24 stsp done:
4016 0ebf8283 2019-07-24 stsp if (commit_ref)
4017 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4018 0ebf8283 2019-07-24 stsp return err;
4019 0ebf8283 2019-07-24 stsp }
4020 0ebf8283 2019-07-24 stsp
4021 0ebf8283 2019-07-24 stsp static const struct got_error *
4022 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
4023 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
4024 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4025 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
4026 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4027 3e3a69f1 2019-07-25 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4028 0ebf8283 2019-07-24 stsp {
4029 0ebf8283 2019-07-24 stsp const struct got_error *err;
4030 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4031 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
4032 3e3a69f1 2019-07-25 stsp char *fileindex_path;
4033 818c7501 2019-07-11 stsp
4034 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4035 0ebf8283 2019-07-24 stsp
4036 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4037 0ebf8283 2019-07-24 stsp if (err)
4038 0ebf8283 2019-07-24 stsp return err;
4039 0ebf8283 2019-07-24 stsp
4040 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
4041 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
4042 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
4043 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
4044 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
4045 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
4046 818c7501 2019-07-11 stsp if (commit_ref)
4047 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
4048 818c7501 2019-07-11 stsp return err;
4049 818c7501 2019-07-11 stsp }
4050 818c7501 2019-07-11 stsp
4051 818c7501 2019-07-11 stsp const struct got_error *
4052 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4053 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4054 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4055 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4056 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4057 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4058 818c7501 2019-07-11 stsp {
4059 0ebf8283 2019-07-24 stsp const struct got_error *err;
4060 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4061 0ebf8283 2019-07-24 stsp
4062 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4063 0ebf8283 2019-07-24 stsp if (err)
4064 0ebf8283 2019-07-24 stsp return err;
4065 0ebf8283 2019-07-24 stsp
4066 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4067 0ebf8283 2019-07-24 stsp if (err)
4068 0ebf8283 2019-07-24 stsp goto done;
4069 0ebf8283 2019-07-24 stsp
4070 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4071 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4072 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4073 0ebf8283 2019-07-24 stsp done:
4074 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4075 0ebf8283 2019-07-24 stsp return err;
4076 0ebf8283 2019-07-24 stsp }
4077 0ebf8283 2019-07-24 stsp
4078 0ebf8283 2019-07-24 stsp const struct got_error *
4079 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4080 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4081 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4082 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4083 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4084 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4085 0ebf8283 2019-07-24 stsp {
4086 0ebf8283 2019-07-24 stsp const struct got_error *err;
4087 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4088 0ebf8283 2019-07-24 stsp
4089 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4090 0ebf8283 2019-07-24 stsp if (err)
4091 0ebf8283 2019-07-24 stsp return err;
4092 0ebf8283 2019-07-24 stsp
4093 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4094 0ebf8283 2019-07-24 stsp if (err)
4095 0ebf8283 2019-07-24 stsp goto done;
4096 0ebf8283 2019-07-24 stsp
4097 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4098 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4099 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4100 0ebf8283 2019-07-24 stsp done:
4101 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4102 0ebf8283 2019-07-24 stsp return err;
4103 0ebf8283 2019-07-24 stsp }
4104 0ebf8283 2019-07-24 stsp
4105 0ebf8283 2019-07-24 stsp static const struct got_error *
4106 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
4107 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4108 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4109 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4110 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
4111 0ebf8283 2019-07-24 stsp {
4112 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
4113 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4114 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4115 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4116 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
4117 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4118 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
4119 818c7501 2019-07-11 stsp
4120 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4121 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
4122 a0e95631 2019-07-12 stsp
4123 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4124 818c7501 2019-07-11 stsp
4125 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4126 a0e95631 2019-07-12 stsp if (err)
4127 3e3a69f1 2019-07-25 stsp return err;
4128 a0e95631 2019-07-12 stsp
4129 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4130 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
4131 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
4132 01757395 2019-07-12 stsp /*
4133 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
4134 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
4135 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
4136 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
4137 01757395 2019-07-12 stsp */
4138 01757395 2019-07-12 stsp if (merged_paths) {
4139 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4140 8f8646e5 2019-07-25 stsp if (TAILQ_EMPTY(merged_paths)) {
4141 8f8646e5 2019-07-25 stsp err = got_error(GOT_ERR_NO_MERGED_PATHS);
4142 8f8646e5 2019-07-25 stsp goto done;
4143 8f8646e5 2019-07-25 stsp }
4144 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
4145 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
4146 01757395 2019-07-12 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
4147 01757395 2019-07-12 stsp if (err)
4148 01757395 2019-07-12 stsp goto done;
4149 01757395 2019-07-12 stsp }
4150 01757395 2019-07-12 stsp } else {
4151 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4152 01757395 2019-07-12 stsp collect_commitables, &cc_arg, NULL, NULL);
4153 01757395 2019-07-12 stsp if (err)
4154 01757395 2019-07-12 stsp goto done;
4155 01757395 2019-07-12 stsp }
4156 a0e95631 2019-07-12 stsp
4157 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4158 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
4159 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4160 a0e95631 2019-07-12 stsp if (err)
4161 a0e95631 2019-07-12 stsp goto done;
4162 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4163 a0e95631 2019-07-12 stsp goto done;
4164 ff0d2220 2019-07-11 stsp }
4165 818c7501 2019-07-11 stsp
4166 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4167 edd02c5e 2019-07-11 stsp if (err)
4168 edd02c5e 2019-07-11 stsp goto done;
4169 edd02c5e 2019-07-11 stsp
4170 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4171 818c7501 2019-07-11 stsp if (err)
4172 818c7501 2019-07-11 stsp goto done;
4173 818c7501 2019-07-11 stsp
4174 0ebf8283 2019-07-24 stsp if (new_logmsg)
4175 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
4176 0ebf8283 2019-07-24 stsp else
4177 0ebf8283 2019-07-24 stsp logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4178 0ebf8283 2019-07-24 stsp if (logmsg == NULL)
4179 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
4180 0ebf8283 2019-07-24 stsp
4181 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4182 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
4183 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
4184 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4185 a0e95631 2019-07-12 stsp if (err)
4186 a0e95631 2019-07-12 stsp goto done;
4187 a0e95631 2019-07-12 stsp
4188 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
4189 a0e95631 2019-07-12 stsp if (err)
4190 a0e95631 2019-07-12 stsp goto done;
4191 a0e95631 2019-07-12 stsp
4192 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4193 a0e95631 2019-07-12 stsp if (err)
4194 a0e95631 2019-07-12 stsp goto done;
4195 a0e95631 2019-07-12 stsp
4196 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4197 93ec1b5f 2019-07-12 stsp fileindex);
4198 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4199 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
4200 a0e95631 2019-07-12 stsp err = sync_err;
4201 818c7501 2019-07-11 stsp done:
4202 a0e95631 2019-07-12 stsp free(fileindex_path);
4203 a0e95631 2019-07-12 stsp free(head_commit_id);
4204 a0e95631 2019-07-12 stsp if (head_ref)
4205 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
4206 818c7501 2019-07-11 stsp if (err) {
4207 818c7501 2019-07-11 stsp free(*new_commit_id);
4208 818c7501 2019-07-11 stsp *new_commit_id = NULL;
4209 818c7501 2019-07-11 stsp }
4210 818c7501 2019-07-11 stsp return err;
4211 818c7501 2019-07-11 stsp }
4212 818c7501 2019-07-11 stsp
4213 818c7501 2019-07-11 stsp const struct got_error *
4214 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4215 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4216 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4217 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4218 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
4219 0ebf8283 2019-07-24 stsp {
4220 0ebf8283 2019-07-24 stsp const struct got_error *err;
4221 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4222 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4223 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4224 0ebf8283 2019-07-24 stsp
4225 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4226 0ebf8283 2019-07-24 stsp if (err)
4227 0ebf8283 2019-07-24 stsp return err;
4228 0ebf8283 2019-07-24 stsp
4229 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4230 0ebf8283 2019-07-24 stsp if (err)
4231 0ebf8283 2019-07-24 stsp goto done;
4232 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4233 0ebf8283 2019-07-24 stsp if (err)
4234 0ebf8283 2019-07-24 stsp goto done;
4235 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4236 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4237 0ebf8283 2019-07-24 stsp goto done;
4238 0ebf8283 2019-07-24 stsp }
4239 0ebf8283 2019-07-24 stsp
4240 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4241 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4242 0ebf8283 2019-07-24 stsp done:
4243 0ebf8283 2019-07-24 stsp if (commit_ref)
4244 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4245 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4246 0ebf8283 2019-07-24 stsp free(commit_id);
4247 0ebf8283 2019-07-24 stsp return err;
4248 0ebf8283 2019-07-24 stsp }
4249 0ebf8283 2019-07-24 stsp
4250 0ebf8283 2019-07-24 stsp const struct got_error *
4251 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4252 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4253 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4254 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4255 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
4256 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4257 0ebf8283 2019-07-24 stsp {
4258 0ebf8283 2019-07-24 stsp const struct got_error *err;
4259 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4260 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4261 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4262 0ebf8283 2019-07-24 stsp
4263 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4264 0ebf8283 2019-07-24 stsp if (err)
4265 0ebf8283 2019-07-24 stsp return err;
4266 0ebf8283 2019-07-24 stsp
4267 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4268 0ebf8283 2019-07-24 stsp if (err)
4269 0ebf8283 2019-07-24 stsp goto done;
4270 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4271 0ebf8283 2019-07-24 stsp if (err)
4272 0ebf8283 2019-07-24 stsp goto done;
4273 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4274 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4275 0ebf8283 2019-07-24 stsp goto done;
4276 0ebf8283 2019-07-24 stsp }
4277 0ebf8283 2019-07-24 stsp
4278 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4279 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4280 0ebf8283 2019-07-24 stsp done:
4281 0ebf8283 2019-07-24 stsp if (commit_ref)
4282 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4283 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4284 0ebf8283 2019-07-24 stsp free(commit_id);
4285 0ebf8283 2019-07-24 stsp return err;
4286 0ebf8283 2019-07-24 stsp }
4287 0ebf8283 2019-07-24 stsp
4288 0ebf8283 2019-07-24 stsp const struct got_error *
4289 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
4290 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4291 818c7501 2019-07-11 stsp {
4292 3e3a69f1 2019-07-25 stsp if (fileindex)
4293 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4294 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
4295 69844fba 2019-07-11 stsp }
4296 69844fba 2019-07-11 stsp
4297 69844fba 2019-07-11 stsp static const struct got_error *
4298 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
4299 69844fba 2019-07-11 stsp {
4300 69844fba 2019-07-11 stsp const struct got_error *err;
4301 69844fba 2019-07-11 stsp struct got_reference *ref;
4302 69844fba 2019-07-11 stsp
4303 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
4304 69844fba 2019-07-11 stsp if (err) {
4305 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
4306 69844fba 2019-07-11 stsp return NULL;
4307 69844fba 2019-07-11 stsp return err;
4308 69844fba 2019-07-11 stsp }
4309 69844fba 2019-07-11 stsp
4310 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
4311 69844fba 2019-07-11 stsp got_ref_close(ref);
4312 69844fba 2019-07-11 stsp return err;
4313 818c7501 2019-07-11 stsp }
4314 818c7501 2019-07-11 stsp
4315 69844fba 2019-07-11 stsp static const struct got_error *
4316 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4317 69844fba 2019-07-11 stsp {
4318 69844fba 2019-07-11 stsp const struct got_error *err;
4319 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4320 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4321 69844fba 2019-07-11 stsp
4322 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4323 69844fba 2019-07-11 stsp if (err)
4324 69844fba 2019-07-11 stsp goto done;
4325 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
4326 69844fba 2019-07-11 stsp if (err)
4327 69844fba 2019-07-11 stsp goto done;
4328 69844fba 2019-07-11 stsp
4329 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4330 69844fba 2019-07-11 stsp if (err)
4331 69844fba 2019-07-11 stsp goto done;
4332 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
4333 69844fba 2019-07-11 stsp if (err)
4334 69844fba 2019-07-11 stsp goto done;
4335 69844fba 2019-07-11 stsp
4336 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4337 69844fba 2019-07-11 stsp if (err)
4338 69844fba 2019-07-11 stsp goto done;
4339 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
4340 69844fba 2019-07-11 stsp if (err)
4341 69844fba 2019-07-11 stsp goto done;
4342 69844fba 2019-07-11 stsp
4343 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4344 69844fba 2019-07-11 stsp if (err)
4345 69844fba 2019-07-11 stsp goto done;
4346 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
4347 69844fba 2019-07-11 stsp if (err)
4348 69844fba 2019-07-11 stsp goto done;
4349 69844fba 2019-07-11 stsp
4350 69844fba 2019-07-11 stsp done:
4351 69844fba 2019-07-11 stsp free(tmp_branch_name);
4352 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
4353 69844fba 2019-07-11 stsp free(branch_ref_name);
4354 69844fba 2019-07-11 stsp free(commit_ref_name);
4355 69844fba 2019-07-11 stsp return err;
4356 69844fba 2019-07-11 stsp }
4357 69844fba 2019-07-11 stsp
4358 818c7501 2019-07-11 stsp const struct got_error *
4359 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
4360 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4361 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4362 818c7501 2019-07-11 stsp struct got_repository *repo)
4363 818c7501 2019-07-11 stsp {
4364 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
4365 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
4366 818c7501 2019-07-11 stsp
4367 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4368 818c7501 2019-07-11 stsp if (err)
4369 818c7501 2019-07-11 stsp return err;
4370 818c7501 2019-07-11 stsp
4371 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4372 818c7501 2019-07-11 stsp if (err)
4373 818c7501 2019-07-11 stsp goto done;
4374 818c7501 2019-07-11 stsp
4375 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
4376 818c7501 2019-07-11 stsp if (err)
4377 818c7501 2019-07-11 stsp goto done;
4378 818c7501 2019-07-11 stsp
4379 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
4380 818c7501 2019-07-11 stsp if (err)
4381 818c7501 2019-07-11 stsp goto done;
4382 818c7501 2019-07-11 stsp
4383 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
4384 818c7501 2019-07-11 stsp done:
4385 3e3a69f1 2019-07-25 stsp if (fileindex)
4386 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4387 818c7501 2019-07-11 stsp free(new_head_commit_id);
4388 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4389 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
4390 818c7501 2019-07-11 stsp err = unlockerr;
4391 818c7501 2019-07-11 stsp return err;
4392 818c7501 2019-07-11 stsp }
4393 818c7501 2019-07-11 stsp
4394 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg {
4395 818c7501 2019-07-11 stsp struct got_pathlist_head *revertible_paths;
4396 818c7501 2019-07-11 stsp struct got_worktree *worktree;
4397 818c7501 2019-07-11 stsp };
4398 818c7501 2019-07-11 stsp
4399 818c7501 2019-07-11 stsp static const struct got_error *
4400 88d0e355 2019-08-03 stsp collect_revertible_paths(void *arg, unsigned char status,
4401 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4402 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4403 537ac44b 2019-08-03 stsp struct got_object_id *commit_id)
4404 818c7501 2019-07-11 stsp {
4405 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg *a = arg;
4406 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4407 818c7501 2019-07-11 stsp struct got_pathlist_entry *new = NULL;
4408 818c7501 2019-07-11 stsp char *path = NULL;
4409 818c7501 2019-07-11 stsp
4410 818c7501 2019-07-11 stsp if (status != GOT_STATUS_ADD &&
4411 818c7501 2019-07-11 stsp status != GOT_STATUS_DELETE &&
4412 818c7501 2019-07-11 stsp status != GOT_STATUS_MODIFY &&
4413 818c7501 2019-07-11 stsp status != GOT_STATUS_CONFLICT &&
4414 818c7501 2019-07-11 stsp status != GOT_STATUS_MISSING)
4415 818c7501 2019-07-11 stsp return NULL;
4416 818c7501 2019-07-11 stsp
4417 818c7501 2019-07-11 stsp if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4418 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
4419 818c7501 2019-07-11 stsp
4420 818c7501 2019-07-11 stsp err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4421 818c7501 2019-07-11 stsp if (err || new == NULL)
4422 818c7501 2019-07-11 stsp free(path);
4423 818c7501 2019-07-11 stsp return err;
4424 818c7501 2019-07-11 stsp }
4425 818c7501 2019-07-11 stsp
4426 818c7501 2019-07-11 stsp const struct got_error *
4427 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
4428 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4429 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
4430 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4431 818c7501 2019-07-11 stsp {
4432 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
4433 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
4434 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
4435 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
4436 818c7501 2019-07-11 stsp struct got_pathlist_head revertible_paths;
4437 818c7501 2019-07-11 stsp struct got_pathlist_entry *pe;
4438 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg crp_arg;
4439 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
4440 818c7501 2019-07-11 stsp
4441 818c7501 2019-07-11 stsp TAILQ_INIT(&revertible_paths);
4442 818c7501 2019-07-11 stsp
4443 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
4444 818c7501 2019-07-11 stsp if (err)
4445 818c7501 2019-07-11 stsp return err;
4446 818c7501 2019-07-11 stsp
4447 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
4448 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
4449 818c7501 2019-07-11 stsp if (err)
4450 818c7501 2019-07-11 stsp goto done;
4451 818c7501 2019-07-11 stsp
4452 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
4453 818c7501 2019-07-11 stsp if (err)
4454 818c7501 2019-07-11 stsp goto done;
4455 818c7501 2019-07-11 stsp
4456 818c7501 2019-07-11 stsp /*
4457 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
4458 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
4459 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
4460 818c7501 2019-07-11 stsp */
4461 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
4462 818c7501 2019-07-11 stsp if (err)
4463 818c7501 2019-07-11 stsp goto done;
4464 818c7501 2019-07-11 stsp
4465 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4466 818c7501 2019-07-11 stsp if (err)
4467 818c7501 2019-07-11 stsp goto done;
4468 818c7501 2019-07-11 stsp
4469 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
4470 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
4471 ca355955 2019-07-12 stsp if (err)
4472 ca355955 2019-07-12 stsp goto done;
4473 ca355955 2019-07-12 stsp
4474 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
4475 ca355955 2019-07-12 stsp if (err)
4476 ca355955 2019-07-12 stsp goto done;
4477 ca355955 2019-07-12 stsp
4478 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4479 818c7501 2019-07-11 stsp if (err)
4480 818c7501 2019-07-11 stsp goto done;
4481 818c7501 2019-07-11 stsp
4482 347d1d3e 2019-07-12 stsp crp_arg.revertible_paths = &revertible_paths;
4483 347d1d3e 2019-07-12 stsp crp_arg.worktree = worktree;
4484 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4485 347d1d3e 2019-07-12 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
4486 55bd499d 2019-07-12 stsp if (err)
4487 55bd499d 2019-07-12 stsp goto done;
4488 55bd499d 2019-07-12 stsp
4489 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
4490 818c7501 2019-07-11 stsp err = revert_file(worktree, fileindex, pe->path,
4491 818c7501 2019-07-11 stsp progress_cb, progress_arg, repo);
4492 818c7501 2019-07-11 stsp if (err)
4493 ca355955 2019-07-12 stsp goto sync;
4494 818c7501 2019-07-11 stsp }
4495 69844fba 2019-07-11 stsp
4496 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4497 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
4498 ca355955 2019-07-12 stsp sync:
4499 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4500 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
4501 ca355955 2019-07-12 stsp err = sync_err;
4502 818c7501 2019-07-11 stsp done:
4503 818c7501 2019-07-11 stsp got_ref_close(resolved);
4504 a3a2faf2 2019-07-12 stsp free(tree_id);
4505 818c7501 2019-07-11 stsp free(commit_id);
4506 0ebf8283 2019-07-24 stsp if (fileindex)
4507 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
4508 0ebf8283 2019-07-24 stsp free(fileindex_path);
4509 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
4510 0ebf8283 2019-07-24 stsp free((char *)pe->path);
4511 0ebf8283 2019-07-24 stsp got_pathlist_free(&revertible_paths);
4512 0ebf8283 2019-07-24 stsp
4513 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4514 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
4515 0ebf8283 2019-07-24 stsp err = unlockerr;
4516 0ebf8283 2019-07-24 stsp return err;
4517 0ebf8283 2019-07-24 stsp }
4518 0ebf8283 2019-07-24 stsp
4519 0ebf8283 2019-07-24 stsp const struct got_error *
4520 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4521 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4522 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
4523 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4524 0ebf8283 2019-07-24 stsp {
4525 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4526 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
4527 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
4528 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
4529 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4530 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
4531 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
4532 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
4533 0ebf8283 2019-07-24 stsp
4534 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4535 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
4536 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4537 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4538 0ebf8283 2019-07-24 stsp
4539 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
4540 0ebf8283 2019-07-24 stsp if (err)
4541 0ebf8283 2019-07-24 stsp return err;
4542 0ebf8283 2019-07-24 stsp
4543 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4544 0ebf8283 2019-07-24 stsp if (err)
4545 0ebf8283 2019-07-24 stsp goto done;
4546 0ebf8283 2019-07-24 stsp
4547 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
4548 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
4549 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4550 0ebf8283 2019-07-24 stsp &ok_arg);
4551 0ebf8283 2019-07-24 stsp if (err)
4552 0ebf8283 2019-07-24 stsp goto done;
4553 0ebf8283 2019-07-24 stsp
4554 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4555 0ebf8283 2019-07-24 stsp if (err)
4556 0ebf8283 2019-07-24 stsp goto done;
4557 0ebf8283 2019-07-24 stsp
4558 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4559 0ebf8283 2019-07-24 stsp if (err)
4560 0ebf8283 2019-07-24 stsp goto done;
4561 0ebf8283 2019-07-24 stsp
4562 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4563 0ebf8283 2019-07-24 stsp worktree);
4564 0ebf8283 2019-07-24 stsp if (err)
4565 0ebf8283 2019-07-24 stsp goto done;
4566 0ebf8283 2019-07-24 stsp
4567 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4568 0ebf8283 2019-07-24 stsp 0);
4569 0ebf8283 2019-07-24 stsp if (err)
4570 0ebf8283 2019-07-24 stsp goto done;
4571 0ebf8283 2019-07-24 stsp
4572 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4573 0ebf8283 2019-07-24 stsp if (err)
4574 0ebf8283 2019-07-24 stsp goto done;
4575 0ebf8283 2019-07-24 stsp
4576 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
4577 0ebf8283 2019-07-24 stsp if (err)
4578 0ebf8283 2019-07-24 stsp goto done;
4579 0ebf8283 2019-07-24 stsp
4580 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4581 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
4582 0ebf8283 2019-07-24 stsp if (err)
4583 0ebf8283 2019-07-24 stsp goto done;
4584 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
4585 0ebf8283 2019-07-24 stsp if (err)
4586 0ebf8283 2019-07-24 stsp goto done;
4587 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4588 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
4589 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
4590 0ebf8283 2019-07-24 stsp goto done;
4591 0ebf8283 2019-07-24 stsp }
4592 0ebf8283 2019-07-24 stsp
4593 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
4594 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
4595 0ebf8283 2019-07-24 stsp if (err)
4596 0ebf8283 2019-07-24 stsp goto done;
4597 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
4598 0ebf8283 2019-07-24 stsp if (err)
4599 0ebf8283 2019-07-24 stsp goto done;
4600 0ebf8283 2019-07-24 stsp
4601 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
4602 0ebf8283 2019-07-24 stsp if (err)
4603 0ebf8283 2019-07-24 stsp goto done;
4604 0ebf8283 2019-07-24 stsp done:
4605 0ebf8283 2019-07-24 stsp free(fileindex_path);
4606 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4607 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4608 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
4609 0ebf8283 2019-07-24 stsp if (wt_branch)
4610 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
4611 0ebf8283 2019-07-24 stsp if (err) {
4612 0ebf8283 2019-07-24 stsp if (*branch_ref) {
4613 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
4614 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
4615 0ebf8283 2019-07-24 stsp }
4616 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
4617 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
4618 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4619 0ebf8283 2019-07-24 stsp }
4620 0ebf8283 2019-07-24 stsp free(*base_commit_id);
4621 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4622 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4623 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4624 3e3a69f1 2019-07-25 stsp }
4625 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
4626 0ebf8283 2019-07-24 stsp }
4627 0ebf8283 2019-07-24 stsp return err;
4628 0ebf8283 2019-07-24 stsp }
4629 0ebf8283 2019-07-24 stsp
4630 0ebf8283 2019-07-24 stsp const struct got_error *
4631 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
4632 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4633 0ebf8283 2019-07-24 stsp {
4634 3e3a69f1 2019-07-25 stsp if (fileindex)
4635 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4636 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
4637 0ebf8283 2019-07-24 stsp }
4638 0ebf8283 2019-07-24 stsp
4639 0ebf8283 2019-07-24 stsp const struct got_error *
4640 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
4641 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
4642 0ebf8283 2019-07-24 stsp {
4643 0ebf8283 2019-07-24 stsp const struct got_error *err;
4644 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
4645 0ebf8283 2019-07-24 stsp
4646 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4647 0ebf8283 2019-07-24 stsp if (err)
4648 0ebf8283 2019-07-24 stsp return err;
4649 0ebf8283 2019-07-24 stsp
4650 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4651 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4652 0ebf8283 2019-07-24 stsp return NULL;
4653 0ebf8283 2019-07-24 stsp }
4654 0ebf8283 2019-07-24 stsp
4655 0ebf8283 2019-07-24 stsp const struct got_error *
4656 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
4657 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
4658 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4659 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4660 0ebf8283 2019-07-24 stsp {
4661 0ebf8283 2019-07-24 stsp const struct got_error *err;
4662 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4663 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4664 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4665 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
4666 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
4667 0ebf8283 2019-07-24 stsp
4668 0ebf8283 2019-07-24 stsp *commit_id = NULL;
4669 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4670 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4671 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4672 0ebf8283 2019-07-24 stsp
4673 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
4674 3e3a69f1 2019-07-25 stsp if (err)
4675 3e3a69f1 2019-07-25 stsp return err;
4676 3e3a69f1 2019-07-25 stsp
4677 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4678 3e3a69f1 2019-07-25 stsp if (err)
4679 3e3a69f1 2019-07-25 stsp goto done;
4680 3e3a69f1 2019-07-25 stsp
4681 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4682 0ebf8283 2019-07-24 stsp if (err)
4683 0ebf8283 2019-07-24 stsp return err;
4684 0ebf8283 2019-07-24 stsp
4685 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4686 0ebf8283 2019-07-24 stsp if (err)
4687 0ebf8283 2019-07-24 stsp goto done;
4688 0ebf8283 2019-07-24 stsp
4689 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4690 0ebf8283 2019-07-24 stsp if (err)
4691 0ebf8283 2019-07-24 stsp goto done;
4692 0ebf8283 2019-07-24 stsp
4693 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4694 0ebf8283 2019-07-24 stsp worktree);
4695 0ebf8283 2019-07-24 stsp if (err)
4696 0ebf8283 2019-07-24 stsp goto done;
4697 0ebf8283 2019-07-24 stsp
4698 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4699 0ebf8283 2019-07-24 stsp if (err)
4700 0ebf8283 2019-07-24 stsp goto done;
4701 0ebf8283 2019-07-24 stsp
4702 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4703 0ebf8283 2019-07-24 stsp if (err)
4704 0ebf8283 2019-07-24 stsp goto done;
4705 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
4706 0ebf8283 2019-07-24 stsp if (err)
4707 0ebf8283 2019-07-24 stsp goto done;
4708 0ebf8283 2019-07-24 stsp
4709 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4710 0ebf8283 2019-07-24 stsp if (err)
4711 0ebf8283 2019-07-24 stsp goto done;
4712 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4713 0ebf8283 2019-07-24 stsp if (err)
4714 0ebf8283 2019-07-24 stsp goto done;
4715 0ebf8283 2019-07-24 stsp
4716 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4717 0ebf8283 2019-07-24 stsp if (err)
4718 0ebf8283 2019-07-24 stsp goto done;
4719 0ebf8283 2019-07-24 stsp done:
4720 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4721 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4722 3e3a69f1 2019-07-25 stsp free(fileindex_path);
4723 0ebf8283 2019-07-24 stsp if (commit_ref)
4724 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4725 0ebf8283 2019-07-24 stsp if (base_commit_ref)
4726 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
4727 0ebf8283 2019-07-24 stsp if (err) {
4728 0ebf8283 2019-07-24 stsp free(*commit_id);
4729 0ebf8283 2019-07-24 stsp *commit_id = NULL;
4730 0ebf8283 2019-07-24 stsp free(*base_commit_id);
4731 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4732 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
4733 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
4734 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4735 0ebf8283 2019-07-24 stsp }
4736 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4737 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4738 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4739 3e3a69f1 2019-07-25 stsp }
4740 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
4741 0ebf8283 2019-07-24 stsp }
4742 0ebf8283 2019-07-24 stsp return err;
4743 0ebf8283 2019-07-24 stsp }
4744 0ebf8283 2019-07-24 stsp
4745 0ebf8283 2019-07-24 stsp static const struct got_error *
4746 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4747 0ebf8283 2019-07-24 stsp {
4748 0ebf8283 2019-07-24 stsp const struct got_error *err;
4749 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4750 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4751 0ebf8283 2019-07-24 stsp
4752 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4753 0ebf8283 2019-07-24 stsp if (err)
4754 0ebf8283 2019-07-24 stsp goto done;
4755 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
4756 0ebf8283 2019-07-24 stsp if (err)
4757 0ebf8283 2019-07-24 stsp goto done;
4758 0ebf8283 2019-07-24 stsp
4759 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4760 0ebf8283 2019-07-24 stsp worktree);
4761 0ebf8283 2019-07-24 stsp if (err)
4762 0ebf8283 2019-07-24 stsp goto done;
4763 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
4764 0ebf8283 2019-07-24 stsp if (err)
4765 0ebf8283 2019-07-24 stsp goto done;
4766 0ebf8283 2019-07-24 stsp
4767 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4768 0ebf8283 2019-07-24 stsp if (err)
4769 0ebf8283 2019-07-24 stsp goto done;
4770 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
4771 0ebf8283 2019-07-24 stsp if (err)
4772 0ebf8283 2019-07-24 stsp goto done;
4773 0ebf8283 2019-07-24 stsp
4774 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4775 0ebf8283 2019-07-24 stsp if (err)
4776 0ebf8283 2019-07-24 stsp goto done;
4777 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
4778 0ebf8283 2019-07-24 stsp if (err)
4779 0ebf8283 2019-07-24 stsp goto done;
4780 0ebf8283 2019-07-24 stsp done:
4781 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4782 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
4783 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4784 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4785 0ebf8283 2019-07-24 stsp return err;
4786 0ebf8283 2019-07-24 stsp }
4787 0ebf8283 2019-07-24 stsp
4788 0ebf8283 2019-07-24 stsp const struct got_error *
4789 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
4790 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4791 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
4792 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4793 0ebf8283 2019-07-24 stsp {
4794 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
4795 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
4796 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4797 0ebf8283 2019-07-24 stsp struct got_pathlist_head revertible_paths;
4798 0ebf8283 2019-07-24 stsp struct got_pathlist_entry *pe;
4799 0ebf8283 2019-07-24 stsp struct collect_revertible_paths_arg crp_arg;
4800 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
4801 0ebf8283 2019-07-24 stsp
4802 0ebf8283 2019-07-24 stsp TAILQ_INIT(&revertible_paths);
4803 0ebf8283 2019-07-24 stsp
4804 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
4805 0ebf8283 2019-07-24 stsp if (err)
4806 0ebf8283 2019-07-24 stsp return err;
4807 0ebf8283 2019-07-24 stsp
4808 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
4809 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
4810 0ebf8283 2019-07-24 stsp if (err)
4811 0ebf8283 2019-07-24 stsp goto done;
4812 0ebf8283 2019-07-24 stsp
4813 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
4814 0ebf8283 2019-07-24 stsp if (err)
4815 0ebf8283 2019-07-24 stsp goto done;
4816 0ebf8283 2019-07-24 stsp
4817 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4818 0ebf8283 2019-07-24 stsp if (err)
4819 0ebf8283 2019-07-24 stsp goto done;
4820 0ebf8283 2019-07-24 stsp
4821 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4822 0ebf8283 2019-07-24 stsp worktree->path_prefix);
4823 0ebf8283 2019-07-24 stsp if (err)
4824 0ebf8283 2019-07-24 stsp goto done;
4825 0ebf8283 2019-07-24 stsp
4826 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
4827 0ebf8283 2019-07-24 stsp if (err)
4828 0ebf8283 2019-07-24 stsp goto done;
4829 0ebf8283 2019-07-24 stsp
4830 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4831 0ebf8283 2019-07-24 stsp if (err)
4832 0ebf8283 2019-07-24 stsp goto done;
4833 0ebf8283 2019-07-24 stsp
4834 0ebf8283 2019-07-24 stsp crp_arg.revertible_paths = &revertible_paths;
4835 0ebf8283 2019-07-24 stsp crp_arg.worktree = worktree;
4836 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
4837 0ebf8283 2019-07-24 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
4838 0ebf8283 2019-07-24 stsp if (err)
4839 0ebf8283 2019-07-24 stsp goto done;
4840 0ebf8283 2019-07-24 stsp
4841 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
4842 0ebf8283 2019-07-24 stsp err = revert_file(worktree, fileindex, pe->path,
4843 0ebf8283 2019-07-24 stsp progress_cb, progress_arg, repo);
4844 0ebf8283 2019-07-24 stsp if (err)
4845 0ebf8283 2019-07-24 stsp goto sync;
4846 0ebf8283 2019-07-24 stsp }
4847 0ebf8283 2019-07-24 stsp
4848 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4849 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
4850 0ebf8283 2019-07-24 stsp sync:
4851 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4852 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
4853 0ebf8283 2019-07-24 stsp err = sync_err;
4854 0ebf8283 2019-07-24 stsp done:
4855 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
4856 0ebf8283 2019-07-24 stsp free(tree_id);
4857 818c7501 2019-07-11 stsp free(fileindex_path);
4858 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
4859 818c7501 2019-07-11 stsp free((char *)pe->path);
4860 818c7501 2019-07-11 stsp got_pathlist_free(&revertible_paths);
4861 818c7501 2019-07-11 stsp
4862 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4863 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
4864 818c7501 2019-07-11 stsp err = unlockerr;
4865 818c7501 2019-07-11 stsp return err;
4866 818c7501 2019-07-11 stsp }
4867 0ebf8283 2019-07-24 stsp
4868 0ebf8283 2019-07-24 stsp const struct got_error *
4869 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
4870 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4871 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
4872 0ebf8283 2019-07-24 stsp {
4873 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
4874 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
4875 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
4876 0ebf8283 2019-07-24 stsp
4877 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4878 0ebf8283 2019-07-24 stsp if (err)
4879 0ebf8283 2019-07-24 stsp return err;
4880 0ebf8283 2019-07-24 stsp
4881 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
4882 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
4883 0ebf8283 2019-07-24 stsp if (err)
4884 0ebf8283 2019-07-24 stsp goto done;
4885 0ebf8283 2019-07-24 stsp
4886 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
4887 0ebf8283 2019-07-24 stsp if (err)
4888 0ebf8283 2019-07-24 stsp goto done;
4889 0ebf8283 2019-07-24 stsp
4890 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
4891 0ebf8283 2019-07-24 stsp if (err)
4892 0ebf8283 2019-07-24 stsp goto done;
4893 0ebf8283 2019-07-24 stsp
4894 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
4895 0ebf8283 2019-07-24 stsp if (err)
4896 0ebf8283 2019-07-24 stsp goto done;
4897 0ebf8283 2019-07-24 stsp
4898 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
4899 0ebf8283 2019-07-24 stsp done:
4900 3e3a69f1 2019-07-25 stsp if (fileindex)
4901 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4902 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
4903 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4904 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
4905 0ebf8283 2019-07-24 stsp err = unlockerr;
4906 0ebf8283 2019-07-24 stsp return err;
4907 0ebf8283 2019-07-24 stsp }
4908 0ebf8283 2019-07-24 stsp
4909 0ebf8283 2019-07-24 stsp const struct got_error *
4910 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4911 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4912 0ebf8283 2019-07-24 stsp {
4913 0ebf8283 2019-07-24 stsp const struct got_error *err;
4914 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4915 0ebf8283 2019-07-24 stsp
4916 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4917 0ebf8283 2019-07-24 stsp if (err)
4918 0ebf8283 2019-07-24 stsp return err;
4919 0ebf8283 2019-07-24 stsp
4920 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4921 0ebf8283 2019-07-24 stsp if (err)
4922 0ebf8283 2019-07-24 stsp goto done;
4923 0ebf8283 2019-07-24 stsp
4924 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
4925 0ebf8283 2019-07-24 stsp done:
4926 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4927 0cb83759 2019-08-03 stsp return err;
4928 0cb83759 2019-08-03 stsp }
4929 0cb83759 2019-08-03 stsp
4930 0cb83759 2019-08-03 stsp static const struct got_error *
4931 42005733 2019-08-03 stsp stage_path(const char *relpath, const char *ondisk_path,
4932 42005733 2019-08-03 stsp const char *path_content, struct got_worktree *worktree,
4933 42005733 2019-08-03 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4934 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg)
4935 0cb83759 2019-08-03 stsp {
4936 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
4937 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
4938 d3e7c587 2019-08-03 stsp unsigned char status, staged_status;
4939 0cb83759 2019-08-03 stsp struct stat sb;
4940 537ac44b 2019-08-03 stsp struct got_object_id blob_id, *staged_blob_id = NULL;
4941 0cb83759 2019-08-03 stsp uint32_t stage;
4942 0cb83759 2019-08-03 stsp
4943 42005733 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
4944 d3e7c587 2019-08-03 stsp if (ie == NULL)
4945 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4946 0cb83759 2019-08-03 stsp
4947 0cb83759 2019-08-03 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
4948 0cb83759 2019-08-03 stsp if (err)
4949 d3e7c587 2019-08-03 stsp return err;
4950 d3e7c587 2019-08-03 stsp staged_status = get_staged_status(ie);
4951 0cb83759 2019-08-03 stsp
4952 0cb83759 2019-08-03 stsp switch (status) {
4953 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
4954 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
4955 537ac44b 2019-08-03 stsp err = got_object_blob_create(&staged_blob_id,
4956 fdfa9bf2 2019-08-03 stsp path_content ? path_content : ondisk_path, repo);
4957 0cb83759 2019-08-03 stsp if (err)
4958 0cb83759 2019-08-03 stsp goto done;
4959 537ac44b 2019-08-03 stsp memcpy(&blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
4960 537ac44b 2019-08-03 stsp memcpy(ie->staged_blob_sha1, staged_blob_id->sha1,
4961 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4962 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
4963 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
4964 0cb83759 2019-08-03 stsp else
4965 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
4966 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
4967 537ac44b 2019-08-03 stsp err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4968 537ac44b 2019-08-03 stsp get_staged_status(ie), relpath, &blob_id,
4969 537ac44b 2019-08-03 stsp staged_blob_id, NULL);
4970 0cb83759 2019-08-03 stsp break;
4971 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
4972 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4973 d3e7c587 2019-08-03 stsp break;
4974 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
4975 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
4976 537ac44b 2019-08-03 stsp err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4977 537ac44b 2019-08-03 stsp get_staged_status(ie), relpath, NULL, NULL, NULL);
4978 0cb83759 2019-08-03 stsp break;
4979 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
4980 d3e7c587 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
4981 d3e7c587 2019-08-03 stsp break;
4982 0cb83759 2019-08-03 stsp default:
4983 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4984 537ac44b 2019-08-03 stsp break;
4985 0cb83759 2019-08-03 stsp }
4986 0cb83759 2019-08-03 stsp done:
4987 537ac44b 2019-08-03 stsp free(staged_blob_id);
4988 0ebf8283 2019-07-24 stsp return err;
4989 0ebf8283 2019-07-24 stsp }
4990 0cb83759 2019-08-03 stsp
4991 0cb83759 2019-08-03 stsp const struct got_error *
4992 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
4993 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
4994 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
4995 1e71573e 2019-08-03 stsp struct got_repository *repo)
4996 0cb83759 2019-08-03 stsp {
4997 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4998 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
4999 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
5000 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
5001 0cb83759 2019-08-03 stsp
5002 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
5003 0cb83759 2019-08-03 stsp if (err)
5004 0cb83759 2019-08-03 stsp return err;
5005 0cb83759 2019-08-03 stsp
5006 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5007 0cb83759 2019-08-03 stsp if (err)
5008 0cb83759 2019-08-03 stsp goto done;
5009 0cb83759 2019-08-03 stsp
5010 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5011 42005733 2019-08-03 stsp char *relpath;
5012 42005733 2019-08-03 stsp err = got_path_skip_common_ancestor(&relpath,
5013 42005733 2019-08-03 stsp got_worktree_get_root_path(worktree), pe->path);
5014 0cb83759 2019-08-03 stsp if (err)
5015 0cb83759 2019-08-03 stsp break;
5016 42005733 2019-08-03 stsp err = stage_path(relpath, pe->path,
5017 42005733 2019-08-03 stsp (const char *)pe->data, worktree, fileindex, repo,
5018 42005733 2019-08-03 stsp status_cb, status_arg);
5019 42005733 2019-08-03 stsp free(relpath);
5020 42005733 2019-08-03 stsp if (err)
5021 42005733 2019-08-03 stsp break;
5022 0cb83759 2019-08-03 stsp }
5023 0cb83759 2019-08-03 stsp
5024 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5025 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
5026 0cb83759 2019-08-03 stsp err = sync_err;
5027 0cb83759 2019-08-03 stsp done:
5028 0cb83759 2019-08-03 stsp free(fileindex_path);
5029 0cb83759 2019-08-03 stsp if (fileindex)
5030 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
5031 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5032 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
5033 0cb83759 2019-08-03 stsp err = unlockerr;
5034 0cb83759 2019-08-03 stsp return err;
5035 0cb83759 2019-08-03 stsp }