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 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2274 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2275 0584f854 2019-04-06 stsp
2276 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2277 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2278 eb6fb9cd 2023-07-26 thomas if (err)
2279 eb6fb9cd 2023-07-26 thomas return err;
2280 eb6fb9cd 2023-07-26 thomas }
2281 512f0d0e 2019-01-02 stsp
2282 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2283 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2284 8da9e5f4 2019-01-12 stsp }
2285 512f0d0e 2019-01-02 stsp
2286 8da9e5f4 2019-01-12 stsp static const struct got_error *
2287 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2288 8da9e5f4 2019-01-12 stsp {
2289 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2290 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2291 7a9df742 2019-01-08 stsp
2292 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2293 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2294 eb6fb9cd 2023-07-26 thomas if (err)
2295 eb6fb9cd 2023-07-26 thomas return err;
2296 eb6fb9cd 2023-07-26 thomas }
2297 0584f854 2019-04-06 stsp
2298 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2299 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2300 9d31a1d8 2018-03-11 stsp }
2301 9d31a1d8 2018-03-11 stsp
2302 9d31a1d8 2018-03-11 stsp static const struct got_error *
2303 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2304 9d31a1d8 2018-03-11 stsp {
2305 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2306 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2307 8da9e5f4 2019-01-12 stsp char *path;
2308 9d31a1d8 2018-03-11 stsp
2309 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2310 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2311 eb6fb9cd 2023-07-26 thomas if (err)
2312 eb6fb9cd 2023-07-26 thomas return err;
2313 eb6fb9cd 2023-07-26 thomas }
2314 0584f854 2019-04-06 stsp
2315 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2316 59530ea7 2023-07-17 thomas return NULL;
2317 59530ea7 2023-07-17 thomas
2318 59530ea7 2023-07-17 thomas if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2319 63c5ca5d 2019-08-24 stsp return NULL;
2320 63c5ca5d 2019-08-24 stsp
2321 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2322 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2323 8da9e5f4 2019-01-12 stsp == -1)
2324 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2325 9d31a1d8 2018-03-11 stsp
2326 59530ea7 2023-07-17 thomas err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2327 59530ea7 2023-07-17 thomas a->repo, a->progress_cb, a->progress_arg);
2328 9d31a1d8 2018-03-11 stsp
2329 8da9e5f4 2019-01-12 stsp free(path);
2330 0cd1c46a 2019-03-11 stsp return err;
2331 0cd1c46a 2019-03-11 stsp }
2332 0cd1c46a 2019-03-11 stsp
2333 b2118c49 2020-07-28 stsp const struct got_error *
2334 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2335 b2118c49 2020-07-28 stsp {
2336 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2337 b2118c49 2020-07-28 stsp
2338 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2339 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2340 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2341 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2342 b2118c49 2020-07-28 stsp }
2343 b2118c49 2020-07-28 stsp
2344 b2118c49 2020-07-28 stsp return NULL;
2345 b2118c49 2020-07-28 stsp }
2346 b2118c49 2020-07-28 stsp
2347 818c7501 2019-07-11 stsp static const struct got_error *
2348 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2349 0cd1c46a 2019-03-11 stsp {
2350 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2351 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2352 0cd1c46a 2019-03-11 stsp
2353 517bab73 2019-03-11 stsp *refname = NULL;
2354 517bab73 2019-03-11 stsp
2355 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2356 b2118c49 2020-07-28 stsp if (err)
2357 b2118c49 2020-07-28 stsp return err;
2358 0cd1c46a 2019-03-11 stsp
2359 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2360 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2361 0647c563 2019-03-11 stsp *refname = NULL;
2362 0cd1c46a 2019-03-11 stsp }
2363 517bab73 2019-03-11 stsp free(uuidstr);
2364 517bab73 2019-03-11 stsp return err;
2365 517bab73 2019-03-11 stsp }
2366 517bab73 2019-03-11 stsp
2367 818c7501 2019-07-11 stsp const struct got_error *
2368 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2369 f6cd0243 2023-01-28 thomas const char *prefix)
2370 f6cd0243 2023-01-28 thomas {
2371 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2372 f6cd0243 2023-01-28 thomas }
2373 f6cd0243 2023-01-28 thomas
2374 fcfaa6d5 2023-07-05 thomas static const struct got_error *
2375 fcfaa6d5 2023-07-05 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
2376 818c7501 2019-07-11 stsp {
2377 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2378 818c7501 2019-07-11 stsp }
2379 818c7501 2019-07-11 stsp
2380 818c7501 2019-07-11 stsp static const struct got_error *
2381 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2382 818c7501 2019-07-11 stsp {
2383 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2384 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2385 818c7501 2019-07-11 stsp }
2386 818c7501 2019-07-11 stsp
2387 818c7501 2019-07-11 stsp static const struct got_error *
2388 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2389 818c7501 2019-07-11 stsp {
2390 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2391 818c7501 2019-07-11 stsp }
2392 818c7501 2019-07-11 stsp
2393 818c7501 2019-07-11 stsp static const struct got_error *
2394 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2395 818c7501 2019-07-11 stsp {
2396 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2397 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2398 818c7501 2019-07-11 stsp }
2399 818c7501 2019-07-11 stsp
2400 818c7501 2019-07-11 stsp static const struct got_error *
2401 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2402 818c7501 2019-07-11 stsp {
2403 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2404 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2405 0ebf8283 2019-07-24 stsp }
2406 0ebf8283 2019-07-24 stsp
2407 0ebf8283 2019-07-24 stsp static const struct got_error *
2408 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2409 0ebf8283 2019-07-24 stsp {
2410 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2411 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2412 0ebf8283 2019-07-24 stsp }
2413 0ebf8283 2019-07-24 stsp
2414 0ebf8283 2019-07-24 stsp static const struct got_error *
2415 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2416 0ebf8283 2019-07-24 stsp {
2417 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2418 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2419 0ebf8283 2019-07-24 stsp }
2420 0ebf8283 2019-07-24 stsp
2421 0ebf8283 2019-07-24 stsp static const struct got_error *
2422 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2423 0ebf8283 2019-07-24 stsp {
2424 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2425 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2426 818c7501 2019-07-11 stsp }
2427 818c7501 2019-07-11 stsp
2428 0ebf8283 2019-07-24 stsp static const struct got_error *
2429 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2430 0ebf8283 2019-07-24 stsp {
2431 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2432 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2433 0ebf8283 2019-07-24 stsp }
2434 818c7501 2019-07-11 stsp
2435 0ebf8283 2019-07-24 stsp const struct got_error *
2436 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2437 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2438 0ebf8283 2019-07-24 stsp {
2439 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2440 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2441 0ebf8283 2019-07-24 stsp *path = NULL;
2442 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2443 0ebf8283 2019-07-24 stsp }
2444 0ebf8283 2019-07-24 stsp return NULL;
2445 10604dce 2021-09-24 thomas }
2446 10604dce 2021-09-24 thomas
2447 10604dce 2021-09-24 thomas static const struct got_error *
2448 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2449 10604dce 2021-09-24 thomas {
2450 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2451 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2452 0ebf8283 2019-07-24 stsp }
2453 0ebf8283 2019-07-24 stsp
2454 10604dce 2021-09-24 thomas static const struct got_error *
2455 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2456 10604dce 2021-09-24 thomas {
2457 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2458 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2459 10604dce 2021-09-24 thomas }
2460 10604dce 2021-09-24 thomas
2461 517bab73 2019-03-11 stsp /*
2462 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2463 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2464 517bab73 2019-03-11 stsp */
2465 517bab73 2019-03-11 stsp static const struct got_error *
2466 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2467 517bab73 2019-03-11 stsp {
2468 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2469 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2470 517bab73 2019-03-11 stsp char *refname;
2471 517bab73 2019-03-11 stsp
2472 fcfaa6d5 2023-07-05 thomas err = get_base_ref_name(&refname, worktree);
2473 517bab73 2019-03-11 stsp if (err)
2474 517bab73 2019-03-11 stsp return err;
2475 0cd1c46a 2019-03-11 stsp
2476 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2477 0cd1c46a 2019-03-11 stsp if (err)
2478 0cd1c46a 2019-03-11 stsp goto done;
2479 0cd1c46a 2019-03-11 stsp
2480 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2481 0cd1c46a 2019-03-11 stsp done:
2482 0cd1c46a 2019-03-11 stsp free(refname);
2483 0cd1c46a 2019-03-11 stsp if (ref)
2484 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2485 3e3a69f1 2019-07-25 stsp return err;
2486 3e3a69f1 2019-07-25 stsp }
2487 3e3a69f1 2019-07-25 stsp
2488 3e3a69f1 2019-07-25 stsp static const struct got_error *
2489 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2490 3e3a69f1 2019-07-25 stsp {
2491 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2492 3e3a69f1 2019-07-25 stsp
2493 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2494 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2495 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2496 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2497 3e3a69f1 2019-07-25 stsp }
2498 9d31a1d8 2018-03-11 stsp return err;
2499 9d31a1d8 2018-03-11 stsp }
2500 9d31a1d8 2018-03-11 stsp
2501 3e3a69f1 2019-07-25 stsp
2502 ebf99748 2019-05-09 stsp static const struct got_error *
2503 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2504 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2505 ebf99748 2019-05-09 stsp {
2506 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2507 ebf99748 2019-05-09 stsp FILE *index = NULL;
2508 0cd1c46a 2019-03-11 stsp
2509 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2510 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2511 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2512 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2513 ebf99748 2019-05-09 stsp
2514 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2515 3e3a69f1 2019-07-25 stsp if (err)
2516 ebf99748 2019-05-09 stsp goto done;
2517 ebf99748 2019-05-09 stsp
2518 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2519 ebf99748 2019-05-09 stsp if (index == NULL) {
2520 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2521 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2522 ebf99748 2019-05-09 stsp } else {
2523 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2524 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2525 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2526 ebf99748 2019-05-09 stsp }
2527 ebf99748 2019-05-09 stsp done:
2528 ebf99748 2019-05-09 stsp if (err) {
2529 ebf99748 2019-05-09 stsp free(*fileindex_path);
2530 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2531 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2532 ebf99748 2019-05-09 stsp *fileindex = NULL;
2533 ebf99748 2019-05-09 stsp }
2534 ebf99748 2019-05-09 stsp return err;
2535 ebf99748 2019-05-09 stsp }
2536 c932eeeb 2019-05-22 stsp
2537 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2538 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2539 c932eeeb 2019-05-22 stsp const char *path;
2540 c932eeeb 2019-05-22 stsp size_t path_len;
2541 c932eeeb 2019-05-22 stsp const char *entry_name;
2542 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2543 a484d721 2019-06-10 stsp void *progress_arg;
2544 c932eeeb 2019-05-22 stsp };
2545 ebf99748 2019-05-09 stsp
2546 c932eeeb 2019-05-22 stsp static const struct got_error *
2547 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2548 c932eeeb 2019-05-22 stsp {
2549 1ee397ad 2019-07-12 stsp const struct got_error *err;
2550 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2551 c932eeeb 2019-05-22 stsp
2552 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2553 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2554 c932eeeb 2019-05-22 stsp return NULL;
2555 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2556 102ff934 2019-06-11 stsp return NULL;
2557 102ff934 2019-06-11 stsp
2558 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2559 a769b60b 2021-06-27 stsp return NULL;
2560 a769b60b 2021-06-27 stsp
2561 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2562 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2563 c932eeeb 2019-05-22 stsp return NULL;
2564 c932eeeb 2019-05-22 stsp
2565 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2566 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2567 edd02c5e 2019-07-11 stsp ie->path);
2568 1ee397ad 2019-07-12 stsp if (err)
2569 1ee397ad 2019-07-12 stsp return err;
2570 1ee397ad 2019-07-12 stsp }
2571 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2572 c932eeeb 2019-05-22 stsp return NULL;
2573 a615e0e7 2020-12-16 stsp }
2574 a615e0e7 2020-12-16 stsp
2575 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2576 a615e0e7 2020-12-16 stsp static const struct got_error *
2577 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2578 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2579 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2580 a615e0e7 2020-12-16 stsp {
2581 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2582 a615e0e7 2020-12-16 stsp
2583 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2584 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2585 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2586 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2587 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2588 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2589 a615e0e7 2020-12-16 stsp
2590 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2591 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2592 9c6338c4 2019-06-02 stsp }
2593 9c6338c4 2019-06-02 stsp
2594 9c6338c4 2019-06-02 stsp static const struct got_error *
2595 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2596 9c6338c4 2019-06-02 stsp {
2597 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2598 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2599 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2600 867630bb 2020-01-17 stsp struct timespec timeout;
2601 9c6338c4 2019-06-02 stsp
2602 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2603 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2604 9c6338c4 2019-06-02 stsp if (err)
2605 9c6338c4 2019-06-02 stsp goto done;
2606 9c6338c4 2019-06-02 stsp
2607 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2608 9c6338c4 2019-06-02 stsp if (err)
2609 9c6338c4 2019-06-02 stsp goto done;
2610 9c6338c4 2019-06-02 stsp
2611 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2612 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2613 9c6338c4 2019-06-02 stsp fileindex_path);
2614 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2615 9c6338c4 2019-06-02 stsp }
2616 867630bb 2020-01-17 stsp
2617 867630bb 2020-01-17 stsp /*
2618 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2619 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2620 867630bb 2020-01-17 stsp * was recorded in the file index.
2621 867630bb 2020-01-17 stsp */
2622 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2623 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2624 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2625 9c6338c4 2019-06-02 stsp done:
2626 9c6338c4 2019-06-02 stsp if (new_index)
2627 9c6338c4 2019-06-02 stsp fclose(new_index);
2628 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2629 9c6338c4 2019-06-02 stsp return err;
2630 c932eeeb 2019-05-22 stsp }
2631 6ced4176 2019-07-12 stsp
2632 6ced4176 2019-07-12 stsp static const struct got_error *
2633 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2634 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2635 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2636 945f9229 2022-04-16 thomas struct got_repository *repo)
2637 6ced4176 2019-07-12 stsp {
2638 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2639 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2640 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2641 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2642 6ced4176 2019-07-12 stsp
2643 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2644 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2645 6ced4176 2019-07-12 stsp *tree_id = NULL;
2646 6ced4176 2019-07-12 stsp
2647 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2648 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2649 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2650 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2651 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2652 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2653 6ced4176 2019-07-12 stsp goto done;
2654 6ced4176 2019-07-12 stsp }
2655 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2656 945f9229 2022-04-16 thomas worktree->path_prefix);
2657 6ced4176 2019-07-12 stsp if (err)
2658 6ced4176 2019-07-12 stsp goto done;
2659 6ced4176 2019-07-12 stsp return NULL;
2660 6ced4176 2019-07-12 stsp }
2661 6ced4176 2019-07-12 stsp
2662 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2663 6ced4176 2019-07-12 stsp
2664 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2665 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2666 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2667 6ced4176 2019-07-12 stsp goto done;
2668 6ced4176 2019-07-12 stsp }
2669 6ced4176 2019-07-12 stsp
2670 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2671 6ced4176 2019-07-12 stsp if (err)
2672 6ced4176 2019-07-12 stsp goto done;
2673 6ced4176 2019-07-12 stsp
2674 6ced4176 2019-07-12 stsp free(in_repo_path);
2675 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2676 6ced4176 2019-07-12 stsp
2677 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2678 6ced4176 2019-07-12 stsp if (err)
2679 6ced4176 2019-07-12 stsp goto done;
2680 c932eeeb 2019-05-22 stsp
2681 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2682 6ced4176 2019-07-12 stsp /* Check out a single file. */
2683 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2684 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2685 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2686 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2687 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2688 6ced4176 2019-07-12 stsp goto done;
2689 6ced4176 2019-07-12 stsp }
2690 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2691 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2692 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2693 6ced4176 2019-07-12 stsp goto done;
2694 6ced4176 2019-07-12 stsp }
2695 6ced4176 2019-07-12 stsp } else {
2696 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2697 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2698 6ced4176 2019-07-12 stsp if (err)
2699 6ced4176 2019-07-12 stsp return err;
2700 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2701 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2702 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2703 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2704 6ced4176 2019-07-12 stsp goto done;
2705 6ced4176 2019-07-12 stsp }
2706 6ced4176 2019-07-12 stsp }
2707 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2708 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2709 6ced4176 2019-07-12 stsp } else {
2710 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2711 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2712 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2713 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2714 6ced4176 2019-07-12 stsp goto done;
2715 6ced4176 2019-07-12 stsp }
2716 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2717 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2718 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2719 6ced4176 2019-07-12 stsp goto done;
2720 6ced4176 2019-07-12 stsp }
2721 6ced4176 2019-07-12 stsp }
2722 6ced4176 2019-07-12 stsp done:
2723 6ced4176 2019-07-12 stsp free(id);
2724 6ced4176 2019-07-12 stsp free(in_repo_path);
2725 6ced4176 2019-07-12 stsp if (err) {
2726 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2727 6ced4176 2019-07-12 stsp free(*tree_relpath);
2728 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2729 6ced4176 2019-07-12 stsp free(*tree_id);
2730 6ced4176 2019-07-12 stsp *tree_id = NULL;
2731 6ced4176 2019-07-12 stsp }
2732 07ed472d 2019-07-12 stsp return err;
2733 07ed472d 2019-07-12 stsp }
2734 07ed472d 2019-07-12 stsp
2735 07ed472d 2019-07-12 stsp static const struct got_error *
2736 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2737 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2738 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2739 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2740 07ed472d 2019-07-12 stsp {
2741 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2742 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2743 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2744 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2745 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2746 07ed472d 2019-07-12 stsp
2747 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2748 7f47418f 2019-12-20 stsp if (err) {
2749 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2750 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2751 7f47418f 2019-12-20 stsp goto done;
2752 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2753 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2754 7f47418f 2019-12-20 stsp if (err)
2755 7f47418f 2019-12-20 stsp return err;
2756 7f47418f 2019-12-20 stsp }
2757 07ed472d 2019-07-12 stsp
2758 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2759 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2760 07ed472d 2019-07-12 stsp if (err)
2761 07ed472d 2019-07-12 stsp goto done;
2762 07ed472d 2019-07-12 stsp
2763 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2764 07ed472d 2019-07-12 stsp if (err)
2765 07ed472d 2019-07-12 stsp goto done;
2766 07ed472d 2019-07-12 stsp
2767 07ed472d 2019-07-12 stsp if (entry_name &&
2768 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2769 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2770 07ed472d 2019-07-12 stsp goto done;
2771 07ed472d 2019-07-12 stsp }
2772 07ed472d 2019-07-12 stsp
2773 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2774 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2775 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2776 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2777 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2778 07ed472d 2019-07-12 stsp arg.repo = repo;
2779 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2780 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2781 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2782 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2783 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2784 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2785 07ed472d 2019-07-12 stsp done:
2786 07ed472d 2019-07-12 stsp if (tree)
2787 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2788 07ed472d 2019-07-12 stsp if (commit)
2789 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2790 6ced4176 2019-07-12 stsp return err;
2791 6ced4176 2019-07-12 stsp }
2792 6ced4176 2019-07-12 stsp
2793 9d31a1d8 2018-03-11 stsp const struct got_error *
2794 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2795 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2796 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2797 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2798 9d31a1d8 2018-03-11 stsp {
2799 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2800 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2801 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2802 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2803 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2804 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2805 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2806 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2807 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2808 f2ea84fa 2019-07-27 stsp int entry_type;
2809 f2ea84fa 2019-07-27 stsp char *relpath;
2810 f2ea84fa 2019-07-27 stsp char *entry_name;
2811 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2812 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2813 9d31a1d8 2018-03-11 stsp
2814 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2815 f2ea84fa 2019-07-27 stsp
2816 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2817 9d31a1d8 2018-03-11 stsp if (err)
2818 9d31a1d8 2018-03-11 stsp return err;
2819 945f9229 2022-04-16 thomas
2820 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2821 945f9229 2022-04-16 thomas worktree->base_commit_id);
2822 945f9229 2022-04-16 thomas if (err)
2823 945f9229 2022-04-16 thomas goto done;
2824 93a30277 2018-12-24 stsp
2825 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2826 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2827 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2828 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2829 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2830 f2ea84fa 2019-07-27 stsp goto done;
2831 f2ea84fa 2019-07-27 stsp }
2832 6ced4176 2019-07-12 stsp
2833 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2834 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2835 945f9229 2022-04-16 thomas worktree, repo);
2836 f2ea84fa 2019-07-27 stsp if (err) {
2837 f2ea84fa 2019-07-27 stsp free(tpd);
2838 6ced4176 2019-07-12 stsp goto done;
2839 6ced4176 2019-07-12 stsp }
2840 f2ea84fa 2019-07-27 stsp
2841 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2842 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2843 f2ea84fa 2019-07-27 stsp if (err) {
2844 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2845 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2846 f2ea84fa 2019-07-27 stsp free(tpd);
2847 f2ea84fa 2019-07-27 stsp goto done;
2848 f2ea84fa 2019-07-27 stsp }
2849 f2ea84fa 2019-07-27 stsp } else
2850 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2851 f2ea84fa 2019-07-27 stsp
2852 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2853 6ced4176 2019-07-12 stsp }
2854 6ced4176 2019-07-12 stsp
2855 51514078 2018-12-25 stsp /*
2856 51514078 2018-12-25 stsp * Read the file index.
2857 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2858 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2859 51514078 2018-12-25 stsp */
2860 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2861 8da9e5f4 2019-01-12 stsp if (err)
2862 c4cdcb68 2019-04-03 stsp goto done;
2863 c4cdcb68 2019-04-03 stsp
2864 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2865 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2866 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2867 f2ea84fa 2019-07-27 stsp
2868 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2869 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2870 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2871 f2ea84fa 2019-07-27 stsp if (err)
2872 f2ea84fa 2019-07-27 stsp break;
2873 f2ea84fa 2019-07-27 stsp
2874 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2875 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2876 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2877 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2878 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2879 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2880 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2881 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2882 f2ea84fa 2019-07-27 stsp if (err)
2883 f2ea84fa 2019-07-27 stsp break;
2884 f2ea84fa 2019-07-27 stsp
2885 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2886 711d9cd9 2019-07-12 stsp }
2887 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2888 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2889 af12c6b9 2019-06-04 stsp err = sync_err;
2890 9d31a1d8 2018-03-11 stsp done:
2891 9c6338c4 2019-06-02 stsp free(fileindex_path);
2892 edfa7d7f 2018-09-11 stsp if (tree)
2893 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2894 9d31a1d8 2018-03-11 stsp if (commit)
2895 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2896 5ade8233 2019-07-12 stsp if (fileindex)
2897 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2898 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2899 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2900 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2901 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2902 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2903 f2ea84fa 2019-07-27 stsp free(tpd);
2904 f2ea84fa 2019-07-27 stsp }
2905 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2906 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2907 9d31a1d8 2018-03-11 stsp err = unlockerr;
2908 f8d1f275 2019-02-04 stsp return err;
2909 f8d1f275 2019-02-04 stsp }
2910 f8d1f275 2019-02-04 stsp
2911 0529f8df 2023-03-10 thomas static const struct got_error *
2912 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2913 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2914 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2915 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2916 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2917 0529f8df 2023-03-10 thomas struct got_repository *repo,
2918 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2919 0529f8df 2023-03-10 thomas {
2920 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2921 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2922 0529f8df 2023-03-10 thomas
2923 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2924 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2925 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2926 0529f8df 2023-03-10 thomas restoring_missing_file,
2927 0529f8df 2023-03-10 thomas reverting_versioned_file,
2928 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2929 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2930 0529f8df 2023-03-10 thomas } else {
2931 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2932 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2933 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2934 0529f8df 2023-03-10 thomas path_is_unversioned, repo, progress_cb, progress_arg);
2935 0529f8df 2023-03-10 thomas }
2936 0529f8df 2023-03-10 thomas if (err)
2937 0529f8df 2023-03-10 thomas return err;
2938 0529f8df 2023-03-10 thomas if (ie == NULL) {
2939 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2940 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2941 0529f8df 2023-03-10 thomas if (err)
2942 0529f8df 2023-03-10 thomas return err;
2943 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2944 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2945 0529f8df 2023-03-10 thomas if (err) {
2946 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2947 0529f8df 2023-03-10 thomas return err;
2948 0529f8df 2023-03-10 thomas }
2949 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2950 0529f8df 2023-03-10 thomas if (err) {
2951 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2952 0529f8df 2023-03-10 thomas return err;
2953 0529f8df 2023-03-10 thomas }
2954 0529f8df 2023-03-10 thomas } else {
2955 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2956 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2957 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2958 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2959 0529f8df 2023-03-10 thomas if (err)
2960 0529f8df 2023-03-10 thomas return err;
2961 0529f8df 2023-03-10 thomas }
2962 0529f8df 2023-03-10 thomas
2963 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2964 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2965 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2966 0529f8df 2023-03-10 thomas }
2967 0529f8df 2023-03-10 thomas
2968 0529f8df 2023-03-10 thomas return NULL;
2969 0529f8df 2023-03-10 thomas }
2970 0529f8df 2023-03-10 thomas
2971 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2972 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2973 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2974 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2975 234035bc 2019-06-01 stsp void *progress_arg;
2976 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2977 234035bc 2019-06-01 stsp void *cancel_arg;
2978 f69721c3 2019-10-21 stsp const char *label_orig;
2979 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2980 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2981 234035bc 2019-06-01 stsp };
2982 234035bc 2019-06-01 stsp
2983 234035bc 2019-06-01 stsp static const struct got_error *
2984 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2985 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2986 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2987 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2988 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2989 234035bc 2019-06-01 stsp {
2990 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2991 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2992 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2993 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2994 234035bc 2019-06-01 stsp struct stat sb;
2995 234035bc 2019-06-01 stsp unsigned char status;
2996 234035bc 2019-06-01 stsp int local_changes_subsumed;
2997 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2998 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2999 4c3671a9 2023-07-26 thomas struct got_object_id *id = NULL;
3000 234035bc 2019-06-01 stsp
3001 234035bc 2019-06-01 stsp if (blob1 && blob2) {
3002 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3003 d6c87207 2019-08-02 stsp strlen(path2));
3004 1ee397ad 2019-07-12 stsp if (ie == NULL)
3005 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3006 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
3007 234035bc 2019-06-01 stsp
3008 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3009 234035bc 2019-06-01 stsp path2) == -1)
3010 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3011 234035bc 2019-06-01 stsp
3012 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3013 7f91a133 2019-12-13 stsp repo);
3014 234035bc 2019-06-01 stsp if (err)
3015 234035bc 2019-06-01 stsp goto done;
3016 234035bc 2019-06-01 stsp
3017 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3018 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3019 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3020 234035bc 2019-06-01 stsp goto done;
3021 234035bc 2019-06-01 stsp }
3022 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3023 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3024 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3025 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3026 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3027 234035bc 2019-06-01 stsp goto done;
3028 234035bc 2019-06-01 stsp }
3029 234035bc 2019-06-01 stsp
3030 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3031 36bf999c 2020-07-23 stsp char *link_target2;
3032 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3033 36bf999c 2020-07-23 stsp if (err)
3034 36bf999c 2020-07-23 stsp goto done;
3035 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3036 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3037 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3038 36bf999c 2020-07-23 stsp free(link_target2);
3039 af57b12a 2020-07-23 stsp } else {
3040 1af628f4 2021-06-03 stsp int fd;
3041 1af628f4 2021-06-03 stsp
3042 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3043 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3044 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3045 1af628f4 2021-06-03 stsp goto done;
3046 1af628f4 2021-06-03 stsp }
3047 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3048 1af628f4 2021-06-03 stsp f_orig, blob1);
3049 1af628f4 2021-06-03 stsp if (err)
3050 1af628f4 2021-06-03 stsp goto done;
3051 1af628f4 2021-06-03 stsp
3052 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3053 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3054 1af628f4 2021-06-03 stsp goto done;
3055 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3056 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3057 1af628f4 2021-06-03 stsp if (err)
3058 1af628f4 2021-06-03 stsp goto done;
3059 1af628f4 2021-06-03 stsp
3060 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3061 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3062 1af628f4 2021-06-03 stsp if (fd == -1) {
3063 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3064 1af628f4 2021-06-03 stsp ondisk_path);
3065 1af628f4 2021-06-03 stsp goto done;
3066 1af628f4 2021-06-03 stsp }
3067 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3068 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3069 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3070 1af628f4 2021-06-03 stsp ondisk_path);
3071 1af628f4 2021-06-03 stsp close(fd);
3072 1af628f4 2021-06-03 stsp goto done;
3073 1af628f4 2021-06-03 stsp }
3074 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3075 1af628f4 2021-06-03 stsp if (err)
3076 1af628f4 2021-06-03 stsp goto done;
3077 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3078 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3079 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3080 1af628f4 2021-06-03 stsp goto done;
3081 1af628f4 2021-06-03 stsp }
3082 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3083 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3084 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3085 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3086 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3087 af57b12a 2020-07-23 stsp }
3088 234035bc 2019-06-01 stsp } else if (blob1) {
3089 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3090 d6c87207 2019-08-02 stsp strlen(path1));
3091 1ee397ad 2019-07-12 stsp if (ie == NULL)
3092 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3093 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3094 2b92fad7 2019-06-02 stsp
3095 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3096 2b92fad7 2019-06-02 stsp path1) == -1)
3097 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3098 2b92fad7 2019-06-02 stsp
3099 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3100 7f91a133 2019-12-13 stsp repo);
3101 2b92fad7 2019-06-02 stsp if (err)
3102 2b92fad7 2019-06-02 stsp goto done;
3103 2b92fad7 2019-06-02 stsp
3104 2b92fad7 2019-06-02 stsp switch (status) {
3105 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
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 err = remove_ondisk_file(a->worktree->root_path, path1);
3111 2b92fad7 2019-06-02 stsp if (err)
3112 2b92fad7 2019-06-02 stsp goto done;
3113 2b92fad7 2019-06-02 stsp if (ie)
3114 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3115 2b92fad7 2019-06-02 stsp break;
3116 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3117 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3118 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3119 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3120 1ee397ad 2019-07-12 stsp if (err)
3121 1ee397ad 2019-07-12 stsp goto done;
3122 2b92fad7 2019-06-02 stsp if (ie)
3123 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3124 2b92fad7 2019-06-02 stsp break;
3125 4c3671a9 2023-07-26 thomas case GOT_STATUS_MODIFY: {
3126 4c3671a9 2023-07-26 thomas FILE *blob1_f;
3127 4c3671a9 2023-07-26 thomas off_t blob1_size;
3128 4c3671a9 2023-07-26 thomas int obj_type;
3129 4c3671a9 2023-07-26 thomas /*
3130 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3131 4c3671a9 2023-07-26 thomas * exists in the repository.
3132 4c3671a9 2023-07-26 thomas */
3133 4c3671a9 2023-07-26 thomas err = got_object_blob_file_create(&id, &blob1_f,
3134 4c3671a9 2023-07-26 thomas &blob1_size, path1);
3135 4c3671a9 2023-07-26 thomas if (err)
3136 4c3671a9 2023-07-26 thomas goto done;
3137 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3138 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3139 4c3671a9 2023-07-26 thomas goto done;
3140 4c3671a9 2023-07-26 thomas }
3141 4c3671a9 2023-07-26 thomas
3142 4c3671a9 2023-07-26 thomas /* Implied existence check. */
3143 4c3671a9 2023-07-26 thomas err = got_object_get_type(&obj_type, repo, id);
3144 4c3671a9 2023-07-26 thomas if (err) {
3145 4c3671a9 2023-07-26 thomas if (err->code != GOT_ERR_NO_OBJ)
3146 4c3671a9 2023-07-26 thomas goto done;
3147 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3148 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3149 4c3671a9 2023-07-26 thomas goto done;
3150 4c3671a9 2023-07-26 thomas } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3151 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3152 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3153 4c3671a9 2023-07-26 thomas goto done;
3154 4c3671a9 2023-07-26 thomas }
3155 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3156 4c3671a9 2023-07-26 thomas GOT_STATUS_DELETE, path1);
3157 4c3671a9 2023-07-26 thomas if (err)
3158 4c3671a9 2023-07-26 thomas goto done;
3159 4c3671a9 2023-07-26 thomas err = remove_ondisk_file(a->worktree->root_path,
3160 4c3671a9 2023-07-26 thomas path1);
3161 4c3671a9 2023-07-26 thomas if (err)
3162 4c3671a9 2023-07-26 thomas goto done;
3163 4c3671a9 2023-07-26 thomas if (ie)
3164 4c3671a9 2023-07-26 thomas got_fileindex_entry_mark_deleted_from_disk(ie);
3165 4c3671a9 2023-07-26 thomas break;
3166 4c3671a9 2023-07-26 thomas }
3167 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3168 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3169 e8f02263 2022-01-23 thomas off_t blob1_size;
3170 0a22ca1a 2020-09-23 stsp /*
3171 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3172 0a22ca1a 2020-09-23 stsp * exists in the repository.
3173 0a22ca1a 2020-09-23 stsp */
3174 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3175 e8f02263 2022-01-23 thomas &blob1_size, path1);
3176 0a22ca1a 2020-09-23 stsp if (err)
3177 0a22ca1a 2020-09-23 stsp goto done;
3178 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3179 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3180 4c3671a9 2023-07-26 thomas goto done;
3181 4c3671a9 2023-07-26 thomas }
3182 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3183 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3184 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3185 0a22ca1a 2020-09-23 stsp if (err)
3186 0a22ca1a 2020-09-23 stsp goto done;
3187 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3188 0a22ca1a 2020-09-23 stsp path1);
3189 0a22ca1a 2020-09-23 stsp if (err)
3190 0a22ca1a 2020-09-23 stsp goto done;
3191 0a22ca1a 2020-09-23 stsp if (ie)
3192 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3193 0a22ca1a 2020-09-23 stsp ie);
3194 0a22ca1a 2020-09-23 stsp } else {
3195 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3196 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3197 0a22ca1a 2020-09-23 stsp }
3198 0a22ca1a 2020-09-23 stsp if (err)
3199 0a22ca1a 2020-09-23 stsp goto done;
3200 0a22ca1a 2020-09-23 stsp break;
3201 0a22ca1a 2020-09-23 stsp }
3202 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3203 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3204 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3205 1ee397ad 2019-07-12 stsp if (err)
3206 1ee397ad 2019-07-12 stsp goto done;
3207 2b92fad7 2019-06-02 stsp break;
3208 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3209 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3210 1ee397ad 2019-07-12 stsp if (err)
3211 1ee397ad 2019-07-12 stsp goto done;
3212 2b92fad7 2019-06-02 stsp break;
3213 2b92fad7 2019-06-02 stsp default:
3214 2b92fad7 2019-06-02 stsp break;
3215 2b92fad7 2019-06-02 stsp }
3216 234035bc 2019-06-01 stsp } else if (blob2) {
3217 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3218 234035bc 2019-06-01 stsp path2) == -1)
3219 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3220 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3221 d6c87207 2019-08-02 stsp strlen(path2));
3222 234035bc 2019-06-01 stsp if (ie) {
3223 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3224 7f91a133 2019-12-13 stsp -1, NULL, repo);
3225 234035bc 2019-06-01 stsp if (err)
3226 234035bc 2019-06-01 stsp goto done;
3227 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3228 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3229 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3230 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3231 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3232 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3233 1ee397ad 2019-07-12 stsp status, path2);
3234 234035bc 2019-06-01 stsp goto done;
3235 234035bc 2019-06-01 stsp }
3236 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3237 36bf999c 2020-07-23 stsp char *link_target2;
3238 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3239 36bf999c 2020-07-23 stsp blob2);
3240 36bf999c 2020-07-23 stsp if (err)
3241 36bf999c 2020-07-23 stsp goto done;
3242 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3243 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3244 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3245 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3246 36bf999c 2020-07-23 stsp free(link_target2);
3247 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3248 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3249 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3250 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3251 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3252 526a746f 2020-07-23 stsp a->progress_arg);
3253 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3254 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3255 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3256 526a746f 2020-07-23 stsp }
3257 dfe9fba0 2020-07-23 stsp if (err)
3258 dfe9fba0 2020-07-23 stsp goto done;
3259 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3260 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3261 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3262 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3263 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3264 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3265 234035bc 2019-06-01 stsp if (err)
3266 234035bc 2019-06-01 stsp goto done;
3267 234035bc 2019-06-01 stsp }
3268 234035bc 2019-06-01 stsp } else {
3269 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3270 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3271 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3272 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3273 234035bc 2019-06-01 stsp if (err)
3274 2b92fad7 2019-06-02 stsp goto done;
3275 234035bc 2019-06-01 stsp }
3276 234035bc 2019-06-01 stsp }
3277 234035bc 2019-06-01 stsp done:
3278 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3279 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3280 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3281 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3282 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3283 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3284 1af628f4 2021-06-03 stsp free(id_str);
3285 4c3671a9 2023-07-26 thomas free(id);
3286 54d5be07 2021-06-03 stsp free(label_deriv2);
3287 234035bc 2019-06-01 stsp free(ondisk_path);
3288 234035bc 2019-06-01 stsp return err;
3289 234035bc 2019-06-01 stsp }
3290 6ebb2263 2023-07-26 thomas
3291 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args {
3292 6ebb2263 2023-07-26 thomas struct got_worktree *worktree;
3293 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb;
3294 6ebb2263 2023-07-26 thomas void *cancel_arg;
3295 6ebb2263 2023-07-26 thomas };
3296 234035bc 2019-06-01 stsp
3297 69de9dd4 2021-09-03 stsp static const struct got_error *
3298 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3299 69de9dd4 2021-09-03 stsp {
3300 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
3301 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args *a = arg;
3302 6ebb2263 2023-07-26 thomas
3303 6ebb2263 2023-07-26 thomas if (a->cancel_cb) {
3304 6ebb2263 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3305 6ebb2263 2023-07-26 thomas if (err)
3306 6ebb2263 2023-07-26 thomas return err;
3307 6ebb2263 2023-07-26 thomas }
3308 69de9dd4 2021-09-03 stsp
3309 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3310 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3311 6ebb2263 2023-07-26 thomas memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3312 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3313 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3314 69de9dd4 2021-09-03 stsp
3315 69de9dd4 2021-09-03 stsp return NULL;
3316 69de9dd4 2021-09-03 stsp }
3317 69de9dd4 2021-09-03 stsp
3318 349dfd1e 2023-07-23 thomas const struct got_error *
3319 349dfd1e 2023-07-23 thomas got_worktree_get_state(char *state, struct got_repository *repo,
3320 6ebb2263 2023-07-26 thomas struct got_worktree *worktree,
3321 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3322 349dfd1e 2023-07-23 thomas {
3323 349dfd1e 2023-07-23 thomas const struct got_error *err;
3324 349dfd1e 2023-07-23 thomas struct got_object_id *base_id, *head_id = NULL;
3325 349dfd1e 2023-07-23 thomas struct got_reference *head_ref;
3326 349dfd1e 2023-07-23 thomas struct got_fileindex *fileindex = NULL;
3327 349dfd1e 2023-07-23 thomas char *fileindex_path = NULL;
3328 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3329 349dfd1e 2023-07-23 thomas
3330 349dfd1e 2023-07-23 thomas if (worktree == NULL)
3331 349dfd1e 2023-07-23 thomas return got_error(GOT_ERR_NOT_WORKTREE);
3332 349dfd1e 2023-07-23 thomas
3333 349dfd1e 2023-07-23 thomas err = got_ref_open(&head_ref, repo,
3334 349dfd1e 2023-07-23 thomas got_worktree_get_head_ref_name(worktree), 0);
3335 349dfd1e 2023-07-23 thomas if (err)
3336 349dfd1e 2023-07-23 thomas return err;
3337 349dfd1e 2023-07-23 thomas
3338 349dfd1e 2023-07-23 thomas err = got_ref_resolve(&head_id, repo, head_ref);
3339 349dfd1e 2023-07-23 thomas if (err)
3340 349dfd1e 2023-07-23 thomas goto done;
3341 349dfd1e 2023-07-23 thomas
3342 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UNKNOWN;
3343 349dfd1e 2023-07-23 thomas base_id = got_worktree_get_base_commit_id(worktree);
3344 349dfd1e 2023-07-23 thomas
3345 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3346 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3347 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3348 6ebb2263 2023-07-26 thomas
3349 349dfd1e 2023-07-23 thomas if (got_object_id_cmp(base_id, head_id) == 0) {
3350 349dfd1e 2023-07-23 thomas err = open_fileindex(&fileindex, &fileindex_path, worktree);
3351 349dfd1e 2023-07-23 thomas if (err)
3352 349dfd1e 2023-07-23 thomas goto done;
3353 349dfd1e 2023-07-23 thomas
3354 349dfd1e 2023-07-23 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3355 6ebb2263 2023-07-26 thomas check_mixed_commits, &cma);
3356 349dfd1e 2023-07-23 thomas if (err == NULL)
3357 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UPTODATE;
3358 92845f09 2023-07-26 thomas else if (err->code == GOT_ERR_MIXED_COMMITS) {
3359 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3360 349dfd1e 2023-07-23 thomas err = NULL;
3361 92845f09 2023-07-26 thomas }
3362 92845f09 2023-07-26 thomas } else
3363 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3364 349dfd1e 2023-07-23 thomas
3365 349dfd1e 2023-07-23 thomas done:
3366 349dfd1e 2023-07-23 thomas free(head_id);
3367 349dfd1e 2023-07-23 thomas free(fileindex_path);
3368 349dfd1e 2023-07-23 thomas got_ref_close(head_ref);
3369 349dfd1e 2023-07-23 thomas if (fileindex != NULL)
3370 349dfd1e 2023-07-23 thomas got_fileindex_free(fileindex);
3371 349dfd1e 2023-07-23 thomas return err;
3372 349dfd1e 2023-07-23 thomas }
3373 349dfd1e 2023-07-23 thomas
3374 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3375 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3376 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3377 234035bc 2019-06-01 stsp struct got_repository *repo;
3378 234035bc 2019-06-01 stsp };
3379 234035bc 2019-06-01 stsp
3380 234035bc 2019-06-01 stsp static const struct got_error *
3381 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3382 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3383 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3384 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3385 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3386 234035bc 2019-06-01 stsp {
3387 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3388 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3389 234035bc 2019-06-01 stsp unsigned char status;
3390 234035bc 2019-06-01 stsp struct stat sb;
3391 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3392 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3393 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3394 234035bc 2019-06-01 stsp char *ondisk_path;
3395 234035bc 2019-06-01 stsp
3396 69de9dd4 2021-09-03 stsp if (id == NULL)
3397 69de9dd4 2021-09-03 stsp return NULL;
3398 234035bc 2019-06-01 stsp
3399 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3400 69de9dd4 2021-09-03 stsp if (ie == NULL)
3401 69de9dd4 2021-09-03 stsp return NULL;
3402 69de9dd4 2021-09-03 stsp
3403 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3404 234035bc 2019-06-01 stsp == -1)
3405 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3406 234035bc 2019-06-01 stsp
3407 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3408 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3409 5546d466 2021-09-02 stsp free(ondisk_path);
3410 234035bc 2019-06-01 stsp if (err)
3411 234035bc 2019-06-01 stsp return err;
3412 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3413 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3414 234035bc 2019-06-01 stsp
3415 234035bc 2019-06-01 stsp return NULL;
3416 234035bc 2019-06-01 stsp }
3417 234035bc 2019-06-01 stsp
3418 818c7501 2019-07-11 stsp static const struct got_error *
3419 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3420 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3421 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3422 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3423 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3424 234035bc 2019-06-01 stsp {
3425 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3426 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3427 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3428 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3429 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3430 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3431 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3432 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3433 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3434 234035bc 2019-06-01 stsp
3435 03415a1a 2019-06-02 stsp if (commit_id1) {
3436 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3437 945f9229 2022-04-16 thomas if (err)
3438 945f9229 2022-04-16 thomas goto done;
3439 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3440 03415a1a 2019-06-02 stsp worktree->path_prefix);
3441 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3442 03415a1a 2019-06-02 stsp goto done;
3443 69d57f3d 2020-07-31 stsp }
3444 69d57f3d 2020-07-31 stsp if (tree_id1) {
3445 69d57f3d 2020-07-31 stsp char *id_str;
3446 03415a1a 2019-06-02 stsp
3447 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3448 03415a1a 2019-06-02 stsp if (err)
3449 03415a1a 2019-06-02 stsp goto done;
3450 f69721c3 2019-10-21 stsp
3451 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3452 f69721c3 2019-10-21 stsp if (err)
3453 f69721c3 2019-10-21 stsp goto done;
3454 f69721c3 2019-10-21 stsp
3455 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3456 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3457 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3458 f69721c3 2019-10-21 stsp free(id_str);
3459 f69721c3 2019-10-21 stsp goto done;
3460 f69721c3 2019-10-21 stsp }
3461 f69721c3 2019-10-21 stsp free(id_str);
3462 a0f32f33 2022-06-13 thomas
3463 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3464 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3465 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3466 a0f32f33 2022-06-13 thomas goto done;
3467 a0f32f33 2022-06-13 thomas }
3468 03415a1a 2019-06-02 stsp }
3469 234035bc 2019-06-01 stsp
3470 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3471 945f9229 2022-04-16 thomas if (err)
3472 945f9229 2022-04-16 thomas goto done;
3473 945f9229 2022-04-16 thomas
3474 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3475 234035bc 2019-06-01 stsp worktree->path_prefix);
3476 234035bc 2019-06-01 stsp if (err)
3477 234035bc 2019-06-01 stsp goto done;
3478 234035bc 2019-06-01 stsp
3479 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3480 234035bc 2019-06-01 stsp if (err)
3481 234035bc 2019-06-01 stsp goto done;
3482 234035bc 2019-06-01 stsp
3483 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3484 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3485 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3486 19a6a6b5 2022-07-01 thomas goto done;
3487 19a6a6b5 2022-07-01 thomas }
3488 19a6a6b5 2022-07-01 thomas
3489 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3490 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3491 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3492 a0f32f33 2022-06-13 thomas goto done;
3493 a0f32f33 2022-06-13 thomas }
3494 a0f32f33 2022-06-13 thomas
3495 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3496 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3497 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3498 19a6a6b5 2022-07-01 thomas goto done;
3499 19a6a6b5 2022-07-01 thomas }
3500 19a6a6b5 2022-07-01 thomas
3501 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3502 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3503 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3504 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3505 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3506 69de9dd4 2021-09-03 stsp if (err)
3507 69de9dd4 2021-09-03 stsp goto done;
3508 69de9dd4 2021-09-03 stsp
3509 234035bc 2019-06-01 stsp arg.worktree = worktree;
3510 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3511 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3512 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3513 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3514 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3515 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3516 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3517 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3518 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3519 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3520 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3521 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3522 af12c6b9 2019-06-04 stsp err = sync_err;
3523 234035bc 2019-06-01 stsp done:
3524 945f9229 2022-04-16 thomas if (commit1)
3525 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3526 945f9229 2022-04-16 thomas if (commit2)
3527 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3528 234035bc 2019-06-01 stsp if (tree1)
3529 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3530 234035bc 2019-06-01 stsp if (tree2)
3531 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3532 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3533 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3534 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3535 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3536 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3537 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3538 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3539 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3540 f69721c3 2019-10-21 stsp free(label_orig);
3541 818c7501 2019-07-11 stsp return err;
3542 818c7501 2019-07-11 stsp }
3543 234035bc 2019-06-01 stsp
3544 818c7501 2019-07-11 stsp const struct got_error *
3545 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3546 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3547 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3548 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3549 818c7501 2019-07-11 stsp {
3550 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3551 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3552 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3553 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3554 818c7501 2019-07-11 stsp
3555 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3556 818c7501 2019-07-11 stsp if (err)
3557 818c7501 2019-07-11 stsp return err;
3558 818c7501 2019-07-11 stsp
3559 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3560 818c7501 2019-07-11 stsp if (err)
3561 818c7501 2019-07-11 stsp goto done;
3562 818c7501 2019-07-11 stsp
3563 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3564 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3565 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3566 6ebb2263 2023-07-26 thomas
3567 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3568 6ebb2263 2023-07-26 thomas &cma);
3569 818c7501 2019-07-11 stsp if (err)
3570 818c7501 2019-07-11 stsp goto done;
3571 818c7501 2019-07-11 stsp
3572 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3573 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3574 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3575 818c7501 2019-07-11 stsp done:
3576 818c7501 2019-07-11 stsp if (fileindex)
3577 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3578 818c7501 2019-07-11 stsp free(fileindex_path);
3579 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3580 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3581 234035bc 2019-06-01 stsp err = unlockerr;
3582 234035bc 2019-06-01 stsp return err;
3583 234035bc 2019-06-01 stsp }
3584 234035bc 2019-06-01 stsp
3585 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3586 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3587 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3588 927df6b7 2019-02-10 stsp const char *status_path;
3589 927df6b7 2019-02-10 stsp size_t status_path_len;
3590 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3591 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3592 f8d1f275 2019-02-04 stsp void *status_arg;
3593 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3594 f8d1f275 2019-02-04 stsp void *cancel_arg;
3595 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3596 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3597 f2a9dc41 2019-12-13 tracey int report_unchanged;
3598 3143d852 2020-06-25 stsp int no_ignores;
3599 f8d1f275 2019-02-04 stsp };
3600 88d0e355 2019-08-03 stsp
3601 f8d1f275 2019-02-04 stsp static const struct got_error *
3602 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3603 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3604 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3605 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3606 927df6b7 2019-02-10 stsp {
3607 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3608 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3609 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3610 927df6b7 2019-02-10 stsp struct stat sb;
3611 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3612 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3613 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3614 927df6b7 2019-02-10 stsp
3615 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3616 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3617 98eaaa12 2019-08-03 stsp if (err)
3618 98eaaa12 2019-08-03 stsp return err;
3619 98eaaa12 2019-08-03 stsp
3620 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3621 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3622 98eaaa12 2019-08-03 stsp return NULL;
3623 98eaaa12 2019-08-03 stsp
3624 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3625 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3626 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3627 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3628 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3629 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3630 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3631 43010591 2023-02-17 thomas &staged_blob_id, ie);
3632 98eaaa12 2019-08-03 stsp }
3633 98eaaa12 2019-08-03 stsp
3634 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3635 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3636 927df6b7 2019-02-10 stsp }
3637 927df6b7 2019-02-10 stsp
3638 927df6b7 2019-02-10 stsp static const struct got_error *
3639 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3640 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3641 f8d1f275 2019-02-04 stsp {
3642 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3643 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3644 f8d1f275 2019-02-04 stsp char *abspath;
3645 f8d1f275 2019-02-04 stsp
3646 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3647 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3648 eb6fb9cd 2023-07-26 thomas if (err)
3649 eb6fb9cd 2023-07-26 thomas return err;
3650 eb6fb9cd 2023-07-26 thomas }
3651 0584f854 2019-04-06 stsp
3652 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3653 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3654 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3655 927df6b7 2019-02-10 stsp return NULL;
3656 927df6b7 2019-02-10 stsp
3657 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3658 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3659 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3660 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3661 f8d1f275 2019-02-04 stsp } else {
3662 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3663 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3664 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3665 f8d1f275 2019-02-04 stsp }
3666 f8d1f275 2019-02-04 stsp
3667 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3668 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3669 f8d1f275 2019-02-04 stsp free(abspath);
3670 9d31a1d8 2018-03-11 stsp return err;
3671 9d31a1d8 2018-03-11 stsp }
3672 f8d1f275 2019-02-04 stsp
3673 f8d1f275 2019-02-04 stsp static const struct got_error *
3674 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3675 f8d1f275 2019-02-04 stsp {
3676 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
3677 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3678 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3679 2ec1f75b 2019-03-26 stsp unsigned char status;
3680 927df6b7 2019-02-10 stsp
3681 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3682 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3683 eb6fb9cd 2023-07-26 thomas if (err)
3684 eb6fb9cd 2023-07-26 thomas return err;
3685 eb6fb9cd 2023-07-26 thomas }
3686 0584f854 2019-04-06 stsp
3687 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3688 927df6b7 2019-02-10 stsp return NULL;
3689 927df6b7 2019-02-10 stsp
3690 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3691 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3692 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3693 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3694 2ec1f75b 2019-03-26 stsp else
3695 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3696 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3697 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3698 6841da00 2019-08-08 stsp }
3699 6841da00 2019-08-08 stsp
3700 ef20f542 2022-06-26 thomas static void
3701 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3702 6841da00 2019-08-08 stsp {
3703 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3704 6841da00 2019-08-08 stsp
3705 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3706 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3707 21c2d8be 2023-01-10 thomas
3708 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3709 6841da00 2019-08-08 stsp }
3710 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3711 6841da00 2019-08-08 stsp }
3712 6841da00 2019-08-08 stsp
3713 6841da00 2019-08-08 stsp static const struct got_error *
3714 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3715 6841da00 2019-08-08 stsp {
3716 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3717 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3718 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3719 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3720 6841da00 2019-08-08 stsp size_t linesize = 0;
3721 6841da00 2019-08-08 stsp ssize_t linelen;
3722 6841da00 2019-08-08 stsp
3723 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3724 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3725 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3726 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3727 6841da00 2019-08-08 stsp
3728 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3729 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3730 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3731 bd8de430 2019-10-04 stsp
3732 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3733 bd8de430 2019-10-04 stsp if (line[0] == '#')
3734 bd8de430 2019-10-04 stsp continue;
3735 bd8de430 2019-10-04 stsp
3736 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3737 bd8de430 2019-10-04 stsp if (line[0] == '!')
3738 bd8de430 2019-10-04 stsp continue;
3739 bd8de430 2019-10-04 stsp
3740 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3741 6841da00 2019-08-08 stsp line) == -1) {
3742 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3743 6841da00 2019-08-08 stsp goto done;
3744 6841da00 2019-08-08 stsp }
3745 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3746 6841da00 2019-08-08 stsp if (err)
3747 6841da00 2019-08-08 stsp goto done;
3748 6841da00 2019-08-08 stsp }
3749 6841da00 2019-08-08 stsp if (ferror(f)) {
3750 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3751 6841da00 2019-08-08 stsp goto done;
3752 6841da00 2019-08-08 stsp }
3753 6841da00 2019-08-08 stsp
3754 6841da00 2019-08-08 stsp dirpath = strdup(path);
3755 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3756 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3757 6841da00 2019-08-08 stsp goto done;
3758 6841da00 2019-08-08 stsp }
3759 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3760 6841da00 2019-08-08 stsp done:
3761 6841da00 2019-08-08 stsp free(line);
3762 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3763 6841da00 2019-08-08 stsp free(dirpath);
3764 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3765 6841da00 2019-08-08 stsp }
3766 6841da00 2019-08-08 stsp return err;
3767 1b5d300f 2023-02-20 thomas }
3768 1b5d300f 2023-02-20 thomas
3769 1b5d300f 2023-02-20 thomas static int
3770 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3771 1b5d300f 2023-02-20 thomas int flags)
3772 1b5d300f 2023-02-20 thomas {
3773 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3774 1b5d300f 2023-02-20 thomas
3775 1b5d300f 2023-02-20 thomas /*
3776 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3777 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3778 1b5d300f 2023-02-20 thomas */
3779 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3780 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3781 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3782 1b5d300f 2023-02-20 thomas
3783 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3784 1b5d300f 2023-02-20 thomas }
3785 1b5d300f 2023-02-20 thomas
3786 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3787 6841da00 2019-08-08 stsp }
3788 6841da00 2019-08-08 stsp
3789 ef20f542 2022-06-26 thomas static int
3790 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3791 6841da00 2019-08-08 stsp {
3792 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3793 bd8de430 2019-10-04 stsp
3794 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3795 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3796 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3797 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3798 bd8de430 2019-10-04 stsp
3799 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3800 1b5d300f 2023-02-20 thomas const char *p;
3801 6841da00 2019-08-08 stsp
3802 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3803 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3804 bd8de430 2019-10-04 stsp continue;
3805 bd8de430 2019-10-04 stsp p = path;
3806 bd8de430 2019-10-04 stsp while (*p) {
3807 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3808 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3809 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3810 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3811 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3812 bd8de430 2019-10-04 stsp p++;
3813 bd8de430 2019-10-04 stsp while (*p == '/')
3814 bd8de430 2019-10-04 stsp p++;
3815 bd8de430 2019-10-04 stsp continue;
3816 bd8de430 2019-10-04 stsp }
3817 bd8de430 2019-10-04 stsp return 1;
3818 bd8de430 2019-10-04 stsp }
3819 bd8de430 2019-10-04 stsp }
3820 bd8de430 2019-10-04 stsp }
3821 bd8de430 2019-10-04 stsp
3822 6841da00 2019-08-08 stsp /*
3823 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3824 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3825 6841da00 2019-08-08 stsp * ignores backwards.
3826 6841da00 2019-08-08 stsp */
3827 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3828 6841da00 2019-08-08 stsp while (pe) {
3829 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3830 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3831 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3832 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3833 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3834 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3835 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3836 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3837 1b5d300f 2023-02-20 thomas path, flags))
3838 6841da00 2019-08-08 stsp continue;
3839 6841da00 2019-08-08 stsp return 1;
3840 6841da00 2019-08-08 stsp }
3841 6841da00 2019-08-08 stsp }
3842 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3843 6841da00 2019-08-08 stsp }
3844 6841da00 2019-08-08 stsp
3845 6841da00 2019-08-08 stsp return 0;
3846 6841da00 2019-08-08 stsp }
3847 6841da00 2019-08-08 stsp
3848 6841da00 2019-08-08 stsp static const struct got_error *
3849 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3850 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3851 6841da00 2019-08-08 stsp {
3852 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3853 6841da00 2019-08-08 stsp char *ignorespath;
3854 886cec17 2019-12-15 stsp int fd = -1;
3855 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3856 6841da00 2019-08-08 stsp
3857 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3858 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3859 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3860 6841da00 2019-08-08 stsp
3861 886cec17 2019-12-15 stsp if (dirfd != -1) {
3862 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3863 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3864 886cec17 2019-12-15 stsp if (fd == -1) {
3865 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3866 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3867 886cec17 2019-12-15 stsp ignorespath);
3868 886cec17 2019-12-15 stsp } else {
3869 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3870 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3871 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3872 886cec17 2019-12-15 stsp ignorespath);
3873 886cec17 2019-12-15 stsp else {
3874 886cec17 2019-12-15 stsp fd = -1;
3875 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3876 886cec17 2019-12-15 stsp }
3877 886cec17 2019-12-15 stsp }
3878 886cec17 2019-12-15 stsp } else {
3879 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3880 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3881 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3882 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3883 886cec17 2019-12-15 stsp ignorespath);
3884 886cec17 2019-12-15 stsp } else
3885 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3886 886cec17 2019-12-15 stsp }
3887 6841da00 2019-08-08 stsp
3888 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3889 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3890 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3891 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3892 6841da00 2019-08-08 stsp free(ignorespath);
3893 6841da00 2019-08-08 stsp return err;
3894 f8d1f275 2019-02-04 stsp }
3895 f8d1f275 2019-02-04 stsp
3896 f8d1f275 2019-02-04 stsp static const struct got_error *
3897 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3898 6092c299 2021-10-04 thomas int dirfd)
3899 f8d1f275 2019-02-04 stsp {
3900 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3901 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3902 f8d1f275 2019-02-04 stsp char *path = NULL;
3903 6092c299 2021-10-04 thomas
3904 6092c299 2021-10-04 thomas if (ignore != NULL)
3905 6092c299 2021-10-04 thomas *ignore = 0;
3906 f8d1f275 2019-02-04 stsp
3907 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3908 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3909 eb6fb9cd 2023-07-26 thomas if (err)
3910 eb6fb9cd 2023-07-26 thomas return err;
3911 eb6fb9cd 2023-07-26 thomas }
3912 0584f854 2019-04-06 stsp
3913 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3914 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3915 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3916 f8d1f275 2019-02-04 stsp } else {
3917 f8d1f275 2019-02-04 stsp path = de->d_name;
3918 f8d1f275 2019-02-04 stsp }
3919 f8d1f275 2019-02-04 stsp
3920 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3921 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3922 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3923 6092c299 2021-10-04 thomas *ignore = 1;
3924 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3925 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3926 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3927 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3928 f8d1f275 2019-02-04 stsp if (parent_path[0])
3929 f8d1f275 2019-02-04 stsp free(path);
3930 b72f483a 2019-02-05 stsp return err;
3931 f8d1f275 2019-02-04 stsp }
3932 f8d1f275 2019-02-04 stsp
3933 347d1d3e 2019-07-12 stsp static const struct got_error *
3934 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3935 3143d852 2020-06-25 stsp {
3936 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3937 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3938 3143d852 2020-06-25 stsp
3939 3143d852 2020-06-25 stsp if (a->no_ignores)
3940 3143d852 2020-06-25 stsp return NULL;
3941 3143d852 2020-06-25 stsp
3942 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3943 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3944 3143d852 2020-06-25 stsp if (err)
3945 3143d852 2020-06-25 stsp return err;
3946 3143d852 2020-06-25 stsp
3947 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3948 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3949 3143d852 2020-06-25 stsp
3950 3143d852 2020-06-25 stsp return err;
3951 3143d852 2020-06-25 stsp }
3952 3143d852 2020-06-25 stsp
3953 3143d852 2020-06-25 stsp static const struct got_error *
3954 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3955 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3956 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3957 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3958 abb4604f 2019-07-27 stsp {
3959 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3960 abb4604f 2019-07-27 stsp struct stat sb;
3961 ff56836b 2021-07-08 stsp
3962 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3963 abb4604f 2019-07-27 stsp if (ie)
3964 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3965 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3966 abb4604f 2019-07-27 stsp
3967 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3968 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3969 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3970 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3971 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3972 abb4604f 2019-07-27 stsp }
3973 abb4604f 2019-07-27 stsp
3974 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3975 fe1d8685 2022-02-12 thomas return NULL;
3976 fe1d8685 2022-02-12 thomas
3977 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3978 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3979 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3980 abb4604f 2019-07-27 stsp
3981 abb4604f 2019-07-27 stsp return NULL;
3982 3143d852 2020-06-25 stsp }
3983 3143d852 2020-06-25 stsp
3984 3143d852 2020-06-25 stsp static const struct got_error *
3985 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3986 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3987 3143d852 2020-06-25 stsp {
3988 3143d852 2020-06-25 stsp const struct got_error *err;
3989 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3990 3143d852 2020-06-25 stsp
3991 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3992 3143d852 2020-06-25 stsp ".cvsignore");
3993 3143d852 2020-06-25 stsp if (err)
3994 3143d852 2020-06-25 stsp return err;
3995 3143d852 2020-06-25 stsp
3996 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3997 3143d852 2020-06-25 stsp ".gitignore");
3998 3143d852 2020-06-25 stsp if (err)
3999 3143d852 2020-06-25 stsp return err;
4000 3143d852 2020-06-25 stsp
4001 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
4002 3143d852 2020-06-25 stsp if (err) {
4003 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
4004 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
4005 3143d852 2020-06-25 stsp return err;
4006 3143d852 2020-06-25 stsp }
4007 3143d852 2020-06-25 stsp for (;;) {
4008 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4009 3143d852 2020-06-25 stsp ".cvsignore");
4010 3143d852 2020-06-25 stsp if (err)
4011 3143d852 2020-06-25 stsp break;
4012 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4013 3143d852 2020-06-25 stsp ".gitignore");
4014 3143d852 2020-06-25 stsp if (err)
4015 3143d852 2020-06-25 stsp break;
4016 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
4017 3143d852 2020-06-25 stsp if (err) {
4018 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
4019 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
4020 3143d852 2020-06-25 stsp break;
4021 3143d852 2020-06-25 stsp }
4022 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
4023 ff56836b 2021-07-08 stsp break;
4024 b737c85a 2020-06-26 stsp free(parent_path);
4025 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
4026 b737c85a 2020-06-26 stsp next_parent_path = NULL;
4027 3143d852 2020-06-25 stsp }
4028 3143d852 2020-06-25 stsp
4029 b737c85a 2020-06-26 stsp free(parent_path);
4030 b737c85a 2020-06-26 stsp free(next_parent_path);
4031 3143d852 2020-06-25 stsp return err;
4032 abb4604f 2019-07-27 stsp }
4033 f8836425 2023-04-14 thomas
4034 f8836425 2023-04-14 thomas struct find_missing_children_args {
4035 f8836425 2023-04-14 thomas const char *parent_path;
4036 f8836425 2023-04-14 thomas size_t parent_len;
4037 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
4038 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
4039 f8836425 2023-04-14 thomas void *cancel_arg;
4040 f8836425 2023-04-14 thomas };
4041 f8836425 2023-04-14 thomas
4042 abb4604f 2019-07-27 stsp static const struct got_error *
4043 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
4044 f8836425 2023-04-14 thomas {
4045 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4046 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
4047 f8836425 2023-04-14 thomas
4048 f8836425 2023-04-14 thomas if (a->cancel_cb) {
4049 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
4050 f8836425 2023-04-14 thomas if (err)
4051 f8836425 2023-04-14 thomas return err;
4052 f8836425 2023-04-14 thomas }
4053 f8836425 2023-04-14 thomas
4054 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4055 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
4056 f8836425 2023-04-14 thomas
4057 f8836425 2023-04-14 thomas return err;
4058 f8836425 2023-04-14 thomas }
4059 f8836425 2023-04-14 thomas
4060 f8836425 2023-04-14 thomas static const struct got_error *
4061 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
4062 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
4063 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
4064 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
4065 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
4066 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
4067 f8836425 2023-04-14 thomas {
4068 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4069 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
4070 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
4071 f8836425 2023-04-14 thomas
4072 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
4073 f8836425 2023-04-14 thomas if (cancel_cb) {
4074 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
4075 f8836425 2023-04-14 thomas if (err)
4076 f8836425 2023-04-14 thomas break;
4077 f8836425 2023-04-14 thomas }
4078 f8836425 2023-04-14 thomas
4079 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4080 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
4081 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
4082 f8836425 2023-04-14 thomas ondisk_path = NULL;
4083 f8836425 2023-04-14 thomas break;
4084 f8836425 2023-04-14 thomas }
4085 f8836425 2023-04-14 thomas
4086 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
4087 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
4088 f8836425 2023-04-14 thomas ignores, no_ignores);
4089 f8836425 2023-04-14 thomas if (err)
4090 f8836425 2023-04-14 thomas break;
4091 f8836425 2023-04-14 thomas
4092 f8836425 2023-04-14 thomas free(ondisk_path);
4093 f8836425 2023-04-14 thomas ondisk_path = NULL;
4094 f8836425 2023-04-14 thomas }
4095 f8836425 2023-04-14 thomas
4096 f8836425 2023-04-14 thomas free(ondisk_path);
4097 f8836425 2023-04-14 thomas return err;
4098 f8836425 2023-04-14 thomas }
4099 f8836425 2023-04-14 thomas
4100 f8836425 2023-04-14 thomas static const struct got_error *
4101 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4102 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4103 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4104 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4105 f2a9dc41 2019-12-13 tracey int report_unchanged)
4106 f8d1f275 2019-02-04 stsp {
4107 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4108 6fc93f37 2019-12-13 stsp int fd = -1;
4109 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4110 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4111 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4112 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
4113 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
4114 3143d852 2020-06-25 stsp
4115 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4116 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
4117 f8d1f275 2019-02-04 stsp
4118 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4119 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4120 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4121 8dc303cc 2019-07-27 stsp
4122 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4123 a78810f8 2022-03-13 thomas if (ie) {
4124 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
4125 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
4126 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
4127 a78810f8 2022-03-13 thomas goto done;
4128 f8836425 2023-04-14 thomas } else {
4129 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
4130 f8836425 2023-04-14 thomas fmca.parent_path = path;
4131 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
4132 f8836425 2023-04-14 thomas fmca.children = &missing_children;
4133 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
4134 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
4135 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
4136 f8836425 2023-04-14 thomas find_missing_children, &fmca);
4137 f8836425 2023-04-14 thomas if (err)
4138 f8836425 2023-04-14 thomas goto done;
4139 a78810f8 2022-03-13 thomas }
4140 a78810f8 2022-03-13 thomas
4141 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4142 6fc93f37 2019-12-13 stsp if (fd == -1) {
4143 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4144 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
4145 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4146 ff56836b 2021-07-08 stsp else {
4147 ff56836b 2021-07-08 stsp if (!no_ignores) {
4148 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4149 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4150 ff56836b 2021-07-08 stsp if (err)
4151 ff56836b 2021-07-08 stsp goto done;
4152 ff56836b 2021-07-08 stsp }
4153 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4154 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4155 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4156 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4157 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4158 f8836425 2023-04-14 thomas if (err)
4159 f8836425 2023-04-14 thomas goto done;
4160 f8836425 2023-04-14 thomas } else {
4161 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4162 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4163 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4164 f8836425 2023-04-14 thomas &ignores, no_ignores,
4165 f8836425 2023-04-14 thomas status_cb, status_arg,
4166 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4167 6f97aa83 2023-04-14 thomas if (err)
4168 6f97aa83 2023-04-14 thomas goto done;
4169 f8836425 2023-04-14 thomas }
4170 ff56836b 2021-07-08 stsp }
4171 abb4604f 2019-07-27 stsp } else {
4172 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4173 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4174 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4175 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4176 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4177 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4178 abb4604f 2019-07-27 stsp arg.status_path = path;
4179 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4180 abb4604f 2019-07-27 stsp arg.repo = repo;
4181 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4182 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4183 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4184 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4185 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4186 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4187 022fae89 2019-12-06 tracey if (!no_ignores) {
4188 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4189 3143d852 2020-06-25 stsp worktree->root_path, path);
4190 3143d852 2020-06-25 stsp if (err)
4191 3143d852 2020-06-25 stsp goto done;
4192 022fae89 2019-12-06 tracey }
4193 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4194 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4195 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4196 927df6b7 2019-02-10 stsp }
4197 3143d852 2020-06-25 stsp done:
4198 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4199 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4200 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4201 927df6b7 2019-02-10 stsp free(ondisk_path);
4202 347d1d3e 2019-07-12 stsp return err;
4203 347d1d3e 2019-07-12 stsp }
4204 347d1d3e 2019-07-12 stsp
4205 347d1d3e 2019-07-12 stsp const struct got_error *
4206 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4207 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4208 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4209 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4210 347d1d3e 2019-07-12 stsp {
4211 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4212 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4213 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4214 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4215 347d1d3e 2019-07-12 stsp
4216 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4217 347d1d3e 2019-07-12 stsp if (err)
4218 347d1d3e 2019-07-12 stsp return err;
4219 347d1d3e 2019-07-12 stsp
4220 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4221 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4222 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4223 f6343036 2021-06-22 stsp no_ignores, 0);
4224 72ea6654 2019-07-27 stsp if (err)
4225 72ea6654 2019-07-27 stsp break;
4226 72ea6654 2019-07-27 stsp }
4227 f8d1f275 2019-02-04 stsp free(fileindex_path);
4228 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4229 f8d1f275 2019-02-04 stsp return err;
4230 f8d1f275 2019-02-04 stsp }
4231 6c7ab921 2019-03-18 stsp
4232 6c7ab921 2019-03-18 stsp const struct got_error *
4233 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4234 6c7ab921 2019-03-18 stsp const char *arg)
4235 6c7ab921 2019-03-18 stsp {
4236 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4237 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4238 6c7ab921 2019-03-18 stsp size_t len;
4239 00bb5ea0 2020-07-23 stsp struct stat sb;
4240 01740607 2020-11-04 stsp char *abspath = NULL;
4241 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4242 6c7ab921 2019-03-18 stsp
4243 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4244 6c7ab921 2019-03-18 stsp
4245 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4246 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4247 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4248 00bb5ea0 2020-07-23 stsp
4249 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4250 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4251 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4252 00bb5ea0 2020-07-23 stsp goto done;
4253 00bb5ea0 2020-07-23 stsp }
4254 727173c3 2020-11-06 stsp sb.st_mode = 0;
4255 00bb5ea0 2020-07-23 stsp }
4256 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4257 00bb5ea0 2020-07-23 stsp /*
4258 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4259 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4260 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4261 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4262 00bb5ea0 2020-07-23 stsp */
4263 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4264 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4265 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4266 00bb5ea0 2020-07-23 stsp goto done;
4267 00bb5ea0 2020-07-23 stsp }
4268 00bb5ea0 2020-07-23 stsp
4269 00bb5ea0 2020-07-23 stsp }
4270 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4271 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4272 00bb5ea0 2020-07-23 stsp if (err)
4273 d0710d08 2019-07-22 stsp goto done;
4274 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4275 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4276 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4277 00bb5ea0 2020-07-23 stsp goto done;
4278 00bb5ea0 2020-07-23 stsp }
4279 00bb5ea0 2020-07-23 stsp } else {
4280 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4281 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4282 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4283 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4284 00bb5ea0 2020-07-23 stsp goto done;
4285 00bb5ea0 2020-07-23 stsp }
4286 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4287 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4288 01740607 2020-11-04 stsp goto done;
4289 01740607 2020-11-04 stsp }
4290 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4291 01740607 2020-11-04 stsp sizeof(canonpath));
4292 01740607 2020-11-04 stsp if (err)
4293 00bb5ea0 2020-07-23 stsp goto done;
4294 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4295 01740607 2020-11-04 stsp if (resolved == NULL) {
4296 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4297 01740607 2020-11-04 stsp goto done;
4298 00bb5ea0 2020-07-23 stsp }
4299 d0710d08 2019-07-22 stsp }
4300 d0710d08 2019-07-22 stsp }
4301 6c7ab921 2019-03-18 stsp
4302 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4303 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4304 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4305 6c7ab921 2019-03-18 stsp goto done;
4306 6c7ab921 2019-03-18 stsp }
4307 6c7ab921 2019-03-18 stsp
4308 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4309 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4310 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4311 f6d88e1a 2019-05-29 stsp if (err)
4312 f6d88e1a 2019-05-29 stsp goto done;
4313 f6d88e1a 2019-05-29 stsp } else {
4314 f6d88e1a 2019-05-29 stsp path = strdup("");
4315 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4316 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4317 f6d88e1a 2019-05-29 stsp goto done;
4318 f6d88e1a 2019-05-29 stsp }
4319 6c7ab921 2019-03-18 stsp }
4320 6c7ab921 2019-03-18 stsp
4321 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4322 6c7ab921 2019-03-18 stsp len = strlen(path);
4323 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4324 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4325 6c7ab921 2019-03-18 stsp len--;
4326 6c7ab921 2019-03-18 stsp }
4327 6c7ab921 2019-03-18 stsp done:
4328 01740607 2020-11-04 stsp free(abspath);
4329 6c7ab921 2019-03-18 stsp free(resolved);
4330 d0710d08 2019-07-22 stsp free(cwd);
4331 6c7ab921 2019-03-18 stsp if (err == NULL)
4332 6c7ab921 2019-03-18 stsp *wt_path = path;
4333 6c7ab921 2019-03-18 stsp else
4334 6c7ab921 2019-03-18 stsp free(path);
4335 d00136be 2019-03-26 stsp return err;
4336 d00136be 2019-03-26 stsp }
4337 d00136be 2019-03-26 stsp
4338 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4339 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4340 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4341 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4342 4e68cba3 2019-11-23 stsp void *progress_arg;
4343 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4344 4e68cba3 2019-11-23 stsp };
4345 4e68cba3 2019-11-23 stsp
4346 b21ebdb0 2023-06-22 thomas static int
4347 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4348 b21ebdb0 2023-06-22 thomas {
4349 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4350 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4351 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4352 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4353 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4354 b21ebdb0 2023-06-22 thomas }
4355 b21ebdb0 2023-06-22 thomas
4356 4e68cba3 2019-11-23 stsp static const struct got_error *
4357 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4358 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4359 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4360 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4361 07f5b47a 2019-06-02 stsp {
4362 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4363 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4364 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4365 6d022e97 2019-08-04 stsp struct stat sb;
4366 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4367 07f5b47a 2019-06-02 stsp
4368 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4369 dbb83fbd 2019-12-12 stsp relpath) == -1)
4370 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4371 dbb83fbd 2019-12-12 stsp
4372 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4373 6d022e97 2019-08-04 stsp if (ie) {
4374 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4375 12463d8b 2019-12-13 stsp de_name, a->repo);
4376 6d022e97 2019-08-04 stsp if (err)
4377 dbb83fbd 2019-12-12 stsp goto done;
4378 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4379 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4380 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4381 dbb83fbd 2019-12-12 stsp goto done;
4382 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4383 dbb83fbd 2019-12-12 stsp if (err)
4384 dbb83fbd 2019-12-12 stsp goto done;
4385 6d022e97 2019-08-04 stsp }
4386 07f5b47a 2019-06-02 stsp
4387 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4388 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4389 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4390 84bf00a6 2022-02-12 thomas else
4391 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4392 dbb83fbd 2019-12-12 stsp goto done;
4393 4e68cba3 2019-11-23 stsp }
4394 07f5b47a 2019-06-02 stsp
4395 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4396 4e68cba3 2019-11-23 stsp if (err)
4397 4e68cba3 2019-11-23 stsp goto done;
4398 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4399 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4400 3969253a 2020-03-07 stsp if (err) {
4401 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4402 3969253a 2020-03-07 stsp goto done;
4403 3969253a 2020-03-07 stsp }
4404 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4405 07f5b47a 2019-06-02 stsp if (err) {
4406 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4407 4e68cba3 2019-11-23 stsp goto done;
4408 07f5b47a 2019-06-02 stsp }
4409 4e68cba3 2019-11-23 stsp done:
4410 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4411 dbb83fbd 2019-12-12 stsp if (err)
4412 dbb83fbd 2019-12-12 stsp return err;
4413 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4414 dbb83fbd 2019-12-12 stsp return NULL;
4415 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4416 07f5b47a 2019-06-02 stsp }
4417 07f5b47a 2019-06-02 stsp
4418 d00136be 2019-03-26 stsp const struct got_error *
4419 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4420 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4421 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4422 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4423 d00136be 2019-03-26 stsp {
4424 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4425 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4426 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4427 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4428 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4429 d00136be 2019-03-26 stsp
4430 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4431 d00136be 2019-03-26 stsp if (err)
4432 d00136be 2019-03-26 stsp return err;
4433 d00136be 2019-03-26 stsp
4434 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4435 d00136be 2019-03-26 stsp if (err)
4436 d00136be 2019-03-26 stsp goto done;
4437 d00136be 2019-03-26 stsp
4438 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4439 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4440 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4441 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4442 4e68cba3 2019-11-23 stsp saa.repo = repo;
4443 4e68cba3 2019-11-23 stsp
4444 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4445 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4446 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4447 1dd54920 2019-05-11 stsp if (err)
4448 af12c6b9 2019-06-04 stsp break;
4449 1dd54920 2019-05-11 stsp }
4450 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4451 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4452 af12c6b9 2019-06-04 stsp err = sync_err;
4453 d00136be 2019-03-26 stsp done:
4454 fb399478 2019-07-12 stsp free(fileindex_path);
4455 d00136be 2019-03-26 stsp if (fileindex)
4456 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4457 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4458 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4459 d00136be 2019-03-26 stsp err = unlockerr;
4460 6c7ab921 2019-03-18 stsp return err;
4461 6c7ab921 2019-03-18 stsp }
4462 17ed4618 2019-06-02 stsp
4463 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4464 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4465 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4466 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4467 f2a9dc41 2019-12-13 tracey void *progress_arg;
4468 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4469 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4470 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4471 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4472 8ec7352a 2023-06-01 thomas const char *status_path;
4473 8ec7352a 2023-06-01 thomas size_t status_path_len;
4474 766841c2 2020-08-13 stsp const char *status_codes;
4475 f2a9dc41 2019-12-13 tracey };
4476 f2a9dc41 2019-12-13 tracey
4477 f2a9dc41 2019-12-13 tracey static const struct got_error *
4478 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4479 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4480 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4481 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4482 17ed4618 2019-06-02 stsp {
4483 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4484 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4485 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4486 17ed4618 2019-06-02 stsp struct stat sb;
4487 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4488 d64cc78a 2022-01-25 thomas
4489 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4490 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4491 d64cc78a 2022-01-25 thomas return NULL;
4492 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4493 d64cc78a 2022-01-25 thomas }
4494 17ed4618 2019-06-02 stsp
4495 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4496 17ed4618 2019-06-02 stsp if (ie == NULL)
4497 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4498 2ec1f75b 2019-03-26 stsp
4499 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4500 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4501 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4502 9acbc4fa 2019-08-03 stsp return NULL;
4503 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4504 9acbc4fa 2019-08-03 stsp }
4505 9acbc4fa 2019-08-03 stsp
4506 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4507 f2a9dc41 2019-12-13 tracey relpath) == -1)
4508 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4509 f2a9dc41 2019-12-13 tracey
4510 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4511 12463d8b 2019-12-13 stsp a->repo);
4512 17ed4618 2019-06-02 stsp if (err)
4513 f2a9dc41 2019-12-13 tracey goto done;
4514 17ed4618 2019-06-02 stsp
4515 766841c2 2020-08-13 stsp if (a->status_codes) {
4516 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4517 766841c2 2020-08-13 stsp int i;
4518 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4519 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4520 766841c2 2020-08-13 stsp break;
4521 766841c2 2020-08-13 stsp }
4522 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4523 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4524 766841c2 2020-08-13 stsp free(ondisk_path);
4525 766841c2 2020-08-13 stsp return NULL;
4526 766841c2 2020-08-13 stsp }
4527 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4528 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4529 766841c2 2020-08-13 stsp static char msg[64];
4530 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4531 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4532 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4533 766841c2 2020-08-13 stsp goto done;
4534 766841c2 2020-08-13 stsp }
4535 766841c2 2020-08-13 stsp }
4536 766841c2 2020-08-13 stsp
4537 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4538 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4539 f2a9dc41 2019-12-13 tracey goto done;
4540 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4541 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4542 f2a9dc41 2019-12-13 tracey goto done;
4543 f2a9dc41 2019-12-13 tracey }
4544 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4545 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4546 d64cc78a 2022-01-25 thomas goto done;
4547 d64cc78a 2022-01-25 thomas }
4548 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4549 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4550 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4551 f2a9dc41 2019-12-13 tracey goto done;
4552 f2a9dc41 2019-12-13 tracey }
4553 17ed4618 2019-06-02 stsp }
4554 17ed4618 2019-06-02 stsp
4555 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4556 20a2ad1c 2020-10-20 stsp size_t root_len;
4557 20a2ad1c 2020-10-20 stsp
4558 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4559 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4560 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4561 12463d8b 2019-12-13 stsp ondisk_path);
4562 12463d8b 2019-12-13 stsp goto done;
4563 12463d8b 2019-12-13 stsp }
4564 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4565 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4566 12463d8b 2019-12-13 stsp goto done;
4567 15341bfd 2020-03-05 tracey }
4568 15341bfd 2020-03-05 tracey
4569 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4570 20a2ad1c 2020-10-20 stsp do {
4571 20a2ad1c 2020-10-20 stsp char *parent;
4572 8ec7352a 2023-06-01 thomas
4573 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4574 20a2ad1c 2020-10-20 stsp if (err)
4575 2513f20a 2020-10-20 stsp goto done;
4576 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4577 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4578 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4579 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4580 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4581 8ec7352a 2023-06-01 thomas a->status_path_len))
4582 8ec7352a 2023-06-01 thomas break;
4583 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4584 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4585 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4586 20a2ad1c 2020-10-20 stsp ondisk_path);
4587 15341bfd 2020-03-05 tracey break;
4588 15341bfd 2020-03-05 tracey }
4589 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4590 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4591 f2a9dc41 2019-12-13 tracey }
4592 17ed4618 2019-06-02 stsp
4593 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4594 f2a9dc41 2019-12-13 tracey done:
4595 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4596 f2a9dc41 2019-12-13 tracey if (err)
4597 f2a9dc41 2019-12-13 tracey return err;
4598 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4599 f2a9dc41 2019-12-13 tracey return NULL;
4600 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4601 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4602 17ed4618 2019-06-02 stsp }
4603 17ed4618 2019-06-02 stsp
4604 2ec1f75b 2019-03-26 stsp const struct got_error *
4605 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4606 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4607 766841c2 2020-08-13 stsp const char *status_codes,
4608 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4609 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4610 2ec1f75b 2019-03-26 stsp {
4611 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4612 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4613 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4614 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4615 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4616 2ec1f75b 2019-03-26 stsp
4617 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4618 2ec1f75b 2019-03-26 stsp if (err)
4619 2ec1f75b 2019-03-26 stsp return err;
4620 2ec1f75b 2019-03-26 stsp
4621 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4622 2ec1f75b 2019-03-26 stsp if (err)
4623 2ec1f75b 2019-03-26 stsp goto done;
4624 f2a9dc41 2019-12-13 tracey
4625 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4626 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4627 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4628 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4629 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4630 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4631 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4632 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4633 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4634 2ec1f75b 2019-03-26 stsp
4635 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4636 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4637 8ec7352a 2023-06-01 thomas
4638 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4639 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4640 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4641 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4642 8ec7352a 2023-06-01 thomas goto done;
4643 8ec7352a 2023-06-01 thomas }
4644 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4645 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4646 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4647 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4648 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4649 17ed4618 2019-06-02 stsp if (err)
4650 af12c6b9 2019-06-04 stsp break;
4651 2ec1f75b 2019-03-26 stsp }
4652 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4653 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4654 af12c6b9 2019-06-04 stsp err = sync_err;
4655 2ec1f75b 2019-03-26 stsp done:
4656 fb399478 2019-07-12 stsp free(fileindex_path);
4657 2ec1f75b 2019-03-26 stsp if (fileindex)
4658 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4659 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4660 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4661 2ec1f75b 2019-03-26 stsp err = unlockerr;
4662 33aa809d 2019-08-08 stsp return err;
4663 33aa809d 2019-08-08 stsp }
4664 33aa809d 2019-08-08 stsp
4665 33aa809d 2019-08-08 stsp static const struct got_error *
4666 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4667 33aa809d 2019-08-08 stsp {
4668 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4669 33aa809d 2019-08-08 stsp char *line = NULL;
4670 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4671 33aa809d 2019-08-08 stsp ssize_t linelen;
4672 33aa809d 2019-08-08 stsp
4673 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4674 33aa809d 2019-08-08 stsp if (linelen == -1) {
4675 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4676 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4677 33aa809d 2019-08-08 stsp goto done;
4678 33aa809d 2019-08-08 stsp }
4679 33aa809d 2019-08-08 stsp return NULL;
4680 33aa809d 2019-08-08 stsp }
4681 33aa809d 2019-08-08 stsp if (outfile) {
4682 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4683 33aa809d 2019-08-08 stsp if (n != linelen) {
4684 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4685 33aa809d 2019-08-08 stsp goto done;
4686 33aa809d 2019-08-08 stsp }
4687 33aa809d 2019-08-08 stsp }
4688 33aa809d 2019-08-08 stsp if (rejectfile) {
4689 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4690 33aa809d 2019-08-08 stsp if (n != linelen)
4691 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4692 33aa809d 2019-08-08 stsp }
4693 33aa809d 2019-08-08 stsp done:
4694 33aa809d 2019-08-08 stsp free(line);
4695 2ec1f75b 2019-03-26 stsp return err;
4696 2ec1f75b 2019-03-26 stsp }
4697 1f1abb7e 2019-08-08 stsp
4698 33aa809d 2019-08-08 stsp static const struct got_error *
4699 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4700 33aa809d 2019-08-08 stsp {
4701 33aa809d 2019-08-08 stsp char *line = NULL;
4702 33aa809d 2019-08-08 stsp size_t linesize = 0;
4703 33aa809d 2019-08-08 stsp ssize_t linelen;
4704 33aa809d 2019-08-08 stsp
4705 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4706 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4707 500467ff 2019-09-25 hiltjo if (ferror(f))
4708 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4709 500467ff 2019-09-25 hiltjo return NULL;
4710 500467ff 2019-09-25 hiltjo }
4711 33aa809d 2019-08-08 stsp free(line);
4712 33aa809d 2019-08-08 stsp return NULL;
4713 33aa809d 2019-08-08 stsp }
4714 33aa809d 2019-08-08 stsp
4715 33aa809d 2019-08-08 stsp static const struct got_error *
4716 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4717 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4718 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4719 abc59930 2021-09-05 naddy {
4720 33aa809d 2019-08-08 stsp const struct got_error *err;
4721 33aa809d 2019-08-08 stsp
4722 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4723 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4724 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4725 33aa809d 2019-08-08 stsp if (err)
4726 33aa809d 2019-08-08 stsp return err;
4727 33aa809d 2019-08-08 stsp (*line_cur1)++;
4728 33aa809d 2019-08-08 stsp }
4729 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4730 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4731 33aa809d 2019-08-08 stsp if (rejectfile)
4732 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4733 33aa809d 2019-08-08 stsp else
4734 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4735 33aa809d 2019-08-08 stsp if (err)
4736 33aa809d 2019-08-08 stsp return err;
4737 33aa809d 2019-08-08 stsp (*line_cur2)++;
4738 33aa809d 2019-08-08 stsp }
4739 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4740 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4741 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4742 33aa809d 2019-08-08 stsp if (err)
4743 33aa809d 2019-08-08 stsp return err;
4744 33aa809d 2019-08-08 stsp (*line_cur2)++;
4745 33aa809d 2019-08-08 stsp }
4746 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4747 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4748 33aa809d 2019-08-08 stsp if (rejectfile)
4749 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4750 33aa809d 2019-08-08 stsp else
4751 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4752 33aa809d 2019-08-08 stsp if (err)
4753 33aa809d 2019-08-08 stsp return err;
4754 33aa809d 2019-08-08 stsp (*line_cur1)++;
4755 33aa809d 2019-08-08 stsp }
4756 f1e81a05 2019-08-10 stsp
4757 f1e81a05 2019-08-10 stsp return NULL;
4758 f1e81a05 2019-08-10 stsp }
4759 f1e81a05 2019-08-10 stsp
4760 f1e81a05 2019-08-10 stsp static const struct got_error *
4761 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4762 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4763 f1e81a05 2019-08-10 stsp {
4764 f1e81a05 2019-08-10 stsp const struct got_error *err;
4765 f1e81a05 2019-08-10 stsp
4766 f1e81a05 2019-08-10 stsp if (outfile) {
4767 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4768 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4769 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4770 f1e81a05 2019-08-10 stsp if (err)
4771 f1e81a05 2019-08-10 stsp return err;
4772 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4773 f1e81a05 2019-08-10 stsp }
4774 f1e81a05 2019-08-10 stsp }
4775 f1e81a05 2019-08-10 stsp if (rejectfile) {
4776 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4777 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4778 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4779 f1e81a05 2019-08-10 stsp if (err)
4780 f1e81a05 2019-08-10 stsp return err;
4781 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4782 f1e81a05 2019-08-10 stsp }
4783 33aa809d 2019-08-08 stsp }
4784 33aa809d 2019-08-08 stsp
4785 33aa809d 2019-08-08 stsp return NULL;
4786 33aa809d 2019-08-08 stsp }
4787 33aa809d 2019-08-08 stsp
4788 33aa809d 2019-08-08 stsp static const struct got_error *
4789 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4790 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4791 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4792 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4793 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4794 33aa809d 2019-08-08 stsp {
4795 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4796 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4797 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4798 33aa809d 2019-08-08 stsp FILE *hunkfile;
4799 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4800 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4801 fe621944 2020-11-10 stsp int rc;
4802 33aa809d 2019-08-08 stsp
4803 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4804 33aa809d 2019-08-08 stsp
4805 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4806 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4807 fe621944 2020-11-10 stsp start_old = cc.left.start;
4808 fe621944 2020-11-10 stsp end_old = cc.left.end;
4809 fe621944 2020-11-10 stsp start_new = cc.right.start;
4810 fe621944 2020-11-10 stsp end_new = cc.right.end;
4811 33aa809d 2019-08-08 stsp
4812 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4813 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4814 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4815 33aa809d 2019-08-08 stsp
4816 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4817 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4818 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4819 33aa809d 2019-08-08 stsp
4820 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4821 fe621944 2020-11-10 stsp if (diff_state == NULL)
4822 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4823 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4824 fe621944 2020-11-10 stsp
4825 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4826 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4827 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4828 33aa809d 2019-08-08 stsp goto done;
4829 33aa809d 2019-08-08 stsp }
4830 fe621944 2020-11-10 stsp
4831 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4832 fe621944 2020-11-10 stsp diff_result, &cc);
4833 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4834 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4835 33aa809d 2019-08-08 stsp goto done;
4836 33aa809d 2019-08-08 stsp }
4837 fe621944 2020-11-10 stsp
4838 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4839 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4840 33aa809d 2019-08-08 stsp goto done;
4841 33aa809d 2019-08-08 stsp }
4842 33aa809d 2019-08-08 stsp
4843 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4844 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4845 33aa809d 2019-08-08 stsp if (err)
4846 33aa809d 2019-08-08 stsp goto done;
4847 33aa809d 2019-08-08 stsp
4848 33aa809d 2019-08-08 stsp switch (*choice) {
4849 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4850 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4851 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4852 33aa809d 2019-08-08 stsp break;
4853 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4854 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4855 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4856 33aa809d 2019-08-08 stsp break;
4857 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4858 33aa809d 2019-08-08 stsp break;
4859 33aa809d 2019-08-08 stsp default:
4860 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4861 33aa809d 2019-08-08 stsp break;
4862 33aa809d 2019-08-08 stsp }
4863 33aa809d 2019-08-08 stsp done:
4864 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4865 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4866 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4867 33aa809d 2019-08-08 stsp return err;
4868 33aa809d 2019-08-08 stsp }
4869 33aa809d 2019-08-08 stsp
4870 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4871 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4872 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4873 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4874 1f1abb7e 2019-08-08 stsp void *progress_arg;
4875 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4876 33aa809d 2019-08-08 stsp void *patch_arg;
4877 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4878 10604dce 2021-09-24 thomas int unlink_added_files;
4879 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4880 1f1abb7e 2019-08-08 stsp };
4881 a129376b 2019-03-28 stsp
4882 e20a8b6f 2019-06-04 stsp static const struct got_error *
4883 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4884 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4885 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4886 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4887 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4888 33aa809d 2019-08-08 stsp {
4889 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4890 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4891 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4892 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4893 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4894 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4895 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4896 fe621944 2020-11-10 stsp struct stat sb2;
4897 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4898 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4899 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4900 33aa809d 2019-08-08 stsp
4901 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4902 33aa809d 2019-08-08 stsp
4903 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4904 33aa809d 2019-08-08 stsp if (err)
4905 33aa809d 2019-08-08 stsp return err;
4906 33aa809d 2019-08-08 stsp
4907 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4908 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4909 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4910 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4911 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4912 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4913 fa3cef63 2020-07-23 stsp goto done;
4914 fa3cef63 2020-07-23 stsp }
4915 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4916 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4917 48b4f239 2021-12-31 thomas if (link_len == -1) {
4918 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4919 48b4f239 2021-12-31 thomas path2);
4920 48b4f239 2021-12-31 thomas }
4921 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4922 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4923 12463d8b 2019-12-13 stsp }
4924 12463d8b 2019-12-13 stsp } else {
4925 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4926 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4927 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4928 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4929 fa3cef63 2020-07-23 stsp goto done;
4930 fa3cef63 2020-07-23 stsp }
4931 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4932 fa3cef63 2020-07-23 stsp sizeof(link_target));
4933 fa3cef63 2020-07-23 stsp if (link_len == -1)
4934 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4935 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4936 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4937 12463d8b 2019-12-13 stsp }
4938 1ebedb77 2019-10-19 stsp }
4939 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4940 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4941 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4942 fa3cef63 2020-07-23 stsp goto done;
4943 fa3cef63 2020-07-23 stsp }
4944 1ebedb77 2019-10-19 stsp
4945 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4946 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4947 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4948 fa3cef63 2020-07-23 stsp goto done;
4949 fa3cef63 2020-07-23 stsp }
4950 fa3cef63 2020-07-23 stsp fd2 = -1;
4951 fa3cef63 2020-07-23 stsp } else {
4952 fa3cef63 2020-07-23 stsp size_t n;
4953 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4954 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4955 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4956 fa3cef63 2020-07-23 stsp goto done;
4957 fa3cef63 2020-07-23 stsp }
4958 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4959 fa3cef63 2020-07-23 stsp if (n != link_len) {
4960 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4961 fa3cef63 2020-07-23 stsp goto done;
4962 fa3cef63 2020-07-23 stsp }
4963 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4964 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4965 fa3cef63 2020-07-23 stsp goto done;
4966 fa3cef63 2020-07-23 stsp }
4967 fa3cef63 2020-07-23 stsp rewind(f2);
4968 33aa809d 2019-08-08 stsp }
4969 33aa809d 2019-08-08 stsp
4970 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4971 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4972 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4973 f4ae6ddb 2022-07-01 thomas goto done;
4974 f4ae6ddb 2022-07-01 thomas }
4975 f4ae6ddb 2022-07-01 thomas
4976 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4977 33aa809d 2019-08-08 stsp if (err)
4978 33aa809d 2019-08-08 stsp goto done;
4979 33aa809d 2019-08-08 stsp
4980 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4981 33aa809d 2019-08-08 stsp if (err)
4982 33aa809d 2019-08-08 stsp goto done;
4983 33aa809d 2019-08-08 stsp
4984 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4985 33aa809d 2019-08-08 stsp if (err)
4986 33aa809d 2019-08-08 stsp goto done;
4987 33aa809d 2019-08-08 stsp
4988 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4989 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4990 33aa809d 2019-08-08 stsp if (err)
4991 33aa809d 2019-08-08 stsp goto done;
4992 33aa809d 2019-08-08 stsp
4993 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4994 fc2a50f2 2022-11-01 thomas "");
4995 33aa809d 2019-08-08 stsp if (err)
4996 33aa809d 2019-08-08 stsp goto done;
4997 33aa809d 2019-08-08 stsp
4998 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4999 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
5000 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5001 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
5002 fe621944 2020-11-10 stsp
5003 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
5004 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5005 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
5006 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
5007 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
5008 fe621944 2020-11-10 stsp nchanges++;
5009 fe621944 2020-11-10 stsp }
5010 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5011 33aa809d 2019-08-08 stsp int choice;
5012 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
5013 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
5014 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
5015 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
5016 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
5017 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
5018 33aa809d 2019-08-08 stsp if (err)
5019 33aa809d 2019-08-08 stsp goto done;
5020 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5021 33aa809d 2019-08-08 stsp have_content = 1;
5022 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5023 33aa809d 2019-08-08 stsp break;
5024 33aa809d 2019-08-08 stsp }
5025 1ebedb77 2019-10-19 stsp if (have_content) {
5026 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5027 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
5028 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
5029 1ebedb77 2019-10-19 stsp if (err)
5030 1ebedb77 2019-10-19 stsp goto done;
5031 1ebedb77 2019-10-19 stsp
5032 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
5033 a2c162eb 2022-10-30 thomas mode_t mode;
5034 a2c162eb 2022-10-30 thomas
5035 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
5036 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
5037 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
5038 fa3cef63 2020-07-23 stsp goto done;
5039 fa3cef63 2020-07-23 stsp }
5040 1ebedb77 2019-10-19 stsp }
5041 1ebedb77 2019-10-19 stsp }
5042 33aa809d 2019-08-08 stsp done:
5043 33aa809d 2019-08-08 stsp free(id_str);
5044 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5045 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5046 33aa809d 2019-08-08 stsp if (blob)
5047 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5048 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5049 fe621944 2020-11-10 stsp if (err == NULL)
5050 fe621944 2020-11-10 stsp err = free_err;
5051 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5052 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5053 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5054 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5055 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5056 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5057 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5058 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5059 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5060 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5061 33aa809d 2019-08-08 stsp if (err || !have_content) {
5062 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5063 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5064 33aa809d 2019-08-08 stsp free(*path_outfile);
5065 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5066 33aa809d 2019-08-08 stsp }
5067 33aa809d 2019-08-08 stsp free(path1);
5068 33aa809d 2019-08-08 stsp return err;
5069 33aa809d 2019-08-08 stsp }
5070 33aa809d 2019-08-08 stsp
5071 33aa809d 2019-08-08 stsp static const struct got_error *
5072 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5073 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5074 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5075 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5076 a129376b 2019-03-28 stsp {
5077 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5078 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5079 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5080 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5081 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
5082 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5083 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5084 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5085 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5086 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5087 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5088 f4ae6ddb 2022-07-01 thomas int fd = -1;
5089 a129376b 2019-03-28 stsp
5090 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5091 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5092 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5093 d3bcc3d1 2019-08-08 stsp return NULL;
5094 3d69ad8d 2019-08-17 semarie
5095 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5096 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5097 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5098 d3bcc3d1 2019-08-08 stsp
5099 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5100 65084dad 2019-08-08 stsp if (ie == NULL)
5101 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5102 a129376b 2019-03-28 stsp
5103 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5104 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5105 a129376b 2019-03-28 stsp if (err) {
5106 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5107 a129376b 2019-03-28 stsp goto done;
5108 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5109 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5110 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5111 e20a8b6f 2019-06-04 stsp goto done;
5112 e20a8b6f 2019-06-04 stsp }
5113 a129376b 2019-03-28 stsp }
5114 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5115 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5116 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5117 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5118 a129376b 2019-03-28 stsp goto done;
5119 a129376b 2019-03-28 stsp }
5120 a129376b 2019-03-28 stsp } else {
5121 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5122 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5123 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5124 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5125 a129376b 2019-03-28 stsp goto done;
5126 a129376b 2019-03-28 stsp }
5127 a129376b 2019-03-28 stsp } else {
5128 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5129 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5130 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5131 a129376b 2019-03-28 stsp goto done;
5132 a129376b 2019-03-28 stsp }
5133 a129376b 2019-03-28 stsp }
5134 a129376b 2019-03-28 stsp }
5135 a129376b 2019-03-28 stsp
5136 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
5137 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
5138 945f9229 2022-04-16 thomas if (err)
5139 945f9229 2022-04-16 thomas goto done;
5140 945f9229 2022-04-16 thomas
5141 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5142 a9fa2909 2019-07-27 stsp if (err) {
5143 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5144 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5145 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5146 a9fa2909 2019-07-27 stsp goto done;
5147 a9fa2909 2019-07-27 stsp } else {
5148 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5149 a9fa2909 2019-07-27 stsp if (err)
5150 a9fa2909 2019-07-27 stsp goto done;
5151 a9fa2909 2019-07-27 stsp
5152 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5153 1233e6b6 2020-10-19 stsp if (err)
5154 a9fa2909 2019-07-27 stsp goto done;
5155 a9fa2909 2019-07-27 stsp
5156 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5157 1233e6b6 2020-10-19 stsp free(te_name);
5158 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5159 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5160 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5161 a9fa2909 2019-07-27 stsp goto done;
5162 a9fa2909 2019-07-27 stsp }
5163 a129376b 2019-03-28 stsp }
5164 a129376b 2019-03-28 stsp
5165 a129376b 2019-03-28 stsp switch (status) {
5166 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5167 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5168 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5169 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5170 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5171 33aa809d 2019-08-08 stsp if (err)
5172 33aa809d 2019-08-08 stsp goto done;
5173 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5174 33aa809d 2019-08-08 stsp break;
5175 33aa809d 2019-08-08 stsp }
5176 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5177 1f1abb7e 2019-08-08 stsp ie->path);
5178 1ee397ad 2019-07-12 stsp if (err)
5179 1ee397ad 2019-07-12 stsp goto done;
5180 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5181 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5182 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5183 8641a332 2023-04-14 thomas
5184 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5185 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5186 8641a332 2023-04-14 thomas
5187 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5188 8641a332 2023-04-14 thomas entry) {
5189 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5190 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5191 8641a332 2023-04-14 thomas continue;
5192 8641a332 2023-04-14 thomas do_unlink = 1;
5193 8641a332 2023-04-14 thomas break;
5194 8641a332 2023-04-14 thomas }
5195 10604dce 2021-09-24 thomas }
5196 8641a332 2023-04-14 thomas
5197 8641a332 2023-04-14 thomas if (do_unlink) {
5198 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5199 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5200 8641a332 2023-04-14 thomas relpath) == -1) {
5201 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5202 8641a332 2023-04-14 thomas goto done;
5203 8641a332 2023-04-14 thomas }
5204 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5205 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5206 8641a332 2023-04-14 thomas ondisk_path);
5207 8641a332 2023-04-14 thomas break;
5208 8641a332 2023-04-14 thomas }
5209 10604dce 2021-09-24 thomas }
5210 10604dce 2021-09-24 thomas }
5211 a129376b 2019-03-28 stsp break;
5212 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5213 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5214 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5215 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5216 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5217 33aa809d 2019-08-08 stsp if (err)
5218 33aa809d 2019-08-08 stsp goto done;
5219 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5220 33aa809d 2019-08-08 stsp break;
5221 33aa809d 2019-08-08 stsp }
5222 33aa809d 2019-08-08 stsp /* fall through */
5223 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5224 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5225 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5226 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5227 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5228 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5229 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5230 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5231 43010591 2023-02-17 thomas else
5232 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5233 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5234 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5235 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5236 f4ae6ddb 2022-07-01 thomas goto done;
5237 f4ae6ddb 2022-07-01 thomas }
5238 f4ae6ddb 2022-07-01 thomas
5239 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5240 a129376b 2019-03-28 stsp if (err)
5241 65084dad 2019-08-08 stsp goto done;
5242 65084dad 2019-08-08 stsp
5243 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5244 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5245 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5246 a129376b 2019-03-28 stsp goto done;
5247 65084dad 2019-08-08 stsp }
5248 65084dad 2019-08-08 stsp
5249 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5250 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5251 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5252 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5253 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5254 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5255 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5256 33aa809d 2019-08-08 stsp break;
5257 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5258 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5259 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5260 369fd7e5 2020-07-23 stsp path_content);
5261 369fd7e5 2020-07-23 stsp break;
5262 369fd7e5 2020-07-23 stsp }
5263 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5264 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5265 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5266 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5267 369fd7e5 2020-07-23 stsp } else {
5268 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5269 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5270 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5271 369fd7e5 2020-07-23 stsp goto done;
5272 369fd7e5 2020-07-23 stsp }
5273 33aa809d 2019-08-08 stsp }
5274 33aa809d 2019-08-08 stsp } else {
5275 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5276 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5277 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5278 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5279 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5280 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5281 2e1fa222 2020-07-23 stsp } else {
5282 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5283 2e1fa222 2020-07-23 stsp ie->path,
5284 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5285 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5286 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5287 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5288 2e1fa222 2020-07-23 stsp }
5289 a129376b 2019-03-28 stsp if (err)
5290 a129376b 2019-03-28 stsp goto done;
5291 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5292 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5293 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5294 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5295 437adc9d 2020-12-10 yzhong blob->id.sha1,
5296 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5297 2e1fa222 2020-07-23 stsp if (err)
5298 2e1fa222 2020-07-23 stsp goto done;
5299 2e1fa222 2020-07-23 stsp }
5300 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5301 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5302 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5303 33aa809d 2019-08-08 stsp }
5304 a129376b 2019-03-28 stsp }
5305 a129376b 2019-03-28 stsp break;
5306 e20a8b6f 2019-06-04 stsp }
5307 a129376b 2019-03-28 stsp default:
5308 1f1abb7e 2019-08-08 stsp break;
5309 a129376b 2019-03-28 stsp }
5310 a129376b 2019-03-28 stsp done:
5311 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5312 33aa809d 2019-08-08 stsp free(path_content);
5313 e20a8b6f 2019-06-04 stsp free(parent_path);
5314 a129376b 2019-03-28 stsp free(tree_path);
5315 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5316 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5317 a129376b 2019-03-28 stsp if (blob)
5318 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5319 a129376b 2019-03-28 stsp if (tree)
5320 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5321 a129376b 2019-03-28 stsp free(tree_id);
5322 945f9229 2022-04-16 thomas if (base_commit)
5323 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5324 e20a8b6f 2019-06-04 stsp return err;
5325 e20a8b6f 2019-06-04 stsp }
5326 e20a8b6f 2019-06-04 stsp
5327 e20a8b6f 2019-06-04 stsp const struct got_error *
5328 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5329 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5330 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5331 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5332 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5333 e20a8b6f 2019-06-04 stsp {
5334 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5335 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5336 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5337 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5338 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5339 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5340 e20a8b6f 2019-06-04 stsp
5341 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5342 e20a8b6f 2019-06-04 stsp if (err)
5343 e20a8b6f 2019-06-04 stsp return err;
5344 e20a8b6f 2019-06-04 stsp
5345 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5346 e20a8b6f 2019-06-04 stsp if (err)
5347 e20a8b6f 2019-06-04 stsp goto done;
5348 e20a8b6f 2019-06-04 stsp
5349 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5350 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5351 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5352 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5353 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5354 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5355 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5356 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5357 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5358 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5359 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5360 e20a8b6f 2019-06-04 stsp if (err)
5361 af12c6b9 2019-06-04 stsp break;
5362 e20a8b6f 2019-06-04 stsp }
5363 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5364 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5365 e20a8b6f 2019-06-04 stsp err = sync_err;
5366 af12c6b9 2019-06-04 stsp done:
5367 fb399478 2019-07-12 stsp free(fileindex_path);
5368 a129376b 2019-03-28 stsp if (fileindex)
5369 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5370 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5371 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5372 a129376b 2019-03-28 stsp err = unlockerr;
5373 c4296144 2019-05-09 stsp return err;
5374 c4296144 2019-05-09 stsp }
5375 c4296144 2019-05-09 stsp
5376 cf066bf8 2019-05-09 stsp static void
5377 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5378 cf066bf8 2019-05-09 stsp {
5379 24519714 2019-05-09 stsp free(ct->path);
5380 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5381 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5382 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5383 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5384 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5385 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5386 cf066bf8 2019-05-09 stsp free(ct);
5387 cf066bf8 2019-05-09 stsp }
5388 24519714 2019-05-09 stsp
5389 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5390 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5391 24519714 2019-05-09 stsp struct got_repository *repo;
5392 24519714 2019-05-09 stsp struct got_worktree *worktree;
5393 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5394 5f8a88c6 2019-08-03 stsp int have_staged_files;
5395 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5396 ef899790 2022-11-01 thomas int diff_header_shown;
5397 be94c032 2023-02-20 thomas int commit_conflicts;
5398 ef899790 2022-11-01 thomas FILE *diff_outfile;
5399 ef899790 2022-11-01 thomas FILE *f1;
5400 ef899790 2022-11-01 thomas FILE *f2;
5401 24519714 2019-05-09 stsp };
5402 ef899790 2022-11-01 thomas
5403 ef899790 2022-11-01 thomas /*
5404 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5405 ef899790 2022-11-01 thomas * it as content to the diff engine.
5406 ef899790 2022-11-01 thomas */
5407 ef899790 2022-11-01 thomas static const struct got_error *
5408 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5409 ef899790 2022-11-01 thomas const char *abspath)
5410 ef899790 2022-11-01 thomas {
5411 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5412 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5413 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5414 ef899790 2022-11-01 thomas
5415 ef899790 2022-11-01 thomas *fd = -1;
5416 ef899790 2022-11-01 thomas
5417 ef899790 2022-11-01 thomas if (dirfd != -1) {
5418 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5419 ef899790 2022-11-01 thomas if (target_len == -1)
5420 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5421 ef899790 2022-11-01 thomas } else {
5422 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5423 ef899790 2022-11-01 thomas if (target_len == -1)
5424 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5425 ef899790 2022-11-01 thomas }
5426 ef899790 2022-11-01 thomas
5427 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5428 ef899790 2022-11-01 thomas if (*fd == -1)
5429 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5430 ef899790 2022-11-01 thomas
5431 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5432 ef899790 2022-11-01 thomas if (outlen == -1) {
5433 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5434 ef899790 2022-11-01 thomas goto done;
5435 ef899790 2022-11-01 thomas }
5436 ef899790 2022-11-01 thomas
5437 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5438 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5439 ef899790 2022-11-01 thomas goto done;
5440 ef899790 2022-11-01 thomas }
5441 ef899790 2022-11-01 thomas done:
5442 ef899790 2022-11-01 thomas if (err) {
5443 ef899790 2022-11-01 thomas close(*fd);
5444 ef899790 2022-11-01 thomas *fd = -1;
5445 ef899790 2022-11-01 thomas }
5446 ef899790 2022-11-01 thomas return err;
5447 ef899790 2022-11-01 thomas }
5448 ef899790 2022-11-01 thomas
5449 ef899790 2022-11-01 thomas static const struct got_error *
5450 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5451 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5452 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5453 ef899790 2022-11-01 thomas {
5454 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5455 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5456 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5457 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5458 ef899790 2022-11-01 thomas char *label1 = NULL;
5459 ef899790 2022-11-01 thomas struct stat sb;
5460 ef899790 2022-11-01 thomas off_t size1 = 0;
5461 ef899790 2022-11-01 thomas int f2_exists = 0;
5462 ef899790 2022-11-01 thomas char *id_str = NULL;
5463 ef899790 2022-11-01 thomas
5464 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5465 d259e491 2022-11-01 thomas
5466 d259e491 2022-11-01 thomas if (diff_staged) {
5467 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5468 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5469 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5470 d259e491 2022-11-01 thomas return NULL;
5471 d259e491 2022-11-01 thomas } else {
5472 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5473 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5474 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5475 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5476 d259e491 2022-11-01 thomas return NULL;
5477 d259e491 2022-11-01 thomas }
5478 ef899790 2022-11-01 thomas
5479 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5480 ef899790 2022-11-01 thomas if (err)
5481 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5482 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5483 ef899790 2022-11-01 thomas if (err)
5484 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5485 ef899790 2022-11-01 thomas
5486 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5487 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5488 ef899790 2022-11-01 thomas if (err)
5489 ef899790 2022-11-01 thomas return err;
5490 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5491 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5492 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5493 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5494 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5495 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5496 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5497 ef899790 2022-11-01 thomas }
5498 ef899790 2022-11-01 thomas
5499 ef899790 2022-11-01 thomas if (diff_staged) {
5500 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5501 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5502 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5503 ef899790 2022-11-01 thomas label1 = ct->path;
5504 ef899790 2022-11-01 thomas label2 = ct->path;
5505 ef899790 2022-11-01 thomas break;
5506 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5507 ef899790 2022-11-01 thomas label2 = ct->path;
5508 ef899790 2022-11-01 thomas break;
5509 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5510 ef899790 2022-11-01 thomas label1 = ct->path;
5511 ef899790 2022-11-01 thomas break;
5512 ef899790 2022-11-01 thomas default:
5513 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5514 ef899790 2022-11-01 thomas }
5515 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5516 ef899790 2022-11-01 thomas if (fd1 == -1) {
5517 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5518 ef899790 2022-11-01 thomas goto done;
5519 ef899790 2022-11-01 thomas }
5520 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5521 ef899790 2022-11-01 thomas if (fd2 == -1) {
5522 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5523 ef899790 2022-11-01 thomas goto done;
5524 ef899790 2022-11-01 thomas }
5525 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5526 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5527 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5528 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5529 ef899790 2022-11-01 thomas goto done;
5530 ef899790 2022-11-01 thomas }
5531 ef899790 2022-11-01 thomas
5532 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5533 ef899790 2022-11-01 thomas if (fd1 == -1) {
5534 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5535 ef899790 2022-11-01 thomas goto done;
5536 ef899790 2022-11-01 thomas }
5537 ef899790 2022-11-01 thomas
5538 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5539 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5540 ef899790 2022-11-01 thomas 8192, fd1);
5541 ef899790 2022-11-01 thomas if (err)
5542 ef899790 2022-11-01 thomas goto done;
5543 ef899790 2022-11-01 thomas }
5544 ef899790 2022-11-01 thomas
5545 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5546 ef899790 2022-11-01 thomas if (dirfd != -1) {
5547 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5548 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5549 ef899790 2022-11-01 thomas if (fd == -1) {
5550 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5551 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5552 ef899790 2022-11-01 thomas ct->ondisk_path);
5553 ef899790 2022-11-01 thomas goto done;
5554 ef899790 2022-11-01 thomas }
5555 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5556 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5557 ef899790 2022-11-01 thomas if (err)
5558 ef899790 2022-11-01 thomas goto done;
5559 ef899790 2022-11-01 thomas }
5560 ef899790 2022-11-01 thomas } else {
5561 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5562 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5563 ef899790 2022-11-01 thomas if (fd == -1) {
5564 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5565 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5566 ef899790 2022-11-01 thomas ct->ondisk_path);
5567 ef899790 2022-11-01 thomas goto done;
5568 ef899790 2022-11-01 thomas }
5569 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5570 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5571 ef899790 2022-11-01 thomas if (err)
5572 ef899790 2022-11-01 thomas goto done;
5573 ef899790 2022-11-01 thomas }
5574 ef899790 2022-11-01 thomas }
5575 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5576 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5577 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5578 ef899790 2022-11-01 thomas goto done;
5579 ef899790 2022-11-01 thomas }
5580 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5581 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5582 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5583 ef899790 2022-11-01 thomas goto done;
5584 ef899790 2022-11-01 thomas }
5585 ef899790 2022-11-01 thomas fd = -1;
5586 ef899790 2022-11-01 thomas f2_exists = 1;
5587 ef899790 2022-11-01 thomas }
5588 ef899790 2022-11-01 thomas
5589 ef899790 2022-11-01 thomas if (blob1) {
5590 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5591 ef899790 2022-11-01 thomas f1, blob1);
5592 ef899790 2022-11-01 thomas if (err)
5593 ef899790 2022-11-01 thomas goto done;
5594 ef899790 2022-11-01 thomas }
5595 ef899790 2022-11-01 thomas
5596 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5597 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5598 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5599 ef899790 2022-11-01 thomas done:
5600 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5601 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5602 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5603 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5604 ef899790 2022-11-01 thomas if (blob1)
5605 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5606 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5607 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5608 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5609 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5610 ef899790 2022-11-01 thomas return err;
5611 ef899790 2022-11-01 thomas }
5612 cf066bf8 2019-05-09 stsp
5613 c4296144 2019-05-09 stsp static const struct got_error *
5614 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5615 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5616 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5617 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5618 c4296144 2019-05-09 stsp {
5619 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5620 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5621 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5622 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5623 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5624 768aea60 2019-05-09 stsp struct stat sb;
5625 c4296144 2019-05-09 stsp
5626 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5627 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5628 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5629 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5630 5f8a88c6 2019-08-03 stsp return NULL;
5631 5f8a88c6 2019-08-03 stsp } else {
5632 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5633 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5634 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5635 be94c032 2023-02-20 thomas }
5636 c4296144 2019-05-09 stsp
5637 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5638 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5639 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5640 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5641 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5642 5f8a88c6 2019-08-03 stsp return NULL;
5643 5f8a88c6 2019-08-03 stsp }
5644 0b5cc0d6 2019-05-09 stsp
5645 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5646 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5647 036813ee 2019-05-09 stsp goto done;
5648 036813ee 2019-05-09 stsp }
5649 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5650 036813ee 2019-05-09 stsp parent_path = strdup("");
5651 69960a46 2019-05-09 stsp if (parent_path == NULL)
5652 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5653 69960a46 2019-05-09 stsp } else {
5654 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5655 69960a46 2019-05-09 stsp if (err)
5656 69960a46 2019-05-09 stsp return err;
5657 69960a46 2019-05-09 stsp }
5658 c4296144 2019-05-09 stsp
5659 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5660 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5661 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5662 c4296144 2019-05-09 stsp goto done;
5663 768aea60 2019-05-09 stsp }
5664 768aea60 2019-05-09 stsp
5665 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5666 768aea60 2019-05-09 stsp relpath) == -1) {
5667 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5668 768aea60 2019-05-09 stsp goto done;
5669 768aea60 2019-05-09 stsp }
5670 0aeb8099 2020-07-23 stsp
5671 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5672 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5673 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5674 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5675 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5676 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5677 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5678 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5679 0aeb8099 2020-07-23 stsp break;
5680 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5681 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5682 0aeb8099 2020-07-23 stsp break;
5683 0aeb8099 2020-07-23 stsp default:
5684 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5685 0aeb8099 2020-07-23 stsp goto done;
5686 0aeb8099 2020-07-23 stsp }
5687 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5688 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5689 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5690 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5691 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5692 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5693 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5694 12463d8b 2019-12-13 stsp ct->ondisk_path);
5695 12463d8b 2019-12-13 stsp goto done;
5696 12463d8b 2019-12-13 stsp }
5697 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5698 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5699 768aea60 2019-05-09 stsp goto done;
5700 768aea60 2019-05-09 stsp }
5701 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5702 c4296144 2019-05-09 stsp }
5703 c4296144 2019-05-09 stsp
5704 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5705 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5706 44d03001 2019-05-09 stsp relpath) == -1) {
5707 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5708 44d03001 2019-05-09 stsp goto done;
5709 44d03001 2019-05-09 stsp }
5710 44d03001 2019-05-09 stsp
5711 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5712 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5713 35213c7c 2020-07-23 stsp int is_bad_symlink;
5714 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5715 35213c7c 2020-07-23 stsp ssize_t target_len;
5716 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5717 35213c7c 2020-07-23 stsp sizeof(target_path));
5718 35213c7c 2020-07-23 stsp if (target_len == -1) {
5719 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5720 35213c7c 2020-07-23 stsp ct->ondisk_path);
5721 35213c7c 2020-07-23 stsp goto done;
5722 35213c7c 2020-07-23 stsp }
5723 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5724 ad10f64e 2023-07-19 thomas target_len, ct->ondisk_path, a->worktree->root_path,
5725 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
5726 35213c7c 2020-07-23 stsp if (err)
5727 35213c7c 2020-07-23 stsp goto done;
5728 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5729 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5730 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5731 35213c7c 2020-07-23 stsp goto done;
5732 35213c7c 2020-07-23 stsp }
5733 35213c7c 2020-07-23 stsp }
5734 35213c7c 2020-07-23 stsp
5735 35213c7c 2020-07-23 stsp
5736 cf066bf8 2019-05-09 stsp ct->status = status;
5737 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5738 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5739 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5740 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5741 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5742 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5743 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5744 b416585c 2019-05-13 stsp goto done;
5745 b416585c 2019-05-13 stsp }
5746 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5747 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5748 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5749 f0b75401 2019-08-03 stsp goto done;
5750 f0b75401 2019-08-03 stsp }
5751 f0b75401 2019-08-03 stsp }
5752 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5753 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5754 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5755 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5756 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5757 036813ee 2019-05-09 stsp goto done;
5758 036813ee 2019-05-09 stsp }
5759 ca2503ea 2019-05-09 stsp }
5760 24519714 2019-05-09 stsp ct->path = strdup(path);
5761 24519714 2019-05-09 stsp if (ct->path == NULL) {
5762 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5763 24519714 2019-05-09 stsp goto done;
5764 24519714 2019-05-09 stsp }
5765 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5766 ef899790 2022-11-01 thomas if (err)
5767 ef899790 2022-11-01 thomas goto done;
5768 ef899790 2022-11-01 thomas
5769 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5770 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5771 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5772 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5773 ef899790 2022-11-01 thomas if (err)
5774 ef899790 2022-11-01 thomas goto done;
5775 ef899790 2022-11-01 thomas }
5776 c4296144 2019-05-09 stsp done:
5777 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5778 ed175427 2019-05-09 stsp free_commitable(ct);
5779 24519714 2019-05-09 stsp free(parent_path);
5780 036813ee 2019-05-09 stsp free(path);
5781 a129376b 2019-03-28 stsp return err;
5782 a129376b 2019-03-28 stsp }
5783 c4296144 2019-05-09 stsp
5784 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5785 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5786 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5787 036813ee 2019-05-09 stsp struct got_repository *);
5788 ed175427 2019-05-09 stsp
5789 ed175427 2019-05-09 stsp static const struct got_error *
5790 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5791 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5792 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5793 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5794 afa376bf 2019-05-09 stsp struct got_repository *repo)
5795 ed175427 2019-05-09 stsp {
5796 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5797 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5798 036813ee 2019-05-09 stsp char *subpath;
5799 ed175427 2019-05-09 stsp
5800 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5801 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5802 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5803 ed175427 2019-05-09 stsp
5804 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5805 ed175427 2019-05-09 stsp if (err)
5806 ed175427 2019-05-09 stsp return err;
5807 ed175427 2019-05-09 stsp
5808 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5809 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5810 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5811 036813ee 2019-05-09 stsp free(subpath);
5812 036813ee 2019-05-09 stsp return err;
5813 036813ee 2019-05-09 stsp }
5814 ed175427 2019-05-09 stsp
5815 036813ee 2019-05-09 stsp static const struct got_error *
5816 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5817 036813ee 2019-05-09 stsp {
5818 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5819 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5820 ed175427 2019-05-09 stsp
5821 036813ee 2019-05-09 stsp *match = 0;
5822 ed175427 2019-05-09 stsp
5823 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5824 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5825 0f63689d 2019-05-10 stsp return NULL;
5826 036813ee 2019-05-09 stsp }
5827 0b5cc0d6 2019-05-09 stsp
5828 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5829 0f63689d 2019-05-10 stsp if (err)
5830 0f63689d 2019-05-10 stsp return err;
5831 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5832 036813ee 2019-05-09 stsp free(ct_parent_path);
5833 036813ee 2019-05-09 stsp return err;
5834 036813ee 2019-05-09 stsp }
5835 0b5cc0d6 2019-05-09 stsp
5836 768aea60 2019-05-09 stsp static mode_t
5837 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5838 768aea60 2019-05-09 stsp {
5839 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5840 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5841 3d9a4ec4 2020-07-23 stsp
5842 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5843 768aea60 2019-05-09 stsp }
5844 768aea60 2019-05-09 stsp
5845 036813ee 2019-05-09 stsp static const struct got_error *
5846 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5847 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5848 036813ee 2019-05-09 stsp {
5849 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5850 ca2503ea 2019-05-09 stsp
5851 036813ee 2019-05-09 stsp *new_te = NULL;
5852 0b5cc0d6 2019-05-09 stsp
5853 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5854 036813ee 2019-05-09 stsp if (err)
5855 036813ee 2019-05-09 stsp goto done;
5856 0b5cc0d6 2019-05-09 stsp
5857 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5858 036813ee 2019-05-09 stsp
5859 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5860 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5861 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5862 5f8a88c6 2019-08-03 stsp else
5863 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5864 036813ee 2019-05-09 stsp done:
5865 036813ee 2019-05-09 stsp if (err && *new_te) {
5866 56e0773d 2019-11-28 stsp free(*new_te);
5867 036813ee 2019-05-09 stsp *new_te = NULL;
5868 036813ee 2019-05-09 stsp }
5869 036813ee 2019-05-09 stsp return err;
5870 036813ee 2019-05-09 stsp }
5871 036813ee 2019-05-09 stsp
5872 036813ee 2019-05-09 stsp static const struct got_error *
5873 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5874 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5875 036813ee 2019-05-09 stsp {
5876 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5877 102b254e 2020-10-19 stsp char *ct_name = NULL;
5878 036813ee 2019-05-09 stsp
5879 036813ee 2019-05-09 stsp *new_te = NULL;
5880 036813ee 2019-05-09 stsp
5881 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5882 036813ee 2019-05-09 stsp if (*new_te == NULL)
5883 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5884 036813ee 2019-05-09 stsp
5885 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5886 102b254e 2020-10-19 stsp if (err)
5887 036813ee 2019-05-09 stsp goto done;
5888 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5889 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5890 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5891 036813ee 2019-05-09 stsp goto done;
5892 036813ee 2019-05-09 stsp }
5893 036813ee 2019-05-09 stsp
5894 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5895 036813ee 2019-05-09 stsp
5896 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5897 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5898 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5899 5f8a88c6 2019-08-03 stsp else
5900 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5901 036813ee 2019-05-09 stsp done:
5902 102b254e 2020-10-19 stsp free(ct_name);
5903 036813ee 2019-05-09 stsp if (err && *new_te) {
5904 56e0773d 2019-11-28 stsp free(*new_te);
5905 036813ee 2019-05-09 stsp *new_te = NULL;
5906 036813ee 2019-05-09 stsp }
5907 036813ee 2019-05-09 stsp return err;
5908 036813ee 2019-05-09 stsp }
5909 036813ee 2019-05-09 stsp
5910 036813ee 2019-05-09 stsp static const struct got_error *
5911 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5912 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5913 036813ee 2019-05-09 stsp {
5914 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5915 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5916 036813ee 2019-05-09 stsp
5917 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5918 036813ee 2019-05-09 stsp if (err)
5919 036813ee 2019-05-09 stsp return err;
5920 036813ee 2019-05-09 stsp if (new_pe == NULL)
5921 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5922 036813ee 2019-05-09 stsp return NULL;
5923 afa376bf 2019-05-09 stsp }
5924 afa376bf 2019-05-09 stsp
5925 afa376bf 2019-05-09 stsp static const struct got_error *
5926 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5927 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5928 afa376bf 2019-05-09 stsp {
5929 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5930 5f8a88c6 2019-08-03 stsp unsigned char status;
5931 ae1e948a 2021-09-28 thomas
5932 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5933 ae1e948a 2021-09-28 thomas return NULL;
5934 5f8a88c6 2019-08-03 stsp
5935 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5936 afa376bf 2019-05-09 stsp ct_path++;
5937 5f8a88c6 2019-08-03 stsp
5938 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5939 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5940 5f8a88c6 2019-08-03 stsp else
5941 5f8a88c6 2019-08-03 stsp status = ct->status;
5942 5f8a88c6 2019-08-03 stsp
5943 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5944 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5945 036813ee 2019-05-09 stsp }
5946 036813ee 2019-05-09 stsp
5947 036813ee 2019-05-09 stsp static const struct got_error *
5948 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5949 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5950 44d03001 2019-05-09 stsp {
5951 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5952 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5953 44d03001 2019-05-09 stsp char *te_path;
5954 44d03001 2019-05-09 stsp
5955 44d03001 2019-05-09 stsp *modified = 0;
5956 44d03001 2019-05-09 stsp
5957 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5958 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5959 44d03001 2019-05-09 stsp te->name) == -1)
5960 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5961 44d03001 2019-05-09 stsp
5962 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5963 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5964 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5965 44d03001 2019-05-09 stsp strlen(te_path));
5966 62d463ca 2020-10-20 naddy if (*modified)
5967 44d03001 2019-05-09 stsp break;
5968 44d03001 2019-05-09 stsp }
5969 44d03001 2019-05-09 stsp
5970 44d03001 2019-05-09 stsp free(te_path);
5971 44d03001 2019-05-09 stsp return err;
5972 44d03001 2019-05-09 stsp }
5973 44d03001 2019-05-09 stsp
5974 44d03001 2019-05-09 stsp static const struct got_error *
5975 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5976 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5977 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5978 036813ee 2019-05-09 stsp {
5979 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5980 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5981 036813ee 2019-05-09 stsp
5982 036813ee 2019-05-09 stsp *ctp = NULL;
5983 036813ee 2019-05-09 stsp
5984 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5985 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5986 036813ee 2019-05-09 stsp char *ct_name = NULL;
5987 036813ee 2019-05-09 stsp int path_matches;
5988 036813ee 2019-05-09 stsp
5989 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5990 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5991 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5992 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5993 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5994 5f8a88c6 2019-08-03 stsp continue;
5995 5f8a88c6 2019-08-03 stsp } else {
5996 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5997 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5998 5f8a88c6 2019-08-03 stsp continue;
5999 5f8a88c6 2019-08-03 stsp }
6000 036813ee 2019-05-09 stsp
6001 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6002 036813ee 2019-05-09 stsp continue;
6003 036813ee 2019-05-09 stsp
6004 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6005 62d463ca 2020-10-20 naddy if (err)
6006 036813ee 2019-05-09 stsp return err;
6007 036813ee 2019-05-09 stsp if (!path_matches)
6008 036813ee 2019-05-09 stsp continue;
6009 036813ee 2019-05-09 stsp
6010 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
6011 d34b633e 2020-10-19 stsp if (err)
6012 d34b633e 2020-10-19 stsp return err;
6013 d34b633e 2020-10-19 stsp
6014 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
6015 d34b633e 2020-10-19 stsp free(ct_name);
6016 036813ee 2019-05-09 stsp continue;
6017 d34b633e 2020-10-19 stsp }
6018 d34b633e 2020-10-19 stsp free(ct_name);
6019 036813ee 2019-05-09 stsp
6020 036813ee 2019-05-09 stsp *ctp = ct;
6021 036813ee 2019-05-09 stsp break;
6022 036813ee 2019-05-09 stsp }
6023 2b496619 2019-07-10 stsp
6024 2b496619 2019-07-10 stsp return err;
6025 2b496619 2019-07-10 stsp }
6026 2b496619 2019-07-10 stsp
6027 2b496619 2019-07-10 stsp static const struct got_error *
6028 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6029 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
6030 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
6031 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
6032 2b496619 2019-07-10 stsp struct got_repository *repo)
6033 2b496619 2019-07-10 stsp {
6034 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
6035 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
6036 2b496619 2019-07-10 stsp char *subtree_path;
6037 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
6038 ba580f68 2020-03-22 stsp int nentries;
6039 2b496619 2019-07-10 stsp
6040 2b496619 2019-07-10 stsp *new_tep = NULL;
6041 2b496619 2019-07-10 stsp
6042 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6043 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6044 2b496619 2019-07-10 stsp child_path) == -1)
6045 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6046 036813ee 2019-05-09 stsp
6047 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6048 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6049 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6050 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6051 56e0773d 2019-11-28 stsp
6052 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6053 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6054 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6055 2b496619 2019-07-10 stsp goto done;
6056 2b496619 2019-07-10 stsp }
6057 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6058 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6059 2b496619 2019-07-10 stsp if (err) {
6060 56e0773d 2019-11-28 stsp free(new_te);
6061 2b496619 2019-07-10 stsp goto done;
6062 2b496619 2019-07-10 stsp }
6063 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6064 2b496619 2019-07-10 stsp done:
6065 56e0773d 2019-11-28 stsp free(id);
6066 2b496619 2019-07-10 stsp free(subtree_path);
6067 2b496619 2019-07-10 stsp if (err == NULL)
6068 2b496619 2019-07-10 stsp *new_tep = new_te;
6069 036813ee 2019-05-09 stsp return err;
6070 036813ee 2019-05-09 stsp }
6071 036813ee 2019-05-09 stsp
6072 036813ee 2019-05-09 stsp static const struct got_error *
6073 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6074 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6075 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6076 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6077 036813ee 2019-05-09 stsp struct got_repository *repo)
6078 036813ee 2019-05-09 stsp {
6079 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6080 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6081 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6082 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6083 036813ee 2019-05-09 stsp
6084 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6085 ba580f68 2020-03-22 stsp *nentries = 0;
6086 036813ee 2019-05-09 stsp
6087 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6088 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6089 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6090 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6091 036813ee 2019-05-09 stsp
6092 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6093 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6094 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6095 036813ee 2019-05-09 stsp continue;
6096 036813ee 2019-05-09 stsp
6097 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6098 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6099 036813ee 2019-05-09 stsp continue;
6100 036813ee 2019-05-09 stsp
6101 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6102 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6103 036813ee 2019-05-09 stsp if (err)
6104 036813ee 2019-05-09 stsp goto done;
6105 036813ee 2019-05-09 stsp
6106 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6107 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6108 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6109 036813ee 2019-05-09 stsp if (err)
6110 036813ee 2019-05-09 stsp goto done;
6111 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6112 afa376bf 2019-05-09 stsp if (err)
6113 afa376bf 2019-05-09 stsp goto done;
6114 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6115 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6116 2b496619 2019-07-10 stsp if (err)
6117 2f51b5b3 2019-05-09 stsp goto done;
6118 ba580f68 2020-03-22 stsp (*nentries)++;
6119 2b496619 2019-07-10 stsp } else {
6120 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6121 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6122 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6123 2b496619 2019-07-10 stsp == NULL) {
6124 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6125 2b496619 2019-07-10 stsp child_path, path_base_tree,
6126 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6127 2b496619 2019-07-10 stsp repo);
6128 2b496619 2019-07-10 stsp if (err)
6129 2b496619 2019-07-10 stsp goto done;
6130 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6131 2b496619 2019-07-10 stsp if (err)
6132 2b496619 2019-07-10 stsp goto done;
6133 ba580f68 2020-03-22 stsp (*nentries)++;
6134 9ba0479c 2019-05-10 stsp }
6135 ed175427 2019-05-09 stsp }
6136 2f51b5b3 2019-05-09 stsp }
6137 2f51b5b3 2019-05-09 stsp
6138 2f51b5b3 2019-05-09 stsp if (base_tree) {
6139 56e0773d 2019-11-28 stsp int i, nbase_entries;
6140 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6141 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6142 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6143 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6144 63c5ca5d 2019-08-24 stsp
6145 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6146 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6147 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6148 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6149 63c5ca5d 2019-08-24 stsp if (err)
6150 63c5ca5d 2019-08-24 stsp goto done;
6151 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6152 63c5ca5d 2019-08-24 stsp if (err)
6153 63c5ca5d 2019-08-24 stsp goto done;
6154 ba580f68 2020-03-22 stsp (*nentries)++;
6155 63c5ca5d 2019-08-24 stsp continue;
6156 63c5ca5d 2019-08-24 stsp }
6157 2f51b5b3 2019-05-09 stsp
6158 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6159 44d03001 2019-05-09 stsp int modified;
6160 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6161 036813ee 2019-05-09 stsp if (err)
6162 036813ee 2019-05-09 stsp goto done;
6163 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6164 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6165 2f51b5b3 2019-05-09 stsp if (err)
6166 2f51b5b3 2019-05-09 stsp goto done;
6167 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6168 44d03001 2019-05-09 stsp if (modified) {
6169 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6170 ba580f68 2020-03-22 stsp int nsubentries;
6171 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6172 ba580f68 2020-03-22 stsp &nsubentries, te,
6173 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6174 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6175 44d03001 2019-05-09 stsp if (err)
6176 44d03001 2019-05-09 stsp goto done;
6177 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6178 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6179 ba580f68 2020-03-22 stsp free(new_id);
6180 ba580f68 2020-03-22 stsp continue;
6181 ba580f68 2020-03-22 stsp }
6182 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6183 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6184 56e0773d 2019-11-28 stsp free(new_id);
6185 44d03001 2019-05-09 stsp }
6186 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6187 036813ee 2019-05-09 stsp if (err)
6188 036813ee 2019-05-09 stsp goto done;
6189 ba580f68 2020-03-22 stsp (*nentries)++;
6190 2f51b5b3 2019-05-09 stsp continue;
6191 036813ee 2019-05-09 stsp }
6192 2f51b5b3 2019-05-09 stsp
6193 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6194 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6195 f66c734c 2019-09-22 stsp if (err)
6196 f66c734c 2019-09-22 stsp goto done;
6197 2f51b5b3 2019-05-09 stsp if (ct) {
6198 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6199 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6200 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6201 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6202 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6203 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6204 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6205 2f51b5b3 2019-05-09 stsp if (err)
6206 2f51b5b3 2019-05-09 stsp goto done;
6207 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6208 2f51b5b3 2019-05-09 stsp if (err)
6209 2f51b5b3 2019-05-09 stsp goto done;
6210 ba580f68 2020-03-22 stsp (*nentries)++;
6211 2f51b5b3 2019-05-09 stsp }
6212 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6213 b416585c 2019-05-13 stsp status_arg);
6214 afa376bf 2019-05-09 stsp if (err)
6215 afa376bf 2019-05-09 stsp goto done;
6216 2f51b5b3 2019-05-09 stsp } else {
6217 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6218 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6219 2f51b5b3 2019-05-09 stsp if (err)
6220 2f51b5b3 2019-05-09 stsp goto done;
6221 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6222 2f51b5b3 2019-05-09 stsp if (err)
6223 2f51b5b3 2019-05-09 stsp goto done;
6224 ba580f68 2020-03-22 stsp (*nentries)++;
6225 2f51b5b3 2019-05-09 stsp }
6226 ca2503ea 2019-05-09 stsp }
6227 ed175427 2019-05-09 stsp }
6228 0b5cc0d6 2019-05-09 stsp
6229 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6230 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6231 ed175427 2019-05-09 stsp done:
6232 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6233 2e1fa222 2020-07-23 stsp return err;
6234 2e1fa222 2020-07-23 stsp }
6235 2e1fa222 2020-07-23 stsp
6236 2e1fa222 2020-07-23 stsp static const struct got_error *
6237 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6238 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6239 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6240 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6241 ebf99748 2019-05-09 stsp {
6242 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6243 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6244 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6245 ebf99748 2019-05-09 stsp
6246 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6247 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6248 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6249 ebf99748 2019-05-09 stsp
6250 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6251 437adc9d 2020-12-10 yzhong
6252 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6253 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6254 437adc9d 2020-12-10 yzhong if (err)
6255 437adc9d 2020-12-10 yzhong goto done;
6256 437adc9d 2020-12-10 yzhong
6257 ebf99748 2019-05-09 stsp if (ie) {
6258 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6259 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6260 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6261 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6262 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6263 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6264 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6265 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6266 437adc9d 2020-12-10 yzhong
6267 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6268 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6269 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6270 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6271 72fd46fa 2019-09-06 stsp !have_staged_files);
6272 ebf99748 2019-05-09 stsp } else
6273 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6274 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6275 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6276 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6277 72fd46fa 2019-09-06 stsp !have_staged_files);
6278 ebf99748 2019-05-09 stsp } else {
6279 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6280 ebf99748 2019-05-09 stsp if (err)
6281 437adc9d 2020-12-10 yzhong goto done;
6282 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6283 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6284 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6285 3969253a 2020-03-07 stsp if (err) {
6286 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6287 437adc9d 2020-12-10 yzhong goto done;
6288 3969253a 2020-03-07 stsp }
6289 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6290 3969253a 2020-03-07 stsp if (err) {
6291 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6292 437adc9d 2020-12-10 yzhong goto done;
6293 3969253a 2020-03-07 stsp }
6294 ebf99748 2019-05-09 stsp }
6295 437adc9d 2020-12-10 yzhong free(relpath);
6296 437adc9d 2020-12-10 yzhong relpath = NULL;
6297 ebf99748 2019-05-09 stsp }
6298 437adc9d 2020-12-10 yzhong done:
6299 437adc9d 2020-12-10 yzhong free(relpath);
6300 d56d26ce 2019-05-10 stsp return err;
6301 d56d26ce 2019-05-10 stsp }
6302 735ef5ac 2019-08-03 stsp
6303 d56d26ce 2019-05-10 stsp
6304 d56d26ce 2019-05-10 stsp static const struct got_error *
6305 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6306 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6307 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6308 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6309 735ef5ac 2019-08-03 stsp int ood_errcode)
6310 d56d26ce 2019-05-10 stsp {
6311 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6312 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6313 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6314 1a36436d 2019-06-10 stsp
6315 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6316 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6317 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6318 1a36436d 2019-06-10 stsp return NULL;
6319 9bead371 2019-07-28 stsp /*
6320 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6321 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6322 9bead371 2019-07-28 stsp */
6323 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6324 945f9229 2022-04-16 thomas if (err)
6325 945f9229 2022-04-16 thomas goto done;
6326 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6327 9bead371 2019-07-28 stsp if (err) {
6328 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6329 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6330 1a36436d 2019-06-10 stsp goto done;
6331 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6332 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6333 1a36436d 2019-06-10 stsp } else {
6334 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6335 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6336 945f9229 2022-04-16 thomas if (err)
6337 945f9229 2022-04-16 thomas goto done;
6338 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6339 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6340 1a36436d 2019-06-10 stsp goto done;
6341 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6342 1a36436d 2019-06-10 stsp }
6343 1a36436d 2019-06-10 stsp done:
6344 1a36436d 2019-06-10 stsp free(id);
6345 945f9229 2022-04-16 thomas if (commit)
6346 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6347 1a36436d 2019-06-10 stsp return err;
6348 ebf99748 2019-05-09 stsp }
6349 ebf99748 2019-05-09 stsp
6350 ef20f542 2022-06-26 thomas static const struct got_error *
6351 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6352 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6353 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6354 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6355 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6356 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6357 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6358 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6359 afa376bf 2019-05-09 stsp struct got_repository *repo)
6360 c4296144 2019-05-09 stsp {
6361 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6362 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6363 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6364 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6365 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6366 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6367 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6368 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6369 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6370 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6371 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6372 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6373 892eab0a 2022-07-22 thomas time_t timestamp;
6374 c4296144 2019-05-09 stsp
6375 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6376 c4296144 2019-05-09 stsp
6377 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6378 675c7539 2019-05-09 stsp
6379 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6380 588edf97 2019-05-10 stsp if (err)
6381 588edf97 2019-05-10 stsp goto done;
6382 de18fc63 2019-05-09 stsp
6383 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6384 588edf97 2019-05-10 stsp if (err)
6385 588edf97 2019-05-10 stsp goto done;
6386 588edf97 2019-05-10 stsp
6387 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6388 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6389 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6390 33ad4cbe 2019-05-12 jcs if (err)
6391 33ad4cbe 2019-05-12 jcs goto done;
6392 33ad4cbe 2019-05-12 jcs }
6393 c4296144 2019-05-09 stsp
6394 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6395 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6396 33ad4cbe 2019-05-12 jcs goto done;
6397 33ad4cbe 2019-05-12 jcs }
6398 33ad4cbe 2019-05-12 jcs
6399 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6400 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6401 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6402 cf066bf8 2019-05-09 stsp char *ondisk_path;
6403 cf066bf8 2019-05-09 stsp
6404 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6405 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6406 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6407 5f8a88c6 2019-08-03 stsp continue;
6408 5f8a88c6 2019-08-03 stsp
6409 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6410 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6411 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6412 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6413 cf066bf8 2019-05-09 stsp continue;
6414 cf066bf8 2019-05-09 stsp
6415 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6416 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6417 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6418 cf066bf8 2019-05-09 stsp goto done;
6419 cf066bf8 2019-05-09 stsp }
6420 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6421 2e1fa222 2020-07-23 stsp free(ondisk_path);
6422 2e1fa222 2020-07-23 stsp if (err)
6423 cf066bf8 2019-05-09 stsp goto done;
6424 cf066bf8 2019-05-09 stsp }
6425 036813ee 2019-05-09 stsp
6426 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6427 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6428 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6429 036813ee 2019-05-09 stsp if (err)
6430 036813ee 2019-05-09 stsp goto done;
6431 036813ee 2019-05-09 stsp
6432 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6433 de18fc63 2019-05-09 stsp if (err)
6434 de18fc63 2019-05-09 stsp goto done;
6435 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6436 10604dce 2021-09-24 thomas nparents++;
6437 10604dce 2021-09-24 thomas if (parent_id2) {
6438 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6439 10604dce 2021-09-24 thomas if (err)
6440 10604dce 2021-09-24 thomas goto done;
6441 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6442 10604dce 2021-09-24 thomas nparents++;
6443 10604dce 2021-09-24 thomas }
6444 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6445 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6446 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6447 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6448 33ad4cbe 2019-05-12 jcs free(logmsg);
6449 9d40349a 2019-05-09 stsp if (err)
6450 9d40349a 2019-05-09 stsp goto done;
6451 9d40349a 2019-05-09 stsp
6452 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6453 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6454 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6455 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6456 09f5bd90 2019-05-09 stsp goto done;
6457 09f5bd90 2019-05-09 stsp }
6458 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6459 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6460 09f5bd90 2019-05-09 stsp if (err)
6461 09f5bd90 2019-05-09 stsp goto done;
6462 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6463 ebf99748 2019-05-09 stsp if (err)
6464 ebf99748 2019-05-09 stsp goto done;
6465 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6466 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6467 09f5bd90 2019-05-09 stsp goto done;
6468 09f5bd90 2019-05-09 stsp }
6469 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6470 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6471 f2c16586 2019-05-09 stsp if (err)
6472 f2c16586 2019-05-09 stsp goto done;
6473 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6474 f2c16586 2019-05-09 stsp if (err)
6475 f2c16586 2019-05-09 stsp goto done;
6476 f2c16586 2019-05-09 stsp
6477 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6478 09f5bd90 2019-05-09 stsp if (err)
6479 09f5bd90 2019-05-09 stsp goto done;
6480 09f5bd90 2019-05-09 stsp
6481 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6482 ebf99748 2019-05-09 stsp if (err)
6483 ebf99748 2019-05-09 stsp goto done;
6484 c4296144 2019-05-09 stsp done:
6485 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6486 588edf97 2019-05-10 stsp if (head_tree)
6487 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6488 588edf97 2019-05-10 stsp if (head_commit)
6489 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6490 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6491 2f17228e 2019-05-12 stsp if (head_ref2) {
6492 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6493 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6494 2f17228e 2019-05-12 stsp err = unlockerr;
6495 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6496 39cd0ff6 2019-07-12 stsp }
6497 39cd0ff6 2019-07-12 stsp return err;
6498 39cd0ff6 2019-07-12 stsp }
6499 5c1e53bc 2019-07-28 stsp
6500 5c1e53bc 2019-07-28 stsp static const struct got_error *
6501 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6502 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6503 5c1e53bc 2019-07-28 stsp {
6504 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6505 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6506 39cd0ff6 2019-07-12 stsp
6507 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6508 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6509 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6510 5c1e53bc 2019-07-28 stsp
6511 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6512 5c1e53bc 2019-07-28 stsp ct_path++;
6513 5c1e53bc 2019-07-28 stsp
6514 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6515 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6516 5c1e53bc 2019-07-28 stsp break;
6517 5c1e53bc 2019-07-28 stsp }
6518 5c1e53bc 2019-07-28 stsp
6519 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6520 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6521 5c1e53bc 2019-07-28 stsp
6522 5c1e53bc 2019-07-28 stsp return NULL;
6523 5c1e53bc 2019-07-28 stsp }
6524 5c1e53bc 2019-07-28 stsp
6525 f0b75401 2019-08-03 stsp static const struct got_error *
6526 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6527 f0b75401 2019-08-03 stsp {
6528 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6529 f0b75401 2019-08-03 stsp
6530 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6531 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6532 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6533 f0b75401 2019-08-03 stsp }
6534 f0b75401 2019-08-03 stsp
6535 f0b75401 2019-08-03 stsp return NULL;
6536 f0b75401 2019-08-03 stsp }
6537 f0b75401 2019-08-03 stsp
6538 5f8a88c6 2019-08-03 stsp static const struct got_error *
6539 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6540 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6541 5f8a88c6 2019-08-03 stsp {
6542 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6543 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6544 5f8a88c6 2019-08-03 stsp
6545 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6546 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6547 5f8a88c6 2019-08-03 stsp continue;
6548 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6549 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6550 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6551 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6552 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6553 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6554 5f8a88c6 2019-08-03 stsp }
6555 5f8a88c6 2019-08-03 stsp
6556 5f8a88c6 2019-08-03 stsp return NULL;
6557 5f8a88c6 2019-08-03 stsp }
6558 5f8a88c6 2019-08-03 stsp
6559 39cd0ff6 2019-07-12 stsp const struct got_error *
6560 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6561 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6562 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6563 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6564 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6565 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6566 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6567 39cd0ff6 2019-07-12 stsp {
6568 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6569 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6570 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6571 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6572 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6573 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6574 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6575 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6576 ef899790 2022-11-01 thomas char *diff_path = NULL;
6577 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6578 39cd0ff6 2019-07-12 stsp
6579 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6580 39cd0ff6 2019-07-12 stsp
6581 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6582 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6583 39cd0ff6 2019-07-12 stsp
6584 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6585 39cd0ff6 2019-07-12 stsp if (err)
6586 39cd0ff6 2019-07-12 stsp goto done;
6587 39cd0ff6 2019-07-12 stsp
6588 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6589 39cd0ff6 2019-07-12 stsp if (err)
6590 39cd0ff6 2019-07-12 stsp goto done;
6591 39cd0ff6 2019-07-12 stsp
6592 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6593 39cd0ff6 2019-07-12 stsp if (err)
6594 39cd0ff6 2019-07-12 stsp goto done;
6595 39cd0ff6 2019-07-12 stsp
6596 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6597 39cd0ff6 2019-07-12 stsp if (err)
6598 39cd0ff6 2019-07-12 stsp goto done;
6599 39cd0ff6 2019-07-12 stsp
6600 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6601 5f8a88c6 2019-08-03 stsp &have_staged_files);
6602 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6603 5f8a88c6 2019-08-03 stsp goto done;
6604 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6605 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6606 5f8a88c6 2019-08-03 stsp if (err)
6607 5f8a88c6 2019-08-03 stsp goto done;
6608 5f8a88c6 2019-08-03 stsp }
6609 5f8a88c6 2019-08-03 stsp
6610 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6611 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6612 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6613 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6614 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6615 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6616 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6617 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6618 ef899790 2022-11-01 thomas if (show_diff) {
6619 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6620 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6621 ef899790 2022-11-01 thomas if (err)
6622 ef899790 2022-11-01 thomas goto done;
6623 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6624 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6625 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6626 ef899790 2022-11-01 thomas goto done;
6627 ef899790 2022-11-01 thomas }
6628 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6629 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6630 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6631 ef899790 2022-11-01 thomas goto done;
6632 ef899790 2022-11-01 thomas }
6633 ef899790 2022-11-01 thomas }
6634 ef899790 2022-11-01 thomas
6635 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6636 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6637 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6638 5c1e53bc 2019-07-28 stsp if (err)
6639 5c1e53bc 2019-07-28 stsp goto done;
6640 5c1e53bc 2019-07-28 stsp }
6641 39cd0ff6 2019-07-12 stsp
6642 ef899790 2022-11-01 thomas if (show_diff) {
6643 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6644 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6645 ef899790 2022-11-01 thomas goto done;
6646 ef899790 2022-11-01 thomas }
6647 ef899790 2022-11-01 thomas }
6648 ef899790 2022-11-01 thomas
6649 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6650 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6651 39cd0ff6 2019-07-12 stsp goto done;
6652 5c1e53bc 2019-07-28 stsp }
6653 5c1e53bc 2019-07-28 stsp
6654 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6655 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6656 5c1e53bc 2019-07-28 stsp if (err)
6657 5c1e53bc 2019-07-28 stsp goto done;
6658 39cd0ff6 2019-07-12 stsp }
6659 f0b75401 2019-08-03 stsp
6660 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6661 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6662 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6663 f0b75401 2019-08-03 stsp
6664 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6665 f0b75401 2019-08-03 stsp ct_path++;
6666 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6667 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6668 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6669 b50cabdf 2019-07-12 stsp if (err)
6670 b50cabdf 2019-07-12 stsp goto done;
6671 f0b75401 2019-08-03 stsp
6672 b50cabdf 2019-07-12 stsp }
6673 b50cabdf 2019-07-12 stsp
6674 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6675 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6676 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6677 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6678 39cd0ff6 2019-07-12 stsp if (err)
6679 39cd0ff6 2019-07-12 stsp goto done;
6680 39cd0ff6 2019-07-12 stsp
6681 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6682 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6683 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6684 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6685 39cd0ff6 2019-07-12 stsp err = sync_err;
6686 39cd0ff6 2019-07-12 stsp done:
6687 39cd0ff6 2019-07-12 stsp if (fileindex)
6688 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6689 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6690 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6691 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6692 39cd0ff6 2019-07-12 stsp err = unlockerr;
6693 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6694 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6695 21c2d8be 2023-01-10 thomas
6696 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6697 39cd0ff6 2019-07-12 stsp }
6698 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6699 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6700 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6701 ef899790 2022-11-01 thomas free(diff_path);
6702 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6703 ef899790 2022-11-01 thomas err == NULL)
6704 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6705 c4296144 2019-05-09 stsp return err;
6706 8656d6c4 2019-05-20 stsp }
6707 8656d6c4 2019-05-20 stsp
6708 8656d6c4 2019-05-20 stsp const char *
6709 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6710 8656d6c4 2019-05-20 stsp {
6711 8656d6c4 2019-05-20 stsp return ct->path;
6712 8656d6c4 2019-05-20 stsp }
6713 8656d6c4 2019-05-20 stsp
6714 8656d6c4 2019-05-20 stsp unsigned int
6715 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6716 8656d6c4 2019-05-20 stsp {
6717 8656d6c4 2019-05-20 stsp return ct->status;
6718 818c7501 2019-07-11 stsp }
6719 818c7501 2019-07-11 stsp
6720 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6721 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6722 818c7501 2019-07-11 stsp struct got_repository *repo;
6723 818c7501 2019-07-11 stsp };
6724 818c7501 2019-07-11 stsp
6725 818c7501 2019-07-11 stsp static const struct got_error *
6726 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6727 818c7501 2019-07-11 stsp {
6728 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6729 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6730 818c7501 2019-07-11 stsp unsigned char status;
6731 818c7501 2019-07-11 stsp struct stat sb;
6732 818c7501 2019-07-11 stsp char *ondisk_path;
6733 818c7501 2019-07-11 stsp
6734 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6735 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6736 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6737 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6738 818c7501 2019-07-11 stsp
6739 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6740 818c7501 2019-07-11 stsp == -1)
6741 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6742 818c7501 2019-07-11 stsp
6743 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6744 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6745 818c7501 2019-07-11 stsp free(ondisk_path);
6746 818c7501 2019-07-11 stsp if (err)
6747 818c7501 2019-07-11 stsp return err;
6748 818c7501 2019-07-11 stsp
6749 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6750 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6751 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6752 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6753 818c7501 2019-07-11 stsp
6754 818c7501 2019-07-11 stsp return NULL;
6755 818c7501 2019-07-11 stsp }
6756 818c7501 2019-07-11 stsp
6757 818c7501 2019-07-11 stsp const struct got_error *
6758 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6759 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6760 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6761 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6762 818c7501 2019-07-11 stsp {
6763 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6764 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6765 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6766 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6767 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6768 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6769 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6770 818c7501 2019-07-11 stsp
6771 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6772 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6773 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6774 818c7501 2019-07-11 stsp
6775 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6776 818c7501 2019-07-11 stsp if (err)
6777 818c7501 2019-07-11 stsp return err;
6778 818c7501 2019-07-11 stsp
6779 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6780 818c7501 2019-07-11 stsp if (err)
6781 818c7501 2019-07-11 stsp goto done;
6782 818c7501 2019-07-11 stsp
6783 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6784 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6785 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6786 818c7501 2019-07-11 stsp &ok_arg);
6787 818c7501 2019-07-11 stsp if (err)
6788 818c7501 2019-07-11 stsp goto done;
6789 818c7501 2019-07-11 stsp
6790 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6791 818c7501 2019-07-11 stsp if (err)
6792 818c7501 2019-07-11 stsp goto done;
6793 818c7501 2019-07-11 stsp
6794 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6795 818c7501 2019-07-11 stsp if (err)
6796 818c7501 2019-07-11 stsp goto done;
6797 818c7501 2019-07-11 stsp
6798 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6799 818c7501 2019-07-11 stsp if (err)
6800 818c7501 2019-07-11 stsp goto done;
6801 818c7501 2019-07-11 stsp
6802 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6803 818c7501 2019-07-11 stsp 0);
6804 e51d7b55 2020-01-04 stsp if (err)
6805 e51d7b55 2020-01-04 stsp goto done;
6806 e51d7b55 2020-01-04 stsp
6807 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6808 818c7501 2019-07-11 stsp if (err)
6809 818c7501 2019-07-11 stsp goto done;
6810 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6811 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6812 e51d7b55 2020-01-04 stsp goto done;
6813 e51d7b55 2020-01-04 stsp }
6814 818c7501 2019-07-11 stsp
6815 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6816 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6817 818c7501 2019-07-11 stsp if (err)
6818 818c7501 2019-07-11 stsp goto done;
6819 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6820 818c7501 2019-07-11 stsp if (err)
6821 818c7501 2019-07-11 stsp goto done;
6822 818c7501 2019-07-11 stsp
6823 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6824 818c7501 2019-07-11 stsp
6825 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6826 818c7501 2019-07-11 stsp if (err)
6827 818c7501 2019-07-11 stsp goto done;
6828 818c7501 2019-07-11 stsp
6829 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6830 818c7501 2019-07-11 stsp if (err)
6831 818c7501 2019-07-11 stsp goto done;
6832 818c7501 2019-07-11 stsp
6833 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6834 818c7501 2019-07-11 stsp worktree->base_commit_id);
6835 818c7501 2019-07-11 stsp if (err)
6836 818c7501 2019-07-11 stsp goto done;
6837 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6838 818c7501 2019-07-11 stsp if (err)
6839 818c7501 2019-07-11 stsp goto done;
6840 818c7501 2019-07-11 stsp
6841 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6842 818c7501 2019-07-11 stsp if (err)
6843 818c7501 2019-07-11 stsp goto done;
6844 818c7501 2019-07-11 stsp done:
6845 818c7501 2019-07-11 stsp free(fileindex_path);
6846 818c7501 2019-07-11 stsp free(tmp_branch_name);
6847 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6848 818c7501 2019-07-11 stsp free(branch_ref_name);
6849 818c7501 2019-07-11 stsp if (branch_ref)
6850 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6851 818c7501 2019-07-11 stsp if (wt_branch)
6852 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6853 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6854 818c7501 2019-07-11 stsp if (err) {
6855 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6856 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6857 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6858 818c7501 2019-07-11 stsp }
6859 818c7501 2019-07-11 stsp if (*tmp_branch) {
6860 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6861 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6862 818c7501 2019-07-11 stsp }
6863 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6864 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6865 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6866 3e3a69f1 2019-07-25 stsp }
6867 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6868 818c7501 2019-07-11 stsp }
6869 818c7501 2019-07-11 stsp return err;
6870 818c7501 2019-07-11 stsp }
6871 818c7501 2019-07-11 stsp
6872 818c7501 2019-07-11 stsp const struct got_error *
6873 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6874 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6875 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6876 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6877 818c7501 2019-07-11 stsp {
6878 818c7501 2019-07-11 stsp const struct got_error *err;
6879 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6880 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6881 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6882 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6883 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6884 818c7501 2019-07-11 stsp
6885 818c7501 2019-07-11 stsp *commit_id = NULL;
6886 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6887 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6888 3e3a69f1 2019-07-25 stsp *branch = NULL;
6889 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6890 3e3a69f1 2019-07-25 stsp
6891 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6892 3e3a69f1 2019-07-25 stsp if (err)
6893 3e3a69f1 2019-07-25 stsp return err;
6894 818c7501 2019-07-11 stsp
6895 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6896 3e3a69f1 2019-07-25 stsp if (err)
6897 f032f1f7 2019-08-04 stsp goto done;
6898 f032f1f7 2019-08-04 stsp
6899 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6900 f032f1f7 2019-08-04 stsp &have_staged_files);
6901 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6902 f032f1f7 2019-08-04 stsp goto done;
6903 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6904 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6905 3e3a69f1 2019-07-25 stsp goto done;
6906 f032f1f7 2019-08-04 stsp }
6907 3e3a69f1 2019-07-25 stsp
6908 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6909 818c7501 2019-07-11 stsp if (err)
6910 f032f1f7 2019-08-04 stsp goto done;
6911 818c7501 2019-07-11 stsp
6912 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6913 818c7501 2019-07-11 stsp if (err)
6914 818c7501 2019-07-11 stsp goto done;
6915 818c7501 2019-07-11 stsp
6916 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6917 818c7501 2019-07-11 stsp if (err)
6918 818c7501 2019-07-11 stsp goto done;
6919 818c7501 2019-07-11 stsp
6920 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6921 818c7501 2019-07-11 stsp if (err)
6922 818c7501 2019-07-11 stsp goto done;
6923 818c7501 2019-07-11 stsp
6924 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6925 818c7501 2019-07-11 stsp if (err)
6926 818c7501 2019-07-11 stsp goto done;
6927 818c7501 2019-07-11 stsp
6928 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6929 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6930 818c7501 2019-07-11 stsp if (err)
6931 818c7501 2019-07-11 stsp goto done;
6932 818c7501 2019-07-11 stsp
6933 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6934 818c7501 2019-07-11 stsp if (err)
6935 818c7501 2019-07-11 stsp goto done;
6936 818c7501 2019-07-11 stsp
6937 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6938 818c7501 2019-07-11 stsp if (err)
6939 818c7501 2019-07-11 stsp goto done;
6940 818c7501 2019-07-11 stsp
6941 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6942 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6943 818c7501 2019-07-11 stsp if (err)
6944 818c7501 2019-07-11 stsp goto done;
6945 818c7501 2019-07-11 stsp
6946 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6947 818c7501 2019-07-11 stsp if (err)
6948 818c7501 2019-07-11 stsp goto done;
6949 818c7501 2019-07-11 stsp done:
6950 818c7501 2019-07-11 stsp free(commit_ref_name);
6951 818c7501 2019-07-11 stsp free(branch_ref_name);
6952 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6953 818c7501 2019-07-11 stsp if (commit_ref)
6954 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6955 818c7501 2019-07-11 stsp if (branch_ref)
6956 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6957 818c7501 2019-07-11 stsp if (err) {
6958 818c7501 2019-07-11 stsp free(*commit_id);
6959 818c7501 2019-07-11 stsp *commit_id = NULL;
6960 818c7501 2019-07-11 stsp if (*tmp_branch) {
6961 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6962 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6963 818c7501 2019-07-11 stsp }
6964 818c7501 2019-07-11 stsp if (*new_base_branch) {
6965 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6966 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6967 818c7501 2019-07-11 stsp }
6968 818c7501 2019-07-11 stsp if (*branch) {
6969 818c7501 2019-07-11 stsp got_ref_close(*branch);
6970 818c7501 2019-07-11 stsp *branch = NULL;
6971 818c7501 2019-07-11 stsp }
6972 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6973 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6974 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6975 3e3a69f1 2019-07-25 stsp }
6976 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6977 818c7501 2019-07-11 stsp }
6978 818c7501 2019-07-11 stsp return err;
6979 c4296144 2019-05-09 stsp }
6980 818c7501 2019-07-11 stsp
6981 818c7501 2019-07-11 stsp const struct got_error *
6982 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6983 818c7501 2019-07-11 stsp {
6984 818c7501 2019-07-11 stsp const struct got_error *err;
6985 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6986 818c7501 2019-07-11 stsp
6987 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6988 818c7501 2019-07-11 stsp if (err)
6989 818c7501 2019-07-11 stsp return err;
6990 818c7501 2019-07-11 stsp
6991 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6992 818c7501 2019-07-11 stsp free(tmp_branch_name);
6993 818c7501 2019-07-11 stsp return NULL;
6994 818c7501 2019-07-11 stsp }
6995 818c7501 2019-07-11 stsp
6996 818c7501 2019-07-11 stsp static const struct got_error *
6997 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6998 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6999 818c7501 2019-07-11 stsp {
7000 0ebf8283 2019-07-24 stsp *logmsg = arg;
7001 818c7501 2019-07-11 stsp return NULL;
7002 818c7501 2019-07-11 stsp }
7003 818c7501 2019-07-11 stsp
7004 818c7501 2019-07-11 stsp static const struct got_error *
7005 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7006 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7007 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7008 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7009 818c7501 2019-07-11 stsp {
7010 818c7501 2019-07-11 stsp return NULL;
7011 01757395 2019-07-12 stsp }
7012 01757395 2019-07-12 stsp
7013 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
7014 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
7015 01757395 2019-07-12 stsp void *progress_arg;
7016 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
7017 01757395 2019-07-12 stsp };
7018 01757395 2019-07-12 stsp
7019 01757395 2019-07-12 stsp static const struct got_error *
7020 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
7021 01757395 2019-07-12 stsp {
7022 01757395 2019-07-12 stsp const struct got_error *err;
7023 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
7024 01757395 2019-07-12 stsp char *p;
7025 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
7026 01757395 2019-07-12 stsp
7027 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
7028 01757395 2019-07-12 stsp if (err)
7029 01757395 2019-07-12 stsp return err;
7030 01757395 2019-07-12 stsp
7031 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
7032 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
7033 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
7034 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
7035 01757395 2019-07-12 stsp return NULL;
7036 01757395 2019-07-12 stsp
7037 01757395 2019-07-12 stsp p = strdup(path);
7038 01757395 2019-07-12 stsp if (p == NULL)
7039 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7040 01757395 2019-07-12 stsp
7041 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7042 01757395 2019-07-12 stsp if (err || new == NULL)
7043 01757395 2019-07-12 stsp free(p);
7044 01757395 2019-07-12 stsp return err;
7045 818c7501 2019-07-11 stsp }
7046 818c7501 2019-07-11 stsp
7047 0ebf8283 2019-07-24 stsp static const struct got_error *
7048 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7049 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7050 818c7501 2019-07-11 stsp {
7051 818c7501 2019-07-11 stsp const struct got_error *err;
7052 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7053 818c7501 2019-07-11 stsp
7054 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7055 818c7501 2019-07-11 stsp if (err) {
7056 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7057 818c7501 2019-07-11 stsp goto done;
7058 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7059 818c7501 2019-07-11 stsp if (err)
7060 818c7501 2019-07-11 stsp goto done;
7061 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7062 818c7501 2019-07-11 stsp if (err)
7063 818c7501 2019-07-11 stsp goto done;
7064 de05890f 2020-03-05 stsp } else if (is_rebase) {
7065 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7066 818c7501 2019-07-11 stsp int cmp;
7067 818c7501 2019-07-11 stsp
7068 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7069 818c7501 2019-07-11 stsp if (err)
7070 818c7501 2019-07-11 stsp goto done;
7071 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7072 818c7501 2019-07-11 stsp free(stored_id);
7073 818c7501 2019-07-11 stsp if (cmp != 0) {
7074 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7075 818c7501 2019-07-11 stsp goto done;
7076 818c7501 2019-07-11 stsp }
7077 818c7501 2019-07-11 stsp }
7078 0ebf8283 2019-07-24 stsp done:
7079 0ebf8283 2019-07-24 stsp if (commit_ref)
7080 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7081 0ebf8283 2019-07-24 stsp return err;
7082 0ebf8283 2019-07-24 stsp }
7083 0ebf8283 2019-07-24 stsp
7084 0ebf8283 2019-07-24 stsp static const struct got_error *
7085 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7086 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7087 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7088 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7089 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7090 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7091 0ebf8283 2019-07-24 stsp {
7092 0ebf8283 2019-07-24 stsp const struct got_error *err;
7093 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7094 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7095 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7096 818c7501 2019-07-11 stsp
7097 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7098 0ebf8283 2019-07-24 stsp
7099 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7100 0ebf8283 2019-07-24 stsp if (err)
7101 0ebf8283 2019-07-24 stsp return err;
7102 0ebf8283 2019-07-24 stsp
7103 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7104 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7105 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7106 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7107 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7108 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7109 818c7501 2019-07-11 stsp if (commit_ref)
7110 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7111 818c7501 2019-07-11 stsp return err;
7112 818c7501 2019-07-11 stsp }
7113 818c7501 2019-07-11 stsp
7114 818c7501 2019-07-11 stsp const struct got_error *
7115 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7116 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7117 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7118 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7119 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7120 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7121 818c7501 2019-07-11 stsp {
7122 0ebf8283 2019-07-24 stsp const struct got_error *err;
7123 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7124 0ebf8283 2019-07-24 stsp
7125 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7126 0ebf8283 2019-07-24 stsp if (err)
7127 0ebf8283 2019-07-24 stsp return err;
7128 0ebf8283 2019-07-24 stsp
7129 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7130 0ebf8283 2019-07-24 stsp if (err)
7131 0ebf8283 2019-07-24 stsp goto done;
7132 0ebf8283 2019-07-24 stsp
7133 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7134 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7135 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7136 0ebf8283 2019-07-24 stsp done:
7137 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7138 0ebf8283 2019-07-24 stsp return err;
7139 0ebf8283 2019-07-24 stsp }
7140 0ebf8283 2019-07-24 stsp
7141 0ebf8283 2019-07-24 stsp const struct got_error *
7142 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7143 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7144 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7145 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7146 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7147 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7148 0ebf8283 2019-07-24 stsp {
7149 0ebf8283 2019-07-24 stsp const struct got_error *err;
7150 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7151 0ebf8283 2019-07-24 stsp
7152 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7153 0ebf8283 2019-07-24 stsp if (err)
7154 0ebf8283 2019-07-24 stsp return err;
7155 0ebf8283 2019-07-24 stsp
7156 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7157 0ebf8283 2019-07-24 stsp if (err)
7158 0ebf8283 2019-07-24 stsp goto done;
7159 0ebf8283 2019-07-24 stsp
7160 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7161 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7162 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7163 0ebf8283 2019-07-24 stsp done:
7164 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7165 0ebf8283 2019-07-24 stsp return err;
7166 0ebf8283 2019-07-24 stsp }
7167 0ebf8283 2019-07-24 stsp
7168 0ebf8283 2019-07-24 stsp static const struct got_error *
7169 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7170 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7171 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7172 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7173 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7174 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7175 0ebf8283 2019-07-24 stsp {
7176 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7177 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7178 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7179 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7180 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7181 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7182 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7183 818c7501 2019-07-11 stsp
7184 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7185 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7186 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7187 a0e95631 2019-07-12 stsp
7188 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7189 818c7501 2019-07-11 stsp
7190 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7191 a0e95631 2019-07-12 stsp if (err)
7192 3e3a69f1 2019-07-25 stsp return err;
7193 a0e95631 2019-07-12 stsp
7194 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7195 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7196 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7197 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7198 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7199 01757395 2019-07-12 stsp /*
7200 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7201 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7202 6c13b005 2021-09-02 stsp *
7203 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7204 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7205 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7206 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7207 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7208 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7209 01757395 2019-07-12 stsp */
7210 01757395 2019-07-12 stsp if (merged_paths) {
7211 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7212 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7213 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7214 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7215 f2a9dc41 2019-12-13 tracey 0);
7216 01757395 2019-07-12 stsp if (err)
7217 01757395 2019-07-12 stsp goto done;
7218 01757395 2019-07-12 stsp }
7219 01757395 2019-07-12 stsp } else {
7220 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7221 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7222 01757395 2019-07-12 stsp if (err)
7223 01757395 2019-07-12 stsp goto done;
7224 01757395 2019-07-12 stsp }
7225 a0e95631 2019-07-12 stsp
7226 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7227 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7228 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7229 a0e95631 2019-07-12 stsp if (err)
7230 a0e95631 2019-07-12 stsp goto done;
7231 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7232 a0e95631 2019-07-12 stsp goto done;
7233 ff0d2220 2019-07-11 stsp }
7234 818c7501 2019-07-11 stsp
7235 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7236 edd02c5e 2019-07-11 stsp if (err)
7237 edd02c5e 2019-07-11 stsp goto done;
7238 edd02c5e 2019-07-11 stsp
7239 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7240 818c7501 2019-07-11 stsp if (err)
7241 818c7501 2019-07-11 stsp goto done;
7242 818c7501 2019-07-11 stsp
7243 5943eee2 2019-08-13 stsp if (new_logmsg) {
7244 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7245 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7246 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7247 5943eee2 2019-08-13 stsp goto done;
7248 5943eee2 2019-08-13 stsp }
7249 5943eee2 2019-08-13 stsp } else {
7250 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7251 5943eee2 2019-08-13 stsp if (err)
7252 5943eee2 2019-08-13 stsp goto done;
7253 5943eee2 2019-08-13 stsp }
7254 0ebf8283 2019-07-24 stsp
7255 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7256 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7257 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7258 8db00f97 2022-07-22 thomas committer ? committer :
7259 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7260 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7261 a0e95631 2019-07-12 stsp if (err)
7262 a0e95631 2019-07-12 stsp goto done;
7263 a0e95631 2019-07-12 stsp
7264 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7265 a0e95631 2019-07-12 stsp if (err)
7266 a0e95631 2019-07-12 stsp goto done;
7267 a0e95631 2019-07-12 stsp
7268 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7269 a0e95631 2019-07-12 stsp if (err)
7270 a0e95631 2019-07-12 stsp goto done;
7271 a0e95631 2019-07-12 stsp
7272 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7273 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7274 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7275 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7276 a0e95631 2019-07-12 stsp err = sync_err;
7277 818c7501 2019-07-11 stsp done:
7278 a0e95631 2019-07-12 stsp free(fileindex_path);
7279 a0e95631 2019-07-12 stsp free(head_commit_id);
7280 a0e95631 2019-07-12 stsp if (head_ref)
7281 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7282 818c7501 2019-07-11 stsp if (err) {
7283 818c7501 2019-07-11 stsp free(*new_commit_id);
7284 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7285 818c7501 2019-07-11 stsp }
7286 818c7501 2019-07-11 stsp return err;
7287 818c7501 2019-07-11 stsp }
7288 818c7501 2019-07-11 stsp
7289 818c7501 2019-07-11 stsp const struct got_error *
7290 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7291 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7292 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7293 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7294 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7295 be94c032 2023-02-20 thomas struct got_repository *repo)
7296 0ebf8283 2019-07-24 stsp {
7297 0ebf8283 2019-07-24 stsp const struct got_error *err;
7298 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7299 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7300 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7301 0ebf8283 2019-07-24 stsp
7302 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7303 0ebf8283 2019-07-24 stsp if (err)
7304 0ebf8283 2019-07-24 stsp return err;
7305 0ebf8283 2019-07-24 stsp
7306 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7307 0ebf8283 2019-07-24 stsp if (err)
7308 0ebf8283 2019-07-24 stsp goto done;
7309 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7310 0ebf8283 2019-07-24 stsp if (err)
7311 0ebf8283 2019-07-24 stsp goto done;
7312 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7313 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7314 0ebf8283 2019-07-24 stsp goto done;
7315 0ebf8283 2019-07-24 stsp }
7316 0ebf8283 2019-07-24 stsp
7317 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7318 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7319 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7320 0ebf8283 2019-07-24 stsp done:
7321 0ebf8283 2019-07-24 stsp if (commit_ref)
7322 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7323 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7324 0ebf8283 2019-07-24 stsp free(commit_id);
7325 0ebf8283 2019-07-24 stsp return err;
7326 0ebf8283 2019-07-24 stsp }
7327 0ebf8283 2019-07-24 stsp
7328 0ebf8283 2019-07-24 stsp const struct got_error *
7329 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7330 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7331 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7332 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7333 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7334 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7335 0ebf8283 2019-07-24 stsp {
7336 0ebf8283 2019-07-24 stsp const struct got_error *err;
7337 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7338 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7339 0ebf8283 2019-07-24 stsp
7340 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7341 0ebf8283 2019-07-24 stsp if (err)
7342 0ebf8283 2019-07-24 stsp return err;
7343 0ebf8283 2019-07-24 stsp
7344 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7345 0ebf8283 2019-07-24 stsp if (err)
7346 0ebf8283 2019-07-24 stsp goto done;
7347 0ebf8283 2019-07-24 stsp
7348 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7349 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7350 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7351 0ebf8283 2019-07-24 stsp done:
7352 0ebf8283 2019-07-24 stsp if (commit_ref)
7353 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7354 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7355 0ebf8283 2019-07-24 stsp return err;
7356 0ebf8283 2019-07-24 stsp }
7357 0ebf8283 2019-07-24 stsp
7358 0ebf8283 2019-07-24 stsp const struct got_error *
7359 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7360 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7361 818c7501 2019-07-11 stsp {
7362 3e3a69f1 2019-07-25 stsp if (fileindex)
7363 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7364 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7365 69844fba 2019-07-11 stsp }
7366 69844fba 2019-07-11 stsp
7367 69844fba 2019-07-11 stsp static const struct got_error *
7368 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7369 69844fba 2019-07-11 stsp {
7370 69844fba 2019-07-11 stsp const struct got_error *err;
7371 69844fba 2019-07-11 stsp struct got_reference *ref;
7372 69844fba 2019-07-11 stsp
7373 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7374 69844fba 2019-07-11 stsp if (err) {
7375 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7376 69844fba 2019-07-11 stsp return NULL;
7377 69844fba 2019-07-11 stsp return err;
7378 69844fba 2019-07-11 stsp }
7379 69844fba 2019-07-11 stsp
7380 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7381 69844fba 2019-07-11 stsp got_ref_close(ref);
7382 69844fba 2019-07-11 stsp return err;
7383 818c7501 2019-07-11 stsp }
7384 818c7501 2019-07-11 stsp
7385 69844fba 2019-07-11 stsp static const struct got_error *
7386 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7387 69844fba 2019-07-11 stsp {
7388 69844fba 2019-07-11 stsp const struct got_error *err;
7389 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7390 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7391 69844fba 2019-07-11 stsp
7392 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7393 69844fba 2019-07-11 stsp if (err)
7394 69844fba 2019-07-11 stsp goto done;
7395 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7396 69844fba 2019-07-11 stsp if (err)
7397 69844fba 2019-07-11 stsp goto done;
7398 69844fba 2019-07-11 stsp
7399 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7400 69844fba 2019-07-11 stsp if (err)
7401 69844fba 2019-07-11 stsp goto done;
7402 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7403 69844fba 2019-07-11 stsp if (err)
7404 69844fba 2019-07-11 stsp goto done;
7405 69844fba 2019-07-11 stsp
7406 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7407 69844fba 2019-07-11 stsp if (err)
7408 69844fba 2019-07-11 stsp goto done;
7409 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7410 69844fba 2019-07-11 stsp if (err)
7411 69844fba 2019-07-11 stsp goto done;
7412 69844fba 2019-07-11 stsp
7413 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7414 69844fba 2019-07-11 stsp if (err)
7415 69844fba 2019-07-11 stsp goto done;
7416 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7417 69844fba 2019-07-11 stsp if (err)
7418 69844fba 2019-07-11 stsp goto done;
7419 69844fba 2019-07-11 stsp
7420 69844fba 2019-07-11 stsp done:
7421 69844fba 2019-07-11 stsp free(tmp_branch_name);
7422 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7423 69844fba 2019-07-11 stsp free(branch_ref_name);
7424 69844fba 2019-07-11 stsp free(commit_ref_name);
7425 e600f124 2021-03-21 stsp return err;
7426 e600f124 2021-03-21 stsp }
7427 e600f124 2021-03-21 stsp
7428 ef20f542 2022-06-26 thomas static const struct got_error *
7429 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7430 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7431 e600f124 2021-03-21 stsp {
7432 e600f124 2021-03-21 stsp const struct got_error *err;
7433 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7434 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7435 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7436 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7437 e600f124 2021-03-21 stsp char *refname = NULL;
7438 e600f124 2021-03-21 stsp
7439 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7440 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7441 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7442 e600f124 2021-03-21 stsp branch_name += 11;
7443 e600f124 2021-03-21 stsp
7444 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7445 e600f124 2021-03-21 stsp if (err)
7446 e600f124 2021-03-21 stsp return err;
7447 e600f124 2021-03-21 stsp
7448 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7449 e600f124 2021-03-21 stsp new_id_str) == -1) {
7450 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7451 e600f124 2021-03-21 stsp goto done;
7452 e600f124 2021-03-21 stsp }
7453 e600f124 2021-03-21 stsp
7454 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7455 e600f124 2021-03-21 stsp if (err)
7456 e600f124 2021-03-21 stsp goto done;
7457 e600f124 2021-03-21 stsp
7458 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7459 e600f124 2021-03-21 stsp if (err)
7460 e600f124 2021-03-21 stsp goto done;
7461 e600f124 2021-03-21 stsp
7462 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7463 e600f124 2021-03-21 stsp done:
7464 e600f124 2021-03-21 stsp free(new_id_str);
7465 e600f124 2021-03-21 stsp free(refname);
7466 e600f124 2021-03-21 stsp free(old_commit_id);
7467 e600f124 2021-03-21 stsp if (ref)
7468 e600f124 2021-03-21 stsp got_ref_close(ref);
7469 69844fba 2019-07-11 stsp return err;
7470 69844fba 2019-07-11 stsp }
7471 69844fba 2019-07-11 stsp
7472 818c7501 2019-07-11 stsp const struct got_error *
7473 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7474 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7475 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7476 f08f28be 2023-02-03 thomas int create_backup)
7477 818c7501 2019-07-11 stsp {
7478 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7479 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7480 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7481 818c7501 2019-07-11 stsp
7482 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7483 818c7501 2019-07-11 stsp if (err)
7484 818c7501 2019-07-11 stsp return err;
7485 e600f124 2021-03-21 stsp
7486 e600f124 2021-03-21 stsp if (create_backup) {
7487 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7488 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7489 e600f124 2021-03-21 stsp if (err)
7490 e600f124 2021-03-21 stsp goto done;
7491 e600f124 2021-03-21 stsp }
7492 818c7501 2019-07-11 stsp
7493 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7494 818c7501 2019-07-11 stsp if (err)
7495 818c7501 2019-07-11 stsp goto done;
7496 818c7501 2019-07-11 stsp
7497 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7498 818c7501 2019-07-11 stsp if (err)
7499 818c7501 2019-07-11 stsp goto done;
7500 818c7501 2019-07-11 stsp
7501 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7502 818c7501 2019-07-11 stsp if (err)
7503 818c7501 2019-07-11 stsp goto done;
7504 818c7501 2019-07-11 stsp
7505 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7506 a615e0e7 2020-12-16 stsp if (err)
7507 a615e0e7 2020-12-16 stsp goto done;
7508 a615e0e7 2020-12-16 stsp
7509 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7510 a615e0e7 2020-12-16 stsp if (err)
7511 a615e0e7 2020-12-16 stsp goto done;
7512 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7513 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7514 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7515 a615e0e7 2020-12-16 stsp err = sync_err;
7516 818c7501 2019-07-11 stsp done:
7517 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7518 a615e0e7 2020-12-16 stsp free(fileindex_path);
7519 818c7501 2019-07-11 stsp free(new_head_commit_id);
7520 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7521 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7522 818c7501 2019-07-11 stsp err = unlockerr;
7523 818c7501 2019-07-11 stsp return err;
7524 818c7501 2019-07-11 stsp }
7525 8641a332 2023-04-14 thomas
7526 8641a332 2023-04-14 thomas static const struct got_error *
7527 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7528 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7529 8641a332 2023-04-14 thomas struct got_repository *repo)
7530 8641a332 2023-04-14 thomas {
7531 8641a332 2023-04-14 thomas const struct got_error *err;
7532 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7533 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7534 8641a332 2023-04-14 thomas
7535 8641a332 2023-04-14 thomas if (id1) {
7536 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7537 8641a332 2023-04-14 thomas if (err)
7538 8641a332 2023-04-14 thomas goto done;
7539 8641a332 2023-04-14 thomas
7540 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7541 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7542 8641a332 2023-04-14 thomas if (err)
7543 8641a332 2023-04-14 thomas goto done;
7544 8641a332 2023-04-14 thomas }
7545 818c7501 2019-07-11 stsp
7546 8641a332 2023-04-14 thomas if (id2) {
7547 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7548 8641a332 2023-04-14 thomas if (err)
7549 8641a332 2023-04-14 thomas goto done;
7550 8641a332 2023-04-14 thomas
7551 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7552 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7553 8641a332 2023-04-14 thomas if (err)
7554 8641a332 2023-04-14 thomas goto done;
7555 8641a332 2023-04-14 thomas }
7556 8641a332 2023-04-14 thomas
7557 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7558 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7559 8641a332 2023-04-14 thomas if (err)
7560 8641a332 2023-04-14 thomas goto done;
7561 8641a332 2023-04-14 thomas done:
7562 8641a332 2023-04-14 thomas if (commit1)
7563 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7564 8641a332 2023-04-14 thomas if (commit2)
7565 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7566 8641a332 2023-04-14 thomas if (tree1)
7567 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7568 8641a332 2023-04-14 thomas if (tree2)
7569 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7570 8641a332 2023-04-14 thomas return err;
7571 8641a332 2023-04-14 thomas }
7572 8641a332 2023-04-14 thomas
7573 8641a332 2023-04-14 thomas static const struct got_error *
7574 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7575 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7576 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7577 8641a332 2023-04-14 thomas {
7578 8641a332 2023-04-14 thomas const struct got_error *err;
7579 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7580 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7581 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7582 8641a332 2023-04-14 thomas
7583 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7584 8641a332 2023-04-14 thomas
7585 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7586 8641a332 2023-04-14 thomas if (err)
7587 8641a332 2023-04-14 thomas goto done;
7588 8641a332 2023-04-14 thomas
7589 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7590 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7591 8641a332 2023-04-14 thomas
7592 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7593 8641a332 2023-04-14 thomas continue;
7594 8641a332 2023-04-14 thomas
7595 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7596 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7597 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7598 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7599 8641a332 2023-04-14 thomas goto done;
7600 8641a332 2023-04-14 thomas }
7601 8641a332 2023-04-14 thomas } else {
7602 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7603 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7604 8641a332 2023-04-14 thomas goto done;
7605 8641a332 2023-04-14 thomas }
7606 8641a332 2023-04-14 thomas
7607 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7608 8641a332 2023-04-14 thomas path_prefix, abspath);
7609 8641a332 2023-04-14 thomas if (err)
7610 8641a332 2023-04-14 thomas goto done;
7611 8641a332 2023-04-14 thomas free(abspath);
7612 8641a332 2023-04-14 thomas abspath = NULL;
7613 8641a332 2023-04-14 thomas }
7614 8641a332 2023-04-14 thomas
7615 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7616 8641a332 2023-04-14 thomas if (err)
7617 8641a332 2023-04-14 thomas goto done;
7618 8641a332 2023-04-14 thomas wt_path = NULL;
7619 8641a332 2023-04-14 thomas }
7620 8641a332 2023-04-14 thomas
7621 8641a332 2023-04-14 thomas done:
7622 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7623 8641a332 2023-04-14 thomas free(abspath);
7624 8641a332 2023-04-14 thomas free(wt_path);
7625 8641a332 2023-04-14 thomas return err;
7626 8641a332 2023-04-14 thomas }
7627 8641a332 2023-04-14 thomas
7628 8641a332 2023-04-14 thomas static const struct got_error *
7629 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7630 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7631 8641a332 2023-04-14 thomas struct got_repository *repo)
7632 8641a332 2023-04-14 thomas {
7633 8641a332 2023-04-14 thomas const struct got_error *err;
7634 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7635 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7636 8641a332 2023-04-14 thomas
7637 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7638 8641a332 2023-04-14 thomas if (err)
7639 8641a332 2023-04-14 thomas goto done;
7640 8641a332 2023-04-14 thomas
7641 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7642 8641a332 2023-04-14 thomas
7643 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7644 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7645 8641a332 2023-04-14 thomas if (err)
7646 8641a332 2023-04-14 thomas goto done;
7647 8641a332 2023-04-14 thomas done:
7648 8641a332 2023-04-14 thomas if (commit)
7649 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7650 8641a332 2023-04-14 thomas return err;
7651 8641a332 2023-04-14 thomas }
7652 8641a332 2023-04-14 thomas
7653 818c7501 2019-07-11 stsp const struct got_error *
7654 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7655 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7656 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7657 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7658 818c7501 2019-07-11 stsp {
7659 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7660 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7661 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7662 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7663 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7664 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7665 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7666 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7667 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7668 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7669 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7670 818c7501 2019-07-11 stsp
7671 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7672 8641a332 2023-04-14 thomas
7673 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7674 818c7501 2019-07-11 stsp if (err)
7675 818c7501 2019-07-11 stsp return err;
7676 8641a332 2023-04-14 thomas
7677 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7678 8641a332 2023-04-14 thomas if (err)
7679 8641a332 2023-04-14 thomas goto done;
7680 8641a332 2023-04-14 thomas
7681 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7682 8641a332 2023-04-14 thomas if (err)
7683 8641a332 2023-04-14 thomas goto done;
7684 8641a332 2023-04-14 thomas
7685 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7686 8641a332 2023-04-14 thomas if (err)
7687 8641a332 2023-04-14 thomas goto done;
7688 945f9229 2022-04-16 thomas
7689 8641a332 2023-04-14 thomas /*
7690 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7691 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7692 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7693 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7694 8641a332 2023-04-14 thomas */
7695 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7696 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7697 8641a332 2023-04-14 thomas if (err)
7698 8641a332 2023-04-14 thomas goto done;
7699 8641a332 2023-04-14 thomas
7700 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7701 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7702 818c7501 2019-07-11 stsp if (err)
7703 818c7501 2019-07-11 stsp goto done;
7704 818c7501 2019-07-11 stsp
7705 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7706 818c7501 2019-07-11 stsp if (err)
7707 818c7501 2019-07-11 stsp goto done;
7708 818c7501 2019-07-11 stsp
7709 818c7501 2019-07-11 stsp /*
7710 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7711 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7712 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7713 818c7501 2019-07-11 stsp */
7714 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7715 818c7501 2019-07-11 stsp if (err)
7716 818c7501 2019-07-11 stsp goto done;
7717 818c7501 2019-07-11 stsp
7718 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7719 25a102ed 2023-04-14 thomas if (err)
7720 25a102ed 2023-04-14 thomas goto done;
7721 25a102ed 2023-04-14 thomas
7722 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7723 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7724 818c7501 2019-07-11 stsp if (err)
7725 818c7501 2019-07-11 stsp goto done;
7726 818c7501 2019-07-11 stsp
7727 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7728 945f9229 2022-04-16 thomas worktree->path_prefix);
7729 ca355955 2019-07-12 stsp if (err)
7730 ca355955 2019-07-12 stsp goto done;
7731 ca355955 2019-07-12 stsp
7732 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7733 ca355955 2019-07-12 stsp if (err)
7734 ca355955 2019-07-12 stsp goto done;
7735 ca355955 2019-07-12 stsp
7736 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7737 818c7501 2019-07-11 stsp if (err)
7738 818c7501 2019-07-11 stsp goto done;
7739 818c7501 2019-07-11 stsp
7740 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7741 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7742 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7743 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7744 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7745 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7746 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7747 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7748 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7749 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7750 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7751 55bd499d 2019-07-12 stsp if (err)
7752 1f1abb7e 2019-08-08 stsp goto sync;
7753 55bd499d 2019-07-12 stsp
7754 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7755 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7756 ca355955 2019-07-12 stsp sync:
7757 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7758 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7759 ca355955 2019-07-12 stsp err = sync_err;
7760 818c7501 2019-07-11 stsp done:
7761 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7762 818c7501 2019-07-11 stsp got_ref_close(resolved);
7763 a3a2faf2 2019-07-12 stsp free(tree_id);
7764 818c7501 2019-07-11 stsp free(commit_id);
7765 8641a332 2023-04-14 thomas free(merged_commit_id);
7766 945f9229 2022-04-16 thomas if (commit)
7767 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7768 0ebf8283 2019-07-24 stsp if (fileindex)
7769 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7770 0ebf8283 2019-07-24 stsp free(fileindex_path);
7771 8641a332 2023-04-14 thomas free(commit_ref_name);
7772 8641a332 2023-04-14 thomas if (commit_ref)
7773 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7774 0ebf8283 2019-07-24 stsp
7775 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7776 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7777 0ebf8283 2019-07-24 stsp err = unlockerr;
7778 0ebf8283 2019-07-24 stsp return err;
7779 0ebf8283 2019-07-24 stsp }
7780 0ebf8283 2019-07-24 stsp
7781 0ebf8283 2019-07-24 stsp const struct got_error *
7782 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7783 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7784 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7785 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7786 0ebf8283 2019-07-24 stsp {
7787 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7788 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7789 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7790 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7791 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7792 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7793 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7794 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7795 0ebf8283 2019-07-24 stsp
7796 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7797 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7798 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7799 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7800 0ebf8283 2019-07-24 stsp
7801 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7802 0ebf8283 2019-07-24 stsp if (err)
7803 0ebf8283 2019-07-24 stsp return err;
7804 0ebf8283 2019-07-24 stsp
7805 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7806 0ebf8283 2019-07-24 stsp if (err)
7807 0ebf8283 2019-07-24 stsp goto done;
7808 0ebf8283 2019-07-24 stsp
7809 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7810 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7811 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7812 0ebf8283 2019-07-24 stsp &ok_arg);
7813 0ebf8283 2019-07-24 stsp if (err)
7814 0ebf8283 2019-07-24 stsp goto done;
7815 0ebf8283 2019-07-24 stsp
7816 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7817 0ebf8283 2019-07-24 stsp if (err)
7818 0ebf8283 2019-07-24 stsp goto done;
7819 0ebf8283 2019-07-24 stsp
7820 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7821 0ebf8283 2019-07-24 stsp if (err)
7822 0ebf8283 2019-07-24 stsp goto done;
7823 0ebf8283 2019-07-24 stsp
7824 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7825 0ebf8283 2019-07-24 stsp worktree);
7826 0ebf8283 2019-07-24 stsp if (err)
7827 0ebf8283 2019-07-24 stsp goto done;
7828 0ebf8283 2019-07-24 stsp
7829 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7830 0ebf8283 2019-07-24 stsp 0);
7831 0ebf8283 2019-07-24 stsp if (err)
7832 0ebf8283 2019-07-24 stsp goto done;
7833 0ebf8283 2019-07-24 stsp
7834 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7835 0ebf8283 2019-07-24 stsp if (err)
7836 0ebf8283 2019-07-24 stsp goto done;
7837 0ebf8283 2019-07-24 stsp
7838 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7839 0ebf8283 2019-07-24 stsp if (err)
7840 0ebf8283 2019-07-24 stsp goto done;
7841 0ebf8283 2019-07-24 stsp
7842 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7843 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7844 0ebf8283 2019-07-24 stsp if (err)
7845 0ebf8283 2019-07-24 stsp goto done;
7846 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7847 0ebf8283 2019-07-24 stsp if (err)
7848 0ebf8283 2019-07-24 stsp goto done;
7849 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7850 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7851 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7852 0ebf8283 2019-07-24 stsp goto done;
7853 0ebf8283 2019-07-24 stsp }
7854 0ebf8283 2019-07-24 stsp
7855 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7856 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7857 0ebf8283 2019-07-24 stsp if (err)
7858 0ebf8283 2019-07-24 stsp goto done;
7859 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7860 0ebf8283 2019-07-24 stsp if (err)
7861 0ebf8283 2019-07-24 stsp goto done;
7862 0ebf8283 2019-07-24 stsp
7863 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7864 0ebf8283 2019-07-24 stsp if (err)
7865 0ebf8283 2019-07-24 stsp goto done;
7866 0ebf8283 2019-07-24 stsp done:
7867 0ebf8283 2019-07-24 stsp free(fileindex_path);
7868 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7869 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7870 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7871 0ebf8283 2019-07-24 stsp if (wt_branch)
7872 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7873 0ebf8283 2019-07-24 stsp if (err) {
7874 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7875 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7876 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7877 0ebf8283 2019-07-24 stsp }
7878 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7879 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7880 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7881 0ebf8283 2019-07-24 stsp }
7882 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7883 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7884 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7885 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7886 3e3a69f1 2019-07-25 stsp }
7887 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7888 0ebf8283 2019-07-24 stsp }
7889 0ebf8283 2019-07-24 stsp return err;
7890 0ebf8283 2019-07-24 stsp }
7891 0ebf8283 2019-07-24 stsp
7892 0ebf8283 2019-07-24 stsp const struct got_error *
7893 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7894 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7895 0ebf8283 2019-07-24 stsp {
7896 3e3a69f1 2019-07-25 stsp if (fileindex)
7897 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7898 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7899 0ebf8283 2019-07-24 stsp }
7900 0ebf8283 2019-07-24 stsp
7901 0ebf8283 2019-07-24 stsp const struct got_error *
7902 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7903 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7904 0ebf8283 2019-07-24 stsp {
7905 0ebf8283 2019-07-24 stsp const struct got_error *err;
7906 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7907 0ebf8283 2019-07-24 stsp
7908 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7909 0ebf8283 2019-07-24 stsp if (err)
7910 0ebf8283 2019-07-24 stsp return err;
7911 0ebf8283 2019-07-24 stsp
7912 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7913 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7914 0ebf8283 2019-07-24 stsp return NULL;
7915 0ebf8283 2019-07-24 stsp }
7916 0ebf8283 2019-07-24 stsp
7917 0ebf8283 2019-07-24 stsp const struct got_error *
7918 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7919 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7920 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7921 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7922 0ebf8283 2019-07-24 stsp {
7923 0ebf8283 2019-07-24 stsp const struct got_error *err;
7924 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7925 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7926 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7927 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7928 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7929 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7930 0ebf8283 2019-07-24 stsp
7931 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7932 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7933 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7934 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7935 0ebf8283 2019-07-24 stsp
7936 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7937 3e3a69f1 2019-07-25 stsp if (err)
7938 3e3a69f1 2019-07-25 stsp return err;
7939 3e3a69f1 2019-07-25 stsp
7940 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7941 3e3a69f1 2019-07-25 stsp if (err)
7942 f032f1f7 2019-08-04 stsp goto done;
7943 f032f1f7 2019-08-04 stsp
7944 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7945 f032f1f7 2019-08-04 stsp &have_staged_files);
7946 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7947 f032f1f7 2019-08-04 stsp goto done;
7948 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7949 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7950 3e3a69f1 2019-07-25 stsp goto done;
7951 f032f1f7 2019-08-04 stsp }
7952 3e3a69f1 2019-07-25 stsp
7953 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7954 0ebf8283 2019-07-24 stsp if (err)
7955 f032f1f7 2019-08-04 stsp goto done;
7956 0ebf8283 2019-07-24 stsp
7957 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7958 0ebf8283 2019-07-24 stsp if (err)
7959 0ebf8283 2019-07-24 stsp goto done;
7960 0ebf8283 2019-07-24 stsp
7961 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7962 0ebf8283 2019-07-24 stsp if (err)
7963 0ebf8283 2019-07-24 stsp goto done;
7964 0ebf8283 2019-07-24 stsp
7965 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7966 0ebf8283 2019-07-24 stsp worktree);
7967 0ebf8283 2019-07-24 stsp if (err)
7968 0ebf8283 2019-07-24 stsp goto done;
7969 0ebf8283 2019-07-24 stsp
7970 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7971 0ebf8283 2019-07-24 stsp if (err)
7972 0ebf8283 2019-07-24 stsp goto done;
7973 0ebf8283 2019-07-24 stsp
7974 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7975 0ebf8283 2019-07-24 stsp if (err)
7976 0ebf8283 2019-07-24 stsp goto done;
7977 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7978 0ebf8283 2019-07-24 stsp if (err)
7979 0ebf8283 2019-07-24 stsp goto done;
7980 0ebf8283 2019-07-24 stsp
7981 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7982 0ebf8283 2019-07-24 stsp if (err)
7983 0ebf8283 2019-07-24 stsp goto done;
7984 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7985 0ebf8283 2019-07-24 stsp if (err)
7986 0ebf8283 2019-07-24 stsp goto done;
7987 0ebf8283 2019-07-24 stsp
7988 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7989 0ebf8283 2019-07-24 stsp if (err)
7990 0ebf8283 2019-07-24 stsp goto done;
7991 0ebf8283 2019-07-24 stsp done:
7992 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7993 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7994 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7995 0ebf8283 2019-07-24 stsp if (commit_ref)
7996 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7997 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7998 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7999 0ebf8283 2019-07-24 stsp if (err) {
8000 0ebf8283 2019-07-24 stsp free(*commit_id);
8001 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8002 0ebf8283 2019-07-24 stsp free(*base_commit_id);
8003 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8004 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
8005 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
8006 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8007 0ebf8283 2019-07-24 stsp }
8008 3e3a69f1 2019-07-25 stsp if (*fileindex) {
8009 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
8010 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8011 3e3a69f1 2019-07-25 stsp }
8012 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
8013 0ebf8283 2019-07-24 stsp }
8014 0ebf8283 2019-07-24 stsp return err;
8015 0ebf8283 2019-07-24 stsp }
8016 0ebf8283 2019-07-24 stsp
8017 0ebf8283 2019-07-24 stsp static const struct got_error *
8018 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8019 0ebf8283 2019-07-24 stsp {
8020 0ebf8283 2019-07-24 stsp const struct got_error *err;
8021 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8022 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
8023 0ebf8283 2019-07-24 stsp
8024 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8025 0ebf8283 2019-07-24 stsp if (err)
8026 0ebf8283 2019-07-24 stsp goto done;
8027 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
8028 0ebf8283 2019-07-24 stsp if (err)
8029 0ebf8283 2019-07-24 stsp goto done;
8030 0ebf8283 2019-07-24 stsp
8031 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8032 0ebf8283 2019-07-24 stsp worktree);
8033 0ebf8283 2019-07-24 stsp if (err)
8034 0ebf8283 2019-07-24 stsp goto done;
8035 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
8036 0ebf8283 2019-07-24 stsp if (err)
8037 0ebf8283 2019-07-24 stsp goto done;
8038 0ebf8283 2019-07-24 stsp
8039 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8040 0ebf8283 2019-07-24 stsp if (err)
8041 0ebf8283 2019-07-24 stsp goto done;
8042 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8043 0ebf8283 2019-07-24 stsp if (err)
8044 0ebf8283 2019-07-24 stsp goto done;
8045 0ebf8283 2019-07-24 stsp
8046 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8047 0ebf8283 2019-07-24 stsp if (err)
8048 0ebf8283 2019-07-24 stsp goto done;
8049 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8050 0ebf8283 2019-07-24 stsp if (err)
8051 0ebf8283 2019-07-24 stsp goto done;
8052 0ebf8283 2019-07-24 stsp done:
8053 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8054 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8055 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8056 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8057 0ebf8283 2019-07-24 stsp return err;
8058 0ebf8283 2019-07-24 stsp }
8059 0ebf8283 2019-07-24 stsp
8060 0ebf8283 2019-07-24 stsp const struct got_error *
8061 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8062 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8063 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8064 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8065 0ebf8283 2019-07-24 stsp {
8066 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8067 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8068 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8069 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8070 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8071 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8072 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8073 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8074 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8075 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8076 0ebf8283 2019-07-24 stsp
8077 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8078 8641a332 2023-04-14 thomas
8079 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8080 0ebf8283 2019-07-24 stsp if (err)
8081 0ebf8283 2019-07-24 stsp return err;
8082 945f9229 2022-04-16 thomas
8083 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8084 8641a332 2023-04-14 thomas if (err)
8085 8641a332 2023-04-14 thomas goto done;
8086 8641a332 2023-04-14 thomas
8087 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8088 8641a332 2023-04-14 thomas if (err) {
8089 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
8090 8641a332 2023-04-14 thomas goto done;
8091 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
8092 8641a332 2023-04-14 thomas commit_ref = NULL;
8093 8641a332 2023-04-14 thomas }
8094 8641a332 2023-04-14 thomas
8095 8641a332 2023-04-14 thomas if (commit_ref) {
8096 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8097 8641a332 2023-04-14 thomas if (err)
8098 8641a332 2023-04-14 thomas goto done;
8099 8641a332 2023-04-14 thomas
8100 8641a332 2023-04-14 thomas /*
8101 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8102 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8103 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
8104 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
8105 8641a332 2023-04-14 thomas */
8106 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8107 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8108 8641a332 2023-04-14 thomas if (err)
8109 8641a332 2023-04-14 thomas goto done;
8110 8641a332 2023-04-14 thomas }
8111 8641a332 2023-04-14 thomas
8112 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8113 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8114 0ebf8283 2019-07-24 stsp if (err)
8115 0ebf8283 2019-07-24 stsp goto done;
8116 0ebf8283 2019-07-24 stsp
8117 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8118 0ebf8283 2019-07-24 stsp if (err)
8119 0ebf8283 2019-07-24 stsp goto done;
8120 0ebf8283 2019-07-24 stsp
8121 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8122 0ebf8283 2019-07-24 stsp if (err)
8123 0ebf8283 2019-07-24 stsp goto done;
8124 0ebf8283 2019-07-24 stsp
8125 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
8126 25a102ed 2023-04-14 thomas worktree->base_commit_id);
8127 25a102ed 2023-04-14 thomas if (err)
8128 25a102ed 2023-04-14 thomas goto done;
8129 25a102ed 2023-04-14 thomas
8130 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8131 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8132 0ebf8283 2019-07-24 stsp if (err)
8133 0ebf8283 2019-07-24 stsp goto done;
8134 0ebf8283 2019-07-24 stsp
8135 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8136 0ebf8283 2019-07-24 stsp if (err)
8137 0ebf8283 2019-07-24 stsp goto done;
8138 0ebf8283 2019-07-24 stsp
8139 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8140 0ebf8283 2019-07-24 stsp if (err)
8141 0ebf8283 2019-07-24 stsp goto done;
8142 0ebf8283 2019-07-24 stsp
8143 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8144 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8145 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8146 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8147 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8148 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8149 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8150 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
8151 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8152 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8153 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8154 0ebf8283 2019-07-24 stsp if (err)
8155 1f1abb7e 2019-08-08 stsp goto sync;
8156 0ebf8283 2019-07-24 stsp
8157 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8158 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8159 0ebf8283 2019-07-24 stsp sync:
8160 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8161 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8162 0ebf8283 2019-07-24 stsp err = sync_err;
8163 0ebf8283 2019-07-24 stsp done:
8164 8641a332 2023-04-14 thomas if (resolved)
8165 8641a332 2023-04-14 thomas got_ref_close(resolved);
8166 8641a332 2023-04-14 thomas if (commit_ref)
8167 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8168 8641a332 2023-04-14 thomas free(merged_commit_id);
8169 0ebf8283 2019-07-24 stsp free(tree_id);
8170 818c7501 2019-07-11 stsp free(fileindex_path);
8171 8641a332 2023-04-14 thomas free(commit_ref_name);
8172 818c7501 2019-07-11 stsp
8173 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8174 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8175 818c7501 2019-07-11 stsp err = unlockerr;
8176 818c7501 2019-07-11 stsp return err;
8177 818c7501 2019-07-11 stsp }
8178 0ebf8283 2019-07-24 stsp
8179 0ebf8283 2019-07-24 stsp const struct got_error *
8180 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8181 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8182 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8183 0ebf8283 2019-07-24 stsp {
8184 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8185 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8186 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8187 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8188 0ebf8283 2019-07-24 stsp
8189 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8190 0ebf8283 2019-07-24 stsp if (err)
8191 0ebf8283 2019-07-24 stsp return err;
8192 0ebf8283 2019-07-24 stsp
8193 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8194 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8195 e600f124 2021-03-21 stsp if (err)
8196 e600f124 2021-03-21 stsp goto done;
8197 e600f124 2021-03-21 stsp
8198 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8199 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8200 0ebf8283 2019-07-24 stsp if (err)
8201 0ebf8283 2019-07-24 stsp goto done;
8202 0ebf8283 2019-07-24 stsp
8203 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8204 0ebf8283 2019-07-24 stsp if (err)
8205 0ebf8283 2019-07-24 stsp goto done;
8206 0ebf8283 2019-07-24 stsp
8207 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8208 0ebf8283 2019-07-24 stsp if (err)
8209 0ebf8283 2019-07-24 stsp goto done;
8210 0ebf8283 2019-07-24 stsp
8211 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8212 0ebf8283 2019-07-24 stsp if (err)
8213 0ebf8283 2019-07-24 stsp goto done;
8214 0ebf8283 2019-07-24 stsp
8215 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8216 a615e0e7 2020-12-16 stsp if (err)
8217 a615e0e7 2020-12-16 stsp goto done;
8218 a615e0e7 2020-12-16 stsp
8219 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8220 a615e0e7 2020-12-16 stsp if (err)
8221 a615e0e7 2020-12-16 stsp goto done;
8222 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8223 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8224 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8225 a615e0e7 2020-12-16 stsp err = sync_err;
8226 0ebf8283 2019-07-24 stsp done:
8227 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8228 a615e0e7 2020-12-16 stsp free(fileindex_path);
8229 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8230 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8231 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8232 0ebf8283 2019-07-24 stsp err = unlockerr;
8233 0ebf8283 2019-07-24 stsp return err;
8234 0ebf8283 2019-07-24 stsp }
8235 0ebf8283 2019-07-24 stsp
8236 0ebf8283 2019-07-24 stsp const struct got_error *
8237 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8238 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8239 0ebf8283 2019-07-24 stsp {
8240 0ebf8283 2019-07-24 stsp const struct got_error *err;
8241 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8242 0ebf8283 2019-07-24 stsp
8243 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8244 0ebf8283 2019-07-24 stsp if (err)
8245 0ebf8283 2019-07-24 stsp return err;
8246 0ebf8283 2019-07-24 stsp
8247 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8248 0ebf8283 2019-07-24 stsp if (err)
8249 0ebf8283 2019-07-24 stsp goto done;
8250 0ebf8283 2019-07-24 stsp
8251 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8252 0ebf8283 2019-07-24 stsp done:
8253 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8254 2822a352 2019-10-15 stsp return err;
8255 2822a352 2019-10-15 stsp }
8256 2822a352 2019-10-15 stsp
8257 2822a352 2019-10-15 stsp const struct got_error *
8258 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8259 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8260 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8261 2822a352 2019-10-15 stsp struct got_repository *repo)
8262 2822a352 2019-10-15 stsp {
8263 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8264 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8265 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8266 2822a352 2019-10-15 stsp
8267 2822a352 2019-10-15 stsp *fileindex = NULL;
8268 2822a352 2019-10-15 stsp *branch_ref = NULL;
8269 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8270 2822a352 2019-10-15 stsp
8271 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8272 2822a352 2019-10-15 stsp if (err)
8273 2822a352 2019-10-15 stsp return err;
8274 2822a352 2019-10-15 stsp
8275 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8276 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8277 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8278 2822a352 2019-10-15 stsp "update -b or different branch name required");
8279 2822a352 2019-10-15 stsp goto done;
8280 2822a352 2019-10-15 stsp }
8281 2822a352 2019-10-15 stsp
8282 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8283 2822a352 2019-10-15 stsp if (err)
8284 2822a352 2019-10-15 stsp goto done;
8285 2822a352 2019-10-15 stsp
8286 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8287 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8288 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8289 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8290 2822a352 2019-10-15 stsp &ok_arg);
8291 2822a352 2019-10-15 stsp if (err)
8292 2822a352 2019-10-15 stsp goto done;
8293 2822a352 2019-10-15 stsp
8294 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8295 2822a352 2019-10-15 stsp if (err)
8296 2822a352 2019-10-15 stsp goto done;
8297 2822a352 2019-10-15 stsp
8298 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8299 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8300 2822a352 2019-10-15 stsp done:
8301 2822a352 2019-10-15 stsp if (err) {
8302 2822a352 2019-10-15 stsp if (*branch_ref) {
8303 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8304 2822a352 2019-10-15 stsp *branch_ref = NULL;
8305 2822a352 2019-10-15 stsp }
8306 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8307 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8308 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8309 2822a352 2019-10-15 stsp }
8310 2822a352 2019-10-15 stsp if (*fileindex) {
8311 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8312 2822a352 2019-10-15 stsp *fileindex = NULL;
8313 2822a352 2019-10-15 stsp }
8314 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8315 2822a352 2019-10-15 stsp }
8316 0cb83759 2019-08-03 stsp return err;
8317 0cb83759 2019-08-03 stsp }
8318 0cb83759 2019-08-03 stsp
8319 2822a352 2019-10-15 stsp const struct got_error *
8320 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8321 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8322 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8323 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8324 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8325 2822a352 2019-10-15 stsp {
8326 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8327 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8328 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8329 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8330 2822a352 2019-10-15 stsp
8331 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8332 2822a352 2019-10-15 stsp if (err)
8333 2822a352 2019-10-15 stsp goto done;
8334 2822a352 2019-10-15 stsp
8335 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8336 2822a352 2019-10-15 stsp if (err)
8337 2822a352 2019-10-15 stsp goto done;
8338 2822a352 2019-10-15 stsp
8339 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8340 945f9229 2022-04-16 thomas if (err)
8341 945f9229 2022-04-16 thomas goto done;
8342 945f9229 2022-04-16 thomas
8343 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8344 2822a352 2019-10-15 stsp worktree->path_prefix);
8345 2822a352 2019-10-15 stsp if (err)
8346 2822a352 2019-10-15 stsp goto done;
8347 2822a352 2019-10-15 stsp
8348 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8349 2822a352 2019-10-15 stsp if (err)
8350 2822a352 2019-10-15 stsp goto done;
8351 2822a352 2019-10-15 stsp
8352 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8353 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8354 2822a352 2019-10-15 stsp if (err)
8355 2822a352 2019-10-15 stsp goto sync;
8356 2822a352 2019-10-15 stsp
8357 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8358 2822a352 2019-10-15 stsp if (err)
8359 2822a352 2019-10-15 stsp goto sync;
8360 2822a352 2019-10-15 stsp
8361 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8362 6e210706 2021-01-22 stsp if (err)
8363 6e210706 2021-01-22 stsp goto sync;
8364 6e210706 2021-01-22 stsp
8365 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8366 2822a352 2019-10-15 stsp sync:
8367 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8368 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8369 2822a352 2019-10-15 stsp err = sync_err;
8370 2822a352 2019-10-15 stsp
8371 2822a352 2019-10-15 stsp done:
8372 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8373 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8374 2822a352 2019-10-15 stsp err = unlockerr;
8375 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8376 2822a352 2019-10-15 stsp
8377 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8378 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8379 2822a352 2019-10-15 stsp err = unlockerr;
8380 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8381 2822a352 2019-10-15 stsp
8382 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8383 2822a352 2019-10-15 stsp free(fileindex_path);
8384 2822a352 2019-10-15 stsp free(tree_id);
8385 945f9229 2022-04-16 thomas if (commit)
8386 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8387 2822a352 2019-10-15 stsp
8388 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8389 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8390 2822a352 2019-10-15 stsp err = unlockerr;
8391 2822a352 2019-10-15 stsp return err;
8392 2822a352 2019-10-15 stsp }
8393 2822a352 2019-10-15 stsp
8394 2822a352 2019-10-15 stsp const struct got_error *
8395 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8396 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8397 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8398 2822a352 2019-10-15 stsp {
8399 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8400 8b692cd0 2019-10-21 stsp
8401 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8402 8b692cd0 2019-10-21 stsp
8403 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8404 8b692cd0 2019-10-21 stsp
8405 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8406 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8407 8b692cd0 2019-10-21 stsp err = unlockerr;
8408 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8409 8b692cd0 2019-10-21 stsp
8410 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8411 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8412 8b692cd0 2019-10-21 stsp err = unlockerr;
8413 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8414 10604dce 2021-09-24 thomas
8415 10604dce 2021-09-24 thomas return err;
8416 10604dce 2021-09-24 thomas }
8417 10604dce 2021-09-24 thomas
8418 10604dce 2021-09-24 thomas const struct got_error *
8419 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8420 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8421 10604dce 2021-09-24 thomas {
8422 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8423 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8424 10604dce 2021-09-24 thomas
8425 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8426 10604dce 2021-09-24 thomas if (err)
8427 10604dce 2021-09-24 thomas goto done;
8428 8b692cd0 2019-10-21 stsp
8429 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8430 10604dce 2021-09-24 thomas
8431 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8432 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8433 10604dce 2021-09-24 thomas err = sync_err;
8434 10604dce 2021-09-24 thomas done:
8435 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8436 10604dce 2021-09-24 thomas free(fileindex_path);
8437 8b692cd0 2019-10-21 stsp return err;
8438 2822a352 2019-10-15 stsp }
8439 2822a352 2019-10-15 stsp
8440 10604dce 2021-09-24 thomas static const struct got_error *
8441 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8442 10604dce 2021-09-24 thomas {
8443 10604dce 2021-09-24 thomas const struct got_error *err;
8444 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8445 10604dce 2021-09-24 thomas
8446 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8447 10604dce 2021-09-24 thomas if (err)
8448 10604dce 2021-09-24 thomas goto done;
8449 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
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 = get_merge_commit_ref_name(&commit_refname, worktree);
8454 10604dce 2021-09-24 thomas if (err)
8455 10604dce 2021-09-24 thomas goto done;
8456 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8457 10604dce 2021-09-24 thomas if (err)
8458 10604dce 2021-09-24 thomas goto done;
8459 10604dce 2021-09-24 thomas
8460 10604dce 2021-09-24 thomas done:
8461 10604dce 2021-09-24 thomas free(branch_refname);
8462 10604dce 2021-09-24 thomas free(commit_refname);
8463 10604dce 2021-09-24 thomas return err;
8464 10604dce 2021-09-24 thomas }
8465 10604dce 2021-09-24 thomas
8466 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8467 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8468 10604dce 2021-09-24 thomas const char *branch_name;
8469 10604dce 2021-09-24 thomas };
8470 10604dce 2021-09-24 thomas
8471 10604dce 2021-09-24 thomas static const struct got_error *
8472 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8473 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8474 10604dce 2021-09-24 thomas {
8475 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8476 10604dce 2021-09-24 thomas
8477 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8478 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8479 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8480 10604dce 2021-09-24 thomas
8481 10604dce 2021-09-24 thomas return NULL;
8482 10604dce 2021-09-24 thomas }
8483 10604dce 2021-09-24 thomas
8484 10604dce 2021-09-24 thomas
8485 10604dce 2021-09-24 thomas const struct got_error *
8486 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8487 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8488 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8489 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8490 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8491 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8492 10604dce 2021-09-24 thomas {
8493 10604dce 2021-09-24 thomas const struct got_error *err;
8494 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8495 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
8496 10604dce 2021-09-24 thomas
8497 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8498 10604dce 2021-09-24 thomas if (err)
8499 10604dce 2021-09-24 thomas goto done;
8500 10604dce 2021-09-24 thomas
8501 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
8502 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
8503 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
8504 6ebb2263 2023-07-26 thomas
8505 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8506 d1c85b42 2023-07-26 thomas &cma);
8507 10604dce 2021-09-24 thomas if (err)
8508 10604dce 2021-09-24 thomas goto done;
8509 10604dce 2021-09-24 thomas
8510 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8511 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8512 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8513 10604dce 2021-09-24 thomas done:
8514 10604dce 2021-09-24 thomas free(fileindex_path);
8515 10604dce 2021-09-24 thomas return err;
8516 10604dce 2021-09-24 thomas }
8517 10604dce 2021-09-24 thomas
8518 10604dce 2021-09-24 thomas const struct got_error *
8519 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8520 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8521 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8522 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8523 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8524 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8525 ae1e948a 2021-09-28 thomas
8526 10604dce 2021-09-24 thomas {
8527 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8528 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8529 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8530 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8531 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8532 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8533 10604dce 2021-09-24 thomas int have_staged_files = 0;
8534 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8535 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8536 10604dce 2021-09-24 thomas
8537 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8538 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8539 10604dce 2021-09-24 thomas
8540 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8541 10604dce 2021-09-24 thomas
8542 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8543 10604dce 2021-09-24 thomas if (err)
8544 10604dce 2021-09-24 thomas goto done;
8545 10604dce 2021-09-24 thomas
8546 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8547 10604dce 2021-09-24 thomas if (err)
8548 10604dce 2021-09-24 thomas goto done;
8549 10604dce 2021-09-24 thomas
8550 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8551 10604dce 2021-09-24 thomas if (err)
8552 10604dce 2021-09-24 thomas goto done;
8553 10604dce 2021-09-24 thomas
8554 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8555 10604dce 2021-09-24 thomas &have_staged_files);
8556 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8557 10604dce 2021-09-24 thomas goto done;
8558 10604dce 2021-09-24 thomas if (have_staged_files) {
8559 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8560 10604dce 2021-09-24 thomas goto done;
8561 10604dce 2021-09-24 thomas }
8562 10604dce 2021-09-24 thomas
8563 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8564 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8565 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8566 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8567 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8568 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8569 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8570 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8571 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8572 10604dce 2021-09-24 thomas if (err)
8573 10604dce 2021-09-24 thomas goto done;
8574 10604dce 2021-09-24 thomas
8575 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8576 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8577 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8578 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8579 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8580 10604dce 2021-09-24 thomas if (err)
8581 10604dce 2021-09-24 thomas goto done;
8582 10604dce 2021-09-24 thomas
8583 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8584 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8585 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8586 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8587 10604dce 2021-09-24 thomas err = sync_err;
8588 10604dce 2021-09-24 thomas done:
8589 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8590 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8591 21c2d8be 2023-01-10 thomas
8592 10604dce 2021-09-24 thomas free_commitable(ct);
8593 10604dce 2021-09-24 thomas }
8594 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8595 10604dce 2021-09-24 thomas free(fileindex_path);
8596 10604dce 2021-09-24 thomas return err;
8597 10604dce 2021-09-24 thomas }
8598 10604dce 2021-09-24 thomas
8599 10604dce 2021-09-24 thomas const struct got_error *
8600 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8601 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8602 10604dce 2021-09-24 thomas {
8603 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8604 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8605 10604dce 2021-09-24 thomas
8606 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8607 10604dce 2021-09-24 thomas if (err)
8608 10604dce 2021-09-24 thomas goto done;
8609 10604dce 2021-09-24 thomas
8610 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8611 10604dce 2021-09-24 thomas if (err)
8612 10604dce 2021-09-24 thomas goto done;
8613 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8614 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8615 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8616 10604dce 2021-09-24 thomas err = sync_err;
8617 10604dce 2021-09-24 thomas done:
8618 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8619 10604dce 2021-09-24 thomas free(fileindex_path);
8620 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8621 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8622 10604dce 2021-09-24 thomas err = unlockerr;
8623 10604dce 2021-09-24 thomas return err;
8624 10604dce 2021-09-24 thomas }
8625 10604dce 2021-09-24 thomas
8626 10604dce 2021-09-24 thomas const struct got_error *
8627 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8628 10604dce 2021-09-24 thomas struct got_repository *repo)
8629 10604dce 2021-09-24 thomas {
8630 10604dce 2021-09-24 thomas const struct got_error *err;
8631 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8632 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8633 10604dce 2021-09-24 thomas
8634 10604dce 2021-09-24 thomas *in_progress = 0;
8635 10604dce 2021-09-24 thomas
8636 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8637 10604dce 2021-09-24 thomas if (err)
8638 10604dce 2021-09-24 thomas return err;
8639 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8640 dfe86d1f 2021-09-24 thomas free(branch_refname);
8641 10604dce 2021-09-24 thomas if (err) {
8642 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8643 10604dce 2021-09-24 thomas return err;
8644 10604dce 2021-09-24 thomas } else
8645 10604dce 2021-09-24 thomas *in_progress = 1;
8646 10604dce 2021-09-24 thomas
8647 10604dce 2021-09-24 thomas return NULL;
8648 10604dce 2021-09-24 thomas }
8649 10604dce 2021-09-24 thomas
8650 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8651 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8652 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8653 10604dce 2021-09-24 thomas {
8654 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8655 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8656 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8657 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8658 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8659 10604dce 2021-09-24 thomas
8660 10604dce 2021-09-24 thomas *fileindex = NULL;
8661 10604dce 2021-09-24 thomas
8662 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8663 10604dce 2021-09-24 thomas if (err)
8664 10604dce 2021-09-24 thomas return err;
8665 10604dce 2021-09-24 thomas
8666 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8667 10604dce 2021-09-24 thomas if (err)
8668 10604dce 2021-09-24 thomas goto done;
8669 10604dce 2021-09-24 thomas
8670 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8671 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8672 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8673 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8674 10604dce 2021-09-24 thomas &ok_arg);
8675 10604dce 2021-09-24 thomas if (err)
8676 10604dce 2021-09-24 thomas goto done;
8677 10604dce 2021-09-24 thomas
8678 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8679 10604dce 2021-09-24 thomas 0);
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_ref_resolve(&wt_branch_tip, repo, wt_branch);
8684 10604dce 2021-09-24 thomas if (err)
8685 10604dce 2021-09-24 thomas goto done;
8686 10604dce 2021-09-24 thomas
8687 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8688 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8689 10604dce 2021-09-24 thomas goto done;
8690 10604dce 2021-09-24 thomas }
8691 2b72f32d 2023-06-22 thomas
8692 2b72f32d 2023-06-22 thomas done:
8693 2b72f32d 2023-06-22 thomas free(fileindex_path);
8694 2b72f32d 2023-06-22 thomas if (wt_branch)
8695 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8696 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8697 2b72f32d 2023-06-22 thomas if (err) {
8698 2b72f32d 2023-06-22 thomas if (*fileindex) {
8699 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8700 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8701 2b72f32d 2023-06-22 thomas }
8702 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8703 2b72f32d 2023-06-22 thomas }
8704 2b72f32d 2023-06-22 thomas return err;
8705 2b72f32d 2023-06-22 thomas }
8706 10604dce 2021-09-24 thomas
8707 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8708 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8709 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8710 2b72f32d 2023-06-22 thomas {
8711 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8712 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8713 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8714 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8715 2b72f32d 2023-06-22 thomas
8716 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8717 2b72f32d 2023-06-22 thomas if (err)
8718 2b72f32d 2023-06-22 thomas return err;
8719 2b72f32d 2023-06-22 thomas
8720 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8721 2b72f32d 2023-06-22 thomas if (err)
8722 2b72f32d 2023-06-22 thomas return err;
8723 2b72f32d 2023-06-22 thomas
8724 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8725 10604dce 2021-09-24 thomas if (err)
8726 10604dce 2021-09-24 thomas goto done;
8727 10604dce 2021-09-24 thomas
8728 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8729 10604dce 2021-09-24 thomas if (err)
8730 10604dce 2021-09-24 thomas goto done;
8731 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8732 10604dce 2021-09-24 thomas if (err)
8733 10604dce 2021-09-24 thomas goto done;
8734 10604dce 2021-09-24 thomas
8735 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8736 10604dce 2021-09-24 thomas if (err)
8737 10604dce 2021-09-24 thomas goto done;
8738 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8739 10604dce 2021-09-24 thomas if (err)
8740 10604dce 2021-09-24 thomas goto done;
8741 10604dce 2021-09-24 thomas
8742 10604dce 2021-09-24 thomas done:
8743 10604dce 2021-09-24 thomas free(branch_refname);
8744 10604dce 2021-09-24 thomas free(commit_refname);
8745 10604dce 2021-09-24 thomas if (branch_ref)
8746 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8747 10604dce 2021-09-24 thomas if (commit_ref)
8748 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8749 2b72f32d 2023-06-22 thomas free(branch_tip);
8750 10604dce 2021-09-24 thomas return err;
8751 10604dce 2021-09-24 thomas }
8752 10604dce 2021-09-24 thomas
8753 10604dce 2021-09-24 thomas const struct got_error *
8754 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8755 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8756 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8757 10604dce 2021-09-24 thomas {
8758 10604dce 2021-09-24 thomas const struct got_error *err;
8759 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8760 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8761 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8762 10604dce 2021-09-24 thomas int have_staged_files = 0;
8763 10604dce 2021-09-24 thomas
8764 10604dce 2021-09-24 thomas *branch_name = NULL;
8765 10604dce 2021-09-24 thomas *branch_tip = NULL;
8766 10604dce 2021-09-24 thomas *fileindex = NULL;
8767 10604dce 2021-09-24 thomas
8768 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8769 10604dce 2021-09-24 thomas if (err)
8770 10604dce 2021-09-24 thomas return err;
8771 10604dce 2021-09-24 thomas
8772 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8773 10604dce 2021-09-24 thomas if (err)
8774 10604dce 2021-09-24 thomas goto done;
8775 10604dce 2021-09-24 thomas
8776 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8777 10604dce 2021-09-24 thomas &have_staged_files);
8778 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8779 10604dce 2021-09-24 thomas goto done;
8780 10604dce 2021-09-24 thomas if (have_staged_files) {
8781 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8782 10604dce 2021-09-24 thomas goto done;
8783 10604dce 2021-09-24 thomas }
8784 10604dce 2021-09-24 thomas
8785 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8786 10604dce 2021-09-24 thomas if (err)
8787 10604dce 2021-09-24 thomas goto done;
8788 10604dce 2021-09-24 thomas
8789 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8790 10604dce 2021-09-24 thomas if (err)
8791 10604dce 2021-09-24 thomas goto done;
8792 10604dce 2021-09-24 thomas
8793 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8794 10604dce 2021-09-24 thomas if (err)
8795 10604dce 2021-09-24 thomas goto done;
8796 10604dce 2021-09-24 thomas
8797 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8798 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8799 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8800 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8801 10604dce 2021-09-24 thomas goto done;
8802 10604dce 2021-09-24 thomas }
8803 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8804 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8805 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8806 10604dce 2021-09-24 thomas goto done;
8807 10604dce 2021-09-24 thomas }
8808 10604dce 2021-09-24 thomas
8809 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8810 10604dce 2021-09-24 thomas if (err)
8811 10604dce 2021-09-24 thomas goto done;
8812 10604dce 2021-09-24 thomas
8813 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8814 10604dce 2021-09-24 thomas if (err)
8815 10604dce 2021-09-24 thomas goto done;
8816 10604dce 2021-09-24 thomas done:
8817 10604dce 2021-09-24 thomas free(commit_refname);
8818 10604dce 2021-09-24 thomas free(branch_refname);
8819 10604dce 2021-09-24 thomas free(fileindex_path);
8820 10604dce 2021-09-24 thomas if (commit_ref)
8821 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8822 10604dce 2021-09-24 thomas if (branch_ref)
8823 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8824 10604dce 2021-09-24 thomas if (err) {
8825 10604dce 2021-09-24 thomas if (*branch_name) {
8826 10604dce 2021-09-24 thomas free(*branch_name);
8827 10604dce 2021-09-24 thomas *branch_name = NULL;
8828 10604dce 2021-09-24 thomas }
8829 10604dce 2021-09-24 thomas free(*branch_tip);
8830 10604dce 2021-09-24 thomas *branch_tip = NULL;
8831 10604dce 2021-09-24 thomas if (*fileindex) {
8832 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8833 10604dce 2021-09-24 thomas *fileindex = NULL;
8834 10604dce 2021-09-24 thomas }
8835 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8836 10604dce 2021-09-24 thomas }
8837 10604dce 2021-09-24 thomas return err;
8838 10604dce 2021-09-24 thomas }
8839 10604dce 2021-09-24 thomas
8840 10604dce 2021-09-24 thomas const struct got_error *
8841 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8842 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8843 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8844 10604dce 2021-09-24 thomas {
8845 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8846 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8847 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8848 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8849 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8850 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8851 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8852 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8853 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8854 8641a332 2023-04-14 thomas
8855 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8856 8641a332 2023-04-14 thomas
8857 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8858 8641a332 2023-04-14 thomas if (err)
8859 8641a332 2023-04-14 thomas goto done;
8860 8641a332 2023-04-14 thomas
8861 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8862 8641a332 2023-04-14 thomas if (err)
8863 8641a332 2023-04-14 thomas goto done;
8864 8641a332 2023-04-14 thomas
8865 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8866 8641a332 2023-04-14 thomas if (err)
8867 8641a332 2023-04-14 thomas goto done;
8868 8641a332 2023-04-14 thomas
8869 8641a332 2023-04-14 thomas /*
8870 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8871 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8872 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8873 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8874 8641a332 2023-04-14 thomas */
8875 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8876 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8877 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8878 8641a332 2023-04-14 thomas if (err)
8879 8641a332 2023-04-14 thomas goto done;
8880 10604dce 2021-09-24 thomas
8881 8641a332 2023-04-14 thomas
8882 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8883 945f9229 2022-04-16 thomas worktree->base_commit_id);
8884 945f9229 2022-04-16 thomas if (err)
8885 945f9229 2022-04-16 thomas goto done;
8886 945f9229 2022-04-16 thomas
8887 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8888 945f9229 2022-04-16 thomas worktree->path_prefix);
8889 10604dce 2021-09-24 thomas if (err)
8890 10604dce 2021-09-24 thomas goto done;
8891 10604dce 2021-09-24 thomas
8892 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8893 10604dce 2021-09-24 thomas if (err)
8894 10604dce 2021-09-24 thomas goto done;
8895 10604dce 2021-09-24 thomas
8896 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8897 10604dce 2021-09-24 thomas if (err)
8898 10604dce 2021-09-24 thomas goto done;
8899 10604dce 2021-09-24 thomas
8900 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8901 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8902 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8903 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8904 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8905 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8906 10604dce 2021-09-24 thomas rfa.repo = repo;
8907 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8908 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8909 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8910 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8911 10604dce 2021-09-24 thomas if (err)
8912 10604dce 2021-09-24 thomas goto sync;
8913 10604dce 2021-09-24 thomas
8914 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8915 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8916 10604dce 2021-09-24 thomas sync:
8917 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8918 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8919 10604dce 2021-09-24 thomas err = sync_err;
8920 10604dce 2021-09-24 thomas done:
8921 10604dce 2021-09-24 thomas free(tree_id);
8922 8641a332 2023-04-14 thomas free(merged_commit_id);
8923 945f9229 2022-04-16 thomas if (commit)
8924 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8925 10604dce 2021-09-24 thomas if (fileindex)
8926 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8927 10604dce 2021-09-24 thomas free(fileindex_path);
8928 8641a332 2023-04-14 thomas if (commit_ref)
8929 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8930 8641a332 2023-04-14 thomas free(commit_ref_name);
8931 10604dce 2021-09-24 thomas
8932 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8933 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8934 10604dce 2021-09-24 thomas err = unlockerr;
8935 10604dce 2021-09-24 thomas return err;
8936 10604dce 2021-09-24 thomas }
8937 10604dce 2021-09-24 thomas
8938 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8939 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8940 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8941 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8942 2db2652d 2019-08-07 stsp struct got_repository *repo;
8943 2db2652d 2019-08-07 stsp int have_changes;
8944 2db2652d 2019-08-07 stsp };
8945 2db2652d 2019-08-07 stsp
8946 ef20f542 2022-06-26 thomas static const struct got_error *
8947 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8948 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8949 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8950 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8951 735ef5ac 2019-08-03 stsp {
8952 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8953 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8954 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8955 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8956 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8957 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8958 8b13ce36 2019-08-08 stsp
8959 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8960 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8961 8b13ce36 2019-08-08 stsp return NULL;
8962 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8963 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8964 735ef5ac 2019-08-03 stsp
8965 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8966 735ef5ac 2019-08-03 stsp if (ie == NULL)
8967 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8968 735ef5ac 2019-08-03 stsp
8969 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8970 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8971 735ef5ac 2019-08-03 stsp relpath) == -1)
8972 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8973 735ef5ac 2019-08-03 stsp
8974 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8975 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8976 43010591 2023-02-17 thomas &base_commit_id, ie);
8977 735ef5ac 2019-08-03 stsp }
8978 735ef5ac 2019-08-03 stsp
8979 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8980 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8981 3aa5969e 2019-08-06 stsp goto done;
8982 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8983 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8984 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8985 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8986 735ef5ac 2019-08-03 stsp goto done;
8987 3aa5969e 2019-08-06 stsp }
8988 735ef5ac 2019-08-03 stsp
8989 2db2652d 2019-08-07 stsp a->have_changes = 1;
8990 2db2652d 2019-08-07 stsp
8991 735ef5ac 2019-08-03 stsp p = in_repo_path;
8992 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8993 735ef5ac 2019-08-03 stsp p++;
8994 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8995 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8996 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8997 735ef5ac 2019-08-03 stsp done:
8998 735ef5ac 2019-08-03 stsp free(in_repo_path);
8999 dc424a06 2019-08-07 stsp return err;
9000 dc424a06 2019-08-07 stsp }
9001 dc424a06 2019-08-07 stsp
9002 2db2652d 2019-08-07 stsp struct stage_path_arg {
9003 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9004 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9005 2db2652d 2019-08-07 stsp struct got_repository *repo;
9006 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
9007 2db2652d 2019-08-07 stsp void *status_arg;
9008 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
9009 2db2652d 2019-08-07 stsp void *patch_arg;
9010 7b5dc508 2019-10-28 stsp int staged_something;
9011 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
9012 2db2652d 2019-08-07 stsp };
9013 2db2652d 2019-08-07 stsp
9014 2db2652d 2019-08-07 stsp static const struct got_error *
9015 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
9016 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9017 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9018 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9019 0cb83759 2019-08-03 stsp {
9020 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
9021 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
9022 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
9023 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
9024 0cb83759 2019-08-03 stsp uint32_t stage;
9025 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
9026 0aeb8099 2020-07-23 stsp struct stat sb;
9027 8b13ce36 2019-08-08 stsp
9028 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
9029 8b13ce36 2019-08-08 stsp return NULL;
9030 0cb83759 2019-08-03 stsp
9031 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9032 d3e7c587 2019-08-03 stsp if (ie == NULL)
9033 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9034 0cb83759 2019-08-03 stsp
9035 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9036 2db2652d 2019-08-07 stsp relpath)== -1)
9037 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
9038 0cb83759 2019-08-03 stsp
9039 0cb83759 2019-08-03 stsp switch (status) {
9040 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9041 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9042 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9043 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9044 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9045 0aeb8099 2020-07-23 stsp break;
9046 0aeb8099 2020-07-23 stsp }
9047 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9048 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9049 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9050 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9051 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9052 dc424a06 2019-08-07 stsp if (err)
9053 dc424a06 2019-08-07 stsp break;
9054 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9055 dc424a06 2019-08-07 stsp break;
9056 dc424a06 2019-08-07 stsp } else {
9057 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9058 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9059 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9060 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9061 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9062 dc424a06 2019-08-07 stsp break;
9063 dc424a06 2019-08-07 stsp }
9064 dc424a06 2019-08-07 stsp }
9065 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9066 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9067 0cb83759 2019-08-03 stsp if (err)
9068 dc424a06 2019-08-07 stsp break;
9069 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9070 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9071 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9072 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9073 0cb83759 2019-08-03 stsp else
9074 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9075 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9076 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9077 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9078 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9079 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9080 35213c7c 2020-07-23 stsp ssize_t target_len;
9081 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9082 35213c7c 2020-07-23 stsp sizeof(target_path));
9083 35213c7c 2020-07-23 stsp if (target_len == -1) {
9084 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9085 35213c7c 2020-07-23 stsp ondisk_path);
9086 35213c7c 2020-07-23 stsp break;
9087 35213c7c 2020-07-23 stsp }
9088 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9089 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9090 ad10f64e 2023-07-19 thomas a->worktree->root_path,
9091 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
9092 35213c7c 2020-07-23 stsp if (err)
9093 35213c7c 2020-07-23 stsp break;
9094 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9095 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9096 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9097 35213c7c 2020-07-23 stsp break;
9098 35213c7c 2020-07-23 stsp }
9099 35213c7c 2020-07-23 stsp }
9100 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9101 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9102 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9103 35213c7c 2020-07-23 stsp else
9104 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9105 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9106 0aeb8099 2020-07-23 stsp } else {
9107 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9108 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9109 0aeb8099 2020-07-23 stsp }
9110 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9111 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9112 dc424a06 2019-08-07 stsp break;
9113 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9114 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9115 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9116 f289c82c 2022-06-13 thomas if (err)
9117 f289c82c 2022-06-13 thomas break;
9118 f289c82c 2022-06-13 thomas /*
9119 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
9120 f289c82c 2022-06-13 thomas * implicitly unstage the file.
9121 f289c82c 2022-06-13 thomas */
9122 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9123 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
9124 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
9125 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
9126 f289c82c 2022-06-13 thomas }
9127 0cb83759 2019-08-03 stsp break;
9128 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9129 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9130 d3e7c587 2019-08-03 stsp break;
9131 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9132 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9133 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9134 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9135 dc424a06 2019-08-07 stsp if (err)
9136 dc424a06 2019-08-07 stsp break;
9137 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9138 88f33a19 2019-08-08 stsp break;
9139 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9140 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9141 dc424a06 2019-08-07 stsp break;
9142 88f33a19 2019-08-08 stsp }
9143 dc424a06 2019-08-07 stsp }
9144 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9145 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9146 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9147 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9148 dc424a06 2019-08-07 stsp break;
9149 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9150 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9151 12463d8b 2019-12-13 stsp de_name);
9152 0cb83759 2019-08-03 stsp break;
9153 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9154 d3e7c587 2019-08-03 stsp break;
9155 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9156 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9157 ebf48fd5 2019-08-03 stsp break;
9158 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9159 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9160 2a06fe5f 2019-08-24 stsp break;
9161 0cb83759 2019-08-03 stsp default:
9162 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9163 537ac44b 2019-08-03 stsp break;
9164 0cb83759 2019-08-03 stsp }
9165 dc424a06 2019-08-07 stsp
9166 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9167 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9168 dc424a06 2019-08-07 stsp free(path_content);
9169 2db2652d 2019-08-07 stsp free(ondisk_path);
9170 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9171 0ebf8283 2019-07-24 stsp return err;
9172 0ebf8283 2019-07-24 stsp }
9173 0cb83759 2019-08-03 stsp
9174 0cb83759 2019-08-03 stsp const struct got_error *
9175 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9176 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9177 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9178 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9179 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9180 0cb83759 2019-08-03 stsp {
9181 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9182 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9183 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9184 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9185 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9186 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9187 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9188 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9189 0cb83759 2019-08-03 stsp
9190 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9191 0cb83759 2019-08-03 stsp if (err)
9192 0cb83759 2019-08-03 stsp return err;
9193 0cb83759 2019-08-03 stsp
9194 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9195 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9196 735ef5ac 2019-08-03 stsp if (err)
9197 735ef5ac 2019-08-03 stsp goto done;
9198 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9199 735ef5ac 2019-08-03 stsp if (err)
9200 735ef5ac 2019-08-03 stsp goto done;
9201 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9202 0cb83759 2019-08-03 stsp if (err)
9203 0cb83759 2019-08-03 stsp goto done;
9204 0cb83759 2019-08-03 stsp
9205 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9206 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9207 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9208 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9209 2db2652d 2019-08-07 stsp oka.repo = repo;
9210 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9211 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9212 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9213 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9214 735ef5ac 2019-08-03 stsp if (err)
9215 735ef5ac 2019-08-03 stsp goto done;
9216 735ef5ac 2019-08-03 stsp }
9217 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9218 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9219 2db2652d 2019-08-07 stsp goto done;
9220 2db2652d 2019-08-07 stsp }
9221 735ef5ac 2019-08-03 stsp
9222 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9223 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9224 2db2652d 2019-08-07 stsp spa.repo = repo;
9225 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9226 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9227 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9228 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9229 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9230 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9231 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9232 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9233 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9234 42005733 2019-08-03 stsp if (err)
9235 2db2652d 2019-08-07 stsp goto done;
9236 7b5dc508 2019-10-28 stsp }
9237 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9238 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9239 7b5dc508 2019-10-28 stsp goto done;
9240 0cb83759 2019-08-03 stsp }
9241 0cb83759 2019-08-03 stsp
9242 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9243 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9244 0cb83759 2019-08-03 stsp err = sync_err;
9245 0cb83759 2019-08-03 stsp done:
9246 735ef5ac 2019-08-03 stsp if (head_ref)
9247 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9248 735ef5ac 2019-08-03 stsp free(head_commit_id);
9249 0cb83759 2019-08-03 stsp free(fileindex_path);
9250 0cb83759 2019-08-03 stsp if (fileindex)
9251 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9252 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9253 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9254 0cb83759 2019-08-03 stsp err = unlockerr;
9255 0cb83759 2019-08-03 stsp return err;
9256 0cb83759 2019-08-03 stsp }
9257 ad493afc 2019-08-03 stsp
9258 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9259 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9260 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9261 ad493afc 2019-08-03 stsp struct got_repository *repo;
9262 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9263 ad493afc 2019-08-03 stsp void *progress_arg;
9264 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9265 2e1f37b0 2019-08-08 stsp void *patch_arg;
9266 ad493afc 2019-08-03 stsp };
9267 2e1f37b0 2019-08-08 stsp
9268 2e1f37b0 2019-08-08 stsp static const struct got_error *
9269 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9270 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9271 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9272 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9273 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9274 2e1f37b0 2019-08-08 stsp {
9275 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9276 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9277 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9278 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9279 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9280 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9281 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9282 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9283 2e1f37b0 2019-08-08 stsp
9284 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9285 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9286 2e1f37b0 2019-08-08 stsp
9287 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9288 2e1f37b0 2019-08-08 stsp if (err)
9289 2e1f37b0 2019-08-08 stsp return err;
9290 f4ae6ddb 2022-07-01 thomas
9291 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9292 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9293 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9294 f4ae6ddb 2022-07-01 thomas goto done;
9295 f4ae6ddb 2022-07-01 thomas }
9296 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9297 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9298 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9299 f4ae6ddb 2022-07-01 thomas goto done;
9300 f4ae6ddb 2022-07-01 thomas }
9301 f4ae6ddb 2022-07-01 thomas
9302 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9303 2e1f37b0 2019-08-08 stsp if (err)
9304 2e1f37b0 2019-08-08 stsp goto done;
9305 2e1f37b0 2019-08-08 stsp
9306 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9307 2e1f37b0 2019-08-08 stsp if (err)
9308 2e1f37b0 2019-08-08 stsp goto done;
9309 2e1f37b0 2019-08-08 stsp
9310 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9311 2e1f37b0 2019-08-08 stsp if (err)
9312 2e1f37b0 2019-08-08 stsp goto done;
9313 2e1f37b0 2019-08-08 stsp
9314 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9315 f4ae6ddb 2022-07-01 thomas fd2);
9316 2e1f37b0 2019-08-08 stsp if (err)
9317 2e1f37b0 2019-08-08 stsp goto done;
9318 2e1f37b0 2019-08-08 stsp
9319 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9320 2e1f37b0 2019-08-08 stsp if (err)
9321 2e1f37b0 2019-08-08 stsp goto done;
9322 ad493afc 2019-08-03 stsp
9323 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9324 2e1f37b0 2019-08-08 stsp if (err)
9325 2e1f37b0 2019-08-08 stsp goto done;
9326 2e1f37b0 2019-08-08 stsp
9327 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9328 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9329 2e1f37b0 2019-08-08 stsp if (err)
9330 2e1f37b0 2019-08-08 stsp goto done;
9331 2e1f37b0 2019-08-08 stsp
9332 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9333 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9334 2e1f37b0 2019-08-08 stsp if (err)
9335 2e1f37b0 2019-08-08 stsp goto done;
9336 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9337 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9338 2e1f37b0 2019-08-08 stsp if (err)
9339 2e1f37b0 2019-08-08 stsp goto done;
9340 2e1f37b0 2019-08-08 stsp
9341 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9342 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9343 2e1f37b0 2019-08-08 stsp goto done;
9344 2e1f37b0 2019-08-08 stsp }
9345 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9346 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9347 2e1f37b0 2019-08-08 stsp goto done;
9348 2e1f37b0 2019-08-08 stsp }
9349 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9350 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9351 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9352 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9353 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9354 fe621944 2020-11-10 stsp nchanges++;
9355 fe621944 2020-11-10 stsp }
9356 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9357 2e1f37b0 2019-08-08 stsp int choice;
9358 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9359 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9360 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9361 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9362 2e1f37b0 2019-08-08 stsp if (err)
9363 2e1f37b0 2019-08-08 stsp goto done;
9364 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9365 2e1f37b0 2019-08-08 stsp have_content = 1;
9366 19e4b907 2019-08-08 stsp else
9367 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9368 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9369 2e1f37b0 2019-08-08 stsp break;
9370 2e1f37b0 2019-08-08 stsp }
9371 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9372 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9373 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9374 2e1f37b0 2019-08-08 stsp done:
9375 2e1f37b0 2019-08-08 stsp free(label1);
9376 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9377 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9378 2e1f37b0 2019-08-08 stsp if (blob)
9379 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9380 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9381 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9382 2e1f37b0 2019-08-08 stsp if (staged_blob)
9383 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9384 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9385 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9386 fe621944 2020-11-10 stsp err = free_err;
9387 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9388 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9389 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9390 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9391 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9392 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9393 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9394 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9395 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9396 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9397 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9398 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9399 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9400 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9401 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9402 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9403 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9404 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9405 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9406 2e1f37b0 2019-08-08 stsp }
9407 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9408 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9409 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9410 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9411 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9412 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9413 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9414 2e1f37b0 2019-08-08 stsp }
9415 2e1f37b0 2019-08-08 stsp free(path1);
9416 2e1f37b0 2019-08-08 stsp free(path2);
9417 fda8017d 2020-07-23 stsp return err;
9418 fda8017d 2020-07-23 stsp }
9419 fda8017d 2020-07-23 stsp
9420 fda8017d 2020-07-23 stsp static const struct got_error *
9421 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9422 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9423 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9424 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9425 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9426 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9427 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9428 fda8017d 2020-07-23 stsp {
9429 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9430 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9431 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9432 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9433 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9434 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9435 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9436 fda8017d 2020-07-23 stsp struct stat sb;
9437 fda8017d 2020-07-23 stsp
9438 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9439 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9440 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9441 fda8017d 2020-07-23 stsp if (err)
9442 fda8017d 2020-07-23 stsp return err;
9443 fda8017d 2020-07-23 stsp
9444 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9445 fda8017d 2020-07-23 stsp return NULL;
9446 fda8017d 2020-07-23 stsp
9447 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9448 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9449 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9450 fda8017d 2020-07-23 stsp if (err)
9451 fda8017d 2020-07-23 stsp goto done;
9452 fda8017d 2020-07-23 stsp }
9453 fda8017d 2020-07-23 stsp
9454 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9455 fda8017d 2020-07-23 stsp if (f == NULL) {
9456 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9457 fda8017d 2020-07-23 stsp path_unstaged_content);
9458 fda8017d 2020-07-23 stsp goto done;
9459 fda8017d 2020-07-23 stsp }
9460 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9461 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9462 fda8017d 2020-07-23 stsp goto done;
9463 fda8017d 2020-07-23 stsp }
9464 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9465 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9466 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9467 fda8017d 2020-07-23 stsp size_t r;
9468 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9469 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9470 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9471 fda8017d 2020-07-23 stsp goto done;
9472 fda8017d 2020-07-23 stsp }
9473 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9474 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9475 fda8017d 2020-07-23 stsp goto done;
9476 fda8017d 2020-07-23 stsp }
9477 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9478 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9479 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9480 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9481 fda8017d 2020-07-23 stsp progress_arg);
9482 fda8017d 2020-07-23 stsp } else {
9483 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9484 67a66647 2021-05-31 stsp
9485 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9486 67a66647 2021-05-31 stsp if (err)
9487 67a66647 2021-05-31 stsp return err;
9488 67a66647 2021-05-31 stsp
9489 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9490 67a66647 2021-05-31 stsp parent) == -1) {
9491 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9492 67a66647 2021-05-31 stsp base_path = NULL;
9493 67a66647 2021-05-31 stsp goto done;
9494 67a66647 2021-05-31 stsp }
9495 67a66647 2021-05-31 stsp
9496 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9497 fc2a50f2 2022-11-01 thomas base_path, "");
9498 67a66647 2021-05-31 stsp if (err)
9499 67a66647 2021-05-31 stsp goto done;
9500 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9501 67a66647 2021-05-31 stsp blob_base);
9502 67a66647 2021-05-31 stsp if (err)
9503 67a66647 2021-05-31 stsp goto done;
9504 67a66647 2021-05-31 stsp
9505 b6b86fd1 2022-08-30 thomas /*
9506 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9507 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9508 eec2f5a9 2021-06-03 stsp */
9509 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9510 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9511 eec2f5a9 2021-06-03 stsp ondisk_path);
9512 eec2f5a9 2021-06-03 stsp if (err)
9513 eec2f5a9 2021-06-03 stsp goto done;
9514 eec2f5a9 2021-06-03 stsp } else {
9515 eec2f5a9 2021-06-03 stsp int fd;
9516 06340621 2021-12-31 thomas fd = open(ondisk_path,
9517 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9518 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9519 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9520 eec2f5a9 2021-06-03 stsp goto done;
9521 eec2f5a9 2021-06-03 stsp }
9522 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9523 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9524 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9525 eec2f5a9 2021-06-03 stsp close(fd);
9526 eec2f5a9 2021-06-03 stsp goto done;
9527 eec2f5a9 2021-06-03 stsp }
9528 eec2f5a9 2021-06-03 stsp }
9529 eec2f5a9 2021-06-03 stsp
9530 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9531 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9532 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9533 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9534 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9535 fda8017d 2020-07-23 stsp }
9536 fda8017d 2020-07-23 stsp if (err)
9537 fda8017d 2020-07-23 stsp goto done;
9538 fda8017d 2020-07-23 stsp
9539 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9540 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9541 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9542 2ac8aa02 2020-11-09 stsp } else {
9543 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9544 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9545 2ac8aa02 2020-11-09 stsp }
9546 fda8017d 2020-07-23 stsp done:
9547 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9548 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9549 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9550 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9551 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9552 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9553 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9554 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9555 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9556 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9557 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9558 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9559 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9560 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9561 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9562 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9563 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9564 67a66647 2021-05-31 stsp free(blob_base_path);
9565 67a66647 2021-05-31 stsp free(parent);
9566 67a66647 2021-05-31 stsp free(base_path);
9567 2e1f37b0 2019-08-08 stsp return err;
9568 2e1f37b0 2019-08-08 stsp }
9569 2e1f37b0 2019-08-08 stsp
9570 ad493afc 2019-08-03 stsp static const struct got_error *
9571 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9572 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9573 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9574 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9575 ad493afc 2019-08-03 stsp {
9576 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9577 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9578 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9579 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9580 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9581 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9582 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9583 9bc94a15 2019-08-03 stsp struct stat sb;
9584 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9585 ad493afc 2019-08-03 stsp
9586 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9587 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9588 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9589 2e1f37b0 2019-08-08 stsp return NULL;
9590 2e1f37b0 2019-08-08 stsp
9591 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9592 ad493afc 2019-08-03 stsp if (ie == NULL)
9593 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9594 9bc94a15 2019-08-03 stsp
9595 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9596 9bc94a15 2019-08-03 stsp == -1)
9597 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9598 ad493afc 2019-08-03 stsp
9599 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9600 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9601 f69721c3 2019-10-21 stsp if (err)
9602 f69721c3 2019-10-21 stsp goto done;
9603 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9604 f69721c3 2019-10-21 stsp id_str) == -1) {
9605 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9606 f4ae6ddb 2022-07-01 thomas goto done;
9607 f4ae6ddb 2022-07-01 thomas }
9608 f4ae6ddb 2022-07-01 thomas
9609 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9610 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9611 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9612 f4ae6ddb 2022-07-01 thomas goto done;
9613 f4ae6ddb 2022-07-01 thomas }
9614 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9615 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9616 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9617 f69721c3 2019-10-21 stsp goto done;
9618 f69721c3 2019-10-21 stsp }
9619 f69721c3 2019-10-21 stsp
9620 ad493afc 2019-08-03 stsp switch (staged_status) {
9621 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9622 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9623 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9624 ad493afc 2019-08-03 stsp if (err)
9625 ad493afc 2019-08-03 stsp break;
9626 ad493afc 2019-08-03 stsp /* fall through */
9627 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9628 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9629 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9630 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9631 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9632 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9633 2e1f37b0 2019-08-08 stsp if (err)
9634 2e1f37b0 2019-08-08 stsp break;
9635 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9636 2e1f37b0 2019-08-08 stsp break;
9637 2e1f37b0 2019-08-08 stsp } else {
9638 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9639 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9640 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9641 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9642 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9643 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9644 2e1f37b0 2019-08-08 stsp }
9645 2e1f37b0 2019-08-08 stsp }
9646 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9647 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9648 ad493afc 2019-08-03 stsp if (err)
9649 ad493afc 2019-08-03 stsp break;
9650 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9651 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9652 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9653 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9654 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9655 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9656 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9657 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9658 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9659 ea7786be 2020-07-23 stsp break;
9660 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9661 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9662 36bf999c 2020-07-23 stsp char *staged_target;
9663 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9664 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9665 36bf999c 2020-07-23 stsp if (err)
9666 36bf999c 2020-07-23 stsp goto done;
9667 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9668 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9669 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9670 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9671 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9672 36bf999c 2020-07-23 stsp free(staged_target);
9673 dfe9fba0 2020-07-23 stsp } else {
9674 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9675 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9676 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9677 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9678 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9679 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9680 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9681 dfe9fba0 2020-07-23 stsp }
9682 ea7786be 2020-07-23 stsp break;
9683 ea7786be 2020-07-23 stsp default:
9684 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9685 ea7786be 2020-07-23 stsp break;
9686 ea7786be 2020-07-23 stsp }
9687 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9688 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9689 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9690 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9691 2ac8aa02 2020-11-09 stsp }
9692 ad493afc 2019-08-03 stsp break;
9693 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9694 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9695 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9696 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9697 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9698 2e1f37b0 2019-08-08 stsp if (err)
9699 2e1f37b0 2019-08-08 stsp break;
9700 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9701 2e1f37b0 2019-08-08 stsp break;
9702 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9703 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9704 2e1f37b0 2019-08-08 stsp break;
9705 2e1f37b0 2019-08-08 stsp }
9706 2e1f37b0 2019-08-08 stsp }
9707 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9708 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9709 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9710 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9711 9bc94a15 2019-08-03 stsp if (err)
9712 9bc94a15 2019-08-03 stsp break;
9713 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9714 ad493afc 2019-08-03 stsp break;
9715 ad493afc 2019-08-03 stsp }
9716 f69721c3 2019-10-21 stsp done:
9717 ad493afc 2019-08-03 stsp free(ondisk_path);
9718 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9719 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9720 ad493afc 2019-08-03 stsp if (blob_base)
9721 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9722 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9723 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9724 ad493afc 2019-08-03 stsp if (blob_staged)
9725 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9726 f69721c3 2019-10-21 stsp free(id_str);
9727 f69721c3 2019-10-21 stsp free(label_orig);
9728 ad493afc 2019-08-03 stsp return err;
9729 ad493afc 2019-08-03 stsp }
9730 ad493afc 2019-08-03 stsp
9731 ad493afc 2019-08-03 stsp const struct got_error *
9732 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9733 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9734 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9735 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9736 ad493afc 2019-08-03 stsp struct got_repository *repo)
9737 ad493afc 2019-08-03 stsp {
9738 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9739 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9740 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9741 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9742 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9743 ad493afc 2019-08-03 stsp
9744 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9745 ad493afc 2019-08-03 stsp if (err)
9746 ad493afc 2019-08-03 stsp return err;
9747 ad493afc 2019-08-03 stsp
9748 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9749 ad493afc 2019-08-03 stsp if (err)
9750 ad493afc 2019-08-03 stsp goto done;
9751 ad493afc 2019-08-03 stsp
9752 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9753 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9754 ad493afc 2019-08-03 stsp upa.repo = repo;
9755 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9756 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9757 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9758 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9759 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9760 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9761 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9762 ad493afc 2019-08-03 stsp if (err)
9763 ad493afc 2019-08-03 stsp goto done;
9764 ad493afc 2019-08-03 stsp }
9765 ad493afc 2019-08-03 stsp
9766 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9767 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9768 ad493afc 2019-08-03 stsp err = sync_err;
9769 ad493afc 2019-08-03 stsp done:
9770 ad493afc 2019-08-03 stsp free(fileindex_path);
9771 ad493afc 2019-08-03 stsp if (fileindex)
9772 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9773 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9774 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9775 ad493afc 2019-08-03 stsp err = unlockerr;
9776 ad493afc 2019-08-03 stsp return err;
9777 ad493afc 2019-08-03 stsp }
9778 b2118c49 2020-07-28 stsp
9779 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9780 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9781 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9782 b2118c49 2020-07-28 stsp void *info_arg;
9783 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9784 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9785 b2118c49 2020-07-28 stsp void *cancel_arg;
9786 b2118c49 2020-07-28 stsp };
9787 b2118c49 2020-07-28 stsp
9788 b2118c49 2020-07-28 stsp static const struct got_error *
9789 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9790 b2118c49 2020-07-28 stsp {
9791 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
9792 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9793 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9794 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9795 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9796 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9797 b2118c49 2020-07-28 stsp int stage;
9798 b2118c49 2020-07-28 stsp
9799 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
9800 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
9801 eb6fb9cd 2023-07-26 thomas if (err)
9802 eb6fb9cd 2023-07-26 thomas return err;
9803 eb6fb9cd 2023-07-26 thomas }
9804 b2118c49 2020-07-28 stsp
9805 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9806 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9807 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9808 b2118c49 2020-07-28 stsp break;
9809 b2118c49 2020-07-28 stsp }
9810 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9811 b2118c49 2020-07-28 stsp return NULL;
9812 b2118c49 2020-07-28 stsp
9813 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9814 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9815 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9816 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9817 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9818 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9819 43010591 2023-02-17 thomas &staged_blob_id, ie);
9820 b2118c49 2020-07-28 stsp }
9821 b2118c49 2020-07-28 stsp
9822 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9823 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9824 b2118c49 2020-07-28 stsp
9825 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9826 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9827 b2118c49 2020-07-28 stsp }
9828 b2118c49 2020-07-28 stsp
9829 b2118c49 2020-07-28 stsp const struct got_error *
9830 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9831 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9832 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9833 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9834 b2118c49 2020-07-28 stsp
9835 b2118c49 2020-07-28 stsp {
9836 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9837 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9838 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9839 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9840 b2118c49 2020-07-28 stsp
9841 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9842 b2118c49 2020-07-28 stsp if (err)
9843 b2118c49 2020-07-28 stsp return err;
9844 b2118c49 2020-07-28 stsp
9845 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9846 b2118c49 2020-07-28 stsp if (err)
9847 b2118c49 2020-07-28 stsp goto done;
9848 b2118c49 2020-07-28 stsp
9849 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9850 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9851 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9852 b2118c49 2020-07-28 stsp arg.paths = paths;
9853 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9854 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9855 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9856 b2118c49 2020-07-28 stsp &arg);
9857 b2118c49 2020-07-28 stsp done:
9858 b2118c49 2020-07-28 stsp free(fileindex_path);
9859 b2118c49 2020-07-28 stsp if (fileindex)
9860 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9861 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9862 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9863 b2118c49 2020-07-28 stsp err = unlockerr;
9864 b2118c49 2020-07-28 stsp return err;
9865 b2118c49 2020-07-28 stsp }
9866 814624e7 2022-03-22 thomas
9867 814624e7 2022-03-22 thomas static const struct got_error *
9868 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9869 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9870 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9871 814624e7 2022-03-22 thomas {
9872 814624e7 2022-03-22 thomas const struct got_error *err;
9873 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9874 814624e7 2022-03-22 thomas struct stat sb;
9875 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9876 814624e7 2022-03-22 thomas
9877 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9878 814624e7 2022-03-22 thomas if (err)
9879 814624e7 2022-03-22 thomas return err;
9880 814624e7 2022-03-22 thomas
9881 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9882 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9883 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9884 814624e7 2022-03-22 thomas
9885 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9886 814624e7 2022-03-22 thomas if (ie) {
9887 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9888 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9889 12de5570 2022-05-01 thomas repo);
9890 814624e7 2022-03-22 thomas if (err)
9891 814624e7 2022-03-22 thomas goto done;
9892 814624e7 2022-03-22 thomas } else {
9893 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9894 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9895 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9896 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9897 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9898 814624e7 2022-03-22 thomas ondisk_path);
9899 814624e7 2022-03-22 thomas goto done;
9900 814624e7 2022-03-22 thomas }
9901 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9902 814624e7 2022-03-22 thomas }
9903 814624e7 2022-03-22 thomas }
9904 814624e7 2022-03-22 thomas
9905 814624e7 2022-03-22 thomas done:
9906 814624e7 2022-03-22 thomas free(ondisk_path);
9907 814624e7 2022-03-22 thomas return err;
9908 814624e7 2022-03-22 thomas }
9909 814624e7 2022-03-22 thomas
9910 814624e7 2022-03-22 thomas static const struct got_error *
9911 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9912 814624e7 2022-03-22 thomas unsigned char staged_status)
9913 814624e7 2022-03-22 thomas {
9914 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9915 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9916 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9917 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9918 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9919 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9920 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9921 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9922 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9923 814624e7 2022-03-22 thomas return NULL;
9924 814624e7 2022-03-22 thomas }
9925 814624e7 2022-03-22 thomas
9926 814624e7 2022-03-22 thomas static const struct got_error *
9927 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9928 814624e7 2022-03-22 thomas {
9929 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9930 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9931 814624e7 2022-03-22 thomas return NULL;
9932 814624e7 2022-03-22 thomas }
9933 814624e7 2022-03-22 thomas
9934 814624e7 2022-03-22 thomas static const struct got_error *
9935 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9936 814624e7 2022-03-22 thomas unsigned char staged_status)
9937 814624e7 2022-03-22 thomas {
9938 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9939 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9940 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9941 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9942 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9943 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9944 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9945 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9946 814624e7 2022-03-22 thomas return NULL;
9947 814624e7 2022-03-22 thomas }
9948 814624e7 2022-03-22 thomas
9949 814624e7 2022-03-22 thomas const struct got_error *
9950 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9951 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9952 814624e7 2022-03-22 thomas {
9953 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9954 814624e7 2022-03-22 thomas }
9955 814624e7 2022-03-22 thomas
9956 814624e7 2022-03-22 thomas const struct got_error *
9957 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9958 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9959 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9960 814624e7 2022-03-22 thomas {
9961 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9962 814624e7 2022-03-22 thomas int file_renamed = 0;
9963 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9964 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9965 814624e7 2022-03-22 thomas
9966 814624e7 2022-03-22 thomas *oldpath = NULL;
9967 814624e7 2022-03-22 thomas *newpath = NULL;
9968 814624e7 2022-03-22 thomas
9969 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9970 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9971 814624e7 2022-03-22 thomas if (err)
9972 814624e7 2022-03-22 thomas goto done;
9973 814624e7 2022-03-22 thomas
9974 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9975 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9976 814624e7 2022-03-22 thomas if (err)
9977 814624e7 2022-03-22 thomas goto done;
9978 814624e7 2022-03-22 thomas
9979 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9980 814624e7 2022-03-22 thomas file_renamed = 1;
9981 814624e7 2022-03-22 thomas
9982 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9983 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9984 814624e7 2022-03-22 thomas else if (file_renamed) {
9985 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9986 814624e7 2022-03-22 thomas if (err == NULL)
9987 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9988 814624e7 2022-03-22 thomas } else if (old == NULL)
9989 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9990 814624e7 2022-03-22 thomas else
9991 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9992 814624e7 2022-03-22 thomas
9993 814624e7 2022-03-22 thomas done:
9994 814624e7 2022-03-22 thomas if (err) {
9995 814624e7 2022-03-22 thomas free(*oldpath);
9996 814624e7 2022-03-22 thomas *oldpath = NULL;
9997 814624e7 2022-03-22 thomas free(*newpath);
9998 814624e7 2022-03-22 thomas *newpath = NULL;
9999 814624e7 2022-03-22 thomas }
10000 814624e7 2022-03-22 thomas return err;
10001 814624e7 2022-03-22 thomas }
10002 814624e7 2022-03-22 thomas
10003 814624e7 2022-03-22 thomas const struct got_error *
10004 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10005 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10006 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
10007 814624e7 2022-03-22 thomas {
10008 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
10009 5e22b737 2022-05-31 thomas
10010 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
10011 5e22b737 2022-05-31 thomas saa.worktree = worktree;
10012 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
10013 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
10014 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
10015 5e22b737 2022-05-31 thomas saa.repo = repo;
10016 5e22b737 2022-05-31 thomas
10017 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
10018 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
10019 814624e7 2022-03-22 thomas }
10020 5e22b737 2022-05-31 thomas
10021 5e22b737 2022-05-31 thomas const struct got_error *
10022 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10023 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10024 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
10025 5e22b737 2022-05-31 thomas {
10026 8ec7352a 2023-06-01 thomas const struct got_error *err;
10027 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
10028 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
10029 5e22b737 2022-05-31 thomas
10030 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
10031 5e22b737 2022-05-31 thomas sda.worktree = worktree;
10032 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
10033 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
10034 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
10035 5e22b737 2022-05-31 thomas sda.repo = repo;
10036 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
10037 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
10038 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
10039 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
10040 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
10041 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
10042 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
10043 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
10044 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
10045 8ec7352a 2023-06-01 thomas
10046 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
10047 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10048 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
10049 8ec7352a 2023-06-01 thomas return err;
10050 5e22b737 2022-05-31 thomas }
10051 5e22b737 2022-05-31 thomas
10052 5e22b737 2022-05-31 thomas const struct got_error *
10053 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
10054 36832a8e 2022-05-31 thomas const char *fileindex_path)
10055 5e22b737 2022-05-31 thomas {
10056 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
10057 5e22b737 2022-05-31 thomas
10058 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
10059 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
10060 5e22b737 2022-05-31 thomas
10061 5e22b737 2022-05-31 thomas return err;
10062 5e22b737 2022-05-31 thomas }