Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 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 86c3caaf 2018-03-09 stsp
19 d1f6d47b 2019-02-04 stsp #include <dirent.h>
20 12ce7a6c 2019-08-12 stsp #include <limits.h>
21 f5c49f82 2019-01-06 stsp #include <stddef.h>
22 86c3caaf 2018-03-09 stsp #include <string.h>
23 86c3caaf 2018-03-09 stsp #include <stdio.h>
24 86c3caaf 2018-03-09 stsp #include <stdlib.h>
25 303e14b5 2019-09-23 stsp #include <time.h>
26 86c3caaf 2018-03-09 stsp #include <fcntl.h>
27 86c3caaf 2018-03-09 stsp #include <errno.h>
28 86c3caaf 2018-03-09 stsp #include <unistd.h>
29 9d31a1d8 2018-03-11 stsp #include <zlib.h>
30 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
31 512f0d0e 2019-01-02 stsp #include <libgen.h>
32 92a9e85d 2021-09-24 thomas #include <uuid.h>
33 dd038bc6 2021-09-21 thomas.ad
34 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
35 86c3caaf 2018-03-09 stsp
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.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 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
57 9d31a1d8 2018-03-11 stsp
58 9d31a1d8 2018-03-11 stsp #ifndef MIN
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 9d31a1d8 2018-03-11 stsp #endif
61 f69721c3 2019-10-21 stsp
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 86c3caaf 2018-03-09 stsp
65 99724ed4 2018-03-10 stsp static const struct got_error *
66 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
67 99724ed4 2018-03-10 stsp {
68 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
69 99724ed4 2018-03-10 stsp char *path;
70 99724ed4 2018-03-10 stsp
71 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
73 99724ed4 2018-03-10 stsp
74 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
75 99724ed4 2018-03-10 stsp free(path);
76 507dc3bb 2018-12-29 stsp return err;
77 507dc3bb 2018-12-29 stsp }
78 507dc3bb 2018-12-29 stsp
79 507dc3bb 2018-12-29 stsp static const struct got_error *
80 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
81 507dc3bb 2018-12-29 stsp {
82 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
83 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
84 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
85 507dc3bb 2018-12-29 stsp char *path = NULL;
86 507dc3bb 2018-12-29 stsp
87 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
89 507dc3bb 2018-12-29 stsp path = NULL;
90 507dc3bb 2018-12-29 stsp goto done;
91 507dc3bb 2018-12-29 stsp }
92 507dc3bb 2018-12-29 stsp
93 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
94 507dc3bb 2018-12-29 stsp if (err)
95 507dc3bb 2018-12-29 stsp goto done;
96 507dc3bb 2018-12-29 stsp
97 507dc3bb 2018-12-29 stsp if (content) {
98 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
99 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
100 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
101 507dc3bb 2018-12-29 stsp goto done;
102 507dc3bb 2018-12-29 stsp }
103 507dc3bb 2018-12-29 stsp }
104 507dc3bb 2018-12-29 stsp
105 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
106 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
107 2a57020b 2019-02-20 stsp unlink(tmppath);
108 507dc3bb 2018-12-29 stsp goto done;
109 507dc3bb 2018-12-29 stsp }
110 507dc3bb 2018-12-29 stsp
111 507dc3bb 2018-12-29 stsp done:
112 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
113 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
114 230a42bd 2019-05-11 jcs free(tmppath);
115 99724ed4 2018-03-10 stsp return err;
116 99724ed4 2018-03-10 stsp }
117 99724ed4 2018-03-10 stsp
118 09fe317a 2018-03-11 stsp static const struct got_error *
119 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
120 09fe317a 2018-03-11 stsp {
121 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
122 09fe317a 2018-03-11 stsp char *path;
123 09fe317a 2018-03-11 stsp int fd = -1;
124 09fe317a 2018-03-11 stsp ssize_t n;
125 09fe317a 2018-03-11 stsp struct stat sb;
126 09fe317a 2018-03-11 stsp
127 09fe317a 2018-03-11 stsp *content = NULL;
128 09fe317a 2018-03-11 stsp
129 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
130 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
131 09fe317a 2018-03-11 stsp path = NULL;
132 09fe317a 2018-03-11 stsp goto done;
133 09fe317a 2018-03-11 stsp }
134 09fe317a 2018-03-11 stsp
135 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
136 09fe317a 2018-03-11 stsp if (fd == -1) {
137 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
138 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
139 f02eaa22 2019-03-11 stsp else
140 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
141 ef99fdb1 2018-03-11 stsp goto done;
142 ef99fdb1 2018-03-11 stsp }
143 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
144 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
145 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
146 09fe317a 2018-03-11 stsp goto done;
147 09fe317a 2018-03-11 stsp }
148 09fe317a 2018-03-11 stsp
149 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
150 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
151 d10c9b58 2019-02-19 stsp goto done;
152 d10c9b58 2019-02-19 stsp }
153 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
154 09fe317a 2018-03-11 stsp if (*content == NULL) {
155 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
156 09fe317a 2018-03-11 stsp goto done;
157 09fe317a 2018-03-11 stsp }
158 09fe317a 2018-03-11 stsp
159 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
160 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
161 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
162 a2e6d162 2019-07-27 stsp 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 if ((*content)[sb.st_size - 1] != '\n') {
166 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
167 09fe317a 2018-03-11 stsp goto done;
168 09fe317a 2018-03-11 stsp }
169 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
170 09fe317a 2018-03-11 stsp
171 09fe317a 2018-03-11 stsp done:
172 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
173 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
174 09fe317a 2018-03-11 stsp free(path);
175 09fe317a 2018-03-11 stsp if (err) {
176 09fe317a 2018-03-11 stsp free(*content);
177 09fe317a 2018-03-11 stsp *content = NULL;
178 09fe317a 2018-03-11 stsp }
179 09fe317a 2018-03-11 stsp return err;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp
182 024e9686 2019-05-14 stsp static const struct got_error *
183 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
184 024e9686 2019-05-14 stsp {
185 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
186 024e9686 2019-05-14 stsp char *refstr = NULL;
187 024e9686 2019-05-14 stsp
188 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
189 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
190 024e9686 2019-05-14 stsp if (refstr == NULL)
191 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
192 024e9686 2019-05-14 stsp } else {
193 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
194 024e9686 2019-05-14 stsp if (refstr == NULL)
195 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
196 024e9686 2019-05-14 stsp }
197 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
198 024e9686 2019-05-14 stsp free(refstr);
199 024e9686 2019-05-14 stsp return err;
200 024e9686 2019-05-14 stsp }
201 024e9686 2019-05-14 stsp
202 86c3caaf 2018-03-09 stsp const struct got_error *
203 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
204 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
205 86c3caaf 2018-03-09 stsp {
206 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
207 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
208 ec22038e 2019-03-10 stsp uuid_t uuid;
209 ec22038e 2019-03-10 stsp uint32_t uuid_status;
210 65596e15 2018-12-24 stsp int obj_type;
211 7ac97322 2018-03-11 stsp char *path_got = NULL;
212 1451e70d 2018-03-10 stsp char *formatstr = NULL;
213 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
214 65596e15 2018-12-24 stsp char *basestr = NULL;
215 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
216 65596e15 2018-12-24 stsp
217 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
218 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
219 0c48fee2 2019-03-11 stsp goto done;
220 0c48fee2 2019-03-11 stsp }
221 0c48fee2 2019-03-11 stsp
222 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
223 65596e15 2018-12-24 stsp if (err)
224 65596e15 2018-12-24 stsp return err;
225 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
226 65596e15 2018-12-24 stsp if (err)
227 65596e15 2018-12-24 stsp return err;
228 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
229 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
230 86c3caaf 2018-03-09 stsp
231 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
232 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
233 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
234 0bb8a95e 2018-03-12 stsp }
235 577ec78f 2018-03-11 stsp
236 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
237 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
238 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
239 86c3caaf 2018-03-09 stsp goto done;
240 86c3caaf 2018-03-09 stsp }
241 86c3caaf 2018-03-09 stsp
242 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
243 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
245 86c3caaf 2018-03-09 stsp goto done;
246 86c3caaf 2018-03-09 stsp }
247 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
248 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
249 86c3caaf 2018-03-09 stsp goto done;
250 86c3caaf 2018-03-09 stsp }
251 86c3caaf 2018-03-09 stsp
252 056e7441 2018-03-11 stsp /* Create an empty lock file. */
253 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
254 056e7441 2018-03-11 stsp if (err)
255 056e7441 2018-03-11 stsp goto done;
256 056e7441 2018-03-11 stsp
257 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
258 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
259 99724ed4 2018-03-10 stsp if (err)
260 86c3caaf 2018-03-09 stsp goto done;
261 86c3caaf 2018-03-09 stsp
262 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
263 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
264 65596e15 2018-12-24 stsp if (err)
265 65596e15 2018-12-24 stsp goto done;
266 65596e15 2018-12-24 stsp
267 65596e15 2018-12-24 stsp /* Record our base commit. */
268 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
269 65596e15 2018-12-24 stsp if (err)
270 65596e15 2018-12-24 stsp goto done;
271 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
272 99724ed4 2018-03-10 stsp if (err)
273 86c3caaf 2018-03-09 stsp goto done;
274 86c3caaf 2018-03-09 stsp
275 1451e70d 2018-03-10 stsp /* Store path to repository. */
276 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
277 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
278 99724ed4 2018-03-10 stsp if (err)
279 86c3caaf 2018-03-09 stsp goto done;
280 86c3caaf 2018-03-09 stsp
281 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
282 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
283 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
284 ec22038e 2019-03-10 stsp if (err)
285 ec22038e 2019-03-10 stsp goto done;
286 ec22038e 2019-03-10 stsp
287 ec22038e 2019-03-10 stsp /* Generate UUID. */
288 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
289 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
290 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
291 ec22038e 2019-03-10 stsp goto done;
292 ec22038e 2019-03-10 stsp }
293 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
294 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
295 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
296 ec22038e 2019-03-10 stsp goto done;
297 ec22038e 2019-03-10 stsp }
298 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
299 577ec78f 2018-03-11 stsp if (err)
300 577ec78f 2018-03-11 stsp goto done;
301 577ec78f 2018-03-11 stsp
302 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
303 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
304 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
305 1451e70d 2018-03-10 stsp goto done;
306 1451e70d 2018-03-10 stsp }
307 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
308 99724ed4 2018-03-10 stsp if (err)
309 1451e70d 2018-03-10 stsp goto done;
310 1451e70d 2018-03-10 stsp
311 86c3caaf 2018-03-09 stsp done:
312 65596e15 2018-12-24 stsp free(commit_id);
313 7ac97322 2018-03-11 stsp free(path_got);
314 1451e70d 2018-03-10 stsp free(formatstr);
315 0bb8a95e 2018-03-12 stsp free(absprefix);
316 65596e15 2018-12-24 stsp free(basestr);
317 ec22038e 2019-03-10 stsp free(uuidstr);
318 86c3caaf 2018-03-09 stsp return err;
319 86c3caaf 2018-03-09 stsp }
320 86c3caaf 2018-03-09 stsp
321 247140b2 2019-02-05 stsp static const struct got_error *
322 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
323 86c3caaf 2018-03-09 stsp {
324 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
325 7ac97322 2018-03-11 stsp char *path_got;
326 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
327 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
328 056e7441 2018-03-11 stsp char *path_lock = NULL;
329 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
330 6d9d28c3 2018-03-11 stsp int version, fd = -1;
331 6d9d28c3 2018-03-11 stsp const char *errstr;
332 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
333 c442a90d 2019-03-10 stsp uint32_t uuid_status;
334 6d9d28c3 2018-03-11 stsp
335 6d9d28c3 2018-03-11 stsp *worktree = NULL;
336 6d9d28c3 2018-03-11 stsp
337 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
338 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
339 7ac97322 2018-03-11 stsp path_got = NULL;
340 6d9d28c3 2018-03-11 stsp goto done;
341 6d9d28c3 2018-03-11 stsp }
342 6d9d28c3 2018-03-11 stsp
343 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
344 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
345 056e7441 2018-03-11 stsp path_lock = NULL;
346 6d9d28c3 2018-03-11 stsp goto done;
347 6d9d28c3 2018-03-11 stsp }
348 6d9d28c3 2018-03-11 stsp
349 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
350 6d9d28c3 2018-03-11 stsp if (fd == -1) {
351 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
352 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
353 6d9d28c3 2018-03-11 stsp goto done;
354 6d9d28c3 2018-03-11 stsp }
355 6d9d28c3 2018-03-11 stsp
356 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
357 6d9d28c3 2018-03-11 stsp if (err)
358 6d9d28c3 2018-03-11 stsp goto done;
359 6d9d28c3 2018-03-11 stsp
360 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
361 6d9d28c3 2018-03-11 stsp if (errstr) {
362 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
363 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
364 6d9d28c3 2018-03-11 stsp goto done;
365 6d9d28c3 2018-03-11 stsp }
366 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
367 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
368 6d9d28c3 2018-03-11 stsp goto done;
369 6d9d28c3 2018-03-11 stsp }
370 6d9d28c3 2018-03-11 stsp
371 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
372 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
373 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
374 6d9d28c3 2018-03-11 stsp goto done;
375 6d9d28c3 2018-03-11 stsp }
376 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
377 6d9d28c3 2018-03-11 stsp
378 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
379 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
380 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
381 6d9d28c3 2018-03-11 stsp goto done;
382 6d9d28c3 2018-03-11 stsp }
383 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
384 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
385 6d9d28c3 2018-03-11 stsp if (err)
386 6d9d28c3 2018-03-11 stsp goto done;
387 eaccb85f 2018-12-25 stsp
388 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
389 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
390 93a30277 2018-12-24 stsp if (err)
391 93a30277 2018-12-24 stsp goto done;
392 93a30277 2018-12-24 stsp
393 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
394 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
395 c442a90d 2019-03-10 stsp if (err)
396 c442a90d 2019-03-10 stsp goto done;
397 c442a90d 2019-03-10 stsp
398 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
399 eaccb85f 2018-12-25 stsp if (err)
400 c442a90d 2019-03-10 stsp goto done;
401 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
402 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
403 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
404 eaccb85f 2018-12-25 stsp goto done;
405 c442a90d 2019-03-10 stsp }
406 eaccb85f 2018-12-25 stsp
407 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
408 eaccb85f 2018-12-25 stsp if (err)
409 eaccb85f 2018-12-25 stsp goto done;
410 eaccb85f 2018-12-25 stsp
411 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
412 eaccb85f 2018-12-25 stsp base_commit_id_str);
413 f5baf295 2018-03-11 stsp if (err)
414 6d9d28c3 2018-03-11 stsp goto done;
415 6d9d28c3 2018-03-11 stsp
416 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
417 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
418 50b0790e 2020-09-11 stsp if (err)
419 50b0790e 2020-09-11 stsp goto done;
420 50b0790e 2020-09-11 stsp
421 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
422 50b0790e 2020-09-11 stsp (*worktree)->root_path,
423 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
424 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
425 50b0790e 2020-09-11 stsp goto done;
426 50b0790e 2020-09-11 stsp }
427 50b0790e 2020-09-11 stsp
428 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
429 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
430 437adc9d 2020-12-10 yzhong
431 437adc9d 2020-12-10 yzhong (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
432 437adc9d 2020-12-10 yzhong if ((*worktree)->root_fd == -1) {
433 437adc9d 2020-12-10 yzhong err = got_error_from_errno2("open", (*worktree)->root_path);
434 437adc9d 2020-12-10 yzhong goto done;
435 437adc9d 2020-12-10 yzhong }
436 6d9d28c3 2018-03-11 stsp done:
437 1d0f4054 2021-06-17 stsp if (repo) {
438 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
439 1d0f4054 2021-06-17 stsp if (err == NULL)
440 1d0f4054 2021-06-17 stsp err = close_err;
441 1d0f4054 2021-06-17 stsp }
442 7ac97322 2018-03-11 stsp free(path_got);
443 056e7441 2018-03-11 stsp free(path_lock);
444 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
445 c442a90d 2019-03-10 stsp free(uuidstr);
446 bd165944 2019-03-10 stsp free(formatstr);
447 6d9d28c3 2018-03-11 stsp if (err) {
448 6d9d28c3 2018-03-11 stsp if (fd != -1)
449 6d9d28c3 2018-03-11 stsp close(fd);
450 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
451 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
452 6d9d28c3 2018-03-11 stsp *worktree = NULL;
453 6d9d28c3 2018-03-11 stsp } else
454 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
455 6d9d28c3 2018-03-11 stsp
456 6d9d28c3 2018-03-11 stsp return err;
457 86c3caaf 2018-03-09 stsp }
458 247140b2 2019-02-05 stsp
459 247140b2 2019-02-05 stsp const struct got_error *
460 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
461 247140b2 2019-02-05 stsp {
462 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
463 aedea96d 2020-10-20 stsp char *worktree_path;
464 86c3caaf 2018-03-09 stsp
465 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
466 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
467 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
468 aedea96d 2020-10-20 stsp
469 aedea96d 2020-10-20 stsp for (;;) {
470 aedea96d 2020-10-20 stsp char *parent_path;
471 aedea96d 2020-10-20 stsp
472 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
473 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
474 aedea96d 2020-10-20 stsp free(worktree_path);
475 247140b2 2019-02-05 stsp return err;
476 aedea96d 2020-10-20 stsp }
477 aedea96d 2020-10-20 stsp if (*worktree) {
478 aedea96d 2020-10-20 stsp free(worktree_path);
479 aedea96d 2020-10-20 stsp return NULL;
480 aedea96d 2020-10-20 stsp }
481 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
482 aedea96d 2020-10-20 stsp break;
483 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
484 aedea96d 2020-10-20 stsp if (err) {
485 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
486 aedea96d 2020-10-20 stsp free(worktree_path);
487 aedea96d 2020-10-20 stsp return err;
488 aedea96d 2020-10-20 stsp }
489 aedea96d 2020-10-20 stsp break;
490 aedea96d 2020-10-20 stsp }
491 aedea96d 2020-10-20 stsp free(worktree_path);
492 aedea96d 2020-10-20 stsp worktree_path = parent_path;
493 aedea96d 2020-10-20 stsp }
494 247140b2 2019-02-05 stsp
495 aedea96d 2020-10-20 stsp free(worktree_path);
496 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
497 247140b2 2019-02-05 stsp }
498 247140b2 2019-02-05 stsp
499 3a6ce05a 2019-02-11 stsp const struct got_error *
500 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
501 86c3caaf 2018-03-09 stsp {
502 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
503 60e40e95 2021-01-22 stsp
504 a6b21eef 2021-01-21 stsp if (worktree->lockfd != -1) {
505 08578a35 2021-01-22 stsp if (close(worktree->lockfd) == -1)
506 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
507 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
508 a6b21eef 2021-01-21 stsp }
509 e7abd6b6 2021-01-22 stsp if (close(worktree->root_fd) == -1 && err == NULL)
510 e7abd6b6 2021-01-22 stsp err = got_error_from_errno2("close",
511 e7abd6b6 2021-01-22 stsp got_worktree_get_root_path(worktree));
512 60e40e95 2021-01-22 stsp free(worktree->repo_path);
513 60e40e95 2021-01-22 stsp free(worktree->path_prefix);
514 60e40e95 2021-01-22 stsp free(worktree->base_commit_id);
515 60e40e95 2021-01-22 stsp free(worktree->head_ref_name);
516 60e40e95 2021-01-22 stsp free(worktree->root_path);
517 60e40e95 2021-01-22 stsp free(worktree->gotconfig_path);
518 60e40e95 2021-01-22 stsp got_gotconfig_free(worktree->gotconfig);
519 a06ff56f 2021-01-21 naddy free(worktree);
520 3a6ce05a 2019-02-11 stsp return err;
521 86c3caaf 2018-03-09 stsp }
522 86c3caaf 2018-03-09 stsp
523 2fbdb5ae 2018-12-29 stsp const char *
524 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
525 c7f4312f 2019-02-05 stsp {
526 c7f4312f 2019-02-05 stsp return worktree->root_path;
527 c7f4312f 2019-02-05 stsp }
528 c7f4312f 2019-02-05 stsp
529 c7f4312f 2019-02-05 stsp const char *
530 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
531 86c3caaf 2018-03-09 stsp {
532 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
533 49520a32 2018-12-29 stsp }
534 49520a32 2018-12-29 stsp const char *
535 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
536 49520a32 2018-12-29 stsp {
537 f609be2e 2018-12-29 stsp return worktree->path_prefix;
538 e5dc7198 2018-12-29 stsp }
539 e5dc7198 2018-12-29 stsp
540 e5dc7198 2018-12-29 stsp const struct got_error *
541 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
542 e5dc7198 2018-12-29 stsp const char *path_prefix)
543 e5dc7198 2018-12-29 stsp {
544 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
545 e5dc7198 2018-12-29 stsp
546 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
547 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
548 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
549 e5dc7198 2018-12-29 stsp }
550 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
551 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
552 e5dc7198 2018-12-29 stsp free(absprefix);
553 e5dc7198 2018-12-29 stsp return NULL;
554 86c3caaf 2018-03-09 stsp }
555 86c3caaf 2018-03-09 stsp
556 bc70eb79 2019-05-09 stsp const char *
557 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
558 86c3caaf 2018-03-09 stsp {
559 36a38700 2019-05-10 stsp return worktree->head_ref_name;
560 024e9686 2019-05-14 stsp }
561 024e9686 2019-05-14 stsp
562 024e9686 2019-05-14 stsp const struct got_error *
563 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
564 024e9686 2019-05-14 stsp struct got_reference *head_ref)
565 024e9686 2019-05-14 stsp {
566 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
567 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
568 024e9686 2019-05-14 stsp
569 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
570 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
571 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
572 024e9686 2019-05-14 stsp path_got = NULL;
573 024e9686 2019-05-14 stsp goto done;
574 024e9686 2019-05-14 stsp }
575 024e9686 2019-05-14 stsp
576 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
577 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
578 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
579 024e9686 2019-05-14 stsp goto done;
580 024e9686 2019-05-14 stsp }
581 024e9686 2019-05-14 stsp
582 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
583 024e9686 2019-05-14 stsp if (err)
584 024e9686 2019-05-14 stsp goto done;
585 024e9686 2019-05-14 stsp
586 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
587 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
588 024e9686 2019-05-14 stsp done:
589 024e9686 2019-05-14 stsp free(path_got);
590 024e9686 2019-05-14 stsp if (err)
591 024e9686 2019-05-14 stsp free(head_ref_name);
592 024e9686 2019-05-14 stsp return err;
593 507dc3bb 2018-12-29 stsp }
594 507dc3bb 2018-12-29 stsp
595 b72f483a 2019-02-05 stsp struct got_object_id *
596 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
597 507dc3bb 2018-12-29 stsp {
598 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
599 86c3caaf 2018-03-09 stsp }
600 86c3caaf 2018-03-09 stsp
601 507dc3bb 2018-12-29 stsp const struct got_error *
602 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
603 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
604 507dc3bb 2018-12-29 stsp {
605 507dc3bb 2018-12-29 stsp const struct got_error *err;
606 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
607 507dc3bb 2018-12-29 stsp char *id_str = NULL;
608 507dc3bb 2018-12-29 stsp char *path_got = NULL;
609 507dc3bb 2018-12-29 stsp
610 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
611 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
612 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
613 507dc3bb 2018-12-29 stsp path_got = NULL;
614 507dc3bb 2018-12-29 stsp goto done;
615 507dc3bb 2018-12-29 stsp }
616 507dc3bb 2018-12-29 stsp
617 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
618 507dc3bb 2018-12-29 stsp if (err)
619 507dc3bb 2018-12-29 stsp return err;
620 507dc3bb 2018-12-29 stsp
621 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
622 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
623 507dc3bb 2018-12-29 stsp goto done;
624 507dc3bb 2018-12-29 stsp }
625 507dc3bb 2018-12-29 stsp
626 507dc3bb 2018-12-29 stsp /* Record our base commit. */
627 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
628 507dc3bb 2018-12-29 stsp if (err)
629 507dc3bb 2018-12-29 stsp goto done;
630 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
631 507dc3bb 2018-12-29 stsp if (err)
632 507dc3bb 2018-12-29 stsp goto done;
633 507dc3bb 2018-12-29 stsp
634 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
635 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
636 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
637 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
638 507dc3bb 2018-12-29 stsp goto done;
639 507dc3bb 2018-12-29 stsp }
640 507dc3bb 2018-12-29 stsp done:
641 507dc3bb 2018-12-29 stsp if (obj)
642 507dc3bb 2018-12-29 stsp got_object_close(obj);
643 507dc3bb 2018-12-29 stsp free(id_str);
644 507dc3bb 2018-12-29 stsp free(path_got);
645 507dc3bb 2018-12-29 stsp return err;
646 50b0790e 2020-09-11 stsp }
647 50b0790e 2020-09-11 stsp
648 50b0790e 2020-09-11 stsp const struct got_gotconfig *
649 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
650 50b0790e 2020-09-11 stsp {
651 50b0790e 2020-09-11 stsp return worktree->gotconfig;
652 507dc3bb 2018-12-29 stsp }
653 507dc3bb 2018-12-29 stsp
654 9d31a1d8 2018-03-11 stsp static const struct got_error *
655 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
656 86c3caaf 2018-03-09 stsp {
657 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
658 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
659 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
660 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
661 86c3caaf 2018-03-09 stsp return NULL;
662 21908da4 2019-01-13 stsp }
663 21908da4 2019-01-13 stsp
664 21908da4 2019-01-13 stsp static const struct got_error *
665 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
666 4a1ddfc2 2019-01-12 stsp {
667 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
668 4a1ddfc2 2019-01-12 stsp char *abspath;
669 4a1ddfc2 2019-01-12 stsp
670 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
671 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
672 4a1ddfc2 2019-01-12 stsp
673 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
674 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
675 ddcd8544 2019-03-11 stsp struct stat sb;
676 ddcd8544 2019-03-11 stsp err = NULL;
677 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
678 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
679 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
680 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
681 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
682 ddcd8544 2019-03-11 stsp }
683 ddcd8544 2019-03-11 stsp }
684 4a1ddfc2 2019-01-12 stsp free(abspath);
685 68c76935 2019-02-19 stsp return err;
686 68c76935 2019-02-19 stsp }
687 68c76935 2019-02-19 stsp
688 68c76935 2019-02-19 stsp static const struct got_error *
689 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
690 68c76935 2019-02-19 stsp {
691 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
692 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
693 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
694 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
695 68c76935 2019-02-19 stsp
696 68c76935 2019-02-19 stsp *same = 1;
697 68c76935 2019-02-19 stsp
698 230a42bd 2019-05-11 jcs for (;;) {
699 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
700 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
701 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
702 68c76935 2019-02-19 stsp break;
703 68c76935 2019-02-19 stsp }
704 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
705 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
706 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
707 68c76935 2019-02-19 stsp break;
708 68c76935 2019-02-19 stsp }
709 68c76935 2019-02-19 stsp if (flen1 == 0) {
710 68c76935 2019-02-19 stsp if (flen2 != 0)
711 68c76935 2019-02-19 stsp *same = 0;
712 68c76935 2019-02-19 stsp break;
713 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
714 68c76935 2019-02-19 stsp if (flen1 != 0)
715 68c76935 2019-02-19 stsp *same = 0;
716 68c76935 2019-02-19 stsp break;
717 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
718 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
719 68c76935 2019-02-19 stsp *same = 0;
720 68c76935 2019-02-19 stsp break;
721 68c76935 2019-02-19 stsp }
722 68c76935 2019-02-19 stsp } else {
723 68c76935 2019-02-19 stsp *same = 0;
724 68c76935 2019-02-19 stsp break;
725 68c76935 2019-02-19 stsp }
726 68c76935 2019-02-19 stsp }
727 68c76935 2019-02-19 stsp
728 68c76935 2019-02-19 stsp return err;
729 68c76935 2019-02-19 stsp }
730 68c76935 2019-02-19 stsp
731 68c76935 2019-02-19 stsp static const struct got_error *
732 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
733 68c76935 2019-02-19 stsp {
734 68c76935 2019-02-19 stsp struct stat sb;
735 68c76935 2019-02-19 stsp size_t size1, size2;
736 68c76935 2019-02-19 stsp
737 68c76935 2019-02-19 stsp *same = 1;
738 68c76935 2019-02-19 stsp
739 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
740 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
741 68c76935 2019-02-19 stsp size1 = sb.st_size;
742 68c76935 2019-02-19 stsp
743 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
744 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
745 68c76935 2019-02-19 stsp size2 = sb.st_size;
746 68c76935 2019-02-19 stsp
747 68c76935 2019-02-19 stsp if (size1 != size2) {
748 68c76935 2019-02-19 stsp *same = 0;
749 68c76935 2019-02-19 stsp return NULL;
750 68c76935 2019-02-19 stsp }
751 68c76935 2019-02-19 stsp
752 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
753 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
754 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
755 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
756 68c76935 2019-02-19 stsp
757 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
758 6353ad76 2019-02-08 stsp }
759 6353ad76 2019-02-08 stsp
760 6353ad76 2019-02-08 stsp /*
761 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
762 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
763 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
764 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
765 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
766 6353ad76 2019-02-08 stsp */
767 6353ad76 2019-02-08 stsp static const struct got_error *
768 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
769 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
770 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
771 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
772 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
773 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
774 6353ad76 2019-02-08 stsp {
775 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
776 6353ad76 2019-02-08 stsp int merged_fd = -1;
777 db590691 2021-06-02 stsp FILE *f_merged = NULL;
778 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
779 234035bc 2019-06-01 stsp int overlapcnt = 0;
780 3524bbf9 2020-10-20 stsp char *parent = NULL;
781 6353ad76 2019-02-08 stsp
782 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
783 234035bc 2019-06-01 stsp
784 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
785 3524bbf9 2020-10-20 stsp if (err)
786 3524bbf9 2020-10-20 stsp return err;
787 af54ae4a 2019-02-19 stsp
788 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
789 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
790 3524bbf9 2020-10-20 stsp goto done;
791 3524bbf9 2020-10-20 stsp }
792 af54ae4a 2019-02-19 stsp
793 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
794 6353ad76 2019-02-08 stsp if (err)
795 6353ad76 2019-02-08 stsp goto done;
796 3b9f0f87 2020-07-23 stsp
797 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
798 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
799 6353ad76 2019-02-08 stsp if (err)
800 6353ad76 2019-02-08 stsp goto done;
801 6353ad76 2019-02-08 stsp
802 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
803 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
804 1ee397ad 2019-07-12 stsp if (err)
805 1ee397ad 2019-07-12 stsp goto done;
806 6353ad76 2019-02-08 stsp
807 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
808 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
809 816dc654 2019-02-16 stsp goto done;
810 68c76935 2019-02-19 stsp }
811 68c76935 2019-02-19 stsp
812 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
813 db590691 2021-06-02 stsp if (f_merged == NULL) {
814 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
815 db590691 2021-06-02 stsp goto done;
816 db590691 2021-06-02 stsp }
817 db590691 2021-06-02 stsp merged_fd = -1;
818 db590691 2021-06-02 stsp
819 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
820 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
821 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
822 db590691 2021-06-02 stsp f_merged);
823 68c76935 2019-02-19 stsp if (err)
824 68c76935 2019-02-19 stsp goto done;
825 816dc654 2019-02-16 stsp }
826 6353ad76 2019-02-08 stsp
827 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
828 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
829 70a0c8ec 2019-02-20 stsp goto done;
830 70a0c8ec 2019-02-20 stsp }
831 70a0c8ec 2019-02-20 stsp
832 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
833 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
834 230a42bd 2019-05-11 jcs ondisk_path);
835 6353ad76 2019-02-08 stsp goto done;
836 6353ad76 2019-02-08 stsp }
837 6353ad76 2019-02-08 stsp done:
838 fdcb7daf 2019-12-15 stsp if (err) {
839 fdcb7daf 2019-12-15 stsp if (merged_path)
840 fdcb7daf 2019-12-15 stsp unlink(merged_path);
841 3b9f0f87 2020-07-23 stsp }
842 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
843 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
844 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
845 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
846 6353ad76 2019-02-08 stsp free(merged_path);
847 af54ae4a 2019-02-19 stsp free(base_path);
848 3524bbf9 2020-10-20 stsp free(parent);
849 af57b12a 2020-07-23 stsp return err;
850 af57b12a 2020-07-23 stsp }
851 af57b12a 2020-07-23 stsp
852 af57b12a 2020-07-23 stsp static const struct got_error *
853 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
854 af57b12a 2020-07-23 stsp size_t target_len)
855 af57b12a 2020-07-23 stsp {
856 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
857 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
858 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
859 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
860 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
861 af57b12a 2020-07-23 stsp ondisk_path);
862 af57b12a 2020-07-23 stsp return NULL;
863 af57b12a 2020-07-23 stsp }
864 af57b12a 2020-07-23 stsp
865 af57b12a 2020-07-23 stsp /*
866 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
867 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
868 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
869 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
870 993e2a1b 2020-07-23 stsp *
871 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
872 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
873 993e2a1b 2020-07-23 stsp *
874 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
875 993e2a1b 2020-07-23 stsp * has deleted this symlink.
876 11cc08c1 2020-07-23 stsp */
877 11cc08c1 2020-07-23 stsp static const struct got_error *
878 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
879 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
880 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
881 11cc08c1 2020-07-23 stsp {
882 11cc08c1 2020-07-23 stsp const struct got_error *err;
883 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
884 11cc08c1 2020-07-23 stsp FILE *f = NULL;
885 11cc08c1 2020-07-23 stsp
886 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
887 11cc08c1 2020-07-23 stsp if (err)
888 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
889 11cc08c1 2020-07-23 stsp
890 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
891 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
892 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
893 11cc08c1 2020-07-23 stsp goto done;
894 11cc08c1 2020-07-23 stsp }
895 11cc08c1 2020-07-23 stsp
896 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
897 11cc08c1 2020-07-23 stsp if (err)
898 3818e3c4 2020-11-01 naddy goto done;
899 3818e3c4 2020-11-01 naddy
900 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
901 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
902 11cc08c1 2020-07-23 stsp goto done;
903 3818e3c4 2020-11-01 naddy }
904 11cc08c1 2020-07-23 stsp
905 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
906 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
907 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
908 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
909 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
910 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
911 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
912 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
913 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
914 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
915 11cc08c1 2020-07-23 stsp goto done;
916 11cc08c1 2020-07-23 stsp }
917 11cc08c1 2020-07-23 stsp
918 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
919 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
920 11cc08c1 2020-07-23 stsp goto done;
921 11cc08c1 2020-07-23 stsp }
922 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
923 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
924 11cc08c1 2020-07-23 stsp goto done;
925 11cc08c1 2020-07-23 stsp }
926 11cc08c1 2020-07-23 stsp done:
927 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
928 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
929 11cc08c1 2020-07-23 stsp free(path);
930 11cc08c1 2020-07-23 stsp free(id_str);
931 11cc08c1 2020-07-23 stsp free(label_deriv);
932 11cc08c1 2020-07-23 stsp return err;
933 11cc08c1 2020-07-23 stsp }
934 d219f183 2020-07-23 stsp
935 d219f183 2020-07-23 stsp /* forward declaration */
936 d219f183 2020-07-23 stsp static const struct got_error *
937 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
938 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
939 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
940 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
941 11cc08c1 2020-07-23 stsp
942 11cc08c1 2020-07-23 stsp /*
943 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
944 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
945 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
946 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
947 af57b12a 2020-07-23 stsp */
948 af57b12a 2020-07-23 stsp static const struct got_error *
949 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
950 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
951 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
952 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
953 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
954 af57b12a 2020-07-23 stsp {
955 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
956 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
957 af57b12a 2020-07-23 stsp struct stat sb;
958 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
959 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
960 11cc08c1 2020-07-23 stsp int have_local_change = 0;
961 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
962 af57b12a 2020-07-23 stsp
963 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
964 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
965 af57b12a 2020-07-23 stsp
966 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
967 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
968 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
969 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
970 af57b12a 2020-07-23 stsp ondisk_path);
971 af57b12a 2020-07-23 stsp goto done;
972 af57b12a 2020-07-23 stsp }
973 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
974 af57b12a 2020-07-23 stsp
975 526a746f 2020-07-23 stsp if (blob_orig) {
976 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
977 526a746f 2020-07-23 stsp if (err)
978 526a746f 2020-07-23 stsp goto done;
979 526a746f 2020-07-23 stsp }
980 af57b12a 2020-07-23 stsp
981 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
982 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
983 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
984 11cc08c1 2020-07-23 stsp have_local_change = 1;
985 11cc08c1 2020-07-23 stsp
986 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
987 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
988 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
989 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
990 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
991 11cc08c1 2020-07-23 stsp
992 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
993 11cc08c1 2020-07-23 stsp if (ancestor_target) {
994 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
995 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
996 11cc08c1 2020-07-23 stsp path);
997 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
998 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
999 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1000 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1001 11cc08c1 2020-07-23 stsp path);
1002 11cc08c1 2020-07-23 stsp } else {
1003 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1004 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1005 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1006 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1007 11cc08c1 2020-07-23 stsp if (err)
1008 11cc08c1 2020-07-23 stsp goto done;
1009 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1010 11cc08c1 2020-07-23 stsp path);
1011 11cc08c1 2020-07-23 stsp }
1012 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1013 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1014 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1015 af57b12a 2020-07-23 stsp strlen(deriv_target));
1016 af57b12a 2020-07-23 stsp if (err)
1017 af57b12a 2020-07-23 stsp goto done;
1018 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1019 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1020 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1021 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1022 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1023 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1024 ea7786be 2020-07-23 stsp path);
1025 ea7786be 2020-07-23 stsp } else {
1026 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1027 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1028 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1029 ea7786be 2020-07-23 stsp if (err)
1030 ea7786be 2020-07-23 stsp goto done;
1031 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1032 ea7786be 2020-07-23 stsp path);
1033 ea7786be 2020-07-23 stsp }
1034 6353ad76 2019-02-08 stsp }
1035 11cc08c1 2020-07-23 stsp
1036 af57b12a 2020-07-23 stsp done:
1037 af57b12a 2020-07-23 stsp free(ancestor_target);
1038 eec2f5a9 2021-06-03 stsp return err;
1039 eec2f5a9 2021-06-03 stsp }
1040 eec2f5a9 2021-06-03 stsp
1041 eec2f5a9 2021-06-03 stsp static const struct got_error *
1042 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1043 eec2f5a9 2021-06-03 stsp {
1044 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
1045 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
1046 eec2f5a9 2021-06-03 stsp ssize_t target_len;
1047 eec2f5a9 2021-06-03 stsp size_t n;
1048 eec2f5a9 2021-06-03 stsp FILE *f;
1049 eec2f5a9 2021-06-03 stsp
1050 eec2f5a9 2021-06-03 stsp *outfile = NULL;
1051 eec2f5a9 2021-06-03 stsp
1052 eec2f5a9 2021-06-03 stsp f = got_opentemp();
1053 eec2f5a9 2021-06-03 stsp if (f == NULL)
1054 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
1055 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1056 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
1057 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
1058 eec2f5a9 2021-06-03 stsp goto done;
1059 eec2f5a9 2021-06-03 stsp }
1060 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
1061 eec2f5a9 2021-06-03 stsp if (n != target_len) {
1062 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1063 eec2f5a9 2021-06-03 stsp goto done;
1064 eec2f5a9 2021-06-03 stsp }
1065 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
1066 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
1067 eec2f5a9 2021-06-03 stsp goto done;
1068 eec2f5a9 2021-06-03 stsp }
1069 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
1070 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1071 eec2f5a9 2021-06-03 stsp goto done;
1072 eec2f5a9 2021-06-03 stsp }
1073 eec2f5a9 2021-06-03 stsp done:
1074 eec2f5a9 2021-06-03 stsp if (err)
1075 eec2f5a9 2021-06-03 stsp fclose(f);
1076 eec2f5a9 2021-06-03 stsp else
1077 eec2f5a9 2021-06-03 stsp *outfile = f;
1078 14c901f1 2019-08-08 stsp return err;
1079 14c901f1 2019-08-08 stsp }
1080 14c901f1 2019-08-08 stsp
1081 14c901f1 2019-08-08 stsp /*
1082 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1083 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1084 14c901f1 2019-08-08 stsp * acts as the second derived version.
1085 14c901f1 2019-08-08 stsp */
1086 14c901f1 2019-08-08 stsp static const struct got_error *
1087 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1088 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1089 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1090 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1091 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1092 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1093 14c901f1 2019-08-08 stsp {
1094 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1095 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1096 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
1097 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1098 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1099 14c901f1 2019-08-08 stsp
1100 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1101 14c901f1 2019-08-08 stsp
1102 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1103 ed6b5030 2020-10-20 stsp if (err)
1104 ed6b5030 2020-10-20 stsp return err;
1105 14c901f1 2019-08-08 stsp
1106 67a66647 2021-05-31 stsp if (blob_orig) {
1107 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1108 67a66647 2021-05-31 stsp parent) == -1) {
1109 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1110 67a66647 2021-05-31 stsp base_path = NULL;
1111 67a66647 2021-05-31 stsp goto done;
1112 67a66647 2021-05-31 stsp }
1113 67a66647 2021-05-31 stsp
1114 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1115 67a66647 2021-05-31 stsp if (err)
1116 67a66647 2021-05-31 stsp goto done;
1117 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1118 67a66647 2021-05-31 stsp blob_orig);
1119 67a66647 2021-05-31 stsp if (err)
1120 67a66647 2021-05-31 stsp goto done;
1121 67a66647 2021-05-31 stsp free(base_path);
1122 db590691 2021-06-02 stsp } else {
1123 db590691 2021-06-02 stsp /*
1124 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1125 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1126 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1127 db590691 2021-06-02 stsp */
1128 db590691 2021-06-02 stsp f_orig = got_opentemp();
1129 db590691 2021-06-02 stsp if (f_orig == NULL) {
1130 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1131 db590691 2021-06-02 stsp goto done;
1132 db590691 2021-06-02 stsp }
1133 67a66647 2021-05-31 stsp }
1134 67a66647 2021-05-31 stsp
1135 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1136 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1137 14c901f1 2019-08-08 stsp base_path = NULL;
1138 14c901f1 2019-08-08 stsp goto done;
1139 14c901f1 2019-08-08 stsp }
1140 14c901f1 2019-08-08 stsp
1141 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1142 14c901f1 2019-08-08 stsp if (err)
1143 14c901f1 2019-08-08 stsp goto done;
1144 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1145 14c901f1 2019-08-08 stsp blob_deriv);
1146 14c901f1 2019-08-08 stsp if (err)
1147 14c901f1 2019-08-08 stsp goto done;
1148 14c901f1 2019-08-08 stsp
1149 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1150 14c901f1 2019-08-08 stsp if (err)
1151 14c901f1 2019-08-08 stsp goto done;
1152 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1153 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1154 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1155 14c901f1 2019-08-08 stsp goto done;
1156 eec2f5a9 2021-06-03 stsp }
1157 eec2f5a9 2021-06-03 stsp
1158 eec2f5a9 2021-06-03 stsp /*
1159 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1160 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1161 eec2f5a9 2021-06-03 stsp */
1162 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1163 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1164 eec2f5a9 2021-06-03 stsp if (err)
1165 eec2f5a9 2021-06-03 stsp goto done;
1166 eec2f5a9 2021-06-03 stsp } else {
1167 eec2f5a9 2021-06-03 stsp int fd;
1168 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1169 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1170 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1171 eec2f5a9 2021-06-03 stsp goto done;
1172 eec2f5a9 2021-06-03 stsp }
1173 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1174 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1175 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1176 eec2f5a9 2021-06-03 stsp close(fd);
1177 eec2f5a9 2021-06-03 stsp goto done;
1178 eec2f5a9 2021-06-03 stsp }
1179 14c901f1 2019-08-08 stsp }
1180 14c901f1 2019-08-08 stsp
1181 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1182 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1183 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1184 14c901f1 2019-08-08 stsp done:
1185 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1186 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1187 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1188 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1189 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1190 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1191 14c901f1 2019-08-08 stsp free(base_path);
1192 67a66647 2021-05-31 stsp if (blob_orig_path) {
1193 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1194 67a66647 2021-05-31 stsp free(blob_orig_path);
1195 67a66647 2021-05-31 stsp }
1196 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1197 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1198 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1199 14c901f1 2019-08-08 stsp }
1200 6353ad76 2019-02-08 stsp free(id_str);
1201 818c7501 2019-07-11 stsp free(label_deriv);
1202 ed6b5030 2020-10-20 stsp free(parent);
1203 4a1ddfc2 2019-01-12 stsp return err;
1204 4a1ddfc2 2019-01-12 stsp }
1205 4a1ddfc2 2019-01-12 stsp
1206 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1207 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1208 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1209 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1210 13d9040b 2019-03-27 stsp {
1211 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1212 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1213 13d9040b 2019-03-27 stsp
1214 65b05cec 2020-07-23 stsp *new_iep = NULL;
1215 65b05cec 2020-07-23 stsp
1216 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1217 054041d0 2020-06-24 stsp if (err)
1218 054041d0 2020-06-24 stsp return err;
1219 054041d0 2020-06-24 stsp
1220 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1221 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1222 054041d0 2020-06-24 stsp if (err)
1223 054041d0 2020-06-24 stsp goto done;
1224 054041d0 2020-06-24 stsp
1225 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1226 054041d0 2020-06-24 stsp done:
1227 054041d0 2020-06-24 stsp if (err)
1228 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1229 65b05cec 2020-07-23 stsp else
1230 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1231 13d9040b 2019-03-27 stsp return err;
1232 1ebedb77 2019-10-19 stsp }
1233 1ebedb77 2019-10-19 stsp
1234 1ebedb77 2019-10-19 stsp static mode_t
1235 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1236 1ebedb77 2019-10-19 stsp {
1237 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1238 1ebedb77 2019-10-19 stsp
1239 1ebedb77 2019-10-19 stsp if (executable) {
1240 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1241 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1242 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1243 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1244 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1245 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1246 1ebedb77 2019-10-19 stsp }
1247 1ebedb77 2019-10-19 stsp
1248 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1249 13d9040b 2019-03-27 stsp }
1250 13d9040b 2019-03-27 stsp
1251 8ba819a3 2020-07-23 stsp /* forward declaration */
1252 13d9040b 2019-03-27 stsp static const struct got_error *
1253 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1254 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1255 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1256 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1257 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1258 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1259 8ba819a3 2020-07-23 stsp
1260 5a1fbc73 2020-07-23 stsp /*
1261 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1262 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1263 5a1fbc73 2020-07-23 stsp */
1264 8ba819a3 2020-07-23 stsp static const struct got_error *
1265 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1266 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1267 5a1fbc73 2020-07-23 stsp {
1268 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1269 5a1fbc73 2020-07-23 stsp ssize_t elen;
1270 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1271 5a1fbc73 2020-07-23 stsp int fd;
1272 5a1fbc73 2020-07-23 stsp
1273 c6e8a826 2021-04-05 stsp *did_something = 0;
1274 c6e8a826 2021-04-05 stsp
1275 5a1fbc73 2020-07-23 stsp /*
1276 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1277 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1278 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1279 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1280 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1281 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1282 5a1fbc73 2020-07-23 stsp */
1283 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1284 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1285 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1286 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1287 5a1fbc73 2020-07-23 stsp
1288 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1289 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1290 5a1fbc73 2020-07-23 stsp if (elen == -1)
1291 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1292 5a1fbc73 2020-07-23 stsp
1293 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1294 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1295 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1296 5a1fbc73 2020-07-23 stsp }
1297 5a1fbc73 2020-07-23 stsp
1298 c6e8a826 2021-04-05 stsp *did_something = 1;
1299 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1300 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1301 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1302 5a1fbc73 2020-07-23 stsp return err;
1303 3c1ec782 2020-07-23 stsp }
1304 3c1ec782 2020-07-23 stsp
1305 3c1ec782 2020-07-23 stsp static const struct got_error *
1306 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1307 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1308 3c1ec782 2020-07-23 stsp {
1309 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1310 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1311 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1312 3c1ec782 2020-07-23 stsp
1313 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1314 3c1ec782 2020-07-23 stsp
1315 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1316 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1317 3c1ec782 2020-07-23 stsp return NULL;
1318 3c1ec782 2020-07-23 stsp }
1319 3c1ec782 2020-07-23 stsp
1320 3c1ec782 2020-07-23 stsp /*
1321 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1322 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1323 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1324 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1325 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1326 3c1ec782 2020-07-23 stsp */
1327 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1328 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1329 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1330 ce031e9e 2020-10-20 stsp if (err)
1331 ce031e9e 2020-10-20 stsp return err;
1332 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1333 ce031e9e 2020-10-20 stsp free(parent);
1334 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1335 ce031e9e 2020-10-20 stsp }
1336 ce031e9e 2020-10-20 stsp free(parent);
1337 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1338 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1339 3c1ec782 2020-07-23 stsp free(abspath);
1340 3c1ec782 2020-07-23 stsp return err;
1341 3c1ec782 2020-07-23 stsp }
1342 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1343 3c1ec782 2020-07-23 stsp free(abspath);
1344 3c1ec782 2020-07-23 stsp if (err)
1345 3c1ec782 2020-07-23 stsp return err;
1346 3c1ec782 2020-07-23 stsp } else {
1347 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1348 3c1ec782 2020-07-23 stsp if (err)
1349 3c1ec782 2020-07-23 stsp return err;
1350 3c1ec782 2020-07-23 stsp }
1351 3c1ec782 2020-07-23 stsp
1352 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1353 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1354 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1355 3c1ec782 2020-07-23 stsp return NULL;
1356 3c1ec782 2020-07-23 stsp }
1357 3c1ec782 2020-07-23 stsp
1358 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1359 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1360 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1361 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1362 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1363 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1364 3c1ec782 2020-07-23 stsp
1365 3c1ec782 2020-07-23 stsp free(path_got);
1366 3c1ec782 2020-07-23 stsp return NULL;
1367 5a1fbc73 2020-07-23 stsp }
1368 5a1fbc73 2020-07-23 stsp
1369 5a1fbc73 2020-07-23 stsp static const struct got_error *
1370 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1371 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1372 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1373 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1374 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1375 9d31a1d8 2018-03-11 stsp {
1376 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1377 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1378 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1379 906c123b 2020-07-23 stsp char *path_got = NULL;
1380 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1381 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1382 8ba819a3 2020-07-23 stsp
1383 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1384 2e1fa222 2020-07-23 stsp
1385 8ba819a3 2020-07-23 stsp /*
1386 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1387 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1388 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1389 8ba819a3 2020-07-23 stsp * in the blob object.
1390 8ba819a3 2020-07-23 stsp */
1391 8ba819a3 2020-07-23 stsp do {
1392 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1393 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1394 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1395 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1396 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1397 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1398 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1399 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1400 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1401 3b9f0f87 2020-07-23 stsp progress_arg);
1402 8ba819a3 2020-07-23 stsp }
1403 8ba819a3 2020-07-23 stsp if (len > 0) {
1404 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1405 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1406 8ba819a3 2020-07-23 stsp len - hdrlen);
1407 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1408 8ba819a3 2020-07-23 stsp hdrlen = 0;
1409 8ba819a3 2020-07-23 stsp }
1410 8ba819a3 2020-07-23 stsp } while (len != 0);
1411 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1412 8ba819a3 2020-07-23 stsp
1413 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1414 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1415 3c1ec782 2020-07-23 stsp if (err)
1416 3c1ec782 2020-07-23 stsp return err;
1417 8ba819a3 2020-07-23 stsp
1418 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1419 906c123b 2020-07-23 stsp /* install as a regular file */
1420 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1421 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1422 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1423 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1424 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1425 906c123b 2020-07-23 stsp goto done;
1426 906c123b 2020-07-23 stsp }
1427 906c123b 2020-07-23 stsp
1428 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1429 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1430 c6e8a826 2021-04-05 stsp int symlink_replaced;
1431 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1432 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1433 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1434 c90c8ce3 2020-07-23 stsp goto done;
1435 c90c8ce3 2020-07-23 stsp }
1436 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1437 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1438 5a1fbc73 2020-07-23 stsp if (err)
1439 f35fa46a 2020-07-23 stsp goto done;
1440 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1441 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1442 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1443 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1444 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1445 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1446 c6e8a826 2021-04-05 stsp } else {
1447 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1448 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1449 c6e8a826 2021-04-05 stsp }
1450 f35fa46a 2020-07-23 stsp }
1451 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1452 f35fa46a 2020-07-23 stsp }
1453 f35fa46a 2020-07-23 stsp
1454 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1455 f4994adc 2020-10-20 stsp char *parent;
1456 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1457 f4994adc 2020-10-20 stsp if (err)
1458 f4994adc 2020-10-20 stsp goto done;
1459 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1460 f4994adc 2020-10-20 stsp free(parent);
1461 f35fa46a 2020-07-23 stsp if (err)
1462 f35fa46a 2020-07-23 stsp goto done;
1463 f35fa46a 2020-07-23 stsp /*
1464 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1465 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1466 f35fa46a 2020-07-23 stsp */
1467 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1468 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1469 f35fa46a 2020-07-23 stsp goto done;
1470 f35fa46a 2020-07-23 stsp }
1471 f35fa46a 2020-07-23 stsp }
1472 f35fa46a 2020-07-23 stsp
1473 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1474 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1475 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1476 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1477 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1478 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1479 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1480 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1481 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1482 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1483 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1484 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1485 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1486 8ba819a3 2020-07-23 stsp } else {
1487 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1488 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1489 8ba819a3 2020-07-23 stsp }
1490 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1491 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1492 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1493 8ba819a3 2020-07-23 stsp done:
1494 906c123b 2020-07-23 stsp free(path_got);
1495 8ba819a3 2020-07-23 stsp return err;
1496 8ba819a3 2020-07-23 stsp }
1497 8ba819a3 2020-07-23 stsp
1498 8ba819a3 2020-07-23 stsp static const struct got_error *
1499 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1500 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1501 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1502 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1503 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1504 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1505 8ba819a3 2020-07-23 stsp {
1506 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1507 507dc3bb 2018-12-29 stsp int fd = -1;
1508 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1509 507dc3bb 2018-12-29 stsp int update = 0;
1510 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1511 8ba819a3 2020-07-23 stsp
1512 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1513 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1514 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1515 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1516 f5375317 2020-10-20 stsp char *parent;
1517 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1518 f5375317 2020-10-20 stsp if (err)
1519 f5375317 2020-10-20 stsp return err;
1520 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1521 f5375317 2020-10-20 stsp free(parent);
1522 21908da4 2019-01-13 stsp if (err)
1523 21908da4 2019-01-13 stsp return err;
1524 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1525 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1526 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1527 21908da4 2019-01-13 stsp if (fd == -1)
1528 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1529 230a42bd 2019-05-11 jcs ondisk_path);
1530 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1531 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1532 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1533 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1534 3b9f0f87 2020-07-23 stsp goto done;
1535 3b9f0f87 2020-07-23 stsp }
1536 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1537 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1538 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1539 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1540 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1541 507dc3bb 2018-12-29 stsp goto done;
1542 d70b8e30 2018-12-27 stsp } else {
1543 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1544 507dc3bb 2018-12-29 stsp ondisk_path);
1545 507dc3bb 2018-12-29 stsp if (err)
1546 507dc3bb 2018-12-29 stsp goto done;
1547 507dc3bb 2018-12-29 stsp update = 1;
1548 9d31a1d8 2018-03-11 stsp }
1549 507dc3bb 2018-12-29 stsp } else
1550 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1551 9d31a1d8 2018-03-11 stsp }
1552 9d31a1d8 2018-03-11 stsp
1553 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1554 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1555 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1556 3818e3c4 2020-11-01 naddy goto done;
1557 3818e3c4 2020-11-01 naddy }
1558 3818e3c4 2020-11-01 naddy
1559 bd6aa359 2020-07-23 stsp if (progress_cb) {
1560 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1561 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1562 bd6aa359 2020-07-23 stsp path);
1563 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1564 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1565 bd6aa359 2020-07-23 stsp path);
1566 bd6aa359 2020-07-23 stsp else
1567 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1568 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1569 bd6aa359 2020-07-23 stsp if (err)
1570 bd6aa359 2020-07-23 stsp goto done;
1571 bd6aa359 2020-07-23 stsp }
1572 d7b62c98 2018-12-27 stsp
1573 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1574 9d31a1d8 2018-03-11 stsp do {
1575 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1576 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1577 9d31a1d8 2018-03-11 stsp if (err)
1578 9d31a1d8 2018-03-11 stsp break;
1579 9d31a1d8 2018-03-11 stsp if (len > 0) {
1580 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1581 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1582 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1583 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1584 b87c6f83 2018-12-24 stsp goto done;
1585 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1586 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1587 b87c6f83 2018-12-24 stsp goto done;
1588 9d31a1d8 2018-03-11 stsp }
1589 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1590 9d31a1d8 2018-03-11 stsp }
1591 9d31a1d8 2018-03-11 stsp } while (len != 0);
1592 9d31a1d8 2018-03-11 stsp
1593 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1594 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1595 816dc654 2019-02-16 stsp goto done;
1596 816dc654 2019-02-16 stsp }
1597 9d31a1d8 2018-03-11 stsp
1598 507dc3bb 2018-12-29 stsp if (update) {
1599 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1600 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1601 f6d8c0ac 2020-11-09 stsp goto done;
1602 f6d8c0ac 2020-11-09 stsp }
1603 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1604 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1605 230a42bd 2019-05-11 jcs ondisk_path);
1606 68ed9ba5 2019-02-10 stsp goto done;
1607 68ed9ba5 2019-02-10 stsp }
1608 63df146d 2020-11-09 stsp free(tmppath);
1609 63df146d 2020-11-09 stsp tmppath = NULL;
1610 507dc3bb 2018-12-29 stsp }
1611 507dc3bb 2018-12-29 stsp
1612 9d31a1d8 2018-03-11 stsp done:
1613 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1614 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1615 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1616 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1617 507dc3bb 2018-12-29 stsp free(tmppath);
1618 6353ad76 2019-02-08 stsp return err;
1619 6353ad76 2019-02-08 stsp }
1620 6353ad76 2019-02-08 stsp
1621 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1622 6353ad76 2019-02-08 stsp static const struct got_error *
1623 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1624 7154f6ce 2019-03-27 stsp {
1625 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1626 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1627 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1628 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1629 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1630 7154f6ce 2019-03-27 stsp };
1631 7154f6ce 2019-03-27 stsp int i = 0;
1632 9bdd68dd 2020-01-02 naddy char *line = NULL;
1633 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1634 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1635 7154f6ce 2019-03-27 stsp
1636 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1637 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1638 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1639 7154f6ce 2019-03-27 stsp if (feof(f))
1640 7154f6ce 2019-03-27 stsp break;
1641 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1642 7154f6ce 2019-03-27 stsp break;
1643 7154f6ce 2019-03-27 stsp }
1644 7154f6ce 2019-03-27 stsp
1645 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1646 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1647 19332e6d 2019-05-13 stsp == 0)
1648 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1649 7154f6ce 2019-03-27 stsp else
1650 7154f6ce 2019-03-27 stsp i++;
1651 7154f6ce 2019-03-27 stsp }
1652 7154f6ce 2019-03-27 stsp }
1653 9bdd68dd 2020-01-02 naddy free(line);
1654 7154f6ce 2019-03-27 stsp
1655 7154f6ce 2019-03-27 stsp return err;
1656 e2b1e152 2019-07-13 stsp }
1657 e2b1e152 2019-07-13 stsp
1658 e2b1e152 2019-07-13 stsp static int
1659 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1660 1ebedb77 2019-10-19 stsp {
1661 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1662 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1663 1ebedb77 2019-10-19 stsp }
1664 1ebedb77 2019-10-19 stsp
1665 1ebedb77 2019-10-19 stsp static int
1666 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1667 e2b1e152 2019-07-13 stsp {
1668 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1669 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1670 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1671 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1672 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1673 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1674 c363b2c1 2019-08-03 stsp }
1675 c363b2c1 2019-08-03 stsp
1676 c363b2c1 2019-08-03 stsp static unsigned char
1677 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1678 c363b2c1 2019-08-03 stsp {
1679 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1680 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1681 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1682 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1683 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1684 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1685 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1686 c363b2c1 2019-08-03 stsp default:
1687 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1688 a919d5c4 2020-07-23 stsp }
1689 a919d5c4 2020-07-23 stsp }
1690 a919d5c4 2020-07-23 stsp
1691 a919d5c4 2020-07-23 stsp static const struct got_error *
1692 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1693 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1694 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1695 a919d5c4 2020-07-23 stsp {
1696 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1697 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1698 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1699 a919d5c4 2020-07-23 stsp ssize_t elen;
1700 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1701 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1702 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1703 a919d5c4 2020-07-23 stsp
1704 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1705 a919d5c4 2020-07-23 stsp
1706 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1707 a919d5c4 2020-07-23 stsp do {
1708 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1709 a919d5c4 2020-07-23 stsp if (err)
1710 a919d5c4 2020-07-23 stsp return err;
1711 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1712 a919d5c4 2020-07-23 stsp /*
1713 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1714 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1715 a919d5c4 2020-07-23 stsp */
1716 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1717 a919d5c4 2020-07-23 stsp }
1718 a919d5c4 2020-07-23 stsp if (len > 0) {
1719 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1720 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1721 a919d5c4 2020-07-23 stsp len - hdrlen);
1722 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1723 a919d5c4 2020-07-23 stsp hdrlen = 0;
1724 a919d5c4 2020-07-23 stsp }
1725 a919d5c4 2020-07-23 stsp } while (len != 0);
1726 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1727 a919d5c4 2020-07-23 stsp
1728 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1729 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1730 a919d5c4 2020-07-23 stsp if (elen == -1)
1731 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1732 a919d5c4 2020-07-23 stsp } else {
1733 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1734 a919d5c4 2020-07-23 stsp if (elen == -1)
1735 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1736 c363b2c1 2019-08-03 stsp }
1737 a919d5c4 2020-07-23 stsp
1738 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1739 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1740 a919d5c4 2020-07-23 stsp
1741 a919d5c4 2020-07-23 stsp return NULL;
1742 7154f6ce 2019-03-27 stsp }
1743 7154f6ce 2019-03-27 stsp
1744 7154f6ce 2019-03-27 stsp static const struct got_error *
1745 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1746 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1747 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1748 6353ad76 2019-02-08 stsp {
1749 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1750 6353ad76 2019-02-08 stsp struct got_object_id id;
1751 6353ad76 2019-02-08 stsp size_t hdrlen;
1752 1338848f 2019-12-13 stsp int fd = -1;
1753 6353ad76 2019-02-08 stsp FILE *f = NULL;
1754 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1755 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1756 6353ad76 2019-02-08 stsp size_t flen, blen;
1757 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1758 6353ad76 2019-02-08 stsp
1759 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1760 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1761 6353ad76 2019-02-08 stsp
1762 7f91a133 2019-12-13 stsp /*
1763 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1764 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1765 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1766 7f91a133 2019-12-13 stsp */
1767 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1768 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1769 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1770 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1771 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1772 882ef1b9 2019-12-13 stsp else
1773 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1774 882ef1b9 2019-12-13 stsp goto done;
1775 882ef1b9 2019-12-13 stsp }
1776 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1777 3d35a492 2019-12-13 stsp goto done;
1778 3d35a492 2019-12-13 stsp }
1779 7f91a133 2019-12-13 stsp } else {
1780 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1781 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1782 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1783 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1784 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1785 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1786 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1787 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1788 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1789 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1790 3d35a492 2019-12-13 stsp else
1791 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1792 3d35a492 2019-12-13 stsp goto done;
1793 3d35a492 2019-12-13 stsp }
1794 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1795 1338848f 2019-12-13 stsp goto done;
1796 a378724f 2019-02-10 stsp }
1797 a378724f 2019-02-10 stsp }
1798 6353ad76 2019-02-08 stsp
1799 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1800 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1801 1338848f 2019-12-13 stsp goto done;
1802 3f148bc6 2019-07-27 stsp }
1803 efdd40df 2019-07-27 stsp
1804 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1805 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1806 1338848f 2019-12-13 stsp goto done;
1807 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1808 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1809 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1810 1338848f 2019-12-13 stsp goto done;
1811 d00136be 2019-03-26 stsp }
1812 b8f41171 2019-02-10 stsp
1813 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1814 f179e66d 2020-07-23 stsp goto done;
1815 f179e66d 2020-07-23 stsp
1816 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1817 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1818 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1819 1338848f 2019-12-13 stsp goto done;
1820 f179e66d 2020-07-23 stsp }
1821 6353ad76 2019-02-08 stsp
1822 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1823 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1824 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1825 c363b2c1 2019-08-03 stsp else
1826 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1827 c363b2c1 2019-08-03 stsp
1828 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1829 6353ad76 2019-02-08 stsp if (err)
1830 1338848f 2019-12-13 stsp goto done;
1831 6353ad76 2019-02-08 stsp
1832 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1833 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1834 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1835 a919d5c4 2020-07-23 stsp goto done;
1836 a919d5c4 2020-07-23 stsp }
1837 a919d5c4 2020-07-23 stsp
1838 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1839 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1840 ab0d4361 2019-12-13 stsp if (fd == -1) {
1841 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1842 ab0d4361 2019-12-13 stsp goto done;
1843 ab0d4361 2019-12-13 stsp }
1844 3d35a492 2019-12-13 stsp }
1845 3d35a492 2019-12-13 stsp
1846 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1847 6353ad76 2019-02-08 stsp if (f == NULL) {
1848 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1849 6353ad76 2019-02-08 stsp goto done;
1850 6353ad76 2019-02-08 stsp }
1851 1338848f 2019-12-13 stsp fd = -1;
1852 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1853 656b1f76 2019-05-11 jcs for (;;) {
1854 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1855 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1856 6353ad76 2019-02-08 stsp if (err)
1857 7154f6ce 2019-03-27 stsp goto done;
1858 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1859 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1860 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1861 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1862 7154f6ce 2019-03-27 stsp goto done;
1863 80c5c120 2019-02-19 stsp }
1864 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1865 6353ad76 2019-02-08 stsp if (flen != 0)
1866 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1867 6353ad76 2019-02-08 stsp break;
1868 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1869 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1870 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1871 6353ad76 2019-02-08 stsp break;
1872 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1873 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1874 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1875 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1876 6353ad76 2019-02-08 stsp break;
1877 6353ad76 2019-02-08 stsp }
1878 6353ad76 2019-02-08 stsp } else {
1879 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1880 6353ad76 2019-02-08 stsp break;
1881 6353ad76 2019-02-08 stsp }
1882 6353ad76 2019-02-08 stsp hdrlen = 0;
1883 6353ad76 2019-02-08 stsp }
1884 7154f6ce 2019-03-27 stsp
1885 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1886 7154f6ce 2019-03-27 stsp rewind(f);
1887 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1888 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1889 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1890 6353ad76 2019-02-08 stsp done:
1891 6353ad76 2019-02-08 stsp if (blob)
1892 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1893 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1894 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1895 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1896 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1897 9d31a1d8 2018-03-11 stsp return err;
1898 e2b1e152 2019-07-13 stsp }
1899 e2b1e152 2019-07-13 stsp
1900 e2b1e152 2019-07-13 stsp /*
1901 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1902 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1903 e2b1e152 2019-07-13 stsp */
1904 e2b1e152 2019-07-13 stsp static const struct got_error *
1905 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1906 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1907 e2b1e152 2019-07-13 stsp {
1908 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1909 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1910 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1911 e2b1e152 2019-07-13 stsp
1912 e2b1e152 2019-07-13 stsp return NULL;
1913 9d31a1d8 2018-03-11 stsp }
1914 9d31a1d8 2018-03-11 stsp
1915 9d31a1d8 2018-03-11 stsp static const struct got_error *
1916 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1917 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1918 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1919 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1920 0584f854 2019-04-06 stsp void *progress_arg)
1921 9d31a1d8 2018-03-11 stsp {
1922 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1923 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1924 6353ad76 2019-02-08 stsp char *ondisk_path;
1925 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1926 b8f41171 2019-02-10 stsp struct stat sb;
1927 9d31a1d8 2018-03-11 stsp
1928 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1929 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1930 6353ad76 2019-02-08 stsp
1931 abb4604f 2019-07-27 stsp if (ie) {
1932 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1933 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1934 a76c42e6 2019-08-03 stsp goto done;
1935 a76c42e6 2019-08-03 stsp }
1936 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1937 7f91a133 2019-12-13 stsp repo);
1938 abb4604f 2019-07-27 stsp if (err)
1939 abb4604f 2019-07-27 stsp goto done;
1940 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1941 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1942 c90c8ce3 2020-07-23 stsp } else {
1943 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1944 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1945 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1946 e6f4ba31 2021-09-24 thomas ondisk_path);
1947 e6f4ba31 2021-09-24 thomas goto done;
1948 e6f4ba31 2021-09-24 thomas }
1949 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1950 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1951 e6f4ba31 2021-09-24 thomas } else {
1952 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1953 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1954 e6f4ba31 2021-09-24 thomas else
1955 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1956 e6f4ba31 2021-09-24 thomas }
1957 c90c8ce3 2020-07-23 stsp }
1958 6353ad76 2019-02-08 stsp
1959 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1960 2c41dce7 2021-06-27 stsp if (ie)
1961 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1962 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1963 b8f41171 2019-02-10 stsp goto done;
1964 b8f41171 2019-02-10 stsp }
1965 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1966 a769b60b 2021-06-27 stsp if (ie)
1967 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1968 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1969 5036ab18 2020-04-18 stsp path);
1970 5036ab18 2020-04-18 stsp goto done;
1971 5036ab18 2020-04-18 stsp }
1972 b8f41171 2019-02-10 stsp
1973 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1974 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1975 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1976 c6e8a826 2021-04-05 stsp /*
1977 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1978 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1979 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1980 c6e8a826 2021-04-05 stsp * updating contents of this file.
1981 c6e8a826 2021-04-05 stsp */
1982 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1983 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1984 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1985 c6e8a826 2021-04-05 stsp /* Same commit. */
1986 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1987 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1988 e2b1e152 2019-07-13 stsp if (err)
1989 e2b1e152 2019-07-13 stsp goto done;
1990 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1991 b8f41171 2019-02-10 stsp path);
1992 6353ad76 2019-02-08 stsp goto done;
1993 a378724f 2019-02-10 stsp }
1994 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1995 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1996 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1997 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1998 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1999 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2000 0f58026f 2021-04-05 stsp if (err)
2001 0f58026f 2021-04-05 stsp goto done;
2002 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2003 0f58026f 2021-04-05 stsp path);
2004 b8f41171 2019-02-10 stsp goto done;
2005 e2b1e152 2019-07-13 stsp }
2006 9d31a1d8 2018-03-11 stsp }
2007 9d31a1d8 2018-03-11 stsp
2008 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2009 8da9e5f4 2019-01-12 stsp if (err)
2010 6353ad76 2019-02-08 stsp goto done;
2011 512f0d0e 2019-01-02 stsp
2012 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2013 234035bc 2019-06-01 stsp int update_timestamps;
2014 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2015 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2016 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2017 234035bc 2019-06-01 stsp struct got_object_id id2;
2018 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2019 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2020 234035bc 2019-06-01 stsp if (err)
2021 f69721c3 2019-10-21 stsp goto done;
2022 f69721c3 2019-10-21 stsp }
2023 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2024 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2025 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2026 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2027 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2028 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2029 f69721c3 2019-10-21 stsp goto done;
2030 f69721c3 2019-10-21 stsp }
2031 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2032 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2033 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2034 234035bc 2019-06-01 stsp goto done;
2035 f69721c3 2019-10-21 stsp }
2036 234035bc 2019-06-01 stsp }
2037 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2038 36bf999c 2020-07-23 stsp char *link_target;
2039 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2040 36bf999c 2020-07-23 stsp if (err)
2041 36bf999c 2020-07-23 stsp goto done;
2042 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2043 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2044 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2045 36bf999c 2020-07-23 stsp free(link_target);
2046 993e2a1b 2020-07-23 stsp } else {
2047 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2048 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2049 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2050 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2051 993e2a1b 2020-07-23 stsp }
2052 f69721c3 2019-10-21 stsp free(label_orig);
2053 234035bc 2019-06-01 stsp if (blob2)
2054 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2055 909d120e 2019-09-22 stsp if (err)
2056 909d120e 2019-09-22 stsp goto done;
2057 234035bc 2019-06-01 stsp /*
2058 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2059 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2060 234035bc 2019-06-01 stsp * unmodified files again.
2061 234035bc 2019-06-01 stsp */
2062 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2063 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2064 234035bc 2019-06-01 stsp update_timestamps);
2065 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2066 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2067 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2068 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2069 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2070 1ee397ad 2019-07-12 stsp if (err)
2071 1ee397ad 2019-07-12 stsp goto done;
2072 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2073 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2074 13d9040b 2019-03-27 stsp if (err)
2075 13d9040b 2019-03-27 stsp goto done;
2076 13d9040b 2019-03-27 stsp } else {
2077 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2078 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2079 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2080 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2081 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2082 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2083 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2084 2e1fa222 2020-07-23 stsp } else {
2085 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2086 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2087 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2088 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2089 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2090 2e1fa222 2020-07-23 stsp }
2091 13d9040b 2019-03-27 stsp if (err)
2092 13d9040b 2019-03-27 stsp goto done;
2093 65b05cec 2020-07-23 stsp
2094 054041d0 2020-06-24 stsp if (ie) {
2095 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2096 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2097 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2098 054041d0 2020-06-24 stsp } else {
2099 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2100 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2101 054041d0 2020-06-24 stsp &blob->id);
2102 054041d0 2020-06-24 stsp }
2103 13d9040b 2019-03-27 stsp if (err)
2104 13d9040b 2019-03-27 stsp goto done;
2105 65b05cec 2020-07-23 stsp
2106 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2107 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2108 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2109 2e1fa222 2020-07-23 stsp }
2110 13d9040b 2019-03-27 stsp }
2111 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2112 6353ad76 2019-02-08 stsp done:
2113 6353ad76 2019-02-08 stsp free(ondisk_path);
2114 8da9e5f4 2019-01-12 stsp return err;
2115 90285c3b 2019-01-08 stsp }
2116 90285c3b 2019-01-08 stsp
2117 90285c3b 2019-01-08 stsp static const struct got_error *
2118 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2119 90285c3b 2019-01-08 stsp {
2120 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2121 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2122 90285c3b 2019-01-08 stsp
2123 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2124 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2125 90285c3b 2019-01-08 stsp
2126 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2127 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2128 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2129 c97665ae 2019-01-13 stsp } else {
2130 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2131 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2132 1c4cdd89 2021-06-20 stsp if (err)
2133 1c4cdd89 2021-06-20 stsp goto done;
2134 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2135 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2136 fddefe3b 2020-10-20 stsp free(ondisk_path);
2137 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2138 1c4cdd89 2021-06-20 stsp parent = NULL;
2139 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2140 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2141 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2142 fddefe3b 2020-10-20 stsp ondisk_path);
2143 90285c3b 2019-01-08 stsp break;
2144 90285c3b 2019-01-08 stsp }
2145 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2146 1c4cdd89 2021-06-20 stsp if (err)
2147 1c4cdd89 2021-06-20 stsp break;
2148 1c4cdd89 2021-06-20 stsp }
2149 90285c3b 2019-01-08 stsp }
2150 1c4cdd89 2021-06-20 stsp done:
2151 90285c3b 2019-01-08 stsp free(ondisk_path);
2152 1c4cdd89 2021-06-20 stsp free(parent);
2153 90285c3b 2019-01-08 stsp return err;
2154 512f0d0e 2019-01-02 stsp }
2155 512f0d0e 2019-01-02 stsp
2156 708d8e67 2019-03-27 stsp static const struct got_error *
2157 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2158 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2159 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2160 708d8e67 2019-03-27 stsp {
2161 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2162 708d8e67 2019-03-27 stsp unsigned char status;
2163 708d8e67 2019-03-27 stsp struct stat sb;
2164 708d8e67 2019-03-27 stsp char *ondisk_path;
2165 708d8e67 2019-03-27 stsp
2166 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2167 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2168 a76c42e6 2019-08-03 stsp
2169 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2170 708d8e67 2019-03-27 stsp == -1)
2171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2172 708d8e67 2019-03-27 stsp
2173 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2174 708d8e67 2019-03-27 stsp if (err)
2175 5a58a424 2020-06-23 stsp goto done;
2176 708d8e67 2019-03-27 stsp
2177 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2178 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2179 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2180 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2181 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2182 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2183 993e2a1b 2020-07-23 stsp goto done;
2184 993e2a1b 2020-07-23 stsp }
2185 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2186 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2187 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2188 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2189 993e2a1b 2020-07-23 stsp if (err)
2190 993e2a1b 2020-07-23 stsp goto done;
2191 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2192 993e2a1b 2020-07-23 stsp ie->path);
2193 993e2a1b 2020-07-23 stsp goto done;
2194 993e2a1b 2020-07-23 stsp }
2195 993e2a1b 2020-07-23 stsp
2196 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2197 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2198 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2199 1ee397ad 2019-07-12 stsp if (err)
2200 5a58a424 2020-06-23 stsp goto done;
2201 fc6346c4 2019-03-27 stsp /*
2202 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2203 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2204 fc6346c4 2019-03-27 stsp */
2205 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2206 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2207 fc6346c4 2019-03-27 stsp } else {
2208 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2209 1ee397ad 2019-07-12 stsp if (err)
2210 5a58a424 2020-06-23 stsp goto done;
2211 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2212 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2213 fc6346c4 2019-03-27 stsp if (err)
2214 5a58a424 2020-06-23 stsp goto done;
2215 fc6346c4 2019-03-27 stsp }
2216 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2217 708d8e67 2019-03-27 stsp }
2218 5a58a424 2020-06-23 stsp done:
2219 5a58a424 2020-06-23 stsp free(ondisk_path);
2220 fc6346c4 2019-03-27 stsp return err;
2221 708d8e67 2019-03-27 stsp }
2222 708d8e67 2019-03-27 stsp
2223 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2224 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2225 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2226 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2227 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2228 8da9e5f4 2019-01-12 stsp void *progress_arg;
2229 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2230 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2231 8da9e5f4 2019-01-12 stsp };
2232 8da9e5f4 2019-01-12 stsp
2233 512f0d0e 2019-01-02 stsp static const struct got_error *
2234 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2235 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2236 512f0d0e 2019-01-02 stsp {
2237 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2238 0584f854 2019-04-06 stsp
2239 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2240 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2241 512f0d0e 2019-01-02 stsp
2242 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2243 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2244 8da9e5f4 2019-01-12 stsp }
2245 512f0d0e 2019-01-02 stsp
2246 8da9e5f4 2019-01-12 stsp static const struct got_error *
2247 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2248 8da9e5f4 2019-01-12 stsp {
2249 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2250 7a9df742 2019-01-08 stsp
2251 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2252 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2253 0584f854 2019-04-06 stsp
2254 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2255 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2256 9d31a1d8 2018-03-11 stsp }
2257 9d31a1d8 2018-03-11 stsp
2258 9d31a1d8 2018-03-11 stsp static const struct got_error *
2259 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2260 9d31a1d8 2018-03-11 stsp {
2261 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2262 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2263 8da9e5f4 2019-01-12 stsp char *path;
2264 9d31a1d8 2018-03-11 stsp
2265 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2266 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2267 0584f854 2019-04-06 stsp
2268 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2269 63c5ca5d 2019-08-24 stsp return NULL;
2270 63c5ca5d 2019-08-24 stsp
2271 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2272 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2273 8da9e5f4 2019-01-12 stsp == -1)
2274 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2275 9d31a1d8 2018-03-11 stsp
2276 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2277 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2278 8da9e5f4 2019-01-12 stsp else
2279 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2280 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2281 9d31a1d8 2018-03-11 stsp
2282 8da9e5f4 2019-01-12 stsp free(path);
2283 0cd1c46a 2019-03-11 stsp return err;
2284 0cd1c46a 2019-03-11 stsp }
2285 0cd1c46a 2019-03-11 stsp
2286 b2118c49 2020-07-28 stsp const struct got_error *
2287 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2288 b2118c49 2020-07-28 stsp {
2289 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2290 b2118c49 2020-07-28 stsp
2291 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2292 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2293 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2294 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2295 b2118c49 2020-07-28 stsp }
2296 b2118c49 2020-07-28 stsp
2297 b2118c49 2020-07-28 stsp return NULL;
2298 b2118c49 2020-07-28 stsp }
2299 b2118c49 2020-07-28 stsp
2300 818c7501 2019-07-11 stsp static const struct got_error *
2301 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2302 0cd1c46a 2019-03-11 stsp {
2303 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2304 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2305 0cd1c46a 2019-03-11 stsp
2306 517bab73 2019-03-11 stsp *refname = NULL;
2307 517bab73 2019-03-11 stsp
2308 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2309 b2118c49 2020-07-28 stsp if (err)
2310 b2118c49 2020-07-28 stsp return err;
2311 0cd1c46a 2019-03-11 stsp
2312 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2313 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2314 0647c563 2019-03-11 stsp *refname = NULL;
2315 0cd1c46a 2019-03-11 stsp }
2316 517bab73 2019-03-11 stsp free(uuidstr);
2317 517bab73 2019-03-11 stsp return err;
2318 517bab73 2019-03-11 stsp }
2319 517bab73 2019-03-11 stsp
2320 818c7501 2019-07-11 stsp const struct got_error *
2321 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2322 818c7501 2019-07-11 stsp {
2323 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2324 818c7501 2019-07-11 stsp }
2325 818c7501 2019-07-11 stsp
2326 818c7501 2019-07-11 stsp static const struct got_error *
2327 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2328 818c7501 2019-07-11 stsp {
2329 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2330 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2331 818c7501 2019-07-11 stsp }
2332 818c7501 2019-07-11 stsp
2333 818c7501 2019-07-11 stsp static const struct got_error *
2334 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2335 818c7501 2019-07-11 stsp {
2336 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2337 818c7501 2019-07-11 stsp }
2338 818c7501 2019-07-11 stsp
2339 818c7501 2019-07-11 stsp static const struct got_error *
2340 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2341 818c7501 2019-07-11 stsp {
2342 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2343 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2344 818c7501 2019-07-11 stsp }
2345 818c7501 2019-07-11 stsp
2346 818c7501 2019-07-11 stsp static const struct got_error *
2347 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2348 818c7501 2019-07-11 stsp {
2349 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2350 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2351 0ebf8283 2019-07-24 stsp }
2352 0ebf8283 2019-07-24 stsp
2353 0ebf8283 2019-07-24 stsp static const struct got_error *
2354 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2355 0ebf8283 2019-07-24 stsp {
2356 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2357 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2358 0ebf8283 2019-07-24 stsp }
2359 0ebf8283 2019-07-24 stsp
2360 0ebf8283 2019-07-24 stsp static const struct got_error *
2361 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2362 0ebf8283 2019-07-24 stsp {
2363 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2364 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2365 0ebf8283 2019-07-24 stsp }
2366 0ebf8283 2019-07-24 stsp
2367 0ebf8283 2019-07-24 stsp static const struct got_error *
2368 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2369 0ebf8283 2019-07-24 stsp {
2370 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2371 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2372 818c7501 2019-07-11 stsp }
2373 818c7501 2019-07-11 stsp
2374 0ebf8283 2019-07-24 stsp static const struct got_error *
2375 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2376 0ebf8283 2019-07-24 stsp {
2377 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2378 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2379 0ebf8283 2019-07-24 stsp }
2380 818c7501 2019-07-11 stsp
2381 0ebf8283 2019-07-24 stsp const struct got_error *
2382 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2383 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2384 0ebf8283 2019-07-24 stsp {
2385 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2386 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2387 0ebf8283 2019-07-24 stsp *path = NULL;
2388 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2389 0ebf8283 2019-07-24 stsp }
2390 0ebf8283 2019-07-24 stsp return NULL;
2391 10604dce 2021-09-24 thomas }
2392 10604dce 2021-09-24 thomas
2393 10604dce 2021-09-24 thomas static const struct got_error *
2394 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2395 10604dce 2021-09-24 thomas {
2396 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2397 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2398 0ebf8283 2019-07-24 stsp }
2399 0ebf8283 2019-07-24 stsp
2400 10604dce 2021-09-24 thomas static const struct got_error *
2401 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2402 10604dce 2021-09-24 thomas {
2403 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2404 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2405 10604dce 2021-09-24 thomas }
2406 10604dce 2021-09-24 thomas
2407 517bab73 2019-03-11 stsp /*
2408 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2409 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2410 517bab73 2019-03-11 stsp */
2411 517bab73 2019-03-11 stsp static const struct got_error *
2412 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2413 517bab73 2019-03-11 stsp {
2414 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2415 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2416 517bab73 2019-03-11 stsp char *refname;
2417 517bab73 2019-03-11 stsp
2418 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2419 517bab73 2019-03-11 stsp if (err)
2420 517bab73 2019-03-11 stsp return err;
2421 0cd1c46a 2019-03-11 stsp
2422 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2423 0cd1c46a 2019-03-11 stsp if (err)
2424 0cd1c46a 2019-03-11 stsp goto done;
2425 0cd1c46a 2019-03-11 stsp
2426 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2427 0cd1c46a 2019-03-11 stsp done:
2428 0cd1c46a 2019-03-11 stsp free(refname);
2429 0cd1c46a 2019-03-11 stsp if (ref)
2430 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2431 3e3a69f1 2019-07-25 stsp return err;
2432 3e3a69f1 2019-07-25 stsp }
2433 3e3a69f1 2019-07-25 stsp
2434 3e3a69f1 2019-07-25 stsp static const struct got_error *
2435 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2436 3e3a69f1 2019-07-25 stsp {
2437 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2438 3e3a69f1 2019-07-25 stsp
2439 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2440 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2441 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2442 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2443 3e3a69f1 2019-07-25 stsp }
2444 9d31a1d8 2018-03-11 stsp return err;
2445 9d31a1d8 2018-03-11 stsp }
2446 9d31a1d8 2018-03-11 stsp
2447 3e3a69f1 2019-07-25 stsp
2448 ebf99748 2019-05-09 stsp static const struct got_error *
2449 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2450 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2451 ebf99748 2019-05-09 stsp {
2452 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2453 ebf99748 2019-05-09 stsp FILE *index = NULL;
2454 0cd1c46a 2019-03-11 stsp
2455 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2456 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2457 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2458 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2459 ebf99748 2019-05-09 stsp
2460 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2461 3e3a69f1 2019-07-25 stsp if (err)
2462 ebf99748 2019-05-09 stsp goto done;
2463 ebf99748 2019-05-09 stsp
2464 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2465 ebf99748 2019-05-09 stsp if (index == NULL) {
2466 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2467 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2468 ebf99748 2019-05-09 stsp } else {
2469 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2470 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2471 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2472 ebf99748 2019-05-09 stsp }
2473 ebf99748 2019-05-09 stsp done:
2474 ebf99748 2019-05-09 stsp if (err) {
2475 ebf99748 2019-05-09 stsp free(*fileindex_path);
2476 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2477 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2478 ebf99748 2019-05-09 stsp *fileindex = NULL;
2479 ebf99748 2019-05-09 stsp }
2480 ebf99748 2019-05-09 stsp return err;
2481 ebf99748 2019-05-09 stsp }
2482 c932eeeb 2019-05-22 stsp
2483 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2484 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2485 c932eeeb 2019-05-22 stsp const char *path;
2486 c932eeeb 2019-05-22 stsp size_t path_len;
2487 c932eeeb 2019-05-22 stsp const char *entry_name;
2488 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2489 a484d721 2019-06-10 stsp void *progress_arg;
2490 c932eeeb 2019-05-22 stsp };
2491 ebf99748 2019-05-09 stsp
2492 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2493 c932eeeb 2019-05-22 stsp static const struct got_error *
2494 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2495 c932eeeb 2019-05-22 stsp {
2496 1ee397ad 2019-07-12 stsp const struct got_error *err;
2497 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2498 c932eeeb 2019-05-22 stsp
2499 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2500 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2501 c932eeeb 2019-05-22 stsp return NULL;
2502 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2503 102ff934 2019-06-11 stsp return NULL;
2504 102ff934 2019-06-11 stsp
2505 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2506 a769b60b 2021-06-27 stsp return NULL;
2507 a769b60b 2021-06-27 stsp
2508 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2509 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2510 c932eeeb 2019-05-22 stsp return NULL;
2511 c932eeeb 2019-05-22 stsp
2512 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2513 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2514 edd02c5e 2019-07-11 stsp ie->path);
2515 1ee397ad 2019-07-12 stsp if (err)
2516 1ee397ad 2019-07-12 stsp return err;
2517 1ee397ad 2019-07-12 stsp }
2518 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2519 c932eeeb 2019-05-22 stsp return NULL;
2520 a615e0e7 2020-12-16 stsp }
2521 a615e0e7 2020-12-16 stsp
2522 a615e0e7 2020-12-16 stsp static const struct got_error *
2523 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2524 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2525 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2526 a615e0e7 2020-12-16 stsp {
2527 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2528 a615e0e7 2020-12-16 stsp
2529 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2530 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2531 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2532 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2533 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2534 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2535 a615e0e7 2020-12-16 stsp
2536 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2537 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2538 9c6338c4 2019-06-02 stsp }
2539 9c6338c4 2019-06-02 stsp
2540 9c6338c4 2019-06-02 stsp static const struct got_error *
2541 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2542 9c6338c4 2019-06-02 stsp {
2543 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2544 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2545 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2546 867630bb 2020-01-17 stsp struct timespec timeout;
2547 9c6338c4 2019-06-02 stsp
2548 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2549 9c6338c4 2019-06-02 stsp fileindex_path);
2550 9c6338c4 2019-06-02 stsp if (err)
2551 9c6338c4 2019-06-02 stsp goto done;
2552 9c6338c4 2019-06-02 stsp
2553 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2554 9c6338c4 2019-06-02 stsp if (err)
2555 9c6338c4 2019-06-02 stsp goto done;
2556 9c6338c4 2019-06-02 stsp
2557 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2558 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2559 9c6338c4 2019-06-02 stsp fileindex_path);
2560 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2561 9c6338c4 2019-06-02 stsp }
2562 867630bb 2020-01-17 stsp
2563 867630bb 2020-01-17 stsp /*
2564 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2565 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2566 867630bb 2020-01-17 stsp * was recorded in the file index.
2567 867630bb 2020-01-17 stsp */
2568 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2569 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2570 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2571 9c6338c4 2019-06-02 stsp done:
2572 9c6338c4 2019-06-02 stsp if (new_index)
2573 9c6338c4 2019-06-02 stsp fclose(new_index);
2574 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2575 9c6338c4 2019-06-02 stsp return err;
2576 c932eeeb 2019-05-22 stsp }
2577 6ced4176 2019-07-12 stsp
2578 6ced4176 2019-07-12 stsp static const struct got_error *
2579 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2580 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2581 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2582 6ced4176 2019-07-12 stsp {
2583 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2584 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2585 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2586 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2587 6ced4176 2019-07-12 stsp
2588 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2589 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2590 6ced4176 2019-07-12 stsp *tree_id = NULL;
2591 6ced4176 2019-07-12 stsp
2592 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2593 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2594 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2595 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2596 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2597 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2598 6ced4176 2019-07-12 stsp goto done;
2599 6ced4176 2019-07-12 stsp }
2600 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2601 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2602 6ced4176 2019-07-12 stsp if (err)
2603 6ced4176 2019-07-12 stsp goto done;
2604 6ced4176 2019-07-12 stsp return NULL;
2605 6ced4176 2019-07-12 stsp }
2606 6ced4176 2019-07-12 stsp
2607 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2608 6ced4176 2019-07-12 stsp
2609 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2610 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2611 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2612 6ced4176 2019-07-12 stsp goto done;
2613 6ced4176 2019-07-12 stsp }
2614 6ced4176 2019-07-12 stsp
2615 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2616 6ced4176 2019-07-12 stsp in_repo_path);
2617 6ced4176 2019-07-12 stsp if (err)
2618 6ced4176 2019-07-12 stsp goto done;
2619 6ced4176 2019-07-12 stsp
2620 6ced4176 2019-07-12 stsp free(in_repo_path);
2621 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2622 6ced4176 2019-07-12 stsp
2623 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2624 6ced4176 2019-07-12 stsp if (err)
2625 6ced4176 2019-07-12 stsp goto done;
2626 c932eeeb 2019-05-22 stsp
2627 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2628 6ced4176 2019-07-12 stsp /* Check out a single file. */
2629 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2630 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2631 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2632 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2633 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2634 6ced4176 2019-07-12 stsp goto done;
2635 6ced4176 2019-07-12 stsp }
2636 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2637 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2638 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2639 6ced4176 2019-07-12 stsp goto done;
2640 6ced4176 2019-07-12 stsp }
2641 6ced4176 2019-07-12 stsp } else {
2642 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2643 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2644 6ced4176 2019-07-12 stsp if (err)
2645 6ced4176 2019-07-12 stsp return err;
2646 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2647 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2648 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2649 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2650 6ced4176 2019-07-12 stsp goto done;
2651 6ced4176 2019-07-12 stsp }
2652 6ced4176 2019-07-12 stsp }
2653 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2654 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2655 6ced4176 2019-07-12 stsp } else {
2656 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2657 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2658 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2659 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2660 6ced4176 2019-07-12 stsp goto done;
2661 6ced4176 2019-07-12 stsp }
2662 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2663 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2664 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2665 6ced4176 2019-07-12 stsp goto done;
2666 6ced4176 2019-07-12 stsp }
2667 6ced4176 2019-07-12 stsp }
2668 6ced4176 2019-07-12 stsp done:
2669 6ced4176 2019-07-12 stsp free(id);
2670 6ced4176 2019-07-12 stsp free(in_repo_path);
2671 6ced4176 2019-07-12 stsp if (err) {
2672 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2673 6ced4176 2019-07-12 stsp free(*tree_relpath);
2674 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2675 6ced4176 2019-07-12 stsp free(*tree_id);
2676 6ced4176 2019-07-12 stsp *tree_id = NULL;
2677 6ced4176 2019-07-12 stsp }
2678 07ed472d 2019-07-12 stsp return err;
2679 07ed472d 2019-07-12 stsp }
2680 07ed472d 2019-07-12 stsp
2681 07ed472d 2019-07-12 stsp static const struct got_error *
2682 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2683 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2684 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2685 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2686 07ed472d 2019-07-12 stsp {
2687 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2688 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2689 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2690 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2691 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2692 07ed472d 2019-07-12 stsp
2693 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2694 7f47418f 2019-12-20 stsp if (err) {
2695 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2696 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2697 7f47418f 2019-12-20 stsp goto done;
2698 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2699 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2700 7f47418f 2019-12-20 stsp if (err)
2701 7f47418f 2019-12-20 stsp return err;
2702 7f47418f 2019-12-20 stsp }
2703 07ed472d 2019-07-12 stsp
2704 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2705 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2706 07ed472d 2019-07-12 stsp if (err)
2707 07ed472d 2019-07-12 stsp goto done;
2708 07ed472d 2019-07-12 stsp
2709 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2710 07ed472d 2019-07-12 stsp if (err)
2711 07ed472d 2019-07-12 stsp goto done;
2712 07ed472d 2019-07-12 stsp
2713 07ed472d 2019-07-12 stsp if (entry_name &&
2714 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2715 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2716 07ed472d 2019-07-12 stsp goto done;
2717 07ed472d 2019-07-12 stsp }
2718 07ed472d 2019-07-12 stsp
2719 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2720 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2721 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2722 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2723 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2724 07ed472d 2019-07-12 stsp arg.repo = repo;
2725 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2726 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2727 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2728 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2729 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2730 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2731 07ed472d 2019-07-12 stsp done:
2732 07ed472d 2019-07-12 stsp if (tree)
2733 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2734 07ed472d 2019-07-12 stsp if (commit)
2735 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2736 6ced4176 2019-07-12 stsp return err;
2737 6ced4176 2019-07-12 stsp }
2738 6ced4176 2019-07-12 stsp
2739 9d31a1d8 2018-03-11 stsp const struct got_error *
2740 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2741 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2742 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2743 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2744 9d31a1d8 2018-03-11 stsp {
2745 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2746 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2747 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2748 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2749 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2750 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2751 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2752 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2753 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2754 f2ea84fa 2019-07-27 stsp int entry_type;
2755 f2ea84fa 2019-07-27 stsp char *relpath;
2756 f2ea84fa 2019-07-27 stsp char *entry_name;
2757 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2758 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2759 9d31a1d8 2018-03-11 stsp
2760 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2761 f2ea84fa 2019-07-27 stsp
2762 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2763 9d31a1d8 2018-03-11 stsp if (err)
2764 9d31a1d8 2018-03-11 stsp return err;
2765 93a30277 2018-12-24 stsp
2766 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2767 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2768 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2769 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2770 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2771 f2ea84fa 2019-07-27 stsp goto done;
2772 f2ea84fa 2019-07-27 stsp }
2773 6ced4176 2019-07-12 stsp
2774 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2775 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2776 f2ea84fa 2019-07-27 stsp if (err) {
2777 f2ea84fa 2019-07-27 stsp free(tpd);
2778 6ced4176 2019-07-12 stsp goto done;
2779 6ced4176 2019-07-12 stsp }
2780 f2ea84fa 2019-07-27 stsp
2781 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2782 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2783 f2ea84fa 2019-07-27 stsp if (err) {
2784 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2785 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2786 f2ea84fa 2019-07-27 stsp free(tpd);
2787 f2ea84fa 2019-07-27 stsp goto done;
2788 f2ea84fa 2019-07-27 stsp }
2789 f2ea84fa 2019-07-27 stsp } else
2790 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2791 f2ea84fa 2019-07-27 stsp
2792 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2793 6ced4176 2019-07-12 stsp }
2794 6ced4176 2019-07-12 stsp
2795 51514078 2018-12-25 stsp /*
2796 51514078 2018-12-25 stsp * Read the file index.
2797 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2798 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2799 51514078 2018-12-25 stsp */
2800 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2801 8da9e5f4 2019-01-12 stsp if (err)
2802 c4cdcb68 2019-04-03 stsp goto done;
2803 c4cdcb68 2019-04-03 stsp
2804 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2805 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2806 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2807 f2ea84fa 2019-07-27 stsp
2808 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2809 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2810 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2811 f2ea84fa 2019-07-27 stsp if (err)
2812 f2ea84fa 2019-07-27 stsp break;
2813 f2ea84fa 2019-07-27 stsp
2814 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2815 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2816 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2817 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2818 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2819 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2820 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2821 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2822 f2ea84fa 2019-07-27 stsp if (err)
2823 f2ea84fa 2019-07-27 stsp break;
2824 f2ea84fa 2019-07-27 stsp
2825 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2826 711d9cd9 2019-07-12 stsp }
2827 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2828 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2829 af12c6b9 2019-06-04 stsp err = sync_err;
2830 9d31a1d8 2018-03-11 stsp done:
2831 9c6338c4 2019-06-02 stsp free(fileindex_path);
2832 edfa7d7f 2018-09-11 stsp if (tree)
2833 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2834 9d31a1d8 2018-03-11 stsp if (commit)
2835 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2836 5ade8233 2019-07-12 stsp if (fileindex)
2837 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2838 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2839 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2840 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2841 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2842 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2843 f2ea84fa 2019-07-27 stsp free(tpd);
2844 f2ea84fa 2019-07-27 stsp }
2845 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2846 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2847 9d31a1d8 2018-03-11 stsp err = unlockerr;
2848 f8d1f275 2019-02-04 stsp return err;
2849 f8d1f275 2019-02-04 stsp }
2850 f8d1f275 2019-02-04 stsp
2851 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2852 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2853 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2854 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2855 234035bc 2019-06-01 stsp void *progress_arg;
2856 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2857 234035bc 2019-06-01 stsp void *cancel_arg;
2858 f69721c3 2019-10-21 stsp const char *label_orig;
2859 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2860 234035bc 2019-06-01 stsp };
2861 234035bc 2019-06-01 stsp
2862 234035bc 2019-06-01 stsp static const struct got_error *
2863 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2864 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2865 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2866 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2867 234035bc 2019-06-01 stsp {
2868 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2869 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2870 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2871 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2872 234035bc 2019-06-01 stsp struct stat sb;
2873 234035bc 2019-06-01 stsp unsigned char status;
2874 234035bc 2019-06-01 stsp int local_changes_subsumed;
2875 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2876 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2877 234035bc 2019-06-01 stsp
2878 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2879 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2880 d6c87207 2019-08-02 stsp strlen(path2));
2881 1ee397ad 2019-07-12 stsp if (ie == NULL)
2882 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2883 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2884 234035bc 2019-06-01 stsp
2885 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2886 234035bc 2019-06-01 stsp path2) == -1)
2887 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2888 234035bc 2019-06-01 stsp
2889 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2890 7f91a133 2019-12-13 stsp repo);
2891 234035bc 2019-06-01 stsp if (err)
2892 234035bc 2019-06-01 stsp goto done;
2893 234035bc 2019-06-01 stsp
2894 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2895 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2896 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2897 234035bc 2019-06-01 stsp goto done;
2898 234035bc 2019-06-01 stsp }
2899 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2900 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2901 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2902 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2903 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2904 234035bc 2019-06-01 stsp goto done;
2905 234035bc 2019-06-01 stsp }
2906 234035bc 2019-06-01 stsp
2907 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2908 36bf999c 2020-07-23 stsp char *link_target2;
2909 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2910 36bf999c 2020-07-23 stsp if (err)
2911 36bf999c 2020-07-23 stsp goto done;
2912 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2913 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2914 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2915 36bf999c 2020-07-23 stsp free(link_target2);
2916 af57b12a 2020-07-23 stsp } else {
2917 1af628f4 2021-06-03 stsp int fd;
2918 1af628f4 2021-06-03 stsp
2919 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2920 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2921 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2922 1af628f4 2021-06-03 stsp goto done;
2923 1af628f4 2021-06-03 stsp }
2924 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2925 1af628f4 2021-06-03 stsp f_orig, blob1);
2926 1af628f4 2021-06-03 stsp if (err)
2927 1af628f4 2021-06-03 stsp goto done;
2928 1af628f4 2021-06-03 stsp
2929 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2930 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2931 1af628f4 2021-06-03 stsp goto done;
2932 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2933 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2934 1af628f4 2021-06-03 stsp if (err)
2935 1af628f4 2021-06-03 stsp goto done;
2936 1af628f4 2021-06-03 stsp
2937 1af628f4 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2938 1af628f4 2021-06-03 stsp if (fd == -1) {
2939 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2940 1af628f4 2021-06-03 stsp ondisk_path);
2941 1af628f4 2021-06-03 stsp goto done;
2942 1af628f4 2021-06-03 stsp }
2943 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2944 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2945 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2946 1af628f4 2021-06-03 stsp ondisk_path);
2947 1af628f4 2021-06-03 stsp close(fd);
2948 1af628f4 2021-06-03 stsp goto done;
2949 1af628f4 2021-06-03 stsp }
2950 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2951 1af628f4 2021-06-03 stsp if (err)
2952 1af628f4 2021-06-03 stsp goto done;
2953 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2954 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2955 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2956 1af628f4 2021-06-03 stsp goto done;
2957 1af628f4 2021-06-03 stsp }
2958 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2959 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2960 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2961 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2962 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2963 af57b12a 2020-07-23 stsp }
2964 234035bc 2019-06-01 stsp } else if (blob1) {
2965 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2966 d6c87207 2019-08-02 stsp strlen(path1));
2967 1ee397ad 2019-07-12 stsp if (ie == NULL)
2968 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2969 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2970 2b92fad7 2019-06-02 stsp
2971 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2972 2b92fad7 2019-06-02 stsp path1) == -1)
2973 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2974 2b92fad7 2019-06-02 stsp
2975 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2976 7f91a133 2019-12-13 stsp repo);
2977 2b92fad7 2019-06-02 stsp if (err)
2978 2b92fad7 2019-06-02 stsp goto done;
2979 2b92fad7 2019-06-02 stsp
2980 2b92fad7 2019-06-02 stsp switch (status) {
2981 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2982 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2983 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2984 1ee397ad 2019-07-12 stsp if (err)
2985 1ee397ad 2019-07-12 stsp goto done;
2986 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2987 2b92fad7 2019-06-02 stsp if (err)
2988 2b92fad7 2019-06-02 stsp goto done;
2989 2b92fad7 2019-06-02 stsp if (ie)
2990 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2991 2b92fad7 2019-06-02 stsp break;
2992 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2993 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2994 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2995 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2996 1ee397ad 2019-07-12 stsp if (err)
2997 1ee397ad 2019-07-12 stsp goto done;
2998 2b92fad7 2019-06-02 stsp if (ie)
2999 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3000 2b92fad7 2019-06-02 stsp break;
3001 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3002 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3003 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3004 0a22ca1a 2020-09-23 stsp /*
3005 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3006 0a22ca1a 2020-09-23 stsp * exists in the repository.
3007 0a22ca1a 2020-09-23 stsp */
3008 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
3009 0a22ca1a 2020-09-23 stsp if (err)
3010 0a22ca1a 2020-09-23 stsp goto done;
3011 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3012 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3013 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3014 0a22ca1a 2020-09-23 stsp if (err)
3015 0a22ca1a 2020-09-23 stsp goto done;
3016 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3017 0a22ca1a 2020-09-23 stsp path1);
3018 0a22ca1a 2020-09-23 stsp if (err)
3019 0a22ca1a 2020-09-23 stsp goto done;
3020 0a22ca1a 2020-09-23 stsp if (ie)
3021 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3022 0a22ca1a 2020-09-23 stsp ie);
3023 0a22ca1a 2020-09-23 stsp } else {
3024 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3025 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3026 0a22ca1a 2020-09-23 stsp }
3027 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3028 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3029 0a22ca1a 2020-09-23 stsp free(id);
3030 0a22ca1a 2020-09-23 stsp if (err)
3031 0a22ca1a 2020-09-23 stsp goto done;
3032 0a22ca1a 2020-09-23 stsp break;
3033 0a22ca1a 2020-09-23 stsp }
3034 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3035 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3036 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3037 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3038 1ee397ad 2019-07-12 stsp if (err)
3039 1ee397ad 2019-07-12 stsp goto done;
3040 2b92fad7 2019-06-02 stsp break;
3041 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3042 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3043 1ee397ad 2019-07-12 stsp if (err)
3044 1ee397ad 2019-07-12 stsp goto done;
3045 2b92fad7 2019-06-02 stsp break;
3046 2b92fad7 2019-06-02 stsp default:
3047 2b92fad7 2019-06-02 stsp break;
3048 2b92fad7 2019-06-02 stsp }
3049 234035bc 2019-06-01 stsp } else if (blob2) {
3050 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3051 234035bc 2019-06-01 stsp path2) == -1)
3052 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3053 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3054 d6c87207 2019-08-02 stsp strlen(path2));
3055 234035bc 2019-06-01 stsp if (ie) {
3056 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3057 7f91a133 2019-12-13 stsp -1, NULL, repo);
3058 234035bc 2019-06-01 stsp if (err)
3059 234035bc 2019-06-01 stsp goto done;
3060 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3061 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3062 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3063 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3064 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3065 1ee397ad 2019-07-12 stsp status, path2);
3066 234035bc 2019-06-01 stsp goto done;
3067 234035bc 2019-06-01 stsp }
3068 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3069 36bf999c 2020-07-23 stsp char *link_target2;
3070 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3071 36bf999c 2020-07-23 stsp blob2);
3072 36bf999c 2020-07-23 stsp if (err)
3073 36bf999c 2020-07-23 stsp goto done;
3074 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3075 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3076 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3077 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3078 36bf999c 2020-07-23 stsp free(link_target2);
3079 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3080 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3081 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3082 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3083 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3084 526a746f 2020-07-23 stsp a->progress_arg);
3085 dfe9fba0 2020-07-23 stsp } else {
3086 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3087 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3088 526a746f 2020-07-23 stsp }
3089 dfe9fba0 2020-07-23 stsp if (err)
3090 dfe9fba0 2020-07-23 stsp goto done;
3091 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3092 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3093 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3094 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3095 234035bc 2019-06-01 stsp if (err)
3096 234035bc 2019-06-01 stsp goto done;
3097 234035bc 2019-06-01 stsp }
3098 234035bc 2019-06-01 stsp } else {
3099 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3100 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3101 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3102 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3103 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3104 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
3105 2e1fa222 2020-07-23 stsp } else {
3106 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3107 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3108 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3109 2e1fa222 2020-07-23 stsp }
3110 234035bc 2019-06-01 stsp if (err)
3111 234035bc 2019-06-01 stsp goto done;
3112 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3113 234035bc 2019-06-01 stsp if (err)
3114 234035bc 2019-06-01 stsp goto done;
3115 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3116 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3117 3969253a 2020-03-07 stsp if (err) {
3118 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3119 3969253a 2020-03-07 stsp goto done;
3120 3969253a 2020-03-07 stsp }
3121 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3122 2b92fad7 2019-06-02 stsp if (err) {
3123 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3124 2b92fad7 2019-06-02 stsp goto done;
3125 2b92fad7 2019-06-02 stsp }
3126 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3127 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3128 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3129 2e1fa222 2020-07-23 stsp }
3130 234035bc 2019-06-01 stsp }
3131 234035bc 2019-06-01 stsp }
3132 234035bc 2019-06-01 stsp done:
3133 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3134 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3135 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3136 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3137 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3138 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3139 1af628f4 2021-06-03 stsp free(id_str);
3140 54d5be07 2021-06-03 stsp free(label_deriv2);
3141 234035bc 2019-06-01 stsp free(ondisk_path);
3142 234035bc 2019-06-01 stsp return err;
3143 234035bc 2019-06-01 stsp }
3144 234035bc 2019-06-01 stsp
3145 69de9dd4 2021-09-03 stsp static const struct got_error *
3146 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3147 69de9dd4 2021-09-03 stsp {
3148 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3149 69de9dd4 2021-09-03 stsp
3150 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3151 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3152 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3153 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3154 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3155 69de9dd4 2021-09-03 stsp
3156 69de9dd4 2021-09-03 stsp return NULL;
3157 69de9dd4 2021-09-03 stsp }
3158 69de9dd4 2021-09-03 stsp
3159 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3160 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3161 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3162 234035bc 2019-06-01 stsp struct got_repository *repo;
3163 234035bc 2019-06-01 stsp };
3164 234035bc 2019-06-01 stsp
3165 234035bc 2019-06-01 stsp static const struct got_error *
3166 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3167 69de9dd4 2021-09-03 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3168 69de9dd4 2021-09-03 stsp struct got_object_id *id2, const char *path1, const char *path2,
3169 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3170 234035bc 2019-06-01 stsp {
3171 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3172 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3173 234035bc 2019-06-01 stsp unsigned char status;
3174 234035bc 2019-06-01 stsp struct stat sb;
3175 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3176 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3177 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3178 234035bc 2019-06-01 stsp char *ondisk_path;
3179 234035bc 2019-06-01 stsp
3180 69de9dd4 2021-09-03 stsp if (id == NULL)
3181 69de9dd4 2021-09-03 stsp return NULL;
3182 234035bc 2019-06-01 stsp
3183 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3184 69de9dd4 2021-09-03 stsp if (ie == NULL)
3185 69de9dd4 2021-09-03 stsp return NULL;
3186 69de9dd4 2021-09-03 stsp
3187 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3188 234035bc 2019-06-01 stsp == -1)
3189 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3190 234035bc 2019-06-01 stsp
3191 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3192 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3193 5546d466 2021-09-02 stsp free(ondisk_path);
3194 234035bc 2019-06-01 stsp if (err)
3195 234035bc 2019-06-01 stsp return err;
3196 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3197 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3198 234035bc 2019-06-01 stsp
3199 234035bc 2019-06-01 stsp return NULL;
3200 234035bc 2019-06-01 stsp }
3201 234035bc 2019-06-01 stsp
3202 818c7501 2019-07-11 stsp static const struct got_error *
3203 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3204 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3205 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3206 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3207 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3208 234035bc 2019-06-01 stsp {
3209 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3210 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3211 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3212 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3213 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3214 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3215 234035bc 2019-06-01 stsp
3216 03415a1a 2019-06-02 stsp if (commit_id1) {
3217 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3218 03415a1a 2019-06-02 stsp worktree->path_prefix);
3219 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3220 03415a1a 2019-06-02 stsp goto done;
3221 69d57f3d 2020-07-31 stsp }
3222 69d57f3d 2020-07-31 stsp if (tree_id1) {
3223 69d57f3d 2020-07-31 stsp char *id_str;
3224 03415a1a 2019-06-02 stsp
3225 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3226 03415a1a 2019-06-02 stsp if (err)
3227 03415a1a 2019-06-02 stsp goto done;
3228 f69721c3 2019-10-21 stsp
3229 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3230 f69721c3 2019-10-21 stsp if (err)
3231 f69721c3 2019-10-21 stsp goto done;
3232 f69721c3 2019-10-21 stsp
3233 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3234 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3235 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3236 f69721c3 2019-10-21 stsp free(id_str);
3237 f69721c3 2019-10-21 stsp goto done;
3238 f69721c3 2019-10-21 stsp }
3239 f69721c3 2019-10-21 stsp free(id_str);
3240 03415a1a 2019-06-02 stsp }
3241 234035bc 2019-06-01 stsp
3242 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3243 234035bc 2019-06-01 stsp worktree->path_prefix);
3244 234035bc 2019-06-01 stsp if (err)
3245 234035bc 2019-06-01 stsp goto done;
3246 234035bc 2019-06-01 stsp
3247 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3248 234035bc 2019-06-01 stsp if (err)
3249 234035bc 2019-06-01 stsp goto done;
3250 234035bc 2019-06-01 stsp
3251 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3252 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3253 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3254 69de9dd4 2021-09-03 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3255 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3256 69de9dd4 2021-09-03 stsp if (err)
3257 69de9dd4 2021-09-03 stsp goto done;
3258 69de9dd4 2021-09-03 stsp
3259 234035bc 2019-06-01 stsp arg.worktree = worktree;
3260 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3261 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3262 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3263 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3264 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3265 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3266 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3267 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3268 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3269 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3270 af12c6b9 2019-06-04 stsp err = sync_err;
3271 234035bc 2019-06-01 stsp done:
3272 234035bc 2019-06-01 stsp if (tree1)
3273 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3274 234035bc 2019-06-01 stsp if (tree2)
3275 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3276 f69721c3 2019-10-21 stsp free(label_orig);
3277 818c7501 2019-07-11 stsp return err;
3278 818c7501 2019-07-11 stsp }
3279 234035bc 2019-06-01 stsp
3280 818c7501 2019-07-11 stsp const struct got_error *
3281 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3282 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3283 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3284 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3285 818c7501 2019-07-11 stsp {
3286 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3287 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3288 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3289 818c7501 2019-07-11 stsp
3290 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3291 818c7501 2019-07-11 stsp if (err)
3292 818c7501 2019-07-11 stsp return err;
3293 818c7501 2019-07-11 stsp
3294 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3295 818c7501 2019-07-11 stsp if (err)
3296 818c7501 2019-07-11 stsp goto done;
3297 818c7501 2019-07-11 stsp
3298 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3299 69de9dd4 2021-09-03 stsp worktree);
3300 818c7501 2019-07-11 stsp if (err)
3301 818c7501 2019-07-11 stsp goto done;
3302 818c7501 2019-07-11 stsp
3303 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3304 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3305 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3306 818c7501 2019-07-11 stsp done:
3307 818c7501 2019-07-11 stsp if (fileindex)
3308 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3309 818c7501 2019-07-11 stsp free(fileindex_path);
3310 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3311 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3312 234035bc 2019-06-01 stsp err = unlockerr;
3313 234035bc 2019-06-01 stsp return err;
3314 234035bc 2019-06-01 stsp }
3315 234035bc 2019-06-01 stsp
3316 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3317 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3318 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3319 927df6b7 2019-02-10 stsp const char *status_path;
3320 927df6b7 2019-02-10 stsp size_t status_path_len;
3321 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3322 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3323 f8d1f275 2019-02-04 stsp void *status_arg;
3324 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3325 f8d1f275 2019-02-04 stsp void *cancel_arg;
3326 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3327 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3328 f2a9dc41 2019-12-13 tracey int report_unchanged;
3329 3143d852 2020-06-25 stsp int no_ignores;
3330 f8d1f275 2019-02-04 stsp };
3331 88d0e355 2019-08-03 stsp
3332 f8d1f275 2019-02-04 stsp static const struct got_error *
3333 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3334 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3335 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3336 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3337 927df6b7 2019-02-10 stsp {
3338 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3339 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3340 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3341 927df6b7 2019-02-10 stsp struct stat sb;
3342 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3343 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3344 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3345 927df6b7 2019-02-10 stsp
3346 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3347 98eaaa12 2019-08-03 stsp if (err)
3348 98eaaa12 2019-08-03 stsp return err;
3349 98eaaa12 2019-08-03 stsp
3350 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3351 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3352 98eaaa12 2019-08-03 stsp return NULL;
3353 98eaaa12 2019-08-03 stsp
3354 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3355 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3356 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3357 98eaaa12 2019-08-03 stsp }
3358 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3359 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3360 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3361 927df6b7 2019-02-10 stsp }
3362 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3363 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3364 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3365 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3366 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3367 98eaaa12 2019-08-03 stsp }
3368 98eaaa12 2019-08-03 stsp
3369 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3370 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3371 927df6b7 2019-02-10 stsp }
3372 927df6b7 2019-02-10 stsp
3373 927df6b7 2019-02-10 stsp static const struct got_error *
3374 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3375 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3376 f8d1f275 2019-02-04 stsp {
3377 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3378 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3379 f8d1f275 2019-02-04 stsp char *abspath;
3380 f8d1f275 2019-02-04 stsp
3381 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3382 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3383 0584f854 2019-04-06 stsp
3384 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3385 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3386 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3387 927df6b7 2019-02-10 stsp return NULL;
3388 927df6b7 2019-02-10 stsp
3389 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3390 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3391 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3392 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3393 f8d1f275 2019-02-04 stsp } else {
3394 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3395 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3396 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3397 f8d1f275 2019-02-04 stsp }
3398 f8d1f275 2019-02-04 stsp
3399 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3400 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3401 f8d1f275 2019-02-04 stsp free(abspath);
3402 9d31a1d8 2018-03-11 stsp return err;
3403 9d31a1d8 2018-03-11 stsp }
3404 f8d1f275 2019-02-04 stsp
3405 f8d1f275 2019-02-04 stsp static const struct got_error *
3406 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3407 f8d1f275 2019-02-04 stsp {
3408 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3409 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3410 2ec1f75b 2019-03-26 stsp unsigned char status;
3411 927df6b7 2019-02-10 stsp
3412 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3413 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3414 0584f854 2019-04-06 stsp
3415 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3416 927df6b7 2019-02-10 stsp return NULL;
3417 927df6b7 2019-02-10 stsp
3418 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3419 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3420 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3421 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3422 2ec1f75b 2019-03-26 stsp else
3423 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3424 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3425 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3426 6841da00 2019-08-08 stsp }
3427 6841da00 2019-08-08 stsp
3428 6841da00 2019-08-08 stsp void
3429 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3430 6841da00 2019-08-08 stsp {
3431 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3432 6841da00 2019-08-08 stsp
3433 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3434 6841da00 2019-08-08 stsp free((char *)pe->path);
3435 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3436 6841da00 2019-08-08 stsp }
3437 6841da00 2019-08-08 stsp
3438 6841da00 2019-08-08 stsp void
3439 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3440 6841da00 2019-08-08 stsp {
3441 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3442 6841da00 2019-08-08 stsp
3443 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3444 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3445 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3446 6841da00 2019-08-08 stsp free((char *)pe->path);
3447 6841da00 2019-08-08 stsp }
3448 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3449 6841da00 2019-08-08 stsp }
3450 6841da00 2019-08-08 stsp
3451 6841da00 2019-08-08 stsp static const struct got_error *
3452 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3453 6841da00 2019-08-08 stsp {
3454 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3455 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3456 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3457 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3458 6841da00 2019-08-08 stsp size_t linesize = 0;
3459 6841da00 2019-08-08 stsp ssize_t linelen;
3460 6841da00 2019-08-08 stsp
3461 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3462 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3463 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3464 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3465 6841da00 2019-08-08 stsp
3466 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3467 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3468 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3469 bd8de430 2019-10-04 stsp
3470 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3471 bd8de430 2019-10-04 stsp if (line[0] == '#')
3472 bd8de430 2019-10-04 stsp continue;
3473 bd8de430 2019-10-04 stsp
3474 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3475 bd8de430 2019-10-04 stsp if (line[0] == '!')
3476 bd8de430 2019-10-04 stsp continue;
3477 bd8de430 2019-10-04 stsp
3478 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3479 6841da00 2019-08-08 stsp line) == -1) {
3480 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3481 6841da00 2019-08-08 stsp goto done;
3482 6841da00 2019-08-08 stsp }
3483 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3484 6841da00 2019-08-08 stsp if (err)
3485 6841da00 2019-08-08 stsp goto done;
3486 6841da00 2019-08-08 stsp }
3487 6841da00 2019-08-08 stsp if (ferror(f)) {
3488 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3489 6841da00 2019-08-08 stsp goto done;
3490 6841da00 2019-08-08 stsp }
3491 6841da00 2019-08-08 stsp
3492 6841da00 2019-08-08 stsp dirpath = strdup(path);
3493 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3494 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3495 6841da00 2019-08-08 stsp goto done;
3496 6841da00 2019-08-08 stsp }
3497 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3498 6841da00 2019-08-08 stsp done:
3499 6841da00 2019-08-08 stsp free(line);
3500 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3501 6841da00 2019-08-08 stsp free(dirpath);
3502 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3503 6841da00 2019-08-08 stsp }
3504 6841da00 2019-08-08 stsp return err;
3505 6841da00 2019-08-08 stsp }
3506 6841da00 2019-08-08 stsp
3507 6841da00 2019-08-08 stsp int
3508 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3509 6841da00 2019-08-08 stsp {
3510 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3511 bd8de430 2019-10-04 stsp
3512 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3513 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3514 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3515 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3516 bd8de430 2019-10-04 stsp
3517 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3518 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3519 6841da00 2019-08-08 stsp
3520 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3521 bd8de430 2019-10-04 stsp continue;
3522 bd8de430 2019-10-04 stsp pattern += 3;
3523 bd8de430 2019-10-04 stsp p = path;
3524 bd8de430 2019-10-04 stsp while (*p) {
3525 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3526 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3527 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3528 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3529 bd8de430 2019-10-04 stsp p++;
3530 bd8de430 2019-10-04 stsp while (*p == '/')
3531 bd8de430 2019-10-04 stsp p++;
3532 bd8de430 2019-10-04 stsp continue;
3533 bd8de430 2019-10-04 stsp }
3534 bd8de430 2019-10-04 stsp return 1;
3535 bd8de430 2019-10-04 stsp }
3536 bd8de430 2019-10-04 stsp }
3537 bd8de430 2019-10-04 stsp }
3538 bd8de430 2019-10-04 stsp
3539 6841da00 2019-08-08 stsp /*
3540 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3541 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3542 6841da00 2019-08-08 stsp * ignores backwards.
3543 6841da00 2019-08-08 stsp */
3544 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3545 6841da00 2019-08-08 stsp while (pe) {
3546 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3547 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3548 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3549 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3550 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3551 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3552 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3553 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3554 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3555 6841da00 2019-08-08 stsp continue;
3556 6841da00 2019-08-08 stsp return 1;
3557 6841da00 2019-08-08 stsp }
3558 6841da00 2019-08-08 stsp }
3559 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3560 6841da00 2019-08-08 stsp }
3561 6841da00 2019-08-08 stsp
3562 6841da00 2019-08-08 stsp return 0;
3563 6841da00 2019-08-08 stsp }
3564 6841da00 2019-08-08 stsp
3565 6841da00 2019-08-08 stsp static const struct got_error *
3566 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3567 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3568 6841da00 2019-08-08 stsp {
3569 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3570 6841da00 2019-08-08 stsp char *ignorespath;
3571 886cec17 2019-12-15 stsp int fd = -1;
3572 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3573 6841da00 2019-08-08 stsp
3574 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3575 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3576 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3577 6841da00 2019-08-08 stsp
3578 886cec17 2019-12-15 stsp if (dirfd != -1) {
3579 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3580 886cec17 2019-12-15 stsp if (fd == -1) {
3581 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3582 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3583 886cec17 2019-12-15 stsp ignorespath);
3584 886cec17 2019-12-15 stsp } else {
3585 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3586 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3587 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3588 886cec17 2019-12-15 stsp ignorespath);
3589 886cec17 2019-12-15 stsp else {
3590 886cec17 2019-12-15 stsp fd = -1;
3591 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3592 886cec17 2019-12-15 stsp }
3593 886cec17 2019-12-15 stsp }
3594 886cec17 2019-12-15 stsp } else {
3595 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3596 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3597 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3598 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3599 886cec17 2019-12-15 stsp ignorespath);
3600 886cec17 2019-12-15 stsp } else
3601 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3602 886cec17 2019-12-15 stsp }
3603 6841da00 2019-08-08 stsp
3604 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3605 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3606 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3607 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3608 6841da00 2019-08-08 stsp free(ignorespath);
3609 6841da00 2019-08-08 stsp return err;
3610 f8d1f275 2019-02-04 stsp }
3611 f8d1f275 2019-02-04 stsp
3612 f8d1f275 2019-02-04 stsp static const struct got_error *
3613 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3614 f8d1f275 2019-02-04 stsp {
3615 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3616 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3617 f8d1f275 2019-02-04 stsp char *path = NULL;
3618 f8d1f275 2019-02-04 stsp
3619 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3620 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3621 0584f854 2019-04-06 stsp
3622 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3623 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3624 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3625 f8d1f275 2019-02-04 stsp } else {
3626 f8d1f275 2019-02-04 stsp path = de->d_name;
3627 f8d1f275 2019-02-04 stsp }
3628 f8d1f275 2019-02-04 stsp
3629 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3630 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3631 ff56836b 2021-07-08 stsp && !match_ignores(a->ignores, path))
3632 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3633 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3634 f8d1f275 2019-02-04 stsp if (parent_path[0])
3635 f8d1f275 2019-02-04 stsp free(path);
3636 b72f483a 2019-02-05 stsp return err;
3637 f8d1f275 2019-02-04 stsp }
3638 f8d1f275 2019-02-04 stsp
3639 347d1d3e 2019-07-12 stsp static const struct got_error *
3640 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3641 3143d852 2020-06-25 stsp {
3642 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3643 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3644 3143d852 2020-06-25 stsp
3645 3143d852 2020-06-25 stsp if (a->no_ignores)
3646 3143d852 2020-06-25 stsp return NULL;
3647 3143d852 2020-06-25 stsp
3648 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3649 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3650 3143d852 2020-06-25 stsp if (err)
3651 3143d852 2020-06-25 stsp return err;
3652 3143d852 2020-06-25 stsp
3653 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3654 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3655 3143d852 2020-06-25 stsp
3656 3143d852 2020-06-25 stsp return err;
3657 3143d852 2020-06-25 stsp }
3658 3143d852 2020-06-25 stsp
3659 3143d852 2020-06-25 stsp static const struct got_error *
3660 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3661 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3662 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3663 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3664 abb4604f 2019-07-27 stsp {
3665 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3666 abb4604f 2019-07-27 stsp struct stat sb;
3667 abb4604f 2019-07-27 stsp
3668 0e33f8e0 2021-09-01 stsp if (!no_ignores && match_ignores(ignores, path))
3669 ff56836b 2021-07-08 stsp return NULL;
3670 ff56836b 2021-07-08 stsp
3671 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3672 abb4604f 2019-07-27 stsp if (ie)
3673 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3674 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3675 abb4604f 2019-07-27 stsp
3676 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3677 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3678 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3679 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3680 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3681 abb4604f 2019-07-27 stsp return NULL;
3682 abb4604f 2019-07-27 stsp }
3683 abb4604f 2019-07-27 stsp
3684 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3685 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3686 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3687 abb4604f 2019-07-27 stsp
3688 abb4604f 2019-07-27 stsp return NULL;
3689 3143d852 2020-06-25 stsp }
3690 3143d852 2020-06-25 stsp
3691 3143d852 2020-06-25 stsp static const struct got_error *
3692 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3693 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3694 3143d852 2020-06-25 stsp {
3695 3143d852 2020-06-25 stsp const struct got_error *err;
3696 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3697 3143d852 2020-06-25 stsp
3698 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3699 3143d852 2020-06-25 stsp ".cvsignore");
3700 3143d852 2020-06-25 stsp if (err)
3701 3143d852 2020-06-25 stsp return err;
3702 3143d852 2020-06-25 stsp
3703 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3704 3143d852 2020-06-25 stsp ".gitignore");
3705 3143d852 2020-06-25 stsp if (err)
3706 3143d852 2020-06-25 stsp return err;
3707 3143d852 2020-06-25 stsp
3708 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3709 3143d852 2020-06-25 stsp if (err) {
3710 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3711 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3712 3143d852 2020-06-25 stsp return err;
3713 3143d852 2020-06-25 stsp }
3714 3143d852 2020-06-25 stsp for (;;) {
3715 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3716 3143d852 2020-06-25 stsp ".cvsignore");
3717 3143d852 2020-06-25 stsp if (err)
3718 3143d852 2020-06-25 stsp break;
3719 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3720 3143d852 2020-06-25 stsp ".gitignore");
3721 3143d852 2020-06-25 stsp if (err)
3722 3143d852 2020-06-25 stsp break;
3723 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3724 3143d852 2020-06-25 stsp if (err) {
3725 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3726 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3727 3143d852 2020-06-25 stsp break;
3728 3143d852 2020-06-25 stsp }
3729 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3730 ff56836b 2021-07-08 stsp break;
3731 b737c85a 2020-06-26 stsp free(parent_path);
3732 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3733 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3734 3143d852 2020-06-25 stsp }
3735 3143d852 2020-06-25 stsp
3736 b737c85a 2020-06-26 stsp free(parent_path);
3737 b737c85a 2020-06-26 stsp free(next_parent_path);
3738 3143d852 2020-06-25 stsp return err;
3739 abb4604f 2019-07-27 stsp }
3740 abb4604f 2019-07-27 stsp
3741 abb4604f 2019-07-27 stsp static const struct got_error *
3742 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3743 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3744 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3745 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3746 f2a9dc41 2019-12-13 tracey int report_unchanged)
3747 f8d1f275 2019-02-04 stsp {
3748 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3749 6fc93f37 2019-12-13 stsp int fd = -1;
3750 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3751 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3752 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3753 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3754 3143d852 2020-06-25 stsp
3755 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3756 f8d1f275 2019-02-04 stsp
3757 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3758 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3759 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3760 8dc303cc 2019-07-27 stsp
3761 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3762 6fc93f37 2019-12-13 stsp if (fd == -1) {
3763 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3764 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3765 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3766 ff56836b 2021-07-08 stsp else {
3767 ff56836b 2021-07-08 stsp if (!no_ignores) {
3768 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3769 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3770 ff56836b 2021-07-08 stsp if (err)
3771 ff56836b 2021-07-08 stsp goto done;
3772 ff56836b 2021-07-08 stsp }
3773 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3774 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3775 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3776 ff56836b 2021-07-08 stsp }
3777 abb4604f 2019-07-27 stsp } else {
3778 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3779 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3780 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3781 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3782 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3783 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3784 abb4604f 2019-07-27 stsp arg.status_path = path;
3785 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3786 abb4604f 2019-07-27 stsp arg.repo = repo;
3787 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3788 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3789 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3790 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3791 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3792 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3793 022fae89 2019-12-06 tracey if (!no_ignores) {
3794 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3795 3143d852 2020-06-25 stsp worktree->root_path, path);
3796 3143d852 2020-06-25 stsp if (err)
3797 3143d852 2020-06-25 stsp goto done;
3798 022fae89 2019-12-06 tracey }
3799 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3800 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3801 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3802 927df6b7 2019-02-10 stsp }
3803 3143d852 2020-06-25 stsp done:
3804 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3805 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3806 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3807 927df6b7 2019-02-10 stsp free(ondisk_path);
3808 347d1d3e 2019-07-12 stsp return err;
3809 347d1d3e 2019-07-12 stsp }
3810 347d1d3e 2019-07-12 stsp
3811 347d1d3e 2019-07-12 stsp const struct got_error *
3812 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3813 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3814 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3815 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3816 347d1d3e 2019-07-12 stsp {
3817 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3818 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3819 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3820 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3821 347d1d3e 2019-07-12 stsp
3822 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3823 347d1d3e 2019-07-12 stsp if (err)
3824 347d1d3e 2019-07-12 stsp return err;
3825 347d1d3e 2019-07-12 stsp
3826 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3827 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3828 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3829 f6343036 2021-06-22 stsp no_ignores, 0);
3830 72ea6654 2019-07-27 stsp if (err)
3831 72ea6654 2019-07-27 stsp break;
3832 72ea6654 2019-07-27 stsp }
3833 f8d1f275 2019-02-04 stsp free(fileindex_path);
3834 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3835 f8d1f275 2019-02-04 stsp return err;
3836 f8d1f275 2019-02-04 stsp }
3837 6c7ab921 2019-03-18 stsp
3838 6c7ab921 2019-03-18 stsp const struct got_error *
3839 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3840 6c7ab921 2019-03-18 stsp const char *arg)
3841 6c7ab921 2019-03-18 stsp {
3842 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3843 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3844 6c7ab921 2019-03-18 stsp size_t len;
3845 00bb5ea0 2020-07-23 stsp struct stat sb;
3846 01740607 2020-11-04 stsp char *abspath = NULL;
3847 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3848 6c7ab921 2019-03-18 stsp
3849 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3850 6c7ab921 2019-03-18 stsp
3851 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3852 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3853 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3854 00bb5ea0 2020-07-23 stsp
3855 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3856 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3857 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3858 00bb5ea0 2020-07-23 stsp goto done;
3859 00bb5ea0 2020-07-23 stsp }
3860 727173c3 2020-11-06 stsp sb.st_mode = 0;
3861 00bb5ea0 2020-07-23 stsp }
3862 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3863 00bb5ea0 2020-07-23 stsp /*
3864 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3865 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3866 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3867 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3868 00bb5ea0 2020-07-23 stsp */
3869 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3870 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3871 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3872 00bb5ea0 2020-07-23 stsp goto done;
3873 00bb5ea0 2020-07-23 stsp }
3874 00bb5ea0 2020-07-23 stsp
3875 00bb5ea0 2020-07-23 stsp }
3876 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3877 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3878 00bb5ea0 2020-07-23 stsp if (err)
3879 d0710d08 2019-07-22 stsp goto done;
3880 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3881 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3882 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3883 00bb5ea0 2020-07-23 stsp goto done;
3884 00bb5ea0 2020-07-23 stsp }
3885 00bb5ea0 2020-07-23 stsp } else {
3886 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3887 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3888 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3889 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3890 00bb5ea0 2020-07-23 stsp goto done;
3891 00bb5ea0 2020-07-23 stsp }
3892 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3893 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3894 01740607 2020-11-04 stsp goto done;
3895 01740607 2020-11-04 stsp }
3896 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3897 01740607 2020-11-04 stsp sizeof(canonpath));
3898 01740607 2020-11-04 stsp if (err)
3899 00bb5ea0 2020-07-23 stsp goto done;
3900 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3901 01740607 2020-11-04 stsp if (resolved == NULL) {
3902 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3903 01740607 2020-11-04 stsp goto done;
3904 00bb5ea0 2020-07-23 stsp }
3905 d0710d08 2019-07-22 stsp }
3906 d0710d08 2019-07-22 stsp }
3907 6c7ab921 2019-03-18 stsp
3908 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3909 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3910 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3911 6c7ab921 2019-03-18 stsp goto done;
3912 6c7ab921 2019-03-18 stsp }
3913 6c7ab921 2019-03-18 stsp
3914 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3915 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3916 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3917 f6d88e1a 2019-05-29 stsp if (err)
3918 f6d88e1a 2019-05-29 stsp goto done;
3919 f6d88e1a 2019-05-29 stsp } else {
3920 f6d88e1a 2019-05-29 stsp path = strdup("");
3921 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3922 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3923 f6d88e1a 2019-05-29 stsp goto done;
3924 f6d88e1a 2019-05-29 stsp }
3925 6c7ab921 2019-03-18 stsp }
3926 6c7ab921 2019-03-18 stsp
3927 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3928 6c7ab921 2019-03-18 stsp len = strlen(path);
3929 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3930 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3931 6c7ab921 2019-03-18 stsp len--;
3932 6c7ab921 2019-03-18 stsp }
3933 6c7ab921 2019-03-18 stsp done:
3934 01740607 2020-11-04 stsp free(abspath);
3935 6c7ab921 2019-03-18 stsp free(resolved);
3936 d0710d08 2019-07-22 stsp free(cwd);
3937 6c7ab921 2019-03-18 stsp if (err == NULL)
3938 6c7ab921 2019-03-18 stsp *wt_path = path;
3939 6c7ab921 2019-03-18 stsp else
3940 6c7ab921 2019-03-18 stsp free(path);
3941 d00136be 2019-03-26 stsp return err;
3942 d00136be 2019-03-26 stsp }
3943 d00136be 2019-03-26 stsp
3944 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3945 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3946 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3947 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3948 4e68cba3 2019-11-23 stsp void *progress_arg;
3949 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3950 4e68cba3 2019-11-23 stsp };
3951 4e68cba3 2019-11-23 stsp
3952 4e68cba3 2019-11-23 stsp static const struct got_error *
3953 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3954 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3955 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3956 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3957 07f5b47a 2019-06-02 stsp {
3958 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3959 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3960 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3961 6d022e97 2019-08-04 stsp struct stat sb;
3962 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3963 07f5b47a 2019-06-02 stsp
3964 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3965 dbb83fbd 2019-12-12 stsp relpath) == -1)
3966 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3967 dbb83fbd 2019-12-12 stsp
3968 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3969 6d022e97 2019-08-04 stsp if (ie) {
3970 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3971 12463d8b 2019-12-13 stsp de_name, a->repo);
3972 6d022e97 2019-08-04 stsp if (err)
3973 dbb83fbd 2019-12-12 stsp goto done;
3974 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3975 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3976 dbb83fbd 2019-12-12 stsp goto done;
3977 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3978 dbb83fbd 2019-12-12 stsp if (err)
3979 dbb83fbd 2019-12-12 stsp goto done;
3980 6d022e97 2019-08-04 stsp }
3981 07f5b47a 2019-06-02 stsp
3982 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3983 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3984 dbb83fbd 2019-12-12 stsp goto done;
3985 4e68cba3 2019-11-23 stsp }
3986 07f5b47a 2019-06-02 stsp
3987 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3988 4e68cba3 2019-11-23 stsp if (err)
3989 4e68cba3 2019-11-23 stsp goto done;
3990 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3991 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3992 3969253a 2020-03-07 stsp if (err) {
3993 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3994 3969253a 2020-03-07 stsp goto done;
3995 3969253a 2020-03-07 stsp }
3996 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3997 07f5b47a 2019-06-02 stsp if (err) {
3998 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3999 4e68cba3 2019-11-23 stsp goto done;
4000 07f5b47a 2019-06-02 stsp }
4001 4e68cba3 2019-11-23 stsp done:
4002 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4003 dbb83fbd 2019-12-12 stsp if (err)
4004 dbb83fbd 2019-12-12 stsp return err;
4005 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4006 dbb83fbd 2019-12-12 stsp return NULL;
4007 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4008 07f5b47a 2019-06-02 stsp }
4009 07f5b47a 2019-06-02 stsp
4010 d00136be 2019-03-26 stsp const struct got_error *
4011 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4012 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4013 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4014 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4015 d00136be 2019-03-26 stsp {
4016 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4017 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4018 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4019 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4020 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4021 d00136be 2019-03-26 stsp
4022 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4023 d00136be 2019-03-26 stsp if (err)
4024 d00136be 2019-03-26 stsp return err;
4025 d00136be 2019-03-26 stsp
4026 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4027 d00136be 2019-03-26 stsp if (err)
4028 d00136be 2019-03-26 stsp goto done;
4029 d00136be 2019-03-26 stsp
4030 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4031 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4032 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4033 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4034 4e68cba3 2019-11-23 stsp saa.repo = repo;
4035 4e68cba3 2019-11-23 stsp
4036 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4037 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4038 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4039 1dd54920 2019-05-11 stsp if (err)
4040 af12c6b9 2019-06-04 stsp break;
4041 1dd54920 2019-05-11 stsp }
4042 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4043 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4044 af12c6b9 2019-06-04 stsp err = sync_err;
4045 d00136be 2019-03-26 stsp done:
4046 fb399478 2019-07-12 stsp free(fileindex_path);
4047 d00136be 2019-03-26 stsp if (fileindex)
4048 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4049 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4050 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4051 d00136be 2019-03-26 stsp err = unlockerr;
4052 6c7ab921 2019-03-18 stsp return err;
4053 6c7ab921 2019-03-18 stsp }
4054 17ed4618 2019-06-02 stsp
4055 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4056 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4057 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4058 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4059 f2a9dc41 2019-12-13 tracey void *progress_arg;
4060 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4061 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4062 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4063 766841c2 2020-08-13 stsp const char *status_codes;
4064 f2a9dc41 2019-12-13 tracey };
4065 f2a9dc41 2019-12-13 tracey
4066 f2a9dc41 2019-12-13 tracey static const struct got_error *
4067 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4068 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4069 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4070 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4071 17ed4618 2019-06-02 stsp {
4072 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4073 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4074 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4075 17ed4618 2019-06-02 stsp struct stat sb;
4076 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4077 17ed4618 2019-06-02 stsp
4078 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4079 17ed4618 2019-06-02 stsp if (ie == NULL)
4080 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4081 2ec1f75b 2019-03-26 stsp
4082 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4083 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4084 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4085 9acbc4fa 2019-08-03 stsp return NULL;
4086 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4087 9acbc4fa 2019-08-03 stsp }
4088 9acbc4fa 2019-08-03 stsp
4089 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4090 f2a9dc41 2019-12-13 tracey relpath) == -1)
4091 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4092 f2a9dc41 2019-12-13 tracey
4093 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4094 12463d8b 2019-12-13 stsp a->repo);
4095 17ed4618 2019-06-02 stsp if (err)
4096 f2a9dc41 2019-12-13 tracey goto done;
4097 17ed4618 2019-06-02 stsp
4098 766841c2 2020-08-13 stsp if (a->status_codes) {
4099 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4100 766841c2 2020-08-13 stsp int i;
4101 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4102 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4103 766841c2 2020-08-13 stsp break;
4104 766841c2 2020-08-13 stsp }
4105 766841c2 2020-08-13 stsp if (i == ncodes) {
4106 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4107 766841c2 2020-08-13 stsp free(ondisk_path);
4108 766841c2 2020-08-13 stsp return NULL;
4109 766841c2 2020-08-13 stsp }
4110 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4111 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4112 766841c2 2020-08-13 stsp static char msg[64];
4113 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4114 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4115 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4116 766841c2 2020-08-13 stsp goto done;
4117 766841c2 2020-08-13 stsp }
4118 766841c2 2020-08-13 stsp }
4119 766841c2 2020-08-13 stsp
4120 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4121 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4122 f2a9dc41 2019-12-13 tracey goto done;
4123 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4124 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4125 f2a9dc41 2019-12-13 tracey goto done;
4126 f2a9dc41 2019-12-13 tracey }
4127 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4128 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4129 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4130 f2a9dc41 2019-12-13 tracey goto done;
4131 f2a9dc41 2019-12-13 tracey }
4132 17ed4618 2019-06-02 stsp }
4133 17ed4618 2019-06-02 stsp
4134 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4135 20a2ad1c 2020-10-20 stsp size_t root_len;
4136 20a2ad1c 2020-10-20 stsp
4137 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4138 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4139 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4140 12463d8b 2019-12-13 stsp ondisk_path);
4141 12463d8b 2019-12-13 stsp goto done;
4142 12463d8b 2019-12-13 stsp }
4143 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4144 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4145 12463d8b 2019-12-13 stsp goto done;
4146 15341bfd 2020-03-05 tracey }
4147 15341bfd 2020-03-05 tracey
4148 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4149 20a2ad1c 2020-10-20 stsp do {
4150 20a2ad1c 2020-10-20 stsp char *parent;
4151 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4152 20a2ad1c 2020-10-20 stsp if (err)
4153 2513f20a 2020-10-20 stsp goto done;
4154 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4155 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4156 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4157 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4158 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4159 20a2ad1c 2020-10-20 stsp ondisk_path);
4160 15341bfd 2020-03-05 tracey break;
4161 15341bfd 2020-03-05 tracey }
4162 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4163 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4164 f2a9dc41 2019-12-13 tracey }
4165 17ed4618 2019-06-02 stsp
4166 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4167 f2a9dc41 2019-12-13 tracey done:
4168 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4169 f2a9dc41 2019-12-13 tracey if (err)
4170 f2a9dc41 2019-12-13 tracey return err;
4171 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4172 f2a9dc41 2019-12-13 tracey return NULL;
4173 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4174 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4175 17ed4618 2019-06-02 stsp }
4176 17ed4618 2019-06-02 stsp
4177 2ec1f75b 2019-03-26 stsp const struct got_error *
4178 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4179 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4180 766841c2 2020-08-13 stsp const char *status_codes,
4181 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4182 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4183 2ec1f75b 2019-03-26 stsp {
4184 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4185 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4186 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4187 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4188 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4189 2ec1f75b 2019-03-26 stsp
4190 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4191 2ec1f75b 2019-03-26 stsp if (err)
4192 2ec1f75b 2019-03-26 stsp return err;
4193 2ec1f75b 2019-03-26 stsp
4194 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4195 2ec1f75b 2019-03-26 stsp if (err)
4196 2ec1f75b 2019-03-26 stsp goto done;
4197 f2a9dc41 2019-12-13 tracey
4198 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4199 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4200 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4201 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4202 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4203 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4204 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4205 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4206 2ec1f75b 2019-03-26 stsp
4207 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4208 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4209 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4210 17ed4618 2019-06-02 stsp if (err)
4211 af12c6b9 2019-06-04 stsp break;
4212 2ec1f75b 2019-03-26 stsp }
4213 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4214 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4215 af12c6b9 2019-06-04 stsp err = sync_err;
4216 2ec1f75b 2019-03-26 stsp done:
4217 fb399478 2019-07-12 stsp free(fileindex_path);
4218 2ec1f75b 2019-03-26 stsp if (fileindex)
4219 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4220 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4221 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4222 2ec1f75b 2019-03-26 stsp err = unlockerr;
4223 33aa809d 2019-08-08 stsp return err;
4224 33aa809d 2019-08-08 stsp }
4225 33aa809d 2019-08-08 stsp
4226 33aa809d 2019-08-08 stsp static const struct got_error *
4227 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4228 33aa809d 2019-08-08 stsp {
4229 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4230 33aa809d 2019-08-08 stsp char *line = NULL;
4231 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4232 33aa809d 2019-08-08 stsp ssize_t linelen;
4233 33aa809d 2019-08-08 stsp
4234 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4235 33aa809d 2019-08-08 stsp if (linelen == -1) {
4236 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4237 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4238 33aa809d 2019-08-08 stsp goto done;
4239 33aa809d 2019-08-08 stsp }
4240 33aa809d 2019-08-08 stsp return NULL;
4241 33aa809d 2019-08-08 stsp }
4242 33aa809d 2019-08-08 stsp if (outfile) {
4243 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4244 33aa809d 2019-08-08 stsp if (n != linelen) {
4245 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4246 33aa809d 2019-08-08 stsp goto done;
4247 33aa809d 2019-08-08 stsp }
4248 33aa809d 2019-08-08 stsp }
4249 33aa809d 2019-08-08 stsp if (rejectfile) {
4250 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4251 33aa809d 2019-08-08 stsp if (n != linelen)
4252 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4253 33aa809d 2019-08-08 stsp }
4254 33aa809d 2019-08-08 stsp done:
4255 33aa809d 2019-08-08 stsp free(line);
4256 2ec1f75b 2019-03-26 stsp return err;
4257 2ec1f75b 2019-03-26 stsp }
4258 1f1abb7e 2019-08-08 stsp
4259 33aa809d 2019-08-08 stsp static const struct got_error *
4260 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4261 33aa809d 2019-08-08 stsp {
4262 33aa809d 2019-08-08 stsp char *line = NULL;
4263 33aa809d 2019-08-08 stsp size_t linesize = 0;
4264 33aa809d 2019-08-08 stsp ssize_t linelen;
4265 33aa809d 2019-08-08 stsp
4266 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4267 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4268 500467ff 2019-09-25 hiltjo if (ferror(f))
4269 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4270 500467ff 2019-09-25 hiltjo return NULL;
4271 500467ff 2019-09-25 hiltjo }
4272 33aa809d 2019-08-08 stsp free(line);
4273 33aa809d 2019-08-08 stsp return NULL;
4274 33aa809d 2019-08-08 stsp }
4275 33aa809d 2019-08-08 stsp
4276 33aa809d 2019-08-08 stsp static const struct got_error *
4277 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4278 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4279 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4280 abc59930 2021-09-05 naddy {
4281 33aa809d 2019-08-08 stsp const struct got_error *err;
4282 33aa809d 2019-08-08 stsp
4283 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4284 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4285 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4286 33aa809d 2019-08-08 stsp if (err)
4287 33aa809d 2019-08-08 stsp return err;
4288 33aa809d 2019-08-08 stsp (*line_cur1)++;
4289 33aa809d 2019-08-08 stsp }
4290 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4291 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4292 33aa809d 2019-08-08 stsp if (rejectfile)
4293 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4294 33aa809d 2019-08-08 stsp else
4295 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4296 33aa809d 2019-08-08 stsp if (err)
4297 33aa809d 2019-08-08 stsp return err;
4298 33aa809d 2019-08-08 stsp (*line_cur2)++;
4299 33aa809d 2019-08-08 stsp }
4300 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4301 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4302 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4303 33aa809d 2019-08-08 stsp if (err)
4304 33aa809d 2019-08-08 stsp return err;
4305 33aa809d 2019-08-08 stsp (*line_cur2)++;
4306 33aa809d 2019-08-08 stsp }
4307 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4308 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4309 33aa809d 2019-08-08 stsp if (rejectfile)
4310 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4311 33aa809d 2019-08-08 stsp else
4312 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4313 33aa809d 2019-08-08 stsp if (err)
4314 33aa809d 2019-08-08 stsp return err;
4315 33aa809d 2019-08-08 stsp (*line_cur1)++;
4316 33aa809d 2019-08-08 stsp }
4317 f1e81a05 2019-08-10 stsp
4318 f1e81a05 2019-08-10 stsp return NULL;
4319 f1e81a05 2019-08-10 stsp }
4320 f1e81a05 2019-08-10 stsp
4321 f1e81a05 2019-08-10 stsp static const struct got_error *
4322 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4323 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4324 f1e81a05 2019-08-10 stsp {
4325 f1e81a05 2019-08-10 stsp const struct got_error *err;
4326 f1e81a05 2019-08-10 stsp
4327 f1e81a05 2019-08-10 stsp if (outfile) {
4328 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4329 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4330 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4331 f1e81a05 2019-08-10 stsp if (err)
4332 f1e81a05 2019-08-10 stsp return err;
4333 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4334 f1e81a05 2019-08-10 stsp }
4335 f1e81a05 2019-08-10 stsp }
4336 f1e81a05 2019-08-10 stsp if (rejectfile) {
4337 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4338 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4339 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4340 f1e81a05 2019-08-10 stsp if (err)
4341 f1e81a05 2019-08-10 stsp return err;
4342 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4343 f1e81a05 2019-08-10 stsp }
4344 33aa809d 2019-08-08 stsp }
4345 33aa809d 2019-08-08 stsp
4346 33aa809d 2019-08-08 stsp return NULL;
4347 33aa809d 2019-08-08 stsp }
4348 33aa809d 2019-08-08 stsp
4349 33aa809d 2019-08-08 stsp static const struct got_error *
4350 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4351 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4352 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4353 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4354 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4355 33aa809d 2019-08-08 stsp {
4356 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4357 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4358 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4359 33aa809d 2019-08-08 stsp FILE *hunkfile;
4360 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4361 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4362 fe621944 2020-11-10 stsp int rc;
4363 33aa809d 2019-08-08 stsp
4364 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4365 33aa809d 2019-08-08 stsp
4366 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4367 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4368 fe621944 2020-11-10 stsp start_old = cc.left.start;
4369 fe621944 2020-11-10 stsp end_old = cc.left.end;
4370 fe621944 2020-11-10 stsp start_new = cc.right.start;
4371 fe621944 2020-11-10 stsp end_new = cc.right.end;
4372 33aa809d 2019-08-08 stsp
4373 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4374 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4375 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4376 33aa809d 2019-08-08 stsp
4377 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4378 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4379 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4380 33aa809d 2019-08-08 stsp
4381 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4382 fe621944 2020-11-10 stsp if (diff_state == NULL)
4383 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4384 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4385 fe621944 2020-11-10 stsp
4386 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4387 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4388 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4389 33aa809d 2019-08-08 stsp goto done;
4390 33aa809d 2019-08-08 stsp }
4391 fe621944 2020-11-10 stsp
4392 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4393 fe621944 2020-11-10 stsp diff_result, &cc);
4394 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4395 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4396 33aa809d 2019-08-08 stsp goto done;
4397 33aa809d 2019-08-08 stsp }
4398 fe621944 2020-11-10 stsp
4399 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4400 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4401 33aa809d 2019-08-08 stsp goto done;
4402 33aa809d 2019-08-08 stsp }
4403 33aa809d 2019-08-08 stsp
4404 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4405 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4406 33aa809d 2019-08-08 stsp if (err)
4407 33aa809d 2019-08-08 stsp goto done;
4408 33aa809d 2019-08-08 stsp
4409 33aa809d 2019-08-08 stsp switch (*choice) {
4410 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4411 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4412 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4413 33aa809d 2019-08-08 stsp break;
4414 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4415 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4416 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4417 33aa809d 2019-08-08 stsp break;
4418 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4419 33aa809d 2019-08-08 stsp break;
4420 33aa809d 2019-08-08 stsp default:
4421 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4422 33aa809d 2019-08-08 stsp break;
4423 33aa809d 2019-08-08 stsp }
4424 33aa809d 2019-08-08 stsp done:
4425 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4426 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4427 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4428 33aa809d 2019-08-08 stsp return err;
4429 33aa809d 2019-08-08 stsp }
4430 33aa809d 2019-08-08 stsp
4431 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4432 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4433 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4434 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4435 1f1abb7e 2019-08-08 stsp void *progress_arg;
4436 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4437 33aa809d 2019-08-08 stsp void *patch_arg;
4438 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4439 10604dce 2021-09-24 thomas int unlink_added_files;
4440 1f1abb7e 2019-08-08 stsp };
4441 a129376b 2019-03-28 stsp
4442 e20a8b6f 2019-06-04 stsp static const struct got_error *
4443 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4444 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4445 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4446 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4447 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4448 33aa809d 2019-08-08 stsp {
4449 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4450 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4451 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4452 1ebedb77 2019-10-19 stsp int fd2 = -1;
4453 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4454 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4455 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4456 fe621944 2020-11-10 stsp struct stat sb2;
4457 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4458 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4459 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4460 33aa809d 2019-08-08 stsp
4461 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4462 33aa809d 2019-08-08 stsp
4463 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4464 33aa809d 2019-08-08 stsp if (err)
4465 33aa809d 2019-08-08 stsp return err;
4466 33aa809d 2019-08-08 stsp
4467 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4468 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4469 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4470 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4471 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4472 fa3cef63 2020-07-23 stsp goto done;
4473 fa3cef63 2020-07-23 stsp }
4474 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4475 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4476 fa3cef63 2020-07-23 stsp if (link_len == -1)
4477 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4478 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4479 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4480 12463d8b 2019-12-13 stsp }
4481 12463d8b 2019-12-13 stsp } else {
4482 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4483 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4484 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4485 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4486 fa3cef63 2020-07-23 stsp goto done;
4487 fa3cef63 2020-07-23 stsp }
4488 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4489 fa3cef63 2020-07-23 stsp sizeof(link_target));
4490 fa3cef63 2020-07-23 stsp if (link_len == -1)
4491 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4492 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4493 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4494 12463d8b 2019-12-13 stsp }
4495 1ebedb77 2019-10-19 stsp }
4496 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4497 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4498 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4499 fa3cef63 2020-07-23 stsp goto done;
4500 fa3cef63 2020-07-23 stsp }
4501 1ebedb77 2019-10-19 stsp
4502 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4503 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4504 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4505 fa3cef63 2020-07-23 stsp goto done;
4506 fa3cef63 2020-07-23 stsp }
4507 fa3cef63 2020-07-23 stsp fd2 = -1;
4508 fa3cef63 2020-07-23 stsp } else {
4509 fa3cef63 2020-07-23 stsp size_t n;
4510 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4511 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4512 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4513 fa3cef63 2020-07-23 stsp goto done;
4514 fa3cef63 2020-07-23 stsp }
4515 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4516 fa3cef63 2020-07-23 stsp if (n != link_len) {
4517 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4518 fa3cef63 2020-07-23 stsp goto done;
4519 fa3cef63 2020-07-23 stsp }
4520 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4521 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4522 fa3cef63 2020-07-23 stsp goto done;
4523 fa3cef63 2020-07-23 stsp }
4524 fa3cef63 2020-07-23 stsp rewind(f2);
4525 33aa809d 2019-08-08 stsp }
4526 33aa809d 2019-08-08 stsp
4527 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4528 33aa809d 2019-08-08 stsp if (err)
4529 33aa809d 2019-08-08 stsp goto done;
4530 33aa809d 2019-08-08 stsp
4531 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4532 33aa809d 2019-08-08 stsp if (err)
4533 33aa809d 2019-08-08 stsp goto done;
4534 33aa809d 2019-08-08 stsp
4535 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4536 33aa809d 2019-08-08 stsp if (err)
4537 33aa809d 2019-08-08 stsp goto done;
4538 33aa809d 2019-08-08 stsp
4539 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4540 fe621944 2020-11-10 stsp NULL);
4541 33aa809d 2019-08-08 stsp if (err)
4542 33aa809d 2019-08-08 stsp goto done;
4543 33aa809d 2019-08-08 stsp
4544 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4545 33aa809d 2019-08-08 stsp if (err)
4546 33aa809d 2019-08-08 stsp goto done;
4547 33aa809d 2019-08-08 stsp
4548 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4549 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4550 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4551 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4552 fe621944 2020-11-10 stsp
4553 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4554 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4555 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4556 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4557 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4558 fe621944 2020-11-10 stsp nchanges++;
4559 fe621944 2020-11-10 stsp }
4560 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4561 33aa809d 2019-08-08 stsp int choice;
4562 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4563 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4564 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4565 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4566 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4567 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4568 33aa809d 2019-08-08 stsp if (err)
4569 33aa809d 2019-08-08 stsp goto done;
4570 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4571 33aa809d 2019-08-08 stsp have_content = 1;
4572 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4573 33aa809d 2019-08-08 stsp break;
4574 33aa809d 2019-08-08 stsp }
4575 1ebedb77 2019-10-19 stsp if (have_content) {
4576 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4577 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4578 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4579 1ebedb77 2019-10-19 stsp if (err)
4580 1ebedb77 2019-10-19 stsp goto done;
4581 1ebedb77 2019-10-19 stsp
4582 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4583 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4584 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4585 fa3cef63 2020-07-23 stsp goto done;
4586 fa3cef63 2020-07-23 stsp }
4587 1ebedb77 2019-10-19 stsp }
4588 1ebedb77 2019-10-19 stsp }
4589 33aa809d 2019-08-08 stsp done:
4590 33aa809d 2019-08-08 stsp free(id_str);
4591 33aa809d 2019-08-08 stsp if (blob)
4592 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4593 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4594 fe621944 2020-11-10 stsp if (err == NULL)
4595 fe621944 2020-11-10 stsp err = free_err;
4596 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4597 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4598 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4599 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4600 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4601 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4602 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4603 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4604 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4605 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4606 33aa809d 2019-08-08 stsp if (err || !have_content) {
4607 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4608 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4609 33aa809d 2019-08-08 stsp free(*path_outfile);
4610 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4611 33aa809d 2019-08-08 stsp }
4612 33aa809d 2019-08-08 stsp free(path1);
4613 33aa809d 2019-08-08 stsp return err;
4614 33aa809d 2019-08-08 stsp }
4615 33aa809d 2019-08-08 stsp
4616 33aa809d 2019-08-08 stsp static const struct got_error *
4617 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4618 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4619 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4620 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4621 a129376b 2019-03-28 stsp {
4622 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4623 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4624 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4625 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4626 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4627 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4628 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4629 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4630 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4631 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4632 a129376b 2019-03-28 stsp
4633 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4634 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4635 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4636 d3bcc3d1 2019-08-08 stsp return NULL;
4637 3d69ad8d 2019-08-17 semarie
4638 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4639 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4640 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4641 d3bcc3d1 2019-08-08 stsp
4642 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4643 65084dad 2019-08-08 stsp if (ie == NULL)
4644 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4645 a129376b 2019-03-28 stsp
4646 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4647 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4648 a129376b 2019-03-28 stsp if (err) {
4649 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4650 a129376b 2019-03-28 stsp goto done;
4651 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4652 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4653 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4654 e20a8b6f 2019-06-04 stsp goto done;
4655 e20a8b6f 2019-06-04 stsp }
4656 a129376b 2019-03-28 stsp }
4657 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4658 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4659 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4660 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4661 a129376b 2019-03-28 stsp goto done;
4662 a129376b 2019-03-28 stsp }
4663 a129376b 2019-03-28 stsp } else {
4664 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4665 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4666 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4667 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4668 a129376b 2019-03-28 stsp goto done;
4669 a129376b 2019-03-28 stsp }
4670 a129376b 2019-03-28 stsp } else {
4671 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4672 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4673 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4674 a129376b 2019-03-28 stsp goto done;
4675 a129376b 2019-03-28 stsp }
4676 a129376b 2019-03-28 stsp }
4677 a129376b 2019-03-28 stsp }
4678 a129376b 2019-03-28 stsp
4679 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4680 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4681 a9fa2909 2019-07-27 stsp if (err) {
4682 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4683 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4684 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4685 a9fa2909 2019-07-27 stsp goto done;
4686 a9fa2909 2019-07-27 stsp } else {
4687 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4688 a9fa2909 2019-07-27 stsp if (err)
4689 a9fa2909 2019-07-27 stsp goto done;
4690 a9fa2909 2019-07-27 stsp
4691 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4692 1233e6b6 2020-10-19 stsp if (err)
4693 a9fa2909 2019-07-27 stsp goto done;
4694 a9fa2909 2019-07-27 stsp
4695 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4696 1233e6b6 2020-10-19 stsp free(te_name);
4697 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4698 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4699 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4700 a9fa2909 2019-07-27 stsp goto done;
4701 a9fa2909 2019-07-27 stsp }
4702 a129376b 2019-03-28 stsp }
4703 a129376b 2019-03-28 stsp
4704 a129376b 2019-03-28 stsp switch (status) {
4705 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4706 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4707 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4708 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4709 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4710 33aa809d 2019-08-08 stsp if (err)
4711 33aa809d 2019-08-08 stsp goto done;
4712 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4713 33aa809d 2019-08-08 stsp break;
4714 33aa809d 2019-08-08 stsp }
4715 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4716 1f1abb7e 2019-08-08 stsp ie->path);
4717 1ee397ad 2019-07-12 stsp if (err)
4718 1ee397ad 2019-07-12 stsp goto done;
4719 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4720 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4721 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4722 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4723 10604dce 2021-09-24 thomas relpath) == -1) {
4724 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4725 10604dce 2021-09-24 thomas goto done;
4726 10604dce 2021-09-24 thomas }
4727 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4728 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4729 10604dce 2021-09-24 thomas ondisk_path);
4730 10604dce 2021-09-24 thomas break;
4731 10604dce 2021-09-24 thomas }
4732 10604dce 2021-09-24 thomas }
4733 a129376b 2019-03-28 stsp break;
4734 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4735 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4736 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4737 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4738 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4739 33aa809d 2019-08-08 stsp if (err)
4740 33aa809d 2019-08-08 stsp goto done;
4741 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4742 33aa809d 2019-08-08 stsp break;
4743 33aa809d 2019-08-08 stsp }
4744 33aa809d 2019-08-08 stsp /* fall through */
4745 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4746 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4747 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4748 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4749 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4750 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4751 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4752 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4753 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4754 24278f30 2019-08-03 stsp } else
4755 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4756 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4757 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4758 a129376b 2019-03-28 stsp if (err)
4759 65084dad 2019-08-08 stsp goto done;
4760 65084dad 2019-08-08 stsp
4761 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4762 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4763 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4764 a129376b 2019-03-28 stsp goto done;
4765 65084dad 2019-08-08 stsp }
4766 65084dad 2019-08-08 stsp
4767 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4768 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4769 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4770 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4771 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4772 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4773 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4774 33aa809d 2019-08-08 stsp break;
4775 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4776 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4777 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4778 369fd7e5 2020-07-23 stsp path_content);
4779 369fd7e5 2020-07-23 stsp break;
4780 369fd7e5 2020-07-23 stsp }
4781 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4782 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4783 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4784 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4785 369fd7e5 2020-07-23 stsp } else {
4786 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4787 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4788 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4789 369fd7e5 2020-07-23 stsp goto done;
4790 369fd7e5 2020-07-23 stsp }
4791 33aa809d 2019-08-08 stsp }
4792 33aa809d 2019-08-08 stsp } else {
4793 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4794 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4795 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4796 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4797 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4798 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4799 2e1fa222 2020-07-23 stsp } else {
4800 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4801 2e1fa222 2020-07-23 stsp ie->path,
4802 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4803 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4804 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4805 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4806 2e1fa222 2020-07-23 stsp }
4807 a129376b 2019-03-28 stsp if (err)
4808 a129376b 2019-03-28 stsp goto done;
4809 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4810 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4811 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4812 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4813 437adc9d 2020-12-10 yzhong blob->id.sha1,
4814 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4815 2e1fa222 2020-07-23 stsp if (err)
4816 2e1fa222 2020-07-23 stsp goto done;
4817 2e1fa222 2020-07-23 stsp }
4818 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4819 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4820 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4821 33aa809d 2019-08-08 stsp }
4822 a129376b 2019-03-28 stsp }
4823 a129376b 2019-03-28 stsp break;
4824 e20a8b6f 2019-06-04 stsp }
4825 a129376b 2019-03-28 stsp default:
4826 1f1abb7e 2019-08-08 stsp break;
4827 a129376b 2019-03-28 stsp }
4828 a129376b 2019-03-28 stsp done:
4829 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4830 33aa809d 2019-08-08 stsp free(path_content);
4831 e20a8b6f 2019-06-04 stsp free(parent_path);
4832 a129376b 2019-03-28 stsp free(tree_path);
4833 a129376b 2019-03-28 stsp if (blob)
4834 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4835 a129376b 2019-03-28 stsp if (tree)
4836 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4837 a129376b 2019-03-28 stsp free(tree_id);
4838 e20a8b6f 2019-06-04 stsp return err;
4839 e20a8b6f 2019-06-04 stsp }
4840 e20a8b6f 2019-06-04 stsp
4841 e20a8b6f 2019-06-04 stsp const struct got_error *
4842 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4843 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4844 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4845 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4846 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4847 e20a8b6f 2019-06-04 stsp {
4848 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4849 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4850 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4851 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4852 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4853 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4854 e20a8b6f 2019-06-04 stsp
4855 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4856 e20a8b6f 2019-06-04 stsp if (err)
4857 e20a8b6f 2019-06-04 stsp return err;
4858 e20a8b6f 2019-06-04 stsp
4859 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4860 e20a8b6f 2019-06-04 stsp if (err)
4861 e20a8b6f 2019-06-04 stsp goto done;
4862 e20a8b6f 2019-06-04 stsp
4863 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4864 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4865 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4866 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4867 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4868 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4869 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4870 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4871 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4872 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4873 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4874 e20a8b6f 2019-06-04 stsp if (err)
4875 af12c6b9 2019-06-04 stsp break;
4876 e20a8b6f 2019-06-04 stsp }
4877 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4878 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4879 e20a8b6f 2019-06-04 stsp err = sync_err;
4880 af12c6b9 2019-06-04 stsp done:
4881 fb399478 2019-07-12 stsp free(fileindex_path);
4882 a129376b 2019-03-28 stsp if (fileindex)
4883 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4884 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4885 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4886 a129376b 2019-03-28 stsp err = unlockerr;
4887 c4296144 2019-05-09 stsp return err;
4888 c4296144 2019-05-09 stsp }
4889 c4296144 2019-05-09 stsp
4890 cf066bf8 2019-05-09 stsp static void
4891 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4892 cf066bf8 2019-05-09 stsp {
4893 24519714 2019-05-09 stsp free(ct->path);
4894 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4895 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4896 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4897 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4898 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4899 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4900 cf066bf8 2019-05-09 stsp free(ct);
4901 cf066bf8 2019-05-09 stsp }
4902 24519714 2019-05-09 stsp
4903 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4904 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4905 24519714 2019-05-09 stsp struct got_repository *repo;
4906 24519714 2019-05-09 stsp struct got_worktree *worktree;
4907 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4908 5f8a88c6 2019-08-03 stsp int have_staged_files;
4909 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4910 24519714 2019-05-09 stsp };
4911 cf066bf8 2019-05-09 stsp
4912 c4296144 2019-05-09 stsp static const struct got_error *
4913 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4914 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4915 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4916 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4917 c4296144 2019-05-09 stsp {
4918 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4919 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4920 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4921 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4922 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4923 768aea60 2019-05-09 stsp struct stat sb;
4924 c4296144 2019-05-09 stsp
4925 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4926 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4927 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4928 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4929 5f8a88c6 2019-08-03 stsp return NULL;
4930 5f8a88c6 2019-08-03 stsp } else {
4931 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4932 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4933 c4296144 2019-05-09 stsp
4934 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4935 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4936 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4937 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4938 5f8a88c6 2019-08-03 stsp return NULL;
4939 5f8a88c6 2019-08-03 stsp }
4940 0b5cc0d6 2019-05-09 stsp
4941 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4942 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4943 036813ee 2019-05-09 stsp goto done;
4944 036813ee 2019-05-09 stsp }
4945 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4946 036813ee 2019-05-09 stsp parent_path = strdup("");
4947 69960a46 2019-05-09 stsp if (parent_path == NULL)
4948 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4949 69960a46 2019-05-09 stsp } else {
4950 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4951 69960a46 2019-05-09 stsp if (err)
4952 69960a46 2019-05-09 stsp return err;
4953 69960a46 2019-05-09 stsp }
4954 c4296144 2019-05-09 stsp
4955 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4956 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4957 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4958 c4296144 2019-05-09 stsp goto done;
4959 768aea60 2019-05-09 stsp }
4960 768aea60 2019-05-09 stsp
4961 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4962 768aea60 2019-05-09 stsp relpath) == -1) {
4963 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4964 768aea60 2019-05-09 stsp goto done;
4965 768aea60 2019-05-09 stsp }
4966 0aeb8099 2020-07-23 stsp
4967 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4968 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4969 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4970 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4971 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4972 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4973 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4974 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4975 0aeb8099 2020-07-23 stsp break;
4976 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4977 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4978 0aeb8099 2020-07-23 stsp break;
4979 0aeb8099 2020-07-23 stsp default:
4980 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4981 0aeb8099 2020-07-23 stsp goto done;
4982 0aeb8099 2020-07-23 stsp }
4983 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4984 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4985 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4986 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4987 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4988 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4989 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4990 12463d8b 2019-12-13 stsp ct->ondisk_path);
4991 12463d8b 2019-12-13 stsp goto done;
4992 12463d8b 2019-12-13 stsp }
4993 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4994 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4995 768aea60 2019-05-09 stsp goto done;
4996 768aea60 2019-05-09 stsp }
4997 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4998 c4296144 2019-05-09 stsp }
4999 c4296144 2019-05-09 stsp
5000 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5001 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5002 44d03001 2019-05-09 stsp relpath) == -1) {
5003 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5004 44d03001 2019-05-09 stsp goto done;
5005 44d03001 2019-05-09 stsp }
5006 44d03001 2019-05-09 stsp
5007 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5008 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5009 35213c7c 2020-07-23 stsp int is_bad_symlink;
5010 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5011 35213c7c 2020-07-23 stsp ssize_t target_len;
5012 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5013 35213c7c 2020-07-23 stsp sizeof(target_path));
5014 35213c7c 2020-07-23 stsp if (target_len == -1) {
5015 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5016 35213c7c 2020-07-23 stsp ct->ondisk_path);
5017 35213c7c 2020-07-23 stsp goto done;
5018 35213c7c 2020-07-23 stsp }
5019 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5020 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5021 35213c7c 2020-07-23 stsp if (err)
5022 35213c7c 2020-07-23 stsp goto done;
5023 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5024 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5025 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5026 35213c7c 2020-07-23 stsp goto done;
5027 35213c7c 2020-07-23 stsp }
5028 35213c7c 2020-07-23 stsp }
5029 35213c7c 2020-07-23 stsp
5030 35213c7c 2020-07-23 stsp
5031 cf066bf8 2019-05-09 stsp ct->status = status;
5032 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5033 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5034 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5035 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5036 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5037 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5038 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5039 b416585c 2019-05-13 stsp goto done;
5040 b416585c 2019-05-13 stsp }
5041 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5042 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5043 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5044 f0b75401 2019-08-03 stsp goto done;
5045 f0b75401 2019-08-03 stsp }
5046 f0b75401 2019-08-03 stsp }
5047 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5048 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5049 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5050 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5051 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5052 036813ee 2019-05-09 stsp goto done;
5053 036813ee 2019-05-09 stsp }
5054 ca2503ea 2019-05-09 stsp }
5055 24519714 2019-05-09 stsp ct->path = strdup(path);
5056 24519714 2019-05-09 stsp if (ct->path == NULL) {
5057 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5058 24519714 2019-05-09 stsp goto done;
5059 24519714 2019-05-09 stsp }
5060 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5061 c4296144 2019-05-09 stsp done:
5062 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5063 ed175427 2019-05-09 stsp free_commitable(ct);
5064 24519714 2019-05-09 stsp free(parent_path);
5065 036813ee 2019-05-09 stsp free(path);
5066 a129376b 2019-03-28 stsp return err;
5067 a129376b 2019-03-28 stsp }
5068 c4296144 2019-05-09 stsp
5069 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5070 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5071 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5072 036813ee 2019-05-09 stsp struct got_repository *);
5073 ed175427 2019-05-09 stsp
5074 ed175427 2019-05-09 stsp static const struct got_error *
5075 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5076 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5077 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5078 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5079 afa376bf 2019-05-09 stsp struct got_repository *repo)
5080 ed175427 2019-05-09 stsp {
5081 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5082 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5083 036813ee 2019-05-09 stsp char *subpath;
5084 ed175427 2019-05-09 stsp
5085 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5086 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5087 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5088 ed175427 2019-05-09 stsp
5089 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5090 ed175427 2019-05-09 stsp if (err)
5091 ed175427 2019-05-09 stsp return err;
5092 ed175427 2019-05-09 stsp
5093 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5094 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5095 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5096 036813ee 2019-05-09 stsp free(subpath);
5097 036813ee 2019-05-09 stsp return err;
5098 036813ee 2019-05-09 stsp }
5099 ed175427 2019-05-09 stsp
5100 036813ee 2019-05-09 stsp static const struct got_error *
5101 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5102 036813ee 2019-05-09 stsp {
5103 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5104 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5105 ed175427 2019-05-09 stsp
5106 036813ee 2019-05-09 stsp *match = 0;
5107 ed175427 2019-05-09 stsp
5108 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5109 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5110 0f63689d 2019-05-10 stsp return NULL;
5111 036813ee 2019-05-09 stsp }
5112 0b5cc0d6 2019-05-09 stsp
5113 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5114 0f63689d 2019-05-10 stsp if (err)
5115 0f63689d 2019-05-10 stsp return err;
5116 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5117 036813ee 2019-05-09 stsp free(ct_parent_path);
5118 036813ee 2019-05-09 stsp return err;
5119 036813ee 2019-05-09 stsp }
5120 0b5cc0d6 2019-05-09 stsp
5121 768aea60 2019-05-09 stsp static mode_t
5122 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5123 768aea60 2019-05-09 stsp {
5124 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5125 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5126 3d9a4ec4 2020-07-23 stsp
5127 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5128 768aea60 2019-05-09 stsp }
5129 768aea60 2019-05-09 stsp
5130 036813ee 2019-05-09 stsp static const struct got_error *
5131 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5132 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5133 036813ee 2019-05-09 stsp {
5134 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5135 ca2503ea 2019-05-09 stsp
5136 036813ee 2019-05-09 stsp *new_te = NULL;
5137 0b5cc0d6 2019-05-09 stsp
5138 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5139 036813ee 2019-05-09 stsp if (err)
5140 036813ee 2019-05-09 stsp goto done;
5141 0b5cc0d6 2019-05-09 stsp
5142 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5143 036813ee 2019-05-09 stsp
5144 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5145 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5146 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5147 5f8a88c6 2019-08-03 stsp else
5148 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5149 036813ee 2019-05-09 stsp done:
5150 036813ee 2019-05-09 stsp if (err && *new_te) {
5151 56e0773d 2019-11-28 stsp free(*new_te);
5152 036813ee 2019-05-09 stsp *new_te = NULL;
5153 036813ee 2019-05-09 stsp }
5154 036813ee 2019-05-09 stsp return err;
5155 036813ee 2019-05-09 stsp }
5156 036813ee 2019-05-09 stsp
5157 036813ee 2019-05-09 stsp static const struct got_error *
5158 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5159 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5160 036813ee 2019-05-09 stsp {
5161 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5162 102b254e 2020-10-19 stsp char *ct_name = NULL;
5163 036813ee 2019-05-09 stsp
5164 036813ee 2019-05-09 stsp *new_te = NULL;
5165 036813ee 2019-05-09 stsp
5166 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5167 036813ee 2019-05-09 stsp if (*new_te == NULL)
5168 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5169 036813ee 2019-05-09 stsp
5170 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5171 102b254e 2020-10-19 stsp if (err)
5172 036813ee 2019-05-09 stsp goto done;
5173 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5174 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5175 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5176 036813ee 2019-05-09 stsp goto done;
5177 036813ee 2019-05-09 stsp }
5178 036813ee 2019-05-09 stsp
5179 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5180 036813ee 2019-05-09 stsp
5181 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5182 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5183 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5184 5f8a88c6 2019-08-03 stsp else
5185 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5186 036813ee 2019-05-09 stsp done:
5187 102b254e 2020-10-19 stsp free(ct_name);
5188 036813ee 2019-05-09 stsp if (err && *new_te) {
5189 56e0773d 2019-11-28 stsp free(*new_te);
5190 036813ee 2019-05-09 stsp *new_te = NULL;
5191 036813ee 2019-05-09 stsp }
5192 036813ee 2019-05-09 stsp return err;
5193 036813ee 2019-05-09 stsp }
5194 036813ee 2019-05-09 stsp
5195 036813ee 2019-05-09 stsp static const struct got_error *
5196 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5197 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5198 036813ee 2019-05-09 stsp {
5199 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5200 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5201 036813ee 2019-05-09 stsp
5202 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5203 036813ee 2019-05-09 stsp if (err)
5204 036813ee 2019-05-09 stsp return err;
5205 036813ee 2019-05-09 stsp if (new_pe == NULL)
5206 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5207 036813ee 2019-05-09 stsp return NULL;
5208 afa376bf 2019-05-09 stsp }
5209 afa376bf 2019-05-09 stsp
5210 afa376bf 2019-05-09 stsp static const struct got_error *
5211 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5212 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5213 afa376bf 2019-05-09 stsp {
5214 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5215 5f8a88c6 2019-08-03 stsp unsigned char status;
5216 5f8a88c6 2019-08-03 stsp
5217 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5218 afa376bf 2019-05-09 stsp ct_path++;
5219 5f8a88c6 2019-08-03 stsp
5220 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5221 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5222 5f8a88c6 2019-08-03 stsp else
5223 5f8a88c6 2019-08-03 stsp status = ct->status;
5224 5f8a88c6 2019-08-03 stsp
5225 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5226 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5227 036813ee 2019-05-09 stsp }
5228 036813ee 2019-05-09 stsp
5229 036813ee 2019-05-09 stsp static const struct got_error *
5230 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5231 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5232 44d03001 2019-05-09 stsp {
5233 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5234 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5235 44d03001 2019-05-09 stsp char *te_path;
5236 44d03001 2019-05-09 stsp
5237 44d03001 2019-05-09 stsp *modified = 0;
5238 44d03001 2019-05-09 stsp
5239 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5240 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5241 44d03001 2019-05-09 stsp te->name) == -1)
5242 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5243 44d03001 2019-05-09 stsp
5244 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5245 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5246 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5247 44d03001 2019-05-09 stsp strlen(te_path));
5248 62d463ca 2020-10-20 naddy if (*modified)
5249 44d03001 2019-05-09 stsp break;
5250 44d03001 2019-05-09 stsp }
5251 44d03001 2019-05-09 stsp
5252 44d03001 2019-05-09 stsp free(te_path);
5253 44d03001 2019-05-09 stsp return err;
5254 44d03001 2019-05-09 stsp }
5255 44d03001 2019-05-09 stsp
5256 44d03001 2019-05-09 stsp static const struct got_error *
5257 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5258 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5259 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5260 036813ee 2019-05-09 stsp {
5261 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5262 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5263 036813ee 2019-05-09 stsp
5264 036813ee 2019-05-09 stsp *ctp = NULL;
5265 036813ee 2019-05-09 stsp
5266 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5267 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5268 036813ee 2019-05-09 stsp char *ct_name = NULL;
5269 036813ee 2019-05-09 stsp int path_matches;
5270 036813ee 2019-05-09 stsp
5271 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5272 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5273 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5274 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5275 5f8a88c6 2019-08-03 stsp continue;
5276 5f8a88c6 2019-08-03 stsp } else {
5277 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5278 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5279 5f8a88c6 2019-08-03 stsp continue;
5280 5f8a88c6 2019-08-03 stsp }
5281 036813ee 2019-05-09 stsp
5282 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5283 036813ee 2019-05-09 stsp continue;
5284 036813ee 2019-05-09 stsp
5285 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5286 62d463ca 2020-10-20 naddy if (err)
5287 036813ee 2019-05-09 stsp return err;
5288 036813ee 2019-05-09 stsp if (!path_matches)
5289 036813ee 2019-05-09 stsp continue;
5290 036813ee 2019-05-09 stsp
5291 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5292 d34b633e 2020-10-19 stsp if (err)
5293 d34b633e 2020-10-19 stsp return err;
5294 d34b633e 2020-10-19 stsp
5295 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5296 d34b633e 2020-10-19 stsp free(ct_name);
5297 036813ee 2019-05-09 stsp continue;
5298 d34b633e 2020-10-19 stsp }
5299 d34b633e 2020-10-19 stsp free(ct_name);
5300 036813ee 2019-05-09 stsp
5301 036813ee 2019-05-09 stsp *ctp = ct;
5302 036813ee 2019-05-09 stsp break;
5303 036813ee 2019-05-09 stsp }
5304 2b496619 2019-07-10 stsp
5305 2b496619 2019-07-10 stsp return err;
5306 2b496619 2019-07-10 stsp }
5307 2b496619 2019-07-10 stsp
5308 2b496619 2019-07-10 stsp static const struct got_error *
5309 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5310 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5311 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5312 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5313 2b496619 2019-07-10 stsp struct got_repository *repo)
5314 2b496619 2019-07-10 stsp {
5315 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5316 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5317 2b496619 2019-07-10 stsp char *subtree_path;
5318 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5319 ba580f68 2020-03-22 stsp int nentries;
5320 2b496619 2019-07-10 stsp
5321 2b496619 2019-07-10 stsp *new_tep = NULL;
5322 2b496619 2019-07-10 stsp
5323 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5324 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5325 2b496619 2019-07-10 stsp child_path) == -1)
5326 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5327 036813ee 2019-05-09 stsp
5328 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5329 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5330 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5331 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5332 56e0773d 2019-11-28 stsp
5333 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5334 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5335 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5336 2b496619 2019-07-10 stsp goto done;
5337 2b496619 2019-07-10 stsp }
5338 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5339 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5340 2b496619 2019-07-10 stsp if (err) {
5341 56e0773d 2019-11-28 stsp free(new_te);
5342 2b496619 2019-07-10 stsp goto done;
5343 2b496619 2019-07-10 stsp }
5344 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5345 2b496619 2019-07-10 stsp done:
5346 56e0773d 2019-11-28 stsp free(id);
5347 2b496619 2019-07-10 stsp free(subtree_path);
5348 2b496619 2019-07-10 stsp if (err == NULL)
5349 2b496619 2019-07-10 stsp *new_tep = new_te;
5350 036813ee 2019-05-09 stsp return err;
5351 036813ee 2019-05-09 stsp }
5352 036813ee 2019-05-09 stsp
5353 036813ee 2019-05-09 stsp static const struct got_error *
5354 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5355 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5356 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5357 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5358 036813ee 2019-05-09 stsp struct got_repository *repo)
5359 036813ee 2019-05-09 stsp {
5360 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5361 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5362 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5363 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5364 036813ee 2019-05-09 stsp
5365 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5366 ba580f68 2020-03-22 stsp *nentries = 0;
5367 036813ee 2019-05-09 stsp
5368 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5369 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5370 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5371 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5372 036813ee 2019-05-09 stsp
5373 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5374 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5375 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5376 036813ee 2019-05-09 stsp continue;
5377 036813ee 2019-05-09 stsp
5378 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5379 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5380 036813ee 2019-05-09 stsp continue;
5381 036813ee 2019-05-09 stsp
5382 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5383 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5384 036813ee 2019-05-09 stsp if (err)
5385 036813ee 2019-05-09 stsp goto done;
5386 036813ee 2019-05-09 stsp
5387 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5388 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5389 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5390 036813ee 2019-05-09 stsp if (err)
5391 036813ee 2019-05-09 stsp goto done;
5392 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5393 afa376bf 2019-05-09 stsp if (err)
5394 afa376bf 2019-05-09 stsp goto done;
5395 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5396 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5397 2b496619 2019-07-10 stsp if (err)
5398 2f51b5b3 2019-05-09 stsp goto done;
5399 ba580f68 2020-03-22 stsp (*nentries)++;
5400 2b496619 2019-07-10 stsp } else {
5401 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5402 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5403 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5404 2b496619 2019-07-10 stsp == NULL) {
5405 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5406 2b496619 2019-07-10 stsp child_path, path_base_tree,
5407 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5408 2b496619 2019-07-10 stsp repo);
5409 2b496619 2019-07-10 stsp if (err)
5410 2b496619 2019-07-10 stsp goto done;
5411 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5412 2b496619 2019-07-10 stsp if (err)
5413 2b496619 2019-07-10 stsp goto done;
5414 ba580f68 2020-03-22 stsp (*nentries)++;
5415 9ba0479c 2019-05-10 stsp }
5416 ed175427 2019-05-09 stsp }
5417 2f51b5b3 2019-05-09 stsp }
5418 2f51b5b3 2019-05-09 stsp
5419 2f51b5b3 2019-05-09 stsp if (base_tree) {
5420 56e0773d 2019-11-28 stsp int i, nbase_entries;
5421 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5422 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5423 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5424 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5425 63c5ca5d 2019-08-24 stsp
5426 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5427 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5428 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5429 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5430 63c5ca5d 2019-08-24 stsp if (err)
5431 63c5ca5d 2019-08-24 stsp goto done;
5432 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5433 63c5ca5d 2019-08-24 stsp if (err)
5434 63c5ca5d 2019-08-24 stsp goto done;
5435 ba580f68 2020-03-22 stsp (*nentries)++;
5436 63c5ca5d 2019-08-24 stsp continue;
5437 63c5ca5d 2019-08-24 stsp }
5438 2f51b5b3 2019-05-09 stsp
5439 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5440 44d03001 2019-05-09 stsp int modified;
5441 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5442 036813ee 2019-05-09 stsp if (err)
5443 036813ee 2019-05-09 stsp goto done;
5444 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5445 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5446 2f51b5b3 2019-05-09 stsp if (err)
5447 2f51b5b3 2019-05-09 stsp goto done;
5448 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5449 44d03001 2019-05-09 stsp if (modified) {
5450 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5451 ba580f68 2020-03-22 stsp int nsubentries;
5452 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5453 ba580f68 2020-03-22 stsp &nsubentries, te,
5454 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5455 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5456 44d03001 2019-05-09 stsp if (err)
5457 44d03001 2019-05-09 stsp goto done;
5458 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5459 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5460 ba580f68 2020-03-22 stsp free(new_id);
5461 ba580f68 2020-03-22 stsp continue;
5462 ba580f68 2020-03-22 stsp }
5463 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5464 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5465 56e0773d 2019-11-28 stsp free(new_id);
5466 44d03001 2019-05-09 stsp }
5467 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5468 036813ee 2019-05-09 stsp if (err)
5469 036813ee 2019-05-09 stsp goto done;
5470 ba580f68 2020-03-22 stsp (*nentries)++;
5471 2f51b5b3 2019-05-09 stsp continue;
5472 036813ee 2019-05-09 stsp }
5473 2f51b5b3 2019-05-09 stsp
5474 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5475 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5476 f66c734c 2019-09-22 stsp if (err)
5477 f66c734c 2019-09-22 stsp goto done;
5478 2f51b5b3 2019-05-09 stsp if (ct) {
5479 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5480 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5481 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5482 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5483 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5484 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5485 2f51b5b3 2019-05-09 stsp if (err)
5486 2f51b5b3 2019-05-09 stsp goto done;
5487 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5488 2f51b5b3 2019-05-09 stsp if (err)
5489 2f51b5b3 2019-05-09 stsp goto done;
5490 ba580f68 2020-03-22 stsp (*nentries)++;
5491 2f51b5b3 2019-05-09 stsp }
5492 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5493 b416585c 2019-05-13 stsp status_arg);
5494 afa376bf 2019-05-09 stsp if (err)
5495 afa376bf 2019-05-09 stsp goto done;
5496 2f51b5b3 2019-05-09 stsp } else {
5497 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5498 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5499 2f51b5b3 2019-05-09 stsp if (err)
5500 2f51b5b3 2019-05-09 stsp goto done;
5501 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5502 2f51b5b3 2019-05-09 stsp if (err)
5503 2f51b5b3 2019-05-09 stsp goto done;
5504 ba580f68 2020-03-22 stsp (*nentries)++;
5505 2f51b5b3 2019-05-09 stsp }
5506 ca2503ea 2019-05-09 stsp }
5507 ed175427 2019-05-09 stsp }
5508 0b5cc0d6 2019-05-09 stsp
5509 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5510 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5511 ed175427 2019-05-09 stsp done:
5512 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5513 2e1fa222 2020-07-23 stsp return err;
5514 2e1fa222 2020-07-23 stsp }
5515 2e1fa222 2020-07-23 stsp
5516 2e1fa222 2020-07-23 stsp static const struct got_error *
5517 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5518 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5519 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5520 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5521 ebf99748 2019-05-09 stsp {
5522 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5523 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5524 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5525 ebf99748 2019-05-09 stsp
5526 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5527 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5528 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5529 ebf99748 2019-05-09 stsp
5530 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5531 437adc9d 2020-12-10 yzhong
5532 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5533 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5534 437adc9d 2020-12-10 yzhong if (err)
5535 437adc9d 2020-12-10 yzhong goto done;
5536 437adc9d 2020-12-10 yzhong
5537 ebf99748 2019-05-09 stsp if (ie) {
5538 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5539 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5540 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5541 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5542 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5543 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5544 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5545 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5546 437adc9d 2020-12-10 yzhong
5547 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5548 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5549 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5550 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5551 72fd46fa 2019-09-06 stsp !have_staged_files);
5552 ebf99748 2019-05-09 stsp } else
5553 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5554 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5555 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5556 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5557 72fd46fa 2019-09-06 stsp !have_staged_files);
5558 ebf99748 2019-05-09 stsp } else {
5559 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5560 ebf99748 2019-05-09 stsp if (err)
5561 437adc9d 2020-12-10 yzhong goto done;
5562 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5563 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5564 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5565 3969253a 2020-03-07 stsp if (err) {
5566 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5567 437adc9d 2020-12-10 yzhong goto done;
5568 3969253a 2020-03-07 stsp }
5569 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5570 3969253a 2020-03-07 stsp if (err) {
5571 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5572 437adc9d 2020-12-10 yzhong goto done;
5573 3969253a 2020-03-07 stsp }
5574 ebf99748 2019-05-09 stsp }
5575 437adc9d 2020-12-10 yzhong free(relpath);
5576 437adc9d 2020-12-10 yzhong relpath = NULL;
5577 ebf99748 2019-05-09 stsp }
5578 437adc9d 2020-12-10 yzhong done:
5579 437adc9d 2020-12-10 yzhong free(relpath);
5580 d56d26ce 2019-05-10 stsp return err;
5581 d56d26ce 2019-05-10 stsp }
5582 735ef5ac 2019-08-03 stsp
5583 d56d26ce 2019-05-10 stsp
5584 d56d26ce 2019-05-10 stsp static const struct got_error *
5585 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5586 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5587 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5588 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5589 735ef5ac 2019-08-03 stsp int ood_errcode)
5590 d56d26ce 2019-05-10 stsp {
5591 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5592 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5593 1a36436d 2019-06-10 stsp
5594 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5595 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5596 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5597 1a36436d 2019-06-10 stsp return NULL;
5598 9bead371 2019-07-28 stsp /*
5599 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5600 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5601 9bead371 2019-07-28 stsp */
5602 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5603 735ef5ac 2019-08-03 stsp in_repo_path);
5604 9bead371 2019-07-28 stsp if (err) {
5605 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5606 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5607 1a36436d 2019-06-10 stsp goto done;
5608 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5609 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5610 1a36436d 2019-06-10 stsp } else {
5611 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5612 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5613 735ef5ac 2019-08-03 stsp in_repo_path);
5614 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5615 1a36436d 2019-06-10 stsp goto done;
5616 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5617 1a36436d 2019-06-10 stsp }
5618 1a36436d 2019-06-10 stsp done:
5619 1a36436d 2019-06-10 stsp free(id);
5620 1a36436d 2019-06-10 stsp return err;
5621 ebf99748 2019-05-09 stsp }
5622 ebf99748 2019-05-09 stsp
5623 c4296144 2019-05-09 stsp const struct got_error *
5624 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5625 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5626 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5627 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5628 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5629 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5630 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5631 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5632 afa376bf 2019-05-09 stsp struct got_repository *repo)
5633 c4296144 2019-05-09 stsp {
5634 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5635 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5636 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5637 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5638 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5639 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5640 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5641 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5642 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5643 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5644 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5645 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5646 c4296144 2019-05-09 stsp
5647 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5648 c4296144 2019-05-09 stsp
5649 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5650 675c7539 2019-05-09 stsp
5651 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5652 588edf97 2019-05-10 stsp if (err)
5653 588edf97 2019-05-10 stsp goto done;
5654 de18fc63 2019-05-09 stsp
5655 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5656 588edf97 2019-05-10 stsp if (err)
5657 588edf97 2019-05-10 stsp goto done;
5658 588edf97 2019-05-10 stsp
5659 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5660 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5661 33ad4cbe 2019-05-12 jcs if (err)
5662 33ad4cbe 2019-05-12 jcs goto done;
5663 33ad4cbe 2019-05-12 jcs }
5664 c4296144 2019-05-09 stsp
5665 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5666 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5667 33ad4cbe 2019-05-12 jcs goto done;
5668 33ad4cbe 2019-05-12 jcs }
5669 33ad4cbe 2019-05-12 jcs
5670 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5671 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5672 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5673 cf066bf8 2019-05-09 stsp char *ondisk_path;
5674 cf066bf8 2019-05-09 stsp
5675 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5676 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5677 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5678 5f8a88c6 2019-08-03 stsp continue;
5679 5f8a88c6 2019-08-03 stsp
5680 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5681 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5682 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5683 cf066bf8 2019-05-09 stsp continue;
5684 cf066bf8 2019-05-09 stsp
5685 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5686 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5687 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5688 cf066bf8 2019-05-09 stsp goto done;
5689 cf066bf8 2019-05-09 stsp }
5690 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5691 2e1fa222 2020-07-23 stsp free(ondisk_path);
5692 2e1fa222 2020-07-23 stsp if (err)
5693 cf066bf8 2019-05-09 stsp goto done;
5694 cf066bf8 2019-05-09 stsp }
5695 036813ee 2019-05-09 stsp
5696 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5697 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5698 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5699 036813ee 2019-05-09 stsp if (err)
5700 036813ee 2019-05-09 stsp goto done;
5701 036813ee 2019-05-09 stsp
5702 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5703 de18fc63 2019-05-09 stsp if (err)
5704 de18fc63 2019-05-09 stsp goto done;
5705 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5706 10604dce 2021-09-24 thomas nparents++;
5707 10604dce 2021-09-24 thomas if (parent_id2) {
5708 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
5709 10604dce 2021-09-24 thomas if (err)
5710 10604dce 2021-09-24 thomas goto done;
5711 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5712 10604dce 2021-09-24 thomas nparents++;
5713 10604dce 2021-09-24 thomas }
5714 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5715 10604dce 2021-09-24 thomas nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5716 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5717 33ad4cbe 2019-05-12 jcs free(logmsg);
5718 9d40349a 2019-05-09 stsp if (err)
5719 9d40349a 2019-05-09 stsp goto done;
5720 9d40349a 2019-05-09 stsp
5721 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5722 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5723 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5724 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5725 09f5bd90 2019-05-09 stsp goto done;
5726 09f5bd90 2019-05-09 stsp }
5727 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5728 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5729 09f5bd90 2019-05-09 stsp if (err)
5730 09f5bd90 2019-05-09 stsp goto done;
5731 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5732 ebf99748 2019-05-09 stsp if (err)
5733 ebf99748 2019-05-09 stsp goto done;
5734 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5735 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5736 09f5bd90 2019-05-09 stsp goto done;
5737 09f5bd90 2019-05-09 stsp }
5738 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5739 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5740 f2c16586 2019-05-09 stsp if (err)
5741 f2c16586 2019-05-09 stsp goto done;
5742 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5743 f2c16586 2019-05-09 stsp if (err)
5744 f2c16586 2019-05-09 stsp goto done;
5745 f2c16586 2019-05-09 stsp
5746 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5747 09f5bd90 2019-05-09 stsp if (err)
5748 09f5bd90 2019-05-09 stsp goto done;
5749 09f5bd90 2019-05-09 stsp
5750 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5751 ebf99748 2019-05-09 stsp if (err)
5752 ebf99748 2019-05-09 stsp goto done;
5753 c4296144 2019-05-09 stsp done:
5754 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
5755 588edf97 2019-05-10 stsp if (head_tree)
5756 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5757 588edf97 2019-05-10 stsp if (head_commit)
5758 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5759 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5760 2f17228e 2019-05-12 stsp if (head_ref2) {
5761 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5762 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5763 2f17228e 2019-05-12 stsp err = unlockerr;
5764 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5765 39cd0ff6 2019-07-12 stsp }
5766 39cd0ff6 2019-07-12 stsp return err;
5767 39cd0ff6 2019-07-12 stsp }
5768 5c1e53bc 2019-07-28 stsp
5769 5c1e53bc 2019-07-28 stsp static const struct got_error *
5770 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5771 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5772 5c1e53bc 2019-07-28 stsp {
5773 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5774 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5775 39cd0ff6 2019-07-12 stsp
5776 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5777 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5778 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5779 5c1e53bc 2019-07-28 stsp
5780 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5781 5c1e53bc 2019-07-28 stsp ct_path++;
5782 5c1e53bc 2019-07-28 stsp
5783 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5784 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5785 5c1e53bc 2019-07-28 stsp break;
5786 5c1e53bc 2019-07-28 stsp }
5787 5c1e53bc 2019-07-28 stsp
5788 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5789 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5790 5c1e53bc 2019-07-28 stsp
5791 5c1e53bc 2019-07-28 stsp return NULL;
5792 5c1e53bc 2019-07-28 stsp }
5793 5c1e53bc 2019-07-28 stsp
5794 f0b75401 2019-08-03 stsp static const struct got_error *
5795 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5796 f0b75401 2019-08-03 stsp {
5797 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5798 f0b75401 2019-08-03 stsp
5799 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5800 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5801 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5802 f0b75401 2019-08-03 stsp }
5803 f0b75401 2019-08-03 stsp
5804 f0b75401 2019-08-03 stsp return NULL;
5805 f0b75401 2019-08-03 stsp }
5806 f0b75401 2019-08-03 stsp
5807 5f8a88c6 2019-08-03 stsp static const struct got_error *
5808 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5809 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5810 5f8a88c6 2019-08-03 stsp {
5811 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5812 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5813 5f8a88c6 2019-08-03 stsp
5814 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5815 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5816 5f8a88c6 2019-08-03 stsp continue;
5817 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5818 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5819 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5820 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5821 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5822 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5823 5f8a88c6 2019-08-03 stsp }
5824 5f8a88c6 2019-08-03 stsp
5825 5f8a88c6 2019-08-03 stsp return NULL;
5826 5f8a88c6 2019-08-03 stsp }
5827 5f8a88c6 2019-08-03 stsp
5828 39cd0ff6 2019-07-12 stsp const struct got_error *
5829 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5830 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5831 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5832 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5833 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5834 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5835 39cd0ff6 2019-07-12 stsp {
5836 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5837 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5838 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5839 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5840 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5841 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5842 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5843 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5844 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5845 39cd0ff6 2019-07-12 stsp
5846 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5847 39cd0ff6 2019-07-12 stsp
5848 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5849 39cd0ff6 2019-07-12 stsp
5850 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5851 39cd0ff6 2019-07-12 stsp if (err)
5852 39cd0ff6 2019-07-12 stsp goto done;
5853 39cd0ff6 2019-07-12 stsp
5854 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5855 39cd0ff6 2019-07-12 stsp if (err)
5856 39cd0ff6 2019-07-12 stsp goto done;
5857 39cd0ff6 2019-07-12 stsp
5858 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5859 39cd0ff6 2019-07-12 stsp if (err)
5860 39cd0ff6 2019-07-12 stsp goto done;
5861 39cd0ff6 2019-07-12 stsp
5862 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5863 39cd0ff6 2019-07-12 stsp if (err)
5864 39cd0ff6 2019-07-12 stsp goto done;
5865 39cd0ff6 2019-07-12 stsp
5866 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5867 5f8a88c6 2019-08-03 stsp &have_staged_files);
5868 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5869 5f8a88c6 2019-08-03 stsp goto done;
5870 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5871 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5872 5f8a88c6 2019-08-03 stsp if (err)
5873 5f8a88c6 2019-08-03 stsp goto done;
5874 5f8a88c6 2019-08-03 stsp }
5875 5f8a88c6 2019-08-03 stsp
5876 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5877 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5878 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5879 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5880 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5881 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5882 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5883 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5884 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5885 5c1e53bc 2019-07-28 stsp if (err)
5886 5c1e53bc 2019-07-28 stsp goto done;
5887 5c1e53bc 2019-07-28 stsp }
5888 39cd0ff6 2019-07-12 stsp
5889 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5890 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5891 39cd0ff6 2019-07-12 stsp goto done;
5892 5c1e53bc 2019-07-28 stsp }
5893 5c1e53bc 2019-07-28 stsp
5894 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5895 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5896 5c1e53bc 2019-07-28 stsp if (err)
5897 5c1e53bc 2019-07-28 stsp goto done;
5898 39cd0ff6 2019-07-12 stsp }
5899 f0b75401 2019-08-03 stsp
5900 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5901 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5902 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5903 f0b75401 2019-08-03 stsp
5904 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5905 f0b75401 2019-08-03 stsp ct_path++;
5906 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5907 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5908 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5909 b50cabdf 2019-07-12 stsp if (err)
5910 b50cabdf 2019-07-12 stsp goto done;
5911 f0b75401 2019-08-03 stsp
5912 b50cabdf 2019-07-12 stsp }
5913 b50cabdf 2019-07-12 stsp
5914 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5915 10604dce 2021-09-24 thomas head_commit_id, NULL, worktree, author, committer,
5916 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5917 39cd0ff6 2019-07-12 stsp if (err)
5918 39cd0ff6 2019-07-12 stsp goto done;
5919 39cd0ff6 2019-07-12 stsp
5920 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5921 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5922 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5923 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5924 39cd0ff6 2019-07-12 stsp err = sync_err;
5925 39cd0ff6 2019-07-12 stsp done:
5926 39cd0ff6 2019-07-12 stsp if (fileindex)
5927 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5928 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5929 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5930 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5931 39cd0ff6 2019-07-12 stsp err = unlockerr;
5932 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5933 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5934 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5935 39cd0ff6 2019-07-12 stsp }
5936 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5937 c4296144 2019-05-09 stsp return err;
5938 8656d6c4 2019-05-20 stsp }
5939 8656d6c4 2019-05-20 stsp
5940 8656d6c4 2019-05-20 stsp const char *
5941 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5942 8656d6c4 2019-05-20 stsp {
5943 8656d6c4 2019-05-20 stsp return ct->path;
5944 8656d6c4 2019-05-20 stsp }
5945 8656d6c4 2019-05-20 stsp
5946 8656d6c4 2019-05-20 stsp unsigned int
5947 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5948 8656d6c4 2019-05-20 stsp {
5949 8656d6c4 2019-05-20 stsp return ct->status;
5950 818c7501 2019-07-11 stsp }
5951 818c7501 2019-07-11 stsp
5952 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5953 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5954 818c7501 2019-07-11 stsp struct got_repository *repo;
5955 818c7501 2019-07-11 stsp };
5956 818c7501 2019-07-11 stsp
5957 818c7501 2019-07-11 stsp static const struct got_error *
5958 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5959 818c7501 2019-07-11 stsp {
5960 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5961 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5962 818c7501 2019-07-11 stsp unsigned char status;
5963 818c7501 2019-07-11 stsp struct stat sb;
5964 818c7501 2019-07-11 stsp char *ondisk_path;
5965 818c7501 2019-07-11 stsp
5966 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5967 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5968 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5969 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5970 818c7501 2019-07-11 stsp
5971 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5972 818c7501 2019-07-11 stsp == -1)
5973 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5974 818c7501 2019-07-11 stsp
5975 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5976 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5977 818c7501 2019-07-11 stsp free(ondisk_path);
5978 818c7501 2019-07-11 stsp if (err)
5979 818c7501 2019-07-11 stsp return err;
5980 818c7501 2019-07-11 stsp
5981 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5982 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5983 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5984 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5985 818c7501 2019-07-11 stsp
5986 818c7501 2019-07-11 stsp return NULL;
5987 818c7501 2019-07-11 stsp }
5988 818c7501 2019-07-11 stsp
5989 818c7501 2019-07-11 stsp const struct got_error *
5990 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5991 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5992 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5993 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5994 818c7501 2019-07-11 stsp {
5995 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5996 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5997 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5998 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5999 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6000 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6001 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6002 818c7501 2019-07-11 stsp
6003 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6004 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6005 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6006 818c7501 2019-07-11 stsp
6007 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6008 818c7501 2019-07-11 stsp if (err)
6009 818c7501 2019-07-11 stsp return err;
6010 818c7501 2019-07-11 stsp
6011 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6012 818c7501 2019-07-11 stsp if (err)
6013 818c7501 2019-07-11 stsp goto done;
6014 818c7501 2019-07-11 stsp
6015 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6016 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6017 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6018 818c7501 2019-07-11 stsp &ok_arg);
6019 818c7501 2019-07-11 stsp if (err)
6020 818c7501 2019-07-11 stsp goto done;
6021 818c7501 2019-07-11 stsp
6022 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6023 818c7501 2019-07-11 stsp if (err)
6024 818c7501 2019-07-11 stsp goto done;
6025 818c7501 2019-07-11 stsp
6026 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6027 818c7501 2019-07-11 stsp if (err)
6028 818c7501 2019-07-11 stsp goto done;
6029 818c7501 2019-07-11 stsp
6030 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6031 818c7501 2019-07-11 stsp if (err)
6032 818c7501 2019-07-11 stsp goto done;
6033 818c7501 2019-07-11 stsp
6034 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6035 818c7501 2019-07-11 stsp 0);
6036 e51d7b55 2020-01-04 stsp if (err)
6037 e51d7b55 2020-01-04 stsp goto done;
6038 e51d7b55 2020-01-04 stsp
6039 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6040 818c7501 2019-07-11 stsp if (err)
6041 818c7501 2019-07-11 stsp goto done;
6042 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6043 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6044 e51d7b55 2020-01-04 stsp goto done;
6045 e51d7b55 2020-01-04 stsp }
6046 818c7501 2019-07-11 stsp
6047 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6048 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6049 818c7501 2019-07-11 stsp if (err)
6050 818c7501 2019-07-11 stsp goto done;
6051 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6052 818c7501 2019-07-11 stsp if (err)
6053 818c7501 2019-07-11 stsp goto done;
6054 818c7501 2019-07-11 stsp
6055 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6056 818c7501 2019-07-11 stsp
6057 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6058 818c7501 2019-07-11 stsp if (err)
6059 818c7501 2019-07-11 stsp goto done;
6060 818c7501 2019-07-11 stsp
6061 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6062 818c7501 2019-07-11 stsp if (err)
6063 818c7501 2019-07-11 stsp goto done;
6064 818c7501 2019-07-11 stsp
6065 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6066 818c7501 2019-07-11 stsp worktree->base_commit_id);
6067 818c7501 2019-07-11 stsp if (err)
6068 818c7501 2019-07-11 stsp goto done;
6069 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6070 818c7501 2019-07-11 stsp if (err)
6071 818c7501 2019-07-11 stsp goto done;
6072 818c7501 2019-07-11 stsp
6073 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6074 818c7501 2019-07-11 stsp if (err)
6075 818c7501 2019-07-11 stsp goto done;
6076 818c7501 2019-07-11 stsp done:
6077 818c7501 2019-07-11 stsp free(fileindex_path);
6078 818c7501 2019-07-11 stsp free(tmp_branch_name);
6079 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6080 818c7501 2019-07-11 stsp free(branch_ref_name);
6081 818c7501 2019-07-11 stsp if (branch_ref)
6082 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6083 818c7501 2019-07-11 stsp if (wt_branch)
6084 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6085 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6086 818c7501 2019-07-11 stsp if (err) {
6087 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6088 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6089 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6090 818c7501 2019-07-11 stsp }
6091 818c7501 2019-07-11 stsp if (*tmp_branch) {
6092 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6093 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6094 818c7501 2019-07-11 stsp }
6095 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6096 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6097 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6098 3e3a69f1 2019-07-25 stsp }
6099 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6100 818c7501 2019-07-11 stsp }
6101 818c7501 2019-07-11 stsp return err;
6102 818c7501 2019-07-11 stsp }
6103 818c7501 2019-07-11 stsp
6104 818c7501 2019-07-11 stsp const struct got_error *
6105 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6106 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6107 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6108 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6109 818c7501 2019-07-11 stsp {
6110 818c7501 2019-07-11 stsp const struct got_error *err;
6111 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6112 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6113 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6114 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6115 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6116 818c7501 2019-07-11 stsp
6117 818c7501 2019-07-11 stsp *commit_id = NULL;
6118 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6119 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6120 3e3a69f1 2019-07-25 stsp *branch = NULL;
6121 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6122 3e3a69f1 2019-07-25 stsp
6123 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6124 3e3a69f1 2019-07-25 stsp if (err)
6125 3e3a69f1 2019-07-25 stsp return err;
6126 818c7501 2019-07-11 stsp
6127 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6128 3e3a69f1 2019-07-25 stsp if (err)
6129 f032f1f7 2019-08-04 stsp goto done;
6130 f032f1f7 2019-08-04 stsp
6131 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6132 f032f1f7 2019-08-04 stsp &have_staged_files);
6133 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6134 f032f1f7 2019-08-04 stsp goto done;
6135 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6136 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6137 3e3a69f1 2019-07-25 stsp goto done;
6138 f032f1f7 2019-08-04 stsp }
6139 3e3a69f1 2019-07-25 stsp
6140 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6141 818c7501 2019-07-11 stsp if (err)
6142 f032f1f7 2019-08-04 stsp goto done;
6143 818c7501 2019-07-11 stsp
6144 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6145 818c7501 2019-07-11 stsp if (err)
6146 818c7501 2019-07-11 stsp goto done;
6147 818c7501 2019-07-11 stsp
6148 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6149 818c7501 2019-07-11 stsp if (err)
6150 818c7501 2019-07-11 stsp goto done;
6151 818c7501 2019-07-11 stsp
6152 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6153 818c7501 2019-07-11 stsp if (err)
6154 818c7501 2019-07-11 stsp goto done;
6155 818c7501 2019-07-11 stsp
6156 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6157 818c7501 2019-07-11 stsp if (err)
6158 818c7501 2019-07-11 stsp goto done;
6159 818c7501 2019-07-11 stsp
6160 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6161 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6162 818c7501 2019-07-11 stsp if (err)
6163 818c7501 2019-07-11 stsp goto done;
6164 818c7501 2019-07-11 stsp
6165 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6166 818c7501 2019-07-11 stsp if (err)
6167 818c7501 2019-07-11 stsp goto done;
6168 818c7501 2019-07-11 stsp
6169 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6170 818c7501 2019-07-11 stsp if (err)
6171 818c7501 2019-07-11 stsp goto done;
6172 818c7501 2019-07-11 stsp
6173 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6174 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6175 818c7501 2019-07-11 stsp if (err)
6176 818c7501 2019-07-11 stsp goto done;
6177 818c7501 2019-07-11 stsp
6178 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6179 818c7501 2019-07-11 stsp if (err)
6180 818c7501 2019-07-11 stsp goto done;
6181 818c7501 2019-07-11 stsp done:
6182 818c7501 2019-07-11 stsp free(commit_ref_name);
6183 818c7501 2019-07-11 stsp free(branch_ref_name);
6184 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6185 818c7501 2019-07-11 stsp if (commit_ref)
6186 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6187 818c7501 2019-07-11 stsp if (branch_ref)
6188 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6189 818c7501 2019-07-11 stsp if (err) {
6190 818c7501 2019-07-11 stsp free(*commit_id);
6191 818c7501 2019-07-11 stsp *commit_id = NULL;
6192 818c7501 2019-07-11 stsp if (*tmp_branch) {
6193 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6194 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6195 818c7501 2019-07-11 stsp }
6196 818c7501 2019-07-11 stsp if (*new_base_branch) {
6197 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6198 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6199 818c7501 2019-07-11 stsp }
6200 818c7501 2019-07-11 stsp if (*branch) {
6201 818c7501 2019-07-11 stsp got_ref_close(*branch);
6202 818c7501 2019-07-11 stsp *branch = NULL;
6203 818c7501 2019-07-11 stsp }
6204 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6205 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6206 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6207 3e3a69f1 2019-07-25 stsp }
6208 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6209 818c7501 2019-07-11 stsp }
6210 818c7501 2019-07-11 stsp return err;
6211 c4296144 2019-05-09 stsp }
6212 818c7501 2019-07-11 stsp
6213 818c7501 2019-07-11 stsp const struct got_error *
6214 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6215 818c7501 2019-07-11 stsp {
6216 818c7501 2019-07-11 stsp const struct got_error *err;
6217 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6218 818c7501 2019-07-11 stsp
6219 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6220 818c7501 2019-07-11 stsp if (err)
6221 818c7501 2019-07-11 stsp return err;
6222 818c7501 2019-07-11 stsp
6223 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6224 818c7501 2019-07-11 stsp free(tmp_branch_name);
6225 818c7501 2019-07-11 stsp return NULL;
6226 818c7501 2019-07-11 stsp }
6227 818c7501 2019-07-11 stsp
6228 818c7501 2019-07-11 stsp static const struct got_error *
6229 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6230 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6231 818c7501 2019-07-11 stsp {
6232 0ebf8283 2019-07-24 stsp *logmsg = arg;
6233 818c7501 2019-07-11 stsp return NULL;
6234 818c7501 2019-07-11 stsp }
6235 818c7501 2019-07-11 stsp
6236 818c7501 2019-07-11 stsp static const struct got_error *
6237 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6238 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6239 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6240 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6241 818c7501 2019-07-11 stsp {
6242 818c7501 2019-07-11 stsp return NULL;
6243 01757395 2019-07-12 stsp }
6244 01757395 2019-07-12 stsp
6245 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6246 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6247 01757395 2019-07-12 stsp void *progress_arg;
6248 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6249 01757395 2019-07-12 stsp };
6250 01757395 2019-07-12 stsp
6251 01757395 2019-07-12 stsp static const struct got_error *
6252 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6253 01757395 2019-07-12 stsp {
6254 01757395 2019-07-12 stsp const struct got_error *err;
6255 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6256 01757395 2019-07-12 stsp char *p;
6257 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6258 01757395 2019-07-12 stsp
6259 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6260 01757395 2019-07-12 stsp if (err)
6261 01757395 2019-07-12 stsp return err;
6262 01757395 2019-07-12 stsp
6263 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6264 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6265 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6266 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6267 01757395 2019-07-12 stsp return NULL;
6268 01757395 2019-07-12 stsp
6269 01757395 2019-07-12 stsp p = strdup(path);
6270 01757395 2019-07-12 stsp if (p == NULL)
6271 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6272 01757395 2019-07-12 stsp
6273 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6274 01757395 2019-07-12 stsp if (err || new == NULL)
6275 01757395 2019-07-12 stsp free(p);
6276 01757395 2019-07-12 stsp return err;
6277 01757395 2019-07-12 stsp }
6278 01757395 2019-07-12 stsp
6279 01757395 2019-07-12 stsp void
6280 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6281 01757395 2019-07-12 stsp {
6282 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6283 01757395 2019-07-12 stsp
6284 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6285 01757395 2019-07-12 stsp free((char *)pe->path);
6286 01757395 2019-07-12 stsp
6287 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6288 818c7501 2019-07-11 stsp }
6289 818c7501 2019-07-11 stsp
6290 0ebf8283 2019-07-24 stsp static const struct got_error *
6291 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6292 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6293 818c7501 2019-07-11 stsp {
6294 818c7501 2019-07-11 stsp const struct got_error *err;
6295 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6296 818c7501 2019-07-11 stsp
6297 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6298 818c7501 2019-07-11 stsp if (err) {
6299 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6300 818c7501 2019-07-11 stsp goto done;
6301 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6302 818c7501 2019-07-11 stsp if (err)
6303 818c7501 2019-07-11 stsp goto done;
6304 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6305 818c7501 2019-07-11 stsp if (err)
6306 818c7501 2019-07-11 stsp goto done;
6307 de05890f 2020-03-05 stsp } else if (is_rebase) {
6308 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6309 818c7501 2019-07-11 stsp int cmp;
6310 818c7501 2019-07-11 stsp
6311 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6312 818c7501 2019-07-11 stsp if (err)
6313 818c7501 2019-07-11 stsp goto done;
6314 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6315 818c7501 2019-07-11 stsp free(stored_id);
6316 818c7501 2019-07-11 stsp if (cmp != 0) {
6317 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6318 818c7501 2019-07-11 stsp goto done;
6319 818c7501 2019-07-11 stsp }
6320 818c7501 2019-07-11 stsp }
6321 0ebf8283 2019-07-24 stsp done:
6322 0ebf8283 2019-07-24 stsp if (commit_ref)
6323 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6324 0ebf8283 2019-07-24 stsp return err;
6325 0ebf8283 2019-07-24 stsp }
6326 0ebf8283 2019-07-24 stsp
6327 0ebf8283 2019-07-24 stsp static const struct got_error *
6328 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6329 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6330 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6331 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6332 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6333 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6334 0ebf8283 2019-07-24 stsp {
6335 0ebf8283 2019-07-24 stsp const struct got_error *err;
6336 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6337 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6338 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6339 818c7501 2019-07-11 stsp
6340 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6341 0ebf8283 2019-07-24 stsp
6342 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6343 0ebf8283 2019-07-24 stsp if (err)
6344 0ebf8283 2019-07-24 stsp return err;
6345 0ebf8283 2019-07-24 stsp
6346 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6347 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6348 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6349 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6350 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6351 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6352 818c7501 2019-07-11 stsp if (commit_ref)
6353 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6354 818c7501 2019-07-11 stsp return err;
6355 818c7501 2019-07-11 stsp }
6356 818c7501 2019-07-11 stsp
6357 818c7501 2019-07-11 stsp const struct got_error *
6358 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6359 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6360 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6361 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6362 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6363 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6364 818c7501 2019-07-11 stsp {
6365 0ebf8283 2019-07-24 stsp const struct got_error *err;
6366 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6367 0ebf8283 2019-07-24 stsp
6368 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6369 0ebf8283 2019-07-24 stsp if (err)
6370 0ebf8283 2019-07-24 stsp return err;
6371 0ebf8283 2019-07-24 stsp
6372 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6373 0ebf8283 2019-07-24 stsp if (err)
6374 0ebf8283 2019-07-24 stsp goto done;
6375 0ebf8283 2019-07-24 stsp
6376 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6377 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6378 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6379 0ebf8283 2019-07-24 stsp done:
6380 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6381 0ebf8283 2019-07-24 stsp return err;
6382 0ebf8283 2019-07-24 stsp }
6383 0ebf8283 2019-07-24 stsp
6384 0ebf8283 2019-07-24 stsp const struct got_error *
6385 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6386 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6387 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6388 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6389 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6390 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6391 0ebf8283 2019-07-24 stsp {
6392 0ebf8283 2019-07-24 stsp const struct got_error *err;
6393 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6394 0ebf8283 2019-07-24 stsp
6395 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6396 0ebf8283 2019-07-24 stsp if (err)
6397 0ebf8283 2019-07-24 stsp return err;
6398 0ebf8283 2019-07-24 stsp
6399 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6400 0ebf8283 2019-07-24 stsp if (err)
6401 0ebf8283 2019-07-24 stsp goto done;
6402 0ebf8283 2019-07-24 stsp
6403 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6404 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6405 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6406 0ebf8283 2019-07-24 stsp done:
6407 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6408 0ebf8283 2019-07-24 stsp return err;
6409 0ebf8283 2019-07-24 stsp }
6410 0ebf8283 2019-07-24 stsp
6411 0ebf8283 2019-07-24 stsp static const struct got_error *
6412 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6413 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6414 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6415 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6416 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6417 0ebf8283 2019-07-24 stsp {
6418 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6419 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6420 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6421 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6422 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6423 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6424 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6425 818c7501 2019-07-11 stsp
6426 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6427 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6428 a0e95631 2019-07-12 stsp
6429 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6430 818c7501 2019-07-11 stsp
6431 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6432 a0e95631 2019-07-12 stsp if (err)
6433 3e3a69f1 2019-07-25 stsp return err;
6434 a0e95631 2019-07-12 stsp
6435 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6436 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6437 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6438 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6439 01757395 2019-07-12 stsp /*
6440 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6441 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6442 6c13b005 2021-09-02 stsp *
6443 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6444 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6445 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6446 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6447 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6448 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6449 01757395 2019-07-12 stsp */
6450 01757395 2019-07-12 stsp if (merged_paths) {
6451 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6452 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6453 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6454 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6455 f2a9dc41 2019-12-13 tracey 0);
6456 01757395 2019-07-12 stsp if (err)
6457 01757395 2019-07-12 stsp goto done;
6458 01757395 2019-07-12 stsp }
6459 01757395 2019-07-12 stsp } else {
6460 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6461 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6462 01757395 2019-07-12 stsp if (err)
6463 01757395 2019-07-12 stsp goto done;
6464 01757395 2019-07-12 stsp }
6465 a0e95631 2019-07-12 stsp
6466 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6467 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6468 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6469 a0e95631 2019-07-12 stsp if (err)
6470 a0e95631 2019-07-12 stsp goto done;
6471 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6472 a0e95631 2019-07-12 stsp goto done;
6473 ff0d2220 2019-07-11 stsp }
6474 818c7501 2019-07-11 stsp
6475 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6476 edd02c5e 2019-07-11 stsp if (err)
6477 edd02c5e 2019-07-11 stsp goto done;
6478 edd02c5e 2019-07-11 stsp
6479 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6480 818c7501 2019-07-11 stsp if (err)
6481 818c7501 2019-07-11 stsp goto done;
6482 818c7501 2019-07-11 stsp
6483 5943eee2 2019-08-13 stsp if (new_logmsg) {
6484 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6485 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6486 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6487 5943eee2 2019-08-13 stsp goto done;
6488 5943eee2 2019-08-13 stsp }
6489 5943eee2 2019-08-13 stsp } else {
6490 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6491 5943eee2 2019-08-13 stsp if (err)
6492 5943eee2 2019-08-13 stsp goto done;
6493 5943eee2 2019-08-13 stsp }
6494 0ebf8283 2019-07-24 stsp
6495 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6496 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6497 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6498 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6499 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6500 a0e95631 2019-07-12 stsp if (err)
6501 a0e95631 2019-07-12 stsp goto done;
6502 a0e95631 2019-07-12 stsp
6503 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6504 a0e95631 2019-07-12 stsp if (err)
6505 a0e95631 2019-07-12 stsp goto done;
6506 a0e95631 2019-07-12 stsp
6507 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6508 a0e95631 2019-07-12 stsp if (err)
6509 a0e95631 2019-07-12 stsp goto done;
6510 a0e95631 2019-07-12 stsp
6511 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6512 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6513 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6514 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6515 a0e95631 2019-07-12 stsp err = sync_err;
6516 818c7501 2019-07-11 stsp done:
6517 a0e95631 2019-07-12 stsp free(fileindex_path);
6518 a0e95631 2019-07-12 stsp free(head_commit_id);
6519 a0e95631 2019-07-12 stsp if (head_ref)
6520 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6521 818c7501 2019-07-11 stsp if (err) {
6522 818c7501 2019-07-11 stsp free(*new_commit_id);
6523 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6524 818c7501 2019-07-11 stsp }
6525 818c7501 2019-07-11 stsp return err;
6526 818c7501 2019-07-11 stsp }
6527 818c7501 2019-07-11 stsp
6528 818c7501 2019-07-11 stsp const struct got_error *
6529 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6530 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6531 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6532 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6533 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6534 0ebf8283 2019-07-24 stsp {
6535 0ebf8283 2019-07-24 stsp const struct got_error *err;
6536 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6537 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6538 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6539 0ebf8283 2019-07-24 stsp
6540 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6541 0ebf8283 2019-07-24 stsp if (err)
6542 0ebf8283 2019-07-24 stsp return err;
6543 0ebf8283 2019-07-24 stsp
6544 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6545 0ebf8283 2019-07-24 stsp if (err)
6546 0ebf8283 2019-07-24 stsp goto done;
6547 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6548 0ebf8283 2019-07-24 stsp if (err)
6549 0ebf8283 2019-07-24 stsp goto done;
6550 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6551 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6552 0ebf8283 2019-07-24 stsp goto done;
6553 0ebf8283 2019-07-24 stsp }
6554 0ebf8283 2019-07-24 stsp
6555 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6556 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6557 0ebf8283 2019-07-24 stsp done:
6558 0ebf8283 2019-07-24 stsp if (commit_ref)
6559 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6560 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6561 0ebf8283 2019-07-24 stsp free(commit_id);
6562 0ebf8283 2019-07-24 stsp return err;
6563 0ebf8283 2019-07-24 stsp }
6564 0ebf8283 2019-07-24 stsp
6565 0ebf8283 2019-07-24 stsp const struct got_error *
6566 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6567 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6568 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6569 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6570 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6571 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6572 0ebf8283 2019-07-24 stsp {
6573 0ebf8283 2019-07-24 stsp const struct got_error *err;
6574 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6575 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6576 0ebf8283 2019-07-24 stsp
6577 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6578 0ebf8283 2019-07-24 stsp if (err)
6579 0ebf8283 2019-07-24 stsp return err;
6580 0ebf8283 2019-07-24 stsp
6581 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6582 0ebf8283 2019-07-24 stsp if (err)
6583 0ebf8283 2019-07-24 stsp goto done;
6584 0ebf8283 2019-07-24 stsp
6585 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6586 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6587 0ebf8283 2019-07-24 stsp done:
6588 0ebf8283 2019-07-24 stsp if (commit_ref)
6589 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6590 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6591 0ebf8283 2019-07-24 stsp return err;
6592 0ebf8283 2019-07-24 stsp }
6593 0ebf8283 2019-07-24 stsp
6594 0ebf8283 2019-07-24 stsp const struct got_error *
6595 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6596 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6597 818c7501 2019-07-11 stsp {
6598 3e3a69f1 2019-07-25 stsp if (fileindex)
6599 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6600 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6601 69844fba 2019-07-11 stsp }
6602 69844fba 2019-07-11 stsp
6603 69844fba 2019-07-11 stsp static const struct got_error *
6604 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6605 69844fba 2019-07-11 stsp {
6606 69844fba 2019-07-11 stsp const struct got_error *err;
6607 69844fba 2019-07-11 stsp struct got_reference *ref;
6608 69844fba 2019-07-11 stsp
6609 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6610 69844fba 2019-07-11 stsp if (err) {
6611 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6612 69844fba 2019-07-11 stsp return NULL;
6613 69844fba 2019-07-11 stsp return err;
6614 69844fba 2019-07-11 stsp }
6615 69844fba 2019-07-11 stsp
6616 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6617 69844fba 2019-07-11 stsp got_ref_close(ref);
6618 69844fba 2019-07-11 stsp return err;
6619 818c7501 2019-07-11 stsp }
6620 818c7501 2019-07-11 stsp
6621 69844fba 2019-07-11 stsp static const struct got_error *
6622 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6623 69844fba 2019-07-11 stsp {
6624 69844fba 2019-07-11 stsp const struct got_error *err;
6625 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6626 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6627 69844fba 2019-07-11 stsp
6628 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6629 69844fba 2019-07-11 stsp if (err)
6630 69844fba 2019-07-11 stsp goto done;
6631 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6632 69844fba 2019-07-11 stsp if (err)
6633 69844fba 2019-07-11 stsp goto done;
6634 69844fba 2019-07-11 stsp
6635 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6636 69844fba 2019-07-11 stsp if (err)
6637 69844fba 2019-07-11 stsp goto done;
6638 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6639 69844fba 2019-07-11 stsp if (err)
6640 69844fba 2019-07-11 stsp goto done;
6641 69844fba 2019-07-11 stsp
6642 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6643 69844fba 2019-07-11 stsp if (err)
6644 69844fba 2019-07-11 stsp goto done;
6645 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6646 69844fba 2019-07-11 stsp if (err)
6647 69844fba 2019-07-11 stsp goto done;
6648 69844fba 2019-07-11 stsp
6649 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6650 69844fba 2019-07-11 stsp if (err)
6651 69844fba 2019-07-11 stsp goto done;
6652 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6653 69844fba 2019-07-11 stsp if (err)
6654 69844fba 2019-07-11 stsp goto done;
6655 69844fba 2019-07-11 stsp
6656 69844fba 2019-07-11 stsp done:
6657 69844fba 2019-07-11 stsp free(tmp_branch_name);
6658 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6659 69844fba 2019-07-11 stsp free(branch_ref_name);
6660 69844fba 2019-07-11 stsp free(commit_ref_name);
6661 e600f124 2021-03-21 stsp return err;
6662 e600f124 2021-03-21 stsp }
6663 e600f124 2021-03-21 stsp
6664 e600f124 2021-03-21 stsp const struct got_error *
6665 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6666 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6667 e600f124 2021-03-21 stsp {
6668 e600f124 2021-03-21 stsp const struct got_error *err;
6669 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6670 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6671 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6672 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6673 e600f124 2021-03-21 stsp char *refname = NULL;
6674 e600f124 2021-03-21 stsp
6675 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6676 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6677 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6678 e600f124 2021-03-21 stsp branch_name += 11;
6679 e600f124 2021-03-21 stsp
6680 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6681 e600f124 2021-03-21 stsp if (err)
6682 e600f124 2021-03-21 stsp return err;
6683 e600f124 2021-03-21 stsp
6684 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6685 e600f124 2021-03-21 stsp new_id_str) == -1) {
6686 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6687 e600f124 2021-03-21 stsp goto done;
6688 e600f124 2021-03-21 stsp }
6689 e600f124 2021-03-21 stsp
6690 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6691 e600f124 2021-03-21 stsp if (err)
6692 e600f124 2021-03-21 stsp goto done;
6693 e600f124 2021-03-21 stsp
6694 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6695 e600f124 2021-03-21 stsp if (err)
6696 e600f124 2021-03-21 stsp goto done;
6697 e600f124 2021-03-21 stsp
6698 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6699 e600f124 2021-03-21 stsp done:
6700 e600f124 2021-03-21 stsp free(new_id_str);
6701 e600f124 2021-03-21 stsp free(refname);
6702 e600f124 2021-03-21 stsp free(old_commit_id);
6703 e600f124 2021-03-21 stsp if (ref)
6704 e600f124 2021-03-21 stsp got_ref_close(ref);
6705 69844fba 2019-07-11 stsp return err;
6706 69844fba 2019-07-11 stsp }
6707 69844fba 2019-07-11 stsp
6708 818c7501 2019-07-11 stsp const struct got_error *
6709 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6710 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6711 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6712 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6713 818c7501 2019-07-11 stsp {
6714 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6715 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6716 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6717 818c7501 2019-07-11 stsp
6718 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6719 818c7501 2019-07-11 stsp if (err)
6720 818c7501 2019-07-11 stsp return err;
6721 e600f124 2021-03-21 stsp
6722 e600f124 2021-03-21 stsp if (create_backup) {
6723 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6724 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6725 e600f124 2021-03-21 stsp if (err)
6726 e600f124 2021-03-21 stsp goto done;
6727 e600f124 2021-03-21 stsp }
6728 818c7501 2019-07-11 stsp
6729 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6730 818c7501 2019-07-11 stsp if (err)
6731 818c7501 2019-07-11 stsp goto done;
6732 818c7501 2019-07-11 stsp
6733 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6734 818c7501 2019-07-11 stsp if (err)
6735 818c7501 2019-07-11 stsp goto done;
6736 818c7501 2019-07-11 stsp
6737 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6738 818c7501 2019-07-11 stsp if (err)
6739 818c7501 2019-07-11 stsp goto done;
6740 818c7501 2019-07-11 stsp
6741 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6742 a615e0e7 2020-12-16 stsp if (err)
6743 a615e0e7 2020-12-16 stsp goto done;
6744 a615e0e7 2020-12-16 stsp
6745 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6746 a615e0e7 2020-12-16 stsp if (err)
6747 a615e0e7 2020-12-16 stsp goto done;
6748 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6749 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6750 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6751 a615e0e7 2020-12-16 stsp err = sync_err;
6752 818c7501 2019-07-11 stsp done:
6753 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6754 a615e0e7 2020-12-16 stsp free(fileindex_path);
6755 818c7501 2019-07-11 stsp free(new_head_commit_id);
6756 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6757 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6758 818c7501 2019-07-11 stsp err = unlockerr;
6759 818c7501 2019-07-11 stsp return err;
6760 818c7501 2019-07-11 stsp }
6761 818c7501 2019-07-11 stsp
6762 818c7501 2019-07-11 stsp const struct got_error *
6763 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6764 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6765 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6766 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6767 818c7501 2019-07-11 stsp {
6768 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6769 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6770 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6771 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6772 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6773 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6774 818c7501 2019-07-11 stsp
6775 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6776 818c7501 2019-07-11 stsp if (err)
6777 818c7501 2019-07-11 stsp return err;
6778 818c7501 2019-07-11 stsp
6779 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6780 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6781 818c7501 2019-07-11 stsp if (err)
6782 818c7501 2019-07-11 stsp goto done;
6783 818c7501 2019-07-11 stsp
6784 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6785 818c7501 2019-07-11 stsp if (err)
6786 818c7501 2019-07-11 stsp goto done;
6787 818c7501 2019-07-11 stsp
6788 818c7501 2019-07-11 stsp /*
6789 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6790 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6791 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6792 818c7501 2019-07-11 stsp */
6793 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6794 818c7501 2019-07-11 stsp if (err)
6795 818c7501 2019-07-11 stsp goto done;
6796 818c7501 2019-07-11 stsp
6797 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6798 818c7501 2019-07-11 stsp if (err)
6799 818c7501 2019-07-11 stsp goto done;
6800 818c7501 2019-07-11 stsp
6801 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6802 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6803 ca355955 2019-07-12 stsp if (err)
6804 ca355955 2019-07-12 stsp goto done;
6805 ca355955 2019-07-12 stsp
6806 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6807 ca355955 2019-07-12 stsp if (err)
6808 ca355955 2019-07-12 stsp goto done;
6809 ca355955 2019-07-12 stsp
6810 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6811 818c7501 2019-07-11 stsp if (err)
6812 818c7501 2019-07-11 stsp goto done;
6813 818c7501 2019-07-11 stsp
6814 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6815 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6816 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6817 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6818 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6819 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6820 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6821 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
6822 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6823 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6824 55bd499d 2019-07-12 stsp if (err)
6825 1f1abb7e 2019-08-08 stsp goto sync;
6826 55bd499d 2019-07-12 stsp
6827 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6828 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6829 ca355955 2019-07-12 stsp sync:
6830 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6831 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6832 ca355955 2019-07-12 stsp err = sync_err;
6833 818c7501 2019-07-11 stsp done:
6834 818c7501 2019-07-11 stsp got_ref_close(resolved);
6835 a3a2faf2 2019-07-12 stsp free(tree_id);
6836 818c7501 2019-07-11 stsp free(commit_id);
6837 0ebf8283 2019-07-24 stsp if (fileindex)
6838 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6839 0ebf8283 2019-07-24 stsp free(fileindex_path);
6840 0ebf8283 2019-07-24 stsp
6841 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6842 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6843 0ebf8283 2019-07-24 stsp err = unlockerr;
6844 0ebf8283 2019-07-24 stsp return err;
6845 0ebf8283 2019-07-24 stsp }
6846 0ebf8283 2019-07-24 stsp
6847 0ebf8283 2019-07-24 stsp const struct got_error *
6848 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6849 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6850 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6851 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6852 0ebf8283 2019-07-24 stsp {
6853 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6854 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6855 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6856 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6857 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6858 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6859 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6860 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6861 0ebf8283 2019-07-24 stsp
6862 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6863 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6864 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6865 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6866 0ebf8283 2019-07-24 stsp
6867 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6868 0ebf8283 2019-07-24 stsp if (err)
6869 0ebf8283 2019-07-24 stsp return err;
6870 0ebf8283 2019-07-24 stsp
6871 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6872 0ebf8283 2019-07-24 stsp if (err)
6873 0ebf8283 2019-07-24 stsp goto done;
6874 0ebf8283 2019-07-24 stsp
6875 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6876 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6877 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6878 0ebf8283 2019-07-24 stsp &ok_arg);
6879 0ebf8283 2019-07-24 stsp if (err)
6880 0ebf8283 2019-07-24 stsp goto done;
6881 0ebf8283 2019-07-24 stsp
6882 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6883 0ebf8283 2019-07-24 stsp if (err)
6884 0ebf8283 2019-07-24 stsp goto done;
6885 0ebf8283 2019-07-24 stsp
6886 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6887 0ebf8283 2019-07-24 stsp if (err)
6888 0ebf8283 2019-07-24 stsp goto done;
6889 0ebf8283 2019-07-24 stsp
6890 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6891 0ebf8283 2019-07-24 stsp worktree);
6892 0ebf8283 2019-07-24 stsp if (err)
6893 0ebf8283 2019-07-24 stsp goto done;
6894 0ebf8283 2019-07-24 stsp
6895 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6896 0ebf8283 2019-07-24 stsp 0);
6897 0ebf8283 2019-07-24 stsp if (err)
6898 0ebf8283 2019-07-24 stsp goto done;
6899 0ebf8283 2019-07-24 stsp
6900 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6901 0ebf8283 2019-07-24 stsp if (err)
6902 0ebf8283 2019-07-24 stsp goto done;
6903 0ebf8283 2019-07-24 stsp
6904 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6905 0ebf8283 2019-07-24 stsp if (err)
6906 0ebf8283 2019-07-24 stsp goto done;
6907 0ebf8283 2019-07-24 stsp
6908 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6909 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6910 0ebf8283 2019-07-24 stsp if (err)
6911 0ebf8283 2019-07-24 stsp goto done;
6912 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6913 0ebf8283 2019-07-24 stsp if (err)
6914 0ebf8283 2019-07-24 stsp goto done;
6915 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6916 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6917 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6918 0ebf8283 2019-07-24 stsp goto done;
6919 0ebf8283 2019-07-24 stsp }
6920 0ebf8283 2019-07-24 stsp
6921 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6922 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6923 0ebf8283 2019-07-24 stsp if (err)
6924 0ebf8283 2019-07-24 stsp goto done;
6925 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6926 0ebf8283 2019-07-24 stsp if (err)
6927 0ebf8283 2019-07-24 stsp goto done;
6928 0ebf8283 2019-07-24 stsp
6929 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6930 0ebf8283 2019-07-24 stsp if (err)
6931 0ebf8283 2019-07-24 stsp goto done;
6932 0ebf8283 2019-07-24 stsp done:
6933 0ebf8283 2019-07-24 stsp free(fileindex_path);
6934 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6935 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6936 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6937 0ebf8283 2019-07-24 stsp if (wt_branch)
6938 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6939 0ebf8283 2019-07-24 stsp if (err) {
6940 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6941 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6942 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6943 0ebf8283 2019-07-24 stsp }
6944 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6945 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6946 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6947 0ebf8283 2019-07-24 stsp }
6948 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6949 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6950 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6951 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6952 3e3a69f1 2019-07-25 stsp }
6953 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6954 0ebf8283 2019-07-24 stsp }
6955 0ebf8283 2019-07-24 stsp return err;
6956 0ebf8283 2019-07-24 stsp }
6957 0ebf8283 2019-07-24 stsp
6958 0ebf8283 2019-07-24 stsp const struct got_error *
6959 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6960 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6961 0ebf8283 2019-07-24 stsp {
6962 3e3a69f1 2019-07-25 stsp if (fileindex)
6963 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6964 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6965 0ebf8283 2019-07-24 stsp }
6966 0ebf8283 2019-07-24 stsp
6967 0ebf8283 2019-07-24 stsp const struct got_error *
6968 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6969 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6970 0ebf8283 2019-07-24 stsp {
6971 0ebf8283 2019-07-24 stsp const struct got_error *err;
6972 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6973 0ebf8283 2019-07-24 stsp
6974 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6975 0ebf8283 2019-07-24 stsp if (err)
6976 0ebf8283 2019-07-24 stsp return err;
6977 0ebf8283 2019-07-24 stsp
6978 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6979 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6980 0ebf8283 2019-07-24 stsp return NULL;
6981 0ebf8283 2019-07-24 stsp }
6982 0ebf8283 2019-07-24 stsp
6983 0ebf8283 2019-07-24 stsp const struct got_error *
6984 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6985 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6986 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6987 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6988 0ebf8283 2019-07-24 stsp {
6989 0ebf8283 2019-07-24 stsp const struct got_error *err;
6990 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6991 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6992 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6993 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6994 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6995 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6996 0ebf8283 2019-07-24 stsp
6997 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6998 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6999 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7000 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7001 0ebf8283 2019-07-24 stsp
7002 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7003 3e3a69f1 2019-07-25 stsp if (err)
7004 3e3a69f1 2019-07-25 stsp return err;
7005 3e3a69f1 2019-07-25 stsp
7006 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7007 3e3a69f1 2019-07-25 stsp if (err)
7008 f032f1f7 2019-08-04 stsp goto done;
7009 f032f1f7 2019-08-04 stsp
7010 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7011 f032f1f7 2019-08-04 stsp &have_staged_files);
7012 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7013 f032f1f7 2019-08-04 stsp goto done;
7014 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7015 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7016 3e3a69f1 2019-07-25 stsp goto done;
7017 f032f1f7 2019-08-04 stsp }
7018 3e3a69f1 2019-07-25 stsp
7019 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7020 0ebf8283 2019-07-24 stsp if (err)
7021 f032f1f7 2019-08-04 stsp goto done;
7022 0ebf8283 2019-07-24 stsp
7023 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7024 0ebf8283 2019-07-24 stsp if (err)
7025 0ebf8283 2019-07-24 stsp goto done;
7026 0ebf8283 2019-07-24 stsp
7027 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7028 0ebf8283 2019-07-24 stsp if (err)
7029 0ebf8283 2019-07-24 stsp goto done;
7030 0ebf8283 2019-07-24 stsp
7031 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7032 0ebf8283 2019-07-24 stsp worktree);
7033 0ebf8283 2019-07-24 stsp if (err)
7034 0ebf8283 2019-07-24 stsp goto done;
7035 0ebf8283 2019-07-24 stsp
7036 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7037 0ebf8283 2019-07-24 stsp if (err)
7038 0ebf8283 2019-07-24 stsp goto done;
7039 0ebf8283 2019-07-24 stsp
7040 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7041 0ebf8283 2019-07-24 stsp if (err)
7042 0ebf8283 2019-07-24 stsp goto done;
7043 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7044 0ebf8283 2019-07-24 stsp if (err)
7045 0ebf8283 2019-07-24 stsp goto done;
7046 0ebf8283 2019-07-24 stsp
7047 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7048 0ebf8283 2019-07-24 stsp if (err)
7049 0ebf8283 2019-07-24 stsp goto done;
7050 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7051 0ebf8283 2019-07-24 stsp if (err)
7052 0ebf8283 2019-07-24 stsp goto done;
7053 0ebf8283 2019-07-24 stsp
7054 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7055 0ebf8283 2019-07-24 stsp if (err)
7056 0ebf8283 2019-07-24 stsp goto done;
7057 0ebf8283 2019-07-24 stsp done:
7058 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7059 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7060 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7061 0ebf8283 2019-07-24 stsp if (commit_ref)
7062 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7063 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7064 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7065 0ebf8283 2019-07-24 stsp if (err) {
7066 0ebf8283 2019-07-24 stsp free(*commit_id);
7067 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7068 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7069 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7070 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7071 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7072 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7073 0ebf8283 2019-07-24 stsp }
7074 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7075 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7076 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7077 3e3a69f1 2019-07-25 stsp }
7078 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7079 0ebf8283 2019-07-24 stsp }
7080 0ebf8283 2019-07-24 stsp return err;
7081 0ebf8283 2019-07-24 stsp }
7082 0ebf8283 2019-07-24 stsp
7083 0ebf8283 2019-07-24 stsp static const struct got_error *
7084 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7085 0ebf8283 2019-07-24 stsp {
7086 0ebf8283 2019-07-24 stsp const struct got_error *err;
7087 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7088 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7089 0ebf8283 2019-07-24 stsp
7090 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7091 0ebf8283 2019-07-24 stsp if (err)
7092 0ebf8283 2019-07-24 stsp goto done;
7093 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7094 0ebf8283 2019-07-24 stsp if (err)
7095 0ebf8283 2019-07-24 stsp goto done;
7096 0ebf8283 2019-07-24 stsp
7097 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7098 0ebf8283 2019-07-24 stsp worktree);
7099 0ebf8283 2019-07-24 stsp if (err)
7100 0ebf8283 2019-07-24 stsp goto done;
7101 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7102 0ebf8283 2019-07-24 stsp if (err)
7103 0ebf8283 2019-07-24 stsp goto done;
7104 0ebf8283 2019-07-24 stsp
7105 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7106 0ebf8283 2019-07-24 stsp if (err)
7107 0ebf8283 2019-07-24 stsp goto done;
7108 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7109 0ebf8283 2019-07-24 stsp if (err)
7110 0ebf8283 2019-07-24 stsp goto done;
7111 0ebf8283 2019-07-24 stsp
7112 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7113 0ebf8283 2019-07-24 stsp if (err)
7114 0ebf8283 2019-07-24 stsp goto done;
7115 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7116 0ebf8283 2019-07-24 stsp if (err)
7117 0ebf8283 2019-07-24 stsp goto done;
7118 0ebf8283 2019-07-24 stsp done:
7119 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7120 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7121 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7122 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7123 0ebf8283 2019-07-24 stsp return err;
7124 0ebf8283 2019-07-24 stsp }
7125 0ebf8283 2019-07-24 stsp
7126 0ebf8283 2019-07-24 stsp const struct got_error *
7127 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7128 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7129 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7130 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7131 0ebf8283 2019-07-24 stsp {
7132 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7133 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7134 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7135 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7136 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7137 0ebf8283 2019-07-24 stsp
7138 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7139 0ebf8283 2019-07-24 stsp if (err)
7140 0ebf8283 2019-07-24 stsp return err;
7141 0ebf8283 2019-07-24 stsp
7142 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7143 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7144 0ebf8283 2019-07-24 stsp if (err)
7145 0ebf8283 2019-07-24 stsp goto done;
7146 0ebf8283 2019-07-24 stsp
7147 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7148 0ebf8283 2019-07-24 stsp if (err)
7149 0ebf8283 2019-07-24 stsp goto done;
7150 0ebf8283 2019-07-24 stsp
7151 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7152 0ebf8283 2019-07-24 stsp if (err)
7153 0ebf8283 2019-07-24 stsp goto done;
7154 0ebf8283 2019-07-24 stsp
7155 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7156 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7157 0ebf8283 2019-07-24 stsp if (err)
7158 0ebf8283 2019-07-24 stsp goto done;
7159 0ebf8283 2019-07-24 stsp
7160 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7161 0ebf8283 2019-07-24 stsp if (err)
7162 0ebf8283 2019-07-24 stsp goto done;
7163 0ebf8283 2019-07-24 stsp
7164 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7165 0ebf8283 2019-07-24 stsp if (err)
7166 0ebf8283 2019-07-24 stsp goto done;
7167 0ebf8283 2019-07-24 stsp
7168 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7169 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7170 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7171 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7172 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7173 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7174 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7175 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7176 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7177 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
7178 0ebf8283 2019-07-24 stsp if (err)
7179 1f1abb7e 2019-08-08 stsp goto sync;
7180 0ebf8283 2019-07-24 stsp
7181 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7182 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7183 0ebf8283 2019-07-24 stsp sync:
7184 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7185 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7186 0ebf8283 2019-07-24 stsp err = sync_err;
7187 0ebf8283 2019-07-24 stsp done:
7188 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7189 0ebf8283 2019-07-24 stsp free(tree_id);
7190 818c7501 2019-07-11 stsp free(fileindex_path);
7191 818c7501 2019-07-11 stsp
7192 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7193 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7194 818c7501 2019-07-11 stsp err = unlockerr;
7195 818c7501 2019-07-11 stsp return err;
7196 818c7501 2019-07-11 stsp }
7197 0ebf8283 2019-07-24 stsp
7198 0ebf8283 2019-07-24 stsp const struct got_error *
7199 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7200 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7201 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7202 0ebf8283 2019-07-24 stsp {
7203 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7204 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7205 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7206 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7207 0ebf8283 2019-07-24 stsp
7208 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7209 0ebf8283 2019-07-24 stsp if (err)
7210 0ebf8283 2019-07-24 stsp return err;
7211 0ebf8283 2019-07-24 stsp
7212 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7213 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7214 e600f124 2021-03-21 stsp if (err)
7215 e600f124 2021-03-21 stsp goto done;
7216 e600f124 2021-03-21 stsp
7217 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7218 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7219 0ebf8283 2019-07-24 stsp if (err)
7220 0ebf8283 2019-07-24 stsp goto done;
7221 0ebf8283 2019-07-24 stsp
7222 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7223 0ebf8283 2019-07-24 stsp if (err)
7224 0ebf8283 2019-07-24 stsp goto done;
7225 0ebf8283 2019-07-24 stsp
7226 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7227 0ebf8283 2019-07-24 stsp if (err)
7228 0ebf8283 2019-07-24 stsp goto done;
7229 0ebf8283 2019-07-24 stsp
7230 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7231 0ebf8283 2019-07-24 stsp if (err)
7232 0ebf8283 2019-07-24 stsp goto done;
7233 0ebf8283 2019-07-24 stsp
7234 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7235 a615e0e7 2020-12-16 stsp if (err)
7236 a615e0e7 2020-12-16 stsp goto done;
7237 a615e0e7 2020-12-16 stsp
7238 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7239 a615e0e7 2020-12-16 stsp if (err)
7240 a615e0e7 2020-12-16 stsp goto done;
7241 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7242 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7243 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7244 a615e0e7 2020-12-16 stsp err = sync_err;
7245 0ebf8283 2019-07-24 stsp done:
7246 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7247 a615e0e7 2020-12-16 stsp free(fileindex_path);
7248 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7249 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7250 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7251 0ebf8283 2019-07-24 stsp err = unlockerr;
7252 0ebf8283 2019-07-24 stsp return err;
7253 0ebf8283 2019-07-24 stsp }
7254 0ebf8283 2019-07-24 stsp
7255 0ebf8283 2019-07-24 stsp const struct got_error *
7256 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7257 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7258 0ebf8283 2019-07-24 stsp {
7259 0ebf8283 2019-07-24 stsp const struct got_error *err;
7260 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7261 0ebf8283 2019-07-24 stsp
7262 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7263 0ebf8283 2019-07-24 stsp if (err)
7264 0ebf8283 2019-07-24 stsp return err;
7265 0ebf8283 2019-07-24 stsp
7266 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7267 0ebf8283 2019-07-24 stsp if (err)
7268 0ebf8283 2019-07-24 stsp goto done;
7269 0ebf8283 2019-07-24 stsp
7270 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7271 0ebf8283 2019-07-24 stsp done:
7272 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7273 2822a352 2019-10-15 stsp return err;
7274 2822a352 2019-10-15 stsp }
7275 2822a352 2019-10-15 stsp
7276 2822a352 2019-10-15 stsp const struct got_error *
7277 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7278 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7279 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7280 2822a352 2019-10-15 stsp struct got_repository *repo)
7281 2822a352 2019-10-15 stsp {
7282 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7283 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7284 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7285 2822a352 2019-10-15 stsp
7286 2822a352 2019-10-15 stsp *fileindex = NULL;
7287 2822a352 2019-10-15 stsp *branch_ref = NULL;
7288 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7289 2822a352 2019-10-15 stsp
7290 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7291 2822a352 2019-10-15 stsp if (err)
7292 2822a352 2019-10-15 stsp return err;
7293 2822a352 2019-10-15 stsp
7294 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7295 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7296 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7297 2822a352 2019-10-15 stsp "update -b or different branch name required");
7298 2822a352 2019-10-15 stsp goto done;
7299 2822a352 2019-10-15 stsp }
7300 2822a352 2019-10-15 stsp
7301 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7302 2822a352 2019-10-15 stsp if (err)
7303 2822a352 2019-10-15 stsp goto done;
7304 2822a352 2019-10-15 stsp
7305 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7306 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7307 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7308 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7309 2822a352 2019-10-15 stsp &ok_arg);
7310 2822a352 2019-10-15 stsp if (err)
7311 2822a352 2019-10-15 stsp goto done;
7312 2822a352 2019-10-15 stsp
7313 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7314 2822a352 2019-10-15 stsp if (err)
7315 2822a352 2019-10-15 stsp goto done;
7316 2822a352 2019-10-15 stsp
7317 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7318 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7319 2822a352 2019-10-15 stsp done:
7320 2822a352 2019-10-15 stsp if (err) {
7321 2822a352 2019-10-15 stsp if (*branch_ref) {
7322 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7323 2822a352 2019-10-15 stsp *branch_ref = NULL;
7324 2822a352 2019-10-15 stsp }
7325 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7326 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7327 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7328 2822a352 2019-10-15 stsp }
7329 2822a352 2019-10-15 stsp if (*fileindex) {
7330 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7331 2822a352 2019-10-15 stsp *fileindex = NULL;
7332 2822a352 2019-10-15 stsp }
7333 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7334 2822a352 2019-10-15 stsp }
7335 0cb83759 2019-08-03 stsp return err;
7336 0cb83759 2019-08-03 stsp }
7337 0cb83759 2019-08-03 stsp
7338 2822a352 2019-10-15 stsp const struct got_error *
7339 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7340 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7341 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7342 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7343 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7344 2822a352 2019-10-15 stsp {
7345 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7346 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7347 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7348 2822a352 2019-10-15 stsp
7349 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7350 2822a352 2019-10-15 stsp if (err)
7351 2822a352 2019-10-15 stsp goto done;
7352 2822a352 2019-10-15 stsp
7353 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7354 2822a352 2019-10-15 stsp if (err)
7355 2822a352 2019-10-15 stsp goto done;
7356 2822a352 2019-10-15 stsp
7357 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7358 2822a352 2019-10-15 stsp worktree->path_prefix);
7359 2822a352 2019-10-15 stsp if (err)
7360 2822a352 2019-10-15 stsp goto done;
7361 2822a352 2019-10-15 stsp
7362 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7363 2822a352 2019-10-15 stsp if (err)
7364 2822a352 2019-10-15 stsp goto done;
7365 2822a352 2019-10-15 stsp
7366 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7367 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7368 2822a352 2019-10-15 stsp if (err)
7369 2822a352 2019-10-15 stsp goto sync;
7370 2822a352 2019-10-15 stsp
7371 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7372 2822a352 2019-10-15 stsp if (err)
7373 2822a352 2019-10-15 stsp goto sync;
7374 2822a352 2019-10-15 stsp
7375 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7376 6e210706 2021-01-22 stsp if (err)
7377 6e210706 2021-01-22 stsp goto sync;
7378 6e210706 2021-01-22 stsp
7379 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7380 2822a352 2019-10-15 stsp sync:
7381 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7382 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7383 2822a352 2019-10-15 stsp err = sync_err;
7384 2822a352 2019-10-15 stsp
7385 2822a352 2019-10-15 stsp done:
7386 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7387 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7388 2822a352 2019-10-15 stsp err = unlockerr;
7389 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7390 2822a352 2019-10-15 stsp
7391 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7392 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7393 2822a352 2019-10-15 stsp err = unlockerr;
7394 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7395 2822a352 2019-10-15 stsp
7396 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7397 2822a352 2019-10-15 stsp free(fileindex_path);
7398 2822a352 2019-10-15 stsp free(tree_id);
7399 2822a352 2019-10-15 stsp
7400 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7401 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7402 2822a352 2019-10-15 stsp err = unlockerr;
7403 2822a352 2019-10-15 stsp return err;
7404 2822a352 2019-10-15 stsp }
7405 2822a352 2019-10-15 stsp
7406 2822a352 2019-10-15 stsp const struct got_error *
7407 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7408 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7409 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7410 2822a352 2019-10-15 stsp {
7411 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7412 8b692cd0 2019-10-21 stsp
7413 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7414 8b692cd0 2019-10-21 stsp
7415 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7416 8b692cd0 2019-10-21 stsp
7417 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7418 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7419 8b692cd0 2019-10-21 stsp err = unlockerr;
7420 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7421 8b692cd0 2019-10-21 stsp
7422 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7423 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7424 8b692cd0 2019-10-21 stsp err = unlockerr;
7425 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7426 10604dce 2021-09-24 thomas
7427 10604dce 2021-09-24 thomas return err;
7428 10604dce 2021-09-24 thomas }
7429 10604dce 2021-09-24 thomas
7430 10604dce 2021-09-24 thomas const struct got_error *
7431 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7432 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7433 10604dce 2021-09-24 thomas {
7434 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7435 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7436 10604dce 2021-09-24 thomas
7437 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7438 10604dce 2021-09-24 thomas if (err)
7439 10604dce 2021-09-24 thomas goto done;
7440 8b692cd0 2019-10-21 stsp
7441 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7442 10604dce 2021-09-24 thomas
7443 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7444 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7445 10604dce 2021-09-24 thomas err = sync_err;
7446 10604dce 2021-09-24 thomas done:
7447 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7448 10604dce 2021-09-24 thomas free(fileindex_path);
7449 8b692cd0 2019-10-21 stsp return err;
7450 2822a352 2019-10-15 stsp }
7451 2822a352 2019-10-15 stsp
7452 10604dce 2021-09-24 thomas static const struct got_error *
7453 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7454 10604dce 2021-09-24 thomas {
7455 10604dce 2021-09-24 thomas const struct got_error *err;
7456 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7457 10604dce 2021-09-24 thomas
7458 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7459 10604dce 2021-09-24 thomas if (err)
7460 10604dce 2021-09-24 thomas goto done;
7461 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7462 10604dce 2021-09-24 thomas if (err)
7463 10604dce 2021-09-24 thomas goto done;
7464 10604dce 2021-09-24 thomas
7465 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7466 10604dce 2021-09-24 thomas if (err)
7467 10604dce 2021-09-24 thomas goto done;
7468 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7469 10604dce 2021-09-24 thomas if (err)
7470 10604dce 2021-09-24 thomas goto done;
7471 10604dce 2021-09-24 thomas
7472 10604dce 2021-09-24 thomas done:
7473 10604dce 2021-09-24 thomas free(branch_refname);
7474 10604dce 2021-09-24 thomas free(commit_refname);
7475 10604dce 2021-09-24 thomas return err;
7476 10604dce 2021-09-24 thomas }
7477 10604dce 2021-09-24 thomas
7478 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7479 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7480 10604dce 2021-09-24 thomas const char *branch_name;
7481 10604dce 2021-09-24 thomas };
7482 10604dce 2021-09-24 thomas
7483 10604dce 2021-09-24 thomas static const struct got_error *
7484 10604dce 2021-09-24 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7485 10604dce 2021-09-24 thomas void *arg)
7486 10604dce 2021-09-24 thomas {
7487 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7488 10604dce 2021-09-24 thomas
7489 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7490 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7491 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7492 10604dce 2021-09-24 thomas
7493 10604dce 2021-09-24 thomas return NULL;
7494 10604dce 2021-09-24 thomas }
7495 10604dce 2021-09-24 thomas
7496 10604dce 2021-09-24 thomas static const struct got_error *
7497 10604dce 2021-09-24 thomas merge_status_cb(void *arg, unsigned char status, unsigned char staged_status,
7498 10604dce 2021-09-24 thomas const char *path, struct got_object_id *blob_id,
7499 10604dce 2021-09-24 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7500 10604dce 2021-09-24 thomas int dirfd, const char *de_name)
7501 10604dce 2021-09-24 thomas {
7502 10604dce 2021-09-24 thomas return NULL;
7503 10604dce 2021-09-24 thomas }
7504 10604dce 2021-09-24 thomas
7505 10604dce 2021-09-24 thomas const struct got_error *
7506 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7507 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7508 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7509 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7510 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7511 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7512 10604dce 2021-09-24 thomas {
7513 10604dce 2021-09-24 thomas const struct got_error *err;
7514 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7515 10604dce 2021-09-24 thomas
7516 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7517 10604dce 2021-09-24 thomas if (err)
7518 10604dce 2021-09-24 thomas goto done;
7519 10604dce 2021-09-24 thomas
7520 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7521 10604dce 2021-09-24 thomas worktree);
7522 10604dce 2021-09-24 thomas if (err)
7523 10604dce 2021-09-24 thomas goto done;
7524 10604dce 2021-09-24 thomas
7525 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7526 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7527 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7528 10604dce 2021-09-24 thomas done:
7529 10604dce 2021-09-24 thomas free(fileindex_path);
7530 10604dce 2021-09-24 thomas return err;
7531 10604dce 2021-09-24 thomas }
7532 10604dce 2021-09-24 thomas
7533 10604dce 2021-09-24 thomas const struct got_error *
7534 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7535 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7536 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7537 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7538 10604dce 2021-09-24 thomas struct got_repository *repo)
7539 10604dce 2021-09-24 thomas {
7540 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7541 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7542 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7543 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7544 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7545 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7546 10604dce 2021-09-24 thomas int have_staged_files = 0;
7547 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7548 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7549 10604dce 2021-09-24 thomas
7550 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7551 10604dce 2021-09-24 thomas
7552 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7553 10604dce 2021-09-24 thomas
7554 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7555 10604dce 2021-09-24 thomas if (err)
7556 10604dce 2021-09-24 thomas goto done;
7557 10604dce 2021-09-24 thomas
7558 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7559 10604dce 2021-09-24 thomas if (err)
7560 10604dce 2021-09-24 thomas goto done;
7561 10604dce 2021-09-24 thomas
7562 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7563 10604dce 2021-09-24 thomas if (err)
7564 10604dce 2021-09-24 thomas goto done;
7565 10604dce 2021-09-24 thomas
7566 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7567 10604dce 2021-09-24 thomas &have_staged_files);
7568 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7569 10604dce 2021-09-24 thomas goto done;
7570 10604dce 2021-09-24 thomas if (have_staged_files) {
7571 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7572 10604dce 2021-09-24 thomas goto done;
7573 10604dce 2021-09-24 thomas }
7574 10604dce 2021-09-24 thomas
7575 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7576 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7577 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7578 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7579 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7580 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7581 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7582 10604dce 2021-09-24 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
7583 10604dce 2021-09-24 thomas if (err)
7584 10604dce 2021-09-24 thomas goto done;
7585 10604dce 2021-09-24 thomas
7586 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7587 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7588 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7589 10604dce 2021-09-24 thomas goto done;
7590 10604dce 2021-09-24 thomas }
7591 10604dce 2021-09-24 thomas
7592 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7593 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7594 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7595 10604dce 2021-09-24 thomas
7596 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7597 10604dce 2021-09-24 thomas ct_path++;
7598 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7599 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7600 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7601 10604dce 2021-09-24 thomas if (err)
7602 10604dce 2021-09-24 thomas goto done;
7603 10604dce 2021-09-24 thomas
7604 10604dce 2021-09-24 thomas }
7605 10604dce 2021-09-24 thomas
7606 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7607 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7608 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7609 10604dce 2021-09-24 thomas head_commit_id, branch_tip, worktree, author, committer,
7610 10604dce 2021-09-24 thomas merge_commit_msg_cb, &mcm_arg, merge_status_cb, NULL, repo);
7611 10604dce 2021-09-24 thomas if (err)
7612 10604dce 2021-09-24 thomas goto done;
7613 10604dce 2021-09-24 thomas
7614 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7615 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7616 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7617 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7618 10604dce 2021-09-24 thomas err = sync_err;
7619 10604dce 2021-09-24 thomas done:
7620 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7621 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7622 10604dce 2021-09-24 thomas free_commitable(ct);
7623 10604dce 2021-09-24 thomas }
7624 10604dce 2021-09-24 thomas got_pathlist_free(&commitable_paths);
7625 10604dce 2021-09-24 thomas free(fileindex_path);
7626 10604dce 2021-09-24 thomas return err;
7627 10604dce 2021-09-24 thomas }
7628 10604dce 2021-09-24 thomas
7629 10604dce 2021-09-24 thomas const struct got_error *
7630 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
7631 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
7632 10604dce 2021-09-24 thomas {
7633 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7634 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7635 10604dce 2021-09-24 thomas
7636 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7637 10604dce 2021-09-24 thomas if (err)
7638 10604dce 2021-09-24 thomas goto done;
7639 10604dce 2021-09-24 thomas
7640 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7641 10604dce 2021-09-24 thomas if (err)
7642 10604dce 2021-09-24 thomas goto done;
7643 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7644 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7645 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7646 10604dce 2021-09-24 thomas err = sync_err;
7647 10604dce 2021-09-24 thomas done:
7648 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7649 10604dce 2021-09-24 thomas free(fileindex_path);
7650 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7651 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7652 10604dce 2021-09-24 thomas err = unlockerr;
7653 10604dce 2021-09-24 thomas return err;
7654 10604dce 2021-09-24 thomas }
7655 10604dce 2021-09-24 thomas
7656 10604dce 2021-09-24 thomas const struct got_error *
7657 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7658 10604dce 2021-09-24 thomas struct got_repository *repo)
7659 10604dce 2021-09-24 thomas {
7660 10604dce 2021-09-24 thomas const struct got_error *err;
7661 10604dce 2021-09-24 thomas char *branch_refname = NULL;
7662 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
7663 10604dce 2021-09-24 thomas
7664 10604dce 2021-09-24 thomas *in_progress = 0;
7665 10604dce 2021-09-24 thomas
7666 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7667 10604dce 2021-09-24 thomas if (err)
7668 10604dce 2021-09-24 thomas return err;
7669 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7670 dfe86d1f 2021-09-24 thomas free(branch_refname);
7671 10604dce 2021-09-24 thomas if (err) {
7672 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
7673 10604dce 2021-09-24 thomas return err;
7674 10604dce 2021-09-24 thomas } else
7675 10604dce 2021-09-24 thomas *in_progress = 1;
7676 10604dce 2021-09-24 thomas
7677 10604dce 2021-09-24 thomas return NULL;
7678 10604dce 2021-09-24 thomas }
7679 10604dce 2021-09-24 thomas
7680 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
7681 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
7682 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
7683 10604dce 2021-09-24 thomas {
7684 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
7685 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7686 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7687 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7688 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
7689 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7690 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
7691 10604dce 2021-09-24 thomas
7692 10604dce 2021-09-24 thomas *fileindex = NULL;
7693 10604dce 2021-09-24 thomas
7694 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7695 10604dce 2021-09-24 thomas if (err)
7696 10604dce 2021-09-24 thomas return err;
7697 10604dce 2021-09-24 thomas
7698 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7699 10604dce 2021-09-24 thomas if (err)
7700 10604dce 2021-09-24 thomas goto done;
7701 10604dce 2021-09-24 thomas
7702 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
7703 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
7704 10604dce 2021-09-24 thomas ok_arg.repo = repo;
7705 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7706 10604dce 2021-09-24 thomas &ok_arg);
7707 10604dce 2021-09-24 thomas if (err)
7708 10604dce 2021-09-24 thomas goto done;
7709 10604dce 2021-09-24 thomas
7710 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7711 10604dce 2021-09-24 thomas if (err)
7712 10604dce 2021-09-24 thomas return err;
7713 10604dce 2021-09-24 thomas
7714 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7715 10604dce 2021-09-24 thomas if (err)
7716 10604dce 2021-09-24 thomas return err;
7717 10604dce 2021-09-24 thomas
7718 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7719 10604dce 2021-09-24 thomas 0);
7720 10604dce 2021-09-24 thomas if (err)
7721 10604dce 2021-09-24 thomas goto done;
7722 10604dce 2021-09-24 thomas
7723 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7724 10604dce 2021-09-24 thomas if (err)
7725 10604dce 2021-09-24 thomas goto done;
7726 10604dce 2021-09-24 thomas
7727 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7728 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7729 10604dce 2021-09-24 thomas goto done;
7730 10604dce 2021-09-24 thomas }
7731 10604dce 2021-09-24 thomas
7732 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
7733 10604dce 2021-09-24 thomas if (err)
7734 10604dce 2021-09-24 thomas goto done;
7735 10604dce 2021-09-24 thomas
7736 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7737 10604dce 2021-09-24 thomas if (err)
7738 10604dce 2021-09-24 thomas goto done;
7739 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
7740 10604dce 2021-09-24 thomas if (err)
7741 10604dce 2021-09-24 thomas goto done;
7742 10604dce 2021-09-24 thomas
7743 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7744 10604dce 2021-09-24 thomas if (err)
7745 10604dce 2021-09-24 thomas goto done;
7746 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
7747 10604dce 2021-09-24 thomas if (err)
7748 10604dce 2021-09-24 thomas goto done;
7749 10604dce 2021-09-24 thomas
7750 10604dce 2021-09-24 thomas done:
7751 10604dce 2021-09-24 thomas free(branch_refname);
7752 10604dce 2021-09-24 thomas free(commit_refname);
7753 10604dce 2021-09-24 thomas free(fileindex_path);
7754 10604dce 2021-09-24 thomas if (branch_ref)
7755 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7756 10604dce 2021-09-24 thomas if (commit_ref)
7757 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7758 10604dce 2021-09-24 thomas if (wt_branch)
7759 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
7760 10604dce 2021-09-24 thomas free(wt_branch_tip);
7761 10604dce 2021-09-24 thomas if (err) {
7762 10604dce 2021-09-24 thomas if (*fileindex) {
7763 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7764 10604dce 2021-09-24 thomas *fileindex = NULL;
7765 10604dce 2021-09-24 thomas }
7766 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7767 10604dce 2021-09-24 thomas }
7768 10604dce 2021-09-24 thomas return err;
7769 10604dce 2021-09-24 thomas }
7770 10604dce 2021-09-24 thomas
7771 10604dce 2021-09-24 thomas const struct got_error *
7772 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
7773 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7774 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
7775 10604dce 2021-09-24 thomas {
7776 10604dce 2021-09-24 thomas const struct got_error *err;
7777 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
7778 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7779 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7780 10604dce 2021-09-24 thomas int have_staged_files = 0;
7781 10604dce 2021-09-24 thomas
7782 10604dce 2021-09-24 thomas *branch_name = NULL;
7783 10604dce 2021-09-24 thomas *branch_tip = NULL;
7784 10604dce 2021-09-24 thomas *fileindex = NULL;
7785 10604dce 2021-09-24 thomas
7786 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7787 10604dce 2021-09-24 thomas if (err)
7788 10604dce 2021-09-24 thomas return err;
7789 10604dce 2021-09-24 thomas
7790 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7791 10604dce 2021-09-24 thomas if (err)
7792 10604dce 2021-09-24 thomas goto done;
7793 10604dce 2021-09-24 thomas
7794 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7795 10604dce 2021-09-24 thomas &have_staged_files);
7796 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7797 10604dce 2021-09-24 thomas goto done;
7798 10604dce 2021-09-24 thomas if (have_staged_files) {
7799 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
7800 10604dce 2021-09-24 thomas goto done;
7801 10604dce 2021-09-24 thomas }
7802 10604dce 2021-09-24 thomas
7803 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7804 10604dce 2021-09-24 thomas if (err)
7805 10604dce 2021-09-24 thomas goto done;
7806 10604dce 2021-09-24 thomas
7807 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7808 10604dce 2021-09-24 thomas if (err)
7809 10604dce 2021-09-24 thomas goto done;
7810 10604dce 2021-09-24 thomas
7811 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7812 10604dce 2021-09-24 thomas if (err)
7813 10604dce 2021-09-24 thomas goto done;
7814 10604dce 2021-09-24 thomas
7815 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
7816 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7817 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
7818 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
7819 10604dce 2021-09-24 thomas goto done;
7820 10604dce 2021-09-24 thomas }
7821 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7822 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
7823 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
7824 10604dce 2021-09-24 thomas goto done;
7825 10604dce 2021-09-24 thomas }
7826 10604dce 2021-09-24 thomas
7827 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7828 10604dce 2021-09-24 thomas if (err)
7829 10604dce 2021-09-24 thomas goto done;
7830 10604dce 2021-09-24 thomas
7831 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
7832 10604dce 2021-09-24 thomas if (err)
7833 10604dce 2021-09-24 thomas goto done;
7834 10604dce 2021-09-24 thomas done:
7835 10604dce 2021-09-24 thomas free(commit_refname);
7836 10604dce 2021-09-24 thomas free(branch_refname);
7837 10604dce 2021-09-24 thomas free(fileindex_path);
7838 10604dce 2021-09-24 thomas if (commit_ref)
7839 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7840 10604dce 2021-09-24 thomas if (branch_ref)
7841 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7842 10604dce 2021-09-24 thomas if (err) {
7843 10604dce 2021-09-24 thomas if (*branch_name) {
7844 10604dce 2021-09-24 thomas free(*branch_name);
7845 10604dce 2021-09-24 thomas *branch_name = NULL;
7846 10604dce 2021-09-24 thomas }
7847 10604dce 2021-09-24 thomas free(*branch_tip);
7848 10604dce 2021-09-24 thomas *branch_tip = NULL;
7849 10604dce 2021-09-24 thomas if (*fileindex) {
7850 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7851 10604dce 2021-09-24 thomas *fileindex = NULL;
7852 10604dce 2021-09-24 thomas }
7853 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7854 10604dce 2021-09-24 thomas }
7855 10604dce 2021-09-24 thomas return err;
7856 10604dce 2021-09-24 thomas }
7857 10604dce 2021-09-24 thomas
7858 10604dce 2021-09-24 thomas const struct got_error *
7859 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
7860 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
7861 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
7862 10604dce 2021-09-24 thomas {
7863 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7864 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
7865 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7866 10604dce 2021-09-24 thomas struct revert_file_args rfa;
7867 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
7868 10604dce 2021-09-24 thomas
7869 10604dce 2021-09-24 thomas err = got_object_id_by_path(&tree_id, repo,
7870 10604dce 2021-09-24 thomas worktree->base_commit_id, worktree->path_prefix);
7871 10604dce 2021-09-24 thomas if (err)
7872 10604dce 2021-09-24 thomas goto done;
7873 10604dce 2021-09-24 thomas
7874 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7875 10604dce 2021-09-24 thomas if (err)
7876 10604dce 2021-09-24 thomas goto done;
7877 10604dce 2021-09-24 thomas
7878 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7879 10604dce 2021-09-24 thomas if (err)
7880 10604dce 2021-09-24 thomas goto done;
7881 10604dce 2021-09-24 thomas
7882 10604dce 2021-09-24 thomas rfa.worktree = worktree;
7883 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
7884 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
7885 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
7886 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
7887 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
7888 10604dce 2021-09-24 thomas rfa.repo = repo;
7889 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
7890 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7891 10604dce 2021-09-24 thomas revert_file, &rfa, NULL, NULL, 0, 0);
7892 10604dce 2021-09-24 thomas if (err)
7893 10604dce 2021-09-24 thomas goto sync;
7894 10604dce 2021-09-24 thomas
7895 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7896 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
7897 10604dce 2021-09-24 thomas sync:
7898 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7899 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7900 10604dce 2021-09-24 thomas err = sync_err;
7901 10604dce 2021-09-24 thomas done:
7902 10604dce 2021-09-24 thomas free(tree_id);
7903 10604dce 2021-09-24 thomas free(commit_id);
7904 10604dce 2021-09-24 thomas if (fileindex)
7905 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7906 10604dce 2021-09-24 thomas free(fileindex_path);
7907 10604dce 2021-09-24 thomas
7908 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7909 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7910 10604dce 2021-09-24 thomas err = unlockerr;
7911 10604dce 2021-09-24 thomas return err;
7912 10604dce 2021-09-24 thomas }
7913 10604dce 2021-09-24 thomas
7914 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7915 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7916 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7917 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7918 2db2652d 2019-08-07 stsp struct got_repository *repo;
7919 2db2652d 2019-08-07 stsp int have_changes;
7920 2db2652d 2019-08-07 stsp };
7921 2db2652d 2019-08-07 stsp
7922 2db2652d 2019-08-07 stsp const struct got_error *
7923 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7924 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7925 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7926 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7927 735ef5ac 2019-08-03 stsp {
7928 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7929 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7930 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7931 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7932 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7933 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7934 8b13ce36 2019-08-08 stsp
7935 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7936 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7937 8b13ce36 2019-08-08 stsp return NULL;
7938 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7939 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7940 735ef5ac 2019-08-03 stsp
7941 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7942 735ef5ac 2019-08-03 stsp if (ie == NULL)
7943 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7944 735ef5ac 2019-08-03 stsp
7945 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7946 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7947 735ef5ac 2019-08-03 stsp relpath) == -1)
7948 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7949 735ef5ac 2019-08-03 stsp
7950 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7951 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7952 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7953 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7954 735ef5ac 2019-08-03 stsp }
7955 735ef5ac 2019-08-03 stsp
7956 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7957 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7958 3aa5969e 2019-08-06 stsp goto done;
7959 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7960 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7961 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7962 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7963 735ef5ac 2019-08-03 stsp goto done;
7964 3aa5969e 2019-08-06 stsp }
7965 735ef5ac 2019-08-03 stsp
7966 2db2652d 2019-08-07 stsp a->have_changes = 1;
7967 2db2652d 2019-08-07 stsp
7968 735ef5ac 2019-08-03 stsp p = in_repo_path;
7969 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7970 735ef5ac 2019-08-03 stsp p++;
7971 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7972 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7973 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7974 735ef5ac 2019-08-03 stsp done:
7975 735ef5ac 2019-08-03 stsp free(in_repo_path);
7976 dc424a06 2019-08-07 stsp return err;
7977 dc424a06 2019-08-07 stsp }
7978 dc424a06 2019-08-07 stsp
7979 2db2652d 2019-08-07 stsp struct stage_path_arg {
7980 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7981 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7982 2db2652d 2019-08-07 stsp struct got_repository *repo;
7983 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7984 2db2652d 2019-08-07 stsp void *status_arg;
7985 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7986 2db2652d 2019-08-07 stsp void *patch_arg;
7987 7b5dc508 2019-10-28 stsp int staged_something;
7988 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7989 2db2652d 2019-08-07 stsp };
7990 2db2652d 2019-08-07 stsp
7991 2db2652d 2019-08-07 stsp static const struct got_error *
7992 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7993 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7994 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7995 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7996 0cb83759 2019-08-03 stsp {
7997 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7998 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7999 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8000 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8001 0cb83759 2019-08-03 stsp uint32_t stage;
8002 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8003 0aeb8099 2020-07-23 stsp struct stat sb;
8004 8b13ce36 2019-08-08 stsp
8005 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8006 8b13ce36 2019-08-08 stsp return NULL;
8007 0cb83759 2019-08-03 stsp
8008 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8009 d3e7c587 2019-08-03 stsp if (ie == NULL)
8010 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8011 0cb83759 2019-08-03 stsp
8012 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8013 2db2652d 2019-08-07 stsp relpath)== -1)
8014 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8015 0cb83759 2019-08-03 stsp
8016 0cb83759 2019-08-03 stsp switch (status) {
8017 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8018 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8019 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8020 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8021 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8022 0aeb8099 2020-07-23 stsp break;
8023 0aeb8099 2020-07-23 stsp }
8024 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8025 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8026 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8027 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8028 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8029 dc424a06 2019-08-07 stsp if (err)
8030 dc424a06 2019-08-07 stsp break;
8031 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8032 dc424a06 2019-08-07 stsp break;
8033 dc424a06 2019-08-07 stsp } else {
8034 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8035 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8036 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8037 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8038 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8039 dc424a06 2019-08-07 stsp break;
8040 dc424a06 2019-08-07 stsp }
8041 dc424a06 2019-08-07 stsp }
8042 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8043 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8044 0cb83759 2019-08-03 stsp if (err)
8045 dc424a06 2019-08-07 stsp break;
8046 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8047 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8048 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8049 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8050 0cb83759 2019-08-03 stsp else
8051 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8052 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8053 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8054 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8055 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8056 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8057 35213c7c 2020-07-23 stsp ssize_t target_len;
8058 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8059 35213c7c 2020-07-23 stsp sizeof(target_path));
8060 35213c7c 2020-07-23 stsp if (target_len == -1) {
8061 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8062 35213c7c 2020-07-23 stsp ondisk_path);
8063 35213c7c 2020-07-23 stsp break;
8064 35213c7c 2020-07-23 stsp }
8065 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8066 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8067 35213c7c 2020-07-23 stsp a->worktree->root_path);
8068 35213c7c 2020-07-23 stsp if (err)
8069 35213c7c 2020-07-23 stsp break;
8070 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8071 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8072 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8073 35213c7c 2020-07-23 stsp break;
8074 35213c7c 2020-07-23 stsp }
8075 35213c7c 2020-07-23 stsp }
8076 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8077 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8078 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8079 35213c7c 2020-07-23 stsp else
8080 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8081 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8082 0aeb8099 2020-07-23 stsp } else {
8083 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8084 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8085 0aeb8099 2020-07-23 stsp }
8086 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8087 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8088 dc424a06 2019-08-07 stsp break;
8089 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8090 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8091 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8092 0cb83759 2019-08-03 stsp break;
8093 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8094 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8095 d3e7c587 2019-08-03 stsp break;
8096 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8097 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8098 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8099 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8100 dc424a06 2019-08-07 stsp if (err)
8101 dc424a06 2019-08-07 stsp break;
8102 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8103 88f33a19 2019-08-08 stsp break;
8104 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8105 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8106 dc424a06 2019-08-07 stsp break;
8107 88f33a19 2019-08-08 stsp }
8108 dc424a06 2019-08-07 stsp }
8109 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8110 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8111 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8112 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8113 dc424a06 2019-08-07 stsp break;
8114 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8115 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8116 12463d8b 2019-12-13 stsp de_name);
8117 0cb83759 2019-08-03 stsp break;
8118 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8119 d3e7c587 2019-08-03 stsp break;
8120 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8121 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8122 ebf48fd5 2019-08-03 stsp break;
8123 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8124 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8125 2a06fe5f 2019-08-24 stsp break;
8126 0cb83759 2019-08-03 stsp default:
8127 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8128 537ac44b 2019-08-03 stsp break;
8129 0cb83759 2019-08-03 stsp }
8130 dc424a06 2019-08-07 stsp
8131 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8132 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8133 dc424a06 2019-08-07 stsp free(path_content);
8134 2db2652d 2019-08-07 stsp free(ondisk_path);
8135 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8136 0ebf8283 2019-07-24 stsp return err;
8137 0ebf8283 2019-07-24 stsp }
8138 0cb83759 2019-08-03 stsp
8139 0cb83759 2019-08-03 stsp const struct got_error *
8140 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8141 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8142 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8143 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8144 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8145 0cb83759 2019-08-03 stsp {
8146 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8147 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8148 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8149 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8150 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8151 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8152 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8153 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8154 0cb83759 2019-08-03 stsp
8155 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8156 0cb83759 2019-08-03 stsp if (err)
8157 0cb83759 2019-08-03 stsp return err;
8158 0cb83759 2019-08-03 stsp
8159 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8160 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8161 735ef5ac 2019-08-03 stsp if (err)
8162 735ef5ac 2019-08-03 stsp goto done;
8163 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8164 735ef5ac 2019-08-03 stsp if (err)
8165 735ef5ac 2019-08-03 stsp goto done;
8166 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8167 0cb83759 2019-08-03 stsp if (err)
8168 0cb83759 2019-08-03 stsp goto done;
8169 0cb83759 2019-08-03 stsp
8170 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8171 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8172 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8173 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8174 2db2652d 2019-08-07 stsp oka.repo = repo;
8175 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8176 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8177 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8178 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
8179 735ef5ac 2019-08-03 stsp if (err)
8180 735ef5ac 2019-08-03 stsp goto done;
8181 735ef5ac 2019-08-03 stsp }
8182 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8183 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8184 2db2652d 2019-08-07 stsp goto done;
8185 2db2652d 2019-08-07 stsp }
8186 735ef5ac 2019-08-03 stsp
8187 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8188 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8189 2db2652d 2019-08-07 stsp spa.repo = repo;
8190 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8191 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8192 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8193 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8194 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8195 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8196 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8197 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8198 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
8199 42005733 2019-08-03 stsp if (err)
8200 2db2652d 2019-08-07 stsp goto done;
8201 7b5dc508 2019-10-28 stsp }
8202 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8203 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8204 7b5dc508 2019-10-28 stsp goto done;
8205 0cb83759 2019-08-03 stsp }
8206 0cb83759 2019-08-03 stsp
8207 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8208 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8209 0cb83759 2019-08-03 stsp err = sync_err;
8210 0cb83759 2019-08-03 stsp done:
8211 735ef5ac 2019-08-03 stsp if (head_ref)
8212 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8213 735ef5ac 2019-08-03 stsp free(head_commit_id);
8214 0cb83759 2019-08-03 stsp free(fileindex_path);
8215 0cb83759 2019-08-03 stsp if (fileindex)
8216 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8217 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8218 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8219 0cb83759 2019-08-03 stsp err = unlockerr;
8220 0cb83759 2019-08-03 stsp return err;
8221 0cb83759 2019-08-03 stsp }
8222 ad493afc 2019-08-03 stsp
8223 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8224 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8225 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8226 ad493afc 2019-08-03 stsp struct got_repository *repo;
8227 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8228 ad493afc 2019-08-03 stsp void *progress_arg;
8229 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8230 2e1f37b0 2019-08-08 stsp void *patch_arg;
8231 ad493afc 2019-08-03 stsp };
8232 2e1f37b0 2019-08-08 stsp
8233 2e1f37b0 2019-08-08 stsp static const struct got_error *
8234 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8235 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8236 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8237 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8238 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8239 2e1f37b0 2019-08-08 stsp {
8240 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8241 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8242 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8243 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8244 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8245 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8246 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8247 2e1f37b0 2019-08-08 stsp
8248 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8249 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8250 2e1f37b0 2019-08-08 stsp
8251 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8252 2e1f37b0 2019-08-08 stsp if (err)
8253 2e1f37b0 2019-08-08 stsp return err;
8254 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8255 2e1f37b0 2019-08-08 stsp if (err)
8256 2e1f37b0 2019-08-08 stsp goto done;
8257 2e1f37b0 2019-08-08 stsp
8258 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8259 2e1f37b0 2019-08-08 stsp if (err)
8260 2e1f37b0 2019-08-08 stsp goto done;
8261 2e1f37b0 2019-08-08 stsp
8262 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8263 2e1f37b0 2019-08-08 stsp if (err)
8264 2e1f37b0 2019-08-08 stsp goto done;
8265 2e1f37b0 2019-08-08 stsp
8266 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8267 2e1f37b0 2019-08-08 stsp if (err)
8268 2e1f37b0 2019-08-08 stsp goto done;
8269 2e1f37b0 2019-08-08 stsp
8270 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8271 2e1f37b0 2019-08-08 stsp if (err)
8272 2e1f37b0 2019-08-08 stsp goto done;
8273 ad493afc 2019-08-03 stsp
8274 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8275 2e1f37b0 2019-08-08 stsp if (err)
8276 2e1f37b0 2019-08-08 stsp goto done;
8277 2e1f37b0 2019-08-08 stsp
8278 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8279 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
8280 2e1f37b0 2019-08-08 stsp if (err)
8281 2e1f37b0 2019-08-08 stsp goto done;
8282 2e1f37b0 2019-08-08 stsp
8283 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8284 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8285 2e1f37b0 2019-08-08 stsp if (err)
8286 2e1f37b0 2019-08-08 stsp goto done;
8287 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8288 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8289 2e1f37b0 2019-08-08 stsp if (err)
8290 2e1f37b0 2019-08-08 stsp goto done;
8291 2e1f37b0 2019-08-08 stsp
8292 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8293 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8294 2e1f37b0 2019-08-08 stsp goto done;
8295 2e1f37b0 2019-08-08 stsp }
8296 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8297 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8298 2e1f37b0 2019-08-08 stsp goto done;
8299 2e1f37b0 2019-08-08 stsp }
8300 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8301 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8302 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
8303 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8304 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8305 fe621944 2020-11-10 stsp nchanges++;
8306 fe621944 2020-11-10 stsp }
8307 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8308 2e1f37b0 2019-08-08 stsp int choice;
8309 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8310 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8311 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8312 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8313 2e1f37b0 2019-08-08 stsp if (err)
8314 2e1f37b0 2019-08-08 stsp goto done;
8315 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8316 2e1f37b0 2019-08-08 stsp have_content = 1;
8317 19e4b907 2019-08-08 stsp else
8318 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8319 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8320 2e1f37b0 2019-08-08 stsp break;
8321 2e1f37b0 2019-08-08 stsp }
8322 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8323 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8324 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8325 2e1f37b0 2019-08-08 stsp done:
8326 2e1f37b0 2019-08-08 stsp free(label1);
8327 2e1f37b0 2019-08-08 stsp if (blob)
8328 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8329 2e1f37b0 2019-08-08 stsp if (staged_blob)
8330 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8331 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8332 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8333 fe621944 2020-11-10 stsp err = free_err;
8334 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8335 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8336 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8337 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8338 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8339 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8340 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8341 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8342 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8343 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8344 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8345 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8346 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8347 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8348 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8349 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8350 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8351 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8352 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8353 2e1f37b0 2019-08-08 stsp }
8354 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8355 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8356 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8357 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8358 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8359 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8360 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8361 2e1f37b0 2019-08-08 stsp }
8362 2e1f37b0 2019-08-08 stsp free(path1);
8363 2e1f37b0 2019-08-08 stsp free(path2);
8364 fda8017d 2020-07-23 stsp return err;
8365 fda8017d 2020-07-23 stsp }
8366 fda8017d 2020-07-23 stsp
8367 fda8017d 2020-07-23 stsp static const struct got_error *
8368 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8369 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8370 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8371 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8372 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8373 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8374 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8375 fda8017d 2020-07-23 stsp {
8376 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8377 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8378 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8379 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8380 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8381 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8382 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8383 fda8017d 2020-07-23 stsp struct stat sb;
8384 fda8017d 2020-07-23 stsp
8385 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8386 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8387 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8388 fda8017d 2020-07-23 stsp if (err)
8389 fda8017d 2020-07-23 stsp return err;
8390 fda8017d 2020-07-23 stsp
8391 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8392 fda8017d 2020-07-23 stsp return NULL;
8393 fda8017d 2020-07-23 stsp
8394 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8395 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8396 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8397 fda8017d 2020-07-23 stsp if (err)
8398 fda8017d 2020-07-23 stsp goto done;
8399 fda8017d 2020-07-23 stsp }
8400 fda8017d 2020-07-23 stsp
8401 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
8402 fda8017d 2020-07-23 stsp if (f == NULL) {
8403 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8404 fda8017d 2020-07-23 stsp path_unstaged_content);
8405 fda8017d 2020-07-23 stsp goto done;
8406 fda8017d 2020-07-23 stsp }
8407 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8408 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8409 fda8017d 2020-07-23 stsp goto done;
8410 fda8017d 2020-07-23 stsp }
8411 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8412 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8413 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8414 fda8017d 2020-07-23 stsp size_t r;
8415 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8416 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8417 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8418 fda8017d 2020-07-23 stsp goto done;
8419 fda8017d 2020-07-23 stsp }
8420 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8421 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8422 fda8017d 2020-07-23 stsp goto done;
8423 fda8017d 2020-07-23 stsp }
8424 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8425 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8426 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8427 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8428 fda8017d 2020-07-23 stsp progress_arg);
8429 fda8017d 2020-07-23 stsp } else {
8430 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8431 67a66647 2021-05-31 stsp
8432 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8433 67a66647 2021-05-31 stsp if (err)
8434 67a66647 2021-05-31 stsp return err;
8435 67a66647 2021-05-31 stsp
8436 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8437 67a66647 2021-05-31 stsp parent) == -1) {
8438 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8439 67a66647 2021-05-31 stsp base_path = NULL;
8440 67a66647 2021-05-31 stsp goto done;
8441 67a66647 2021-05-31 stsp }
8442 67a66647 2021-05-31 stsp
8443 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8444 67a66647 2021-05-31 stsp if (err)
8445 67a66647 2021-05-31 stsp goto done;
8446 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8447 67a66647 2021-05-31 stsp blob_base);
8448 67a66647 2021-05-31 stsp if (err)
8449 67a66647 2021-05-31 stsp goto done;
8450 67a66647 2021-05-31 stsp
8451 eec2f5a9 2021-06-03 stsp /*
8452 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8453 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8454 eec2f5a9 2021-06-03 stsp */
8455 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8456 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8457 eec2f5a9 2021-06-03 stsp ondisk_path);
8458 eec2f5a9 2021-06-03 stsp if (err)
8459 eec2f5a9 2021-06-03 stsp goto done;
8460 eec2f5a9 2021-06-03 stsp } else {
8461 eec2f5a9 2021-06-03 stsp int fd;
8462 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8463 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8464 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8465 eec2f5a9 2021-06-03 stsp goto done;
8466 eec2f5a9 2021-06-03 stsp }
8467 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8468 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8469 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8470 eec2f5a9 2021-06-03 stsp close(fd);
8471 eec2f5a9 2021-06-03 stsp goto done;
8472 eec2f5a9 2021-06-03 stsp }
8473 eec2f5a9 2021-06-03 stsp }
8474 eec2f5a9 2021-06-03 stsp
8475 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8476 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8477 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8478 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8479 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8480 fda8017d 2020-07-23 stsp }
8481 fda8017d 2020-07-23 stsp if (err)
8482 fda8017d 2020-07-23 stsp goto done;
8483 fda8017d 2020-07-23 stsp
8484 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8485 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8486 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8487 2ac8aa02 2020-11-09 stsp } else {
8488 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8489 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8490 2ac8aa02 2020-11-09 stsp }
8491 fda8017d 2020-07-23 stsp done:
8492 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8493 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8494 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8495 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8496 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8497 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8498 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8499 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8500 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8501 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8502 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8503 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8504 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8505 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8506 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8507 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8508 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8509 67a66647 2021-05-31 stsp free(blob_base_path);
8510 67a66647 2021-05-31 stsp free(parent);
8511 67a66647 2021-05-31 stsp free(base_path);
8512 2e1f37b0 2019-08-08 stsp return err;
8513 2e1f37b0 2019-08-08 stsp }
8514 2e1f37b0 2019-08-08 stsp
8515 ad493afc 2019-08-03 stsp static const struct got_error *
8516 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8517 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8518 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8519 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8520 ad493afc 2019-08-03 stsp {
8521 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8522 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8523 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8524 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8525 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8526 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8527 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8528 9bc94a15 2019-08-03 stsp struct stat sb;
8529 ad493afc 2019-08-03 stsp
8530 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8531 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8532 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8533 2e1f37b0 2019-08-08 stsp return NULL;
8534 2e1f37b0 2019-08-08 stsp
8535 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8536 ad493afc 2019-08-03 stsp if (ie == NULL)
8537 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8538 9bc94a15 2019-08-03 stsp
8539 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8540 9bc94a15 2019-08-03 stsp == -1)
8541 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8542 ad493afc 2019-08-03 stsp
8543 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8544 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8545 f69721c3 2019-10-21 stsp if (err)
8546 f69721c3 2019-10-21 stsp goto done;
8547 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8548 f69721c3 2019-10-21 stsp id_str) == -1) {
8549 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8550 f69721c3 2019-10-21 stsp goto done;
8551 f69721c3 2019-10-21 stsp }
8552 f69721c3 2019-10-21 stsp
8553 ad493afc 2019-08-03 stsp switch (staged_status) {
8554 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8555 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8556 ad493afc 2019-08-03 stsp blob_id, 8192);
8557 ad493afc 2019-08-03 stsp if (err)
8558 ad493afc 2019-08-03 stsp break;
8559 ad493afc 2019-08-03 stsp /* fall through */
8560 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8561 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8562 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8563 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8564 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8565 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8566 2e1f37b0 2019-08-08 stsp if (err)
8567 2e1f37b0 2019-08-08 stsp break;
8568 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8569 2e1f37b0 2019-08-08 stsp break;
8570 2e1f37b0 2019-08-08 stsp } else {
8571 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8572 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8573 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8574 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8575 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8576 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8577 2e1f37b0 2019-08-08 stsp }
8578 2e1f37b0 2019-08-08 stsp }
8579 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8580 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8581 ad493afc 2019-08-03 stsp if (err)
8582 ad493afc 2019-08-03 stsp break;
8583 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8584 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8585 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8586 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8587 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8588 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8589 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8590 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8591 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8592 ea7786be 2020-07-23 stsp break;
8593 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8594 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8595 36bf999c 2020-07-23 stsp char *staged_target;
8596 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8597 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8598 36bf999c 2020-07-23 stsp if (err)
8599 36bf999c 2020-07-23 stsp goto done;
8600 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8601 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8602 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8603 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8604 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8605 36bf999c 2020-07-23 stsp free(staged_target);
8606 dfe9fba0 2020-07-23 stsp } else {
8607 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8608 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8609 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8610 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8611 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8612 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8613 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8614 dfe9fba0 2020-07-23 stsp }
8615 ea7786be 2020-07-23 stsp break;
8616 ea7786be 2020-07-23 stsp default:
8617 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8618 ea7786be 2020-07-23 stsp break;
8619 ea7786be 2020-07-23 stsp }
8620 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8621 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8622 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8623 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8624 2ac8aa02 2020-11-09 stsp }
8625 ad493afc 2019-08-03 stsp break;
8626 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8627 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8628 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8629 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8630 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8631 2e1f37b0 2019-08-08 stsp if (err)
8632 2e1f37b0 2019-08-08 stsp break;
8633 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8634 2e1f37b0 2019-08-08 stsp break;
8635 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8636 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8637 2e1f37b0 2019-08-08 stsp break;
8638 2e1f37b0 2019-08-08 stsp }
8639 2e1f37b0 2019-08-08 stsp }
8640 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8641 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8642 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8643 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8644 9bc94a15 2019-08-03 stsp if (err)
8645 9bc94a15 2019-08-03 stsp break;
8646 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8647 ad493afc 2019-08-03 stsp break;
8648 ad493afc 2019-08-03 stsp }
8649 f69721c3 2019-10-21 stsp done:
8650 ad493afc 2019-08-03 stsp free(ondisk_path);
8651 ad493afc 2019-08-03 stsp if (blob_base)
8652 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8653 ad493afc 2019-08-03 stsp if (blob_staged)
8654 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8655 f69721c3 2019-10-21 stsp free(id_str);
8656 f69721c3 2019-10-21 stsp free(label_orig);
8657 ad493afc 2019-08-03 stsp return err;
8658 ad493afc 2019-08-03 stsp }
8659 ad493afc 2019-08-03 stsp
8660 ad493afc 2019-08-03 stsp const struct got_error *
8661 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8662 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8663 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8664 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8665 ad493afc 2019-08-03 stsp struct got_repository *repo)
8666 ad493afc 2019-08-03 stsp {
8667 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8668 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8669 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8670 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8671 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8672 ad493afc 2019-08-03 stsp
8673 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8674 ad493afc 2019-08-03 stsp if (err)
8675 ad493afc 2019-08-03 stsp return err;
8676 ad493afc 2019-08-03 stsp
8677 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8678 ad493afc 2019-08-03 stsp if (err)
8679 ad493afc 2019-08-03 stsp goto done;
8680 ad493afc 2019-08-03 stsp
8681 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8682 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8683 ad493afc 2019-08-03 stsp upa.repo = repo;
8684 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8685 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8686 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8687 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8688 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8689 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8690 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
8691 ad493afc 2019-08-03 stsp if (err)
8692 ad493afc 2019-08-03 stsp goto done;
8693 ad493afc 2019-08-03 stsp }
8694 ad493afc 2019-08-03 stsp
8695 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8696 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8697 ad493afc 2019-08-03 stsp err = sync_err;
8698 ad493afc 2019-08-03 stsp done:
8699 ad493afc 2019-08-03 stsp free(fileindex_path);
8700 ad493afc 2019-08-03 stsp if (fileindex)
8701 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8702 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8703 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8704 ad493afc 2019-08-03 stsp err = unlockerr;
8705 ad493afc 2019-08-03 stsp return err;
8706 ad493afc 2019-08-03 stsp }
8707 b2118c49 2020-07-28 stsp
8708 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8709 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8710 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8711 b2118c49 2020-07-28 stsp void *info_arg;
8712 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8713 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8714 b2118c49 2020-07-28 stsp void *cancel_arg;
8715 b2118c49 2020-07-28 stsp };
8716 b2118c49 2020-07-28 stsp
8717 b2118c49 2020-07-28 stsp static const struct got_error *
8718 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8719 b2118c49 2020-07-28 stsp {
8720 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8721 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8722 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8723 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8724 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8725 b2118c49 2020-07-28 stsp int stage;
8726 b2118c49 2020-07-28 stsp
8727 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8728 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8729 b2118c49 2020-07-28 stsp
8730 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8731 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8732 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8733 b2118c49 2020-07-28 stsp break;
8734 b2118c49 2020-07-28 stsp }
8735 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8736 b2118c49 2020-07-28 stsp return NULL;
8737 b2118c49 2020-07-28 stsp
8738 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8739 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8740 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8741 b2118c49 2020-07-28 stsp }
8742 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8743 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8744 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8745 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8746 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8747 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8748 b2118c49 2020-07-28 stsp }
8749 b2118c49 2020-07-28 stsp
8750 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8751 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8752 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8753 b2118c49 2020-07-28 stsp }
8754 b2118c49 2020-07-28 stsp
8755 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8756 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8757 b2118c49 2020-07-28 stsp }
8758 b2118c49 2020-07-28 stsp
8759 b2118c49 2020-07-28 stsp const struct got_error *
8760 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8761 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8762 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8763 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8764 b2118c49 2020-07-28 stsp
8765 b2118c49 2020-07-28 stsp {
8766 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8767 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8768 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8769 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8770 b2118c49 2020-07-28 stsp
8771 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8772 b2118c49 2020-07-28 stsp if (err)
8773 b2118c49 2020-07-28 stsp return err;
8774 b2118c49 2020-07-28 stsp
8775 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8776 b2118c49 2020-07-28 stsp if (err)
8777 b2118c49 2020-07-28 stsp goto done;
8778 b2118c49 2020-07-28 stsp
8779 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8780 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8781 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8782 b2118c49 2020-07-28 stsp arg.paths = paths;
8783 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8784 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8785 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8786 b2118c49 2020-07-28 stsp &arg);
8787 b2118c49 2020-07-28 stsp done:
8788 b2118c49 2020-07-28 stsp free(fileindex_path);
8789 b2118c49 2020-07-28 stsp if (fileindex)
8790 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8791 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8792 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8793 b2118c49 2020-07-28 stsp err = unlockerr;
8794 b2118c49 2020-07-28 stsp return err;
8795 b2118c49 2020-07-28 stsp }