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 4fccd2fe 2023-03-08 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 86c3caaf 2018-03-09 stsp
19 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 86c3caaf 2018-03-09 stsp
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 12ce7a6c 2019-08-12 stsp #include <limits.h>
24 f5c49f82 2019-01-06 stsp #include <stddef.h>
25 86c3caaf 2018-03-09 stsp #include <string.h>
26 86c3caaf 2018-03-09 stsp #include <stdio.h>
27 86c3caaf 2018-03-09 stsp #include <stdlib.h>
28 303e14b5 2019-09-23 stsp #include <time.h>
29 86c3caaf 2018-03-09 stsp #include <fcntl.h>
30 86c3caaf 2018-03-09 stsp #include <errno.h>
31 86c3caaf 2018-03-09 stsp #include <unistd.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.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 be288a59 2023-02-23 thomas #include "got_lib_hash.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 a2c162eb 2022-10-30 thomas
65 a807c50b 2023-07-05 thomas static mode_t
66 a807c50b 2023-07-05 thomas apply_umask(mode_t mode)
67 a807c50b 2023-07-05 thomas {
68 a807c50b 2023-07-05 thomas mode_t um;
69 a807c50b 2023-07-05 thomas
70 a807c50b 2023-07-05 thomas um = umask(000);
71 a807c50b 2023-07-05 thomas umask(um);
72 a807c50b 2023-07-05 thomas return mode & ~um;
73 a807c50b 2023-07-05 thomas }
74 86c3caaf 2018-03-09 stsp
75 99724ed4 2018-03-10 stsp static const struct got_error *
76 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
77 99724ed4 2018-03-10 stsp {
78 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
79 99724ed4 2018-03-10 stsp char *path;
80 99724ed4 2018-03-10 stsp
81 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
82 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
83 99724ed4 2018-03-10 stsp
84 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
85 99724ed4 2018-03-10 stsp free(path);
86 507dc3bb 2018-12-29 stsp return err;
87 507dc3bb 2018-12-29 stsp }
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp static const struct got_error *
90 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
91 507dc3bb 2018-12-29 stsp {
92 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
93 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
94 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
95 507dc3bb 2018-12-29 stsp char *path = NULL;
96 507dc3bb 2018-12-29 stsp
97 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
99 507dc3bb 2018-12-29 stsp path = NULL;
100 507dc3bb 2018-12-29 stsp goto done;
101 507dc3bb 2018-12-29 stsp }
102 507dc3bb 2018-12-29 stsp
103 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
104 507dc3bb 2018-12-29 stsp if (err)
105 507dc3bb 2018-12-29 stsp goto done;
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (content) {
108 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
109 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
110 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
111 507dc3bb 2018-12-29 stsp goto done;
112 507dc3bb 2018-12-29 stsp }
113 507dc3bb 2018-12-29 stsp }
114 507dc3bb 2018-12-29 stsp
115 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
116 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
117 2a57020b 2019-02-20 stsp unlink(tmppath);
118 507dc3bb 2018-12-29 stsp goto done;
119 507dc3bb 2018-12-29 stsp }
120 507dc3bb 2018-12-29 stsp
121 507dc3bb 2018-12-29 stsp done:
122 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
123 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
124 230a42bd 2019-05-11 jcs free(tmppath);
125 09fe317a 2018-03-11 stsp return err;
126 09fe317a 2018-03-11 stsp }
127 09fe317a 2018-03-11 stsp
128 024e9686 2019-05-14 stsp static const struct got_error *
129 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
130 024e9686 2019-05-14 stsp {
131 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
132 024e9686 2019-05-14 stsp char *refstr = NULL;
133 024e9686 2019-05-14 stsp
134 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
135 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
136 024e9686 2019-05-14 stsp if (refstr == NULL)
137 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
138 024e9686 2019-05-14 stsp } else {
139 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
140 024e9686 2019-05-14 stsp if (refstr == NULL)
141 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
142 024e9686 2019-05-14 stsp }
143 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
144 024e9686 2019-05-14 stsp free(refstr);
145 024e9686 2019-05-14 stsp return err;
146 024e9686 2019-05-14 stsp }
147 024e9686 2019-05-14 stsp
148 86c3caaf 2018-03-09 stsp const struct got_error *
149 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
150 ad10f64e 2023-07-19 thomas const char *prefix, const char *meta_dir, struct got_repository *repo)
151 86c3caaf 2018-03-09 stsp {
152 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
153 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
154 ec22038e 2019-03-10 stsp uuid_t uuid;
155 ec22038e 2019-03-10 stsp uint32_t uuid_status;
156 65596e15 2018-12-24 stsp int obj_type;
157 7ac97322 2018-03-11 stsp char *path_got = NULL;
158 1451e70d 2018-03-10 stsp char *formatstr = NULL;
159 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
160 65596e15 2018-12-24 stsp char *basestr = NULL;
161 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
162 65596e15 2018-12-24 stsp
163 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
164 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
165 0c48fee2 2019-03-11 stsp goto done;
166 0c48fee2 2019-03-11 stsp }
167 0c48fee2 2019-03-11 stsp
168 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
169 65596e15 2018-12-24 stsp if (err)
170 65596e15 2018-12-24 stsp return err;
171 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
172 65596e15 2018-12-24 stsp if (err)
173 65596e15 2018-12-24 stsp return err;
174 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
175 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
176 86c3caaf 2018-03-09 stsp
177 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
178 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
179 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
180 0bb8a95e 2018-03-12 stsp }
181 577ec78f 2018-03-11 stsp
182 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
183 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
185 86c3caaf 2018-03-09 stsp goto done;
186 86c3caaf 2018-03-09 stsp }
187 86c3caaf 2018-03-09 stsp
188 ad10f64e 2023-07-19 thomas /* Create .got/.cvg directory (may already exist). */
189 ad10f64e 2023-07-19 thomas if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
190 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
191 86c3caaf 2018-03-09 stsp goto done;
192 86c3caaf 2018-03-09 stsp }
193 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
194 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
195 86c3caaf 2018-03-09 stsp goto done;
196 86c3caaf 2018-03-09 stsp }
197 86c3caaf 2018-03-09 stsp
198 056e7441 2018-03-11 stsp /* Create an empty lock file. */
199 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
200 056e7441 2018-03-11 stsp if (err)
201 056e7441 2018-03-11 stsp goto done;
202 056e7441 2018-03-11 stsp
203 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
204 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
205 99724ed4 2018-03-10 stsp if (err)
206 86c3caaf 2018-03-09 stsp goto done;
207 86c3caaf 2018-03-09 stsp
208 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
209 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
210 65596e15 2018-12-24 stsp if (err)
211 65596e15 2018-12-24 stsp goto done;
212 65596e15 2018-12-24 stsp
213 65596e15 2018-12-24 stsp /* Record our base commit. */
214 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
215 65596e15 2018-12-24 stsp if (err)
216 65596e15 2018-12-24 stsp goto done;
217 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
218 99724ed4 2018-03-10 stsp if (err)
219 86c3caaf 2018-03-09 stsp goto done;
220 86c3caaf 2018-03-09 stsp
221 1451e70d 2018-03-10 stsp /* Store path to repository. */
222 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
223 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
224 99724ed4 2018-03-10 stsp if (err)
225 86c3caaf 2018-03-09 stsp goto done;
226 86c3caaf 2018-03-09 stsp
227 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
228 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
229 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
230 ec22038e 2019-03-10 stsp if (err)
231 ec22038e 2019-03-10 stsp goto done;
232 ec22038e 2019-03-10 stsp
233 ec22038e 2019-03-10 stsp /* Generate UUID. */
234 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
235 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
236 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
237 ec22038e 2019-03-10 stsp goto done;
238 ec22038e 2019-03-10 stsp }
239 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
240 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
241 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
242 ec22038e 2019-03-10 stsp goto done;
243 ec22038e 2019-03-10 stsp }
244 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
245 577ec78f 2018-03-11 stsp if (err)
246 577ec78f 2018-03-11 stsp goto done;
247 577ec78f 2018-03-11 stsp
248 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
249 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
251 1451e70d 2018-03-10 stsp goto done;
252 1451e70d 2018-03-10 stsp }
253 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
254 99724ed4 2018-03-10 stsp if (err)
255 1451e70d 2018-03-10 stsp goto done;
256 1451e70d 2018-03-10 stsp
257 86c3caaf 2018-03-09 stsp done:
258 65596e15 2018-12-24 stsp free(commit_id);
259 7ac97322 2018-03-11 stsp free(path_got);
260 1451e70d 2018-03-10 stsp free(formatstr);
261 0bb8a95e 2018-03-12 stsp free(absprefix);
262 65596e15 2018-12-24 stsp free(basestr);
263 ec22038e 2019-03-10 stsp free(uuidstr);
264 86c3caaf 2018-03-09 stsp return err;
265 86c3caaf 2018-03-09 stsp }
266 86c3caaf 2018-03-09 stsp
267 247140b2 2019-02-05 stsp const struct got_error *
268 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
269 e5dc7198 2018-12-29 stsp const char *path_prefix)
270 e5dc7198 2018-12-29 stsp {
271 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
272 e5dc7198 2018-12-29 stsp
273 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
274 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
275 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
276 e5dc7198 2018-12-29 stsp }
277 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
278 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
279 e5dc7198 2018-12-29 stsp free(absprefix);
280 e5dc7198 2018-12-29 stsp return NULL;
281 86c3caaf 2018-03-09 stsp }
282 86c3caaf 2018-03-09 stsp
283 bc70eb79 2019-05-09 stsp const char *
284 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
285 86c3caaf 2018-03-09 stsp {
286 36a38700 2019-05-10 stsp return worktree->head_ref_name;
287 024e9686 2019-05-14 stsp }
288 024e9686 2019-05-14 stsp
289 024e9686 2019-05-14 stsp const struct got_error *
290 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
291 024e9686 2019-05-14 stsp struct got_reference *head_ref)
292 024e9686 2019-05-14 stsp {
293 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
294 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
295 024e9686 2019-05-14 stsp
296 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
297 ad10f64e 2023-07-19 thomas worktree->meta_dir) == -1) {
298 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
299 024e9686 2019-05-14 stsp path_got = NULL;
300 024e9686 2019-05-14 stsp goto done;
301 024e9686 2019-05-14 stsp }
302 024e9686 2019-05-14 stsp
303 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
304 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
305 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
306 024e9686 2019-05-14 stsp goto done;
307 024e9686 2019-05-14 stsp }
308 024e9686 2019-05-14 stsp
309 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
310 024e9686 2019-05-14 stsp if (err)
311 024e9686 2019-05-14 stsp goto done;
312 024e9686 2019-05-14 stsp
313 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
314 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
315 024e9686 2019-05-14 stsp done:
316 024e9686 2019-05-14 stsp free(path_got);
317 024e9686 2019-05-14 stsp if (err)
318 024e9686 2019-05-14 stsp free(head_ref_name);
319 024e9686 2019-05-14 stsp return err;
320 507dc3bb 2018-12-29 stsp }
321 507dc3bb 2018-12-29 stsp
322 b72f483a 2019-02-05 stsp struct got_object_id *
323 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
324 507dc3bb 2018-12-29 stsp {
325 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
326 86c3caaf 2018-03-09 stsp }
327 86c3caaf 2018-03-09 stsp
328 507dc3bb 2018-12-29 stsp const struct got_error *
329 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
330 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
331 507dc3bb 2018-12-29 stsp {
332 507dc3bb 2018-12-29 stsp const struct got_error *err;
333 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
334 507dc3bb 2018-12-29 stsp char *id_str = NULL;
335 507dc3bb 2018-12-29 stsp char *path_got = NULL;
336 507dc3bb 2018-12-29 stsp
337 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
338 ad10f64e 2023-07-19 thomas worktree->meta_dir) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 507dc3bb 2018-12-29 stsp path_got = NULL;
341 507dc3bb 2018-12-29 stsp goto done;
342 507dc3bb 2018-12-29 stsp }
343 507dc3bb 2018-12-29 stsp
344 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
345 507dc3bb 2018-12-29 stsp if (err)
346 507dc3bb 2018-12-29 stsp return err;
347 507dc3bb 2018-12-29 stsp
348 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
349 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
350 507dc3bb 2018-12-29 stsp goto done;
351 507dc3bb 2018-12-29 stsp }
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp /* Record our base commit. */
354 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
355 507dc3bb 2018-12-29 stsp if (err)
356 507dc3bb 2018-12-29 stsp goto done;
357 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
358 507dc3bb 2018-12-29 stsp if (err)
359 507dc3bb 2018-12-29 stsp goto done;
360 507dc3bb 2018-12-29 stsp
361 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
362 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
363 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
364 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
365 507dc3bb 2018-12-29 stsp goto done;
366 507dc3bb 2018-12-29 stsp }
367 507dc3bb 2018-12-29 stsp done:
368 507dc3bb 2018-12-29 stsp if (obj)
369 507dc3bb 2018-12-29 stsp got_object_close(obj);
370 507dc3bb 2018-12-29 stsp free(id_str);
371 507dc3bb 2018-12-29 stsp free(path_got);
372 507dc3bb 2018-12-29 stsp return err;
373 50b0790e 2020-09-11 stsp }
374 50b0790e 2020-09-11 stsp
375 50b0790e 2020-09-11 stsp const struct got_gotconfig *
376 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
377 50b0790e 2020-09-11 stsp {
378 50b0790e 2020-09-11 stsp return worktree->gotconfig;
379 507dc3bb 2018-12-29 stsp }
380 507dc3bb 2018-12-29 stsp
381 9d31a1d8 2018-03-11 stsp static const struct got_error *
382 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
383 86c3caaf 2018-03-09 stsp {
384 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
385 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
386 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
387 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
388 86c3caaf 2018-03-09 stsp return NULL;
389 21908da4 2019-01-13 stsp }
390 21908da4 2019-01-13 stsp
391 21908da4 2019-01-13 stsp static const struct got_error *
392 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
393 4a1ddfc2 2019-01-12 stsp {
394 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
395 4a1ddfc2 2019-01-12 stsp char *abspath;
396 59530ea7 2023-07-17 thomas
397 59530ea7 2023-07-17 thomas /* We only accept worktree-relative paths here. */
398 59530ea7 2023-07-17 thomas if (got_path_is_absolute(path)) {
399 59530ea7 2023-07-17 thomas return got_error_fmt(GOT_ERR_BAD_PATH,
400 59530ea7 2023-07-17 thomas "%s does not accept absolute paths: %s",
401 59530ea7 2023-07-17 thomas __func__, path);
402 59530ea7 2023-07-17 thomas }
403 4a1ddfc2 2019-01-12 stsp
404 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
405 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
406 4a1ddfc2 2019-01-12 stsp
407 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
408 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
409 ddcd8544 2019-03-11 stsp struct stat sb;
410 ddcd8544 2019-03-11 stsp err = NULL;
411 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
412 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
413 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
414 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
415 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
416 ddcd8544 2019-03-11 stsp }
417 ddcd8544 2019-03-11 stsp }
418 4a1ddfc2 2019-01-12 stsp free(abspath);
419 68c76935 2019-02-19 stsp return err;
420 68c76935 2019-02-19 stsp }
421 68c76935 2019-02-19 stsp
422 68c76935 2019-02-19 stsp static const struct got_error *
423 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
424 68c76935 2019-02-19 stsp {
425 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
426 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
427 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
428 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
429 68c76935 2019-02-19 stsp
430 68c76935 2019-02-19 stsp *same = 1;
431 68c76935 2019-02-19 stsp
432 230a42bd 2019-05-11 jcs for (;;) {
433 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
434 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
435 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
436 68c76935 2019-02-19 stsp break;
437 68c76935 2019-02-19 stsp }
438 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
439 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
440 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
441 68c76935 2019-02-19 stsp break;
442 68c76935 2019-02-19 stsp }
443 68c76935 2019-02-19 stsp if (flen1 == 0) {
444 68c76935 2019-02-19 stsp if (flen2 != 0)
445 68c76935 2019-02-19 stsp *same = 0;
446 68c76935 2019-02-19 stsp break;
447 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
448 68c76935 2019-02-19 stsp if (flen1 != 0)
449 68c76935 2019-02-19 stsp *same = 0;
450 68c76935 2019-02-19 stsp break;
451 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
452 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
453 68c76935 2019-02-19 stsp *same = 0;
454 68c76935 2019-02-19 stsp break;
455 68c76935 2019-02-19 stsp }
456 68c76935 2019-02-19 stsp } else {
457 68c76935 2019-02-19 stsp *same = 0;
458 68c76935 2019-02-19 stsp break;
459 68c76935 2019-02-19 stsp }
460 68c76935 2019-02-19 stsp }
461 68c76935 2019-02-19 stsp
462 68c76935 2019-02-19 stsp return err;
463 68c76935 2019-02-19 stsp }
464 68c76935 2019-02-19 stsp
465 68c76935 2019-02-19 stsp static const struct got_error *
466 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
467 68c76935 2019-02-19 stsp {
468 68c76935 2019-02-19 stsp struct stat sb;
469 68c76935 2019-02-19 stsp size_t size1, size2;
470 68c76935 2019-02-19 stsp
471 68c76935 2019-02-19 stsp *same = 1;
472 68c76935 2019-02-19 stsp
473 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
474 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
475 68c76935 2019-02-19 stsp size1 = sb.st_size;
476 68c76935 2019-02-19 stsp
477 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
478 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
479 68c76935 2019-02-19 stsp size2 = sb.st_size;
480 68c76935 2019-02-19 stsp
481 68c76935 2019-02-19 stsp if (size1 != size2) {
482 68c76935 2019-02-19 stsp *same = 0;
483 68c76935 2019-02-19 stsp return NULL;
484 68c76935 2019-02-19 stsp }
485 68c76935 2019-02-19 stsp
486 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
487 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
488 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
489 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
490 68c76935 2019-02-19 stsp
491 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
492 24f136e0 2021-11-20 thomas }
493 24f136e0 2021-11-20 thomas
494 24f136e0 2021-11-20 thomas static const struct got_error *
495 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
496 24f136e0 2021-11-20 thomas {
497 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
498 24f136e0 2021-11-20 thomas size_t flen;
499 24f136e0 2021-11-20 thomas ssize_t outlen;
500 24f136e0 2021-11-20 thomas
501 24f136e0 2021-11-20 thomas *outsize = 0;
502 24f136e0 2021-11-20 thomas
503 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
504 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
505 24f136e0 2021-11-20 thomas
506 24f136e0 2021-11-20 thomas for (;;) {
507 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
508 24f136e0 2021-11-20 thomas if (flen == 0) {
509 24f136e0 2021-11-20 thomas if (ferror(f))
510 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
511 24f136e0 2021-11-20 thomas if (feof(f))
512 24f136e0 2021-11-20 thomas break;
513 24f136e0 2021-11-20 thomas }
514 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
515 24f136e0 2021-11-20 thomas if (outlen == -1)
516 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
517 24f136e0 2021-11-20 thomas if (outlen != flen)
518 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
519 24f136e0 2021-11-20 thomas *outsize += outlen;
520 24f136e0 2021-11-20 thomas }
521 24f136e0 2021-11-20 thomas
522 24f136e0 2021-11-20 thomas return NULL;
523 24f136e0 2021-11-20 thomas }
524 24f136e0 2021-11-20 thomas
525 24f136e0 2021-11-20 thomas static const struct got_error *
526 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
527 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
528 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
529 24f136e0 2021-11-20 thomas const char *ondisk_path)
530 24f136e0 2021-11-20 thomas {
531 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
532 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
533 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
534 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
535 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
536 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
537 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
538 24f136e0 2021-11-20 thomas
539 24f136e0 2021-11-20 thomas *overlapcnt = 0;
540 24f136e0 2021-11-20 thomas
541 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
542 24f136e0 2021-11-20 thomas if (err)
543 24f136e0 2021-11-20 thomas return err;
544 24f136e0 2021-11-20 thomas
545 24f136e0 2021-11-20 thomas if (same_content)
546 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
547 24f136e0 2021-11-20 thomas
548 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
549 24f136e0 2021-11-20 thomas if (err)
550 24f136e0 2021-11-20 thomas return err;
551 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
552 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
553 24f136e0 2021-11-20 thomas if (err)
554 24f136e0 2021-11-20 thomas return err;
555 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
556 24f136e0 2021-11-20 thomas
557 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
558 24f136e0 2021-11-20 thomas *overlapcnt = 1;
559 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
560 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
561 24f136e0 2021-11-20 thomas goto done;
562 24f136e0 2021-11-20 thomas }
563 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
564 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
565 24f136e0 2021-11-20 thomas goto done;
566 24f136e0 2021-11-20 thomas }
567 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
568 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
569 24f136e0 2021-11-20 thomas goto done;
570 24f136e0 2021-11-20 thomas }
571 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
572 fc2a50f2 2022-11-01 thomas base_path_orig, "");
573 24f136e0 2021-11-20 thomas if (err)
574 24f136e0 2021-11-20 thomas goto done;
575 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
576 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
577 24f136e0 2021-11-20 thomas if (err)
578 24f136e0 2021-11-20 thomas goto done;
579 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
580 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
581 24f136e0 2021-11-20 thomas if (err)
582 24f136e0 2021-11-20 thomas goto done;
583 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
584 24f136e0 2021-11-20 thomas if (err)
585 24f136e0 2021-11-20 thomas goto done;
586 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
587 24f136e0 2021-11-20 thomas if (err)
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
590 24f136e0 2021-11-20 thomas if (err)
591 24f136e0 2021-11-20 thomas goto done;
592 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
593 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
594 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
595 24f136e0 2021-11-20 thomas goto done;
596 24f136e0 2021-11-20 thomas }
597 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
598 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
599 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
600 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
601 24f136e0 2021-11-20 thomas path_deriv) < 0) {
602 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
603 24f136e0 2021-11-20 thomas goto done;
604 24f136e0 2021-11-20 thomas }
605 24f136e0 2021-11-20 thomas if (size_orig > 0) {
606 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
607 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
608 24f136e0 2021-11-20 thomas label_orig ? " " : "",
609 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
610 24f136e0 2021-11-20 thomas path_orig) < 0) {
611 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
612 24f136e0 2021-11-20 thomas goto done;
613 24f136e0 2021-11-20 thomas }
614 24f136e0 2021-11-20 thomas }
615 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
616 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
617 24f136e0 2021-11-20 thomas path_deriv2,
618 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
619 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
620 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
621 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
622 24f136e0 2021-11-20 thomas goto done;
623 24f136e0 2021-11-20 thomas }
624 24f136e0 2021-11-20 thomas } else if (changed_deriv)
625 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
626 24f136e0 2021-11-20 thomas else if (changed_deriv2)
627 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
628 24f136e0 2021-11-20 thomas done:
629 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
630 24f136e0 2021-11-20 thomas err == NULL)
631 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
632 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
633 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
634 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
635 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
636 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
637 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
638 24f136e0 2021-11-20 thomas free(path_orig);
639 24f136e0 2021-11-20 thomas free(path_deriv);
640 24f136e0 2021-11-20 thomas free(path_deriv2);
641 24f136e0 2021-11-20 thomas free(base_path_orig);
642 24f136e0 2021-11-20 thomas free(base_path_deriv);
643 24f136e0 2021-11-20 thomas free(base_path_deriv2);
644 24f136e0 2021-11-20 thomas return err;
645 6353ad76 2019-02-08 stsp }
646 6353ad76 2019-02-08 stsp
647 6353ad76 2019-02-08 stsp /*
648 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
649 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
650 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
651 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
652 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
653 6353ad76 2019-02-08 stsp */
654 6353ad76 2019-02-08 stsp static const struct got_error *
655 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
656 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
657 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
658 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
659 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
660 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
661 6353ad76 2019-02-08 stsp {
662 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
663 6353ad76 2019-02-08 stsp int merged_fd = -1;
664 db590691 2021-06-02 stsp FILE *f_merged = NULL;
665 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
666 234035bc 2019-06-01 stsp int overlapcnt = 0;
667 3524bbf9 2020-10-20 stsp char *parent = NULL;
668 6353ad76 2019-02-08 stsp
669 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
670 234035bc 2019-06-01 stsp
671 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
672 3524bbf9 2020-10-20 stsp if (err)
673 3524bbf9 2020-10-20 stsp return err;
674 af54ae4a 2019-02-19 stsp
675 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
676 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
677 3524bbf9 2020-10-20 stsp goto done;
678 3524bbf9 2020-10-20 stsp }
679 af54ae4a 2019-02-19 stsp
680 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
681 6353ad76 2019-02-08 stsp if (err)
682 6353ad76 2019-02-08 stsp goto done;
683 3b9f0f87 2020-07-23 stsp
684 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
685 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
686 24f136e0 2021-11-20 thomas if (err) {
687 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
688 24f136e0 2021-11-20 thomas goto done;
689 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
690 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
691 24f136e0 2021-11-20 thomas ondisk_path);
692 24f136e0 2021-11-20 thomas if (err)
693 24f136e0 2021-11-20 thomas goto done;
694 24f136e0 2021-11-20 thomas }
695 6353ad76 2019-02-08 stsp
696 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
697 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
698 1ee397ad 2019-07-12 stsp if (err)
699 1ee397ad 2019-07-12 stsp goto done;
700 6353ad76 2019-02-08 stsp
701 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
702 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
703 816dc654 2019-02-16 stsp goto done;
704 68c76935 2019-02-19 stsp }
705 68c76935 2019-02-19 stsp
706 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
707 db590691 2021-06-02 stsp if (f_merged == NULL) {
708 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
709 db590691 2021-06-02 stsp goto done;
710 db590691 2021-06-02 stsp }
711 db590691 2021-06-02 stsp merged_fd = -1;
712 db590691 2021-06-02 stsp
713 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
714 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
715 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
716 db590691 2021-06-02 stsp f_merged);
717 68c76935 2019-02-19 stsp if (err)
718 68c76935 2019-02-19 stsp goto done;
719 816dc654 2019-02-16 stsp }
720 6353ad76 2019-02-08 stsp
721 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
722 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
723 70a0c8ec 2019-02-20 stsp goto done;
724 70a0c8ec 2019-02-20 stsp }
725 70a0c8ec 2019-02-20 stsp
726 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
727 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
728 230a42bd 2019-05-11 jcs ondisk_path);
729 6353ad76 2019-02-08 stsp goto done;
730 6353ad76 2019-02-08 stsp }
731 6353ad76 2019-02-08 stsp done:
732 fdcb7daf 2019-12-15 stsp if (err) {
733 fdcb7daf 2019-12-15 stsp if (merged_path)
734 fdcb7daf 2019-12-15 stsp unlink(merged_path);
735 3b9f0f87 2020-07-23 stsp }
736 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
737 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
738 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
739 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
740 6353ad76 2019-02-08 stsp free(merged_path);
741 af54ae4a 2019-02-19 stsp free(base_path);
742 3524bbf9 2020-10-20 stsp free(parent);
743 af57b12a 2020-07-23 stsp return err;
744 af57b12a 2020-07-23 stsp }
745 af57b12a 2020-07-23 stsp
746 af57b12a 2020-07-23 stsp static const struct got_error *
747 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
748 af57b12a 2020-07-23 stsp size_t target_len)
749 af57b12a 2020-07-23 stsp {
750 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
751 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
752 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
753 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
754 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
755 af57b12a 2020-07-23 stsp ondisk_path);
756 af57b12a 2020-07-23 stsp return NULL;
757 af57b12a 2020-07-23 stsp }
758 af57b12a 2020-07-23 stsp
759 af57b12a 2020-07-23 stsp /*
760 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
761 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
762 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
763 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
764 993e2a1b 2020-07-23 stsp *
765 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
766 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
767 993e2a1b 2020-07-23 stsp *
768 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
769 993e2a1b 2020-07-23 stsp * has deleted this symlink.
770 11cc08c1 2020-07-23 stsp */
771 11cc08c1 2020-07-23 stsp static const struct got_error *
772 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
773 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
774 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
775 11cc08c1 2020-07-23 stsp {
776 11cc08c1 2020-07-23 stsp const struct got_error *err;
777 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
778 11cc08c1 2020-07-23 stsp FILE *f = NULL;
779 11cc08c1 2020-07-23 stsp
780 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
781 11cc08c1 2020-07-23 stsp if (err)
782 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
783 11cc08c1 2020-07-23 stsp
784 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
785 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
786 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
787 11cc08c1 2020-07-23 stsp goto done;
788 11cc08c1 2020-07-23 stsp }
789 11cc08c1 2020-07-23 stsp
790 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
791 11cc08c1 2020-07-23 stsp if (err)
792 3818e3c4 2020-11-01 naddy goto done;
793 3818e3c4 2020-11-01 naddy
794 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
795 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
796 11cc08c1 2020-07-23 stsp goto done;
797 3818e3c4 2020-11-01 naddy }
798 11cc08c1 2020-07-23 stsp
799 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
800 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
801 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
802 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
803 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
804 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
805 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
806 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
807 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
808 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
809 11cc08c1 2020-07-23 stsp goto done;
810 11cc08c1 2020-07-23 stsp }
811 11cc08c1 2020-07-23 stsp
812 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
813 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
814 11cc08c1 2020-07-23 stsp goto done;
815 11cc08c1 2020-07-23 stsp }
816 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
817 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
818 11cc08c1 2020-07-23 stsp goto done;
819 11cc08c1 2020-07-23 stsp }
820 11cc08c1 2020-07-23 stsp done:
821 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
822 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
823 11cc08c1 2020-07-23 stsp free(path);
824 11cc08c1 2020-07-23 stsp free(id_str);
825 11cc08c1 2020-07-23 stsp free(label_deriv);
826 11cc08c1 2020-07-23 stsp return err;
827 11cc08c1 2020-07-23 stsp }
828 d219f183 2020-07-23 stsp
829 d219f183 2020-07-23 stsp /* forward declaration */
830 d219f183 2020-07-23 stsp static const struct got_error *
831 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
832 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
833 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
834 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
835 11cc08c1 2020-07-23 stsp
836 11cc08c1 2020-07-23 stsp /*
837 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
838 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
839 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
840 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
841 af57b12a 2020-07-23 stsp */
842 af57b12a 2020-07-23 stsp static const struct got_error *
843 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
844 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
845 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
846 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
847 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
848 af57b12a 2020-07-23 stsp {
849 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
850 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
851 af57b12a 2020-07-23 stsp struct stat sb;
852 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
853 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
854 11cc08c1 2020-07-23 stsp int have_local_change = 0;
855 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
856 af57b12a 2020-07-23 stsp
857 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
858 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
859 af57b12a 2020-07-23 stsp
860 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
861 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
862 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
863 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
864 af57b12a 2020-07-23 stsp ondisk_path);
865 af57b12a 2020-07-23 stsp goto done;
866 af57b12a 2020-07-23 stsp }
867 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
868 af57b12a 2020-07-23 stsp
869 526a746f 2020-07-23 stsp if (blob_orig) {
870 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
871 526a746f 2020-07-23 stsp if (err)
872 526a746f 2020-07-23 stsp goto done;
873 526a746f 2020-07-23 stsp }
874 af57b12a 2020-07-23 stsp
875 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
876 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
878 11cc08c1 2020-07-23 stsp have_local_change = 1;
879 11cc08c1 2020-07-23 stsp
880 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
881 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
882 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
883 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
884 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
885 11cc08c1 2020-07-23 stsp
886 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
887 11cc08c1 2020-07-23 stsp if (ancestor_target) {
888 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
889 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
890 11cc08c1 2020-07-23 stsp path);
891 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
892 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
893 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
894 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
895 11cc08c1 2020-07-23 stsp path);
896 11cc08c1 2020-07-23 stsp } else {
897 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
898 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
899 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
900 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
901 11cc08c1 2020-07-23 stsp if (err)
902 11cc08c1 2020-07-23 stsp goto done;
903 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
904 11cc08c1 2020-07-23 stsp path);
905 11cc08c1 2020-07-23 stsp }
906 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
907 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
908 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
909 af57b12a 2020-07-23 stsp strlen(deriv_target));
910 af57b12a 2020-07-23 stsp if (err)
911 af57b12a 2020-07-23 stsp goto done;
912 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
913 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
914 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
915 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
916 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
917 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
918 ea7786be 2020-07-23 stsp path);
919 ea7786be 2020-07-23 stsp } else {
920 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
921 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
922 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
923 ea7786be 2020-07-23 stsp if (err)
924 ea7786be 2020-07-23 stsp goto done;
925 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
926 ea7786be 2020-07-23 stsp path);
927 ea7786be 2020-07-23 stsp }
928 6353ad76 2019-02-08 stsp }
929 11cc08c1 2020-07-23 stsp
930 af57b12a 2020-07-23 stsp done:
931 af57b12a 2020-07-23 stsp free(ancestor_target);
932 eec2f5a9 2021-06-03 stsp return err;
933 eec2f5a9 2021-06-03 stsp }
934 eec2f5a9 2021-06-03 stsp
935 eec2f5a9 2021-06-03 stsp static const struct got_error *
936 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
937 eec2f5a9 2021-06-03 stsp {
938 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
939 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
940 eec2f5a9 2021-06-03 stsp ssize_t target_len;
941 eec2f5a9 2021-06-03 stsp size_t n;
942 eec2f5a9 2021-06-03 stsp FILE *f;
943 eec2f5a9 2021-06-03 stsp
944 eec2f5a9 2021-06-03 stsp *outfile = NULL;
945 eec2f5a9 2021-06-03 stsp
946 eec2f5a9 2021-06-03 stsp f = got_opentemp();
947 eec2f5a9 2021-06-03 stsp if (f == NULL)
948 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
949 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
950 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
951 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
952 eec2f5a9 2021-06-03 stsp goto done;
953 eec2f5a9 2021-06-03 stsp }
954 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
955 eec2f5a9 2021-06-03 stsp if (n != target_len) {
956 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
957 eec2f5a9 2021-06-03 stsp goto done;
958 eec2f5a9 2021-06-03 stsp }
959 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
960 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
961 eec2f5a9 2021-06-03 stsp goto done;
962 eec2f5a9 2021-06-03 stsp }
963 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
964 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
965 eec2f5a9 2021-06-03 stsp goto done;
966 eec2f5a9 2021-06-03 stsp }
967 eec2f5a9 2021-06-03 stsp done:
968 eec2f5a9 2021-06-03 stsp if (err)
969 eec2f5a9 2021-06-03 stsp fclose(f);
970 eec2f5a9 2021-06-03 stsp else
971 eec2f5a9 2021-06-03 stsp *outfile = f;
972 14c901f1 2019-08-08 stsp return err;
973 14c901f1 2019-08-08 stsp }
974 14c901f1 2019-08-08 stsp
975 14c901f1 2019-08-08 stsp /*
976 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
977 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
978 14c901f1 2019-08-08 stsp * acts as the second derived version.
979 14c901f1 2019-08-08 stsp */
980 14c901f1 2019-08-08 stsp static const struct got_error *
981 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
982 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
983 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
984 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
985 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
986 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
987 14c901f1 2019-08-08 stsp {
988 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
989 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
990 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
991 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
992 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
993 14c901f1 2019-08-08 stsp
994 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
995 14c901f1 2019-08-08 stsp
996 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
997 ed6b5030 2020-10-20 stsp if (err)
998 ed6b5030 2020-10-20 stsp return err;
999 14c901f1 2019-08-08 stsp
1000 67a66647 2021-05-31 stsp if (blob_orig) {
1001 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1002 67a66647 2021-05-31 stsp parent) == -1) {
1003 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1004 67a66647 2021-05-31 stsp base_path = NULL;
1005 67a66647 2021-05-31 stsp goto done;
1006 67a66647 2021-05-31 stsp }
1007 67a66647 2021-05-31 stsp
1008 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
1009 fc2a50f2 2022-11-01 thomas base_path, "");
1010 67a66647 2021-05-31 stsp if (err)
1011 67a66647 2021-05-31 stsp goto done;
1012 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1013 67a66647 2021-05-31 stsp blob_orig);
1014 67a66647 2021-05-31 stsp if (err)
1015 67a66647 2021-05-31 stsp goto done;
1016 67a66647 2021-05-31 stsp free(base_path);
1017 db590691 2021-06-02 stsp } else {
1018 db590691 2021-06-02 stsp /*
1019 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1020 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1021 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1022 db590691 2021-06-02 stsp */
1023 db590691 2021-06-02 stsp f_orig = got_opentemp();
1024 db590691 2021-06-02 stsp if (f_orig == NULL) {
1025 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1026 db590691 2021-06-02 stsp goto done;
1027 db590691 2021-06-02 stsp }
1028 67a66647 2021-05-31 stsp }
1029 67a66647 2021-05-31 stsp
1030 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1031 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1032 14c901f1 2019-08-08 stsp base_path = NULL;
1033 14c901f1 2019-08-08 stsp goto done;
1034 14c901f1 2019-08-08 stsp }
1035 14c901f1 2019-08-08 stsp
1036 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1037 14c901f1 2019-08-08 stsp if (err)
1038 14c901f1 2019-08-08 stsp goto done;
1039 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1040 14c901f1 2019-08-08 stsp blob_deriv);
1041 14c901f1 2019-08-08 stsp if (err)
1042 14c901f1 2019-08-08 stsp goto done;
1043 14c901f1 2019-08-08 stsp
1044 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1045 14c901f1 2019-08-08 stsp if (err)
1046 14c901f1 2019-08-08 stsp goto done;
1047 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1048 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1049 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1050 14c901f1 2019-08-08 stsp goto done;
1051 eec2f5a9 2021-06-03 stsp }
1052 eec2f5a9 2021-06-03 stsp
1053 b6b86fd1 2022-08-30 thomas /*
1054 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1055 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1056 eec2f5a9 2021-06-03 stsp */
1057 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1058 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1059 eec2f5a9 2021-06-03 stsp if (err)
1060 eec2f5a9 2021-06-03 stsp goto done;
1061 eec2f5a9 2021-06-03 stsp } else {
1062 eec2f5a9 2021-06-03 stsp int fd;
1063 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1064 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1065 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1066 eec2f5a9 2021-06-03 stsp goto done;
1067 eec2f5a9 2021-06-03 stsp }
1068 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1070 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1071 eec2f5a9 2021-06-03 stsp close(fd);
1072 eec2f5a9 2021-06-03 stsp goto done;
1073 eec2f5a9 2021-06-03 stsp }
1074 14c901f1 2019-08-08 stsp }
1075 14c901f1 2019-08-08 stsp
1076 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1077 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1078 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1079 14c901f1 2019-08-08 stsp done:
1080 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1081 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1082 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1083 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1084 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1085 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1086 14c901f1 2019-08-08 stsp free(base_path);
1087 67a66647 2021-05-31 stsp if (blob_orig_path) {
1088 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1089 67a66647 2021-05-31 stsp free(blob_orig_path);
1090 67a66647 2021-05-31 stsp }
1091 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1092 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1093 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1094 14c901f1 2019-08-08 stsp }
1095 6353ad76 2019-02-08 stsp free(id_str);
1096 818c7501 2019-07-11 stsp free(label_deriv);
1097 ed6b5030 2020-10-20 stsp free(parent);
1098 4a1ddfc2 2019-01-12 stsp return err;
1099 4a1ddfc2 2019-01-12 stsp }
1100 4a1ddfc2 2019-01-12 stsp
1101 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1102 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1103 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1104 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1105 13d9040b 2019-03-27 stsp {
1106 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1107 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1108 13d9040b 2019-03-27 stsp
1109 65b05cec 2020-07-23 stsp *new_iep = NULL;
1110 65b05cec 2020-07-23 stsp
1111 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1112 054041d0 2020-06-24 stsp if (err)
1113 054041d0 2020-06-24 stsp return err;
1114 054041d0 2020-06-24 stsp
1115 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1116 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1117 054041d0 2020-06-24 stsp if (err)
1118 054041d0 2020-06-24 stsp goto done;
1119 054041d0 2020-06-24 stsp
1120 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1121 054041d0 2020-06-24 stsp done:
1122 054041d0 2020-06-24 stsp if (err)
1123 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1124 65b05cec 2020-07-23 stsp else
1125 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1126 13d9040b 2019-03-27 stsp return err;
1127 1ebedb77 2019-10-19 stsp }
1128 1ebedb77 2019-10-19 stsp
1129 1ebedb77 2019-10-19 stsp static mode_t
1130 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1131 1ebedb77 2019-10-19 stsp {
1132 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1133 1ebedb77 2019-10-19 stsp
1134 1ebedb77 2019-10-19 stsp if (executable) {
1135 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1136 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1137 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1138 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1139 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1140 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1141 1ebedb77 2019-10-19 stsp }
1142 1ebedb77 2019-10-19 stsp
1143 a2c162eb 2022-10-30 thomas return st_mode;
1144 13d9040b 2019-03-27 stsp }
1145 13d9040b 2019-03-27 stsp
1146 8ba819a3 2020-07-23 stsp /* forward declaration */
1147 13d9040b 2019-03-27 stsp static const struct got_error *
1148 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1149 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1150 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1151 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1152 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1153 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1154 8ba819a3 2020-07-23 stsp
1155 5a1fbc73 2020-07-23 stsp /*
1156 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1157 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1158 5a1fbc73 2020-07-23 stsp */
1159 8ba819a3 2020-07-23 stsp static const struct got_error *
1160 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1161 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1162 5a1fbc73 2020-07-23 stsp {
1163 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1164 5a1fbc73 2020-07-23 stsp ssize_t elen;
1165 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1166 5a1fbc73 2020-07-23 stsp int fd;
1167 5a1fbc73 2020-07-23 stsp
1168 c6e8a826 2021-04-05 stsp *did_something = 0;
1169 c6e8a826 2021-04-05 stsp
1170 5a1fbc73 2020-07-23 stsp /*
1171 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1172 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1173 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1174 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1175 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1176 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1177 5a1fbc73 2020-07-23 stsp */
1178 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1179 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1180 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1182 5a1fbc73 2020-07-23 stsp
1183 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1184 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 5a1fbc73 2020-07-23 stsp if (elen == -1)
1186 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1187 5a1fbc73 2020-07-23 stsp
1188 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1189 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1190 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1191 5a1fbc73 2020-07-23 stsp }
1192 5a1fbc73 2020-07-23 stsp
1193 c6e8a826 2021-04-05 stsp *did_something = 1;
1194 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1195 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1196 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1197 5a1fbc73 2020-07-23 stsp return err;
1198 3c1ec782 2020-07-23 stsp }
1199 3c1ec782 2020-07-23 stsp
1200 3c1ec782 2020-07-23 stsp static const struct got_error *
1201 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1202 ad10f64e 2023-07-19 thomas size_t target_len, const char *ondisk_path, const char *wtroot_path,
1203 ad10f64e 2023-07-19 thomas const char *meta_dir)
1204 3c1ec782 2020-07-23 stsp {
1205 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1206 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1207 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1208 3c1ec782 2020-07-23 stsp
1209 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1210 3c1ec782 2020-07-23 stsp
1211 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1212 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1213 3c1ec782 2020-07-23 stsp return NULL;
1214 3c1ec782 2020-07-23 stsp }
1215 3c1ec782 2020-07-23 stsp
1216 3c1ec782 2020-07-23 stsp /*
1217 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1218 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1219 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1220 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1221 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1222 3c1ec782 2020-07-23 stsp */
1223 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1224 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1225 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1226 ce031e9e 2020-10-20 stsp if (err)
1227 ce031e9e 2020-10-20 stsp return err;
1228 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1229 ce031e9e 2020-10-20 stsp free(parent);
1230 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1231 ce031e9e 2020-10-20 stsp }
1232 ce031e9e 2020-10-20 stsp free(parent);
1233 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1234 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1235 3c1ec782 2020-07-23 stsp free(abspath);
1236 3c1ec782 2020-07-23 stsp return err;
1237 3c1ec782 2020-07-23 stsp }
1238 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1239 3c1ec782 2020-07-23 stsp free(abspath);
1240 3c1ec782 2020-07-23 stsp if (err)
1241 3c1ec782 2020-07-23 stsp return err;
1242 3c1ec782 2020-07-23 stsp } else {
1243 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1244 3c1ec782 2020-07-23 stsp if (err)
1245 3c1ec782 2020-07-23 stsp return err;
1246 3c1ec782 2020-07-23 stsp }
1247 3c1ec782 2020-07-23 stsp
1248 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1249 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1250 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1251 3c1ec782 2020-07-23 stsp return NULL;
1252 3c1ec782 2020-07-23 stsp }
1253 3c1ec782 2020-07-23 stsp
1254 ad10f64e 2023-07-19 thomas /* Do not allow symlinks pointing into the meta directory. */
1255 ad10f64e 2023-07-19 thomas if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1256 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1257 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1258 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1259 3c1ec782 2020-07-23 stsp
1260 3c1ec782 2020-07-23 stsp free(path_got);
1261 3c1ec782 2020-07-23 stsp return NULL;
1262 5a1fbc73 2020-07-23 stsp }
1263 5a1fbc73 2020-07-23 stsp
1264 5a1fbc73 2020-07-23 stsp static const struct got_error *
1265 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1266 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1267 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1268 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1269 ace90326 2021-09-27 thomas struct got_repository *repo,
1270 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1271 9d31a1d8 2018-03-11 stsp {
1272 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1273 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1274 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1275 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1276 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1277 8ba819a3 2020-07-23 stsp
1278 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1279 2e1fa222 2020-07-23 stsp
1280 9e39ca77 2022-07-21 thomas /*
1281 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1282 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1283 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1284 8ba819a3 2020-07-23 stsp * in the blob object.
1285 8ba819a3 2020-07-23 stsp */
1286 8ba819a3 2020-07-23 stsp do {
1287 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1288 3521b466 2022-07-21 thomas if (err)
1289 3521b466 2022-07-21 thomas return err;
1290 3521b466 2022-07-21 thomas
1291 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1292 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1293 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1294 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1295 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1296 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1297 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1298 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1299 3b9f0f87 2020-07-23 stsp progress_arg);
1300 8ba819a3 2020-07-23 stsp }
1301 8ba819a3 2020-07-23 stsp if (len > 0) {
1302 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1303 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1304 8ba819a3 2020-07-23 stsp len - hdrlen);
1305 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1306 8ba819a3 2020-07-23 stsp hdrlen = 0;
1307 8ba819a3 2020-07-23 stsp }
1308 8ba819a3 2020-07-23 stsp } while (len != 0);
1309 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1310 8ba819a3 2020-07-23 stsp
1311 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1312 ad10f64e 2023-07-19 thomas ondisk_path, worktree->root_path, worktree->meta_dir);
1313 3c1ec782 2020-07-23 stsp if (err)
1314 3c1ec782 2020-07-23 stsp return err;
1315 8ba819a3 2020-07-23 stsp
1316 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1317 906c123b 2020-07-23 stsp /* install as a regular file */
1318 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1319 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1320 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1321 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1322 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1323 9e39ca77 2022-07-21 thomas return err;
1324 906c123b 2020-07-23 stsp }
1325 906c123b 2020-07-23 stsp
1326 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1327 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1328 c6e8a826 2021-04-05 stsp int symlink_replaced;
1329 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1330 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1331 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1332 9e39ca77 2022-07-21 thomas return err;
1333 c90c8ce3 2020-07-23 stsp }
1334 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1335 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1336 5a1fbc73 2020-07-23 stsp if (err)
1337 9e39ca77 2022-07-21 thomas return err;
1338 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1339 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1340 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1341 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1342 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1343 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1344 c6e8a826 2021-04-05 stsp } else {
1345 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1346 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1347 c6e8a826 2021-04-05 stsp }
1348 f35fa46a 2020-07-23 stsp }
1349 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1350 f35fa46a 2020-07-23 stsp }
1351 f35fa46a 2020-07-23 stsp
1352 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1353 f4994adc 2020-10-20 stsp char *parent;
1354 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1355 f4994adc 2020-10-20 stsp if (err)
1356 9e39ca77 2022-07-21 thomas return err;
1357 59530ea7 2023-07-17 thomas err = got_path_mkdir(parent);
1358 f4994adc 2020-10-20 stsp free(parent);
1359 f35fa46a 2020-07-23 stsp if (err)
1360 9e39ca77 2022-07-21 thomas return err;
1361 f35fa46a 2020-07-23 stsp /*
1362 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1363 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1364 f35fa46a 2020-07-23 stsp */
1365 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1366 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1367 59530ea7 2023-07-17 thomas if (progress_cb) {
1368 59530ea7 2023-07-17 thomas err = (*progress_cb)(progress_arg,
1369 59530ea7 2023-07-17 thomas reverting_versioned_file ?
1370 59530ea7 2023-07-17 thomas GOT_STATUS_REVERT : GOT_STATUS_ADD,
1371 59530ea7 2023-07-17 thomas path);
1372 59530ea7 2023-07-17 thomas }
1373 9e39ca77 2022-07-21 thomas return err;
1374 f35fa46a 2020-07-23 stsp }
1375 f35fa46a 2020-07-23 stsp }
1376 f35fa46a 2020-07-23 stsp
1377 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1378 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1379 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1380 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1381 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1382 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1383 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1384 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1385 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1386 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1387 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1388 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1389 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1390 8ba819a3 2020-07-23 stsp } else {
1391 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1392 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1393 8ba819a3 2020-07-23 stsp }
1394 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1395 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1396 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1397 8ba819a3 2020-07-23 stsp return err;
1398 8ba819a3 2020-07-23 stsp }
1399 8ba819a3 2020-07-23 stsp
1400 8ba819a3 2020-07-23 stsp static const struct got_error *
1401 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1402 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1403 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1404 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1405 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1406 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1407 8ba819a3 2020-07-23 stsp {
1408 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1409 507dc3bb 2018-12-29 stsp int fd = -1;
1410 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1411 507dc3bb 2018-12-29 stsp int update = 0;
1412 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1413 a2c162eb 2022-10-30 thomas mode_t mode;
1414 8ba819a3 2020-07-23 stsp
1415 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1416 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1417 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1418 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1419 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1420 f5375317 2020-10-20 stsp char *parent;
1421 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1422 f5375317 2020-10-20 stsp if (err)
1423 f5375317 2020-10-20 stsp return err;
1424 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1425 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1426 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1427 f5375317 2020-10-20 stsp free(parent);
1428 21908da4 2019-01-13 stsp if (err)
1429 21908da4 2019-01-13 stsp return err;
1430 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1431 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1432 a2c162eb 2022-10-30 thomas mode);
1433 21908da4 2019-01-13 stsp if (fd == -1)
1434 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1435 230a42bd 2019-05-11 jcs ondisk_path);
1436 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1437 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1438 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1439 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1440 3b9f0f87 2020-07-23 stsp goto done;
1441 3b9f0f87 2020-07-23 stsp }
1442 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1443 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1444 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1445 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1446 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1447 507dc3bb 2018-12-29 stsp goto done;
1448 d70b8e30 2018-12-27 stsp } else {
1449 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1450 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1451 507dc3bb 2018-12-29 stsp if (err)
1452 507dc3bb 2018-12-29 stsp goto done;
1453 507dc3bb 2018-12-29 stsp update = 1;
1454 a2c162eb 2022-10-30 thomas
1455 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1456 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1457 a2c162eb 2022-10-30 thomas tmppath);
1458 a2c162eb 2022-10-30 thomas goto done;
1459 a2c162eb 2022-10-30 thomas }
1460 9d31a1d8 2018-03-11 stsp }
1461 507dc3bb 2018-12-29 stsp } else
1462 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1463 3818e3c4 2020-11-01 naddy }
1464 3818e3c4 2020-11-01 naddy
1465 bd6aa359 2020-07-23 stsp if (progress_cb) {
1466 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1467 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1468 bd6aa359 2020-07-23 stsp path);
1469 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1470 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1471 bd6aa359 2020-07-23 stsp path);
1472 bd6aa359 2020-07-23 stsp else
1473 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1474 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1475 bd6aa359 2020-07-23 stsp if (err)
1476 bd6aa359 2020-07-23 stsp goto done;
1477 bd6aa359 2020-07-23 stsp }
1478 d7b62c98 2018-12-27 stsp
1479 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1480 9d31a1d8 2018-03-11 stsp do {
1481 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1483 9d31a1d8 2018-03-11 stsp if (err)
1484 9d31a1d8 2018-03-11 stsp break;
1485 9d31a1d8 2018-03-11 stsp if (len > 0) {
1486 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1487 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1488 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1490 b87c6f83 2018-12-24 stsp goto done;
1491 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1492 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1493 b87c6f83 2018-12-24 stsp goto done;
1494 9d31a1d8 2018-03-11 stsp }
1495 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1496 9d31a1d8 2018-03-11 stsp }
1497 9d31a1d8 2018-03-11 stsp } while (len != 0);
1498 9d31a1d8 2018-03-11 stsp
1499 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1500 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1501 816dc654 2019-02-16 stsp goto done;
1502 816dc654 2019-02-16 stsp }
1503 9d31a1d8 2018-03-11 stsp
1504 507dc3bb 2018-12-29 stsp if (update) {
1505 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1506 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1507 f6d8c0ac 2020-11-09 stsp goto done;
1508 f6d8c0ac 2020-11-09 stsp }
1509 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1510 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1511 230a42bd 2019-05-11 jcs ondisk_path);
1512 68ed9ba5 2019-02-10 stsp goto done;
1513 68ed9ba5 2019-02-10 stsp }
1514 63df146d 2020-11-09 stsp free(tmppath);
1515 63df146d 2020-11-09 stsp tmppath = NULL;
1516 507dc3bb 2018-12-29 stsp }
1517 507dc3bb 2018-12-29 stsp
1518 9d31a1d8 2018-03-11 stsp done:
1519 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1520 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1521 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1522 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1523 507dc3bb 2018-12-29 stsp free(tmppath);
1524 6353ad76 2019-02-08 stsp return err;
1525 6353ad76 2019-02-08 stsp }
1526 6353ad76 2019-02-08 stsp
1527 be94c032 2023-02-20 thomas /*
1528 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1529 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1530 be94c032 2023-02-20 thomas */
1531 6353ad76 2019-02-08 stsp static const struct got_error *
1532 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1533 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1534 be94c032 2023-02-20 thomas FILE *ondisk_file)
1535 7154f6ce 2019-03-27 stsp {
1536 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1537 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1538 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1539 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1540 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1541 7154f6ce 2019-03-27 stsp };
1542 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1543 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1544 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1545 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1546 9bdd68dd 2020-01-02 naddy char *line = NULL;
1547 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1548 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1549 7154f6ce 2019-03-27 stsp
1550 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1551 8de9d8ad 2023-02-20 thomas return NULL;
1552 be94c032 2023-02-20 thomas
1553 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1554 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1555 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1556 be94c032 2023-02-20 thomas
1557 8de9d8ad 2023-02-20 thomas if (blob) {
1558 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1559 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1560 be94c032 2023-02-20 thomas if (err)
1561 be94c032 2023-02-20 thomas goto done;
1562 8de9d8ad 2023-02-20 thomas }
1563 be94c032 2023-02-20 thomas
1564 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1565 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1566 8de9d8ad 2023-02-20 thomas if (err)
1567 8de9d8ad 2023-02-20 thomas goto done;
1568 8de9d8ad 2023-02-20 thomas
1569 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1570 8de9d8ad 2023-02-20 thomas
1571 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1572 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1573 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1574 450d9f6b 2023-02-20 thomas off_t pos;
1575 8de9d8ad 2023-02-20 thomas
1576 8de9d8ad 2023-02-20 thomas /*
1577 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1578 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1579 8de9d8ad 2023-02-20 thomas */
1580 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1581 8de9d8ad 2023-02-20 thomas
1582 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1583 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1584 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1585 7154f6ce 2019-03-27 stsp }
1586 7154f6ce 2019-03-27 stsp
1587 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1588 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1589 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1590 450d9f6b 2023-02-20 thomas goto done;
1591 7154f6ce 2019-03-27 stsp }
1592 8de9d8ad 2023-02-20 thomas
1593 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1594 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1595 450d9f6b 2023-02-20 thomas
1596 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1597 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1598 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1599 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1600 8de9d8ad 2023-02-20 thomas break;
1601 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1602 8de9d8ad 2023-02-20 thomas break;
1603 8de9d8ad 2023-02-20 thomas }
1604 8de9d8ad 2023-02-20 thomas
1605 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1606 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1607 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1608 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1609 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1610 8de9d8ad 2023-02-20 thomas goto done;
1611 8de9d8ad 2023-02-20 thomas } else
1612 8de9d8ad 2023-02-20 thomas i++;
1613 8de9d8ad 2023-02-20 thomas }
1614 8de9d8ad 2023-02-20 thomas ++ln;
1615 8de9d8ad 2023-02-20 thomas }
1616 7154f6ce 2019-03-27 stsp }
1617 be94c032 2023-02-20 thomas
1618 be94c032 2023-02-20 thomas done:
1619 9bdd68dd 2020-01-02 naddy free(line);
1620 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1621 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1622 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1623 8de9d8ad 2023-02-20 thomas if (err == NULL)
1624 8de9d8ad 2023-02-20 thomas err = free_err;
1625 7154f6ce 2019-03-27 stsp
1626 7154f6ce 2019-03-27 stsp return err;
1627 e2b1e152 2019-07-13 stsp }
1628 e2b1e152 2019-07-13 stsp
1629 e2b1e152 2019-07-13 stsp static int
1630 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1631 1ebedb77 2019-10-19 stsp {
1632 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1633 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1634 1ebedb77 2019-10-19 stsp }
1635 1ebedb77 2019-10-19 stsp
1636 1ebedb77 2019-10-19 stsp static int
1637 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1638 e2b1e152 2019-07-13 stsp {
1639 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1640 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1641 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1642 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1643 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1644 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1645 c363b2c1 2019-08-03 stsp }
1646 c363b2c1 2019-08-03 stsp
1647 c363b2c1 2019-08-03 stsp static unsigned char
1648 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1649 c363b2c1 2019-08-03 stsp {
1650 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1651 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1652 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1653 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1654 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1655 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1656 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1657 c363b2c1 2019-08-03 stsp default:
1658 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1659 a919d5c4 2020-07-23 stsp }
1660 a919d5c4 2020-07-23 stsp }
1661 a919d5c4 2020-07-23 stsp
1662 a919d5c4 2020-07-23 stsp static const struct got_error *
1663 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1664 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1665 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1666 a919d5c4 2020-07-23 stsp {
1667 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1668 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1669 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1670 a919d5c4 2020-07-23 stsp ssize_t elen;
1671 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1672 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1673 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1674 a919d5c4 2020-07-23 stsp
1675 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1676 a919d5c4 2020-07-23 stsp
1677 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1678 a919d5c4 2020-07-23 stsp do {
1679 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1680 a919d5c4 2020-07-23 stsp if (err)
1681 a919d5c4 2020-07-23 stsp return err;
1682 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1683 a919d5c4 2020-07-23 stsp /*
1684 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1685 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1686 a919d5c4 2020-07-23 stsp */
1687 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1688 a919d5c4 2020-07-23 stsp }
1689 a919d5c4 2020-07-23 stsp if (len > 0) {
1690 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1691 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1692 a919d5c4 2020-07-23 stsp len - hdrlen);
1693 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1694 a919d5c4 2020-07-23 stsp hdrlen = 0;
1695 a919d5c4 2020-07-23 stsp }
1696 a919d5c4 2020-07-23 stsp } while (len != 0);
1697 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1698 a919d5c4 2020-07-23 stsp
1699 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1700 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1701 a919d5c4 2020-07-23 stsp if (elen == -1)
1702 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1703 a919d5c4 2020-07-23 stsp } else {
1704 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1705 a919d5c4 2020-07-23 stsp if (elen == -1)
1706 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1707 c363b2c1 2019-08-03 stsp }
1708 a919d5c4 2020-07-23 stsp
1709 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1710 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1711 a919d5c4 2020-07-23 stsp
1712 a919d5c4 2020-07-23 stsp return NULL;
1713 7154f6ce 2019-03-27 stsp }
1714 7154f6ce 2019-03-27 stsp
1715 7154f6ce 2019-03-27 stsp static const struct got_error *
1716 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1717 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1718 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1719 6353ad76 2019-02-08 stsp {
1720 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1721 6353ad76 2019-02-08 stsp struct got_object_id id;
1722 6353ad76 2019-02-08 stsp size_t hdrlen;
1723 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1724 6353ad76 2019-02-08 stsp FILE *f = NULL;
1725 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1726 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1727 6353ad76 2019-02-08 stsp size_t flen, blen;
1728 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1729 6353ad76 2019-02-08 stsp
1730 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1731 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1732 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1733 6353ad76 2019-02-08 stsp
1734 7f91a133 2019-12-13 stsp /*
1735 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1736 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1737 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1738 7f91a133 2019-12-13 stsp */
1739 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1740 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1741 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1742 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1743 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1744 882ef1b9 2019-12-13 stsp else
1745 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1746 882ef1b9 2019-12-13 stsp goto done;
1747 882ef1b9 2019-12-13 stsp }
1748 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1749 3d35a492 2019-12-13 stsp goto done;
1750 3d35a492 2019-12-13 stsp }
1751 7f91a133 2019-12-13 stsp } else {
1752 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1753 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1754 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1755 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1756 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1757 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1758 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1759 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1760 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1761 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1762 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1763 3d35a492 2019-12-13 stsp else
1764 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1765 3d35a492 2019-12-13 stsp goto done;
1766 3d35a492 2019-12-13 stsp }
1767 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1768 1338848f 2019-12-13 stsp goto done;
1769 a378724f 2019-02-10 stsp }
1770 a378724f 2019-02-10 stsp }
1771 6353ad76 2019-02-08 stsp
1772 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1773 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1774 1338848f 2019-12-13 stsp goto done;
1775 3f148bc6 2019-07-27 stsp }
1776 efdd40df 2019-07-27 stsp
1777 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1778 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1779 1338848f 2019-12-13 stsp goto done;
1780 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1781 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1782 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1783 1338848f 2019-12-13 stsp goto done;
1784 d00136be 2019-03-26 stsp }
1785 b8f41171 2019-02-10 stsp
1786 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1787 f179e66d 2020-07-23 stsp goto done;
1788 f179e66d 2020-07-23 stsp
1789 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1790 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1791 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1792 1338848f 2019-12-13 stsp goto done;
1793 f179e66d 2020-07-23 stsp }
1794 6353ad76 2019-02-08 stsp
1795 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1796 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1797 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1798 c363b2c1 2019-08-03 stsp else
1799 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1800 c363b2c1 2019-08-03 stsp
1801 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1802 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1803 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1804 f4ae6ddb 2022-07-01 thomas goto done;
1805 f4ae6ddb 2022-07-01 thomas }
1806 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1807 6353ad76 2019-02-08 stsp if (err)
1808 1338848f 2019-12-13 stsp goto done;
1809 6353ad76 2019-02-08 stsp
1810 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1811 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1812 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1813 a919d5c4 2020-07-23 stsp goto done;
1814 a919d5c4 2020-07-23 stsp }
1815 a919d5c4 2020-07-23 stsp
1816 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1817 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1818 ab0d4361 2019-12-13 stsp if (fd == -1) {
1819 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1820 ab0d4361 2019-12-13 stsp goto done;
1821 ab0d4361 2019-12-13 stsp }
1822 3d35a492 2019-12-13 stsp }
1823 3d35a492 2019-12-13 stsp
1824 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1825 6353ad76 2019-02-08 stsp if (f == NULL) {
1826 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1827 6353ad76 2019-02-08 stsp goto done;
1828 6353ad76 2019-02-08 stsp }
1829 1338848f 2019-12-13 stsp fd = -1;
1830 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1831 656b1f76 2019-05-11 jcs for (;;) {
1832 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1833 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1834 6353ad76 2019-02-08 stsp if (err)
1835 7154f6ce 2019-03-27 stsp goto done;
1836 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1837 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1838 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1839 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1840 7154f6ce 2019-03-27 stsp goto done;
1841 80c5c120 2019-02-19 stsp }
1842 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1843 6353ad76 2019-02-08 stsp if (flen != 0)
1844 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1845 6353ad76 2019-02-08 stsp break;
1846 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1847 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1848 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1849 6353ad76 2019-02-08 stsp break;
1850 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1851 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1852 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1853 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1854 6353ad76 2019-02-08 stsp break;
1855 6353ad76 2019-02-08 stsp }
1856 6353ad76 2019-02-08 stsp } else {
1857 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1858 6353ad76 2019-02-08 stsp break;
1859 6353ad76 2019-02-08 stsp }
1860 6353ad76 2019-02-08 stsp hdrlen = 0;
1861 6353ad76 2019-02-08 stsp }
1862 7154f6ce 2019-03-27 stsp
1863 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1864 7154f6ce 2019-03-27 stsp rewind(f);
1865 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1866 be94c032 2023-02-20 thomas sb, f);
1867 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1868 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1869 6353ad76 2019-02-08 stsp done:
1870 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1871 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1872 6353ad76 2019-02-08 stsp if (blob)
1873 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1874 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1875 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1876 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1877 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1878 9d31a1d8 2018-03-11 stsp return err;
1879 e2b1e152 2019-07-13 stsp }
1880 e2b1e152 2019-07-13 stsp
1881 e2b1e152 2019-07-13 stsp /*
1882 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1883 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1884 e2b1e152 2019-07-13 stsp */
1885 e2b1e152 2019-07-13 stsp static const struct got_error *
1886 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1887 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1888 e2b1e152 2019-07-13 stsp {
1889 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1890 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1891 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1892 e2b1e152 2019-07-13 stsp
1893 e2b1e152 2019-07-13 stsp return NULL;
1894 9d31a1d8 2018-03-11 stsp }
1895 9d31a1d8 2018-03-11 stsp
1896 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1897 9f323212 2023-03-10 thomas
1898 9d31a1d8 2018-03-11 stsp static const struct got_error *
1899 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1900 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1901 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1902 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1903 0584f854 2019-04-06 stsp void *progress_arg)
1904 9d31a1d8 2018-03-11 stsp {
1905 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1906 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1907 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1908 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1909 b8f41171 2019-02-10 stsp struct stat sb;
1910 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1911 9d31a1d8 2018-03-11 stsp
1912 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1913 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1914 6353ad76 2019-02-08 stsp
1915 abb4604f 2019-07-27 stsp if (ie) {
1916 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1917 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1918 a76c42e6 2019-08-03 stsp goto done;
1919 a76c42e6 2019-08-03 stsp }
1920 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1921 7f91a133 2019-12-13 stsp repo);
1922 abb4604f 2019-07-27 stsp if (err)
1923 abb4604f 2019-07-27 stsp goto done;
1924 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1925 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1926 c90c8ce3 2020-07-23 stsp } else {
1927 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1928 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1929 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1930 e6f4ba31 2021-09-24 thomas ondisk_path);
1931 e6f4ba31 2021-09-24 thomas goto done;
1932 e6f4ba31 2021-09-24 thomas }
1933 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1934 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1935 e6f4ba31 2021-09-24 thomas } else {
1936 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1937 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1938 e6f4ba31 2021-09-24 thomas else
1939 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1940 e6f4ba31 2021-09-24 thomas }
1941 c90c8ce3 2020-07-23 stsp }
1942 6353ad76 2019-02-08 stsp
1943 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1944 2c41dce7 2021-06-27 stsp if (ie)
1945 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1946 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1947 b8f41171 2019-02-10 stsp goto done;
1948 b8f41171 2019-02-10 stsp }
1949 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1950 a769b60b 2021-06-27 stsp if (ie)
1951 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1952 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1953 5036ab18 2020-04-18 stsp path);
1954 5036ab18 2020-04-18 stsp goto done;
1955 9f323212 2023-03-10 thomas }
1956 9f323212 2023-03-10 thomas
1957 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1958 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1959 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1960 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1961 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1962 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1963 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1964 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1965 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1966 9f323212 2023-03-10 thomas } else if (ie) {
1967 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1968 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1969 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1970 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1971 9f323212 2023-03-10 thomas ie->path);
1972 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1973 9f323212 2023-03-10 thomas errno == ENOENT))
1974 9f323212 2023-03-10 thomas goto done;
1975 9f323212 2023-03-10 thomas }
1976 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1977 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1978 9f323212 2023-03-10 thomas ie->path);
1979 9f323212 2023-03-10 thomas }
1980 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1981 5036ab18 2020-04-18 stsp }
1982 b8f41171 2019-02-10 stsp
1983 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1984 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1985 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1986 c6e8a826 2021-04-05 stsp /*
1987 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1988 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1989 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1990 c6e8a826 2021-04-05 stsp * updating contents of this file.
1991 c6e8a826 2021-04-05 stsp */
1992 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1993 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1994 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1995 c6e8a826 2021-04-05 stsp /* Same commit. */
1996 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1997 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1998 e2b1e152 2019-07-13 stsp if (err)
1999 e2b1e152 2019-07-13 stsp goto done;
2000 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2001 b8f41171 2019-02-10 stsp path);
2002 6353ad76 2019-02-08 stsp goto done;
2003 a378724f 2019-02-10 stsp }
2004 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
2005 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
2006 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
2007 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2008 b36a9774 2023-04-14 thomas if (got_fileindex_entry_has_commit(ie)) {
2009 b36a9774 2023-04-14 thomas /* Update the base commit ID of this file. */
2010 b36a9774 2023-04-14 thomas memcpy(ie->commit_sha1,
2011 b36a9774 2023-04-14 thomas worktree->base_commit_id->sha1,
2012 b36a9774 2023-04-14 thomas sizeof(ie->commit_sha1));
2013 b36a9774 2023-04-14 thomas }
2014 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2015 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2016 0f58026f 2021-04-05 stsp if (err)
2017 0f58026f 2021-04-05 stsp goto done;
2018 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2019 0f58026f 2021-04-05 stsp path);
2020 b8f41171 2019-02-10 stsp goto done;
2021 e2b1e152 2019-07-13 stsp }
2022 9d31a1d8 2018-03-11 stsp }
2023 9d31a1d8 2018-03-11 stsp
2024 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2025 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2026 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2027 f4ae6ddb 2022-07-01 thomas goto done;
2028 f4ae6ddb 2022-07-01 thomas }
2029 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2030 8da9e5f4 2019-01-12 stsp if (err)
2031 6353ad76 2019-02-08 stsp goto done;
2032 512f0d0e 2019-01-02 stsp
2033 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2034 234035bc 2019-06-01 stsp int update_timestamps;
2035 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2036 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2037 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2038 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2039 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2040 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2041 f4ae6ddb 2022-07-01 thomas goto done;
2042 f4ae6ddb 2022-07-01 thomas }
2043 234035bc 2019-06-01 stsp struct got_object_id id2;
2044 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2045 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2046 f4ae6ddb 2022-07-01 thomas fd2);
2047 234035bc 2019-06-01 stsp if (err)
2048 f69721c3 2019-10-21 stsp goto done;
2049 f69721c3 2019-10-21 stsp }
2050 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2051 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2052 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2053 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2054 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2055 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2056 f69721c3 2019-10-21 stsp goto done;
2057 f69721c3 2019-10-21 stsp }
2058 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2059 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2060 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2061 234035bc 2019-06-01 stsp goto done;
2062 f69721c3 2019-10-21 stsp }
2063 234035bc 2019-06-01 stsp }
2064 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2065 36bf999c 2020-07-23 stsp char *link_target;
2066 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2067 36bf999c 2020-07-23 stsp if (err)
2068 36bf999c 2020-07-23 stsp goto done;
2069 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2070 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2071 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2072 36bf999c 2020-07-23 stsp free(link_target);
2073 993e2a1b 2020-07-23 stsp } else {
2074 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2075 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2076 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2077 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2078 993e2a1b 2020-07-23 stsp }
2079 f69721c3 2019-10-21 stsp free(label_orig);
2080 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2081 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2082 f4ae6ddb 2022-07-01 thomas goto done;
2083 f4ae6ddb 2022-07-01 thomas }
2084 234035bc 2019-06-01 stsp if (blob2)
2085 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2086 909d120e 2019-09-22 stsp if (err)
2087 909d120e 2019-09-22 stsp goto done;
2088 234035bc 2019-06-01 stsp /*
2089 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2090 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2091 234035bc 2019-06-01 stsp * unmodified files again.
2092 234035bc 2019-06-01 stsp */
2093 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2094 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2095 234035bc 2019-06-01 stsp update_timestamps);
2096 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2097 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2098 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2099 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2100 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2101 1ee397ad 2019-07-12 stsp if (err)
2102 1ee397ad 2019-07-12 stsp goto done;
2103 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2104 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2105 13d9040b 2019-03-27 stsp if (err)
2106 13d9040b 2019-03-27 stsp goto done;
2107 13d9040b 2019-03-27 stsp } else {
2108 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2109 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2110 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2111 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2112 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2113 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2114 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2115 2e1fa222 2020-07-23 stsp } else {
2116 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2117 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2118 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2119 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2120 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2121 2e1fa222 2020-07-23 stsp }
2122 13d9040b 2019-03-27 stsp if (err)
2123 13d9040b 2019-03-27 stsp goto done;
2124 65b05cec 2020-07-23 stsp
2125 054041d0 2020-06-24 stsp if (ie) {
2126 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2127 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2128 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2129 054041d0 2020-06-24 stsp } else {
2130 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2131 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2132 054041d0 2020-06-24 stsp &blob->id);
2133 054041d0 2020-06-24 stsp }
2134 13d9040b 2019-03-27 stsp if (err)
2135 13d9040b 2019-03-27 stsp goto done;
2136 65b05cec 2020-07-23 stsp
2137 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2138 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2139 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2140 2e1fa222 2020-07-23 stsp }
2141 13d9040b 2019-03-27 stsp }
2142 f4ae6ddb 2022-07-01 thomas
2143 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2144 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2145 f4ae6ddb 2022-07-01 thomas goto done;
2146 f4ae6ddb 2022-07-01 thomas }
2147 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2148 6353ad76 2019-02-08 stsp done:
2149 6353ad76 2019-02-08 stsp free(ondisk_path);
2150 8da9e5f4 2019-01-12 stsp return err;
2151 90285c3b 2019-01-08 stsp }
2152 90285c3b 2019-01-08 stsp
2153 90285c3b 2019-01-08 stsp static const struct got_error *
2154 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2155 90285c3b 2019-01-08 stsp {
2156 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2157 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2158 90285c3b 2019-01-08 stsp
2159 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2160 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2161 90285c3b 2019-01-08 stsp
2162 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2163 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2164 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2165 c97665ae 2019-01-13 stsp } else {
2166 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2167 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2168 1c4cdd89 2021-06-20 stsp if (err)
2169 1c4cdd89 2021-06-20 stsp goto done;
2170 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2171 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2172 fddefe3b 2020-10-20 stsp free(ondisk_path);
2173 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2174 1c4cdd89 2021-06-20 stsp parent = NULL;
2175 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2176 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2177 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2178 fddefe3b 2020-10-20 stsp ondisk_path);
2179 90285c3b 2019-01-08 stsp break;
2180 90285c3b 2019-01-08 stsp }
2181 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2182 1c4cdd89 2021-06-20 stsp if (err)
2183 1c4cdd89 2021-06-20 stsp break;
2184 1c4cdd89 2021-06-20 stsp }
2185 90285c3b 2019-01-08 stsp }
2186 1c4cdd89 2021-06-20 stsp done:
2187 90285c3b 2019-01-08 stsp free(ondisk_path);
2188 1c4cdd89 2021-06-20 stsp free(parent);
2189 90285c3b 2019-01-08 stsp return err;
2190 512f0d0e 2019-01-02 stsp }
2191 512f0d0e 2019-01-02 stsp
2192 708d8e67 2019-03-27 stsp static const struct got_error *
2193 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2194 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2195 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2196 708d8e67 2019-03-27 stsp {
2197 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2198 708d8e67 2019-03-27 stsp unsigned char status;
2199 708d8e67 2019-03-27 stsp struct stat sb;
2200 708d8e67 2019-03-27 stsp char *ondisk_path;
2201 708d8e67 2019-03-27 stsp
2202 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2203 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2204 a76c42e6 2019-08-03 stsp
2205 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2206 708d8e67 2019-03-27 stsp == -1)
2207 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2208 708d8e67 2019-03-27 stsp
2209 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2210 708d8e67 2019-03-27 stsp if (err)
2211 5a58a424 2020-06-23 stsp goto done;
2212 708d8e67 2019-03-27 stsp
2213 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2214 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2215 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2216 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2217 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2218 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2219 993e2a1b 2020-07-23 stsp goto done;
2220 993e2a1b 2020-07-23 stsp }
2221 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2222 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2223 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2224 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2225 993e2a1b 2020-07-23 stsp if (err)
2226 993e2a1b 2020-07-23 stsp goto done;
2227 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2228 993e2a1b 2020-07-23 stsp ie->path);
2229 993e2a1b 2020-07-23 stsp goto done;
2230 993e2a1b 2020-07-23 stsp }
2231 993e2a1b 2020-07-23 stsp
2232 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2233 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2234 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2235 1ee397ad 2019-07-12 stsp if (err)
2236 5a58a424 2020-06-23 stsp goto done;
2237 fc6346c4 2019-03-27 stsp /*
2238 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2239 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2240 fc6346c4 2019-03-27 stsp */
2241 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2242 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2243 fc6346c4 2019-03-27 stsp } else {
2244 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2245 1ee397ad 2019-07-12 stsp if (err)
2246 5a58a424 2020-06-23 stsp goto done;
2247 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2248 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2249 fc6346c4 2019-03-27 stsp if (err)
2250 5a58a424 2020-06-23 stsp goto done;
2251 fc6346c4 2019-03-27 stsp }
2252 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2253 708d8e67 2019-03-27 stsp }
2254 5a58a424 2020-06-23 stsp done:
2255 5a58a424 2020-06-23 stsp free(ondisk_path);
2256 fc6346c4 2019-03-27 stsp return err;
2257 708d8e67 2019-03-27 stsp }
2258 708d8e67 2019-03-27 stsp
2259 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2260 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2261 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2262 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2263 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2264 8da9e5f4 2019-01-12 stsp void *progress_arg;
2265 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2266 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2267 8da9e5f4 2019-01-12 stsp };
2268 8da9e5f4 2019-01-12 stsp
2269 512f0d0e 2019-01-02 stsp static const struct got_error *
2270 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2271 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2272 512f0d0e 2019-01-02 stsp {
2273 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2274 0584f854 2019-04-06 stsp
2275 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2276 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2277 512f0d0e 2019-01-02 stsp
2278 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2279 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2280 8da9e5f4 2019-01-12 stsp }
2281 512f0d0e 2019-01-02 stsp
2282 8da9e5f4 2019-01-12 stsp static const struct got_error *
2283 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2284 8da9e5f4 2019-01-12 stsp {
2285 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2286 7a9df742 2019-01-08 stsp
2287 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2288 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2289 0584f854 2019-04-06 stsp
2290 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2291 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2292 9d31a1d8 2018-03-11 stsp }
2293 9d31a1d8 2018-03-11 stsp
2294 9d31a1d8 2018-03-11 stsp static const struct got_error *
2295 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2296 9d31a1d8 2018-03-11 stsp {
2297 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2298 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2299 8da9e5f4 2019-01-12 stsp char *path;
2300 9d31a1d8 2018-03-11 stsp
2301 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2302 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2303 0584f854 2019-04-06 stsp
2304 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2305 59530ea7 2023-07-17 thomas return NULL;
2306 59530ea7 2023-07-17 thomas
2307 59530ea7 2023-07-17 thomas if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2308 63c5ca5d 2019-08-24 stsp return NULL;
2309 63c5ca5d 2019-08-24 stsp
2310 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2311 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2312 8da9e5f4 2019-01-12 stsp == -1)
2313 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2314 9d31a1d8 2018-03-11 stsp
2315 59530ea7 2023-07-17 thomas err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2316 59530ea7 2023-07-17 thomas a->repo, a->progress_cb, a->progress_arg);
2317 9d31a1d8 2018-03-11 stsp
2318 8da9e5f4 2019-01-12 stsp free(path);
2319 0cd1c46a 2019-03-11 stsp return err;
2320 0cd1c46a 2019-03-11 stsp }
2321 0cd1c46a 2019-03-11 stsp
2322 b2118c49 2020-07-28 stsp const struct got_error *
2323 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2324 b2118c49 2020-07-28 stsp {
2325 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2326 b2118c49 2020-07-28 stsp
2327 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2328 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2329 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2330 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2331 b2118c49 2020-07-28 stsp }
2332 b2118c49 2020-07-28 stsp
2333 b2118c49 2020-07-28 stsp return NULL;
2334 b2118c49 2020-07-28 stsp }
2335 b2118c49 2020-07-28 stsp
2336 818c7501 2019-07-11 stsp static const struct got_error *
2337 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2338 0cd1c46a 2019-03-11 stsp {
2339 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2340 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2341 0cd1c46a 2019-03-11 stsp
2342 517bab73 2019-03-11 stsp *refname = NULL;
2343 517bab73 2019-03-11 stsp
2344 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2345 b2118c49 2020-07-28 stsp if (err)
2346 b2118c49 2020-07-28 stsp return err;
2347 0cd1c46a 2019-03-11 stsp
2348 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2349 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2350 0647c563 2019-03-11 stsp *refname = NULL;
2351 0cd1c46a 2019-03-11 stsp }
2352 517bab73 2019-03-11 stsp free(uuidstr);
2353 517bab73 2019-03-11 stsp return err;
2354 517bab73 2019-03-11 stsp }
2355 517bab73 2019-03-11 stsp
2356 818c7501 2019-07-11 stsp const struct got_error *
2357 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2358 f6cd0243 2023-01-28 thomas const char *prefix)
2359 f6cd0243 2023-01-28 thomas {
2360 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2361 f6cd0243 2023-01-28 thomas }
2362 f6cd0243 2023-01-28 thomas
2363 fcfaa6d5 2023-07-05 thomas static const struct got_error *
2364 fcfaa6d5 2023-07-05 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
2365 818c7501 2019-07-11 stsp {
2366 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2367 818c7501 2019-07-11 stsp }
2368 818c7501 2019-07-11 stsp
2369 818c7501 2019-07-11 stsp static const struct got_error *
2370 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2371 818c7501 2019-07-11 stsp {
2372 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2373 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2374 818c7501 2019-07-11 stsp }
2375 818c7501 2019-07-11 stsp
2376 818c7501 2019-07-11 stsp static const struct got_error *
2377 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2378 818c7501 2019-07-11 stsp {
2379 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2380 818c7501 2019-07-11 stsp }
2381 818c7501 2019-07-11 stsp
2382 818c7501 2019-07-11 stsp static const struct got_error *
2383 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2384 818c7501 2019-07-11 stsp {
2385 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2386 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2387 818c7501 2019-07-11 stsp }
2388 818c7501 2019-07-11 stsp
2389 818c7501 2019-07-11 stsp static const struct got_error *
2390 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2391 818c7501 2019-07-11 stsp {
2392 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2393 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2394 0ebf8283 2019-07-24 stsp }
2395 0ebf8283 2019-07-24 stsp
2396 0ebf8283 2019-07-24 stsp static const struct got_error *
2397 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2398 0ebf8283 2019-07-24 stsp {
2399 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2400 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2401 0ebf8283 2019-07-24 stsp }
2402 0ebf8283 2019-07-24 stsp
2403 0ebf8283 2019-07-24 stsp static const struct got_error *
2404 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2405 0ebf8283 2019-07-24 stsp {
2406 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2407 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2408 0ebf8283 2019-07-24 stsp }
2409 0ebf8283 2019-07-24 stsp
2410 0ebf8283 2019-07-24 stsp static const struct got_error *
2411 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2412 0ebf8283 2019-07-24 stsp {
2413 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2414 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2415 818c7501 2019-07-11 stsp }
2416 818c7501 2019-07-11 stsp
2417 0ebf8283 2019-07-24 stsp static const struct got_error *
2418 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2419 0ebf8283 2019-07-24 stsp {
2420 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2421 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2422 0ebf8283 2019-07-24 stsp }
2423 818c7501 2019-07-11 stsp
2424 0ebf8283 2019-07-24 stsp const struct got_error *
2425 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2426 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2427 0ebf8283 2019-07-24 stsp {
2428 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2429 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2430 0ebf8283 2019-07-24 stsp *path = NULL;
2431 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2432 0ebf8283 2019-07-24 stsp }
2433 0ebf8283 2019-07-24 stsp return NULL;
2434 10604dce 2021-09-24 thomas }
2435 10604dce 2021-09-24 thomas
2436 10604dce 2021-09-24 thomas static const struct got_error *
2437 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2438 10604dce 2021-09-24 thomas {
2439 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2440 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2441 0ebf8283 2019-07-24 stsp }
2442 0ebf8283 2019-07-24 stsp
2443 10604dce 2021-09-24 thomas static const struct got_error *
2444 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2445 10604dce 2021-09-24 thomas {
2446 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2447 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2448 10604dce 2021-09-24 thomas }
2449 10604dce 2021-09-24 thomas
2450 517bab73 2019-03-11 stsp /*
2451 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2452 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2453 517bab73 2019-03-11 stsp */
2454 517bab73 2019-03-11 stsp static const struct got_error *
2455 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2456 517bab73 2019-03-11 stsp {
2457 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2458 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2459 517bab73 2019-03-11 stsp char *refname;
2460 517bab73 2019-03-11 stsp
2461 fcfaa6d5 2023-07-05 thomas err = get_base_ref_name(&refname, worktree);
2462 517bab73 2019-03-11 stsp if (err)
2463 517bab73 2019-03-11 stsp return err;
2464 0cd1c46a 2019-03-11 stsp
2465 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2466 0cd1c46a 2019-03-11 stsp if (err)
2467 0cd1c46a 2019-03-11 stsp goto done;
2468 0cd1c46a 2019-03-11 stsp
2469 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2470 0cd1c46a 2019-03-11 stsp done:
2471 0cd1c46a 2019-03-11 stsp free(refname);
2472 0cd1c46a 2019-03-11 stsp if (ref)
2473 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2474 3e3a69f1 2019-07-25 stsp return err;
2475 3e3a69f1 2019-07-25 stsp }
2476 3e3a69f1 2019-07-25 stsp
2477 3e3a69f1 2019-07-25 stsp static const struct got_error *
2478 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2479 3e3a69f1 2019-07-25 stsp {
2480 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2481 3e3a69f1 2019-07-25 stsp
2482 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2483 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2484 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2485 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2486 3e3a69f1 2019-07-25 stsp }
2487 9d31a1d8 2018-03-11 stsp return err;
2488 9d31a1d8 2018-03-11 stsp }
2489 9d31a1d8 2018-03-11 stsp
2490 3e3a69f1 2019-07-25 stsp
2491 ebf99748 2019-05-09 stsp static const struct got_error *
2492 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2493 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2494 ebf99748 2019-05-09 stsp {
2495 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2496 ebf99748 2019-05-09 stsp FILE *index = NULL;
2497 0cd1c46a 2019-03-11 stsp
2498 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2499 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2500 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2501 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2502 ebf99748 2019-05-09 stsp
2503 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2504 3e3a69f1 2019-07-25 stsp if (err)
2505 ebf99748 2019-05-09 stsp goto done;
2506 ebf99748 2019-05-09 stsp
2507 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2508 ebf99748 2019-05-09 stsp if (index == NULL) {
2509 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2510 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2511 ebf99748 2019-05-09 stsp } else {
2512 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2513 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2514 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2515 ebf99748 2019-05-09 stsp }
2516 ebf99748 2019-05-09 stsp done:
2517 ebf99748 2019-05-09 stsp if (err) {
2518 ebf99748 2019-05-09 stsp free(*fileindex_path);
2519 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2520 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2521 ebf99748 2019-05-09 stsp *fileindex = NULL;
2522 ebf99748 2019-05-09 stsp }
2523 ebf99748 2019-05-09 stsp return err;
2524 ebf99748 2019-05-09 stsp }
2525 c932eeeb 2019-05-22 stsp
2526 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2527 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2528 c932eeeb 2019-05-22 stsp const char *path;
2529 c932eeeb 2019-05-22 stsp size_t path_len;
2530 c932eeeb 2019-05-22 stsp const char *entry_name;
2531 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2532 a484d721 2019-06-10 stsp void *progress_arg;
2533 c932eeeb 2019-05-22 stsp };
2534 ebf99748 2019-05-09 stsp
2535 c932eeeb 2019-05-22 stsp static const struct got_error *
2536 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2537 c932eeeb 2019-05-22 stsp {
2538 1ee397ad 2019-07-12 stsp const struct got_error *err;
2539 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2540 c932eeeb 2019-05-22 stsp
2541 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2542 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2543 c932eeeb 2019-05-22 stsp return NULL;
2544 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2545 102ff934 2019-06-11 stsp return NULL;
2546 102ff934 2019-06-11 stsp
2547 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2548 a769b60b 2021-06-27 stsp return NULL;
2549 a769b60b 2021-06-27 stsp
2550 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2551 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2552 c932eeeb 2019-05-22 stsp return NULL;
2553 c932eeeb 2019-05-22 stsp
2554 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2555 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2556 edd02c5e 2019-07-11 stsp ie->path);
2557 1ee397ad 2019-07-12 stsp if (err)
2558 1ee397ad 2019-07-12 stsp return err;
2559 1ee397ad 2019-07-12 stsp }
2560 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2561 c932eeeb 2019-05-22 stsp return NULL;
2562 a615e0e7 2020-12-16 stsp }
2563 a615e0e7 2020-12-16 stsp
2564 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2565 a615e0e7 2020-12-16 stsp static const struct got_error *
2566 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2567 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2568 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2569 a615e0e7 2020-12-16 stsp {
2570 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2571 a615e0e7 2020-12-16 stsp
2572 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2573 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2574 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2575 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2576 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2577 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2578 a615e0e7 2020-12-16 stsp
2579 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2580 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2581 9c6338c4 2019-06-02 stsp }
2582 9c6338c4 2019-06-02 stsp
2583 9c6338c4 2019-06-02 stsp static const struct got_error *
2584 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2585 9c6338c4 2019-06-02 stsp {
2586 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2587 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2588 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2589 867630bb 2020-01-17 stsp struct timespec timeout;
2590 9c6338c4 2019-06-02 stsp
2591 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2592 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2593 9c6338c4 2019-06-02 stsp if (err)
2594 9c6338c4 2019-06-02 stsp goto done;
2595 9c6338c4 2019-06-02 stsp
2596 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2597 9c6338c4 2019-06-02 stsp if (err)
2598 9c6338c4 2019-06-02 stsp goto done;
2599 9c6338c4 2019-06-02 stsp
2600 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2601 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2602 9c6338c4 2019-06-02 stsp fileindex_path);
2603 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2604 9c6338c4 2019-06-02 stsp }
2605 867630bb 2020-01-17 stsp
2606 867630bb 2020-01-17 stsp /*
2607 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2608 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2609 867630bb 2020-01-17 stsp * was recorded in the file index.
2610 867630bb 2020-01-17 stsp */
2611 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2612 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2613 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2614 9c6338c4 2019-06-02 stsp done:
2615 9c6338c4 2019-06-02 stsp if (new_index)
2616 9c6338c4 2019-06-02 stsp fclose(new_index);
2617 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2618 9c6338c4 2019-06-02 stsp return err;
2619 c932eeeb 2019-05-22 stsp }
2620 6ced4176 2019-07-12 stsp
2621 6ced4176 2019-07-12 stsp static const struct got_error *
2622 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2623 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2624 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2625 945f9229 2022-04-16 thomas struct got_repository *repo)
2626 6ced4176 2019-07-12 stsp {
2627 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2628 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2629 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2630 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2631 6ced4176 2019-07-12 stsp
2632 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2633 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2634 6ced4176 2019-07-12 stsp *tree_id = NULL;
2635 6ced4176 2019-07-12 stsp
2636 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2637 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2638 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2639 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2640 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2641 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2642 6ced4176 2019-07-12 stsp goto done;
2643 6ced4176 2019-07-12 stsp }
2644 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2645 945f9229 2022-04-16 thomas worktree->path_prefix);
2646 6ced4176 2019-07-12 stsp if (err)
2647 6ced4176 2019-07-12 stsp goto done;
2648 6ced4176 2019-07-12 stsp return NULL;
2649 6ced4176 2019-07-12 stsp }
2650 6ced4176 2019-07-12 stsp
2651 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2652 6ced4176 2019-07-12 stsp
2653 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2654 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2655 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2656 6ced4176 2019-07-12 stsp goto done;
2657 6ced4176 2019-07-12 stsp }
2658 6ced4176 2019-07-12 stsp
2659 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2660 6ced4176 2019-07-12 stsp if (err)
2661 6ced4176 2019-07-12 stsp goto done;
2662 6ced4176 2019-07-12 stsp
2663 6ced4176 2019-07-12 stsp free(in_repo_path);
2664 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2665 6ced4176 2019-07-12 stsp
2666 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2667 6ced4176 2019-07-12 stsp if (err)
2668 6ced4176 2019-07-12 stsp goto done;
2669 c932eeeb 2019-05-22 stsp
2670 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2671 6ced4176 2019-07-12 stsp /* Check out a single file. */
2672 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2673 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2674 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2675 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2676 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2677 6ced4176 2019-07-12 stsp goto done;
2678 6ced4176 2019-07-12 stsp }
2679 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2680 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2681 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2682 6ced4176 2019-07-12 stsp goto done;
2683 6ced4176 2019-07-12 stsp }
2684 6ced4176 2019-07-12 stsp } else {
2685 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2686 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2687 6ced4176 2019-07-12 stsp if (err)
2688 6ced4176 2019-07-12 stsp return err;
2689 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2690 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2691 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2692 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2693 6ced4176 2019-07-12 stsp goto done;
2694 6ced4176 2019-07-12 stsp }
2695 6ced4176 2019-07-12 stsp }
2696 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2697 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2698 6ced4176 2019-07-12 stsp } else {
2699 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2700 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2701 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2702 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2703 6ced4176 2019-07-12 stsp goto done;
2704 6ced4176 2019-07-12 stsp }
2705 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2706 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2707 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2708 6ced4176 2019-07-12 stsp goto done;
2709 6ced4176 2019-07-12 stsp }
2710 6ced4176 2019-07-12 stsp }
2711 6ced4176 2019-07-12 stsp done:
2712 6ced4176 2019-07-12 stsp free(id);
2713 6ced4176 2019-07-12 stsp free(in_repo_path);
2714 6ced4176 2019-07-12 stsp if (err) {
2715 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2716 6ced4176 2019-07-12 stsp free(*tree_relpath);
2717 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2718 6ced4176 2019-07-12 stsp free(*tree_id);
2719 6ced4176 2019-07-12 stsp *tree_id = NULL;
2720 6ced4176 2019-07-12 stsp }
2721 07ed472d 2019-07-12 stsp return err;
2722 07ed472d 2019-07-12 stsp }
2723 07ed472d 2019-07-12 stsp
2724 07ed472d 2019-07-12 stsp static const struct got_error *
2725 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2726 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2727 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2728 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2729 07ed472d 2019-07-12 stsp {
2730 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2731 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2732 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2733 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2734 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2735 07ed472d 2019-07-12 stsp
2736 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2737 7f47418f 2019-12-20 stsp if (err) {
2738 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2739 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2740 7f47418f 2019-12-20 stsp goto done;
2741 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2742 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2743 7f47418f 2019-12-20 stsp if (err)
2744 7f47418f 2019-12-20 stsp return err;
2745 7f47418f 2019-12-20 stsp }
2746 07ed472d 2019-07-12 stsp
2747 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2748 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2749 07ed472d 2019-07-12 stsp if (err)
2750 07ed472d 2019-07-12 stsp goto done;
2751 07ed472d 2019-07-12 stsp
2752 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2753 07ed472d 2019-07-12 stsp if (err)
2754 07ed472d 2019-07-12 stsp goto done;
2755 07ed472d 2019-07-12 stsp
2756 07ed472d 2019-07-12 stsp if (entry_name &&
2757 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2758 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2759 07ed472d 2019-07-12 stsp goto done;
2760 07ed472d 2019-07-12 stsp }
2761 07ed472d 2019-07-12 stsp
2762 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2763 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2764 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2765 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2766 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2767 07ed472d 2019-07-12 stsp arg.repo = repo;
2768 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2769 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2770 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2771 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2772 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2773 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2774 07ed472d 2019-07-12 stsp done:
2775 07ed472d 2019-07-12 stsp if (tree)
2776 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2777 07ed472d 2019-07-12 stsp if (commit)
2778 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2779 6ced4176 2019-07-12 stsp return err;
2780 6ced4176 2019-07-12 stsp }
2781 6ced4176 2019-07-12 stsp
2782 9d31a1d8 2018-03-11 stsp const struct got_error *
2783 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2784 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2785 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2786 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2787 9d31a1d8 2018-03-11 stsp {
2788 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2789 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2790 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2791 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2792 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2793 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2794 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2795 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2796 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2797 f2ea84fa 2019-07-27 stsp int entry_type;
2798 f2ea84fa 2019-07-27 stsp char *relpath;
2799 f2ea84fa 2019-07-27 stsp char *entry_name;
2800 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2801 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2802 9d31a1d8 2018-03-11 stsp
2803 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2804 f2ea84fa 2019-07-27 stsp
2805 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2806 9d31a1d8 2018-03-11 stsp if (err)
2807 9d31a1d8 2018-03-11 stsp return err;
2808 945f9229 2022-04-16 thomas
2809 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2810 945f9229 2022-04-16 thomas worktree->base_commit_id);
2811 945f9229 2022-04-16 thomas if (err)
2812 945f9229 2022-04-16 thomas goto done;
2813 93a30277 2018-12-24 stsp
2814 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2815 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2816 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2817 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2818 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2819 f2ea84fa 2019-07-27 stsp goto done;
2820 f2ea84fa 2019-07-27 stsp }
2821 6ced4176 2019-07-12 stsp
2822 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2823 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2824 945f9229 2022-04-16 thomas worktree, repo);
2825 f2ea84fa 2019-07-27 stsp if (err) {
2826 f2ea84fa 2019-07-27 stsp free(tpd);
2827 6ced4176 2019-07-12 stsp goto done;
2828 6ced4176 2019-07-12 stsp }
2829 f2ea84fa 2019-07-27 stsp
2830 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2831 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2832 f2ea84fa 2019-07-27 stsp if (err) {
2833 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2834 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2835 f2ea84fa 2019-07-27 stsp free(tpd);
2836 f2ea84fa 2019-07-27 stsp goto done;
2837 f2ea84fa 2019-07-27 stsp }
2838 f2ea84fa 2019-07-27 stsp } else
2839 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2840 f2ea84fa 2019-07-27 stsp
2841 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2842 6ced4176 2019-07-12 stsp }
2843 6ced4176 2019-07-12 stsp
2844 51514078 2018-12-25 stsp /*
2845 51514078 2018-12-25 stsp * Read the file index.
2846 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2847 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2848 51514078 2018-12-25 stsp */
2849 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2850 8da9e5f4 2019-01-12 stsp if (err)
2851 c4cdcb68 2019-04-03 stsp goto done;
2852 c4cdcb68 2019-04-03 stsp
2853 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2854 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2855 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2856 f2ea84fa 2019-07-27 stsp
2857 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2858 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2859 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2860 f2ea84fa 2019-07-27 stsp if (err)
2861 f2ea84fa 2019-07-27 stsp break;
2862 f2ea84fa 2019-07-27 stsp
2863 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2864 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2865 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2866 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2867 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2868 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2869 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2870 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2871 f2ea84fa 2019-07-27 stsp if (err)
2872 f2ea84fa 2019-07-27 stsp break;
2873 f2ea84fa 2019-07-27 stsp
2874 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2875 711d9cd9 2019-07-12 stsp }
2876 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2877 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2878 af12c6b9 2019-06-04 stsp err = sync_err;
2879 9d31a1d8 2018-03-11 stsp done:
2880 9c6338c4 2019-06-02 stsp free(fileindex_path);
2881 edfa7d7f 2018-09-11 stsp if (tree)
2882 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2883 9d31a1d8 2018-03-11 stsp if (commit)
2884 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2885 5ade8233 2019-07-12 stsp if (fileindex)
2886 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2887 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2888 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2889 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2890 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2891 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2892 f2ea84fa 2019-07-27 stsp free(tpd);
2893 f2ea84fa 2019-07-27 stsp }
2894 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2895 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2896 9d31a1d8 2018-03-11 stsp err = unlockerr;
2897 f8d1f275 2019-02-04 stsp return err;
2898 f8d1f275 2019-02-04 stsp }
2899 f8d1f275 2019-02-04 stsp
2900 0529f8df 2023-03-10 thomas static const struct got_error *
2901 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2902 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2903 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2904 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2905 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2906 0529f8df 2023-03-10 thomas struct got_repository *repo,
2907 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2908 0529f8df 2023-03-10 thomas {
2909 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2910 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2911 0529f8df 2023-03-10 thomas
2912 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2913 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2914 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2915 0529f8df 2023-03-10 thomas restoring_missing_file,
2916 0529f8df 2023-03-10 thomas reverting_versioned_file,
2917 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2918 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2919 0529f8df 2023-03-10 thomas } else {
2920 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2921 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2922 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2923 0529f8df 2023-03-10 thomas path_is_unversioned, repo, progress_cb, progress_arg);
2924 0529f8df 2023-03-10 thomas }
2925 0529f8df 2023-03-10 thomas if (err)
2926 0529f8df 2023-03-10 thomas return err;
2927 0529f8df 2023-03-10 thomas if (ie == NULL) {
2928 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2929 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2930 0529f8df 2023-03-10 thomas if (err)
2931 0529f8df 2023-03-10 thomas return err;
2932 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2933 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2934 0529f8df 2023-03-10 thomas if (err) {
2935 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2936 0529f8df 2023-03-10 thomas return err;
2937 0529f8df 2023-03-10 thomas }
2938 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2939 0529f8df 2023-03-10 thomas if (err) {
2940 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2941 0529f8df 2023-03-10 thomas return err;
2942 0529f8df 2023-03-10 thomas }
2943 0529f8df 2023-03-10 thomas } else {
2944 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2945 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2946 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2947 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2948 0529f8df 2023-03-10 thomas if (err)
2949 0529f8df 2023-03-10 thomas return err;
2950 0529f8df 2023-03-10 thomas }
2951 0529f8df 2023-03-10 thomas
2952 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2953 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2954 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2955 0529f8df 2023-03-10 thomas }
2956 0529f8df 2023-03-10 thomas
2957 0529f8df 2023-03-10 thomas return NULL;
2958 0529f8df 2023-03-10 thomas }
2959 0529f8df 2023-03-10 thomas
2960 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2961 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2962 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2963 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2964 234035bc 2019-06-01 stsp void *progress_arg;
2965 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2966 234035bc 2019-06-01 stsp void *cancel_arg;
2967 f69721c3 2019-10-21 stsp const char *label_orig;
2968 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2969 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2970 234035bc 2019-06-01 stsp };
2971 234035bc 2019-06-01 stsp
2972 234035bc 2019-06-01 stsp static const struct got_error *
2973 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2974 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2975 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2976 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2977 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2978 234035bc 2019-06-01 stsp {
2979 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2980 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2981 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2982 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2983 234035bc 2019-06-01 stsp struct stat sb;
2984 234035bc 2019-06-01 stsp unsigned char status;
2985 234035bc 2019-06-01 stsp int local_changes_subsumed;
2986 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2987 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2988 234035bc 2019-06-01 stsp
2989 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2990 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2991 d6c87207 2019-08-02 stsp strlen(path2));
2992 1ee397ad 2019-07-12 stsp if (ie == NULL)
2993 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2994 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2995 234035bc 2019-06-01 stsp
2996 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2997 234035bc 2019-06-01 stsp path2) == -1)
2998 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2999 234035bc 2019-06-01 stsp
3000 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3001 7f91a133 2019-12-13 stsp repo);
3002 234035bc 2019-06-01 stsp if (err)
3003 234035bc 2019-06-01 stsp goto done;
3004 234035bc 2019-06-01 stsp
3005 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3006 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3007 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3008 234035bc 2019-06-01 stsp goto done;
3009 234035bc 2019-06-01 stsp }
3010 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3011 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3012 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3013 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3014 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3015 234035bc 2019-06-01 stsp goto done;
3016 234035bc 2019-06-01 stsp }
3017 234035bc 2019-06-01 stsp
3018 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3019 36bf999c 2020-07-23 stsp char *link_target2;
3020 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3021 36bf999c 2020-07-23 stsp if (err)
3022 36bf999c 2020-07-23 stsp goto done;
3023 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3024 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3025 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3026 36bf999c 2020-07-23 stsp free(link_target2);
3027 af57b12a 2020-07-23 stsp } else {
3028 1af628f4 2021-06-03 stsp int fd;
3029 1af628f4 2021-06-03 stsp
3030 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3031 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3032 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3033 1af628f4 2021-06-03 stsp goto done;
3034 1af628f4 2021-06-03 stsp }
3035 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3036 1af628f4 2021-06-03 stsp f_orig, blob1);
3037 1af628f4 2021-06-03 stsp if (err)
3038 1af628f4 2021-06-03 stsp goto done;
3039 1af628f4 2021-06-03 stsp
3040 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3041 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3042 1af628f4 2021-06-03 stsp goto done;
3043 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3044 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3045 1af628f4 2021-06-03 stsp if (err)
3046 1af628f4 2021-06-03 stsp goto done;
3047 1af628f4 2021-06-03 stsp
3048 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3049 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3050 1af628f4 2021-06-03 stsp if (fd == -1) {
3051 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3052 1af628f4 2021-06-03 stsp ondisk_path);
3053 1af628f4 2021-06-03 stsp goto done;
3054 1af628f4 2021-06-03 stsp }
3055 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3056 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3057 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3058 1af628f4 2021-06-03 stsp ondisk_path);
3059 1af628f4 2021-06-03 stsp close(fd);
3060 1af628f4 2021-06-03 stsp goto done;
3061 1af628f4 2021-06-03 stsp }
3062 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3063 1af628f4 2021-06-03 stsp if (err)
3064 1af628f4 2021-06-03 stsp goto done;
3065 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3066 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3067 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3068 1af628f4 2021-06-03 stsp goto done;
3069 1af628f4 2021-06-03 stsp }
3070 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3071 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3072 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3073 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3074 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3075 af57b12a 2020-07-23 stsp }
3076 234035bc 2019-06-01 stsp } else if (blob1) {
3077 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3078 d6c87207 2019-08-02 stsp strlen(path1));
3079 1ee397ad 2019-07-12 stsp if (ie == NULL)
3080 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3081 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3082 2b92fad7 2019-06-02 stsp
3083 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3084 2b92fad7 2019-06-02 stsp path1) == -1)
3085 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3086 2b92fad7 2019-06-02 stsp
3087 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3088 7f91a133 2019-12-13 stsp repo);
3089 2b92fad7 2019-06-02 stsp if (err)
3090 2b92fad7 2019-06-02 stsp goto done;
3091 2b92fad7 2019-06-02 stsp
3092 2b92fad7 2019-06-02 stsp switch (status) {
3093 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3094 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3095 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3096 1ee397ad 2019-07-12 stsp if (err)
3097 1ee397ad 2019-07-12 stsp goto done;
3098 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3099 2b92fad7 2019-06-02 stsp if (err)
3100 2b92fad7 2019-06-02 stsp goto done;
3101 2b92fad7 2019-06-02 stsp if (ie)
3102 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3103 2b92fad7 2019-06-02 stsp break;
3104 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3105 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3106 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3107 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3108 1ee397ad 2019-07-12 stsp if (err)
3109 1ee397ad 2019-07-12 stsp goto done;
3110 2b92fad7 2019-06-02 stsp if (ie)
3111 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3112 2b92fad7 2019-06-02 stsp break;
3113 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3114 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3115 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3116 e8f02263 2022-01-23 thomas off_t blob1_size;
3117 0a22ca1a 2020-09-23 stsp /*
3118 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3119 0a22ca1a 2020-09-23 stsp * exists in the repository.
3120 0a22ca1a 2020-09-23 stsp */
3121 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3122 e8f02263 2022-01-23 thomas &blob1_size, path1);
3123 0a22ca1a 2020-09-23 stsp if (err)
3124 0a22ca1a 2020-09-23 stsp goto done;
3125 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3126 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3127 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3128 0a22ca1a 2020-09-23 stsp if (err)
3129 0a22ca1a 2020-09-23 stsp goto done;
3130 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3131 0a22ca1a 2020-09-23 stsp path1);
3132 0a22ca1a 2020-09-23 stsp if (err)
3133 0a22ca1a 2020-09-23 stsp goto done;
3134 0a22ca1a 2020-09-23 stsp if (ie)
3135 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3136 0a22ca1a 2020-09-23 stsp ie);
3137 0a22ca1a 2020-09-23 stsp } else {
3138 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3139 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3140 0a22ca1a 2020-09-23 stsp }
3141 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3142 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3143 0a22ca1a 2020-09-23 stsp free(id);
3144 0a22ca1a 2020-09-23 stsp if (err)
3145 0a22ca1a 2020-09-23 stsp goto done;
3146 0a22ca1a 2020-09-23 stsp break;
3147 0a22ca1a 2020-09-23 stsp }
3148 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3149 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3150 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3151 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3152 1ee397ad 2019-07-12 stsp if (err)
3153 1ee397ad 2019-07-12 stsp goto done;
3154 2b92fad7 2019-06-02 stsp break;
3155 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3156 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3157 1ee397ad 2019-07-12 stsp if (err)
3158 1ee397ad 2019-07-12 stsp goto done;
3159 2b92fad7 2019-06-02 stsp break;
3160 2b92fad7 2019-06-02 stsp default:
3161 2b92fad7 2019-06-02 stsp break;
3162 2b92fad7 2019-06-02 stsp }
3163 234035bc 2019-06-01 stsp } else if (blob2) {
3164 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3165 234035bc 2019-06-01 stsp path2) == -1)
3166 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3167 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3168 d6c87207 2019-08-02 stsp strlen(path2));
3169 234035bc 2019-06-01 stsp if (ie) {
3170 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3171 7f91a133 2019-12-13 stsp -1, NULL, repo);
3172 234035bc 2019-06-01 stsp if (err)
3173 234035bc 2019-06-01 stsp goto done;
3174 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3175 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3176 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3177 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3178 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3179 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3180 1ee397ad 2019-07-12 stsp status, path2);
3181 234035bc 2019-06-01 stsp goto done;
3182 234035bc 2019-06-01 stsp }
3183 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3184 36bf999c 2020-07-23 stsp char *link_target2;
3185 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3186 36bf999c 2020-07-23 stsp blob2);
3187 36bf999c 2020-07-23 stsp if (err)
3188 36bf999c 2020-07-23 stsp goto done;
3189 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3190 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3191 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3192 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3193 36bf999c 2020-07-23 stsp free(link_target2);
3194 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3195 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3196 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3197 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3198 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3199 526a746f 2020-07-23 stsp a->progress_arg);
3200 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3201 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3202 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3203 526a746f 2020-07-23 stsp }
3204 dfe9fba0 2020-07-23 stsp if (err)
3205 dfe9fba0 2020-07-23 stsp goto done;
3206 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3207 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3208 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3209 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3210 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3211 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3212 234035bc 2019-06-01 stsp if (err)
3213 234035bc 2019-06-01 stsp goto done;
3214 234035bc 2019-06-01 stsp }
3215 234035bc 2019-06-01 stsp } else {
3216 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3217 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3218 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3219 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3220 234035bc 2019-06-01 stsp if (err)
3221 2b92fad7 2019-06-02 stsp goto done;
3222 234035bc 2019-06-01 stsp }
3223 234035bc 2019-06-01 stsp }
3224 234035bc 2019-06-01 stsp done:
3225 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3226 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3227 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3228 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3229 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3230 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3231 1af628f4 2021-06-03 stsp free(id_str);
3232 54d5be07 2021-06-03 stsp free(label_deriv2);
3233 234035bc 2019-06-01 stsp free(ondisk_path);
3234 234035bc 2019-06-01 stsp return err;
3235 234035bc 2019-06-01 stsp }
3236 234035bc 2019-06-01 stsp
3237 69de9dd4 2021-09-03 stsp static const struct got_error *
3238 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3239 69de9dd4 2021-09-03 stsp {
3240 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3241 69de9dd4 2021-09-03 stsp
3242 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3243 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3244 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3245 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3246 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3247 69de9dd4 2021-09-03 stsp
3248 69de9dd4 2021-09-03 stsp return NULL;
3249 69de9dd4 2021-09-03 stsp }
3250 69de9dd4 2021-09-03 stsp
3251 349dfd1e 2023-07-23 thomas const struct got_error *
3252 349dfd1e 2023-07-23 thomas got_worktree_get_state(char *state, struct got_repository *repo,
3253 349dfd1e 2023-07-23 thomas struct got_worktree *worktree)
3254 349dfd1e 2023-07-23 thomas {
3255 349dfd1e 2023-07-23 thomas const struct got_error *err;
3256 349dfd1e 2023-07-23 thomas struct got_object_id *base_id, *head_id = NULL;
3257 349dfd1e 2023-07-23 thomas struct got_reference *head_ref;
3258 349dfd1e 2023-07-23 thomas struct got_fileindex *fileindex = NULL;
3259 349dfd1e 2023-07-23 thomas char *fileindex_path = NULL;
3260 349dfd1e 2023-07-23 thomas
3261 349dfd1e 2023-07-23 thomas if (worktree == NULL)
3262 349dfd1e 2023-07-23 thomas return got_error(GOT_ERR_NOT_WORKTREE);
3263 349dfd1e 2023-07-23 thomas
3264 349dfd1e 2023-07-23 thomas err = got_ref_open(&head_ref, repo,
3265 349dfd1e 2023-07-23 thomas got_worktree_get_head_ref_name(worktree), 0);
3266 349dfd1e 2023-07-23 thomas if (err)
3267 349dfd1e 2023-07-23 thomas return err;
3268 349dfd1e 2023-07-23 thomas
3269 349dfd1e 2023-07-23 thomas err = got_ref_resolve(&head_id, repo, head_ref);
3270 349dfd1e 2023-07-23 thomas if (err)
3271 349dfd1e 2023-07-23 thomas goto done;
3272 349dfd1e 2023-07-23 thomas
3273 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UNKNOWN;
3274 349dfd1e 2023-07-23 thomas base_id = got_worktree_get_base_commit_id(worktree);
3275 349dfd1e 2023-07-23 thomas
3276 349dfd1e 2023-07-23 thomas if (got_object_id_cmp(base_id, head_id) == 0) {
3277 349dfd1e 2023-07-23 thomas err = open_fileindex(&fileindex, &fileindex_path, worktree);
3278 349dfd1e 2023-07-23 thomas if (err)
3279 349dfd1e 2023-07-23 thomas goto done;
3280 349dfd1e 2023-07-23 thomas
3281 349dfd1e 2023-07-23 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3282 349dfd1e 2023-07-23 thomas check_mixed_commits, worktree);
3283 349dfd1e 2023-07-23 thomas if (err == NULL)
3284 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UPTODATE;
3285 92845f09 2023-07-26 thomas else if (err->code == GOT_ERR_MIXED_COMMITS) {
3286 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3287 349dfd1e 2023-07-23 thomas err = NULL;
3288 92845f09 2023-07-26 thomas }
3289 92845f09 2023-07-26 thomas } else
3290 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3291 349dfd1e 2023-07-23 thomas
3292 349dfd1e 2023-07-23 thomas done:
3293 349dfd1e 2023-07-23 thomas free(head_id);
3294 349dfd1e 2023-07-23 thomas free(fileindex_path);
3295 349dfd1e 2023-07-23 thomas got_ref_close(head_ref);
3296 349dfd1e 2023-07-23 thomas if (fileindex != NULL)
3297 349dfd1e 2023-07-23 thomas got_fileindex_free(fileindex);
3298 349dfd1e 2023-07-23 thomas return err;
3299 349dfd1e 2023-07-23 thomas }
3300 349dfd1e 2023-07-23 thomas
3301 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3302 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3303 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3304 234035bc 2019-06-01 stsp struct got_repository *repo;
3305 234035bc 2019-06-01 stsp };
3306 234035bc 2019-06-01 stsp
3307 234035bc 2019-06-01 stsp static const struct got_error *
3308 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3309 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3310 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3311 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3312 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3313 234035bc 2019-06-01 stsp {
3314 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3315 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3316 234035bc 2019-06-01 stsp unsigned char status;
3317 234035bc 2019-06-01 stsp struct stat sb;
3318 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3319 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3320 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3321 234035bc 2019-06-01 stsp char *ondisk_path;
3322 234035bc 2019-06-01 stsp
3323 69de9dd4 2021-09-03 stsp if (id == NULL)
3324 69de9dd4 2021-09-03 stsp return NULL;
3325 234035bc 2019-06-01 stsp
3326 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3327 69de9dd4 2021-09-03 stsp if (ie == NULL)
3328 69de9dd4 2021-09-03 stsp return NULL;
3329 69de9dd4 2021-09-03 stsp
3330 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3331 234035bc 2019-06-01 stsp == -1)
3332 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3333 234035bc 2019-06-01 stsp
3334 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3335 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3336 5546d466 2021-09-02 stsp free(ondisk_path);
3337 234035bc 2019-06-01 stsp if (err)
3338 234035bc 2019-06-01 stsp return err;
3339 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3340 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3341 234035bc 2019-06-01 stsp
3342 234035bc 2019-06-01 stsp return NULL;
3343 234035bc 2019-06-01 stsp }
3344 234035bc 2019-06-01 stsp
3345 818c7501 2019-07-11 stsp static const struct got_error *
3346 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3347 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3348 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3349 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3350 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3351 234035bc 2019-06-01 stsp {
3352 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3353 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3354 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3355 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3356 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3357 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3358 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3359 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3360 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3361 234035bc 2019-06-01 stsp
3362 03415a1a 2019-06-02 stsp if (commit_id1) {
3363 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3364 945f9229 2022-04-16 thomas if (err)
3365 945f9229 2022-04-16 thomas goto done;
3366 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3367 03415a1a 2019-06-02 stsp worktree->path_prefix);
3368 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3369 03415a1a 2019-06-02 stsp goto done;
3370 69d57f3d 2020-07-31 stsp }
3371 69d57f3d 2020-07-31 stsp if (tree_id1) {
3372 69d57f3d 2020-07-31 stsp char *id_str;
3373 03415a1a 2019-06-02 stsp
3374 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3375 03415a1a 2019-06-02 stsp if (err)
3376 03415a1a 2019-06-02 stsp goto done;
3377 f69721c3 2019-10-21 stsp
3378 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3379 f69721c3 2019-10-21 stsp if (err)
3380 f69721c3 2019-10-21 stsp goto done;
3381 f69721c3 2019-10-21 stsp
3382 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3383 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3384 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3385 f69721c3 2019-10-21 stsp free(id_str);
3386 f69721c3 2019-10-21 stsp goto done;
3387 f69721c3 2019-10-21 stsp }
3388 f69721c3 2019-10-21 stsp free(id_str);
3389 a0f32f33 2022-06-13 thomas
3390 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3391 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3392 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3393 a0f32f33 2022-06-13 thomas goto done;
3394 a0f32f33 2022-06-13 thomas }
3395 03415a1a 2019-06-02 stsp }
3396 234035bc 2019-06-01 stsp
3397 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3398 945f9229 2022-04-16 thomas if (err)
3399 945f9229 2022-04-16 thomas goto done;
3400 945f9229 2022-04-16 thomas
3401 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3402 234035bc 2019-06-01 stsp worktree->path_prefix);
3403 234035bc 2019-06-01 stsp if (err)
3404 234035bc 2019-06-01 stsp goto done;
3405 234035bc 2019-06-01 stsp
3406 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3407 234035bc 2019-06-01 stsp if (err)
3408 234035bc 2019-06-01 stsp goto done;
3409 234035bc 2019-06-01 stsp
3410 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3411 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3412 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3413 19a6a6b5 2022-07-01 thomas goto done;
3414 19a6a6b5 2022-07-01 thomas }
3415 19a6a6b5 2022-07-01 thomas
3416 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3417 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3418 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3419 a0f32f33 2022-06-13 thomas goto done;
3420 a0f32f33 2022-06-13 thomas }
3421 a0f32f33 2022-06-13 thomas
3422 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3423 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3424 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3425 19a6a6b5 2022-07-01 thomas goto done;
3426 19a6a6b5 2022-07-01 thomas }
3427 19a6a6b5 2022-07-01 thomas
3428 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3429 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3430 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3431 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3432 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3433 69de9dd4 2021-09-03 stsp if (err)
3434 69de9dd4 2021-09-03 stsp goto done;
3435 69de9dd4 2021-09-03 stsp
3436 234035bc 2019-06-01 stsp arg.worktree = worktree;
3437 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3438 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3439 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3440 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3441 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3442 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3443 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3444 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3445 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3446 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3447 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3448 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3449 af12c6b9 2019-06-04 stsp err = sync_err;
3450 234035bc 2019-06-01 stsp done:
3451 945f9229 2022-04-16 thomas if (commit1)
3452 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3453 945f9229 2022-04-16 thomas if (commit2)
3454 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3455 234035bc 2019-06-01 stsp if (tree1)
3456 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3457 234035bc 2019-06-01 stsp if (tree2)
3458 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3459 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3460 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3461 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3462 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3463 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3464 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3465 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3466 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3467 f69721c3 2019-10-21 stsp free(label_orig);
3468 818c7501 2019-07-11 stsp return err;
3469 818c7501 2019-07-11 stsp }
3470 234035bc 2019-06-01 stsp
3471 818c7501 2019-07-11 stsp const struct got_error *
3472 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3473 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3474 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3475 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3476 818c7501 2019-07-11 stsp {
3477 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3478 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3479 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3480 818c7501 2019-07-11 stsp
3481 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3482 818c7501 2019-07-11 stsp if (err)
3483 818c7501 2019-07-11 stsp return err;
3484 818c7501 2019-07-11 stsp
3485 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3486 818c7501 2019-07-11 stsp if (err)
3487 818c7501 2019-07-11 stsp goto done;
3488 818c7501 2019-07-11 stsp
3489 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3490 69de9dd4 2021-09-03 stsp worktree);
3491 818c7501 2019-07-11 stsp if (err)
3492 818c7501 2019-07-11 stsp goto done;
3493 818c7501 2019-07-11 stsp
3494 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3495 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3496 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3497 818c7501 2019-07-11 stsp done:
3498 818c7501 2019-07-11 stsp if (fileindex)
3499 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3500 818c7501 2019-07-11 stsp free(fileindex_path);
3501 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3502 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3503 234035bc 2019-06-01 stsp err = unlockerr;
3504 234035bc 2019-06-01 stsp return err;
3505 234035bc 2019-06-01 stsp }
3506 234035bc 2019-06-01 stsp
3507 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3508 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3509 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3510 927df6b7 2019-02-10 stsp const char *status_path;
3511 927df6b7 2019-02-10 stsp size_t status_path_len;
3512 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3513 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3514 f8d1f275 2019-02-04 stsp void *status_arg;
3515 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3516 f8d1f275 2019-02-04 stsp void *cancel_arg;
3517 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3518 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3519 f2a9dc41 2019-12-13 tracey int report_unchanged;
3520 3143d852 2020-06-25 stsp int no_ignores;
3521 f8d1f275 2019-02-04 stsp };
3522 88d0e355 2019-08-03 stsp
3523 f8d1f275 2019-02-04 stsp static const struct got_error *
3524 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3525 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3526 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3527 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3528 927df6b7 2019-02-10 stsp {
3529 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3530 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3531 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3532 927df6b7 2019-02-10 stsp struct stat sb;
3533 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3534 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3535 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3536 927df6b7 2019-02-10 stsp
3537 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3538 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3539 98eaaa12 2019-08-03 stsp if (err)
3540 98eaaa12 2019-08-03 stsp return err;
3541 98eaaa12 2019-08-03 stsp
3542 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3543 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3544 98eaaa12 2019-08-03 stsp return NULL;
3545 98eaaa12 2019-08-03 stsp
3546 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3547 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3548 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3549 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3550 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3551 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3552 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3553 43010591 2023-02-17 thomas &staged_blob_id, ie);
3554 98eaaa12 2019-08-03 stsp }
3555 98eaaa12 2019-08-03 stsp
3556 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3557 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3558 927df6b7 2019-02-10 stsp }
3559 927df6b7 2019-02-10 stsp
3560 927df6b7 2019-02-10 stsp static const struct got_error *
3561 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3562 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3563 f8d1f275 2019-02-04 stsp {
3564 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3565 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3566 f8d1f275 2019-02-04 stsp char *abspath;
3567 f8d1f275 2019-02-04 stsp
3568 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3569 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3570 0584f854 2019-04-06 stsp
3571 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3572 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3573 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3574 927df6b7 2019-02-10 stsp return NULL;
3575 927df6b7 2019-02-10 stsp
3576 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3577 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3578 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3579 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3580 f8d1f275 2019-02-04 stsp } else {
3581 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3582 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3583 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3584 f8d1f275 2019-02-04 stsp }
3585 f8d1f275 2019-02-04 stsp
3586 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3587 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3588 f8d1f275 2019-02-04 stsp free(abspath);
3589 9d31a1d8 2018-03-11 stsp return err;
3590 9d31a1d8 2018-03-11 stsp }
3591 f8d1f275 2019-02-04 stsp
3592 f8d1f275 2019-02-04 stsp static const struct got_error *
3593 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3594 f8d1f275 2019-02-04 stsp {
3595 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3596 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3597 2ec1f75b 2019-03-26 stsp unsigned char status;
3598 927df6b7 2019-02-10 stsp
3599 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3600 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3601 0584f854 2019-04-06 stsp
3602 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3603 927df6b7 2019-02-10 stsp return NULL;
3604 927df6b7 2019-02-10 stsp
3605 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3606 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3607 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3608 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3609 2ec1f75b 2019-03-26 stsp else
3610 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3611 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3612 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3613 6841da00 2019-08-08 stsp }
3614 6841da00 2019-08-08 stsp
3615 ef20f542 2022-06-26 thomas static void
3616 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3617 6841da00 2019-08-08 stsp {
3618 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3619 6841da00 2019-08-08 stsp
3620 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3621 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3622 21c2d8be 2023-01-10 thomas
3623 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3624 6841da00 2019-08-08 stsp }
3625 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3626 6841da00 2019-08-08 stsp }
3627 6841da00 2019-08-08 stsp
3628 6841da00 2019-08-08 stsp static const struct got_error *
3629 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3630 6841da00 2019-08-08 stsp {
3631 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3632 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3633 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3634 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3635 6841da00 2019-08-08 stsp size_t linesize = 0;
3636 6841da00 2019-08-08 stsp ssize_t linelen;
3637 6841da00 2019-08-08 stsp
3638 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3639 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3640 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3641 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3642 6841da00 2019-08-08 stsp
3643 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3644 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3645 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3646 bd8de430 2019-10-04 stsp
3647 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3648 bd8de430 2019-10-04 stsp if (line[0] == '#')
3649 bd8de430 2019-10-04 stsp continue;
3650 bd8de430 2019-10-04 stsp
3651 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3652 bd8de430 2019-10-04 stsp if (line[0] == '!')
3653 bd8de430 2019-10-04 stsp continue;
3654 bd8de430 2019-10-04 stsp
3655 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3656 6841da00 2019-08-08 stsp line) == -1) {
3657 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3658 6841da00 2019-08-08 stsp goto done;
3659 6841da00 2019-08-08 stsp }
3660 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3661 6841da00 2019-08-08 stsp if (err)
3662 6841da00 2019-08-08 stsp goto done;
3663 6841da00 2019-08-08 stsp }
3664 6841da00 2019-08-08 stsp if (ferror(f)) {
3665 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3666 6841da00 2019-08-08 stsp goto done;
3667 6841da00 2019-08-08 stsp }
3668 6841da00 2019-08-08 stsp
3669 6841da00 2019-08-08 stsp dirpath = strdup(path);
3670 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3671 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3672 6841da00 2019-08-08 stsp goto done;
3673 6841da00 2019-08-08 stsp }
3674 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3675 6841da00 2019-08-08 stsp done:
3676 6841da00 2019-08-08 stsp free(line);
3677 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3678 6841da00 2019-08-08 stsp free(dirpath);
3679 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3680 6841da00 2019-08-08 stsp }
3681 6841da00 2019-08-08 stsp return err;
3682 1b5d300f 2023-02-20 thomas }
3683 1b5d300f 2023-02-20 thomas
3684 1b5d300f 2023-02-20 thomas static int
3685 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3686 1b5d300f 2023-02-20 thomas int flags)
3687 1b5d300f 2023-02-20 thomas {
3688 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3689 1b5d300f 2023-02-20 thomas
3690 1b5d300f 2023-02-20 thomas /*
3691 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3692 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3693 1b5d300f 2023-02-20 thomas */
3694 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3695 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3696 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3697 1b5d300f 2023-02-20 thomas
3698 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3699 1b5d300f 2023-02-20 thomas }
3700 1b5d300f 2023-02-20 thomas
3701 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3702 6841da00 2019-08-08 stsp }
3703 6841da00 2019-08-08 stsp
3704 ef20f542 2022-06-26 thomas static int
3705 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3706 6841da00 2019-08-08 stsp {
3707 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3708 bd8de430 2019-10-04 stsp
3709 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3710 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3711 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3712 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3713 bd8de430 2019-10-04 stsp
3714 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3715 1b5d300f 2023-02-20 thomas const char *p;
3716 6841da00 2019-08-08 stsp
3717 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3718 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3719 bd8de430 2019-10-04 stsp continue;
3720 bd8de430 2019-10-04 stsp p = path;
3721 bd8de430 2019-10-04 stsp while (*p) {
3722 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3723 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3724 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3725 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3726 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3727 bd8de430 2019-10-04 stsp p++;
3728 bd8de430 2019-10-04 stsp while (*p == '/')
3729 bd8de430 2019-10-04 stsp p++;
3730 bd8de430 2019-10-04 stsp continue;
3731 bd8de430 2019-10-04 stsp }
3732 bd8de430 2019-10-04 stsp return 1;
3733 bd8de430 2019-10-04 stsp }
3734 bd8de430 2019-10-04 stsp }
3735 bd8de430 2019-10-04 stsp }
3736 bd8de430 2019-10-04 stsp
3737 6841da00 2019-08-08 stsp /*
3738 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3739 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3740 6841da00 2019-08-08 stsp * ignores backwards.
3741 6841da00 2019-08-08 stsp */
3742 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3743 6841da00 2019-08-08 stsp while (pe) {
3744 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3745 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3746 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3747 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3748 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3749 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3750 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3751 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3752 1b5d300f 2023-02-20 thomas path, flags))
3753 6841da00 2019-08-08 stsp continue;
3754 6841da00 2019-08-08 stsp return 1;
3755 6841da00 2019-08-08 stsp }
3756 6841da00 2019-08-08 stsp }
3757 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3758 6841da00 2019-08-08 stsp }
3759 6841da00 2019-08-08 stsp
3760 6841da00 2019-08-08 stsp return 0;
3761 6841da00 2019-08-08 stsp }
3762 6841da00 2019-08-08 stsp
3763 6841da00 2019-08-08 stsp static const struct got_error *
3764 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3765 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3766 6841da00 2019-08-08 stsp {
3767 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3768 6841da00 2019-08-08 stsp char *ignorespath;
3769 886cec17 2019-12-15 stsp int fd = -1;
3770 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3771 6841da00 2019-08-08 stsp
3772 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3773 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3774 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3775 6841da00 2019-08-08 stsp
3776 886cec17 2019-12-15 stsp if (dirfd != -1) {
3777 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3778 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3779 886cec17 2019-12-15 stsp if (fd == -1) {
3780 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3781 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3782 886cec17 2019-12-15 stsp ignorespath);
3783 886cec17 2019-12-15 stsp } else {
3784 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3785 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3786 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3787 886cec17 2019-12-15 stsp ignorespath);
3788 886cec17 2019-12-15 stsp else {
3789 886cec17 2019-12-15 stsp fd = -1;
3790 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3791 886cec17 2019-12-15 stsp }
3792 886cec17 2019-12-15 stsp }
3793 886cec17 2019-12-15 stsp } else {
3794 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3795 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3796 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3797 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3798 886cec17 2019-12-15 stsp ignorespath);
3799 886cec17 2019-12-15 stsp } else
3800 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3801 886cec17 2019-12-15 stsp }
3802 6841da00 2019-08-08 stsp
3803 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3804 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3805 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3806 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3807 6841da00 2019-08-08 stsp free(ignorespath);
3808 6841da00 2019-08-08 stsp return err;
3809 f8d1f275 2019-02-04 stsp }
3810 f8d1f275 2019-02-04 stsp
3811 f8d1f275 2019-02-04 stsp static const struct got_error *
3812 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3813 6092c299 2021-10-04 thomas int dirfd)
3814 f8d1f275 2019-02-04 stsp {
3815 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3816 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3817 f8d1f275 2019-02-04 stsp char *path = NULL;
3818 6092c299 2021-10-04 thomas
3819 6092c299 2021-10-04 thomas if (ignore != NULL)
3820 6092c299 2021-10-04 thomas *ignore = 0;
3821 f8d1f275 2019-02-04 stsp
3822 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3823 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3824 0584f854 2019-04-06 stsp
3825 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3826 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3827 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3828 f8d1f275 2019-02-04 stsp } else {
3829 f8d1f275 2019-02-04 stsp path = de->d_name;
3830 f8d1f275 2019-02-04 stsp }
3831 f8d1f275 2019-02-04 stsp
3832 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3833 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3834 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3835 6092c299 2021-10-04 thomas *ignore = 1;
3836 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3837 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3838 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3839 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3840 f8d1f275 2019-02-04 stsp if (parent_path[0])
3841 f8d1f275 2019-02-04 stsp free(path);
3842 b72f483a 2019-02-05 stsp return err;
3843 f8d1f275 2019-02-04 stsp }
3844 f8d1f275 2019-02-04 stsp
3845 347d1d3e 2019-07-12 stsp static const struct got_error *
3846 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3847 3143d852 2020-06-25 stsp {
3848 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3849 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3850 3143d852 2020-06-25 stsp
3851 3143d852 2020-06-25 stsp if (a->no_ignores)
3852 3143d852 2020-06-25 stsp return NULL;
3853 3143d852 2020-06-25 stsp
3854 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3855 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3856 3143d852 2020-06-25 stsp if (err)
3857 3143d852 2020-06-25 stsp return err;
3858 3143d852 2020-06-25 stsp
3859 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3860 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3861 3143d852 2020-06-25 stsp
3862 3143d852 2020-06-25 stsp return err;
3863 3143d852 2020-06-25 stsp }
3864 3143d852 2020-06-25 stsp
3865 3143d852 2020-06-25 stsp static const struct got_error *
3866 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3867 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3868 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3869 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3870 abb4604f 2019-07-27 stsp {
3871 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3872 abb4604f 2019-07-27 stsp struct stat sb;
3873 ff56836b 2021-07-08 stsp
3874 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3875 abb4604f 2019-07-27 stsp if (ie)
3876 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3877 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3878 abb4604f 2019-07-27 stsp
3879 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3880 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3881 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3882 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3883 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3884 abb4604f 2019-07-27 stsp }
3885 abb4604f 2019-07-27 stsp
3886 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3887 fe1d8685 2022-02-12 thomas return NULL;
3888 fe1d8685 2022-02-12 thomas
3889 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3890 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3891 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3892 abb4604f 2019-07-27 stsp
3893 abb4604f 2019-07-27 stsp return NULL;
3894 3143d852 2020-06-25 stsp }
3895 3143d852 2020-06-25 stsp
3896 3143d852 2020-06-25 stsp static const struct got_error *
3897 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3898 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3899 3143d852 2020-06-25 stsp {
3900 3143d852 2020-06-25 stsp const struct got_error *err;
3901 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3902 3143d852 2020-06-25 stsp
3903 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3904 3143d852 2020-06-25 stsp ".cvsignore");
3905 3143d852 2020-06-25 stsp if (err)
3906 3143d852 2020-06-25 stsp return err;
3907 3143d852 2020-06-25 stsp
3908 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3909 3143d852 2020-06-25 stsp ".gitignore");
3910 3143d852 2020-06-25 stsp if (err)
3911 3143d852 2020-06-25 stsp return err;
3912 3143d852 2020-06-25 stsp
3913 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3914 3143d852 2020-06-25 stsp if (err) {
3915 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3916 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3917 3143d852 2020-06-25 stsp return err;
3918 3143d852 2020-06-25 stsp }
3919 3143d852 2020-06-25 stsp for (;;) {
3920 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3921 3143d852 2020-06-25 stsp ".cvsignore");
3922 3143d852 2020-06-25 stsp if (err)
3923 3143d852 2020-06-25 stsp break;
3924 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3925 3143d852 2020-06-25 stsp ".gitignore");
3926 3143d852 2020-06-25 stsp if (err)
3927 3143d852 2020-06-25 stsp break;
3928 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3929 3143d852 2020-06-25 stsp if (err) {
3930 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3931 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3932 3143d852 2020-06-25 stsp break;
3933 3143d852 2020-06-25 stsp }
3934 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3935 ff56836b 2021-07-08 stsp break;
3936 b737c85a 2020-06-26 stsp free(parent_path);
3937 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3938 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3939 3143d852 2020-06-25 stsp }
3940 3143d852 2020-06-25 stsp
3941 b737c85a 2020-06-26 stsp free(parent_path);
3942 b737c85a 2020-06-26 stsp free(next_parent_path);
3943 3143d852 2020-06-25 stsp return err;
3944 abb4604f 2019-07-27 stsp }
3945 f8836425 2023-04-14 thomas
3946 f8836425 2023-04-14 thomas struct find_missing_children_args {
3947 f8836425 2023-04-14 thomas const char *parent_path;
3948 f8836425 2023-04-14 thomas size_t parent_len;
3949 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
3950 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
3951 f8836425 2023-04-14 thomas void *cancel_arg;
3952 f8836425 2023-04-14 thomas };
3953 f8836425 2023-04-14 thomas
3954 abb4604f 2019-07-27 stsp static const struct got_error *
3955 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
3956 f8836425 2023-04-14 thomas {
3957 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3958 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
3959 f8836425 2023-04-14 thomas
3960 f8836425 2023-04-14 thomas if (a->cancel_cb) {
3961 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
3962 f8836425 2023-04-14 thomas if (err)
3963 f8836425 2023-04-14 thomas return err;
3964 f8836425 2023-04-14 thomas }
3965 f8836425 2023-04-14 thomas
3966 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3967 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
3968 f8836425 2023-04-14 thomas
3969 f8836425 2023-04-14 thomas return err;
3970 f8836425 2023-04-14 thomas }
3971 f8836425 2023-04-14 thomas
3972 f8836425 2023-04-14 thomas static const struct got_error *
3973 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
3974 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
3975 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
3976 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
3977 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
3978 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3979 f8836425 2023-04-14 thomas {
3980 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3981 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
3982 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
3983 f8836425 2023-04-14 thomas
3984 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
3985 f8836425 2023-04-14 thomas if (cancel_cb) {
3986 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
3987 f8836425 2023-04-14 thomas if (err)
3988 f8836425 2023-04-14 thomas break;
3989 f8836425 2023-04-14 thomas }
3990 f8836425 2023-04-14 thomas
3991 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3992 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
3993 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
3994 f8836425 2023-04-14 thomas ondisk_path = NULL;
3995 f8836425 2023-04-14 thomas break;
3996 f8836425 2023-04-14 thomas }
3997 f8836425 2023-04-14 thomas
3998 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
3999 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
4000 f8836425 2023-04-14 thomas ignores, no_ignores);
4001 f8836425 2023-04-14 thomas if (err)
4002 f8836425 2023-04-14 thomas break;
4003 f8836425 2023-04-14 thomas
4004 f8836425 2023-04-14 thomas free(ondisk_path);
4005 f8836425 2023-04-14 thomas ondisk_path = NULL;
4006 f8836425 2023-04-14 thomas }
4007 f8836425 2023-04-14 thomas
4008 f8836425 2023-04-14 thomas free(ondisk_path);
4009 f8836425 2023-04-14 thomas return err;
4010 f8836425 2023-04-14 thomas }
4011 f8836425 2023-04-14 thomas
4012 f8836425 2023-04-14 thomas static const struct got_error *
4013 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4014 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4015 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4016 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4017 f2a9dc41 2019-12-13 tracey int report_unchanged)
4018 f8d1f275 2019-02-04 stsp {
4019 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4020 6fc93f37 2019-12-13 stsp int fd = -1;
4021 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4022 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4023 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4024 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
4025 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
4026 3143d852 2020-06-25 stsp
4027 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4028 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
4029 f8d1f275 2019-02-04 stsp
4030 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4031 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4032 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4033 8dc303cc 2019-07-27 stsp
4034 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4035 a78810f8 2022-03-13 thomas if (ie) {
4036 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
4037 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
4038 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
4039 a78810f8 2022-03-13 thomas goto done;
4040 f8836425 2023-04-14 thomas } else {
4041 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
4042 f8836425 2023-04-14 thomas fmca.parent_path = path;
4043 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
4044 f8836425 2023-04-14 thomas fmca.children = &missing_children;
4045 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
4046 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
4047 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
4048 f8836425 2023-04-14 thomas find_missing_children, &fmca);
4049 f8836425 2023-04-14 thomas if (err)
4050 f8836425 2023-04-14 thomas goto done;
4051 a78810f8 2022-03-13 thomas }
4052 a78810f8 2022-03-13 thomas
4053 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4054 6fc93f37 2019-12-13 stsp if (fd == -1) {
4055 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4056 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
4057 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4058 ff56836b 2021-07-08 stsp else {
4059 ff56836b 2021-07-08 stsp if (!no_ignores) {
4060 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4061 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4062 ff56836b 2021-07-08 stsp if (err)
4063 ff56836b 2021-07-08 stsp goto done;
4064 ff56836b 2021-07-08 stsp }
4065 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4066 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4067 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4068 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4069 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4070 f8836425 2023-04-14 thomas if (err)
4071 f8836425 2023-04-14 thomas goto done;
4072 f8836425 2023-04-14 thomas } else {
4073 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4074 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4075 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4076 f8836425 2023-04-14 thomas &ignores, no_ignores,
4077 f8836425 2023-04-14 thomas status_cb, status_arg,
4078 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4079 6f97aa83 2023-04-14 thomas if (err)
4080 6f97aa83 2023-04-14 thomas goto done;
4081 f8836425 2023-04-14 thomas }
4082 ff56836b 2021-07-08 stsp }
4083 abb4604f 2019-07-27 stsp } else {
4084 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4085 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4086 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4087 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4088 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4089 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4090 abb4604f 2019-07-27 stsp arg.status_path = path;
4091 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4092 abb4604f 2019-07-27 stsp arg.repo = repo;
4093 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4094 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4095 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4096 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4097 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4098 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4099 022fae89 2019-12-06 tracey if (!no_ignores) {
4100 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4101 3143d852 2020-06-25 stsp worktree->root_path, path);
4102 3143d852 2020-06-25 stsp if (err)
4103 3143d852 2020-06-25 stsp goto done;
4104 022fae89 2019-12-06 tracey }
4105 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4106 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4107 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4108 927df6b7 2019-02-10 stsp }
4109 3143d852 2020-06-25 stsp done:
4110 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4111 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4112 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4113 927df6b7 2019-02-10 stsp free(ondisk_path);
4114 347d1d3e 2019-07-12 stsp return err;
4115 347d1d3e 2019-07-12 stsp }
4116 347d1d3e 2019-07-12 stsp
4117 347d1d3e 2019-07-12 stsp const struct got_error *
4118 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4119 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4120 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4121 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4122 347d1d3e 2019-07-12 stsp {
4123 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4124 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4125 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4126 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4127 347d1d3e 2019-07-12 stsp
4128 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4129 347d1d3e 2019-07-12 stsp if (err)
4130 347d1d3e 2019-07-12 stsp return err;
4131 347d1d3e 2019-07-12 stsp
4132 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4133 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4134 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4135 f6343036 2021-06-22 stsp no_ignores, 0);
4136 72ea6654 2019-07-27 stsp if (err)
4137 72ea6654 2019-07-27 stsp break;
4138 72ea6654 2019-07-27 stsp }
4139 f8d1f275 2019-02-04 stsp free(fileindex_path);
4140 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4141 f8d1f275 2019-02-04 stsp return err;
4142 f8d1f275 2019-02-04 stsp }
4143 6c7ab921 2019-03-18 stsp
4144 6c7ab921 2019-03-18 stsp const struct got_error *
4145 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4146 6c7ab921 2019-03-18 stsp const char *arg)
4147 6c7ab921 2019-03-18 stsp {
4148 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4149 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4150 6c7ab921 2019-03-18 stsp size_t len;
4151 00bb5ea0 2020-07-23 stsp struct stat sb;
4152 01740607 2020-11-04 stsp char *abspath = NULL;
4153 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4154 6c7ab921 2019-03-18 stsp
4155 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4156 6c7ab921 2019-03-18 stsp
4157 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4158 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4159 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4160 00bb5ea0 2020-07-23 stsp
4161 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4162 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4163 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4164 00bb5ea0 2020-07-23 stsp goto done;
4165 00bb5ea0 2020-07-23 stsp }
4166 727173c3 2020-11-06 stsp sb.st_mode = 0;
4167 00bb5ea0 2020-07-23 stsp }
4168 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4169 00bb5ea0 2020-07-23 stsp /*
4170 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4171 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4172 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4173 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4174 00bb5ea0 2020-07-23 stsp */
4175 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4176 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4177 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4178 00bb5ea0 2020-07-23 stsp goto done;
4179 00bb5ea0 2020-07-23 stsp }
4180 00bb5ea0 2020-07-23 stsp
4181 00bb5ea0 2020-07-23 stsp }
4182 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4183 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4184 00bb5ea0 2020-07-23 stsp if (err)
4185 d0710d08 2019-07-22 stsp goto done;
4186 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4187 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4188 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4189 00bb5ea0 2020-07-23 stsp goto done;
4190 00bb5ea0 2020-07-23 stsp }
4191 00bb5ea0 2020-07-23 stsp } else {
4192 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4193 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4194 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4195 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4196 00bb5ea0 2020-07-23 stsp goto done;
4197 00bb5ea0 2020-07-23 stsp }
4198 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4199 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4200 01740607 2020-11-04 stsp goto done;
4201 01740607 2020-11-04 stsp }
4202 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4203 01740607 2020-11-04 stsp sizeof(canonpath));
4204 01740607 2020-11-04 stsp if (err)
4205 00bb5ea0 2020-07-23 stsp goto done;
4206 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4207 01740607 2020-11-04 stsp if (resolved == NULL) {
4208 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4209 01740607 2020-11-04 stsp goto done;
4210 00bb5ea0 2020-07-23 stsp }
4211 d0710d08 2019-07-22 stsp }
4212 d0710d08 2019-07-22 stsp }
4213 6c7ab921 2019-03-18 stsp
4214 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4215 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4216 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4217 6c7ab921 2019-03-18 stsp goto done;
4218 6c7ab921 2019-03-18 stsp }
4219 6c7ab921 2019-03-18 stsp
4220 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4221 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4222 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4223 f6d88e1a 2019-05-29 stsp if (err)
4224 f6d88e1a 2019-05-29 stsp goto done;
4225 f6d88e1a 2019-05-29 stsp } else {
4226 f6d88e1a 2019-05-29 stsp path = strdup("");
4227 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4228 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4229 f6d88e1a 2019-05-29 stsp goto done;
4230 f6d88e1a 2019-05-29 stsp }
4231 6c7ab921 2019-03-18 stsp }
4232 6c7ab921 2019-03-18 stsp
4233 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4234 6c7ab921 2019-03-18 stsp len = strlen(path);
4235 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4236 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4237 6c7ab921 2019-03-18 stsp len--;
4238 6c7ab921 2019-03-18 stsp }
4239 6c7ab921 2019-03-18 stsp done:
4240 01740607 2020-11-04 stsp free(abspath);
4241 6c7ab921 2019-03-18 stsp free(resolved);
4242 d0710d08 2019-07-22 stsp free(cwd);
4243 6c7ab921 2019-03-18 stsp if (err == NULL)
4244 6c7ab921 2019-03-18 stsp *wt_path = path;
4245 6c7ab921 2019-03-18 stsp else
4246 6c7ab921 2019-03-18 stsp free(path);
4247 d00136be 2019-03-26 stsp return err;
4248 d00136be 2019-03-26 stsp }
4249 d00136be 2019-03-26 stsp
4250 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4251 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4252 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4253 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4254 4e68cba3 2019-11-23 stsp void *progress_arg;
4255 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4256 4e68cba3 2019-11-23 stsp };
4257 4e68cba3 2019-11-23 stsp
4258 b21ebdb0 2023-06-22 thomas static int
4259 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4260 b21ebdb0 2023-06-22 thomas {
4261 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4262 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4263 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4264 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4265 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4266 b21ebdb0 2023-06-22 thomas }
4267 b21ebdb0 2023-06-22 thomas
4268 4e68cba3 2019-11-23 stsp static const struct got_error *
4269 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4270 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4271 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4272 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4273 07f5b47a 2019-06-02 stsp {
4274 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4275 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4276 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4277 6d022e97 2019-08-04 stsp struct stat sb;
4278 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4279 07f5b47a 2019-06-02 stsp
4280 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4281 dbb83fbd 2019-12-12 stsp relpath) == -1)
4282 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4283 dbb83fbd 2019-12-12 stsp
4284 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4285 6d022e97 2019-08-04 stsp if (ie) {
4286 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4287 12463d8b 2019-12-13 stsp de_name, a->repo);
4288 6d022e97 2019-08-04 stsp if (err)
4289 dbb83fbd 2019-12-12 stsp goto done;
4290 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4291 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4292 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4293 dbb83fbd 2019-12-12 stsp goto done;
4294 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4295 dbb83fbd 2019-12-12 stsp if (err)
4296 dbb83fbd 2019-12-12 stsp goto done;
4297 6d022e97 2019-08-04 stsp }
4298 07f5b47a 2019-06-02 stsp
4299 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4300 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4301 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4302 84bf00a6 2022-02-12 thomas else
4303 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4304 dbb83fbd 2019-12-12 stsp goto done;
4305 4e68cba3 2019-11-23 stsp }
4306 07f5b47a 2019-06-02 stsp
4307 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4308 4e68cba3 2019-11-23 stsp if (err)
4309 4e68cba3 2019-11-23 stsp goto done;
4310 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4311 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4312 3969253a 2020-03-07 stsp if (err) {
4313 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4314 3969253a 2020-03-07 stsp goto done;
4315 3969253a 2020-03-07 stsp }
4316 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4317 07f5b47a 2019-06-02 stsp if (err) {
4318 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4319 4e68cba3 2019-11-23 stsp goto done;
4320 07f5b47a 2019-06-02 stsp }
4321 4e68cba3 2019-11-23 stsp done:
4322 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4323 dbb83fbd 2019-12-12 stsp if (err)
4324 dbb83fbd 2019-12-12 stsp return err;
4325 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4326 dbb83fbd 2019-12-12 stsp return NULL;
4327 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4328 07f5b47a 2019-06-02 stsp }
4329 07f5b47a 2019-06-02 stsp
4330 d00136be 2019-03-26 stsp const struct got_error *
4331 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4332 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4333 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4334 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4335 d00136be 2019-03-26 stsp {
4336 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4337 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4338 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4339 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4340 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4341 d00136be 2019-03-26 stsp
4342 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4343 d00136be 2019-03-26 stsp if (err)
4344 d00136be 2019-03-26 stsp return err;
4345 d00136be 2019-03-26 stsp
4346 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4347 d00136be 2019-03-26 stsp if (err)
4348 d00136be 2019-03-26 stsp goto done;
4349 d00136be 2019-03-26 stsp
4350 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4351 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4352 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4353 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4354 4e68cba3 2019-11-23 stsp saa.repo = repo;
4355 4e68cba3 2019-11-23 stsp
4356 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4357 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4358 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4359 1dd54920 2019-05-11 stsp if (err)
4360 af12c6b9 2019-06-04 stsp break;
4361 1dd54920 2019-05-11 stsp }
4362 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4363 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4364 af12c6b9 2019-06-04 stsp err = sync_err;
4365 d00136be 2019-03-26 stsp done:
4366 fb399478 2019-07-12 stsp free(fileindex_path);
4367 d00136be 2019-03-26 stsp if (fileindex)
4368 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4369 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4370 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4371 d00136be 2019-03-26 stsp err = unlockerr;
4372 6c7ab921 2019-03-18 stsp return err;
4373 6c7ab921 2019-03-18 stsp }
4374 17ed4618 2019-06-02 stsp
4375 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4376 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4377 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4378 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4379 f2a9dc41 2019-12-13 tracey void *progress_arg;
4380 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4381 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4382 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4383 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4384 8ec7352a 2023-06-01 thomas const char *status_path;
4385 8ec7352a 2023-06-01 thomas size_t status_path_len;
4386 766841c2 2020-08-13 stsp const char *status_codes;
4387 f2a9dc41 2019-12-13 tracey };
4388 f2a9dc41 2019-12-13 tracey
4389 f2a9dc41 2019-12-13 tracey static const struct got_error *
4390 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4391 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4392 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4393 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4394 17ed4618 2019-06-02 stsp {
4395 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4396 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4397 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4398 17ed4618 2019-06-02 stsp struct stat sb;
4399 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4400 d64cc78a 2022-01-25 thomas
4401 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4402 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4403 d64cc78a 2022-01-25 thomas return NULL;
4404 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4405 d64cc78a 2022-01-25 thomas }
4406 17ed4618 2019-06-02 stsp
4407 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4408 17ed4618 2019-06-02 stsp if (ie == NULL)
4409 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4410 2ec1f75b 2019-03-26 stsp
4411 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4412 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4413 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4414 9acbc4fa 2019-08-03 stsp return NULL;
4415 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4416 9acbc4fa 2019-08-03 stsp }
4417 9acbc4fa 2019-08-03 stsp
4418 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4419 f2a9dc41 2019-12-13 tracey relpath) == -1)
4420 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4421 f2a9dc41 2019-12-13 tracey
4422 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4423 12463d8b 2019-12-13 stsp a->repo);
4424 17ed4618 2019-06-02 stsp if (err)
4425 f2a9dc41 2019-12-13 tracey goto done;
4426 17ed4618 2019-06-02 stsp
4427 766841c2 2020-08-13 stsp if (a->status_codes) {
4428 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4429 766841c2 2020-08-13 stsp int i;
4430 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4431 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4432 766841c2 2020-08-13 stsp break;
4433 766841c2 2020-08-13 stsp }
4434 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4435 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4436 766841c2 2020-08-13 stsp free(ondisk_path);
4437 766841c2 2020-08-13 stsp return NULL;
4438 766841c2 2020-08-13 stsp }
4439 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4440 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4441 766841c2 2020-08-13 stsp static char msg[64];
4442 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4443 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4444 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4445 766841c2 2020-08-13 stsp goto done;
4446 766841c2 2020-08-13 stsp }
4447 766841c2 2020-08-13 stsp }
4448 766841c2 2020-08-13 stsp
4449 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4450 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4451 f2a9dc41 2019-12-13 tracey goto done;
4452 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4453 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4454 f2a9dc41 2019-12-13 tracey goto done;
4455 f2a9dc41 2019-12-13 tracey }
4456 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4457 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4458 d64cc78a 2022-01-25 thomas goto done;
4459 d64cc78a 2022-01-25 thomas }
4460 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4461 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4462 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4463 f2a9dc41 2019-12-13 tracey goto done;
4464 f2a9dc41 2019-12-13 tracey }
4465 17ed4618 2019-06-02 stsp }
4466 17ed4618 2019-06-02 stsp
4467 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4468 20a2ad1c 2020-10-20 stsp size_t root_len;
4469 20a2ad1c 2020-10-20 stsp
4470 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4471 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4472 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4473 12463d8b 2019-12-13 stsp ondisk_path);
4474 12463d8b 2019-12-13 stsp goto done;
4475 12463d8b 2019-12-13 stsp }
4476 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4477 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4478 12463d8b 2019-12-13 stsp goto done;
4479 15341bfd 2020-03-05 tracey }
4480 15341bfd 2020-03-05 tracey
4481 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4482 20a2ad1c 2020-10-20 stsp do {
4483 20a2ad1c 2020-10-20 stsp char *parent;
4484 8ec7352a 2023-06-01 thomas
4485 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4486 20a2ad1c 2020-10-20 stsp if (err)
4487 2513f20a 2020-10-20 stsp goto done;
4488 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4489 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4490 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4491 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4492 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4493 8ec7352a 2023-06-01 thomas a->status_path_len))
4494 8ec7352a 2023-06-01 thomas break;
4495 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4496 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4497 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4498 20a2ad1c 2020-10-20 stsp ondisk_path);
4499 15341bfd 2020-03-05 tracey break;
4500 15341bfd 2020-03-05 tracey }
4501 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4502 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4503 f2a9dc41 2019-12-13 tracey }
4504 17ed4618 2019-06-02 stsp
4505 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4506 f2a9dc41 2019-12-13 tracey done:
4507 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4508 f2a9dc41 2019-12-13 tracey if (err)
4509 f2a9dc41 2019-12-13 tracey return err;
4510 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4511 f2a9dc41 2019-12-13 tracey return NULL;
4512 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4513 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4514 17ed4618 2019-06-02 stsp }
4515 17ed4618 2019-06-02 stsp
4516 2ec1f75b 2019-03-26 stsp const struct got_error *
4517 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4518 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4519 766841c2 2020-08-13 stsp const char *status_codes,
4520 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4521 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4522 2ec1f75b 2019-03-26 stsp {
4523 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4524 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4525 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4526 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4527 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4528 2ec1f75b 2019-03-26 stsp
4529 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4530 2ec1f75b 2019-03-26 stsp if (err)
4531 2ec1f75b 2019-03-26 stsp return err;
4532 2ec1f75b 2019-03-26 stsp
4533 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4534 2ec1f75b 2019-03-26 stsp if (err)
4535 2ec1f75b 2019-03-26 stsp goto done;
4536 f2a9dc41 2019-12-13 tracey
4537 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4538 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4539 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4540 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4541 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4542 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4543 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4544 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4545 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4546 2ec1f75b 2019-03-26 stsp
4547 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4548 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4549 8ec7352a 2023-06-01 thomas
4550 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4551 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4552 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4553 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4554 8ec7352a 2023-06-01 thomas goto done;
4555 8ec7352a 2023-06-01 thomas }
4556 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4557 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4558 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4559 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4560 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4561 17ed4618 2019-06-02 stsp if (err)
4562 af12c6b9 2019-06-04 stsp break;
4563 2ec1f75b 2019-03-26 stsp }
4564 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4565 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4566 af12c6b9 2019-06-04 stsp err = sync_err;
4567 2ec1f75b 2019-03-26 stsp done:
4568 fb399478 2019-07-12 stsp free(fileindex_path);
4569 2ec1f75b 2019-03-26 stsp if (fileindex)
4570 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4571 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4572 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4573 2ec1f75b 2019-03-26 stsp err = unlockerr;
4574 33aa809d 2019-08-08 stsp return err;
4575 33aa809d 2019-08-08 stsp }
4576 33aa809d 2019-08-08 stsp
4577 33aa809d 2019-08-08 stsp static const struct got_error *
4578 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4579 33aa809d 2019-08-08 stsp {
4580 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4581 33aa809d 2019-08-08 stsp char *line = NULL;
4582 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4583 33aa809d 2019-08-08 stsp ssize_t linelen;
4584 33aa809d 2019-08-08 stsp
4585 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4586 33aa809d 2019-08-08 stsp if (linelen == -1) {
4587 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4588 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4589 33aa809d 2019-08-08 stsp goto done;
4590 33aa809d 2019-08-08 stsp }
4591 33aa809d 2019-08-08 stsp return NULL;
4592 33aa809d 2019-08-08 stsp }
4593 33aa809d 2019-08-08 stsp if (outfile) {
4594 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4595 33aa809d 2019-08-08 stsp if (n != linelen) {
4596 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4597 33aa809d 2019-08-08 stsp goto done;
4598 33aa809d 2019-08-08 stsp }
4599 33aa809d 2019-08-08 stsp }
4600 33aa809d 2019-08-08 stsp if (rejectfile) {
4601 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4602 33aa809d 2019-08-08 stsp if (n != linelen)
4603 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4604 33aa809d 2019-08-08 stsp }
4605 33aa809d 2019-08-08 stsp done:
4606 33aa809d 2019-08-08 stsp free(line);
4607 2ec1f75b 2019-03-26 stsp return err;
4608 2ec1f75b 2019-03-26 stsp }
4609 1f1abb7e 2019-08-08 stsp
4610 33aa809d 2019-08-08 stsp static const struct got_error *
4611 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4612 33aa809d 2019-08-08 stsp {
4613 33aa809d 2019-08-08 stsp char *line = NULL;
4614 33aa809d 2019-08-08 stsp size_t linesize = 0;
4615 33aa809d 2019-08-08 stsp ssize_t linelen;
4616 33aa809d 2019-08-08 stsp
4617 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4618 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4619 500467ff 2019-09-25 hiltjo if (ferror(f))
4620 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4621 500467ff 2019-09-25 hiltjo return NULL;
4622 500467ff 2019-09-25 hiltjo }
4623 33aa809d 2019-08-08 stsp free(line);
4624 33aa809d 2019-08-08 stsp return NULL;
4625 33aa809d 2019-08-08 stsp }
4626 33aa809d 2019-08-08 stsp
4627 33aa809d 2019-08-08 stsp static const struct got_error *
4628 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4629 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4630 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4631 abc59930 2021-09-05 naddy {
4632 33aa809d 2019-08-08 stsp const struct got_error *err;
4633 33aa809d 2019-08-08 stsp
4634 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4635 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4636 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4637 33aa809d 2019-08-08 stsp if (err)
4638 33aa809d 2019-08-08 stsp return err;
4639 33aa809d 2019-08-08 stsp (*line_cur1)++;
4640 33aa809d 2019-08-08 stsp }
4641 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4642 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4643 33aa809d 2019-08-08 stsp if (rejectfile)
4644 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4645 33aa809d 2019-08-08 stsp else
4646 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4647 33aa809d 2019-08-08 stsp if (err)
4648 33aa809d 2019-08-08 stsp return err;
4649 33aa809d 2019-08-08 stsp (*line_cur2)++;
4650 33aa809d 2019-08-08 stsp }
4651 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4652 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4653 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4654 33aa809d 2019-08-08 stsp if (err)
4655 33aa809d 2019-08-08 stsp return err;
4656 33aa809d 2019-08-08 stsp (*line_cur2)++;
4657 33aa809d 2019-08-08 stsp }
4658 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4659 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4660 33aa809d 2019-08-08 stsp if (rejectfile)
4661 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4662 33aa809d 2019-08-08 stsp else
4663 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4664 33aa809d 2019-08-08 stsp if (err)
4665 33aa809d 2019-08-08 stsp return err;
4666 33aa809d 2019-08-08 stsp (*line_cur1)++;
4667 33aa809d 2019-08-08 stsp }
4668 f1e81a05 2019-08-10 stsp
4669 f1e81a05 2019-08-10 stsp return NULL;
4670 f1e81a05 2019-08-10 stsp }
4671 f1e81a05 2019-08-10 stsp
4672 f1e81a05 2019-08-10 stsp static const struct got_error *
4673 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4674 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4675 f1e81a05 2019-08-10 stsp {
4676 f1e81a05 2019-08-10 stsp const struct got_error *err;
4677 f1e81a05 2019-08-10 stsp
4678 f1e81a05 2019-08-10 stsp if (outfile) {
4679 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4680 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4681 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4682 f1e81a05 2019-08-10 stsp if (err)
4683 f1e81a05 2019-08-10 stsp return err;
4684 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4685 f1e81a05 2019-08-10 stsp }
4686 f1e81a05 2019-08-10 stsp }
4687 f1e81a05 2019-08-10 stsp if (rejectfile) {
4688 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4689 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4690 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4691 f1e81a05 2019-08-10 stsp if (err)
4692 f1e81a05 2019-08-10 stsp return err;
4693 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4694 f1e81a05 2019-08-10 stsp }
4695 33aa809d 2019-08-08 stsp }
4696 33aa809d 2019-08-08 stsp
4697 33aa809d 2019-08-08 stsp return NULL;
4698 33aa809d 2019-08-08 stsp }
4699 33aa809d 2019-08-08 stsp
4700 33aa809d 2019-08-08 stsp static const struct got_error *
4701 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4702 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4703 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4704 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4705 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4706 33aa809d 2019-08-08 stsp {
4707 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4708 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4709 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4710 33aa809d 2019-08-08 stsp FILE *hunkfile;
4711 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4712 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4713 fe621944 2020-11-10 stsp int rc;
4714 33aa809d 2019-08-08 stsp
4715 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4716 33aa809d 2019-08-08 stsp
4717 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4718 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4719 fe621944 2020-11-10 stsp start_old = cc.left.start;
4720 fe621944 2020-11-10 stsp end_old = cc.left.end;
4721 fe621944 2020-11-10 stsp start_new = cc.right.start;
4722 fe621944 2020-11-10 stsp end_new = cc.right.end;
4723 33aa809d 2019-08-08 stsp
4724 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4725 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4726 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4727 33aa809d 2019-08-08 stsp
4728 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4729 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4730 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4731 33aa809d 2019-08-08 stsp
4732 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4733 fe621944 2020-11-10 stsp if (diff_state == NULL)
4734 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4735 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4736 fe621944 2020-11-10 stsp
4737 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4738 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4739 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4740 33aa809d 2019-08-08 stsp goto done;
4741 33aa809d 2019-08-08 stsp }
4742 fe621944 2020-11-10 stsp
4743 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4744 fe621944 2020-11-10 stsp diff_result, &cc);
4745 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4746 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4747 33aa809d 2019-08-08 stsp goto done;
4748 33aa809d 2019-08-08 stsp }
4749 fe621944 2020-11-10 stsp
4750 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4751 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4752 33aa809d 2019-08-08 stsp goto done;
4753 33aa809d 2019-08-08 stsp }
4754 33aa809d 2019-08-08 stsp
4755 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4756 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4757 33aa809d 2019-08-08 stsp if (err)
4758 33aa809d 2019-08-08 stsp goto done;
4759 33aa809d 2019-08-08 stsp
4760 33aa809d 2019-08-08 stsp switch (*choice) {
4761 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4762 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4763 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4764 33aa809d 2019-08-08 stsp break;
4765 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4766 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4767 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4768 33aa809d 2019-08-08 stsp break;
4769 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4770 33aa809d 2019-08-08 stsp break;
4771 33aa809d 2019-08-08 stsp default:
4772 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4773 33aa809d 2019-08-08 stsp break;
4774 33aa809d 2019-08-08 stsp }
4775 33aa809d 2019-08-08 stsp done:
4776 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4777 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4778 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4779 33aa809d 2019-08-08 stsp return err;
4780 33aa809d 2019-08-08 stsp }
4781 33aa809d 2019-08-08 stsp
4782 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4783 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4784 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4785 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4786 1f1abb7e 2019-08-08 stsp void *progress_arg;
4787 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4788 33aa809d 2019-08-08 stsp void *patch_arg;
4789 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4790 10604dce 2021-09-24 thomas int unlink_added_files;
4791 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4792 1f1abb7e 2019-08-08 stsp };
4793 a129376b 2019-03-28 stsp
4794 e20a8b6f 2019-06-04 stsp static const struct got_error *
4795 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4796 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4797 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4798 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4799 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4800 33aa809d 2019-08-08 stsp {
4801 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4802 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4803 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4804 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4805 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4806 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4807 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4808 fe621944 2020-11-10 stsp struct stat sb2;
4809 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4810 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4811 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4812 33aa809d 2019-08-08 stsp
4813 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4814 33aa809d 2019-08-08 stsp
4815 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4816 33aa809d 2019-08-08 stsp if (err)
4817 33aa809d 2019-08-08 stsp return err;
4818 33aa809d 2019-08-08 stsp
4819 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4820 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4821 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4822 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4823 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4824 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4825 fa3cef63 2020-07-23 stsp goto done;
4826 fa3cef63 2020-07-23 stsp }
4827 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4828 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4829 48b4f239 2021-12-31 thomas if (link_len == -1) {
4830 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4831 48b4f239 2021-12-31 thomas path2);
4832 48b4f239 2021-12-31 thomas }
4833 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4834 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4835 12463d8b 2019-12-13 stsp }
4836 12463d8b 2019-12-13 stsp } else {
4837 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4838 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4839 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4840 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4841 fa3cef63 2020-07-23 stsp goto done;
4842 fa3cef63 2020-07-23 stsp }
4843 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4844 fa3cef63 2020-07-23 stsp sizeof(link_target));
4845 fa3cef63 2020-07-23 stsp if (link_len == -1)
4846 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4847 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4848 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4849 12463d8b 2019-12-13 stsp }
4850 1ebedb77 2019-10-19 stsp }
4851 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4852 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4853 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4854 fa3cef63 2020-07-23 stsp goto done;
4855 fa3cef63 2020-07-23 stsp }
4856 1ebedb77 2019-10-19 stsp
4857 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4858 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4859 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4860 fa3cef63 2020-07-23 stsp goto done;
4861 fa3cef63 2020-07-23 stsp }
4862 fa3cef63 2020-07-23 stsp fd2 = -1;
4863 fa3cef63 2020-07-23 stsp } else {
4864 fa3cef63 2020-07-23 stsp size_t n;
4865 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4866 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4867 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4868 fa3cef63 2020-07-23 stsp goto done;
4869 fa3cef63 2020-07-23 stsp }
4870 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4871 fa3cef63 2020-07-23 stsp if (n != link_len) {
4872 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4873 fa3cef63 2020-07-23 stsp goto done;
4874 fa3cef63 2020-07-23 stsp }
4875 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4876 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4877 fa3cef63 2020-07-23 stsp goto done;
4878 fa3cef63 2020-07-23 stsp }
4879 fa3cef63 2020-07-23 stsp rewind(f2);
4880 33aa809d 2019-08-08 stsp }
4881 33aa809d 2019-08-08 stsp
4882 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4883 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4884 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4885 f4ae6ddb 2022-07-01 thomas goto done;
4886 f4ae6ddb 2022-07-01 thomas }
4887 f4ae6ddb 2022-07-01 thomas
4888 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4889 33aa809d 2019-08-08 stsp if (err)
4890 33aa809d 2019-08-08 stsp goto done;
4891 33aa809d 2019-08-08 stsp
4892 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4893 33aa809d 2019-08-08 stsp if (err)
4894 33aa809d 2019-08-08 stsp goto done;
4895 33aa809d 2019-08-08 stsp
4896 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4897 33aa809d 2019-08-08 stsp if (err)
4898 33aa809d 2019-08-08 stsp goto done;
4899 33aa809d 2019-08-08 stsp
4900 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4901 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4902 33aa809d 2019-08-08 stsp if (err)
4903 33aa809d 2019-08-08 stsp goto done;
4904 33aa809d 2019-08-08 stsp
4905 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4906 fc2a50f2 2022-11-01 thomas "");
4907 33aa809d 2019-08-08 stsp if (err)
4908 33aa809d 2019-08-08 stsp goto done;
4909 33aa809d 2019-08-08 stsp
4910 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4911 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4912 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4913 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4914 fe621944 2020-11-10 stsp
4915 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4916 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4917 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4918 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4919 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4920 fe621944 2020-11-10 stsp nchanges++;
4921 fe621944 2020-11-10 stsp }
4922 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4923 33aa809d 2019-08-08 stsp int choice;
4924 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4925 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4926 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4927 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4928 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4929 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4930 33aa809d 2019-08-08 stsp if (err)
4931 33aa809d 2019-08-08 stsp goto done;
4932 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4933 33aa809d 2019-08-08 stsp have_content = 1;
4934 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4935 33aa809d 2019-08-08 stsp break;
4936 33aa809d 2019-08-08 stsp }
4937 1ebedb77 2019-10-19 stsp if (have_content) {
4938 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4939 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4940 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4941 1ebedb77 2019-10-19 stsp if (err)
4942 1ebedb77 2019-10-19 stsp goto done;
4943 1ebedb77 2019-10-19 stsp
4944 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4945 a2c162eb 2022-10-30 thomas mode_t mode;
4946 a2c162eb 2022-10-30 thomas
4947 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4948 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4949 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4950 fa3cef63 2020-07-23 stsp goto done;
4951 fa3cef63 2020-07-23 stsp }
4952 1ebedb77 2019-10-19 stsp }
4953 1ebedb77 2019-10-19 stsp }
4954 33aa809d 2019-08-08 stsp done:
4955 33aa809d 2019-08-08 stsp free(id_str);
4956 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4957 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4958 33aa809d 2019-08-08 stsp if (blob)
4959 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4960 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4961 fe621944 2020-11-10 stsp if (err == NULL)
4962 fe621944 2020-11-10 stsp err = free_err;
4963 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4964 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4965 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4966 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4967 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4968 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4969 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4970 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4971 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4972 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4973 33aa809d 2019-08-08 stsp if (err || !have_content) {
4974 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4975 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4976 33aa809d 2019-08-08 stsp free(*path_outfile);
4977 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4978 33aa809d 2019-08-08 stsp }
4979 33aa809d 2019-08-08 stsp free(path1);
4980 33aa809d 2019-08-08 stsp return err;
4981 33aa809d 2019-08-08 stsp }
4982 33aa809d 2019-08-08 stsp
4983 33aa809d 2019-08-08 stsp static const struct got_error *
4984 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4985 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4986 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4987 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4988 a129376b 2019-03-28 stsp {
4989 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4990 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4991 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4992 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4993 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4994 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4995 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4996 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4997 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4998 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4999 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5000 f4ae6ddb 2022-07-01 thomas int fd = -1;
5001 a129376b 2019-03-28 stsp
5002 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5003 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5004 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5005 d3bcc3d1 2019-08-08 stsp return NULL;
5006 3d69ad8d 2019-08-17 semarie
5007 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5008 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5009 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5010 d3bcc3d1 2019-08-08 stsp
5011 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5012 65084dad 2019-08-08 stsp if (ie == NULL)
5013 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5014 a129376b 2019-03-28 stsp
5015 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5016 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5017 a129376b 2019-03-28 stsp if (err) {
5018 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5019 a129376b 2019-03-28 stsp goto done;
5020 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5021 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5022 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5023 e20a8b6f 2019-06-04 stsp goto done;
5024 e20a8b6f 2019-06-04 stsp }
5025 a129376b 2019-03-28 stsp }
5026 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5027 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5028 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5029 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5030 a129376b 2019-03-28 stsp goto done;
5031 a129376b 2019-03-28 stsp }
5032 a129376b 2019-03-28 stsp } else {
5033 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5034 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5035 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5036 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5037 a129376b 2019-03-28 stsp goto done;
5038 a129376b 2019-03-28 stsp }
5039 a129376b 2019-03-28 stsp } else {
5040 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5041 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5042 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5043 a129376b 2019-03-28 stsp goto done;
5044 a129376b 2019-03-28 stsp }
5045 a129376b 2019-03-28 stsp }
5046 a129376b 2019-03-28 stsp }
5047 a129376b 2019-03-28 stsp
5048 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
5049 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
5050 945f9229 2022-04-16 thomas if (err)
5051 945f9229 2022-04-16 thomas goto done;
5052 945f9229 2022-04-16 thomas
5053 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5054 a9fa2909 2019-07-27 stsp if (err) {
5055 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5056 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5057 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5058 a9fa2909 2019-07-27 stsp goto done;
5059 a9fa2909 2019-07-27 stsp } else {
5060 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5061 a9fa2909 2019-07-27 stsp if (err)
5062 a9fa2909 2019-07-27 stsp goto done;
5063 a9fa2909 2019-07-27 stsp
5064 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5065 1233e6b6 2020-10-19 stsp if (err)
5066 a9fa2909 2019-07-27 stsp goto done;
5067 a9fa2909 2019-07-27 stsp
5068 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5069 1233e6b6 2020-10-19 stsp free(te_name);
5070 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5071 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5072 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5073 a9fa2909 2019-07-27 stsp goto done;
5074 a9fa2909 2019-07-27 stsp }
5075 a129376b 2019-03-28 stsp }
5076 a129376b 2019-03-28 stsp
5077 a129376b 2019-03-28 stsp switch (status) {
5078 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5079 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5080 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5081 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5082 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5083 33aa809d 2019-08-08 stsp if (err)
5084 33aa809d 2019-08-08 stsp goto done;
5085 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5086 33aa809d 2019-08-08 stsp break;
5087 33aa809d 2019-08-08 stsp }
5088 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5089 1f1abb7e 2019-08-08 stsp ie->path);
5090 1ee397ad 2019-07-12 stsp if (err)
5091 1ee397ad 2019-07-12 stsp goto done;
5092 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5093 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5094 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5095 8641a332 2023-04-14 thomas
5096 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5097 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5098 8641a332 2023-04-14 thomas
5099 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5100 8641a332 2023-04-14 thomas entry) {
5101 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5102 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5103 8641a332 2023-04-14 thomas continue;
5104 8641a332 2023-04-14 thomas do_unlink = 1;
5105 8641a332 2023-04-14 thomas break;
5106 8641a332 2023-04-14 thomas }
5107 10604dce 2021-09-24 thomas }
5108 8641a332 2023-04-14 thomas
5109 8641a332 2023-04-14 thomas if (do_unlink) {
5110 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5111 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5112 8641a332 2023-04-14 thomas relpath) == -1) {
5113 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5114 8641a332 2023-04-14 thomas goto done;
5115 8641a332 2023-04-14 thomas }
5116 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5117 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5118 8641a332 2023-04-14 thomas ondisk_path);
5119 8641a332 2023-04-14 thomas break;
5120 8641a332 2023-04-14 thomas }
5121 10604dce 2021-09-24 thomas }
5122 10604dce 2021-09-24 thomas }
5123 a129376b 2019-03-28 stsp break;
5124 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5125 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5126 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5127 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5128 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5129 33aa809d 2019-08-08 stsp if (err)
5130 33aa809d 2019-08-08 stsp goto done;
5131 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5132 33aa809d 2019-08-08 stsp break;
5133 33aa809d 2019-08-08 stsp }
5134 33aa809d 2019-08-08 stsp /* fall through */
5135 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5136 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5137 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5138 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5139 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5140 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5141 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5142 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5143 43010591 2023-02-17 thomas else
5144 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5145 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5146 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5147 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5148 f4ae6ddb 2022-07-01 thomas goto done;
5149 f4ae6ddb 2022-07-01 thomas }
5150 f4ae6ddb 2022-07-01 thomas
5151 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5152 a129376b 2019-03-28 stsp if (err)
5153 65084dad 2019-08-08 stsp goto done;
5154 65084dad 2019-08-08 stsp
5155 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5156 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5157 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5158 a129376b 2019-03-28 stsp goto done;
5159 65084dad 2019-08-08 stsp }
5160 65084dad 2019-08-08 stsp
5161 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5162 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5163 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5164 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5165 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5166 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5167 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5168 33aa809d 2019-08-08 stsp break;
5169 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5170 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5171 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5172 369fd7e5 2020-07-23 stsp path_content);
5173 369fd7e5 2020-07-23 stsp break;
5174 369fd7e5 2020-07-23 stsp }
5175 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5176 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5177 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5178 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5179 369fd7e5 2020-07-23 stsp } else {
5180 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5181 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5182 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5183 369fd7e5 2020-07-23 stsp goto done;
5184 369fd7e5 2020-07-23 stsp }
5185 33aa809d 2019-08-08 stsp }
5186 33aa809d 2019-08-08 stsp } else {
5187 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5188 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5189 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5190 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5191 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5192 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5193 2e1fa222 2020-07-23 stsp } else {
5194 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5195 2e1fa222 2020-07-23 stsp ie->path,
5196 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5197 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5198 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5199 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5200 2e1fa222 2020-07-23 stsp }
5201 a129376b 2019-03-28 stsp if (err)
5202 a129376b 2019-03-28 stsp goto done;
5203 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5204 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5205 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5206 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5207 437adc9d 2020-12-10 yzhong blob->id.sha1,
5208 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5209 2e1fa222 2020-07-23 stsp if (err)
5210 2e1fa222 2020-07-23 stsp goto done;
5211 2e1fa222 2020-07-23 stsp }
5212 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5213 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5214 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5215 33aa809d 2019-08-08 stsp }
5216 a129376b 2019-03-28 stsp }
5217 a129376b 2019-03-28 stsp break;
5218 e20a8b6f 2019-06-04 stsp }
5219 a129376b 2019-03-28 stsp default:
5220 1f1abb7e 2019-08-08 stsp break;
5221 a129376b 2019-03-28 stsp }
5222 a129376b 2019-03-28 stsp done:
5223 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5224 33aa809d 2019-08-08 stsp free(path_content);
5225 e20a8b6f 2019-06-04 stsp free(parent_path);
5226 a129376b 2019-03-28 stsp free(tree_path);
5227 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5228 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5229 a129376b 2019-03-28 stsp if (blob)
5230 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5231 a129376b 2019-03-28 stsp if (tree)
5232 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5233 a129376b 2019-03-28 stsp free(tree_id);
5234 945f9229 2022-04-16 thomas if (base_commit)
5235 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5236 e20a8b6f 2019-06-04 stsp return err;
5237 e20a8b6f 2019-06-04 stsp }
5238 e20a8b6f 2019-06-04 stsp
5239 e20a8b6f 2019-06-04 stsp const struct got_error *
5240 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5241 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5242 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5243 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5244 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5245 e20a8b6f 2019-06-04 stsp {
5246 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5247 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5248 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5249 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5250 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5251 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5252 e20a8b6f 2019-06-04 stsp
5253 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5254 e20a8b6f 2019-06-04 stsp if (err)
5255 e20a8b6f 2019-06-04 stsp return err;
5256 e20a8b6f 2019-06-04 stsp
5257 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5258 e20a8b6f 2019-06-04 stsp if (err)
5259 e20a8b6f 2019-06-04 stsp goto done;
5260 e20a8b6f 2019-06-04 stsp
5261 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5262 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5263 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5264 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5265 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5266 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5267 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5268 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5269 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5270 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5271 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5272 e20a8b6f 2019-06-04 stsp if (err)
5273 af12c6b9 2019-06-04 stsp break;
5274 e20a8b6f 2019-06-04 stsp }
5275 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5276 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5277 e20a8b6f 2019-06-04 stsp err = sync_err;
5278 af12c6b9 2019-06-04 stsp done:
5279 fb399478 2019-07-12 stsp free(fileindex_path);
5280 a129376b 2019-03-28 stsp if (fileindex)
5281 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5282 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5283 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5284 a129376b 2019-03-28 stsp err = unlockerr;
5285 c4296144 2019-05-09 stsp return err;
5286 c4296144 2019-05-09 stsp }
5287 c4296144 2019-05-09 stsp
5288 cf066bf8 2019-05-09 stsp static void
5289 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5290 cf066bf8 2019-05-09 stsp {
5291 24519714 2019-05-09 stsp free(ct->path);
5292 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5293 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5294 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5295 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5296 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5297 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5298 cf066bf8 2019-05-09 stsp free(ct);
5299 cf066bf8 2019-05-09 stsp }
5300 24519714 2019-05-09 stsp
5301 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5302 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5303 24519714 2019-05-09 stsp struct got_repository *repo;
5304 24519714 2019-05-09 stsp struct got_worktree *worktree;
5305 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5306 5f8a88c6 2019-08-03 stsp int have_staged_files;
5307 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5308 ef899790 2022-11-01 thomas int diff_header_shown;
5309 be94c032 2023-02-20 thomas int commit_conflicts;
5310 ef899790 2022-11-01 thomas FILE *diff_outfile;
5311 ef899790 2022-11-01 thomas FILE *f1;
5312 ef899790 2022-11-01 thomas FILE *f2;
5313 24519714 2019-05-09 stsp };
5314 ef899790 2022-11-01 thomas
5315 ef899790 2022-11-01 thomas /*
5316 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5317 ef899790 2022-11-01 thomas * it as content to the diff engine.
5318 ef899790 2022-11-01 thomas */
5319 ef899790 2022-11-01 thomas static const struct got_error *
5320 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5321 ef899790 2022-11-01 thomas const char *abspath)
5322 ef899790 2022-11-01 thomas {
5323 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5324 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5325 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5326 ef899790 2022-11-01 thomas
5327 ef899790 2022-11-01 thomas *fd = -1;
5328 ef899790 2022-11-01 thomas
5329 ef899790 2022-11-01 thomas if (dirfd != -1) {
5330 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5331 ef899790 2022-11-01 thomas if (target_len == -1)
5332 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5333 ef899790 2022-11-01 thomas } else {
5334 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5335 ef899790 2022-11-01 thomas if (target_len == -1)
5336 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5337 ef899790 2022-11-01 thomas }
5338 ef899790 2022-11-01 thomas
5339 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5340 ef899790 2022-11-01 thomas if (*fd == -1)
5341 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5342 ef899790 2022-11-01 thomas
5343 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5344 ef899790 2022-11-01 thomas if (outlen == -1) {
5345 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5346 ef899790 2022-11-01 thomas goto done;
5347 ef899790 2022-11-01 thomas }
5348 ef899790 2022-11-01 thomas
5349 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5350 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5351 ef899790 2022-11-01 thomas goto done;
5352 ef899790 2022-11-01 thomas }
5353 ef899790 2022-11-01 thomas done:
5354 ef899790 2022-11-01 thomas if (err) {
5355 ef899790 2022-11-01 thomas close(*fd);
5356 ef899790 2022-11-01 thomas *fd = -1;
5357 ef899790 2022-11-01 thomas }
5358 ef899790 2022-11-01 thomas return err;
5359 ef899790 2022-11-01 thomas }
5360 ef899790 2022-11-01 thomas
5361 ef899790 2022-11-01 thomas static const struct got_error *
5362 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5363 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5364 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5365 ef899790 2022-11-01 thomas {
5366 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5367 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5368 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5369 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5370 ef899790 2022-11-01 thomas char *label1 = NULL;
5371 ef899790 2022-11-01 thomas struct stat sb;
5372 ef899790 2022-11-01 thomas off_t size1 = 0;
5373 ef899790 2022-11-01 thomas int f2_exists = 0;
5374 ef899790 2022-11-01 thomas char *id_str = NULL;
5375 ef899790 2022-11-01 thomas
5376 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5377 d259e491 2022-11-01 thomas
5378 d259e491 2022-11-01 thomas if (diff_staged) {
5379 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5380 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5381 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5382 d259e491 2022-11-01 thomas return NULL;
5383 d259e491 2022-11-01 thomas } else {
5384 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5385 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5386 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5387 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5388 d259e491 2022-11-01 thomas return NULL;
5389 d259e491 2022-11-01 thomas }
5390 ef899790 2022-11-01 thomas
5391 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5392 ef899790 2022-11-01 thomas if (err)
5393 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5394 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5395 ef899790 2022-11-01 thomas if (err)
5396 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5397 ef899790 2022-11-01 thomas
5398 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5399 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5400 ef899790 2022-11-01 thomas if (err)
5401 ef899790 2022-11-01 thomas return err;
5402 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5403 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5404 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5405 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5406 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5407 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5408 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5409 ef899790 2022-11-01 thomas }
5410 ef899790 2022-11-01 thomas
5411 ef899790 2022-11-01 thomas if (diff_staged) {
5412 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5413 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5414 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5415 ef899790 2022-11-01 thomas label1 = ct->path;
5416 ef899790 2022-11-01 thomas label2 = ct->path;
5417 ef899790 2022-11-01 thomas break;
5418 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5419 ef899790 2022-11-01 thomas label2 = ct->path;
5420 ef899790 2022-11-01 thomas break;
5421 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5422 ef899790 2022-11-01 thomas label1 = ct->path;
5423 ef899790 2022-11-01 thomas break;
5424 ef899790 2022-11-01 thomas default:
5425 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5426 ef899790 2022-11-01 thomas }
5427 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5428 ef899790 2022-11-01 thomas if (fd1 == -1) {
5429 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5430 ef899790 2022-11-01 thomas goto done;
5431 ef899790 2022-11-01 thomas }
5432 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5433 ef899790 2022-11-01 thomas if (fd2 == -1) {
5434 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5435 ef899790 2022-11-01 thomas goto done;
5436 ef899790 2022-11-01 thomas }
5437 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5438 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5439 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5440 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5441 ef899790 2022-11-01 thomas goto done;
5442 ef899790 2022-11-01 thomas }
5443 ef899790 2022-11-01 thomas
5444 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5445 ef899790 2022-11-01 thomas if (fd1 == -1) {
5446 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5447 ef899790 2022-11-01 thomas goto done;
5448 ef899790 2022-11-01 thomas }
5449 ef899790 2022-11-01 thomas
5450 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5451 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5452 ef899790 2022-11-01 thomas 8192, fd1);
5453 ef899790 2022-11-01 thomas if (err)
5454 ef899790 2022-11-01 thomas goto done;
5455 ef899790 2022-11-01 thomas }
5456 ef899790 2022-11-01 thomas
5457 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5458 ef899790 2022-11-01 thomas if (dirfd != -1) {
5459 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5460 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5461 ef899790 2022-11-01 thomas if (fd == -1) {
5462 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5463 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5464 ef899790 2022-11-01 thomas ct->ondisk_path);
5465 ef899790 2022-11-01 thomas goto done;
5466 ef899790 2022-11-01 thomas }
5467 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5468 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5469 ef899790 2022-11-01 thomas if (err)
5470 ef899790 2022-11-01 thomas goto done;
5471 ef899790 2022-11-01 thomas }
5472 ef899790 2022-11-01 thomas } else {
5473 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5474 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5475 ef899790 2022-11-01 thomas if (fd == -1) {
5476 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5477 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5478 ef899790 2022-11-01 thomas ct->ondisk_path);
5479 ef899790 2022-11-01 thomas goto done;
5480 ef899790 2022-11-01 thomas }
5481 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5482 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5483 ef899790 2022-11-01 thomas if (err)
5484 ef899790 2022-11-01 thomas goto done;
5485 ef899790 2022-11-01 thomas }
5486 ef899790 2022-11-01 thomas }
5487 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5488 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5489 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5490 ef899790 2022-11-01 thomas goto done;
5491 ef899790 2022-11-01 thomas }
5492 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5493 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5494 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5495 ef899790 2022-11-01 thomas goto done;
5496 ef899790 2022-11-01 thomas }
5497 ef899790 2022-11-01 thomas fd = -1;
5498 ef899790 2022-11-01 thomas f2_exists = 1;
5499 ef899790 2022-11-01 thomas }
5500 ef899790 2022-11-01 thomas
5501 ef899790 2022-11-01 thomas if (blob1) {
5502 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5503 ef899790 2022-11-01 thomas f1, blob1);
5504 ef899790 2022-11-01 thomas if (err)
5505 ef899790 2022-11-01 thomas goto done;
5506 ef899790 2022-11-01 thomas }
5507 ef899790 2022-11-01 thomas
5508 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5509 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5510 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5511 ef899790 2022-11-01 thomas done:
5512 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5513 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5514 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5515 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5516 ef899790 2022-11-01 thomas if (blob1)
5517 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5518 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5519 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5520 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5521 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5522 ef899790 2022-11-01 thomas return err;
5523 ef899790 2022-11-01 thomas }
5524 cf066bf8 2019-05-09 stsp
5525 c4296144 2019-05-09 stsp static const struct got_error *
5526 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5527 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5528 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5529 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5530 c4296144 2019-05-09 stsp {
5531 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5532 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5533 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5534 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5535 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5536 768aea60 2019-05-09 stsp struct stat sb;
5537 c4296144 2019-05-09 stsp
5538 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5539 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5540 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5541 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5542 5f8a88c6 2019-08-03 stsp return NULL;
5543 5f8a88c6 2019-08-03 stsp } else {
5544 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5545 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5546 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5547 be94c032 2023-02-20 thomas }
5548 c4296144 2019-05-09 stsp
5549 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5550 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5551 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5552 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5553 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5554 5f8a88c6 2019-08-03 stsp return NULL;
5555 5f8a88c6 2019-08-03 stsp }
5556 0b5cc0d6 2019-05-09 stsp
5557 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5558 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5559 036813ee 2019-05-09 stsp goto done;
5560 036813ee 2019-05-09 stsp }
5561 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5562 036813ee 2019-05-09 stsp parent_path = strdup("");
5563 69960a46 2019-05-09 stsp if (parent_path == NULL)
5564 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5565 69960a46 2019-05-09 stsp } else {
5566 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5567 69960a46 2019-05-09 stsp if (err)
5568 69960a46 2019-05-09 stsp return err;
5569 69960a46 2019-05-09 stsp }
5570 c4296144 2019-05-09 stsp
5571 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5572 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5573 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5574 c4296144 2019-05-09 stsp goto done;
5575 768aea60 2019-05-09 stsp }
5576 768aea60 2019-05-09 stsp
5577 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5578 768aea60 2019-05-09 stsp relpath) == -1) {
5579 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5580 768aea60 2019-05-09 stsp goto done;
5581 768aea60 2019-05-09 stsp }
5582 0aeb8099 2020-07-23 stsp
5583 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5584 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5585 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5586 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5587 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5588 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5589 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5590 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5591 0aeb8099 2020-07-23 stsp break;
5592 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5593 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5594 0aeb8099 2020-07-23 stsp break;
5595 0aeb8099 2020-07-23 stsp default:
5596 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5597 0aeb8099 2020-07-23 stsp goto done;
5598 0aeb8099 2020-07-23 stsp }
5599 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5600 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5601 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5602 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5603 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5604 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5605 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5606 12463d8b 2019-12-13 stsp ct->ondisk_path);
5607 12463d8b 2019-12-13 stsp goto done;
5608 12463d8b 2019-12-13 stsp }
5609 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5610 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5611 768aea60 2019-05-09 stsp goto done;
5612 768aea60 2019-05-09 stsp }
5613 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5614 c4296144 2019-05-09 stsp }
5615 c4296144 2019-05-09 stsp
5616 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5617 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5618 44d03001 2019-05-09 stsp relpath) == -1) {
5619 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5620 44d03001 2019-05-09 stsp goto done;
5621 44d03001 2019-05-09 stsp }
5622 44d03001 2019-05-09 stsp
5623 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5624 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5625 35213c7c 2020-07-23 stsp int is_bad_symlink;
5626 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5627 35213c7c 2020-07-23 stsp ssize_t target_len;
5628 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5629 35213c7c 2020-07-23 stsp sizeof(target_path));
5630 35213c7c 2020-07-23 stsp if (target_len == -1) {
5631 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5632 35213c7c 2020-07-23 stsp ct->ondisk_path);
5633 35213c7c 2020-07-23 stsp goto done;
5634 35213c7c 2020-07-23 stsp }
5635 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5636 ad10f64e 2023-07-19 thomas target_len, ct->ondisk_path, a->worktree->root_path,
5637 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
5638 35213c7c 2020-07-23 stsp if (err)
5639 35213c7c 2020-07-23 stsp goto done;
5640 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5641 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5642 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5643 35213c7c 2020-07-23 stsp goto done;
5644 35213c7c 2020-07-23 stsp }
5645 35213c7c 2020-07-23 stsp }
5646 35213c7c 2020-07-23 stsp
5647 35213c7c 2020-07-23 stsp
5648 cf066bf8 2019-05-09 stsp ct->status = status;
5649 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5650 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5651 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5652 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5653 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5654 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5655 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5656 b416585c 2019-05-13 stsp goto done;
5657 b416585c 2019-05-13 stsp }
5658 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5659 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5660 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5661 f0b75401 2019-08-03 stsp goto done;
5662 f0b75401 2019-08-03 stsp }
5663 f0b75401 2019-08-03 stsp }
5664 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5665 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5666 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5667 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5668 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5669 036813ee 2019-05-09 stsp goto done;
5670 036813ee 2019-05-09 stsp }
5671 ca2503ea 2019-05-09 stsp }
5672 24519714 2019-05-09 stsp ct->path = strdup(path);
5673 24519714 2019-05-09 stsp if (ct->path == NULL) {
5674 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5675 24519714 2019-05-09 stsp goto done;
5676 24519714 2019-05-09 stsp }
5677 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5678 ef899790 2022-11-01 thomas if (err)
5679 ef899790 2022-11-01 thomas goto done;
5680 ef899790 2022-11-01 thomas
5681 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5682 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5683 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5684 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5685 ef899790 2022-11-01 thomas if (err)
5686 ef899790 2022-11-01 thomas goto done;
5687 ef899790 2022-11-01 thomas }
5688 c4296144 2019-05-09 stsp done:
5689 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5690 ed175427 2019-05-09 stsp free_commitable(ct);
5691 24519714 2019-05-09 stsp free(parent_path);
5692 036813ee 2019-05-09 stsp free(path);
5693 a129376b 2019-03-28 stsp return err;
5694 a129376b 2019-03-28 stsp }
5695 c4296144 2019-05-09 stsp
5696 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5697 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5698 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5699 036813ee 2019-05-09 stsp struct got_repository *);
5700 ed175427 2019-05-09 stsp
5701 ed175427 2019-05-09 stsp static const struct got_error *
5702 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5703 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5704 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5705 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5706 afa376bf 2019-05-09 stsp struct got_repository *repo)
5707 ed175427 2019-05-09 stsp {
5708 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5709 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5710 036813ee 2019-05-09 stsp char *subpath;
5711 ed175427 2019-05-09 stsp
5712 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5713 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5714 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5715 ed175427 2019-05-09 stsp
5716 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5717 ed175427 2019-05-09 stsp if (err)
5718 ed175427 2019-05-09 stsp return err;
5719 ed175427 2019-05-09 stsp
5720 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5721 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5722 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5723 036813ee 2019-05-09 stsp free(subpath);
5724 036813ee 2019-05-09 stsp return err;
5725 036813ee 2019-05-09 stsp }
5726 ed175427 2019-05-09 stsp
5727 036813ee 2019-05-09 stsp static const struct got_error *
5728 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5729 036813ee 2019-05-09 stsp {
5730 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5731 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5732 ed175427 2019-05-09 stsp
5733 036813ee 2019-05-09 stsp *match = 0;
5734 ed175427 2019-05-09 stsp
5735 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5736 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5737 0f63689d 2019-05-10 stsp return NULL;
5738 036813ee 2019-05-09 stsp }
5739 0b5cc0d6 2019-05-09 stsp
5740 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5741 0f63689d 2019-05-10 stsp if (err)
5742 0f63689d 2019-05-10 stsp return err;
5743 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5744 036813ee 2019-05-09 stsp free(ct_parent_path);
5745 036813ee 2019-05-09 stsp return err;
5746 036813ee 2019-05-09 stsp }
5747 0b5cc0d6 2019-05-09 stsp
5748 768aea60 2019-05-09 stsp static mode_t
5749 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5750 768aea60 2019-05-09 stsp {
5751 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5752 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5753 3d9a4ec4 2020-07-23 stsp
5754 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5755 768aea60 2019-05-09 stsp }
5756 768aea60 2019-05-09 stsp
5757 036813ee 2019-05-09 stsp static const struct got_error *
5758 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5759 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5760 036813ee 2019-05-09 stsp {
5761 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5762 ca2503ea 2019-05-09 stsp
5763 036813ee 2019-05-09 stsp *new_te = NULL;
5764 0b5cc0d6 2019-05-09 stsp
5765 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5766 036813ee 2019-05-09 stsp if (err)
5767 036813ee 2019-05-09 stsp goto done;
5768 0b5cc0d6 2019-05-09 stsp
5769 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5770 036813ee 2019-05-09 stsp
5771 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5772 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5773 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5774 5f8a88c6 2019-08-03 stsp else
5775 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5776 036813ee 2019-05-09 stsp done:
5777 036813ee 2019-05-09 stsp if (err && *new_te) {
5778 56e0773d 2019-11-28 stsp free(*new_te);
5779 036813ee 2019-05-09 stsp *new_te = NULL;
5780 036813ee 2019-05-09 stsp }
5781 036813ee 2019-05-09 stsp return err;
5782 036813ee 2019-05-09 stsp }
5783 036813ee 2019-05-09 stsp
5784 036813ee 2019-05-09 stsp static const struct got_error *
5785 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5786 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5787 036813ee 2019-05-09 stsp {
5788 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5789 102b254e 2020-10-19 stsp char *ct_name = NULL;
5790 036813ee 2019-05-09 stsp
5791 036813ee 2019-05-09 stsp *new_te = NULL;
5792 036813ee 2019-05-09 stsp
5793 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5794 036813ee 2019-05-09 stsp if (*new_te == NULL)
5795 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5796 036813ee 2019-05-09 stsp
5797 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5798 102b254e 2020-10-19 stsp if (err)
5799 036813ee 2019-05-09 stsp goto done;
5800 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5801 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5802 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5803 036813ee 2019-05-09 stsp goto done;
5804 036813ee 2019-05-09 stsp }
5805 036813ee 2019-05-09 stsp
5806 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5807 036813ee 2019-05-09 stsp
5808 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5809 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5810 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5811 5f8a88c6 2019-08-03 stsp else
5812 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5813 036813ee 2019-05-09 stsp done:
5814 102b254e 2020-10-19 stsp free(ct_name);
5815 036813ee 2019-05-09 stsp if (err && *new_te) {
5816 56e0773d 2019-11-28 stsp free(*new_te);
5817 036813ee 2019-05-09 stsp *new_te = NULL;
5818 036813ee 2019-05-09 stsp }
5819 036813ee 2019-05-09 stsp return err;
5820 036813ee 2019-05-09 stsp }
5821 036813ee 2019-05-09 stsp
5822 036813ee 2019-05-09 stsp static const struct got_error *
5823 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5824 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5825 036813ee 2019-05-09 stsp {
5826 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5827 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5828 036813ee 2019-05-09 stsp
5829 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5830 036813ee 2019-05-09 stsp if (err)
5831 036813ee 2019-05-09 stsp return err;
5832 036813ee 2019-05-09 stsp if (new_pe == NULL)
5833 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5834 036813ee 2019-05-09 stsp return NULL;
5835 afa376bf 2019-05-09 stsp }
5836 afa376bf 2019-05-09 stsp
5837 afa376bf 2019-05-09 stsp static const struct got_error *
5838 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5839 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5840 afa376bf 2019-05-09 stsp {
5841 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5842 5f8a88c6 2019-08-03 stsp unsigned char status;
5843 ae1e948a 2021-09-28 thomas
5844 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5845 ae1e948a 2021-09-28 thomas return NULL;
5846 5f8a88c6 2019-08-03 stsp
5847 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5848 afa376bf 2019-05-09 stsp ct_path++;
5849 5f8a88c6 2019-08-03 stsp
5850 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5851 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5852 5f8a88c6 2019-08-03 stsp else
5853 5f8a88c6 2019-08-03 stsp status = ct->status;
5854 5f8a88c6 2019-08-03 stsp
5855 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5856 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5857 036813ee 2019-05-09 stsp }
5858 036813ee 2019-05-09 stsp
5859 036813ee 2019-05-09 stsp static const struct got_error *
5860 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5861 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5862 44d03001 2019-05-09 stsp {
5863 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5864 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5865 44d03001 2019-05-09 stsp char *te_path;
5866 44d03001 2019-05-09 stsp
5867 44d03001 2019-05-09 stsp *modified = 0;
5868 44d03001 2019-05-09 stsp
5869 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5870 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5871 44d03001 2019-05-09 stsp te->name) == -1)
5872 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5873 44d03001 2019-05-09 stsp
5874 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5875 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5876 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5877 44d03001 2019-05-09 stsp strlen(te_path));
5878 62d463ca 2020-10-20 naddy if (*modified)
5879 44d03001 2019-05-09 stsp break;
5880 44d03001 2019-05-09 stsp }
5881 44d03001 2019-05-09 stsp
5882 44d03001 2019-05-09 stsp free(te_path);
5883 44d03001 2019-05-09 stsp return err;
5884 44d03001 2019-05-09 stsp }
5885 44d03001 2019-05-09 stsp
5886 44d03001 2019-05-09 stsp static const struct got_error *
5887 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5888 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5889 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5890 036813ee 2019-05-09 stsp {
5891 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5892 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5893 036813ee 2019-05-09 stsp
5894 036813ee 2019-05-09 stsp *ctp = NULL;
5895 036813ee 2019-05-09 stsp
5896 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5897 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5898 036813ee 2019-05-09 stsp char *ct_name = NULL;
5899 036813ee 2019-05-09 stsp int path_matches;
5900 036813ee 2019-05-09 stsp
5901 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5902 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5903 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5904 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5905 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5906 5f8a88c6 2019-08-03 stsp continue;
5907 5f8a88c6 2019-08-03 stsp } else {
5908 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5909 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5910 5f8a88c6 2019-08-03 stsp continue;
5911 5f8a88c6 2019-08-03 stsp }
5912 036813ee 2019-05-09 stsp
5913 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5914 036813ee 2019-05-09 stsp continue;
5915 036813ee 2019-05-09 stsp
5916 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5917 62d463ca 2020-10-20 naddy if (err)
5918 036813ee 2019-05-09 stsp return err;
5919 036813ee 2019-05-09 stsp if (!path_matches)
5920 036813ee 2019-05-09 stsp continue;
5921 036813ee 2019-05-09 stsp
5922 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5923 d34b633e 2020-10-19 stsp if (err)
5924 d34b633e 2020-10-19 stsp return err;
5925 d34b633e 2020-10-19 stsp
5926 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5927 d34b633e 2020-10-19 stsp free(ct_name);
5928 036813ee 2019-05-09 stsp continue;
5929 d34b633e 2020-10-19 stsp }
5930 d34b633e 2020-10-19 stsp free(ct_name);
5931 036813ee 2019-05-09 stsp
5932 036813ee 2019-05-09 stsp *ctp = ct;
5933 036813ee 2019-05-09 stsp break;
5934 036813ee 2019-05-09 stsp }
5935 2b496619 2019-07-10 stsp
5936 2b496619 2019-07-10 stsp return err;
5937 2b496619 2019-07-10 stsp }
5938 2b496619 2019-07-10 stsp
5939 2b496619 2019-07-10 stsp static const struct got_error *
5940 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5941 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5942 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5943 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5944 2b496619 2019-07-10 stsp struct got_repository *repo)
5945 2b496619 2019-07-10 stsp {
5946 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5947 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5948 2b496619 2019-07-10 stsp char *subtree_path;
5949 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5950 ba580f68 2020-03-22 stsp int nentries;
5951 2b496619 2019-07-10 stsp
5952 2b496619 2019-07-10 stsp *new_tep = NULL;
5953 2b496619 2019-07-10 stsp
5954 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5955 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5956 2b496619 2019-07-10 stsp child_path) == -1)
5957 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5958 036813ee 2019-05-09 stsp
5959 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5960 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5961 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5962 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5963 56e0773d 2019-11-28 stsp
5964 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5965 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5966 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5967 2b496619 2019-07-10 stsp goto done;
5968 2b496619 2019-07-10 stsp }
5969 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5970 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5971 2b496619 2019-07-10 stsp if (err) {
5972 56e0773d 2019-11-28 stsp free(new_te);
5973 2b496619 2019-07-10 stsp goto done;
5974 2b496619 2019-07-10 stsp }
5975 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5976 2b496619 2019-07-10 stsp done:
5977 56e0773d 2019-11-28 stsp free(id);
5978 2b496619 2019-07-10 stsp free(subtree_path);
5979 2b496619 2019-07-10 stsp if (err == NULL)
5980 2b496619 2019-07-10 stsp *new_tep = new_te;
5981 036813ee 2019-05-09 stsp return err;
5982 036813ee 2019-05-09 stsp }
5983 036813ee 2019-05-09 stsp
5984 036813ee 2019-05-09 stsp static const struct got_error *
5985 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5986 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5987 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5988 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5989 036813ee 2019-05-09 stsp struct got_repository *repo)
5990 036813ee 2019-05-09 stsp {
5991 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5992 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5993 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5994 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5995 036813ee 2019-05-09 stsp
5996 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5997 ba580f68 2020-03-22 stsp *nentries = 0;
5998 036813ee 2019-05-09 stsp
5999 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6000 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6001 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6002 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6003 036813ee 2019-05-09 stsp
6004 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6005 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6006 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6007 036813ee 2019-05-09 stsp continue;
6008 036813ee 2019-05-09 stsp
6009 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6010 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6011 036813ee 2019-05-09 stsp continue;
6012 036813ee 2019-05-09 stsp
6013 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6014 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6015 036813ee 2019-05-09 stsp if (err)
6016 036813ee 2019-05-09 stsp goto done;
6017 036813ee 2019-05-09 stsp
6018 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6019 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6020 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6021 036813ee 2019-05-09 stsp if (err)
6022 036813ee 2019-05-09 stsp goto done;
6023 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6024 afa376bf 2019-05-09 stsp if (err)
6025 afa376bf 2019-05-09 stsp goto done;
6026 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6027 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6028 2b496619 2019-07-10 stsp if (err)
6029 2f51b5b3 2019-05-09 stsp goto done;
6030 ba580f68 2020-03-22 stsp (*nentries)++;
6031 2b496619 2019-07-10 stsp } else {
6032 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6033 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6034 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6035 2b496619 2019-07-10 stsp == NULL) {
6036 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6037 2b496619 2019-07-10 stsp child_path, path_base_tree,
6038 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6039 2b496619 2019-07-10 stsp repo);
6040 2b496619 2019-07-10 stsp if (err)
6041 2b496619 2019-07-10 stsp goto done;
6042 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6043 2b496619 2019-07-10 stsp if (err)
6044 2b496619 2019-07-10 stsp goto done;
6045 ba580f68 2020-03-22 stsp (*nentries)++;
6046 9ba0479c 2019-05-10 stsp }
6047 ed175427 2019-05-09 stsp }
6048 2f51b5b3 2019-05-09 stsp }
6049 2f51b5b3 2019-05-09 stsp
6050 2f51b5b3 2019-05-09 stsp if (base_tree) {
6051 56e0773d 2019-11-28 stsp int i, nbase_entries;
6052 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6053 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6054 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6055 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6056 63c5ca5d 2019-08-24 stsp
6057 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6058 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6059 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6060 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6061 63c5ca5d 2019-08-24 stsp if (err)
6062 63c5ca5d 2019-08-24 stsp goto done;
6063 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6064 63c5ca5d 2019-08-24 stsp if (err)
6065 63c5ca5d 2019-08-24 stsp goto done;
6066 ba580f68 2020-03-22 stsp (*nentries)++;
6067 63c5ca5d 2019-08-24 stsp continue;
6068 63c5ca5d 2019-08-24 stsp }
6069 2f51b5b3 2019-05-09 stsp
6070 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6071 44d03001 2019-05-09 stsp int modified;
6072 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6073 036813ee 2019-05-09 stsp if (err)
6074 036813ee 2019-05-09 stsp goto done;
6075 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6076 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6077 2f51b5b3 2019-05-09 stsp if (err)
6078 2f51b5b3 2019-05-09 stsp goto done;
6079 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6080 44d03001 2019-05-09 stsp if (modified) {
6081 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6082 ba580f68 2020-03-22 stsp int nsubentries;
6083 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6084 ba580f68 2020-03-22 stsp &nsubentries, te,
6085 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6086 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6087 44d03001 2019-05-09 stsp if (err)
6088 44d03001 2019-05-09 stsp goto done;
6089 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6090 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6091 ba580f68 2020-03-22 stsp free(new_id);
6092 ba580f68 2020-03-22 stsp continue;
6093 ba580f68 2020-03-22 stsp }
6094 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6095 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6096 56e0773d 2019-11-28 stsp free(new_id);
6097 44d03001 2019-05-09 stsp }
6098 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6099 036813ee 2019-05-09 stsp if (err)
6100 036813ee 2019-05-09 stsp goto done;
6101 ba580f68 2020-03-22 stsp (*nentries)++;
6102 2f51b5b3 2019-05-09 stsp continue;
6103 036813ee 2019-05-09 stsp }
6104 2f51b5b3 2019-05-09 stsp
6105 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6106 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6107 f66c734c 2019-09-22 stsp if (err)
6108 f66c734c 2019-09-22 stsp goto done;
6109 2f51b5b3 2019-05-09 stsp if (ct) {
6110 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6111 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6112 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6113 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6114 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6115 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6116 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6117 2f51b5b3 2019-05-09 stsp if (err)
6118 2f51b5b3 2019-05-09 stsp goto done;
6119 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6120 2f51b5b3 2019-05-09 stsp if (err)
6121 2f51b5b3 2019-05-09 stsp goto done;
6122 ba580f68 2020-03-22 stsp (*nentries)++;
6123 2f51b5b3 2019-05-09 stsp }
6124 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6125 b416585c 2019-05-13 stsp status_arg);
6126 afa376bf 2019-05-09 stsp if (err)
6127 afa376bf 2019-05-09 stsp goto done;
6128 2f51b5b3 2019-05-09 stsp } else {
6129 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6130 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6131 2f51b5b3 2019-05-09 stsp if (err)
6132 2f51b5b3 2019-05-09 stsp goto done;
6133 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6134 2f51b5b3 2019-05-09 stsp if (err)
6135 2f51b5b3 2019-05-09 stsp goto done;
6136 ba580f68 2020-03-22 stsp (*nentries)++;
6137 2f51b5b3 2019-05-09 stsp }
6138 ca2503ea 2019-05-09 stsp }
6139 ed175427 2019-05-09 stsp }
6140 0b5cc0d6 2019-05-09 stsp
6141 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6142 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6143 ed175427 2019-05-09 stsp done:
6144 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6145 2e1fa222 2020-07-23 stsp return err;
6146 2e1fa222 2020-07-23 stsp }
6147 2e1fa222 2020-07-23 stsp
6148 2e1fa222 2020-07-23 stsp static const struct got_error *
6149 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6150 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6151 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6152 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6153 ebf99748 2019-05-09 stsp {
6154 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6155 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6156 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6157 ebf99748 2019-05-09 stsp
6158 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6159 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6160 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6161 ebf99748 2019-05-09 stsp
6162 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6163 437adc9d 2020-12-10 yzhong
6164 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6165 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6166 437adc9d 2020-12-10 yzhong if (err)
6167 437adc9d 2020-12-10 yzhong goto done;
6168 437adc9d 2020-12-10 yzhong
6169 ebf99748 2019-05-09 stsp if (ie) {
6170 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6171 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6172 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6173 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6174 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6175 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6176 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6177 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6178 437adc9d 2020-12-10 yzhong
6179 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6180 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6181 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6182 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6183 72fd46fa 2019-09-06 stsp !have_staged_files);
6184 ebf99748 2019-05-09 stsp } else
6185 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6186 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6187 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6188 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6189 72fd46fa 2019-09-06 stsp !have_staged_files);
6190 ebf99748 2019-05-09 stsp } else {
6191 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6192 ebf99748 2019-05-09 stsp if (err)
6193 437adc9d 2020-12-10 yzhong goto done;
6194 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6195 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6196 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6197 3969253a 2020-03-07 stsp if (err) {
6198 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6199 437adc9d 2020-12-10 yzhong goto done;
6200 3969253a 2020-03-07 stsp }
6201 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6202 3969253a 2020-03-07 stsp if (err) {
6203 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6204 437adc9d 2020-12-10 yzhong goto done;
6205 3969253a 2020-03-07 stsp }
6206 ebf99748 2019-05-09 stsp }
6207 437adc9d 2020-12-10 yzhong free(relpath);
6208 437adc9d 2020-12-10 yzhong relpath = NULL;
6209 ebf99748 2019-05-09 stsp }
6210 437adc9d 2020-12-10 yzhong done:
6211 437adc9d 2020-12-10 yzhong free(relpath);
6212 d56d26ce 2019-05-10 stsp return err;
6213 d56d26ce 2019-05-10 stsp }
6214 735ef5ac 2019-08-03 stsp
6215 d56d26ce 2019-05-10 stsp
6216 d56d26ce 2019-05-10 stsp static const struct got_error *
6217 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6218 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6219 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6220 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6221 735ef5ac 2019-08-03 stsp int ood_errcode)
6222 d56d26ce 2019-05-10 stsp {
6223 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6224 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6225 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6226 1a36436d 2019-06-10 stsp
6227 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6228 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6229 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6230 1a36436d 2019-06-10 stsp return NULL;
6231 9bead371 2019-07-28 stsp /*
6232 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6233 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6234 9bead371 2019-07-28 stsp */
6235 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6236 945f9229 2022-04-16 thomas if (err)
6237 945f9229 2022-04-16 thomas goto done;
6238 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6239 9bead371 2019-07-28 stsp if (err) {
6240 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6241 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6242 1a36436d 2019-06-10 stsp goto done;
6243 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6244 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6245 1a36436d 2019-06-10 stsp } else {
6246 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6247 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6248 945f9229 2022-04-16 thomas if (err)
6249 945f9229 2022-04-16 thomas goto done;
6250 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6251 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6252 1a36436d 2019-06-10 stsp goto done;
6253 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6254 1a36436d 2019-06-10 stsp }
6255 1a36436d 2019-06-10 stsp done:
6256 1a36436d 2019-06-10 stsp free(id);
6257 945f9229 2022-04-16 thomas if (commit)
6258 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6259 1a36436d 2019-06-10 stsp return err;
6260 ebf99748 2019-05-09 stsp }
6261 ebf99748 2019-05-09 stsp
6262 ef20f542 2022-06-26 thomas static const struct got_error *
6263 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6264 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6265 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6266 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6267 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6268 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6269 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6270 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6271 afa376bf 2019-05-09 stsp struct got_repository *repo)
6272 c4296144 2019-05-09 stsp {
6273 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6274 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6275 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6276 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6277 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6278 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6279 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6280 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6281 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6282 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6283 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6284 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6285 892eab0a 2022-07-22 thomas time_t timestamp;
6286 c4296144 2019-05-09 stsp
6287 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6288 c4296144 2019-05-09 stsp
6289 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6290 675c7539 2019-05-09 stsp
6291 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6292 588edf97 2019-05-10 stsp if (err)
6293 588edf97 2019-05-10 stsp goto done;
6294 de18fc63 2019-05-09 stsp
6295 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6296 588edf97 2019-05-10 stsp if (err)
6297 588edf97 2019-05-10 stsp goto done;
6298 588edf97 2019-05-10 stsp
6299 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6300 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6301 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6302 33ad4cbe 2019-05-12 jcs if (err)
6303 33ad4cbe 2019-05-12 jcs goto done;
6304 33ad4cbe 2019-05-12 jcs }
6305 c4296144 2019-05-09 stsp
6306 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6307 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6308 33ad4cbe 2019-05-12 jcs goto done;
6309 33ad4cbe 2019-05-12 jcs }
6310 33ad4cbe 2019-05-12 jcs
6311 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6312 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6313 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6314 cf066bf8 2019-05-09 stsp char *ondisk_path;
6315 cf066bf8 2019-05-09 stsp
6316 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6317 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6318 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6319 5f8a88c6 2019-08-03 stsp continue;
6320 5f8a88c6 2019-08-03 stsp
6321 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6322 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6323 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6324 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6325 cf066bf8 2019-05-09 stsp continue;
6326 cf066bf8 2019-05-09 stsp
6327 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6328 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6329 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6330 cf066bf8 2019-05-09 stsp goto done;
6331 cf066bf8 2019-05-09 stsp }
6332 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6333 2e1fa222 2020-07-23 stsp free(ondisk_path);
6334 2e1fa222 2020-07-23 stsp if (err)
6335 cf066bf8 2019-05-09 stsp goto done;
6336 cf066bf8 2019-05-09 stsp }
6337 036813ee 2019-05-09 stsp
6338 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6339 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6340 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6341 036813ee 2019-05-09 stsp if (err)
6342 036813ee 2019-05-09 stsp goto done;
6343 036813ee 2019-05-09 stsp
6344 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6345 de18fc63 2019-05-09 stsp if (err)
6346 de18fc63 2019-05-09 stsp goto done;
6347 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6348 10604dce 2021-09-24 thomas nparents++;
6349 10604dce 2021-09-24 thomas if (parent_id2) {
6350 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6351 10604dce 2021-09-24 thomas if (err)
6352 10604dce 2021-09-24 thomas goto done;
6353 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6354 10604dce 2021-09-24 thomas nparents++;
6355 10604dce 2021-09-24 thomas }
6356 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6357 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6358 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6359 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6360 33ad4cbe 2019-05-12 jcs free(logmsg);
6361 9d40349a 2019-05-09 stsp if (err)
6362 9d40349a 2019-05-09 stsp goto done;
6363 9d40349a 2019-05-09 stsp
6364 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6365 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6366 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6367 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6368 09f5bd90 2019-05-09 stsp goto done;
6369 09f5bd90 2019-05-09 stsp }
6370 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6371 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6372 09f5bd90 2019-05-09 stsp if (err)
6373 09f5bd90 2019-05-09 stsp goto done;
6374 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6375 ebf99748 2019-05-09 stsp if (err)
6376 ebf99748 2019-05-09 stsp goto done;
6377 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6378 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6379 09f5bd90 2019-05-09 stsp goto done;
6380 09f5bd90 2019-05-09 stsp }
6381 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6382 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6383 f2c16586 2019-05-09 stsp if (err)
6384 f2c16586 2019-05-09 stsp goto done;
6385 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6386 f2c16586 2019-05-09 stsp if (err)
6387 f2c16586 2019-05-09 stsp goto done;
6388 f2c16586 2019-05-09 stsp
6389 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6390 09f5bd90 2019-05-09 stsp if (err)
6391 09f5bd90 2019-05-09 stsp goto done;
6392 09f5bd90 2019-05-09 stsp
6393 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6394 ebf99748 2019-05-09 stsp if (err)
6395 ebf99748 2019-05-09 stsp goto done;
6396 c4296144 2019-05-09 stsp done:
6397 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6398 588edf97 2019-05-10 stsp if (head_tree)
6399 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6400 588edf97 2019-05-10 stsp if (head_commit)
6401 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6402 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6403 2f17228e 2019-05-12 stsp if (head_ref2) {
6404 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6405 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6406 2f17228e 2019-05-12 stsp err = unlockerr;
6407 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6408 39cd0ff6 2019-07-12 stsp }
6409 39cd0ff6 2019-07-12 stsp return err;
6410 39cd0ff6 2019-07-12 stsp }
6411 5c1e53bc 2019-07-28 stsp
6412 5c1e53bc 2019-07-28 stsp static const struct got_error *
6413 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6414 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6415 5c1e53bc 2019-07-28 stsp {
6416 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6417 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6418 39cd0ff6 2019-07-12 stsp
6419 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6420 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6421 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6422 5c1e53bc 2019-07-28 stsp
6423 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6424 5c1e53bc 2019-07-28 stsp ct_path++;
6425 5c1e53bc 2019-07-28 stsp
6426 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6427 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6428 5c1e53bc 2019-07-28 stsp break;
6429 5c1e53bc 2019-07-28 stsp }
6430 5c1e53bc 2019-07-28 stsp
6431 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6432 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6433 5c1e53bc 2019-07-28 stsp
6434 5c1e53bc 2019-07-28 stsp return NULL;
6435 5c1e53bc 2019-07-28 stsp }
6436 5c1e53bc 2019-07-28 stsp
6437 f0b75401 2019-08-03 stsp static const struct got_error *
6438 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6439 f0b75401 2019-08-03 stsp {
6440 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6441 f0b75401 2019-08-03 stsp
6442 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6443 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6444 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6445 f0b75401 2019-08-03 stsp }
6446 f0b75401 2019-08-03 stsp
6447 f0b75401 2019-08-03 stsp return NULL;
6448 f0b75401 2019-08-03 stsp }
6449 f0b75401 2019-08-03 stsp
6450 5f8a88c6 2019-08-03 stsp static const struct got_error *
6451 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6452 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6453 5f8a88c6 2019-08-03 stsp {
6454 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6455 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6456 5f8a88c6 2019-08-03 stsp
6457 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6458 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6459 5f8a88c6 2019-08-03 stsp continue;
6460 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6461 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6462 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6463 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6464 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6465 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6466 5f8a88c6 2019-08-03 stsp }
6467 5f8a88c6 2019-08-03 stsp
6468 5f8a88c6 2019-08-03 stsp return NULL;
6469 5f8a88c6 2019-08-03 stsp }
6470 5f8a88c6 2019-08-03 stsp
6471 39cd0ff6 2019-07-12 stsp const struct got_error *
6472 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6473 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6474 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6475 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6476 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6477 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6478 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6479 39cd0ff6 2019-07-12 stsp {
6480 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6481 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6482 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6483 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6484 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6485 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6486 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6487 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6488 ef899790 2022-11-01 thomas char *diff_path = NULL;
6489 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6490 39cd0ff6 2019-07-12 stsp
6491 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6492 39cd0ff6 2019-07-12 stsp
6493 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6494 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6495 39cd0ff6 2019-07-12 stsp
6496 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6497 39cd0ff6 2019-07-12 stsp if (err)
6498 39cd0ff6 2019-07-12 stsp goto done;
6499 39cd0ff6 2019-07-12 stsp
6500 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6501 39cd0ff6 2019-07-12 stsp if (err)
6502 39cd0ff6 2019-07-12 stsp goto done;
6503 39cd0ff6 2019-07-12 stsp
6504 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6505 39cd0ff6 2019-07-12 stsp if (err)
6506 39cd0ff6 2019-07-12 stsp goto done;
6507 39cd0ff6 2019-07-12 stsp
6508 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6509 39cd0ff6 2019-07-12 stsp if (err)
6510 39cd0ff6 2019-07-12 stsp goto done;
6511 39cd0ff6 2019-07-12 stsp
6512 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6513 5f8a88c6 2019-08-03 stsp &have_staged_files);
6514 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6515 5f8a88c6 2019-08-03 stsp goto done;
6516 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6517 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6518 5f8a88c6 2019-08-03 stsp if (err)
6519 5f8a88c6 2019-08-03 stsp goto done;
6520 5f8a88c6 2019-08-03 stsp }
6521 5f8a88c6 2019-08-03 stsp
6522 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6523 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6524 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6525 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6526 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6527 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6528 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6529 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6530 ef899790 2022-11-01 thomas if (show_diff) {
6531 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6532 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6533 ef899790 2022-11-01 thomas if (err)
6534 ef899790 2022-11-01 thomas goto done;
6535 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6536 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6537 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6538 ef899790 2022-11-01 thomas goto done;
6539 ef899790 2022-11-01 thomas }
6540 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6541 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6542 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6543 ef899790 2022-11-01 thomas goto done;
6544 ef899790 2022-11-01 thomas }
6545 ef899790 2022-11-01 thomas }
6546 ef899790 2022-11-01 thomas
6547 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6548 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6549 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6550 5c1e53bc 2019-07-28 stsp if (err)
6551 5c1e53bc 2019-07-28 stsp goto done;
6552 5c1e53bc 2019-07-28 stsp }
6553 39cd0ff6 2019-07-12 stsp
6554 ef899790 2022-11-01 thomas if (show_diff) {
6555 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6556 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6557 ef899790 2022-11-01 thomas goto done;
6558 ef899790 2022-11-01 thomas }
6559 ef899790 2022-11-01 thomas }
6560 ef899790 2022-11-01 thomas
6561 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6562 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6563 39cd0ff6 2019-07-12 stsp goto done;
6564 5c1e53bc 2019-07-28 stsp }
6565 5c1e53bc 2019-07-28 stsp
6566 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6567 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6568 5c1e53bc 2019-07-28 stsp if (err)
6569 5c1e53bc 2019-07-28 stsp goto done;
6570 39cd0ff6 2019-07-12 stsp }
6571 f0b75401 2019-08-03 stsp
6572 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6573 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6574 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6575 f0b75401 2019-08-03 stsp
6576 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6577 f0b75401 2019-08-03 stsp ct_path++;
6578 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6579 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6580 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6581 b50cabdf 2019-07-12 stsp if (err)
6582 b50cabdf 2019-07-12 stsp goto done;
6583 f0b75401 2019-08-03 stsp
6584 b50cabdf 2019-07-12 stsp }
6585 b50cabdf 2019-07-12 stsp
6586 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6587 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6588 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6589 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6590 39cd0ff6 2019-07-12 stsp if (err)
6591 39cd0ff6 2019-07-12 stsp goto done;
6592 39cd0ff6 2019-07-12 stsp
6593 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6594 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6595 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6596 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6597 39cd0ff6 2019-07-12 stsp err = sync_err;
6598 39cd0ff6 2019-07-12 stsp done:
6599 39cd0ff6 2019-07-12 stsp if (fileindex)
6600 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6601 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6602 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6603 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6604 39cd0ff6 2019-07-12 stsp err = unlockerr;
6605 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6606 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6607 21c2d8be 2023-01-10 thomas
6608 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6609 39cd0ff6 2019-07-12 stsp }
6610 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6611 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6612 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6613 ef899790 2022-11-01 thomas free(diff_path);
6614 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6615 ef899790 2022-11-01 thomas err == NULL)
6616 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6617 c4296144 2019-05-09 stsp return err;
6618 8656d6c4 2019-05-20 stsp }
6619 8656d6c4 2019-05-20 stsp
6620 8656d6c4 2019-05-20 stsp const char *
6621 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6622 8656d6c4 2019-05-20 stsp {
6623 8656d6c4 2019-05-20 stsp return ct->path;
6624 8656d6c4 2019-05-20 stsp }
6625 8656d6c4 2019-05-20 stsp
6626 8656d6c4 2019-05-20 stsp unsigned int
6627 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6628 8656d6c4 2019-05-20 stsp {
6629 8656d6c4 2019-05-20 stsp return ct->status;
6630 818c7501 2019-07-11 stsp }
6631 818c7501 2019-07-11 stsp
6632 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6633 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6634 818c7501 2019-07-11 stsp struct got_repository *repo;
6635 818c7501 2019-07-11 stsp };
6636 818c7501 2019-07-11 stsp
6637 818c7501 2019-07-11 stsp static const struct got_error *
6638 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6639 818c7501 2019-07-11 stsp {
6640 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6641 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6642 818c7501 2019-07-11 stsp unsigned char status;
6643 818c7501 2019-07-11 stsp struct stat sb;
6644 818c7501 2019-07-11 stsp char *ondisk_path;
6645 818c7501 2019-07-11 stsp
6646 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6647 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6648 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6649 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6650 818c7501 2019-07-11 stsp
6651 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6652 818c7501 2019-07-11 stsp == -1)
6653 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6654 818c7501 2019-07-11 stsp
6655 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6656 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6657 818c7501 2019-07-11 stsp free(ondisk_path);
6658 818c7501 2019-07-11 stsp if (err)
6659 818c7501 2019-07-11 stsp return err;
6660 818c7501 2019-07-11 stsp
6661 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6662 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6663 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6664 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6665 818c7501 2019-07-11 stsp
6666 818c7501 2019-07-11 stsp return NULL;
6667 818c7501 2019-07-11 stsp }
6668 818c7501 2019-07-11 stsp
6669 818c7501 2019-07-11 stsp const struct got_error *
6670 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6671 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6672 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6673 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6674 818c7501 2019-07-11 stsp {
6675 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6676 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6677 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6678 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6679 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6680 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6681 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6682 818c7501 2019-07-11 stsp
6683 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6684 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6685 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6686 818c7501 2019-07-11 stsp
6687 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6688 818c7501 2019-07-11 stsp if (err)
6689 818c7501 2019-07-11 stsp return err;
6690 818c7501 2019-07-11 stsp
6691 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6692 818c7501 2019-07-11 stsp if (err)
6693 818c7501 2019-07-11 stsp goto done;
6694 818c7501 2019-07-11 stsp
6695 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6696 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6697 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6698 818c7501 2019-07-11 stsp &ok_arg);
6699 818c7501 2019-07-11 stsp if (err)
6700 818c7501 2019-07-11 stsp goto done;
6701 818c7501 2019-07-11 stsp
6702 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6703 818c7501 2019-07-11 stsp if (err)
6704 818c7501 2019-07-11 stsp goto done;
6705 818c7501 2019-07-11 stsp
6706 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6707 818c7501 2019-07-11 stsp if (err)
6708 818c7501 2019-07-11 stsp goto done;
6709 818c7501 2019-07-11 stsp
6710 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6711 818c7501 2019-07-11 stsp if (err)
6712 818c7501 2019-07-11 stsp goto done;
6713 818c7501 2019-07-11 stsp
6714 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6715 818c7501 2019-07-11 stsp 0);
6716 e51d7b55 2020-01-04 stsp if (err)
6717 e51d7b55 2020-01-04 stsp goto done;
6718 e51d7b55 2020-01-04 stsp
6719 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6720 818c7501 2019-07-11 stsp if (err)
6721 818c7501 2019-07-11 stsp goto done;
6722 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6723 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6724 e51d7b55 2020-01-04 stsp goto done;
6725 e51d7b55 2020-01-04 stsp }
6726 818c7501 2019-07-11 stsp
6727 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6728 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6729 818c7501 2019-07-11 stsp if (err)
6730 818c7501 2019-07-11 stsp goto done;
6731 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6732 818c7501 2019-07-11 stsp if (err)
6733 818c7501 2019-07-11 stsp goto done;
6734 818c7501 2019-07-11 stsp
6735 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6736 818c7501 2019-07-11 stsp
6737 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6738 818c7501 2019-07-11 stsp if (err)
6739 818c7501 2019-07-11 stsp goto done;
6740 818c7501 2019-07-11 stsp
6741 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6742 818c7501 2019-07-11 stsp if (err)
6743 818c7501 2019-07-11 stsp goto done;
6744 818c7501 2019-07-11 stsp
6745 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6746 818c7501 2019-07-11 stsp worktree->base_commit_id);
6747 818c7501 2019-07-11 stsp if (err)
6748 818c7501 2019-07-11 stsp goto done;
6749 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6750 818c7501 2019-07-11 stsp if (err)
6751 818c7501 2019-07-11 stsp goto done;
6752 818c7501 2019-07-11 stsp
6753 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6754 818c7501 2019-07-11 stsp if (err)
6755 818c7501 2019-07-11 stsp goto done;
6756 818c7501 2019-07-11 stsp done:
6757 818c7501 2019-07-11 stsp free(fileindex_path);
6758 818c7501 2019-07-11 stsp free(tmp_branch_name);
6759 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6760 818c7501 2019-07-11 stsp free(branch_ref_name);
6761 818c7501 2019-07-11 stsp if (branch_ref)
6762 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6763 818c7501 2019-07-11 stsp if (wt_branch)
6764 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6765 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6766 818c7501 2019-07-11 stsp if (err) {
6767 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6768 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6769 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6770 818c7501 2019-07-11 stsp }
6771 818c7501 2019-07-11 stsp if (*tmp_branch) {
6772 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6773 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6774 818c7501 2019-07-11 stsp }
6775 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6776 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6777 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6778 3e3a69f1 2019-07-25 stsp }
6779 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6780 818c7501 2019-07-11 stsp }
6781 818c7501 2019-07-11 stsp return err;
6782 818c7501 2019-07-11 stsp }
6783 818c7501 2019-07-11 stsp
6784 818c7501 2019-07-11 stsp const struct got_error *
6785 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6786 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6787 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6788 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6789 818c7501 2019-07-11 stsp {
6790 818c7501 2019-07-11 stsp const struct got_error *err;
6791 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6792 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6793 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6794 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6795 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6796 818c7501 2019-07-11 stsp
6797 818c7501 2019-07-11 stsp *commit_id = NULL;
6798 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6799 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6800 3e3a69f1 2019-07-25 stsp *branch = NULL;
6801 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6802 3e3a69f1 2019-07-25 stsp
6803 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6804 3e3a69f1 2019-07-25 stsp if (err)
6805 3e3a69f1 2019-07-25 stsp return err;
6806 818c7501 2019-07-11 stsp
6807 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6808 3e3a69f1 2019-07-25 stsp if (err)
6809 f032f1f7 2019-08-04 stsp goto done;
6810 f032f1f7 2019-08-04 stsp
6811 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6812 f032f1f7 2019-08-04 stsp &have_staged_files);
6813 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6814 f032f1f7 2019-08-04 stsp goto done;
6815 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6816 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6817 3e3a69f1 2019-07-25 stsp goto done;
6818 f032f1f7 2019-08-04 stsp }
6819 3e3a69f1 2019-07-25 stsp
6820 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6821 818c7501 2019-07-11 stsp if (err)
6822 f032f1f7 2019-08-04 stsp goto done;
6823 818c7501 2019-07-11 stsp
6824 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6825 818c7501 2019-07-11 stsp if (err)
6826 818c7501 2019-07-11 stsp goto done;
6827 818c7501 2019-07-11 stsp
6828 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6829 818c7501 2019-07-11 stsp if (err)
6830 818c7501 2019-07-11 stsp goto done;
6831 818c7501 2019-07-11 stsp
6832 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6833 818c7501 2019-07-11 stsp if (err)
6834 818c7501 2019-07-11 stsp goto done;
6835 818c7501 2019-07-11 stsp
6836 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6837 818c7501 2019-07-11 stsp if (err)
6838 818c7501 2019-07-11 stsp goto done;
6839 818c7501 2019-07-11 stsp
6840 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6841 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6842 818c7501 2019-07-11 stsp if (err)
6843 818c7501 2019-07-11 stsp goto done;
6844 818c7501 2019-07-11 stsp
6845 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6846 818c7501 2019-07-11 stsp if (err)
6847 818c7501 2019-07-11 stsp goto done;
6848 818c7501 2019-07-11 stsp
6849 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6850 818c7501 2019-07-11 stsp if (err)
6851 818c7501 2019-07-11 stsp goto done;
6852 818c7501 2019-07-11 stsp
6853 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6854 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6855 818c7501 2019-07-11 stsp if (err)
6856 818c7501 2019-07-11 stsp goto done;
6857 818c7501 2019-07-11 stsp
6858 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6859 818c7501 2019-07-11 stsp if (err)
6860 818c7501 2019-07-11 stsp goto done;
6861 818c7501 2019-07-11 stsp done:
6862 818c7501 2019-07-11 stsp free(commit_ref_name);
6863 818c7501 2019-07-11 stsp free(branch_ref_name);
6864 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6865 818c7501 2019-07-11 stsp if (commit_ref)
6866 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6867 818c7501 2019-07-11 stsp if (branch_ref)
6868 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6869 818c7501 2019-07-11 stsp if (err) {
6870 818c7501 2019-07-11 stsp free(*commit_id);
6871 818c7501 2019-07-11 stsp *commit_id = NULL;
6872 818c7501 2019-07-11 stsp if (*tmp_branch) {
6873 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6874 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6875 818c7501 2019-07-11 stsp }
6876 818c7501 2019-07-11 stsp if (*new_base_branch) {
6877 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6878 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6879 818c7501 2019-07-11 stsp }
6880 818c7501 2019-07-11 stsp if (*branch) {
6881 818c7501 2019-07-11 stsp got_ref_close(*branch);
6882 818c7501 2019-07-11 stsp *branch = NULL;
6883 818c7501 2019-07-11 stsp }
6884 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6885 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6886 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6887 3e3a69f1 2019-07-25 stsp }
6888 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6889 818c7501 2019-07-11 stsp }
6890 818c7501 2019-07-11 stsp return err;
6891 c4296144 2019-05-09 stsp }
6892 818c7501 2019-07-11 stsp
6893 818c7501 2019-07-11 stsp const struct got_error *
6894 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6895 818c7501 2019-07-11 stsp {
6896 818c7501 2019-07-11 stsp const struct got_error *err;
6897 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6898 818c7501 2019-07-11 stsp
6899 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6900 818c7501 2019-07-11 stsp if (err)
6901 818c7501 2019-07-11 stsp return err;
6902 818c7501 2019-07-11 stsp
6903 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6904 818c7501 2019-07-11 stsp free(tmp_branch_name);
6905 818c7501 2019-07-11 stsp return NULL;
6906 818c7501 2019-07-11 stsp }
6907 818c7501 2019-07-11 stsp
6908 818c7501 2019-07-11 stsp static const struct got_error *
6909 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6910 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6911 818c7501 2019-07-11 stsp {
6912 0ebf8283 2019-07-24 stsp *logmsg = arg;
6913 818c7501 2019-07-11 stsp return NULL;
6914 818c7501 2019-07-11 stsp }
6915 818c7501 2019-07-11 stsp
6916 818c7501 2019-07-11 stsp static const struct got_error *
6917 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6918 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6919 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6920 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6921 818c7501 2019-07-11 stsp {
6922 818c7501 2019-07-11 stsp return NULL;
6923 01757395 2019-07-12 stsp }
6924 01757395 2019-07-12 stsp
6925 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6926 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6927 01757395 2019-07-12 stsp void *progress_arg;
6928 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6929 01757395 2019-07-12 stsp };
6930 01757395 2019-07-12 stsp
6931 01757395 2019-07-12 stsp static const struct got_error *
6932 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6933 01757395 2019-07-12 stsp {
6934 01757395 2019-07-12 stsp const struct got_error *err;
6935 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6936 01757395 2019-07-12 stsp char *p;
6937 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6938 01757395 2019-07-12 stsp
6939 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6940 01757395 2019-07-12 stsp if (err)
6941 01757395 2019-07-12 stsp return err;
6942 01757395 2019-07-12 stsp
6943 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6944 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6945 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6946 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6947 01757395 2019-07-12 stsp return NULL;
6948 01757395 2019-07-12 stsp
6949 01757395 2019-07-12 stsp p = strdup(path);
6950 01757395 2019-07-12 stsp if (p == NULL)
6951 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6952 01757395 2019-07-12 stsp
6953 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6954 01757395 2019-07-12 stsp if (err || new == NULL)
6955 01757395 2019-07-12 stsp free(p);
6956 01757395 2019-07-12 stsp return err;
6957 818c7501 2019-07-11 stsp }
6958 818c7501 2019-07-11 stsp
6959 0ebf8283 2019-07-24 stsp static const struct got_error *
6960 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6961 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6962 818c7501 2019-07-11 stsp {
6963 818c7501 2019-07-11 stsp const struct got_error *err;
6964 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6965 818c7501 2019-07-11 stsp
6966 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6967 818c7501 2019-07-11 stsp if (err) {
6968 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6969 818c7501 2019-07-11 stsp goto done;
6970 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6971 818c7501 2019-07-11 stsp if (err)
6972 818c7501 2019-07-11 stsp goto done;
6973 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6974 818c7501 2019-07-11 stsp if (err)
6975 818c7501 2019-07-11 stsp goto done;
6976 de05890f 2020-03-05 stsp } else if (is_rebase) {
6977 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6978 818c7501 2019-07-11 stsp int cmp;
6979 818c7501 2019-07-11 stsp
6980 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6981 818c7501 2019-07-11 stsp if (err)
6982 818c7501 2019-07-11 stsp goto done;
6983 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6984 818c7501 2019-07-11 stsp free(stored_id);
6985 818c7501 2019-07-11 stsp if (cmp != 0) {
6986 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6987 818c7501 2019-07-11 stsp goto done;
6988 818c7501 2019-07-11 stsp }
6989 818c7501 2019-07-11 stsp }
6990 0ebf8283 2019-07-24 stsp done:
6991 0ebf8283 2019-07-24 stsp if (commit_ref)
6992 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6993 0ebf8283 2019-07-24 stsp return err;
6994 0ebf8283 2019-07-24 stsp }
6995 0ebf8283 2019-07-24 stsp
6996 0ebf8283 2019-07-24 stsp static const struct got_error *
6997 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6998 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6999 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7000 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7001 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7002 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7003 0ebf8283 2019-07-24 stsp {
7004 0ebf8283 2019-07-24 stsp const struct got_error *err;
7005 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7006 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7007 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7008 818c7501 2019-07-11 stsp
7009 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7010 0ebf8283 2019-07-24 stsp
7011 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7012 0ebf8283 2019-07-24 stsp if (err)
7013 0ebf8283 2019-07-24 stsp return err;
7014 0ebf8283 2019-07-24 stsp
7015 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7016 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7017 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7018 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7019 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7020 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7021 818c7501 2019-07-11 stsp if (commit_ref)
7022 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7023 818c7501 2019-07-11 stsp return err;
7024 818c7501 2019-07-11 stsp }
7025 818c7501 2019-07-11 stsp
7026 818c7501 2019-07-11 stsp const struct got_error *
7027 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7028 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7029 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7030 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7031 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7032 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7033 818c7501 2019-07-11 stsp {
7034 0ebf8283 2019-07-24 stsp const struct got_error *err;
7035 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7036 0ebf8283 2019-07-24 stsp
7037 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7038 0ebf8283 2019-07-24 stsp if (err)
7039 0ebf8283 2019-07-24 stsp return err;
7040 0ebf8283 2019-07-24 stsp
7041 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7042 0ebf8283 2019-07-24 stsp if (err)
7043 0ebf8283 2019-07-24 stsp goto done;
7044 0ebf8283 2019-07-24 stsp
7045 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7046 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7047 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7048 0ebf8283 2019-07-24 stsp done:
7049 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7050 0ebf8283 2019-07-24 stsp return err;
7051 0ebf8283 2019-07-24 stsp }
7052 0ebf8283 2019-07-24 stsp
7053 0ebf8283 2019-07-24 stsp const struct got_error *
7054 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7055 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7056 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7057 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7058 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7059 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7060 0ebf8283 2019-07-24 stsp {
7061 0ebf8283 2019-07-24 stsp const struct got_error *err;
7062 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7063 0ebf8283 2019-07-24 stsp
7064 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7065 0ebf8283 2019-07-24 stsp if (err)
7066 0ebf8283 2019-07-24 stsp return err;
7067 0ebf8283 2019-07-24 stsp
7068 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7069 0ebf8283 2019-07-24 stsp if (err)
7070 0ebf8283 2019-07-24 stsp goto done;
7071 0ebf8283 2019-07-24 stsp
7072 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7073 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7074 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7075 0ebf8283 2019-07-24 stsp done:
7076 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7077 0ebf8283 2019-07-24 stsp return err;
7078 0ebf8283 2019-07-24 stsp }
7079 0ebf8283 2019-07-24 stsp
7080 0ebf8283 2019-07-24 stsp static const struct got_error *
7081 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7082 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7083 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7084 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7085 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7086 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7087 0ebf8283 2019-07-24 stsp {
7088 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7089 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7090 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7091 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7092 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7093 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7094 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7095 818c7501 2019-07-11 stsp
7096 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7097 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7098 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7099 a0e95631 2019-07-12 stsp
7100 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7101 818c7501 2019-07-11 stsp
7102 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7103 a0e95631 2019-07-12 stsp if (err)
7104 3e3a69f1 2019-07-25 stsp return err;
7105 a0e95631 2019-07-12 stsp
7106 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7107 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7108 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7109 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7110 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7111 01757395 2019-07-12 stsp /*
7112 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7113 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7114 6c13b005 2021-09-02 stsp *
7115 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7116 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7117 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7118 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7119 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7120 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7121 01757395 2019-07-12 stsp */
7122 01757395 2019-07-12 stsp if (merged_paths) {
7123 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7124 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7125 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7126 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7127 f2a9dc41 2019-12-13 tracey 0);
7128 01757395 2019-07-12 stsp if (err)
7129 01757395 2019-07-12 stsp goto done;
7130 01757395 2019-07-12 stsp }
7131 01757395 2019-07-12 stsp } else {
7132 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7133 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7134 01757395 2019-07-12 stsp if (err)
7135 01757395 2019-07-12 stsp goto done;
7136 01757395 2019-07-12 stsp }
7137 a0e95631 2019-07-12 stsp
7138 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7139 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7140 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7141 a0e95631 2019-07-12 stsp if (err)
7142 a0e95631 2019-07-12 stsp goto done;
7143 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7144 a0e95631 2019-07-12 stsp goto done;
7145 ff0d2220 2019-07-11 stsp }
7146 818c7501 2019-07-11 stsp
7147 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7148 edd02c5e 2019-07-11 stsp if (err)
7149 edd02c5e 2019-07-11 stsp goto done;
7150 edd02c5e 2019-07-11 stsp
7151 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7152 818c7501 2019-07-11 stsp if (err)
7153 818c7501 2019-07-11 stsp goto done;
7154 818c7501 2019-07-11 stsp
7155 5943eee2 2019-08-13 stsp if (new_logmsg) {
7156 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7157 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7158 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7159 5943eee2 2019-08-13 stsp goto done;
7160 5943eee2 2019-08-13 stsp }
7161 5943eee2 2019-08-13 stsp } else {
7162 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7163 5943eee2 2019-08-13 stsp if (err)
7164 5943eee2 2019-08-13 stsp goto done;
7165 5943eee2 2019-08-13 stsp }
7166 0ebf8283 2019-07-24 stsp
7167 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7168 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7169 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7170 8db00f97 2022-07-22 thomas committer ? committer :
7171 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7172 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7173 a0e95631 2019-07-12 stsp if (err)
7174 a0e95631 2019-07-12 stsp goto done;
7175 a0e95631 2019-07-12 stsp
7176 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7177 a0e95631 2019-07-12 stsp if (err)
7178 a0e95631 2019-07-12 stsp goto done;
7179 a0e95631 2019-07-12 stsp
7180 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7181 a0e95631 2019-07-12 stsp if (err)
7182 a0e95631 2019-07-12 stsp goto done;
7183 a0e95631 2019-07-12 stsp
7184 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7185 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7186 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7187 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7188 a0e95631 2019-07-12 stsp err = sync_err;
7189 818c7501 2019-07-11 stsp done:
7190 a0e95631 2019-07-12 stsp free(fileindex_path);
7191 a0e95631 2019-07-12 stsp free(head_commit_id);
7192 a0e95631 2019-07-12 stsp if (head_ref)
7193 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7194 818c7501 2019-07-11 stsp if (err) {
7195 818c7501 2019-07-11 stsp free(*new_commit_id);
7196 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7197 818c7501 2019-07-11 stsp }
7198 818c7501 2019-07-11 stsp return err;
7199 818c7501 2019-07-11 stsp }
7200 818c7501 2019-07-11 stsp
7201 818c7501 2019-07-11 stsp const struct got_error *
7202 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7203 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7204 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7205 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7206 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7207 be94c032 2023-02-20 thomas struct got_repository *repo)
7208 0ebf8283 2019-07-24 stsp {
7209 0ebf8283 2019-07-24 stsp const struct got_error *err;
7210 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7211 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7212 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7213 0ebf8283 2019-07-24 stsp
7214 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7215 0ebf8283 2019-07-24 stsp if (err)
7216 0ebf8283 2019-07-24 stsp return err;
7217 0ebf8283 2019-07-24 stsp
7218 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7219 0ebf8283 2019-07-24 stsp if (err)
7220 0ebf8283 2019-07-24 stsp goto done;
7221 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7222 0ebf8283 2019-07-24 stsp if (err)
7223 0ebf8283 2019-07-24 stsp goto done;
7224 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7225 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7226 0ebf8283 2019-07-24 stsp goto done;
7227 0ebf8283 2019-07-24 stsp }
7228 0ebf8283 2019-07-24 stsp
7229 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7230 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7231 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7232 0ebf8283 2019-07-24 stsp done:
7233 0ebf8283 2019-07-24 stsp if (commit_ref)
7234 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7235 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7236 0ebf8283 2019-07-24 stsp free(commit_id);
7237 0ebf8283 2019-07-24 stsp return err;
7238 0ebf8283 2019-07-24 stsp }
7239 0ebf8283 2019-07-24 stsp
7240 0ebf8283 2019-07-24 stsp const struct got_error *
7241 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7242 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7243 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7244 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7245 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7246 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7247 0ebf8283 2019-07-24 stsp {
7248 0ebf8283 2019-07-24 stsp const struct got_error *err;
7249 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7250 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7251 0ebf8283 2019-07-24 stsp
7252 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7253 0ebf8283 2019-07-24 stsp if (err)
7254 0ebf8283 2019-07-24 stsp return err;
7255 0ebf8283 2019-07-24 stsp
7256 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7257 0ebf8283 2019-07-24 stsp if (err)
7258 0ebf8283 2019-07-24 stsp goto done;
7259 0ebf8283 2019-07-24 stsp
7260 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7261 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7262 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7263 0ebf8283 2019-07-24 stsp done:
7264 0ebf8283 2019-07-24 stsp if (commit_ref)
7265 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7266 0ebf8283 2019-07-24 stsp free(commit_ref_name);
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 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7272 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7273 818c7501 2019-07-11 stsp {
7274 3e3a69f1 2019-07-25 stsp if (fileindex)
7275 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7276 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7277 69844fba 2019-07-11 stsp }
7278 69844fba 2019-07-11 stsp
7279 69844fba 2019-07-11 stsp static const struct got_error *
7280 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7281 69844fba 2019-07-11 stsp {
7282 69844fba 2019-07-11 stsp const struct got_error *err;
7283 69844fba 2019-07-11 stsp struct got_reference *ref;
7284 69844fba 2019-07-11 stsp
7285 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7286 69844fba 2019-07-11 stsp if (err) {
7287 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7288 69844fba 2019-07-11 stsp return NULL;
7289 69844fba 2019-07-11 stsp return err;
7290 69844fba 2019-07-11 stsp }
7291 69844fba 2019-07-11 stsp
7292 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7293 69844fba 2019-07-11 stsp got_ref_close(ref);
7294 69844fba 2019-07-11 stsp return err;
7295 818c7501 2019-07-11 stsp }
7296 818c7501 2019-07-11 stsp
7297 69844fba 2019-07-11 stsp static const struct got_error *
7298 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7299 69844fba 2019-07-11 stsp {
7300 69844fba 2019-07-11 stsp const struct got_error *err;
7301 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7302 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7303 69844fba 2019-07-11 stsp
7304 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7305 69844fba 2019-07-11 stsp if (err)
7306 69844fba 2019-07-11 stsp goto done;
7307 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7308 69844fba 2019-07-11 stsp if (err)
7309 69844fba 2019-07-11 stsp goto done;
7310 69844fba 2019-07-11 stsp
7311 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7312 69844fba 2019-07-11 stsp if (err)
7313 69844fba 2019-07-11 stsp goto done;
7314 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7315 69844fba 2019-07-11 stsp if (err)
7316 69844fba 2019-07-11 stsp goto done;
7317 69844fba 2019-07-11 stsp
7318 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7319 69844fba 2019-07-11 stsp if (err)
7320 69844fba 2019-07-11 stsp goto done;
7321 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7322 69844fba 2019-07-11 stsp if (err)
7323 69844fba 2019-07-11 stsp goto done;
7324 69844fba 2019-07-11 stsp
7325 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7326 69844fba 2019-07-11 stsp if (err)
7327 69844fba 2019-07-11 stsp goto done;
7328 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7329 69844fba 2019-07-11 stsp if (err)
7330 69844fba 2019-07-11 stsp goto done;
7331 69844fba 2019-07-11 stsp
7332 69844fba 2019-07-11 stsp done:
7333 69844fba 2019-07-11 stsp free(tmp_branch_name);
7334 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7335 69844fba 2019-07-11 stsp free(branch_ref_name);
7336 69844fba 2019-07-11 stsp free(commit_ref_name);
7337 e600f124 2021-03-21 stsp return err;
7338 e600f124 2021-03-21 stsp }
7339 e600f124 2021-03-21 stsp
7340 ef20f542 2022-06-26 thomas static const struct got_error *
7341 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7342 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7343 e600f124 2021-03-21 stsp {
7344 e600f124 2021-03-21 stsp const struct got_error *err;
7345 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7346 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7347 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7348 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7349 e600f124 2021-03-21 stsp char *refname = NULL;
7350 e600f124 2021-03-21 stsp
7351 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7352 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7353 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7354 e600f124 2021-03-21 stsp branch_name += 11;
7355 e600f124 2021-03-21 stsp
7356 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7357 e600f124 2021-03-21 stsp if (err)
7358 e600f124 2021-03-21 stsp return err;
7359 e600f124 2021-03-21 stsp
7360 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7361 e600f124 2021-03-21 stsp new_id_str) == -1) {
7362 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7363 e600f124 2021-03-21 stsp goto done;
7364 e600f124 2021-03-21 stsp }
7365 e600f124 2021-03-21 stsp
7366 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7367 e600f124 2021-03-21 stsp if (err)
7368 e600f124 2021-03-21 stsp goto done;
7369 e600f124 2021-03-21 stsp
7370 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7371 e600f124 2021-03-21 stsp if (err)
7372 e600f124 2021-03-21 stsp goto done;
7373 e600f124 2021-03-21 stsp
7374 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7375 e600f124 2021-03-21 stsp done:
7376 e600f124 2021-03-21 stsp free(new_id_str);
7377 e600f124 2021-03-21 stsp free(refname);
7378 e600f124 2021-03-21 stsp free(old_commit_id);
7379 e600f124 2021-03-21 stsp if (ref)
7380 e600f124 2021-03-21 stsp got_ref_close(ref);
7381 69844fba 2019-07-11 stsp return err;
7382 69844fba 2019-07-11 stsp }
7383 69844fba 2019-07-11 stsp
7384 818c7501 2019-07-11 stsp const struct got_error *
7385 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7386 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7387 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7388 f08f28be 2023-02-03 thomas int create_backup)
7389 818c7501 2019-07-11 stsp {
7390 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7391 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7392 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7393 818c7501 2019-07-11 stsp
7394 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7395 818c7501 2019-07-11 stsp if (err)
7396 818c7501 2019-07-11 stsp return err;
7397 e600f124 2021-03-21 stsp
7398 e600f124 2021-03-21 stsp if (create_backup) {
7399 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7400 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7401 e600f124 2021-03-21 stsp if (err)
7402 e600f124 2021-03-21 stsp goto done;
7403 e600f124 2021-03-21 stsp }
7404 818c7501 2019-07-11 stsp
7405 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7406 818c7501 2019-07-11 stsp if (err)
7407 818c7501 2019-07-11 stsp goto done;
7408 818c7501 2019-07-11 stsp
7409 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7410 818c7501 2019-07-11 stsp if (err)
7411 818c7501 2019-07-11 stsp goto done;
7412 818c7501 2019-07-11 stsp
7413 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7414 818c7501 2019-07-11 stsp if (err)
7415 818c7501 2019-07-11 stsp goto done;
7416 818c7501 2019-07-11 stsp
7417 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7418 a615e0e7 2020-12-16 stsp if (err)
7419 a615e0e7 2020-12-16 stsp goto done;
7420 a615e0e7 2020-12-16 stsp
7421 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7422 a615e0e7 2020-12-16 stsp if (err)
7423 a615e0e7 2020-12-16 stsp goto done;
7424 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7425 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7426 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7427 a615e0e7 2020-12-16 stsp err = sync_err;
7428 818c7501 2019-07-11 stsp done:
7429 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7430 a615e0e7 2020-12-16 stsp free(fileindex_path);
7431 818c7501 2019-07-11 stsp free(new_head_commit_id);
7432 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7433 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7434 818c7501 2019-07-11 stsp err = unlockerr;
7435 818c7501 2019-07-11 stsp return err;
7436 818c7501 2019-07-11 stsp }
7437 8641a332 2023-04-14 thomas
7438 8641a332 2023-04-14 thomas static const struct got_error *
7439 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7440 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7441 8641a332 2023-04-14 thomas struct got_repository *repo)
7442 8641a332 2023-04-14 thomas {
7443 8641a332 2023-04-14 thomas const struct got_error *err;
7444 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7445 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7446 8641a332 2023-04-14 thomas
7447 8641a332 2023-04-14 thomas if (id1) {
7448 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7449 8641a332 2023-04-14 thomas if (err)
7450 8641a332 2023-04-14 thomas goto done;
7451 8641a332 2023-04-14 thomas
7452 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7453 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7454 8641a332 2023-04-14 thomas if (err)
7455 8641a332 2023-04-14 thomas goto done;
7456 8641a332 2023-04-14 thomas }
7457 818c7501 2019-07-11 stsp
7458 8641a332 2023-04-14 thomas if (id2) {
7459 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7460 8641a332 2023-04-14 thomas if (err)
7461 8641a332 2023-04-14 thomas goto done;
7462 8641a332 2023-04-14 thomas
7463 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7464 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7465 8641a332 2023-04-14 thomas if (err)
7466 8641a332 2023-04-14 thomas goto done;
7467 8641a332 2023-04-14 thomas }
7468 8641a332 2023-04-14 thomas
7469 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7470 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7471 8641a332 2023-04-14 thomas if (err)
7472 8641a332 2023-04-14 thomas goto done;
7473 8641a332 2023-04-14 thomas done:
7474 8641a332 2023-04-14 thomas if (commit1)
7475 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7476 8641a332 2023-04-14 thomas if (commit2)
7477 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7478 8641a332 2023-04-14 thomas if (tree1)
7479 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7480 8641a332 2023-04-14 thomas if (tree2)
7481 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7482 8641a332 2023-04-14 thomas return err;
7483 8641a332 2023-04-14 thomas }
7484 8641a332 2023-04-14 thomas
7485 8641a332 2023-04-14 thomas static const struct got_error *
7486 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7487 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7488 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7489 8641a332 2023-04-14 thomas {
7490 8641a332 2023-04-14 thomas const struct got_error *err;
7491 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7492 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7493 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7494 8641a332 2023-04-14 thomas
7495 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7496 8641a332 2023-04-14 thomas
7497 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7498 8641a332 2023-04-14 thomas if (err)
7499 8641a332 2023-04-14 thomas goto done;
7500 8641a332 2023-04-14 thomas
7501 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7502 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7503 8641a332 2023-04-14 thomas
7504 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7505 8641a332 2023-04-14 thomas continue;
7506 8641a332 2023-04-14 thomas
7507 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7508 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7509 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7510 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7511 8641a332 2023-04-14 thomas goto done;
7512 8641a332 2023-04-14 thomas }
7513 8641a332 2023-04-14 thomas } else {
7514 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7515 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7516 8641a332 2023-04-14 thomas goto done;
7517 8641a332 2023-04-14 thomas }
7518 8641a332 2023-04-14 thomas
7519 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7520 8641a332 2023-04-14 thomas path_prefix, abspath);
7521 8641a332 2023-04-14 thomas if (err)
7522 8641a332 2023-04-14 thomas goto done;
7523 8641a332 2023-04-14 thomas free(abspath);
7524 8641a332 2023-04-14 thomas abspath = NULL;
7525 8641a332 2023-04-14 thomas }
7526 8641a332 2023-04-14 thomas
7527 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7528 8641a332 2023-04-14 thomas if (err)
7529 8641a332 2023-04-14 thomas goto done;
7530 8641a332 2023-04-14 thomas wt_path = NULL;
7531 8641a332 2023-04-14 thomas }
7532 8641a332 2023-04-14 thomas
7533 8641a332 2023-04-14 thomas done:
7534 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7535 8641a332 2023-04-14 thomas free(abspath);
7536 8641a332 2023-04-14 thomas free(wt_path);
7537 8641a332 2023-04-14 thomas return err;
7538 8641a332 2023-04-14 thomas }
7539 8641a332 2023-04-14 thomas
7540 8641a332 2023-04-14 thomas static const struct got_error *
7541 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7542 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7543 8641a332 2023-04-14 thomas struct got_repository *repo)
7544 8641a332 2023-04-14 thomas {
7545 8641a332 2023-04-14 thomas const struct got_error *err;
7546 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7547 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7548 8641a332 2023-04-14 thomas
7549 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7550 8641a332 2023-04-14 thomas if (err)
7551 8641a332 2023-04-14 thomas goto done;
7552 8641a332 2023-04-14 thomas
7553 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7554 8641a332 2023-04-14 thomas
7555 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7556 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7557 8641a332 2023-04-14 thomas if (err)
7558 8641a332 2023-04-14 thomas goto done;
7559 8641a332 2023-04-14 thomas done:
7560 8641a332 2023-04-14 thomas if (commit)
7561 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7562 8641a332 2023-04-14 thomas return err;
7563 8641a332 2023-04-14 thomas }
7564 8641a332 2023-04-14 thomas
7565 818c7501 2019-07-11 stsp const struct got_error *
7566 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7567 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7568 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7569 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7570 818c7501 2019-07-11 stsp {
7571 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7572 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7573 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7574 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7575 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7576 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7577 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7578 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7579 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7580 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7581 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7582 818c7501 2019-07-11 stsp
7583 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7584 8641a332 2023-04-14 thomas
7585 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7586 818c7501 2019-07-11 stsp if (err)
7587 818c7501 2019-07-11 stsp return err;
7588 8641a332 2023-04-14 thomas
7589 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7590 8641a332 2023-04-14 thomas if (err)
7591 8641a332 2023-04-14 thomas goto done;
7592 8641a332 2023-04-14 thomas
7593 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7594 8641a332 2023-04-14 thomas if (err)
7595 8641a332 2023-04-14 thomas goto done;
7596 8641a332 2023-04-14 thomas
7597 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7598 8641a332 2023-04-14 thomas if (err)
7599 8641a332 2023-04-14 thomas goto done;
7600 945f9229 2022-04-16 thomas
7601 8641a332 2023-04-14 thomas /*
7602 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7603 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7604 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7605 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7606 8641a332 2023-04-14 thomas */
7607 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7608 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7609 8641a332 2023-04-14 thomas if (err)
7610 8641a332 2023-04-14 thomas goto done;
7611 8641a332 2023-04-14 thomas
7612 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7613 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7614 818c7501 2019-07-11 stsp if (err)
7615 818c7501 2019-07-11 stsp goto done;
7616 818c7501 2019-07-11 stsp
7617 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7618 818c7501 2019-07-11 stsp if (err)
7619 818c7501 2019-07-11 stsp goto done;
7620 818c7501 2019-07-11 stsp
7621 818c7501 2019-07-11 stsp /*
7622 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7623 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7624 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7625 818c7501 2019-07-11 stsp */
7626 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7627 818c7501 2019-07-11 stsp if (err)
7628 818c7501 2019-07-11 stsp goto done;
7629 818c7501 2019-07-11 stsp
7630 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7631 25a102ed 2023-04-14 thomas if (err)
7632 25a102ed 2023-04-14 thomas goto done;
7633 25a102ed 2023-04-14 thomas
7634 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7635 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7636 818c7501 2019-07-11 stsp if (err)
7637 818c7501 2019-07-11 stsp goto done;
7638 818c7501 2019-07-11 stsp
7639 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7640 945f9229 2022-04-16 thomas worktree->path_prefix);
7641 ca355955 2019-07-12 stsp if (err)
7642 ca355955 2019-07-12 stsp goto done;
7643 ca355955 2019-07-12 stsp
7644 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7645 ca355955 2019-07-12 stsp if (err)
7646 ca355955 2019-07-12 stsp goto done;
7647 ca355955 2019-07-12 stsp
7648 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7649 818c7501 2019-07-11 stsp if (err)
7650 818c7501 2019-07-11 stsp goto done;
7651 818c7501 2019-07-11 stsp
7652 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7653 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7654 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7655 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7656 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7657 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7658 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7659 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7660 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7661 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7662 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7663 55bd499d 2019-07-12 stsp if (err)
7664 1f1abb7e 2019-08-08 stsp goto sync;
7665 55bd499d 2019-07-12 stsp
7666 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7667 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7668 ca355955 2019-07-12 stsp sync:
7669 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7670 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7671 ca355955 2019-07-12 stsp err = sync_err;
7672 818c7501 2019-07-11 stsp done:
7673 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7674 818c7501 2019-07-11 stsp got_ref_close(resolved);
7675 a3a2faf2 2019-07-12 stsp free(tree_id);
7676 818c7501 2019-07-11 stsp free(commit_id);
7677 8641a332 2023-04-14 thomas free(merged_commit_id);
7678 945f9229 2022-04-16 thomas if (commit)
7679 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7680 0ebf8283 2019-07-24 stsp if (fileindex)
7681 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7682 0ebf8283 2019-07-24 stsp free(fileindex_path);
7683 8641a332 2023-04-14 thomas free(commit_ref_name);
7684 8641a332 2023-04-14 thomas if (commit_ref)
7685 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7686 0ebf8283 2019-07-24 stsp
7687 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7688 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7689 0ebf8283 2019-07-24 stsp err = unlockerr;
7690 0ebf8283 2019-07-24 stsp return err;
7691 0ebf8283 2019-07-24 stsp }
7692 0ebf8283 2019-07-24 stsp
7693 0ebf8283 2019-07-24 stsp const struct got_error *
7694 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7695 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7696 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7697 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7698 0ebf8283 2019-07-24 stsp {
7699 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7700 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7701 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7702 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7703 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7704 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7705 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7706 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7707 0ebf8283 2019-07-24 stsp
7708 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7709 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7710 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7711 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7712 0ebf8283 2019-07-24 stsp
7713 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7714 0ebf8283 2019-07-24 stsp if (err)
7715 0ebf8283 2019-07-24 stsp return err;
7716 0ebf8283 2019-07-24 stsp
7717 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7718 0ebf8283 2019-07-24 stsp if (err)
7719 0ebf8283 2019-07-24 stsp goto done;
7720 0ebf8283 2019-07-24 stsp
7721 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7722 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7723 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7724 0ebf8283 2019-07-24 stsp &ok_arg);
7725 0ebf8283 2019-07-24 stsp if (err)
7726 0ebf8283 2019-07-24 stsp goto done;
7727 0ebf8283 2019-07-24 stsp
7728 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7729 0ebf8283 2019-07-24 stsp if (err)
7730 0ebf8283 2019-07-24 stsp goto done;
7731 0ebf8283 2019-07-24 stsp
7732 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7733 0ebf8283 2019-07-24 stsp if (err)
7734 0ebf8283 2019-07-24 stsp goto done;
7735 0ebf8283 2019-07-24 stsp
7736 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7737 0ebf8283 2019-07-24 stsp worktree);
7738 0ebf8283 2019-07-24 stsp if (err)
7739 0ebf8283 2019-07-24 stsp goto done;
7740 0ebf8283 2019-07-24 stsp
7741 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7742 0ebf8283 2019-07-24 stsp 0);
7743 0ebf8283 2019-07-24 stsp if (err)
7744 0ebf8283 2019-07-24 stsp goto done;
7745 0ebf8283 2019-07-24 stsp
7746 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7747 0ebf8283 2019-07-24 stsp if (err)
7748 0ebf8283 2019-07-24 stsp goto done;
7749 0ebf8283 2019-07-24 stsp
7750 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7751 0ebf8283 2019-07-24 stsp if (err)
7752 0ebf8283 2019-07-24 stsp goto done;
7753 0ebf8283 2019-07-24 stsp
7754 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7755 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7756 0ebf8283 2019-07-24 stsp if (err)
7757 0ebf8283 2019-07-24 stsp goto done;
7758 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7759 0ebf8283 2019-07-24 stsp if (err)
7760 0ebf8283 2019-07-24 stsp goto done;
7761 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7762 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7763 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7764 0ebf8283 2019-07-24 stsp goto done;
7765 0ebf8283 2019-07-24 stsp }
7766 0ebf8283 2019-07-24 stsp
7767 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7768 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7769 0ebf8283 2019-07-24 stsp if (err)
7770 0ebf8283 2019-07-24 stsp goto done;
7771 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7772 0ebf8283 2019-07-24 stsp if (err)
7773 0ebf8283 2019-07-24 stsp goto done;
7774 0ebf8283 2019-07-24 stsp
7775 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7776 0ebf8283 2019-07-24 stsp if (err)
7777 0ebf8283 2019-07-24 stsp goto done;
7778 0ebf8283 2019-07-24 stsp done:
7779 0ebf8283 2019-07-24 stsp free(fileindex_path);
7780 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7781 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7782 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7783 0ebf8283 2019-07-24 stsp if (wt_branch)
7784 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7785 0ebf8283 2019-07-24 stsp if (err) {
7786 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7787 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7788 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7789 0ebf8283 2019-07-24 stsp }
7790 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7791 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7792 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7793 0ebf8283 2019-07-24 stsp }
7794 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7795 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7796 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7797 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7798 3e3a69f1 2019-07-25 stsp }
7799 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7800 0ebf8283 2019-07-24 stsp }
7801 0ebf8283 2019-07-24 stsp return err;
7802 0ebf8283 2019-07-24 stsp }
7803 0ebf8283 2019-07-24 stsp
7804 0ebf8283 2019-07-24 stsp const struct got_error *
7805 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7806 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7807 0ebf8283 2019-07-24 stsp {
7808 3e3a69f1 2019-07-25 stsp if (fileindex)
7809 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7810 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7811 0ebf8283 2019-07-24 stsp }
7812 0ebf8283 2019-07-24 stsp
7813 0ebf8283 2019-07-24 stsp const struct got_error *
7814 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7815 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7816 0ebf8283 2019-07-24 stsp {
7817 0ebf8283 2019-07-24 stsp const struct got_error *err;
7818 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7819 0ebf8283 2019-07-24 stsp
7820 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7821 0ebf8283 2019-07-24 stsp if (err)
7822 0ebf8283 2019-07-24 stsp return err;
7823 0ebf8283 2019-07-24 stsp
7824 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7825 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7826 0ebf8283 2019-07-24 stsp return NULL;
7827 0ebf8283 2019-07-24 stsp }
7828 0ebf8283 2019-07-24 stsp
7829 0ebf8283 2019-07-24 stsp const struct got_error *
7830 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7831 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7832 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7833 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7834 0ebf8283 2019-07-24 stsp {
7835 0ebf8283 2019-07-24 stsp const struct got_error *err;
7836 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7837 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7838 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7839 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7840 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7841 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7842 0ebf8283 2019-07-24 stsp
7843 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7844 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7845 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7846 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7847 0ebf8283 2019-07-24 stsp
7848 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7849 3e3a69f1 2019-07-25 stsp if (err)
7850 3e3a69f1 2019-07-25 stsp return err;
7851 3e3a69f1 2019-07-25 stsp
7852 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7853 3e3a69f1 2019-07-25 stsp if (err)
7854 f032f1f7 2019-08-04 stsp goto done;
7855 f032f1f7 2019-08-04 stsp
7856 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7857 f032f1f7 2019-08-04 stsp &have_staged_files);
7858 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7859 f032f1f7 2019-08-04 stsp goto done;
7860 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7861 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7862 3e3a69f1 2019-07-25 stsp goto done;
7863 f032f1f7 2019-08-04 stsp }
7864 3e3a69f1 2019-07-25 stsp
7865 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7866 0ebf8283 2019-07-24 stsp if (err)
7867 f032f1f7 2019-08-04 stsp goto done;
7868 0ebf8283 2019-07-24 stsp
7869 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7870 0ebf8283 2019-07-24 stsp if (err)
7871 0ebf8283 2019-07-24 stsp goto done;
7872 0ebf8283 2019-07-24 stsp
7873 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7874 0ebf8283 2019-07-24 stsp if (err)
7875 0ebf8283 2019-07-24 stsp goto done;
7876 0ebf8283 2019-07-24 stsp
7877 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7878 0ebf8283 2019-07-24 stsp worktree);
7879 0ebf8283 2019-07-24 stsp if (err)
7880 0ebf8283 2019-07-24 stsp goto done;
7881 0ebf8283 2019-07-24 stsp
7882 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7883 0ebf8283 2019-07-24 stsp if (err)
7884 0ebf8283 2019-07-24 stsp goto done;
7885 0ebf8283 2019-07-24 stsp
7886 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7887 0ebf8283 2019-07-24 stsp if (err)
7888 0ebf8283 2019-07-24 stsp goto done;
7889 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7890 0ebf8283 2019-07-24 stsp if (err)
7891 0ebf8283 2019-07-24 stsp goto done;
7892 0ebf8283 2019-07-24 stsp
7893 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7894 0ebf8283 2019-07-24 stsp if (err)
7895 0ebf8283 2019-07-24 stsp goto done;
7896 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7897 0ebf8283 2019-07-24 stsp if (err)
7898 0ebf8283 2019-07-24 stsp goto done;
7899 0ebf8283 2019-07-24 stsp
7900 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7901 0ebf8283 2019-07-24 stsp if (err)
7902 0ebf8283 2019-07-24 stsp goto done;
7903 0ebf8283 2019-07-24 stsp done:
7904 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7905 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7906 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7907 0ebf8283 2019-07-24 stsp if (commit_ref)
7908 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7909 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7910 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7911 0ebf8283 2019-07-24 stsp if (err) {
7912 0ebf8283 2019-07-24 stsp free(*commit_id);
7913 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7914 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7915 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7916 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7917 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7918 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7919 0ebf8283 2019-07-24 stsp }
7920 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7921 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7922 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7923 3e3a69f1 2019-07-25 stsp }
7924 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7925 0ebf8283 2019-07-24 stsp }
7926 0ebf8283 2019-07-24 stsp return err;
7927 0ebf8283 2019-07-24 stsp }
7928 0ebf8283 2019-07-24 stsp
7929 0ebf8283 2019-07-24 stsp static const struct got_error *
7930 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7931 0ebf8283 2019-07-24 stsp {
7932 0ebf8283 2019-07-24 stsp const struct got_error *err;
7933 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7934 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7935 0ebf8283 2019-07-24 stsp
7936 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7937 0ebf8283 2019-07-24 stsp if (err)
7938 0ebf8283 2019-07-24 stsp goto done;
7939 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7940 0ebf8283 2019-07-24 stsp if (err)
7941 0ebf8283 2019-07-24 stsp goto done;
7942 0ebf8283 2019-07-24 stsp
7943 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7944 0ebf8283 2019-07-24 stsp worktree);
7945 0ebf8283 2019-07-24 stsp if (err)
7946 0ebf8283 2019-07-24 stsp goto done;
7947 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7948 0ebf8283 2019-07-24 stsp if (err)
7949 0ebf8283 2019-07-24 stsp goto done;
7950 0ebf8283 2019-07-24 stsp
7951 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7952 0ebf8283 2019-07-24 stsp if (err)
7953 0ebf8283 2019-07-24 stsp goto done;
7954 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7955 0ebf8283 2019-07-24 stsp if (err)
7956 0ebf8283 2019-07-24 stsp goto done;
7957 0ebf8283 2019-07-24 stsp
7958 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7959 0ebf8283 2019-07-24 stsp if (err)
7960 0ebf8283 2019-07-24 stsp goto done;
7961 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7962 0ebf8283 2019-07-24 stsp if (err)
7963 0ebf8283 2019-07-24 stsp goto done;
7964 0ebf8283 2019-07-24 stsp done:
7965 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7966 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7967 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7968 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7969 0ebf8283 2019-07-24 stsp return err;
7970 0ebf8283 2019-07-24 stsp }
7971 0ebf8283 2019-07-24 stsp
7972 0ebf8283 2019-07-24 stsp const struct got_error *
7973 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7974 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7975 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7976 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7977 0ebf8283 2019-07-24 stsp {
7978 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7979 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7980 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7981 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7982 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7983 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7984 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7985 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7986 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7987 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7988 0ebf8283 2019-07-24 stsp
7989 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7990 8641a332 2023-04-14 thomas
7991 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7992 0ebf8283 2019-07-24 stsp if (err)
7993 0ebf8283 2019-07-24 stsp return err;
7994 945f9229 2022-04-16 thomas
7995 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7996 8641a332 2023-04-14 thomas if (err)
7997 8641a332 2023-04-14 thomas goto done;
7998 8641a332 2023-04-14 thomas
7999 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8000 8641a332 2023-04-14 thomas if (err) {
8001 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
8002 8641a332 2023-04-14 thomas goto done;
8003 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
8004 8641a332 2023-04-14 thomas commit_ref = NULL;
8005 8641a332 2023-04-14 thomas }
8006 8641a332 2023-04-14 thomas
8007 8641a332 2023-04-14 thomas if (commit_ref) {
8008 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8009 8641a332 2023-04-14 thomas if (err)
8010 8641a332 2023-04-14 thomas goto done;
8011 8641a332 2023-04-14 thomas
8012 8641a332 2023-04-14 thomas /*
8013 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8014 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8015 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
8016 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
8017 8641a332 2023-04-14 thomas */
8018 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8019 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8020 8641a332 2023-04-14 thomas if (err)
8021 8641a332 2023-04-14 thomas goto done;
8022 8641a332 2023-04-14 thomas }
8023 8641a332 2023-04-14 thomas
8024 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8025 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8026 0ebf8283 2019-07-24 stsp if (err)
8027 0ebf8283 2019-07-24 stsp goto done;
8028 0ebf8283 2019-07-24 stsp
8029 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8030 0ebf8283 2019-07-24 stsp if (err)
8031 0ebf8283 2019-07-24 stsp goto done;
8032 0ebf8283 2019-07-24 stsp
8033 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8034 0ebf8283 2019-07-24 stsp if (err)
8035 0ebf8283 2019-07-24 stsp goto done;
8036 0ebf8283 2019-07-24 stsp
8037 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
8038 25a102ed 2023-04-14 thomas worktree->base_commit_id);
8039 25a102ed 2023-04-14 thomas if (err)
8040 25a102ed 2023-04-14 thomas goto done;
8041 25a102ed 2023-04-14 thomas
8042 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8043 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8044 0ebf8283 2019-07-24 stsp if (err)
8045 0ebf8283 2019-07-24 stsp goto done;
8046 0ebf8283 2019-07-24 stsp
8047 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8048 0ebf8283 2019-07-24 stsp if (err)
8049 0ebf8283 2019-07-24 stsp goto done;
8050 0ebf8283 2019-07-24 stsp
8051 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8052 0ebf8283 2019-07-24 stsp if (err)
8053 0ebf8283 2019-07-24 stsp goto done;
8054 0ebf8283 2019-07-24 stsp
8055 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8056 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8057 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8058 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8059 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8060 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8061 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8062 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
8063 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8064 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8065 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8066 0ebf8283 2019-07-24 stsp if (err)
8067 1f1abb7e 2019-08-08 stsp goto sync;
8068 0ebf8283 2019-07-24 stsp
8069 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8070 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8071 0ebf8283 2019-07-24 stsp sync:
8072 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8073 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8074 0ebf8283 2019-07-24 stsp err = sync_err;
8075 0ebf8283 2019-07-24 stsp done:
8076 8641a332 2023-04-14 thomas if (resolved)
8077 8641a332 2023-04-14 thomas got_ref_close(resolved);
8078 8641a332 2023-04-14 thomas if (commit_ref)
8079 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8080 8641a332 2023-04-14 thomas free(merged_commit_id);
8081 0ebf8283 2019-07-24 stsp free(tree_id);
8082 818c7501 2019-07-11 stsp free(fileindex_path);
8083 8641a332 2023-04-14 thomas free(commit_ref_name);
8084 818c7501 2019-07-11 stsp
8085 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8086 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8087 818c7501 2019-07-11 stsp err = unlockerr;
8088 818c7501 2019-07-11 stsp return err;
8089 818c7501 2019-07-11 stsp }
8090 0ebf8283 2019-07-24 stsp
8091 0ebf8283 2019-07-24 stsp const struct got_error *
8092 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8093 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8094 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8095 0ebf8283 2019-07-24 stsp {
8096 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8097 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8098 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8099 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8100 0ebf8283 2019-07-24 stsp
8101 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8102 0ebf8283 2019-07-24 stsp if (err)
8103 0ebf8283 2019-07-24 stsp return err;
8104 0ebf8283 2019-07-24 stsp
8105 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8106 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8107 e600f124 2021-03-21 stsp if (err)
8108 e600f124 2021-03-21 stsp goto done;
8109 e600f124 2021-03-21 stsp
8110 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8111 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8112 0ebf8283 2019-07-24 stsp if (err)
8113 0ebf8283 2019-07-24 stsp goto done;
8114 0ebf8283 2019-07-24 stsp
8115 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8116 0ebf8283 2019-07-24 stsp if (err)
8117 0ebf8283 2019-07-24 stsp goto done;
8118 0ebf8283 2019-07-24 stsp
8119 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8120 0ebf8283 2019-07-24 stsp if (err)
8121 0ebf8283 2019-07-24 stsp goto done;
8122 0ebf8283 2019-07-24 stsp
8123 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8124 0ebf8283 2019-07-24 stsp if (err)
8125 0ebf8283 2019-07-24 stsp goto done;
8126 0ebf8283 2019-07-24 stsp
8127 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8128 a615e0e7 2020-12-16 stsp if (err)
8129 a615e0e7 2020-12-16 stsp goto done;
8130 a615e0e7 2020-12-16 stsp
8131 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8132 a615e0e7 2020-12-16 stsp if (err)
8133 a615e0e7 2020-12-16 stsp goto done;
8134 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8135 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8136 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8137 a615e0e7 2020-12-16 stsp err = sync_err;
8138 0ebf8283 2019-07-24 stsp done:
8139 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8140 a615e0e7 2020-12-16 stsp free(fileindex_path);
8141 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8142 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8143 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8144 0ebf8283 2019-07-24 stsp err = unlockerr;
8145 0ebf8283 2019-07-24 stsp return err;
8146 0ebf8283 2019-07-24 stsp }
8147 0ebf8283 2019-07-24 stsp
8148 0ebf8283 2019-07-24 stsp const struct got_error *
8149 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8150 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8151 0ebf8283 2019-07-24 stsp {
8152 0ebf8283 2019-07-24 stsp const struct got_error *err;
8153 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8154 0ebf8283 2019-07-24 stsp
8155 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8156 0ebf8283 2019-07-24 stsp if (err)
8157 0ebf8283 2019-07-24 stsp return err;
8158 0ebf8283 2019-07-24 stsp
8159 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8160 0ebf8283 2019-07-24 stsp if (err)
8161 0ebf8283 2019-07-24 stsp goto done;
8162 0ebf8283 2019-07-24 stsp
8163 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8164 0ebf8283 2019-07-24 stsp done:
8165 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8166 2822a352 2019-10-15 stsp return err;
8167 2822a352 2019-10-15 stsp }
8168 2822a352 2019-10-15 stsp
8169 2822a352 2019-10-15 stsp const struct got_error *
8170 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8171 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8172 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8173 2822a352 2019-10-15 stsp struct got_repository *repo)
8174 2822a352 2019-10-15 stsp {
8175 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8176 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8177 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8178 2822a352 2019-10-15 stsp
8179 2822a352 2019-10-15 stsp *fileindex = NULL;
8180 2822a352 2019-10-15 stsp *branch_ref = NULL;
8181 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8182 2822a352 2019-10-15 stsp
8183 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8184 2822a352 2019-10-15 stsp if (err)
8185 2822a352 2019-10-15 stsp return err;
8186 2822a352 2019-10-15 stsp
8187 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8188 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8189 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8190 2822a352 2019-10-15 stsp "update -b or different branch name required");
8191 2822a352 2019-10-15 stsp goto done;
8192 2822a352 2019-10-15 stsp }
8193 2822a352 2019-10-15 stsp
8194 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8195 2822a352 2019-10-15 stsp if (err)
8196 2822a352 2019-10-15 stsp goto done;
8197 2822a352 2019-10-15 stsp
8198 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8199 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8200 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8201 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8202 2822a352 2019-10-15 stsp &ok_arg);
8203 2822a352 2019-10-15 stsp if (err)
8204 2822a352 2019-10-15 stsp goto done;
8205 2822a352 2019-10-15 stsp
8206 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8207 2822a352 2019-10-15 stsp if (err)
8208 2822a352 2019-10-15 stsp goto done;
8209 2822a352 2019-10-15 stsp
8210 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8211 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8212 2822a352 2019-10-15 stsp done:
8213 2822a352 2019-10-15 stsp if (err) {
8214 2822a352 2019-10-15 stsp if (*branch_ref) {
8215 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8216 2822a352 2019-10-15 stsp *branch_ref = NULL;
8217 2822a352 2019-10-15 stsp }
8218 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8219 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8220 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8221 2822a352 2019-10-15 stsp }
8222 2822a352 2019-10-15 stsp if (*fileindex) {
8223 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8224 2822a352 2019-10-15 stsp *fileindex = NULL;
8225 2822a352 2019-10-15 stsp }
8226 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8227 2822a352 2019-10-15 stsp }
8228 0cb83759 2019-08-03 stsp return err;
8229 0cb83759 2019-08-03 stsp }
8230 0cb83759 2019-08-03 stsp
8231 2822a352 2019-10-15 stsp const struct got_error *
8232 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8233 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8234 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8235 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8236 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8237 2822a352 2019-10-15 stsp {
8238 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8239 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8240 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8241 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8242 2822a352 2019-10-15 stsp
8243 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8244 2822a352 2019-10-15 stsp if (err)
8245 2822a352 2019-10-15 stsp goto done;
8246 2822a352 2019-10-15 stsp
8247 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8248 2822a352 2019-10-15 stsp if (err)
8249 2822a352 2019-10-15 stsp goto done;
8250 2822a352 2019-10-15 stsp
8251 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8252 945f9229 2022-04-16 thomas if (err)
8253 945f9229 2022-04-16 thomas goto done;
8254 945f9229 2022-04-16 thomas
8255 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8256 2822a352 2019-10-15 stsp worktree->path_prefix);
8257 2822a352 2019-10-15 stsp if (err)
8258 2822a352 2019-10-15 stsp goto done;
8259 2822a352 2019-10-15 stsp
8260 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8261 2822a352 2019-10-15 stsp if (err)
8262 2822a352 2019-10-15 stsp goto done;
8263 2822a352 2019-10-15 stsp
8264 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8265 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8266 2822a352 2019-10-15 stsp if (err)
8267 2822a352 2019-10-15 stsp goto sync;
8268 2822a352 2019-10-15 stsp
8269 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8270 2822a352 2019-10-15 stsp if (err)
8271 2822a352 2019-10-15 stsp goto sync;
8272 2822a352 2019-10-15 stsp
8273 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8274 6e210706 2021-01-22 stsp if (err)
8275 6e210706 2021-01-22 stsp goto sync;
8276 6e210706 2021-01-22 stsp
8277 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8278 2822a352 2019-10-15 stsp sync:
8279 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8280 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8281 2822a352 2019-10-15 stsp err = sync_err;
8282 2822a352 2019-10-15 stsp
8283 2822a352 2019-10-15 stsp done:
8284 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8285 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8286 2822a352 2019-10-15 stsp err = unlockerr;
8287 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8288 2822a352 2019-10-15 stsp
8289 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8290 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8291 2822a352 2019-10-15 stsp err = unlockerr;
8292 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8293 2822a352 2019-10-15 stsp
8294 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8295 2822a352 2019-10-15 stsp free(fileindex_path);
8296 2822a352 2019-10-15 stsp free(tree_id);
8297 945f9229 2022-04-16 thomas if (commit)
8298 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8299 2822a352 2019-10-15 stsp
8300 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8301 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8302 2822a352 2019-10-15 stsp err = unlockerr;
8303 2822a352 2019-10-15 stsp return err;
8304 2822a352 2019-10-15 stsp }
8305 2822a352 2019-10-15 stsp
8306 2822a352 2019-10-15 stsp const struct got_error *
8307 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8308 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8309 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8310 2822a352 2019-10-15 stsp {
8311 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8312 8b692cd0 2019-10-21 stsp
8313 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8314 8b692cd0 2019-10-21 stsp
8315 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8316 8b692cd0 2019-10-21 stsp
8317 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8318 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8319 8b692cd0 2019-10-21 stsp err = unlockerr;
8320 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8321 8b692cd0 2019-10-21 stsp
8322 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8323 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8324 8b692cd0 2019-10-21 stsp err = unlockerr;
8325 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8326 10604dce 2021-09-24 thomas
8327 10604dce 2021-09-24 thomas return err;
8328 10604dce 2021-09-24 thomas }
8329 10604dce 2021-09-24 thomas
8330 10604dce 2021-09-24 thomas const struct got_error *
8331 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8332 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8333 10604dce 2021-09-24 thomas {
8334 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8335 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8336 10604dce 2021-09-24 thomas
8337 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8338 10604dce 2021-09-24 thomas if (err)
8339 10604dce 2021-09-24 thomas goto done;
8340 8b692cd0 2019-10-21 stsp
8341 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8342 10604dce 2021-09-24 thomas
8343 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8344 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8345 10604dce 2021-09-24 thomas err = sync_err;
8346 10604dce 2021-09-24 thomas done:
8347 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8348 10604dce 2021-09-24 thomas free(fileindex_path);
8349 8b692cd0 2019-10-21 stsp return err;
8350 2822a352 2019-10-15 stsp }
8351 2822a352 2019-10-15 stsp
8352 10604dce 2021-09-24 thomas static const struct got_error *
8353 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8354 10604dce 2021-09-24 thomas {
8355 10604dce 2021-09-24 thomas const struct got_error *err;
8356 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8357 10604dce 2021-09-24 thomas
8358 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8359 10604dce 2021-09-24 thomas if (err)
8360 10604dce 2021-09-24 thomas goto done;
8361 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8362 10604dce 2021-09-24 thomas if (err)
8363 10604dce 2021-09-24 thomas goto done;
8364 10604dce 2021-09-24 thomas
8365 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8366 10604dce 2021-09-24 thomas if (err)
8367 10604dce 2021-09-24 thomas goto done;
8368 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8369 10604dce 2021-09-24 thomas if (err)
8370 10604dce 2021-09-24 thomas goto done;
8371 10604dce 2021-09-24 thomas
8372 10604dce 2021-09-24 thomas done:
8373 10604dce 2021-09-24 thomas free(branch_refname);
8374 10604dce 2021-09-24 thomas free(commit_refname);
8375 10604dce 2021-09-24 thomas return err;
8376 10604dce 2021-09-24 thomas }
8377 10604dce 2021-09-24 thomas
8378 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8379 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8380 10604dce 2021-09-24 thomas const char *branch_name;
8381 10604dce 2021-09-24 thomas };
8382 10604dce 2021-09-24 thomas
8383 10604dce 2021-09-24 thomas static const struct got_error *
8384 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8385 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8386 10604dce 2021-09-24 thomas {
8387 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8388 10604dce 2021-09-24 thomas
8389 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8390 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8391 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8392 10604dce 2021-09-24 thomas
8393 10604dce 2021-09-24 thomas return NULL;
8394 10604dce 2021-09-24 thomas }
8395 10604dce 2021-09-24 thomas
8396 10604dce 2021-09-24 thomas
8397 10604dce 2021-09-24 thomas const struct got_error *
8398 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8399 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8400 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8401 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8402 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8403 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8404 10604dce 2021-09-24 thomas {
8405 10604dce 2021-09-24 thomas const struct got_error *err;
8406 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8407 10604dce 2021-09-24 thomas
8408 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8409 10604dce 2021-09-24 thomas if (err)
8410 10604dce 2021-09-24 thomas goto done;
8411 10604dce 2021-09-24 thomas
8412 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8413 10604dce 2021-09-24 thomas worktree);
8414 10604dce 2021-09-24 thomas if (err)
8415 10604dce 2021-09-24 thomas goto done;
8416 10604dce 2021-09-24 thomas
8417 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8418 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8419 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8420 10604dce 2021-09-24 thomas done:
8421 10604dce 2021-09-24 thomas free(fileindex_path);
8422 10604dce 2021-09-24 thomas return err;
8423 10604dce 2021-09-24 thomas }
8424 10604dce 2021-09-24 thomas
8425 10604dce 2021-09-24 thomas const struct got_error *
8426 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8427 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8428 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8429 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8430 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8431 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8432 ae1e948a 2021-09-28 thomas
8433 10604dce 2021-09-24 thomas {
8434 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8435 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8436 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8437 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8438 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8439 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8440 10604dce 2021-09-24 thomas int have_staged_files = 0;
8441 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8442 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8443 10604dce 2021-09-24 thomas
8444 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8445 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8446 10604dce 2021-09-24 thomas
8447 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8448 10604dce 2021-09-24 thomas
8449 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8450 10604dce 2021-09-24 thomas if (err)
8451 10604dce 2021-09-24 thomas goto done;
8452 10604dce 2021-09-24 thomas
8453 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8454 10604dce 2021-09-24 thomas if (err)
8455 10604dce 2021-09-24 thomas goto done;
8456 10604dce 2021-09-24 thomas
8457 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8458 10604dce 2021-09-24 thomas if (err)
8459 10604dce 2021-09-24 thomas goto done;
8460 10604dce 2021-09-24 thomas
8461 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8462 10604dce 2021-09-24 thomas &have_staged_files);
8463 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8464 10604dce 2021-09-24 thomas goto done;
8465 10604dce 2021-09-24 thomas if (have_staged_files) {
8466 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8467 10604dce 2021-09-24 thomas goto done;
8468 10604dce 2021-09-24 thomas }
8469 10604dce 2021-09-24 thomas
8470 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8471 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8472 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8473 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8474 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8475 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8476 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8477 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8478 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8479 10604dce 2021-09-24 thomas if (err)
8480 10604dce 2021-09-24 thomas goto done;
8481 10604dce 2021-09-24 thomas
8482 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8483 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8484 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8485 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8486 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8487 10604dce 2021-09-24 thomas if (err)
8488 10604dce 2021-09-24 thomas goto done;
8489 10604dce 2021-09-24 thomas
8490 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8491 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8492 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8493 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8494 10604dce 2021-09-24 thomas err = sync_err;
8495 10604dce 2021-09-24 thomas done:
8496 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8497 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8498 21c2d8be 2023-01-10 thomas
8499 10604dce 2021-09-24 thomas free_commitable(ct);
8500 10604dce 2021-09-24 thomas }
8501 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8502 10604dce 2021-09-24 thomas free(fileindex_path);
8503 10604dce 2021-09-24 thomas return err;
8504 10604dce 2021-09-24 thomas }
8505 10604dce 2021-09-24 thomas
8506 10604dce 2021-09-24 thomas const struct got_error *
8507 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8508 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8509 10604dce 2021-09-24 thomas {
8510 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8511 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8512 10604dce 2021-09-24 thomas
8513 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8514 10604dce 2021-09-24 thomas if (err)
8515 10604dce 2021-09-24 thomas goto done;
8516 10604dce 2021-09-24 thomas
8517 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8518 10604dce 2021-09-24 thomas if (err)
8519 10604dce 2021-09-24 thomas goto done;
8520 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8521 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8522 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8523 10604dce 2021-09-24 thomas err = sync_err;
8524 10604dce 2021-09-24 thomas done:
8525 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8526 10604dce 2021-09-24 thomas free(fileindex_path);
8527 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8528 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8529 10604dce 2021-09-24 thomas err = unlockerr;
8530 10604dce 2021-09-24 thomas return err;
8531 10604dce 2021-09-24 thomas }
8532 10604dce 2021-09-24 thomas
8533 10604dce 2021-09-24 thomas const struct got_error *
8534 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8535 10604dce 2021-09-24 thomas struct got_repository *repo)
8536 10604dce 2021-09-24 thomas {
8537 10604dce 2021-09-24 thomas const struct got_error *err;
8538 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8539 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8540 10604dce 2021-09-24 thomas
8541 10604dce 2021-09-24 thomas *in_progress = 0;
8542 10604dce 2021-09-24 thomas
8543 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8544 10604dce 2021-09-24 thomas if (err)
8545 10604dce 2021-09-24 thomas return err;
8546 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8547 dfe86d1f 2021-09-24 thomas free(branch_refname);
8548 10604dce 2021-09-24 thomas if (err) {
8549 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8550 10604dce 2021-09-24 thomas return err;
8551 10604dce 2021-09-24 thomas } else
8552 10604dce 2021-09-24 thomas *in_progress = 1;
8553 10604dce 2021-09-24 thomas
8554 10604dce 2021-09-24 thomas return NULL;
8555 10604dce 2021-09-24 thomas }
8556 10604dce 2021-09-24 thomas
8557 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8558 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8559 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8560 10604dce 2021-09-24 thomas {
8561 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8562 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8563 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8564 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8565 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8566 10604dce 2021-09-24 thomas
8567 10604dce 2021-09-24 thomas *fileindex = NULL;
8568 10604dce 2021-09-24 thomas
8569 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8570 10604dce 2021-09-24 thomas if (err)
8571 10604dce 2021-09-24 thomas return err;
8572 10604dce 2021-09-24 thomas
8573 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8574 10604dce 2021-09-24 thomas if (err)
8575 10604dce 2021-09-24 thomas goto done;
8576 10604dce 2021-09-24 thomas
8577 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8578 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8579 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8580 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8581 10604dce 2021-09-24 thomas &ok_arg);
8582 10604dce 2021-09-24 thomas if (err)
8583 10604dce 2021-09-24 thomas goto done;
8584 10604dce 2021-09-24 thomas
8585 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8586 10604dce 2021-09-24 thomas 0);
8587 10604dce 2021-09-24 thomas if (err)
8588 10604dce 2021-09-24 thomas goto done;
8589 10604dce 2021-09-24 thomas
8590 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8591 10604dce 2021-09-24 thomas if (err)
8592 10604dce 2021-09-24 thomas goto done;
8593 10604dce 2021-09-24 thomas
8594 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8595 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8596 10604dce 2021-09-24 thomas goto done;
8597 10604dce 2021-09-24 thomas }
8598 2b72f32d 2023-06-22 thomas
8599 2b72f32d 2023-06-22 thomas done:
8600 2b72f32d 2023-06-22 thomas free(fileindex_path);
8601 2b72f32d 2023-06-22 thomas if (wt_branch)
8602 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8603 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8604 2b72f32d 2023-06-22 thomas if (err) {
8605 2b72f32d 2023-06-22 thomas if (*fileindex) {
8606 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8607 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8608 2b72f32d 2023-06-22 thomas }
8609 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8610 2b72f32d 2023-06-22 thomas }
8611 2b72f32d 2023-06-22 thomas return err;
8612 2b72f32d 2023-06-22 thomas }
8613 10604dce 2021-09-24 thomas
8614 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8615 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8616 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8617 2b72f32d 2023-06-22 thomas {
8618 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8619 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8620 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8621 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8622 2b72f32d 2023-06-22 thomas
8623 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8624 2b72f32d 2023-06-22 thomas if (err)
8625 2b72f32d 2023-06-22 thomas return err;
8626 2b72f32d 2023-06-22 thomas
8627 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8628 2b72f32d 2023-06-22 thomas if (err)
8629 2b72f32d 2023-06-22 thomas return err;
8630 2b72f32d 2023-06-22 thomas
8631 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8632 10604dce 2021-09-24 thomas if (err)
8633 10604dce 2021-09-24 thomas goto done;
8634 10604dce 2021-09-24 thomas
8635 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8636 10604dce 2021-09-24 thomas if (err)
8637 10604dce 2021-09-24 thomas goto done;
8638 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8639 10604dce 2021-09-24 thomas if (err)
8640 10604dce 2021-09-24 thomas goto done;
8641 10604dce 2021-09-24 thomas
8642 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8643 10604dce 2021-09-24 thomas if (err)
8644 10604dce 2021-09-24 thomas goto done;
8645 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8646 10604dce 2021-09-24 thomas if (err)
8647 10604dce 2021-09-24 thomas goto done;
8648 10604dce 2021-09-24 thomas
8649 10604dce 2021-09-24 thomas done:
8650 10604dce 2021-09-24 thomas free(branch_refname);
8651 10604dce 2021-09-24 thomas free(commit_refname);
8652 10604dce 2021-09-24 thomas if (branch_ref)
8653 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8654 10604dce 2021-09-24 thomas if (commit_ref)
8655 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8656 2b72f32d 2023-06-22 thomas free(branch_tip);
8657 10604dce 2021-09-24 thomas return err;
8658 10604dce 2021-09-24 thomas }
8659 10604dce 2021-09-24 thomas
8660 10604dce 2021-09-24 thomas const struct got_error *
8661 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8662 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8663 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8664 10604dce 2021-09-24 thomas {
8665 10604dce 2021-09-24 thomas const struct got_error *err;
8666 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8667 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8668 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8669 10604dce 2021-09-24 thomas int have_staged_files = 0;
8670 10604dce 2021-09-24 thomas
8671 10604dce 2021-09-24 thomas *branch_name = NULL;
8672 10604dce 2021-09-24 thomas *branch_tip = NULL;
8673 10604dce 2021-09-24 thomas *fileindex = NULL;
8674 10604dce 2021-09-24 thomas
8675 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8676 10604dce 2021-09-24 thomas if (err)
8677 10604dce 2021-09-24 thomas return err;
8678 10604dce 2021-09-24 thomas
8679 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8680 10604dce 2021-09-24 thomas if (err)
8681 10604dce 2021-09-24 thomas goto done;
8682 10604dce 2021-09-24 thomas
8683 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8684 10604dce 2021-09-24 thomas &have_staged_files);
8685 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8686 10604dce 2021-09-24 thomas goto done;
8687 10604dce 2021-09-24 thomas if (have_staged_files) {
8688 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8689 10604dce 2021-09-24 thomas goto done;
8690 10604dce 2021-09-24 thomas }
8691 10604dce 2021-09-24 thomas
8692 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8693 10604dce 2021-09-24 thomas if (err)
8694 10604dce 2021-09-24 thomas goto done;
8695 10604dce 2021-09-24 thomas
8696 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8697 10604dce 2021-09-24 thomas if (err)
8698 10604dce 2021-09-24 thomas goto done;
8699 10604dce 2021-09-24 thomas
8700 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8701 10604dce 2021-09-24 thomas if (err)
8702 10604dce 2021-09-24 thomas goto done;
8703 10604dce 2021-09-24 thomas
8704 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8705 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8706 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8707 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8708 10604dce 2021-09-24 thomas goto done;
8709 10604dce 2021-09-24 thomas }
8710 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8711 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8712 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8713 10604dce 2021-09-24 thomas goto done;
8714 10604dce 2021-09-24 thomas }
8715 10604dce 2021-09-24 thomas
8716 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8717 10604dce 2021-09-24 thomas if (err)
8718 10604dce 2021-09-24 thomas goto done;
8719 10604dce 2021-09-24 thomas
8720 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8721 10604dce 2021-09-24 thomas if (err)
8722 10604dce 2021-09-24 thomas goto done;
8723 10604dce 2021-09-24 thomas done:
8724 10604dce 2021-09-24 thomas free(commit_refname);
8725 10604dce 2021-09-24 thomas free(branch_refname);
8726 10604dce 2021-09-24 thomas free(fileindex_path);
8727 10604dce 2021-09-24 thomas if (commit_ref)
8728 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8729 10604dce 2021-09-24 thomas if (branch_ref)
8730 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8731 10604dce 2021-09-24 thomas if (err) {
8732 10604dce 2021-09-24 thomas if (*branch_name) {
8733 10604dce 2021-09-24 thomas free(*branch_name);
8734 10604dce 2021-09-24 thomas *branch_name = NULL;
8735 10604dce 2021-09-24 thomas }
8736 10604dce 2021-09-24 thomas free(*branch_tip);
8737 10604dce 2021-09-24 thomas *branch_tip = NULL;
8738 10604dce 2021-09-24 thomas if (*fileindex) {
8739 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8740 10604dce 2021-09-24 thomas *fileindex = NULL;
8741 10604dce 2021-09-24 thomas }
8742 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8743 10604dce 2021-09-24 thomas }
8744 10604dce 2021-09-24 thomas return err;
8745 10604dce 2021-09-24 thomas }
8746 10604dce 2021-09-24 thomas
8747 10604dce 2021-09-24 thomas const struct got_error *
8748 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8749 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8750 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8751 10604dce 2021-09-24 thomas {
8752 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8753 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8754 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8755 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8756 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8757 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8758 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8759 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8760 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8761 8641a332 2023-04-14 thomas
8762 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8763 8641a332 2023-04-14 thomas
8764 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8765 8641a332 2023-04-14 thomas if (err)
8766 8641a332 2023-04-14 thomas goto done;
8767 8641a332 2023-04-14 thomas
8768 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8769 8641a332 2023-04-14 thomas if (err)
8770 8641a332 2023-04-14 thomas goto done;
8771 8641a332 2023-04-14 thomas
8772 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8773 8641a332 2023-04-14 thomas if (err)
8774 8641a332 2023-04-14 thomas goto done;
8775 8641a332 2023-04-14 thomas
8776 8641a332 2023-04-14 thomas /*
8777 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8778 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8779 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8780 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8781 8641a332 2023-04-14 thomas */
8782 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8783 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8784 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8785 8641a332 2023-04-14 thomas if (err)
8786 8641a332 2023-04-14 thomas goto done;
8787 10604dce 2021-09-24 thomas
8788 8641a332 2023-04-14 thomas
8789 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8790 945f9229 2022-04-16 thomas worktree->base_commit_id);
8791 945f9229 2022-04-16 thomas if (err)
8792 945f9229 2022-04-16 thomas goto done;
8793 945f9229 2022-04-16 thomas
8794 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8795 945f9229 2022-04-16 thomas worktree->path_prefix);
8796 10604dce 2021-09-24 thomas if (err)
8797 10604dce 2021-09-24 thomas goto done;
8798 10604dce 2021-09-24 thomas
8799 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8800 10604dce 2021-09-24 thomas if (err)
8801 10604dce 2021-09-24 thomas goto done;
8802 10604dce 2021-09-24 thomas
8803 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8804 10604dce 2021-09-24 thomas if (err)
8805 10604dce 2021-09-24 thomas goto done;
8806 10604dce 2021-09-24 thomas
8807 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8808 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8809 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8810 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8811 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8812 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8813 10604dce 2021-09-24 thomas rfa.repo = repo;
8814 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8815 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8816 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8817 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8818 10604dce 2021-09-24 thomas if (err)
8819 10604dce 2021-09-24 thomas goto sync;
8820 10604dce 2021-09-24 thomas
8821 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8822 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8823 10604dce 2021-09-24 thomas sync:
8824 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8825 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8826 10604dce 2021-09-24 thomas err = sync_err;
8827 10604dce 2021-09-24 thomas done:
8828 10604dce 2021-09-24 thomas free(tree_id);
8829 8641a332 2023-04-14 thomas free(merged_commit_id);
8830 945f9229 2022-04-16 thomas if (commit)
8831 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8832 10604dce 2021-09-24 thomas if (fileindex)
8833 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8834 10604dce 2021-09-24 thomas free(fileindex_path);
8835 8641a332 2023-04-14 thomas if (commit_ref)
8836 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8837 8641a332 2023-04-14 thomas free(commit_ref_name);
8838 10604dce 2021-09-24 thomas
8839 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8840 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8841 10604dce 2021-09-24 thomas err = unlockerr;
8842 10604dce 2021-09-24 thomas return err;
8843 10604dce 2021-09-24 thomas }
8844 10604dce 2021-09-24 thomas
8845 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8846 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8847 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8848 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8849 2db2652d 2019-08-07 stsp struct got_repository *repo;
8850 2db2652d 2019-08-07 stsp int have_changes;
8851 2db2652d 2019-08-07 stsp };
8852 2db2652d 2019-08-07 stsp
8853 ef20f542 2022-06-26 thomas static const struct got_error *
8854 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8855 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8856 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8857 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8858 735ef5ac 2019-08-03 stsp {
8859 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8860 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8861 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8862 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8863 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8864 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8865 8b13ce36 2019-08-08 stsp
8866 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8867 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8868 8b13ce36 2019-08-08 stsp return NULL;
8869 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8870 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8871 735ef5ac 2019-08-03 stsp
8872 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8873 735ef5ac 2019-08-03 stsp if (ie == NULL)
8874 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8875 735ef5ac 2019-08-03 stsp
8876 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8877 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8878 735ef5ac 2019-08-03 stsp relpath) == -1)
8879 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8880 735ef5ac 2019-08-03 stsp
8881 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8882 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8883 43010591 2023-02-17 thomas &base_commit_id, ie);
8884 735ef5ac 2019-08-03 stsp }
8885 735ef5ac 2019-08-03 stsp
8886 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8887 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8888 3aa5969e 2019-08-06 stsp goto done;
8889 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8890 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8891 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8892 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8893 735ef5ac 2019-08-03 stsp goto done;
8894 3aa5969e 2019-08-06 stsp }
8895 735ef5ac 2019-08-03 stsp
8896 2db2652d 2019-08-07 stsp a->have_changes = 1;
8897 2db2652d 2019-08-07 stsp
8898 735ef5ac 2019-08-03 stsp p = in_repo_path;
8899 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8900 735ef5ac 2019-08-03 stsp p++;
8901 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8902 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8903 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8904 735ef5ac 2019-08-03 stsp done:
8905 735ef5ac 2019-08-03 stsp free(in_repo_path);
8906 dc424a06 2019-08-07 stsp return err;
8907 dc424a06 2019-08-07 stsp }
8908 dc424a06 2019-08-07 stsp
8909 2db2652d 2019-08-07 stsp struct stage_path_arg {
8910 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8911 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8912 2db2652d 2019-08-07 stsp struct got_repository *repo;
8913 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8914 2db2652d 2019-08-07 stsp void *status_arg;
8915 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8916 2db2652d 2019-08-07 stsp void *patch_arg;
8917 7b5dc508 2019-10-28 stsp int staged_something;
8918 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8919 2db2652d 2019-08-07 stsp };
8920 2db2652d 2019-08-07 stsp
8921 2db2652d 2019-08-07 stsp static const struct got_error *
8922 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8923 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8924 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8925 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8926 0cb83759 2019-08-03 stsp {
8927 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8928 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8929 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8930 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8931 0cb83759 2019-08-03 stsp uint32_t stage;
8932 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8933 0aeb8099 2020-07-23 stsp struct stat sb;
8934 8b13ce36 2019-08-08 stsp
8935 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8936 8b13ce36 2019-08-08 stsp return NULL;
8937 0cb83759 2019-08-03 stsp
8938 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8939 d3e7c587 2019-08-03 stsp if (ie == NULL)
8940 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8941 0cb83759 2019-08-03 stsp
8942 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8943 2db2652d 2019-08-07 stsp relpath)== -1)
8944 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8945 0cb83759 2019-08-03 stsp
8946 0cb83759 2019-08-03 stsp switch (status) {
8947 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8948 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8949 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8950 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8951 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8952 0aeb8099 2020-07-23 stsp break;
8953 0aeb8099 2020-07-23 stsp }
8954 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8955 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8956 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8957 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8958 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8959 dc424a06 2019-08-07 stsp if (err)
8960 dc424a06 2019-08-07 stsp break;
8961 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8962 dc424a06 2019-08-07 stsp break;
8963 dc424a06 2019-08-07 stsp } else {
8964 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8965 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8966 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8967 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8968 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8969 dc424a06 2019-08-07 stsp break;
8970 dc424a06 2019-08-07 stsp }
8971 dc424a06 2019-08-07 stsp }
8972 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8973 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8974 0cb83759 2019-08-03 stsp if (err)
8975 dc424a06 2019-08-07 stsp break;
8976 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8977 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8978 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8979 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8980 0cb83759 2019-08-03 stsp else
8981 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8982 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8983 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8984 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8985 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8986 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8987 35213c7c 2020-07-23 stsp ssize_t target_len;
8988 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8989 35213c7c 2020-07-23 stsp sizeof(target_path));
8990 35213c7c 2020-07-23 stsp if (target_len == -1) {
8991 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8992 35213c7c 2020-07-23 stsp ondisk_path);
8993 35213c7c 2020-07-23 stsp break;
8994 35213c7c 2020-07-23 stsp }
8995 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8996 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8997 ad10f64e 2023-07-19 thomas a->worktree->root_path,
8998 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
8999 35213c7c 2020-07-23 stsp if (err)
9000 35213c7c 2020-07-23 stsp break;
9001 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9002 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9003 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9004 35213c7c 2020-07-23 stsp break;
9005 35213c7c 2020-07-23 stsp }
9006 35213c7c 2020-07-23 stsp }
9007 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9008 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9009 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9010 35213c7c 2020-07-23 stsp else
9011 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9012 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9013 0aeb8099 2020-07-23 stsp } else {
9014 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9015 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9016 0aeb8099 2020-07-23 stsp }
9017 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9018 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9019 dc424a06 2019-08-07 stsp break;
9020 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9021 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9022 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9023 f289c82c 2022-06-13 thomas if (err)
9024 f289c82c 2022-06-13 thomas break;
9025 f289c82c 2022-06-13 thomas /*
9026 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
9027 f289c82c 2022-06-13 thomas * implicitly unstage the file.
9028 f289c82c 2022-06-13 thomas */
9029 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9030 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
9031 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
9032 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
9033 f289c82c 2022-06-13 thomas }
9034 0cb83759 2019-08-03 stsp break;
9035 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9036 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9037 d3e7c587 2019-08-03 stsp break;
9038 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9039 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9040 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9041 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9042 dc424a06 2019-08-07 stsp if (err)
9043 dc424a06 2019-08-07 stsp break;
9044 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9045 88f33a19 2019-08-08 stsp break;
9046 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9047 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9048 dc424a06 2019-08-07 stsp break;
9049 88f33a19 2019-08-08 stsp }
9050 dc424a06 2019-08-07 stsp }
9051 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9052 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9053 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9054 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9055 dc424a06 2019-08-07 stsp break;
9056 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9057 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9058 12463d8b 2019-12-13 stsp de_name);
9059 0cb83759 2019-08-03 stsp break;
9060 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9061 d3e7c587 2019-08-03 stsp break;
9062 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9063 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9064 ebf48fd5 2019-08-03 stsp break;
9065 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9066 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9067 2a06fe5f 2019-08-24 stsp break;
9068 0cb83759 2019-08-03 stsp default:
9069 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9070 537ac44b 2019-08-03 stsp break;
9071 0cb83759 2019-08-03 stsp }
9072 dc424a06 2019-08-07 stsp
9073 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9074 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9075 dc424a06 2019-08-07 stsp free(path_content);
9076 2db2652d 2019-08-07 stsp free(ondisk_path);
9077 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9078 0ebf8283 2019-07-24 stsp return err;
9079 0ebf8283 2019-07-24 stsp }
9080 0cb83759 2019-08-03 stsp
9081 0cb83759 2019-08-03 stsp const struct got_error *
9082 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9083 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9084 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9085 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9086 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9087 0cb83759 2019-08-03 stsp {
9088 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9089 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9090 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9091 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9092 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9093 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9094 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9095 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9096 0cb83759 2019-08-03 stsp
9097 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9098 0cb83759 2019-08-03 stsp if (err)
9099 0cb83759 2019-08-03 stsp return err;
9100 0cb83759 2019-08-03 stsp
9101 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9102 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9103 735ef5ac 2019-08-03 stsp if (err)
9104 735ef5ac 2019-08-03 stsp goto done;
9105 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9106 735ef5ac 2019-08-03 stsp if (err)
9107 735ef5ac 2019-08-03 stsp goto done;
9108 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9109 0cb83759 2019-08-03 stsp if (err)
9110 0cb83759 2019-08-03 stsp goto done;
9111 0cb83759 2019-08-03 stsp
9112 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9113 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9114 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9115 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9116 2db2652d 2019-08-07 stsp oka.repo = repo;
9117 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9118 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9119 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9120 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9121 735ef5ac 2019-08-03 stsp if (err)
9122 735ef5ac 2019-08-03 stsp goto done;
9123 735ef5ac 2019-08-03 stsp }
9124 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9125 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9126 2db2652d 2019-08-07 stsp goto done;
9127 2db2652d 2019-08-07 stsp }
9128 735ef5ac 2019-08-03 stsp
9129 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9130 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9131 2db2652d 2019-08-07 stsp spa.repo = repo;
9132 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9133 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9134 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9135 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9136 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9137 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9138 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9139 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9140 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9141 42005733 2019-08-03 stsp if (err)
9142 2db2652d 2019-08-07 stsp goto done;
9143 7b5dc508 2019-10-28 stsp }
9144 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9145 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9146 7b5dc508 2019-10-28 stsp goto done;
9147 0cb83759 2019-08-03 stsp }
9148 0cb83759 2019-08-03 stsp
9149 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9150 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9151 0cb83759 2019-08-03 stsp err = sync_err;
9152 0cb83759 2019-08-03 stsp done:
9153 735ef5ac 2019-08-03 stsp if (head_ref)
9154 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9155 735ef5ac 2019-08-03 stsp free(head_commit_id);
9156 0cb83759 2019-08-03 stsp free(fileindex_path);
9157 0cb83759 2019-08-03 stsp if (fileindex)
9158 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9159 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9160 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9161 0cb83759 2019-08-03 stsp err = unlockerr;
9162 0cb83759 2019-08-03 stsp return err;
9163 0cb83759 2019-08-03 stsp }
9164 ad493afc 2019-08-03 stsp
9165 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9166 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9167 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9168 ad493afc 2019-08-03 stsp struct got_repository *repo;
9169 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9170 ad493afc 2019-08-03 stsp void *progress_arg;
9171 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9172 2e1f37b0 2019-08-08 stsp void *patch_arg;
9173 ad493afc 2019-08-03 stsp };
9174 2e1f37b0 2019-08-08 stsp
9175 2e1f37b0 2019-08-08 stsp static const struct got_error *
9176 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9177 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9178 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9179 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9180 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9181 2e1f37b0 2019-08-08 stsp {
9182 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9183 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9184 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9185 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9186 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9187 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9188 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9189 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9190 2e1f37b0 2019-08-08 stsp
9191 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9192 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9193 2e1f37b0 2019-08-08 stsp
9194 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9195 2e1f37b0 2019-08-08 stsp if (err)
9196 2e1f37b0 2019-08-08 stsp return err;
9197 f4ae6ddb 2022-07-01 thomas
9198 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9199 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9200 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9201 f4ae6ddb 2022-07-01 thomas goto done;
9202 f4ae6ddb 2022-07-01 thomas }
9203 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9204 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9205 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9206 f4ae6ddb 2022-07-01 thomas goto done;
9207 f4ae6ddb 2022-07-01 thomas }
9208 f4ae6ddb 2022-07-01 thomas
9209 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9210 2e1f37b0 2019-08-08 stsp if (err)
9211 2e1f37b0 2019-08-08 stsp goto done;
9212 2e1f37b0 2019-08-08 stsp
9213 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9214 2e1f37b0 2019-08-08 stsp if (err)
9215 2e1f37b0 2019-08-08 stsp goto done;
9216 2e1f37b0 2019-08-08 stsp
9217 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9218 2e1f37b0 2019-08-08 stsp if (err)
9219 2e1f37b0 2019-08-08 stsp goto done;
9220 2e1f37b0 2019-08-08 stsp
9221 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9222 f4ae6ddb 2022-07-01 thomas fd2);
9223 2e1f37b0 2019-08-08 stsp if (err)
9224 2e1f37b0 2019-08-08 stsp goto done;
9225 2e1f37b0 2019-08-08 stsp
9226 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9227 2e1f37b0 2019-08-08 stsp if (err)
9228 2e1f37b0 2019-08-08 stsp goto done;
9229 ad493afc 2019-08-03 stsp
9230 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9231 2e1f37b0 2019-08-08 stsp if (err)
9232 2e1f37b0 2019-08-08 stsp goto done;
9233 2e1f37b0 2019-08-08 stsp
9234 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9235 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9236 2e1f37b0 2019-08-08 stsp if (err)
9237 2e1f37b0 2019-08-08 stsp goto done;
9238 2e1f37b0 2019-08-08 stsp
9239 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9240 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9241 2e1f37b0 2019-08-08 stsp if (err)
9242 2e1f37b0 2019-08-08 stsp goto done;
9243 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9244 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9245 2e1f37b0 2019-08-08 stsp if (err)
9246 2e1f37b0 2019-08-08 stsp goto done;
9247 2e1f37b0 2019-08-08 stsp
9248 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9249 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9250 2e1f37b0 2019-08-08 stsp goto done;
9251 2e1f37b0 2019-08-08 stsp }
9252 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9253 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9254 2e1f37b0 2019-08-08 stsp goto done;
9255 2e1f37b0 2019-08-08 stsp }
9256 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9257 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9258 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9259 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9260 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9261 fe621944 2020-11-10 stsp nchanges++;
9262 fe621944 2020-11-10 stsp }
9263 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9264 2e1f37b0 2019-08-08 stsp int choice;
9265 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9266 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9267 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9268 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9269 2e1f37b0 2019-08-08 stsp if (err)
9270 2e1f37b0 2019-08-08 stsp goto done;
9271 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9272 2e1f37b0 2019-08-08 stsp have_content = 1;
9273 19e4b907 2019-08-08 stsp else
9274 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9275 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9276 2e1f37b0 2019-08-08 stsp break;
9277 2e1f37b0 2019-08-08 stsp }
9278 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9279 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9280 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9281 2e1f37b0 2019-08-08 stsp done:
9282 2e1f37b0 2019-08-08 stsp free(label1);
9283 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9284 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9285 2e1f37b0 2019-08-08 stsp if (blob)
9286 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9287 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9288 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9289 2e1f37b0 2019-08-08 stsp if (staged_blob)
9290 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9291 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9292 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9293 fe621944 2020-11-10 stsp err = free_err;
9294 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9295 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9296 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9297 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9298 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9299 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9300 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9301 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9302 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9303 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9304 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9305 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9306 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9307 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9308 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9309 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9310 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9311 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9312 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9313 2e1f37b0 2019-08-08 stsp }
9314 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9315 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9316 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9317 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9318 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9319 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9320 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9321 2e1f37b0 2019-08-08 stsp }
9322 2e1f37b0 2019-08-08 stsp free(path1);
9323 2e1f37b0 2019-08-08 stsp free(path2);
9324 fda8017d 2020-07-23 stsp return err;
9325 fda8017d 2020-07-23 stsp }
9326 fda8017d 2020-07-23 stsp
9327 fda8017d 2020-07-23 stsp static const struct got_error *
9328 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9329 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9330 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9331 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9332 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9333 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9334 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9335 fda8017d 2020-07-23 stsp {
9336 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9337 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9338 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9339 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9340 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9341 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9342 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9343 fda8017d 2020-07-23 stsp struct stat sb;
9344 fda8017d 2020-07-23 stsp
9345 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9346 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9347 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9348 fda8017d 2020-07-23 stsp if (err)
9349 fda8017d 2020-07-23 stsp return err;
9350 fda8017d 2020-07-23 stsp
9351 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9352 fda8017d 2020-07-23 stsp return NULL;
9353 fda8017d 2020-07-23 stsp
9354 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9355 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9356 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9357 fda8017d 2020-07-23 stsp if (err)
9358 fda8017d 2020-07-23 stsp goto done;
9359 fda8017d 2020-07-23 stsp }
9360 fda8017d 2020-07-23 stsp
9361 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9362 fda8017d 2020-07-23 stsp if (f == NULL) {
9363 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9364 fda8017d 2020-07-23 stsp path_unstaged_content);
9365 fda8017d 2020-07-23 stsp goto done;
9366 fda8017d 2020-07-23 stsp }
9367 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9368 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9369 fda8017d 2020-07-23 stsp goto done;
9370 fda8017d 2020-07-23 stsp }
9371 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9372 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9373 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9374 fda8017d 2020-07-23 stsp size_t r;
9375 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9376 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9377 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9378 fda8017d 2020-07-23 stsp goto done;
9379 fda8017d 2020-07-23 stsp }
9380 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9381 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9382 fda8017d 2020-07-23 stsp goto done;
9383 fda8017d 2020-07-23 stsp }
9384 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9385 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9386 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9387 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9388 fda8017d 2020-07-23 stsp progress_arg);
9389 fda8017d 2020-07-23 stsp } else {
9390 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9391 67a66647 2021-05-31 stsp
9392 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9393 67a66647 2021-05-31 stsp if (err)
9394 67a66647 2021-05-31 stsp return err;
9395 67a66647 2021-05-31 stsp
9396 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9397 67a66647 2021-05-31 stsp parent) == -1) {
9398 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9399 67a66647 2021-05-31 stsp base_path = NULL;
9400 67a66647 2021-05-31 stsp goto done;
9401 67a66647 2021-05-31 stsp }
9402 67a66647 2021-05-31 stsp
9403 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9404 fc2a50f2 2022-11-01 thomas base_path, "");
9405 67a66647 2021-05-31 stsp if (err)
9406 67a66647 2021-05-31 stsp goto done;
9407 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9408 67a66647 2021-05-31 stsp blob_base);
9409 67a66647 2021-05-31 stsp if (err)
9410 67a66647 2021-05-31 stsp goto done;
9411 67a66647 2021-05-31 stsp
9412 b6b86fd1 2022-08-30 thomas /*
9413 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9414 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9415 eec2f5a9 2021-06-03 stsp */
9416 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9417 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9418 eec2f5a9 2021-06-03 stsp ondisk_path);
9419 eec2f5a9 2021-06-03 stsp if (err)
9420 eec2f5a9 2021-06-03 stsp goto done;
9421 eec2f5a9 2021-06-03 stsp } else {
9422 eec2f5a9 2021-06-03 stsp int fd;
9423 06340621 2021-12-31 thomas fd = open(ondisk_path,
9424 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9425 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9426 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9427 eec2f5a9 2021-06-03 stsp goto done;
9428 eec2f5a9 2021-06-03 stsp }
9429 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9430 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9431 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9432 eec2f5a9 2021-06-03 stsp close(fd);
9433 eec2f5a9 2021-06-03 stsp goto done;
9434 eec2f5a9 2021-06-03 stsp }
9435 eec2f5a9 2021-06-03 stsp }
9436 eec2f5a9 2021-06-03 stsp
9437 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9438 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9439 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9440 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9441 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9442 fda8017d 2020-07-23 stsp }
9443 fda8017d 2020-07-23 stsp if (err)
9444 fda8017d 2020-07-23 stsp goto done;
9445 fda8017d 2020-07-23 stsp
9446 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9447 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9448 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9449 2ac8aa02 2020-11-09 stsp } else {
9450 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9451 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9452 2ac8aa02 2020-11-09 stsp }
9453 fda8017d 2020-07-23 stsp done:
9454 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9455 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9456 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9457 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9458 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9459 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9460 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9461 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9462 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9463 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9464 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9465 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9466 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9467 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9468 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9469 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9470 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9471 67a66647 2021-05-31 stsp free(blob_base_path);
9472 67a66647 2021-05-31 stsp free(parent);
9473 67a66647 2021-05-31 stsp free(base_path);
9474 2e1f37b0 2019-08-08 stsp return err;
9475 2e1f37b0 2019-08-08 stsp }
9476 2e1f37b0 2019-08-08 stsp
9477 ad493afc 2019-08-03 stsp static const struct got_error *
9478 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9479 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9480 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9481 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9482 ad493afc 2019-08-03 stsp {
9483 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9484 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9485 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9486 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9487 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9488 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9489 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9490 9bc94a15 2019-08-03 stsp struct stat sb;
9491 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9492 ad493afc 2019-08-03 stsp
9493 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9494 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9495 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9496 2e1f37b0 2019-08-08 stsp return NULL;
9497 2e1f37b0 2019-08-08 stsp
9498 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9499 ad493afc 2019-08-03 stsp if (ie == NULL)
9500 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9501 9bc94a15 2019-08-03 stsp
9502 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9503 9bc94a15 2019-08-03 stsp == -1)
9504 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9505 ad493afc 2019-08-03 stsp
9506 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9507 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9508 f69721c3 2019-10-21 stsp if (err)
9509 f69721c3 2019-10-21 stsp goto done;
9510 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9511 f69721c3 2019-10-21 stsp id_str) == -1) {
9512 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9513 f4ae6ddb 2022-07-01 thomas goto done;
9514 f4ae6ddb 2022-07-01 thomas }
9515 f4ae6ddb 2022-07-01 thomas
9516 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9517 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9518 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9519 f4ae6ddb 2022-07-01 thomas goto done;
9520 f4ae6ddb 2022-07-01 thomas }
9521 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9522 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9523 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9524 f69721c3 2019-10-21 stsp goto done;
9525 f69721c3 2019-10-21 stsp }
9526 f69721c3 2019-10-21 stsp
9527 ad493afc 2019-08-03 stsp switch (staged_status) {
9528 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9529 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9530 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9531 ad493afc 2019-08-03 stsp if (err)
9532 ad493afc 2019-08-03 stsp break;
9533 ad493afc 2019-08-03 stsp /* fall through */
9534 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9535 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9536 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9537 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9538 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9539 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9540 2e1f37b0 2019-08-08 stsp if (err)
9541 2e1f37b0 2019-08-08 stsp break;
9542 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9543 2e1f37b0 2019-08-08 stsp break;
9544 2e1f37b0 2019-08-08 stsp } else {
9545 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9546 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9547 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9548 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9549 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9550 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9551 2e1f37b0 2019-08-08 stsp }
9552 2e1f37b0 2019-08-08 stsp }
9553 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9554 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9555 ad493afc 2019-08-03 stsp if (err)
9556 ad493afc 2019-08-03 stsp break;
9557 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9558 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9559 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9560 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9561 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9562 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9563 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9564 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9565 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9566 ea7786be 2020-07-23 stsp break;
9567 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9568 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9569 36bf999c 2020-07-23 stsp char *staged_target;
9570 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9571 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9572 36bf999c 2020-07-23 stsp if (err)
9573 36bf999c 2020-07-23 stsp goto done;
9574 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9575 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9576 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9577 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9578 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9579 36bf999c 2020-07-23 stsp free(staged_target);
9580 dfe9fba0 2020-07-23 stsp } else {
9581 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9582 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9583 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9584 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9585 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9586 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9587 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9588 dfe9fba0 2020-07-23 stsp }
9589 ea7786be 2020-07-23 stsp break;
9590 ea7786be 2020-07-23 stsp default:
9591 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9592 ea7786be 2020-07-23 stsp break;
9593 ea7786be 2020-07-23 stsp }
9594 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9595 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9596 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9597 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9598 2ac8aa02 2020-11-09 stsp }
9599 ad493afc 2019-08-03 stsp break;
9600 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9601 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9602 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9603 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9604 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9605 2e1f37b0 2019-08-08 stsp if (err)
9606 2e1f37b0 2019-08-08 stsp break;
9607 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9608 2e1f37b0 2019-08-08 stsp break;
9609 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9610 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9611 2e1f37b0 2019-08-08 stsp break;
9612 2e1f37b0 2019-08-08 stsp }
9613 2e1f37b0 2019-08-08 stsp }
9614 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9615 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9616 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9617 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9618 9bc94a15 2019-08-03 stsp if (err)
9619 9bc94a15 2019-08-03 stsp break;
9620 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9621 ad493afc 2019-08-03 stsp break;
9622 ad493afc 2019-08-03 stsp }
9623 f69721c3 2019-10-21 stsp done:
9624 ad493afc 2019-08-03 stsp free(ondisk_path);
9625 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9626 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9627 ad493afc 2019-08-03 stsp if (blob_base)
9628 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9629 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9630 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9631 ad493afc 2019-08-03 stsp if (blob_staged)
9632 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9633 f69721c3 2019-10-21 stsp free(id_str);
9634 f69721c3 2019-10-21 stsp free(label_orig);
9635 ad493afc 2019-08-03 stsp return err;
9636 ad493afc 2019-08-03 stsp }
9637 ad493afc 2019-08-03 stsp
9638 ad493afc 2019-08-03 stsp const struct got_error *
9639 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9640 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9641 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9642 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9643 ad493afc 2019-08-03 stsp struct got_repository *repo)
9644 ad493afc 2019-08-03 stsp {
9645 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9646 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9647 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9648 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9649 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9650 ad493afc 2019-08-03 stsp
9651 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9652 ad493afc 2019-08-03 stsp if (err)
9653 ad493afc 2019-08-03 stsp return err;
9654 ad493afc 2019-08-03 stsp
9655 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9656 ad493afc 2019-08-03 stsp if (err)
9657 ad493afc 2019-08-03 stsp goto done;
9658 ad493afc 2019-08-03 stsp
9659 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9660 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9661 ad493afc 2019-08-03 stsp upa.repo = repo;
9662 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9663 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9664 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9665 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9666 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9667 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9668 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9669 ad493afc 2019-08-03 stsp if (err)
9670 ad493afc 2019-08-03 stsp goto done;
9671 ad493afc 2019-08-03 stsp }
9672 ad493afc 2019-08-03 stsp
9673 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9674 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9675 ad493afc 2019-08-03 stsp err = sync_err;
9676 ad493afc 2019-08-03 stsp done:
9677 ad493afc 2019-08-03 stsp free(fileindex_path);
9678 ad493afc 2019-08-03 stsp if (fileindex)
9679 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9680 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9681 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9682 ad493afc 2019-08-03 stsp err = unlockerr;
9683 ad493afc 2019-08-03 stsp return err;
9684 ad493afc 2019-08-03 stsp }
9685 b2118c49 2020-07-28 stsp
9686 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9687 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9688 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9689 b2118c49 2020-07-28 stsp void *info_arg;
9690 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9691 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9692 b2118c49 2020-07-28 stsp void *cancel_arg;
9693 b2118c49 2020-07-28 stsp };
9694 b2118c49 2020-07-28 stsp
9695 b2118c49 2020-07-28 stsp static const struct got_error *
9696 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9697 b2118c49 2020-07-28 stsp {
9698 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9699 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9700 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9701 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9702 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9703 b2118c49 2020-07-28 stsp int stage;
9704 b2118c49 2020-07-28 stsp
9705 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9706 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9707 b2118c49 2020-07-28 stsp
9708 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9709 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9710 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9711 b2118c49 2020-07-28 stsp break;
9712 b2118c49 2020-07-28 stsp }
9713 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9714 b2118c49 2020-07-28 stsp return NULL;
9715 b2118c49 2020-07-28 stsp
9716 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9717 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9718 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9719 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9720 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9721 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9722 43010591 2023-02-17 thomas &staged_blob_id, ie);
9723 b2118c49 2020-07-28 stsp }
9724 b2118c49 2020-07-28 stsp
9725 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9726 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9727 b2118c49 2020-07-28 stsp
9728 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9729 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9730 b2118c49 2020-07-28 stsp }
9731 b2118c49 2020-07-28 stsp
9732 b2118c49 2020-07-28 stsp const struct got_error *
9733 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9734 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9735 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9736 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9737 b2118c49 2020-07-28 stsp
9738 b2118c49 2020-07-28 stsp {
9739 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9740 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9741 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9742 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9743 b2118c49 2020-07-28 stsp
9744 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9745 b2118c49 2020-07-28 stsp if (err)
9746 b2118c49 2020-07-28 stsp return err;
9747 b2118c49 2020-07-28 stsp
9748 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9749 b2118c49 2020-07-28 stsp if (err)
9750 b2118c49 2020-07-28 stsp goto done;
9751 b2118c49 2020-07-28 stsp
9752 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9753 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9754 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9755 b2118c49 2020-07-28 stsp arg.paths = paths;
9756 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9757 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9758 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9759 b2118c49 2020-07-28 stsp &arg);
9760 b2118c49 2020-07-28 stsp done:
9761 b2118c49 2020-07-28 stsp free(fileindex_path);
9762 b2118c49 2020-07-28 stsp if (fileindex)
9763 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9764 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9765 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9766 b2118c49 2020-07-28 stsp err = unlockerr;
9767 b2118c49 2020-07-28 stsp return err;
9768 b2118c49 2020-07-28 stsp }
9769 814624e7 2022-03-22 thomas
9770 814624e7 2022-03-22 thomas static const struct got_error *
9771 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9772 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9773 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9774 814624e7 2022-03-22 thomas {
9775 814624e7 2022-03-22 thomas const struct got_error *err;
9776 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9777 814624e7 2022-03-22 thomas struct stat sb;
9778 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9779 814624e7 2022-03-22 thomas
9780 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9781 814624e7 2022-03-22 thomas if (err)
9782 814624e7 2022-03-22 thomas return err;
9783 814624e7 2022-03-22 thomas
9784 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9785 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9786 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9787 814624e7 2022-03-22 thomas
9788 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9789 814624e7 2022-03-22 thomas if (ie) {
9790 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9791 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9792 12de5570 2022-05-01 thomas repo);
9793 814624e7 2022-03-22 thomas if (err)
9794 814624e7 2022-03-22 thomas goto done;
9795 814624e7 2022-03-22 thomas } else {
9796 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9797 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9798 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9799 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9800 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9801 814624e7 2022-03-22 thomas ondisk_path);
9802 814624e7 2022-03-22 thomas goto done;
9803 814624e7 2022-03-22 thomas }
9804 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9805 814624e7 2022-03-22 thomas }
9806 814624e7 2022-03-22 thomas }
9807 814624e7 2022-03-22 thomas
9808 814624e7 2022-03-22 thomas done:
9809 814624e7 2022-03-22 thomas free(ondisk_path);
9810 814624e7 2022-03-22 thomas return err;
9811 814624e7 2022-03-22 thomas }
9812 814624e7 2022-03-22 thomas
9813 814624e7 2022-03-22 thomas static const struct got_error *
9814 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9815 814624e7 2022-03-22 thomas unsigned char staged_status)
9816 814624e7 2022-03-22 thomas {
9817 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9818 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9819 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9820 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9821 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9822 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9823 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9824 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9825 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9826 814624e7 2022-03-22 thomas return NULL;
9827 814624e7 2022-03-22 thomas }
9828 814624e7 2022-03-22 thomas
9829 814624e7 2022-03-22 thomas static const struct got_error *
9830 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9831 814624e7 2022-03-22 thomas {
9832 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9833 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9834 814624e7 2022-03-22 thomas return NULL;
9835 814624e7 2022-03-22 thomas }
9836 814624e7 2022-03-22 thomas
9837 814624e7 2022-03-22 thomas static const struct got_error *
9838 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9839 814624e7 2022-03-22 thomas unsigned char staged_status)
9840 814624e7 2022-03-22 thomas {
9841 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9842 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9843 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9844 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9845 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9846 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9847 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9848 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9849 814624e7 2022-03-22 thomas return NULL;
9850 814624e7 2022-03-22 thomas }
9851 814624e7 2022-03-22 thomas
9852 814624e7 2022-03-22 thomas const struct got_error *
9853 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9854 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9855 814624e7 2022-03-22 thomas {
9856 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9857 814624e7 2022-03-22 thomas }
9858 814624e7 2022-03-22 thomas
9859 814624e7 2022-03-22 thomas const struct got_error *
9860 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9861 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9862 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9863 814624e7 2022-03-22 thomas {
9864 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9865 814624e7 2022-03-22 thomas int file_renamed = 0;
9866 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9867 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9868 814624e7 2022-03-22 thomas
9869 814624e7 2022-03-22 thomas *oldpath = NULL;
9870 814624e7 2022-03-22 thomas *newpath = NULL;
9871 814624e7 2022-03-22 thomas
9872 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9873 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9874 814624e7 2022-03-22 thomas if (err)
9875 814624e7 2022-03-22 thomas goto done;
9876 814624e7 2022-03-22 thomas
9877 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9878 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9879 814624e7 2022-03-22 thomas if (err)
9880 814624e7 2022-03-22 thomas goto done;
9881 814624e7 2022-03-22 thomas
9882 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9883 814624e7 2022-03-22 thomas file_renamed = 1;
9884 814624e7 2022-03-22 thomas
9885 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9886 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9887 814624e7 2022-03-22 thomas else if (file_renamed) {
9888 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9889 814624e7 2022-03-22 thomas if (err == NULL)
9890 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9891 814624e7 2022-03-22 thomas } else if (old == NULL)
9892 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9893 814624e7 2022-03-22 thomas else
9894 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9895 814624e7 2022-03-22 thomas
9896 814624e7 2022-03-22 thomas done:
9897 814624e7 2022-03-22 thomas if (err) {
9898 814624e7 2022-03-22 thomas free(*oldpath);
9899 814624e7 2022-03-22 thomas *oldpath = NULL;
9900 814624e7 2022-03-22 thomas free(*newpath);
9901 814624e7 2022-03-22 thomas *newpath = NULL;
9902 814624e7 2022-03-22 thomas }
9903 814624e7 2022-03-22 thomas return err;
9904 814624e7 2022-03-22 thomas }
9905 814624e7 2022-03-22 thomas
9906 814624e7 2022-03-22 thomas const struct got_error *
9907 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9908 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9909 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9910 814624e7 2022-03-22 thomas {
9911 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9912 5e22b737 2022-05-31 thomas
9913 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9914 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9915 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9916 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9917 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9918 5e22b737 2022-05-31 thomas saa.repo = repo;
9919 5e22b737 2022-05-31 thomas
9920 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9921 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9922 814624e7 2022-03-22 thomas }
9923 5e22b737 2022-05-31 thomas
9924 5e22b737 2022-05-31 thomas const struct got_error *
9925 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9926 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9927 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9928 5e22b737 2022-05-31 thomas {
9929 8ec7352a 2023-06-01 thomas const struct got_error *err;
9930 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9931 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
9932 5e22b737 2022-05-31 thomas
9933 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9934 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9935 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9936 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9937 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9938 5e22b737 2022-05-31 thomas sda.repo = repo;
9939 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9940 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9941 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9942 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9943 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
9944 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
9945 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
9946 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
9947 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
9948 8ec7352a 2023-06-01 thomas
9949 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
9950 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9951 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
9952 8ec7352a 2023-06-01 thomas return err;
9953 5e22b737 2022-05-31 thomas }
9954 5e22b737 2022-05-31 thomas
9955 5e22b737 2022-05-31 thomas const struct got_error *
9956 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9957 36832a8e 2022-05-31 thomas const char *fileindex_path)
9958 5e22b737 2022-05-31 thomas {
9959 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9960 5e22b737 2022-05-31 thomas
9961 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9962 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9963 5e22b737 2022-05-31 thomas
9964 5e22b737 2022-05-31 thomas return err;
9965 5e22b737 2022-05-31 thomas }