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 aaf972e2 2023-09-18 thomas int wt_fd, const char *path, struct got_object_id *blob_id,
1105 aaf972e2 2023-09-18 thomas int update_timestamps)
1106 13d9040b 2019-03-27 stsp {
1107 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1108 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1109 13d9040b 2019-03-27 stsp
1110 65b05cec 2020-07-23 stsp *new_iep = NULL;
1111 65b05cec 2020-07-23 stsp
1112 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1113 054041d0 2020-06-24 stsp if (err)
1114 054041d0 2020-06-24 stsp return err;
1115 054041d0 2020-06-24 stsp
1116 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1117 aaf972e2 2023-09-18 thomas blob_id->sha1, base_commit_id->sha1, update_timestamps);
1118 054041d0 2020-06-24 stsp if (err)
1119 054041d0 2020-06-24 stsp goto done;
1120 054041d0 2020-06-24 stsp
1121 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1122 054041d0 2020-06-24 stsp done:
1123 054041d0 2020-06-24 stsp if (err)
1124 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1125 65b05cec 2020-07-23 stsp else
1126 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1127 13d9040b 2019-03-27 stsp return err;
1128 1ebedb77 2019-10-19 stsp }
1129 1ebedb77 2019-10-19 stsp
1130 1ebedb77 2019-10-19 stsp static mode_t
1131 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1132 1ebedb77 2019-10-19 stsp {
1133 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1134 1ebedb77 2019-10-19 stsp
1135 1ebedb77 2019-10-19 stsp if (executable) {
1136 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1137 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1138 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1139 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1140 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1141 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1142 1ebedb77 2019-10-19 stsp }
1143 1ebedb77 2019-10-19 stsp
1144 a2c162eb 2022-10-30 thomas return st_mode;
1145 13d9040b 2019-03-27 stsp }
1146 13d9040b 2019-03-27 stsp
1147 8ba819a3 2020-07-23 stsp /* forward declaration */
1148 13d9040b 2019-03-27 stsp static const struct got_error *
1149 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1150 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1151 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1152 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1153 aaf972e2 2023-09-18 thomas int path_is_unversioned, int *update_timestamps,
1154 aaf972e2 2023-09-18 thomas struct got_repository *repo,
1155 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1156 8ba819a3 2020-07-23 stsp
1157 5a1fbc73 2020-07-23 stsp /*
1158 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1159 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1160 5a1fbc73 2020-07-23 stsp */
1161 8ba819a3 2020-07-23 stsp static const struct got_error *
1162 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1163 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1164 5a1fbc73 2020-07-23 stsp {
1165 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1166 5a1fbc73 2020-07-23 stsp ssize_t elen;
1167 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1168 5a1fbc73 2020-07-23 stsp int fd;
1169 5a1fbc73 2020-07-23 stsp
1170 c6e8a826 2021-04-05 stsp *did_something = 0;
1171 c6e8a826 2021-04-05 stsp
1172 5a1fbc73 2020-07-23 stsp /*
1173 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1174 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1175 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1176 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1177 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1178 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1179 5a1fbc73 2020-07-23 stsp */
1180 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1181 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1182 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1183 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1184 5a1fbc73 2020-07-23 stsp
1185 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1186 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1187 5a1fbc73 2020-07-23 stsp if (elen == -1)
1188 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1189 5a1fbc73 2020-07-23 stsp
1190 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1191 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1192 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1193 5a1fbc73 2020-07-23 stsp }
1194 5a1fbc73 2020-07-23 stsp
1195 c6e8a826 2021-04-05 stsp *did_something = 1;
1196 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1197 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1198 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1199 5a1fbc73 2020-07-23 stsp return err;
1200 3c1ec782 2020-07-23 stsp }
1201 3c1ec782 2020-07-23 stsp
1202 3c1ec782 2020-07-23 stsp static const struct got_error *
1203 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1204 ad10f64e 2023-07-19 thomas size_t target_len, const char *ondisk_path, const char *wtroot_path,
1205 ad10f64e 2023-07-19 thomas const char *meta_dir)
1206 3c1ec782 2020-07-23 stsp {
1207 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1208 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1209 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1210 3c1ec782 2020-07-23 stsp
1211 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1212 3c1ec782 2020-07-23 stsp
1213 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1214 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1215 3c1ec782 2020-07-23 stsp return NULL;
1216 3c1ec782 2020-07-23 stsp }
1217 3c1ec782 2020-07-23 stsp
1218 3c1ec782 2020-07-23 stsp /*
1219 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1220 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1221 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1222 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1223 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1224 3c1ec782 2020-07-23 stsp */
1225 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1226 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1227 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1228 ce031e9e 2020-10-20 stsp if (err)
1229 ce031e9e 2020-10-20 stsp return err;
1230 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1231 ce031e9e 2020-10-20 stsp free(parent);
1232 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1233 ce031e9e 2020-10-20 stsp }
1234 ce031e9e 2020-10-20 stsp free(parent);
1235 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1236 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1237 3c1ec782 2020-07-23 stsp free(abspath);
1238 3c1ec782 2020-07-23 stsp return err;
1239 3c1ec782 2020-07-23 stsp }
1240 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1241 3c1ec782 2020-07-23 stsp free(abspath);
1242 3c1ec782 2020-07-23 stsp if (err)
1243 3c1ec782 2020-07-23 stsp return err;
1244 3c1ec782 2020-07-23 stsp } else {
1245 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1246 3c1ec782 2020-07-23 stsp if (err)
1247 3c1ec782 2020-07-23 stsp return err;
1248 3c1ec782 2020-07-23 stsp }
1249 3c1ec782 2020-07-23 stsp
1250 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1251 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1252 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1253 3c1ec782 2020-07-23 stsp return NULL;
1254 3c1ec782 2020-07-23 stsp }
1255 3c1ec782 2020-07-23 stsp
1256 ad10f64e 2023-07-19 thomas /* Do not allow symlinks pointing into the meta directory. */
1257 ad10f64e 2023-07-19 thomas if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1258 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1259 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1260 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1261 3c1ec782 2020-07-23 stsp
1262 3c1ec782 2020-07-23 stsp free(path_got);
1263 3c1ec782 2020-07-23 stsp return NULL;
1264 5a1fbc73 2020-07-23 stsp }
1265 5a1fbc73 2020-07-23 stsp
1266 5a1fbc73 2020-07-23 stsp static const struct got_error *
1267 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1268 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1269 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1270 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1271 ace90326 2021-09-27 thomas struct got_repository *repo,
1272 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1273 9d31a1d8 2018-03-11 stsp {
1274 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1275 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1276 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1277 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1278 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1279 8ba819a3 2020-07-23 stsp
1280 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1281 2e1fa222 2020-07-23 stsp
1282 9e39ca77 2022-07-21 thomas /*
1283 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1284 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1285 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1286 8ba819a3 2020-07-23 stsp * in the blob object.
1287 8ba819a3 2020-07-23 stsp */
1288 8ba819a3 2020-07-23 stsp do {
1289 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1290 3521b466 2022-07-21 thomas if (err)
1291 3521b466 2022-07-21 thomas return err;
1292 3521b466 2022-07-21 thomas
1293 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1294 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1295 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1296 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1297 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1298 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1299 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1300 aaf972e2 2023-09-18 thomas 1, path_is_unversioned, NULL, repo, progress_cb,
1301 3b9f0f87 2020-07-23 stsp progress_arg);
1302 8ba819a3 2020-07-23 stsp }
1303 8ba819a3 2020-07-23 stsp if (len > 0) {
1304 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1305 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1306 8ba819a3 2020-07-23 stsp len - hdrlen);
1307 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1308 8ba819a3 2020-07-23 stsp hdrlen = 0;
1309 8ba819a3 2020-07-23 stsp }
1310 8ba819a3 2020-07-23 stsp } while (len != 0);
1311 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1312 8ba819a3 2020-07-23 stsp
1313 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1314 ad10f64e 2023-07-19 thomas ondisk_path, worktree->root_path, worktree->meta_dir);
1315 3c1ec782 2020-07-23 stsp if (err)
1316 3c1ec782 2020-07-23 stsp return err;
1317 8ba819a3 2020-07-23 stsp
1318 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1319 906c123b 2020-07-23 stsp /* install as a regular file */
1320 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1321 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1322 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1323 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1324 aaf972e2 2023-09-18 thomas path_is_unversioned, NULL, repo,
1325 aaf972e2 2023-09-18 thomas progress_cb, progress_arg);
1326 9e39ca77 2022-07-21 thomas return err;
1327 906c123b 2020-07-23 stsp }
1328 906c123b 2020-07-23 stsp
1329 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1330 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1331 c6e8a826 2021-04-05 stsp int symlink_replaced;
1332 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1333 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1334 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1335 9e39ca77 2022-07-21 thomas return err;
1336 c90c8ce3 2020-07-23 stsp }
1337 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1338 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1339 5a1fbc73 2020-07-23 stsp if (err)
1340 9e39ca77 2022-07-21 thomas return err;
1341 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1342 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1343 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1344 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1345 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1346 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1347 c6e8a826 2021-04-05 stsp } else {
1348 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1349 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1350 c6e8a826 2021-04-05 stsp }
1351 f35fa46a 2020-07-23 stsp }
1352 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1353 f35fa46a 2020-07-23 stsp }
1354 f35fa46a 2020-07-23 stsp
1355 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1356 f4994adc 2020-10-20 stsp char *parent;
1357 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1358 f4994adc 2020-10-20 stsp if (err)
1359 9e39ca77 2022-07-21 thomas return err;
1360 59530ea7 2023-07-17 thomas err = got_path_mkdir(parent);
1361 f4994adc 2020-10-20 stsp free(parent);
1362 f35fa46a 2020-07-23 stsp if (err)
1363 9e39ca77 2022-07-21 thomas return err;
1364 f35fa46a 2020-07-23 stsp /*
1365 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1366 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1367 f35fa46a 2020-07-23 stsp */
1368 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1369 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1370 59530ea7 2023-07-17 thomas if (progress_cb) {
1371 59530ea7 2023-07-17 thomas err = (*progress_cb)(progress_arg,
1372 59530ea7 2023-07-17 thomas reverting_versioned_file ?
1373 59530ea7 2023-07-17 thomas GOT_STATUS_REVERT : GOT_STATUS_ADD,
1374 59530ea7 2023-07-17 thomas path);
1375 59530ea7 2023-07-17 thomas }
1376 9e39ca77 2022-07-21 thomas return err;
1377 f35fa46a 2020-07-23 stsp }
1378 f35fa46a 2020-07-23 stsp }
1379 f35fa46a 2020-07-23 stsp
1380 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1381 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1382 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1383 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1384 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1385 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1386 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1387 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1388 aaf972e2 2023-09-18 thomas path_is_unversioned, NULL, repo,
1389 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1390 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1391 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1392 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1393 8ba819a3 2020-07-23 stsp } else {
1394 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1395 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1396 8ba819a3 2020-07-23 stsp }
1397 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1398 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1399 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1400 8ba819a3 2020-07-23 stsp return err;
1401 8ba819a3 2020-07-23 stsp }
1402 8ba819a3 2020-07-23 stsp
1403 8ba819a3 2020-07-23 stsp static const struct got_error *
1404 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1405 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1406 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1407 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1408 aaf972e2 2023-09-18 thomas int path_is_unversioned, int *update_timestamps,
1409 aaf972e2 2023-09-18 thomas struct got_repository *repo,
1410 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1411 8ba819a3 2020-07-23 stsp {
1412 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1413 507dc3bb 2018-12-29 stsp int fd = -1;
1414 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1415 507dc3bb 2018-12-29 stsp int update = 0;
1416 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1417 a2c162eb 2022-10-30 thomas mode_t mode;
1418 aaf972e2 2023-09-18 thomas
1419 aaf972e2 2023-09-18 thomas if (update_timestamps)
1420 aaf972e2 2023-09-18 thomas *update_timestamps = 1;
1421 8ba819a3 2020-07-23 stsp
1422 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1423 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1424 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1425 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1426 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1427 f5375317 2020-10-20 stsp char *parent;
1428 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1429 f5375317 2020-10-20 stsp if (err)
1430 f5375317 2020-10-20 stsp return err;
1431 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1432 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1433 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1434 f5375317 2020-10-20 stsp free(parent);
1435 21908da4 2019-01-13 stsp if (err)
1436 21908da4 2019-01-13 stsp return err;
1437 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1438 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1439 a2c162eb 2022-10-30 thomas mode);
1440 21908da4 2019-01-13 stsp if (fd == -1)
1441 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1442 230a42bd 2019-05-11 jcs ondisk_path);
1443 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1444 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1445 aaf972e2 2023-09-18 thomas if (update_timestamps)
1446 aaf972e2 2023-09-18 thomas *update_timestamps = 0;
1447 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1448 aaf972e2 2023-09-18 thomas GOT_STATUS_EXISTS, path);
1449 3b9f0f87 2020-07-23 stsp goto done;
1450 3b9f0f87 2020-07-23 stsp }
1451 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1452 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1453 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1454 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1455 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1456 507dc3bb 2018-12-29 stsp goto done;
1457 d70b8e30 2018-12-27 stsp } else {
1458 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1459 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1460 507dc3bb 2018-12-29 stsp if (err)
1461 507dc3bb 2018-12-29 stsp goto done;
1462 507dc3bb 2018-12-29 stsp update = 1;
1463 a2c162eb 2022-10-30 thomas
1464 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1465 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1466 a2c162eb 2022-10-30 thomas tmppath);
1467 a2c162eb 2022-10-30 thomas goto done;
1468 a2c162eb 2022-10-30 thomas }
1469 9d31a1d8 2018-03-11 stsp }
1470 507dc3bb 2018-12-29 stsp } else
1471 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1472 3818e3c4 2020-11-01 naddy }
1473 3818e3c4 2020-11-01 naddy
1474 bd6aa359 2020-07-23 stsp if (progress_cb) {
1475 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1476 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1477 bd6aa359 2020-07-23 stsp path);
1478 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1479 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1480 bd6aa359 2020-07-23 stsp path);
1481 bd6aa359 2020-07-23 stsp else
1482 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1483 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1484 bd6aa359 2020-07-23 stsp if (err)
1485 bd6aa359 2020-07-23 stsp goto done;
1486 bd6aa359 2020-07-23 stsp }
1487 d7b62c98 2018-12-27 stsp
1488 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1489 9d31a1d8 2018-03-11 stsp do {
1490 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1491 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1492 9d31a1d8 2018-03-11 stsp if (err)
1493 9d31a1d8 2018-03-11 stsp break;
1494 9d31a1d8 2018-03-11 stsp if (len > 0) {
1495 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1496 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1497 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1498 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1499 b87c6f83 2018-12-24 stsp goto done;
1500 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1501 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1502 b87c6f83 2018-12-24 stsp goto done;
1503 9d31a1d8 2018-03-11 stsp }
1504 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1505 9d31a1d8 2018-03-11 stsp }
1506 9d31a1d8 2018-03-11 stsp } while (len != 0);
1507 9d31a1d8 2018-03-11 stsp
1508 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1510 816dc654 2019-02-16 stsp goto done;
1511 816dc654 2019-02-16 stsp }
1512 9d31a1d8 2018-03-11 stsp
1513 507dc3bb 2018-12-29 stsp if (update) {
1514 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1515 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1516 f6d8c0ac 2020-11-09 stsp goto done;
1517 f6d8c0ac 2020-11-09 stsp }
1518 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1519 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1520 230a42bd 2019-05-11 jcs ondisk_path);
1521 68ed9ba5 2019-02-10 stsp goto done;
1522 68ed9ba5 2019-02-10 stsp }
1523 63df146d 2020-11-09 stsp free(tmppath);
1524 63df146d 2020-11-09 stsp tmppath = NULL;
1525 507dc3bb 2018-12-29 stsp }
1526 507dc3bb 2018-12-29 stsp
1527 9d31a1d8 2018-03-11 stsp done:
1528 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1529 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1530 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1531 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1532 507dc3bb 2018-12-29 stsp free(tmppath);
1533 6353ad76 2019-02-08 stsp return err;
1534 6353ad76 2019-02-08 stsp }
1535 6353ad76 2019-02-08 stsp
1536 be94c032 2023-02-20 thomas /*
1537 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1538 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1539 be94c032 2023-02-20 thomas */
1540 6353ad76 2019-02-08 stsp static const struct got_error *
1541 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1542 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1543 be94c032 2023-02-20 thomas FILE *ondisk_file)
1544 7154f6ce 2019-03-27 stsp {
1545 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1546 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1547 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1548 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1549 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1550 7154f6ce 2019-03-27 stsp };
1551 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1552 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1553 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1554 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1555 9bdd68dd 2020-01-02 naddy char *line = NULL;
1556 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1557 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1558 7154f6ce 2019-03-27 stsp
1559 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1560 8de9d8ad 2023-02-20 thomas return NULL;
1561 be94c032 2023-02-20 thomas
1562 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1563 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1564 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1565 be94c032 2023-02-20 thomas
1566 8de9d8ad 2023-02-20 thomas if (blob) {
1567 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1568 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1569 be94c032 2023-02-20 thomas if (err)
1570 be94c032 2023-02-20 thomas goto done;
1571 8de9d8ad 2023-02-20 thomas }
1572 be94c032 2023-02-20 thomas
1573 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1574 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1575 8de9d8ad 2023-02-20 thomas if (err)
1576 8de9d8ad 2023-02-20 thomas goto done;
1577 8de9d8ad 2023-02-20 thomas
1578 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1579 8de9d8ad 2023-02-20 thomas
1580 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1581 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1582 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1583 450d9f6b 2023-02-20 thomas off_t pos;
1584 8de9d8ad 2023-02-20 thomas
1585 8de9d8ad 2023-02-20 thomas /*
1586 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1587 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1588 8de9d8ad 2023-02-20 thomas */
1589 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1590 8de9d8ad 2023-02-20 thomas
1591 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1592 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1593 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1594 7154f6ce 2019-03-27 stsp }
1595 7154f6ce 2019-03-27 stsp
1596 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1597 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1598 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1599 450d9f6b 2023-02-20 thomas goto done;
1600 7154f6ce 2019-03-27 stsp }
1601 8de9d8ad 2023-02-20 thomas
1602 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1603 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1604 450d9f6b 2023-02-20 thomas
1605 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1606 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1607 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1608 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1609 8de9d8ad 2023-02-20 thomas break;
1610 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1611 8de9d8ad 2023-02-20 thomas break;
1612 8de9d8ad 2023-02-20 thomas }
1613 8de9d8ad 2023-02-20 thomas
1614 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1615 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1616 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1617 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1618 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1619 8de9d8ad 2023-02-20 thomas goto done;
1620 8de9d8ad 2023-02-20 thomas } else
1621 8de9d8ad 2023-02-20 thomas i++;
1622 8de9d8ad 2023-02-20 thomas }
1623 8de9d8ad 2023-02-20 thomas ++ln;
1624 8de9d8ad 2023-02-20 thomas }
1625 7154f6ce 2019-03-27 stsp }
1626 be94c032 2023-02-20 thomas
1627 be94c032 2023-02-20 thomas done:
1628 9bdd68dd 2020-01-02 naddy free(line);
1629 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1630 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1631 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1632 8de9d8ad 2023-02-20 thomas if (err == NULL)
1633 8de9d8ad 2023-02-20 thomas err = free_err;
1634 7154f6ce 2019-03-27 stsp
1635 7154f6ce 2019-03-27 stsp return err;
1636 e2b1e152 2019-07-13 stsp }
1637 e2b1e152 2019-07-13 stsp
1638 e2b1e152 2019-07-13 stsp static int
1639 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1640 1ebedb77 2019-10-19 stsp {
1641 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1642 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1643 1ebedb77 2019-10-19 stsp }
1644 1ebedb77 2019-10-19 stsp
1645 1ebedb77 2019-10-19 stsp static int
1646 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1647 e2b1e152 2019-07-13 stsp {
1648 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1649 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1650 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1651 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1652 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1653 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1654 c363b2c1 2019-08-03 stsp }
1655 c363b2c1 2019-08-03 stsp
1656 c363b2c1 2019-08-03 stsp static unsigned char
1657 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1658 c363b2c1 2019-08-03 stsp {
1659 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1660 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1661 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1662 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1663 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1664 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1665 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1666 c363b2c1 2019-08-03 stsp default:
1667 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1668 a919d5c4 2020-07-23 stsp }
1669 a919d5c4 2020-07-23 stsp }
1670 a919d5c4 2020-07-23 stsp
1671 a919d5c4 2020-07-23 stsp static const struct got_error *
1672 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1673 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1674 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1675 a919d5c4 2020-07-23 stsp {
1676 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1677 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1678 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1679 a919d5c4 2020-07-23 stsp ssize_t elen;
1680 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1681 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1682 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1683 a919d5c4 2020-07-23 stsp
1684 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1685 a919d5c4 2020-07-23 stsp
1686 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1687 a919d5c4 2020-07-23 stsp do {
1688 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1689 a919d5c4 2020-07-23 stsp if (err)
1690 a919d5c4 2020-07-23 stsp return err;
1691 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1692 a919d5c4 2020-07-23 stsp /*
1693 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1694 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1695 a919d5c4 2020-07-23 stsp */
1696 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1697 a919d5c4 2020-07-23 stsp }
1698 a919d5c4 2020-07-23 stsp if (len > 0) {
1699 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1700 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1701 a919d5c4 2020-07-23 stsp len - hdrlen);
1702 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1703 a919d5c4 2020-07-23 stsp hdrlen = 0;
1704 a919d5c4 2020-07-23 stsp }
1705 a919d5c4 2020-07-23 stsp } while (len != 0);
1706 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1707 a919d5c4 2020-07-23 stsp
1708 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1709 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1710 a919d5c4 2020-07-23 stsp if (elen == -1)
1711 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1712 a919d5c4 2020-07-23 stsp } else {
1713 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1714 a919d5c4 2020-07-23 stsp if (elen == -1)
1715 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1716 c363b2c1 2019-08-03 stsp }
1717 a919d5c4 2020-07-23 stsp
1718 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1719 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1720 a919d5c4 2020-07-23 stsp
1721 a919d5c4 2020-07-23 stsp return NULL;
1722 7154f6ce 2019-03-27 stsp }
1723 7154f6ce 2019-03-27 stsp
1724 7154f6ce 2019-03-27 stsp static const struct got_error *
1725 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1726 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1727 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1728 6353ad76 2019-02-08 stsp {
1729 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1730 6353ad76 2019-02-08 stsp struct got_object_id id;
1731 6353ad76 2019-02-08 stsp size_t hdrlen;
1732 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1733 6353ad76 2019-02-08 stsp FILE *f = NULL;
1734 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1735 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1736 6353ad76 2019-02-08 stsp size_t flen, blen;
1737 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1738 6353ad76 2019-02-08 stsp
1739 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1740 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1741 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1742 6353ad76 2019-02-08 stsp
1743 7f91a133 2019-12-13 stsp /*
1744 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1745 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1746 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1747 7f91a133 2019-12-13 stsp */
1748 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1749 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1750 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1751 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1752 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1753 882ef1b9 2019-12-13 stsp else
1754 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1755 882ef1b9 2019-12-13 stsp goto done;
1756 882ef1b9 2019-12-13 stsp }
1757 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1758 3d35a492 2019-12-13 stsp goto done;
1759 3d35a492 2019-12-13 stsp }
1760 7f91a133 2019-12-13 stsp } else {
1761 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1762 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1763 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1764 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1765 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1766 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1767 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1768 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1769 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1770 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1771 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1772 3d35a492 2019-12-13 stsp else
1773 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1774 3d35a492 2019-12-13 stsp goto done;
1775 3d35a492 2019-12-13 stsp }
1776 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1777 1338848f 2019-12-13 stsp goto done;
1778 a378724f 2019-02-10 stsp }
1779 a378724f 2019-02-10 stsp }
1780 6353ad76 2019-02-08 stsp
1781 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1782 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1783 1338848f 2019-12-13 stsp goto done;
1784 3f148bc6 2019-07-27 stsp }
1785 efdd40df 2019-07-27 stsp
1786 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1787 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1788 1338848f 2019-12-13 stsp goto done;
1789 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1790 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1791 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1792 1338848f 2019-12-13 stsp goto done;
1793 d00136be 2019-03-26 stsp }
1794 b8f41171 2019-02-10 stsp
1795 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1796 f179e66d 2020-07-23 stsp goto done;
1797 f179e66d 2020-07-23 stsp
1798 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1799 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1800 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1801 1338848f 2019-12-13 stsp goto done;
1802 f179e66d 2020-07-23 stsp }
1803 6353ad76 2019-02-08 stsp
1804 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1805 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1806 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1807 c363b2c1 2019-08-03 stsp else
1808 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1809 c363b2c1 2019-08-03 stsp
1810 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1811 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1812 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1813 f4ae6ddb 2022-07-01 thomas goto done;
1814 f4ae6ddb 2022-07-01 thomas }
1815 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1816 6353ad76 2019-02-08 stsp if (err)
1817 1338848f 2019-12-13 stsp goto done;
1818 6353ad76 2019-02-08 stsp
1819 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1820 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1821 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1822 a919d5c4 2020-07-23 stsp goto done;
1823 a919d5c4 2020-07-23 stsp }
1824 a919d5c4 2020-07-23 stsp
1825 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1826 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1827 ab0d4361 2019-12-13 stsp if (fd == -1) {
1828 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1829 ab0d4361 2019-12-13 stsp goto done;
1830 ab0d4361 2019-12-13 stsp }
1831 3d35a492 2019-12-13 stsp }
1832 3d35a492 2019-12-13 stsp
1833 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1834 6353ad76 2019-02-08 stsp if (f == NULL) {
1835 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1836 6353ad76 2019-02-08 stsp goto done;
1837 6353ad76 2019-02-08 stsp }
1838 1338848f 2019-12-13 stsp fd = -1;
1839 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1840 656b1f76 2019-05-11 jcs for (;;) {
1841 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1842 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1843 6353ad76 2019-02-08 stsp if (err)
1844 7154f6ce 2019-03-27 stsp goto done;
1845 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1846 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1847 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1848 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1849 7154f6ce 2019-03-27 stsp goto done;
1850 80c5c120 2019-02-19 stsp }
1851 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1852 6353ad76 2019-02-08 stsp if (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 } else if (flen == 0) {
1856 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1857 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1858 6353ad76 2019-02-08 stsp break;
1859 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1860 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1861 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1862 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1863 6353ad76 2019-02-08 stsp break;
1864 6353ad76 2019-02-08 stsp }
1865 6353ad76 2019-02-08 stsp } else {
1866 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1867 6353ad76 2019-02-08 stsp break;
1868 6353ad76 2019-02-08 stsp }
1869 6353ad76 2019-02-08 stsp hdrlen = 0;
1870 6353ad76 2019-02-08 stsp }
1871 7154f6ce 2019-03-27 stsp
1872 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1873 7154f6ce 2019-03-27 stsp rewind(f);
1874 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1875 be94c032 2023-02-20 thomas sb, f);
1876 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1877 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1878 6353ad76 2019-02-08 stsp done:
1879 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1880 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1881 6353ad76 2019-02-08 stsp if (blob)
1882 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1883 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1884 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1885 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1886 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1887 9d31a1d8 2018-03-11 stsp return err;
1888 e2b1e152 2019-07-13 stsp }
1889 e2b1e152 2019-07-13 stsp
1890 e2b1e152 2019-07-13 stsp /*
1891 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1892 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1893 e2b1e152 2019-07-13 stsp */
1894 e2b1e152 2019-07-13 stsp static const struct got_error *
1895 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1896 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1897 e2b1e152 2019-07-13 stsp {
1898 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1899 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1900 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1901 e2b1e152 2019-07-13 stsp
1902 e2b1e152 2019-07-13 stsp return NULL;
1903 9d31a1d8 2018-03-11 stsp }
1904 9d31a1d8 2018-03-11 stsp
1905 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1906 9f323212 2023-03-10 thomas
1907 9d31a1d8 2018-03-11 stsp static const struct got_error *
1908 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1909 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1910 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1911 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1912 0584f854 2019-04-06 stsp void *progress_arg)
1913 9d31a1d8 2018-03-11 stsp {
1914 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1915 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1916 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1917 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1918 b8f41171 2019-02-10 stsp struct stat sb;
1919 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1920 aaf972e2 2023-09-18 thomas int update_timestamps;
1921 9d31a1d8 2018-03-11 stsp
1922 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1923 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1924 6353ad76 2019-02-08 stsp
1925 abb4604f 2019-07-27 stsp if (ie) {
1926 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1927 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1928 a76c42e6 2019-08-03 stsp goto done;
1929 a76c42e6 2019-08-03 stsp }
1930 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1931 7f91a133 2019-12-13 stsp repo);
1932 abb4604f 2019-07-27 stsp if (err)
1933 abb4604f 2019-07-27 stsp goto done;
1934 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1935 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1936 c90c8ce3 2020-07-23 stsp } else {
1937 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1938 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1939 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1940 e6f4ba31 2021-09-24 thomas ondisk_path);
1941 e6f4ba31 2021-09-24 thomas goto done;
1942 e6f4ba31 2021-09-24 thomas }
1943 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1944 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1945 e6f4ba31 2021-09-24 thomas } else {
1946 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1947 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1948 e6f4ba31 2021-09-24 thomas else
1949 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1950 e6f4ba31 2021-09-24 thomas }
1951 c90c8ce3 2020-07-23 stsp }
1952 6353ad76 2019-02-08 stsp
1953 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1954 2c41dce7 2021-06-27 stsp if (ie)
1955 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1956 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1957 b8f41171 2019-02-10 stsp goto done;
1958 b8f41171 2019-02-10 stsp }
1959 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1960 a769b60b 2021-06-27 stsp if (ie)
1961 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1962 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1963 5036ab18 2020-04-18 stsp path);
1964 5036ab18 2020-04-18 stsp goto done;
1965 9f323212 2023-03-10 thomas }
1966 9f323212 2023-03-10 thomas
1967 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1968 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1969 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1970 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1971 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1972 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1973 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1974 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1975 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1976 9f323212 2023-03-10 thomas } else if (ie) {
1977 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1978 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1979 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1980 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1981 9f323212 2023-03-10 thomas ie->path);
1982 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1983 9f323212 2023-03-10 thomas errno == ENOENT))
1984 9f323212 2023-03-10 thomas goto done;
1985 9f323212 2023-03-10 thomas }
1986 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1987 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1988 9f323212 2023-03-10 thomas ie->path);
1989 9f323212 2023-03-10 thomas }
1990 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1991 5036ab18 2020-04-18 stsp }
1992 b8f41171 2019-02-10 stsp
1993 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1994 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1995 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1996 c6e8a826 2021-04-05 stsp /*
1997 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1998 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1999 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
2000 c6e8a826 2021-04-05 stsp * updating contents of this file.
2001 c6e8a826 2021-04-05 stsp */
2002 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
2003 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2004 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
2005 c6e8a826 2021-04-05 stsp /* Same commit. */
2006 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2007 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2008 e2b1e152 2019-07-13 stsp if (err)
2009 e2b1e152 2019-07-13 stsp goto done;
2010 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2011 b8f41171 2019-02-10 stsp path);
2012 6353ad76 2019-02-08 stsp goto done;
2013 a378724f 2019-02-10 stsp }
2014 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
2015 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
2016 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
2017 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2018 b36a9774 2023-04-14 thomas if (got_fileindex_entry_has_commit(ie)) {
2019 b36a9774 2023-04-14 thomas /* Update the base commit ID of this file. */
2020 b36a9774 2023-04-14 thomas memcpy(ie->commit_sha1,
2021 b36a9774 2023-04-14 thomas worktree->base_commit_id->sha1,
2022 b36a9774 2023-04-14 thomas sizeof(ie->commit_sha1));
2023 b36a9774 2023-04-14 thomas }
2024 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2025 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2026 0f58026f 2021-04-05 stsp if (err)
2027 0f58026f 2021-04-05 stsp goto done;
2028 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2029 0f58026f 2021-04-05 stsp path);
2030 b8f41171 2019-02-10 stsp goto done;
2031 e2b1e152 2019-07-13 stsp }
2032 9d31a1d8 2018-03-11 stsp }
2033 9d31a1d8 2018-03-11 stsp
2034 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2035 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2036 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2037 f4ae6ddb 2022-07-01 thomas goto done;
2038 f4ae6ddb 2022-07-01 thomas }
2039 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2040 8da9e5f4 2019-01-12 stsp if (err)
2041 6353ad76 2019-02-08 stsp goto done;
2042 512f0d0e 2019-01-02 stsp
2043 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2044 234035bc 2019-06-01 stsp int update_timestamps;
2045 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2046 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2047 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2048 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2049 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2050 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2051 f4ae6ddb 2022-07-01 thomas goto done;
2052 f4ae6ddb 2022-07-01 thomas }
2053 234035bc 2019-06-01 stsp struct got_object_id id2;
2054 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2055 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2056 f4ae6ddb 2022-07-01 thomas fd2);
2057 234035bc 2019-06-01 stsp if (err)
2058 f69721c3 2019-10-21 stsp goto done;
2059 f69721c3 2019-10-21 stsp }
2060 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2061 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2062 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2063 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2064 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2065 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2066 f69721c3 2019-10-21 stsp goto done;
2067 f69721c3 2019-10-21 stsp }
2068 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2069 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2070 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2071 234035bc 2019-06-01 stsp goto done;
2072 f69721c3 2019-10-21 stsp }
2073 234035bc 2019-06-01 stsp }
2074 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2075 36bf999c 2020-07-23 stsp char *link_target;
2076 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2077 36bf999c 2020-07-23 stsp if (err)
2078 36bf999c 2020-07-23 stsp goto done;
2079 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2080 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2081 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2082 36bf999c 2020-07-23 stsp free(link_target);
2083 993e2a1b 2020-07-23 stsp } else {
2084 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2085 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2086 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2087 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2088 993e2a1b 2020-07-23 stsp }
2089 f69721c3 2019-10-21 stsp free(label_orig);
2090 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2091 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2092 f4ae6ddb 2022-07-01 thomas goto done;
2093 f4ae6ddb 2022-07-01 thomas }
2094 234035bc 2019-06-01 stsp if (blob2)
2095 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2096 909d120e 2019-09-22 stsp if (err)
2097 909d120e 2019-09-22 stsp goto done;
2098 234035bc 2019-06-01 stsp /*
2099 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2100 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2101 234035bc 2019-06-01 stsp * unmodified files again.
2102 234035bc 2019-06-01 stsp */
2103 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2104 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2105 234035bc 2019-06-01 stsp update_timestamps);
2106 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2107 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2108 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2109 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2110 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2111 1ee397ad 2019-07-12 stsp if (err)
2112 1ee397ad 2019-07-12 stsp goto done;
2113 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2114 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2115 13d9040b 2019-03-27 stsp if (err)
2116 13d9040b 2019-03-27 stsp goto done;
2117 13d9040b 2019-03-27 stsp } else {
2118 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2119 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2120 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2121 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2122 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2123 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2124 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2125 2e1fa222 2020-07-23 stsp } else {
2126 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2127 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2128 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2129 aaf972e2 2023-09-18 thomas status == GOT_STATUS_UNVERSIONED,
2130 aaf972e2 2023-09-18 thomas &update_timestamps,
2131 aaf972e2 2023-09-18 thomas repo, progress_cb, progress_arg);
2132 2e1fa222 2020-07-23 stsp }
2133 13d9040b 2019-03-27 stsp if (err)
2134 13d9040b 2019-03-27 stsp goto done;
2135 65b05cec 2020-07-23 stsp
2136 054041d0 2020-06-24 stsp if (ie) {
2137 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2138 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2139 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2140 054041d0 2020-06-24 stsp } else {
2141 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2142 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2143 aaf972e2 2023-09-18 thomas &blob->id, update_timestamps);
2144 054041d0 2020-06-24 stsp }
2145 13d9040b 2019-03-27 stsp if (err)
2146 13d9040b 2019-03-27 stsp goto done;
2147 65b05cec 2020-07-23 stsp
2148 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2149 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2150 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2151 2e1fa222 2020-07-23 stsp }
2152 13d9040b 2019-03-27 stsp }
2153 f4ae6ddb 2022-07-01 thomas
2154 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2155 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2156 f4ae6ddb 2022-07-01 thomas goto done;
2157 f4ae6ddb 2022-07-01 thomas }
2158 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2159 6353ad76 2019-02-08 stsp done:
2160 6353ad76 2019-02-08 stsp free(ondisk_path);
2161 8da9e5f4 2019-01-12 stsp return err;
2162 90285c3b 2019-01-08 stsp }
2163 90285c3b 2019-01-08 stsp
2164 90285c3b 2019-01-08 stsp static const struct got_error *
2165 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2166 90285c3b 2019-01-08 stsp {
2167 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2168 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2169 90285c3b 2019-01-08 stsp
2170 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2172 90285c3b 2019-01-08 stsp
2173 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2174 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2175 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2176 c97665ae 2019-01-13 stsp } else {
2177 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2178 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2179 1c4cdd89 2021-06-20 stsp if (err)
2180 1c4cdd89 2021-06-20 stsp goto done;
2181 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2182 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2183 fddefe3b 2020-10-20 stsp free(ondisk_path);
2184 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2185 1c4cdd89 2021-06-20 stsp parent = NULL;
2186 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2187 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2188 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2189 fddefe3b 2020-10-20 stsp ondisk_path);
2190 90285c3b 2019-01-08 stsp break;
2191 90285c3b 2019-01-08 stsp }
2192 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2193 1c4cdd89 2021-06-20 stsp if (err)
2194 1c4cdd89 2021-06-20 stsp break;
2195 1c4cdd89 2021-06-20 stsp }
2196 90285c3b 2019-01-08 stsp }
2197 1c4cdd89 2021-06-20 stsp done:
2198 90285c3b 2019-01-08 stsp free(ondisk_path);
2199 1c4cdd89 2021-06-20 stsp free(parent);
2200 90285c3b 2019-01-08 stsp return err;
2201 512f0d0e 2019-01-02 stsp }
2202 512f0d0e 2019-01-02 stsp
2203 708d8e67 2019-03-27 stsp static const struct got_error *
2204 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2205 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2206 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2207 708d8e67 2019-03-27 stsp {
2208 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2209 708d8e67 2019-03-27 stsp unsigned char status;
2210 708d8e67 2019-03-27 stsp struct stat sb;
2211 708d8e67 2019-03-27 stsp char *ondisk_path;
2212 708d8e67 2019-03-27 stsp
2213 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2214 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2215 a76c42e6 2019-08-03 stsp
2216 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2217 708d8e67 2019-03-27 stsp == -1)
2218 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2219 708d8e67 2019-03-27 stsp
2220 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2221 708d8e67 2019-03-27 stsp if (err)
2222 5a58a424 2020-06-23 stsp goto done;
2223 708d8e67 2019-03-27 stsp
2224 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2225 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2226 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2227 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2228 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2229 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2230 993e2a1b 2020-07-23 stsp goto done;
2231 993e2a1b 2020-07-23 stsp }
2232 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2233 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2234 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2235 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2236 993e2a1b 2020-07-23 stsp if (err)
2237 993e2a1b 2020-07-23 stsp goto done;
2238 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2239 993e2a1b 2020-07-23 stsp ie->path);
2240 993e2a1b 2020-07-23 stsp goto done;
2241 993e2a1b 2020-07-23 stsp }
2242 993e2a1b 2020-07-23 stsp
2243 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2244 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2245 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2246 1ee397ad 2019-07-12 stsp if (err)
2247 5a58a424 2020-06-23 stsp goto done;
2248 fc6346c4 2019-03-27 stsp /*
2249 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2250 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2251 fc6346c4 2019-03-27 stsp */
2252 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2253 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2254 fc6346c4 2019-03-27 stsp } else {
2255 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2256 1ee397ad 2019-07-12 stsp if (err)
2257 5a58a424 2020-06-23 stsp goto done;
2258 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2259 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2260 fc6346c4 2019-03-27 stsp if (err)
2261 5a58a424 2020-06-23 stsp goto done;
2262 fc6346c4 2019-03-27 stsp }
2263 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2264 708d8e67 2019-03-27 stsp }
2265 5a58a424 2020-06-23 stsp done:
2266 5a58a424 2020-06-23 stsp free(ondisk_path);
2267 fc6346c4 2019-03-27 stsp return err;
2268 708d8e67 2019-03-27 stsp }
2269 708d8e67 2019-03-27 stsp
2270 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2271 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2272 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2273 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2274 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2275 8da9e5f4 2019-01-12 stsp void *progress_arg;
2276 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2277 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2278 8da9e5f4 2019-01-12 stsp };
2279 8da9e5f4 2019-01-12 stsp
2280 512f0d0e 2019-01-02 stsp static const struct got_error *
2281 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2282 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2283 512f0d0e 2019-01-02 stsp {
2284 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2285 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2286 0584f854 2019-04-06 stsp
2287 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2288 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2289 eb6fb9cd 2023-07-26 thomas if (err)
2290 eb6fb9cd 2023-07-26 thomas return err;
2291 eb6fb9cd 2023-07-26 thomas }
2292 512f0d0e 2019-01-02 stsp
2293 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2294 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2295 8da9e5f4 2019-01-12 stsp }
2296 512f0d0e 2019-01-02 stsp
2297 8da9e5f4 2019-01-12 stsp static const struct got_error *
2298 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2299 8da9e5f4 2019-01-12 stsp {
2300 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2301 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2302 7a9df742 2019-01-08 stsp
2303 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2304 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2305 eb6fb9cd 2023-07-26 thomas if (err)
2306 eb6fb9cd 2023-07-26 thomas return err;
2307 eb6fb9cd 2023-07-26 thomas }
2308 0584f854 2019-04-06 stsp
2309 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2310 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2311 9d31a1d8 2018-03-11 stsp }
2312 9d31a1d8 2018-03-11 stsp
2313 9d31a1d8 2018-03-11 stsp static const struct got_error *
2314 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2315 9d31a1d8 2018-03-11 stsp {
2316 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2317 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2318 8da9e5f4 2019-01-12 stsp char *path;
2319 9d31a1d8 2018-03-11 stsp
2320 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2321 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2322 eb6fb9cd 2023-07-26 thomas if (err)
2323 eb6fb9cd 2023-07-26 thomas return err;
2324 eb6fb9cd 2023-07-26 thomas }
2325 0584f854 2019-04-06 stsp
2326 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2327 59530ea7 2023-07-17 thomas return NULL;
2328 59530ea7 2023-07-17 thomas
2329 59530ea7 2023-07-17 thomas if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2330 63c5ca5d 2019-08-24 stsp return NULL;
2331 63c5ca5d 2019-08-24 stsp
2332 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2333 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2334 8da9e5f4 2019-01-12 stsp == -1)
2335 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2336 9d31a1d8 2018-03-11 stsp
2337 59530ea7 2023-07-17 thomas err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2338 59530ea7 2023-07-17 thomas a->repo, a->progress_cb, a->progress_arg);
2339 9d31a1d8 2018-03-11 stsp
2340 8da9e5f4 2019-01-12 stsp free(path);
2341 0cd1c46a 2019-03-11 stsp return err;
2342 0cd1c46a 2019-03-11 stsp }
2343 0cd1c46a 2019-03-11 stsp
2344 b2118c49 2020-07-28 stsp const struct got_error *
2345 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2346 b2118c49 2020-07-28 stsp {
2347 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2348 b2118c49 2020-07-28 stsp
2349 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2350 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2351 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2352 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2353 b2118c49 2020-07-28 stsp }
2354 b2118c49 2020-07-28 stsp
2355 b2118c49 2020-07-28 stsp return NULL;
2356 b2118c49 2020-07-28 stsp }
2357 b2118c49 2020-07-28 stsp
2358 818c7501 2019-07-11 stsp static const struct got_error *
2359 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2360 0cd1c46a 2019-03-11 stsp {
2361 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2362 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2363 0cd1c46a 2019-03-11 stsp
2364 517bab73 2019-03-11 stsp *refname = NULL;
2365 517bab73 2019-03-11 stsp
2366 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2367 b2118c49 2020-07-28 stsp if (err)
2368 b2118c49 2020-07-28 stsp return err;
2369 0cd1c46a 2019-03-11 stsp
2370 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2371 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2372 0647c563 2019-03-11 stsp *refname = NULL;
2373 0cd1c46a 2019-03-11 stsp }
2374 517bab73 2019-03-11 stsp free(uuidstr);
2375 517bab73 2019-03-11 stsp return err;
2376 517bab73 2019-03-11 stsp }
2377 517bab73 2019-03-11 stsp
2378 818c7501 2019-07-11 stsp const struct got_error *
2379 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2380 f6cd0243 2023-01-28 thomas const char *prefix)
2381 f6cd0243 2023-01-28 thomas {
2382 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2383 f6cd0243 2023-01-28 thomas }
2384 f6cd0243 2023-01-28 thomas
2385 fcfaa6d5 2023-07-05 thomas static const struct got_error *
2386 fcfaa6d5 2023-07-05 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
2387 818c7501 2019-07-11 stsp {
2388 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2389 818c7501 2019-07-11 stsp }
2390 818c7501 2019-07-11 stsp
2391 818c7501 2019-07-11 stsp static const struct got_error *
2392 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2393 818c7501 2019-07-11 stsp {
2394 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2395 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2396 818c7501 2019-07-11 stsp }
2397 818c7501 2019-07-11 stsp
2398 818c7501 2019-07-11 stsp static const struct got_error *
2399 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2400 818c7501 2019-07-11 stsp {
2401 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2402 818c7501 2019-07-11 stsp }
2403 818c7501 2019-07-11 stsp
2404 818c7501 2019-07-11 stsp static const struct got_error *
2405 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2406 818c7501 2019-07-11 stsp {
2407 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2408 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2409 818c7501 2019-07-11 stsp }
2410 818c7501 2019-07-11 stsp
2411 818c7501 2019-07-11 stsp static const struct got_error *
2412 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2413 818c7501 2019-07-11 stsp {
2414 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2415 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2416 0ebf8283 2019-07-24 stsp }
2417 0ebf8283 2019-07-24 stsp
2418 0ebf8283 2019-07-24 stsp static const struct got_error *
2419 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2420 0ebf8283 2019-07-24 stsp {
2421 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2422 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2423 0ebf8283 2019-07-24 stsp }
2424 0ebf8283 2019-07-24 stsp
2425 0ebf8283 2019-07-24 stsp static const struct got_error *
2426 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2427 0ebf8283 2019-07-24 stsp {
2428 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2429 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2430 0ebf8283 2019-07-24 stsp }
2431 0ebf8283 2019-07-24 stsp
2432 0ebf8283 2019-07-24 stsp static const struct got_error *
2433 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2434 0ebf8283 2019-07-24 stsp {
2435 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2436 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2437 818c7501 2019-07-11 stsp }
2438 818c7501 2019-07-11 stsp
2439 0ebf8283 2019-07-24 stsp static const struct got_error *
2440 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2441 0ebf8283 2019-07-24 stsp {
2442 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2443 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2444 0ebf8283 2019-07-24 stsp }
2445 818c7501 2019-07-11 stsp
2446 0ebf8283 2019-07-24 stsp const struct got_error *
2447 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2448 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2449 0ebf8283 2019-07-24 stsp {
2450 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2451 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2452 0ebf8283 2019-07-24 stsp *path = NULL;
2453 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2454 0ebf8283 2019-07-24 stsp }
2455 0ebf8283 2019-07-24 stsp return NULL;
2456 10604dce 2021-09-24 thomas }
2457 10604dce 2021-09-24 thomas
2458 10604dce 2021-09-24 thomas static const struct got_error *
2459 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2460 10604dce 2021-09-24 thomas {
2461 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2462 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2463 0ebf8283 2019-07-24 stsp }
2464 0ebf8283 2019-07-24 stsp
2465 10604dce 2021-09-24 thomas static const struct got_error *
2466 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2467 10604dce 2021-09-24 thomas {
2468 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2469 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2470 10604dce 2021-09-24 thomas }
2471 10604dce 2021-09-24 thomas
2472 517bab73 2019-03-11 stsp /*
2473 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2474 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2475 517bab73 2019-03-11 stsp */
2476 517bab73 2019-03-11 stsp static const struct got_error *
2477 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2478 517bab73 2019-03-11 stsp {
2479 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2480 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2481 517bab73 2019-03-11 stsp char *refname;
2482 517bab73 2019-03-11 stsp
2483 fcfaa6d5 2023-07-05 thomas err = get_base_ref_name(&refname, worktree);
2484 517bab73 2019-03-11 stsp if (err)
2485 517bab73 2019-03-11 stsp return err;
2486 0cd1c46a 2019-03-11 stsp
2487 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2488 0cd1c46a 2019-03-11 stsp if (err)
2489 0cd1c46a 2019-03-11 stsp goto done;
2490 0cd1c46a 2019-03-11 stsp
2491 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2492 0cd1c46a 2019-03-11 stsp done:
2493 0cd1c46a 2019-03-11 stsp free(refname);
2494 0cd1c46a 2019-03-11 stsp if (ref)
2495 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2496 3e3a69f1 2019-07-25 stsp return err;
2497 3e3a69f1 2019-07-25 stsp }
2498 3e3a69f1 2019-07-25 stsp
2499 3e3a69f1 2019-07-25 stsp static const struct got_error *
2500 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2501 3e3a69f1 2019-07-25 stsp {
2502 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2503 3e3a69f1 2019-07-25 stsp
2504 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2505 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2506 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2507 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2508 3e3a69f1 2019-07-25 stsp }
2509 9d31a1d8 2018-03-11 stsp return err;
2510 9d31a1d8 2018-03-11 stsp }
2511 9d31a1d8 2018-03-11 stsp
2512 3e3a69f1 2019-07-25 stsp
2513 ebf99748 2019-05-09 stsp static const struct got_error *
2514 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2515 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2516 ebf99748 2019-05-09 stsp {
2517 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2518 ebf99748 2019-05-09 stsp FILE *index = NULL;
2519 0cd1c46a 2019-03-11 stsp
2520 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2521 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2522 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2523 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2524 ebf99748 2019-05-09 stsp
2525 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2526 3e3a69f1 2019-07-25 stsp if (err)
2527 ebf99748 2019-05-09 stsp goto done;
2528 ebf99748 2019-05-09 stsp
2529 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2530 ebf99748 2019-05-09 stsp if (index == NULL) {
2531 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2532 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2533 ebf99748 2019-05-09 stsp } else {
2534 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2535 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2536 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2537 ebf99748 2019-05-09 stsp }
2538 ebf99748 2019-05-09 stsp done:
2539 ebf99748 2019-05-09 stsp if (err) {
2540 ebf99748 2019-05-09 stsp free(*fileindex_path);
2541 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2542 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2543 ebf99748 2019-05-09 stsp *fileindex = NULL;
2544 ebf99748 2019-05-09 stsp }
2545 ebf99748 2019-05-09 stsp return err;
2546 ebf99748 2019-05-09 stsp }
2547 c932eeeb 2019-05-22 stsp
2548 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2549 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2550 c932eeeb 2019-05-22 stsp const char *path;
2551 c932eeeb 2019-05-22 stsp size_t path_len;
2552 c932eeeb 2019-05-22 stsp const char *entry_name;
2553 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2554 a484d721 2019-06-10 stsp void *progress_arg;
2555 c932eeeb 2019-05-22 stsp };
2556 ebf99748 2019-05-09 stsp
2557 c932eeeb 2019-05-22 stsp static const struct got_error *
2558 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2559 c932eeeb 2019-05-22 stsp {
2560 1ee397ad 2019-07-12 stsp const struct got_error *err;
2561 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2562 c932eeeb 2019-05-22 stsp
2563 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2564 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2565 c932eeeb 2019-05-22 stsp return NULL;
2566 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2567 102ff934 2019-06-11 stsp return NULL;
2568 102ff934 2019-06-11 stsp
2569 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2570 a769b60b 2021-06-27 stsp return NULL;
2571 a769b60b 2021-06-27 stsp
2572 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2573 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2574 c932eeeb 2019-05-22 stsp return NULL;
2575 c932eeeb 2019-05-22 stsp
2576 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2577 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2578 edd02c5e 2019-07-11 stsp ie->path);
2579 1ee397ad 2019-07-12 stsp if (err)
2580 1ee397ad 2019-07-12 stsp return err;
2581 1ee397ad 2019-07-12 stsp }
2582 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2583 c932eeeb 2019-05-22 stsp return NULL;
2584 a615e0e7 2020-12-16 stsp }
2585 a615e0e7 2020-12-16 stsp
2586 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2587 a615e0e7 2020-12-16 stsp static const struct got_error *
2588 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2589 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2590 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2591 a615e0e7 2020-12-16 stsp {
2592 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2593 a615e0e7 2020-12-16 stsp
2594 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2595 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2596 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2597 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2598 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2599 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2600 a615e0e7 2020-12-16 stsp
2601 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2602 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2603 9c6338c4 2019-06-02 stsp }
2604 9c6338c4 2019-06-02 stsp
2605 9c6338c4 2019-06-02 stsp static const struct got_error *
2606 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2607 9c6338c4 2019-06-02 stsp {
2608 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2609 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2610 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2611 867630bb 2020-01-17 stsp struct timespec timeout;
2612 9c6338c4 2019-06-02 stsp
2613 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2614 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2615 9c6338c4 2019-06-02 stsp if (err)
2616 9c6338c4 2019-06-02 stsp goto done;
2617 9c6338c4 2019-06-02 stsp
2618 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2619 9c6338c4 2019-06-02 stsp if (err)
2620 9c6338c4 2019-06-02 stsp goto done;
2621 9c6338c4 2019-06-02 stsp
2622 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2623 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2624 9c6338c4 2019-06-02 stsp fileindex_path);
2625 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2626 9c6338c4 2019-06-02 stsp }
2627 867630bb 2020-01-17 stsp
2628 867630bb 2020-01-17 stsp /*
2629 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2630 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2631 867630bb 2020-01-17 stsp * was recorded in the file index.
2632 867630bb 2020-01-17 stsp */
2633 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2634 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2635 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2636 9c6338c4 2019-06-02 stsp done:
2637 9c6338c4 2019-06-02 stsp if (new_index)
2638 9c6338c4 2019-06-02 stsp fclose(new_index);
2639 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2640 9c6338c4 2019-06-02 stsp return err;
2641 c932eeeb 2019-05-22 stsp }
2642 6ced4176 2019-07-12 stsp
2643 6ced4176 2019-07-12 stsp static const struct got_error *
2644 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2645 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2646 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2647 945f9229 2022-04-16 thomas struct got_repository *repo)
2648 6ced4176 2019-07-12 stsp {
2649 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2650 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2651 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2652 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2653 6ced4176 2019-07-12 stsp
2654 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2655 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2656 6ced4176 2019-07-12 stsp *tree_id = NULL;
2657 6ced4176 2019-07-12 stsp
2658 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2659 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2660 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2661 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2662 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2663 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2664 6ced4176 2019-07-12 stsp goto done;
2665 6ced4176 2019-07-12 stsp }
2666 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2667 945f9229 2022-04-16 thomas worktree->path_prefix);
2668 6ced4176 2019-07-12 stsp if (err)
2669 6ced4176 2019-07-12 stsp goto done;
2670 6ced4176 2019-07-12 stsp return NULL;
2671 6ced4176 2019-07-12 stsp }
2672 6ced4176 2019-07-12 stsp
2673 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2674 6ced4176 2019-07-12 stsp
2675 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2676 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2677 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2678 6ced4176 2019-07-12 stsp goto done;
2679 6ced4176 2019-07-12 stsp }
2680 6ced4176 2019-07-12 stsp
2681 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2682 6ced4176 2019-07-12 stsp if (err)
2683 6ced4176 2019-07-12 stsp goto done;
2684 6ced4176 2019-07-12 stsp
2685 6ced4176 2019-07-12 stsp free(in_repo_path);
2686 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2687 6ced4176 2019-07-12 stsp
2688 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2689 6ced4176 2019-07-12 stsp if (err)
2690 6ced4176 2019-07-12 stsp goto done;
2691 c932eeeb 2019-05-22 stsp
2692 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2693 6ced4176 2019-07-12 stsp /* Check out a single file. */
2694 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2695 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2696 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2697 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2698 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2699 6ced4176 2019-07-12 stsp goto done;
2700 6ced4176 2019-07-12 stsp }
2701 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2702 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2703 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2704 6ced4176 2019-07-12 stsp goto done;
2705 6ced4176 2019-07-12 stsp }
2706 6ced4176 2019-07-12 stsp } else {
2707 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2708 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2709 6ced4176 2019-07-12 stsp if (err)
2710 6ced4176 2019-07-12 stsp return err;
2711 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2712 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2713 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2714 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2715 6ced4176 2019-07-12 stsp goto done;
2716 6ced4176 2019-07-12 stsp }
2717 6ced4176 2019-07-12 stsp }
2718 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2719 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2720 6ced4176 2019-07-12 stsp } else {
2721 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2722 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2723 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2724 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2725 6ced4176 2019-07-12 stsp goto done;
2726 6ced4176 2019-07-12 stsp }
2727 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2728 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2729 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2730 6ced4176 2019-07-12 stsp goto done;
2731 6ced4176 2019-07-12 stsp }
2732 6ced4176 2019-07-12 stsp }
2733 6ced4176 2019-07-12 stsp done:
2734 6ced4176 2019-07-12 stsp free(id);
2735 6ced4176 2019-07-12 stsp free(in_repo_path);
2736 6ced4176 2019-07-12 stsp if (err) {
2737 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2738 6ced4176 2019-07-12 stsp free(*tree_relpath);
2739 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2740 6ced4176 2019-07-12 stsp free(*tree_id);
2741 6ced4176 2019-07-12 stsp *tree_id = NULL;
2742 6ced4176 2019-07-12 stsp }
2743 07ed472d 2019-07-12 stsp return err;
2744 07ed472d 2019-07-12 stsp }
2745 07ed472d 2019-07-12 stsp
2746 07ed472d 2019-07-12 stsp static const struct got_error *
2747 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2748 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2749 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2750 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2751 07ed472d 2019-07-12 stsp {
2752 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2753 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2754 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2755 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2756 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2757 07ed472d 2019-07-12 stsp
2758 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2759 7f47418f 2019-12-20 stsp if (err) {
2760 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2761 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2762 7f47418f 2019-12-20 stsp goto done;
2763 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2764 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2765 7f47418f 2019-12-20 stsp if (err)
2766 7f47418f 2019-12-20 stsp return err;
2767 7f47418f 2019-12-20 stsp }
2768 07ed472d 2019-07-12 stsp
2769 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2770 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2771 07ed472d 2019-07-12 stsp if (err)
2772 07ed472d 2019-07-12 stsp goto done;
2773 07ed472d 2019-07-12 stsp
2774 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2775 07ed472d 2019-07-12 stsp if (err)
2776 07ed472d 2019-07-12 stsp goto done;
2777 07ed472d 2019-07-12 stsp
2778 07ed472d 2019-07-12 stsp if (entry_name &&
2779 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2780 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2781 07ed472d 2019-07-12 stsp goto done;
2782 07ed472d 2019-07-12 stsp }
2783 07ed472d 2019-07-12 stsp
2784 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2785 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2786 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2787 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2788 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2789 07ed472d 2019-07-12 stsp arg.repo = repo;
2790 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2791 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2792 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2793 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2794 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2795 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2796 07ed472d 2019-07-12 stsp done:
2797 07ed472d 2019-07-12 stsp if (tree)
2798 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2799 07ed472d 2019-07-12 stsp if (commit)
2800 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2801 6ced4176 2019-07-12 stsp return err;
2802 6ced4176 2019-07-12 stsp }
2803 6ced4176 2019-07-12 stsp
2804 9d31a1d8 2018-03-11 stsp const struct got_error *
2805 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2806 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2807 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2808 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2809 9d31a1d8 2018-03-11 stsp {
2810 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2811 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2812 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2813 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2814 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2815 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2816 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2817 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2818 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2819 f2ea84fa 2019-07-27 stsp int entry_type;
2820 f2ea84fa 2019-07-27 stsp char *relpath;
2821 f2ea84fa 2019-07-27 stsp char *entry_name;
2822 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2823 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2824 9d31a1d8 2018-03-11 stsp
2825 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2826 f2ea84fa 2019-07-27 stsp
2827 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2828 9d31a1d8 2018-03-11 stsp if (err)
2829 9d31a1d8 2018-03-11 stsp return err;
2830 945f9229 2022-04-16 thomas
2831 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2832 945f9229 2022-04-16 thomas worktree->base_commit_id);
2833 945f9229 2022-04-16 thomas if (err)
2834 945f9229 2022-04-16 thomas goto done;
2835 93a30277 2018-12-24 stsp
2836 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2837 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2838 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2839 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2840 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2841 f2ea84fa 2019-07-27 stsp goto done;
2842 f2ea84fa 2019-07-27 stsp }
2843 6ced4176 2019-07-12 stsp
2844 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2845 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2846 945f9229 2022-04-16 thomas worktree, repo);
2847 f2ea84fa 2019-07-27 stsp if (err) {
2848 f2ea84fa 2019-07-27 stsp free(tpd);
2849 6ced4176 2019-07-12 stsp goto done;
2850 6ced4176 2019-07-12 stsp }
2851 f2ea84fa 2019-07-27 stsp
2852 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2853 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2854 f2ea84fa 2019-07-27 stsp if (err) {
2855 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2856 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2857 f2ea84fa 2019-07-27 stsp free(tpd);
2858 f2ea84fa 2019-07-27 stsp goto done;
2859 f2ea84fa 2019-07-27 stsp }
2860 f2ea84fa 2019-07-27 stsp } else
2861 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2862 f2ea84fa 2019-07-27 stsp
2863 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2864 6ced4176 2019-07-12 stsp }
2865 6ced4176 2019-07-12 stsp
2866 51514078 2018-12-25 stsp /*
2867 51514078 2018-12-25 stsp * Read the file index.
2868 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2869 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2870 51514078 2018-12-25 stsp */
2871 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2872 8da9e5f4 2019-01-12 stsp if (err)
2873 c4cdcb68 2019-04-03 stsp goto done;
2874 c4cdcb68 2019-04-03 stsp
2875 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2876 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2877 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2878 f2ea84fa 2019-07-27 stsp
2879 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2880 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2881 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2882 f2ea84fa 2019-07-27 stsp if (err)
2883 f2ea84fa 2019-07-27 stsp break;
2884 f2ea84fa 2019-07-27 stsp
2885 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2886 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2887 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2888 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2889 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2890 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2891 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2892 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2893 f2ea84fa 2019-07-27 stsp if (err)
2894 f2ea84fa 2019-07-27 stsp break;
2895 f2ea84fa 2019-07-27 stsp
2896 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2897 711d9cd9 2019-07-12 stsp }
2898 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2899 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2900 af12c6b9 2019-06-04 stsp err = sync_err;
2901 9d31a1d8 2018-03-11 stsp done:
2902 9c6338c4 2019-06-02 stsp free(fileindex_path);
2903 edfa7d7f 2018-09-11 stsp if (tree)
2904 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2905 9d31a1d8 2018-03-11 stsp if (commit)
2906 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2907 5ade8233 2019-07-12 stsp if (fileindex)
2908 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2909 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2910 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2911 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2912 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2913 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2914 f2ea84fa 2019-07-27 stsp free(tpd);
2915 f2ea84fa 2019-07-27 stsp }
2916 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2917 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2918 9d31a1d8 2018-03-11 stsp err = unlockerr;
2919 f8d1f275 2019-02-04 stsp return err;
2920 f8d1f275 2019-02-04 stsp }
2921 f8d1f275 2019-02-04 stsp
2922 0529f8df 2023-03-10 thomas static const struct got_error *
2923 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2924 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2925 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2926 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2927 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2928 0529f8df 2023-03-10 thomas struct got_repository *repo,
2929 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2930 0529f8df 2023-03-10 thomas {
2931 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2932 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2933 0529f8df 2023-03-10 thomas
2934 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2935 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2936 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2937 0529f8df 2023-03-10 thomas restoring_missing_file,
2938 0529f8df 2023-03-10 thomas reverting_versioned_file,
2939 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2940 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2941 0529f8df 2023-03-10 thomas } else {
2942 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2943 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2944 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2945 aaf972e2 2023-09-18 thomas path_is_unversioned, NULL, repo,
2946 aaf972e2 2023-09-18 thomas progress_cb, progress_arg);
2947 0529f8df 2023-03-10 thomas }
2948 0529f8df 2023-03-10 thomas if (err)
2949 0529f8df 2023-03-10 thomas return err;
2950 0529f8df 2023-03-10 thomas if (ie == NULL) {
2951 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2952 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2953 0529f8df 2023-03-10 thomas if (err)
2954 0529f8df 2023-03-10 thomas return err;
2955 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2956 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2957 0529f8df 2023-03-10 thomas if (err) {
2958 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2959 0529f8df 2023-03-10 thomas return err;
2960 0529f8df 2023-03-10 thomas }
2961 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2962 0529f8df 2023-03-10 thomas if (err) {
2963 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2964 0529f8df 2023-03-10 thomas return err;
2965 0529f8df 2023-03-10 thomas }
2966 0529f8df 2023-03-10 thomas } else {
2967 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2968 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2969 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2970 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2971 0529f8df 2023-03-10 thomas if (err)
2972 0529f8df 2023-03-10 thomas return err;
2973 0529f8df 2023-03-10 thomas }
2974 0529f8df 2023-03-10 thomas
2975 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2976 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2977 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2978 0529f8df 2023-03-10 thomas }
2979 0529f8df 2023-03-10 thomas
2980 0529f8df 2023-03-10 thomas return NULL;
2981 0529f8df 2023-03-10 thomas }
2982 0529f8df 2023-03-10 thomas
2983 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2984 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2985 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2986 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2987 234035bc 2019-06-01 stsp void *progress_arg;
2988 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2989 234035bc 2019-06-01 stsp void *cancel_arg;
2990 f69721c3 2019-10-21 stsp const char *label_orig;
2991 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2992 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2993 234035bc 2019-06-01 stsp };
2994 234035bc 2019-06-01 stsp
2995 234035bc 2019-06-01 stsp static const struct got_error *
2996 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2997 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2998 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2999 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3000 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3001 234035bc 2019-06-01 stsp {
3002 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
3003 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
3004 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
3005 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
3006 234035bc 2019-06-01 stsp struct stat sb;
3007 234035bc 2019-06-01 stsp unsigned char status;
3008 234035bc 2019-06-01 stsp int local_changes_subsumed;
3009 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3010 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
3011 4c3671a9 2023-07-26 thomas struct got_object_id *id = NULL;
3012 234035bc 2019-06-01 stsp
3013 234035bc 2019-06-01 stsp if (blob1 && blob2) {
3014 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3015 d6c87207 2019-08-02 stsp strlen(path2));
3016 1ee397ad 2019-07-12 stsp if (ie == NULL)
3017 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3018 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
3019 234035bc 2019-06-01 stsp
3020 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3021 234035bc 2019-06-01 stsp path2) == -1)
3022 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3023 234035bc 2019-06-01 stsp
3024 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3025 7f91a133 2019-12-13 stsp repo);
3026 234035bc 2019-06-01 stsp if (err)
3027 234035bc 2019-06-01 stsp goto done;
3028 234035bc 2019-06-01 stsp
3029 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3030 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3031 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3032 234035bc 2019-06-01 stsp goto done;
3033 234035bc 2019-06-01 stsp }
3034 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3035 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3036 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3037 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3038 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3039 234035bc 2019-06-01 stsp goto done;
3040 234035bc 2019-06-01 stsp }
3041 234035bc 2019-06-01 stsp
3042 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3043 36bf999c 2020-07-23 stsp char *link_target2;
3044 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3045 36bf999c 2020-07-23 stsp if (err)
3046 36bf999c 2020-07-23 stsp goto done;
3047 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3048 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3049 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3050 36bf999c 2020-07-23 stsp free(link_target2);
3051 af57b12a 2020-07-23 stsp } else {
3052 1af628f4 2021-06-03 stsp int fd;
3053 1af628f4 2021-06-03 stsp
3054 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3055 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3056 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3057 1af628f4 2021-06-03 stsp goto done;
3058 1af628f4 2021-06-03 stsp }
3059 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3060 1af628f4 2021-06-03 stsp f_orig, blob1);
3061 1af628f4 2021-06-03 stsp if (err)
3062 1af628f4 2021-06-03 stsp goto done;
3063 1af628f4 2021-06-03 stsp
3064 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3065 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3066 1af628f4 2021-06-03 stsp goto done;
3067 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3068 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3069 1af628f4 2021-06-03 stsp if (err)
3070 1af628f4 2021-06-03 stsp goto done;
3071 1af628f4 2021-06-03 stsp
3072 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3073 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3074 1af628f4 2021-06-03 stsp if (fd == -1) {
3075 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3076 1af628f4 2021-06-03 stsp ondisk_path);
3077 1af628f4 2021-06-03 stsp goto done;
3078 1af628f4 2021-06-03 stsp }
3079 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3080 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3081 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3082 1af628f4 2021-06-03 stsp ondisk_path);
3083 1af628f4 2021-06-03 stsp close(fd);
3084 1af628f4 2021-06-03 stsp goto done;
3085 1af628f4 2021-06-03 stsp }
3086 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3087 1af628f4 2021-06-03 stsp if (err)
3088 1af628f4 2021-06-03 stsp goto done;
3089 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3090 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3091 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3092 1af628f4 2021-06-03 stsp goto done;
3093 1af628f4 2021-06-03 stsp }
3094 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3095 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3096 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3097 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3098 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3099 af57b12a 2020-07-23 stsp }
3100 234035bc 2019-06-01 stsp } else if (blob1) {
3101 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3102 d6c87207 2019-08-02 stsp strlen(path1));
3103 1ee397ad 2019-07-12 stsp if (ie == NULL)
3104 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3105 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3106 2b92fad7 2019-06-02 stsp
3107 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3108 2b92fad7 2019-06-02 stsp path1) == -1)
3109 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3110 2b92fad7 2019-06-02 stsp
3111 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3112 7f91a133 2019-12-13 stsp repo);
3113 2b92fad7 2019-06-02 stsp if (err)
3114 2b92fad7 2019-06-02 stsp goto done;
3115 2b92fad7 2019-06-02 stsp
3116 2b92fad7 2019-06-02 stsp switch (status) {
3117 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
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 err = remove_ondisk_file(a->worktree->root_path, path1);
3123 2b92fad7 2019-06-02 stsp if (err)
3124 2b92fad7 2019-06-02 stsp goto done;
3125 2b92fad7 2019-06-02 stsp if (ie)
3126 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3127 2b92fad7 2019-06-02 stsp break;
3128 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3129 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3130 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3131 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3132 1ee397ad 2019-07-12 stsp if (err)
3133 1ee397ad 2019-07-12 stsp goto done;
3134 2b92fad7 2019-06-02 stsp if (ie)
3135 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3136 2b92fad7 2019-06-02 stsp break;
3137 4c3671a9 2023-07-26 thomas case GOT_STATUS_MODIFY: {
3138 4c3671a9 2023-07-26 thomas FILE *blob1_f;
3139 4c3671a9 2023-07-26 thomas off_t blob1_size;
3140 4c3671a9 2023-07-26 thomas int obj_type;
3141 4c3671a9 2023-07-26 thomas /*
3142 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3143 4c3671a9 2023-07-26 thomas * exists in the repository.
3144 4c3671a9 2023-07-26 thomas */
3145 4c3671a9 2023-07-26 thomas err = got_object_blob_file_create(&id, &blob1_f,
3146 4c3671a9 2023-07-26 thomas &blob1_size, path1);
3147 4c3671a9 2023-07-26 thomas if (err)
3148 4c3671a9 2023-07-26 thomas goto done;
3149 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3150 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3151 4c3671a9 2023-07-26 thomas goto done;
3152 4c3671a9 2023-07-26 thomas }
3153 4c3671a9 2023-07-26 thomas
3154 4c3671a9 2023-07-26 thomas /* Implied existence check. */
3155 4c3671a9 2023-07-26 thomas err = got_object_get_type(&obj_type, repo, id);
3156 4c3671a9 2023-07-26 thomas if (err) {
3157 4c3671a9 2023-07-26 thomas if (err->code != GOT_ERR_NO_OBJ)
3158 4c3671a9 2023-07-26 thomas goto done;
3159 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3160 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3161 4c3671a9 2023-07-26 thomas goto done;
3162 4c3671a9 2023-07-26 thomas } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3163 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3164 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3165 4c3671a9 2023-07-26 thomas goto done;
3166 4c3671a9 2023-07-26 thomas }
3167 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3168 4c3671a9 2023-07-26 thomas GOT_STATUS_DELETE, path1);
3169 4c3671a9 2023-07-26 thomas if (err)
3170 4c3671a9 2023-07-26 thomas goto done;
3171 4c3671a9 2023-07-26 thomas err = remove_ondisk_file(a->worktree->root_path,
3172 4c3671a9 2023-07-26 thomas path1);
3173 4c3671a9 2023-07-26 thomas if (err)
3174 4c3671a9 2023-07-26 thomas goto done;
3175 4c3671a9 2023-07-26 thomas if (ie)
3176 4c3671a9 2023-07-26 thomas got_fileindex_entry_mark_deleted_from_disk(ie);
3177 4c3671a9 2023-07-26 thomas break;
3178 4c3671a9 2023-07-26 thomas }
3179 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3180 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3181 e8f02263 2022-01-23 thomas off_t blob1_size;
3182 0a22ca1a 2020-09-23 stsp /*
3183 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3184 0a22ca1a 2020-09-23 stsp * exists in the repository.
3185 0a22ca1a 2020-09-23 stsp */
3186 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3187 e8f02263 2022-01-23 thomas &blob1_size, path1);
3188 0a22ca1a 2020-09-23 stsp if (err)
3189 0a22ca1a 2020-09-23 stsp goto done;
3190 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3191 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3192 4c3671a9 2023-07-26 thomas goto done;
3193 4c3671a9 2023-07-26 thomas }
3194 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3195 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3196 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3197 0a22ca1a 2020-09-23 stsp if (err)
3198 0a22ca1a 2020-09-23 stsp goto done;
3199 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3200 0a22ca1a 2020-09-23 stsp path1);
3201 0a22ca1a 2020-09-23 stsp if (err)
3202 0a22ca1a 2020-09-23 stsp goto done;
3203 0a22ca1a 2020-09-23 stsp if (ie)
3204 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3205 0a22ca1a 2020-09-23 stsp ie);
3206 0a22ca1a 2020-09-23 stsp } else {
3207 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3208 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3209 0a22ca1a 2020-09-23 stsp }
3210 0a22ca1a 2020-09-23 stsp if (err)
3211 0a22ca1a 2020-09-23 stsp goto done;
3212 0a22ca1a 2020-09-23 stsp break;
3213 0a22ca1a 2020-09-23 stsp }
3214 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3215 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3216 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3217 1ee397ad 2019-07-12 stsp if (err)
3218 1ee397ad 2019-07-12 stsp goto done;
3219 2b92fad7 2019-06-02 stsp break;
3220 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3221 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3222 1ee397ad 2019-07-12 stsp if (err)
3223 1ee397ad 2019-07-12 stsp goto done;
3224 2b92fad7 2019-06-02 stsp break;
3225 2b92fad7 2019-06-02 stsp default:
3226 2b92fad7 2019-06-02 stsp break;
3227 2b92fad7 2019-06-02 stsp }
3228 234035bc 2019-06-01 stsp } else if (blob2) {
3229 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3230 234035bc 2019-06-01 stsp path2) == -1)
3231 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3232 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3233 d6c87207 2019-08-02 stsp strlen(path2));
3234 234035bc 2019-06-01 stsp if (ie) {
3235 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3236 7f91a133 2019-12-13 stsp -1, NULL, repo);
3237 234035bc 2019-06-01 stsp if (err)
3238 234035bc 2019-06-01 stsp goto done;
3239 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3240 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3241 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3242 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3243 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3244 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3245 1ee397ad 2019-07-12 stsp status, path2);
3246 234035bc 2019-06-01 stsp goto done;
3247 234035bc 2019-06-01 stsp }
3248 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3249 36bf999c 2020-07-23 stsp char *link_target2;
3250 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3251 36bf999c 2020-07-23 stsp blob2);
3252 36bf999c 2020-07-23 stsp if (err)
3253 36bf999c 2020-07-23 stsp goto done;
3254 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3255 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3256 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3257 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3258 36bf999c 2020-07-23 stsp free(link_target2);
3259 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3260 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3261 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3262 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3263 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3264 526a746f 2020-07-23 stsp a->progress_arg);
3265 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3266 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3267 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3268 526a746f 2020-07-23 stsp }
3269 dfe9fba0 2020-07-23 stsp if (err)
3270 dfe9fba0 2020-07-23 stsp goto done;
3271 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3272 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3273 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3274 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3275 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3276 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3277 234035bc 2019-06-01 stsp if (err)
3278 234035bc 2019-06-01 stsp goto done;
3279 234035bc 2019-06-01 stsp }
3280 234035bc 2019-06-01 stsp } else {
3281 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3282 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3283 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3284 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3285 234035bc 2019-06-01 stsp if (err)
3286 2b92fad7 2019-06-02 stsp goto done;
3287 234035bc 2019-06-01 stsp }
3288 234035bc 2019-06-01 stsp }
3289 234035bc 2019-06-01 stsp done:
3290 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3291 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3292 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3293 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3294 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3295 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3296 1af628f4 2021-06-03 stsp free(id_str);
3297 4c3671a9 2023-07-26 thomas free(id);
3298 54d5be07 2021-06-03 stsp free(label_deriv2);
3299 234035bc 2019-06-01 stsp free(ondisk_path);
3300 234035bc 2019-06-01 stsp return err;
3301 234035bc 2019-06-01 stsp }
3302 6ebb2263 2023-07-26 thomas
3303 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args {
3304 6ebb2263 2023-07-26 thomas struct got_worktree *worktree;
3305 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb;
3306 6ebb2263 2023-07-26 thomas void *cancel_arg;
3307 6ebb2263 2023-07-26 thomas };
3308 234035bc 2019-06-01 stsp
3309 69de9dd4 2021-09-03 stsp static const struct got_error *
3310 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3311 69de9dd4 2021-09-03 stsp {
3312 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
3313 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args *a = arg;
3314 6ebb2263 2023-07-26 thomas
3315 6ebb2263 2023-07-26 thomas if (a->cancel_cb) {
3316 6ebb2263 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3317 6ebb2263 2023-07-26 thomas if (err)
3318 6ebb2263 2023-07-26 thomas return err;
3319 6ebb2263 2023-07-26 thomas }
3320 69de9dd4 2021-09-03 stsp
3321 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3322 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3323 6ebb2263 2023-07-26 thomas memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3324 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3325 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3326 69de9dd4 2021-09-03 stsp
3327 69de9dd4 2021-09-03 stsp return NULL;
3328 69de9dd4 2021-09-03 stsp }
3329 69de9dd4 2021-09-03 stsp
3330 349dfd1e 2023-07-23 thomas const struct got_error *
3331 349dfd1e 2023-07-23 thomas got_worktree_get_state(char *state, struct got_repository *repo,
3332 6ebb2263 2023-07-26 thomas struct got_worktree *worktree,
3333 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3334 349dfd1e 2023-07-23 thomas {
3335 349dfd1e 2023-07-23 thomas const struct got_error *err;
3336 349dfd1e 2023-07-23 thomas struct got_object_id *base_id, *head_id = NULL;
3337 349dfd1e 2023-07-23 thomas struct got_reference *head_ref;
3338 349dfd1e 2023-07-23 thomas struct got_fileindex *fileindex = NULL;
3339 349dfd1e 2023-07-23 thomas char *fileindex_path = NULL;
3340 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3341 349dfd1e 2023-07-23 thomas
3342 349dfd1e 2023-07-23 thomas if (worktree == NULL)
3343 349dfd1e 2023-07-23 thomas return got_error(GOT_ERR_NOT_WORKTREE);
3344 349dfd1e 2023-07-23 thomas
3345 349dfd1e 2023-07-23 thomas err = got_ref_open(&head_ref, repo,
3346 349dfd1e 2023-07-23 thomas got_worktree_get_head_ref_name(worktree), 0);
3347 349dfd1e 2023-07-23 thomas if (err)
3348 349dfd1e 2023-07-23 thomas return err;
3349 349dfd1e 2023-07-23 thomas
3350 349dfd1e 2023-07-23 thomas err = got_ref_resolve(&head_id, repo, head_ref);
3351 349dfd1e 2023-07-23 thomas if (err)
3352 349dfd1e 2023-07-23 thomas goto done;
3353 349dfd1e 2023-07-23 thomas
3354 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UNKNOWN;
3355 349dfd1e 2023-07-23 thomas base_id = got_worktree_get_base_commit_id(worktree);
3356 349dfd1e 2023-07-23 thomas
3357 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3358 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3359 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3360 6ebb2263 2023-07-26 thomas
3361 349dfd1e 2023-07-23 thomas if (got_object_id_cmp(base_id, head_id) == 0) {
3362 349dfd1e 2023-07-23 thomas err = open_fileindex(&fileindex, &fileindex_path, worktree);
3363 349dfd1e 2023-07-23 thomas if (err)
3364 349dfd1e 2023-07-23 thomas goto done;
3365 349dfd1e 2023-07-23 thomas
3366 349dfd1e 2023-07-23 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3367 6ebb2263 2023-07-26 thomas check_mixed_commits, &cma);
3368 349dfd1e 2023-07-23 thomas if (err == NULL)
3369 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UPTODATE;
3370 92845f09 2023-07-26 thomas else if (err->code == GOT_ERR_MIXED_COMMITS) {
3371 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3372 349dfd1e 2023-07-23 thomas err = NULL;
3373 92845f09 2023-07-26 thomas }
3374 92845f09 2023-07-26 thomas } else
3375 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3376 349dfd1e 2023-07-23 thomas
3377 349dfd1e 2023-07-23 thomas done:
3378 349dfd1e 2023-07-23 thomas free(head_id);
3379 349dfd1e 2023-07-23 thomas free(fileindex_path);
3380 349dfd1e 2023-07-23 thomas got_ref_close(head_ref);
3381 349dfd1e 2023-07-23 thomas if (fileindex != NULL)
3382 349dfd1e 2023-07-23 thomas got_fileindex_free(fileindex);
3383 349dfd1e 2023-07-23 thomas return err;
3384 349dfd1e 2023-07-23 thomas }
3385 349dfd1e 2023-07-23 thomas
3386 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3387 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3388 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3389 234035bc 2019-06-01 stsp struct got_repository *repo;
3390 234035bc 2019-06-01 stsp };
3391 234035bc 2019-06-01 stsp
3392 234035bc 2019-06-01 stsp static const struct got_error *
3393 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3394 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3395 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3396 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3397 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3398 234035bc 2019-06-01 stsp {
3399 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3400 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3401 234035bc 2019-06-01 stsp unsigned char status;
3402 234035bc 2019-06-01 stsp struct stat sb;
3403 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3404 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3405 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3406 234035bc 2019-06-01 stsp char *ondisk_path;
3407 234035bc 2019-06-01 stsp
3408 69de9dd4 2021-09-03 stsp if (id == NULL)
3409 69de9dd4 2021-09-03 stsp return NULL;
3410 234035bc 2019-06-01 stsp
3411 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3412 69de9dd4 2021-09-03 stsp if (ie == NULL)
3413 69de9dd4 2021-09-03 stsp return NULL;
3414 69de9dd4 2021-09-03 stsp
3415 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3416 234035bc 2019-06-01 stsp == -1)
3417 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3418 234035bc 2019-06-01 stsp
3419 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3420 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3421 5546d466 2021-09-02 stsp free(ondisk_path);
3422 234035bc 2019-06-01 stsp if (err)
3423 234035bc 2019-06-01 stsp return err;
3424 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3425 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3426 234035bc 2019-06-01 stsp
3427 234035bc 2019-06-01 stsp return NULL;
3428 234035bc 2019-06-01 stsp }
3429 234035bc 2019-06-01 stsp
3430 818c7501 2019-07-11 stsp static const struct got_error *
3431 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3432 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3433 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3434 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3435 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3436 234035bc 2019-06-01 stsp {
3437 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3438 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3439 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3440 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3441 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3442 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3443 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3444 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3445 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3446 234035bc 2019-06-01 stsp
3447 03415a1a 2019-06-02 stsp if (commit_id1) {
3448 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3449 945f9229 2022-04-16 thomas if (err)
3450 945f9229 2022-04-16 thomas goto done;
3451 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3452 03415a1a 2019-06-02 stsp worktree->path_prefix);
3453 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3454 03415a1a 2019-06-02 stsp goto done;
3455 69d57f3d 2020-07-31 stsp }
3456 69d57f3d 2020-07-31 stsp if (tree_id1) {
3457 69d57f3d 2020-07-31 stsp char *id_str;
3458 03415a1a 2019-06-02 stsp
3459 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3460 03415a1a 2019-06-02 stsp if (err)
3461 03415a1a 2019-06-02 stsp goto done;
3462 f69721c3 2019-10-21 stsp
3463 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3464 f69721c3 2019-10-21 stsp if (err)
3465 f69721c3 2019-10-21 stsp goto done;
3466 f69721c3 2019-10-21 stsp
3467 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3468 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3469 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3470 f69721c3 2019-10-21 stsp free(id_str);
3471 f69721c3 2019-10-21 stsp goto done;
3472 f69721c3 2019-10-21 stsp }
3473 f69721c3 2019-10-21 stsp free(id_str);
3474 a0f32f33 2022-06-13 thomas
3475 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3476 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3477 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3478 a0f32f33 2022-06-13 thomas goto done;
3479 a0f32f33 2022-06-13 thomas }
3480 03415a1a 2019-06-02 stsp }
3481 234035bc 2019-06-01 stsp
3482 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3483 945f9229 2022-04-16 thomas if (err)
3484 945f9229 2022-04-16 thomas goto done;
3485 945f9229 2022-04-16 thomas
3486 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3487 234035bc 2019-06-01 stsp worktree->path_prefix);
3488 234035bc 2019-06-01 stsp if (err)
3489 234035bc 2019-06-01 stsp goto done;
3490 234035bc 2019-06-01 stsp
3491 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3492 234035bc 2019-06-01 stsp if (err)
3493 234035bc 2019-06-01 stsp goto done;
3494 234035bc 2019-06-01 stsp
3495 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3496 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3497 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3498 19a6a6b5 2022-07-01 thomas goto done;
3499 19a6a6b5 2022-07-01 thomas }
3500 19a6a6b5 2022-07-01 thomas
3501 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3502 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3503 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3504 a0f32f33 2022-06-13 thomas goto done;
3505 a0f32f33 2022-06-13 thomas }
3506 a0f32f33 2022-06-13 thomas
3507 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3508 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3509 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3510 19a6a6b5 2022-07-01 thomas goto done;
3511 19a6a6b5 2022-07-01 thomas }
3512 19a6a6b5 2022-07-01 thomas
3513 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3514 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3515 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3516 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3517 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3518 69de9dd4 2021-09-03 stsp if (err)
3519 69de9dd4 2021-09-03 stsp goto done;
3520 69de9dd4 2021-09-03 stsp
3521 234035bc 2019-06-01 stsp arg.worktree = worktree;
3522 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3523 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3524 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3525 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3526 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3527 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3528 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3529 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3530 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3531 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3532 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3533 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3534 af12c6b9 2019-06-04 stsp err = sync_err;
3535 234035bc 2019-06-01 stsp done:
3536 945f9229 2022-04-16 thomas if (commit1)
3537 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3538 945f9229 2022-04-16 thomas if (commit2)
3539 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3540 234035bc 2019-06-01 stsp if (tree1)
3541 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3542 234035bc 2019-06-01 stsp if (tree2)
3543 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3544 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3545 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3546 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3547 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3548 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3549 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3550 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3551 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3552 f69721c3 2019-10-21 stsp free(label_orig);
3553 818c7501 2019-07-11 stsp return err;
3554 818c7501 2019-07-11 stsp }
3555 234035bc 2019-06-01 stsp
3556 818c7501 2019-07-11 stsp const struct got_error *
3557 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3558 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3559 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3560 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3561 818c7501 2019-07-11 stsp {
3562 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3563 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3564 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3565 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3566 818c7501 2019-07-11 stsp
3567 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3568 818c7501 2019-07-11 stsp if (err)
3569 818c7501 2019-07-11 stsp return err;
3570 818c7501 2019-07-11 stsp
3571 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3572 818c7501 2019-07-11 stsp if (err)
3573 818c7501 2019-07-11 stsp goto done;
3574 818c7501 2019-07-11 stsp
3575 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3576 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3577 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3578 6ebb2263 2023-07-26 thomas
3579 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3580 6ebb2263 2023-07-26 thomas &cma);
3581 818c7501 2019-07-11 stsp if (err)
3582 818c7501 2019-07-11 stsp goto done;
3583 818c7501 2019-07-11 stsp
3584 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3585 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3586 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3587 818c7501 2019-07-11 stsp done:
3588 818c7501 2019-07-11 stsp if (fileindex)
3589 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3590 818c7501 2019-07-11 stsp free(fileindex_path);
3591 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3592 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3593 234035bc 2019-06-01 stsp err = unlockerr;
3594 234035bc 2019-06-01 stsp return err;
3595 234035bc 2019-06-01 stsp }
3596 234035bc 2019-06-01 stsp
3597 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3598 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3599 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3600 927df6b7 2019-02-10 stsp const char *status_path;
3601 927df6b7 2019-02-10 stsp size_t status_path_len;
3602 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3603 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3604 f8d1f275 2019-02-04 stsp void *status_arg;
3605 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3606 f8d1f275 2019-02-04 stsp void *cancel_arg;
3607 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3608 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3609 f2a9dc41 2019-12-13 tracey int report_unchanged;
3610 3143d852 2020-06-25 stsp int no_ignores;
3611 f8d1f275 2019-02-04 stsp };
3612 88d0e355 2019-08-03 stsp
3613 f8d1f275 2019-02-04 stsp static const struct got_error *
3614 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3615 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3616 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3617 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3618 927df6b7 2019-02-10 stsp {
3619 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3620 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3621 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3622 927df6b7 2019-02-10 stsp struct stat sb;
3623 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3624 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3625 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3626 927df6b7 2019-02-10 stsp
3627 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3628 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3629 98eaaa12 2019-08-03 stsp if (err)
3630 98eaaa12 2019-08-03 stsp return err;
3631 98eaaa12 2019-08-03 stsp
3632 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3633 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3634 98eaaa12 2019-08-03 stsp return NULL;
3635 98eaaa12 2019-08-03 stsp
3636 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3637 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3638 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3639 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3640 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3641 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3642 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3643 43010591 2023-02-17 thomas &staged_blob_id, ie);
3644 98eaaa12 2019-08-03 stsp }
3645 98eaaa12 2019-08-03 stsp
3646 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3647 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3648 927df6b7 2019-02-10 stsp }
3649 927df6b7 2019-02-10 stsp
3650 927df6b7 2019-02-10 stsp static const struct got_error *
3651 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3652 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3653 f8d1f275 2019-02-04 stsp {
3654 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3655 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3656 f8d1f275 2019-02-04 stsp char *abspath;
3657 f8d1f275 2019-02-04 stsp
3658 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3659 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3660 eb6fb9cd 2023-07-26 thomas if (err)
3661 eb6fb9cd 2023-07-26 thomas return err;
3662 eb6fb9cd 2023-07-26 thomas }
3663 0584f854 2019-04-06 stsp
3664 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3665 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3666 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3667 927df6b7 2019-02-10 stsp return NULL;
3668 927df6b7 2019-02-10 stsp
3669 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3670 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3671 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3672 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3673 f8d1f275 2019-02-04 stsp } else {
3674 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3675 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3676 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3677 f8d1f275 2019-02-04 stsp }
3678 f8d1f275 2019-02-04 stsp
3679 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3680 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3681 f8d1f275 2019-02-04 stsp free(abspath);
3682 9d31a1d8 2018-03-11 stsp return err;
3683 9d31a1d8 2018-03-11 stsp }
3684 f8d1f275 2019-02-04 stsp
3685 f8d1f275 2019-02-04 stsp static const struct got_error *
3686 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3687 f8d1f275 2019-02-04 stsp {
3688 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
3689 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3690 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3691 2ec1f75b 2019-03-26 stsp unsigned char status;
3692 927df6b7 2019-02-10 stsp
3693 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3694 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3695 eb6fb9cd 2023-07-26 thomas if (err)
3696 eb6fb9cd 2023-07-26 thomas return err;
3697 eb6fb9cd 2023-07-26 thomas }
3698 0584f854 2019-04-06 stsp
3699 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3700 927df6b7 2019-02-10 stsp return NULL;
3701 927df6b7 2019-02-10 stsp
3702 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3703 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3704 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3705 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3706 2ec1f75b 2019-03-26 stsp else
3707 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3708 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3709 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3710 6841da00 2019-08-08 stsp }
3711 6841da00 2019-08-08 stsp
3712 ef20f542 2022-06-26 thomas static void
3713 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3714 6841da00 2019-08-08 stsp {
3715 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3716 6841da00 2019-08-08 stsp
3717 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3718 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3719 21c2d8be 2023-01-10 thomas
3720 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3721 6841da00 2019-08-08 stsp }
3722 5f42e012 2023-12-09 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3723 6841da00 2019-08-08 stsp }
3724 6841da00 2019-08-08 stsp
3725 6841da00 2019-08-08 stsp static const struct got_error *
3726 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3727 6841da00 2019-08-08 stsp {
3728 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3729 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3730 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3731 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3732 6841da00 2019-08-08 stsp size_t linesize = 0;
3733 6841da00 2019-08-08 stsp ssize_t linelen;
3734 6841da00 2019-08-08 stsp
3735 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3736 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3737 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3738 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3739 6841da00 2019-08-08 stsp
3740 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3741 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3742 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3743 bd8de430 2019-10-04 stsp
3744 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3745 bd8de430 2019-10-04 stsp if (line[0] == '#')
3746 bd8de430 2019-10-04 stsp continue;
3747 bd8de430 2019-10-04 stsp
3748 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3749 bd8de430 2019-10-04 stsp if (line[0] == '!')
3750 bd8de430 2019-10-04 stsp continue;
3751 bd8de430 2019-10-04 stsp
3752 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3753 6841da00 2019-08-08 stsp line) == -1) {
3754 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3755 6841da00 2019-08-08 stsp goto done;
3756 6841da00 2019-08-08 stsp }
3757 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3758 6841da00 2019-08-08 stsp if (err)
3759 6841da00 2019-08-08 stsp goto done;
3760 6841da00 2019-08-08 stsp }
3761 6841da00 2019-08-08 stsp if (ferror(f)) {
3762 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3763 6841da00 2019-08-08 stsp goto done;
3764 6841da00 2019-08-08 stsp }
3765 6841da00 2019-08-08 stsp
3766 6841da00 2019-08-08 stsp dirpath = strdup(path);
3767 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3768 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3769 6841da00 2019-08-08 stsp goto done;
3770 6841da00 2019-08-08 stsp }
3771 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3772 6841da00 2019-08-08 stsp done:
3773 6841da00 2019-08-08 stsp free(line);
3774 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3775 6841da00 2019-08-08 stsp free(dirpath);
3776 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3777 5f42e012 2023-12-09 thomas free(ignorelist);
3778 6841da00 2019-08-08 stsp }
3779 6841da00 2019-08-08 stsp return err;
3780 1b5d300f 2023-02-20 thomas }
3781 1b5d300f 2023-02-20 thomas
3782 1b5d300f 2023-02-20 thomas static int
3783 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3784 1b5d300f 2023-02-20 thomas int flags)
3785 1b5d300f 2023-02-20 thomas {
3786 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3787 1b5d300f 2023-02-20 thomas
3788 1b5d300f 2023-02-20 thomas /*
3789 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3790 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3791 1b5d300f 2023-02-20 thomas */
3792 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3793 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3794 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3795 1b5d300f 2023-02-20 thomas
3796 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3797 1b5d300f 2023-02-20 thomas }
3798 1b5d300f 2023-02-20 thomas
3799 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3800 6841da00 2019-08-08 stsp }
3801 6841da00 2019-08-08 stsp
3802 ef20f542 2022-06-26 thomas static int
3803 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3804 6841da00 2019-08-08 stsp {
3805 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3806 bd8de430 2019-10-04 stsp
3807 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3808 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3809 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3810 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3811 bd8de430 2019-10-04 stsp
3812 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3813 1b5d300f 2023-02-20 thomas const char *p;
3814 6841da00 2019-08-08 stsp
3815 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3816 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3817 bd8de430 2019-10-04 stsp continue;
3818 bd8de430 2019-10-04 stsp p = path;
3819 bd8de430 2019-10-04 stsp while (*p) {
3820 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3821 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3822 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3823 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3824 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3825 bd8de430 2019-10-04 stsp p++;
3826 bd8de430 2019-10-04 stsp while (*p == '/')
3827 bd8de430 2019-10-04 stsp p++;
3828 bd8de430 2019-10-04 stsp continue;
3829 bd8de430 2019-10-04 stsp }
3830 bd8de430 2019-10-04 stsp return 1;
3831 bd8de430 2019-10-04 stsp }
3832 bd8de430 2019-10-04 stsp }
3833 bd8de430 2019-10-04 stsp }
3834 bd8de430 2019-10-04 stsp
3835 6841da00 2019-08-08 stsp /*
3836 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3837 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3838 6841da00 2019-08-08 stsp * ignores backwards.
3839 6841da00 2019-08-08 stsp */
3840 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3841 6841da00 2019-08-08 stsp while (pe) {
3842 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3843 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3844 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3845 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3846 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3847 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3848 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3849 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3850 1b5d300f 2023-02-20 thomas path, flags))
3851 6841da00 2019-08-08 stsp continue;
3852 6841da00 2019-08-08 stsp return 1;
3853 6841da00 2019-08-08 stsp }
3854 6841da00 2019-08-08 stsp }
3855 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3856 6841da00 2019-08-08 stsp }
3857 6841da00 2019-08-08 stsp
3858 6841da00 2019-08-08 stsp return 0;
3859 6841da00 2019-08-08 stsp }
3860 6841da00 2019-08-08 stsp
3861 6841da00 2019-08-08 stsp static const struct got_error *
3862 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3863 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3864 6841da00 2019-08-08 stsp {
3865 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3866 6841da00 2019-08-08 stsp char *ignorespath;
3867 886cec17 2019-12-15 stsp int fd = -1;
3868 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3869 6841da00 2019-08-08 stsp
3870 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3871 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3872 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3873 6841da00 2019-08-08 stsp
3874 886cec17 2019-12-15 stsp if (dirfd != -1) {
3875 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3876 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3877 886cec17 2019-12-15 stsp if (fd == -1) {
3878 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3879 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3880 886cec17 2019-12-15 stsp ignorespath);
3881 886cec17 2019-12-15 stsp } else {
3882 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3883 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3884 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3885 886cec17 2019-12-15 stsp ignorespath);
3886 886cec17 2019-12-15 stsp else {
3887 886cec17 2019-12-15 stsp fd = -1;
3888 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3889 886cec17 2019-12-15 stsp }
3890 886cec17 2019-12-15 stsp }
3891 886cec17 2019-12-15 stsp } else {
3892 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3893 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3894 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3895 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3896 886cec17 2019-12-15 stsp ignorespath);
3897 886cec17 2019-12-15 stsp } else
3898 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3899 886cec17 2019-12-15 stsp }
3900 6841da00 2019-08-08 stsp
3901 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3902 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3903 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3904 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3905 6841da00 2019-08-08 stsp free(ignorespath);
3906 6841da00 2019-08-08 stsp return err;
3907 f8d1f275 2019-02-04 stsp }
3908 f8d1f275 2019-02-04 stsp
3909 f8d1f275 2019-02-04 stsp static const struct got_error *
3910 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3911 6092c299 2021-10-04 thomas int dirfd)
3912 f8d1f275 2019-02-04 stsp {
3913 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3914 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3915 f8d1f275 2019-02-04 stsp char *path = NULL;
3916 6092c299 2021-10-04 thomas
3917 6092c299 2021-10-04 thomas if (ignore != NULL)
3918 6092c299 2021-10-04 thomas *ignore = 0;
3919 f8d1f275 2019-02-04 stsp
3920 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3921 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3922 eb6fb9cd 2023-07-26 thomas if (err)
3923 eb6fb9cd 2023-07-26 thomas return err;
3924 eb6fb9cd 2023-07-26 thomas }
3925 0584f854 2019-04-06 stsp
3926 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3927 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3928 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3929 f8d1f275 2019-02-04 stsp } else {
3930 f8d1f275 2019-02-04 stsp path = de->d_name;
3931 f8d1f275 2019-02-04 stsp }
3932 f8d1f275 2019-02-04 stsp
3933 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3934 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3935 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3936 6092c299 2021-10-04 thomas *ignore = 1;
3937 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3938 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3939 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3940 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3941 f8d1f275 2019-02-04 stsp if (parent_path[0])
3942 f8d1f275 2019-02-04 stsp free(path);
3943 b72f483a 2019-02-05 stsp return err;
3944 f8d1f275 2019-02-04 stsp }
3945 f8d1f275 2019-02-04 stsp
3946 347d1d3e 2019-07-12 stsp static const struct got_error *
3947 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3948 3143d852 2020-06-25 stsp {
3949 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3950 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3951 3143d852 2020-06-25 stsp
3952 3143d852 2020-06-25 stsp if (a->no_ignores)
3953 3143d852 2020-06-25 stsp return NULL;
3954 3143d852 2020-06-25 stsp
3955 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3956 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3957 3143d852 2020-06-25 stsp if (err)
3958 3143d852 2020-06-25 stsp return err;
3959 3143d852 2020-06-25 stsp
3960 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3961 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3962 3143d852 2020-06-25 stsp
3963 3143d852 2020-06-25 stsp return err;
3964 3143d852 2020-06-25 stsp }
3965 3143d852 2020-06-25 stsp
3966 3143d852 2020-06-25 stsp static const struct got_error *
3967 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3968 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3969 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3970 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3971 abb4604f 2019-07-27 stsp {
3972 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3973 abb4604f 2019-07-27 stsp struct stat sb;
3974 ff56836b 2021-07-08 stsp
3975 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3976 abb4604f 2019-07-27 stsp if (ie)
3977 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3978 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3979 abb4604f 2019-07-27 stsp
3980 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3981 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3982 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3983 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3984 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3985 abb4604f 2019-07-27 stsp }
3986 abb4604f 2019-07-27 stsp
3987 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3988 fe1d8685 2022-02-12 thomas return NULL;
3989 fe1d8685 2022-02-12 thomas
3990 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3991 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3992 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3993 abb4604f 2019-07-27 stsp
3994 abb4604f 2019-07-27 stsp return NULL;
3995 3143d852 2020-06-25 stsp }
3996 3143d852 2020-06-25 stsp
3997 3143d852 2020-06-25 stsp static const struct got_error *
3998 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3999 3143d852 2020-06-25 stsp const char *root_path, const char *path)
4000 3143d852 2020-06-25 stsp {
4001 3143d852 2020-06-25 stsp const struct got_error *err;
4002 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
4003 3143d852 2020-06-25 stsp
4004 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4005 3143d852 2020-06-25 stsp ".cvsignore");
4006 3143d852 2020-06-25 stsp if (err)
4007 3143d852 2020-06-25 stsp return err;
4008 3143d852 2020-06-25 stsp
4009 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4010 3143d852 2020-06-25 stsp ".gitignore");
4011 3143d852 2020-06-25 stsp if (err)
4012 3143d852 2020-06-25 stsp return err;
4013 3143d852 2020-06-25 stsp
4014 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
4015 3143d852 2020-06-25 stsp if (err) {
4016 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
4017 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
4018 3143d852 2020-06-25 stsp return err;
4019 3143d852 2020-06-25 stsp }
4020 3143d852 2020-06-25 stsp for (;;) {
4021 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4022 3143d852 2020-06-25 stsp ".cvsignore");
4023 3143d852 2020-06-25 stsp if (err)
4024 3143d852 2020-06-25 stsp break;
4025 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4026 3143d852 2020-06-25 stsp ".gitignore");
4027 3143d852 2020-06-25 stsp if (err)
4028 3143d852 2020-06-25 stsp break;
4029 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
4030 3143d852 2020-06-25 stsp if (err) {
4031 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
4032 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
4033 3143d852 2020-06-25 stsp break;
4034 3143d852 2020-06-25 stsp }
4035 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
4036 ff56836b 2021-07-08 stsp break;
4037 b737c85a 2020-06-26 stsp free(parent_path);
4038 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
4039 b737c85a 2020-06-26 stsp next_parent_path = NULL;
4040 3143d852 2020-06-25 stsp }
4041 3143d852 2020-06-25 stsp
4042 b737c85a 2020-06-26 stsp free(parent_path);
4043 b737c85a 2020-06-26 stsp free(next_parent_path);
4044 3143d852 2020-06-25 stsp return err;
4045 abb4604f 2019-07-27 stsp }
4046 f8836425 2023-04-14 thomas
4047 f8836425 2023-04-14 thomas struct find_missing_children_args {
4048 f8836425 2023-04-14 thomas const char *parent_path;
4049 f8836425 2023-04-14 thomas size_t parent_len;
4050 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
4051 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
4052 f8836425 2023-04-14 thomas void *cancel_arg;
4053 f8836425 2023-04-14 thomas };
4054 f8836425 2023-04-14 thomas
4055 abb4604f 2019-07-27 stsp static const struct got_error *
4056 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
4057 f8836425 2023-04-14 thomas {
4058 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4059 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
4060 f8836425 2023-04-14 thomas
4061 f8836425 2023-04-14 thomas if (a->cancel_cb) {
4062 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
4063 f8836425 2023-04-14 thomas if (err)
4064 f8836425 2023-04-14 thomas return err;
4065 f8836425 2023-04-14 thomas }
4066 f8836425 2023-04-14 thomas
4067 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4068 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
4069 f8836425 2023-04-14 thomas
4070 f8836425 2023-04-14 thomas return err;
4071 f8836425 2023-04-14 thomas }
4072 f8836425 2023-04-14 thomas
4073 f8836425 2023-04-14 thomas static const struct got_error *
4074 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
4075 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
4076 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
4077 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
4078 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
4079 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
4080 f8836425 2023-04-14 thomas {
4081 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4082 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
4083 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
4084 f8836425 2023-04-14 thomas
4085 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
4086 f8836425 2023-04-14 thomas if (cancel_cb) {
4087 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
4088 f8836425 2023-04-14 thomas if (err)
4089 f8836425 2023-04-14 thomas break;
4090 f8836425 2023-04-14 thomas }
4091 f8836425 2023-04-14 thomas
4092 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4093 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
4094 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
4095 f8836425 2023-04-14 thomas ondisk_path = NULL;
4096 f8836425 2023-04-14 thomas break;
4097 f8836425 2023-04-14 thomas }
4098 f8836425 2023-04-14 thomas
4099 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
4100 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
4101 f8836425 2023-04-14 thomas ignores, no_ignores);
4102 f8836425 2023-04-14 thomas if (err)
4103 f8836425 2023-04-14 thomas break;
4104 f8836425 2023-04-14 thomas
4105 f8836425 2023-04-14 thomas free(ondisk_path);
4106 f8836425 2023-04-14 thomas ondisk_path = NULL;
4107 f8836425 2023-04-14 thomas }
4108 f8836425 2023-04-14 thomas
4109 f8836425 2023-04-14 thomas free(ondisk_path);
4110 f8836425 2023-04-14 thomas return err;
4111 f8836425 2023-04-14 thomas }
4112 f8836425 2023-04-14 thomas
4113 f8836425 2023-04-14 thomas static const struct got_error *
4114 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4115 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4116 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4117 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4118 f2a9dc41 2019-12-13 tracey int report_unchanged)
4119 f8d1f275 2019-02-04 stsp {
4120 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4121 6fc93f37 2019-12-13 stsp int fd = -1;
4122 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4123 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4124 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4125 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
4126 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
4127 3143d852 2020-06-25 stsp
4128 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4129 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
4130 f8d1f275 2019-02-04 stsp
4131 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4132 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4133 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4134 8dc303cc 2019-07-27 stsp
4135 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4136 a78810f8 2022-03-13 thomas if (ie) {
4137 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
4138 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
4139 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
4140 a78810f8 2022-03-13 thomas goto done;
4141 f8836425 2023-04-14 thomas } else {
4142 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
4143 f8836425 2023-04-14 thomas fmca.parent_path = path;
4144 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
4145 f8836425 2023-04-14 thomas fmca.children = &missing_children;
4146 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
4147 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
4148 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
4149 f8836425 2023-04-14 thomas find_missing_children, &fmca);
4150 f8836425 2023-04-14 thomas if (err)
4151 f8836425 2023-04-14 thomas goto done;
4152 a78810f8 2022-03-13 thomas }
4153 a78810f8 2022-03-13 thomas
4154 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4155 6fc93f37 2019-12-13 stsp if (fd == -1) {
4156 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4157 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
4158 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4159 ff56836b 2021-07-08 stsp else {
4160 ff56836b 2021-07-08 stsp if (!no_ignores) {
4161 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4162 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4163 ff56836b 2021-07-08 stsp if (err)
4164 ff56836b 2021-07-08 stsp goto done;
4165 ff56836b 2021-07-08 stsp }
4166 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4167 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4168 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4169 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4170 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4171 f8836425 2023-04-14 thomas if (err)
4172 f8836425 2023-04-14 thomas goto done;
4173 f8836425 2023-04-14 thomas } else {
4174 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4175 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4176 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4177 f8836425 2023-04-14 thomas &ignores, no_ignores,
4178 f8836425 2023-04-14 thomas status_cb, status_arg,
4179 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4180 6f97aa83 2023-04-14 thomas if (err)
4181 6f97aa83 2023-04-14 thomas goto done;
4182 f8836425 2023-04-14 thomas }
4183 ff56836b 2021-07-08 stsp }
4184 abb4604f 2019-07-27 stsp } else {
4185 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4186 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4187 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4188 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4189 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4190 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4191 abb4604f 2019-07-27 stsp arg.status_path = path;
4192 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4193 abb4604f 2019-07-27 stsp arg.repo = repo;
4194 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4195 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4196 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4197 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4198 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4199 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4200 022fae89 2019-12-06 tracey if (!no_ignores) {
4201 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4202 3143d852 2020-06-25 stsp worktree->root_path, path);
4203 3143d852 2020-06-25 stsp if (err)
4204 3143d852 2020-06-25 stsp goto done;
4205 022fae89 2019-12-06 tracey }
4206 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4207 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4208 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4209 927df6b7 2019-02-10 stsp }
4210 3143d852 2020-06-25 stsp done:
4211 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4212 b2dcbbc7 2023-12-05 thomas got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4213 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4214 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4215 927df6b7 2019-02-10 stsp free(ondisk_path);
4216 347d1d3e 2019-07-12 stsp return err;
4217 347d1d3e 2019-07-12 stsp }
4218 347d1d3e 2019-07-12 stsp
4219 347d1d3e 2019-07-12 stsp const struct got_error *
4220 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4221 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4222 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4223 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4224 347d1d3e 2019-07-12 stsp {
4225 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4226 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4227 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4228 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4229 347d1d3e 2019-07-12 stsp
4230 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4231 347d1d3e 2019-07-12 stsp if (err)
4232 347d1d3e 2019-07-12 stsp return err;
4233 347d1d3e 2019-07-12 stsp
4234 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4235 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4236 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4237 f6343036 2021-06-22 stsp no_ignores, 0);
4238 72ea6654 2019-07-27 stsp if (err)
4239 72ea6654 2019-07-27 stsp break;
4240 72ea6654 2019-07-27 stsp }
4241 f8d1f275 2019-02-04 stsp free(fileindex_path);
4242 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4243 f8d1f275 2019-02-04 stsp return err;
4244 f8d1f275 2019-02-04 stsp }
4245 6c7ab921 2019-03-18 stsp
4246 6c7ab921 2019-03-18 stsp const struct got_error *
4247 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4248 6c7ab921 2019-03-18 stsp const char *arg)
4249 6c7ab921 2019-03-18 stsp {
4250 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4251 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4252 6c7ab921 2019-03-18 stsp size_t len;
4253 00bb5ea0 2020-07-23 stsp struct stat sb;
4254 01740607 2020-11-04 stsp char *abspath = NULL;
4255 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4256 6c7ab921 2019-03-18 stsp
4257 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4258 6c7ab921 2019-03-18 stsp
4259 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4260 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4261 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4262 00bb5ea0 2020-07-23 stsp
4263 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4264 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4265 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4266 00bb5ea0 2020-07-23 stsp goto done;
4267 00bb5ea0 2020-07-23 stsp }
4268 727173c3 2020-11-06 stsp sb.st_mode = 0;
4269 00bb5ea0 2020-07-23 stsp }
4270 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4271 00bb5ea0 2020-07-23 stsp /*
4272 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4273 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4274 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4275 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4276 00bb5ea0 2020-07-23 stsp */
4277 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4278 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4279 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4280 00bb5ea0 2020-07-23 stsp goto done;
4281 00bb5ea0 2020-07-23 stsp }
4282 00bb5ea0 2020-07-23 stsp
4283 00bb5ea0 2020-07-23 stsp }
4284 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4285 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4286 00bb5ea0 2020-07-23 stsp if (err)
4287 d0710d08 2019-07-22 stsp goto done;
4288 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4289 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4290 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4291 00bb5ea0 2020-07-23 stsp goto done;
4292 00bb5ea0 2020-07-23 stsp }
4293 00bb5ea0 2020-07-23 stsp } else {
4294 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4295 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4296 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4297 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4298 00bb5ea0 2020-07-23 stsp goto done;
4299 00bb5ea0 2020-07-23 stsp }
4300 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4301 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4302 01740607 2020-11-04 stsp goto done;
4303 01740607 2020-11-04 stsp }
4304 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4305 01740607 2020-11-04 stsp sizeof(canonpath));
4306 01740607 2020-11-04 stsp if (err)
4307 00bb5ea0 2020-07-23 stsp goto done;
4308 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4309 01740607 2020-11-04 stsp if (resolved == NULL) {
4310 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4311 01740607 2020-11-04 stsp goto done;
4312 00bb5ea0 2020-07-23 stsp }
4313 d0710d08 2019-07-22 stsp }
4314 d0710d08 2019-07-22 stsp }
4315 6c7ab921 2019-03-18 stsp
4316 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4317 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4318 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4319 6c7ab921 2019-03-18 stsp goto done;
4320 6c7ab921 2019-03-18 stsp }
4321 6c7ab921 2019-03-18 stsp
4322 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4323 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4324 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4325 f6d88e1a 2019-05-29 stsp if (err)
4326 f6d88e1a 2019-05-29 stsp goto done;
4327 f6d88e1a 2019-05-29 stsp } else {
4328 f6d88e1a 2019-05-29 stsp path = strdup("");
4329 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4330 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4331 f6d88e1a 2019-05-29 stsp goto done;
4332 f6d88e1a 2019-05-29 stsp }
4333 6c7ab921 2019-03-18 stsp }
4334 6c7ab921 2019-03-18 stsp
4335 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4336 6c7ab921 2019-03-18 stsp len = strlen(path);
4337 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4338 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4339 6c7ab921 2019-03-18 stsp len--;
4340 6c7ab921 2019-03-18 stsp }
4341 6c7ab921 2019-03-18 stsp done:
4342 01740607 2020-11-04 stsp free(abspath);
4343 6c7ab921 2019-03-18 stsp free(resolved);
4344 d0710d08 2019-07-22 stsp free(cwd);
4345 6c7ab921 2019-03-18 stsp if (err == NULL)
4346 6c7ab921 2019-03-18 stsp *wt_path = path;
4347 6c7ab921 2019-03-18 stsp else
4348 6c7ab921 2019-03-18 stsp free(path);
4349 d00136be 2019-03-26 stsp return err;
4350 d00136be 2019-03-26 stsp }
4351 d00136be 2019-03-26 stsp
4352 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4353 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4354 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4355 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4356 4e68cba3 2019-11-23 stsp void *progress_arg;
4357 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4358 4e68cba3 2019-11-23 stsp };
4359 4e68cba3 2019-11-23 stsp
4360 b21ebdb0 2023-06-22 thomas static int
4361 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4362 b21ebdb0 2023-06-22 thomas {
4363 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4364 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4365 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4366 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4367 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4368 b21ebdb0 2023-06-22 thomas }
4369 b21ebdb0 2023-06-22 thomas
4370 4e68cba3 2019-11-23 stsp static const struct got_error *
4371 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4372 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4373 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4374 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4375 07f5b47a 2019-06-02 stsp {
4376 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4377 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4378 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4379 6d022e97 2019-08-04 stsp struct stat sb;
4380 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4381 07f5b47a 2019-06-02 stsp
4382 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4383 dbb83fbd 2019-12-12 stsp relpath) == -1)
4384 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4385 dbb83fbd 2019-12-12 stsp
4386 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4387 6d022e97 2019-08-04 stsp if (ie) {
4388 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4389 12463d8b 2019-12-13 stsp de_name, a->repo);
4390 6d022e97 2019-08-04 stsp if (err)
4391 dbb83fbd 2019-12-12 stsp goto done;
4392 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4393 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4394 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4395 dbb83fbd 2019-12-12 stsp goto done;
4396 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4397 dbb83fbd 2019-12-12 stsp if (err)
4398 dbb83fbd 2019-12-12 stsp goto done;
4399 6d022e97 2019-08-04 stsp }
4400 07f5b47a 2019-06-02 stsp
4401 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4402 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4403 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4404 84bf00a6 2022-02-12 thomas else
4405 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4406 dbb83fbd 2019-12-12 stsp goto done;
4407 4e68cba3 2019-11-23 stsp }
4408 07f5b47a 2019-06-02 stsp
4409 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4410 4e68cba3 2019-11-23 stsp if (err)
4411 4e68cba3 2019-11-23 stsp goto done;
4412 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4413 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4414 3969253a 2020-03-07 stsp if (err) {
4415 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4416 3969253a 2020-03-07 stsp goto done;
4417 3969253a 2020-03-07 stsp }
4418 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4419 07f5b47a 2019-06-02 stsp if (err) {
4420 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4421 4e68cba3 2019-11-23 stsp goto done;
4422 07f5b47a 2019-06-02 stsp }
4423 4e68cba3 2019-11-23 stsp done:
4424 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4425 dbb83fbd 2019-12-12 stsp if (err)
4426 dbb83fbd 2019-12-12 stsp return err;
4427 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4428 dbb83fbd 2019-12-12 stsp return NULL;
4429 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4430 07f5b47a 2019-06-02 stsp }
4431 07f5b47a 2019-06-02 stsp
4432 d00136be 2019-03-26 stsp const struct got_error *
4433 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4434 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4435 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4436 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4437 d00136be 2019-03-26 stsp {
4438 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4439 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4440 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4441 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4442 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4443 d00136be 2019-03-26 stsp
4444 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4445 d00136be 2019-03-26 stsp if (err)
4446 d00136be 2019-03-26 stsp return err;
4447 d00136be 2019-03-26 stsp
4448 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4449 d00136be 2019-03-26 stsp if (err)
4450 d00136be 2019-03-26 stsp goto done;
4451 d00136be 2019-03-26 stsp
4452 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4453 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4454 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4455 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4456 4e68cba3 2019-11-23 stsp saa.repo = repo;
4457 4e68cba3 2019-11-23 stsp
4458 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4459 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4460 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4461 1dd54920 2019-05-11 stsp if (err)
4462 af12c6b9 2019-06-04 stsp break;
4463 1dd54920 2019-05-11 stsp }
4464 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4465 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4466 af12c6b9 2019-06-04 stsp err = sync_err;
4467 d00136be 2019-03-26 stsp done:
4468 fb399478 2019-07-12 stsp free(fileindex_path);
4469 d00136be 2019-03-26 stsp if (fileindex)
4470 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4471 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4472 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4473 d00136be 2019-03-26 stsp err = unlockerr;
4474 6c7ab921 2019-03-18 stsp return err;
4475 6c7ab921 2019-03-18 stsp }
4476 17ed4618 2019-06-02 stsp
4477 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4478 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4479 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4480 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4481 f2a9dc41 2019-12-13 tracey void *progress_arg;
4482 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4483 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4484 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4485 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4486 8ec7352a 2023-06-01 thomas const char *status_path;
4487 8ec7352a 2023-06-01 thomas size_t status_path_len;
4488 766841c2 2020-08-13 stsp const char *status_codes;
4489 f2a9dc41 2019-12-13 tracey };
4490 f2a9dc41 2019-12-13 tracey
4491 f2a9dc41 2019-12-13 tracey static const struct got_error *
4492 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4493 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4494 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4495 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4496 17ed4618 2019-06-02 stsp {
4497 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4498 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4499 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4500 17ed4618 2019-06-02 stsp struct stat sb;
4501 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4502 d64cc78a 2022-01-25 thomas
4503 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4504 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4505 d64cc78a 2022-01-25 thomas return NULL;
4506 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4507 d64cc78a 2022-01-25 thomas }
4508 17ed4618 2019-06-02 stsp
4509 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4510 17ed4618 2019-06-02 stsp if (ie == NULL)
4511 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4512 2ec1f75b 2019-03-26 stsp
4513 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4514 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4515 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4516 9acbc4fa 2019-08-03 stsp return NULL;
4517 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4518 9acbc4fa 2019-08-03 stsp }
4519 9acbc4fa 2019-08-03 stsp
4520 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4521 f2a9dc41 2019-12-13 tracey relpath) == -1)
4522 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4523 f2a9dc41 2019-12-13 tracey
4524 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4525 12463d8b 2019-12-13 stsp a->repo);
4526 17ed4618 2019-06-02 stsp if (err)
4527 f2a9dc41 2019-12-13 tracey goto done;
4528 17ed4618 2019-06-02 stsp
4529 766841c2 2020-08-13 stsp if (a->status_codes) {
4530 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4531 766841c2 2020-08-13 stsp int i;
4532 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4533 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4534 766841c2 2020-08-13 stsp break;
4535 766841c2 2020-08-13 stsp }
4536 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4537 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4538 766841c2 2020-08-13 stsp free(ondisk_path);
4539 766841c2 2020-08-13 stsp return NULL;
4540 766841c2 2020-08-13 stsp }
4541 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4542 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4543 766841c2 2020-08-13 stsp static char msg[64];
4544 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4545 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4546 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4547 766841c2 2020-08-13 stsp goto done;
4548 766841c2 2020-08-13 stsp }
4549 766841c2 2020-08-13 stsp }
4550 766841c2 2020-08-13 stsp
4551 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4552 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4553 f2a9dc41 2019-12-13 tracey goto done;
4554 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4555 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4556 f2a9dc41 2019-12-13 tracey goto done;
4557 f2a9dc41 2019-12-13 tracey }
4558 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4559 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4560 d64cc78a 2022-01-25 thomas goto done;
4561 d64cc78a 2022-01-25 thomas }
4562 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4563 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4564 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4565 f2a9dc41 2019-12-13 tracey goto done;
4566 f2a9dc41 2019-12-13 tracey }
4567 17ed4618 2019-06-02 stsp }
4568 17ed4618 2019-06-02 stsp
4569 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4570 20a2ad1c 2020-10-20 stsp size_t root_len;
4571 20a2ad1c 2020-10-20 stsp
4572 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4573 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4574 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4575 12463d8b 2019-12-13 stsp ondisk_path);
4576 12463d8b 2019-12-13 stsp goto done;
4577 12463d8b 2019-12-13 stsp }
4578 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4579 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4580 12463d8b 2019-12-13 stsp goto done;
4581 15341bfd 2020-03-05 tracey }
4582 15341bfd 2020-03-05 tracey
4583 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4584 20a2ad1c 2020-10-20 stsp do {
4585 20a2ad1c 2020-10-20 stsp char *parent;
4586 8ec7352a 2023-06-01 thomas
4587 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4588 20a2ad1c 2020-10-20 stsp if (err)
4589 2513f20a 2020-10-20 stsp goto done;
4590 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4591 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4592 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4593 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4594 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4595 8ec7352a 2023-06-01 thomas a->status_path_len))
4596 8ec7352a 2023-06-01 thomas break;
4597 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4598 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4599 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4600 20a2ad1c 2020-10-20 stsp ondisk_path);
4601 15341bfd 2020-03-05 tracey break;
4602 15341bfd 2020-03-05 tracey }
4603 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4604 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4605 f2a9dc41 2019-12-13 tracey }
4606 17ed4618 2019-06-02 stsp
4607 97f9158f 2023-08-29 thomas if (got_fileindex_entry_has_blob(ie))
4608 97f9158f 2023-08-29 thomas got_fileindex_entry_mark_deleted_from_disk(ie);
4609 97f9158f 2023-08-29 thomas else
4610 97f9158f 2023-08-29 thomas got_fileindex_entry_remove(a->fileindex, ie);
4611 f2a9dc41 2019-12-13 tracey done:
4612 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4613 f2a9dc41 2019-12-13 tracey if (err)
4614 f2a9dc41 2019-12-13 tracey return err;
4615 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4616 f2a9dc41 2019-12-13 tracey return NULL;
4617 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4618 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4619 17ed4618 2019-06-02 stsp }
4620 17ed4618 2019-06-02 stsp
4621 2ec1f75b 2019-03-26 stsp const struct got_error *
4622 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4623 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4624 766841c2 2020-08-13 stsp const char *status_codes,
4625 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4626 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4627 2ec1f75b 2019-03-26 stsp {
4628 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4629 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4630 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4631 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4632 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4633 2ec1f75b 2019-03-26 stsp
4634 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4635 2ec1f75b 2019-03-26 stsp if (err)
4636 2ec1f75b 2019-03-26 stsp return err;
4637 2ec1f75b 2019-03-26 stsp
4638 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4639 2ec1f75b 2019-03-26 stsp if (err)
4640 2ec1f75b 2019-03-26 stsp goto done;
4641 f2a9dc41 2019-12-13 tracey
4642 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4643 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4644 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4645 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4646 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4647 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4648 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4649 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4650 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4651 2ec1f75b 2019-03-26 stsp
4652 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4653 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4654 8ec7352a 2023-06-01 thomas
4655 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4656 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4657 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4658 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4659 8ec7352a 2023-06-01 thomas goto done;
4660 8ec7352a 2023-06-01 thomas }
4661 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4662 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4663 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4664 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4665 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4666 17ed4618 2019-06-02 stsp if (err)
4667 af12c6b9 2019-06-04 stsp break;
4668 2ec1f75b 2019-03-26 stsp }
4669 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4670 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4671 af12c6b9 2019-06-04 stsp err = sync_err;
4672 2ec1f75b 2019-03-26 stsp done:
4673 fb399478 2019-07-12 stsp free(fileindex_path);
4674 2ec1f75b 2019-03-26 stsp if (fileindex)
4675 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4676 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4677 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4678 2ec1f75b 2019-03-26 stsp err = unlockerr;
4679 33aa809d 2019-08-08 stsp return err;
4680 33aa809d 2019-08-08 stsp }
4681 33aa809d 2019-08-08 stsp
4682 33aa809d 2019-08-08 stsp static const struct got_error *
4683 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4684 33aa809d 2019-08-08 stsp {
4685 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4686 33aa809d 2019-08-08 stsp char *line = NULL;
4687 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4688 33aa809d 2019-08-08 stsp ssize_t linelen;
4689 33aa809d 2019-08-08 stsp
4690 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4691 33aa809d 2019-08-08 stsp if (linelen == -1) {
4692 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4693 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4694 33aa809d 2019-08-08 stsp goto done;
4695 33aa809d 2019-08-08 stsp }
4696 33aa809d 2019-08-08 stsp return NULL;
4697 33aa809d 2019-08-08 stsp }
4698 33aa809d 2019-08-08 stsp if (outfile) {
4699 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4700 33aa809d 2019-08-08 stsp if (n != linelen) {
4701 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4702 33aa809d 2019-08-08 stsp goto done;
4703 33aa809d 2019-08-08 stsp }
4704 33aa809d 2019-08-08 stsp }
4705 33aa809d 2019-08-08 stsp if (rejectfile) {
4706 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4707 33aa809d 2019-08-08 stsp if (n != linelen)
4708 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4709 33aa809d 2019-08-08 stsp }
4710 33aa809d 2019-08-08 stsp done:
4711 33aa809d 2019-08-08 stsp free(line);
4712 2ec1f75b 2019-03-26 stsp return err;
4713 2ec1f75b 2019-03-26 stsp }
4714 1f1abb7e 2019-08-08 stsp
4715 33aa809d 2019-08-08 stsp static const struct got_error *
4716 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4717 33aa809d 2019-08-08 stsp {
4718 33aa809d 2019-08-08 stsp char *line = NULL;
4719 33aa809d 2019-08-08 stsp size_t linesize = 0;
4720 33aa809d 2019-08-08 stsp ssize_t linelen;
4721 33aa809d 2019-08-08 stsp
4722 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4723 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4724 500467ff 2019-09-25 hiltjo if (ferror(f))
4725 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4726 500467ff 2019-09-25 hiltjo return NULL;
4727 500467ff 2019-09-25 hiltjo }
4728 33aa809d 2019-08-08 stsp free(line);
4729 33aa809d 2019-08-08 stsp return NULL;
4730 33aa809d 2019-08-08 stsp }
4731 33aa809d 2019-08-08 stsp
4732 33aa809d 2019-08-08 stsp static const struct got_error *
4733 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4734 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4735 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4736 abc59930 2021-09-05 naddy {
4737 33aa809d 2019-08-08 stsp const struct got_error *err;
4738 33aa809d 2019-08-08 stsp
4739 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4740 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4741 33aa809d 2019-08-08 stsp err = copy_one_line(f1, 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_cur1)++;
4745 33aa809d 2019-08-08 stsp }
4746 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4747 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4748 33aa809d 2019-08-08 stsp if (rejectfile)
4749 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4750 33aa809d 2019-08-08 stsp else
4751 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4752 33aa809d 2019-08-08 stsp if (err)
4753 33aa809d 2019-08-08 stsp return err;
4754 33aa809d 2019-08-08 stsp (*line_cur2)++;
4755 33aa809d 2019-08-08 stsp }
4756 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4757 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4758 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4759 33aa809d 2019-08-08 stsp if (err)
4760 33aa809d 2019-08-08 stsp return err;
4761 33aa809d 2019-08-08 stsp (*line_cur2)++;
4762 33aa809d 2019-08-08 stsp }
4763 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4764 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4765 33aa809d 2019-08-08 stsp if (rejectfile)
4766 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4767 33aa809d 2019-08-08 stsp else
4768 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4769 33aa809d 2019-08-08 stsp if (err)
4770 33aa809d 2019-08-08 stsp return err;
4771 33aa809d 2019-08-08 stsp (*line_cur1)++;
4772 33aa809d 2019-08-08 stsp }
4773 f1e81a05 2019-08-10 stsp
4774 f1e81a05 2019-08-10 stsp return NULL;
4775 f1e81a05 2019-08-10 stsp }
4776 f1e81a05 2019-08-10 stsp
4777 f1e81a05 2019-08-10 stsp static const struct got_error *
4778 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4779 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4780 f1e81a05 2019-08-10 stsp {
4781 f1e81a05 2019-08-10 stsp const struct got_error *err;
4782 f1e81a05 2019-08-10 stsp
4783 f1e81a05 2019-08-10 stsp if (outfile) {
4784 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4785 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4786 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4787 f1e81a05 2019-08-10 stsp if (err)
4788 f1e81a05 2019-08-10 stsp return err;
4789 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4790 f1e81a05 2019-08-10 stsp }
4791 f1e81a05 2019-08-10 stsp }
4792 f1e81a05 2019-08-10 stsp if (rejectfile) {
4793 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4794 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4795 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4796 f1e81a05 2019-08-10 stsp if (err)
4797 f1e81a05 2019-08-10 stsp return err;
4798 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4799 f1e81a05 2019-08-10 stsp }
4800 33aa809d 2019-08-08 stsp }
4801 33aa809d 2019-08-08 stsp
4802 33aa809d 2019-08-08 stsp return NULL;
4803 33aa809d 2019-08-08 stsp }
4804 33aa809d 2019-08-08 stsp
4805 33aa809d 2019-08-08 stsp static const struct got_error *
4806 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4807 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4808 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4809 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4810 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4811 33aa809d 2019-08-08 stsp {
4812 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4813 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4814 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4815 33aa809d 2019-08-08 stsp FILE *hunkfile;
4816 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4817 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4818 fe621944 2020-11-10 stsp int rc;
4819 33aa809d 2019-08-08 stsp
4820 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4821 33aa809d 2019-08-08 stsp
4822 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4823 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4824 fe621944 2020-11-10 stsp start_old = cc.left.start;
4825 fe621944 2020-11-10 stsp end_old = cc.left.end;
4826 fe621944 2020-11-10 stsp start_new = cc.right.start;
4827 fe621944 2020-11-10 stsp end_new = cc.right.end;
4828 33aa809d 2019-08-08 stsp
4829 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4830 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4831 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4832 33aa809d 2019-08-08 stsp
4833 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4834 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4835 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4836 33aa809d 2019-08-08 stsp
4837 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4838 fe621944 2020-11-10 stsp if (diff_state == NULL)
4839 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4840 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4841 fe621944 2020-11-10 stsp
4842 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4843 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4844 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4845 33aa809d 2019-08-08 stsp goto done;
4846 33aa809d 2019-08-08 stsp }
4847 fe621944 2020-11-10 stsp
4848 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4849 fe621944 2020-11-10 stsp diff_result, &cc);
4850 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4851 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4852 33aa809d 2019-08-08 stsp goto done;
4853 33aa809d 2019-08-08 stsp }
4854 fe621944 2020-11-10 stsp
4855 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4856 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4857 33aa809d 2019-08-08 stsp goto done;
4858 33aa809d 2019-08-08 stsp }
4859 33aa809d 2019-08-08 stsp
4860 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4861 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4862 33aa809d 2019-08-08 stsp if (err)
4863 33aa809d 2019-08-08 stsp goto done;
4864 33aa809d 2019-08-08 stsp
4865 33aa809d 2019-08-08 stsp switch (*choice) {
4866 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4867 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4868 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4869 33aa809d 2019-08-08 stsp break;
4870 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4871 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4872 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4873 33aa809d 2019-08-08 stsp break;
4874 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4875 33aa809d 2019-08-08 stsp break;
4876 33aa809d 2019-08-08 stsp default:
4877 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4878 33aa809d 2019-08-08 stsp break;
4879 33aa809d 2019-08-08 stsp }
4880 33aa809d 2019-08-08 stsp done:
4881 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4882 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4883 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4884 33aa809d 2019-08-08 stsp return err;
4885 33aa809d 2019-08-08 stsp }
4886 33aa809d 2019-08-08 stsp
4887 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4888 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4889 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4890 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4891 1f1abb7e 2019-08-08 stsp void *progress_arg;
4892 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4893 33aa809d 2019-08-08 stsp void *patch_arg;
4894 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4895 10604dce 2021-09-24 thomas int unlink_added_files;
4896 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4897 1f1abb7e 2019-08-08 stsp };
4898 a129376b 2019-03-28 stsp
4899 e20a8b6f 2019-06-04 stsp static const struct got_error *
4900 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4901 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4902 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4903 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4904 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4905 33aa809d 2019-08-08 stsp {
4906 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4907 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4908 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4909 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4910 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4911 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4912 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4913 fe621944 2020-11-10 stsp struct stat sb2;
4914 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4915 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4916 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4917 33aa809d 2019-08-08 stsp
4918 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4919 33aa809d 2019-08-08 stsp
4920 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4921 33aa809d 2019-08-08 stsp if (err)
4922 33aa809d 2019-08-08 stsp return err;
4923 33aa809d 2019-08-08 stsp
4924 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4925 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4926 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4927 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4928 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4929 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4930 fa3cef63 2020-07-23 stsp goto done;
4931 fa3cef63 2020-07-23 stsp }
4932 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4933 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4934 48b4f239 2021-12-31 thomas if (link_len == -1) {
4935 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4936 48b4f239 2021-12-31 thomas path2);
4937 48b4f239 2021-12-31 thomas }
4938 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4939 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4940 12463d8b 2019-12-13 stsp }
4941 12463d8b 2019-12-13 stsp } else {
4942 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4943 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4944 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4945 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4946 fa3cef63 2020-07-23 stsp goto done;
4947 fa3cef63 2020-07-23 stsp }
4948 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4949 fa3cef63 2020-07-23 stsp sizeof(link_target));
4950 fa3cef63 2020-07-23 stsp if (link_len == -1)
4951 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4952 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4953 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4954 12463d8b 2019-12-13 stsp }
4955 1ebedb77 2019-10-19 stsp }
4956 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4957 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4958 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4959 fa3cef63 2020-07-23 stsp goto done;
4960 fa3cef63 2020-07-23 stsp }
4961 1ebedb77 2019-10-19 stsp
4962 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4963 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4964 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4965 fa3cef63 2020-07-23 stsp goto done;
4966 fa3cef63 2020-07-23 stsp }
4967 fa3cef63 2020-07-23 stsp fd2 = -1;
4968 fa3cef63 2020-07-23 stsp } else {
4969 fa3cef63 2020-07-23 stsp size_t n;
4970 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4971 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4972 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4973 fa3cef63 2020-07-23 stsp goto done;
4974 fa3cef63 2020-07-23 stsp }
4975 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4976 fa3cef63 2020-07-23 stsp if (n != link_len) {
4977 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4978 fa3cef63 2020-07-23 stsp goto done;
4979 fa3cef63 2020-07-23 stsp }
4980 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4981 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4982 fa3cef63 2020-07-23 stsp goto done;
4983 fa3cef63 2020-07-23 stsp }
4984 fa3cef63 2020-07-23 stsp rewind(f2);
4985 33aa809d 2019-08-08 stsp }
4986 33aa809d 2019-08-08 stsp
4987 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4988 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4989 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4990 f4ae6ddb 2022-07-01 thomas goto done;
4991 f4ae6ddb 2022-07-01 thomas }
4992 f4ae6ddb 2022-07-01 thomas
4993 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4994 33aa809d 2019-08-08 stsp if (err)
4995 33aa809d 2019-08-08 stsp goto done;
4996 33aa809d 2019-08-08 stsp
4997 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4998 33aa809d 2019-08-08 stsp if (err)
4999 33aa809d 2019-08-08 stsp goto done;
5000 33aa809d 2019-08-08 stsp
5001 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5002 33aa809d 2019-08-08 stsp if (err)
5003 33aa809d 2019-08-08 stsp goto done;
5004 33aa809d 2019-08-08 stsp
5005 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5006 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5007 33aa809d 2019-08-08 stsp if (err)
5008 33aa809d 2019-08-08 stsp goto done;
5009 33aa809d 2019-08-08 stsp
5010 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5011 fc2a50f2 2022-11-01 thomas "");
5012 33aa809d 2019-08-08 stsp if (err)
5013 33aa809d 2019-08-08 stsp goto done;
5014 33aa809d 2019-08-08 stsp
5015 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5016 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
5017 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5018 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
5019 fe621944 2020-11-10 stsp
5020 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
5021 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5022 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
5023 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
5024 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
5025 fe621944 2020-11-10 stsp nchanges++;
5026 fe621944 2020-11-10 stsp }
5027 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5028 33aa809d 2019-08-08 stsp int choice;
5029 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
5030 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
5031 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
5032 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
5033 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
5034 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
5035 33aa809d 2019-08-08 stsp if (err)
5036 33aa809d 2019-08-08 stsp goto done;
5037 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5038 33aa809d 2019-08-08 stsp have_content = 1;
5039 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5040 33aa809d 2019-08-08 stsp break;
5041 33aa809d 2019-08-08 stsp }
5042 1ebedb77 2019-10-19 stsp if (have_content) {
5043 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5044 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
5045 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
5046 1ebedb77 2019-10-19 stsp if (err)
5047 1ebedb77 2019-10-19 stsp goto done;
5048 1ebedb77 2019-10-19 stsp
5049 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
5050 a2c162eb 2022-10-30 thomas mode_t mode;
5051 a2c162eb 2022-10-30 thomas
5052 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
5053 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
5054 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
5055 fa3cef63 2020-07-23 stsp goto done;
5056 fa3cef63 2020-07-23 stsp }
5057 1ebedb77 2019-10-19 stsp }
5058 1ebedb77 2019-10-19 stsp }
5059 33aa809d 2019-08-08 stsp done:
5060 33aa809d 2019-08-08 stsp free(id_str);
5061 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5062 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5063 33aa809d 2019-08-08 stsp if (blob)
5064 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5065 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5066 fe621944 2020-11-10 stsp if (err == NULL)
5067 fe621944 2020-11-10 stsp err = free_err;
5068 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5069 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5070 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5071 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5072 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5073 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5074 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5075 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5076 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5077 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5078 33aa809d 2019-08-08 stsp if (err || !have_content) {
5079 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5080 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5081 33aa809d 2019-08-08 stsp free(*path_outfile);
5082 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5083 33aa809d 2019-08-08 stsp }
5084 33aa809d 2019-08-08 stsp free(path1);
5085 33aa809d 2019-08-08 stsp return err;
5086 33aa809d 2019-08-08 stsp }
5087 33aa809d 2019-08-08 stsp
5088 33aa809d 2019-08-08 stsp static const struct got_error *
5089 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5090 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5091 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5092 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5093 a129376b 2019-03-28 stsp {
5094 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5095 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5096 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5097 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5098 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
5099 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5100 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5101 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5102 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5103 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5104 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5105 f4ae6ddb 2022-07-01 thomas int fd = -1;
5106 a129376b 2019-03-28 stsp
5107 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5108 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5109 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5110 d3bcc3d1 2019-08-08 stsp return NULL;
5111 3d69ad8d 2019-08-17 semarie
5112 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5113 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5114 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5115 d3bcc3d1 2019-08-08 stsp
5116 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5117 65084dad 2019-08-08 stsp if (ie == NULL)
5118 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5119 a129376b 2019-03-28 stsp
5120 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5121 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5122 a129376b 2019-03-28 stsp if (err) {
5123 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5124 a129376b 2019-03-28 stsp goto done;
5125 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5126 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5127 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5128 e20a8b6f 2019-06-04 stsp goto done;
5129 e20a8b6f 2019-06-04 stsp }
5130 a129376b 2019-03-28 stsp }
5131 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5132 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5133 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5134 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5135 a129376b 2019-03-28 stsp goto done;
5136 a129376b 2019-03-28 stsp }
5137 a129376b 2019-03-28 stsp } else {
5138 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5139 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5140 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5141 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5142 a129376b 2019-03-28 stsp goto done;
5143 a129376b 2019-03-28 stsp }
5144 a129376b 2019-03-28 stsp } else {
5145 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5146 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5147 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5148 a129376b 2019-03-28 stsp goto done;
5149 a129376b 2019-03-28 stsp }
5150 a129376b 2019-03-28 stsp }
5151 a129376b 2019-03-28 stsp }
5152 a129376b 2019-03-28 stsp
5153 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
5154 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
5155 945f9229 2022-04-16 thomas if (err)
5156 945f9229 2022-04-16 thomas goto done;
5157 945f9229 2022-04-16 thomas
5158 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5159 a9fa2909 2019-07-27 stsp if (err) {
5160 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5161 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5162 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5163 a9fa2909 2019-07-27 stsp goto done;
5164 a9fa2909 2019-07-27 stsp } else {
5165 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5166 a9fa2909 2019-07-27 stsp if (err)
5167 a9fa2909 2019-07-27 stsp goto done;
5168 a9fa2909 2019-07-27 stsp
5169 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5170 1233e6b6 2020-10-19 stsp if (err)
5171 a9fa2909 2019-07-27 stsp goto done;
5172 a9fa2909 2019-07-27 stsp
5173 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5174 1233e6b6 2020-10-19 stsp free(te_name);
5175 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5176 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5177 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5178 a9fa2909 2019-07-27 stsp goto done;
5179 a9fa2909 2019-07-27 stsp }
5180 a129376b 2019-03-28 stsp }
5181 a129376b 2019-03-28 stsp
5182 a129376b 2019-03-28 stsp switch (status) {
5183 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5184 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5185 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5186 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5187 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5188 33aa809d 2019-08-08 stsp if (err)
5189 33aa809d 2019-08-08 stsp goto done;
5190 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5191 33aa809d 2019-08-08 stsp break;
5192 33aa809d 2019-08-08 stsp }
5193 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5194 1f1abb7e 2019-08-08 stsp ie->path);
5195 1ee397ad 2019-07-12 stsp if (err)
5196 1ee397ad 2019-07-12 stsp goto done;
5197 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5198 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5199 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5200 8641a332 2023-04-14 thomas
5201 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5202 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5203 8641a332 2023-04-14 thomas
5204 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5205 8641a332 2023-04-14 thomas entry) {
5206 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5207 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5208 8641a332 2023-04-14 thomas continue;
5209 8641a332 2023-04-14 thomas do_unlink = 1;
5210 8641a332 2023-04-14 thomas break;
5211 8641a332 2023-04-14 thomas }
5212 10604dce 2021-09-24 thomas }
5213 8641a332 2023-04-14 thomas
5214 8641a332 2023-04-14 thomas if (do_unlink) {
5215 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5216 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5217 8641a332 2023-04-14 thomas relpath) == -1) {
5218 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5219 8641a332 2023-04-14 thomas goto done;
5220 8641a332 2023-04-14 thomas }
5221 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5222 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5223 8641a332 2023-04-14 thomas ondisk_path);
5224 8641a332 2023-04-14 thomas break;
5225 8641a332 2023-04-14 thomas }
5226 10604dce 2021-09-24 thomas }
5227 10604dce 2021-09-24 thomas }
5228 a129376b 2019-03-28 stsp break;
5229 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5230 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5231 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5232 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5233 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5234 33aa809d 2019-08-08 stsp if (err)
5235 33aa809d 2019-08-08 stsp goto done;
5236 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5237 33aa809d 2019-08-08 stsp break;
5238 33aa809d 2019-08-08 stsp }
5239 33aa809d 2019-08-08 stsp /* fall through */
5240 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5241 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5242 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5243 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5244 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5245 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5246 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5247 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5248 43010591 2023-02-17 thomas else
5249 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5250 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5251 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5252 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5253 f4ae6ddb 2022-07-01 thomas goto done;
5254 f4ae6ddb 2022-07-01 thomas }
5255 f4ae6ddb 2022-07-01 thomas
5256 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5257 a129376b 2019-03-28 stsp if (err)
5258 65084dad 2019-08-08 stsp goto done;
5259 65084dad 2019-08-08 stsp
5260 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5261 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5262 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5263 a129376b 2019-03-28 stsp goto done;
5264 65084dad 2019-08-08 stsp }
5265 65084dad 2019-08-08 stsp
5266 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5267 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5268 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5269 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5270 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5271 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5272 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5273 33aa809d 2019-08-08 stsp break;
5274 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5275 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5276 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5277 369fd7e5 2020-07-23 stsp path_content);
5278 369fd7e5 2020-07-23 stsp break;
5279 369fd7e5 2020-07-23 stsp }
5280 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5281 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5282 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5283 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5284 369fd7e5 2020-07-23 stsp } else {
5285 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5286 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5287 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5288 369fd7e5 2020-07-23 stsp goto done;
5289 369fd7e5 2020-07-23 stsp }
5290 33aa809d 2019-08-08 stsp }
5291 33aa809d 2019-08-08 stsp } else {
5292 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5293 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5294 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5295 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5296 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5297 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5298 2e1fa222 2020-07-23 stsp } else {
5299 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5300 2e1fa222 2020-07-23 stsp ie->path,
5301 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5302 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5303 aaf972e2 2023-09-18 thomas 0, 1, 0, 0, NULL, a->repo,
5304 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5305 2e1fa222 2020-07-23 stsp }
5306 a129376b 2019-03-28 stsp if (err)
5307 a129376b 2019-03-28 stsp goto done;
5308 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5309 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5310 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5311 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5312 437adc9d 2020-12-10 yzhong blob->id.sha1,
5313 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5314 2e1fa222 2020-07-23 stsp if (err)
5315 2e1fa222 2020-07-23 stsp goto done;
5316 2e1fa222 2020-07-23 stsp }
5317 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5318 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5319 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5320 33aa809d 2019-08-08 stsp }
5321 a129376b 2019-03-28 stsp }
5322 a129376b 2019-03-28 stsp break;
5323 e20a8b6f 2019-06-04 stsp }
5324 a129376b 2019-03-28 stsp default:
5325 1f1abb7e 2019-08-08 stsp break;
5326 a129376b 2019-03-28 stsp }
5327 a129376b 2019-03-28 stsp done:
5328 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5329 33aa809d 2019-08-08 stsp free(path_content);
5330 e20a8b6f 2019-06-04 stsp free(parent_path);
5331 a129376b 2019-03-28 stsp free(tree_path);
5332 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5333 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5334 a129376b 2019-03-28 stsp if (blob)
5335 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5336 a129376b 2019-03-28 stsp if (tree)
5337 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5338 a129376b 2019-03-28 stsp free(tree_id);
5339 945f9229 2022-04-16 thomas if (base_commit)
5340 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5341 e20a8b6f 2019-06-04 stsp return err;
5342 e20a8b6f 2019-06-04 stsp }
5343 e20a8b6f 2019-06-04 stsp
5344 e20a8b6f 2019-06-04 stsp const struct got_error *
5345 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5346 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5347 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5348 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5349 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5350 e20a8b6f 2019-06-04 stsp {
5351 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5352 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5353 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5354 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5355 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5356 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5357 e20a8b6f 2019-06-04 stsp
5358 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5359 e20a8b6f 2019-06-04 stsp if (err)
5360 e20a8b6f 2019-06-04 stsp return err;
5361 e20a8b6f 2019-06-04 stsp
5362 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5363 e20a8b6f 2019-06-04 stsp if (err)
5364 e20a8b6f 2019-06-04 stsp goto done;
5365 e20a8b6f 2019-06-04 stsp
5366 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5367 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5368 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5369 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5370 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5371 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5372 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5373 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5374 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5375 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5376 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5377 e20a8b6f 2019-06-04 stsp if (err)
5378 af12c6b9 2019-06-04 stsp break;
5379 e20a8b6f 2019-06-04 stsp }
5380 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5381 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5382 e20a8b6f 2019-06-04 stsp err = sync_err;
5383 af12c6b9 2019-06-04 stsp done:
5384 fb399478 2019-07-12 stsp free(fileindex_path);
5385 a129376b 2019-03-28 stsp if (fileindex)
5386 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5387 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5388 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5389 a129376b 2019-03-28 stsp err = unlockerr;
5390 c4296144 2019-05-09 stsp return err;
5391 c4296144 2019-05-09 stsp }
5392 c4296144 2019-05-09 stsp
5393 cf066bf8 2019-05-09 stsp static void
5394 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5395 cf066bf8 2019-05-09 stsp {
5396 24519714 2019-05-09 stsp free(ct->path);
5397 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5398 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5399 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5400 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5401 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5402 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5403 cf066bf8 2019-05-09 stsp free(ct);
5404 cf066bf8 2019-05-09 stsp }
5405 24519714 2019-05-09 stsp
5406 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5407 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5408 24519714 2019-05-09 stsp struct got_repository *repo;
5409 24519714 2019-05-09 stsp struct got_worktree *worktree;
5410 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5411 5f8a88c6 2019-08-03 stsp int have_staged_files;
5412 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5413 ef899790 2022-11-01 thomas int diff_header_shown;
5414 be94c032 2023-02-20 thomas int commit_conflicts;
5415 ef899790 2022-11-01 thomas FILE *diff_outfile;
5416 ef899790 2022-11-01 thomas FILE *f1;
5417 ef899790 2022-11-01 thomas FILE *f2;
5418 24519714 2019-05-09 stsp };
5419 ef899790 2022-11-01 thomas
5420 ef899790 2022-11-01 thomas /*
5421 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5422 ef899790 2022-11-01 thomas * it as content to the diff engine.
5423 ef899790 2022-11-01 thomas */
5424 ef899790 2022-11-01 thomas static const struct got_error *
5425 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5426 ef899790 2022-11-01 thomas const char *abspath)
5427 ef899790 2022-11-01 thomas {
5428 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5429 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5430 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5431 ef899790 2022-11-01 thomas
5432 ef899790 2022-11-01 thomas *fd = -1;
5433 ef899790 2022-11-01 thomas
5434 ef899790 2022-11-01 thomas if (dirfd != -1) {
5435 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5436 ef899790 2022-11-01 thomas if (target_len == -1)
5437 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5438 ef899790 2022-11-01 thomas } else {
5439 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5440 ef899790 2022-11-01 thomas if (target_len == -1)
5441 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5442 ef899790 2022-11-01 thomas }
5443 ef899790 2022-11-01 thomas
5444 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5445 ef899790 2022-11-01 thomas if (*fd == -1)
5446 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5447 ef899790 2022-11-01 thomas
5448 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5449 ef899790 2022-11-01 thomas if (outlen == -1) {
5450 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5451 ef899790 2022-11-01 thomas goto done;
5452 ef899790 2022-11-01 thomas }
5453 ef899790 2022-11-01 thomas
5454 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5455 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5456 ef899790 2022-11-01 thomas goto done;
5457 ef899790 2022-11-01 thomas }
5458 ef899790 2022-11-01 thomas done:
5459 ef899790 2022-11-01 thomas if (err) {
5460 ef899790 2022-11-01 thomas close(*fd);
5461 ef899790 2022-11-01 thomas *fd = -1;
5462 ef899790 2022-11-01 thomas }
5463 ef899790 2022-11-01 thomas return err;
5464 ef899790 2022-11-01 thomas }
5465 ef899790 2022-11-01 thomas
5466 ef899790 2022-11-01 thomas static const struct got_error *
5467 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5468 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5469 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5470 ef899790 2022-11-01 thomas {
5471 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5472 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5473 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5474 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5475 ef899790 2022-11-01 thomas char *label1 = NULL;
5476 ef899790 2022-11-01 thomas struct stat sb;
5477 ef899790 2022-11-01 thomas off_t size1 = 0;
5478 ef899790 2022-11-01 thomas int f2_exists = 0;
5479 ef899790 2022-11-01 thomas char *id_str = NULL;
5480 ef899790 2022-11-01 thomas
5481 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5482 d259e491 2022-11-01 thomas
5483 d259e491 2022-11-01 thomas if (diff_staged) {
5484 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5485 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5486 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5487 d259e491 2022-11-01 thomas return NULL;
5488 d259e491 2022-11-01 thomas } else {
5489 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5490 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5491 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5492 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5493 d259e491 2022-11-01 thomas return NULL;
5494 d259e491 2022-11-01 thomas }
5495 ef899790 2022-11-01 thomas
5496 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5497 ef899790 2022-11-01 thomas if (err)
5498 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5499 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5500 ef899790 2022-11-01 thomas if (err)
5501 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5502 ef899790 2022-11-01 thomas
5503 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5504 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5505 ef899790 2022-11-01 thomas if (err)
5506 ef899790 2022-11-01 thomas return err;
5507 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5508 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5509 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5510 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5511 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5512 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5513 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5514 ef899790 2022-11-01 thomas }
5515 ef899790 2022-11-01 thomas
5516 ef899790 2022-11-01 thomas if (diff_staged) {
5517 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5518 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5519 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5520 ef899790 2022-11-01 thomas label1 = ct->path;
5521 ef899790 2022-11-01 thomas label2 = ct->path;
5522 ef899790 2022-11-01 thomas break;
5523 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5524 ef899790 2022-11-01 thomas label2 = ct->path;
5525 ef899790 2022-11-01 thomas break;
5526 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5527 ef899790 2022-11-01 thomas label1 = ct->path;
5528 ef899790 2022-11-01 thomas break;
5529 ef899790 2022-11-01 thomas default:
5530 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
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 fd2 = got_opentempfd();
5538 ef899790 2022-11-01 thomas if (fd2 == -1) {
5539 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5540 ef899790 2022-11-01 thomas goto done;
5541 ef899790 2022-11-01 thomas }
5542 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5543 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5544 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5545 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5546 ef899790 2022-11-01 thomas goto done;
5547 ef899790 2022-11-01 thomas }
5548 ef899790 2022-11-01 thomas
5549 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5550 ef899790 2022-11-01 thomas if (fd1 == -1) {
5551 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5552 ef899790 2022-11-01 thomas goto done;
5553 ef899790 2022-11-01 thomas }
5554 ef899790 2022-11-01 thomas
5555 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5556 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5557 ef899790 2022-11-01 thomas 8192, fd1);
5558 ef899790 2022-11-01 thomas if (err)
5559 ef899790 2022-11-01 thomas goto done;
5560 ef899790 2022-11-01 thomas }
5561 ef899790 2022-11-01 thomas
5562 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5563 ef899790 2022-11-01 thomas if (dirfd != -1) {
5564 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5565 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5566 ef899790 2022-11-01 thomas if (fd == -1) {
5567 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5568 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5569 ef899790 2022-11-01 thomas ct->ondisk_path);
5570 ef899790 2022-11-01 thomas goto done;
5571 ef899790 2022-11-01 thomas }
5572 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5573 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5574 ef899790 2022-11-01 thomas if (err)
5575 ef899790 2022-11-01 thomas goto done;
5576 ef899790 2022-11-01 thomas }
5577 ef899790 2022-11-01 thomas } else {
5578 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5579 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5580 ef899790 2022-11-01 thomas if (fd == -1) {
5581 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5582 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5583 ef899790 2022-11-01 thomas ct->ondisk_path);
5584 ef899790 2022-11-01 thomas goto done;
5585 ef899790 2022-11-01 thomas }
5586 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5587 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5588 ef899790 2022-11-01 thomas if (err)
5589 ef899790 2022-11-01 thomas goto done;
5590 ef899790 2022-11-01 thomas }
5591 ef899790 2022-11-01 thomas }
5592 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5593 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5594 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5595 ef899790 2022-11-01 thomas goto done;
5596 ef899790 2022-11-01 thomas }
5597 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5598 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5599 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5600 ef899790 2022-11-01 thomas goto done;
5601 ef899790 2022-11-01 thomas }
5602 ef899790 2022-11-01 thomas fd = -1;
5603 ef899790 2022-11-01 thomas f2_exists = 1;
5604 ef899790 2022-11-01 thomas }
5605 ef899790 2022-11-01 thomas
5606 ef899790 2022-11-01 thomas if (blob1) {
5607 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5608 ef899790 2022-11-01 thomas f1, blob1);
5609 ef899790 2022-11-01 thomas if (err)
5610 ef899790 2022-11-01 thomas goto done;
5611 ef899790 2022-11-01 thomas }
5612 ef899790 2022-11-01 thomas
5613 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5614 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5615 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5616 ef899790 2022-11-01 thomas done:
5617 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5618 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5619 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5620 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5621 ef899790 2022-11-01 thomas if (blob1)
5622 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5623 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5624 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5625 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5626 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5627 ef899790 2022-11-01 thomas return err;
5628 ef899790 2022-11-01 thomas }
5629 cf066bf8 2019-05-09 stsp
5630 c4296144 2019-05-09 stsp static const struct got_error *
5631 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5632 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5633 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5634 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5635 c4296144 2019-05-09 stsp {
5636 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5637 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5638 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5639 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5640 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5641 768aea60 2019-05-09 stsp struct stat sb;
5642 c4296144 2019-05-09 stsp
5643 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5644 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5645 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5646 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5647 5f8a88c6 2019-08-03 stsp return NULL;
5648 5f8a88c6 2019-08-03 stsp } else {
5649 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5650 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5651 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5652 be94c032 2023-02-20 thomas }
5653 c4296144 2019-05-09 stsp
5654 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5655 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5656 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5657 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5658 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5659 5f8a88c6 2019-08-03 stsp return NULL;
5660 5f8a88c6 2019-08-03 stsp }
5661 0b5cc0d6 2019-05-09 stsp
5662 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5663 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5664 036813ee 2019-05-09 stsp goto done;
5665 036813ee 2019-05-09 stsp }
5666 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5667 036813ee 2019-05-09 stsp parent_path = strdup("");
5668 69960a46 2019-05-09 stsp if (parent_path == NULL)
5669 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5670 69960a46 2019-05-09 stsp } else {
5671 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5672 69960a46 2019-05-09 stsp if (err)
5673 69960a46 2019-05-09 stsp return err;
5674 69960a46 2019-05-09 stsp }
5675 c4296144 2019-05-09 stsp
5676 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5677 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5678 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5679 c4296144 2019-05-09 stsp goto done;
5680 768aea60 2019-05-09 stsp }
5681 768aea60 2019-05-09 stsp
5682 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5683 768aea60 2019-05-09 stsp relpath) == -1) {
5684 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5685 768aea60 2019-05-09 stsp goto done;
5686 768aea60 2019-05-09 stsp }
5687 0aeb8099 2020-07-23 stsp
5688 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5689 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5690 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5691 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5692 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5693 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5694 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5695 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5696 0aeb8099 2020-07-23 stsp break;
5697 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5698 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5699 0aeb8099 2020-07-23 stsp break;
5700 0aeb8099 2020-07-23 stsp default:
5701 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5702 0aeb8099 2020-07-23 stsp goto done;
5703 0aeb8099 2020-07-23 stsp }
5704 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5705 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5706 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5707 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5708 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5709 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5710 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5711 12463d8b 2019-12-13 stsp ct->ondisk_path);
5712 12463d8b 2019-12-13 stsp goto done;
5713 12463d8b 2019-12-13 stsp }
5714 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5715 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5716 768aea60 2019-05-09 stsp goto done;
5717 768aea60 2019-05-09 stsp }
5718 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5719 c4296144 2019-05-09 stsp }
5720 c4296144 2019-05-09 stsp
5721 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5722 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5723 44d03001 2019-05-09 stsp relpath) == -1) {
5724 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5725 44d03001 2019-05-09 stsp goto done;
5726 44d03001 2019-05-09 stsp }
5727 44d03001 2019-05-09 stsp
5728 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5729 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5730 35213c7c 2020-07-23 stsp int is_bad_symlink;
5731 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5732 35213c7c 2020-07-23 stsp ssize_t target_len;
5733 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5734 35213c7c 2020-07-23 stsp sizeof(target_path));
5735 35213c7c 2020-07-23 stsp if (target_len == -1) {
5736 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5737 35213c7c 2020-07-23 stsp ct->ondisk_path);
5738 35213c7c 2020-07-23 stsp goto done;
5739 35213c7c 2020-07-23 stsp }
5740 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5741 ad10f64e 2023-07-19 thomas target_len, ct->ondisk_path, a->worktree->root_path,
5742 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
5743 35213c7c 2020-07-23 stsp if (err)
5744 35213c7c 2020-07-23 stsp goto done;
5745 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5746 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5747 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5748 35213c7c 2020-07-23 stsp goto done;
5749 35213c7c 2020-07-23 stsp }
5750 35213c7c 2020-07-23 stsp }
5751 35213c7c 2020-07-23 stsp
5752 35213c7c 2020-07-23 stsp
5753 cf066bf8 2019-05-09 stsp ct->status = status;
5754 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5755 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5756 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5757 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5758 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5759 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5760 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5761 b416585c 2019-05-13 stsp goto done;
5762 b416585c 2019-05-13 stsp }
5763 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5764 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5765 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5766 f0b75401 2019-08-03 stsp goto done;
5767 f0b75401 2019-08-03 stsp }
5768 f0b75401 2019-08-03 stsp }
5769 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5770 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5771 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5772 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5773 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5774 036813ee 2019-05-09 stsp goto done;
5775 036813ee 2019-05-09 stsp }
5776 ca2503ea 2019-05-09 stsp }
5777 24519714 2019-05-09 stsp ct->path = strdup(path);
5778 24519714 2019-05-09 stsp if (ct->path == NULL) {
5779 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5780 24519714 2019-05-09 stsp goto done;
5781 24519714 2019-05-09 stsp }
5782 ef899790 2022-11-01 thomas
5783 720b08bc 2023-08-29 thomas if (a->diff_outfile) {
5784 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5785 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5786 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5787 ef899790 2022-11-01 thomas if (err)
5788 ef899790 2022-11-01 thomas goto done;
5789 ef899790 2022-11-01 thomas }
5790 720b08bc 2023-08-29 thomas
5791 720b08bc 2023-08-29 thomas err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5792 c4296144 2019-05-09 stsp done:
5793 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5794 ed175427 2019-05-09 stsp free_commitable(ct);
5795 24519714 2019-05-09 stsp free(parent_path);
5796 036813ee 2019-05-09 stsp free(path);
5797 a129376b 2019-03-28 stsp return err;
5798 a129376b 2019-03-28 stsp }
5799 c4296144 2019-05-09 stsp
5800 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5801 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5802 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5803 036813ee 2019-05-09 stsp struct got_repository *);
5804 ed175427 2019-05-09 stsp
5805 ed175427 2019-05-09 stsp static const struct got_error *
5806 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5807 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5808 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5809 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5810 afa376bf 2019-05-09 stsp struct got_repository *repo)
5811 ed175427 2019-05-09 stsp {
5812 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5813 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5814 036813ee 2019-05-09 stsp char *subpath;
5815 ed175427 2019-05-09 stsp
5816 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5817 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5818 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5819 ed175427 2019-05-09 stsp
5820 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5821 ed175427 2019-05-09 stsp if (err)
5822 ed175427 2019-05-09 stsp return err;
5823 ed175427 2019-05-09 stsp
5824 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5825 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5826 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5827 036813ee 2019-05-09 stsp free(subpath);
5828 036813ee 2019-05-09 stsp return err;
5829 036813ee 2019-05-09 stsp }
5830 ed175427 2019-05-09 stsp
5831 036813ee 2019-05-09 stsp static const struct got_error *
5832 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5833 036813ee 2019-05-09 stsp {
5834 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5835 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5836 ed175427 2019-05-09 stsp
5837 036813ee 2019-05-09 stsp *match = 0;
5838 ed175427 2019-05-09 stsp
5839 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5840 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5841 0f63689d 2019-05-10 stsp return NULL;
5842 036813ee 2019-05-09 stsp }
5843 0b5cc0d6 2019-05-09 stsp
5844 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5845 0f63689d 2019-05-10 stsp if (err)
5846 0f63689d 2019-05-10 stsp return err;
5847 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5848 036813ee 2019-05-09 stsp free(ct_parent_path);
5849 036813ee 2019-05-09 stsp return err;
5850 036813ee 2019-05-09 stsp }
5851 0b5cc0d6 2019-05-09 stsp
5852 768aea60 2019-05-09 stsp static mode_t
5853 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5854 768aea60 2019-05-09 stsp {
5855 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5856 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5857 3d9a4ec4 2020-07-23 stsp
5858 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5859 768aea60 2019-05-09 stsp }
5860 768aea60 2019-05-09 stsp
5861 036813ee 2019-05-09 stsp static const struct got_error *
5862 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5863 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5864 036813ee 2019-05-09 stsp {
5865 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5866 ca2503ea 2019-05-09 stsp
5867 036813ee 2019-05-09 stsp *new_te = NULL;
5868 0b5cc0d6 2019-05-09 stsp
5869 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5870 036813ee 2019-05-09 stsp if (err)
5871 036813ee 2019-05-09 stsp goto done;
5872 0b5cc0d6 2019-05-09 stsp
5873 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5874 036813ee 2019-05-09 stsp
5875 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5876 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5877 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5878 5f8a88c6 2019-08-03 stsp else
5879 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5880 036813ee 2019-05-09 stsp done:
5881 036813ee 2019-05-09 stsp if (err && *new_te) {
5882 56e0773d 2019-11-28 stsp free(*new_te);
5883 036813ee 2019-05-09 stsp *new_te = NULL;
5884 036813ee 2019-05-09 stsp }
5885 036813ee 2019-05-09 stsp return err;
5886 036813ee 2019-05-09 stsp }
5887 036813ee 2019-05-09 stsp
5888 036813ee 2019-05-09 stsp static const struct got_error *
5889 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5890 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5891 036813ee 2019-05-09 stsp {
5892 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5893 102b254e 2020-10-19 stsp char *ct_name = NULL;
5894 036813ee 2019-05-09 stsp
5895 036813ee 2019-05-09 stsp *new_te = NULL;
5896 036813ee 2019-05-09 stsp
5897 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5898 036813ee 2019-05-09 stsp if (*new_te == NULL)
5899 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5900 036813ee 2019-05-09 stsp
5901 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5902 102b254e 2020-10-19 stsp if (err)
5903 036813ee 2019-05-09 stsp goto done;
5904 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5905 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5906 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5907 036813ee 2019-05-09 stsp goto done;
5908 036813ee 2019-05-09 stsp }
5909 036813ee 2019-05-09 stsp
5910 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5911 036813ee 2019-05-09 stsp
5912 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5913 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5914 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5915 5f8a88c6 2019-08-03 stsp else
5916 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5917 036813ee 2019-05-09 stsp done:
5918 102b254e 2020-10-19 stsp free(ct_name);
5919 036813ee 2019-05-09 stsp if (err && *new_te) {
5920 56e0773d 2019-11-28 stsp free(*new_te);
5921 036813ee 2019-05-09 stsp *new_te = NULL;
5922 036813ee 2019-05-09 stsp }
5923 036813ee 2019-05-09 stsp return err;
5924 036813ee 2019-05-09 stsp }
5925 036813ee 2019-05-09 stsp
5926 036813ee 2019-05-09 stsp static const struct got_error *
5927 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5928 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5929 036813ee 2019-05-09 stsp {
5930 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5931 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5932 036813ee 2019-05-09 stsp
5933 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5934 036813ee 2019-05-09 stsp if (err)
5935 036813ee 2019-05-09 stsp return err;
5936 036813ee 2019-05-09 stsp if (new_pe == NULL)
5937 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5938 036813ee 2019-05-09 stsp return NULL;
5939 afa376bf 2019-05-09 stsp }
5940 afa376bf 2019-05-09 stsp
5941 afa376bf 2019-05-09 stsp static const struct got_error *
5942 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5943 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5944 afa376bf 2019-05-09 stsp {
5945 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5946 5f8a88c6 2019-08-03 stsp unsigned char status;
5947 ae1e948a 2021-09-28 thomas
5948 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5949 ae1e948a 2021-09-28 thomas return NULL;
5950 5f8a88c6 2019-08-03 stsp
5951 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5952 afa376bf 2019-05-09 stsp ct_path++;
5953 5f8a88c6 2019-08-03 stsp
5954 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5955 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5956 5f8a88c6 2019-08-03 stsp else
5957 5f8a88c6 2019-08-03 stsp status = ct->status;
5958 5f8a88c6 2019-08-03 stsp
5959 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5960 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5961 036813ee 2019-05-09 stsp }
5962 036813ee 2019-05-09 stsp
5963 036813ee 2019-05-09 stsp static const struct got_error *
5964 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5965 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5966 44d03001 2019-05-09 stsp {
5967 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5968 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5969 44d03001 2019-05-09 stsp char *te_path;
5970 44d03001 2019-05-09 stsp
5971 44d03001 2019-05-09 stsp *modified = 0;
5972 44d03001 2019-05-09 stsp
5973 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5974 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5975 44d03001 2019-05-09 stsp te->name) == -1)
5976 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5977 44d03001 2019-05-09 stsp
5978 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5979 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5980 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5981 44d03001 2019-05-09 stsp strlen(te_path));
5982 62d463ca 2020-10-20 naddy if (*modified)
5983 44d03001 2019-05-09 stsp break;
5984 44d03001 2019-05-09 stsp }
5985 44d03001 2019-05-09 stsp
5986 44d03001 2019-05-09 stsp free(te_path);
5987 44d03001 2019-05-09 stsp return err;
5988 44d03001 2019-05-09 stsp }
5989 44d03001 2019-05-09 stsp
5990 44d03001 2019-05-09 stsp static const struct got_error *
5991 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5992 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5993 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5994 036813ee 2019-05-09 stsp {
5995 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5996 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5997 036813ee 2019-05-09 stsp
5998 036813ee 2019-05-09 stsp *ctp = NULL;
5999 036813ee 2019-05-09 stsp
6000 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6001 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6002 036813ee 2019-05-09 stsp char *ct_name = NULL;
6003 036813ee 2019-05-09 stsp int path_matches;
6004 036813ee 2019-05-09 stsp
6005 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6006 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
6007 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
6008 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
6009 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6010 5f8a88c6 2019-08-03 stsp continue;
6011 5f8a88c6 2019-08-03 stsp } else {
6012 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
6013 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
6014 5f8a88c6 2019-08-03 stsp continue;
6015 5f8a88c6 2019-08-03 stsp }
6016 036813ee 2019-05-09 stsp
6017 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6018 036813ee 2019-05-09 stsp continue;
6019 036813ee 2019-05-09 stsp
6020 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6021 62d463ca 2020-10-20 naddy if (err)
6022 036813ee 2019-05-09 stsp return err;
6023 036813ee 2019-05-09 stsp if (!path_matches)
6024 036813ee 2019-05-09 stsp continue;
6025 036813ee 2019-05-09 stsp
6026 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
6027 d34b633e 2020-10-19 stsp if (err)
6028 d34b633e 2020-10-19 stsp return err;
6029 d34b633e 2020-10-19 stsp
6030 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
6031 d34b633e 2020-10-19 stsp free(ct_name);
6032 036813ee 2019-05-09 stsp continue;
6033 d34b633e 2020-10-19 stsp }
6034 d34b633e 2020-10-19 stsp free(ct_name);
6035 036813ee 2019-05-09 stsp
6036 036813ee 2019-05-09 stsp *ctp = ct;
6037 036813ee 2019-05-09 stsp break;
6038 036813ee 2019-05-09 stsp }
6039 2b496619 2019-07-10 stsp
6040 2b496619 2019-07-10 stsp return err;
6041 2b496619 2019-07-10 stsp }
6042 2b496619 2019-07-10 stsp
6043 2b496619 2019-07-10 stsp static const struct got_error *
6044 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6045 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
6046 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
6047 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
6048 2b496619 2019-07-10 stsp struct got_repository *repo)
6049 2b496619 2019-07-10 stsp {
6050 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
6051 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
6052 2b496619 2019-07-10 stsp char *subtree_path;
6053 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
6054 ba580f68 2020-03-22 stsp int nentries;
6055 2b496619 2019-07-10 stsp
6056 2b496619 2019-07-10 stsp *new_tep = NULL;
6057 2b496619 2019-07-10 stsp
6058 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6059 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6060 2b496619 2019-07-10 stsp child_path) == -1)
6061 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6062 036813ee 2019-05-09 stsp
6063 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6064 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6065 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6066 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6067 56e0773d 2019-11-28 stsp
6068 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6069 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6070 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6071 2b496619 2019-07-10 stsp goto done;
6072 2b496619 2019-07-10 stsp }
6073 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6074 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6075 2b496619 2019-07-10 stsp if (err) {
6076 56e0773d 2019-11-28 stsp free(new_te);
6077 2b496619 2019-07-10 stsp goto done;
6078 2b496619 2019-07-10 stsp }
6079 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6080 2b496619 2019-07-10 stsp done:
6081 56e0773d 2019-11-28 stsp free(id);
6082 2b496619 2019-07-10 stsp free(subtree_path);
6083 2b496619 2019-07-10 stsp if (err == NULL)
6084 2b496619 2019-07-10 stsp *new_tep = new_te;
6085 036813ee 2019-05-09 stsp return err;
6086 036813ee 2019-05-09 stsp }
6087 036813ee 2019-05-09 stsp
6088 036813ee 2019-05-09 stsp static const struct got_error *
6089 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6090 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6091 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6092 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6093 036813ee 2019-05-09 stsp struct got_repository *repo)
6094 036813ee 2019-05-09 stsp {
6095 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6096 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6097 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6098 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6099 036813ee 2019-05-09 stsp
6100 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6101 ba580f68 2020-03-22 stsp *nentries = 0;
6102 036813ee 2019-05-09 stsp
6103 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6104 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6105 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6106 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6107 036813ee 2019-05-09 stsp
6108 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6109 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6110 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6111 036813ee 2019-05-09 stsp continue;
6112 036813ee 2019-05-09 stsp
6113 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6114 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6115 036813ee 2019-05-09 stsp continue;
6116 036813ee 2019-05-09 stsp
6117 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6118 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6119 036813ee 2019-05-09 stsp if (err)
6120 036813ee 2019-05-09 stsp goto done;
6121 036813ee 2019-05-09 stsp
6122 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6123 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6124 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6125 036813ee 2019-05-09 stsp if (err)
6126 036813ee 2019-05-09 stsp goto done;
6127 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6128 afa376bf 2019-05-09 stsp if (err)
6129 afa376bf 2019-05-09 stsp goto done;
6130 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6131 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6132 2b496619 2019-07-10 stsp if (err)
6133 2f51b5b3 2019-05-09 stsp goto done;
6134 ba580f68 2020-03-22 stsp (*nentries)++;
6135 2b496619 2019-07-10 stsp } else {
6136 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6137 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6138 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6139 2b496619 2019-07-10 stsp == NULL) {
6140 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6141 2b496619 2019-07-10 stsp child_path, path_base_tree,
6142 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6143 2b496619 2019-07-10 stsp repo);
6144 2b496619 2019-07-10 stsp if (err)
6145 2b496619 2019-07-10 stsp goto done;
6146 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6147 2b496619 2019-07-10 stsp if (err)
6148 2b496619 2019-07-10 stsp goto done;
6149 ba580f68 2020-03-22 stsp (*nentries)++;
6150 9ba0479c 2019-05-10 stsp }
6151 ed175427 2019-05-09 stsp }
6152 2f51b5b3 2019-05-09 stsp }
6153 2f51b5b3 2019-05-09 stsp
6154 2f51b5b3 2019-05-09 stsp if (base_tree) {
6155 56e0773d 2019-11-28 stsp int i, nbase_entries;
6156 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6157 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6158 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6159 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6160 63c5ca5d 2019-08-24 stsp
6161 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6162 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6163 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6164 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6165 63c5ca5d 2019-08-24 stsp if (err)
6166 63c5ca5d 2019-08-24 stsp goto done;
6167 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6168 63c5ca5d 2019-08-24 stsp if (err)
6169 63c5ca5d 2019-08-24 stsp goto done;
6170 ba580f68 2020-03-22 stsp (*nentries)++;
6171 63c5ca5d 2019-08-24 stsp continue;
6172 63c5ca5d 2019-08-24 stsp }
6173 2f51b5b3 2019-05-09 stsp
6174 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6175 44d03001 2019-05-09 stsp int modified;
6176 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6177 036813ee 2019-05-09 stsp if (err)
6178 036813ee 2019-05-09 stsp goto done;
6179 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6180 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6181 2f51b5b3 2019-05-09 stsp if (err)
6182 2f51b5b3 2019-05-09 stsp goto done;
6183 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6184 44d03001 2019-05-09 stsp if (modified) {
6185 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6186 ba580f68 2020-03-22 stsp int nsubentries;
6187 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6188 ba580f68 2020-03-22 stsp &nsubentries, te,
6189 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6190 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6191 44d03001 2019-05-09 stsp if (err)
6192 44d03001 2019-05-09 stsp goto done;
6193 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6194 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6195 ba580f68 2020-03-22 stsp free(new_id);
6196 ba580f68 2020-03-22 stsp continue;
6197 ba580f68 2020-03-22 stsp }
6198 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6199 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6200 56e0773d 2019-11-28 stsp free(new_id);
6201 44d03001 2019-05-09 stsp }
6202 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6203 036813ee 2019-05-09 stsp if (err)
6204 036813ee 2019-05-09 stsp goto done;
6205 ba580f68 2020-03-22 stsp (*nentries)++;
6206 2f51b5b3 2019-05-09 stsp continue;
6207 036813ee 2019-05-09 stsp }
6208 2f51b5b3 2019-05-09 stsp
6209 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6210 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6211 f66c734c 2019-09-22 stsp if (err)
6212 f66c734c 2019-09-22 stsp goto done;
6213 2f51b5b3 2019-05-09 stsp if (ct) {
6214 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6215 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6216 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6217 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6218 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6219 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6220 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6221 2f51b5b3 2019-05-09 stsp if (err)
6222 2f51b5b3 2019-05-09 stsp goto done;
6223 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6224 2f51b5b3 2019-05-09 stsp if (err)
6225 2f51b5b3 2019-05-09 stsp goto done;
6226 ba580f68 2020-03-22 stsp (*nentries)++;
6227 2f51b5b3 2019-05-09 stsp }
6228 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6229 b416585c 2019-05-13 stsp status_arg);
6230 afa376bf 2019-05-09 stsp if (err)
6231 afa376bf 2019-05-09 stsp goto done;
6232 2f51b5b3 2019-05-09 stsp } else {
6233 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6234 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6235 2f51b5b3 2019-05-09 stsp if (err)
6236 2f51b5b3 2019-05-09 stsp goto done;
6237 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6238 2f51b5b3 2019-05-09 stsp if (err)
6239 2f51b5b3 2019-05-09 stsp goto done;
6240 ba580f68 2020-03-22 stsp (*nentries)++;
6241 2f51b5b3 2019-05-09 stsp }
6242 ca2503ea 2019-05-09 stsp }
6243 ed175427 2019-05-09 stsp }
6244 0b5cc0d6 2019-05-09 stsp
6245 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6246 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6247 ed175427 2019-05-09 stsp done:
6248 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6249 2e1fa222 2020-07-23 stsp return err;
6250 2e1fa222 2020-07-23 stsp }
6251 2e1fa222 2020-07-23 stsp
6252 2e1fa222 2020-07-23 stsp static const struct got_error *
6253 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6254 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6255 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6256 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6257 ebf99748 2019-05-09 stsp {
6258 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6259 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6260 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6261 ebf99748 2019-05-09 stsp
6262 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6263 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6264 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6265 ebf99748 2019-05-09 stsp
6266 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6267 437adc9d 2020-12-10 yzhong
6268 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6269 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6270 437adc9d 2020-12-10 yzhong if (err)
6271 437adc9d 2020-12-10 yzhong goto done;
6272 437adc9d 2020-12-10 yzhong
6273 ebf99748 2019-05-09 stsp if (ie) {
6274 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6275 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6276 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6277 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6278 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6279 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6280 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6281 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6282 437adc9d 2020-12-10 yzhong
6283 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6284 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6285 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6286 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6287 72fd46fa 2019-09-06 stsp !have_staged_files);
6288 ebf99748 2019-05-09 stsp } else
6289 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6290 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6291 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6292 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6293 72fd46fa 2019-09-06 stsp !have_staged_files);
6294 ebf99748 2019-05-09 stsp } else {
6295 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6296 ebf99748 2019-05-09 stsp if (err)
6297 437adc9d 2020-12-10 yzhong goto done;
6298 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6299 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6300 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6301 3969253a 2020-03-07 stsp if (err) {
6302 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6303 437adc9d 2020-12-10 yzhong goto done;
6304 3969253a 2020-03-07 stsp }
6305 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6306 3969253a 2020-03-07 stsp if (err) {
6307 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6308 437adc9d 2020-12-10 yzhong goto done;
6309 3969253a 2020-03-07 stsp }
6310 ebf99748 2019-05-09 stsp }
6311 437adc9d 2020-12-10 yzhong free(relpath);
6312 437adc9d 2020-12-10 yzhong relpath = NULL;
6313 ebf99748 2019-05-09 stsp }
6314 437adc9d 2020-12-10 yzhong done:
6315 437adc9d 2020-12-10 yzhong free(relpath);
6316 d56d26ce 2019-05-10 stsp return err;
6317 d56d26ce 2019-05-10 stsp }
6318 735ef5ac 2019-08-03 stsp
6319 d56d26ce 2019-05-10 stsp
6320 d56d26ce 2019-05-10 stsp static const struct got_error *
6321 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6322 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6323 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6324 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6325 735ef5ac 2019-08-03 stsp int ood_errcode)
6326 d56d26ce 2019-05-10 stsp {
6327 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6328 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6329 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6330 1a36436d 2019-06-10 stsp
6331 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6332 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6333 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6334 1a36436d 2019-06-10 stsp return NULL;
6335 9bead371 2019-07-28 stsp /*
6336 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6337 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6338 9bead371 2019-07-28 stsp */
6339 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6340 945f9229 2022-04-16 thomas if (err)
6341 945f9229 2022-04-16 thomas goto done;
6342 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6343 9bead371 2019-07-28 stsp if (err) {
6344 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6345 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6346 1a36436d 2019-06-10 stsp goto done;
6347 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6348 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6349 1a36436d 2019-06-10 stsp } else {
6350 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6351 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6352 945f9229 2022-04-16 thomas if (err)
6353 945f9229 2022-04-16 thomas goto done;
6354 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6355 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6356 1a36436d 2019-06-10 stsp goto done;
6357 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6358 1a36436d 2019-06-10 stsp }
6359 1a36436d 2019-06-10 stsp done:
6360 1a36436d 2019-06-10 stsp free(id);
6361 945f9229 2022-04-16 thomas if (commit)
6362 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6363 1a36436d 2019-06-10 stsp return err;
6364 ebf99748 2019-05-09 stsp }
6365 ebf99748 2019-05-09 stsp
6366 ef20f542 2022-06-26 thomas static const struct got_error *
6367 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6368 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6369 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6370 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6371 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6372 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6373 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6374 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6375 afa376bf 2019-05-09 stsp struct got_repository *repo)
6376 c4296144 2019-05-09 stsp {
6377 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6378 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6379 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6380 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6381 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6382 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6383 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6384 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6385 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6386 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6387 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6388 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6389 892eab0a 2022-07-22 thomas time_t timestamp;
6390 c4296144 2019-05-09 stsp
6391 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6392 c4296144 2019-05-09 stsp
6393 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6394 675c7539 2019-05-09 stsp
6395 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6396 588edf97 2019-05-10 stsp if (err)
6397 588edf97 2019-05-10 stsp goto done;
6398 de18fc63 2019-05-09 stsp
6399 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6400 588edf97 2019-05-10 stsp if (err)
6401 588edf97 2019-05-10 stsp goto done;
6402 588edf97 2019-05-10 stsp
6403 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6404 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6405 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6406 33ad4cbe 2019-05-12 jcs if (err)
6407 33ad4cbe 2019-05-12 jcs goto done;
6408 33ad4cbe 2019-05-12 jcs }
6409 c4296144 2019-05-09 stsp
6410 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6411 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6412 33ad4cbe 2019-05-12 jcs goto done;
6413 33ad4cbe 2019-05-12 jcs }
6414 33ad4cbe 2019-05-12 jcs
6415 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6416 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6417 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6418 cf066bf8 2019-05-09 stsp char *ondisk_path;
6419 cf066bf8 2019-05-09 stsp
6420 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6421 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6422 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6423 5f8a88c6 2019-08-03 stsp continue;
6424 5f8a88c6 2019-08-03 stsp
6425 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6426 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6427 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6428 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6429 cf066bf8 2019-05-09 stsp continue;
6430 cf066bf8 2019-05-09 stsp
6431 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6432 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6433 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6434 cf066bf8 2019-05-09 stsp goto done;
6435 cf066bf8 2019-05-09 stsp }
6436 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6437 2e1fa222 2020-07-23 stsp free(ondisk_path);
6438 2e1fa222 2020-07-23 stsp if (err)
6439 cf066bf8 2019-05-09 stsp goto done;
6440 cf066bf8 2019-05-09 stsp }
6441 036813ee 2019-05-09 stsp
6442 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6443 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6444 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6445 036813ee 2019-05-09 stsp if (err)
6446 036813ee 2019-05-09 stsp goto done;
6447 036813ee 2019-05-09 stsp
6448 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6449 de18fc63 2019-05-09 stsp if (err)
6450 de18fc63 2019-05-09 stsp goto done;
6451 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6452 10604dce 2021-09-24 thomas nparents++;
6453 10604dce 2021-09-24 thomas if (parent_id2) {
6454 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6455 10604dce 2021-09-24 thomas if (err)
6456 10604dce 2021-09-24 thomas goto done;
6457 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6458 10604dce 2021-09-24 thomas nparents++;
6459 10604dce 2021-09-24 thomas }
6460 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6461 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6462 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6463 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6464 33ad4cbe 2019-05-12 jcs free(logmsg);
6465 9d40349a 2019-05-09 stsp if (err)
6466 9d40349a 2019-05-09 stsp goto done;
6467 9d40349a 2019-05-09 stsp
6468 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6469 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6470 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6471 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6472 09f5bd90 2019-05-09 stsp goto done;
6473 09f5bd90 2019-05-09 stsp }
6474 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6475 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6476 09f5bd90 2019-05-09 stsp if (err)
6477 09f5bd90 2019-05-09 stsp goto done;
6478 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6479 ebf99748 2019-05-09 stsp if (err)
6480 ebf99748 2019-05-09 stsp goto done;
6481 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6482 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6483 09f5bd90 2019-05-09 stsp goto done;
6484 09f5bd90 2019-05-09 stsp }
6485 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6486 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6487 f2c16586 2019-05-09 stsp if (err)
6488 f2c16586 2019-05-09 stsp goto done;
6489 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6490 f2c16586 2019-05-09 stsp if (err)
6491 f2c16586 2019-05-09 stsp goto done;
6492 f2c16586 2019-05-09 stsp
6493 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6494 09f5bd90 2019-05-09 stsp if (err)
6495 09f5bd90 2019-05-09 stsp goto done;
6496 09f5bd90 2019-05-09 stsp
6497 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6498 ebf99748 2019-05-09 stsp if (err)
6499 ebf99748 2019-05-09 stsp goto done;
6500 c4296144 2019-05-09 stsp done:
6501 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6502 588edf97 2019-05-10 stsp if (head_tree)
6503 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6504 588edf97 2019-05-10 stsp if (head_commit)
6505 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6506 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6507 2f17228e 2019-05-12 stsp if (head_ref2) {
6508 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6509 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6510 2f17228e 2019-05-12 stsp err = unlockerr;
6511 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6512 39cd0ff6 2019-07-12 stsp }
6513 39cd0ff6 2019-07-12 stsp return err;
6514 39cd0ff6 2019-07-12 stsp }
6515 5c1e53bc 2019-07-28 stsp
6516 5c1e53bc 2019-07-28 stsp static const struct got_error *
6517 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6518 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6519 5c1e53bc 2019-07-28 stsp {
6520 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6521 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6522 39cd0ff6 2019-07-12 stsp
6523 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6524 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6525 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6526 5c1e53bc 2019-07-28 stsp
6527 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6528 5c1e53bc 2019-07-28 stsp ct_path++;
6529 5c1e53bc 2019-07-28 stsp
6530 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6531 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6532 5c1e53bc 2019-07-28 stsp break;
6533 5c1e53bc 2019-07-28 stsp }
6534 5c1e53bc 2019-07-28 stsp
6535 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6536 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6537 5c1e53bc 2019-07-28 stsp
6538 5c1e53bc 2019-07-28 stsp return NULL;
6539 5c1e53bc 2019-07-28 stsp }
6540 5c1e53bc 2019-07-28 stsp
6541 f0b75401 2019-08-03 stsp static const struct got_error *
6542 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6543 f0b75401 2019-08-03 stsp {
6544 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6545 f0b75401 2019-08-03 stsp
6546 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6547 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6548 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6549 f0b75401 2019-08-03 stsp }
6550 f0b75401 2019-08-03 stsp
6551 f0b75401 2019-08-03 stsp return NULL;
6552 f0b75401 2019-08-03 stsp }
6553 f0b75401 2019-08-03 stsp
6554 5f8a88c6 2019-08-03 stsp static const struct got_error *
6555 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6556 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6557 5f8a88c6 2019-08-03 stsp {
6558 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6559 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6560 5f8a88c6 2019-08-03 stsp
6561 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6562 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6563 5f8a88c6 2019-08-03 stsp continue;
6564 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6565 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6566 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6567 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6568 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6569 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6570 5f8a88c6 2019-08-03 stsp }
6571 5f8a88c6 2019-08-03 stsp
6572 5f8a88c6 2019-08-03 stsp return NULL;
6573 5f8a88c6 2019-08-03 stsp }
6574 5f8a88c6 2019-08-03 stsp
6575 39cd0ff6 2019-07-12 stsp const struct got_error *
6576 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6577 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6578 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6579 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6580 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6581 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6582 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6583 39cd0ff6 2019-07-12 stsp {
6584 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6585 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6586 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6587 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6588 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6589 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6590 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6591 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6592 ef899790 2022-11-01 thomas char *diff_path = NULL;
6593 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6594 39cd0ff6 2019-07-12 stsp
6595 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6596 39cd0ff6 2019-07-12 stsp
6597 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6598 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6599 39cd0ff6 2019-07-12 stsp
6600 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6601 39cd0ff6 2019-07-12 stsp if (err)
6602 39cd0ff6 2019-07-12 stsp goto done;
6603 39cd0ff6 2019-07-12 stsp
6604 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6605 39cd0ff6 2019-07-12 stsp if (err)
6606 39cd0ff6 2019-07-12 stsp goto done;
6607 39cd0ff6 2019-07-12 stsp
6608 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6609 39cd0ff6 2019-07-12 stsp if (err)
6610 39cd0ff6 2019-07-12 stsp goto done;
6611 39cd0ff6 2019-07-12 stsp
6612 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6613 39cd0ff6 2019-07-12 stsp if (err)
6614 39cd0ff6 2019-07-12 stsp goto done;
6615 39cd0ff6 2019-07-12 stsp
6616 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6617 5f8a88c6 2019-08-03 stsp &have_staged_files);
6618 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6619 5f8a88c6 2019-08-03 stsp goto done;
6620 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6621 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6622 5f8a88c6 2019-08-03 stsp if (err)
6623 5f8a88c6 2019-08-03 stsp goto done;
6624 5f8a88c6 2019-08-03 stsp }
6625 5f8a88c6 2019-08-03 stsp
6626 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6627 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6628 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6629 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6630 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6631 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6632 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6633 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6634 ef899790 2022-11-01 thomas if (show_diff) {
6635 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6636 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6637 ef899790 2022-11-01 thomas if (err)
6638 ef899790 2022-11-01 thomas goto done;
6639 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6640 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6641 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6642 ef899790 2022-11-01 thomas goto done;
6643 ef899790 2022-11-01 thomas }
6644 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6645 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6646 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6647 ef899790 2022-11-01 thomas goto done;
6648 ef899790 2022-11-01 thomas }
6649 ef899790 2022-11-01 thomas }
6650 ef899790 2022-11-01 thomas
6651 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6652 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6653 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6654 5c1e53bc 2019-07-28 stsp if (err)
6655 5c1e53bc 2019-07-28 stsp goto done;
6656 5c1e53bc 2019-07-28 stsp }
6657 39cd0ff6 2019-07-12 stsp
6658 ef899790 2022-11-01 thomas if (show_diff) {
6659 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6660 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6661 ef899790 2022-11-01 thomas goto done;
6662 ef899790 2022-11-01 thomas }
6663 ef899790 2022-11-01 thomas }
6664 ef899790 2022-11-01 thomas
6665 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6666 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6667 39cd0ff6 2019-07-12 stsp goto done;
6668 5c1e53bc 2019-07-28 stsp }
6669 5c1e53bc 2019-07-28 stsp
6670 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6671 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6672 5c1e53bc 2019-07-28 stsp if (err)
6673 5c1e53bc 2019-07-28 stsp goto done;
6674 39cd0ff6 2019-07-12 stsp }
6675 f0b75401 2019-08-03 stsp
6676 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6677 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6678 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6679 f0b75401 2019-08-03 stsp
6680 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6681 f0b75401 2019-08-03 stsp ct_path++;
6682 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6683 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6684 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6685 b50cabdf 2019-07-12 stsp if (err)
6686 b50cabdf 2019-07-12 stsp goto done;
6687 f0b75401 2019-08-03 stsp
6688 b50cabdf 2019-07-12 stsp }
6689 b50cabdf 2019-07-12 stsp
6690 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6691 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6692 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6693 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6694 39cd0ff6 2019-07-12 stsp if (err)
6695 39cd0ff6 2019-07-12 stsp goto done;
6696 39cd0ff6 2019-07-12 stsp
6697 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6698 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6699 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6700 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6701 39cd0ff6 2019-07-12 stsp err = sync_err;
6702 39cd0ff6 2019-07-12 stsp done:
6703 39cd0ff6 2019-07-12 stsp if (fileindex)
6704 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6705 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6706 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6707 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6708 39cd0ff6 2019-07-12 stsp err = unlockerr;
6709 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6710 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6711 21c2d8be 2023-01-10 thomas
6712 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6713 39cd0ff6 2019-07-12 stsp }
6714 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6715 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6716 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6717 ef899790 2022-11-01 thomas free(diff_path);
6718 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6719 ef899790 2022-11-01 thomas err == NULL)
6720 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6721 c4296144 2019-05-09 stsp return err;
6722 8656d6c4 2019-05-20 stsp }
6723 8656d6c4 2019-05-20 stsp
6724 8656d6c4 2019-05-20 stsp const char *
6725 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6726 8656d6c4 2019-05-20 stsp {
6727 8656d6c4 2019-05-20 stsp return ct->path;
6728 8656d6c4 2019-05-20 stsp }
6729 8656d6c4 2019-05-20 stsp
6730 8656d6c4 2019-05-20 stsp unsigned int
6731 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6732 8656d6c4 2019-05-20 stsp {
6733 8656d6c4 2019-05-20 stsp return ct->status;
6734 818c7501 2019-07-11 stsp }
6735 818c7501 2019-07-11 stsp
6736 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6737 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6738 818c7501 2019-07-11 stsp struct got_repository *repo;
6739 818c7501 2019-07-11 stsp };
6740 818c7501 2019-07-11 stsp
6741 818c7501 2019-07-11 stsp static const struct got_error *
6742 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6743 818c7501 2019-07-11 stsp {
6744 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6745 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6746 818c7501 2019-07-11 stsp unsigned char status;
6747 818c7501 2019-07-11 stsp struct stat sb;
6748 818c7501 2019-07-11 stsp char *ondisk_path;
6749 818c7501 2019-07-11 stsp
6750 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6751 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6752 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6753 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6754 818c7501 2019-07-11 stsp
6755 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6756 818c7501 2019-07-11 stsp == -1)
6757 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6758 818c7501 2019-07-11 stsp
6759 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6760 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6761 818c7501 2019-07-11 stsp free(ondisk_path);
6762 818c7501 2019-07-11 stsp if (err)
6763 818c7501 2019-07-11 stsp return err;
6764 818c7501 2019-07-11 stsp
6765 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6766 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6767 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6768 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6769 818c7501 2019-07-11 stsp
6770 818c7501 2019-07-11 stsp return NULL;
6771 818c7501 2019-07-11 stsp }
6772 818c7501 2019-07-11 stsp
6773 818c7501 2019-07-11 stsp const struct got_error *
6774 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6775 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6776 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6777 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6778 818c7501 2019-07-11 stsp {
6779 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6780 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6781 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6782 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6783 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6784 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6785 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6786 818c7501 2019-07-11 stsp
6787 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6788 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6789 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6790 818c7501 2019-07-11 stsp
6791 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6792 818c7501 2019-07-11 stsp if (err)
6793 818c7501 2019-07-11 stsp return err;
6794 818c7501 2019-07-11 stsp
6795 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6796 818c7501 2019-07-11 stsp if (err)
6797 818c7501 2019-07-11 stsp goto done;
6798 818c7501 2019-07-11 stsp
6799 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6800 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6801 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6802 818c7501 2019-07-11 stsp &ok_arg);
6803 818c7501 2019-07-11 stsp if (err)
6804 818c7501 2019-07-11 stsp goto done;
6805 818c7501 2019-07-11 stsp
6806 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6807 818c7501 2019-07-11 stsp if (err)
6808 818c7501 2019-07-11 stsp goto done;
6809 818c7501 2019-07-11 stsp
6810 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6811 818c7501 2019-07-11 stsp if (err)
6812 818c7501 2019-07-11 stsp goto done;
6813 818c7501 2019-07-11 stsp
6814 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6815 818c7501 2019-07-11 stsp if (err)
6816 818c7501 2019-07-11 stsp goto done;
6817 818c7501 2019-07-11 stsp
6818 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6819 818c7501 2019-07-11 stsp 0);
6820 e51d7b55 2020-01-04 stsp if (err)
6821 e51d7b55 2020-01-04 stsp goto done;
6822 e51d7b55 2020-01-04 stsp
6823 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6824 818c7501 2019-07-11 stsp if (err)
6825 818c7501 2019-07-11 stsp goto done;
6826 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6827 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6828 e51d7b55 2020-01-04 stsp goto done;
6829 e51d7b55 2020-01-04 stsp }
6830 818c7501 2019-07-11 stsp
6831 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6832 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6833 818c7501 2019-07-11 stsp if (err)
6834 818c7501 2019-07-11 stsp goto done;
6835 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6836 818c7501 2019-07-11 stsp if (err)
6837 818c7501 2019-07-11 stsp goto done;
6838 818c7501 2019-07-11 stsp
6839 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6840 818c7501 2019-07-11 stsp
6841 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6842 818c7501 2019-07-11 stsp if (err)
6843 818c7501 2019-07-11 stsp goto done;
6844 818c7501 2019-07-11 stsp
6845 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6846 818c7501 2019-07-11 stsp if (err)
6847 818c7501 2019-07-11 stsp goto done;
6848 818c7501 2019-07-11 stsp
6849 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6850 818c7501 2019-07-11 stsp worktree->base_commit_id);
6851 818c7501 2019-07-11 stsp if (err)
6852 818c7501 2019-07-11 stsp goto done;
6853 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6854 818c7501 2019-07-11 stsp if (err)
6855 818c7501 2019-07-11 stsp goto done;
6856 818c7501 2019-07-11 stsp
6857 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6858 818c7501 2019-07-11 stsp if (err)
6859 818c7501 2019-07-11 stsp goto done;
6860 818c7501 2019-07-11 stsp done:
6861 818c7501 2019-07-11 stsp free(fileindex_path);
6862 818c7501 2019-07-11 stsp free(tmp_branch_name);
6863 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6864 818c7501 2019-07-11 stsp free(branch_ref_name);
6865 818c7501 2019-07-11 stsp if (branch_ref)
6866 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6867 818c7501 2019-07-11 stsp if (wt_branch)
6868 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6869 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6870 818c7501 2019-07-11 stsp if (err) {
6871 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6872 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6873 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6874 818c7501 2019-07-11 stsp }
6875 818c7501 2019-07-11 stsp if (*tmp_branch) {
6876 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6877 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6878 818c7501 2019-07-11 stsp }
6879 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6880 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6881 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6882 3e3a69f1 2019-07-25 stsp }
6883 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6884 818c7501 2019-07-11 stsp }
6885 818c7501 2019-07-11 stsp return err;
6886 818c7501 2019-07-11 stsp }
6887 818c7501 2019-07-11 stsp
6888 818c7501 2019-07-11 stsp const struct got_error *
6889 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6890 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6891 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6892 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6893 818c7501 2019-07-11 stsp {
6894 818c7501 2019-07-11 stsp const struct got_error *err;
6895 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6896 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6897 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6898 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6899 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6900 818c7501 2019-07-11 stsp
6901 818c7501 2019-07-11 stsp *commit_id = NULL;
6902 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6903 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6904 3e3a69f1 2019-07-25 stsp *branch = NULL;
6905 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6906 3e3a69f1 2019-07-25 stsp
6907 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6908 3e3a69f1 2019-07-25 stsp if (err)
6909 3e3a69f1 2019-07-25 stsp return err;
6910 818c7501 2019-07-11 stsp
6911 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6912 3e3a69f1 2019-07-25 stsp if (err)
6913 f032f1f7 2019-08-04 stsp goto done;
6914 f032f1f7 2019-08-04 stsp
6915 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6916 f032f1f7 2019-08-04 stsp &have_staged_files);
6917 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6918 f032f1f7 2019-08-04 stsp goto done;
6919 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6920 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6921 3e3a69f1 2019-07-25 stsp goto done;
6922 f032f1f7 2019-08-04 stsp }
6923 3e3a69f1 2019-07-25 stsp
6924 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6925 818c7501 2019-07-11 stsp if (err)
6926 f032f1f7 2019-08-04 stsp goto done;
6927 818c7501 2019-07-11 stsp
6928 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6929 818c7501 2019-07-11 stsp if (err)
6930 818c7501 2019-07-11 stsp goto done;
6931 818c7501 2019-07-11 stsp
6932 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6933 818c7501 2019-07-11 stsp if (err)
6934 818c7501 2019-07-11 stsp goto done;
6935 818c7501 2019-07-11 stsp
6936 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6937 818c7501 2019-07-11 stsp if (err)
6938 818c7501 2019-07-11 stsp goto done;
6939 818c7501 2019-07-11 stsp
6940 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6941 818c7501 2019-07-11 stsp if (err)
6942 818c7501 2019-07-11 stsp goto done;
6943 818c7501 2019-07-11 stsp
6944 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6945 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6946 818c7501 2019-07-11 stsp if (err)
6947 818c7501 2019-07-11 stsp goto done;
6948 818c7501 2019-07-11 stsp
6949 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6950 818c7501 2019-07-11 stsp if (err)
6951 818c7501 2019-07-11 stsp goto done;
6952 818c7501 2019-07-11 stsp
6953 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6954 818c7501 2019-07-11 stsp if (err)
6955 818c7501 2019-07-11 stsp goto done;
6956 818c7501 2019-07-11 stsp
6957 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6958 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6959 818c7501 2019-07-11 stsp if (err)
6960 818c7501 2019-07-11 stsp goto done;
6961 818c7501 2019-07-11 stsp
6962 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6963 818c7501 2019-07-11 stsp if (err)
6964 818c7501 2019-07-11 stsp goto done;
6965 818c7501 2019-07-11 stsp done:
6966 818c7501 2019-07-11 stsp free(commit_ref_name);
6967 818c7501 2019-07-11 stsp free(branch_ref_name);
6968 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6969 818c7501 2019-07-11 stsp if (commit_ref)
6970 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6971 818c7501 2019-07-11 stsp if (branch_ref)
6972 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6973 818c7501 2019-07-11 stsp if (err) {
6974 818c7501 2019-07-11 stsp free(*commit_id);
6975 818c7501 2019-07-11 stsp *commit_id = NULL;
6976 818c7501 2019-07-11 stsp if (*tmp_branch) {
6977 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6978 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6979 818c7501 2019-07-11 stsp }
6980 818c7501 2019-07-11 stsp if (*new_base_branch) {
6981 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6982 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6983 818c7501 2019-07-11 stsp }
6984 818c7501 2019-07-11 stsp if (*branch) {
6985 818c7501 2019-07-11 stsp got_ref_close(*branch);
6986 818c7501 2019-07-11 stsp *branch = NULL;
6987 818c7501 2019-07-11 stsp }
6988 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6989 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6990 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6991 3e3a69f1 2019-07-25 stsp }
6992 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6993 818c7501 2019-07-11 stsp }
6994 818c7501 2019-07-11 stsp return err;
6995 c4296144 2019-05-09 stsp }
6996 818c7501 2019-07-11 stsp
6997 818c7501 2019-07-11 stsp const struct got_error *
6998 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6999 818c7501 2019-07-11 stsp {
7000 818c7501 2019-07-11 stsp const struct got_error *err;
7001 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
7002 818c7501 2019-07-11 stsp
7003 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7004 818c7501 2019-07-11 stsp if (err)
7005 818c7501 2019-07-11 stsp return err;
7006 818c7501 2019-07-11 stsp
7007 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7008 818c7501 2019-07-11 stsp free(tmp_branch_name);
7009 818c7501 2019-07-11 stsp return NULL;
7010 818c7501 2019-07-11 stsp }
7011 818c7501 2019-07-11 stsp
7012 818c7501 2019-07-11 stsp static const struct got_error *
7013 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7014 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7015 818c7501 2019-07-11 stsp {
7016 0ebf8283 2019-07-24 stsp *logmsg = arg;
7017 818c7501 2019-07-11 stsp return NULL;
7018 818c7501 2019-07-11 stsp }
7019 818c7501 2019-07-11 stsp
7020 818c7501 2019-07-11 stsp static const struct got_error *
7021 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7022 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7023 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7024 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7025 818c7501 2019-07-11 stsp {
7026 818c7501 2019-07-11 stsp return NULL;
7027 01757395 2019-07-12 stsp }
7028 01757395 2019-07-12 stsp
7029 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
7030 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
7031 01757395 2019-07-12 stsp void *progress_arg;
7032 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
7033 01757395 2019-07-12 stsp };
7034 01757395 2019-07-12 stsp
7035 01757395 2019-07-12 stsp static const struct got_error *
7036 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
7037 01757395 2019-07-12 stsp {
7038 01757395 2019-07-12 stsp const struct got_error *err;
7039 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
7040 01757395 2019-07-12 stsp char *p;
7041 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
7042 01757395 2019-07-12 stsp
7043 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
7044 01757395 2019-07-12 stsp if (err)
7045 01757395 2019-07-12 stsp return err;
7046 01757395 2019-07-12 stsp
7047 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
7048 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
7049 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
7050 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
7051 01757395 2019-07-12 stsp return NULL;
7052 01757395 2019-07-12 stsp
7053 01757395 2019-07-12 stsp p = strdup(path);
7054 01757395 2019-07-12 stsp if (p == NULL)
7055 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7056 01757395 2019-07-12 stsp
7057 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7058 01757395 2019-07-12 stsp if (err || new == NULL)
7059 01757395 2019-07-12 stsp free(p);
7060 01757395 2019-07-12 stsp return err;
7061 818c7501 2019-07-11 stsp }
7062 818c7501 2019-07-11 stsp
7063 0ebf8283 2019-07-24 stsp static const struct got_error *
7064 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7065 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7066 818c7501 2019-07-11 stsp {
7067 818c7501 2019-07-11 stsp const struct got_error *err;
7068 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7069 818c7501 2019-07-11 stsp
7070 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7071 818c7501 2019-07-11 stsp if (err) {
7072 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7073 818c7501 2019-07-11 stsp goto done;
7074 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7075 818c7501 2019-07-11 stsp if (err)
7076 818c7501 2019-07-11 stsp goto done;
7077 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7078 818c7501 2019-07-11 stsp if (err)
7079 818c7501 2019-07-11 stsp goto done;
7080 de05890f 2020-03-05 stsp } else if (is_rebase) {
7081 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7082 818c7501 2019-07-11 stsp int cmp;
7083 818c7501 2019-07-11 stsp
7084 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7085 818c7501 2019-07-11 stsp if (err)
7086 818c7501 2019-07-11 stsp goto done;
7087 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7088 818c7501 2019-07-11 stsp free(stored_id);
7089 818c7501 2019-07-11 stsp if (cmp != 0) {
7090 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7091 818c7501 2019-07-11 stsp goto done;
7092 818c7501 2019-07-11 stsp }
7093 818c7501 2019-07-11 stsp }
7094 0ebf8283 2019-07-24 stsp done:
7095 0ebf8283 2019-07-24 stsp if (commit_ref)
7096 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7097 0ebf8283 2019-07-24 stsp return err;
7098 0ebf8283 2019-07-24 stsp }
7099 0ebf8283 2019-07-24 stsp
7100 0ebf8283 2019-07-24 stsp static const struct got_error *
7101 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7102 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7103 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7104 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7105 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7106 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7107 0ebf8283 2019-07-24 stsp {
7108 0ebf8283 2019-07-24 stsp const struct got_error *err;
7109 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7110 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7111 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7112 818c7501 2019-07-11 stsp
7113 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7114 0ebf8283 2019-07-24 stsp
7115 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7116 0ebf8283 2019-07-24 stsp if (err)
7117 0ebf8283 2019-07-24 stsp return err;
7118 0ebf8283 2019-07-24 stsp
7119 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7120 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7121 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7122 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7123 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7124 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7125 818c7501 2019-07-11 stsp if (commit_ref)
7126 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7127 818c7501 2019-07-11 stsp return err;
7128 818c7501 2019-07-11 stsp }
7129 818c7501 2019-07-11 stsp
7130 818c7501 2019-07-11 stsp const struct got_error *
7131 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7132 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7133 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7134 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7135 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7136 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7137 818c7501 2019-07-11 stsp {
7138 0ebf8283 2019-07-24 stsp const struct got_error *err;
7139 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7140 0ebf8283 2019-07-24 stsp
7141 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7142 0ebf8283 2019-07-24 stsp if (err)
7143 0ebf8283 2019-07-24 stsp return err;
7144 0ebf8283 2019-07-24 stsp
7145 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7146 0ebf8283 2019-07-24 stsp if (err)
7147 0ebf8283 2019-07-24 stsp goto done;
7148 0ebf8283 2019-07-24 stsp
7149 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7150 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7151 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7152 0ebf8283 2019-07-24 stsp done:
7153 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7154 0ebf8283 2019-07-24 stsp return err;
7155 0ebf8283 2019-07-24 stsp }
7156 0ebf8283 2019-07-24 stsp
7157 0ebf8283 2019-07-24 stsp const struct got_error *
7158 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7159 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7160 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7161 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7162 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7163 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7164 0ebf8283 2019-07-24 stsp {
7165 0ebf8283 2019-07-24 stsp const struct got_error *err;
7166 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7167 0ebf8283 2019-07-24 stsp
7168 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7169 0ebf8283 2019-07-24 stsp if (err)
7170 0ebf8283 2019-07-24 stsp return err;
7171 0ebf8283 2019-07-24 stsp
7172 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7173 0ebf8283 2019-07-24 stsp if (err)
7174 0ebf8283 2019-07-24 stsp goto done;
7175 0ebf8283 2019-07-24 stsp
7176 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7177 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7178 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7179 0ebf8283 2019-07-24 stsp done:
7180 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7181 0ebf8283 2019-07-24 stsp return err;
7182 0ebf8283 2019-07-24 stsp }
7183 0ebf8283 2019-07-24 stsp
7184 0ebf8283 2019-07-24 stsp static const struct got_error *
7185 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7186 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7187 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7188 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7189 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7190 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7191 0ebf8283 2019-07-24 stsp {
7192 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7193 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7194 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7195 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7196 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7197 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7198 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7199 818c7501 2019-07-11 stsp
7200 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7201 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7202 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7203 a0e95631 2019-07-12 stsp
7204 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7205 818c7501 2019-07-11 stsp
7206 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7207 a0e95631 2019-07-12 stsp if (err)
7208 3e3a69f1 2019-07-25 stsp return err;
7209 a0e95631 2019-07-12 stsp
7210 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7211 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7212 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7213 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7214 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7215 01757395 2019-07-12 stsp /*
7216 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7217 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7218 6c13b005 2021-09-02 stsp *
7219 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7220 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7221 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7222 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7223 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7224 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7225 01757395 2019-07-12 stsp */
7226 01757395 2019-07-12 stsp if (merged_paths) {
7227 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7228 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7229 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7230 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7231 f2a9dc41 2019-12-13 tracey 0);
7232 01757395 2019-07-12 stsp if (err)
7233 01757395 2019-07-12 stsp goto done;
7234 01757395 2019-07-12 stsp }
7235 01757395 2019-07-12 stsp } else {
7236 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7237 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7238 01757395 2019-07-12 stsp if (err)
7239 01757395 2019-07-12 stsp goto done;
7240 01757395 2019-07-12 stsp }
7241 a0e95631 2019-07-12 stsp
7242 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7243 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7244 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7245 a0e95631 2019-07-12 stsp if (err)
7246 a0e95631 2019-07-12 stsp goto done;
7247 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7248 a0e95631 2019-07-12 stsp goto done;
7249 ff0d2220 2019-07-11 stsp }
7250 818c7501 2019-07-11 stsp
7251 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7252 edd02c5e 2019-07-11 stsp if (err)
7253 edd02c5e 2019-07-11 stsp goto done;
7254 edd02c5e 2019-07-11 stsp
7255 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7256 818c7501 2019-07-11 stsp if (err)
7257 818c7501 2019-07-11 stsp goto done;
7258 818c7501 2019-07-11 stsp
7259 5943eee2 2019-08-13 stsp if (new_logmsg) {
7260 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7261 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7262 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7263 5943eee2 2019-08-13 stsp goto done;
7264 5943eee2 2019-08-13 stsp }
7265 5943eee2 2019-08-13 stsp } else {
7266 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7267 5943eee2 2019-08-13 stsp if (err)
7268 5943eee2 2019-08-13 stsp goto done;
7269 5943eee2 2019-08-13 stsp }
7270 0ebf8283 2019-07-24 stsp
7271 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7272 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7273 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7274 8db00f97 2022-07-22 thomas committer ? committer :
7275 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7276 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7277 a0e95631 2019-07-12 stsp if (err)
7278 a0e95631 2019-07-12 stsp goto done;
7279 a0e95631 2019-07-12 stsp
7280 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7281 a0e95631 2019-07-12 stsp if (err)
7282 a0e95631 2019-07-12 stsp goto done;
7283 a0e95631 2019-07-12 stsp
7284 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7285 a0e95631 2019-07-12 stsp if (err)
7286 a0e95631 2019-07-12 stsp goto done;
7287 a0e95631 2019-07-12 stsp
7288 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7289 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7290 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7291 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7292 a0e95631 2019-07-12 stsp err = sync_err;
7293 818c7501 2019-07-11 stsp done:
7294 a0e95631 2019-07-12 stsp free(fileindex_path);
7295 a0e95631 2019-07-12 stsp free(head_commit_id);
7296 a0e95631 2019-07-12 stsp if (head_ref)
7297 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7298 818c7501 2019-07-11 stsp if (err) {
7299 818c7501 2019-07-11 stsp free(*new_commit_id);
7300 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7301 818c7501 2019-07-11 stsp }
7302 818c7501 2019-07-11 stsp return err;
7303 818c7501 2019-07-11 stsp }
7304 818c7501 2019-07-11 stsp
7305 818c7501 2019-07-11 stsp const struct got_error *
7306 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7307 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7308 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7309 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7310 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7311 be94c032 2023-02-20 thomas struct got_repository *repo)
7312 0ebf8283 2019-07-24 stsp {
7313 0ebf8283 2019-07-24 stsp const struct got_error *err;
7314 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7315 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7316 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7317 0ebf8283 2019-07-24 stsp
7318 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7319 0ebf8283 2019-07-24 stsp if (err)
7320 0ebf8283 2019-07-24 stsp return err;
7321 0ebf8283 2019-07-24 stsp
7322 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7323 0ebf8283 2019-07-24 stsp if (err)
7324 0ebf8283 2019-07-24 stsp goto done;
7325 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7326 0ebf8283 2019-07-24 stsp if (err)
7327 0ebf8283 2019-07-24 stsp goto done;
7328 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7329 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7330 0ebf8283 2019-07-24 stsp goto done;
7331 0ebf8283 2019-07-24 stsp }
7332 0ebf8283 2019-07-24 stsp
7333 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7334 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7335 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7336 0ebf8283 2019-07-24 stsp done:
7337 0ebf8283 2019-07-24 stsp if (commit_ref)
7338 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7339 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7340 0ebf8283 2019-07-24 stsp free(commit_id);
7341 0ebf8283 2019-07-24 stsp return err;
7342 0ebf8283 2019-07-24 stsp }
7343 0ebf8283 2019-07-24 stsp
7344 0ebf8283 2019-07-24 stsp const struct got_error *
7345 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7346 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7347 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7348 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7349 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7350 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7351 0ebf8283 2019-07-24 stsp {
7352 0ebf8283 2019-07-24 stsp const struct got_error *err;
7353 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7354 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7355 0ebf8283 2019-07-24 stsp
7356 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7357 0ebf8283 2019-07-24 stsp if (err)
7358 0ebf8283 2019-07-24 stsp return err;
7359 0ebf8283 2019-07-24 stsp
7360 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7361 0ebf8283 2019-07-24 stsp if (err)
7362 0ebf8283 2019-07-24 stsp goto done;
7363 0ebf8283 2019-07-24 stsp
7364 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7365 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7366 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7367 0ebf8283 2019-07-24 stsp done:
7368 0ebf8283 2019-07-24 stsp if (commit_ref)
7369 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7370 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7371 0ebf8283 2019-07-24 stsp return err;
7372 0ebf8283 2019-07-24 stsp }
7373 0ebf8283 2019-07-24 stsp
7374 0ebf8283 2019-07-24 stsp const struct got_error *
7375 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7376 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7377 818c7501 2019-07-11 stsp {
7378 3e3a69f1 2019-07-25 stsp if (fileindex)
7379 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7380 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7381 69844fba 2019-07-11 stsp }
7382 69844fba 2019-07-11 stsp
7383 69844fba 2019-07-11 stsp static const struct got_error *
7384 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7385 69844fba 2019-07-11 stsp {
7386 69844fba 2019-07-11 stsp const struct got_error *err;
7387 69844fba 2019-07-11 stsp struct got_reference *ref;
7388 69844fba 2019-07-11 stsp
7389 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7390 69844fba 2019-07-11 stsp if (err) {
7391 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7392 69844fba 2019-07-11 stsp return NULL;
7393 69844fba 2019-07-11 stsp return err;
7394 69844fba 2019-07-11 stsp }
7395 69844fba 2019-07-11 stsp
7396 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7397 69844fba 2019-07-11 stsp got_ref_close(ref);
7398 69844fba 2019-07-11 stsp return err;
7399 818c7501 2019-07-11 stsp }
7400 818c7501 2019-07-11 stsp
7401 69844fba 2019-07-11 stsp static const struct got_error *
7402 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7403 69844fba 2019-07-11 stsp {
7404 69844fba 2019-07-11 stsp const struct got_error *err;
7405 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7406 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7407 69844fba 2019-07-11 stsp
7408 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7409 69844fba 2019-07-11 stsp if (err)
7410 69844fba 2019-07-11 stsp goto done;
7411 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7412 69844fba 2019-07-11 stsp if (err)
7413 69844fba 2019-07-11 stsp goto done;
7414 69844fba 2019-07-11 stsp
7415 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7416 69844fba 2019-07-11 stsp if (err)
7417 69844fba 2019-07-11 stsp goto done;
7418 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7419 69844fba 2019-07-11 stsp if (err)
7420 69844fba 2019-07-11 stsp goto done;
7421 69844fba 2019-07-11 stsp
7422 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7423 69844fba 2019-07-11 stsp if (err)
7424 69844fba 2019-07-11 stsp goto done;
7425 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7426 69844fba 2019-07-11 stsp if (err)
7427 69844fba 2019-07-11 stsp goto done;
7428 69844fba 2019-07-11 stsp
7429 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7430 69844fba 2019-07-11 stsp if (err)
7431 69844fba 2019-07-11 stsp goto done;
7432 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7433 69844fba 2019-07-11 stsp if (err)
7434 69844fba 2019-07-11 stsp goto done;
7435 69844fba 2019-07-11 stsp
7436 69844fba 2019-07-11 stsp done:
7437 69844fba 2019-07-11 stsp free(tmp_branch_name);
7438 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7439 69844fba 2019-07-11 stsp free(branch_ref_name);
7440 69844fba 2019-07-11 stsp free(commit_ref_name);
7441 e600f124 2021-03-21 stsp return err;
7442 e600f124 2021-03-21 stsp }
7443 e600f124 2021-03-21 stsp
7444 ef20f542 2022-06-26 thomas static const struct got_error *
7445 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7446 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7447 e600f124 2021-03-21 stsp {
7448 e600f124 2021-03-21 stsp const struct got_error *err;
7449 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7450 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7451 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7452 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7453 e600f124 2021-03-21 stsp char *refname = NULL;
7454 e600f124 2021-03-21 stsp
7455 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7456 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7457 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7458 e600f124 2021-03-21 stsp branch_name += 11;
7459 e600f124 2021-03-21 stsp
7460 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7461 e600f124 2021-03-21 stsp if (err)
7462 e600f124 2021-03-21 stsp return err;
7463 e600f124 2021-03-21 stsp
7464 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7465 e600f124 2021-03-21 stsp new_id_str) == -1) {
7466 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7467 e600f124 2021-03-21 stsp goto done;
7468 e600f124 2021-03-21 stsp }
7469 e600f124 2021-03-21 stsp
7470 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7471 e600f124 2021-03-21 stsp if (err)
7472 e600f124 2021-03-21 stsp goto done;
7473 e600f124 2021-03-21 stsp
7474 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7475 e600f124 2021-03-21 stsp if (err)
7476 e600f124 2021-03-21 stsp goto done;
7477 e600f124 2021-03-21 stsp
7478 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7479 e600f124 2021-03-21 stsp done:
7480 e600f124 2021-03-21 stsp free(new_id_str);
7481 e600f124 2021-03-21 stsp free(refname);
7482 e600f124 2021-03-21 stsp free(old_commit_id);
7483 e600f124 2021-03-21 stsp if (ref)
7484 e600f124 2021-03-21 stsp got_ref_close(ref);
7485 69844fba 2019-07-11 stsp return err;
7486 69844fba 2019-07-11 stsp }
7487 69844fba 2019-07-11 stsp
7488 818c7501 2019-07-11 stsp const struct got_error *
7489 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7490 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7491 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7492 f08f28be 2023-02-03 thomas int create_backup)
7493 818c7501 2019-07-11 stsp {
7494 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7495 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7496 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7497 818c7501 2019-07-11 stsp
7498 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7499 818c7501 2019-07-11 stsp if (err)
7500 818c7501 2019-07-11 stsp return err;
7501 e600f124 2021-03-21 stsp
7502 e600f124 2021-03-21 stsp if (create_backup) {
7503 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7504 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7505 e600f124 2021-03-21 stsp if (err)
7506 e600f124 2021-03-21 stsp goto done;
7507 e600f124 2021-03-21 stsp }
7508 818c7501 2019-07-11 stsp
7509 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7510 818c7501 2019-07-11 stsp if (err)
7511 818c7501 2019-07-11 stsp goto done;
7512 818c7501 2019-07-11 stsp
7513 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7514 818c7501 2019-07-11 stsp if (err)
7515 818c7501 2019-07-11 stsp goto done;
7516 818c7501 2019-07-11 stsp
7517 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7518 818c7501 2019-07-11 stsp if (err)
7519 818c7501 2019-07-11 stsp goto done;
7520 818c7501 2019-07-11 stsp
7521 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7522 a615e0e7 2020-12-16 stsp if (err)
7523 a615e0e7 2020-12-16 stsp goto done;
7524 a615e0e7 2020-12-16 stsp
7525 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7526 a615e0e7 2020-12-16 stsp if (err)
7527 a615e0e7 2020-12-16 stsp goto done;
7528 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7529 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7530 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7531 a615e0e7 2020-12-16 stsp err = sync_err;
7532 818c7501 2019-07-11 stsp done:
7533 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7534 a615e0e7 2020-12-16 stsp free(fileindex_path);
7535 818c7501 2019-07-11 stsp free(new_head_commit_id);
7536 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7537 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7538 818c7501 2019-07-11 stsp err = unlockerr;
7539 818c7501 2019-07-11 stsp return err;
7540 818c7501 2019-07-11 stsp }
7541 8641a332 2023-04-14 thomas
7542 8641a332 2023-04-14 thomas static const struct got_error *
7543 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7544 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7545 8641a332 2023-04-14 thomas struct got_repository *repo)
7546 8641a332 2023-04-14 thomas {
7547 8641a332 2023-04-14 thomas const struct got_error *err;
7548 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7549 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7550 8641a332 2023-04-14 thomas
7551 8641a332 2023-04-14 thomas if (id1) {
7552 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
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 err = got_object_open_as_tree(&tree1, repo,
7557 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7558 8641a332 2023-04-14 thomas if (err)
7559 8641a332 2023-04-14 thomas goto done;
7560 8641a332 2023-04-14 thomas }
7561 818c7501 2019-07-11 stsp
7562 8641a332 2023-04-14 thomas if (id2) {
7563 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7564 8641a332 2023-04-14 thomas if (err)
7565 8641a332 2023-04-14 thomas goto done;
7566 8641a332 2023-04-14 thomas
7567 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7568 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7569 8641a332 2023-04-14 thomas if (err)
7570 8641a332 2023-04-14 thomas goto done;
7571 8641a332 2023-04-14 thomas }
7572 8641a332 2023-04-14 thomas
7573 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7574 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7575 8641a332 2023-04-14 thomas if (err)
7576 8641a332 2023-04-14 thomas goto done;
7577 8641a332 2023-04-14 thomas done:
7578 8641a332 2023-04-14 thomas if (commit1)
7579 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7580 8641a332 2023-04-14 thomas if (commit2)
7581 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7582 8641a332 2023-04-14 thomas if (tree1)
7583 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7584 8641a332 2023-04-14 thomas if (tree2)
7585 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7586 8641a332 2023-04-14 thomas return err;
7587 8641a332 2023-04-14 thomas }
7588 8641a332 2023-04-14 thomas
7589 8641a332 2023-04-14 thomas static const struct got_error *
7590 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7591 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7592 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7593 8641a332 2023-04-14 thomas {
7594 8641a332 2023-04-14 thomas const struct got_error *err;
7595 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7596 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7597 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7598 8641a332 2023-04-14 thomas
7599 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7600 8641a332 2023-04-14 thomas
7601 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7602 8641a332 2023-04-14 thomas if (err)
7603 8641a332 2023-04-14 thomas goto done;
7604 8641a332 2023-04-14 thomas
7605 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7606 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7607 8641a332 2023-04-14 thomas
7608 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7609 8641a332 2023-04-14 thomas continue;
7610 8641a332 2023-04-14 thomas
7611 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7612 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7613 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7614 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7615 8641a332 2023-04-14 thomas goto done;
7616 8641a332 2023-04-14 thomas }
7617 8641a332 2023-04-14 thomas } else {
7618 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7619 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7620 8641a332 2023-04-14 thomas goto done;
7621 8641a332 2023-04-14 thomas }
7622 8641a332 2023-04-14 thomas
7623 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7624 8641a332 2023-04-14 thomas path_prefix, abspath);
7625 8641a332 2023-04-14 thomas if (err)
7626 8641a332 2023-04-14 thomas goto done;
7627 8641a332 2023-04-14 thomas free(abspath);
7628 8641a332 2023-04-14 thomas abspath = NULL;
7629 8641a332 2023-04-14 thomas }
7630 8641a332 2023-04-14 thomas
7631 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7632 8641a332 2023-04-14 thomas if (err)
7633 8641a332 2023-04-14 thomas goto done;
7634 8641a332 2023-04-14 thomas wt_path = NULL;
7635 8641a332 2023-04-14 thomas }
7636 8641a332 2023-04-14 thomas
7637 8641a332 2023-04-14 thomas done:
7638 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7639 8641a332 2023-04-14 thomas free(abspath);
7640 8641a332 2023-04-14 thomas free(wt_path);
7641 8641a332 2023-04-14 thomas return err;
7642 8641a332 2023-04-14 thomas }
7643 8641a332 2023-04-14 thomas
7644 8641a332 2023-04-14 thomas static const struct got_error *
7645 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7646 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7647 8641a332 2023-04-14 thomas struct got_repository *repo)
7648 8641a332 2023-04-14 thomas {
7649 8641a332 2023-04-14 thomas const struct got_error *err;
7650 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7651 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7652 8641a332 2023-04-14 thomas
7653 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7654 8641a332 2023-04-14 thomas if (err)
7655 8641a332 2023-04-14 thomas goto done;
7656 8641a332 2023-04-14 thomas
7657 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7658 8641a332 2023-04-14 thomas
7659 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7660 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7661 8641a332 2023-04-14 thomas if (err)
7662 8641a332 2023-04-14 thomas goto done;
7663 8641a332 2023-04-14 thomas done:
7664 8641a332 2023-04-14 thomas if (commit)
7665 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7666 8641a332 2023-04-14 thomas return err;
7667 8641a332 2023-04-14 thomas }
7668 8641a332 2023-04-14 thomas
7669 818c7501 2019-07-11 stsp const struct got_error *
7670 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7671 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7672 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7673 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7674 818c7501 2019-07-11 stsp {
7675 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7676 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7677 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7678 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7679 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7680 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7681 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7682 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7683 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7684 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7685 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7686 818c7501 2019-07-11 stsp
7687 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7688 8641a332 2023-04-14 thomas
7689 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7690 818c7501 2019-07-11 stsp if (err)
7691 818c7501 2019-07-11 stsp return err;
7692 8641a332 2023-04-14 thomas
7693 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7694 8641a332 2023-04-14 thomas if (err)
7695 8641a332 2023-04-14 thomas goto done;
7696 8641a332 2023-04-14 thomas
7697 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7698 8641a332 2023-04-14 thomas if (err)
7699 8641a332 2023-04-14 thomas goto done;
7700 8641a332 2023-04-14 thomas
7701 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7702 8641a332 2023-04-14 thomas if (err)
7703 8641a332 2023-04-14 thomas goto done;
7704 945f9229 2022-04-16 thomas
7705 8641a332 2023-04-14 thomas /*
7706 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7707 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7708 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7709 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7710 8641a332 2023-04-14 thomas */
7711 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7712 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7713 8641a332 2023-04-14 thomas if (err)
7714 8641a332 2023-04-14 thomas goto done;
7715 8641a332 2023-04-14 thomas
7716 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7717 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7718 818c7501 2019-07-11 stsp if (err)
7719 818c7501 2019-07-11 stsp goto done;
7720 818c7501 2019-07-11 stsp
7721 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7722 818c7501 2019-07-11 stsp if (err)
7723 818c7501 2019-07-11 stsp goto done;
7724 818c7501 2019-07-11 stsp
7725 818c7501 2019-07-11 stsp /*
7726 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7727 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7728 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7729 818c7501 2019-07-11 stsp */
7730 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7731 818c7501 2019-07-11 stsp if (err)
7732 818c7501 2019-07-11 stsp goto done;
7733 818c7501 2019-07-11 stsp
7734 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7735 25a102ed 2023-04-14 thomas if (err)
7736 25a102ed 2023-04-14 thomas goto done;
7737 25a102ed 2023-04-14 thomas
7738 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7739 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7740 818c7501 2019-07-11 stsp if (err)
7741 818c7501 2019-07-11 stsp goto done;
7742 818c7501 2019-07-11 stsp
7743 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7744 945f9229 2022-04-16 thomas worktree->path_prefix);
7745 ca355955 2019-07-12 stsp if (err)
7746 ca355955 2019-07-12 stsp goto done;
7747 ca355955 2019-07-12 stsp
7748 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7749 ca355955 2019-07-12 stsp if (err)
7750 ca355955 2019-07-12 stsp goto done;
7751 ca355955 2019-07-12 stsp
7752 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7753 818c7501 2019-07-11 stsp if (err)
7754 818c7501 2019-07-11 stsp goto done;
7755 818c7501 2019-07-11 stsp
7756 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7757 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7758 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7759 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7760 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7761 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7762 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7763 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7764 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7765 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7766 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7767 55bd499d 2019-07-12 stsp if (err)
7768 1f1abb7e 2019-08-08 stsp goto sync;
7769 55bd499d 2019-07-12 stsp
7770 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7771 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7772 ca355955 2019-07-12 stsp sync:
7773 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7774 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7775 ca355955 2019-07-12 stsp err = sync_err;
7776 818c7501 2019-07-11 stsp done:
7777 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7778 818c7501 2019-07-11 stsp got_ref_close(resolved);
7779 a3a2faf2 2019-07-12 stsp free(tree_id);
7780 818c7501 2019-07-11 stsp free(commit_id);
7781 8641a332 2023-04-14 thomas free(merged_commit_id);
7782 945f9229 2022-04-16 thomas if (commit)
7783 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7784 0ebf8283 2019-07-24 stsp if (fileindex)
7785 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7786 0ebf8283 2019-07-24 stsp free(fileindex_path);
7787 8641a332 2023-04-14 thomas free(commit_ref_name);
7788 8641a332 2023-04-14 thomas if (commit_ref)
7789 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7790 0ebf8283 2019-07-24 stsp
7791 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7792 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7793 0ebf8283 2019-07-24 stsp err = unlockerr;
7794 0ebf8283 2019-07-24 stsp return err;
7795 0ebf8283 2019-07-24 stsp }
7796 0ebf8283 2019-07-24 stsp
7797 0ebf8283 2019-07-24 stsp const struct got_error *
7798 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7799 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7800 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7801 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7802 0ebf8283 2019-07-24 stsp {
7803 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7804 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7805 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7806 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7807 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7808 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7809 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7810 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7811 0ebf8283 2019-07-24 stsp
7812 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7813 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7814 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7815 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7816 0ebf8283 2019-07-24 stsp
7817 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7818 0ebf8283 2019-07-24 stsp if (err)
7819 0ebf8283 2019-07-24 stsp return err;
7820 0ebf8283 2019-07-24 stsp
7821 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7822 0ebf8283 2019-07-24 stsp if (err)
7823 0ebf8283 2019-07-24 stsp goto done;
7824 0ebf8283 2019-07-24 stsp
7825 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7826 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7827 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7828 0ebf8283 2019-07-24 stsp &ok_arg);
7829 0ebf8283 2019-07-24 stsp if (err)
7830 0ebf8283 2019-07-24 stsp goto done;
7831 0ebf8283 2019-07-24 stsp
7832 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7833 0ebf8283 2019-07-24 stsp if (err)
7834 0ebf8283 2019-07-24 stsp goto done;
7835 0ebf8283 2019-07-24 stsp
7836 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7837 0ebf8283 2019-07-24 stsp if (err)
7838 0ebf8283 2019-07-24 stsp goto done;
7839 0ebf8283 2019-07-24 stsp
7840 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7841 0ebf8283 2019-07-24 stsp worktree);
7842 0ebf8283 2019-07-24 stsp if (err)
7843 0ebf8283 2019-07-24 stsp goto done;
7844 0ebf8283 2019-07-24 stsp
7845 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7846 0ebf8283 2019-07-24 stsp 0);
7847 0ebf8283 2019-07-24 stsp if (err)
7848 0ebf8283 2019-07-24 stsp goto done;
7849 0ebf8283 2019-07-24 stsp
7850 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7851 0ebf8283 2019-07-24 stsp if (err)
7852 0ebf8283 2019-07-24 stsp goto done;
7853 0ebf8283 2019-07-24 stsp
7854 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7855 0ebf8283 2019-07-24 stsp if (err)
7856 0ebf8283 2019-07-24 stsp goto done;
7857 0ebf8283 2019-07-24 stsp
7858 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7859 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7860 0ebf8283 2019-07-24 stsp if (err)
7861 0ebf8283 2019-07-24 stsp goto done;
7862 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7863 0ebf8283 2019-07-24 stsp if (err)
7864 0ebf8283 2019-07-24 stsp goto done;
7865 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7866 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7867 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7868 0ebf8283 2019-07-24 stsp goto done;
7869 0ebf8283 2019-07-24 stsp }
7870 0ebf8283 2019-07-24 stsp
7871 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7872 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7873 0ebf8283 2019-07-24 stsp if (err)
7874 0ebf8283 2019-07-24 stsp goto done;
7875 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7876 0ebf8283 2019-07-24 stsp if (err)
7877 0ebf8283 2019-07-24 stsp goto done;
7878 0ebf8283 2019-07-24 stsp
7879 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7880 0ebf8283 2019-07-24 stsp if (err)
7881 0ebf8283 2019-07-24 stsp goto done;
7882 0ebf8283 2019-07-24 stsp done:
7883 0ebf8283 2019-07-24 stsp free(fileindex_path);
7884 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7885 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7886 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7887 0ebf8283 2019-07-24 stsp if (wt_branch)
7888 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7889 0ebf8283 2019-07-24 stsp if (err) {
7890 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7891 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7892 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7893 0ebf8283 2019-07-24 stsp }
7894 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7895 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7896 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7897 0ebf8283 2019-07-24 stsp }
7898 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7899 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7900 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7901 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7902 3e3a69f1 2019-07-25 stsp }
7903 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7904 0ebf8283 2019-07-24 stsp }
7905 0ebf8283 2019-07-24 stsp return err;
7906 0ebf8283 2019-07-24 stsp }
7907 0ebf8283 2019-07-24 stsp
7908 0ebf8283 2019-07-24 stsp const struct got_error *
7909 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7910 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7911 0ebf8283 2019-07-24 stsp {
7912 3e3a69f1 2019-07-25 stsp if (fileindex)
7913 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7914 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
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_in_progress(int *in_progress,
7919 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7920 0ebf8283 2019-07-24 stsp {
7921 0ebf8283 2019-07-24 stsp const struct got_error *err;
7922 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7923 0ebf8283 2019-07-24 stsp
7924 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7925 0ebf8283 2019-07-24 stsp if (err)
7926 0ebf8283 2019-07-24 stsp return err;
7927 0ebf8283 2019-07-24 stsp
7928 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7929 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7930 0ebf8283 2019-07-24 stsp return NULL;
7931 0ebf8283 2019-07-24 stsp }
7932 0ebf8283 2019-07-24 stsp
7933 0ebf8283 2019-07-24 stsp const struct got_error *
7934 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7935 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7936 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7937 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7938 0ebf8283 2019-07-24 stsp {
7939 0ebf8283 2019-07-24 stsp const struct got_error *err;
7940 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7941 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7942 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7943 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7944 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7945 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7946 0ebf8283 2019-07-24 stsp
7947 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7948 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7949 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7950 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7951 0ebf8283 2019-07-24 stsp
7952 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7953 3e3a69f1 2019-07-25 stsp if (err)
7954 3e3a69f1 2019-07-25 stsp return err;
7955 3e3a69f1 2019-07-25 stsp
7956 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7957 3e3a69f1 2019-07-25 stsp if (err)
7958 f032f1f7 2019-08-04 stsp goto done;
7959 f032f1f7 2019-08-04 stsp
7960 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7961 f032f1f7 2019-08-04 stsp &have_staged_files);
7962 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7963 f032f1f7 2019-08-04 stsp goto done;
7964 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7965 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7966 3e3a69f1 2019-07-25 stsp goto done;
7967 f032f1f7 2019-08-04 stsp }
7968 3e3a69f1 2019-07-25 stsp
7969 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7970 0ebf8283 2019-07-24 stsp if (err)
7971 f032f1f7 2019-08-04 stsp goto done;
7972 0ebf8283 2019-07-24 stsp
7973 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7974 0ebf8283 2019-07-24 stsp if (err)
7975 0ebf8283 2019-07-24 stsp goto done;
7976 0ebf8283 2019-07-24 stsp
7977 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
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 = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7982 0ebf8283 2019-07-24 stsp worktree);
7983 0ebf8283 2019-07-24 stsp if (err)
7984 0ebf8283 2019-07-24 stsp goto done;
7985 0ebf8283 2019-07-24 stsp
7986 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7987 0ebf8283 2019-07-24 stsp if (err)
7988 0ebf8283 2019-07-24 stsp goto done;
7989 0ebf8283 2019-07-24 stsp
7990 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7991 0ebf8283 2019-07-24 stsp if (err)
7992 0ebf8283 2019-07-24 stsp goto done;
7993 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7994 0ebf8283 2019-07-24 stsp if (err)
7995 0ebf8283 2019-07-24 stsp goto done;
7996 0ebf8283 2019-07-24 stsp
7997 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7998 0ebf8283 2019-07-24 stsp if (err)
7999 0ebf8283 2019-07-24 stsp goto done;
8000 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8001 0ebf8283 2019-07-24 stsp if (err)
8002 0ebf8283 2019-07-24 stsp goto done;
8003 0ebf8283 2019-07-24 stsp
8004 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8005 0ebf8283 2019-07-24 stsp if (err)
8006 0ebf8283 2019-07-24 stsp goto done;
8007 0ebf8283 2019-07-24 stsp done:
8008 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8009 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8010 3e3a69f1 2019-07-25 stsp free(fileindex_path);
8011 0ebf8283 2019-07-24 stsp if (commit_ref)
8012 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
8013 0ebf8283 2019-07-24 stsp if (base_commit_ref)
8014 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
8015 0ebf8283 2019-07-24 stsp if (err) {
8016 0ebf8283 2019-07-24 stsp free(*commit_id);
8017 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8018 0ebf8283 2019-07-24 stsp free(*base_commit_id);
8019 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8020 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
8021 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
8022 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8023 0ebf8283 2019-07-24 stsp }
8024 3e3a69f1 2019-07-25 stsp if (*fileindex) {
8025 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
8026 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8027 3e3a69f1 2019-07-25 stsp }
8028 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
8029 0ebf8283 2019-07-24 stsp }
8030 0ebf8283 2019-07-24 stsp return err;
8031 0ebf8283 2019-07-24 stsp }
8032 0ebf8283 2019-07-24 stsp
8033 0ebf8283 2019-07-24 stsp static const struct got_error *
8034 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8035 0ebf8283 2019-07-24 stsp {
8036 0ebf8283 2019-07-24 stsp const struct got_error *err;
8037 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8038 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
8039 0ebf8283 2019-07-24 stsp
8040 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8041 0ebf8283 2019-07-24 stsp if (err)
8042 0ebf8283 2019-07-24 stsp goto done;
8043 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
8044 0ebf8283 2019-07-24 stsp if (err)
8045 0ebf8283 2019-07-24 stsp goto done;
8046 0ebf8283 2019-07-24 stsp
8047 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8048 0ebf8283 2019-07-24 stsp worktree);
8049 0ebf8283 2019-07-24 stsp if (err)
8050 0ebf8283 2019-07-24 stsp goto done;
8051 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
8052 0ebf8283 2019-07-24 stsp if (err)
8053 0ebf8283 2019-07-24 stsp goto done;
8054 0ebf8283 2019-07-24 stsp
8055 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8056 0ebf8283 2019-07-24 stsp if (err)
8057 0ebf8283 2019-07-24 stsp goto done;
8058 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8059 0ebf8283 2019-07-24 stsp if (err)
8060 0ebf8283 2019-07-24 stsp goto done;
8061 0ebf8283 2019-07-24 stsp
8062 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8063 0ebf8283 2019-07-24 stsp if (err)
8064 0ebf8283 2019-07-24 stsp goto done;
8065 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8066 0ebf8283 2019-07-24 stsp if (err)
8067 0ebf8283 2019-07-24 stsp goto done;
8068 0ebf8283 2019-07-24 stsp done:
8069 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8070 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8071 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8072 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8073 0ebf8283 2019-07-24 stsp return err;
8074 0ebf8283 2019-07-24 stsp }
8075 0ebf8283 2019-07-24 stsp
8076 0ebf8283 2019-07-24 stsp const struct got_error *
8077 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8078 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8079 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8080 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8081 0ebf8283 2019-07-24 stsp {
8082 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8083 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8084 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8085 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8086 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8087 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8088 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8089 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8090 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8091 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8092 0ebf8283 2019-07-24 stsp
8093 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8094 8641a332 2023-04-14 thomas
8095 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8096 0ebf8283 2019-07-24 stsp if (err)
8097 0ebf8283 2019-07-24 stsp return err;
8098 945f9229 2022-04-16 thomas
8099 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8100 8641a332 2023-04-14 thomas if (err)
8101 8641a332 2023-04-14 thomas goto done;
8102 8641a332 2023-04-14 thomas
8103 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8104 8641a332 2023-04-14 thomas if (err) {
8105 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
8106 8641a332 2023-04-14 thomas goto done;
8107 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
8108 8641a332 2023-04-14 thomas commit_ref = NULL;
8109 8641a332 2023-04-14 thomas }
8110 8641a332 2023-04-14 thomas
8111 8641a332 2023-04-14 thomas if (commit_ref) {
8112 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8113 8641a332 2023-04-14 thomas if (err)
8114 8641a332 2023-04-14 thomas goto done;
8115 8641a332 2023-04-14 thomas
8116 8641a332 2023-04-14 thomas /*
8117 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8118 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8119 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
8120 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
8121 8641a332 2023-04-14 thomas */
8122 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8123 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8124 8641a332 2023-04-14 thomas if (err)
8125 8641a332 2023-04-14 thomas goto done;
8126 8641a332 2023-04-14 thomas }
8127 8641a332 2023-04-14 thomas
8128 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8129 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8130 0ebf8283 2019-07-24 stsp if (err)
8131 0ebf8283 2019-07-24 stsp goto done;
8132 0ebf8283 2019-07-24 stsp
8133 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8134 0ebf8283 2019-07-24 stsp if (err)
8135 0ebf8283 2019-07-24 stsp goto done;
8136 0ebf8283 2019-07-24 stsp
8137 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8138 0ebf8283 2019-07-24 stsp if (err)
8139 0ebf8283 2019-07-24 stsp goto done;
8140 0ebf8283 2019-07-24 stsp
8141 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
8142 25a102ed 2023-04-14 thomas worktree->base_commit_id);
8143 25a102ed 2023-04-14 thomas if (err)
8144 25a102ed 2023-04-14 thomas goto done;
8145 25a102ed 2023-04-14 thomas
8146 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8147 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8148 0ebf8283 2019-07-24 stsp if (err)
8149 0ebf8283 2019-07-24 stsp goto done;
8150 0ebf8283 2019-07-24 stsp
8151 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8152 0ebf8283 2019-07-24 stsp if (err)
8153 0ebf8283 2019-07-24 stsp goto done;
8154 0ebf8283 2019-07-24 stsp
8155 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8156 0ebf8283 2019-07-24 stsp if (err)
8157 0ebf8283 2019-07-24 stsp goto done;
8158 0ebf8283 2019-07-24 stsp
8159 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8160 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8161 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8162 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8163 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8164 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8165 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8166 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
8167 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8168 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8169 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8170 0ebf8283 2019-07-24 stsp if (err)
8171 1f1abb7e 2019-08-08 stsp goto sync;
8172 0ebf8283 2019-07-24 stsp
8173 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8174 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8175 0ebf8283 2019-07-24 stsp sync:
8176 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8177 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8178 0ebf8283 2019-07-24 stsp err = sync_err;
8179 0ebf8283 2019-07-24 stsp done:
8180 8641a332 2023-04-14 thomas if (resolved)
8181 8641a332 2023-04-14 thomas got_ref_close(resolved);
8182 8641a332 2023-04-14 thomas if (commit_ref)
8183 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8184 8641a332 2023-04-14 thomas free(merged_commit_id);
8185 0ebf8283 2019-07-24 stsp free(tree_id);
8186 818c7501 2019-07-11 stsp free(fileindex_path);
8187 8641a332 2023-04-14 thomas free(commit_ref_name);
8188 818c7501 2019-07-11 stsp
8189 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8190 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8191 818c7501 2019-07-11 stsp err = unlockerr;
8192 818c7501 2019-07-11 stsp return err;
8193 818c7501 2019-07-11 stsp }
8194 0ebf8283 2019-07-24 stsp
8195 0ebf8283 2019-07-24 stsp const struct got_error *
8196 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8197 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8198 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8199 0ebf8283 2019-07-24 stsp {
8200 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8201 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8202 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8203 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8204 0ebf8283 2019-07-24 stsp
8205 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8206 0ebf8283 2019-07-24 stsp if (err)
8207 0ebf8283 2019-07-24 stsp return err;
8208 0ebf8283 2019-07-24 stsp
8209 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8210 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8211 e600f124 2021-03-21 stsp if (err)
8212 e600f124 2021-03-21 stsp goto done;
8213 e600f124 2021-03-21 stsp
8214 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8215 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8216 0ebf8283 2019-07-24 stsp if (err)
8217 0ebf8283 2019-07-24 stsp goto done;
8218 0ebf8283 2019-07-24 stsp
8219 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8220 0ebf8283 2019-07-24 stsp if (err)
8221 0ebf8283 2019-07-24 stsp goto done;
8222 0ebf8283 2019-07-24 stsp
8223 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8224 0ebf8283 2019-07-24 stsp if (err)
8225 0ebf8283 2019-07-24 stsp goto done;
8226 0ebf8283 2019-07-24 stsp
8227 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8228 0ebf8283 2019-07-24 stsp if (err)
8229 0ebf8283 2019-07-24 stsp goto done;
8230 0ebf8283 2019-07-24 stsp
8231 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8232 a615e0e7 2020-12-16 stsp if (err)
8233 a615e0e7 2020-12-16 stsp goto done;
8234 a615e0e7 2020-12-16 stsp
8235 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8236 a615e0e7 2020-12-16 stsp if (err)
8237 a615e0e7 2020-12-16 stsp goto done;
8238 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8239 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8240 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8241 a615e0e7 2020-12-16 stsp err = sync_err;
8242 0ebf8283 2019-07-24 stsp done:
8243 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8244 a615e0e7 2020-12-16 stsp free(fileindex_path);
8245 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8246 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8247 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8248 0ebf8283 2019-07-24 stsp err = unlockerr;
8249 0ebf8283 2019-07-24 stsp return err;
8250 0ebf8283 2019-07-24 stsp }
8251 0ebf8283 2019-07-24 stsp
8252 0ebf8283 2019-07-24 stsp const struct got_error *
8253 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8254 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8255 0ebf8283 2019-07-24 stsp {
8256 0ebf8283 2019-07-24 stsp const struct got_error *err;
8257 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8258 0ebf8283 2019-07-24 stsp
8259 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8260 0ebf8283 2019-07-24 stsp if (err)
8261 0ebf8283 2019-07-24 stsp return err;
8262 0ebf8283 2019-07-24 stsp
8263 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8264 0ebf8283 2019-07-24 stsp if (err)
8265 0ebf8283 2019-07-24 stsp goto done;
8266 0ebf8283 2019-07-24 stsp
8267 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8268 0ebf8283 2019-07-24 stsp done:
8269 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8270 2822a352 2019-10-15 stsp return err;
8271 2822a352 2019-10-15 stsp }
8272 2822a352 2019-10-15 stsp
8273 2822a352 2019-10-15 stsp const struct got_error *
8274 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8275 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8276 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8277 2822a352 2019-10-15 stsp struct got_repository *repo)
8278 2822a352 2019-10-15 stsp {
8279 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8280 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8281 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8282 2822a352 2019-10-15 stsp
8283 2822a352 2019-10-15 stsp *fileindex = NULL;
8284 2822a352 2019-10-15 stsp *branch_ref = NULL;
8285 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8286 2822a352 2019-10-15 stsp
8287 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8288 2822a352 2019-10-15 stsp if (err)
8289 2822a352 2019-10-15 stsp return err;
8290 2822a352 2019-10-15 stsp
8291 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8292 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8293 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8294 2822a352 2019-10-15 stsp "update -b or different branch name required");
8295 2822a352 2019-10-15 stsp goto done;
8296 2822a352 2019-10-15 stsp }
8297 2822a352 2019-10-15 stsp
8298 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8299 2822a352 2019-10-15 stsp if (err)
8300 2822a352 2019-10-15 stsp goto done;
8301 2822a352 2019-10-15 stsp
8302 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8303 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8304 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8305 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8306 2822a352 2019-10-15 stsp &ok_arg);
8307 2822a352 2019-10-15 stsp if (err)
8308 2822a352 2019-10-15 stsp goto done;
8309 2822a352 2019-10-15 stsp
8310 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8311 2822a352 2019-10-15 stsp if (err)
8312 2822a352 2019-10-15 stsp goto done;
8313 2822a352 2019-10-15 stsp
8314 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8315 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8316 2822a352 2019-10-15 stsp done:
8317 2822a352 2019-10-15 stsp if (err) {
8318 2822a352 2019-10-15 stsp if (*branch_ref) {
8319 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8320 2822a352 2019-10-15 stsp *branch_ref = NULL;
8321 2822a352 2019-10-15 stsp }
8322 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8323 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8324 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8325 2822a352 2019-10-15 stsp }
8326 2822a352 2019-10-15 stsp if (*fileindex) {
8327 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8328 2822a352 2019-10-15 stsp *fileindex = NULL;
8329 2822a352 2019-10-15 stsp }
8330 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8331 2822a352 2019-10-15 stsp }
8332 0cb83759 2019-08-03 stsp return err;
8333 0cb83759 2019-08-03 stsp }
8334 0cb83759 2019-08-03 stsp
8335 2822a352 2019-10-15 stsp const struct got_error *
8336 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8337 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8338 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8339 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8340 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8341 2822a352 2019-10-15 stsp {
8342 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8343 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8344 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8345 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8346 2822a352 2019-10-15 stsp
8347 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8348 2822a352 2019-10-15 stsp if (err)
8349 2822a352 2019-10-15 stsp goto done;
8350 2822a352 2019-10-15 stsp
8351 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8352 2822a352 2019-10-15 stsp if (err)
8353 2822a352 2019-10-15 stsp goto done;
8354 2822a352 2019-10-15 stsp
8355 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8356 945f9229 2022-04-16 thomas if (err)
8357 945f9229 2022-04-16 thomas goto done;
8358 945f9229 2022-04-16 thomas
8359 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8360 2822a352 2019-10-15 stsp worktree->path_prefix);
8361 2822a352 2019-10-15 stsp if (err)
8362 2822a352 2019-10-15 stsp goto done;
8363 2822a352 2019-10-15 stsp
8364 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8365 2822a352 2019-10-15 stsp if (err)
8366 2822a352 2019-10-15 stsp goto done;
8367 2822a352 2019-10-15 stsp
8368 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8369 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8370 2822a352 2019-10-15 stsp if (err)
8371 2822a352 2019-10-15 stsp goto sync;
8372 2822a352 2019-10-15 stsp
8373 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8374 2822a352 2019-10-15 stsp if (err)
8375 2822a352 2019-10-15 stsp goto sync;
8376 2822a352 2019-10-15 stsp
8377 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8378 6e210706 2021-01-22 stsp if (err)
8379 6e210706 2021-01-22 stsp goto sync;
8380 6e210706 2021-01-22 stsp
8381 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8382 2822a352 2019-10-15 stsp sync:
8383 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8384 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8385 2822a352 2019-10-15 stsp err = sync_err;
8386 2822a352 2019-10-15 stsp
8387 2822a352 2019-10-15 stsp done:
8388 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8389 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8390 2822a352 2019-10-15 stsp err = unlockerr;
8391 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8392 2822a352 2019-10-15 stsp
8393 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8394 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8395 2822a352 2019-10-15 stsp err = unlockerr;
8396 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8397 2822a352 2019-10-15 stsp
8398 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8399 2822a352 2019-10-15 stsp free(fileindex_path);
8400 2822a352 2019-10-15 stsp free(tree_id);
8401 945f9229 2022-04-16 thomas if (commit)
8402 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8403 2822a352 2019-10-15 stsp
8404 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8405 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8406 2822a352 2019-10-15 stsp err = unlockerr;
8407 2822a352 2019-10-15 stsp return err;
8408 2822a352 2019-10-15 stsp }
8409 2822a352 2019-10-15 stsp
8410 2822a352 2019-10-15 stsp const struct got_error *
8411 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8412 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8413 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8414 2822a352 2019-10-15 stsp {
8415 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8416 8b692cd0 2019-10-21 stsp
8417 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8418 8b692cd0 2019-10-21 stsp
8419 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8420 8b692cd0 2019-10-21 stsp
8421 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8422 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8423 8b692cd0 2019-10-21 stsp err = unlockerr;
8424 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8425 8b692cd0 2019-10-21 stsp
8426 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8427 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8428 8b692cd0 2019-10-21 stsp err = unlockerr;
8429 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8430 10604dce 2021-09-24 thomas
8431 10604dce 2021-09-24 thomas return err;
8432 10604dce 2021-09-24 thomas }
8433 10604dce 2021-09-24 thomas
8434 10604dce 2021-09-24 thomas const struct got_error *
8435 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8436 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8437 10604dce 2021-09-24 thomas {
8438 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8439 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8440 10604dce 2021-09-24 thomas
8441 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8442 10604dce 2021-09-24 thomas if (err)
8443 10604dce 2021-09-24 thomas goto done;
8444 8b692cd0 2019-10-21 stsp
8445 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8446 10604dce 2021-09-24 thomas
8447 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8448 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8449 10604dce 2021-09-24 thomas err = sync_err;
8450 10604dce 2021-09-24 thomas done:
8451 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8452 10604dce 2021-09-24 thomas free(fileindex_path);
8453 8b692cd0 2019-10-21 stsp return err;
8454 2822a352 2019-10-15 stsp }
8455 2822a352 2019-10-15 stsp
8456 10604dce 2021-09-24 thomas static const struct got_error *
8457 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8458 10604dce 2021-09-24 thomas {
8459 10604dce 2021-09-24 thomas const struct got_error *err;
8460 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8461 10604dce 2021-09-24 thomas
8462 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8463 10604dce 2021-09-24 thomas if (err)
8464 10604dce 2021-09-24 thomas goto done;
8465 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8466 10604dce 2021-09-24 thomas if (err)
8467 10604dce 2021-09-24 thomas goto done;
8468 10604dce 2021-09-24 thomas
8469 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8470 10604dce 2021-09-24 thomas if (err)
8471 10604dce 2021-09-24 thomas goto done;
8472 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8473 10604dce 2021-09-24 thomas if (err)
8474 10604dce 2021-09-24 thomas goto done;
8475 10604dce 2021-09-24 thomas
8476 10604dce 2021-09-24 thomas done:
8477 10604dce 2021-09-24 thomas free(branch_refname);
8478 10604dce 2021-09-24 thomas free(commit_refname);
8479 10604dce 2021-09-24 thomas return err;
8480 10604dce 2021-09-24 thomas }
8481 10604dce 2021-09-24 thomas
8482 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8483 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8484 10604dce 2021-09-24 thomas const char *branch_name;
8485 10604dce 2021-09-24 thomas };
8486 10604dce 2021-09-24 thomas
8487 10604dce 2021-09-24 thomas static const struct got_error *
8488 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8489 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8490 10604dce 2021-09-24 thomas {
8491 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8492 10604dce 2021-09-24 thomas
8493 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8494 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8495 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8496 10604dce 2021-09-24 thomas
8497 10604dce 2021-09-24 thomas return NULL;
8498 10604dce 2021-09-24 thomas }
8499 10604dce 2021-09-24 thomas
8500 10604dce 2021-09-24 thomas
8501 10604dce 2021-09-24 thomas const struct got_error *
8502 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8503 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8504 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8505 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8506 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8507 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8508 10604dce 2021-09-24 thomas {
8509 10604dce 2021-09-24 thomas const struct got_error *err;
8510 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8511 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
8512 10604dce 2021-09-24 thomas
8513 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8514 10604dce 2021-09-24 thomas if (err)
8515 10604dce 2021-09-24 thomas goto done;
8516 10604dce 2021-09-24 thomas
8517 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
8518 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
8519 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
8520 6ebb2263 2023-07-26 thomas
8521 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8522 d1c85b42 2023-07-26 thomas &cma);
8523 10604dce 2021-09-24 thomas if (err)
8524 10604dce 2021-09-24 thomas goto done;
8525 10604dce 2021-09-24 thomas
8526 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8527 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8528 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8529 10604dce 2021-09-24 thomas done:
8530 10604dce 2021-09-24 thomas free(fileindex_path);
8531 10604dce 2021-09-24 thomas return err;
8532 10604dce 2021-09-24 thomas }
8533 10604dce 2021-09-24 thomas
8534 10604dce 2021-09-24 thomas const struct got_error *
8535 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8536 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8537 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8538 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8539 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8540 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8541 ae1e948a 2021-09-28 thomas
8542 10604dce 2021-09-24 thomas {
8543 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8544 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8545 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8546 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8547 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8548 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8549 10604dce 2021-09-24 thomas int have_staged_files = 0;
8550 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8551 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8552 10604dce 2021-09-24 thomas
8553 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8554 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8555 10604dce 2021-09-24 thomas
8556 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8557 10604dce 2021-09-24 thomas
8558 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8559 10604dce 2021-09-24 thomas if (err)
8560 10604dce 2021-09-24 thomas goto done;
8561 10604dce 2021-09-24 thomas
8562 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8563 10604dce 2021-09-24 thomas if (err)
8564 10604dce 2021-09-24 thomas goto done;
8565 10604dce 2021-09-24 thomas
8566 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8567 10604dce 2021-09-24 thomas if (err)
8568 10604dce 2021-09-24 thomas goto done;
8569 10604dce 2021-09-24 thomas
8570 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8571 10604dce 2021-09-24 thomas &have_staged_files);
8572 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8573 10604dce 2021-09-24 thomas goto done;
8574 10604dce 2021-09-24 thomas if (have_staged_files) {
8575 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8576 10604dce 2021-09-24 thomas goto done;
8577 10604dce 2021-09-24 thomas }
8578 10604dce 2021-09-24 thomas
8579 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8580 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8581 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8582 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8583 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8584 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8585 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8586 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8587 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8588 10604dce 2021-09-24 thomas if (err)
8589 10604dce 2021-09-24 thomas goto done;
8590 10604dce 2021-09-24 thomas
8591 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8592 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8593 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8594 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8595 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8596 10604dce 2021-09-24 thomas if (err)
8597 10604dce 2021-09-24 thomas goto done;
8598 10604dce 2021-09-24 thomas
8599 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8600 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8601 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8602 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8603 10604dce 2021-09-24 thomas err = sync_err;
8604 10604dce 2021-09-24 thomas done:
8605 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8606 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8607 21c2d8be 2023-01-10 thomas
8608 10604dce 2021-09-24 thomas free_commitable(ct);
8609 10604dce 2021-09-24 thomas }
8610 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8611 10604dce 2021-09-24 thomas free(fileindex_path);
8612 10604dce 2021-09-24 thomas return err;
8613 10604dce 2021-09-24 thomas }
8614 10604dce 2021-09-24 thomas
8615 10604dce 2021-09-24 thomas const struct got_error *
8616 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8617 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8618 10604dce 2021-09-24 thomas {
8619 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8620 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8621 10604dce 2021-09-24 thomas
8622 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8623 10604dce 2021-09-24 thomas if (err)
8624 10604dce 2021-09-24 thomas goto done;
8625 10604dce 2021-09-24 thomas
8626 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8627 10604dce 2021-09-24 thomas if (err)
8628 10604dce 2021-09-24 thomas goto done;
8629 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8630 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8631 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8632 10604dce 2021-09-24 thomas err = sync_err;
8633 10604dce 2021-09-24 thomas done:
8634 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8635 10604dce 2021-09-24 thomas free(fileindex_path);
8636 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8637 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8638 10604dce 2021-09-24 thomas err = unlockerr;
8639 10604dce 2021-09-24 thomas return err;
8640 10604dce 2021-09-24 thomas }
8641 10604dce 2021-09-24 thomas
8642 10604dce 2021-09-24 thomas const struct got_error *
8643 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8644 10604dce 2021-09-24 thomas struct got_repository *repo)
8645 10604dce 2021-09-24 thomas {
8646 10604dce 2021-09-24 thomas const struct got_error *err;
8647 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8648 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8649 10604dce 2021-09-24 thomas
8650 10604dce 2021-09-24 thomas *in_progress = 0;
8651 10604dce 2021-09-24 thomas
8652 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8653 10604dce 2021-09-24 thomas if (err)
8654 10604dce 2021-09-24 thomas return err;
8655 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8656 dfe86d1f 2021-09-24 thomas free(branch_refname);
8657 10604dce 2021-09-24 thomas if (err) {
8658 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8659 10604dce 2021-09-24 thomas return err;
8660 10604dce 2021-09-24 thomas } else
8661 10604dce 2021-09-24 thomas *in_progress = 1;
8662 10604dce 2021-09-24 thomas
8663 10604dce 2021-09-24 thomas return NULL;
8664 10604dce 2021-09-24 thomas }
8665 10604dce 2021-09-24 thomas
8666 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8667 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8668 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8669 10604dce 2021-09-24 thomas {
8670 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8671 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8672 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8673 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8674 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8675 10604dce 2021-09-24 thomas
8676 10604dce 2021-09-24 thomas *fileindex = NULL;
8677 10604dce 2021-09-24 thomas
8678 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8679 10604dce 2021-09-24 thomas if (err)
8680 10604dce 2021-09-24 thomas return err;
8681 10604dce 2021-09-24 thomas
8682 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8683 10604dce 2021-09-24 thomas if (err)
8684 10604dce 2021-09-24 thomas goto done;
8685 10604dce 2021-09-24 thomas
8686 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8687 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8688 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8689 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8690 10604dce 2021-09-24 thomas &ok_arg);
8691 10604dce 2021-09-24 thomas if (err)
8692 10604dce 2021-09-24 thomas goto done;
8693 10604dce 2021-09-24 thomas
8694 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8695 10604dce 2021-09-24 thomas 0);
8696 10604dce 2021-09-24 thomas if (err)
8697 10604dce 2021-09-24 thomas goto done;
8698 10604dce 2021-09-24 thomas
8699 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8700 10604dce 2021-09-24 thomas if (err)
8701 10604dce 2021-09-24 thomas goto done;
8702 10604dce 2021-09-24 thomas
8703 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8704 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8705 10604dce 2021-09-24 thomas goto done;
8706 10604dce 2021-09-24 thomas }
8707 2b72f32d 2023-06-22 thomas
8708 2b72f32d 2023-06-22 thomas done:
8709 2b72f32d 2023-06-22 thomas free(fileindex_path);
8710 2b72f32d 2023-06-22 thomas if (wt_branch)
8711 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8712 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8713 2b72f32d 2023-06-22 thomas if (err) {
8714 2b72f32d 2023-06-22 thomas if (*fileindex) {
8715 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8716 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8717 2b72f32d 2023-06-22 thomas }
8718 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8719 2b72f32d 2023-06-22 thomas }
8720 2b72f32d 2023-06-22 thomas return err;
8721 2b72f32d 2023-06-22 thomas }
8722 10604dce 2021-09-24 thomas
8723 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8724 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8725 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8726 2b72f32d 2023-06-22 thomas {
8727 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8728 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8729 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8730 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8731 2b72f32d 2023-06-22 thomas
8732 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8733 2b72f32d 2023-06-22 thomas if (err)
8734 2b72f32d 2023-06-22 thomas return err;
8735 2b72f32d 2023-06-22 thomas
8736 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8737 2b72f32d 2023-06-22 thomas if (err)
8738 2b72f32d 2023-06-22 thomas return err;
8739 2b72f32d 2023-06-22 thomas
8740 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8741 10604dce 2021-09-24 thomas if (err)
8742 10604dce 2021-09-24 thomas goto done;
8743 10604dce 2021-09-24 thomas
8744 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8745 10604dce 2021-09-24 thomas if (err)
8746 10604dce 2021-09-24 thomas goto done;
8747 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8748 10604dce 2021-09-24 thomas if (err)
8749 10604dce 2021-09-24 thomas goto done;
8750 10604dce 2021-09-24 thomas
8751 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8752 10604dce 2021-09-24 thomas if (err)
8753 10604dce 2021-09-24 thomas goto done;
8754 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8755 10604dce 2021-09-24 thomas if (err)
8756 10604dce 2021-09-24 thomas goto done;
8757 10604dce 2021-09-24 thomas
8758 10604dce 2021-09-24 thomas done:
8759 10604dce 2021-09-24 thomas free(branch_refname);
8760 10604dce 2021-09-24 thomas free(commit_refname);
8761 10604dce 2021-09-24 thomas if (branch_ref)
8762 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8763 10604dce 2021-09-24 thomas if (commit_ref)
8764 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8765 2b72f32d 2023-06-22 thomas free(branch_tip);
8766 10604dce 2021-09-24 thomas return err;
8767 10604dce 2021-09-24 thomas }
8768 10604dce 2021-09-24 thomas
8769 10604dce 2021-09-24 thomas const struct got_error *
8770 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8771 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8772 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8773 10604dce 2021-09-24 thomas {
8774 10604dce 2021-09-24 thomas const struct got_error *err;
8775 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8776 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8777 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8778 10604dce 2021-09-24 thomas int have_staged_files = 0;
8779 10604dce 2021-09-24 thomas
8780 10604dce 2021-09-24 thomas *branch_name = NULL;
8781 10604dce 2021-09-24 thomas *branch_tip = NULL;
8782 10604dce 2021-09-24 thomas *fileindex = NULL;
8783 10604dce 2021-09-24 thomas
8784 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8785 10604dce 2021-09-24 thomas if (err)
8786 10604dce 2021-09-24 thomas return err;
8787 10604dce 2021-09-24 thomas
8788 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8789 10604dce 2021-09-24 thomas if (err)
8790 10604dce 2021-09-24 thomas goto done;
8791 10604dce 2021-09-24 thomas
8792 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8793 10604dce 2021-09-24 thomas &have_staged_files);
8794 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8795 10604dce 2021-09-24 thomas goto done;
8796 10604dce 2021-09-24 thomas if (have_staged_files) {
8797 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8798 10604dce 2021-09-24 thomas goto done;
8799 10604dce 2021-09-24 thomas }
8800 10604dce 2021-09-24 thomas
8801 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8802 10604dce 2021-09-24 thomas if (err)
8803 10604dce 2021-09-24 thomas goto done;
8804 10604dce 2021-09-24 thomas
8805 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8806 10604dce 2021-09-24 thomas if (err)
8807 10604dce 2021-09-24 thomas goto done;
8808 10604dce 2021-09-24 thomas
8809 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_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 if (!got_ref_is_symbolic(branch_ref)) {
8814 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8815 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8816 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8817 10604dce 2021-09-24 thomas goto done;
8818 10604dce 2021-09-24 thomas }
8819 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8820 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8821 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8822 10604dce 2021-09-24 thomas goto done;
8823 10604dce 2021-09-24 thomas }
8824 10604dce 2021-09-24 thomas
8825 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8826 10604dce 2021-09-24 thomas if (err)
8827 10604dce 2021-09-24 thomas goto done;
8828 10604dce 2021-09-24 thomas
8829 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8830 10604dce 2021-09-24 thomas if (err)
8831 10604dce 2021-09-24 thomas goto done;
8832 10604dce 2021-09-24 thomas done:
8833 10604dce 2021-09-24 thomas free(commit_refname);
8834 10604dce 2021-09-24 thomas free(branch_refname);
8835 10604dce 2021-09-24 thomas free(fileindex_path);
8836 10604dce 2021-09-24 thomas if (commit_ref)
8837 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8838 10604dce 2021-09-24 thomas if (branch_ref)
8839 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8840 10604dce 2021-09-24 thomas if (err) {
8841 10604dce 2021-09-24 thomas if (*branch_name) {
8842 10604dce 2021-09-24 thomas free(*branch_name);
8843 10604dce 2021-09-24 thomas *branch_name = NULL;
8844 10604dce 2021-09-24 thomas }
8845 10604dce 2021-09-24 thomas free(*branch_tip);
8846 10604dce 2021-09-24 thomas *branch_tip = NULL;
8847 10604dce 2021-09-24 thomas if (*fileindex) {
8848 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8849 10604dce 2021-09-24 thomas *fileindex = NULL;
8850 10604dce 2021-09-24 thomas }
8851 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8852 10604dce 2021-09-24 thomas }
8853 10604dce 2021-09-24 thomas return err;
8854 10604dce 2021-09-24 thomas }
8855 10604dce 2021-09-24 thomas
8856 10604dce 2021-09-24 thomas const struct got_error *
8857 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8858 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8859 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8860 10604dce 2021-09-24 thomas {
8861 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8862 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8863 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8864 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8865 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8866 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8867 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8868 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8869 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8870 8641a332 2023-04-14 thomas
8871 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8872 8641a332 2023-04-14 thomas
8873 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8874 8641a332 2023-04-14 thomas if (err)
8875 8641a332 2023-04-14 thomas goto done;
8876 8641a332 2023-04-14 thomas
8877 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8878 8641a332 2023-04-14 thomas if (err)
8879 8641a332 2023-04-14 thomas goto done;
8880 8641a332 2023-04-14 thomas
8881 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8882 8641a332 2023-04-14 thomas if (err)
8883 8641a332 2023-04-14 thomas goto done;
8884 8641a332 2023-04-14 thomas
8885 8641a332 2023-04-14 thomas /*
8886 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8887 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8888 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8889 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8890 8641a332 2023-04-14 thomas */
8891 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8892 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8893 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8894 8641a332 2023-04-14 thomas if (err)
8895 8641a332 2023-04-14 thomas goto done;
8896 10604dce 2021-09-24 thomas
8897 8641a332 2023-04-14 thomas
8898 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8899 945f9229 2022-04-16 thomas worktree->base_commit_id);
8900 945f9229 2022-04-16 thomas if (err)
8901 945f9229 2022-04-16 thomas goto done;
8902 945f9229 2022-04-16 thomas
8903 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8904 945f9229 2022-04-16 thomas worktree->path_prefix);
8905 10604dce 2021-09-24 thomas if (err)
8906 10604dce 2021-09-24 thomas goto done;
8907 10604dce 2021-09-24 thomas
8908 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8909 10604dce 2021-09-24 thomas if (err)
8910 10604dce 2021-09-24 thomas goto done;
8911 10604dce 2021-09-24 thomas
8912 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8913 10604dce 2021-09-24 thomas if (err)
8914 10604dce 2021-09-24 thomas goto done;
8915 10604dce 2021-09-24 thomas
8916 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8917 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8918 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8919 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8920 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8921 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8922 10604dce 2021-09-24 thomas rfa.repo = repo;
8923 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8924 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8925 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8926 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8927 10604dce 2021-09-24 thomas if (err)
8928 10604dce 2021-09-24 thomas goto sync;
8929 10604dce 2021-09-24 thomas
8930 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8931 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8932 10604dce 2021-09-24 thomas sync:
8933 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8934 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8935 10604dce 2021-09-24 thomas err = sync_err;
8936 10604dce 2021-09-24 thomas done:
8937 10604dce 2021-09-24 thomas free(tree_id);
8938 8641a332 2023-04-14 thomas free(merged_commit_id);
8939 945f9229 2022-04-16 thomas if (commit)
8940 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8941 10604dce 2021-09-24 thomas if (fileindex)
8942 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8943 10604dce 2021-09-24 thomas free(fileindex_path);
8944 8641a332 2023-04-14 thomas if (commit_ref)
8945 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8946 8641a332 2023-04-14 thomas free(commit_ref_name);
8947 10604dce 2021-09-24 thomas
8948 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8949 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8950 10604dce 2021-09-24 thomas err = unlockerr;
8951 10604dce 2021-09-24 thomas return err;
8952 10604dce 2021-09-24 thomas }
8953 10604dce 2021-09-24 thomas
8954 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8955 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8956 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8957 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8958 2db2652d 2019-08-07 stsp struct got_repository *repo;
8959 2db2652d 2019-08-07 stsp int have_changes;
8960 2db2652d 2019-08-07 stsp };
8961 2db2652d 2019-08-07 stsp
8962 ef20f542 2022-06-26 thomas static const struct got_error *
8963 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8964 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8965 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8966 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8967 735ef5ac 2019-08-03 stsp {
8968 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8969 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8970 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8971 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8972 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8973 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8974 8b13ce36 2019-08-08 stsp
8975 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8976 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8977 8b13ce36 2019-08-08 stsp return NULL;
8978 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8979 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8980 735ef5ac 2019-08-03 stsp
8981 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8982 735ef5ac 2019-08-03 stsp if (ie == NULL)
8983 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8984 735ef5ac 2019-08-03 stsp
8985 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8986 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8987 735ef5ac 2019-08-03 stsp relpath) == -1)
8988 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8989 735ef5ac 2019-08-03 stsp
8990 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8991 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8992 43010591 2023-02-17 thomas &base_commit_id, ie);
8993 735ef5ac 2019-08-03 stsp }
8994 735ef5ac 2019-08-03 stsp
8995 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8996 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8997 3aa5969e 2019-08-06 stsp goto done;
8998 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8999 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
9000 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
9001 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9002 735ef5ac 2019-08-03 stsp goto done;
9003 3aa5969e 2019-08-06 stsp }
9004 735ef5ac 2019-08-03 stsp
9005 2db2652d 2019-08-07 stsp a->have_changes = 1;
9006 2db2652d 2019-08-07 stsp
9007 735ef5ac 2019-08-03 stsp p = in_repo_path;
9008 735ef5ac 2019-08-03 stsp while (p[0] == '/')
9009 735ef5ac 2019-08-03 stsp p++;
9010 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
9011 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
9012 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
9013 735ef5ac 2019-08-03 stsp done:
9014 735ef5ac 2019-08-03 stsp free(in_repo_path);
9015 dc424a06 2019-08-07 stsp return err;
9016 dc424a06 2019-08-07 stsp }
9017 dc424a06 2019-08-07 stsp
9018 2db2652d 2019-08-07 stsp struct stage_path_arg {
9019 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9020 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9021 2db2652d 2019-08-07 stsp struct got_repository *repo;
9022 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
9023 2db2652d 2019-08-07 stsp void *status_arg;
9024 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
9025 2db2652d 2019-08-07 stsp void *patch_arg;
9026 7b5dc508 2019-10-28 stsp int staged_something;
9027 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
9028 2db2652d 2019-08-07 stsp };
9029 2db2652d 2019-08-07 stsp
9030 2db2652d 2019-08-07 stsp static const struct got_error *
9031 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
9032 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9033 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9034 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9035 0cb83759 2019-08-03 stsp {
9036 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
9037 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
9038 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
9039 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
9040 0cb83759 2019-08-03 stsp uint32_t stage;
9041 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
9042 0aeb8099 2020-07-23 stsp struct stat sb;
9043 8b13ce36 2019-08-08 stsp
9044 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
9045 8b13ce36 2019-08-08 stsp return NULL;
9046 0cb83759 2019-08-03 stsp
9047 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9048 d3e7c587 2019-08-03 stsp if (ie == NULL)
9049 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9050 0cb83759 2019-08-03 stsp
9051 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9052 2db2652d 2019-08-07 stsp relpath)== -1)
9053 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
9054 0cb83759 2019-08-03 stsp
9055 0cb83759 2019-08-03 stsp switch (status) {
9056 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9057 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9058 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9059 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9060 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9061 0aeb8099 2020-07-23 stsp break;
9062 0aeb8099 2020-07-23 stsp }
9063 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9064 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9065 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9066 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9067 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9068 dc424a06 2019-08-07 stsp if (err)
9069 dc424a06 2019-08-07 stsp break;
9070 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9071 dc424a06 2019-08-07 stsp break;
9072 dc424a06 2019-08-07 stsp } else {
9073 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9074 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9075 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9076 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9077 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9078 dc424a06 2019-08-07 stsp break;
9079 dc424a06 2019-08-07 stsp }
9080 dc424a06 2019-08-07 stsp }
9081 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9082 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9083 0cb83759 2019-08-03 stsp if (err)
9084 dc424a06 2019-08-07 stsp break;
9085 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9086 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9087 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9088 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9089 0cb83759 2019-08-03 stsp else
9090 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9091 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9092 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9093 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9094 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9095 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9096 35213c7c 2020-07-23 stsp ssize_t target_len;
9097 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9098 35213c7c 2020-07-23 stsp sizeof(target_path));
9099 35213c7c 2020-07-23 stsp if (target_len == -1) {
9100 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9101 35213c7c 2020-07-23 stsp ondisk_path);
9102 35213c7c 2020-07-23 stsp break;
9103 35213c7c 2020-07-23 stsp }
9104 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9105 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9106 ad10f64e 2023-07-19 thomas a->worktree->root_path,
9107 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
9108 35213c7c 2020-07-23 stsp if (err)
9109 35213c7c 2020-07-23 stsp break;
9110 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9111 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9112 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9113 35213c7c 2020-07-23 stsp break;
9114 35213c7c 2020-07-23 stsp }
9115 35213c7c 2020-07-23 stsp }
9116 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9117 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9118 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9119 35213c7c 2020-07-23 stsp else
9120 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9121 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9122 0aeb8099 2020-07-23 stsp } else {
9123 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9124 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9125 0aeb8099 2020-07-23 stsp }
9126 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9127 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9128 dc424a06 2019-08-07 stsp break;
9129 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9130 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9131 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9132 f289c82c 2022-06-13 thomas if (err)
9133 f289c82c 2022-06-13 thomas break;
9134 f289c82c 2022-06-13 thomas /*
9135 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
9136 f289c82c 2022-06-13 thomas * implicitly unstage the file.
9137 f289c82c 2022-06-13 thomas */
9138 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9139 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
9140 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
9141 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
9142 f289c82c 2022-06-13 thomas }
9143 0cb83759 2019-08-03 stsp break;
9144 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9145 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9146 d3e7c587 2019-08-03 stsp break;
9147 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9148 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9149 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9150 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9151 dc424a06 2019-08-07 stsp if (err)
9152 dc424a06 2019-08-07 stsp break;
9153 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9154 88f33a19 2019-08-08 stsp break;
9155 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9156 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9157 dc424a06 2019-08-07 stsp break;
9158 88f33a19 2019-08-08 stsp }
9159 dc424a06 2019-08-07 stsp }
9160 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9161 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9162 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9163 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9164 dc424a06 2019-08-07 stsp break;
9165 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9166 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9167 12463d8b 2019-12-13 stsp de_name);
9168 0cb83759 2019-08-03 stsp break;
9169 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9170 d3e7c587 2019-08-03 stsp break;
9171 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9172 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9173 ebf48fd5 2019-08-03 stsp break;
9174 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9175 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9176 2a06fe5f 2019-08-24 stsp break;
9177 0cb83759 2019-08-03 stsp default:
9178 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9179 537ac44b 2019-08-03 stsp break;
9180 0cb83759 2019-08-03 stsp }
9181 dc424a06 2019-08-07 stsp
9182 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9183 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9184 dc424a06 2019-08-07 stsp free(path_content);
9185 2db2652d 2019-08-07 stsp free(ondisk_path);
9186 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9187 0ebf8283 2019-07-24 stsp return err;
9188 0ebf8283 2019-07-24 stsp }
9189 0cb83759 2019-08-03 stsp
9190 0cb83759 2019-08-03 stsp const struct got_error *
9191 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9192 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9193 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9194 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9195 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9196 0cb83759 2019-08-03 stsp {
9197 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9198 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9199 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9200 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9201 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9202 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9203 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9204 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9205 0cb83759 2019-08-03 stsp
9206 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9207 0cb83759 2019-08-03 stsp if (err)
9208 0cb83759 2019-08-03 stsp return err;
9209 0cb83759 2019-08-03 stsp
9210 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9211 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9212 735ef5ac 2019-08-03 stsp if (err)
9213 735ef5ac 2019-08-03 stsp goto done;
9214 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9215 735ef5ac 2019-08-03 stsp if (err)
9216 735ef5ac 2019-08-03 stsp goto done;
9217 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9218 0cb83759 2019-08-03 stsp if (err)
9219 0cb83759 2019-08-03 stsp goto done;
9220 0cb83759 2019-08-03 stsp
9221 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9222 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9223 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9224 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9225 2db2652d 2019-08-07 stsp oka.repo = repo;
9226 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9227 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9228 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9229 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9230 735ef5ac 2019-08-03 stsp if (err)
9231 735ef5ac 2019-08-03 stsp goto done;
9232 735ef5ac 2019-08-03 stsp }
9233 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9234 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9235 2db2652d 2019-08-07 stsp goto done;
9236 2db2652d 2019-08-07 stsp }
9237 735ef5ac 2019-08-03 stsp
9238 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9239 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9240 2db2652d 2019-08-07 stsp spa.repo = repo;
9241 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9242 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9243 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9244 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9245 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9246 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9247 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9248 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9249 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9250 42005733 2019-08-03 stsp if (err)
9251 2db2652d 2019-08-07 stsp goto done;
9252 7b5dc508 2019-10-28 stsp }
9253 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9254 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9255 7b5dc508 2019-10-28 stsp goto done;
9256 0cb83759 2019-08-03 stsp }
9257 0cb83759 2019-08-03 stsp
9258 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9259 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9260 0cb83759 2019-08-03 stsp err = sync_err;
9261 0cb83759 2019-08-03 stsp done:
9262 735ef5ac 2019-08-03 stsp if (head_ref)
9263 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9264 735ef5ac 2019-08-03 stsp free(head_commit_id);
9265 0cb83759 2019-08-03 stsp free(fileindex_path);
9266 0cb83759 2019-08-03 stsp if (fileindex)
9267 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9268 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9269 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9270 0cb83759 2019-08-03 stsp err = unlockerr;
9271 0cb83759 2019-08-03 stsp return err;
9272 0cb83759 2019-08-03 stsp }
9273 ad493afc 2019-08-03 stsp
9274 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9275 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9276 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9277 ad493afc 2019-08-03 stsp struct got_repository *repo;
9278 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9279 ad493afc 2019-08-03 stsp void *progress_arg;
9280 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9281 2e1f37b0 2019-08-08 stsp void *patch_arg;
9282 ad493afc 2019-08-03 stsp };
9283 2e1f37b0 2019-08-08 stsp
9284 2e1f37b0 2019-08-08 stsp static const struct got_error *
9285 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9286 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9287 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9288 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9289 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9290 2e1f37b0 2019-08-08 stsp {
9291 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9292 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9293 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9294 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9295 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9296 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9297 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9298 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9299 2e1f37b0 2019-08-08 stsp
9300 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9301 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9302 2e1f37b0 2019-08-08 stsp
9303 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9304 2e1f37b0 2019-08-08 stsp if (err)
9305 2e1f37b0 2019-08-08 stsp return err;
9306 f4ae6ddb 2022-07-01 thomas
9307 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9308 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9309 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9310 f4ae6ddb 2022-07-01 thomas goto done;
9311 f4ae6ddb 2022-07-01 thomas }
9312 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9313 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9314 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9315 f4ae6ddb 2022-07-01 thomas goto done;
9316 f4ae6ddb 2022-07-01 thomas }
9317 f4ae6ddb 2022-07-01 thomas
9318 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9319 2e1f37b0 2019-08-08 stsp if (err)
9320 2e1f37b0 2019-08-08 stsp goto done;
9321 2e1f37b0 2019-08-08 stsp
9322 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9323 2e1f37b0 2019-08-08 stsp if (err)
9324 2e1f37b0 2019-08-08 stsp goto done;
9325 2e1f37b0 2019-08-08 stsp
9326 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9327 2e1f37b0 2019-08-08 stsp if (err)
9328 2e1f37b0 2019-08-08 stsp goto done;
9329 2e1f37b0 2019-08-08 stsp
9330 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9331 f4ae6ddb 2022-07-01 thomas fd2);
9332 2e1f37b0 2019-08-08 stsp if (err)
9333 2e1f37b0 2019-08-08 stsp goto done;
9334 2e1f37b0 2019-08-08 stsp
9335 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9336 2e1f37b0 2019-08-08 stsp if (err)
9337 2e1f37b0 2019-08-08 stsp goto done;
9338 ad493afc 2019-08-03 stsp
9339 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9340 2e1f37b0 2019-08-08 stsp if (err)
9341 2e1f37b0 2019-08-08 stsp goto done;
9342 2e1f37b0 2019-08-08 stsp
9343 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9344 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9345 2e1f37b0 2019-08-08 stsp if (err)
9346 2e1f37b0 2019-08-08 stsp goto done;
9347 2e1f37b0 2019-08-08 stsp
9348 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9349 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9350 2e1f37b0 2019-08-08 stsp if (err)
9351 2e1f37b0 2019-08-08 stsp goto done;
9352 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9353 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9354 2e1f37b0 2019-08-08 stsp if (err)
9355 2e1f37b0 2019-08-08 stsp goto done;
9356 2e1f37b0 2019-08-08 stsp
9357 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9358 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9359 2e1f37b0 2019-08-08 stsp goto done;
9360 2e1f37b0 2019-08-08 stsp }
9361 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9362 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9363 2e1f37b0 2019-08-08 stsp goto done;
9364 2e1f37b0 2019-08-08 stsp }
9365 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9366 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9367 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9368 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9369 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9370 fe621944 2020-11-10 stsp nchanges++;
9371 fe621944 2020-11-10 stsp }
9372 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9373 2e1f37b0 2019-08-08 stsp int choice;
9374 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9375 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9376 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9377 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9378 2e1f37b0 2019-08-08 stsp if (err)
9379 2e1f37b0 2019-08-08 stsp goto done;
9380 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9381 2e1f37b0 2019-08-08 stsp have_content = 1;
9382 19e4b907 2019-08-08 stsp else
9383 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9384 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9385 2e1f37b0 2019-08-08 stsp break;
9386 2e1f37b0 2019-08-08 stsp }
9387 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9388 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9389 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9390 2e1f37b0 2019-08-08 stsp done:
9391 2e1f37b0 2019-08-08 stsp free(label1);
9392 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9393 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9394 2e1f37b0 2019-08-08 stsp if (blob)
9395 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9396 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9397 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9398 2e1f37b0 2019-08-08 stsp if (staged_blob)
9399 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9400 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9401 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9402 fe621944 2020-11-10 stsp err = free_err;
9403 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9404 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9405 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9406 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9407 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9408 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9409 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9410 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9411 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9412 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9413 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9414 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9415 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9416 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9417 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9418 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9419 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9420 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9421 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9422 2e1f37b0 2019-08-08 stsp }
9423 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9424 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9425 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9426 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9427 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9428 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9429 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9430 2e1f37b0 2019-08-08 stsp }
9431 2e1f37b0 2019-08-08 stsp free(path1);
9432 2e1f37b0 2019-08-08 stsp free(path2);
9433 fda8017d 2020-07-23 stsp return err;
9434 fda8017d 2020-07-23 stsp }
9435 fda8017d 2020-07-23 stsp
9436 fda8017d 2020-07-23 stsp static const struct got_error *
9437 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9438 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9439 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9440 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9441 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9442 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9443 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9444 fda8017d 2020-07-23 stsp {
9445 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9446 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9447 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9448 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9449 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9450 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9451 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9452 fda8017d 2020-07-23 stsp struct stat sb;
9453 fda8017d 2020-07-23 stsp
9454 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9455 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9456 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9457 fda8017d 2020-07-23 stsp if (err)
9458 fda8017d 2020-07-23 stsp return err;
9459 fda8017d 2020-07-23 stsp
9460 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9461 fda8017d 2020-07-23 stsp return NULL;
9462 fda8017d 2020-07-23 stsp
9463 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9464 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9465 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9466 fda8017d 2020-07-23 stsp if (err)
9467 fda8017d 2020-07-23 stsp goto done;
9468 fda8017d 2020-07-23 stsp }
9469 fda8017d 2020-07-23 stsp
9470 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9471 fda8017d 2020-07-23 stsp if (f == NULL) {
9472 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9473 fda8017d 2020-07-23 stsp path_unstaged_content);
9474 fda8017d 2020-07-23 stsp goto done;
9475 fda8017d 2020-07-23 stsp }
9476 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9477 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9478 fda8017d 2020-07-23 stsp goto done;
9479 fda8017d 2020-07-23 stsp }
9480 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9481 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9482 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9483 fda8017d 2020-07-23 stsp size_t r;
9484 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9485 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9486 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9487 fda8017d 2020-07-23 stsp goto done;
9488 fda8017d 2020-07-23 stsp }
9489 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9490 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9491 fda8017d 2020-07-23 stsp goto done;
9492 fda8017d 2020-07-23 stsp }
9493 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9494 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9495 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9496 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9497 fda8017d 2020-07-23 stsp progress_arg);
9498 fda8017d 2020-07-23 stsp } else {
9499 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9500 67a66647 2021-05-31 stsp
9501 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9502 67a66647 2021-05-31 stsp if (err)
9503 67a66647 2021-05-31 stsp return err;
9504 67a66647 2021-05-31 stsp
9505 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9506 67a66647 2021-05-31 stsp parent) == -1) {
9507 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9508 67a66647 2021-05-31 stsp base_path = NULL;
9509 67a66647 2021-05-31 stsp goto done;
9510 67a66647 2021-05-31 stsp }
9511 67a66647 2021-05-31 stsp
9512 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9513 fc2a50f2 2022-11-01 thomas base_path, "");
9514 67a66647 2021-05-31 stsp if (err)
9515 67a66647 2021-05-31 stsp goto done;
9516 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9517 67a66647 2021-05-31 stsp blob_base);
9518 67a66647 2021-05-31 stsp if (err)
9519 67a66647 2021-05-31 stsp goto done;
9520 67a66647 2021-05-31 stsp
9521 b6b86fd1 2022-08-30 thomas /*
9522 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9523 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9524 eec2f5a9 2021-06-03 stsp */
9525 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9526 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9527 eec2f5a9 2021-06-03 stsp ondisk_path);
9528 eec2f5a9 2021-06-03 stsp if (err)
9529 eec2f5a9 2021-06-03 stsp goto done;
9530 eec2f5a9 2021-06-03 stsp } else {
9531 eec2f5a9 2021-06-03 stsp int fd;
9532 06340621 2021-12-31 thomas fd = open(ondisk_path,
9533 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9534 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9535 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9536 eec2f5a9 2021-06-03 stsp goto done;
9537 eec2f5a9 2021-06-03 stsp }
9538 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9539 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9540 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9541 eec2f5a9 2021-06-03 stsp close(fd);
9542 eec2f5a9 2021-06-03 stsp goto done;
9543 eec2f5a9 2021-06-03 stsp }
9544 eec2f5a9 2021-06-03 stsp }
9545 eec2f5a9 2021-06-03 stsp
9546 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9547 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9548 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9549 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9550 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9551 fda8017d 2020-07-23 stsp }
9552 fda8017d 2020-07-23 stsp if (err)
9553 fda8017d 2020-07-23 stsp goto done;
9554 fda8017d 2020-07-23 stsp
9555 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9556 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9557 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9558 2ac8aa02 2020-11-09 stsp } else {
9559 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9560 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9561 2ac8aa02 2020-11-09 stsp }
9562 fda8017d 2020-07-23 stsp done:
9563 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9564 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9565 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9566 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9567 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9568 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9569 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9570 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9571 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9572 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9573 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9574 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9575 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9576 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9577 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9578 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9579 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9580 67a66647 2021-05-31 stsp free(blob_base_path);
9581 67a66647 2021-05-31 stsp free(parent);
9582 67a66647 2021-05-31 stsp free(base_path);
9583 2e1f37b0 2019-08-08 stsp return err;
9584 2e1f37b0 2019-08-08 stsp }
9585 2e1f37b0 2019-08-08 stsp
9586 ad493afc 2019-08-03 stsp static const struct got_error *
9587 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9588 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9589 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9590 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9591 ad493afc 2019-08-03 stsp {
9592 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9593 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9594 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9595 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9596 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9597 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9598 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9599 9bc94a15 2019-08-03 stsp struct stat sb;
9600 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9601 ad493afc 2019-08-03 stsp
9602 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9603 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9604 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9605 2e1f37b0 2019-08-08 stsp return NULL;
9606 2e1f37b0 2019-08-08 stsp
9607 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9608 ad493afc 2019-08-03 stsp if (ie == NULL)
9609 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9610 9bc94a15 2019-08-03 stsp
9611 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9612 9bc94a15 2019-08-03 stsp == -1)
9613 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9614 ad493afc 2019-08-03 stsp
9615 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9616 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9617 f69721c3 2019-10-21 stsp if (err)
9618 f69721c3 2019-10-21 stsp goto done;
9619 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9620 f69721c3 2019-10-21 stsp id_str) == -1) {
9621 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9622 f4ae6ddb 2022-07-01 thomas goto done;
9623 f4ae6ddb 2022-07-01 thomas }
9624 f4ae6ddb 2022-07-01 thomas
9625 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9626 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9627 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9628 f4ae6ddb 2022-07-01 thomas goto done;
9629 f4ae6ddb 2022-07-01 thomas }
9630 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9631 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9632 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9633 f69721c3 2019-10-21 stsp goto done;
9634 f69721c3 2019-10-21 stsp }
9635 f69721c3 2019-10-21 stsp
9636 ad493afc 2019-08-03 stsp switch (staged_status) {
9637 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9638 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9639 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9640 ad493afc 2019-08-03 stsp if (err)
9641 ad493afc 2019-08-03 stsp break;
9642 ad493afc 2019-08-03 stsp /* fall through */
9643 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9644 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9645 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9646 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9647 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9648 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9649 2e1f37b0 2019-08-08 stsp if (err)
9650 2e1f37b0 2019-08-08 stsp break;
9651 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9652 2e1f37b0 2019-08-08 stsp break;
9653 2e1f37b0 2019-08-08 stsp } else {
9654 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9655 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9656 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9657 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9658 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9659 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9660 2e1f37b0 2019-08-08 stsp }
9661 2e1f37b0 2019-08-08 stsp }
9662 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9663 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9664 ad493afc 2019-08-03 stsp if (err)
9665 ad493afc 2019-08-03 stsp break;
9666 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9667 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9668 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9669 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9670 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9671 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9672 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9673 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9674 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9675 ea7786be 2020-07-23 stsp break;
9676 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9677 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9678 36bf999c 2020-07-23 stsp char *staged_target;
9679 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9680 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9681 36bf999c 2020-07-23 stsp if (err)
9682 36bf999c 2020-07-23 stsp goto done;
9683 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9684 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9685 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9686 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9687 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9688 36bf999c 2020-07-23 stsp free(staged_target);
9689 dfe9fba0 2020-07-23 stsp } else {
9690 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9691 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9692 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9693 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9694 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9695 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9696 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9697 dfe9fba0 2020-07-23 stsp }
9698 ea7786be 2020-07-23 stsp break;
9699 ea7786be 2020-07-23 stsp default:
9700 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9701 ea7786be 2020-07-23 stsp break;
9702 ea7786be 2020-07-23 stsp }
9703 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9704 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9705 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9706 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9707 2ac8aa02 2020-11-09 stsp }
9708 ad493afc 2019-08-03 stsp break;
9709 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9710 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9711 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9712 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9713 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9714 2e1f37b0 2019-08-08 stsp if (err)
9715 2e1f37b0 2019-08-08 stsp break;
9716 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9717 2e1f37b0 2019-08-08 stsp break;
9718 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9719 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9720 2e1f37b0 2019-08-08 stsp break;
9721 2e1f37b0 2019-08-08 stsp }
9722 2e1f37b0 2019-08-08 stsp }
9723 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9724 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9725 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9726 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9727 9bc94a15 2019-08-03 stsp if (err)
9728 9bc94a15 2019-08-03 stsp break;
9729 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9730 ad493afc 2019-08-03 stsp break;
9731 ad493afc 2019-08-03 stsp }
9732 f69721c3 2019-10-21 stsp done:
9733 ad493afc 2019-08-03 stsp free(ondisk_path);
9734 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9735 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9736 ad493afc 2019-08-03 stsp if (blob_base)
9737 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9738 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9739 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9740 ad493afc 2019-08-03 stsp if (blob_staged)
9741 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9742 f69721c3 2019-10-21 stsp free(id_str);
9743 f69721c3 2019-10-21 stsp free(label_orig);
9744 ad493afc 2019-08-03 stsp return err;
9745 ad493afc 2019-08-03 stsp }
9746 ad493afc 2019-08-03 stsp
9747 ad493afc 2019-08-03 stsp const struct got_error *
9748 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9749 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9750 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9751 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9752 ad493afc 2019-08-03 stsp struct got_repository *repo)
9753 ad493afc 2019-08-03 stsp {
9754 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9755 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9756 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9757 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9758 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9759 ad493afc 2019-08-03 stsp
9760 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9761 ad493afc 2019-08-03 stsp if (err)
9762 ad493afc 2019-08-03 stsp return err;
9763 ad493afc 2019-08-03 stsp
9764 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9765 ad493afc 2019-08-03 stsp if (err)
9766 ad493afc 2019-08-03 stsp goto done;
9767 ad493afc 2019-08-03 stsp
9768 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9769 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9770 ad493afc 2019-08-03 stsp upa.repo = repo;
9771 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9772 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9773 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9774 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9775 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9776 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9777 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9778 ad493afc 2019-08-03 stsp if (err)
9779 ad493afc 2019-08-03 stsp goto done;
9780 ad493afc 2019-08-03 stsp }
9781 ad493afc 2019-08-03 stsp
9782 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9783 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9784 ad493afc 2019-08-03 stsp err = sync_err;
9785 ad493afc 2019-08-03 stsp done:
9786 ad493afc 2019-08-03 stsp free(fileindex_path);
9787 ad493afc 2019-08-03 stsp if (fileindex)
9788 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9789 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9790 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9791 ad493afc 2019-08-03 stsp err = unlockerr;
9792 ad493afc 2019-08-03 stsp return err;
9793 ad493afc 2019-08-03 stsp }
9794 b2118c49 2020-07-28 stsp
9795 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9796 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9797 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9798 b2118c49 2020-07-28 stsp void *info_arg;
9799 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9800 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9801 b2118c49 2020-07-28 stsp void *cancel_arg;
9802 b2118c49 2020-07-28 stsp };
9803 b2118c49 2020-07-28 stsp
9804 b2118c49 2020-07-28 stsp static const struct got_error *
9805 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9806 b2118c49 2020-07-28 stsp {
9807 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
9808 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9809 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9810 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9811 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9812 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9813 b2118c49 2020-07-28 stsp int stage;
9814 b2118c49 2020-07-28 stsp
9815 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
9816 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
9817 eb6fb9cd 2023-07-26 thomas if (err)
9818 eb6fb9cd 2023-07-26 thomas return err;
9819 eb6fb9cd 2023-07-26 thomas }
9820 b2118c49 2020-07-28 stsp
9821 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9822 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9823 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9824 b2118c49 2020-07-28 stsp break;
9825 b2118c49 2020-07-28 stsp }
9826 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9827 b2118c49 2020-07-28 stsp return NULL;
9828 b2118c49 2020-07-28 stsp
9829 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9830 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9831 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9832 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9833 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9834 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9835 43010591 2023-02-17 thomas &staged_blob_id, ie);
9836 b2118c49 2020-07-28 stsp }
9837 b2118c49 2020-07-28 stsp
9838 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9839 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9840 b2118c49 2020-07-28 stsp
9841 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9842 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9843 b2118c49 2020-07-28 stsp }
9844 b2118c49 2020-07-28 stsp
9845 b2118c49 2020-07-28 stsp const struct got_error *
9846 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9847 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9848 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9849 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9850 b2118c49 2020-07-28 stsp
9851 b2118c49 2020-07-28 stsp {
9852 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9853 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9854 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9855 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9856 b2118c49 2020-07-28 stsp
9857 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9858 b2118c49 2020-07-28 stsp if (err)
9859 b2118c49 2020-07-28 stsp return err;
9860 b2118c49 2020-07-28 stsp
9861 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9862 b2118c49 2020-07-28 stsp if (err)
9863 b2118c49 2020-07-28 stsp goto done;
9864 b2118c49 2020-07-28 stsp
9865 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9866 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9867 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9868 b2118c49 2020-07-28 stsp arg.paths = paths;
9869 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9870 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9871 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9872 b2118c49 2020-07-28 stsp &arg);
9873 b2118c49 2020-07-28 stsp done:
9874 b2118c49 2020-07-28 stsp free(fileindex_path);
9875 b2118c49 2020-07-28 stsp if (fileindex)
9876 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9877 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9878 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9879 b2118c49 2020-07-28 stsp err = unlockerr;
9880 b2118c49 2020-07-28 stsp return err;
9881 b2118c49 2020-07-28 stsp }
9882 814624e7 2022-03-22 thomas
9883 814624e7 2022-03-22 thomas static const struct got_error *
9884 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9885 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9886 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9887 814624e7 2022-03-22 thomas {
9888 814624e7 2022-03-22 thomas const struct got_error *err;
9889 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9890 814624e7 2022-03-22 thomas struct stat sb;
9891 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9892 814624e7 2022-03-22 thomas
9893 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9894 814624e7 2022-03-22 thomas if (err)
9895 814624e7 2022-03-22 thomas return err;
9896 814624e7 2022-03-22 thomas
9897 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9898 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9899 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9900 814624e7 2022-03-22 thomas
9901 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9902 814624e7 2022-03-22 thomas if (ie) {
9903 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9904 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9905 12de5570 2022-05-01 thomas repo);
9906 814624e7 2022-03-22 thomas if (err)
9907 814624e7 2022-03-22 thomas goto done;
9908 814624e7 2022-03-22 thomas } else {
9909 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9910 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9911 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9912 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9913 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9914 814624e7 2022-03-22 thomas ondisk_path);
9915 814624e7 2022-03-22 thomas goto done;
9916 814624e7 2022-03-22 thomas }
9917 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9918 814624e7 2022-03-22 thomas }
9919 814624e7 2022-03-22 thomas }
9920 814624e7 2022-03-22 thomas
9921 814624e7 2022-03-22 thomas done:
9922 814624e7 2022-03-22 thomas free(ondisk_path);
9923 814624e7 2022-03-22 thomas return err;
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_rm(const char *path, unsigned char status,
9928 814624e7 2022-03-22 thomas unsigned char staged_status)
9929 814624e7 2022-03-22 thomas {
9930 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9931 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9932 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9933 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9934 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9935 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9936 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9937 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9938 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9939 814624e7 2022-03-22 thomas return NULL;
9940 814624e7 2022-03-22 thomas }
9941 814624e7 2022-03-22 thomas
9942 814624e7 2022-03-22 thomas static const struct got_error *
9943 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9944 814624e7 2022-03-22 thomas {
9945 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9946 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9947 814624e7 2022-03-22 thomas return NULL;
9948 814624e7 2022-03-22 thomas }
9949 814624e7 2022-03-22 thomas
9950 814624e7 2022-03-22 thomas static const struct got_error *
9951 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9952 814624e7 2022-03-22 thomas unsigned char staged_status)
9953 814624e7 2022-03-22 thomas {
9954 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9955 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9956 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9957 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9958 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9959 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9960 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9961 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9962 814624e7 2022-03-22 thomas return NULL;
9963 814624e7 2022-03-22 thomas }
9964 814624e7 2022-03-22 thomas
9965 814624e7 2022-03-22 thomas const struct got_error *
9966 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9967 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9968 814624e7 2022-03-22 thomas {
9969 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9970 814624e7 2022-03-22 thomas }
9971 814624e7 2022-03-22 thomas
9972 814624e7 2022-03-22 thomas const struct got_error *
9973 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9974 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9975 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9976 814624e7 2022-03-22 thomas {
9977 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9978 814624e7 2022-03-22 thomas int file_renamed = 0;
9979 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9980 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9981 814624e7 2022-03-22 thomas
9982 814624e7 2022-03-22 thomas *oldpath = NULL;
9983 814624e7 2022-03-22 thomas *newpath = NULL;
9984 814624e7 2022-03-22 thomas
9985 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9986 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9987 814624e7 2022-03-22 thomas if (err)
9988 814624e7 2022-03-22 thomas goto done;
9989 814624e7 2022-03-22 thomas
9990 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9991 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9992 814624e7 2022-03-22 thomas if (err)
9993 814624e7 2022-03-22 thomas goto done;
9994 814624e7 2022-03-22 thomas
9995 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9996 814624e7 2022-03-22 thomas file_renamed = 1;
9997 814624e7 2022-03-22 thomas
9998 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9999 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
10000 814624e7 2022-03-22 thomas else if (file_renamed) {
10001 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
10002 814624e7 2022-03-22 thomas if (err == NULL)
10003 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
10004 814624e7 2022-03-22 thomas } else if (old == NULL)
10005 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
10006 814624e7 2022-03-22 thomas else
10007 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
10008 814624e7 2022-03-22 thomas
10009 814624e7 2022-03-22 thomas done:
10010 814624e7 2022-03-22 thomas if (err) {
10011 814624e7 2022-03-22 thomas free(*oldpath);
10012 814624e7 2022-03-22 thomas *oldpath = NULL;
10013 814624e7 2022-03-22 thomas free(*newpath);
10014 814624e7 2022-03-22 thomas *newpath = NULL;
10015 814624e7 2022-03-22 thomas }
10016 814624e7 2022-03-22 thomas return err;
10017 814624e7 2022-03-22 thomas }
10018 814624e7 2022-03-22 thomas
10019 814624e7 2022-03-22 thomas const struct got_error *
10020 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10021 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10022 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
10023 814624e7 2022-03-22 thomas {
10024 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
10025 5e22b737 2022-05-31 thomas
10026 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
10027 5e22b737 2022-05-31 thomas saa.worktree = worktree;
10028 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
10029 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
10030 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
10031 5e22b737 2022-05-31 thomas saa.repo = repo;
10032 5e22b737 2022-05-31 thomas
10033 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
10034 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
10035 814624e7 2022-03-22 thomas }
10036 5e22b737 2022-05-31 thomas
10037 5e22b737 2022-05-31 thomas const struct got_error *
10038 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10039 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10040 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
10041 5e22b737 2022-05-31 thomas {
10042 8ec7352a 2023-06-01 thomas const struct got_error *err;
10043 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
10044 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
10045 5e22b737 2022-05-31 thomas
10046 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
10047 5e22b737 2022-05-31 thomas sda.worktree = worktree;
10048 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
10049 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
10050 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
10051 5e22b737 2022-05-31 thomas sda.repo = repo;
10052 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
10053 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
10054 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
10055 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
10056 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
10057 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
10058 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
10059 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
10060 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
10061 8ec7352a 2023-06-01 thomas
10062 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
10063 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10064 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
10065 8ec7352a 2023-06-01 thomas return err;
10066 5e22b737 2022-05-31 thomas }
10067 5e22b737 2022-05-31 thomas
10068 5e22b737 2022-05-31 thomas const struct got_error *
10069 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
10070 36832a8e 2022-05-31 thomas const char *fileindex_path)
10071 5e22b737 2022-05-31 thomas {
10072 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
10073 5e22b737 2022-05-31 thomas
10074 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
10075 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
10076 5e22b737 2022-05-31 thomas
10077 5e22b737 2022-05-31 thomas return err;
10078 5e22b737 2022-05-31 thomas }