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 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
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 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1374 ace90326 2021-09-27 thomas struct got_repository *repo,
1375 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1376 9d31a1d8 2018-03-11 stsp {
1377 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1378 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1379 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1380 906c123b 2020-07-23 stsp char *path_got = NULL;
1381 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1382 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1383 8ba819a3 2020-07-23 stsp
1384 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1385 2e1fa222 2020-07-23 stsp
1386 8ba819a3 2020-07-23 stsp /*
1387 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1388 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1389 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1390 8ba819a3 2020-07-23 stsp * in the blob object.
1391 8ba819a3 2020-07-23 stsp */
1392 8ba819a3 2020-07-23 stsp do {
1393 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1394 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1395 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1396 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1397 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1398 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1399 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1400 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1401 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1402 3b9f0f87 2020-07-23 stsp progress_arg);
1403 8ba819a3 2020-07-23 stsp }
1404 8ba819a3 2020-07-23 stsp if (len > 0) {
1405 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1406 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1407 8ba819a3 2020-07-23 stsp len - hdrlen);
1408 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1409 8ba819a3 2020-07-23 stsp hdrlen = 0;
1410 8ba819a3 2020-07-23 stsp }
1411 8ba819a3 2020-07-23 stsp } while (len != 0);
1412 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1413 8ba819a3 2020-07-23 stsp
1414 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1415 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1416 3c1ec782 2020-07-23 stsp if (err)
1417 3c1ec782 2020-07-23 stsp return err;
1418 8ba819a3 2020-07-23 stsp
1419 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1420 906c123b 2020-07-23 stsp /* install as a regular file */
1421 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1422 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1423 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1424 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1425 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1426 906c123b 2020-07-23 stsp goto done;
1427 906c123b 2020-07-23 stsp }
1428 906c123b 2020-07-23 stsp
1429 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1430 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1431 c6e8a826 2021-04-05 stsp int symlink_replaced;
1432 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1433 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1434 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1435 c90c8ce3 2020-07-23 stsp goto done;
1436 c90c8ce3 2020-07-23 stsp }
1437 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1438 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1439 5a1fbc73 2020-07-23 stsp if (err)
1440 f35fa46a 2020-07-23 stsp goto done;
1441 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1442 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1443 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1444 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1445 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1446 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1447 c6e8a826 2021-04-05 stsp } else {
1448 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1449 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1450 c6e8a826 2021-04-05 stsp }
1451 f35fa46a 2020-07-23 stsp }
1452 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1453 f35fa46a 2020-07-23 stsp }
1454 f35fa46a 2020-07-23 stsp
1455 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1456 f4994adc 2020-10-20 stsp char *parent;
1457 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1458 f4994adc 2020-10-20 stsp if (err)
1459 f4994adc 2020-10-20 stsp goto done;
1460 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1461 f4994adc 2020-10-20 stsp free(parent);
1462 f35fa46a 2020-07-23 stsp if (err)
1463 f35fa46a 2020-07-23 stsp goto done;
1464 f35fa46a 2020-07-23 stsp /*
1465 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1466 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1467 f35fa46a 2020-07-23 stsp */
1468 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1469 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1470 f35fa46a 2020-07-23 stsp goto done;
1471 f35fa46a 2020-07-23 stsp }
1472 f35fa46a 2020-07-23 stsp }
1473 f35fa46a 2020-07-23 stsp
1474 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1475 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1476 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1477 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1478 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1479 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1480 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1481 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1482 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1483 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1484 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1485 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1486 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1487 8ba819a3 2020-07-23 stsp } else {
1488 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1489 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1490 8ba819a3 2020-07-23 stsp }
1491 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1492 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1493 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1494 8ba819a3 2020-07-23 stsp done:
1495 906c123b 2020-07-23 stsp free(path_got);
1496 8ba819a3 2020-07-23 stsp return err;
1497 8ba819a3 2020-07-23 stsp }
1498 8ba819a3 2020-07-23 stsp
1499 8ba819a3 2020-07-23 stsp static const struct got_error *
1500 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1501 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1502 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1503 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1504 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1505 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1506 8ba819a3 2020-07-23 stsp {
1507 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1508 507dc3bb 2018-12-29 stsp int fd = -1;
1509 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1510 507dc3bb 2018-12-29 stsp int update = 0;
1511 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1512 8ba819a3 2020-07-23 stsp
1513 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1514 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1515 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1516 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1517 f5375317 2020-10-20 stsp char *parent;
1518 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1519 f5375317 2020-10-20 stsp if (err)
1520 f5375317 2020-10-20 stsp return err;
1521 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1522 f5375317 2020-10-20 stsp free(parent);
1523 21908da4 2019-01-13 stsp if (err)
1524 21908da4 2019-01-13 stsp return err;
1525 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1526 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1527 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1528 21908da4 2019-01-13 stsp if (fd == -1)
1529 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1530 230a42bd 2019-05-11 jcs ondisk_path);
1531 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1532 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1533 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1534 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1535 3b9f0f87 2020-07-23 stsp goto done;
1536 3b9f0f87 2020-07-23 stsp }
1537 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1538 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1539 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1540 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1541 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1542 507dc3bb 2018-12-29 stsp goto done;
1543 d70b8e30 2018-12-27 stsp } else {
1544 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1545 507dc3bb 2018-12-29 stsp ondisk_path);
1546 507dc3bb 2018-12-29 stsp if (err)
1547 507dc3bb 2018-12-29 stsp goto done;
1548 507dc3bb 2018-12-29 stsp update = 1;
1549 9d31a1d8 2018-03-11 stsp }
1550 507dc3bb 2018-12-29 stsp } else
1551 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1552 9d31a1d8 2018-03-11 stsp }
1553 9d31a1d8 2018-03-11 stsp
1554 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1555 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1556 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1557 3818e3c4 2020-11-01 naddy goto done;
1558 3818e3c4 2020-11-01 naddy }
1559 3818e3c4 2020-11-01 naddy
1560 bd6aa359 2020-07-23 stsp if (progress_cb) {
1561 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1562 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1563 bd6aa359 2020-07-23 stsp path);
1564 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1565 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1566 bd6aa359 2020-07-23 stsp path);
1567 bd6aa359 2020-07-23 stsp else
1568 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1569 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1570 bd6aa359 2020-07-23 stsp if (err)
1571 bd6aa359 2020-07-23 stsp goto done;
1572 bd6aa359 2020-07-23 stsp }
1573 d7b62c98 2018-12-27 stsp
1574 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1575 9d31a1d8 2018-03-11 stsp do {
1576 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1577 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1578 9d31a1d8 2018-03-11 stsp if (err)
1579 9d31a1d8 2018-03-11 stsp break;
1580 9d31a1d8 2018-03-11 stsp if (len > 0) {
1581 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1582 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1583 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1584 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1585 b87c6f83 2018-12-24 stsp goto done;
1586 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1587 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1588 b87c6f83 2018-12-24 stsp goto done;
1589 9d31a1d8 2018-03-11 stsp }
1590 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1591 9d31a1d8 2018-03-11 stsp }
1592 9d31a1d8 2018-03-11 stsp } while (len != 0);
1593 9d31a1d8 2018-03-11 stsp
1594 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1595 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1596 816dc654 2019-02-16 stsp goto done;
1597 816dc654 2019-02-16 stsp }
1598 9d31a1d8 2018-03-11 stsp
1599 507dc3bb 2018-12-29 stsp if (update) {
1600 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1601 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1602 f6d8c0ac 2020-11-09 stsp goto done;
1603 f6d8c0ac 2020-11-09 stsp }
1604 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1605 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1606 230a42bd 2019-05-11 jcs ondisk_path);
1607 68ed9ba5 2019-02-10 stsp goto done;
1608 68ed9ba5 2019-02-10 stsp }
1609 63df146d 2020-11-09 stsp free(tmppath);
1610 63df146d 2020-11-09 stsp tmppath = NULL;
1611 507dc3bb 2018-12-29 stsp }
1612 507dc3bb 2018-12-29 stsp
1613 9d31a1d8 2018-03-11 stsp done:
1614 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1615 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1616 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1617 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1618 507dc3bb 2018-12-29 stsp free(tmppath);
1619 6353ad76 2019-02-08 stsp return err;
1620 6353ad76 2019-02-08 stsp }
1621 6353ad76 2019-02-08 stsp
1622 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1623 6353ad76 2019-02-08 stsp static const struct got_error *
1624 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1625 7154f6ce 2019-03-27 stsp {
1626 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1627 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1628 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1629 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1630 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1631 7154f6ce 2019-03-27 stsp };
1632 7154f6ce 2019-03-27 stsp int i = 0;
1633 9bdd68dd 2020-01-02 naddy char *line = NULL;
1634 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1635 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1636 7154f6ce 2019-03-27 stsp
1637 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1638 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1639 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1640 7154f6ce 2019-03-27 stsp if (feof(f))
1641 7154f6ce 2019-03-27 stsp break;
1642 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1643 7154f6ce 2019-03-27 stsp break;
1644 7154f6ce 2019-03-27 stsp }
1645 7154f6ce 2019-03-27 stsp
1646 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1647 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1648 19332e6d 2019-05-13 stsp == 0)
1649 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1650 7154f6ce 2019-03-27 stsp else
1651 7154f6ce 2019-03-27 stsp i++;
1652 7154f6ce 2019-03-27 stsp }
1653 7154f6ce 2019-03-27 stsp }
1654 9bdd68dd 2020-01-02 naddy free(line);
1655 7154f6ce 2019-03-27 stsp
1656 7154f6ce 2019-03-27 stsp return err;
1657 e2b1e152 2019-07-13 stsp }
1658 e2b1e152 2019-07-13 stsp
1659 e2b1e152 2019-07-13 stsp static int
1660 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1661 1ebedb77 2019-10-19 stsp {
1662 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1663 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1664 1ebedb77 2019-10-19 stsp }
1665 1ebedb77 2019-10-19 stsp
1666 1ebedb77 2019-10-19 stsp static int
1667 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1668 e2b1e152 2019-07-13 stsp {
1669 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1670 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1671 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1672 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1673 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1674 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1675 c363b2c1 2019-08-03 stsp }
1676 c363b2c1 2019-08-03 stsp
1677 c363b2c1 2019-08-03 stsp static unsigned char
1678 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1679 c363b2c1 2019-08-03 stsp {
1680 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1681 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1682 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1683 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1684 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1685 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1686 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1687 c363b2c1 2019-08-03 stsp default:
1688 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1689 a919d5c4 2020-07-23 stsp }
1690 a919d5c4 2020-07-23 stsp }
1691 a919d5c4 2020-07-23 stsp
1692 a919d5c4 2020-07-23 stsp static const struct got_error *
1693 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1694 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1695 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1696 a919d5c4 2020-07-23 stsp {
1697 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1698 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1699 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1700 a919d5c4 2020-07-23 stsp ssize_t elen;
1701 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1702 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1703 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1704 a919d5c4 2020-07-23 stsp
1705 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1706 a919d5c4 2020-07-23 stsp
1707 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1708 a919d5c4 2020-07-23 stsp do {
1709 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1710 a919d5c4 2020-07-23 stsp if (err)
1711 a919d5c4 2020-07-23 stsp return err;
1712 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1713 a919d5c4 2020-07-23 stsp /*
1714 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1715 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1716 a919d5c4 2020-07-23 stsp */
1717 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1718 a919d5c4 2020-07-23 stsp }
1719 a919d5c4 2020-07-23 stsp if (len > 0) {
1720 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1721 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1722 a919d5c4 2020-07-23 stsp len - hdrlen);
1723 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1724 a919d5c4 2020-07-23 stsp hdrlen = 0;
1725 a919d5c4 2020-07-23 stsp }
1726 a919d5c4 2020-07-23 stsp } while (len != 0);
1727 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1728 a919d5c4 2020-07-23 stsp
1729 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1730 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1731 a919d5c4 2020-07-23 stsp if (elen == -1)
1732 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1733 a919d5c4 2020-07-23 stsp } else {
1734 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1735 a919d5c4 2020-07-23 stsp if (elen == -1)
1736 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1737 c363b2c1 2019-08-03 stsp }
1738 a919d5c4 2020-07-23 stsp
1739 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1740 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1741 a919d5c4 2020-07-23 stsp
1742 a919d5c4 2020-07-23 stsp return NULL;
1743 7154f6ce 2019-03-27 stsp }
1744 7154f6ce 2019-03-27 stsp
1745 7154f6ce 2019-03-27 stsp static const struct got_error *
1746 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1747 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1748 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1749 6353ad76 2019-02-08 stsp {
1750 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1751 6353ad76 2019-02-08 stsp struct got_object_id id;
1752 6353ad76 2019-02-08 stsp size_t hdrlen;
1753 1338848f 2019-12-13 stsp int fd = -1;
1754 6353ad76 2019-02-08 stsp FILE *f = NULL;
1755 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1756 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1757 6353ad76 2019-02-08 stsp size_t flen, blen;
1758 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1759 6353ad76 2019-02-08 stsp
1760 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1761 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1762 6353ad76 2019-02-08 stsp
1763 7f91a133 2019-12-13 stsp /*
1764 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1765 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1766 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1767 7f91a133 2019-12-13 stsp */
1768 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1769 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1770 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1771 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1772 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1773 882ef1b9 2019-12-13 stsp else
1774 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1775 882ef1b9 2019-12-13 stsp goto done;
1776 882ef1b9 2019-12-13 stsp }
1777 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1778 3d35a492 2019-12-13 stsp goto done;
1779 3d35a492 2019-12-13 stsp }
1780 7f91a133 2019-12-13 stsp } else {
1781 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1782 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1783 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1784 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1785 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1786 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1787 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1788 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1789 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1790 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1791 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1792 3d35a492 2019-12-13 stsp else
1793 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1794 3d35a492 2019-12-13 stsp goto done;
1795 3d35a492 2019-12-13 stsp }
1796 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1797 1338848f 2019-12-13 stsp goto done;
1798 a378724f 2019-02-10 stsp }
1799 a378724f 2019-02-10 stsp }
1800 6353ad76 2019-02-08 stsp
1801 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1802 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1803 1338848f 2019-12-13 stsp goto done;
1804 3f148bc6 2019-07-27 stsp }
1805 efdd40df 2019-07-27 stsp
1806 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1807 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1808 1338848f 2019-12-13 stsp goto done;
1809 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1810 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1811 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1812 1338848f 2019-12-13 stsp goto done;
1813 d00136be 2019-03-26 stsp }
1814 b8f41171 2019-02-10 stsp
1815 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1816 f179e66d 2020-07-23 stsp goto done;
1817 f179e66d 2020-07-23 stsp
1818 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1819 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1820 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1821 1338848f 2019-12-13 stsp goto done;
1822 f179e66d 2020-07-23 stsp }
1823 6353ad76 2019-02-08 stsp
1824 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1825 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1826 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1827 c363b2c1 2019-08-03 stsp else
1828 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1829 c363b2c1 2019-08-03 stsp
1830 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1831 6353ad76 2019-02-08 stsp if (err)
1832 1338848f 2019-12-13 stsp goto done;
1833 6353ad76 2019-02-08 stsp
1834 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1835 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1836 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1837 a919d5c4 2020-07-23 stsp goto done;
1838 a919d5c4 2020-07-23 stsp }
1839 a919d5c4 2020-07-23 stsp
1840 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1841 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1842 ab0d4361 2019-12-13 stsp if (fd == -1) {
1843 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1844 ab0d4361 2019-12-13 stsp goto done;
1845 ab0d4361 2019-12-13 stsp }
1846 3d35a492 2019-12-13 stsp }
1847 3d35a492 2019-12-13 stsp
1848 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1849 6353ad76 2019-02-08 stsp if (f == NULL) {
1850 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1851 6353ad76 2019-02-08 stsp goto done;
1852 6353ad76 2019-02-08 stsp }
1853 1338848f 2019-12-13 stsp fd = -1;
1854 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1855 656b1f76 2019-05-11 jcs for (;;) {
1856 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1857 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1858 6353ad76 2019-02-08 stsp if (err)
1859 7154f6ce 2019-03-27 stsp goto done;
1860 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1861 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1862 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1863 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1864 7154f6ce 2019-03-27 stsp goto done;
1865 80c5c120 2019-02-19 stsp }
1866 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1867 6353ad76 2019-02-08 stsp if (flen != 0)
1868 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1869 6353ad76 2019-02-08 stsp break;
1870 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1871 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1872 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1873 6353ad76 2019-02-08 stsp break;
1874 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1875 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1876 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1877 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1878 6353ad76 2019-02-08 stsp break;
1879 6353ad76 2019-02-08 stsp }
1880 6353ad76 2019-02-08 stsp } else {
1881 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1882 6353ad76 2019-02-08 stsp break;
1883 6353ad76 2019-02-08 stsp }
1884 6353ad76 2019-02-08 stsp hdrlen = 0;
1885 6353ad76 2019-02-08 stsp }
1886 7154f6ce 2019-03-27 stsp
1887 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1888 7154f6ce 2019-03-27 stsp rewind(f);
1889 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1890 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1891 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1892 6353ad76 2019-02-08 stsp done:
1893 6353ad76 2019-02-08 stsp if (blob)
1894 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1895 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1896 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1897 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1898 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1899 9d31a1d8 2018-03-11 stsp return err;
1900 e2b1e152 2019-07-13 stsp }
1901 e2b1e152 2019-07-13 stsp
1902 e2b1e152 2019-07-13 stsp /*
1903 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1904 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1905 e2b1e152 2019-07-13 stsp */
1906 e2b1e152 2019-07-13 stsp static const struct got_error *
1907 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1908 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1909 e2b1e152 2019-07-13 stsp {
1910 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1911 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1912 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1913 e2b1e152 2019-07-13 stsp
1914 e2b1e152 2019-07-13 stsp return NULL;
1915 9d31a1d8 2018-03-11 stsp }
1916 9d31a1d8 2018-03-11 stsp
1917 9d31a1d8 2018-03-11 stsp static const struct got_error *
1918 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1919 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1920 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1921 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1922 0584f854 2019-04-06 stsp void *progress_arg)
1923 9d31a1d8 2018-03-11 stsp {
1924 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1925 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1926 6353ad76 2019-02-08 stsp char *ondisk_path;
1927 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1928 b8f41171 2019-02-10 stsp struct stat sb;
1929 9d31a1d8 2018-03-11 stsp
1930 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1931 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1932 6353ad76 2019-02-08 stsp
1933 abb4604f 2019-07-27 stsp if (ie) {
1934 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1935 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1936 a76c42e6 2019-08-03 stsp goto done;
1937 a76c42e6 2019-08-03 stsp }
1938 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1939 7f91a133 2019-12-13 stsp repo);
1940 abb4604f 2019-07-27 stsp if (err)
1941 abb4604f 2019-07-27 stsp goto done;
1942 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1943 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1944 c90c8ce3 2020-07-23 stsp } else {
1945 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1946 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1947 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1948 e6f4ba31 2021-09-24 thomas ondisk_path);
1949 e6f4ba31 2021-09-24 thomas goto done;
1950 e6f4ba31 2021-09-24 thomas }
1951 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1952 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1953 e6f4ba31 2021-09-24 thomas } else {
1954 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1955 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1956 e6f4ba31 2021-09-24 thomas else
1957 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1958 e6f4ba31 2021-09-24 thomas }
1959 c90c8ce3 2020-07-23 stsp }
1960 6353ad76 2019-02-08 stsp
1961 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1962 2c41dce7 2021-06-27 stsp if (ie)
1963 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1964 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1965 b8f41171 2019-02-10 stsp goto done;
1966 b8f41171 2019-02-10 stsp }
1967 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1968 a769b60b 2021-06-27 stsp if (ie)
1969 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1970 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1971 5036ab18 2020-04-18 stsp path);
1972 5036ab18 2020-04-18 stsp goto done;
1973 5036ab18 2020-04-18 stsp }
1974 b8f41171 2019-02-10 stsp
1975 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1976 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1977 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1978 c6e8a826 2021-04-05 stsp /*
1979 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1980 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1981 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1982 c6e8a826 2021-04-05 stsp * updating contents of this file.
1983 c6e8a826 2021-04-05 stsp */
1984 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1985 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1986 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1987 c6e8a826 2021-04-05 stsp /* Same commit. */
1988 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1989 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1990 e2b1e152 2019-07-13 stsp if (err)
1991 e2b1e152 2019-07-13 stsp goto done;
1992 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1993 b8f41171 2019-02-10 stsp path);
1994 6353ad76 2019-02-08 stsp goto done;
1995 a378724f 2019-02-10 stsp }
1996 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1997 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1998 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1999 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2000 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2001 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2002 0f58026f 2021-04-05 stsp if (err)
2003 0f58026f 2021-04-05 stsp goto done;
2004 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2005 0f58026f 2021-04-05 stsp path);
2006 b8f41171 2019-02-10 stsp goto done;
2007 e2b1e152 2019-07-13 stsp }
2008 9d31a1d8 2018-03-11 stsp }
2009 9d31a1d8 2018-03-11 stsp
2010 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2011 8da9e5f4 2019-01-12 stsp if (err)
2012 6353ad76 2019-02-08 stsp goto done;
2013 512f0d0e 2019-01-02 stsp
2014 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2015 234035bc 2019-06-01 stsp int update_timestamps;
2016 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2017 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2018 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2019 234035bc 2019-06-01 stsp struct got_object_id id2;
2020 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2021 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2022 234035bc 2019-06-01 stsp if (err)
2023 f69721c3 2019-10-21 stsp goto done;
2024 f69721c3 2019-10-21 stsp }
2025 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2026 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2027 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2028 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2029 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2030 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2031 f69721c3 2019-10-21 stsp goto done;
2032 f69721c3 2019-10-21 stsp }
2033 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2034 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2035 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2036 234035bc 2019-06-01 stsp goto done;
2037 f69721c3 2019-10-21 stsp }
2038 234035bc 2019-06-01 stsp }
2039 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2040 36bf999c 2020-07-23 stsp char *link_target;
2041 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2042 36bf999c 2020-07-23 stsp if (err)
2043 36bf999c 2020-07-23 stsp goto done;
2044 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2045 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2046 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2047 36bf999c 2020-07-23 stsp free(link_target);
2048 993e2a1b 2020-07-23 stsp } else {
2049 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2050 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2051 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2052 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2053 993e2a1b 2020-07-23 stsp }
2054 f69721c3 2019-10-21 stsp free(label_orig);
2055 234035bc 2019-06-01 stsp if (blob2)
2056 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2057 909d120e 2019-09-22 stsp if (err)
2058 909d120e 2019-09-22 stsp goto done;
2059 234035bc 2019-06-01 stsp /*
2060 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2061 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2062 234035bc 2019-06-01 stsp * unmodified files again.
2063 234035bc 2019-06-01 stsp */
2064 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2065 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2066 234035bc 2019-06-01 stsp update_timestamps);
2067 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2068 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2069 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2070 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2071 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2072 1ee397ad 2019-07-12 stsp if (err)
2073 1ee397ad 2019-07-12 stsp goto done;
2074 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2075 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2076 13d9040b 2019-03-27 stsp if (err)
2077 13d9040b 2019-03-27 stsp goto done;
2078 13d9040b 2019-03-27 stsp } else {
2079 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2080 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2081 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2082 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2083 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2084 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2085 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2086 2e1fa222 2020-07-23 stsp } else {
2087 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2088 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2089 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2090 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2091 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2092 2e1fa222 2020-07-23 stsp }
2093 13d9040b 2019-03-27 stsp if (err)
2094 13d9040b 2019-03-27 stsp goto done;
2095 65b05cec 2020-07-23 stsp
2096 054041d0 2020-06-24 stsp if (ie) {
2097 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2098 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2099 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2100 054041d0 2020-06-24 stsp } else {
2101 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2102 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2103 054041d0 2020-06-24 stsp &blob->id);
2104 054041d0 2020-06-24 stsp }
2105 13d9040b 2019-03-27 stsp if (err)
2106 13d9040b 2019-03-27 stsp goto done;
2107 65b05cec 2020-07-23 stsp
2108 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2109 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2110 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2111 2e1fa222 2020-07-23 stsp }
2112 13d9040b 2019-03-27 stsp }
2113 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2114 6353ad76 2019-02-08 stsp done:
2115 6353ad76 2019-02-08 stsp free(ondisk_path);
2116 8da9e5f4 2019-01-12 stsp return err;
2117 90285c3b 2019-01-08 stsp }
2118 90285c3b 2019-01-08 stsp
2119 90285c3b 2019-01-08 stsp static const struct got_error *
2120 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2121 90285c3b 2019-01-08 stsp {
2122 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2123 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2124 90285c3b 2019-01-08 stsp
2125 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2126 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2127 90285c3b 2019-01-08 stsp
2128 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2129 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2130 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2131 c97665ae 2019-01-13 stsp } else {
2132 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2133 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2134 1c4cdd89 2021-06-20 stsp if (err)
2135 1c4cdd89 2021-06-20 stsp goto done;
2136 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2137 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2138 fddefe3b 2020-10-20 stsp free(ondisk_path);
2139 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2140 1c4cdd89 2021-06-20 stsp parent = NULL;
2141 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2142 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2143 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2144 fddefe3b 2020-10-20 stsp ondisk_path);
2145 90285c3b 2019-01-08 stsp break;
2146 90285c3b 2019-01-08 stsp }
2147 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2148 1c4cdd89 2021-06-20 stsp if (err)
2149 1c4cdd89 2021-06-20 stsp break;
2150 1c4cdd89 2021-06-20 stsp }
2151 90285c3b 2019-01-08 stsp }
2152 1c4cdd89 2021-06-20 stsp done:
2153 90285c3b 2019-01-08 stsp free(ondisk_path);
2154 1c4cdd89 2021-06-20 stsp free(parent);
2155 90285c3b 2019-01-08 stsp return err;
2156 512f0d0e 2019-01-02 stsp }
2157 512f0d0e 2019-01-02 stsp
2158 708d8e67 2019-03-27 stsp static const struct got_error *
2159 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2160 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2161 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2162 708d8e67 2019-03-27 stsp {
2163 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2164 708d8e67 2019-03-27 stsp unsigned char status;
2165 708d8e67 2019-03-27 stsp struct stat sb;
2166 708d8e67 2019-03-27 stsp char *ondisk_path;
2167 708d8e67 2019-03-27 stsp
2168 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2169 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2170 a76c42e6 2019-08-03 stsp
2171 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2172 708d8e67 2019-03-27 stsp == -1)
2173 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2174 708d8e67 2019-03-27 stsp
2175 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2176 708d8e67 2019-03-27 stsp if (err)
2177 5a58a424 2020-06-23 stsp goto done;
2178 708d8e67 2019-03-27 stsp
2179 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2180 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2181 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2182 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2183 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2184 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2185 993e2a1b 2020-07-23 stsp goto done;
2186 993e2a1b 2020-07-23 stsp }
2187 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2188 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2189 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2190 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2191 993e2a1b 2020-07-23 stsp if (err)
2192 993e2a1b 2020-07-23 stsp goto done;
2193 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2194 993e2a1b 2020-07-23 stsp ie->path);
2195 993e2a1b 2020-07-23 stsp goto done;
2196 993e2a1b 2020-07-23 stsp }
2197 993e2a1b 2020-07-23 stsp
2198 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2199 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2200 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2201 1ee397ad 2019-07-12 stsp if (err)
2202 5a58a424 2020-06-23 stsp goto done;
2203 fc6346c4 2019-03-27 stsp /*
2204 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2205 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2206 fc6346c4 2019-03-27 stsp */
2207 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2208 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2209 fc6346c4 2019-03-27 stsp } else {
2210 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2211 1ee397ad 2019-07-12 stsp if (err)
2212 5a58a424 2020-06-23 stsp goto done;
2213 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2214 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2215 fc6346c4 2019-03-27 stsp if (err)
2216 5a58a424 2020-06-23 stsp goto done;
2217 fc6346c4 2019-03-27 stsp }
2218 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2219 708d8e67 2019-03-27 stsp }
2220 5a58a424 2020-06-23 stsp done:
2221 5a58a424 2020-06-23 stsp free(ondisk_path);
2222 fc6346c4 2019-03-27 stsp return err;
2223 708d8e67 2019-03-27 stsp }
2224 708d8e67 2019-03-27 stsp
2225 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2226 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2227 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2228 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2229 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2230 8da9e5f4 2019-01-12 stsp void *progress_arg;
2231 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2232 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2233 8da9e5f4 2019-01-12 stsp };
2234 8da9e5f4 2019-01-12 stsp
2235 512f0d0e 2019-01-02 stsp static const struct got_error *
2236 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2237 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2238 512f0d0e 2019-01-02 stsp {
2239 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2240 0584f854 2019-04-06 stsp
2241 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2243 512f0d0e 2019-01-02 stsp
2244 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2245 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2246 8da9e5f4 2019-01-12 stsp }
2247 512f0d0e 2019-01-02 stsp
2248 8da9e5f4 2019-01-12 stsp static const struct got_error *
2249 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2250 8da9e5f4 2019-01-12 stsp {
2251 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2252 7a9df742 2019-01-08 stsp
2253 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2254 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2255 0584f854 2019-04-06 stsp
2256 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2257 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2258 9d31a1d8 2018-03-11 stsp }
2259 9d31a1d8 2018-03-11 stsp
2260 9d31a1d8 2018-03-11 stsp static const struct got_error *
2261 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2262 9d31a1d8 2018-03-11 stsp {
2263 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2264 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2265 8da9e5f4 2019-01-12 stsp char *path;
2266 9d31a1d8 2018-03-11 stsp
2267 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2268 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2269 0584f854 2019-04-06 stsp
2270 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2271 63c5ca5d 2019-08-24 stsp return NULL;
2272 63c5ca5d 2019-08-24 stsp
2273 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2274 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2275 8da9e5f4 2019-01-12 stsp == -1)
2276 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2277 9d31a1d8 2018-03-11 stsp
2278 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2279 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2280 8da9e5f4 2019-01-12 stsp else
2281 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2282 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2283 9d31a1d8 2018-03-11 stsp
2284 8da9e5f4 2019-01-12 stsp free(path);
2285 0cd1c46a 2019-03-11 stsp return err;
2286 0cd1c46a 2019-03-11 stsp }
2287 0cd1c46a 2019-03-11 stsp
2288 b2118c49 2020-07-28 stsp const struct got_error *
2289 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2290 b2118c49 2020-07-28 stsp {
2291 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2292 b2118c49 2020-07-28 stsp
2293 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2294 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2295 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2296 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2297 b2118c49 2020-07-28 stsp }
2298 b2118c49 2020-07-28 stsp
2299 b2118c49 2020-07-28 stsp return NULL;
2300 b2118c49 2020-07-28 stsp }
2301 b2118c49 2020-07-28 stsp
2302 818c7501 2019-07-11 stsp static const struct got_error *
2303 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2304 0cd1c46a 2019-03-11 stsp {
2305 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2306 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2307 0cd1c46a 2019-03-11 stsp
2308 517bab73 2019-03-11 stsp *refname = NULL;
2309 517bab73 2019-03-11 stsp
2310 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2311 b2118c49 2020-07-28 stsp if (err)
2312 b2118c49 2020-07-28 stsp return err;
2313 0cd1c46a 2019-03-11 stsp
2314 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2315 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2316 0647c563 2019-03-11 stsp *refname = NULL;
2317 0cd1c46a 2019-03-11 stsp }
2318 517bab73 2019-03-11 stsp free(uuidstr);
2319 517bab73 2019-03-11 stsp return err;
2320 517bab73 2019-03-11 stsp }
2321 517bab73 2019-03-11 stsp
2322 818c7501 2019-07-11 stsp const struct got_error *
2323 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2324 818c7501 2019-07-11 stsp {
2325 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2326 818c7501 2019-07-11 stsp }
2327 818c7501 2019-07-11 stsp
2328 818c7501 2019-07-11 stsp static const struct got_error *
2329 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2330 818c7501 2019-07-11 stsp {
2331 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2332 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2333 818c7501 2019-07-11 stsp }
2334 818c7501 2019-07-11 stsp
2335 818c7501 2019-07-11 stsp static const struct got_error *
2336 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2337 818c7501 2019-07-11 stsp {
2338 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2339 818c7501 2019-07-11 stsp }
2340 818c7501 2019-07-11 stsp
2341 818c7501 2019-07-11 stsp static const struct got_error *
2342 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2343 818c7501 2019-07-11 stsp {
2344 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2345 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2346 818c7501 2019-07-11 stsp }
2347 818c7501 2019-07-11 stsp
2348 818c7501 2019-07-11 stsp static const struct got_error *
2349 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2350 818c7501 2019-07-11 stsp {
2351 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2352 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2353 0ebf8283 2019-07-24 stsp }
2354 0ebf8283 2019-07-24 stsp
2355 0ebf8283 2019-07-24 stsp static const struct got_error *
2356 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2357 0ebf8283 2019-07-24 stsp {
2358 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2359 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2360 0ebf8283 2019-07-24 stsp }
2361 0ebf8283 2019-07-24 stsp
2362 0ebf8283 2019-07-24 stsp static const struct got_error *
2363 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2364 0ebf8283 2019-07-24 stsp {
2365 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2366 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2367 0ebf8283 2019-07-24 stsp }
2368 0ebf8283 2019-07-24 stsp
2369 0ebf8283 2019-07-24 stsp static const struct got_error *
2370 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2371 0ebf8283 2019-07-24 stsp {
2372 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2373 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2374 818c7501 2019-07-11 stsp }
2375 818c7501 2019-07-11 stsp
2376 0ebf8283 2019-07-24 stsp static const struct got_error *
2377 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 0ebf8283 2019-07-24 stsp {
2379 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2380 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2381 0ebf8283 2019-07-24 stsp }
2382 818c7501 2019-07-11 stsp
2383 0ebf8283 2019-07-24 stsp const struct got_error *
2384 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2385 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2386 0ebf8283 2019-07-24 stsp {
2387 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2388 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2389 0ebf8283 2019-07-24 stsp *path = NULL;
2390 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2391 0ebf8283 2019-07-24 stsp }
2392 0ebf8283 2019-07-24 stsp return NULL;
2393 10604dce 2021-09-24 thomas }
2394 10604dce 2021-09-24 thomas
2395 10604dce 2021-09-24 thomas static const struct got_error *
2396 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2397 10604dce 2021-09-24 thomas {
2398 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2399 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2400 0ebf8283 2019-07-24 stsp }
2401 0ebf8283 2019-07-24 stsp
2402 10604dce 2021-09-24 thomas static const struct got_error *
2403 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2404 10604dce 2021-09-24 thomas {
2405 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2406 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2407 10604dce 2021-09-24 thomas }
2408 10604dce 2021-09-24 thomas
2409 517bab73 2019-03-11 stsp /*
2410 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2411 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2412 517bab73 2019-03-11 stsp */
2413 517bab73 2019-03-11 stsp static const struct got_error *
2414 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2415 517bab73 2019-03-11 stsp {
2416 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2417 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2418 517bab73 2019-03-11 stsp char *refname;
2419 517bab73 2019-03-11 stsp
2420 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2421 517bab73 2019-03-11 stsp if (err)
2422 517bab73 2019-03-11 stsp return err;
2423 0cd1c46a 2019-03-11 stsp
2424 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2425 0cd1c46a 2019-03-11 stsp if (err)
2426 0cd1c46a 2019-03-11 stsp goto done;
2427 0cd1c46a 2019-03-11 stsp
2428 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2429 0cd1c46a 2019-03-11 stsp done:
2430 0cd1c46a 2019-03-11 stsp free(refname);
2431 0cd1c46a 2019-03-11 stsp if (ref)
2432 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2433 3e3a69f1 2019-07-25 stsp return err;
2434 3e3a69f1 2019-07-25 stsp }
2435 3e3a69f1 2019-07-25 stsp
2436 3e3a69f1 2019-07-25 stsp static const struct got_error *
2437 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2438 3e3a69f1 2019-07-25 stsp {
2439 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2440 3e3a69f1 2019-07-25 stsp
2441 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2442 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2443 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2444 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2445 3e3a69f1 2019-07-25 stsp }
2446 9d31a1d8 2018-03-11 stsp return err;
2447 9d31a1d8 2018-03-11 stsp }
2448 9d31a1d8 2018-03-11 stsp
2449 3e3a69f1 2019-07-25 stsp
2450 ebf99748 2019-05-09 stsp static const struct got_error *
2451 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2452 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2453 ebf99748 2019-05-09 stsp {
2454 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2455 ebf99748 2019-05-09 stsp FILE *index = NULL;
2456 0cd1c46a 2019-03-11 stsp
2457 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2458 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2459 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2460 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2461 ebf99748 2019-05-09 stsp
2462 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2463 3e3a69f1 2019-07-25 stsp if (err)
2464 ebf99748 2019-05-09 stsp goto done;
2465 ebf99748 2019-05-09 stsp
2466 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2467 ebf99748 2019-05-09 stsp if (index == NULL) {
2468 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2469 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2470 ebf99748 2019-05-09 stsp } else {
2471 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2472 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2473 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2474 ebf99748 2019-05-09 stsp }
2475 ebf99748 2019-05-09 stsp done:
2476 ebf99748 2019-05-09 stsp if (err) {
2477 ebf99748 2019-05-09 stsp free(*fileindex_path);
2478 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2479 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2480 ebf99748 2019-05-09 stsp *fileindex = NULL;
2481 ebf99748 2019-05-09 stsp }
2482 ebf99748 2019-05-09 stsp return err;
2483 ebf99748 2019-05-09 stsp }
2484 c932eeeb 2019-05-22 stsp
2485 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2486 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2487 c932eeeb 2019-05-22 stsp const char *path;
2488 c932eeeb 2019-05-22 stsp size_t path_len;
2489 c932eeeb 2019-05-22 stsp const char *entry_name;
2490 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2491 a484d721 2019-06-10 stsp void *progress_arg;
2492 c932eeeb 2019-05-22 stsp };
2493 ebf99748 2019-05-09 stsp
2494 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2495 c932eeeb 2019-05-22 stsp static const struct got_error *
2496 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2497 c932eeeb 2019-05-22 stsp {
2498 1ee397ad 2019-07-12 stsp const struct got_error *err;
2499 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2500 c932eeeb 2019-05-22 stsp
2501 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2502 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2503 c932eeeb 2019-05-22 stsp return NULL;
2504 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2505 102ff934 2019-06-11 stsp return NULL;
2506 102ff934 2019-06-11 stsp
2507 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2508 a769b60b 2021-06-27 stsp return NULL;
2509 a769b60b 2021-06-27 stsp
2510 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2511 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2512 c932eeeb 2019-05-22 stsp return NULL;
2513 c932eeeb 2019-05-22 stsp
2514 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2515 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2516 edd02c5e 2019-07-11 stsp ie->path);
2517 1ee397ad 2019-07-12 stsp if (err)
2518 1ee397ad 2019-07-12 stsp return err;
2519 1ee397ad 2019-07-12 stsp }
2520 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2521 c932eeeb 2019-05-22 stsp return NULL;
2522 a615e0e7 2020-12-16 stsp }
2523 a615e0e7 2020-12-16 stsp
2524 a615e0e7 2020-12-16 stsp static const struct got_error *
2525 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2526 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2527 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2528 a615e0e7 2020-12-16 stsp {
2529 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2530 a615e0e7 2020-12-16 stsp
2531 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2532 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2533 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2534 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2535 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2536 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2537 a615e0e7 2020-12-16 stsp
2538 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2539 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2540 9c6338c4 2019-06-02 stsp }
2541 9c6338c4 2019-06-02 stsp
2542 9c6338c4 2019-06-02 stsp static const struct got_error *
2543 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2544 9c6338c4 2019-06-02 stsp {
2545 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2546 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2547 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2548 867630bb 2020-01-17 stsp struct timespec timeout;
2549 9c6338c4 2019-06-02 stsp
2550 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2551 9c6338c4 2019-06-02 stsp fileindex_path);
2552 9c6338c4 2019-06-02 stsp if (err)
2553 9c6338c4 2019-06-02 stsp goto done;
2554 9c6338c4 2019-06-02 stsp
2555 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2556 9c6338c4 2019-06-02 stsp if (err)
2557 9c6338c4 2019-06-02 stsp goto done;
2558 9c6338c4 2019-06-02 stsp
2559 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2560 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2561 9c6338c4 2019-06-02 stsp fileindex_path);
2562 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2563 9c6338c4 2019-06-02 stsp }
2564 867630bb 2020-01-17 stsp
2565 867630bb 2020-01-17 stsp /*
2566 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2567 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2568 867630bb 2020-01-17 stsp * was recorded in the file index.
2569 867630bb 2020-01-17 stsp */
2570 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2571 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2572 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2573 9c6338c4 2019-06-02 stsp done:
2574 9c6338c4 2019-06-02 stsp if (new_index)
2575 9c6338c4 2019-06-02 stsp fclose(new_index);
2576 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2577 9c6338c4 2019-06-02 stsp return err;
2578 c932eeeb 2019-05-22 stsp }
2579 6ced4176 2019-07-12 stsp
2580 6ced4176 2019-07-12 stsp static const struct got_error *
2581 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2582 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2583 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2584 6ced4176 2019-07-12 stsp {
2585 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2586 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2587 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2588 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2589 6ced4176 2019-07-12 stsp
2590 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2591 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2592 6ced4176 2019-07-12 stsp *tree_id = NULL;
2593 6ced4176 2019-07-12 stsp
2594 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2595 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2596 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2597 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2598 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2599 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2600 6ced4176 2019-07-12 stsp goto done;
2601 6ced4176 2019-07-12 stsp }
2602 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2603 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2604 6ced4176 2019-07-12 stsp if (err)
2605 6ced4176 2019-07-12 stsp goto done;
2606 6ced4176 2019-07-12 stsp return NULL;
2607 6ced4176 2019-07-12 stsp }
2608 6ced4176 2019-07-12 stsp
2609 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2610 6ced4176 2019-07-12 stsp
2611 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2612 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2613 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2614 6ced4176 2019-07-12 stsp goto done;
2615 6ced4176 2019-07-12 stsp }
2616 6ced4176 2019-07-12 stsp
2617 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2618 6ced4176 2019-07-12 stsp in_repo_path);
2619 6ced4176 2019-07-12 stsp if (err)
2620 6ced4176 2019-07-12 stsp goto done;
2621 6ced4176 2019-07-12 stsp
2622 6ced4176 2019-07-12 stsp free(in_repo_path);
2623 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2624 6ced4176 2019-07-12 stsp
2625 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2626 6ced4176 2019-07-12 stsp if (err)
2627 6ced4176 2019-07-12 stsp goto done;
2628 c932eeeb 2019-05-22 stsp
2629 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2630 6ced4176 2019-07-12 stsp /* Check out a single file. */
2631 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2632 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2633 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2634 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2635 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2636 6ced4176 2019-07-12 stsp goto done;
2637 6ced4176 2019-07-12 stsp }
2638 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2639 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2640 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2641 6ced4176 2019-07-12 stsp goto done;
2642 6ced4176 2019-07-12 stsp }
2643 6ced4176 2019-07-12 stsp } else {
2644 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2645 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2646 6ced4176 2019-07-12 stsp if (err)
2647 6ced4176 2019-07-12 stsp return err;
2648 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2649 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2650 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2651 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2652 6ced4176 2019-07-12 stsp goto done;
2653 6ced4176 2019-07-12 stsp }
2654 6ced4176 2019-07-12 stsp }
2655 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2656 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2657 6ced4176 2019-07-12 stsp } else {
2658 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2659 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2660 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2661 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2662 6ced4176 2019-07-12 stsp goto done;
2663 6ced4176 2019-07-12 stsp }
2664 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2665 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2666 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2667 6ced4176 2019-07-12 stsp goto done;
2668 6ced4176 2019-07-12 stsp }
2669 6ced4176 2019-07-12 stsp }
2670 6ced4176 2019-07-12 stsp done:
2671 6ced4176 2019-07-12 stsp free(id);
2672 6ced4176 2019-07-12 stsp free(in_repo_path);
2673 6ced4176 2019-07-12 stsp if (err) {
2674 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2675 6ced4176 2019-07-12 stsp free(*tree_relpath);
2676 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2677 6ced4176 2019-07-12 stsp free(*tree_id);
2678 6ced4176 2019-07-12 stsp *tree_id = NULL;
2679 6ced4176 2019-07-12 stsp }
2680 07ed472d 2019-07-12 stsp return err;
2681 07ed472d 2019-07-12 stsp }
2682 07ed472d 2019-07-12 stsp
2683 07ed472d 2019-07-12 stsp static const struct got_error *
2684 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2685 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2686 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2687 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2688 07ed472d 2019-07-12 stsp {
2689 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2690 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2691 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2692 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2693 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2694 07ed472d 2019-07-12 stsp
2695 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2696 7f47418f 2019-12-20 stsp if (err) {
2697 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2698 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2699 7f47418f 2019-12-20 stsp goto done;
2700 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2701 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2702 7f47418f 2019-12-20 stsp if (err)
2703 7f47418f 2019-12-20 stsp return err;
2704 7f47418f 2019-12-20 stsp }
2705 07ed472d 2019-07-12 stsp
2706 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2707 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2708 07ed472d 2019-07-12 stsp if (err)
2709 07ed472d 2019-07-12 stsp goto done;
2710 07ed472d 2019-07-12 stsp
2711 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2712 07ed472d 2019-07-12 stsp if (err)
2713 07ed472d 2019-07-12 stsp goto done;
2714 07ed472d 2019-07-12 stsp
2715 07ed472d 2019-07-12 stsp if (entry_name &&
2716 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2717 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2718 07ed472d 2019-07-12 stsp goto done;
2719 07ed472d 2019-07-12 stsp }
2720 07ed472d 2019-07-12 stsp
2721 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2722 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2723 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2724 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2725 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2726 07ed472d 2019-07-12 stsp arg.repo = repo;
2727 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2728 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2729 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2730 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2731 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2732 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2733 07ed472d 2019-07-12 stsp done:
2734 07ed472d 2019-07-12 stsp if (tree)
2735 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2736 07ed472d 2019-07-12 stsp if (commit)
2737 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2738 6ced4176 2019-07-12 stsp return err;
2739 6ced4176 2019-07-12 stsp }
2740 6ced4176 2019-07-12 stsp
2741 9d31a1d8 2018-03-11 stsp const struct got_error *
2742 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2743 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2744 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2745 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2746 9d31a1d8 2018-03-11 stsp {
2747 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2748 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2749 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2750 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2751 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2752 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2753 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2754 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2755 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2756 f2ea84fa 2019-07-27 stsp int entry_type;
2757 f2ea84fa 2019-07-27 stsp char *relpath;
2758 f2ea84fa 2019-07-27 stsp char *entry_name;
2759 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2760 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2761 9d31a1d8 2018-03-11 stsp
2762 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2763 f2ea84fa 2019-07-27 stsp
2764 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2765 9d31a1d8 2018-03-11 stsp if (err)
2766 9d31a1d8 2018-03-11 stsp return err;
2767 93a30277 2018-12-24 stsp
2768 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2769 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2770 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2771 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2772 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2773 f2ea84fa 2019-07-27 stsp goto done;
2774 f2ea84fa 2019-07-27 stsp }
2775 6ced4176 2019-07-12 stsp
2776 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2777 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2778 f2ea84fa 2019-07-27 stsp if (err) {
2779 f2ea84fa 2019-07-27 stsp free(tpd);
2780 6ced4176 2019-07-12 stsp goto done;
2781 6ced4176 2019-07-12 stsp }
2782 f2ea84fa 2019-07-27 stsp
2783 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2784 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2785 f2ea84fa 2019-07-27 stsp if (err) {
2786 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2787 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2788 f2ea84fa 2019-07-27 stsp free(tpd);
2789 f2ea84fa 2019-07-27 stsp goto done;
2790 f2ea84fa 2019-07-27 stsp }
2791 f2ea84fa 2019-07-27 stsp } else
2792 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2793 f2ea84fa 2019-07-27 stsp
2794 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2795 6ced4176 2019-07-12 stsp }
2796 6ced4176 2019-07-12 stsp
2797 51514078 2018-12-25 stsp /*
2798 51514078 2018-12-25 stsp * Read the file index.
2799 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2800 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2801 51514078 2018-12-25 stsp */
2802 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2803 8da9e5f4 2019-01-12 stsp if (err)
2804 c4cdcb68 2019-04-03 stsp goto done;
2805 c4cdcb68 2019-04-03 stsp
2806 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2807 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2808 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2809 f2ea84fa 2019-07-27 stsp
2810 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2811 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2812 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2813 f2ea84fa 2019-07-27 stsp if (err)
2814 f2ea84fa 2019-07-27 stsp break;
2815 f2ea84fa 2019-07-27 stsp
2816 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2817 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2818 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2819 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2820 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2821 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2822 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2823 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2824 f2ea84fa 2019-07-27 stsp if (err)
2825 f2ea84fa 2019-07-27 stsp break;
2826 f2ea84fa 2019-07-27 stsp
2827 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2828 711d9cd9 2019-07-12 stsp }
2829 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2830 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2831 af12c6b9 2019-06-04 stsp err = sync_err;
2832 9d31a1d8 2018-03-11 stsp done:
2833 9c6338c4 2019-06-02 stsp free(fileindex_path);
2834 edfa7d7f 2018-09-11 stsp if (tree)
2835 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2836 9d31a1d8 2018-03-11 stsp if (commit)
2837 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2838 5ade8233 2019-07-12 stsp if (fileindex)
2839 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2840 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2841 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2842 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2843 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2844 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2845 f2ea84fa 2019-07-27 stsp free(tpd);
2846 f2ea84fa 2019-07-27 stsp }
2847 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2848 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2849 9d31a1d8 2018-03-11 stsp err = unlockerr;
2850 f8d1f275 2019-02-04 stsp return err;
2851 f8d1f275 2019-02-04 stsp }
2852 f8d1f275 2019-02-04 stsp
2853 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2854 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2855 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2856 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2857 234035bc 2019-06-01 stsp void *progress_arg;
2858 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2859 234035bc 2019-06-01 stsp void *cancel_arg;
2860 f69721c3 2019-10-21 stsp const char *label_orig;
2861 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2862 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2863 234035bc 2019-06-01 stsp };
2864 234035bc 2019-06-01 stsp
2865 234035bc 2019-06-01 stsp static const struct got_error *
2866 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2867 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2868 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2869 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2870 234035bc 2019-06-01 stsp {
2871 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2872 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2873 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2874 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2875 234035bc 2019-06-01 stsp struct stat sb;
2876 234035bc 2019-06-01 stsp unsigned char status;
2877 234035bc 2019-06-01 stsp int local_changes_subsumed;
2878 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2879 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2880 234035bc 2019-06-01 stsp
2881 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2882 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2883 d6c87207 2019-08-02 stsp strlen(path2));
2884 1ee397ad 2019-07-12 stsp if (ie == NULL)
2885 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2886 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2887 234035bc 2019-06-01 stsp
2888 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2889 234035bc 2019-06-01 stsp path2) == -1)
2890 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2891 234035bc 2019-06-01 stsp
2892 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2893 7f91a133 2019-12-13 stsp repo);
2894 234035bc 2019-06-01 stsp if (err)
2895 234035bc 2019-06-01 stsp goto done;
2896 234035bc 2019-06-01 stsp
2897 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2898 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2899 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2900 234035bc 2019-06-01 stsp goto done;
2901 234035bc 2019-06-01 stsp }
2902 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2903 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2904 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2905 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2906 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2907 234035bc 2019-06-01 stsp goto done;
2908 234035bc 2019-06-01 stsp }
2909 234035bc 2019-06-01 stsp
2910 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2911 36bf999c 2020-07-23 stsp char *link_target2;
2912 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2913 36bf999c 2020-07-23 stsp if (err)
2914 36bf999c 2020-07-23 stsp goto done;
2915 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2916 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2917 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2918 36bf999c 2020-07-23 stsp free(link_target2);
2919 af57b12a 2020-07-23 stsp } else {
2920 1af628f4 2021-06-03 stsp int fd;
2921 1af628f4 2021-06-03 stsp
2922 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2923 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2924 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2925 1af628f4 2021-06-03 stsp goto done;
2926 1af628f4 2021-06-03 stsp }
2927 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2928 1af628f4 2021-06-03 stsp f_orig, blob1);
2929 1af628f4 2021-06-03 stsp if (err)
2930 1af628f4 2021-06-03 stsp goto done;
2931 1af628f4 2021-06-03 stsp
2932 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2933 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2934 1af628f4 2021-06-03 stsp goto done;
2935 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2936 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2937 1af628f4 2021-06-03 stsp if (err)
2938 1af628f4 2021-06-03 stsp goto done;
2939 1af628f4 2021-06-03 stsp
2940 1af628f4 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2941 1af628f4 2021-06-03 stsp if (fd == -1) {
2942 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2943 1af628f4 2021-06-03 stsp ondisk_path);
2944 1af628f4 2021-06-03 stsp goto done;
2945 1af628f4 2021-06-03 stsp }
2946 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2947 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2948 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2949 1af628f4 2021-06-03 stsp ondisk_path);
2950 1af628f4 2021-06-03 stsp close(fd);
2951 1af628f4 2021-06-03 stsp goto done;
2952 1af628f4 2021-06-03 stsp }
2953 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2954 1af628f4 2021-06-03 stsp if (err)
2955 1af628f4 2021-06-03 stsp goto done;
2956 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2957 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2958 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2959 1af628f4 2021-06-03 stsp goto done;
2960 1af628f4 2021-06-03 stsp }
2961 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2962 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2963 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2964 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2965 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2966 af57b12a 2020-07-23 stsp }
2967 234035bc 2019-06-01 stsp } else if (blob1) {
2968 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2969 d6c87207 2019-08-02 stsp strlen(path1));
2970 1ee397ad 2019-07-12 stsp if (ie == NULL)
2971 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2972 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2973 2b92fad7 2019-06-02 stsp
2974 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2975 2b92fad7 2019-06-02 stsp path1) == -1)
2976 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2977 2b92fad7 2019-06-02 stsp
2978 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2979 7f91a133 2019-12-13 stsp repo);
2980 2b92fad7 2019-06-02 stsp if (err)
2981 2b92fad7 2019-06-02 stsp goto done;
2982 2b92fad7 2019-06-02 stsp
2983 2b92fad7 2019-06-02 stsp switch (status) {
2984 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2985 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2986 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2987 1ee397ad 2019-07-12 stsp if (err)
2988 1ee397ad 2019-07-12 stsp goto done;
2989 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2990 2b92fad7 2019-06-02 stsp if (err)
2991 2b92fad7 2019-06-02 stsp goto done;
2992 2b92fad7 2019-06-02 stsp if (ie)
2993 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2994 2b92fad7 2019-06-02 stsp break;
2995 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2996 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2997 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2998 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2999 1ee397ad 2019-07-12 stsp if (err)
3000 1ee397ad 2019-07-12 stsp goto done;
3001 2b92fad7 2019-06-02 stsp if (ie)
3002 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3003 2b92fad7 2019-06-02 stsp break;
3004 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3005 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3006 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3007 0a22ca1a 2020-09-23 stsp /*
3008 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3009 0a22ca1a 2020-09-23 stsp * exists in the repository.
3010 0a22ca1a 2020-09-23 stsp */
3011 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
3012 0a22ca1a 2020-09-23 stsp if (err)
3013 0a22ca1a 2020-09-23 stsp goto done;
3014 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3015 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3016 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3017 0a22ca1a 2020-09-23 stsp if (err)
3018 0a22ca1a 2020-09-23 stsp goto done;
3019 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3020 0a22ca1a 2020-09-23 stsp path1);
3021 0a22ca1a 2020-09-23 stsp if (err)
3022 0a22ca1a 2020-09-23 stsp goto done;
3023 0a22ca1a 2020-09-23 stsp if (ie)
3024 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3025 0a22ca1a 2020-09-23 stsp ie);
3026 0a22ca1a 2020-09-23 stsp } else {
3027 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3028 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3029 0a22ca1a 2020-09-23 stsp }
3030 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3031 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3032 0a22ca1a 2020-09-23 stsp free(id);
3033 0a22ca1a 2020-09-23 stsp if (err)
3034 0a22ca1a 2020-09-23 stsp goto done;
3035 0a22ca1a 2020-09-23 stsp break;
3036 0a22ca1a 2020-09-23 stsp }
3037 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3038 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3039 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3040 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3041 1ee397ad 2019-07-12 stsp if (err)
3042 1ee397ad 2019-07-12 stsp goto done;
3043 2b92fad7 2019-06-02 stsp break;
3044 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3045 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3046 1ee397ad 2019-07-12 stsp if (err)
3047 1ee397ad 2019-07-12 stsp goto done;
3048 2b92fad7 2019-06-02 stsp break;
3049 2b92fad7 2019-06-02 stsp default:
3050 2b92fad7 2019-06-02 stsp break;
3051 2b92fad7 2019-06-02 stsp }
3052 234035bc 2019-06-01 stsp } else if (blob2) {
3053 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3054 234035bc 2019-06-01 stsp path2) == -1)
3055 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3056 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3057 d6c87207 2019-08-02 stsp strlen(path2));
3058 234035bc 2019-06-01 stsp if (ie) {
3059 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3060 7f91a133 2019-12-13 stsp -1, NULL, repo);
3061 234035bc 2019-06-01 stsp if (err)
3062 234035bc 2019-06-01 stsp goto done;
3063 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3064 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3065 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3066 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3067 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3068 1ee397ad 2019-07-12 stsp status, path2);
3069 234035bc 2019-06-01 stsp goto done;
3070 234035bc 2019-06-01 stsp }
3071 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3072 36bf999c 2020-07-23 stsp char *link_target2;
3073 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3074 36bf999c 2020-07-23 stsp blob2);
3075 36bf999c 2020-07-23 stsp if (err)
3076 36bf999c 2020-07-23 stsp goto done;
3077 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3078 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3079 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3080 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3081 36bf999c 2020-07-23 stsp free(link_target2);
3082 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3083 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3084 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3085 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3086 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3087 526a746f 2020-07-23 stsp a->progress_arg);
3088 dfe9fba0 2020-07-23 stsp } else {
3089 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3090 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3091 526a746f 2020-07-23 stsp }
3092 dfe9fba0 2020-07-23 stsp if (err)
3093 dfe9fba0 2020-07-23 stsp goto done;
3094 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3095 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3096 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3097 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3098 234035bc 2019-06-01 stsp if (err)
3099 234035bc 2019-06-01 stsp goto done;
3100 234035bc 2019-06-01 stsp }
3101 234035bc 2019-06-01 stsp } else {
3102 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3103 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3104 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3105 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3106 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3107 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3108 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3109 2e1fa222 2020-07-23 stsp } else {
3110 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3111 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3112 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3113 2e1fa222 2020-07-23 stsp }
3114 234035bc 2019-06-01 stsp if (err)
3115 234035bc 2019-06-01 stsp goto done;
3116 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3117 234035bc 2019-06-01 stsp if (err)
3118 234035bc 2019-06-01 stsp goto done;
3119 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3120 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3121 3969253a 2020-03-07 stsp if (err) {
3122 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3123 3969253a 2020-03-07 stsp goto done;
3124 3969253a 2020-03-07 stsp }
3125 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3126 2b92fad7 2019-06-02 stsp if (err) {
3127 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3128 2b92fad7 2019-06-02 stsp goto done;
3129 2b92fad7 2019-06-02 stsp }
3130 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3131 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3132 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3133 2e1fa222 2020-07-23 stsp }
3134 234035bc 2019-06-01 stsp }
3135 234035bc 2019-06-01 stsp }
3136 234035bc 2019-06-01 stsp done:
3137 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3138 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3139 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3140 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3141 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3142 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3143 1af628f4 2021-06-03 stsp free(id_str);
3144 54d5be07 2021-06-03 stsp free(label_deriv2);
3145 234035bc 2019-06-01 stsp free(ondisk_path);
3146 234035bc 2019-06-01 stsp return err;
3147 234035bc 2019-06-01 stsp }
3148 234035bc 2019-06-01 stsp
3149 69de9dd4 2021-09-03 stsp static const struct got_error *
3150 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3151 69de9dd4 2021-09-03 stsp {
3152 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3153 69de9dd4 2021-09-03 stsp
3154 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3155 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3156 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3157 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3158 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3159 69de9dd4 2021-09-03 stsp
3160 69de9dd4 2021-09-03 stsp return NULL;
3161 69de9dd4 2021-09-03 stsp }
3162 69de9dd4 2021-09-03 stsp
3163 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3164 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3165 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3166 234035bc 2019-06-01 stsp struct got_repository *repo;
3167 234035bc 2019-06-01 stsp };
3168 234035bc 2019-06-01 stsp
3169 234035bc 2019-06-01 stsp static const struct got_error *
3170 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3171 69de9dd4 2021-09-03 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3172 69de9dd4 2021-09-03 stsp struct got_object_id *id2, const char *path1, const char *path2,
3173 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3174 234035bc 2019-06-01 stsp {
3175 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3176 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3177 234035bc 2019-06-01 stsp unsigned char status;
3178 234035bc 2019-06-01 stsp struct stat sb;
3179 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3180 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3181 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3182 234035bc 2019-06-01 stsp char *ondisk_path;
3183 234035bc 2019-06-01 stsp
3184 69de9dd4 2021-09-03 stsp if (id == NULL)
3185 69de9dd4 2021-09-03 stsp return NULL;
3186 234035bc 2019-06-01 stsp
3187 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3188 69de9dd4 2021-09-03 stsp if (ie == NULL)
3189 69de9dd4 2021-09-03 stsp return NULL;
3190 69de9dd4 2021-09-03 stsp
3191 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3192 234035bc 2019-06-01 stsp == -1)
3193 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3194 234035bc 2019-06-01 stsp
3195 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3196 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3197 5546d466 2021-09-02 stsp free(ondisk_path);
3198 234035bc 2019-06-01 stsp if (err)
3199 234035bc 2019-06-01 stsp return err;
3200 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3201 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3202 234035bc 2019-06-01 stsp
3203 234035bc 2019-06-01 stsp return NULL;
3204 234035bc 2019-06-01 stsp }
3205 234035bc 2019-06-01 stsp
3206 818c7501 2019-07-11 stsp static const struct got_error *
3207 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3208 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3209 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3210 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3211 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3212 234035bc 2019-06-01 stsp {
3213 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3214 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3215 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3216 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3217 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3218 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3219 234035bc 2019-06-01 stsp
3220 03415a1a 2019-06-02 stsp if (commit_id1) {
3221 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3222 03415a1a 2019-06-02 stsp worktree->path_prefix);
3223 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3224 03415a1a 2019-06-02 stsp goto done;
3225 69d57f3d 2020-07-31 stsp }
3226 69d57f3d 2020-07-31 stsp if (tree_id1) {
3227 69d57f3d 2020-07-31 stsp char *id_str;
3228 03415a1a 2019-06-02 stsp
3229 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3230 03415a1a 2019-06-02 stsp if (err)
3231 03415a1a 2019-06-02 stsp goto done;
3232 f69721c3 2019-10-21 stsp
3233 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3234 f69721c3 2019-10-21 stsp if (err)
3235 f69721c3 2019-10-21 stsp goto done;
3236 f69721c3 2019-10-21 stsp
3237 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3238 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3239 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3240 f69721c3 2019-10-21 stsp free(id_str);
3241 f69721c3 2019-10-21 stsp goto done;
3242 f69721c3 2019-10-21 stsp }
3243 f69721c3 2019-10-21 stsp free(id_str);
3244 03415a1a 2019-06-02 stsp }
3245 234035bc 2019-06-01 stsp
3246 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3247 234035bc 2019-06-01 stsp worktree->path_prefix);
3248 234035bc 2019-06-01 stsp if (err)
3249 234035bc 2019-06-01 stsp goto done;
3250 234035bc 2019-06-01 stsp
3251 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3252 234035bc 2019-06-01 stsp if (err)
3253 234035bc 2019-06-01 stsp goto done;
3254 234035bc 2019-06-01 stsp
3255 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3256 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3257 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3258 69de9dd4 2021-09-03 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3259 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3260 69de9dd4 2021-09-03 stsp if (err)
3261 69de9dd4 2021-09-03 stsp goto done;
3262 69de9dd4 2021-09-03 stsp
3263 234035bc 2019-06-01 stsp arg.worktree = worktree;
3264 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3265 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3266 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3267 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3268 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3269 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3270 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3271 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3272 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3273 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3274 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3275 af12c6b9 2019-06-04 stsp err = sync_err;
3276 234035bc 2019-06-01 stsp done:
3277 234035bc 2019-06-01 stsp if (tree1)
3278 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3279 234035bc 2019-06-01 stsp if (tree2)
3280 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3281 f69721c3 2019-10-21 stsp free(label_orig);
3282 818c7501 2019-07-11 stsp return err;
3283 818c7501 2019-07-11 stsp }
3284 234035bc 2019-06-01 stsp
3285 818c7501 2019-07-11 stsp const struct got_error *
3286 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3287 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3288 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3289 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3290 818c7501 2019-07-11 stsp {
3291 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3292 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3293 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3294 818c7501 2019-07-11 stsp
3295 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3296 818c7501 2019-07-11 stsp if (err)
3297 818c7501 2019-07-11 stsp return err;
3298 818c7501 2019-07-11 stsp
3299 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3300 818c7501 2019-07-11 stsp if (err)
3301 818c7501 2019-07-11 stsp goto done;
3302 818c7501 2019-07-11 stsp
3303 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3304 69de9dd4 2021-09-03 stsp worktree);
3305 818c7501 2019-07-11 stsp if (err)
3306 818c7501 2019-07-11 stsp goto done;
3307 818c7501 2019-07-11 stsp
3308 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3309 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3310 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3311 818c7501 2019-07-11 stsp done:
3312 818c7501 2019-07-11 stsp if (fileindex)
3313 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3314 818c7501 2019-07-11 stsp free(fileindex_path);
3315 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3316 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3317 234035bc 2019-06-01 stsp err = unlockerr;
3318 234035bc 2019-06-01 stsp return err;
3319 234035bc 2019-06-01 stsp }
3320 234035bc 2019-06-01 stsp
3321 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3322 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3323 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3324 927df6b7 2019-02-10 stsp const char *status_path;
3325 927df6b7 2019-02-10 stsp size_t status_path_len;
3326 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3327 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3328 f8d1f275 2019-02-04 stsp void *status_arg;
3329 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3330 f8d1f275 2019-02-04 stsp void *cancel_arg;
3331 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3332 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3333 f2a9dc41 2019-12-13 tracey int report_unchanged;
3334 3143d852 2020-06-25 stsp int no_ignores;
3335 f8d1f275 2019-02-04 stsp };
3336 88d0e355 2019-08-03 stsp
3337 f8d1f275 2019-02-04 stsp static const struct got_error *
3338 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3339 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3340 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3341 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3342 927df6b7 2019-02-10 stsp {
3343 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3344 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3345 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3346 927df6b7 2019-02-10 stsp struct stat sb;
3347 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3348 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3349 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3350 927df6b7 2019-02-10 stsp
3351 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3352 98eaaa12 2019-08-03 stsp if (err)
3353 98eaaa12 2019-08-03 stsp return err;
3354 98eaaa12 2019-08-03 stsp
3355 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3356 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3357 98eaaa12 2019-08-03 stsp return NULL;
3358 98eaaa12 2019-08-03 stsp
3359 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3360 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3361 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3362 98eaaa12 2019-08-03 stsp }
3363 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3364 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3365 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3366 927df6b7 2019-02-10 stsp }
3367 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3368 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3369 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3370 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3371 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3372 98eaaa12 2019-08-03 stsp }
3373 98eaaa12 2019-08-03 stsp
3374 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3375 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3376 927df6b7 2019-02-10 stsp }
3377 927df6b7 2019-02-10 stsp
3378 927df6b7 2019-02-10 stsp static const struct got_error *
3379 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3380 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3381 f8d1f275 2019-02-04 stsp {
3382 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3383 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3384 f8d1f275 2019-02-04 stsp char *abspath;
3385 f8d1f275 2019-02-04 stsp
3386 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3387 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3388 0584f854 2019-04-06 stsp
3389 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3390 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3391 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3392 927df6b7 2019-02-10 stsp return NULL;
3393 927df6b7 2019-02-10 stsp
3394 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3395 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3396 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3397 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3398 f8d1f275 2019-02-04 stsp } else {
3399 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3400 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3401 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3402 f8d1f275 2019-02-04 stsp }
3403 f8d1f275 2019-02-04 stsp
3404 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3405 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3406 f8d1f275 2019-02-04 stsp free(abspath);
3407 9d31a1d8 2018-03-11 stsp return err;
3408 9d31a1d8 2018-03-11 stsp }
3409 f8d1f275 2019-02-04 stsp
3410 f8d1f275 2019-02-04 stsp static const struct got_error *
3411 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3412 f8d1f275 2019-02-04 stsp {
3413 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3414 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3415 2ec1f75b 2019-03-26 stsp unsigned char status;
3416 927df6b7 2019-02-10 stsp
3417 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3418 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3419 0584f854 2019-04-06 stsp
3420 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3421 927df6b7 2019-02-10 stsp return NULL;
3422 927df6b7 2019-02-10 stsp
3423 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3424 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3425 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3426 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3427 2ec1f75b 2019-03-26 stsp else
3428 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3429 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3430 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3431 6841da00 2019-08-08 stsp }
3432 6841da00 2019-08-08 stsp
3433 6841da00 2019-08-08 stsp void
3434 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3435 6841da00 2019-08-08 stsp {
3436 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3437 6841da00 2019-08-08 stsp
3438 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3439 6841da00 2019-08-08 stsp free((char *)pe->path);
3440 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3441 6841da00 2019-08-08 stsp }
3442 6841da00 2019-08-08 stsp
3443 6841da00 2019-08-08 stsp void
3444 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3445 6841da00 2019-08-08 stsp {
3446 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3447 6841da00 2019-08-08 stsp
3448 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3449 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3450 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3451 6841da00 2019-08-08 stsp free((char *)pe->path);
3452 6841da00 2019-08-08 stsp }
3453 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3454 6841da00 2019-08-08 stsp }
3455 6841da00 2019-08-08 stsp
3456 6841da00 2019-08-08 stsp static const struct got_error *
3457 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3458 6841da00 2019-08-08 stsp {
3459 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3460 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3461 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3462 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3463 6841da00 2019-08-08 stsp size_t linesize = 0;
3464 6841da00 2019-08-08 stsp ssize_t linelen;
3465 6841da00 2019-08-08 stsp
3466 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3467 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3468 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3469 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3470 6841da00 2019-08-08 stsp
3471 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3472 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3473 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3474 bd8de430 2019-10-04 stsp
3475 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3476 bd8de430 2019-10-04 stsp if (line[0] == '#')
3477 bd8de430 2019-10-04 stsp continue;
3478 bd8de430 2019-10-04 stsp
3479 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3480 bd8de430 2019-10-04 stsp if (line[0] == '!')
3481 bd8de430 2019-10-04 stsp continue;
3482 bd8de430 2019-10-04 stsp
3483 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3484 6841da00 2019-08-08 stsp line) == -1) {
3485 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3486 6841da00 2019-08-08 stsp goto done;
3487 6841da00 2019-08-08 stsp }
3488 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3489 6841da00 2019-08-08 stsp if (err)
3490 6841da00 2019-08-08 stsp goto done;
3491 6841da00 2019-08-08 stsp }
3492 6841da00 2019-08-08 stsp if (ferror(f)) {
3493 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3494 6841da00 2019-08-08 stsp goto done;
3495 6841da00 2019-08-08 stsp }
3496 6841da00 2019-08-08 stsp
3497 6841da00 2019-08-08 stsp dirpath = strdup(path);
3498 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3499 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3500 6841da00 2019-08-08 stsp goto done;
3501 6841da00 2019-08-08 stsp }
3502 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3503 6841da00 2019-08-08 stsp done:
3504 6841da00 2019-08-08 stsp free(line);
3505 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3506 6841da00 2019-08-08 stsp free(dirpath);
3507 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3508 6841da00 2019-08-08 stsp }
3509 6841da00 2019-08-08 stsp return err;
3510 6841da00 2019-08-08 stsp }
3511 6841da00 2019-08-08 stsp
3512 6841da00 2019-08-08 stsp int
3513 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3514 6841da00 2019-08-08 stsp {
3515 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3516 bd8de430 2019-10-04 stsp
3517 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3518 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3519 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3520 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3521 bd8de430 2019-10-04 stsp
3522 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3523 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3524 6841da00 2019-08-08 stsp
3525 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3526 bd8de430 2019-10-04 stsp continue;
3527 bd8de430 2019-10-04 stsp pattern += 3;
3528 bd8de430 2019-10-04 stsp p = path;
3529 bd8de430 2019-10-04 stsp while (*p) {
3530 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3531 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3532 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3533 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3534 bd8de430 2019-10-04 stsp p++;
3535 bd8de430 2019-10-04 stsp while (*p == '/')
3536 bd8de430 2019-10-04 stsp p++;
3537 bd8de430 2019-10-04 stsp continue;
3538 bd8de430 2019-10-04 stsp }
3539 bd8de430 2019-10-04 stsp return 1;
3540 bd8de430 2019-10-04 stsp }
3541 bd8de430 2019-10-04 stsp }
3542 bd8de430 2019-10-04 stsp }
3543 bd8de430 2019-10-04 stsp
3544 6841da00 2019-08-08 stsp /*
3545 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3546 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3547 6841da00 2019-08-08 stsp * ignores backwards.
3548 6841da00 2019-08-08 stsp */
3549 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3550 6841da00 2019-08-08 stsp while (pe) {
3551 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3552 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3553 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3554 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3555 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3556 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3557 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3558 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3559 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3560 6841da00 2019-08-08 stsp continue;
3561 6841da00 2019-08-08 stsp return 1;
3562 6841da00 2019-08-08 stsp }
3563 6841da00 2019-08-08 stsp }
3564 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3565 6841da00 2019-08-08 stsp }
3566 6841da00 2019-08-08 stsp
3567 6841da00 2019-08-08 stsp return 0;
3568 6841da00 2019-08-08 stsp }
3569 6841da00 2019-08-08 stsp
3570 6841da00 2019-08-08 stsp static const struct got_error *
3571 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3572 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3573 6841da00 2019-08-08 stsp {
3574 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3575 6841da00 2019-08-08 stsp char *ignorespath;
3576 886cec17 2019-12-15 stsp int fd = -1;
3577 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3578 6841da00 2019-08-08 stsp
3579 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3580 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3581 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3582 6841da00 2019-08-08 stsp
3583 886cec17 2019-12-15 stsp if (dirfd != -1) {
3584 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3585 886cec17 2019-12-15 stsp if (fd == -1) {
3586 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3587 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3588 886cec17 2019-12-15 stsp ignorespath);
3589 886cec17 2019-12-15 stsp } else {
3590 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3591 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3592 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3593 886cec17 2019-12-15 stsp ignorespath);
3594 886cec17 2019-12-15 stsp else {
3595 886cec17 2019-12-15 stsp fd = -1;
3596 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3597 886cec17 2019-12-15 stsp }
3598 886cec17 2019-12-15 stsp }
3599 886cec17 2019-12-15 stsp } else {
3600 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3601 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3602 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3603 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3604 886cec17 2019-12-15 stsp ignorespath);
3605 886cec17 2019-12-15 stsp } else
3606 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3607 886cec17 2019-12-15 stsp }
3608 6841da00 2019-08-08 stsp
3609 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3610 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3611 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3612 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3613 6841da00 2019-08-08 stsp free(ignorespath);
3614 6841da00 2019-08-08 stsp return err;
3615 f8d1f275 2019-02-04 stsp }
3616 f8d1f275 2019-02-04 stsp
3617 f8d1f275 2019-02-04 stsp static const struct got_error *
3618 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3619 6092c299 2021-10-04 thomas int dirfd)
3620 f8d1f275 2019-02-04 stsp {
3621 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3622 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3623 f8d1f275 2019-02-04 stsp char *path = NULL;
3624 6092c299 2021-10-04 thomas
3625 6092c299 2021-10-04 thomas if (ignore != NULL)
3626 6092c299 2021-10-04 thomas *ignore = 0;
3627 f8d1f275 2019-02-04 stsp
3628 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3629 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3630 0584f854 2019-04-06 stsp
3631 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3632 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3633 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3634 f8d1f275 2019-02-04 stsp } else {
3635 f8d1f275 2019-02-04 stsp path = de->d_name;
3636 f8d1f275 2019-02-04 stsp }
3637 f8d1f275 2019-02-04 stsp
3638 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3639 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3640 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3641 6092c299 2021-10-04 thomas *ignore = 1;
3642 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3643 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3644 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3645 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3646 f8d1f275 2019-02-04 stsp if (parent_path[0])
3647 f8d1f275 2019-02-04 stsp free(path);
3648 b72f483a 2019-02-05 stsp return err;
3649 f8d1f275 2019-02-04 stsp }
3650 f8d1f275 2019-02-04 stsp
3651 347d1d3e 2019-07-12 stsp static const struct got_error *
3652 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3653 3143d852 2020-06-25 stsp {
3654 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3655 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3656 3143d852 2020-06-25 stsp
3657 3143d852 2020-06-25 stsp if (a->no_ignores)
3658 3143d852 2020-06-25 stsp return NULL;
3659 3143d852 2020-06-25 stsp
3660 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3661 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3662 3143d852 2020-06-25 stsp if (err)
3663 3143d852 2020-06-25 stsp return err;
3664 3143d852 2020-06-25 stsp
3665 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3666 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3667 3143d852 2020-06-25 stsp
3668 3143d852 2020-06-25 stsp return err;
3669 3143d852 2020-06-25 stsp }
3670 3143d852 2020-06-25 stsp
3671 3143d852 2020-06-25 stsp static const struct got_error *
3672 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3673 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3674 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3675 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3676 abb4604f 2019-07-27 stsp {
3677 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3678 abb4604f 2019-07-27 stsp struct stat sb;
3679 abb4604f 2019-07-27 stsp
3680 0e33f8e0 2021-09-01 stsp if (!no_ignores && match_ignores(ignores, path))
3681 ff56836b 2021-07-08 stsp return NULL;
3682 ff56836b 2021-07-08 stsp
3683 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3684 abb4604f 2019-07-27 stsp if (ie)
3685 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3686 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3687 abb4604f 2019-07-27 stsp
3688 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3689 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3690 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3691 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3692 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3693 abb4604f 2019-07-27 stsp return NULL;
3694 abb4604f 2019-07-27 stsp }
3695 abb4604f 2019-07-27 stsp
3696 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3697 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3698 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3699 abb4604f 2019-07-27 stsp
3700 abb4604f 2019-07-27 stsp return NULL;
3701 3143d852 2020-06-25 stsp }
3702 3143d852 2020-06-25 stsp
3703 3143d852 2020-06-25 stsp static const struct got_error *
3704 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3705 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3706 3143d852 2020-06-25 stsp {
3707 3143d852 2020-06-25 stsp const struct got_error *err;
3708 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3709 3143d852 2020-06-25 stsp
3710 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3711 3143d852 2020-06-25 stsp ".cvsignore");
3712 3143d852 2020-06-25 stsp if (err)
3713 3143d852 2020-06-25 stsp return err;
3714 3143d852 2020-06-25 stsp
3715 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3716 3143d852 2020-06-25 stsp ".gitignore");
3717 3143d852 2020-06-25 stsp if (err)
3718 3143d852 2020-06-25 stsp return err;
3719 3143d852 2020-06-25 stsp
3720 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3721 3143d852 2020-06-25 stsp if (err) {
3722 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3723 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3724 3143d852 2020-06-25 stsp return err;
3725 3143d852 2020-06-25 stsp }
3726 3143d852 2020-06-25 stsp for (;;) {
3727 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3728 3143d852 2020-06-25 stsp ".cvsignore");
3729 3143d852 2020-06-25 stsp if (err)
3730 3143d852 2020-06-25 stsp break;
3731 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3732 3143d852 2020-06-25 stsp ".gitignore");
3733 3143d852 2020-06-25 stsp if (err)
3734 3143d852 2020-06-25 stsp break;
3735 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3736 3143d852 2020-06-25 stsp if (err) {
3737 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3738 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3739 3143d852 2020-06-25 stsp break;
3740 3143d852 2020-06-25 stsp }
3741 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3742 ff56836b 2021-07-08 stsp break;
3743 b737c85a 2020-06-26 stsp free(parent_path);
3744 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3745 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3746 3143d852 2020-06-25 stsp }
3747 3143d852 2020-06-25 stsp
3748 b737c85a 2020-06-26 stsp free(parent_path);
3749 b737c85a 2020-06-26 stsp free(next_parent_path);
3750 3143d852 2020-06-25 stsp return err;
3751 abb4604f 2019-07-27 stsp }
3752 abb4604f 2019-07-27 stsp
3753 abb4604f 2019-07-27 stsp static const struct got_error *
3754 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3755 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3756 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3757 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3758 f2a9dc41 2019-12-13 tracey int report_unchanged)
3759 f8d1f275 2019-02-04 stsp {
3760 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3761 6fc93f37 2019-12-13 stsp int fd = -1;
3762 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3763 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3764 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3765 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3766 3143d852 2020-06-25 stsp
3767 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3768 f8d1f275 2019-02-04 stsp
3769 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3770 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3771 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3772 8dc303cc 2019-07-27 stsp
3773 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3774 6fc93f37 2019-12-13 stsp if (fd == -1) {
3775 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3776 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3777 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3778 ff56836b 2021-07-08 stsp else {
3779 ff56836b 2021-07-08 stsp if (!no_ignores) {
3780 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3781 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3782 ff56836b 2021-07-08 stsp if (err)
3783 ff56836b 2021-07-08 stsp goto done;
3784 ff56836b 2021-07-08 stsp }
3785 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3786 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3787 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3788 ff56836b 2021-07-08 stsp }
3789 abb4604f 2019-07-27 stsp } else {
3790 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3791 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3792 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3793 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3794 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3795 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3796 abb4604f 2019-07-27 stsp arg.status_path = path;
3797 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3798 abb4604f 2019-07-27 stsp arg.repo = repo;
3799 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3800 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3801 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3802 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3803 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3804 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3805 022fae89 2019-12-06 tracey if (!no_ignores) {
3806 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3807 3143d852 2020-06-25 stsp worktree->root_path, path);
3808 3143d852 2020-06-25 stsp if (err)
3809 3143d852 2020-06-25 stsp goto done;
3810 022fae89 2019-12-06 tracey }
3811 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3812 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3813 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3814 927df6b7 2019-02-10 stsp }
3815 3143d852 2020-06-25 stsp done:
3816 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3817 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3818 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3819 927df6b7 2019-02-10 stsp free(ondisk_path);
3820 347d1d3e 2019-07-12 stsp return err;
3821 347d1d3e 2019-07-12 stsp }
3822 347d1d3e 2019-07-12 stsp
3823 347d1d3e 2019-07-12 stsp const struct got_error *
3824 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3825 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3826 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3827 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3828 347d1d3e 2019-07-12 stsp {
3829 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3830 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3831 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3832 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3833 347d1d3e 2019-07-12 stsp
3834 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3835 347d1d3e 2019-07-12 stsp if (err)
3836 347d1d3e 2019-07-12 stsp return err;
3837 347d1d3e 2019-07-12 stsp
3838 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3839 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3840 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3841 f6343036 2021-06-22 stsp no_ignores, 0);
3842 72ea6654 2019-07-27 stsp if (err)
3843 72ea6654 2019-07-27 stsp break;
3844 72ea6654 2019-07-27 stsp }
3845 f8d1f275 2019-02-04 stsp free(fileindex_path);
3846 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3847 f8d1f275 2019-02-04 stsp return err;
3848 f8d1f275 2019-02-04 stsp }
3849 6c7ab921 2019-03-18 stsp
3850 6c7ab921 2019-03-18 stsp const struct got_error *
3851 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3852 6c7ab921 2019-03-18 stsp const char *arg)
3853 6c7ab921 2019-03-18 stsp {
3854 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3855 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3856 6c7ab921 2019-03-18 stsp size_t len;
3857 00bb5ea0 2020-07-23 stsp struct stat sb;
3858 01740607 2020-11-04 stsp char *abspath = NULL;
3859 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3860 6c7ab921 2019-03-18 stsp
3861 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3862 6c7ab921 2019-03-18 stsp
3863 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3864 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3865 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3866 00bb5ea0 2020-07-23 stsp
3867 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3868 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3869 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3870 00bb5ea0 2020-07-23 stsp goto done;
3871 00bb5ea0 2020-07-23 stsp }
3872 727173c3 2020-11-06 stsp sb.st_mode = 0;
3873 00bb5ea0 2020-07-23 stsp }
3874 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3875 00bb5ea0 2020-07-23 stsp /*
3876 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3877 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3878 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3879 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3880 00bb5ea0 2020-07-23 stsp */
3881 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3882 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3883 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3884 00bb5ea0 2020-07-23 stsp goto done;
3885 00bb5ea0 2020-07-23 stsp }
3886 00bb5ea0 2020-07-23 stsp
3887 00bb5ea0 2020-07-23 stsp }
3888 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3889 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3890 00bb5ea0 2020-07-23 stsp if (err)
3891 d0710d08 2019-07-22 stsp goto done;
3892 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3893 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3894 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3895 00bb5ea0 2020-07-23 stsp goto done;
3896 00bb5ea0 2020-07-23 stsp }
3897 00bb5ea0 2020-07-23 stsp } else {
3898 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3899 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3900 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3901 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3902 00bb5ea0 2020-07-23 stsp goto done;
3903 00bb5ea0 2020-07-23 stsp }
3904 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3905 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3906 01740607 2020-11-04 stsp goto done;
3907 01740607 2020-11-04 stsp }
3908 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3909 01740607 2020-11-04 stsp sizeof(canonpath));
3910 01740607 2020-11-04 stsp if (err)
3911 00bb5ea0 2020-07-23 stsp goto done;
3912 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3913 01740607 2020-11-04 stsp if (resolved == NULL) {
3914 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3915 01740607 2020-11-04 stsp goto done;
3916 00bb5ea0 2020-07-23 stsp }
3917 d0710d08 2019-07-22 stsp }
3918 d0710d08 2019-07-22 stsp }
3919 6c7ab921 2019-03-18 stsp
3920 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3921 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3922 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3923 6c7ab921 2019-03-18 stsp goto done;
3924 6c7ab921 2019-03-18 stsp }
3925 6c7ab921 2019-03-18 stsp
3926 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3927 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3928 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3929 f6d88e1a 2019-05-29 stsp if (err)
3930 f6d88e1a 2019-05-29 stsp goto done;
3931 f6d88e1a 2019-05-29 stsp } else {
3932 f6d88e1a 2019-05-29 stsp path = strdup("");
3933 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3934 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3935 f6d88e1a 2019-05-29 stsp goto done;
3936 f6d88e1a 2019-05-29 stsp }
3937 6c7ab921 2019-03-18 stsp }
3938 6c7ab921 2019-03-18 stsp
3939 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3940 6c7ab921 2019-03-18 stsp len = strlen(path);
3941 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3942 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3943 6c7ab921 2019-03-18 stsp len--;
3944 6c7ab921 2019-03-18 stsp }
3945 6c7ab921 2019-03-18 stsp done:
3946 01740607 2020-11-04 stsp free(abspath);
3947 6c7ab921 2019-03-18 stsp free(resolved);
3948 d0710d08 2019-07-22 stsp free(cwd);
3949 6c7ab921 2019-03-18 stsp if (err == NULL)
3950 6c7ab921 2019-03-18 stsp *wt_path = path;
3951 6c7ab921 2019-03-18 stsp else
3952 6c7ab921 2019-03-18 stsp free(path);
3953 d00136be 2019-03-26 stsp return err;
3954 d00136be 2019-03-26 stsp }
3955 d00136be 2019-03-26 stsp
3956 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3957 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3958 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3959 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3960 4e68cba3 2019-11-23 stsp void *progress_arg;
3961 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3962 4e68cba3 2019-11-23 stsp };
3963 4e68cba3 2019-11-23 stsp
3964 4e68cba3 2019-11-23 stsp static const struct got_error *
3965 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3966 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3967 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3968 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3969 07f5b47a 2019-06-02 stsp {
3970 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3971 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3972 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3973 6d022e97 2019-08-04 stsp struct stat sb;
3974 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3975 07f5b47a 2019-06-02 stsp
3976 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3977 dbb83fbd 2019-12-12 stsp relpath) == -1)
3978 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3979 dbb83fbd 2019-12-12 stsp
3980 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3981 6d022e97 2019-08-04 stsp if (ie) {
3982 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3983 12463d8b 2019-12-13 stsp de_name, a->repo);
3984 6d022e97 2019-08-04 stsp if (err)
3985 dbb83fbd 2019-12-12 stsp goto done;
3986 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3987 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3988 dbb83fbd 2019-12-12 stsp goto done;
3989 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3990 dbb83fbd 2019-12-12 stsp if (err)
3991 dbb83fbd 2019-12-12 stsp goto done;
3992 6d022e97 2019-08-04 stsp }
3993 07f5b47a 2019-06-02 stsp
3994 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3995 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3996 dbb83fbd 2019-12-12 stsp goto done;
3997 4e68cba3 2019-11-23 stsp }
3998 07f5b47a 2019-06-02 stsp
3999 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4000 4e68cba3 2019-11-23 stsp if (err)
4001 4e68cba3 2019-11-23 stsp goto done;
4002 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4003 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4004 3969253a 2020-03-07 stsp if (err) {
4005 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4006 3969253a 2020-03-07 stsp goto done;
4007 3969253a 2020-03-07 stsp }
4008 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4009 07f5b47a 2019-06-02 stsp if (err) {
4010 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4011 4e68cba3 2019-11-23 stsp goto done;
4012 07f5b47a 2019-06-02 stsp }
4013 4e68cba3 2019-11-23 stsp done:
4014 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4015 dbb83fbd 2019-12-12 stsp if (err)
4016 dbb83fbd 2019-12-12 stsp return err;
4017 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4018 dbb83fbd 2019-12-12 stsp return NULL;
4019 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4020 07f5b47a 2019-06-02 stsp }
4021 07f5b47a 2019-06-02 stsp
4022 d00136be 2019-03-26 stsp const struct got_error *
4023 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4024 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4025 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4026 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4027 d00136be 2019-03-26 stsp {
4028 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4029 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4030 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4031 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4032 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4033 d00136be 2019-03-26 stsp
4034 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4035 d00136be 2019-03-26 stsp if (err)
4036 d00136be 2019-03-26 stsp return err;
4037 d00136be 2019-03-26 stsp
4038 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4039 d00136be 2019-03-26 stsp if (err)
4040 d00136be 2019-03-26 stsp goto done;
4041 d00136be 2019-03-26 stsp
4042 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4043 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4044 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4045 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4046 4e68cba3 2019-11-23 stsp saa.repo = repo;
4047 4e68cba3 2019-11-23 stsp
4048 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4049 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4050 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4051 1dd54920 2019-05-11 stsp if (err)
4052 af12c6b9 2019-06-04 stsp break;
4053 1dd54920 2019-05-11 stsp }
4054 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4055 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4056 af12c6b9 2019-06-04 stsp err = sync_err;
4057 d00136be 2019-03-26 stsp done:
4058 fb399478 2019-07-12 stsp free(fileindex_path);
4059 d00136be 2019-03-26 stsp if (fileindex)
4060 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4061 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4062 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4063 d00136be 2019-03-26 stsp err = unlockerr;
4064 6c7ab921 2019-03-18 stsp return err;
4065 6c7ab921 2019-03-18 stsp }
4066 17ed4618 2019-06-02 stsp
4067 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4068 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4069 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4070 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4071 f2a9dc41 2019-12-13 tracey void *progress_arg;
4072 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4073 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4074 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4075 766841c2 2020-08-13 stsp const char *status_codes;
4076 f2a9dc41 2019-12-13 tracey };
4077 f2a9dc41 2019-12-13 tracey
4078 f2a9dc41 2019-12-13 tracey static const struct got_error *
4079 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4080 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4081 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4082 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4083 17ed4618 2019-06-02 stsp {
4084 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4085 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4086 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4087 17ed4618 2019-06-02 stsp struct stat sb;
4088 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4089 17ed4618 2019-06-02 stsp
4090 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4091 17ed4618 2019-06-02 stsp if (ie == NULL)
4092 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4093 2ec1f75b 2019-03-26 stsp
4094 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4095 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4096 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4097 9acbc4fa 2019-08-03 stsp return NULL;
4098 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4099 9acbc4fa 2019-08-03 stsp }
4100 9acbc4fa 2019-08-03 stsp
4101 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4102 f2a9dc41 2019-12-13 tracey relpath) == -1)
4103 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4104 f2a9dc41 2019-12-13 tracey
4105 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4106 12463d8b 2019-12-13 stsp a->repo);
4107 17ed4618 2019-06-02 stsp if (err)
4108 f2a9dc41 2019-12-13 tracey goto done;
4109 17ed4618 2019-06-02 stsp
4110 766841c2 2020-08-13 stsp if (a->status_codes) {
4111 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4112 766841c2 2020-08-13 stsp int i;
4113 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4114 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4115 766841c2 2020-08-13 stsp break;
4116 766841c2 2020-08-13 stsp }
4117 766841c2 2020-08-13 stsp if (i == ncodes) {
4118 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4119 766841c2 2020-08-13 stsp free(ondisk_path);
4120 766841c2 2020-08-13 stsp return NULL;
4121 766841c2 2020-08-13 stsp }
4122 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4123 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4124 766841c2 2020-08-13 stsp static char msg[64];
4125 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4126 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4127 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4128 766841c2 2020-08-13 stsp goto done;
4129 766841c2 2020-08-13 stsp }
4130 766841c2 2020-08-13 stsp }
4131 766841c2 2020-08-13 stsp
4132 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4133 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4134 f2a9dc41 2019-12-13 tracey goto done;
4135 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4136 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4137 f2a9dc41 2019-12-13 tracey goto done;
4138 f2a9dc41 2019-12-13 tracey }
4139 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4140 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4141 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4142 f2a9dc41 2019-12-13 tracey goto done;
4143 f2a9dc41 2019-12-13 tracey }
4144 17ed4618 2019-06-02 stsp }
4145 17ed4618 2019-06-02 stsp
4146 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4147 20a2ad1c 2020-10-20 stsp size_t root_len;
4148 20a2ad1c 2020-10-20 stsp
4149 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4150 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4151 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4152 12463d8b 2019-12-13 stsp ondisk_path);
4153 12463d8b 2019-12-13 stsp goto done;
4154 12463d8b 2019-12-13 stsp }
4155 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4156 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4157 12463d8b 2019-12-13 stsp goto done;
4158 15341bfd 2020-03-05 tracey }
4159 15341bfd 2020-03-05 tracey
4160 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4161 20a2ad1c 2020-10-20 stsp do {
4162 20a2ad1c 2020-10-20 stsp char *parent;
4163 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4164 20a2ad1c 2020-10-20 stsp if (err)
4165 2513f20a 2020-10-20 stsp goto done;
4166 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4167 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4168 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4169 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4170 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4171 20a2ad1c 2020-10-20 stsp ondisk_path);
4172 15341bfd 2020-03-05 tracey break;
4173 15341bfd 2020-03-05 tracey }
4174 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4175 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4176 f2a9dc41 2019-12-13 tracey }
4177 17ed4618 2019-06-02 stsp
4178 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4179 f2a9dc41 2019-12-13 tracey done:
4180 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4181 f2a9dc41 2019-12-13 tracey if (err)
4182 f2a9dc41 2019-12-13 tracey return err;
4183 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4184 f2a9dc41 2019-12-13 tracey return NULL;
4185 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4186 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4187 17ed4618 2019-06-02 stsp }
4188 17ed4618 2019-06-02 stsp
4189 2ec1f75b 2019-03-26 stsp const struct got_error *
4190 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4191 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4192 766841c2 2020-08-13 stsp const char *status_codes,
4193 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4194 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4195 2ec1f75b 2019-03-26 stsp {
4196 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4197 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4198 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4199 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4200 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4201 2ec1f75b 2019-03-26 stsp
4202 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4203 2ec1f75b 2019-03-26 stsp if (err)
4204 2ec1f75b 2019-03-26 stsp return err;
4205 2ec1f75b 2019-03-26 stsp
4206 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4207 2ec1f75b 2019-03-26 stsp if (err)
4208 2ec1f75b 2019-03-26 stsp goto done;
4209 f2a9dc41 2019-12-13 tracey
4210 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4211 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4212 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4213 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4214 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4215 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4216 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4217 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4218 2ec1f75b 2019-03-26 stsp
4219 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4220 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4221 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4222 17ed4618 2019-06-02 stsp if (err)
4223 af12c6b9 2019-06-04 stsp break;
4224 2ec1f75b 2019-03-26 stsp }
4225 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4226 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4227 af12c6b9 2019-06-04 stsp err = sync_err;
4228 2ec1f75b 2019-03-26 stsp done:
4229 fb399478 2019-07-12 stsp free(fileindex_path);
4230 2ec1f75b 2019-03-26 stsp if (fileindex)
4231 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4232 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4233 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4234 2ec1f75b 2019-03-26 stsp err = unlockerr;
4235 33aa809d 2019-08-08 stsp return err;
4236 33aa809d 2019-08-08 stsp }
4237 33aa809d 2019-08-08 stsp
4238 33aa809d 2019-08-08 stsp static const struct got_error *
4239 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4240 33aa809d 2019-08-08 stsp {
4241 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4242 33aa809d 2019-08-08 stsp char *line = NULL;
4243 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4244 33aa809d 2019-08-08 stsp ssize_t linelen;
4245 33aa809d 2019-08-08 stsp
4246 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4247 33aa809d 2019-08-08 stsp if (linelen == -1) {
4248 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4249 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4250 33aa809d 2019-08-08 stsp goto done;
4251 33aa809d 2019-08-08 stsp }
4252 33aa809d 2019-08-08 stsp return NULL;
4253 33aa809d 2019-08-08 stsp }
4254 33aa809d 2019-08-08 stsp if (outfile) {
4255 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4256 33aa809d 2019-08-08 stsp if (n != linelen) {
4257 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4258 33aa809d 2019-08-08 stsp goto done;
4259 33aa809d 2019-08-08 stsp }
4260 33aa809d 2019-08-08 stsp }
4261 33aa809d 2019-08-08 stsp if (rejectfile) {
4262 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4263 33aa809d 2019-08-08 stsp if (n != linelen)
4264 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4265 33aa809d 2019-08-08 stsp }
4266 33aa809d 2019-08-08 stsp done:
4267 33aa809d 2019-08-08 stsp free(line);
4268 2ec1f75b 2019-03-26 stsp return err;
4269 2ec1f75b 2019-03-26 stsp }
4270 1f1abb7e 2019-08-08 stsp
4271 33aa809d 2019-08-08 stsp static const struct got_error *
4272 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4273 33aa809d 2019-08-08 stsp {
4274 33aa809d 2019-08-08 stsp char *line = NULL;
4275 33aa809d 2019-08-08 stsp size_t linesize = 0;
4276 33aa809d 2019-08-08 stsp ssize_t linelen;
4277 33aa809d 2019-08-08 stsp
4278 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4279 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4280 500467ff 2019-09-25 hiltjo if (ferror(f))
4281 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4282 500467ff 2019-09-25 hiltjo return NULL;
4283 500467ff 2019-09-25 hiltjo }
4284 33aa809d 2019-08-08 stsp free(line);
4285 33aa809d 2019-08-08 stsp return NULL;
4286 33aa809d 2019-08-08 stsp }
4287 33aa809d 2019-08-08 stsp
4288 33aa809d 2019-08-08 stsp static const struct got_error *
4289 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4290 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4291 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4292 abc59930 2021-09-05 naddy {
4293 33aa809d 2019-08-08 stsp const struct got_error *err;
4294 33aa809d 2019-08-08 stsp
4295 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4296 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4297 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4298 33aa809d 2019-08-08 stsp if (err)
4299 33aa809d 2019-08-08 stsp return err;
4300 33aa809d 2019-08-08 stsp (*line_cur1)++;
4301 33aa809d 2019-08-08 stsp }
4302 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4303 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4304 33aa809d 2019-08-08 stsp if (rejectfile)
4305 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4306 33aa809d 2019-08-08 stsp else
4307 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4308 33aa809d 2019-08-08 stsp if (err)
4309 33aa809d 2019-08-08 stsp return err;
4310 33aa809d 2019-08-08 stsp (*line_cur2)++;
4311 33aa809d 2019-08-08 stsp }
4312 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4313 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4314 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4315 33aa809d 2019-08-08 stsp if (err)
4316 33aa809d 2019-08-08 stsp return err;
4317 33aa809d 2019-08-08 stsp (*line_cur2)++;
4318 33aa809d 2019-08-08 stsp }
4319 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4320 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4321 33aa809d 2019-08-08 stsp if (rejectfile)
4322 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4323 33aa809d 2019-08-08 stsp else
4324 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4325 33aa809d 2019-08-08 stsp if (err)
4326 33aa809d 2019-08-08 stsp return err;
4327 33aa809d 2019-08-08 stsp (*line_cur1)++;
4328 33aa809d 2019-08-08 stsp }
4329 f1e81a05 2019-08-10 stsp
4330 f1e81a05 2019-08-10 stsp return NULL;
4331 f1e81a05 2019-08-10 stsp }
4332 f1e81a05 2019-08-10 stsp
4333 f1e81a05 2019-08-10 stsp static const struct got_error *
4334 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4335 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4336 f1e81a05 2019-08-10 stsp {
4337 f1e81a05 2019-08-10 stsp const struct got_error *err;
4338 f1e81a05 2019-08-10 stsp
4339 f1e81a05 2019-08-10 stsp if (outfile) {
4340 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4341 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4342 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4343 f1e81a05 2019-08-10 stsp if (err)
4344 f1e81a05 2019-08-10 stsp return err;
4345 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4346 f1e81a05 2019-08-10 stsp }
4347 f1e81a05 2019-08-10 stsp }
4348 f1e81a05 2019-08-10 stsp if (rejectfile) {
4349 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4350 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4351 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4352 f1e81a05 2019-08-10 stsp if (err)
4353 f1e81a05 2019-08-10 stsp return err;
4354 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4355 f1e81a05 2019-08-10 stsp }
4356 33aa809d 2019-08-08 stsp }
4357 33aa809d 2019-08-08 stsp
4358 33aa809d 2019-08-08 stsp return NULL;
4359 33aa809d 2019-08-08 stsp }
4360 33aa809d 2019-08-08 stsp
4361 33aa809d 2019-08-08 stsp static const struct got_error *
4362 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4363 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4364 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4365 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4366 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4367 33aa809d 2019-08-08 stsp {
4368 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4369 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4370 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4371 33aa809d 2019-08-08 stsp FILE *hunkfile;
4372 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4373 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4374 fe621944 2020-11-10 stsp int rc;
4375 33aa809d 2019-08-08 stsp
4376 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4377 33aa809d 2019-08-08 stsp
4378 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4379 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4380 fe621944 2020-11-10 stsp start_old = cc.left.start;
4381 fe621944 2020-11-10 stsp end_old = cc.left.end;
4382 fe621944 2020-11-10 stsp start_new = cc.right.start;
4383 fe621944 2020-11-10 stsp end_new = cc.right.end;
4384 33aa809d 2019-08-08 stsp
4385 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4386 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4387 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4388 33aa809d 2019-08-08 stsp
4389 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4390 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4391 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4392 33aa809d 2019-08-08 stsp
4393 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4394 fe621944 2020-11-10 stsp if (diff_state == NULL)
4395 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4396 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4397 fe621944 2020-11-10 stsp
4398 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4399 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4400 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4401 33aa809d 2019-08-08 stsp goto done;
4402 33aa809d 2019-08-08 stsp }
4403 fe621944 2020-11-10 stsp
4404 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4405 fe621944 2020-11-10 stsp diff_result, &cc);
4406 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4407 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4408 33aa809d 2019-08-08 stsp goto done;
4409 33aa809d 2019-08-08 stsp }
4410 fe621944 2020-11-10 stsp
4411 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4412 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4413 33aa809d 2019-08-08 stsp goto done;
4414 33aa809d 2019-08-08 stsp }
4415 33aa809d 2019-08-08 stsp
4416 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4417 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4418 33aa809d 2019-08-08 stsp if (err)
4419 33aa809d 2019-08-08 stsp goto done;
4420 33aa809d 2019-08-08 stsp
4421 33aa809d 2019-08-08 stsp switch (*choice) {
4422 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4423 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4424 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4425 33aa809d 2019-08-08 stsp break;
4426 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4427 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4428 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4429 33aa809d 2019-08-08 stsp break;
4430 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4431 33aa809d 2019-08-08 stsp break;
4432 33aa809d 2019-08-08 stsp default:
4433 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4434 33aa809d 2019-08-08 stsp break;
4435 33aa809d 2019-08-08 stsp }
4436 33aa809d 2019-08-08 stsp done:
4437 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4438 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4439 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4440 33aa809d 2019-08-08 stsp return err;
4441 33aa809d 2019-08-08 stsp }
4442 33aa809d 2019-08-08 stsp
4443 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4444 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4445 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4446 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4447 1f1abb7e 2019-08-08 stsp void *progress_arg;
4448 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4449 33aa809d 2019-08-08 stsp void *patch_arg;
4450 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4451 10604dce 2021-09-24 thomas int unlink_added_files;
4452 1f1abb7e 2019-08-08 stsp };
4453 a129376b 2019-03-28 stsp
4454 e20a8b6f 2019-06-04 stsp static const struct got_error *
4455 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4456 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4457 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4458 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4459 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4460 33aa809d 2019-08-08 stsp {
4461 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4462 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4463 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4464 1ebedb77 2019-10-19 stsp int fd2 = -1;
4465 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4466 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4467 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4468 fe621944 2020-11-10 stsp struct stat sb2;
4469 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4470 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4471 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4472 33aa809d 2019-08-08 stsp
4473 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4474 33aa809d 2019-08-08 stsp
4475 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4476 33aa809d 2019-08-08 stsp if (err)
4477 33aa809d 2019-08-08 stsp return err;
4478 33aa809d 2019-08-08 stsp
4479 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4480 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4481 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4482 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4483 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4484 fa3cef63 2020-07-23 stsp goto done;
4485 fa3cef63 2020-07-23 stsp }
4486 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4487 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4488 fa3cef63 2020-07-23 stsp if (link_len == -1)
4489 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4490 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4491 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4492 12463d8b 2019-12-13 stsp }
4493 12463d8b 2019-12-13 stsp } else {
4494 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4495 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4496 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4497 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4498 fa3cef63 2020-07-23 stsp goto done;
4499 fa3cef63 2020-07-23 stsp }
4500 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4501 fa3cef63 2020-07-23 stsp sizeof(link_target));
4502 fa3cef63 2020-07-23 stsp if (link_len == -1)
4503 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4504 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4505 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4506 12463d8b 2019-12-13 stsp }
4507 1ebedb77 2019-10-19 stsp }
4508 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4509 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4510 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4511 fa3cef63 2020-07-23 stsp goto done;
4512 fa3cef63 2020-07-23 stsp }
4513 1ebedb77 2019-10-19 stsp
4514 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4515 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4516 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4517 fa3cef63 2020-07-23 stsp goto done;
4518 fa3cef63 2020-07-23 stsp }
4519 fa3cef63 2020-07-23 stsp fd2 = -1;
4520 fa3cef63 2020-07-23 stsp } else {
4521 fa3cef63 2020-07-23 stsp size_t n;
4522 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4523 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4524 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4525 fa3cef63 2020-07-23 stsp goto done;
4526 fa3cef63 2020-07-23 stsp }
4527 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4528 fa3cef63 2020-07-23 stsp if (n != link_len) {
4529 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4530 fa3cef63 2020-07-23 stsp goto done;
4531 fa3cef63 2020-07-23 stsp }
4532 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4533 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4534 fa3cef63 2020-07-23 stsp goto done;
4535 fa3cef63 2020-07-23 stsp }
4536 fa3cef63 2020-07-23 stsp rewind(f2);
4537 33aa809d 2019-08-08 stsp }
4538 33aa809d 2019-08-08 stsp
4539 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4540 33aa809d 2019-08-08 stsp if (err)
4541 33aa809d 2019-08-08 stsp goto done;
4542 33aa809d 2019-08-08 stsp
4543 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4544 33aa809d 2019-08-08 stsp if (err)
4545 33aa809d 2019-08-08 stsp goto done;
4546 33aa809d 2019-08-08 stsp
4547 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4548 33aa809d 2019-08-08 stsp if (err)
4549 33aa809d 2019-08-08 stsp goto done;
4550 33aa809d 2019-08-08 stsp
4551 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4552 fe621944 2020-11-10 stsp NULL);
4553 33aa809d 2019-08-08 stsp if (err)
4554 33aa809d 2019-08-08 stsp goto done;
4555 33aa809d 2019-08-08 stsp
4556 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4557 33aa809d 2019-08-08 stsp if (err)
4558 33aa809d 2019-08-08 stsp goto done;
4559 33aa809d 2019-08-08 stsp
4560 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4561 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4562 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4563 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4564 fe621944 2020-11-10 stsp
4565 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4566 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4567 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4568 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4569 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4570 fe621944 2020-11-10 stsp nchanges++;
4571 fe621944 2020-11-10 stsp }
4572 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4573 33aa809d 2019-08-08 stsp int choice;
4574 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4575 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4576 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4577 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4578 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4579 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4580 33aa809d 2019-08-08 stsp if (err)
4581 33aa809d 2019-08-08 stsp goto done;
4582 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4583 33aa809d 2019-08-08 stsp have_content = 1;
4584 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4585 33aa809d 2019-08-08 stsp break;
4586 33aa809d 2019-08-08 stsp }
4587 1ebedb77 2019-10-19 stsp if (have_content) {
4588 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4589 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4590 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4591 1ebedb77 2019-10-19 stsp if (err)
4592 1ebedb77 2019-10-19 stsp goto done;
4593 1ebedb77 2019-10-19 stsp
4594 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4595 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4596 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4597 fa3cef63 2020-07-23 stsp goto done;
4598 fa3cef63 2020-07-23 stsp }
4599 1ebedb77 2019-10-19 stsp }
4600 1ebedb77 2019-10-19 stsp }
4601 33aa809d 2019-08-08 stsp done:
4602 33aa809d 2019-08-08 stsp free(id_str);
4603 33aa809d 2019-08-08 stsp if (blob)
4604 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4605 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4606 fe621944 2020-11-10 stsp if (err == NULL)
4607 fe621944 2020-11-10 stsp err = free_err;
4608 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4609 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4610 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4611 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4612 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4613 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4614 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4615 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4616 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4617 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4618 33aa809d 2019-08-08 stsp if (err || !have_content) {
4619 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4620 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4621 33aa809d 2019-08-08 stsp free(*path_outfile);
4622 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4623 33aa809d 2019-08-08 stsp }
4624 33aa809d 2019-08-08 stsp free(path1);
4625 33aa809d 2019-08-08 stsp return err;
4626 33aa809d 2019-08-08 stsp }
4627 33aa809d 2019-08-08 stsp
4628 33aa809d 2019-08-08 stsp static const struct got_error *
4629 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4630 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4631 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4632 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4633 a129376b 2019-03-28 stsp {
4634 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4635 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4636 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4637 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4638 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4639 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4640 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4641 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4642 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4643 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4644 a129376b 2019-03-28 stsp
4645 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4646 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4647 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4648 d3bcc3d1 2019-08-08 stsp return NULL;
4649 3d69ad8d 2019-08-17 semarie
4650 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4651 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4652 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4653 d3bcc3d1 2019-08-08 stsp
4654 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4655 65084dad 2019-08-08 stsp if (ie == NULL)
4656 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4657 a129376b 2019-03-28 stsp
4658 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4659 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4660 a129376b 2019-03-28 stsp if (err) {
4661 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4662 a129376b 2019-03-28 stsp goto done;
4663 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4664 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4665 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4666 e20a8b6f 2019-06-04 stsp goto done;
4667 e20a8b6f 2019-06-04 stsp }
4668 a129376b 2019-03-28 stsp }
4669 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4670 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4671 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4672 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4673 a129376b 2019-03-28 stsp goto done;
4674 a129376b 2019-03-28 stsp }
4675 a129376b 2019-03-28 stsp } else {
4676 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4677 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4678 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4679 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4680 a129376b 2019-03-28 stsp goto done;
4681 a129376b 2019-03-28 stsp }
4682 a129376b 2019-03-28 stsp } else {
4683 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4684 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4685 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4686 a129376b 2019-03-28 stsp goto done;
4687 a129376b 2019-03-28 stsp }
4688 a129376b 2019-03-28 stsp }
4689 a129376b 2019-03-28 stsp }
4690 a129376b 2019-03-28 stsp
4691 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4692 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4693 a9fa2909 2019-07-27 stsp if (err) {
4694 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4695 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4696 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4697 a9fa2909 2019-07-27 stsp goto done;
4698 a9fa2909 2019-07-27 stsp } else {
4699 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4700 a9fa2909 2019-07-27 stsp if (err)
4701 a9fa2909 2019-07-27 stsp goto done;
4702 a9fa2909 2019-07-27 stsp
4703 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4704 1233e6b6 2020-10-19 stsp if (err)
4705 a9fa2909 2019-07-27 stsp goto done;
4706 a9fa2909 2019-07-27 stsp
4707 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4708 1233e6b6 2020-10-19 stsp free(te_name);
4709 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4710 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4711 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4712 a9fa2909 2019-07-27 stsp goto done;
4713 a9fa2909 2019-07-27 stsp }
4714 a129376b 2019-03-28 stsp }
4715 a129376b 2019-03-28 stsp
4716 a129376b 2019-03-28 stsp switch (status) {
4717 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4718 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4719 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4720 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4721 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4722 33aa809d 2019-08-08 stsp if (err)
4723 33aa809d 2019-08-08 stsp goto done;
4724 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4725 33aa809d 2019-08-08 stsp break;
4726 33aa809d 2019-08-08 stsp }
4727 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4728 1f1abb7e 2019-08-08 stsp ie->path);
4729 1ee397ad 2019-07-12 stsp if (err)
4730 1ee397ad 2019-07-12 stsp goto done;
4731 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4732 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4733 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4734 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4735 10604dce 2021-09-24 thomas relpath) == -1) {
4736 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4737 10604dce 2021-09-24 thomas goto done;
4738 10604dce 2021-09-24 thomas }
4739 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4740 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4741 10604dce 2021-09-24 thomas ondisk_path);
4742 10604dce 2021-09-24 thomas break;
4743 10604dce 2021-09-24 thomas }
4744 10604dce 2021-09-24 thomas }
4745 a129376b 2019-03-28 stsp break;
4746 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4747 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4748 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4749 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4750 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4751 33aa809d 2019-08-08 stsp if (err)
4752 33aa809d 2019-08-08 stsp goto done;
4753 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4754 33aa809d 2019-08-08 stsp break;
4755 33aa809d 2019-08-08 stsp }
4756 33aa809d 2019-08-08 stsp /* fall through */
4757 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4758 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4759 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4760 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4761 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4762 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4763 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4764 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4765 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4766 24278f30 2019-08-03 stsp } else
4767 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4768 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4769 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4770 a129376b 2019-03-28 stsp if (err)
4771 65084dad 2019-08-08 stsp goto done;
4772 65084dad 2019-08-08 stsp
4773 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4774 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4775 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4776 a129376b 2019-03-28 stsp goto done;
4777 65084dad 2019-08-08 stsp }
4778 65084dad 2019-08-08 stsp
4779 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4780 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4781 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4782 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4783 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4784 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4785 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4786 33aa809d 2019-08-08 stsp break;
4787 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4788 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4789 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4790 369fd7e5 2020-07-23 stsp path_content);
4791 369fd7e5 2020-07-23 stsp break;
4792 369fd7e5 2020-07-23 stsp }
4793 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4794 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4795 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4796 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4797 369fd7e5 2020-07-23 stsp } else {
4798 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4799 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4800 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4801 369fd7e5 2020-07-23 stsp goto done;
4802 369fd7e5 2020-07-23 stsp }
4803 33aa809d 2019-08-08 stsp }
4804 33aa809d 2019-08-08 stsp } else {
4805 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4806 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4807 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4808 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4809 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4810 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4811 2e1fa222 2020-07-23 stsp } else {
4812 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4813 2e1fa222 2020-07-23 stsp ie->path,
4814 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4815 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4816 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4817 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4818 2e1fa222 2020-07-23 stsp }
4819 a129376b 2019-03-28 stsp if (err)
4820 a129376b 2019-03-28 stsp goto done;
4821 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4822 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4823 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4824 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4825 437adc9d 2020-12-10 yzhong blob->id.sha1,
4826 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4827 2e1fa222 2020-07-23 stsp if (err)
4828 2e1fa222 2020-07-23 stsp goto done;
4829 2e1fa222 2020-07-23 stsp }
4830 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4831 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4832 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4833 33aa809d 2019-08-08 stsp }
4834 a129376b 2019-03-28 stsp }
4835 a129376b 2019-03-28 stsp break;
4836 e20a8b6f 2019-06-04 stsp }
4837 a129376b 2019-03-28 stsp default:
4838 1f1abb7e 2019-08-08 stsp break;
4839 a129376b 2019-03-28 stsp }
4840 a129376b 2019-03-28 stsp done:
4841 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4842 33aa809d 2019-08-08 stsp free(path_content);
4843 e20a8b6f 2019-06-04 stsp free(parent_path);
4844 a129376b 2019-03-28 stsp free(tree_path);
4845 a129376b 2019-03-28 stsp if (blob)
4846 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4847 a129376b 2019-03-28 stsp if (tree)
4848 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4849 a129376b 2019-03-28 stsp free(tree_id);
4850 e20a8b6f 2019-06-04 stsp return err;
4851 e20a8b6f 2019-06-04 stsp }
4852 e20a8b6f 2019-06-04 stsp
4853 e20a8b6f 2019-06-04 stsp const struct got_error *
4854 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4855 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4856 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4857 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4858 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4859 e20a8b6f 2019-06-04 stsp {
4860 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4861 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4862 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4863 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4864 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4865 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4866 e20a8b6f 2019-06-04 stsp
4867 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4868 e20a8b6f 2019-06-04 stsp if (err)
4869 e20a8b6f 2019-06-04 stsp return err;
4870 e20a8b6f 2019-06-04 stsp
4871 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4872 e20a8b6f 2019-06-04 stsp if (err)
4873 e20a8b6f 2019-06-04 stsp goto done;
4874 e20a8b6f 2019-06-04 stsp
4875 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4876 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4877 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4878 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4879 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4880 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4881 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4882 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4883 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4884 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4885 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4886 e20a8b6f 2019-06-04 stsp if (err)
4887 af12c6b9 2019-06-04 stsp break;
4888 e20a8b6f 2019-06-04 stsp }
4889 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4890 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4891 e20a8b6f 2019-06-04 stsp err = sync_err;
4892 af12c6b9 2019-06-04 stsp done:
4893 fb399478 2019-07-12 stsp free(fileindex_path);
4894 a129376b 2019-03-28 stsp if (fileindex)
4895 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4896 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4897 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4898 a129376b 2019-03-28 stsp err = unlockerr;
4899 c4296144 2019-05-09 stsp return err;
4900 c4296144 2019-05-09 stsp }
4901 c4296144 2019-05-09 stsp
4902 cf066bf8 2019-05-09 stsp static void
4903 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4904 cf066bf8 2019-05-09 stsp {
4905 24519714 2019-05-09 stsp free(ct->path);
4906 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4907 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4908 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4909 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4910 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4911 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4912 cf066bf8 2019-05-09 stsp free(ct);
4913 cf066bf8 2019-05-09 stsp }
4914 24519714 2019-05-09 stsp
4915 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4916 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4917 24519714 2019-05-09 stsp struct got_repository *repo;
4918 24519714 2019-05-09 stsp struct got_worktree *worktree;
4919 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4920 5f8a88c6 2019-08-03 stsp int have_staged_files;
4921 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4922 24519714 2019-05-09 stsp };
4923 cf066bf8 2019-05-09 stsp
4924 c4296144 2019-05-09 stsp static const struct got_error *
4925 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4926 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4927 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4928 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4929 c4296144 2019-05-09 stsp {
4930 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4931 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4932 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4933 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4934 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4935 768aea60 2019-05-09 stsp struct stat sb;
4936 c4296144 2019-05-09 stsp
4937 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4938 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4939 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4940 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4941 5f8a88c6 2019-08-03 stsp return NULL;
4942 5f8a88c6 2019-08-03 stsp } else {
4943 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4944 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4945 c4296144 2019-05-09 stsp
4946 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4947 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4948 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4949 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4950 5f8a88c6 2019-08-03 stsp return NULL;
4951 5f8a88c6 2019-08-03 stsp }
4952 0b5cc0d6 2019-05-09 stsp
4953 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4954 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4955 036813ee 2019-05-09 stsp goto done;
4956 036813ee 2019-05-09 stsp }
4957 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4958 036813ee 2019-05-09 stsp parent_path = strdup("");
4959 69960a46 2019-05-09 stsp if (parent_path == NULL)
4960 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4961 69960a46 2019-05-09 stsp } else {
4962 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4963 69960a46 2019-05-09 stsp if (err)
4964 69960a46 2019-05-09 stsp return err;
4965 69960a46 2019-05-09 stsp }
4966 c4296144 2019-05-09 stsp
4967 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4968 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4969 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4970 c4296144 2019-05-09 stsp goto done;
4971 768aea60 2019-05-09 stsp }
4972 768aea60 2019-05-09 stsp
4973 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4974 768aea60 2019-05-09 stsp relpath) == -1) {
4975 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4976 768aea60 2019-05-09 stsp goto done;
4977 768aea60 2019-05-09 stsp }
4978 0aeb8099 2020-07-23 stsp
4979 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4980 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4981 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4982 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4983 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4984 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4985 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4986 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4987 0aeb8099 2020-07-23 stsp break;
4988 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4989 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4990 0aeb8099 2020-07-23 stsp break;
4991 0aeb8099 2020-07-23 stsp default:
4992 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4993 0aeb8099 2020-07-23 stsp goto done;
4994 0aeb8099 2020-07-23 stsp }
4995 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4996 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4997 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4998 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4999 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5000 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5001 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5002 12463d8b 2019-12-13 stsp ct->ondisk_path);
5003 12463d8b 2019-12-13 stsp goto done;
5004 12463d8b 2019-12-13 stsp }
5005 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5006 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5007 768aea60 2019-05-09 stsp goto done;
5008 768aea60 2019-05-09 stsp }
5009 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5010 c4296144 2019-05-09 stsp }
5011 c4296144 2019-05-09 stsp
5012 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5013 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5014 44d03001 2019-05-09 stsp relpath) == -1) {
5015 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5016 44d03001 2019-05-09 stsp goto done;
5017 44d03001 2019-05-09 stsp }
5018 44d03001 2019-05-09 stsp
5019 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5020 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5021 35213c7c 2020-07-23 stsp int is_bad_symlink;
5022 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5023 35213c7c 2020-07-23 stsp ssize_t target_len;
5024 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5025 35213c7c 2020-07-23 stsp sizeof(target_path));
5026 35213c7c 2020-07-23 stsp if (target_len == -1) {
5027 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5028 35213c7c 2020-07-23 stsp ct->ondisk_path);
5029 35213c7c 2020-07-23 stsp goto done;
5030 35213c7c 2020-07-23 stsp }
5031 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5032 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5033 35213c7c 2020-07-23 stsp if (err)
5034 35213c7c 2020-07-23 stsp goto done;
5035 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5036 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5037 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5038 35213c7c 2020-07-23 stsp goto done;
5039 35213c7c 2020-07-23 stsp }
5040 35213c7c 2020-07-23 stsp }
5041 35213c7c 2020-07-23 stsp
5042 35213c7c 2020-07-23 stsp
5043 cf066bf8 2019-05-09 stsp ct->status = status;
5044 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5045 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5046 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5047 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5048 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5049 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5050 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5051 b416585c 2019-05-13 stsp goto done;
5052 b416585c 2019-05-13 stsp }
5053 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5054 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5055 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5056 f0b75401 2019-08-03 stsp goto done;
5057 f0b75401 2019-08-03 stsp }
5058 f0b75401 2019-08-03 stsp }
5059 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5060 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5061 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5062 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5063 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5064 036813ee 2019-05-09 stsp goto done;
5065 036813ee 2019-05-09 stsp }
5066 ca2503ea 2019-05-09 stsp }
5067 24519714 2019-05-09 stsp ct->path = strdup(path);
5068 24519714 2019-05-09 stsp if (ct->path == NULL) {
5069 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5070 24519714 2019-05-09 stsp goto done;
5071 24519714 2019-05-09 stsp }
5072 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5073 c4296144 2019-05-09 stsp done:
5074 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5075 ed175427 2019-05-09 stsp free_commitable(ct);
5076 24519714 2019-05-09 stsp free(parent_path);
5077 036813ee 2019-05-09 stsp free(path);
5078 a129376b 2019-03-28 stsp return err;
5079 a129376b 2019-03-28 stsp }
5080 c4296144 2019-05-09 stsp
5081 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5082 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5083 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5084 036813ee 2019-05-09 stsp struct got_repository *);
5085 ed175427 2019-05-09 stsp
5086 ed175427 2019-05-09 stsp static const struct got_error *
5087 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5088 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5089 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5090 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5091 afa376bf 2019-05-09 stsp struct got_repository *repo)
5092 ed175427 2019-05-09 stsp {
5093 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5094 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5095 036813ee 2019-05-09 stsp char *subpath;
5096 ed175427 2019-05-09 stsp
5097 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5098 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5099 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5100 ed175427 2019-05-09 stsp
5101 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5102 ed175427 2019-05-09 stsp if (err)
5103 ed175427 2019-05-09 stsp return err;
5104 ed175427 2019-05-09 stsp
5105 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5106 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5107 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5108 036813ee 2019-05-09 stsp free(subpath);
5109 036813ee 2019-05-09 stsp return err;
5110 036813ee 2019-05-09 stsp }
5111 ed175427 2019-05-09 stsp
5112 036813ee 2019-05-09 stsp static const struct got_error *
5113 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5114 036813ee 2019-05-09 stsp {
5115 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5116 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5117 ed175427 2019-05-09 stsp
5118 036813ee 2019-05-09 stsp *match = 0;
5119 ed175427 2019-05-09 stsp
5120 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5121 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5122 0f63689d 2019-05-10 stsp return NULL;
5123 036813ee 2019-05-09 stsp }
5124 0b5cc0d6 2019-05-09 stsp
5125 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5126 0f63689d 2019-05-10 stsp if (err)
5127 0f63689d 2019-05-10 stsp return err;
5128 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5129 036813ee 2019-05-09 stsp free(ct_parent_path);
5130 036813ee 2019-05-09 stsp return err;
5131 036813ee 2019-05-09 stsp }
5132 0b5cc0d6 2019-05-09 stsp
5133 768aea60 2019-05-09 stsp static mode_t
5134 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5135 768aea60 2019-05-09 stsp {
5136 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5137 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5138 3d9a4ec4 2020-07-23 stsp
5139 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5140 768aea60 2019-05-09 stsp }
5141 768aea60 2019-05-09 stsp
5142 036813ee 2019-05-09 stsp static const struct got_error *
5143 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5144 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5145 036813ee 2019-05-09 stsp {
5146 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5147 ca2503ea 2019-05-09 stsp
5148 036813ee 2019-05-09 stsp *new_te = NULL;
5149 0b5cc0d6 2019-05-09 stsp
5150 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5151 036813ee 2019-05-09 stsp if (err)
5152 036813ee 2019-05-09 stsp goto done;
5153 0b5cc0d6 2019-05-09 stsp
5154 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5155 036813ee 2019-05-09 stsp
5156 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5157 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5158 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5159 5f8a88c6 2019-08-03 stsp else
5160 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5161 036813ee 2019-05-09 stsp done:
5162 036813ee 2019-05-09 stsp if (err && *new_te) {
5163 56e0773d 2019-11-28 stsp free(*new_te);
5164 036813ee 2019-05-09 stsp *new_te = NULL;
5165 036813ee 2019-05-09 stsp }
5166 036813ee 2019-05-09 stsp return err;
5167 036813ee 2019-05-09 stsp }
5168 036813ee 2019-05-09 stsp
5169 036813ee 2019-05-09 stsp static const struct got_error *
5170 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5171 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5172 036813ee 2019-05-09 stsp {
5173 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5174 102b254e 2020-10-19 stsp char *ct_name = NULL;
5175 036813ee 2019-05-09 stsp
5176 036813ee 2019-05-09 stsp *new_te = NULL;
5177 036813ee 2019-05-09 stsp
5178 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5179 036813ee 2019-05-09 stsp if (*new_te == NULL)
5180 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5181 036813ee 2019-05-09 stsp
5182 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5183 102b254e 2020-10-19 stsp if (err)
5184 036813ee 2019-05-09 stsp goto done;
5185 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5186 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5187 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5188 036813ee 2019-05-09 stsp goto done;
5189 036813ee 2019-05-09 stsp }
5190 036813ee 2019-05-09 stsp
5191 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5192 036813ee 2019-05-09 stsp
5193 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5194 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5195 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5196 5f8a88c6 2019-08-03 stsp else
5197 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5198 036813ee 2019-05-09 stsp done:
5199 102b254e 2020-10-19 stsp free(ct_name);
5200 036813ee 2019-05-09 stsp if (err && *new_te) {
5201 56e0773d 2019-11-28 stsp free(*new_te);
5202 036813ee 2019-05-09 stsp *new_te = NULL;
5203 036813ee 2019-05-09 stsp }
5204 036813ee 2019-05-09 stsp return err;
5205 036813ee 2019-05-09 stsp }
5206 036813ee 2019-05-09 stsp
5207 036813ee 2019-05-09 stsp static const struct got_error *
5208 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5209 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5210 036813ee 2019-05-09 stsp {
5211 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5212 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5213 036813ee 2019-05-09 stsp
5214 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5215 036813ee 2019-05-09 stsp if (err)
5216 036813ee 2019-05-09 stsp return err;
5217 036813ee 2019-05-09 stsp if (new_pe == NULL)
5218 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5219 036813ee 2019-05-09 stsp return NULL;
5220 afa376bf 2019-05-09 stsp }
5221 afa376bf 2019-05-09 stsp
5222 afa376bf 2019-05-09 stsp static const struct got_error *
5223 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5224 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5225 afa376bf 2019-05-09 stsp {
5226 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5227 5f8a88c6 2019-08-03 stsp unsigned char status;
5228 ae1e948a 2021-09-28 thomas
5229 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5230 ae1e948a 2021-09-28 thomas return NULL;
5231 5f8a88c6 2019-08-03 stsp
5232 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5233 afa376bf 2019-05-09 stsp ct_path++;
5234 5f8a88c6 2019-08-03 stsp
5235 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5236 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5237 5f8a88c6 2019-08-03 stsp else
5238 5f8a88c6 2019-08-03 stsp status = ct->status;
5239 5f8a88c6 2019-08-03 stsp
5240 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5241 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5242 036813ee 2019-05-09 stsp }
5243 036813ee 2019-05-09 stsp
5244 036813ee 2019-05-09 stsp static const struct got_error *
5245 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5246 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5247 44d03001 2019-05-09 stsp {
5248 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5249 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5250 44d03001 2019-05-09 stsp char *te_path;
5251 44d03001 2019-05-09 stsp
5252 44d03001 2019-05-09 stsp *modified = 0;
5253 44d03001 2019-05-09 stsp
5254 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5255 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5256 44d03001 2019-05-09 stsp te->name) == -1)
5257 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5258 44d03001 2019-05-09 stsp
5259 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5260 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5261 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5262 44d03001 2019-05-09 stsp strlen(te_path));
5263 62d463ca 2020-10-20 naddy if (*modified)
5264 44d03001 2019-05-09 stsp break;
5265 44d03001 2019-05-09 stsp }
5266 44d03001 2019-05-09 stsp
5267 44d03001 2019-05-09 stsp free(te_path);
5268 44d03001 2019-05-09 stsp return err;
5269 44d03001 2019-05-09 stsp }
5270 44d03001 2019-05-09 stsp
5271 44d03001 2019-05-09 stsp static const struct got_error *
5272 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5273 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5274 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5275 036813ee 2019-05-09 stsp {
5276 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5277 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5278 036813ee 2019-05-09 stsp
5279 036813ee 2019-05-09 stsp *ctp = NULL;
5280 036813ee 2019-05-09 stsp
5281 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5282 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5283 036813ee 2019-05-09 stsp char *ct_name = NULL;
5284 036813ee 2019-05-09 stsp int path_matches;
5285 036813ee 2019-05-09 stsp
5286 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5287 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5288 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5289 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5290 5f8a88c6 2019-08-03 stsp continue;
5291 5f8a88c6 2019-08-03 stsp } else {
5292 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5293 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5294 5f8a88c6 2019-08-03 stsp continue;
5295 5f8a88c6 2019-08-03 stsp }
5296 036813ee 2019-05-09 stsp
5297 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5298 036813ee 2019-05-09 stsp continue;
5299 036813ee 2019-05-09 stsp
5300 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5301 62d463ca 2020-10-20 naddy if (err)
5302 036813ee 2019-05-09 stsp return err;
5303 036813ee 2019-05-09 stsp if (!path_matches)
5304 036813ee 2019-05-09 stsp continue;
5305 036813ee 2019-05-09 stsp
5306 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5307 d34b633e 2020-10-19 stsp if (err)
5308 d34b633e 2020-10-19 stsp return err;
5309 d34b633e 2020-10-19 stsp
5310 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5311 d34b633e 2020-10-19 stsp free(ct_name);
5312 036813ee 2019-05-09 stsp continue;
5313 d34b633e 2020-10-19 stsp }
5314 d34b633e 2020-10-19 stsp free(ct_name);
5315 036813ee 2019-05-09 stsp
5316 036813ee 2019-05-09 stsp *ctp = ct;
5317 036813ee 2019-05-09 stsp break;
5318 036813ee 2019-05-09 stsp }
5319 2b496619 2019-07-10 stsp
5320 2b496619 2019-07-10 stsp return err;
5321 2b496619 2019-07-10 stsp }
5322 2b496619 2019-07-10 stsp
5323 2b496619 2019-07-10 stsp static const struct got_error *
5324 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5325 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5326 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5327 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5328 2b496619 2019-07-10 stsp struct got_repository *repo)
5329 2b496619 2019-07-10 stsp {
5330 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5331 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5332 2b496619 2019-07-10 stsp char *subtree_path;
5333 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5334 ba580f68 2020-03-22 stsp int nentries;
5335 2b496619 2019-07-10 stsp
5336 2b496619 2019-07-10 stsp *new_tep = NULL;
5337 2b496619 2019-07-10 stsp
5338 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5339 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5340 2b496619 2019-07-10 stsp child_path) == -1)
5341 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5342 036813ee 2019-05-09 stsp
5343 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5344 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5345 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5346 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5347 56e0773d 2019-11-28 stsp
5348 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5349 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5350 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5351 2b496619 2019-07-10 stsp goto done;
5352 2b496619 2019-07-10 stsp }
5353 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5354 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5355 2b496619 2019-07-10 stsp if (err) {
5356 56e0773d 2019-11-28 stsp free(new_te);
5357 2b496619 2019-07-10 stsp goto done;
5358 2b496619 2019-07-10 stsp }
5359 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5360 2b496619 2019-07-10 stsp done:
5361 56e0773d 2019-11-28 stsp free(id);
5362 2b496619 2019-07-10 stsp free(subtree_path);
5363 2b496619 2019-07-10 stsp if (err == NULL)
5364 2b496619 2019-07-10 stsp *new_tep = new_te;
5365 036813ee 2019-05-09 stsp return err;
5366 036813ee 2019-05-09 stsp }
5367 036813ee 2019-05-09 stsp
5368 036813ee 2019-05-09 stsp static const struct got_error *
5369 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5370 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5371 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5372 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5373 036813ee 2019-05-09 stsp struct got_repository *repo)
5374 036813ee 2019-05-09 stsp {
5375 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5376 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5377 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5378 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5379 036813ee 2019-05-09 stsp
5380 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5381 ba580f68 2020-03-22 stsp *nentries = 0;
5382 036813ee 2019-05-09 stsp
5383 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5384 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5385 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5386 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5387 036813ee 2019-05-09 stsp
5388 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5389 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5390 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5391 036813ee 2019-05-09 stsp continue;
5392 036813ee 2019-05-09 stsp
5393 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5394 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5395 036813ee 2019-05-09 stsp continue;
5396 036813ee 2019-05-09 stsp
5397 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5398 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5399 036813ee 2019-05-09 stsp if (err)
5400 036813ee 2019-05-09 stsp goto done;
5401 036813ee 2019-05-09 stsp
5402 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5403 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5404 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5405 036813ee 2019-05-09 stsp if (err)
5406 036813ee 2019-05-09 stsp goto done;
5407 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5408 afa376bf 2019-05-09 stsp if (err)
5409 afa376bf 2019-05-09 stsp goto done;
5410 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5411 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5412 2b496619 2019-07-10 stsp if (err)
5413 2f51b5b3 2019-05-09 stsp goto done;
5414 ba580f68 2020-03-22 stsp (*nentries)++;
5415 2b496619 2019-07-10 stsp } else {
5416 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5417 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5418 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5419 2b496619 2019-07-10 stsp == NULL) {
5420 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5421 2b496619 2019-07-10 stsp child_path, path_base_tree,
5422 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5423 2b496619 2019-07-10 stsp repo);
5424 2b496619 2019-07-10 stsp if (err)
5425 2b496619 2019-07-10 stsp goto done;
5426 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5427 2b496619 2019-07-10 stsp if (err)
5428 2b496619 2019-07-10 stsp goto done;
5429 ba580f68 2020-03-22 stsp (*nentries)++;
5430 9ba0479c 2019-05-10 stsp }
5431 ed175427 2019-05-09 stsp }
5432 2f51b5b3 2019-05-09 stsp }
5433 2f51b5b3 2019-05-09 stsp
5434 2f51b5b3 2019-05-09 stsp if (base_tree) {
5435 56e0773d 2019-11-28 stsp int i, nbase_entries;
5436 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5437 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5438 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5439 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5440 63c5ca5d 2019-08-24 stsp
5441 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5442 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5443 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5444 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5445 63c5ca5d 2019-08-24 stsp if (err)
5446 63c5ca5d 2019-08-24 stsp goto done;
5447 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5448 63c5ca5d 2019-08-24 stsp if (err)
5449 63c5ca5d 2019-08-24 stsp goto done;
5450 ba580f68 2020-03-22 stsp (*nentries)++;
5451 63c5ca5d 2019-08-24 stsp continue;
5452 63c5ca5d 2019-08-24 stsp }
5453 2f51b5b3 2019-05-09 stsp
5454 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5455 44d03001 2019-05-09 stsp int modified;
5456 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5457 036813ee 2019-05-09 stsp if (err)
5458 036813ee 2019-05-09 stsp goto done;
5459 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5460 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5461 2f51b5b3 2019-05-09 stsp if (err)
5462 2f51b5b3 2019-05-09 stsp goto done;
5463 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5464 44d03001 2019-05-09 stsp if (modified) {
5465 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5466 ba580f68 2020-03-22 stsp int nsubentries;
5467 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5468 ba580f68 2020-03-22 stsp &nsubentries, te,
5469 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5470 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5471 44d03001 2019-05-09 stsp if (err)
5472 44d03001 2019-05-09 stsp goto done;
5473 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5474 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5475 ba580f68 2020-03-22 stsp free(new_id);
5476 ba580f68 2020-03-22 stsp continue;
5477 ba580f68 2020-03-22 stsp }
5478 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5479 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5480 56e0773d 2019-11-28 stsp free(new_id);
5481 44d03001 2019-05-09 stsp }
5482 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5483 036813ee 2019-05-09 stsp if (err)
5484 036813ee 2019-05-09 stsp goto done;
5485 ba580f68 2020-03-22 stsp (*nentries)++;
5486 2f51b5b3 2019-05-09 stsp continue;
5487 036813ee 2019-05-09 stsp }
5488 2f51b5b3 2019-05-09 stsp
5489 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5490 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5491 f66c734c 2019-09-22 stsp if (err)
5492 f66c734c 2019-09-22 stsp goto done;
5493 2f51b5b3 2019-05-09 stsp if (ct) {
5494 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5495 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5496 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5497 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5498 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5499 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5500 2f51b5b3 2019-05-09 stsp if (err)
5501 2f51b5b3 2019-05-09 stsp goto done;
5502 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5503 2f51b5b3 2019-05-09 stsp if (err)
5504 2f51b5b3 2019-05-09 stsp goto done;
5505 ba580f68 2020-03-22 stsp (*nentries)++;
5506 2f51b5b3 2019-05-09 stsp }
5507 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5508 b416585c 2019-05-13 stsp status_arg);
5509 afa376bf 2019-05-09 stsp if (err)
5510 afa376bf 2019-05-09 stsp goto done;
5511 2f51b5b3 2019-05-09 stsp } else {
5512 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5513 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5514 2f51b5b3 2019-05-09 stsp if (err)
5515 2f51b5b3 2019-05-09 stsp goto done;
5516 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5517 2f51b5b3 2019-05-09 stsp if (err)
5518 2f51b5b3 2019-05-09 stsp goto done;
5519 ba580f68 2020-03-22 stsp (*nentries)++;
5520 2f51b5b3 2019-05-09 stsp }
5521 ca2503ea 2019-05-09 stsp }
5522 ed175427 2019-05-09 stsp }
5523 0b5cc0d6 2019-05-09 stsp
5524 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5525 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5526 ed175427 2019-05-09 stsp done:
5527 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5528 2e1fa222 2020-07-23 stsp return err;
5529 2e1fa222 2020-07-23 stsp }
5530 2e1fa222 2020-07-23 stsp
5531 2e1fa222 2020-07-23 stsp static const struct got_error *
5532 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5533 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5534 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5535 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5536 ebf99748 2019-05-09 stsp {
5537 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5538 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5539 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5540 ebf99748 2019-05-09 stsp
5541 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5542 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5543 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5544 ebf99748 2019-05-09 stsp
5545 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5546 437adc9d 2020-12-10 yzhong
5547 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5548 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5549 437adc9d 2020-12-10 yzhong if (err)
5550 437adc9d 2020-12-10 yzhong goto done;
5551 437adc9d 2020-12-10 yzhong
5552 ebf99748 2019-05-09 stsp if (ie) {
5553 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5554 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5555 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5556 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5557 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5558 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5559 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5560 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5561 437adc9d 2020-12-10 yzhong
5562 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5563 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5564 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5565 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5566 72fd46fa 2019-09-06 stsp !have_staged_files);
5567 ebf99748 2019-05-09 stsp } else
5568 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5569 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5570 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5571 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5572 72fd46fa 2019-09-06 stsp !have_staged_files);
5573 ebf99748 2019-05-09 stsp } else {
5574 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5575 ebf99748 2019-05-09 stsp if (err)
5576 437adc9d 2020-12-10 yzhong goto done;
5577 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5578 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5579 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5580 3969253a 2020-03-07 stsp if (err) {
5581 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5582 437adc9d 2020-12-10 yzhong goto done;
5583 3969253a 2020-03-07 stsp }
5584 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5585 3969253a 2020-03-07 stsp if (err) {
5586 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5587 437adc9d 2020-12-10 yzhong goto done;
5588 3969253a 2020-03-07 stsp }
5589 ebf99748 2019-05-09 stsp }
5590 437adc9d 2020-12-10 yzhong free(relpath);
5591 437adc9d 2020-12-10 yzhong relpath = NULL;
5592 ebf99748 2019-05-09 stsp }
5593 437adc9d 2020-12-10 yzhong done:
5594 437adc9d 2020-12-10 yzhong free(relpath);
5595 d56d26ce 2019-05-10 stsp return err;
5596 d56d26ce 2019-05-10 stsp }
5597 735ef5ac 2019-08-03 stsp
5598 d56d26ce 2019-05-10 stsp
5599 d56d26ce 2019-05-10 stsp static const struct got_error *
5600 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5601 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5602 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5603 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5604 735ef5ac 2019-08-03 stsp int ood_errcode)
5605 d56d26ce 2019-05-10 stsp {
5606 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5607 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5608 1a36436d 2019-06-10 stsp
5609 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5610 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5611 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5612 1a36436d 2019-06-10 stsp return NULL;
5613 9bead371 2019-07-28 stsp /*
5614 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5615 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5616 9bead371 2019-07-28 stsp */
5617 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5618 735ef5ac 2019-08-03 stsp in_repo_path);
5619 9bead371 2019-07-28 stsp if (err) {
5620 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5621 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5622 1a36436d 2019-06-10 stsp goto done;
5623 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5624 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5625 1a36436d 2019-06-10 stsp } else {
5626 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5627 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5628 735ef5ac 2019-08-03 stsp in_repo_path);
5629 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5630 1a36436d 2019-06-10 stsp goto done;
5631 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5632 1a36436d 2019-06-10 stsp }
5633 1a36436d 2019-06-10 stsp done:
5634 1a36436d 2019-06-10 stsp free(id);
5635 1a36436d 2019-06-10 stsp return err;
5636 ebf99748 2019-05-09 stsp }
5637 ebf99748 2019-05-09 stsp
5638 c4296144 2019-05-09 stsp const struct got_error *
5639 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5640 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5641 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5642 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5643 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5644 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5645 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5646 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5647 afa376bf 2019-05-09 stsp struct got_repository *repo)
5648 c4296144 2019-05-09 stsp {
5649 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5650 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5651 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5652 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5653 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5654 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5655 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5656 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5657 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5658 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5659 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5660 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5661 c4296144 2019-05-09 stsp
5662 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5663 c4296144 2019-05-09 stsp
5664 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5665 675c7539 2019-05-09 stsp
5666 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5667 588edf97 2019-05-10 stsp if (err)
5668 588edf97 2019-05-10 stsp goto done;
5669 de18fc63 2019-05-09 stsp
5670 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5671 588edf97 2019-05-10 stsp if (err)
5672 588edf97 2019-05-10 stsp goto done;
5673 588edf97 2019-05-10 stsp
5674 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5675 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5676 33ad4cbe 2019-05-12 jcs if (err)
5677 33ad4cbe 2019-05-12 jcs goto done;
5678 33ad4cbe 2019-05-12 jcs }
5679 c4296144 2019-05-09 stsp
5680 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5681 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5682 33ad4cbe 2019-05-12 jcs goto done;
5683 33ad4cbe 2019-05-12 jcs }
5684 33ad4cbe 2019-05-12 jcs
5685 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5686 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5687 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5688 cf066bf8 2019-05-09 stsp char *ondisk_path;
5689 cf066bf8 2019-05-09 stsp
5690 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5691 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5692 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5693 5f8a88c6 2019-08-03 stsp continue;
5694 5f8a88c6 2019-08-03 stsp
5695 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5696 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5697 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5698 cf066bf8 2019-05-09 stsp continue;
5699 cf066bf8 2019-05-09 stsp
5700 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5701 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5702 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5703 cf066bf8 2019-05-09 stsp goto done;
5704 cf066bf8 2019-05-09 stsp }
5705 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5706 2e1fa222 2020-07-23 stsp free(ondisk_path);
5707 2e1fa222 2020-07-23 stsp if (err)
5708 cf066bf8 2019-05-09 stsp goto done;
5709 cf066bf8 2019-05-09 stsp }
5710 036813ee 2019-05-09 stsp
5711 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5712 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5713 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5714 036813ee 2019-05-09 stsp if (err)
5715 036813ee 2019-05-09 stsp goto done;
5716 036813ee 2019-05-09 stsp
5717 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5718 de18fc63 2019-05-09 stsp if (err)
5719 de18fc63 2019-05-09 stsp goto done;
5720 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5721 10604dce 2021-09-24 thomas nparents++;
5722 10604dce 2021-09-24 thomas if (parent_id2) {
5723 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
5724 10604dce 2021-09-24 thomas if (err)
5725 10604dce 2021-09-24 thomas goto done;
5726 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5727 10604dce 2021-09-24 thomas nparents++;
5728 10604dce 2021-09-24 thomas }
5729 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5730 10604dce 2021-09-24 thomas nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5731 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5732 33ad4cbe 2019-05-12 jcs free(logmsg);
5733 9d40349a 2019-05-09 stsp if (err)
5734 9d40349a 2019-05-09 stsp goto done;
5735 9d40349a 2019-05-09 stsp
5736 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5737 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5738 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5739 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5740 09f5bd90 2019-05-09 stsp goto done;
5741 09f5bd90 2019-05-09 stsp }
5742 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5743 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5744 09f5bd90 2019-05-09 stsp if (err)
5745 09f5bd90 2019-05-09 stsp goto done;
5746 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5747 ebf99748 2019-05-09 stsp if (err)
5748 ebf99748 2019-05-09 stsp goto done;
5749 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5750 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5751 09f5bd90 2019-05-09 stsp goto done;
5752 09f5bd90 2019-05-09 stsp }
5753 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5754 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5755 f2c16586 2019-05-09 stsp if (err)
5756 f2c16586 2019-05-09 stsp goto done;
5757 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5758 f2c16586 2019-05-09 stsp if (err)
5759 f2c16586 2019-05-09 stsp goto done;
5760 f2c16586 2019-05-09 stsp
5761 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5762 09f5bd90 2019-05-09 stsp if (err)
5763 09f5bd90 2019-05-09 stsp goto done;
5764 09f5bd90 2019-05-09 stsp
5765 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5766 ebf99748 2019-05-09 stsp if (err)
5767 ebf99748 2019-05-09 stsp goto done;
5768 c4296144 2019-05-09 stsp done:
5769 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
5770 588edf97 2019-05-10 stsp if (head_tree)
5771 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5772 588edf97 2019-05-10 stsp if (head_commit)
5773 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5774 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5775 2f17228e 2019-05-12 stsp if (head_ref2) {
5776 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5777 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5778 2f17228e 2019-05-12 stsp err = unlockerr;
5779 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5780 39cd0ff6 2019-07-12 stsp }
5781 39cd0ff6 2019-07-12 stsp return err;
5782 39cd0ff6 2019-07-12 stsp }
5783 5c1e53bc 2019-07-28 stsp
5784 5c1e53bc 2019-07-28 stsp static const struct got_error *
5785 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5786 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5787 5c1e53bc 2019-07-28 stsp {
5788 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5789 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5790 39cd0ff6 2019-07-12 stsp
5791 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5792 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5793 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5794 5c1e53bc 2019-07-28 stsp
5795 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5796 5c1e53bc 2019-07-28 stsp ct_path++;
5797 5c1e53bc 2019-07-28 stsp
5798 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5799 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5800 5c1e53bc 2019-07-28 stsp break;
5801 5c1e53bc 2019-07-28 stsp }
5802 5c1e53bc 2019-07-28 stsp
5803 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5804 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5805 5c1e53bc 2019-07-28 stsp
5806 5c1e53bc 2019-07-28 stsp return NULL;
5807 5c1e53bc 2019-07-28 stsp }
5808 5c1e53bc 2019-07-28 stsp
5809 f0b75401 2019-08-03 stsp static const struct got_error *
5810 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5811 f0b75401 2019-08-03 stsp {
5812 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5813 f0b75401 2019-08-03 stsp
5814 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5815 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5816 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5817 f0b75401 2019-08-03 stsp }
5818 f0b75401 2019-08-03 stsp
5819 f0b75401 2019-08-03 stsp return NULL;
5820 f0b75401 2019-08-03 stsp }
5821 f0b75401 2019-08-03 stsp
5822 5f8a88c6 2019-08-03 stsp static const struct got_error *
5823 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5824 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5825 5f8a88c6 2019-08-03 stsp {
5826 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5827 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5828 5f8a88c6 2019-08-03 stsp
5829 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5830 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5831 5f8a88c6 2019-08-03 stsp continue;
5832 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5833 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5834 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5835 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5836 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5837 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5838 5f8a88c6 2019-08-03 stsp }
5839 5f8a88c6 2019-08-03 stsp
5840 5f8a88c6 2019-08-03 stsp return NULL;
5841 5f8a88c6 2019-08-03 stsp }
5842 5f8a88c6 2019-08-03 stsp
5843 39cd0ff6 2019-07-12 stsp const struct got_error *
5844 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5845 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5846 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5847 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5848 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5849 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5850 39cd0ff6 2019-07-12 stsp {
5851 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5852 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5853 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5854 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5855 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5856 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5857 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5858 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5859 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5860 39cd0ff6 2019-07-12 stsp
5861 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5862 39cd0ff6 2019-07-12 stsp
5863 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5864 39cd0ff6 2019-07-12 stsp
5865 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5866 39cd0ff6 2019-07-12 stsp if (err)
5867 39cd0ff6 2019-07-12 stsp goto done;
5868 39cd0ff6 2019-07-12 stsp
5869 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5870 39cd0ff6 2019-07-12 stsp if (err)
5871 39cd0ff6 2019-07-12 stsp goto done;
5872 39cd0ff6 2019-07-12 stsp
5873 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5874 39cd0ff6 2019-07-12 stsp if (err)
5875 39cd0ff6 2019-07-12 stsp goto done;
5876 39cd0ff6 2019-07-12 stsp
5877 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5878 39cd0ff6 2019-07-12 stsp if (err)
5879 39cd0ff6 2019-07-12 stsp goto done;
5880 39cd0ff6 2019-07-12 stsp
5881 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5882 5f8a88c6 2019-08-03 stsp &have_staged_files);
5883 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5884 5f8a88c6 2019-08-03 stsp goto done;
5885 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5886 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5887 5f8a88c6 2019-08-03 stsp if (err)
5888 5f8a88c6 2019-08-03 stsp goto done;
5889 5f8a88c6 2019-08-03 stsp }
5890 5f8a88c6 2019-08-03 stsp
5891 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5892 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5893 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5894 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5895 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5896 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5897 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5898 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5899 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5900 5c1e53bc 2019-07-28 stsp if (err)
5901 5c1e53bc 2019-07-28 stsp goto done;
5902 5c1e53bc 2019-07-28 stsp }
5903 39cd0ff6 2019-07-12 stsp
5904 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5905 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5906 39cd0ff6 2019-07-12 stsp goto done;
5907 5c1e53bc 2019-07-28 stsp }
5908 5c1e53bc 2019-07-28 stsp
5909 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5910 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5911 5c1e53bc 2019-07-28 stsp if (err)
5912 5c1e53bc 2019-07-28 stsp goto done;
5913 39cd0ff6 2019-07-12 stsp }
5914 f0b75401 2019-08-03 stsp
5915 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5916 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5917 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5918 f0b75401 2019-08-03 stsp
5919 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5920 f0b75401 2019-08-03 stsp ct_path++;
5921 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5922 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5923 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5924 b50cabdf 2019-07-12 stsp if (err)
5925 b50cabdf 2019-07-12 stsp goto done;
5926 f0b75401 2019-08-03 stsp
5927 b50cabdf 2019-07-12 stsp }
5928 b50cabdf 2019-07-12 stsp
5929 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5930 10604dce 2021-09-24 thomas head_commit_id, NULL, worktree, author, committer,
5931 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5932 39cd0ff6 2019-07-12 stsp if (err)
5933 39cd0ff6 2019-07-12 stsp goto done;
5934 39cd0ff6 2019-07-12 stsp
5935 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5936 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5937 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5938 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5939 39cd0ff6 2019-07-12 stsp err = sync_err;
5940 39cd0ff6 2019-07-12 stsp done:
5941 39cd0ff6 2019-07-12 stsp if (fileindex)
5942 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5943 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5944 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5945 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5946 39cd0ff6 2019-07-12 stsp err = unlockerr;
5947 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5948 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5949 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5950 39cd0ff6 2019-07-12 stsp }
5951 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5952 c4296144 2019-05-09 stsp return err;
5953 8656d6c4 2019-05-20 stsp }
5954 8656d6c4 2019-05-20 stsp
5955 8656d6c4 2019-05-20 stsp const char *
5956 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5957 8656d6c4 2019-05-20 stsp {
5958 8656d6c4 2019-05-20 stsp return ct->path;
5959 8656d6c4 2019-05-20 stsp }
5960 8656d6c4 2019-05-20 stsp
5961 8656d6c4 2019-05-20 stsp unsigned int
5962 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5963 8656d6c4 2019-05-20 stsp {
5964 8656d6c4 2019-05-20 stsp return ct->status;
5965 818c7501 2019-07-11 stsp }
5966 818c7501 2019-07-11 stsp
5967 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5968 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5969 818c7501 2019-07-11 stsp struct got_repository *repo;
5970 818c7501 2019-07-11 stsp };
5971 818c7501 2019-07-11 stsp
5972 818c7501 2019-07-11 stsp static const struct got_error *
5973 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5974 818c7501 2019-07-11 stsp {
5975 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5976 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5977 818c7501 2019-07-11 stsp unsigned char status;
5978 818c7501 2019-07-11 stsp struct stat sb;
5979 818c7501 2019-07-11 stsp char *ondisk_path;
5980 818c7501 2019-07-11 stsp
5981 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5982 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5983 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5984 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5985 818c7501 2019-07-11 stsp
5986 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5987 818c7501 2019-07-11 stsp == -1)
5988 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5989 818c7501 2019-07-11 stsp
5990 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5991 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5992 818c7501 2019-07-11 stsp free(ondisk_path);
5993 818c7501 2019-07-11 stsp if (err)
5994 818c7501 2019-07-11 stsp return err;
5995 818c7501 2019-07-11 stsp
5996 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5997 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5998 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5999 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6000 818c7501 2019-07-11 stsp
6001 818c7501 2019-07-11 stsp return NULL;
6002 818c7501 2019-07-11 stsp }
6003 818c7501 2019-07-11 stsp
6004 818c7501 2019-07-11 stsp const struct got_error *
6005 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6006 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6007 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6008 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6009 818c7501 2019-07-11 stsp {
6010 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6011 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6012 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6013 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6014 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6015 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6016 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6017 818c7501 2019-07-11 stsp
6018 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6019 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6020 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6021 818c7501 2019-07-11 stsp
6022 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6023 818c7501 2019-07-11 stsp if (err)
6024 818c7501 2019-07-11 stsp return err;
6025 818c7501 2019-07-11 stsp
6026 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, 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 ok_arg.worktree = worktree;
6031 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6032 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6033 818c7501 2019-07-11 stsp &ok_arg);
6034 818c7501 2019-07-11 stsp if (err)
6035 818c7501 2019-07-11 stsp goto done;
6036 818c7501 2019-07-11 stsp
6037 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6038 818c7501 2019-07-11 stsp if (err)
6039 818c7501 2019-07-11 stsp goto done;
6040 818c7501 2019-07-11 stsp
6041 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6042 818c7501 2019-07-11 stsp if (err)
6043 818c7501 2019-07-11 stsp goto done;
6044 818c7501 2019-07-11 stsp
6045 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6046 818c7501 2019-07-11 stsp if (err)
6047 818c7501 2019-07-11 stsp goto done;
6048 818c7501 2019-07-11 stsp
6049 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6050 818c7501 2019-07-11 stsp 0);
6051 e51d7b55 2020-01-04 stsp if (err)
6052 e51d7b55 2020-01-04 stsp goto done;
6053 e51d7b55 2020-01-04 stsp
6054 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6055 818c7501 2019-07-11 stsp if (err)
6056 818c7501 2019-07-11 stsp goto done;
6057 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6058 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6059 e51d7b55 2020-01-04 stsp goto done;
6060 e51d7b55 2020-01-04 stsp }
6061 818c7501 2019-07-11 stsp
6062 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6063 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6064 818c7501 2019-07-11 stsp if (err)
6065 818c7501 2019-07-11 stsp goto done;
6066 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6067 818c7501 2019-07-11 stsp if (err)
6068 818c7501 2019-07-11 stsp goto done;
6069 818c7501 2019-07-11 stsp
6070 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6071 818c7501 2019-07-11 stsp
6072 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6073 818c7501 2019-07-11 stsp if (err)
6074 818c7501 2019-07-11 stsp goto done;
6075 818c7501 2019-07-11 stsp
6076 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6077 818c7501 2019-07-11 stsp if (err)
6078 818c7501 2019-07-11 stsp goto done;
6079 818c7501 2019-07-11 stsp
6080 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6081 818c7501 2019-07-11 stsp worktree->base_commit_id);
6082 818c7501 2019-07-11 stsp if (err)
6083 818c7501 2019-07-11 stsp goto done;
6084 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6085 818c7501 2019-07-11 stsp if (err)
6086 818c7501 2019-07-11 stsp goto done;
6087 818c7501 2019-07-11 stsp
6088 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6089 818c7501 2019-07-11 stsp if (err)
6090 818c7501 2019-07-11 stsp goto done;
6091 818c7501 2019-07-11 stsp done:
6092 818c7501 2019-07-11 stsp free(fileindex_path);
6093 818c7501 2019-07-11 stsp free(tmp_branch_name);
6094 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6095 818c7501 2019-07-11 stsp free(branch_ref_name);
6096 818c7501 2019-07-11 stsp if (branch_ref)
6097 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6098 818c7501 2019-07-11 stsp if (wt_branch)
6099 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6100 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6101 818c7501 2019-07-11 stsp if (err) {
6102 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6103 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6104 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6105 818c7501 2019-07-11 stsp }
6106 818c7501 2019-07-11 stsp if (*tmp_branch) {
6107 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6108 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6109 818c7501 2019-07-11 stsp }
6110 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6111 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6112 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6113 3e3a69f1 2019-07-25 stsp }
6114 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6115 818c7501 2019-07-11 stsp }
6116 818c7501 2019-07-11 stsp return err;
6117 818c7501 2019-07-11 stsp }
6118 818c7501 2019-07-11 stsp
6119 818c7501 2019-07-11 stsp const struct got_error *
6120 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6121 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6122 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6123 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6124 818c7501 2019-07-11 stsp {
6125 818c7501 2019-07-11 stsp const struct got_error *err;
6126 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6127 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6128 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6129 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6130 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6131 818c7501 2019-07-11 stsp
6132 818c7501 2019-07-11 stsp *commit_id = NULL;
6133 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6134 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6135 3e3a69f1 2019-07-25 stsp *branch = NULL;
6136 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6137 3e3a69f1 2019-07-25 stsp
6138 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6139 3e3a69f1 2019-07-25 stsp if (err)
6140 3e3a69f1 2019-07-25 stsp return err;
6141 818c7501 2019-07-11 stsp
6142 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6143 3e3a69f1 2019-07-25 stsp if (err)
6144 f032f1f7 2019-08-04 stsp goto done;
6145 f032f1f7 2019-08-04 stsp
6146 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6147 f032f1f7 2019-08-04 stsp &have_staged_files);
6148 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6149 f032f1f7 2019-08-04 stsp goto done;
6150 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6151 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6152 3e3a69f1 2019-07-25 stsp goto done;
6153 f032f1f7 2019-08-04 stsp }
6154 3e3a69f1 2019-07-25 stsp
6155 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6156 818c7501 2019-07-11 stsp if (err)
6157 f032f1f7 2019-08-04 stsp goto done;
6158 818c7501 2019-07-11 stsp
6159 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6160 818c7501 2019-07-11 stsp if (err)
6161 818c7501 2019-07-11 stsp goto done;
6162 818c7501 2019-07-11 stsp
6163 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6164 818c7501 2019-07-11 stsp if (err)
6165 818c7501 2019-07-11 stsp goto done;
6166 818c7501 2019-07-11 stsp
6167 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6168 818c7501 2019-07-11 stsp if (err)
6169 818c7501 2019-07-11 stsp goto done;
6170 818c7501 2019-07-11 stsp
6171 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6172 818c7501 2019-07-11 stsp if (err)
6173 818c7501 2019-07-11 stsp goto done;
6174 818c7501 2019-07-11 stsp
6175 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6176 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6177 818c7501 2019-07-11 stsp if (err)
6178 818c7501 2019-07-11 stsp goto done;
6179 818c7501 2019-07-11 stsp
6180 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6181 818c7501 2019-07-11 stsp if (err)
6182 818c7501 2019-07-11 stsp goto done;
6183 818c7501 2019-07-11 stsp
6184 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6185 818c7501 2019-07-11 stsp if (err)
6186 818c7501 2019-07-11 stsp goto done;
6187 818c7501 2019-07-11 stsp
6188 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6189 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6190 818c7501 2019-07-11 stsp if (err)
6191 818c7501 2019-07-11 stsp goto done;
6192 818c7501 2019-07-11 stsp
6193 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6194 818c7501 2019-07-11 stsp if (err)
6195 818c7501 2019-07-11 stsp goto done;
6196 818c7501 2019-07-11 stsp done:
6197 818c7501 2019-07-11 stsp free(commit_ref_name);
6198 818c7501 2019-07-11 stsp free(branch_ref_name);
6199 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6200 818c7501 2019-07-11 stsp if (commit_ref)
6201 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6202 818c7501 2019-07-11 stsp if (branch_ref)
6203 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6204 818c7501 2019-07-11 stsp if (err) {
6205 818c7501 2019-07-11 stsp free(*commit_id);
6206 818c7501 2019-07-11 stsp *commit_id = NULL;
6207 818c7501 2019-07-11 stsp if (*tmp_branch) {
6208 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6209 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6210 818c7501 2019-07-11 stsp }
6211 818c7501 2019-07-11 stsp if (*new_base_branch) {
6212 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6213 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6214 818c7501 2019-07-11 stsp }
6215 818c7501 2019-07-11 stsp if (*branch) {
6216 818c7501 2019-07-11 stsp got_ref_close(*branch);
6217 818c7501 2019-07-11 stsp *branch = NULL;
6218 818c7501 2019-07-11 stsp }
6219 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6220 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6221 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6222 3e3a69f1 2019-07-25 stsp }
6223 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6224 818c7501 2019-07-11 stsp }
6225 818c7501 2019-07-11 stsp return err;
6226 c4296144 2019-05-09 stsp }
6227 818c7501 2019-07-11 stsp
6228 818c7501 2019-07-11 stsp const struct got_error *
6229 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6230 818c7501 2019-07-11 stsp {
6231 818c7501 2019-07-11 stsp const struct got_error *err;
6232 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6233 818c7501 2019-07-11 stsp
6234 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6235 818c7501 2019-07-11 stsp if (err)
6236 818c7501 2019-07-11 stsp return err;
6237 818c7501 2019-07-11 stsp
6238 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6239 818c7501 2019-07-11 stsp free(tmp_branch_name);
6240 818c7501 2019-07-11 stsp return NULL;
6241 818c7501 2019-07-11 stsp }
6242 818c7501 2019-07-11 stsp
6243 818c7501 2019-07-11 stsp static const struct got_error *
6244 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6245 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6246 818c7501 2019-07-11 stsp {
6247 0ebf8283 2019-07-24 stsp *logmsg = arg;
6248 818c7501 2019-07-11 stsp return NULL;
6249 818c7501 2019-07-11 stsp }
6250 818c7501 2019-07-11 stsp
6251 818c7501 2019-07-11 stsp static const struct got_error *
6252 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6253 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6254 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6255 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6256 818c7501 2019-07-11 stsp {
6257 818c7501 2019-07-11 stsp return NULL;
6258 01757395 2019-07-12 stsp }
6259 01757395 2019-07-12 stsp
6260 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6261 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6262 01757395 2019-07-12 stsp void *progress_arg;
6263 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6264 01757395 2019-07-12 stsp };
6265 01757395 2019-07-12 stsp
6266 01757395 2019-07-12 stsp static const struct got_error *
6267 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6268 01757395 2019-07-12 stsp {
6269 01757395 2019-07-12 stsp const struct got_error *err;
6270 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6271 01757395 2019-07-12 stsp char *p;
6272 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6273 01757395 2019-07-12 stsp
6274 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6275 01757395 2019-07-12 stsp if (err)
6276 01757395 2019-07-12 stsp return err;
6277 01757395 2019-07-12 stsp
6278 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6279 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6280 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6281 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6282 01757395 2019-07-12 stsp return NULL;
6283 01757395 2019-07-12 stsp
6284 01757395 2019-07-12 stsp p = strdup(path);
6285 01757395 2019-07-12 stsp if (p == NULL)
6286 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6287 01757395 2019-07-12 stsp
6288 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6289 01757395 2019-07-12 stsp if (err || new == NULL)
6290 01757395 2019-07-12 stsp free(p);
6291 01757395 2019-07-12 stsp return err;
6292 01757395 2019-07-12 stsp }
6293 01757395 2019-07-12 stsp
6294 01757395 2019-07-12 stsp void
6295 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6296 01757395 2019-07-12 stsp {
6297 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6298 01757395 2019-07-12 stsp
6299 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6300 01757395 2019-07-12 stsp free((char *)pe->path);
6301 01757395 2019-07-12 stsp
6302 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6303 818c7501 2019-07-11 stsp }
6304 818c7501 2019-07-11 stsp
6305 0ebf8283 2019-07-24 stsp static const struct got_error *
6306 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6307 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6308 818c7501 2019-07-11 stsp {
6309 818c7501 2019-07-11 stsp const struct got_error *err;
6310 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6311 818c7501 2019-07-11 stsp
6312 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6313 818c7501 2019-07-11 stsp if (err) {
6314 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6315 818c7501 2019-07-11 stsp goto done;
6316 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6317 818c7501 2019-07-11 stsp if (err)
6318 818c7501 2019-07-11 stsp goto done;
6319 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6320 818c7501 2019-07-11 stsp if (err)
6321 818c7501 2019-07-11 stsp goto done;
6322 de05890f 2020-03-05 stsp } else if (is_rebase) {
6323 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6324 818c7501 2019-07-11 stsp int cmp;
6325 818c7501 2019-07-11 stsp
6326 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6327 818c7501 2019-07-11 stsp if (err)
6328 818c7501 2019-07-11 stsp goto done;
6329 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6330 818c7501 2019-07-11 stsp free(stored_id);
6331 818c7501 2019-07-11 stsp if (cmp != 0) {
6332 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6333 818c7501 2019-07-11 stsp goto done;
6334 818c7501 2019-07-11 stsp }
6335 818c7501 2019-07-11 stsp }
6336 0ebf8283 2019-07-24 stsp done:
6337 0ebf8283 2019-07-24 stsp if (commit_ref)
6338 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6339 0ebf8283 2019-07-24 stsp return err;
6340 0ebf8283 2019-07-24 stsp }
6341 0ebf8283 2019-07-24 stsp
6342 0ebf8283 2019-07-24 stsp static const struct got_error *
6343 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6344 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6345 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6346 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6347 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6348 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6349 0ebf8283 2019-07-24 stsp {
6350 0ebf8283 2019-07-24 stsp const struct got_error *err;
6351 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6352 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6353 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6354 818c7501 2019-07-11 stsp
6355 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6356 0ebf8283 2019-07-24 stsp
6357 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6358 0ebf8283 2019-07-24 stsp if (err)
6359 0ebf8283 2019-07-24 stsp return err;
6360 0ebf8283 2019-07-24 stsp
6361 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6362 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6363 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6364 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6365 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6366 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6367 818c7501 2019-07-11 stsp if (commit_ref)
6368 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6369 818c7501 2019-07-11 stsp return err;
6370 818c7501 2019-07-11 stsp }
6371 818c7501 2019-07-11 stsp
6372 818c7501 2019-07-11 stsp const struct got_error *
6373 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6374 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6375 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6376 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6377 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6378 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6379 818c7501 2019-07-11 stsp {
6380 0ebf8283 2019-07-24 stsp const struct got_error *err;
6381 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6382 0ebf8283 2019-07-24 stsp
6383 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6384 0ebf8283 2019-07-24 stsp if (err)
6385 0ebf8283 2019-07-24 stsp return err;
6386 0ebf8283 2019-07-24 stsp
6387 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6388 0ebf8283 2019-07-24 stsp if (err)
6389 0ebf8283 2019-07-24 stsp goto done;
6390 0ebf8283 2019-07-24 stsp
6391 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6392 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6393 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6394 0ebf8283 2019-07-24 stsp done:
6395 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6396 0ebf8283 2019-07-24 stsp return err;
6397 0ebf8283 2019-07-24 stsp }
6398 0ebf8283 2019-07-24 stsp
6399 0ebf8283 2019-07-24 stsp const struct got_error *
6400 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6401 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6402 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6403 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6404 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6405 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6406 0ebf8283 2019-07-24 stsp {
6407 0ebf8283 2019-07-24 stsp const struct got_error *err;
6408 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6409 0ebf8283 2019-07-24 stsp
6410 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6411 0ebf8283 2019-07-24 stsp if (err)
6412 0ebf8283 2019-07-24 stsp return err;
6413 0ebf8283 2019-07-24 stsp
6414 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6415 0ebf8283 2019-07-24 stsp if (err)
6416 0ebf8283 2019-07-24 stsp goto done;
6417 0ebf8283 2019-07-24 stsp
6418 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6419 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6420 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6421 0ebf8283 2019-07-24 stsp done:
6422 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6423 0ebf8283 2019-07-24 stsp return err;
6424 0ebf8283 2019-07-24 stsp }
6425 0ebf8283 2019-07-24 stsp
6426 0ebf8283 2019-07-24 stsp static const struct got_error *
6427 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6428 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6429 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6430 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6431 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6432 0ebf8283 2019-07-24 stsp {
6433 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6434 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6435 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6436 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6437 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6438 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6439 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6440 818c7501 2019-07-11 stsp
6441 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6442 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6443 a0e95631 2019-07-12 stsp
6444 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6445 818c7501 2019-07-11 stsp
6446 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6447 a0e95631 2019-07-12 stsp if (err)
6448 3e3a69f1 2019-07-25 stsp return err;
6449 a0e95631 2019-07-12 stsp
6450 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6451 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6452 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6453 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6454 01757395 2019-07-12 stsp /*
6455 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6456 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6457 6c13b005 2021-09-02 stsp *
6458 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6459 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6460 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6461 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6462 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6463 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6464 01757395 2019-07-12 stsp */
6465 01757395 2019-07-12 stsp if (merged_paths) {
6466 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6467 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6468 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6469 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6470 f2a9dc41 2019-12-13 tracey 0);
6471 01757395 2019-07-12 stsp if (err)
6472 01757395 2019-07-12 stsp goto done;
6473 01757395 2019-07-12 stsp }
6474 01757395 2019-07-12 stsp } else {
6475 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6476 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6477 01757395 2019-07-12 stsp if (err)
6478 01757395 2019-07-12 stsp goto done;
6479 01757395 2019-07-12 stsp }
6480 a0e95631 2019-07-12 stsp
6481 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6482 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6483 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6484 a0e95631 2019-07-12 stsp if (err)
6485 a0e95631 2019-07-12 stsp goto done;
6486 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6487 a0e95631 2019-07-12 stsp goto done;
6488 ff0d2220 2019-07-11 stsp }
6489 818c7501 2019-07-11 stsp
6490 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6491 edd02c5e 2019-07-11 stsp if (err)
6492 edd02c5e 2019-07-11 stsp goto done;
6493 edd02c5e 2019-07-11 stsp
6494 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6495 818c7501 2019-07-11 stsp if (err)
6496 818c7501 2019-07-11 stsp goto done;
6497 818c7501 2019-07-11 stsp
6498 5943eee2 2019-08-13 stsp if (new_logmsg) {
6499 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6500 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6501 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6502 5943eee2 2019-08-13 stsp goto done;
6503 5943eee2 2019-08-13 stsp }
6504 5943eee2 2019-08-13 stsp } else {
6505 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6506 5943eee2 2019-08-13 stsp if (err)
6507 5943eee2 2019-08-13 stsp goto done;
6508 5943eee2 2019-08-13 stsp }
6509 0ebf8283 2019-07-24 stsp
6510 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6511 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6512 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6513 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6514 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6515 a0e95631 2019-07-12 stsp if (err)
6516 a0e95631 2019-07-12 stsp goto done;
6517 a0e95631 2019-07-12 stsp
6518 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6519 a0e95631 2019-07-12 stsp if (err)
6520 a0e95631 2019-07-12 stsp goto done;
6521 a0e95631 2019-07-12 stsp
6522 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6523 a0e95631 2019-07-12 stsp if (err)
6524 a0e95631 2019-07-12 stsp goto done;
6525 a0e95631 2019-07-12 stsp
6526 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6527 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6528 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6529 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6530 a0e95631 2019-07-12 stsp err = sync_err;
6531 818c7501 2019-07-11 stsp done:
6532 a0e95631 2019-07-12 stsp free(fileindex_path);
6533 a0e95631 2019-07-12 stsp free(head_commit_id);
6534 a0e95631 2019-07-12 stsp if (head_ref)
6535 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6536 818c7501 2019-07-11 stsp if (err) {
6537 818c7501 2019-07-11 stsp free(*new_commit_id);
6538 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6539 818c7501 2019-07-11 stsp }
6540 818c7501 2019-07-11 stsp return err;
6541 818c7501 2019-07-11 stsp }
6542 818c7501 2019-07-11 stsp
6543 818c7501 2019-07-11 stsp const struct got_error *
6544 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6545 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6546 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6547 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6548 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6549 0ebf8283 2019-07-24 stsp {
6550 0ebf8283 2019-07-24 stsp const struct got_error *err;
6551 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6552 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6553 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6554 0ebf8283 2019-07-24 stsp
6555 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6556 0ebf8283 2019-07-24 stsp if (err)
6557 0ebf8283 2019-07-24 stsp return err;
6558 0ebf8283 2019-07-24 stsp
6559 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6560 0ebf8283 2019-07-24 stsp if (err)
6561 0ebf8283 2019-07-24 stsp goto done;
6562 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6563 0ebf8283 2019-07-24 stsp if (err)
6564 0ebf8283 2019-07-24 stsp goto done;
6565 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6566 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6567 0ebf8283 2019-07-24 stsp goto done;
6568 0ebf8283 2019-07-24 stsp }
6569 0ebf8283 2019-07-24 stsp
6570 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6571 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6572 0ebf8283 2019-07-24 stsp done:
6573 0ebf8283 2019-07-24 stsp if (commit_ref)
6574 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6575 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6576 0ebf8283 2019-07-24 stsp free(commit_id);
6577 0ebf8283 2019-07-24 stsp return err;
6578 0ebf8283 2019-07-24 stsp }
6579 0ebf8283 2019-07-24 stsp
6580 0ebf8283 2019-07-24 stsp const struct got_error *
6581 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6582 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6583 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6584 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6585 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6586 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6587 0ebf8283 2019-07-24 stsp {
6588 0ebf8283 2019-07-24 stsp const struct got_error *err;
6589 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6590 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6591 0ebf8283 2019-07-24 stsp
6592 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6593 0ebf8283 2019-07-24 stsp if (err)
6594 0ebf8283 2019-07-24 stsp return err;
6595 0ebf8283 2019-07-24 stsp
6596 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6597 0ebf8283 2019-07-24 stsp if (err)
6598 0ebf8283 2019-07-24 stsp goto done;
6599 0ebf8283 2019-07-24 stsp
6600 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6601 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6602 0ebf8283 2019-07-24 stsp done:
6603 0ebf8283 2019-07-24 stsp if (commit_ref)
6604 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6605 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6606 0ebf8283 2019-07-24 stsp return err;
6607 0ebf8283 2019-07-24 stsp }
6608 0ebf8283 2019-07-24 stsp
6609 0ebf8283 2019-07-24 stsp const struct got_error *
6610 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6611 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6612 818c7501 2019-07-11 stsp {
6613 3e3a69f1 2019-07-25 stsp if (fileindex)
6614 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6615 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6616 69844fba 2019-07-11 stsp }
6617 69844fba 2019-07-11 stsp
6618 69844fba 2019-07-11 stsp static const struct got_error *
6619 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6620 69844fba 2019-07-11 stsp {
6621 69844fba 2019-07-11 stsp const struct got_error *err;
6622 69844fba 2019-07-11 stsp struct got_reference *ref;
6623 69844fba 2019-07-11 stsp
6624 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6625 69844fba 2019-07-11 stsp if (err) {
6626 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6627 69844fba 2019-07-11 stsp return NULL;
6628 69844fba 2019-07-11 stsp return err;
6629 69844fba 2019-07-11 stsp }
6630 69844fba 2019-07-11 stsp
6631 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6632 69844fba 2019-07-11 stsp got_ref_close(ref);
6633 69844fba 2019-07-11 stsp return err;
6634 818c7501 2019-07-11 stsp }
6635 818c7501 2019-07-11 stsp
6636 69844fba 2019-07-11 stsp static const struct got_error *
6637 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6638 69844fba 2019-07-11 stsp {
6639 69844fba 2019-07-11 stsp const struct got_error *err;
6640 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6641 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6642 69844fba 2019-07-11 stsp
6643 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6644 69844fba 2019-07-11 stsp if (err)
6645 69844fba 2019-07-11 stsp goto done;
6646 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6647 69844fba 2019-07-11 stsp if (err)
6648 69844fba 2019-07-11 stsp goto done;
6649 69844fba 2019-07-11 stsp
6650 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6651 69844fba 2019-07-11 stsp if (err)
6652 69844fba 2019-07-11 stsp goto done;
6653 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6654 69844fba 2019-07-11 stsp if (err)
6655 69844fba 2019-07-11 stsp goto done;
6656 69844fba 2019-07-11 stsp
6657 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6658 69844fba 2019-07-11 stsp if (err)
6659 69844fba 2019-07-11 stsp goto done;
6660 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6661 69844fba 2019-07-11 stsp if (err)
6662 69844fba 2019-07-11 stsp goto done;
6663 69844fba 2019-07-11 stsp
6664 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6665 69844fba 2019-07-11 stsp if (err)
6666 69844fba 2019-07-11 stsp goto done;
6667 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6668 69844fba 2019-07-11 stsp if (err)
6669 69844fba 2019-07-11 stsp goto done;
6670 69844fba 2019-07-11 stsp
6671 69844fba 2019-07-11 stsp done:
6672 69844fba 2019-07-11 stsp free(tmp_branch_name);
6673 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6674 69844fba 2019-07-11 stsp free(branch_ref_name);
6675 69844fba 2019-07-11 stsp free(commit_ref_name);
6676 e600f124 2021-03-21 stsp return err;
6677 e600f124 2021-03-21 stsp }
6678 e600f124 2021-03-21 stsp
6679 e600f124 2021-03-21 stsp const struct got_error *
6680 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6681 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6682 e600f124 2021-03-21 stsp {
6683 e600f124 2021-03-21 stsp const struct got_error *err;
6684 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6685 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6686 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6687 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6688 e600f124 2021-03-21 stsp char *refname = NULL;
6689 e600f124 2021-03-21 stsp
6690 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6691 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6692 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6693 e600f124 2021-03-21 stsp branch_name += 11;
6694 e600f124 2021-03-21 stsp
6695 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6696 e600f124 2021-03-21 stsp if (err)
6697 e600f124 2021-03-21 stsp return err;
6698 e600f124 2021-03-21 stsp
6699 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6700 e600f124 2021-03-21 stsp new_id_str) == -1) {
6701 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6702 e600f124 2021-03-21 stsp goto done;
6703 e600f124 2021-03-21 stsp }
6704 e600f124 2021-03-21 stsp
6705 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6706 e600f124 2021-03-21 stsp if (err)
6707 e600f124 2021-03-21 stsp goto done;
6708 e600f124 2021-03-21 stsp
6709 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6710 e600f124 2021-03-21 stsp if (err)
6711 e600f124 2021-03-21 stsp goto done;
6712 e600f124 2021-03-21 stsp
6713 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6714 e600f124 2021-03-21 stsp done:
6715 e600f124 2021-03-21 stsp free(new_id_str);
6716 e600f124 2021-03-21 stsp free(refname);
6717 e600f124 2021-03-21 stsp free(old_commit_id);
6718 e600f124 2021-03-21 stsp if (ref)
6719 e600f124 2021-03-21 stsp got_ref_close(ref);
6720 69844fba 2019-07-11 stsp return err;
6721 69844fba 2019-07-11 stsp }
6722 69844fba 2019-07-11 stsp
6723 818c7501 2019-07-11 stsp const struct got_error *
6724 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6725 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6726 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6727 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6728 818c7501 2019-07-11 stsp {
6729 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6730 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6731 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6732 818c7501 2019-07-11 stsp
6733 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6734 818c7501 2019-07-11 stsp if (err)
6735 818c7501 2019-07-11 stsp return err;
6736 e600f124 2021-03-21 stsp
6737 e600f124 2021-03-21 stsp if (create_backup) {
6738 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6739 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6740 e600f124 2021-03-21 stsp if (err)
6741 e600f124 2021-03-21 stsp goto done;
6742 e600f124 2021-03-21 stsp }
6743 818c7501 2019-07-11 stsp
6744 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6745 818c7501 2019-07-11 stsp if (err)
6746 818c7501 2019-07-11 stsp goto done;
6747 818c7501 2019-07-11 stsp
6748 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6749 818c7501 2019-07-11 stsp if (err)
6750 818c7501 2019-07-11 stsp goto done;
6751 818c7501 2019-07-11 stsp
6752 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6753 818c7501 2019-07-11 stsp if (err)
6754 818c7501 2019-07-11 stsp goto done;
6755 818c7501 2019-07-11 stsp
6756 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6757 a615e0e7 2020-12-16 stsp if (err)
6758 a615e0e7 2020-12-16 stsp goto done;
6759 a615e0e7 2020-12-16 stsp
6760 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6761 a615e0e7 2020-12-16 stsp if (err)
6762 a615e0e7 2020-12-16 stsp goto done;
6763 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6764 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6765 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6766 a615e0e7 2020-12-16 stsp err = sync_err;
6767 818c7501 2019-07-11 stsp done:
6768 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6769 a615e0e7 2020-12-16 stsp free(fileindex_path);
6770 818c7501 2019-07-11 stsp free(new_head_commit_id);
6771 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6772 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6773 818c7501 2019-07-11 stsp err = unlockerr;
6774 818c7501 2019-07-11 stsp return err;
6775 818c7501 2019-07-11 stsp }
6776 818c7501 2019-07-11 stsp
6777 818c7501 2019-07-11 stsp const struct got_error *
6778 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6779 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6780 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6781 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6782 818c7501 2019-07-11 stsp {
6783 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6784 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6785 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6786 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6787 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6788 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6789 818c7501 2019-07-11 stsp
6790 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6791 818c7501 2019-07-11 stsp if (err)
6792 818c7501 2019-07-11 stsp return err;
6793 818c7501 2019-07-11 stsp
6794 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6795 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6796 818c7501 2019-07-11 stsp if (err)
6797 818c7501 2019-07-11 stsp goto done;
6798 818c7501 2019-07-11 stsp
6799 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6800 818c7501 2019-07-11 stsp if (err)
6801 818c7501 2019-07-11 stsp goto done;
6802 818c7501 2019-07-11 stsp
6803 818c7501 2019-07-11 stsp /*
6804 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6805 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6806 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6807 818c7501 2019-07-11 stsp */
6808 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6809 818c7501 2019-07-11 stsp if (err)
6810 818c7501 2019-07-11 stsp goto done;
6811 818c7501 2019-07-11 stsp
6812 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6813 818c7501 2019-07-11 stsp if (err)
6814 818c7501 2019-07-11 stsp goto done;
6815 818c7501 2019-07-11 stsp
6816 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6817 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6818 ca355955 2019-07-12 stsp if (err)
6819 ca355955 2019-07-12 stsp goto done;
6820 ca355955 2019-07-12 stsp
6821 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6822 ca355955 2019-07-12 stsp if (err)
6823 ca355955 2019-07-12 stsp goto done;
6824 ca355955 2019-07-12 stsp
6825 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6826 818c7501 2019-07-11 stsp if (err)
6827 818c7501 2019-07-11 stsp goto done;
6828 818c7501 2019-07-11 stsp
6829 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6830 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6831 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6832 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6833 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6834 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6835 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6836 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
6837 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6838 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
6839 55bd499d 2019-07-12 stsp if (err)
6840 1f1abb7e 2019-08-08 stsp goto sync;
6841 55bd499d 2019-07-12 stsp
6842 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6843 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6844 ca355955 2019-07-12 stsp sync:
6845 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6846 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6847 ca355955 2019-07-12 stsp err = sync_err;
6848 818c7501 2019-07-11 stsp done:
6849 818c7501 2019-07-11 stsp got_ref_close(resolved);
6850 a3a2faf2 2019-07-12 stsp free(tree_id);
6851 818c7501 2019-07-11 stsp free(commit_id);
6852 0ebf8283 2019-07-24 stsp if (fileindex)
6853 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6854 0ebf8283 2019-07-24 stsp free(fileindex_path);
6855 0ebf8283 2019-07-24 stsp
6856 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6857 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6858 0ebf8283 2019-07-24 stsp err = unlockerr;
6859 0ebf8283 2019-07-24 stsp return err;
6860 0ebf8283 2019-07-24 stsp }
6861 0ebf8283 2019-07-24 stsp
6862 0ebf8283 2019-07-24 stsp const struct got_error *
6863 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6864 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6865 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6866 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6867 0ebf8283 2019-07-24 stsp {
6868 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6869 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6870 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6871 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6872 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6873 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6874 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6875 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6876 0ebf8283 2019-07-24 stsp
6877 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6878 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6879 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6880 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6881 0ebf8283 2019-07-24 stsp
6882 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6883 0ebf8283 2019-07-24 stsp if (err)
6884 0ebf8283 2019-07-24 stsp return err;
6885 0ebf8283 2019-07-24 stsp
6886 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, 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 ok_arg.worktree = worktree;
6891 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6892 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6893 0ebf8283 2019-07-24 stsp &ok_arg);
6894 0ebf8283 2019-07-24 stsp if (err)
6895 0ebf8283 2019-07-24 stsp goto done;
6896 0ebf8283 2019-07-24 stsp
6897 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6898 0ebf8283 2019-07-24 stsp if (err)
6899 0ebf8283 2019-07-24 stsp goto done;
6900 0ebf8283 2019-07-24 stsp
6901 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6902 0ebf8283 2019-07-24 stsp if (err)
6903 0ebf8283 2019-07-24 stsp goto done;
6904 0ebf8283 2019-07-24 stsp
6905 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6906 0ebf8283 2019-07-24 stsp worktree);
6907 0ebf8283 2019-07-24 stsp if (err)
6908 0ebf8283 2019-07-24 stsp goto done;
6909 0ebf8283 2019-07-24 stsp
6910 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6911 0ebf8283 2019-07-24 stsp 0);
6912 0ebf8283 2019-07-24 stsp if (err)
6913 0ebf8283 2019-07-24 stsp goto done;
6914 0ebf8283 2019-07-24 stsp
6915 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6916 0ebf8283 2019-07-24 stsp if (err)
6917 0ebf8283 2019-07-24 stsp goto done;
6918 0ebf8283 2019-07-24 stsp
6919 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6920 0ebf8283 2019-07-24 stsp if (err)
6921 0ebf8283 2019-07-24 stsp goto done;
6922 0ebf8283 2019-07-24 stsp
6923 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6924 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6925 0ebf8283 2019-07-24 stsp if (err)
6926 0ebf8283 2019-07-24 stsp goto done;
6927 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6928 0ebf8283 2019-07-24 stsp if (err)
6929 0ebf8283 2019-07-24 stsp goto done;
6930 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6931 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6932 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6933 0ebf8283 2019-07-24 stsp goto done;
6934 0ebf8283 2019-07-24 stsp }
6935 0ebf8283 2019-07-24 stsp
6936 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6937 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6938 0ebf8283 2019-07-24 stsp if (err)
6939 0ebf8283 2019-07-24 stsp goto done;
6940 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6941 0ebf8283 2019-07-24 stsp if (err)
6942 0ebf8283 2019-07-24 stsp goto done;
6943 0ebf8283 2019-07-24 stsp
6944 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6945 0ebf8283 2019-07-24 stsp if (err)
6946 0ebf8283 2019-07-24 stsp goto done;
6947 0ebf8283 2019-07-24 stsp done:
6948 0ebf8283 2019-07-24 stsp free(fileindex_path);
6949 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6950 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6951 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6952 0ebf8283 2019-07-24 stsp if (wt_branch)
6953 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6954 0ebf8283 2019-07-24 stsp if (err) {
6955 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6956 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6957 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6958 0ebf8283 2019-07-24 stsp }
6959 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6960 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6961 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6962 0ebf8283 2019-07-24 stsp }
6963 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6964 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6965 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6966 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6967 3e3a69f1 2019-07-25 stsp }
6968 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6969 0ebf8283 2019-07-24 stsp }
6970 0ebf8283 2019-07-24 stsp return err;
6971 0ebf8283 2019-07-24 stsp }
6972 0ebf8283 2019-07-24 stsp
6973 0ebf8283 2019-07-24 stsp const struct got_error *
6974 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6975 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6976 0ebf8283 2019-07-24 stsp {
6977 3e3a69f1 2019-07-25 stsp if (fileindex)
6978 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6979 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6980 0ebf8283 2019-07-24 stsp }
6981 0ebf8283 2019-07-24 stsp
6982 0ebf8283 2019-07-24 stsp const struct got_error *
6983 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6984 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6985 0ebf8283 2019-07-24 stsp {
6986 0ebf8283 2019-07-24 stsp const struct got_error *err;
6987 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6988 0ebf8283 2019-07-24 stsp
6989 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6990 0ebf8283 2019-07-24 stsp if (err)
6991 0ebf8283 2019-07-24 stsp return err;
6992 0ebf8283 2019-07-24 stsp
6993 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6994 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6995 0ebf8283 2019-07-24 stsp return NULL;
6996 0ebf8283 2019-07-24 stsp }
6997 0ebf8283 2019-07-24 stsp
6998 0ebf8283 2019-07-24 stsp const struct got_error *
6999 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7000 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7001 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7002 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7003 0ebf8283 2019-07-24 stsp {
7004 0ebf8283 2019-07-24 stsp const struct got_error *err;
7005 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7006 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7007 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7008 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7009 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7010 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7011 0ebf8283 2019-07-24 stsp
7012 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7013 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7014 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7015 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7016 0ebf8283 2019-07-24 stsp
7017 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7018 3e3a69f1 2019-07-25 stsp if (err)
7019 3e3a69f1 2019-07-25 stsp return err;
7020 3e3a69f1 2019-07-25 stsp
7021 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7022 3e3a69f1 2019-07-25 stsp if (err)
7023 f032f1f7 2019-08-04 stsp goto done;
7024 f032f1f7 2019-08-04 stsp
7025 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7026 f032f1f7 2019-08-04 stsp &have_staged_files);
7027 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7028 f032f1f7 2019-08-04 stsp goto done;
7029 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7030 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7031 3e3a69f1 2019-07-25 stsp goto done;
7032 f032f1f7 2019-08-04 stsp }
7033 3e3a69f1 2019-07-25 stsp
7034 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7035 0ebf8283 2019-07-24 stsp if (err)
7036 f032f1f7 2019-08-04 stsp goto done;
7037 0ebf8283 2019-07-24 stsp
7038 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7039 0ebf8283 2019-07-24 stsp if (err)
7040 0ebf8283 2019-07-24 stsp goto done;
7041 0ebf8283 2019-07-24 stsp
7042 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7043 0ebf8283 2019-07-24 stsp if (err)
7044 0ebf8283 2019-07-24 stsp goto done;
7045 0ebf8283 2019-07-24 stsp
7046 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7047 0ebf8283 2019-07-24 stsp worktree);
7048 0ebf8283 2019-07-24 stsp if (err)
7049 0ebf8283 2019-07-24 stsp goto done;
7050 0ebf8283 2019-07-24 stsp
7051 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7052 0ebf8283 2019-07-24 stsp if (err)
7053 0ebf8283 2019-07-24 stsp goto done;
7054 0ebf8283 2019-07-24 stsp
7055 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7056 0ebf8283 2019-07-24 stsp if (err)
7057 0ebf8283 2019-07-24 stsp goto done;
7058 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7059 0ebf8283 2019-07-24 stsp if (err)
7060 0ebf8283 2019-07-24 stsp goto done;
7061 0ebf8283 2019-07-24 stsp
7062 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7063 0ebf8283 2019-07-24 stsp if (err)
7064 0ebf8283 2019-07-24 stsp goto done;
7065 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7066 0ebf8283 2019-07-24 stsp if (err)
7067 0ebf8283 2019-07-24 stsp goto done;
7068 0ebf8283 2019-07-24 stsp
7069 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7070 0ebf8283 2019-07-24 stsp if (err)
7071 0ebf8283 2019-07-24 stsp goto done;
7072 0ebf8283 2019-07-24 stsp done:
7073 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7074 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7075 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7076 0ebf8283 2019-07-24 stsp if (commit_ref)
7077 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7078 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7079 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7080 0ebf8283 2019-07-24 stsp if (err) {
7081 0ebf8283 2019-07-24 stsp free(*commit_id);
7082 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7083 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7084 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7085 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7086 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7087 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7088 0ebf8283 2019-07-24 stsp }
7089 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7090 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7091 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7092 3e3a69f1 2019-07-25 stsp }
7093 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7094 0ebf8283 2019-07-24 stsp }
7095 0ebf8283 2019-07-24 stsp return err;
7096 0ebf8283 2019-07-24 stsp }
7097 0ebf8283 2019-07-24 stsp
7098 0ebf8283 2019-07-24 stsp static const struct got_error *
7099 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7100 0ebf8283 2019-07-24 stsp {
7101 0ebf8283 2019-07-24 stsp const struct got_error *err;
7102 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7103 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7104 0ebf8283 2019-07-24 stsp
7105 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_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(tmp_branch_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_base_commit_ref_name(&base_commit_ref_name,
7113 0ebf8283 2019-07-24 stsp worktree);
7114 0ebf8283 2019-07-24 stsp if (err)
7115 0ebf8283 2019-07-24 stsp goto done;
7116 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7117 0ebf8283 2019-07-24 stsp if (err)
7118 0ebf8283 2019-07-24 stsp goto done;
7119 0ebf8283 2019-07-24 stsp
7120 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7121 0ebf8283 2019-07-24 stsp if (err)
7122 0ebf8283 2019-07-24 stsp goto done;
7123 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7124 0ebf8283 2019-07-24 stsp if (err)
7125 0ebf8283 2019-07-24 stsp goto done;
7126 0ebf8283 2019-07-24 stsp
7127 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7128 0ebf8283 2019-07-24 stsp if (err)
7129 0ebf8283 2019-07-24 stsp goto done;
7130 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7131 0ebf8283 2019-07-24 stsp if (err)
7132 0ebf8283 2019-07-24 stsp goto done;
7133 0ebf8283 2019-07-24 stsp done:
7134 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7135 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7136 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7137 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7138 0ebf8283 2019-07-24 stsp return err;
7139 0ebf8283 2019-07-24 stsp }
7140 0ebf8283 2019-07-24 stsp
7141 0ebf8283 2019-07-24 stsp const struct got_error *
7142 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7143 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7144 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7145 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7146 0ebf8283 2019-07-24 stsp {
7147 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7148 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7149 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7150 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7151 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7152 0ebf8283 2019-07-24 stsp
7153 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7154 0ebf8283 2019-07-24 stsp if (err)
7155 0ebf8283 2019-07-24 stsp return err;
7156 0ebf8283 2019-07-24 stsp
7157 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7158 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7159 0ebf8283 2019-07-24 stsp if (err)
7160 0ebf8283 2019-07-24 stsp goto done;
7161 0ebf8283 2019-07-24 stsp
7162 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7163 0ebf8283 2019-07-24 stsp if (err)
7164 0ebf8283 2019-07-24 stsp goto done;
7165 0ebf8283 2019-07-24 stsp
7166 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7167 0ebf8283 2019-07-24 stsp if (err)
7168 0ebf8283 2019-07-24 stsp goto done;
7169 0ebf8283 2019-07-24 stsp
7170 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7171 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7172 0ebf8283 2019-07-24 stsp if (err)
7173 0ebf8283 2019-07-24 stsp goto done;
7174 0ebf8283 2019-07-24 stsp
7175 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7176 0ebf8283 2019-07-24 stsp if (err)
7177 0ebf8283 2019-07-24 stsp goto done;
7178 0ebf8283 2019-07-24 stsp
7179 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7180 0ebf8283 2019-07-24 stsp if (err)
7181 0ebf8283 2019-07-24 stsp goto done;
7182 0ebf8283 2019-07-24 stsp
7183 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7184 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7185 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7186 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7187 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7188 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7189 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7190 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7191 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7192 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7193 0ebf8283 2019-07-24 stsp if (err)
7194 1f1abb7e 2019-08-08 stsp goto sync;
7195 0ebf8283 2019-07-24 stsp
7196 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7197 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7198 0ebf8283 2019-07-24 stsp sync:
7199 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7200 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7201 0ebf8283 2019-07-24 stsp err = sync_err;
7202 0ebf8283 2019-07-24 stsp done:
7203 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7204 0ebf8283 2019-07-24 stsp free(tree_id);
7205 818c7501 2019-07-11 stsp free(fileindex_path);
7206 818c7501 2019-07-11 stsp
7207 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7208 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7209 818c7501 2019-07-11 stsp err = unlockerr;
7210 818c7501 2019-07-11 stsp return err;
7211 818c7501 2019-07-11 stsp }
7212 0ebf8283 2019-07-24 stsp
7213 0ebf8283 2019-07-24 stsp const struct got_error *
7214 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7215 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7216 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7217 0ebf8283 2019-07-24 stsp {
7218 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7219 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7220 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7221 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7222 0ebf8283 2019-07-24 stsp
7223 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7224 0ebf8283 2019-07-24 stsp if (err)
7225 0ebf8283 2019-07-24 stsp return err;
7226 0ebf8283 2019-07-24 stsp
7227 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7228 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7229 e600f124 2021-03-21 stsp if (err)
7230 e600f124 2021-03-21 stsp goto done;
7231 e600f124 2021-03-21 stsp
7232 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7233 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7234 0ebf8283 2019-07-24 stsp if (err)
7235 0ebf8283 2019-07-24 stsp goto done;
7236 0ebf8283 2019-07-24 stsp
7237 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7238 0ebf8283 2019-07-24 stsp if (err)
7239 0ebf8283 2019-07-24 stsp goto done;
7240 0ebf8283 2019-07-24 stsp
7241 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7242 0ebf8283 2019-07-24 stsp if (err)
7243 0ebf8283 2019-07-24 stsp goto done;
7244 0ebf8283 2019-07-24 stsp
7245 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7246 0ebf8283 2019-07-24 stsp if (err)
7247 0ebf8283 2019-07-24 stsp goto done;
7248 0ebf8283 2019-07-24 stsp
7249 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7250 a615e0e7 2020-12-16 stsp if (err)
7251 a615e0e7 2020-12-16 stsp goto done;
7252 a615e0e7 2020-12-16 stsp
7253 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7254 a615e0e7 2020-12-16 stsp if (err)
7255 a615e0e7 2020-12-16 stsp goto done;
7256 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7257 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7258 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7259 a615e0e7 2020-12-16 stsp err = sync_err;
7260 0ebf8283 2019-07-24 stsp done:
7261 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7262 a615e0e7 2020-12-16 stsp free(fileindex_path);
7263 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7264 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7265 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7266 0ebf8283 2019-07-24 stsp err = unlockerr;
7267 0ebf8283 2019-07-24 stsp return err;
7268 0ebf8283 2019-07-24 stsp }
7269 0ebf8283 2019-07-24 stsp
7270 0ebf8283 2019-07-24 stsp const struct got_error *
7271 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7272 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7273 0ebf8283 2019-07-24 stsp {
7274 0ebf8283 2019-07-24 stsp const struct got_error *err;
7275 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7276 0ebf8283 2019-07-24 stsp
7277 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7278 0ebf8283 2019-07-24 stsp if (err)
7279 0ebf8283 2019-07-24 stsp return err;
7280 0ebf8283 2019-07-24 stsp
7281 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7282 0ebf8283 2019-07-24 stsp if (err)
7283 0ebf8283 2019-07-24 stsp goto done;
7284 0ebf8283 2019-07-24 stsp
7285 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7286 0ebf8283 2019-07-24 stsp done:
7287 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7288 2822a352 2019-10-15 stsp return err;
7289 2822a352 2019-10-15 stsp }
7290 2822a352 2019-10-15 stsp
7291 2822a352 2019-10-15 stsp const struct got_error *
7292 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7293 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7294 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7295 2822a352 2019-10-15 stsp struct got_repository *repo)
7296 2822a352 2019-10-15 stsp {
7297 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7298 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7299 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7300 2822a352 2019-10-15 stsp
7301 2822a352 2019-10-15 stsp *fileindex = NULL;
7302 2822a352 2019-10-15 stsp *branch_ref = NULL;
7303 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7304 2822a352 2019-10-15 stsp
7305 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7306 2822a352 2019-10-15 stsp if (err)
7307 2822a352 2019-10-15 stsp return err;
7308 2822a352 2019-10-15 stsp
7309 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7310 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7311 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7312 2822a352 2019-10-15 stsp "update -b or different branch name required");
7313 2822a352 2019-10-15 stsp goto done;
7314 2822a352 2019-10-15 stsp }
7315 2822a352 2019-10-15 stsp
7316 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7317 2822a352 2019-10-15 stsp if (err)
7318 2822a352 2019-10-15 stsp goto done;
7319 2822a352 2019-10-15 stsp
7320 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7321 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7322 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7323 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7324 2822a352 2019-10-15 stsp &ok_arg);
7325 2822a352 2019-10-15 stsp if (err)
7326 2822a352 2019-10-15 stsp goto done;
7327 2822a352 2019-10-15 stsp
7328 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7329 2822a352 2019-10-15 stsp if (err)
7330 2822a352 2019-10-15 stsp goto done;
7331 2822a352 2019-10-15 stsp
7332 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7333 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7334 2822a352 2019-10-15 stsp done:
7335 2822a352 2019-10-15 stsp if (err) {
7336 2822a352 2019-10-15 stsp if (*branch_ref) {
7337 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7338 2822a352 2019-10-15 stsp *branch_ref = NULL;
7339 2822a352 2019-10-15 stsp }
7340 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7341 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7342 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7343 2822a352 2019-10-15 stsp }
7344 2822a352 2019-10-15 stsp if (*fileindex) {
7345 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7346 2822a352 2019-10-15 stsp *fileindex = NULL;
7347 2822a352 2019-10-15 stsp }
7348 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7349 2822a352 2019-10-15 stsp }
7350 0cb83759 2019-08-03 stsp return err;
7351 0cb83759 2019-08-03 stsp }
7352 0cb83759 2019-08-03 stsp
7353 2822a352 2019-10-15 stsp const struct got_error *
7354 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7355 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7356 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7357 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7358 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7359 2822a352 2019-10-15 stsp {
7360 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7361 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7362 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7363 2822a352 2019-10-15 stsp
7364 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7365 2822a352 2019-10-15 stsp if (err)
7366 2822a352 2019-10-15 stsp goto done;
7367 2822a352 2019-10-15 stsp
7368 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7369 2822a352 2019-10-15 stsp if (err)
7370 2822a352 2019-10-15 stsp goto done;
7371 2822a352 2019-10-15 stsp
7372 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7373 2822a352 2019-10-15 stsp worktree->path_prefix);
7374 2822a352 2019-10-15 stsp if (err)
7375 2822a352 2019-10-15 stsp goto done;
7376 2822a352 2019-10-15 stsp
7377 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7378 2822a352 2019-10-15 stsp if (err)
7379 2822a352 2019-10-15 stsp goto done;
7380 2822a352 2019-10-15 stsp
7381 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7382 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7383 2822a352 2019-10-15 stsp if (err)
7384 2822a352 2019-10-15 stsp goto sync;
7385 2822a352 2019-10-15 stsp
7386 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7387 2822a352 2019-10-15 stsp if (err)
7388 2822a352 2019-10-15 stsp goto sync;
7389 2822a352 2019-10-15 stsp
7390 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7391 6e210706 2021-01-22 stsp if (err)
7392 6e210706 2021-01-22 stsp goto sync;
7393 6e210706 2021-01-22 stsp
7394 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7395 2822a352 2019-10-15 stsp sync:
7396 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7397 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7398 2822a352 2019-10-15 stsp err = sync_err;
7399 2822a352 2019-10-15 stsp
7400 2822a352 2019-10-15 stsp done:
7401 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7402 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7403 2822a352 2019-10-15 stsp err = unlockerr;
7404 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7405 2822a352 2019-10-15 stsp
7406 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7407 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7408 2822a352 2019-10-15 stsp err = unlockerr;
7409 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7410 2822a352 2019-10-15 stsp
7411 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7412 2822a352 2019-10-15 stsp free(fileindex_path);
7413 2822a352 2019-10-15 stsp free(tree_id);
7414 2822a352 2019-10-15 stsp
7415 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7416 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7417 2822a352 2019-10-15 stsp err = unlockerr;
7418 2822a352 2019-10-15 stsp return err;
7419 2822a352 2019-10-15 stsp }
7420 2822a352 2019-10-15 stsp
7421 2822a352 2019-10-15 stsp const struct got_error *
7422 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7423 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7424 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7425 2822a352 2019-10-15 stsp {
7426 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7427 8b692cd0 2019-10-21 stsp
7428 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7429 8b692cd0 2019-10-21 stsp
7430 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7431 8b692cd0 2019-10-21 stsp
7432 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7433 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7434 8b692cd0 2019-10-21 stsp err = unlockerr;
7435 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7436 8b692cd0 2019-10-21 stsp
7437 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7438 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7439 8b692cd0 2019-10-21 stsp err = unlockerr;
7440 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7441 10604dce 2021-09-24 thomas
7442 10604dce 2021-09-24 thomas return err;
7443 10604dce 2021-09-24 thomas }
7444 10604dce 2021-09-24 thomas
7445 10604dce 2021-09-24 thomas const struct got_error *
7446 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7447 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7448 10604dce 2021-09-24 thomas {
7449 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7450 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7451 10604dce 2021-09-24 thomas
7452 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7453 10604dce 2021-09-24 thomas if (err)
7454 10604dce 2021-09-24 thomas goto done;
7455 8b692cd0 2019-10-21 stsp
7456 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7457 10604dce 2021-09-24 thomas
7458 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7459 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7460 10604dce 2021-09-24 thomas err = sync_err;
7461 10604dce 2021-09-24 thomas done:
7462 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7463 10604dce 2021-09-24 thomas free(fileindex_path);
7464 8b692cd0 2019-10-21 stsp return err;
7465 2822a352 2019-10-15 stsp }
7466 2822a352 2019-10-15 stsp
7467 10604dce 2021-09-24 thomas static const struct got_error *
7468 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7469 10604dce 2021-09-24 thomas {
7470 10604dce 2021-09-24 thomas const struct got_error *err;
7471 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7472 10604dce 2021-09-24 thomas
7473 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7474 10604dce 2021-09-24 thomas if (err)
7475 10604dce 2021-09-24 thomas goto done;
7476 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7477 10604dce 2021-09-24 thomas if (err)
7478 10604dce 2021-09-24 thomas goto done;
7479 10604dce 2021-09-24 thomas
7480 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7481 10604dce 2021-09-24 thomas if (err)
7482 10604dce 2021-09-24 thomas goto done;
7483 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7484 10604dce 2021-09-24 thomas if (err)
7485 10604dce 2021-09-24 thomas goto done;
7486 10604dce 2021-09-24 thomas
7487 10604dce 2021-09-24 thomas done:
7488 10604dce 2021-09-24 thomas free(branch_refname);
7489 10604dce 2021-09-24 thomas free(commit_refname);
7490 10604dce 2021-09-24 thomas return err;
7491 10604dce 2021-09-24 thomas }
7492 10604dce 2021-09-24 thomas
7493 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7494 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7495 10604dce 2021-09-24 thomas const char *branch_name;
7496 10604dce 2021-09-24 thomas };
7497 10604dce 2021-09-24 thomas
7498 10604dce 2021-09-24 thomas static const struct got_error *
7499 10604dce 2021-09-24 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7500 10604dce 2021-09-24 thomas void *arg)
7501 10604dce 2021-09-24 thomas {
7502 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7503 10604dce 2021-09-24 thomas
7504 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7505 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7506 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7507 10604dce 2021-09-24 thomas
7508 10604dce 2021-09-24 thomas return NULL;
7509 10604dce 2021-09-24 thomas }
7510 10604dce 2021-09-24 thomas
7511 10604dce 2021-09-24 thomas
7512 10604dce 2021-09-24 thomas const struct got_error *
7513 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7514 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7515 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7516 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7517 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7518 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7519 10604dce 2021-09-24 thomas {
7520 10604dce 2021-09-24 thomas const struct got_error *err;
7521 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7522 10604dce 2021-09-24 thomas
7523 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7524 10604dce 2021-09-24 thomas if (err)
7525 10604dce 2021-09-24 thomas goto done;
7526 10604dce 2021-09-24 thomas
7527 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7528 10604dce 2021-09-24 thomas worktree);
7529 10604dce 2021-09-24 thomas if (err)
7530 10604dce 2021-09-24 thomas goto done;
7531 10604dce 2021-09-24 thomas
7532 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7533 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7534 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7535 10604dce 2021-09-24 thomas done:
7536 10604dce 2021-09-24 thomas free(fileindex_path);
7537 10604dce 2021-09-24 thomas return err;
7538 10604dce 2021-09-24 thomas }
7539 10604dce 2021-09-24 thomas
7540 10604dce 2021-09-24 thomas const struct got_error *
7541 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7542 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7543 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7544 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7545 ae1e948a 2021-09-28 thomas struct got_repository *repo,
7546 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7547 ae1e948a 2021-09-28 thomas
7548 10604dce 2021-09-24 thomas {
7549 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7550 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7551 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7552 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7553 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7554 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7555 10604dce 2021-09-24 thomas int have_staged_files = 0;
7556 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7557 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7558 10604dce 2021-09-24 thomas
7559 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7560 10604dce 2021-09-24 thomas
7561 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7562 10604dce 2021-09-24 thomas
7563 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7564 10604dce 2021-09-24 thomas if (err)
7565 10604dce 2021-09-24 thomas goto done;
7566 10604dce 2021-09-24 thomas
7567 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7568 10604dce 2021-09-24 thomas if (err)
7569 10604dce 2021-09-24 thomas goto done;
7570 10604dce 2021-09-24 thomas
7571 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7572 10604dce 2021-09-24 thomas if (err)
7573 10604dce 2021-09-24 thomas goto done;
7574 10604dce 2021-09-24 thomas
7575 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7576 10604dce 2021-09-24 thomas &have_staged_files);
7577 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7578 10604dce 2021-09-24 thomas goto done;
7579 10604dce 2021-09-24 thomas if (have_staged_files) {
7580 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7581 10604dce 2021-09-24 thomas goto done;
7582 10604dce 2021-09-24 thomas }
7583 10604dce 2021-09-24 thomas
7584 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7585 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7586 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7587 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7588 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7589 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7590 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7591 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7592 10604dce 2021-09-24 thomas if (err)
7593 10604dce 2021-09-24 thomas goto done;
7594 10604dce 2021-09-24 thomas
7595 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7596 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7597 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7598 10604dce 2021-09-24 thomas goto done;
7599 10604dce 2021-09-24 thomas }
7600 10604dce 2021-09-24 thomas
7601 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7602 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7603 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7604 10604dce 2021-09-24 thomas
7605 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7606 10604dce 2021-09-24 thomas ct_path++;
7607 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7608 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7609 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7610 10604dce 2021-09-24 thomas if (err)
7611 10604dce 2021-09-24 thomas goto done;
7612 10604dce 2021-09-24 thomas
7613 10604dce 2021-09-24 thomas }
7614 10604dce 2021-09-24 thomas
7615 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7616 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7617 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7618 10604dce 2021-09-24 thomas head_commit_id, branch_tip, worktree, author, committer,
7619 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7620 10604dce 2021-09-24 thomas if (err)
7621 10604dce 2021-09-24 thomas goto done;
7622 10604dce 2021-09-24 thomas
7623 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7624 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7625 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7626 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7627 10604dce 2021-09-24 thomas err = sync_err;
7628 10604dce 2021-09-24 thomas done:
7629 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7630 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7631 10604dce 2021-09-24 thomas free_commitable(ct);
7632 10604dce 2021-09-24 thomas }
7633 10604dce 2021-09-24 thomas got_pathlist_free(&commitable_paths);
7634 10604dce 2021-09-24 thomas free(fileindex_path);
7635 10604dce 2021-09-24 thomas return err;
7636 10604dce 2021-09-24 thomas }
7637 10604dce 2021-09-24 thomas
7638 10604dce 2021-09-24 thomas const struct got_error *
7639 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
7640 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
7641 10604dce 2021-09-24 thomas {
7642 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7643 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7644 10604dce 2021-09-24 thomas
7645 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7646 10604dce 2021-09-24 thomas if (err)
7647 10604dce 2021-09-24 thomas goto done;
7648 10604dce 2021-09-24 thomas
7649 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7650 10604dce 2021-09-24 thomas if (err)
7651 10604dce 2021-09-24 thomas goto done;
7652 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7653 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7654 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7655 10604dce 2021-09-24 thomas err = sync_err;
7656 10604dce 2021-09-24 thomas done:
7657 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7658 10604dce 2021-09-24 thomas free(fileindex_path);
7659 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7660 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7661 10604dce 2021-09-24 thomas err = unlockerr;
7662 10604dce 2021-09-24 thomas return err;
7663 10604dce 2021-09-24 thomas }
7664 10604dce 2021-09-24 thomas
7665 10604dce 2021-09-24 thomas const struct got_error *
7666 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7667 10604dce 2021-09-24 thomas struct got_repository *repo)
7668 10604dce 2021-09-24 thomas {
7669 10604dce 2021-09-24 thomas const struct got_error *err;
7670 10604dce 2021-09-24 thomas char *branch_refname = NULL;
7671 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
7672 10604dce 2021-09-24 thomas
7673 10604dce 2021-09-24 thomas *in_progress = 0;
7674 10604dce 2021-09-24 thomas
7675 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7676 10604dce 2021-09-24 thomas if (err)
7677 10604dce 2021-09-24 thomas return err;
7678 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7679 dfe86d1f 2021-09-24 thomas free(branch_refname);
7680 10604dce 2021-09-24 thomas if (err) {
7681 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
7682 10604dce 2021-09-24 thomas return err;
7683 10604dce 2021-09-24 thomas } else
7684 10604dce 2021-09-24 thomas *in_progress = 1;
7685 10604dce 2021-09-24 thomas
7686 10604dce 2021-09-24 thomas return NULL;
7687 10604dce 2021-09-24 thomas }
7688 10604dce 2021-09-24 thomas
7689 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
7690 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
7691 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
7692 10604dce 2021-09-24 thomas {
7693 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
7694 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7695 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7696 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7697 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
7698 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7699 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
7700 10604dce 2021-09-24 thomas
7701 10604dce 2021-09-24 thomas *fileindex = NULL;
7702 10604dce 2021-09-24 thomas
7703 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7704 10604dce 2021-09-24 thomas if (err)
7705 10604dce 2021-09-24 thomas return err;
7706 10604dce 2021-09-24 thomas
7707 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7708 10604dce 2021-09-24 thomas if (err)
7709 10604dce 2021-09-24 thomas goto done;
7710 10604dce 2021-09-24 thomas
7711 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
7712 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
7713 10604dce 2021-09-24 thomas ok_arg.repo = repo;
7714 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7715 10604dce 2021-09-24 thomas &ok_arg);
7716 10604dce 2021-09-24 thomas if (err)
7717 10604dce 2021-09-24 thomas goto done;
7718 10604dce 2021-09-24 thomas
7719 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7720 10604dce 2021-09-24 thomas if (err)
7721 10604dce 2021-09-24 thomas return err;
7722 10604dce 2021-09-24 thomas
7723 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7724 10604dce 2021-09-24 thomas if (err)
7725 10604dce 2021-09-24 thomas return err;
7726 10604dce 2021-09-24 thomas
7727 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7728 10604dce 2021-09-24 thomas 0);
7729 10604dce 2021-09-24 thomas if (err)
7730 10604dce 2021-09-24 thomas goto done;
7731 10604dce 2021-09-24 thomas
7732 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_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 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7737 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7738 10604dce 2021-09-24 thomas goto done;
7739 10604dce 2021-09-24 thomas }
7740 10604dce 2021-09-24 thomas
7741 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
7742 10604dce 2021-09-24 thomas if (err)
7743 10604dce 2021-09-24 thomas goto done;
7744 10604dce 2021-09-24 thomas
7745 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7746 10604dce 2021-09-24 thomas if (err)
7747 10604dce 2021-09-24 thomas goto done;
7748 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
7749 10604dce 2021-09-24 thomas if (err)
7750 10604dce 2021-09-24 thomas goto done;
7751 10604dce 2021-09-24 thomas
7752 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7753 10604dce 2021-09-24 thomas if (err)
7754 10604dce 2021-09-24 thomas goto done;
7755 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
7756 10604dce 2021-09-24 thomas if (err)
7757 10604dce 2021-09-24 thomas goto done;
7758 10604dce 2021-09-24 thomas
7759 10604dce 2021-09-24 thomas done:
7760 10604dce 2021-09-24 thomas free(branch_refname);
7761 10604dce 2021-09-24 thomas free(commit_refname);
7762 10604dce 2021-09-24 thomas free(fileindex_path);
7763 10604dce 2021-09-24 thomas if (branch_ref)
7764 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7765 10604dce 2021-09-24 thomas if (commit_ref)
7766 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7767 10604dce 2021-09-24 thomas if (wt_branch)
7768 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
7769 10604dce 2021-09-24 thomas free(wt_branch_tip);
7770 10604dce 2021-09-24 thomas if (err) {
7771 10604dce 2021-09-24 thomas if (*fileindex) {
7772 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7773 10604dce 2021-09-24 thomas *fileindex = NULL;
7774 10604dce 2021-09-24 thomas }
7775 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7776 10604dce 2021-09-24 thomas }
7777 10604dce 2021-09-24 thomas return err;
7778 10604dce 2021-09-24 thomas }
7779 10604dce 2021-09-24 thomas
7780 10604dce 2021-09-24 thomas const struct got_error *
7781 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
7782 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7783 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
7784 10604dce 2021-09-24 thomas {
7785 10604dce 2021-09-24 thomas const struct got_error *err;
7786 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
7787 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7788 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7789 10604dce 2021-09-24 thomas int have_staged_files = 0;
7790 10604dce 2021-09-24 thomas
7791 10604dce 2021-09-24 thomas *branch_name = NULL;
7792 10604dce 2021-09-24 thomas *branch_tip = NULL;
7793 10604dce 2021-09-24 thomas *fileindex = NULL;
7794 10604dce 2021-09-24 thomas
7795 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7796 10604dce 2021-09-24 thomas if (err)
7797 10604dce 2021-09-24 thomas return err;
7798 10604dce 2021-09-24 thomas
7799 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7800 10604dce 2021-09-24 thomas if (err)
7801 10604dce 2021-09-24 thomas goto done;
7802 10604dce 2021-09-24 thomas
7803 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7804 10604dce 2021-09-24 thomas &have_staged_files);
7805 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7806 10604dce 2021-09-24 thomas goto done;
7807 10604dce 2021-09-24 thomas if (have_staged_files) {
7808 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
7809 10604dce 2021-09-24 thomas goto done;
7810 10604dce 2021-09-24 thomas }
7811 10604dce 2021-09-24 thomas
7812 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7813 10604dce 2021-09-24 thomas if (err)
7814 10604dce 2021-09-24 thomas goto done;
7815 10604dce 2021-09-24 thomas
7816 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7817 10604dce 2021-09-24 thomas if (err)
7818 10604dce 2021-09-24 thomas goto done;
7819 10604dce 2021-09-24 thomas
7820 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7821 10604dce 2021-09-24 thomas if (err)
7822 10604dce 2021-09-24 thomas goto done;
7823 10604dce 2021-09-24 thomas
7824 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
7825 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7826 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
7827 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
7828 10604dce 2021-09-24 thomas goto done;
7829 10604dce 2021-09-24 thomas }
7830 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7831 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
7832 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
7833 10604dce 2021-09-24 thomas goto done;
7834 10604dce 2021-09-24 thomas }
7835 10604dce 2021-09-24 thomas
7836 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7837 10604dce 2021-09-24 thomas if (err)
7838 10604dce 2021-09-24 thomas goto done;
7839 10604dce 2021-09-24 thomas
7840 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
7841 10604dce 2021-09-24 thomas if (err)
7842 10604dce 2021-09-24 thomas goto done;
7843 10604dce 2021-09-24 thomas done:
7844 10604dce 2021-09-24 thomas free(commit_refname);
7845 10604dce 2021-09-24 thomas free(branch_refname);
7846 10604dce 2021-09-24 thomas free(fileindex_path);
7847 10604dce 2021-09-24 thomas if (commit_ref)
7848 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7849 10604dce 2021-09-24 thomas if (branch_ref)
7850 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7851 10604dce 2021-09-24 thomas if (err) {
7852 10604dce 2021-09-24 thomas if (*branch_name) {
7853 10604dce 2021-09-24 thomas free(*branch_name);
7854 10604dce 2021-09-24 thomas *branch_name = NULL;
7855 10604dce 2021-09-24 thomas }
7856 10604dce 2021-09-24 thomas free(*branch_tip);
7857 10604dce 2021-09-24 thomas *branch_tip = NULL;
7858 10604dce 2021-09-24 thomas if (*fileindex) {
7859 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7860 10604dce 2021-09-24 thomas *fileindex = NULL;
7861 10604dce 2021-09-24 thomas }
7862 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7863 10604dce 2021-09-24 thomas }
7864 10604dce 2021-09-24 thomas return err;
7865 10604dce 2021-09-24 thomas }
7866 10604dce 2021-09-24 thomas
7867 10604dce 2021-09-24 thomas const struct got_error *
7868 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
7869 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
7870 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
7871 10604dce 2021-09-24 thomas {
7872 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7873 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
7874 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7875 10604dce 2021-09-24 thomas struct revert_file_args rfa;
7876 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
7877 10604dce 2021-09-24 thomas
7878 10604dce 2021-09-24 thomas err = got_object_id_by_path(&tree_id, repo,
7879 10604dce 2021-09-24 thomas worktree->base_commit_id, worktree->path_prefix);
7880 10604dce 2021-09-24 thomas if (err)
7881 10604dce 2021-09-24 thomas goto done;
7882 10604dce 2021-09-24 thomas
7883 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7884 10604dce 2021-09-24 thomas if (err)
7885 10604dce 2021-09-24 thomas goto done;
7886 10604dce 2021-09-24 thomas
7887 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7888 10604dce 2021-09-24 thomas if (err)
7889 10604dce 2021-09-24 thomas goto done;
7890 10604dce 2021-09-24 thomas
7891 10604dce 2021-09-24 thomas rfa.worktree = worktree;
7892 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
7893 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
7894 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
7895 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
7896 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
7897 10604dce 2021-09-24 thomas rfa.repo = repo;
7898 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
7899 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7900 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7901 10604dce 2021-09-24 thomas if (err)
7902 10604dce 2021-09-24 thomas goto sync;
7903 10604dce 2021-09-24 thomas
7904 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7905 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
7906 10604dce 2021-09-24 thomas sync:
7907 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7908 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7909 10604dce 2021-09-24 thomas err = sync_err;
7910 10604dce 2021-09-24 thomas done:
7911 10604dce 2021-09-24 thomas free(tree_id);
7912 10604dce 2021-09-24 thomas free(commit_id);
7913 10604dce 2021-09-24 thomas if (fileindex)
7914 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7915 10604dce 2021-09-24 thomas free(fileindex_path);
7916 10604dce 2021-09-24 thomas
7917 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7918 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7919 10604dce 2021-09-24 thomas err = unlockerr;
7920 10604dce 2021-09-24 thomas return err;
7921 10604dce 2021-09-24 thomas }
7922 10604dce 2021-09-24 thomas
7923 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7924 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7925 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7926 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7927 2db2652d 2019-08-07 stsp struct got_repository *repo;
7928 2db2652d 2019-08-07 stsp int have_changes;
7929 2db2652d 2019-08-07 stsp };
7930 2db2652d 2019-08-07 stsp
7931 2db2652d 2019-08-07 stsp const struct got_error *
7932 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7933 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7934 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7935 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7936 735ef5ac 2019-08-03 stsp {
7937 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7938 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7939 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7940 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7941 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7942 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7943 8b13ce36 2019-08-08 stsp
7944 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7945 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7946 8b13ce36 2019-08-08 stsp return NULL;
7947 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7948 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7949 735ef5ac 2019-08-03 stsp
7950 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7951 735ef5ac 2019-08-03 stsp if (ie == NULL)
7952 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7953 735ef5ac 2019-08-03 stsp
7954 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7955 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7956 735ef5ac 2019-08-03 stsp relpath) == -1)
7957 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7958 735ef5ac 2019-08-03 stsp
7959 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7960 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7961 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7962 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7963 735ef5ac 2019-08-03 stsp }
7964 735ef5ac 2019-08-03 stsp
7965 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7966 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7967 3aa5969e 2019-08-06 stsp goto done;
7968 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7969 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7970 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7971 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7972 735ef5ac 2019-08-03 stsp goto done;
7973 3aa5969e 2019-08-06 stsp }
7974 735ef5ac 2019-08-03 stsp
7975 2db2652d 2019-08-07 stsp a->have_changes = 1;
7976 2db2652d 2019-08-07 stsp
7977 735ef5ac 2019-08-03 stsp p = in_repo_path;
7978 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7979 735ef5ac 2019-08-03 stsp p++;
7980 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7981 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7982 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7983 735ef5ac 2019-08-03 stsp done:
7984 735ef5ac 2019-08-03 stsp free(in_repo_path);
7985 dc424a06 2019-08-07 stsp return err;
7986 dc424a06 2019-08-07 stsp }
7987 dc424a06 2019-08-07 stsp
7988 2db2652d 2019-08-07 stsp struct stage_path_arg {
7989 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7990 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7991 2db2652d 2019-08-07 stsp struct got_repository *repo;
7992 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7993 2db2652d 2019-08-07 stsp void *status_arg;
7994 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7995 2db2652d 2019-08-07 stsp void *patch_arg;
7996 7b5dc508 2019-10-28 stsp int staged_something;
7997 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7998 2db2652d 2019-08-07 stsp };
7999 2db2652d 2019-08-07 stsp
8000 2db2652d 2019-08-07 stsp static const struct got_error *
8001 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8002 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8003 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8004 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8005 0cb83759 2019-08-03 stsp {
8006 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8007 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8008 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8009 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8010 0cb83759 2019-08-03 stsp uint32_t stage;
8011 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8012 0aeb8099 2020-07-23 stsp struct stat sb;
8013 8b13ce36 2019-08-08 stsp
8014 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8015 8b13ce36 2019-08-08 stsp return NULL;
8016 0cb83759 2019-08-03 stsp
8017 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8018 d3e7c587 2019-08-03 stsp if (ie == NULL)
8019 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8020 0cb83759 2019-08-03 stsp
8021 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8022 2db2652d 2019-08-07 stsp relpath)== -1)
8023 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8024 0cb83759 2019-08-03 stsp
8025 0cb83759 2019-08-03 stsp switch (status) {
8026 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8027 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8028 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8029 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8030 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8031 0aeb8099 2020-07-23 stsp break;
8032 0aeb8099 2020-07-23 stsp }
8033 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8034 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8035 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8036 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8037 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8038 dc424a06 2019-08-07 stsp if (err)
8039 dc424a06 2019-08-07 stsp break;
8040 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8041 dc424a06 2019-08-07 stsp break;
8042 dc424a06 2019-08-07 stsp } else {
8043 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8044 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8045 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8046 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8047 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8048 dc424a06 2019-08-07 stsp break;
8049 dc424a06 2019-08-07 stsp }
8050 dc424a06 2019-08-07 stsp }
8051 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8052 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8053 0cb83759 2019-08-03 stsp if (err)
8054 dc424a06 2019-08-07 stsp break;
8055 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8056 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8057 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8058 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8059 0cb83759 2019-08-03 stsp else
8060 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8061 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8062 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8063 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8064 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8065 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8066 35213c7c 2020-07-23 stsp ssize_t target_len;
8067 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8068 35213c7c 2020-07-23 stsp sizeof(target_path));
8069 35213c7c 2020-07-23 stsp if (target_len == -1) {
8070 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8071 35213c7c 2020-07-23 stsp ondisk_path);
8072 35213c7c 2020-07-23 stsp break;
8073 35213c7c 2020-07-23 stsp }
8074 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8075 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8076 35213c7c 2020-07-23 stsp a->worktree->root_path);
8077 35213c7c 2020-07-23 stsp if (err)
8078 35213c7c 2020-07-23 stsp break;
8079 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8080 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8081 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8082 35213c7c 2020-07-23 stsp break;
8083 35213c7c 2020-07-23 stsp }
8084 35213c7c 2020-07-23 stsp }
8085 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8086 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8087 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8088 35213c7c 2020-07-23 stsp else
8089 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8090 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8091 0aeb8099 2020-07-23 stsp } else {
8092 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8093 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8094 0aeb8099 2020-07-23 stsp }
8095 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8096 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8097 dc424a06 2019-08-07 stsp break;
8098 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8099 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8100 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8101 0cb83759 2019-08-03 stsp break;
8102 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8103 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8104 d3e7c587 2019-08-03 stsp break;
8105 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8106 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8107 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8108 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8109 dc424a06 2019-08-07 stsp if (err)
8110 dc424a06 2019-08-07 stsp break;
8111 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8112 88f33a19 2019-08-08 stsp break;
8113 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8114 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8115 dc424a06 2019-08-07 stsp break;
8116 88f33a19 2019-08-08 stsp }
8117 dc424a06 2019-08-07 stsp }
8118 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8119 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8120 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8121 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8122 dc424a06 2019-08-07 stsp break;
8123 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8124 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8125 12463d8b 2019-12-13 stsp de_name);
8126 0cb83759 2019-08-03 stsp break;
8127 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8128 d3e7c587 2019-08-03 stsp break;
8129 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8130 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8131 ebf48fd5 2019-08-03 stsp break;
8132 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8133 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8134 2a06fe5f 2019-08-24 stsp break;
8135 0cb83759 2019-08-03 stsp default:
8136 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8137 537ac44b 2019-08-03 stsp break;
8138 0cb83759 2019-08-03 stsp }
8139 dc424a06 2019-08-07 stsp
8140 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8141 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8142 dc424a06 2019-08-07 stsp free(path_content);
8143 2db2652d 2019-08-07 stsp free(ondisk_path);
8144 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8145 0ebf8283 2019-07-24 stsp return err;
8146 0ebf8283 2019-07-24 stsp }
8147 0cb83759 2019-08-03 stsp
8148 0cb83759 2019-08-03 stsp const struct got_error *
8149 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8150 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8151 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8152 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8153 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8154 0cb83759 2019-08-03 stsp {
8155 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8156 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8157 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8158 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8159 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8160 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8161 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8162 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8163 0cb83759 2019-08-03 stsp
8164 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8165 0cb83759 2019-08-03 stsp if (err)
8166 0cb83759 2019-08-03 stsp return err;
8167 0cb83759 2019-08-03 stsp
8168 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8169 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8170 735ef5ac 2019-08-03 stsp if (err)
8171 735ef5ac 2019-08-03 stsp goto done;
8172 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8173 735ef5ac 2019-08-03 stsp if (err)
8174 735ef5ac 2019-08-03 stsp goto done;
8175 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8176 0cb83759 2019-08-03 stsp if (err)
8177 0cb83759 2019-08-03 stsp goto done;
8178 0cb83759 2019-08-03 stsp
8179 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8180 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8181 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8182 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8183 2db2652d 2019-08-07 stsp oka.repo = repo;
8184 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8185 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8186 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8187 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8188 735ef5ac 2019-08-03 stsp if (err)
8189 735ef5ac 2019-08-03 stsp goto done;
8190 735ef5ac 2019-08-03 stsp }
8191 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8192 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8193 2db2652d 2019-08-07 stsp goto done;
8194 2db2652d 2019-08-07 stsp }
8195 735ef5ac 2019-08-03 stsp
8196 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8197 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8198 2db2652d 2019-08-07 stsp spa.repo = repo;
8199 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8200 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8201 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8202 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8203 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8204 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8205 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8206 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8207 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8208 42005733 2019-08-03 stsp if (err)
8209 2db2652d 2019-08-07 stsp goto done;
8210 7b5dc508 2019-10-28 stsp }
8211 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8212 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8213 7b5dc508 2019-10-28 stsp goto done;
8214 0cb83759 2019-08-03 stsp }
8215 0cb83759 2019-08-03 stsp
8216 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8217 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8218 0cb83759 2019-08-03 stsp err = sync_err;
8219 0cb83759 2019-08-03 stsp done:
8220 735ef5ac 2019-08-03 stsp if (head_ref)
8221 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8222 735ef5ac 2019-08-03 stsp free(head_commit_id);
8223 0cb83759 2019-08-03 stsp free(fileindex_path);
8224 0cb83759 2019-08-03 stsp if (fileindex)
8225 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8226 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8227 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8228 0cb83759 2019-08-03 stsp err = unlockerr;
8229 0cb83759 2019-08-03 stsp return err;
8230 0cb83759 2019-08-03 stsp }
8231 ad493afc 2019-08-03 stsp
8232 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8233 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8234 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8235 ad493afc 2019-08-03 stsp struct got_repository *repo;
8236 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8237 ad493afc 2019-08-03 stsp void *progress_arg;
8238 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8239 2e1f37b0 2019-08-08 stsp void *patch_arg;
8240 ad493afc 2019-08-03 stsp };
8241 2e1f37b0 2019-08-08 stsp
8242 2e1f37b0 2019-08-08 stsp static const struct got_error *
8243 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8244 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8245 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8246 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8247 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8248 2e1f37b0 2019-08-08 stsp {
8249 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8250 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8251 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8252 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8253 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8254 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8255 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8256 2e1f37b0 2019-08-08 stsp
8257 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8258 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8259 2e1f37b0 2019-08-08 stsp
8260 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8261 2e1f37b0 2019-08-08 stsp if (err)
8262 2e1f37b0 2019-08-08 stsp return err;
8263 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8264 2e1f37b0 2019-08-08 stsp if (err)
8265 2e1f37b0 2019-08-08 stsp goto done;
8266 2e1f37b0 2019-08-08 stsp
8267 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8268 2e1f37b0 2019-08-08 stsp if (err)
8269 2e1f37b0 2019-08-08 stsp goto done;
8270 2e1f37b0 2019-08-08 stsp
8271 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8272 2e1f37b0 2019-08-08 stsp if (err)
8273 2e1f37b0 2019-08-08 stsp goto done;
8274 2e1f37b0 2019-08-08 stsp
8275 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8276 2e1f37b0 2019-08-08 stsp if (err)
8277 2e1f37b0 2019-08-08 stsp goto done;
8278 2e1f37b0 2019-08-08 stsp
8279 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8280 2e1f37b0 2019-08-08 stsp if (err)
8281 2e1f37b0 2019-08-08 stsp goto done;
8282 ad493afc 2019-08-03 stsp
8283 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8284 2e1f37b0 2019-08-08 stsp if (err)
8285 2e1f37b0 2019-08-08 stsp goto done;
8286 2e1f37b0 2019-08-08 stsp
8287 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8288 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
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 err = got_opentemp_named(path_unstaged_content, &outfile,
8293 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8294 2e1f37b0 2019-08-08 stsp if (err)
8295 2e1f37b0 2019-08-08 stsp goto done;
8296 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8297 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8298 2e1f37b0 2019-08-08 stsp if (err)
8299 2e1f37b0 2019-08-08 stsp goto done;
8300 2e1f37b0 2019-08-08 stsp
8301 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8302 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8303 2e1f37b0 2019-08-08 stsp goto done;
8304 2e1f37b0 2019-08-08 stsp }
8305 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8306 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8307 2e1f37b0 2019-08-08 stsp goto done;
8308 2e1f37b0 2019-08-08 stsp }
8309 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8310 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8311 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
8312 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8313 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8314 fe621944 2020-11-10 stsp nchanges++;
8315 fe621944 2020-11-10 stsp }
8316 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8317 2e1f37b0 2019-08-08 stsp int choice;
8318 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8319 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8320 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8321 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8322 2e1f37b0 2019-08-08 stsp if (err)
8323 2e1f37b0 2019-08-08 stsp goto done;
8324 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8325 2e1f37b0 2019-08-08 stsp have_content = 1;
8326 19e4b907 2019-08-08 stsp else
8327 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8328 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8329 2e1f37b0 2019-08-08 stsp break;
8330 2e1f37b0 2019-08-08 stsp }
8331 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8332 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8333 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8334 2e1f37b0 2019-08-08 stsp done:
8335 2e1f37b0 2019-08-08 stsp free(label1);
8336 2e1f37b0 2019-08-08 stsp if (blob)
8337 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8338 2e1f37b0 2019-08-08 stsp if (staged_blob)
8339 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8340 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8341 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8342 fe621944 2020-11-10 stsp err = free_err;
8343 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8344 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8345 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8346 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8347 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8348 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8349 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8350 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8351 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8352 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8353 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8354 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8355 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8356 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8357 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8358 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8359 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8360 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8361 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8362 2e1f37b0 2019-08-08 stsp }
8363 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8364 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8365 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8366 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8367 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8368 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8369 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8370 2e1f37b0 2019-08-08 stsp }
8371 2e1f37b0 2019-08-08 stsp free(path1);
8372 2e1f37b0 2019-08-08 stsp free(path2);
8373 fda8017d 2020-07-23 stsp return err;
8374 fda8017d 2020-07-23 stsp }
8375 fda8017d 2020-07-23 stsp
8376 fda8017d 2020-07-23 stsp static const struct got_error *
8377 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8378 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8379 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8380 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8381 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8382 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8383 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8384 fda8017d 2020-07-23 stsp {
8385 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8386 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8387 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8388 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8389 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8390 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8391 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8392 fda8017d 2020-07-23 stsp struct stat sb;
8393 fda8017d 2020-07-23 stsp
8394 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8395 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8396 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8397 fda8017d 2020-07-23 stsp if (err)
8398 fda8017d 2020-07-23 stsp return err;
8399 fda8017d 2020-07-23 stsp
8400 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8401 fda8017d 2020-07-23 stsp return NULL;
8402 fda8017d 2020-07-23 stsp
8403 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8404 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8405 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8406 fda8017d 2020-07-23 stsp if (err)
8407 fda8017d 2020-07-23 stsp goto done;
8408 fda8017d 2020-07-23 stsp }
8409 fda8017d 2020-07-23 stsp
8410 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
8411 fda8017d 2020-07-23 stsp if (f == NULL) {
8412 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8413 fda8017d 2020-07-23 stsp path_unstaged_content);
8414 fda8017d 2020-07-23 stsp goto done;
8415 fda8017d 2020-07-23 stsp }
8416 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8417 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8418 fda8017d 2020-07-23 stsp goto done;
8419 fda8017d 2020-07-23 stsp }
8420 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8421 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8422 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8423 fda8017d 2020-07-23 stsp size_t r;
8424 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8425 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8426 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8427 fda8017d 2020-07-23 stsp goto done;
8428 fda8017d 2020-07-23 stsp }
8429 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8430 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8431 fda8017d 2020-07-23 stsp goto done;
8432 fda8017d 2020-07-23 stsp }
8433 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8434 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8435 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8436 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8437 fda8017d 2020-07-23 stsp progress_arg);
8438 fda8017d 2020-07-23 stsp } else {
8439 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8440 67a66647 2021-05-31 stsp
8441 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8442 67a66647 2021-05-31 stsp if (err)
8443 67a66647 2021-05-31 stsp return err;
8444 67a66647 2021-05-31 stsp
8445 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8446 67a66647 2021-05-31 stsp parent) == -1) {
8447 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8448 67a66647 2021-05-31 stsp base_path = NULL;
8449 67a66647 2021-05-31 stsp goto done;
8450 67a66647 2021-05-31 stsp }
8451 67a66647 2021-05-31 stsp
8452 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8453 67a66647 2021-05-31 stsp if (err)
8454 67a66647 2021-05-31 stsp goto done;
8455 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8456 67a66647 2021-05-31 stsp blob_base);
8457 67a66647 2021-05-31 stsp if (err)
8458 67a66647 2021-05-31 stsp goto done;
8459 67a66647 2021-05-31 stsp
8460 eec2f5a9 2021-06-03 stsp /*
8461 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8462 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8463 eec2f5a9 2021-06-03 stsp */
8464 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8465 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8466 eec2f5a9 2021-06-03 stsp ondisk_path);
8467 eec2f5a9 2021-06-03 stsp if (err)
8468 eec2f5a9 2021-06-03 stsp goto done;
8469 eec2f5a9 2021-06-03 stsp } else {
8470 eec2f5a9 2021-06-03 stsp int fd;
8471 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8472 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8473 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8474 eec2f5a9 2021-06-03 stsp goto done;
8475 eec2f5a9 2021-06-03 stsp }
8476 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8477 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8478 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8479 eec2f5a9 2021-06-03 stsp close(fd);
8480 eec2f5a9 2021-06-03 stsp goto done;
8481 eec2f5a9 2021-06-03 stsp }
8482 eec2f5a9 2021-06-03 stsp }
8483 eec2f5a9 2021-06-03 stsp
8484 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8485 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8486 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8487 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8488 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8489 fda8017d 2020-07-23 stsp }
8490 fda8017d 2020-07-23 stsp if (err)
8491 fda8017d 2020-07-23 stsp goto done;
8492 fda8017d 2020-07-23 stsp
8493 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8494 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8495 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8496 2ac8aa02 2020-11-09 stsp } else {
8497 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8498 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8499 2ac8aa02 2020-11-09 stsp }
8500 fda8017d 2020-07-23 stsp done:
8501 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8502 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8503 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8504 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8505 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8506 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8507 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8508 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8509 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8510 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8511 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8512 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8513 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8514 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8515 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8516 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8517 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8518 67a66647 2021-05-31 stsp free(blob_base_path);
8519 67a66647 2021-05-31 stsp free(parent);
8520 67a66647 2021-05-31 stsp free(base_path);
8521 2e1f37b0 2019-08-08 stsp return err;
8522 2e1f37b0 2019-08-08 stsp }
8523 2e1f37b0 2019-08-08 stsp
8524 ad493afc 2019-08-03 stsp static const struct got_error *
8525 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8526 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8527 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8528 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8529 ad493afc 2019-08-03 stsp {
8530 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8531 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8532 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8533 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8534 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8535 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8536 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8537 9bc94a15 2019-08-03 stsp struct stat sb;
8538 ad493afc 2019-08-03 stsp
8539 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8540 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8541 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8542 2e1f37b0 2019-08-08 stsp return NULL;
8543 2e1f37b0 2019-08-08 stsp
8544 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8545 ad493afc 2019-08-03 stsp if (ie == NULL)
8546 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8547 9bc94a15 2019-08-03 stsp
8548 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8549 9bc94a15 2019-08-03 stsp == -1)
8550 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8551 ad493afc 2019-08-03 stsp
8552 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8553 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8554 f69721c3 2019-10-21 stsp if (err)
8555 f69721c3 2019-10-21 stsp goto done;
8556 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8557 f69721c3 2019-10-21 stsp id_str) == -1) {
8558 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8559 f69721c3 2019-10-21 stsp goto done;
8560 f69721c3 2019-10-21 stsp }
8561 f69721c3 2019-10-21 stsp
8562 ad493afc 2019-08-03 stsp switch (staged_status) {
8563 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8564 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8565 ad493afc 2019-08-03 stsp blob_id, 8192);
8566 ad493afc 2019-08-03 stsp if (err)
8567 ad493afc 2019-08-03 stsp break;
8568 ad493afc 2019-08-03 stsp /* fall through */
8569 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8570 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8571 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8572 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8573 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8574 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8575 2e1f37b0 2019-08-08 stsp if (err)
8576 2e1f37b0 2019-08-08 stsp break;
8577 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8578 2e1f37b0 2019-08-08 stsp break;
8579 2e1f37b0 2019-08-08 stsp } else {
8580 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8581 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8582 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8583 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8584 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8585 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8586 2e1f37b0 2019-08-08 stsp }
8587 2e1f37b0 2019-08-08 stsp }
8588 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8589 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8590 ad493afc 2019-08-03 stsp if (err)
8591 ad493afc 2019-08-03 stsp break;
8592 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8593 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8594 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8595 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8596 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8597 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8598 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8599 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8600 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8601 ea7786be 2020-07-23 stsp break;
8602 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8603 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8604 36bf999c 2020-07-23 stsp char *staged_target;
8605 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8606 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8607 36bf999c 2020-07-23 stsp if (err)
8608 36bf999c 2020-07-23 stsp goto done;
8609 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8610 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8611 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8612 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8613 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8614 36bf999c 2020-07-23 stsp free(staged_target);
8615 dfe9fba0 2020-07-23 stsp } else {
8616 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8617 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8618 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8619 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8620 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8621 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8622 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8623 dfe9fba0 2020-07-23 stsp }
8624 ea7786be 2020-07-23 stsp break;
8625 ea7786be 2020-07-23 stsp default:
8626 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8627 ea7786be 2020-07-23 stsp break;
8628 ea7786be 2020-07-23 stsp }
8629 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8630 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8631 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8632 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8633 2ac8aa02 2020-11-09 stsp }
8634 ad493afc 2019-08-03 stsp break;
8635 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8636 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8637 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8638 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8639 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8640 2e1f37b0 2019-08-08 stsp if (err)
8641 2e1f37b0 2019-08-08 stsp break;
8642 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8643 2e1f37b0 2019-08-08 stsp break;
8644 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8645 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8646 2e1f37b0 2019-08-08 stsp break;
8647 2e1f37b0 2019-08-08 stsp }
8648 2e1f37b0 2019-08-08 stsp }
8649 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8650 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8651 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8652 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8653 9bc94a15 2019-08-03 stsp if (err)
8654 9bc94a15 2019-08-03 stsp break;
8655 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8656 ad493afc 2019-08-03 stsp break;
8657 ad493afc 2019-08-03 stsp }
8658 f69721c3 2019-10-21 stsp done:
8659 ad493afc 2019-08-03 stsp free(ondisk_path);
8660 ad493afc 2019-08-03 stsp if (blob_base)
8661 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8662 ad493afc 2019-08-03 stsp if (blob_staged)
8663 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8664 f69721c3 2019-10-21 stsp free(id_str);
8665 f69721c3 2019-10-21 stsp free(label_orig);
8666 ad493afc 2019-08-03 stsp return err;
8667 ad493afc 2019-08-03 stsp }
8668 ad493afc 2019-08-03 stsp
8669 ad493afc 2019-08-03 stsp const struct got_error *
8670 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8671 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8672 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8673 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8674 ad493afc 2019-08-03 stsp struct got_repository *repo)
8675 ad493afc 2019-08-03 stsp {
8676 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8677 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8678 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8679 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8680 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8681 ad493afc 2019-08-03 stsp
8682 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8683 ad493afc 2019-08-03 stsp if (err)
8684 ad493afc 2019-08-03 stsp return err;
8685 ad493afc 2019-08-03 stsp
8686 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8687 ad493afc 2019-08-03 stsp if (err)
8688 ad493afc 2019-08-03 stsp goto done;
8689 ad493afc 2019-08-03 stsp
8690 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8691 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8692 ad493afc 2019-08-03 stsp upa.repo = repo;
8693 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8694 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8695 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8696 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8697 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8698 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8699 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
8700 ad493afc 2019-08-03 stsp if (err)
8701 ad493afc 2019-08-03 stsp goto done;
8702 ad493afc 2019-08-03 stsp }
8703 ad493afc 2019-08-03 stsp
8704 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8705 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8706 ad493afc 2019-08-03 stsp err = sync_err;
8707 ad493afc 2019-08-03 stsp done:
8708 ad493afc 2019-08-03 stsp free(fileindex_path);
8709 ad493afc 2019-08-03 stsp if (fileindex)
8710 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8711 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8712 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8713 ad493afc 2019-08-03 stsp err = unlockerr;
8714 ad493afc 2019-08-03 stsp return err;
8715 ad493afc 2019-08-03 stsp }
8716 b2118c49 2020-07-28 stsp
8717 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8718 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8719 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8720 b2118c49 2020-07-28 stsp void *info_arg;
8721 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8722 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8723 b2118c49 2020-07-28 stsp void *cancel_arg;
8724 b2118c49 2020-07-28 stsp };
8725 b2118c49 2020-07-28 stsp
8726 b2118c49 2020-07-28 stsp static const struct got_error *
8727 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8728 b2118c49 2020-07-28 stsp {
8729 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8730 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8731 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8732 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8733 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8734 b2118c49 2020-07-28 stsp int stage;
8735 b2118c49 2020-07-28 stsp
8736 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8737 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8738 b2118c49 2020-07-28 stsp
8739 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8740 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8741 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8742 b2118c49 2020-07-28 stsp break;
8743 b2118c49 2020-07-28 stsp }
8744 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8745 b2118c49 2020-07-28 stsp return NULL;
8746 b2118c49 2020-07-28 stsp
8747 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8748 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8749 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8750 b2118c49 2020-07-28 stsp }
8751 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8752 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8753 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8754 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8755 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8756 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8757 b2118c49 2020-07-28 stsp }
8758 b2118c49 2020-07-28 stsp
8759 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8760 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8761 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8762 b2118c49 2020-07-28 stsp }
8763 b2118c49 2020-07-28 stsp
8764 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8765 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8766 b2118c49 2020-07-28 stsp }
8767 b2118c49 2020-07-28 stsp
8768 b2118c49 2020-07-28 stsp const struct got_error *
8769 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8770 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8771 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8772 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8773 b2118c49 2020-07-28 stsp
8774 b2118c49 2020-07-28 stsp {
8775 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8776 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8777 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8778 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8779 b2118c49 2020-07-28 stsp
8780 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8781 b2118c49 2020-07-28 stsp if (err)
8782 b2118c49 2020-07-28 stsp return err;
8783 b2118c49 2020-07-28 stsp
8784 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8785 b2118c49 2020-07-28 stsp if (err)
8786 b2118c49 2020-07-28 stsp goto done;
8787 b2118c49 2020-07-28 stsp
8788 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8789 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8790 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8791 b2118c49 2020-07-28 stsp arg.paths = paths;
8792 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8793 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8794 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8795 b2118c49 2020-07-28 stsp &arg);
8796 b2118c49 2020-07-28 stsp done:
8797 b2118c49 2020-07-28 stsp free(fileindex_path);
8798 b2118c49 2020-07-28 stsp if (fileindex)
8799 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8800 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8801 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8802 b2118c49 2020-07-28 stsp err = unlockerr;
8803 b2118c49 2020-07-28 stsp return err;
8804 b2118c49 2020-07-28 stsp }