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 4f46fcf8 2024-02-13 thomas int update_timestamps = 0;
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 struct got_blob_object *blob2 = NULL;
2045 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2046 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2047 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2048 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2049 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2050 f4ae6ddb 2022-07-01 thomas goto done;
2051 f4ae6ddb 2022-07-01 thomas }
2052 234035bc 2019-06-01 stsp struct got_object_id id2;
2053 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2054 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2055 f4ae6ddb 2022-07-01 thomas fd2);
2056 234035bc 2019-06-01 stsp if (err)
2057 f69721c3 2019-10-21 stsp goto done;
2058 f69721c3 2019-10-21 stsp }
2059 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2060 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2061 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2062 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2063 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2064 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2065 f69721c3 2019-10-21 stsp goto done;
2066 f69721c3 2019-10-21 stsp }
2067 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2068 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2069 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2070 234035bc 2019-06-01 stsp goto done;
2071 f69721c3 2019-10-21 stsp }
2072 234035bc 2019-06-01 stsp }
2073 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2074 36bf999c 2020-07-23 stsp char *link_target;
2075 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2076 36bf999c 2020-07-23 stsp if (err)
2077 36bf999c 2020-07-23 stsp goto done;
2078 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2079 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2080 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2081 36bf999c 2020-07-23 stsp free(link_target);
2082 993e2a1b 2020-07-23 stsp } else {
2083 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2084 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2085 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2086 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2087 993e2a1b 2020-07-23 stsp }
2088 f69721c3 2019-10-21 stsp free(label_orig);
2089 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2090 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2091 f4ae6ddb 2022-07-01 thomas goto done;
2092 f4ae6ddb 2022-07-01 thomas }
2093 234035bc 2019-06-01 stsp if (blob2)
2094 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2095 909d120e 2019-09-22 stsp if (err)
2096 909d120e 2019-09-22 stsp goto done;
2097 234035bc 2019-06-01 stsp /*
2098 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2099 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2100 234035bc 2019-06-01 stsp * unmodified files again.
2101 234035bc 2019-06-01 stsp */
2102 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2103 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2104 234035bc 2019-06-01 stsp update_timestamps);
2105 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2106 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2108 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2109 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2110 1ee397ad 2019-07-12 stsp if (err)
2111 1ee397ad 2019-07-12 stsp goto done;
2112 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2113 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2114 13d9040b 2019-03-27 stsp if (err)
2115 13d9040b 2019-03-27 stsp goto done;
2116 13d9040b 2019-03-27 stsp } else {
2117 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2118 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2119 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2120 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2121 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2122 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2123 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2124 2e1fa222 2020-07-23 stsp } else {
2125 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2126 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2127 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2128 aaf972e2 2023-09-18 thomas status == GOT_STATUS_UNVERSIONED,
2129 aaf972e2 2023-09-18 thomas &update_timestamps,
2130 aaf972e2 2023-09-18 thomas repo, progress_cb, progress_arg);
2131 2e1fa222 2020-07-23 stsp }
2132 13d9040b 2019-03-27 stsp if (err)
2133 13d9040b 2019-03-27 stsp goto done;
2134 65b05cec 2020-07-23 stsp
2135 054041d0 2020-06-24 stsp if (ie) {
2136 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2137 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2138 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2139 054041d0 2020-06-24 stsp } else {
2140 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2141 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2142 aaf972e2 2023-09-18 thomas &blob->id, update_timestamps);
2143 054041d0 2020-06-24 stsp }
2144 13d9040b 2019-03-27 stsp if (err)
2145 13d9040b 2019-03-27 stsp goto done;
2146 65b05cec 2020-07-23 stsp
2147 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2148 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2149 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2150 2e1fa222 2020-07-23 stsp }
2151 13d9040b 2019-03-27 stsp }
2152 f4ae6ddb 2022-07-01 thomas
2153 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2154 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2155 f4ae6ddb 2022-07-01 thomas goto done;
2156 f4ae6ddb 2022-07-01 thomas }
2157 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2158 6353ad76 2019-02-08 stsp done:
2159 6353ad76 2019-02-08 stsp free(ondisk_path);
2160 8da9e5f4 2019-01-12 stsp return err;
2161 90285c3b 2019-01-08 stsp }
2162 90285c3b 2019-01-08 stsp
2163 90285c3b 2019-01-08 stsp static const struct got_error *
2164 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2165 90285c3b 2019-01-08 stsp {
2166 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2167 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2168 90285c3b 2019-01-08 stsp
2169 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2170 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2171 90285c3b 2019-01-08 stsp
2172 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2173 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2174 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2175 c97665ae 2019-01-13 stsp } else {
2176 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2177 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2178 1c4cdd89 2021-06-20 stsp if (err)
2179 1c4cdd89 2021-06-20 stsp goto done;
2180 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2181 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2182 fddefe3b 2020-10-20 stsp free(ondisk_path);
2183 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2184 1c4cdd89 2021-06-20 stsp parent = NULL;
2185 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2186 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2187 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2188 fddefe3b 2020-10-20 stsp ondisk_path);
2189 90285c3b 2019-01-08 stsp break;
2190 90285c3b 2019-01-08 stsp }
2191 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2192 1c4cdd89 2021-06-20 stsp if (err)
2193 1c4cdd89 2021-06-20 stsp break;
2194 1c4cdd89 2021-06-20 stsp }
2195 90285c3b 2019-01-08 stsp }
2196 1c4cdd89 2021-06-20 stsp done:
2197 90285c3b 2019-01-08 stsp free(ondisk_path);
2198 1c4cdd89 2021-06-20 stsp free(parent);
2199 90285c3b 2019-01-08 stsp return err;
2200 512f0d0e 2019-01-02 stsp }
2201 512f0d0e 2019-01-02 stsp
2202 708d8e67 2019-03-27 stsp static const struct got_error *
2203 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2204 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2205 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2206 708d8e67 2019-03-27 stsp {
2207 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2208 708d8e67 2019-03-27 stsp unsigned char status;
2209 708d8e67 2019-03-27 stsp struct stat sb;
2210 708d8e67 2019-03-27 stsp char *ondisk_path;
2211 708d8e67 2019-03-27 stsp
2212 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2213 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2214 a76c42e6 2019-08-03 stsp
2215 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2216 708d8e67 2019-03-27 stsp == -1)
2217 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2218 708d8e67 2019-03-27 stsp
2219 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2220 708d8e67 2019-03-27 stsp if (err)
2221 5a58a424 2020-06-23 stsp goto done;
2222 708d8e67 2019-03-27 stsp
2223 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2224 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2225 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2226 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2227 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2228 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2229 993e2a1b 2020-07-23 stsp goto done;
2230 993e2a1b 2020-07-23 stsp }
2231 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2232 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2233 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2234 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2235 993e2a1b 2020-07-23 stsp if (err)
2236 993e2a1b 2020-07-23 stsp goto done;
2237 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2238 993e2a1b 2020-07-23 stsp ie->path);
2239 993e2a1b 2020-07-23 stsp goto done;
2240 993e2a1b 2020-07-23 stsp }
2241 993e2a1b 2020-07-23 stsp
2242 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2243 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2244 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2245 1ee397ad 2019-07-12 stsp if (err)
2246 5a58a424 2020-06-23 stsp goto done;
2247 fc6346c4 2019-03-27 stsp /*
2248 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2249 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2250 fc6346c4 2019-03-27 stsp */
2251 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2252 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2253 fc6346c4 2019-03-27 stsp } else {
2254 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2255 1ee397ad 2019-07-12 stsp if (err)
2256 5a58a424 2020-06-23 stsp goto done;
2257 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2258 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2259 fc6346c4 2019-03-27 stsp if (err)
2260 5a58a424 2020-06-23 stsp goto done;
2261 fc6346c4 2019-03-27 stsp }
2262 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2263 708d8e67 2019-03-27 stsp }
2264 5a58a424 2020-06-23 stsp done:
2265 5a58a424 2020-06-23 stsp free(ondisk_path);
2266 fc6346c4 2019-03-27 stsp return err;
2267 708d8e67 2019-03-27 stsp }
2268 708d8e67 2019-03-27 stsp
2269 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2270 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2271 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2272 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2273 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2274 8da9e5f4 2019-01-12 stsp void *progress_arg;
2275 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2276 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2277 8da9e5f4 2019-01-12 stsp };
2278 8da9e5f4 2019-01-12 stsp
2279 512f0d0e 2019-01-02 stsp static const struct got_error *
2280 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2281 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2282 512f0d0e 2019-01-02 stsp {
2283 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2284 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2285 0584f854 2019-04-06 stsp
2286 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2287 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2288 eb6fb9cd 2023-07-26 thomas if (err)
2289 eb6fb9cd 2023-07-26 thomas return err;
2290 eb6fb9cd 2023-07-26 thomas }
2291 512f0d0e 2019-01-02 stsp
2292 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2293 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2294 8da9e5f4 2019-01-12 stsp }
2295 512f0d0e 2019-01-02 stsp
2296 8da9e5f4 2019-01-12 stsp static const struct got_error *
2297 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2298 8da9e5f4 2019-01-12 stsp {
2299 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2300 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2301 7a9df742 2019-01-08 stsp
2302 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2303 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2304 eb6fb9cd 2023-07-26 thomas if (err)
2305 eb6fb9cd 2023-07-26 thomas return err;
2306 eb6fb9cd 2023-07-26 thomas }
2307 0584f854 2019-04-06 stsp
2308 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2309 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2310 9d31a1d8 2018-03-11 stsp }
2311 9d31a1d8 2018-03-11 stsp
2312 9d31a1d8 2018-03-11 stsp static const struct got_error *
2313 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2314 9d31a1d8 2018-03-11 stsp {
2315 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
2316 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2317 8da9e5f4 2019-01-12 stsp char *path;
2318 9d31a1d8 2018-03-11 stsp
2319 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
2320 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
2321 eb6fb9cd 2023-07-26 thomas if (err)
2322 eb6fb9cd 2023-07-26 thomas return err;
2323 eb6fb9cd 2023-07-26 thomas }
2324 0584f854 2019-04-06 stsp
2325 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2326 59530ea7 2023-07-17 thomas return NULL;
2327 59530ea7 2023-07-17 thomas
2328 59530ea7 2023-07-17 thomas if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2329 63c5ca5d 2019-08-24 stsp return NULL;
2330 63c5ca5d 2019-08-24 stsp
2331 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2332 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2333 8da9e5f4 2019-01-12 stsp == -1)
2334 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2335 9d31a1d8 2018-03-11 stsp
2336 59530ea7 2023-07-17 thomas err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2337 59530ea7 2023-07-17 thomas a->repo, a->progress_cb, a->progress_arg);
2338 9d31a1d8 2018-03-11 stsp
2339 8da9e5f4 2019-01-12 stsp free(path);
2340 0cd1c46a 2019-03-11 stsp return err;
2341 0cd1c46a 2019-03-11 stsp }
2342 0cd1c46a 2019-03-11 stsp
2343 b2118c49 2020-07-28 stsp const struct got_error *
2344 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2345 b2118c49 2020-07-28 stsp {
2346 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2347 b2118c49 2020-07-28 stsp
2348 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2349 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2350 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2351 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2352 b2118c49 2020-07-28 stsp }
2353 b2118c49 2020-07-28 stsp
2354 b2118c49 2020-07-28 stsp return NULL;
2355 b2118c49 2020-07-28 stsp }
2356 b2118c49 2020-07-28 stsp
2357 818c7501 2019-07-11 stsp static const struct got_error *
2358 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2359 0cd1c46a 2019-03-11 stsp {
2360 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2361 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2362 0cd1c46a 2019-03-11 stsp
2363 517bab73 2019-03-11 stsp *refname = NULL;
2364 517bab73 2019-03-11 stsp
2365 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2366 b2118c49 2020-07-28 stsp if (err)
2367 b2118c49 2020-07-28 stsp return err;
2368 0cd1c46a 2019-03-11 stsp
2369 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2370 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2371 0647c563 2019-03-11 stsp *refname = NULL;
2372 0cd1c46a 2019-03-11 stsp }
2373 517bab73 2019-03-11 stsp free(uuidstr);
2374 517bab73 2019-03-11 stsp return err;
2375 517bab73 2019-03-11 stsp }
2376 517bab73 2019-03-11 stsp
2377 818c7501 2019-07-11 stsp const struct got_error *
2378 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2379 f6cd0243 2023-01-28 thomas const char *prefix)
2380 f6cd0243 2023-01-28 thomas {
2381 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2382 f6cd0243 2023-01-28 thomas }
2383 f6cd0243 2023-01-28 thomas
2384 fcfaa6d5 2023-07-05 thomas static const struct got_error *
2385 fcfaa6d5 2023-07-05 thomas get_base_ref_name(char **refname, struct got_worktree *worktree)
2386 818c7501 2019-07-11 stsp {
2387 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2388 818c7501 2019-07-11 stsp }
2389 818c7501 2019-07-11 stsp
2390 818c7501 2019-07-11 stsp static const struct got_error *
2391 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2392 818c7501 2019-07-11 stsp {
2393 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2394 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2395 818c7501 2019-07-11 stsp }
2396 818c7501 2019-07-11 stsp
2397 818c7501 2019-07-11 stsp static const struct got_error *
2398 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2399 818c7501 2019-07-11 stsp {
2400 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2401 818c7501 2019-07-11 stsp }
2402 818c7501 2019-07-11 stsp
2403 818c7501 2019-07-11 stsp static const struct got_error *
2404 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2405 818c7501 2019-07-11 stsp {
2406 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2407 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2408 818c7501 2019-07-11 stsp }
2409 818c7501 2019-07-11 stsp
2410 818c7501 2019-07-11 stsp static const struct got_error *
2411 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2412 818c7501 2019-07-11 stsp {
2413 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2414 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2415 0ebf8283 2019-07-24 stsp }
2416 0ebf8283 2019-07-24 stsp
2417 0ebf8283 2019-07-24 stsp static const struct got_error *
2418 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2419 0ebf8283 2019-07-24 stsp {
2420 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2421 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2422 0ebf8283 2019-07-24 stsp }
2423 0ebf8283 2019-07-24 stsp
2424 0ebf8283 2019-07-24 stsp static const struct got_error *
2425 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2426 0ebf8283 2019-07-24 stsp {
2427 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2428 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2429 0ebf8283 2019-07-24 stsp }
2430 0ebf8283 2019-07-24 stsp
2431 0ebf8283 2019-07-24 stsp static const struct got_error *
2432 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2433 0ebf8283 2019-07-24 stsp {
2434 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2435 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2436 818c7501 2019-07-11 stsp }
2437 818c7501 2019-07-11 stsp
2438 0ebf8283 2019-07-24 stsp static const struct got_error *
2439 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2440 0ebf8283 2019-07-24 stsp {
2441 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2442 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2443 0ebf8283 2019-07-24 stsp }
2444 818c7501 2019-07-11 stsp
2445 0ebf8283 2019-07-24 stsp const struct got_error *
2446 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2447 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2448 0ebf8283 2019-07-24 stsp {
2449 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2450 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2451 0ebf8283 2019-07-24 stsp *path = NULL;
2452 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2453 0ebf8283 2019-07-24 stsp }
2454 0ebf8283 2019-07-24 stsp return NULL;
2455 10604dce 2021-09-24 thomas }
2456 10604dce 2021-09-24 thomas
2457 10604dce 2021-09-24 thomas static const struct got_error *
2458 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2459 10604dce 2021-09-24 thomas {
2460 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2461 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2462 0ebf8283 2019-07-24 stsp }
2463 0ebf8283 2019-07-24 stsp
2464 10604dce 2021-09-24 thomas static const struct got_error *
2465 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2466 10604dce 2021-09-24 thomas {
2467 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2468 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2469 10604dce 2021-09-24 thomas }
2470 10604dce 2021-09-24 thomas
2471 517bab73 2019-03-11 stsp /*
2472 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2473 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2474 517bab73 2019-03-11 stsp */
2475 517bab73 2019-03-11 stsp static const struct got_error *
2476 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2477 517bab73 2019-03-11 stsp {
2478 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2479 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2480 517bab73 2019-03-11 stsp char *refname;
2481 517bab73 2019-03-11 stsp
2482 fcfaa6d5 2023-07-05 thomas err = get_base_ref_name(&refname, worktree);
2483 517bab73 2019-03-11 stsp if (err)
2484 517bab73 2019-03-11 stsp return err;
2485 0cd1c46a 2019-03-11 stsp
2486 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2487 0cd1c46a 2019-03-11 stsp if (err)
2488 0cd1c46a 2019-03-11 stsp goto done;
2489 0cd1c46a 2019-03-11 stsp
2490 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2491 0cd1c46a 2019-03-11 stsp done:
2492 0cd1c46a 2019-03-11 stsp free(refname);
2493 0cd1c46a 2019-03-11 stsp if (ref)
2494 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2495 3e3a69f1 2019-07-25 stsp return err;
2496 3e3a69f1 2019-07-25 stsp }
2497 3e3a69f1 2019-07-25 stsp
2498 3e3a69f1 2019-07-25 stsp static const struct got_error *
2499 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2500 3e3a69f1 2019-07-25 stsp {
2501 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2502 3e3a69f1 2019-07-25 stsp
2503 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2504 ad10f64e 2023-07-19 thomas worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2505 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2506 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2507 3e3a69f1 2019-07-25 stsp }
2508 9d31a1d8 2018-03-11 stsp return err;
2509 9d31a1d8 2018-03-11 stsp }
2510 9d31a1d8 2018-03-11 stsp
2511 3e3a69f1 2019-07-25 stsp
2512 ebf99748 2019-05-09 stsp static const struct got_error *
2513 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2514 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2515 ebf99748 2019-05-09 stsp {
2516 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2517 ebf99748 2019-05-09 stsp FILE *index = NULL;
2518 0cd1c46a 2019-03-11 stsp
2519 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2520 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2521 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2522 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2523 ebf99748 2019-05-09 stsp
2524 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2525 3e3a69f1 2019-07-25 stsp if (err)
2526 ebf99748 2019-05-09 stsp goto done;
2527 ebf99748 2019-05-09 stsp
2528 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2529 ebf99748 2019-05-09 stsp if (index == NULL) {
2530 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2531 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2532 ebf99748 2019-05-09 stsp } else {
2533 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2534 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2535 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2536 ebf99748 2019-05-09 stsp }
2537 ebf99748 2019-05-09 stsp done:
2538 ebf99748 2019-05-09 stsp if (err) {
2539 ebf99748 2019-05-09 stsp free(*fileindex_path);
2540 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2541 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2542 ebf99748 2019-05-09 stsp *fileindex = NULL;
2543 ebf99748 2019-05-09 stsp }
2544 ebf99748 2019-05-09 stsp return err;
2545 ebf99748 2019-05-09 stsp }
2546 c932eeeb 2019-05-22 stsp
2547 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2548 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2549 c932eeeb 2019-05-22 stsp const char *path;
2550 c932eeeb 2019-05-22 stsp size_t path_len;
2551 c932eeeb 2019-05-22 stsp const char *entry_name;
2552 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2553 a484d721 2019-06-10 stsp void *progress_arg;
2554 c932eeeb 2019-05-22 stsp };
2555 ebf99748 2019-05-09 stsp
2556 c932eeeb 2019-05-22 stsp static const struct got_error *
2557 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2558 c932eeeb 2019-05-22 stsp {
2559 1ee397ad 2019-07-12 stsp const struct got_error *err;
2560 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2561 c932eeeb 2019-05-22 stsp
2562 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2563 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2564 c932eeeb 2019-05-22 stsp return NULL;
2565 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2566 102ff934 2019-06-11 stsp return NULL;
2567 102ff934 2019-06-11 stsp
2568 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2569 a769b60b 2021-06-27 stsp return NULL;
2570 a769b60b 2021-06-27 stsp
2571 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2572 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2573 c932eeeb 2019-05-22 stsp return NULL;
2574 c932eeeb 2019-05-22 stsp
2575 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2576 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2577 edd02c5e 2019-07-11 stsp ie->path);
2578 1ee397ad 2019-07-12 stsp if (err)
2579 1ee397ad 2019-07-12 stsp return err;
2580 1ee397ad 2019-07-12 stsp }
2581 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2582 c932eeeb 2019-05-22 stsp return NULL;
2583 a615e0e7 2020-12-16 stsp }
2584 a615e0e7 2020-12-16 stsp
2585 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2586 a615e0e7 2020-12-16 stsp static const struct got_error *
2587 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2588 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2589 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2590 a615e0e7 2020-12-16 stsp {
2591 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2592 a615e0e7 2020-12-16 stsp
2593 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2594 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2595 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2596 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2597 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2598 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2599 a615e0e7 2020-12-16 stsp
2600 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2601 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2602 9c6338c4 2019-06-02 stsp }
2603 9c6338c4 2019-06-02 stsp
2604 9c6338c4 2019-06-02 stsp static const struct got_error *
2605 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2606 9c6338c4 2019-06-02 stsp {
2607 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2608 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2609 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2610 867630bb 2020-01-17 stsp struct timespec timeout;
2611 9c6338c4 2019-06-02 stsp
2612 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2613 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2614 9c6338c4 2019-06-02 stsp if (err)
2615 9c6338c4 2019-06-02 stsp goto done;
2616 9c6338c4 2019-06-02 stsp
2617 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2618 9c6338c4 2019-06-02 stsp if (err)
2619 9c6338c4 2019-06-02 stsp goto done;
2620 9c6338c4 2019-06-02 stsp
2621 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2622 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2623 9c6338c4 2019-06-02 stsp fileindex_path);
2624 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2625 9c6338c4 2019-06-02 stsp }
2626 867630bb 2020-01-17 stsp
2627 867630bb 2020-01-17 stsp /*
2628 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2629 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2630 867630bb 2020-01-17 stsp * was recorded in the file index.
2631 867630bb 2020-01-17 stsp */
2632 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2633 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2634 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2635 9c6338c4 2019-06-02 stsp done:
2636 9c6338c4 2019-06-02 stsp if (new_index)
2637 9c6338c4 2019-06-02 stsp fclose(new_index);
2638 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2639 9c6338c4 2019-06-02 stsp return err;
2640 c932eeeb 2019-05-22 stsp }
2641 6ced4176 2019-07-12 stsp
2642 6ced4176 2019-07-12 stsp static const struct got_error *
2643 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2644 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2645 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2646 945f9229 2022-04-16 thomas struct got_repository *repo)
2647 6ced4176 2019-07-12 stsp {
2648 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2649 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2650 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2651 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2652 6ced4176 2019-07-12 stsp
2653 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2654 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2655 6ced4176 2019-07-12 stsp *tree_id = NULL;
2656 6ced4176 2019-07-12 stsp
2657 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2658 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2659 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2660 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2661 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2662 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2663 6ced4176 2019-07-12 stsp goto done;
2664 6ced4176 2019-07-12 stsp }
2665 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2666 945f9229 2022-04-16 thomas worktree->path_prefix);
2667 6ced4176 2019-07-12 stsp if (err)
2668 6ced4176 2019-07-12 stsp goto done;
2669 6ced4176 2019-07-12 stsp return NULL;
2670 6ced4176 2019-07-12 stsp }
2671 6ced4176 2019-07-12 stsp
2672 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2673 6ced4176 2019-07-12 stsp
2674 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2675 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2676 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2677 6ced4176 2019-07-12 stsp goto done;
2678 6ced4176 2019-07-12 stsp }
2679 6ced4176 2019-07-12 stsp
2680 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2681 6ced4176 2019-07-12 stsp if (err)
2682 6ced4176 2019-07-12 stsp goto done;
2683 6ced4176 2019-07-12 stsp
2684 6ced4176 2019-07-12 stsp free(in_repo_path);
2685 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2686 6ced4176 2019-07-12 stsp
2687 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2688 6ced4176 2019-07-12 stsp if (err)
2689 6ced4176 2019-07-12 stsp goto done;
2690 c932eeeb 2019-05-22 stsp
2691 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2692 6ced4176 2019-07-12 stsp /* Check out a single file. */
2693 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2694 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2695 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2696 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2697 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2698 6ced4176 2019-07-12 stsp goto done;
2699 6ced4176 2019-07-12 stsp }
2700 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2701 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2702 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2703 6ced4176 2019-07-12 stsp goto done;
2704 6ced4176 2019-07-12 stsp }
2705 6ced4176 2019-07-12 stsp } else {
2706 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2707 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2708 6ced4176 2019-07-12 stsp if (err)
2709 6ced4176 2019-07-12 stsp return err;
2710 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2711 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2712 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2713 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2714 6ced4176 2019-07-12 stsp goto done;
2715 6ced4176 2019-07-12 stsp }
2716 6ced4176 2019-07-12 stsp }
2717 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2718 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2719 6ced4176 2019-07-12 stsp } else {
2720 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2721 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2722 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2723 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2724 6ced4176 2019-07-12 stsp goto done;
2725 6ced4176 2019-07-12 stsp }
2726 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2727 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2728 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2729 6ced4176 2019-07-12 stsp goto done;
2730 6ced4176 2019-07-12 stsp }
2731 6ced4176 2019-07-12 stsp }
2732 6ced4176 2019-07-12 stsp done:
2733 6ced4176 2019-07-12 stsp free(id);
2734 6ced4176 2019-07-12 stsp free(in_repo_path);
2735 6ced4176 2019-07-12 stsp if (err) {
2736 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2737 6ced4176 2019-07-12 stsp free(*tree_relpath);
2738 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2739 6ced4176 2019-07-12 stsp free(*tree_id);
2740 6ced4176 2019-07-12 stsp *tree_id = NULL;
2741 6ced4176 2019-07-12 stsp }
2742 07ed472d 2019-07-12 stsp return err;
2743 07ed472d 2019-07-12 stsp }
2744 07ed472d 2019-07-12 stsp
2745 07ed472d 2019-07-12 stsp static const struct got_error *
2746 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2747 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2748 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2749 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2750 07ed472d 2019-07-12 stsp {
2751 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2752 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2753 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2754 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2755 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2756 07ed472d 2019-07-12 stsp
2757 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2758 7f47418f 2019-12-20 stsp if (err) {
2759 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2760 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2761 7f47418f 2019-12-20 stsp goto done;
2762 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2763 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2764 7f47418f 2019-12-20 stsp if (err)
2765 7f47418f 2019-12-20 stsp return err;
2766 7f47418f 2019-12-20 stsp }
2767 07ed472d 2019-07-12 stsp
2768 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2769 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2770 07ed472d 2019-07-12 stsp if (err)
2771 07ed472d 2019-07-12 stsp goto done;
2772 07ed472d 2019-07-12 stsp
2773 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2774 07ed472d 2019-07-12 stsp if (err)
2775 07ed472d 2019-07-12 stsp goto done;
2776 07ed472d 2019-07-12 stsp
2777 07ed472d 2019-07-12 stsp if (entry_name &&
2778 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2779 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2780 07ed472d 2019-07-12 stsp goto done;
2781 07ed472d 2019-07-12 stsp }
2782 07ed472d 2019-07-12 stsp
2783 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2784 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2785 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2786 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2787 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2788 07ed472d 2019-07-12 stsp arg.repo = repo;
2789 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2790 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2791 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2792 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2793 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2794 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2795 07ed472d 2019-07-12 stsp done:
2796 07ed472d 2019-07-12 stsp if (tree)
2797 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2798 07ed472d 2019-07-12 stsp if (commit)
2799 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2800 6ced4176 2019-07-12 stsp return err;
2801 6ced4176 2019-07-12 stsp }
2802 6ced4176 2019-07-12 stsp
2803 9d31a1d8 2018-03-11 stsp const struct got_error *
2804 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2805 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2806 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2807 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2808 9d31a1d8 2018-03-11 stsp {
2809 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2810 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2811 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2812 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2813 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2814 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2815 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2816 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2817 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2818 f2ea84fa 2019-07-27 stsp int entry_type;
2819 f2ea84fa 2019-07-27 stsp char *relpath;
2820 f2ea84fa 2019-07-27 stsp char *entry_name;
2821 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2822 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2823 9d31a1d8 2018-03-11 stsp
2824 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2825 f2ea84fa 2019-07-27 stsp
2826 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2827 9d31a1d8 2018-03-11 stsp if (err)
2828 9d31a1d8 2018-03-11 stsp return err;
2829 945f9229 2022-04-16 thomas
2830 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2831 945f9229 2022-04-16 thomas worktree->base_commit_id);
2832 945f9229 2022-04-16 thomas if (err)
2833 945f9229 2022-04-16 thomas goto done;
2834 93a30277 2018-12-24 stsp
2835 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2836 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2837 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2838 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2839 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2840 f2ea84fa 2019-07-27 stsp goto done;
2841 f2ea84fa 2019-07-27 stsp }
2842 6ced4176 2019-07-12 stsp
2843 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2844 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2845 945f9229 2022-04-16 thomas worktree, repo);
2846 f2ea84fa 2019-07-27 stsp if (err) {
2847 f2ea84fa 2019-07-27 stsp free(tpd);
2848 6ced4176 2019-07-12 stsp goto done;
2849 6ced4176 2019-07-12 stsp }
2850 f2ea84fa 2019-07-27 stsp
2851 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2852 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2853 f2ea84fa 2019-07-27 stsp if (err) {
2854 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2855 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2856 f2ea84fa 2019-07-27 stsp free(tpd);
2857 f2ea84fa 2019-07-27 stsp goto done;
2858 f2ea84fa 2019-07-27 stsp }
2859 f2ea84fa 2019-07-27 stsp } else
2860 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2861 f2ea84fa 2019-07-27 stsp
2862 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2863 6ced4176 2019-07-12 stsp }
2864 6ced4176 2019-07-12 stsp
2865 51514078 2018-12-25 stsp /*
2866 51514078 2018-12-25 stsp * Read the file index.
2867 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2868 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2869 51514078 2018-12-25 stsp */
2870 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2871 8da9e5f4 2019-01-12 stsp if (err)
2872 c4cdcb68 2019-04-03 stsp goto done;
2873 c4cdcb68 2019-04-03 stsp
2874 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2875 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2876 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2877 f2ea84fa 2019-07-27 stsp
2878 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2879 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2880 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2881 f2ea84fa 2019-07-27 stsp if (err)
2882 f2ea84fa 2019-07-27 stsp break;
2883 f2ea84fa 2019-07-27 stsp
2884 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2885 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2886 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2887 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2888 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2889 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2890 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2891 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2892 f2ea84fa 2019-07-27 stsp if (err)
2893 f2ea84fa 2019-07-27 stsp break;
2894 f2ea84fa 2019-07-27 stsp
2895 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2896 711d9cd9 2019-07-12 stsp }
2897 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2898 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2899 af12c6b9 2019-06-04 stsp err = sync_err;
2900 9d31a1d8 2018-03-11 stsp done:
2901 9c6338c4 2019-06-02 stsp free(fileindex_path);
2902 edfa7d7f 2018-09-11 stsp if (tree)
2903 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2904 9d31a1d8 2018-03-11 stsp if (commit)
2905 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2906 5ade8233 2019-07-12 stsp if (fileindex)
2907 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2908 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2909 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2910 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2911 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2912 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2913 f2ea84fa 2019-07-27 stsp free(tpd);
2914 f2ea84fa 2019-07-27 stsp }
2915 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2916 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2917 9d31a1d8 2018-03-11 stsp err = unlockerr;
2918 f8d1f275 2019-02-04 stsp return err;
2919 f8d1f275 2019-02-04 stsp }
2920 f8d1f275 2019-02-04 stsp
2921 0529f8df 2023-03-10 thomas static const struct got_error *
2922 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2923 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2924 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2925 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2926 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2927 0529f8df 2023-03-10 thomas struct got_repository *repo,
2928 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2929 0529f8df 2023-03-10 thomas {
2930 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2931 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2932 0529f8df 2023-03-10 thomas
2933 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2934 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2935 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2936 0529f8df 2023-03-10 thomas restoring_missing_file,
2937 0529f8df 2023-03-10 thomas reverting_versioned_file,
2938 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2939 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2940 0529f8df 2023-03-10 thomas } else {
2941 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2942 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2943 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2944 aaf972e2 2023-09-18 thomas path_is_unversioned, NULL, repo,
2945 aaf972e2 2023-09-18 thomas progress_cb, progress_arg);
2946 0529f8df 2023-03-10 thomas }
2947 0529f8df 2023-03-10 thomas if (err)
2948 0529f8df 2023-03-10 thomas return err;
2949 0529f8df 2023-03-10 thomas if (ie == NULL) {
2950 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2951 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2952 0529f8df 2023-03-10 thomas if (err)
2953 0529f8df 2023-03-10 thomas return err;
2954 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2955 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2956 0529f8df 2023-03-10 thomas if (err) {
2957 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2958 0529f8df 2023-03-10 thomas return err;
2959 0529f8df 2023-03-10 thomas }
2960 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2961 0529f8df 2023-03-10 thomas if (err) {
2962 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2963 0529f8df 2023-03-10 thomas return err;
2964 0529f8df 2023-03-10 thomas }
2965 0529f8df 2023-03-10 thomas } else {
2966 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2967 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2968 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2969 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2970 0529f8df 2023-03-10 thomas if (err)
2971 0529f8df 2023-03-10 thomas return err;
2972 0529f8df 2023-03-10 thomas }
2973 0529f8df 2023-03-10 thomas
2974 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2975 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2976 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2977 0529f8df 2023-03-10 thomas }
2978 0529f8df 2023-03-10 thomas
2979 0529f8df 2023-03-10 thomas return NULL;
2980 0529f8df 2023-03-10 thomas }
2981 0529f8df 2023-03-10 thomas
2982 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2983 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2984 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2985 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2986 234035bc 2019-06-01 stsp void *progress_arg;
2987 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2988 234035bc 2019-06-01 stsp void *cancel_arg;
2989 f69721c3 2019-10-21 stsp const char *label_orig;
2990 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2991 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2992 234035bc 2019-06-01 stsp };
2993 234035bc 2019-06-01 stsp
2994 234035bc 2019-06-01 stsp static const struct got_error *
2995 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2996 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2997 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2998 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2999 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3000 234035bc 2019-06-01 stsp {
3001 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
3002 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
3003 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
3004 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
3005 234035bc 2019-06-01 stsp struct stat sb;
3006 234035bc 2019-06-01 stsp unsigned char status;
3007 234035bc 2019-06-01 stsp int local_changes_subsumed;
3008 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3009 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
3010 4c3671a9 2023-07-26 thomas struct got_object_id *id = NULL;
3011 234035bc 2019-06-01 stsp
3012 234035bc 2019-06-01 stsp if (blob1 && blob2) {
3013 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3014 d6c87207 2019-08-02 stsp strlen(path2));
3015 1ee397ad 2019-07-12 stsp if (ie == NULL)
3016 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3017 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
3018 234035bc 2019-06-01 stsp
3019 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3020 234035bc 2019-06-01 stsp path2) == -1)
3021 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3022 234035bc 2019-06-01 stsp
3023 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3024 7f91a133 2019-12-13 stsp repo);
3025 234035bc 2019-06-01 stsp if (err)
3026 234035bc 2019-06-01 stsp goto done;
3027 234035bc 2019-06-01 stsp
3028 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3029 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3030 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3031 234035bc 2019-06-01 stsp goto done;
3032 234035bc 2019-06-01 stsp }
3033 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3034 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3035 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3036 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3037 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3038 234035bc 2019-06-01 stsp goto done;
3039 234035bc 2019-06-01 stsp }
3040 234035bc 2019-06-01 stsp
3041 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3042 36bf999c 2020-07-23 stsp char *link_target2;
3043 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3044 36bf999c 2020-07-23 stsp if (err)
3045 36bf999c 2020-07-23 stsp goto done;
3046 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3047 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3048 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3049 36bf999c 2020-07-23 stsp free(link_target2);
3050 af57b12a 2020-07-23 stsp } else {
3051 1af628f4 2021-06-03 stsp int fd;
3052 1af628f4 2021-06-03 stsp
3053 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3054 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3055 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3056 1af628f4 2021-06-03 stsp goto done;
3057 1af628f4 2021-06-03 stsp }
3058 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3059 1af628f4 2021-06-03 stsp f_orig, blob1);
3060 1af628f4 2021-06-03 stsp if (err)
3061 1af628f4 2021-06-03 stsp goto done;
3062 1af628f4 2021-06-03 stsp
3063 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3064 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3065 1af628f4 2021-06-03 stsp goto done;
3066 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3067 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3068 1af628f4 2021-06-03 stsp if (err)
3069 1af628f4 2021-06-03 stsp goto done;
3070 1af628f4 2021-06-03 stsp
3071 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3072 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3073 1af628f4 2021-06-03 stsp if (fd == -1) {
3074 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3075 1af628f4 2021-06-03 stsp ondisk_path);
3076 1af628f4 2021-06-03 stsp goto done;
3077 1af628f4 2021-06-03 stsp }
3078 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3079 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3080 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3081 1af628f4 2021-06-03 stsp ondisk_path);
3082 1af628f4 2021-06-03 stsp close(fd);
3083 1af628f4 2021-06-03 stsp goto done;
3084 1af628f4 2021-06-03 stsp }
3085 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3086 1af628f4 2021-06-03 stsp if (err)
3087 1af628f4 2021-06-03 stsp goto done;
3088 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3089 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3090 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3091 1af628f4 2021-06-03 stsp goto done;
3092 1af628f4 2021-06-03 stsp }
3093 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3094 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3095 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3096 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3097 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3098 af57b12a 2020-07-23 stsp }
3099 234035bc 2019-06-01 stsp } else if (blob1) {
3100 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3101 d6c87207 2019-08-02 stsp strlen(path1));
3102 1ee397ad 2019-07-12 stsp if (ie == NULL)
3103 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3104 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3105 2b92fad7 2019-06-02 stsp
3106 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3107 2b92fad7 2019-06-02 stsp path1) == -1)
3108 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3109 2b92fad7 2019-06-02 stsp
3110 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3111 7f91a133 2019-12-13 stsp repo);
3112 2b92fad7 2019-06-02 stsp if (err)
3113 2b92fad7 2019-06-02 stsp goto done;
3114 2b92fad7 2019-06-02 stsp
3115 2b92fad7 2019-06-02 stsp switch (status) {
3116 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3117 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3118 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3119 1ee397ad 2019-07-12 stsp if (err)
3120 1ee397ad 2019-07-12 stsp goto done;
3121 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3122 2b92fad7 2019-06-02 stsp if (err)
3123 2b92fad7 2019-06-02 stsp goto done;
3124 2b92fad7 2019-06-02 stsp if (ie)
3125 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3126 2b92fad7 2019-06-02 stsp break;
3127 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3128 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3129 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3130 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3131 1ee397ad 2019-07-12 stsp if (err)
3132 1ee397ad 2019-07-12 stsp goto done;
3133 2b92fad7 2019-06-02 stsp if (ie)
3134 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3135 2b92fad7 2019-06-02 stsp break;
3136 4c3671a9 2023-07-26 thomas case GOT_STATUS_MODIFY: {
3137 4c3671a9 2023-07-26 thomas FILE *blob1_f;
3138 4c3671a9 2023-07-26 thomas off_t blob1_size;
3139 4c3671a9 2023-07-26 thomas int obj_type;
3140 4c3671a9 2023-07-26 thomas /*
3141 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3142 4c3671a9 2023-07-26 thomas * exists in the repository.
3143 4c3671a9 2023-07-26 thomas */
3144 4c3671a9 2023-07-26 thomas err = got_object_blob_file_create(&id, &blob1_f,
3145 4c3671a9 2023-07-26 thomas &blob1_size, path1);
3146 4c3671a9 2023-07-26 thomas if (err)
3147 4c3671a9 2023-07-26 thomas goto done;
3148 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3149 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3150 4c3671a9 2023-07-26 thomas goto done;
3151 4c3671a9 2023-07-26 thomas }
3152 4c3671a9 2023-07-26 thomas
3153 4c3671a9 2023-07-26 thomas /* Implied existence check. */
3154 4c3671a9 2023-07-26 thomas err = got_object_get_type(&obj_type, repo, id);
3155 4c3671a9 2023-07-26 thomas if (err) {
3156 4c3671a9 2023-07-26 thomas if (err->code != GOT_ERR_NO_OBJ)
3157 4c3671a9 2023-07-26 thomas goto done;
3158 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3159 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3160 4c3671a9 2023-07-26 thomas goto done;
3161 4c3671a9 2023-07-26 thomas } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3162 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3163 4c3671a9 2023-07-26 thomas GOT_STATUS_CANNOT_DELETE, path1);
3164 4c3671a9 2023-07-26 thomas goto done;
3165 4c3671a9 2023-07-26 thomas }
3166 4c3671a9 2023-07-26 thomas err = (*a->progress_cb)(a->progress_arg,
3167 4c3671a9 2023-07-26 thomas GOT_STATUS_DELETE, path1);
3168 4c3671a9 2023-07-26 thomas if (err)
3169 4c3671a9 2023-07-26 thomas goto done;
3170 4c3671a9 2023-07-26 thomas err = remove_ondisk_file(a->worktree->root_path,
3171 4c3671a9 2023-07-26 thomas path1);
3172 4c3671a9 2023-07-26 thomas if (err)
3173 4c3671a9 2023-07-26 thomas goto done;
3174 4c3671a9 2023-07-26 thomas if (ie)
3175 4c3671a9 2023-07-26 thomas got_fileindex_entry_mark_deleted_from_disk(ie);
3176 4c3671a9 2023-07-26 thomas break;
3177 4c3671a9 2023-07-26 thomas }
3178 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3179 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3180 e8f02263 2022-01-23 thomas off_t blob1_size;
3181 0a22ca1a 2020-09-23 stsp /*
3182 4c3671a9 2023-07-26 thomas * Delete the file only if its content already
3183 0a22ca1a 2020-09-23 stsp * exists in the repository.
3184 0a22ca1a 2020-09-23 stsp */
3185 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3186 e8f02263 2022-01-23 thomas &blob1_size, path1);
3187 0a22ca1a 2020-09-23 stsp if (err)
3188 0a22ca1a 2020-09-23 stsp goto done;
3189 4c3671a9 2023-07-26 thomas if (fclose(blob1_f) == EOF) {
3190 4c3671a9 2023-07-26 thomas err = got_error_from_errno("fclose");
3191 4c3671a9 2023-07-26 thomas goto done;
3192 4c3671a9 2023-07-26 thomas }
3193 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3194 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3195 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3196 0a22ca1a 2020-09-23 stsp if (err)
3197 0a22ca1a 2020-09-23 stsp goto done;
3198 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3199 0a22ca1a 2020-09-23 stsp path1);
3200 0a22ca1a 2020-09-23 stsp if (err)
3201 0a22ca1a 2020-09-23 stsp goto done;
3202 0a22ca1a 2020-09-23 stsp if (ie)
3203 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3204 0a22ca1a 2020-09-23 stsp ie);
3205 0a22ca1a 2020-09-23 stsp } else {
3206 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3207 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3208 0a22ca1a 2020-09-23 stsp }
3209 0a22ca1a 2020-09-23 stsp if (err)
3210 0a22ca1a 2020-09-23 stsp goto done;
3211 0a22ca1a 2020-09-23 stsp break;
3212 0a22ca1a 2020-09-23 stsp }
3213 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3214 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3215 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3216 1ee397ad 2019-07-12 stsp if (err)
3217 1ee397ad 2019-07-12 stsp goto done;
3218 2b92fad7 2019-06-02 stsp break;
3219 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3220 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3221 1ee397ad 2019-07-12 stsp if (err)
3222 1ee397ad 2019-07-12 stsp goto done;
3223 2b92fad7 2019-06-02 stsp break;
3224 2b92fad7 2019-06-02 stsp default:
3225 2b92fad7 2019-06-02 stsp break;
3226 2b92fad7 2019-06-02 stsp }
3227 234035bc 2019-06-01 stsp } else if (blob2) {
3228 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3229 234035bc 2019-06-01 stsp path2) == -1)
3230 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3231 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3232 d6c87207 2019-08-02 stsp strlen(path2));
3233 234035bc 2019-06-01 stsp if (ie) {
3234 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3235 7f91a133 2019-12-13 stsp -1, NULL, repo);
3236 234035bc 2019-06-01 stsp if (err)
3237 234035bc 2019-06-01 stsp goto done;
3238 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3239 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3240 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3241 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3242 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3243 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3244 1ee397ad 2019-07-12 stsp status, path2);
3245 234035bc 2019-06-01 stsp goto done;
3246 234035bc 2019-06-01 stsp }
3247 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3248 36bf999c 2020-07-23 stsp char *link_target2;
3249 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3250 36bf999c 2020-07-23 stsp blob2);
3251 36bf999c 2020-07-23 stsp if (err)
3252 36bf999c 2020-07-23 stsp goto done;
3253 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3254 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3255 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3256 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3257 36bf999c 2020-07-23 stsp free(link_target2);
3258 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3259 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3260 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3261 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3262 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3263 526a746f 2020-07-23 stsp a->progress_arg);
3264 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3265 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3266 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3267 526a746f 2020-07-23 stsp }
3268 dfe9fba0 2020-07-23 stsp if (err)
3269 dfe9fba0 2020-07-23 stsp goto done;
3270 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3271 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3272 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3273 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3274 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3275 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3276 234035bc 2019-06-01 stsp if (err)
3277 234035bc 2019-06-01 stsp goto done;
3278 234035bc 2019-06-01 stsp }
3279 234035bc 2019-06-01 stsp } else {
3280 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3281 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3282 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3283 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3284 234035bc 2019-06-01 stsp if (err)
3285 2b92fad7 2019-06-02 stsp goto done;
3286 234035bc 2019-06-01 stsp }
3287 234035bc 2019-06-01 stsp }
3288 234035bc 2019-06-01 stsp done:
3289 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3290 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3291 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3292 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3293 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3294 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3295 1af628f4 2021-06-03 stsp free(id_str);
3296 4c3671a9 2023-07-26 thomas free(id);
3297 54d5be07 2021-06-03 stsp free(label_deriv2);
3298 234035bc 2019-06-01 stsp free(ondisk_path);
3299 234035bc 2019-06-01 stsp return err;
3300 234035bc 2019-06-01 stsp }
3301 6ebb2263 2023-07-26 thomas
3302 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args {
3303 6ebb2263 2023-07-26 thomas struct got_worktree *worktree;
3304 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb;
3305 6ebb2263 2023-07-26 thomas void *cancel_arg;
3306 6ebb2263 2023-07-26 thomas };
3307 234035bc 2019-06-01 stsp
3308 69de9dd4 2021-09-03 stsp static const struct got_error *
3309 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3310 69de9dd4 2021-09-03 stsp {
3311 eb6fb9cd 2023-07-26 thomas const struct got_error *err = NULL;
3312 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args *a = arg;
3313 6ebb2263 2023-07-26 thomas
3314 6ebb2263 2023-07-26 thomas if (a->cancel_cb) {
3315 6ebb2263 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3316 6ebb2263 2023-07-26 thomas if (err)
3317 6ebb2263 2023-07-26 thomas return err;
3318 6ebb2263 2023-07-26 thomas }
3319 69de9dd4 2021-09-03 stsp
3320 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3321 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3322 6ebb2263 2023-07-26 thomas memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3323 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3324 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3325 69de9dd4 2021-09-03 stsp
3326 69de9dd4 2021-09-03 stsp return NULL;
3327 69de9dd4 2021-09-03 stsp }
3328 69de9dd4 2021-09-03 stsp
3329 349dfd1e 2023-07-23 thomas const struct got_error *
3330 349dfd1e 2023-07-23 thomas got_worktree_get_state(char *state, struct got_repository *repo,
3331 6ebb2263 2023-07-26 thomas struct got_worktree *worktree,
3332 6ebb2263 2023-07-26 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3333 349dfd1e 2023-07-23 thomas {
3334 349dfd1e 2023-07-23 thomas const struct got_error *err;
3335 349dfd1e 2023-07-23 thomas struct got_object_id *base_id, *head_id = NULL;
3336 349dfd1e 2023-07-23 thomas struct got_reference *head_ref;
3337 349dfd1e 2023-07-23 thomas struct got_fileindex *fileindex = NULL;
3338 349dfd1e 2023-07-23 thomas char *fileindex_path = NULL;
3339 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3340 349dfd1e 2023-07-23 thomas
3341 349dfd1e 2023-07-23 thomas if (worktree == NULL)
3342 349dfd1e 2023-07-23 thomas return got_error(GOT_ERR_NOT_WORKTREE);
3343 349dfd1e 2023-07-23 thomas
3344 349dfd1e 2023-07-23 thomas err = got_ref_open(&head_ref, repo,
3345 349dfd1e 2023-07-23 thomas got_worktree_get_head_ref_name(worktree), 0);
3346 349dfd1e 2023-07-23 thomas if (err)
3347 349dfd1e 2023-07-23 thomas return err;
3348 349dfd1e 2023-07-23 thomas
3349 349dfd1e 2023-07-23 thomas err = got_ref_resolve(&head_id, repo, head_ref);
3350 349dfd1e 2023-07-23 thomas if (err)
3351 349dfd1e 2023-07-23 thomas goto done;
3352 349dfd1e 2023-07-23 thomas
3353 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UNKNOWN;
3354 349dfd1e 2023-07-23 thomas base_id = got_worktree_get_base_commit_id(worktree);
3355 349dfd1e 2023-07-23 thomas
3356 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3357 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3358 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3359 6ebb2263 2023-07-26 thomas
3360 349dfd1e 2023-07-23 thomas if (got_object_id_cmp(base_id, head_id) == 0) {
3361 349dfd1e 2023-07-23 thomas err = open_fileindex(&fileindex, &fileindex_path, worktree);
3362 349dfd1e 2023-07-23 thomas if (err)
3363 349dfd1e 2023-07-23 thomas goto done;
3364 349dfd1e 2023-07-23 thomas
3365 349dfd1e 2023-07-23 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3366 6ebb2263 2023-07-26 thomas check_mixed_commits, &cma);
3367 349dfd1e 2023-07-23 thomas if (err == NULL)
3368 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_UPTODATE;
3369 92845f09 2023-07-26 thomas else if (err->code == GOT_ERR_MIXED_COMMITS) {
3370 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3371 349dfd1e 2023-07-23 thomas err = NULL;
3372 92845f09 2023-07-26 thomas }
3373 92845f09 2023-07-26 thomas } else
3374 92845f09 2023-07-26 thomas *state = GOT_WORKTREE_STATE_OUTOFDATE;
3375 349dfd1e 2023-07-23 thomas
3376 349dfd1e 2023-07-23 thomas done:
3377 349dfd1e 2023-07-23 thomas free(head_id);
3378 349dfd1e 2023-07-23 thomas free(fileindex_path);
3379 349dfd1e 2023-07-23 thomas got_ref_close(head_ref);
3380 349dfd1e 2023-07-23 thomas if (fileindex != NULL)
3381 349dfd1e 2023-07-23 thomas got_fileindex_free(fileindex);
3382 349dfd1e 2023-07-23 thomas return err;
3383 349dfd1e 2023-07-23 thomas }
3384 349dfd1e 2023-07-23 thomas
3385 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3386 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3387 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3388 234035bc 2019-06-01 stsp struct got_repository *repo;
3389 234035bc 2019-06-01 stsp };
3390 234035bc 2019-06-01 stsp
3391 234035bc 2019-06-01 stsp static const struct got_error *
3392 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3393 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3394 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3395 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3396 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3397 234035bc 2019-06-01 stsp {
3398 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3399 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3400 234035bc 2019-06-01 stsp unsigned char status;
3401 234035bc 2019-06-01 stsp struct stat sb;
3402 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3403 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3404 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3405 234035bc 2019-06-01 stsp char *ondisk_path;
3406 234035bc 2019-06-01 stsp
3407 69de9dd4 2021-09-03 stsp if (id == NULL)
3408 69de9dd4 2021-09-03 stsp return NULL;
3409 234035bc 2019-06-01 stsp
3410 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3411 69de9dd4 2021-09-03 stsp if (ie == NULL)
3412 69de9dd4 2021-09-03 stsp return NULL;
3413 69de9dd4 2021-09-03 stsp
3414 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3415 234035bc 2019-06-01 stsp == -1)
3416 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3417 234035bc 2019-06-01 stsp
3418 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3419 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3420 5546d466 2021-09-02 stsp free(ondisk_path);
3421 234035bc 2019-06-01 stsp if (err)
3422 234035bc 2019-06-01 stsp return err;
3423 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3424 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3425 234035bc 2019-06-01 stsp
3426 234035bc 2019-06-01 stsp return NULL;
3427 234035bc 2019-06-01 stsp }
3428 234035bc 2019-06-01 stsp
3429 818c7501 2019-07-11 stsp static const struct got_error *
3430 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3431 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3432 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3433 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3434 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3435 234035bc 2019-06-01 stsp {
3436 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3437 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3438 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3439 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3440 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3441 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3442 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3443 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3444 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3445 234035bc 2019-06-01 stsp
3446 03415a1a 2019-06-02 stsp if (commit_id1) {
3447 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3448 945f9229 2022-04-16 thomas if (err)
3449 945f9229 2022-04-16 thomas goto done;
3450 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3451 03415a1a 2019-06-02 stsp worktree->path_prefix);
3452 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3453 03415a1a 2019-06-02 stsp goto done;
3454 69d57f3d 2020-07-31 stsp }
3455 69d57f3d 2020-07-31 stsp if (tree_id1) {
3456 69d57f3d 2020-07-31 stsp char *id_str;
3457 03415a1a 2019-06-02 stsp
3458 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3459 03415a1a 2019-06-02 stsp if (err)
3460 03415a1a 2019-06-02 stsp goto done;
3461 f69721c3 2019-10-21 stsp
3462 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3463 f69721c3 2019-10-21 stsp if (err)
3464 f69721c3 2019-10-21 stsp goto done;
3465 f69721c3 2019-10-21 stsp
3466 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3467 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3468 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3469 f69721c3 2019-10-21 stsp free(id_str);
3470 f69721c3 2019-10-21 stsp goto done;
3471 f69721c3 2019-10-21 stsp }
3472 f69721c3 2019-10-21 stsp free(id_str);
3473 a0f32f33 2022-06-13 thomas
3474 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3475 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3476 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3477 a0f32f33 2022-06-13 thomas goto done;
3478 a0f32f33 2022-06-13 thomas }
3479 03415a1a 2019-06-02 stsp }
3480 234035bc 2019-06-01 stsp
3481 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3482 945f9229 2022-04-16 thomas if (err)
3483 945f9229 2022-04-16 thomas goto done;
3484 945f9229 2022-04-16 thomas
3485 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3486 234035bc 2019-06-01 stsp worktree->path_prefix);
3487 234035bc 2019-06-01 stsp if (err)
3488 234035bc 2019-06-01 stsp goto done;
3489 234035bc 2019-06-01 stsp
3490 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3491 234035bc 2019-06-01 stsp if (err)
3492 234035bc 2019-06-01 stsp goto done;
3493 234035bc 2019-06-01 stsp
3494 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3495 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3496 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3497 19a6a6b5 2022-07-01 thomas goto done;
3498 19a6a6b5 2022-07-01 thomas }
3499 19a6a6b5 2022-07-01 thomas
3500 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3501 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3502 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3503 a0f32f33 2022-06-13 thomas goto done;
3504 a0f32f33 2022-06-13 thomas }
3505 a0f32f33 2022-06-13 thomas
3506 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3507 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3508 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3509 19a6a6b5 2022-07-01 thomas goto done;
3510 19a6a6b5 2022-07-01 thomas }
3511 19a6a6b5 2022-07-01 thomas
3512 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3513 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3514 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3515 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3516 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3517 69de9dd4 2021-09-03 stsp if (err)
3518 69de9dd4 2021-09-03 stsp goto done;
3519 69de9dd4 2021-09-03 stsp
3520 234035bc 2019-06-01 stsp arg.worktree = worktree;
3521 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3522 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3523 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3524 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3525 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3526 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3527 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3528 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3529 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3530 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3531 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3532 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3533 af12c6b9 2019-06-04 stsp err = sync_err;
3534 234035bc 2019-06-01 stsp done:
3535 945f9229 2022-04-16 thomas if (commit1)
3536 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3537 945f9229 2022-04-16 thomas if (commit2)
3538 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3539 234035bc 2019-06-01 stsp if (tree1)
3540 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3541 234035bc 2019-06-01 stsp if (tree2)
3542 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3543 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3544 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3545 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3546 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3547 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3548 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3549 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3550 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3551 f69721c3 2019-10-21 stsp free(label_orig);
3552 818c7501 2019-07-11 stsp return err;
3553 818c7501 2019-07-11 stsp }
3554 234035bc 2019-06-01 stsp
3555 818c7501 2019-07-11 stsp const struct got_error *
3556 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3557 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3558 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3559 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3560 818c7501 2019-07-11 stsp {
3561 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3562 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3563 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3564 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
3565 818c7501 2019-07-11 stsp
3566 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3567 818c7501 2019-07-11 stsp if (err)
3568 818c7501 2019-07-11 stsp return err;
3569 818c7501 2019-07-11 stsp
3570 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3571 818c7501 2019-07-11 stsp if (err)
3572 818c7501 2019-07-11 stsp goto done;
3573 818c7501 2019-07-11 stsp
3574 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
3575 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
3576 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
3577 6ebb2263 2023-07-26 thomas
3578 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3579 6ebb2263 2023-07-26 thomas &cma);
3580 818c7501 2019-07-11 stsp if (err)
3581 818c7501 2019-07-11 stsp goto done;
3582 818c7501 2019-07-11 stsp
3583 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3584 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3585 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3586 818c7501 2019-07-11 stsp done:
3587 818c7501 2019-07-11 stsp if (fileindex)
3588 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3589 818c7501 2019-07-11 stsp free(fileindex_path);
3590 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3591 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3592 234035bc 2019-06-01 stsp err = unlockerr;
3593 234035bc 2019-06-01 stsp return err;
3594 234035bc 2019-06-01 stsp }
3595 234035bc 2019-06-01 stsp
3596 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3597 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3598 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3599 927df6b7 2019-02-10 stsp const char *status_path;
3600 927df6b7 2019-02-10 stsp size_t status_path_len;
3601 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3602 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3603 f8d1f275 2019-02-04 stsp void *status_arg;
3604 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3605 f8d1f275 2019-02-04 stsp void *cancel_arg;
3606 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3607 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3608 f2a9dc41 2019-12-13 tracey int report_unchanged;
3609 3143d852 2020-06-25 stsp int no_ignores;
3610 f8d1f275 2019-02-04 stsp };
3611 88d0e355 2019-08-03 stsp
3612 f8d1f275 2019-02-04 stsp static const struct got_error *
3613 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3614 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3615 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3616 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3617 927df6b7 2019-02-10 stsp {
3618 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3619 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3620 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3621 927df6b7 2019-02-10 stsp struct stat sb;
3622 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3623 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3624 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3625 927df6b7 2019-02-10 stsp
3626 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3627 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3628 98eaaa12 2019-08-03 stsp if (err)
3629 98eaaa12 2019-08-03 stsp return err;
3630 98eaaa12 2019-08-03 stsp
3631 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3632 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3633 98eaaa12 2019-08-03 stsp return NULL;
3634 98eaaa12 2019-08-03 stsp
3635 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3636 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3637 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3638 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3639 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3640 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3641 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3642 43010591 2023-02-17 thomas &staged_blob_id, ie);
3643 98eaaa12 2019-08-03 stsp }
3644 98eaaa12 2019-08-03 stsp
3645 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3646 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3647 927df6b7 2019-02-10 stsp }
3648 927df6b7 2019-02-10 stsp
3649 927df6b7 2019-02-10 stsp static const struct got_error *
3650 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3651 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3652 f8d1f275 2019-02-04 stsp {
3653 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3654 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3655 f8d1f275 2019-02-04 stsp char *abspath;
3656 f8d1f275 2019-02-04 stsp
3657 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3658 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3659 eb6fb9cd 2023-07-26 thomas if (err)
3660 eb6fb9cd 2023-07-26 thomas return err;
3661 eb6fb9cd 2023-07-26 thomas }
3662 0584f854 2019-04-06 stsp
3663 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3664 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3665 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3666 927df6b7 2019-02-10 stsp return NULL;
3667 927df6b7 2019-02-10 stsp
3668 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3669 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3670 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3671 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3672 f8d1f275 2019-02-04 stsp } else {
3673 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3674 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3675 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3676 f8d1f275 2019-02-04 stsp }
3677 f8d1f275 2019-02-04 stsp
3678 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3679 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3680 f8d1f275 2019-02-04 stsp free(abspath);
3681 9d31a1d8 2018-03-11 stsp return err;
3682 9d31a1d8 2018-03-11 stsp }
3683 f8d1f275 2019-02-04 stsp
3684 f8d1f275 2019-02-04 stsp static const struct got_error *
3685 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3686 f8d1f275 2019-02-04 stsp {
3687 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
3688 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3689 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3690 2ec1f75b 2019-03-26 stsp unsigned char status;
3691 927df6b7 2019-02-10 stsp
3692 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3693 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3694 eb6fb9cd 2023-07-26 thomas if (err)
3695 eb6fb9cd 2023-07-26 thomas return err;
3696 eb6fb9cd 2023-07-26 thomas }
3697 0584f854 2019-04-06 stsp
3698 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3699 927df6b7 2019-02-10 stsp return NULL;
3700 927df6b7 2019-02-10 stsp
3701 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3702 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3703 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3704 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3705 2ec1f75b 2019-03-26 stsp else
3706 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3707 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3708 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3709 6841da00 2019-08-08 stsp }
3710 6841da00 2019-08-08 stsp
3711 ef20f542 2022-06-26 thomas static void
3712 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3713 6841da00 2019-08-08 stsp {
3714 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3715 6841da00 2019-08-08 stsp
3716 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3717 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3718 21c2d8be 2023-01-10 thomas
3719 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3720 6841da00 2019-08-08 stsp }
3721 5f42e012 2023-12-09 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3722 6841da00 2019-08-08 stsp }
3723 6841da00 2019-08-08 stsp
3724 6841da00 2019-08-08 stsp static const struct got_error *
3725 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3726 6841da00 2019-08-08 stsp {
3727 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3728 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3729 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3730 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3731 6841da00 2019-08-08 stsp size_t linesize = 0;
3732 6841da00 2019-08-08 stsp ssize_t linelen;
3733 6841da00 2019-08-08 stsp
3734 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3735 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3736 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3737 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3738 6841da00 2019-08-08 stsp
3739 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3740 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3741 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3742 bd8de430 2019-10-04 stsp
3743 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3744 bd8de430 2019-10-04 stsp if (line[0] == '#')
3745 bd8de430 2019-10-04 stsp continue;
3746 bd8de430 2019-10-04 stsp
3747 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3748 bd8de430 2019-10-04 stsp if (line[0] == '!')
3749 bd8de430 2019-10-04 stsp continue;
3750 bd8de430 2019-10-04 stsp
3751 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3752 6841da00 2019-08-08 stsp line) == -1) {
3753 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3754 6841da00 2019-08-08 stsp goto done;
3755 6841da00 2019-08-08 stsp }
3756 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3757 6841da00 2019-08-08 stsp if (err)
3758 6841da00 2019-08-08 stsp goto done;
3759 6841da00 2019-08-08 stsp }
3760 6841da00 2019-08-08 stsp if (ferror(f)) {
3761 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3762 6841da00 2019-08-08 stsp goto done;
3763 6841da00 2019-08-08 stsp }
3764 6841da00 2019-08-08 stsp
3765 6841da00 2019-08-08 stsp dirpath = strdup(path);
3766 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3767 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3768 6841da00 2019-08-08 stsp goto done;
3769 6841da00 2019-08-08 stsp }
3770 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3771 6841da00 2019-08-08 stsp done:
3772 6841da00 2019-08-08 stsp free(line);
3773 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3774 6841da00 2019-08-08 stsp free(dirpath);
3775 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3776 5f42e012 2023-12-09 thomas free(ignorelist);
3777 6841da00 2019-08-08 stsp }
3778 6841da00 2019-08-08 stsp return err;
3779 1b5d300f 2023-02-20 thomas }
3780 1b5d300f 2023-02-20 thomas
3781 1b5d300f 2023-02-20 thomas static int
3782 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3783 1b5d300f 2023-02-20 thomas int flags)
3784 1b5d300f 2023-02-20 thomas {
3785 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3786 1b5d300f 2023-02-20 thomas
3787 1b5d300f 2023-02-20 thomas /*
3788 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3789 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3790 1b5d300f 2023-02-20 thomas */
3791 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3792 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3793 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3794 1b5d300f 2023-02-20 thomas
3795 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3796 1b5d300f 2023-02-20 thomas }
3797 1b5d300f 2023-02-20 thomas
3798 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3799 6841da00 2019-08-08 stsp }
3800 6841da00 2019-08-08 stsp
3801 ef20f542 2022-06-26 thomas static int
3802 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3803 6841da00 2019-08-08 stsp {
3804 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3805 bd8de430 2019-10-04 stsp
3806 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3807 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3808 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3809 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3810 bd8de430 2019-10-04 stsp
3811 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3812 1b5d300f 2023-02-20 thomas const char *p;
3813 6841da00 2019-08-08 stsp
3814 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3815 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3816 bd8de430 2019-10-04 stsp continue;
3817 bd8de430 2019-10-04 stsp p = path;
3818 bd8de430 2019-10-04 stsp while (*p) {
3819 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3820 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3821 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3822 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3823 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3824 bd8de430 2019-10-04 stsp p++;
3825 bd8de430 2019-10-04 stsp while (*p == '/')
3826 bd8de430 2019-10-04 stsp p++;
3827 bd8de430 2019-10-04 stsp continue;
3828 bd8de430 2019-10-04 stsp }
3829 bd8de430 2019-10-04 stsp return 1;
3830 bd8de430 2019-10-04 stsp }
3831 bd8de430 2019-10-04 stsp }
3832 bd8de430 2019-10-04 stsp }
3833 bd8de430 2019-10-04 stsp
3834 6841da00 2019-08-08 stsp /*
3835 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3836 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3837 6841da00 2019-08-08 stsp * ignores backwards.
3838 6841da00 2019-08-08 stsp */
3839 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3840 6841da00 2019-08-08 stsp while (pe) {
3841 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3842 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3843 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3844 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3845 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3846 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3847 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3848 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3849 1b5d300f 2023-02-20 thomas path, flags))
3850 6841da00 2019-08-08 stsp continue;
3851 6841da00 2019-08-08 stsp return 1;
3852 6841da00 2019-08-08 stsp }
3853 6841da00 2019-08-08 stsp }
3854 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3855 6841da00 2019-08-08 stsp }
3856 6841da00 2019-08-08 stsp
3857 6841da00 2019-08-08 stsp return 0;
3858 6841da00 2019-08-08 stsp }
3859 6841da00 2019-08-08 stsp
3860 6841da00 2019-08-08 stsp static const struct got_error *
3861 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3862 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3863 6841da00 2019-08-08 stsp {
3864 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3865 6841da00 2019-08-08 stsp char *ignorespath;
3866 886cec17 2019-12-15 stsp int fd = -1;
3867 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3868 6841da00 2019-08-08 stsp
3869 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3870 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3871 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3872 6841da00 2019-08-08 stsp
3873 886cec17 2019-12-15 stsp if (dirfd != -1) {
3874 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3875 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3876 886cec17 2019-12-15 stsp if (fd == -1) {
3877 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3878 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3879 886cec17 2019-12-15 stsp ignorespath);
3880 886cec17 2019-12-15 stsp } else {
3881 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3882 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3883 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3884 886cec17 2019-12-15 stsp ignorespath);
3885 886cec17 2019-12-15 stsp else {
3886 886cec17 2019-12-15 stsp fd = -1;
3887 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3888 886cec17 2019-12-15 stsp }
3889 886cec17 2019-12-15 stsp }
3890 886cec17 2019-12-15 stsp } else {
3891 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3892 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3893 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3894 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3895 886cec17 2019-12-15 stsp ignorespath);
3896 886cec17 2019-12-15 stsp } else
3897 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3898 886cec17 2019-12-15 stsp }
3899 6841da00 2019-08-08 stsp
3900 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3901 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3902 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3903 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3904 6841da00 2019-08-08 stsp free(ignorespath);
3905 6841da00 2019-08-08 stsp return err;
3906 f8d1f275 2019-02-04 stsp }
3907 f8d1f275 2019-02-04 stsp
3908 f8d1f275 2019-02-04 stsp static const struct got_error *
3909 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3910 6092c299 2021-10-04 thomas int dirfd)
3911 f8d1f275 2019-02-04 stsp {
3912 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3913 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3914 f8d1f275 2019-02-04 stsp char *path = NULL;
3915 6092c299 2021-10-04 thomas
3916 6092c299 2021-10-04 thomas if (ignore != NULL)
3917 6092c299 2021-10-04 thomas *ignore = 0;
3918 f8d1f275 2019-02-04 stsp
3919 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
3920 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
3921 eb6fb9cd 2023-07-26 thomas if (err)
3922 eb6fb9cd 2023-07-26 thomas return err;
3923 eb6fb9cd 2023-07-26 thomas }
3924 0584f854 2019-04-06 stsp
3925 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3926 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3927 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3928 f8d1f275 2019-02-04 stsp } else {
3929 f8d1f275 2019-02-04 stsp path = de->d_name;
3930 f8d1f275 2019-02-04 stsp }
3931 f8d1f275 2019-02-04 stsp
3932 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3933 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3934 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3935 6092c299 2021-10-04 thomas *ignore = 1;
3936 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3937 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3938 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3939 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3940 f8d1f275 2019-02-04 stsp if (parent_path[0])
3941 f8d1f275 2019-02-04 stsp free(path);
3942 b72f483a 2019-02-05 stsp return err;
3943 f8d1f275 2019-02-04 stsp }
3944 f8d1f275 2019-02-04 stsp
3945 347d1d3e 2019-07-12 stsp static const struct got_error *
3946 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3947 3143d852 2020-06-25 stsp {
3948 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3949 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3950 3143d852 2020-06-25 stsp
3951 3143d852 2020-06-25 stsp if (a->no_ignores)
3952 3143d852 2020-06-25 stsp return NULL;
3953 3143d852 2020-06-25 stsp
3954 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3955 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3956 3143d852 2020-06-25 stsp if (err)
3957 3143d852 2020-06-25 stsp return err;
3958 3143d852 2020-06-25 stsp
3959 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3960 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3961 3143d852 2020-06-25 stsp
3962 3143d852 2020-06-25 stsp return err;
3963 3143d852 2020-06-25 stsp }
3964 3143d852 2020-06-25 stsp
3965 3143d852 2020-06-25 stsp static const struct got_error *
3966 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3967 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3968 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3969 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3970 abb4604f 2019-07-27 stsp {
3971 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3972 abb4604f 2019-07-27 stsp struct stat sb;
3973 ff56836b 2021-07-08 stsp
3974 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3975 abb4604f 2019-07-27 stsp if (ie)
3976 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3977 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3978 abb4604f 2019-07-27 stsp
3979 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3980 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3981 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3982 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3983 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3984 abb4604f 2019-07-27 stsp }
3985 abb4604f 2019-07-27 stsp
3986 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3987 fe1d8685 2022-02-12 thomas return NULL;
3988 fe1d8685 2022-02-12 thomas
3989 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3990 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3991 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3992 abb4604f 2019-07-27 stsp
3993 abb4604f 2019-07-27 stsp return NULL;
3994 3143d852 2020-06-25 stsp }
3995 3143d852 2020-06-25 stsp
3996 3143d852 2020-06-25 stsp static const struct got_error *
3997 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3998 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3999 3143d852 2020-06-25 stsp {
4000 3143d852 2020-06-25 stsp const struct got_error *err;
4001 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
4002 3143d852 2020-06-25 stsp
4003 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4004 3143d852 2020-06-25 stsp ".cvsignore");
4005 3143d852 2020-06-25 stsp if (err)
4006 3143d852 2020-06-25 stsp return err;
4007 3143d852 2020-06-25 stsp
4008 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4009 3143d852 2020-06-25 stsp ".gitignore");
4010 3143d852 2020-06-25 stsp if (err)
4011 3143d852 2020-06-25 stsp return err;
4012 3143d852 2020-06-25 stsp
4013 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
4014 3143d852 2020-06-25 stsp if (err) {
4015 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
4016 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
4017 3143d852 2020-06-25 stsp return err;
4018 3143d852 2020-06-25 stsp }
4019 3143d852 2020-06-25 stsp for (;;) {
4020 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4021 3143d852 2020-06-25 stsp ".cvsignore");
4022 3143d852 2020-06-25 stsp if (err)
4023 3143d852 2020-06-25 stsp break;
4024 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4025 3143d852 2020-06-25 stsp ".gitignore");
4026 3143d852 2020-06-25 stsp if (err)
4027 3143d852 2020-06-25 stsp break;
4028 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
4029 3143d852 2020-06-25 stsp if (err) {
4030 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
4031 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
4032 3143d852 2020-06-25 stsp break;
4033 3143d852 2020-06-25 stsp }
4034 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
4035 ff56836b 2021-07-08 stsp break;
4036 b737c85a 2020-06-26 stsp free(parent_path);
4037 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
4038 b737c85a 2020-06-26 stsp next_parent_path = NULL;
4039 3143d852 2020-06-25 stsp }
4040 3143d852 2020-06-25 stsp
4041 b737c85a 2020-06-26 stsp free(parent_path);
4042 b737c85a 2020-06-26 stsp free(next_parent_path);
4043 3143d852 2020-06-25 stsp return err;
4044 abb4604f 2019-07-27 stsp }
4045 f8836425 2023-04-14 thomas
4046 f8836425 2023-04-14 thomas struct find_missing_children_args {
4047 f8836425 2023-04-14 thomas const char *parent_path;
4048 f8836425 2023-04-14 thomas size_t parent_len;
4049 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
4050 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
4051 f8836425 2023-04-14 thomas void *cancel_arg;
4052 f8836425 2023-04-14 thomas };
4053 f8836425 2023-04-14 thomas
4054 abb4604f 2019-07-27 stsp static const struct got_error *
4055 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
4056 f8836425 2023-04-14 thomas {
4057 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4058 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
4059 f8836425 2023-04-14 thomas
4060 f8836425 2023-04-14 thomas if (a->cancel_cb) {
4061 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
4062 f8836425 2023-04-14 thomas if (err)
4063 f8836425 2023-04-14 thomas return err;
4064 f8836425 2023-04-14 thomas }
4065 f8836425 2023-04-14 thomas
4066 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4067 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
4068 f8836425 2023-04-14 thomas
4069 f8836425 2023-04-14 thomas return err;
4070 f8836425 2023-04-14 thomas }
4071 f8836425 2023-04-14 thomas
4072 f8836425 2023-04-14 thomas static const struct got_error *
4073 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
4074 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
4075 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
4076 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
4077 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
4078 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
4079 f8836425 2023-04-14 thomas {
4080 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
4081 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
4082 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
4083 f8836425 2023-04-14 thomas
4084 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
4085 f8836425 2023-04-14 thomas if (cancel_cb) {
4086 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
4087 f8836425 2023-04-14 thomas if (err)
4088 f8836425 2023-04-14 thomas break;
4089 f8836425 2023-04-14 thomas }
4090 f8836425 2023-04-14 thomas
4091 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4092 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
4093 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
4094 f8836425 2023-04-14 thomas ondisk_path = NULL;
4095 f8836425 2023-04-14 thomas break;
4096 f8836425 2023-04-14 thomas }
4097 f8836425 2023-04-14 thomas
4098 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
4099 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
4100 f8836425 2023-04-14 thomas ignores, no_ignores);
4101 f8836425 2023-04-14 thomas if (err)
4102 f8836425 2023-04-14 thomas break;
4103 f8836425 2023-04-14 thomas
4104 f8836425 2023-04-14 thomas free(ondisk_path);
4105 f8836425 2023-04-14 thomas ondisk_path = NULL;
4106 f8836425 2023-04-14 thomas }
4107 f8836425 2023-04-14 thomas
4108 f8836425 2023-04-14 thomas free(ondisk_path);
4109 f8836425 2023-04-14 thomas return err;
4110 f8836425 2023-04-14 thomas }
4111 f8836425 2023-04-14 thomas
4112 f8836425 2023-04-14 thomas static const struct got_error *
4113 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4114 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4115 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4116 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4117 f2a9dc41 2019-12-13 tracey int report_unchanged)
4118 f8d1f275 2019-02-04 stsp {
4119 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4120 6fc93f37 2019-12-13 stsp int fd = -1;
4121 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4122 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4123 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4124 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
4125 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
4126 3143d852 2020-06-25 stsp
4127 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4128 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
4129 f8d1f275 2019-02-04 stsp
4130 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4131 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4132 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4133 8dc303cc 2019-07-27 stsp
4134 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4135 a78810f8 2022-03-13 thomas if (ie) {
4136 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
4137 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
4138 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
4139 a78810f8 2022-03-13 thomas goto done;
4140 f8836425 2023-04-14 thomas } else {
4141 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
4142 f8836425 2023-04-14 thomas fmca.parent_path = path;
4143 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
4144 f8836425 2023-04-14 thomas fmca.children = &missing_children;
4145 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
4146 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
4147 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
4148 f8836425 2023-04-14 thomas find_missing_children, &fmca);
4149 f8836425 2023-04-14 thomas if (err)
4150 f8836425 2023-04-14 thomas goto done;
4151 a78810f8 2022-03-13 thomas }
4152 a78810f8 2022-03-13 thomas
4153 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4154 6fc93f37 2019-12-13 stsp if (fd == -1) {
4155 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4156 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
4157 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4158 ff56836b 2021-07-08 stsp else {
4159 ff56836b 2021-07-08 stsp if (!no_ignores) {
4160 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4161 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4162 ff56836b 2021-07-08 stsp if (err)
4163 ff56836b 2021-07-08 stsp goto done;
4164 ff56836b 2021-07-08 stsp }
4165 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4166 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4167 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4168 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4169 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4170 f8836425 2023-04-14 thomas if (err)
4171 f8836425 2023-04-14 thomas goto done;
4172 f8836425 2023-04-14 thomas } else {
4173 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4174 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4175 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4176 f8836425 2023-04-14 thomas &ignores, no_ignores,
4177 f8836425 2023-04-14 thomas status_cb, status_arg,
4178 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4179 6f97aa83 2023-04-14 thomas if (err)
4180 6f97aa83 2023-04-14 thomas goto done;
4181 f8836425 2023-04-14 thomas }
4182 ff56836b 2021-07-08 stsp }
4183 abb4604f 2019-07-27 stsp } else {
4184 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4185 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4186 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4187 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4188 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4189 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4190 abb4604f 2019-07-27 stsp arg.status_path = path;
4191 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4192 abb4604f 2019-07-27 stsp arg.repo = repo;
4193 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4194 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4195 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4196 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4197 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4198 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4199 022fae89 2019-12-06 tracey if (!no_ignores) {
4200 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4201 3143d852 2020-06-25 stsp worktree->root_path, path);
4202 3143d852 2020-06-25 stsp if (err)
4203 3143d852 2020-06-25 stsp goto done;
4204 022fae89 2019-12-06 tracey }
4205 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4206 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4207 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4208 927df6b7 2019-02-10 stsp }
4209 3143d852 2020-06-25 stsp done:
4210 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4211 b2dcbbc7 2023-12-05 thomas got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4212 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4213 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4214 927df6b7 2019-02-10 stsp free(ondisk_path);
4215 347d1d3e 2019-07-12 stsp return err;
4216 347d1d3e 2019-07-12 stsp }
4217 347d1d3e 2019-07-12 stsp
4218 347d1d3e 2019-07-12 stsp const struct got_error *
4219 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4220 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4221 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4222 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4223 347d1d3e 2019-07-12 stsp {
4224 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4225 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4226 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4227 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4228 347d1d3e 2019-07-12 stsp
4229 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4230 347d1d3e 2019-07-12 stsp if (err)
4231 347d1d3e 2019-07-12 stsp return err;
4232 347d1d3e 2019-07-12 stsp
4233 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4234 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4235 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4236 f6343036 2021-06-22 stsp no_ignores, 0);
4237 72ea6654 2019-07-27 stsp if (err)
4238 72ea6654 2019-07-27 stsp break;
4239 72ea6654 2019-07-27 stsp }
4240 f8d1f275 2019-02-04 stsp free(fileindex_path);
4241 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4242 f8d1f275 2019-02-04 stsp return err;
4243 f8d1f275 2019-02-04 stsp }
4244 6c7ab921 2019-03-18 stsp
4245 6c7ab921 2019-03-18 stsp const struct got_error *
4246 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4247 6c7ab921 2019-03-18 stsp const char *arg)
4248 6c7ab921 2019-03-18 stsp {
4249 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4250 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4251 6c7ab921 2019-03-18 stsp size_t len;
4252 00bb5ea0 2020-07-23 stsp struct stat sb;
4253 01740607 2020-11-04 stsp char *abspath = NULL;
4254 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4255 6c7ab921 2019-03-18 stsp
4256 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4257 6c7ab921 2019-03-18 stsp
4258 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4259 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4260 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4261 00bb5ea0 2020-07-23 stsp
4262 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4263 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4264 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4265 00bb5ea0 2020-07-23 stsp goto done;
4266 00bb5ea0 2020-07-23 stsp }
4267 727173c3 2020-11-06 stsp sb.st_mode = 0;
4268 00bb5ea0 2020-07-23 stsp }
4269 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4270 00bb5ea0 2020-07-23 stsp /*
4271 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4272 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4273 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4274 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4275 00bb5ea0 2020-07-23 stsp */
4276 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4277 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4278 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4279 00bb5ea0 2020-07-23 stsp goto done;
4280 00bb5ea0 2020-07-23 stsp }
4281 00bb5ea0 2020-07-23 stsp
4282 00bb5ea0 2020-07-23 stsp }
4283 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4284 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4285 00bb5ea0 2020-07-23 stsp if (err)
4286 d0710d08 2019-07-22 stsp goto done;
4287 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4288 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4289 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4290 00bb5ea0 2020-07-23 stsp goto done;
4291 00bb5ea0 2020-07-23 stsp }
4292 00bb5ea0 2020-07-23 stsp } else {
4293 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4294 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4295 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4296 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4297 00bb5ea0 2020-07-23 stsp goto done;
4298 00bb5ea0 2020-07-23 stsp }
4299 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4300 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4301 01740607 2020-11-04 stsp goto done;
4302 01740607 2020-11-04 stsp }
4303 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4304 01740607 2020-11-04 stsp sizeof(canonpath));
4305 01740607 2020-11-04 stsp if (err)
4306 00bb5ea0 2020-07-23 stsp goto done;
4307 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4308 01740607 2020-11-04 stsp if (resolved == NULL) {
4309 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4310 01740607 2020-11-04 stsp goto done;
4311 00bb5ea0 2020-07-23 stsp }
4312 d0710d08 2019-07-22 stsp }
4313 d0710d08 2019-07-22 stsp }
4314 6c7ab921 2019-03-18 stsp
4315 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4316 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4317 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4318 6c7ab921 2019-03-18 stsp goto done;
4319 6c7ab921 2019-03-18 stsp }
4320 6c7ab921 2019-03-18 stsp
4321 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4322 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4323 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4324 f6d88e1a 2019-05-29 stsp if (err)
4325 f6d88e1a 2019-05-29 stsp goto done;
4326 f6d88e1a 2019-05-29 stsp } else {
4327 f6d88e1a 2019-05-29 stsp path = strdup("");
4328 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4329 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4330 f6d88e1a 2019-05-29 stsp goto done;
4331 f6d88e1a 2019-05-29 stsp }
4332 6c7ab921 2019-03-18 stsp }
4333 6c7ab921 2019-03-18 stsp
4334 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4335 6c7ab921 2019-03-18 stsp len = strlen(path);
4336 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4337 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4338 6c7ab921 2019-03-18 stsp len--;
4339 6c7ab921 2019-03-18 stsp }
4340 6c7ab921 2019-03-18 stsp done:
4341 01740607 2020-11-04 stsp free(abspath);
4342 6c7ab921 2019-03-18 stsp free(resolved);
4343 d0710d08 2019-07-22 stsp free(cwd);
4344 6c7ab921 2019-03-18 stsp if (err == NULL)
4345 6c7ab921 2019-03-18 stsp *wt_path = path;
4346 6c7ab921 2019-03-18 stsp else
4347 6c7ab921 2019-03-18 stsp free(path);
4348 d00136be 2019-03-26 stsp return err;
4349 d00136be 2019-03-26 stsp }
4350 d00136be 2019-03-26 stsp
4351 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4352 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4353 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4354 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4355 4e68cba3 2019-11-23 stsp void *progress_arg;
4356 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4357 4e68cba3 2019-11-23 stsp };
4358 4e68cba3 2019-11-23 stsp
4359 b21ebdb0 2023-06-22 thomas static int
4360 b21ebdb0 2023-06-22 thomas add_noop_status(unsigned char status)
4361 b21ebdb0 2023-06-22 thomas {
4362 b21ebdb0 2023-06-22 thomas return (status == GOT_STATUS_ADD ||
4363 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODIFY ||
4364 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_CONFLICT ||
4365 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_MODE_CHANGE ||
4366 b21ebdb0 2023-06-22 thomas status == GOT_STATUS_NO_CHANGE);
4367 b21ebdb0 2023-06-22 thomas }
4368 b21ebdb0 2023-06-22 thomas
4369 4e68cba3 2019-11-23 stsp static const struct got_error *
4370 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4371 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4372 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4373 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4374 07f5b47a 2019-06-02 stsp {
4375 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4376 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4377 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4378 6d022e97 2019-08-04 stsp struct stat sb;
4379 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4380 07f5b47a 2019-06-02 stsp
4381 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4382 dbb83fbd 2019-12-12 stsp relpath) == -1)
4383 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4384 dbb83fbd 2019-12-12 stsp
4385 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4386 6d022e97 2019-08-04 stsp if (ie) {
4387 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4388 12463d8b 2019-12-13 stsp de_name, a->repo);
4389 6d022e97 2019-08-04 stsp if (err)
4390 dbb83fbd 2019-12-12 stsp goto done;
4391 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4392 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE &&
4393 b21ebdb0 2023-06-22 thomas add_noop_status(status))
4394 dbb83fbd 2019-12-12 stsp goto done;
4395 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4396 dbb83fbd 2019-12-12 stsp if (err)
4397 dbb83fbd 2019-12-12 stsp goto done;
4398 6d022e97 2019-08-04 stsp }
4399 07f5b47a 2019-06-02 stsp
4400 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4401 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4402 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4403 84bf00a6 2022-02-12 thomas else
4404 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4405 dbb83fbd 2019-12-12 stsp goto done;
4406 4e68cba3 2019-11-23 stsp }
4407 07f5b47a 2019-06-02 stsp
4408 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4409 4e68cba3 2019-11-23 stsp if (err)
4410 4e68cba3 2019-11-23 stsp goto done;
4411 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4412 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4413 3969253a 2020-03-07 stsp if (err) {
4414 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4415 3969253a 2020-03-07 stsp goto done;
4416 3969253a 2020-03-07 stsp }
4417 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4418 07f5b47a 2019-06-02 stsp if (err) {
4419 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4420 4e68cba3 2019-11-23 stsp goto done;
4421 07f5b47a 2019-06-02 stsp }
4422 4e68cba3 2019-11-23 stsp done:
4423 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4424 dbb83fbd 2019-12-12 stsp if (err)
4425 dbb83fbd 2019-12-12 stsp return err;
4426 b21ebdb0 2023-06-22 thomas if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4427 dbb83fbd 2019-12-12 stsp return NULL;
4428 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4429 07f5b47a 2019-06-02 stsp }
4430 07f5b47a 2019-06-02 stsp
4431 d00136be 2019-03-26 stsp const struct got_error *
4432 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4433 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4434 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4435 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4436 d00136be 2019-03-26 stsp {
4437 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4438 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4439 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4440 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4441 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4442 d00136be 2019-03-26 stsp
4443 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4444 d00136be 2019-03-26 stsp if (err)
4445 d00136be 2019-03-26 stsp return err;
4446 d00136be 2019-03-26 stsp
4447 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4448 d00136be 2019-03-26 stsp if (err)
4449 d00136be 2019-03-26 stsp goto done;
4450 d00136be 2019-03-26 stsp
4451 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4452 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4453 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4454 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4455 4e68cba3 2019-11-23 stsp saa.repo = repo;
4456 4e68cba3 2019-11-23 stsp
4457 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4458 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4459 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4460 1dd54920 2019-05-11 stsp if (err)
4461 af12c6b9 2019-06-04 stsp break;
4462 1dd54920 2019-05-11 stsp }
4463 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4464 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4465 af12c6b9 2019-06-04 stsp err = sync_err;
4466 d00136be 2019-03-26 stsp done:
4467 fb399478 2019-07-12 stsp free(fileindex_path);
4468 d00136be 2019-03-26 stsp if (fileindex)
4469 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4470 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4471 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4472 d00136be 2019-03-26 stsp err = unlockerr;
4473 6c7ab921 2019-03-18 stsp return err;
4474 6c7ab921 2019-03-18 stsp }
4475 17ed4618 2019-06-02 stsp
4476 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4477 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4478 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4479 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4480 f2a9dc41 2019-12-13 tracey void *progress_arg;
4481 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4482 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4483 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4484 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4485 8ec7352a 2023-06-01 thomas const char *status_path;
4486 8ec7352a 2023-06-01 thomas size_t status_path_len;
4487 766841c2 2020-08-13 stsp const char *status_codes;
4488 f2a9dc41 2019-12-13 tracey };
4489 f2a9dc41 2019-12-13 tracey
4490 f2a9dc41 2019-12-13 tracey static const struct got_error *
4491 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4492 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4493 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4494 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4495 17ed4618 2019-06-02 stsp {
4496 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4497 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4498 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4499 17ed4618 2019-06-02 stsp struct stat sb;
4500 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4501 d64cc78a 2022-01-25 thomas
4502 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4503 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4504 d64cc78a 2022-01-25 thomas return NULL;
4505 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4506 d64cc78a 2022-01-25 thomas }
4507 17ed4618 2019-06-02 stsp
4508 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4509 17ed4618 2019-06-02 stsp if (ie == NULL)
4510 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4511 2ec1f75b 2019-03-26 stsp
4512 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4513 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4514 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4515 9acbc4fa 2019-08-03 stsp return NULL;
4516 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4517 9acbc4fa 2019-08-03 stsp }
4518 9acbc4fa 2019-08-03 stsp
4519 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4520 f2a9dc41 2019-12-13 tracey relpath) == -1)
4521 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4522 f2a9dc41 2019-12-13 tracey
4523 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4524 12463d8b 2019-12-13 stsp a->repo);
4525 17ed4618 2019-06-02 stsp if (err)
4526 f2a9dc41 2019-12-13 tracey goto done;
4527 17ed4618 2019-06-02 stsp
4528 766841c2 2020-08-13 stsp if (a->status_codes) {
4529 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4530 766841c2 2020-08-13 stsp int i;
4531 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4532 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4533 766841c2 2020-08-13 stsp break;
4534 766841c2 2020-08-13 stsp }
4535 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4536 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4537 766841c2 2020-08-13 stsp free(ondisk_path);
4538 766841c2 2020-08-13 stsp return NULL;
4539 766841c2 2020-08-13 stsp }
4540 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4541 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4542 766841c2 2020-08-13 stsp static char msg[64];
4543 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4544 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4545 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4546 766841c2 2020-08-13 stsp goto done;
4547 766841c2 2020-08-13 stsp }
4548 766841c2 2020-08-13 stsp }
4549 766841c2 2020-08-13 stsp
4550 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4551 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4552 f2a9dc41 2019-12-13 tracey goto done;
4553 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4554 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4555 f2a9dc41 2019-12-13 tracey goto done;
4556 f2a9dc41 2019-12-13 tracey }
4557 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4558 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4559 d64cc78a 2022-01-25 thomas goto done;
4560 d64cc78a 2022-01-25 thomas }
4561 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4562 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4563 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4564 f2a9dc41 2019-12-13 tracey goto done;
4565 f2a9dc41 2019-12-13 tracey }
4566 17ed4618 2019-06-02 stsp }
4567 17ed4618 2019-06-02 stsp
4568 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4569 20a2ad1c 2020-10-20 stsp size_t root_len;
4570 20a2ad1c 2020-10-20 stsp
4571 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4572 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4573 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4574 12463d8b 2019-12-13 stsp ondisk_path);
4575 12463d8b 2019-12-13 stsp goto done;
4576 12463d8b 2019-12-13 stsp }
4577 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4578 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4579 12463d8b 2019-12-13 stsp goto done;
4580 15341bfd 2020-03-05 tracey }
4581 15341bfd 2020-03-05 tracey
4582 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4583 20a2ad1c 2020-10-20 stsp do {
4584 20a2ad1c 2020-10-20 stsp char *parent;
4585 8ec7352a 2023-06-01 thomas
4586 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4587 20a2ad1c 2020-10-20 stsp if (err)
4588 2513f20a 2020-10-20 stsp goto done;
4589 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4590 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4591 8ec7352a 2023-06-01 thomas if (got_path_cmp(ondisk_path, a->status_path,
4592 8ec7352a 2023-06-01 thomas strlen(ondisk_path), a->status_path_len) != 0 &&
4593 8ec7352a 2023-06-01 thomas !got_path_is_child(ondisk_path, a->status_path,
4594 8ec7352a 2023-06-01 thomas a->status_path_len))
4595 8ec7352a 2023-06-01 thomas break;
4596 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4597 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4598 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4599 20a2ad1c 2020-10-20 stsp ondisk_path);
4600 15341bfd 2020-03-05 tracey break;
4601 15341bfd 2020-03-05 tracey }
4602 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4603 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4604 f2a9dc41 2019-12-13 tracey }
4605 17ed4618 2019-06-02 stsp
4606 97f9158f 2023-08-29 thomas if (got_fileindex_entry_has_blob(ie))
4607 97f9158f 2023-08-29 thomas got_fileindex_entry_mark_deleted_from_disk(ie);
4608 97f9158f 2023-08-29 thomas else
4609 97f9158f 2023-08-29 thomas got_fileindex_entry_remove(a->fileindex, ie);
4610 f2a9dc41 2019-12-13 tracey done:
4611 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4612 f2a9dc41 2019-12-13 tracey if (err)
4613 f2a9dc41 2019-12-13 tracey return err;
4614 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4615 f2a9dc41 2019-12-13 tracey return NULL;
4616 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4617 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4618 17ed4618 2019-06-02 stsp }
4619 17ed4618 2019-06-02 stsp
4620 2ec1f75b 2019-03-26 stsp const struct got_error *
4621 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4622 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4623 766841c2 2020-08-13 stsp const char *status_codes,
4624 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4625 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4626 2ec1f75b 2019-03-26 stsp {
4627 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4628 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4629 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4630 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4631 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4632 2ec1f75b 2019-03-26 stsp
4633 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4634 2ec1f75b 2019-03-26 stsp if (err)
4635 2ec1f75b 2019-03-26 stsp return err;
4636 2ec1f75b 2019-03-26 stsp
4637 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4638 2ec1f75b 2019-03-26 stsp if (err)
4639 2ec1f75b 2019-03-26 stsp goto done;
4640 f2a9dc41 2019-12-13 tracey
4641 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4642 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4643 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4644 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4645 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4646 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4647 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4648 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4649 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4650 2ec1f75b 2019-03-26 stsp
4651 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4652 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
4653 8ec7352a 2023-06-01 thomas
4654 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s%s%s",
4655 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree),
4656 8ec7352a 2023-06-01 thomas pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4657 8ec7352a 2023-06-01 thomas err = got_error_from_errno("asprintf");
4658 8ec7352a 2023-06-01 thomas goto done;
4659 8ec7352a 2023-06-01 thomas }
4660 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
4661 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
4662 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4663 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4664 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
4665 17ed4618 2019-06-02 stsp if (err)
4666 af12c6b9 2019-06-04 stsp break;
4667 2ec1f75b 2019-03-26 stsp }
4668 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4669 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4670 af12c6b9 2019-06-04 stsp err = sync_err;
4671 2ec1f75b 2019-03-26 stsp done:
4672 fb399478 2019-07-12 stsp free(fileindex_path);
4673 2ec1f75b 2019-03-26 stsp if (fileindex)
4674 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4675 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4676 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4677 2ec1f75b 2019-03-26 stsp err = unlockerr;
4678 33aa809d 2019-08-08 stsp return err;
4679 33aa809d 2019-08-08 stsp }
4680 33aa809d 2019-08-08 stsp
4681 33aa809d 2019-08-08 stsp static const struct got_error *
4682 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4683 33aa809d 2019-08-08 stsp {
4684 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4685 33aa809d 2019-08-08 stsp char *line = NULL;
4686 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4687 33aa809d 2019-08-08 stsp ssize_t linelen;
4688 33aa809d 2019-08-08 stsp
4689 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4690 33aa809d 2019-08-08 stsp if (linelen == -1) {
4691 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4692 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4693 33aa809d 2019-08-08 stsp goto done;
4694 33aa809d 2019-08-08 stsp }
4695 33aa809d 2019-08-08 stsp return NULL;
4696 33aa809d 2019-08-08 stsp }
4697 33aa809d 2019-08-08 stsp if (outfile) {
4698 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4699 33aa809d 2019-08-08 stsp if (n != linelen) {
4700 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4701 33aa809d 2019-08-08 stsp goto done;
4702 33aa809d 2019-08-08 stsp }
4703 33aa809d 2019-08-08 stsp }
4704 33aa809d 2019-08-08 stsp if (rejectfile) {
4705 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4706 33aa809d 2019-08-08 stsp if (n != linelen)
4707 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4708 33aa809d 2019-08-08 stsp }
4709 33aa809d 2019-08-08 stsp done:
4710 33aa809d 2019-08-08 stsp free(line);
4711 2ec1f75b 2019-03-26 stsp return err;
4712 2ec1f75b 2019-03-26 stsp }
4713 1f1abb7e 2019-08-08 stsp
4714 33aa809d 2019-08-08 stsp static const struct got_error *
4715 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4716 33aa809d 2019-08-08 stsp {
4717 33aa809d 2019-08-08 stsp char *line = NULL;
4718 33aa809d 2019-08-08 stsp size_t linesize = 0;
4719 33aa809d 2019-08-08 stsp ssize_t linelen;
4720 33aa809d 2019-08-08 stsp
4721 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4722 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4723 500467ff 2019-09-25 hiltjo if (ferror(f))
4724 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4725 500467ff 2019-09-25 hiltjo return NULL;
4726 500467ff 2019-09-25 hiltjo }
4727 33aa809d 2019-08-08 stsp free(line);
4728 33aa809d 2019-08-08 stsp return NULL;
4729 33aa809d 2019-08-08 stsp }
4730 33aa809d 2019-08-08 stsp
4731 33aa809d 2019-08-08 stsp static const struct got_error *
4732 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4733 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4734 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4735 abc59930 2021-09-05 naddy {
4736 33aa809d 2019-08-08 stsp const struct got_error *err;
4737 33aa809d 2019-08-08 stsp
4738 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4739 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4740 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4741 33aa809d 2019-08-08 stsp if (err)
4742 33aa809d 2019-08-08 stsp return err;
4743 33aa809d 2019-08-08 stsp (*line_cur1)++;
4744 33aa809d 2019-08-08 stsp }
4745 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4746 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4747 33aa809d 2019-08-08 stsp if (rejectfile)
4748 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4749 33aa809d 2019-08-08 stsp else
4750 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4751 33aa809d 2019-08-08 stsp if (err)
4752 33aa809d 2019-08-08 stsp return err;
4753 33aa809d 2019-08-08 stsp (*line_cur2)++;
4754 33aa809d 2019-08-08 stsp }
4755 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4756 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4757 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4758 33aa809d 2019-08-08 stsp if (err)
4759 33aa809d 2019-08-08 stsp return err;
4760 33aa809d 2019-08-08 stsp (*line_cur2)++;
4761 33aa809d 2019-08-08 stsp }
4762 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4763 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4764 33aa809d 2019-08-08 stsp if (rejectfile)
4765 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4766 33aa809d 2019-08-08 stsp else
4767 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4768 33aa809d 2019-08-08 stsp if (err)
4769 33aa809d 2019-08-08 stsp return err;
4770 33aa809d 2019-08-08 stsp (*line_cur1)++;
4771 33aa809d 2019-08-08 stsp }
4772 f1e81a05 2019-08-10 stsp
4773 f1e81a05 2019-08-10 stsp return NULL;
4774 f1e81a05 2019-08-10 stsp }
4775 f1e81a05 2019-08-10 stsp
4776 f1e81a05 2019-08-10 stsp static const struct got_error *
4777 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4778 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4779 f1e81a05 2019-08-10 stsp {
4780 f1e81a05 2019-08-10 stsp const struct got_error *err;
4781 f1e81a05 2019-08-10 stsp
4782 f1e81a05 2019-08-10 stsp if (outfile) {
4783 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4784 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4785 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4786 f1e81a05 2019-08-10 stsp if (err)
4787 f1e81a05 2019-08-10 stsp return err;
4788 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4789 f1e81a05 2019-08-10 stsp }
4790 f1e81a05 2019-08-10 stsp }
4791 f1e81a05 2019-08-10 stsp if (rejectfile) {
4792 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4793 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4794 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4795 f1e81a05 2019-08-10 stsp if (err)
4796 f1e81a05 2019-08-10 stsp return err;
4797 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4798 f1e81a05 2019-08-10 stsp }
4799 33aa809d 2019-08-08 stsp }
4800 33aa809d 2019-08-08 stsp
4801 33aa809d 2019-08-08 stsp return NULL;
4802 33aa809d 2019-08-08 stsp }
4803 33aa809d 2019-08-08 stsp
4804 33aa809d 2019-08-08 stsp static const struct got_error *
4805 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4806 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4807 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4808 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4809 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4810 33aa809d 2019-08-08 stsp {
4811 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4812 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4813 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4814 33aa809d 2019-08-08 stsp FILE *hunkfile;
4815 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4816 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4817 fe621944 2020-11-10 stsp int rc;
4818 33aa809d 2019-08-08 stsp
4819 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4820 33aa809d 2019-08-08 stsp
4821 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4822 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4823 fe621944 2020-11-10 stsp start_old = cc.left.start;
4824 fe621944 2020-11-10 stsp end_old = cc.left.end;
4825 fe621944 2020-11-10 stsp start_new = cc.right.start;
4826 fe621944 2020-11-10 stsp end_new = cc.right.end;
4827 33aa809d 2019-08-08 stsp
4828 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4829 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4830 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4831 33aa809d 2019-08-08 stsp
4832 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4833 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4834 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4835 33aa809d 2019-08-08 stsp
4836 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4837 fe621944 2020-11-10 stsp if (diff_state == NULL)
4838 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4839 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4840 fe621944 2020-11-10 stsp
4841 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4842 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4843 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4844 33aa809d 2019-08-08 stsp goto done;
4845 33aa809d 2019-08-08 stsp }
4846 fe621944 2020-11-10 stsp
4847 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4848 fe621944 2020-11-10 stsp diff_result, &cc);
4849 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4850 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4851 33aa809d 2019-08-08 stsp goto done;
4852 33aa809d 2019-08-08 stsp }
4853 fe621944 2020-11-10 stsp
4854 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4855 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4856 33aa809d 2019-08-08 stsp goto done;
4857 33aa809d 2019-08-08 stsp }
4858 33aa809d 2019-08-08 stsp
4859 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4860 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4861 33aa809d 2019-08-08 stsp if (err)
4862 33aa809d 2019-08-08 stsp goto done;
4863 33aa809d 2019-08-08 stsp
4864 33aa809d 2019-08-08 stsp switch (*choice) {
4865 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4866 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4867 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4868 33aa809d 2019-08-08 stsp break;
4869 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4870 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4871 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4872 33aa809d 2019-08-08 stsp break;
4873 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4874 33aa809d 2019-08-08 stsp break;
4875 33aa809d 2019-08-08 stsp default:
4876 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4877 33aa809d 2019-08-08 stsp break;
4878 33aa809d 2019-08-08 stsp }
4879 33aa809d 2019-08-08 stsp done:
4880 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4881 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4882 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4883 33aa809d 2019-08-08 stsp return err;
4884 33aa809d 2019-08-08 stsp }
4885 33aa809d 2019-08-08 stsp
4886 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4887 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4888 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4889 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4890 1f1abb7e 2019-08-08 stsp void *progress_arg;
4891 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4892 33aa809d 2019-08-08 stsp void *patch_arg;
4893 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4894 10604dce 2021-09-24 thomas int unlink_added_files;
4895 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4896 1f1abb7e 2019-08-08 stsp };
4897 a129376b 2019-03-28 stsp
4898 e20a8b6f 2019-06-04 stsp static const struct got_error *
4899 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4900 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4901 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4902 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4903 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4904 33aa809d 2019-08-08 stsp {
4905 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4906 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4907 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4908 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4909 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4910 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4911 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4912 fe621944 2020-11-10 stsp struct stat sb2;
4913 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4914 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4915 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4916 33aa809d 2019-08-08 stsp
4917 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4918 33aa809d 2019-08-08 stsp
4919 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4920 33aa809d 2019-08-08 stsp if (err)
4921 33aa809d 2019-08-08 stsp return err;
4922 33aa809d 2019-08-08 stsp
4923 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4924 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4925 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4926 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4927 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4928 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4929 fa3cef63 2020-07-23 stsp goto done;
4930 fa3cef63 2020-07-23 stsp }
4931 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4932 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4933 48b4f239 2021-12-31 thomas if (link_len == -1) {
4934 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4935 48b4f239 2021-12-31 thomas path2);
4936 48b4f239 2021-12-31 thomas }
4937 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4938 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4939 12463d8b 2019-12-13 stsp }
4940 12463d8b 2019-12-13 stsp } else {
4941 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4942 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4943 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4944 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4945 fa3cef63 2020-07-23 stsp goto done;
4946 fa3cef63 2020-07-23 stsp }
4947 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4948 fa3cef63 2020-07-23 stsp sizeof(link_target));
4949 fa3cef63 2020-07-23 stsp if (link_len == -1)
4950 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4951 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4952 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4953 12463d8b 2019-12-13 stsp }
4954 1ebedb77 2019-10-19 stsp }
4955 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4956 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4957 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4958 fa3cef63 2020-07-23 stsp goto done;
4959 fa3cef63 2020-07-23 stsp }
4960 1ebedb77 2019-10-19 stsp
4961 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4962 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4963 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4964 fa3cef63 2020-07-23 stsp goto done;
4965 fa3cef63 2020-07-23 stsp }
4966 fa3cef63 2020-07-23 stsp fd2 = -1;
4967 fa3cef63 2020-07-23 stsp } else {
4968 fa3cef63 2020-07-23 stsp size_t n;
4969 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4970 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4971 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4972 fa3cef63 2020-07-23 stsp goto done;
4973 fa3cef63 2020-07-23 stsp }
4974 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4975 fa3cef63 2020-07-23 stsp if (n != link_len) {
4976 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4977 fa3cef63 2020-07-23 stsp goto done;
4978 fa3cef63 2020-07-23 stsp }
4979 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4980 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4981 fa3cef63 2020-07-23 stsp goto done;
4982 fa3cef63 2020-07-23 stsp }
4983 fa3cef63 2020-07-23 stsp rewind(f2);
4984 33aa809d 2019-08-08 stsp }
4985 33aa809d 2019-08-08 stsp
4986 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4987 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4988 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4989 f4ae6ddb 2022-07-01 thomas goto done;
4990 f4ae6ddb 2022-07-01 thomas }
4991 f4ae6ddb 2022-07-01 thomas
4992 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4993 33aa809d 2019-08-08 stsp if (err)
4994 33aa809d 2019-08-08 stsp goto done;
4995 33aa809d 2019-08-08 stsp
4996 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4997 33aa809d 2019-08-08 stsp if (err)
4998 33aa809d 2019-08-08 stsp goto done;
4999 33aa809d 2019-08-08 stsp
5000 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5001 33aa809d 2019-08-08 stsp if (err)
5002 33aa809d 2019-08-08 stsp goto done;
5003 33aa809d 2019-08-08 stsp
5004 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5005 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5006 33aa809d 2019-08-08 stsp if (err)
5007 33aa809d 2019-08-08 stsp goto done;
5008 33aa809d 2019-08-08 stsp
5009 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5010 fc2a50f2 2022-11-01 thomas "");
5011 33aa809d 2019-08-08 stsp if (err)
5012 33aa809d 2019-08-08 stsp goto done;
5013 33aa809d 2019-08-08 stsp
5014 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5015 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
5016 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5017 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
5018 fe621944 2020-11-10 stsp
5019 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
5020 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5021 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
5022 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
5023 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
5024 fe621944 2020-11-10 stsp nchanges++;
5025 fe621944 2020-11-10 stsp }
5026 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5027 33aa809d 2019-08-08 stsp int choice;
5028 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
5029 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
5030 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
5031 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
5032 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
5033 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
5034 33aa809d 2019-08-08 stsp if (err)
5035 33aa809d 2019-08-08 stsp goto done;
5036 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5037 33aa809d 2019-08-08 stsp have_content = 1;
5038 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5039 33aa809d 2019-08-08 stsp break;
5040 33aa809d 2019-08-08 stsp }
5041 1ebedb77 2019-10-19 stsp if (have_content) {
5042 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5043 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
5044 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
5045 1ebedb77 2019-10-19 stsp if (err)
5046 1ebedb77 2019-10-19 stsp goto done;
5047 1ebedb77 2019-10-19 stsp
5048 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
5049 a2c162eb 2022-10-30 thomas mode_t mode;
5050 a2c162eb 2022-10-30 thomas
5051 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
5052 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
5053 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
5054 fa3cef63 2020-07-23 stsp goto done;
5055 fa3cef63 2020-07-23 stsp }
5056 1ebedb77 2019-10-19 stsp }
5057 1ebedb77 2019-10-19 stsp }
5058 33aa809d 2019-08-08 stsp done:
5059 33aa809d 2019-08-08 stsp free(id_str);
5060 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5061 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5062 33aa809d 2019-08-08 stsp if (blob)
5063 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5064 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5065 fe621944 2020-11-10 stsp if (err == NULL)
5066 fe621944 2020-11-10 stsp err = free_err;
5067 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5068 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5069 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5070 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5071 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5072 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5073 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5074 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5075 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5076 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5077 33aa809d 2019-08-08 stsp if (err || !have_content) {
5078 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5079 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5080 33aa809d 2019-08-08 stsp free(*path_outfile);
5081 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5082 33aa809d 2019-08-08 stsp }
5083 33aa809d 2019-08-08 stsp free(path1);
5084 33aa809d 2019-08-08 stsp return err;
5085 33aa809d 2019-08-08 stsp }
5086 33aa809d 2019-08-08 stsp
5087 33aa809d 2019-08-08 stsp static const struct got_error *
5088 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5089 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5090 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5091 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5092 a129376b 2019-03-28 stsp {
5093 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5094 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5095 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5096 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5097 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
5098 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5099 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5100 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5101 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5102 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5103 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5104 f4ae6ddb 2022-07-01 thomas int fd = -1;
5105 a129376b 2019-03-28 stsp
5106 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5107 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5108 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5109 d3bcc3d1 2019-08-08 stsp return NULL;
5110 3d69ad8d 2019-08-17 semarie
5111 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5112 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5113 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5114 d3bcc3d1 2019-08-08 stsp
5115 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5116 65084dad 2019-08-08 stsp if (ie == NULL)
5117 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5118 a129376b 2019-03-28 stsp
5119 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5120 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5121 a129376b 2019-03-28 stsp if (err) {
5122 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5123 a129376b 2019-03-28 stsp goto done;
5124 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5125 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5126 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5127 e20a8b6f 2019-06-04 stsp goto done;
5128 e20a8b6f 2019-06-04 stsp }
5129 a129376b 2019-03-28 stsp }
5130 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5131 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5132 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5133 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5134 a129376b 2019-03-28 stsp goto done;
5135 a129376b 2019-03-28 stsp }
5136 a129376b 2019-03-28 stsp } else {
5137 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5138 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5139 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5140 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5141 a129376b 2019-03-28 stsp goto done;
5142 a129376b 2019-03-28 stsp }
5143 a129376b 2019-03-28 stsp } else {
5144 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5145 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5146 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5147 a129376b 2019-03-28 stsp goto done;
5148 a129376b 2019-03-28 stsp }
5149 a129376b 2019-03-28 stsp }
5150 a129376b 2019-03-28 stsp }
5151 a129376b 2019-03-28 stsp
5152 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
5153 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
5154 945f9229 2022-04-16 thomas if (err)
5155 945f9229 2022-04-16 thomas goto done;
5156 945f9229 2022-04-16 thomas
5157 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5158 a9fa2909 2019-07-27 stsp if (err) {
5159 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5160 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5161 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5162 a9fa2909 2019-07-27 stsp goto done;
5163 a9fa2909 2019-07-27 stsp } else {
5164 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5165 a9fa2909 2019-07-27 stsp if (err)
5166 a9fa2909 2019-07-27 stsp goto done;
5167 a9fa2909 2019-07-27 stsp
5168 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5169 1233e6b6 2020-10-19 stsp if (err)
5170 a9fa2909 2019-07-27 stsp goto done;
5171 a9fa2909 2019-07-27 stsp
5172 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5173 1233e6b6 2020-10-19 stsp free(te_name);
5174 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5175 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5176 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5177 a9fa2909 2019-07-27 stsp goto done;
5178 a9fa2909 2019-07-27 stsp }
5179 a129376b 2019-03-28 stsp }
5180 a129376b 2019-03-28 stsp
5181 a129376b 2019-03-28 stsp switch (status) {
5182 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5183 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5184 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5185 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5186 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5187 33aa809d 2019-08-08 stsp if (err)
5188 33aa809d 2019-08-08 stsp goto done;
5189 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5190 33aa809d 2019-08-08 stsp break;
5191 33aa809d 2019-08-08 stsp }
5192 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5193 1f1abb7e 2019-08-08 stsp ie->path);
5194 1ee397ad 2019-07-12 stsp if (err)
5195 1ee397ad 2019-07-12 stsp goto done;
5196 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5197 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5198 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5199 8641a332 2023-04-14 thomas
5200 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5201 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5202 8641a332 2023-04-14 thomas
5203 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5204 8641a332 2023-04-14 thomas entry) {
5205 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5206 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5207 8641a332 2023-04-14 thomas continue;
5208 8641a332 2023-04-14 thomas do_unlink = 1;
5209 8641a332 2023-04-14 thomas break;
5210 8641a332 2023-04-14 thomas }
5211 10604dce 2021-09-24 thomas }
5212 8641a332 2023-04-14 thomas
5213 8641a332 2023-04-14 thomas if (do_unlink) {
5214 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5215 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5216 8641a332 2023-04-14 thomas relpath) == -1) {
5217 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5218 8641a332 2023-04-14 thomas goto done;
5219 8641a332 2023-04-14 thomas }
5220 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5221 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5222 8641a332 2023-04-14 thomas ondisk_path);
5223 8641a332 2023-04-14 thomas break;
5224 8641a332 2023-04-14 thomas }
5225 10604dce 2021-09-24 thomas }
5226 10604dce 2021-09-24 thomas }
5227 a129376b 2019-03-28 stsp break;
5228 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5229 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5230 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5231 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5232 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5233 33aa809d 2019-08-08 stsp if (err)
5234 33aa809d 2019-08-08 stsp goto done;
5235 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5236 33aa809d 2019-08-08 stsp break;
5237 33aa809d 2019-08-08 stsp }
5238 33aa809d 2019-08-08 stsp /* fall through */
5239 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5240 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5241 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5242 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5243 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5244 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5245 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5246 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5247 43010591 2023-02-17 thomas else
5248 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5249 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5250 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5251 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5252 f4ae6ddb 2022-07-01 thomas goto done;
5253 f4ae6ddb 2022-07-01 thomas }
5254 f4ae6ddb 2022-07-01 thomas
5255 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5256 a129376b 2019-03-28 stsp if (err)
5257 65084dad 2019-08-08 stsp goto done;
5258 65084dad 2019-08-08 stsp
5259 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5260 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5261 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5262 a129376b 2019-03-28 stsp goto done;
5263 65084dad 2019-08-08 stsp }
5264 65084dad 2019-08-08 stsp
5265 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5266 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5267 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5268 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5269 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5270 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5271 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5272 33aa809d 2019-08-08 stsp break;
5273 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5274 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5275 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5276 369fd7e5 2020-07-23 stsp path_content);
5277 369fd7e5 2020-07-23 stsp break;
5278 369fd7e5 2020-07-23 stsp }
5279 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5280 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5281 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5282 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5283 369fd7e5 2020-07-23 stsp } else {
5284 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5285 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5286 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5287 369fd7e5 2020-07-23 stsp goto done;
5288 369fd7e5 2020-07-23 stsp }
5289 33aa809d 2019-08-08 stsp }
5290 33aa809d 2019-08-08 stsp } else {
5291 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5292 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5293 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5294 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5295 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5296 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5297 2e1fa222 2020-07-23 stsp } else {
5298 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5299 2e1fa222 2020-07-23 stsp ie->path,
5300 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5301 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5302 aaf972e2 2023-09-18 thomas 0, 1, 0, 0, NULL, a->repo,
5303 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5304 2e1fa222 2020-07-23 stsp }
5305 a129376b 2019-03-28 stsp if (err)
5306 a129376b 2019-03-28 stsp goto done;
5307 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5308 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5309 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5310 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5311 437adc9d 2020-12-10 yzhong blob->id.sha1,
5312 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5313 2e1fa222 2020-07-23 stsp if (err)
5314 2e1fa222 2020-07-23 stsp goto done;
5315 2e1fa222 2020-07-23 stsp }
5316 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5317 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5318 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5319 33aa809d 2019-08-08 stsp }
5320 a129376b 2019-03-28 stsp }
5321 a129376b 2019-03-28 stsp break;
5322 e20a8b6f 2019-06-04 stsp }
5323 a129376b 2019-03-28 stsp default:
5324 1f1abb7e 2019-08-08 stsp break;
5325 a129376b 2019-03-28 stsp }
5326 a129376b 2019-03-28 stsp done:
5327 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5328 33aa809d 2019-08-08 stsp free(path_content);
5329 e20a8b6f 2019-06-04 stsp free(parent_path);
5330 a129376b 2019-03-28 stsp free(tree_path);
5331 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5332 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5333 a129376b 2019-03-28 stsp if (blob)
5334 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5335 a129376b 2019-03-28 stsp if (tree)
5336 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5337 a129376b 2019-03-28 stsp free(tree_id);
5338 945f9229 2022-04-16 thomas if (base_commit)
5339 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5340 e20a8b6f 2019-06-04 stsp return err;
5341 e20a8b6f 2019-06-04 stsp }
5342 e20a8b6f 2019-06-04 stsp
5343 e20a8b6f 2019-06-04 stsp const struct got_error *
5344 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5345 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5346 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5347 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5348 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5349 e20a8b6f 2019-06-04 stsp {
5350 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5351 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5352 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5353 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5354 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5355 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5356 e20a8b6f 2019-06-04 stsp
5357 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5358 e20a8b6f 2019-06-04 stsp if (err)
5359 e20a8b6f 2019-06-04 stsp return err;
5360 e20a8b6f 2019-06-04 stsp
5361 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5362 e20a8b6f 2019-06-04 stsp if (err)
5363 e20a8b6f 2019-06-04 stsp goto done;
5364 e20a8b6f 2019-06-04 stsp
5365 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5366 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5367 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5368 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5369 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5370 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5371 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5372 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5373 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5374 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5375 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5376 e20a8b6f 2019-06-04 stsp if (err)
5377 af12c6b9 2019-06-04 stsp break;
5378 e20a8b6f 2019-06-04 stsp }
5379 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5380 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5381 e20a8b6f 2019-06-04 stsp err = sync_err;
5382 af12c6b9 2019-06-04 stsp done:
5383 fb399478 2019-07-12 stsp free(fileindex_path);
5384 a129376b 2019-03-28 stsp if (fileindex)
5385 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5386 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5387 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5388 a129376b 2019-03-28 stsp err = unlockerr;
5389 c4296144 2019-05-09 stsp return err;
5390 c4296144 2019-05-09 stsp }
5391 c4296144 2019-05-09 stsp
5392 cf066bf8 2019-05-09 stsp static void
5393 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5394 cf066bf8 2019-05-09 stsp {
5395 24519714 2019-05-09 stsp free(ct->path);
5396 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5397 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5398 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5399 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5400 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5401 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5402 cf066bf8 2019-05-09 stsp free(ct);
5403 cf066bf8 2019-05-09 stsp }
5404 24519714 2019-05-09 stsp
5405 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5406 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5407 24519714 2019-05-09 stsp struct got_repository *repo;
5408 24519714 2019-05-09 stsp struct got_worktree *worktree;
5409 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5410 5f8a88c6 2019-08-03 stsp int have_staged_files;
5411 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5412 ef899790 2022-11-01 thomas int diff_header_shown;
5413 be94c032 2023-02-20 thomas int commit_conflicts;
5414 ef899790 2022-11-01 thomas FILE *diff_outfile;
5415 ef899790 2022-11-01 thomas FILE *f1;
5416 ef899790 2022-11-01 thomas FILE *f2;
5417 24519714 2019-05-09 stsp };
5418 ef899790 2022-11-01 thomas
5419 ef899790 2022-11-01 thomas /*
5420 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5421 ef899790 2022-11-01 thomas * it as content to the diff engine.
5422 ef899790 2022-11-01 thomas */
5423 ef899790 2022-11-01 thomas static const struct got_error *
5424 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5425 ef899790 2022-11-01 thomas const char *abspath)
5426 ef899790 2022-11-01 thomas {
5427 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5428 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5429 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5430 ef899790 2022-11-01 thomas
5431 ef899790 2022-11-01 thomas *fd = -1;
5432 ef899790 2022-11-01 thomas
5433 ef899790 2022-11-01 thomas if (dirfd != -1) {
5434 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5435 ef899790 2022-11-01 thomas if (target_len == -1)
5436 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5437 ef899790 2022-11-01 thomas } else {
5438 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5439 ef899790 2022-11-01 thomas if (target_len == -1)
5440 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5441 ef899790 2022-11-01 thomas }
5442 ef899790 2022-11-01 thomas
5443 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5444 ef899790 2022-11-01 thomas if (*fd == -1)
5445 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5446 ef899790 2022-11-01 thomas
5447 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5448 ef899790 2022-11-01 thomas if (outlen == -1) {
5449 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5450 ef899790 2022-11-01 thomas goto done;
5451 ef899790 2022-11-01 thomas }
5452 ef899790 2022-11-01 thomas
5453 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5454 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5455 ef899790 2022-11-01 thomas goto done;
5456 ef899790 2022-11-01 thomas }
5457 ef899790 2022-11-01 thomas done:
5458 ef899790 2022-11-01 thomas if (err) {
5459 ef899790 2022-11-01 thomas close(*fd);
5460 ef899790 2022-11-01 thomas *fd = -1;
5461 ef899790 2022-11-01 thomas }
5462 ef899790 2022-11-01 thomas return err;
5463 ef899790 2022-11-01 thomas }
5464 ef899790 2022-11-01 thomas
5465 ef899790 2022-11-01 thomas static const struct got_error *
5466 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5467 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5468 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5469 ef899790 2022-11-01 thomas {
5470 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5471 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5472 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5473 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5474 ef899790 2022-11-01 thomas char *label1 = NULL;
5475 ef899790 2022-11-01 thomas struct stat sb;
5476 ef899790 2022-11-01 thomas off_t size1 = 0;
5477 ef899790 2022-11-01 thomas int f2_exists = 0;
5478 ef899790 2022-11-01 thomas char *id_str = NULL;
5479 ef899790 2022-11-01 thomas
5480 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5481 d259e491 2022-11-01 thomas
5482 d259e491 2022-11-01 thomas if (diff_staged) {
5483 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5484 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5485 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5486 d259e491 2022-11-01 thomas return NULL;
5487 d259e491 2022-11-01 thomas } else {
5488 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5489 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5490 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5491 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5492 d259e491 2022-11-01 thomas return NULL;
5493 d259e491 2022-11-01 thomas }
5494 ef899790 2022-11-01 thomas
5495 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5496 ef899790 2022-11-01 thomas if (err)
5497 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5498 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5499 ef899790 2022-11-01 thomas if (err)
5500 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5501 ef899790 2022-11-01 thomas
5502 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5503 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5504 ef899790 2022-11-01 thomas if (err)
5505 ef899790 2022-11-01 thomas return err;
5506 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5507 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5508 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5509 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5510 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5511 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5512 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5513 ef899790 2022-11-01 thomas }
5514 ef899790 2022-11-01 thomas
5515 ef899790 2022-11-01 thomas if (diff_staged) {
5516 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5517 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5518 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5519 ef899790 2022-11-01 thomas label1 = ct->path;
5520 ef899790 2022-11-01 thomas label2 = ct->path;
5521 ef899790 2022-11-01 thomas break;
5522 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5523 ef899790 2022-11-01 thomas label2 = ct->path;
5524 ef899790 2022-11-01 thomas break;
5525 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5526 ef899790 2022-11-01 thomas label1 = ct->path;
5527 ef899790 2022-11-01 thomas break;
5528 ef899790 2022-11-01 thomas default:
5529 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5530 ef899790 2022-11-01 thomas }
5531 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5532 ef899790 2022-11-01 thomas if (fd1 == -1) {
5533 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5534 ef899790 2022-11-01 thomas goto done;
5535 ef899790 2022-11-01 thomas }
5536 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5537 ef899790 2022-11-01 thomas if (fd2 == -1) {
5538 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5539 ef899790 2022-11-01 thomas goto done;
5540 ef899790 2022-11-01 thomas }
5541 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5542 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5543 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5544 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5545 ef899790 2022-11-01 thomas goto done;
5546 ef899790 2022-11-01 thomas }
5547 ef899790 2022-11-01 thomas
5548 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5549 ef899790 2022-11-01 thomas if (fd1 == -1) {
5550 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5551 ef899790 2022-11-01 thomas goto done;
5552 ef899790 2022-11-01 thomas }
5553 ef899790 2022-11-01 thomas
5554 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5555 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5556 ef899790 2022-11-01 thomas 8192, fd1);
5557 ef899790 2022-11-01 thomas if (err)
5558 ef899790 2022-11-01 thomas goto done;
5559 ef899790 2022-11-01 thomas }
5560 ef899790 2022-11-01 thomas
5561 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5562 ef899790 2022-11-01 thomas if (dirfd != -1) {
5563 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5564 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5565 ef899790 2022-11-01 thomas if (fd == -1) {
5566 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5567 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5568 ef899790 2022-11-01 thomas ct->ondisk_path);
5569 ef899790 2022-11-01 thomas goto done;
5570 ef899790 2022-11-01 thomas }
5571 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5572 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5573 ef899790 2022-11-01 thomas if (err)
5574 ef899790 2022-11-01 thomas goto done;
5575 ef899790 2022-11-01 thomas }
5576 ef899790 2022-11-01 thomas } else {
5577 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5578 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5579 ef899790 2022-11-01 thomas if (fd == -1) {
5580 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5581 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5582 ef899790 2022-11-01 thomas ct->ondisk_path);
5583 ef899790 2022-11-01 thomas goto done;
5584 ef899790 2022-11-01 thomas }
5585 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5586 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5587 ef899790 2022-11-01 thomas if (err)
5588 ef899790 2022-11-01 thomas goto done;
5589 ef899790 2022-11-01 thomas }
5590 ef899790 2022-11-01 thomas }
5591 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5592 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5593 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5594 ef899790 2022-11-01 thomas goto done;
5595 ef899790 2022-11-01 thomas }
5596 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5597 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5598 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5599 ef899790 2022-11-01 thomas goto done;
5600 ef899790 2022-11-01 thomas }
5601 ef899790 2022-11-01 thomas fd = -1;
5602 ef899790 2022-11-01 thomas f2_exists = 1;
5603 ef899790 2022-11-01 thomas }
5604 ef899790 2022-11-01 thomas
5605 ef899790 2022-11-01 thomas if (blob1) {
5606 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5607 ef899790 2022-11-01 thomas f1, blob1);
5608 ef899790 2022-11-01 thomas if (err)
5609 ef899790 2022-11-01 thomas goto done;
5610 ef899790 2022-11-01 thomas }
5611 ef899790 2022-11-01 thomas
5612 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5613 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5614 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5615 ef899790 2022-11-01 thomas done:
5616 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5617 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5618 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5619 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5620 ef899790 2022-11-01 thomas if (blob1)
5621 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5622 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5623 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5624 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5625 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5626 ef899790 2022-11-01 thomas return err;
5627 ef899790 2022-11-01 thomas }
5628 cf066bf8 2019-05-09 stsp
5629 c4296144 2019-05-09 stsp static const struct got_error *
5630 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5631 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5632 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5633 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5634 c4296144 2019-05-09 stsp {
5635 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5636 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5637 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5638 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5639 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5640 768aea60 2019-05-09 stsp struct stat sb;
5641 c4296144 2019-05-09 stsp
5642 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5643 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5644 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5645 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5646 5f8a88c6 2019-08-03 stsp return NULL;
5647 5f8a88c6 2019-08-03 stsp } else {
5648 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5649 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5650 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5651 be94c032 2023-02-20 thomas }
5652 c4296144 2019-05-09 stsp
5653 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5654 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5655 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5656 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5657 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5658 5f8a88c6 2019-08-03 stsp return NULL;
5659 5f8a88c6 2019-08-03 stsp }
5660 0b5cc0d6 2019-05-09 stsp
5661 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5662 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5663 036813ee 2019-05-09 stsp goto done;
5664 036813ee 2019-05-09 stsp }
5665 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5666 036813ee 2019-05-09 stsp parent_path = strdup("");
5667 69960a46 2019-05-09 stsp if (parent_path == NULL)
5668 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5669 69960a46 2019-05-09 stsp } else {
5670 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5671 69960a46 2019-05-09 stsp if (err)
5672 69960a46 2019-05-09 stsp return err;
5673 69960a46 2019-05-09 stsp }
5674 c4296144 2019-05-09 stsp
5675 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5676 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5677 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5678 c4296144 2019-05-09 stsp goto done;
5679 768aea60 2019-05-09 stsp }
5680 768aea60 2019-05-09 stsp
5681 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5682 768aea60 2019-05-09 stsp relpath) == -1) {
5683 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5684 768aea60 2019-05-09 stsp goto done;
5685 768aea60 2019-05-09 stsp }
5686 0aeb8099 2020-07-23 stsp
5687 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5688 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5689 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5690 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5691 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5692 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5693 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5694 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5695 0aeb8099 2020-07-23 stsp break;
5696 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5697 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5698 0aeb8099 2020-07-23 stsp break;
5699 0aeb8099 2020-07-23 stsp default:
5700 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5701 0aeb8099 2020-07-23 stsp goto done;
5702 0aeb8099 2020-07-23 stsp }
5703 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5704 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5705 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5706 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5707 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5708 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5709 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5710 12463d8b 2019-12-13 stsp ct->ondisk_path);
5711 12463d8b 2019-12-13 stsp goto done;
5712 12463d8b 2019-12-13 stsp }
5713 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5714 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5715 768aea60 2019-05-09 stsp goto done;
5716 768aea60 2019-05-09 stsp }
5717 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5718 c4296144 2019-05-09 stsp }
5719 c4296144 2019-05-09 stsp
5720 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5721 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5722 44d03001 2019-05-09 stsp relpath) == -1) {
5723 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5724 44d03001 2019-05-09 stsp goto done;
5725 44d03001 2019-05-09 stsp }
5726 44d03001 2019-05-09 stsp
5727 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5728 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5729 35213c7c 2020-07-23 stsp int is_bad_symlink;
5730 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5731 35213c7c 2020-07-23 stsp ssize_t target_len;
5732 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5733 35213c7c 2020-07-23 stsp sizeof(target_path));
5734 35213c7c 2020-07-23 stsp if (target_len == -1) {
5735 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5736 35213c7c 2020-07-23 stsp ct->ondisk_path);
5737 35213c7c 2020-07-23 stsp goto done;
5738 35213c7c 2020-07-23 stsp }
5739 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5740 ad10f64e 2023-07-19 thomas target_len, ct->ondisk_path, a->worktree->root_path,
5741 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
5742 35213c7c 2020-07-23 stsp if (err)
5743 35213c7c 2020-07-23 stsp goto done;
5744 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5745 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5746 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5747 35213c7c 2020-07-23 stsp goto done;
5748 35213c7c 2020-07-23 stsp }
5749 35213c7c 2020-07-23 stsp }
5750 35213c7c 2020-07-23 stsp
5751 35213c7c 2020-07-23 stsp
5752 cf066bf8 2019-05-09 stsp ct->status = status;
5753 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5754 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5755 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5756 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5757 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5758 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5759 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5760 b416585c 2019-05-13 stsp goto done;
5761 b416585c 2019-05-13 stsp }
5762 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5763 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5764 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5765 f0b75401 2019-08-03 stsp goto done;
5766 f0b75401 2019-08-03 stsp }
5767 f0b75401 2019-08-03 stsp }
5768 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5769 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5770 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5771 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5772 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5773 036813ee 2019-05-09 stsp goto done;
5774 036813ee 2019-05-09 stsp }
5775 ca2503ea 2019-05-09 stsp }
5776 24519714 2019-05-09 stsp ct->path = strdup(path);
5777 24519714 2019-05-09 stsp if (ct->path == NULL) {
5778 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5779 24519714 2019-05-09 stsp goto done;
5780 24519714 2019-05-09 stsp }
5781 ef899790 2022-11-01 thomas
5782 720b08bc 2023-08-29 thomas if (a->diff_outfile) {
5783 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5784 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5785 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5786 ef899790 2022-11-01 thomas if (err)
5787 ef899790 2022-11-01 thomas goto done;
5788 ef899790 2022-11-01 thomas }
5789 720b08bc 2023-08-29 thomas
5790 720b08bc 2023-08-29 thomas err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5791 c4296144 2019-05-09 stsp done:
5792 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5793 ed175427 2019-05-09 stsp free_commitable(ct);
5794 24519714 2019-05-09 stsp free(parent_path);
5795 036813ee 2019-05-09 stsp free(path);
5796 a129376b 2019-03-28 stsp return err;
5797 a129376b 2019-03-28 stsp }
5798 c4296144 2019-05-09 stsp
5799 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5800 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5801 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5802 036813ee 2019-05-09 stsp struct got_repository *);
5803 ed175427 2019-05-09 stsp
5804 ed175427 2019-05-09 stsp static const struct got_error *
5805 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5806 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5807 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5808 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5809 afa376bf 2019-05-09 stsp struct got_repository *repo)
5810 ed175427 2019-05-09 stsp {
5811 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5812 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5813 036813ee 2019-05-09 stsp char *subpath;
5814 ed175427 2019-05-09 stsp
5815 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5816 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5817 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5818 ed175427 2019-05-09 stsp
5819 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5820 ed175427 2019-05-09 stsp if (err)
5821 ed175427 2019-05-09 stsp return err;
5822 ed175427 2019-05-09 stsp
5823 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5824 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5825 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5826 036813ee 2019-05-09 stsp free(subpath);
5827 036813ee 2019-05-09 stsp return err;
5828 036813ee 2019-05-09 stsp }
5829 ed175427 2019-05-09 stsp
5830 036813ee 2019-05-09 stsp static const struct got_error *
5831 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5832 036813ee 2019-05-09 stsp {
5833 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5834 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5835 ed175427 2019-05-09 stsp
5836 036813ee 2019-05-09 stsp *match = 0;
5837 ed175427 2019-05-09 stsp
5838 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5839 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5840 0f63689d 2019-05-10 stsp return NULL;
5841 036813ee 2019-05-09 stsp }
5842 0b5cc0d6 2019-05-09 stsp
5843 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5844 0f63689d 2019-05-10 stsp if (err)
5845 0f63689d 2019-05-10 stsp return err;
5846 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5847 036813ee 2019-05-09 stsp free(ct_parent_path);
5848 036813ee 2019-05-09 stsp return err;
5849 036813ee 2019-05-09 stsp }
5850 0b5cc0d6 2019-05-09 stsp
5851 768aea60 2019-05-09 stsp static mode_t
5852 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5853 768aea60 2019-05-09 stsp {
5854 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5855 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5856 3d9a4ec4 2020-07-23 stsp
5857 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5858 768aea60 2019-05-09 stsp }
5859 768aea60 2019-05-09 stsp
5860 036813ee 2019-05-09 stsp static const struct got_error *
5861 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5862 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5863 036813ee 2019-05-09 stsp {
5864 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5865 ca2503ea 2019-05-09 stsp
5866 036813ee 2019-05-09 stsp *new_te = NULL;
5867 0b5cc0d6 2019-05-09 stsp
5868 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5869 036813ee 2019-05-09 stsp if (err)
5870 036813ee 2019-05-09 stsp goto done;
5871 0b5cc0d6 2019-05-09 stsp
5872 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5873 036813ee 2019-05-09 stsp
5874 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5875 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5876 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5877 5f8a88c6 2019-08-03 stsp else
5878 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5879 036813ee 2019-05-09 stsp done:
5880 036813ee 2019-05-09 stsp if (err && *new_te) {
5881 56e0773d 2019-11-28 stsp free(*new_te);
5882 036813ee 2019-05-09 stsp *new_te = NULL;
5883 036813ee 2019-05-09 stsp }
5884 036813ee 2019-05-09 stsp return err;
5885 036813ee 2019-05-09 stsp }
5886 036813ee 2019-05-09 stsp
5887 036813ee 2019-05-09 stsp static const struct got_error *
5888 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5889 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5890 036813ee 2019-05-09 stsp {
5891 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5892 102b254e 2020-10-19 stsp char *ct_name = NULL;
5893 036813ee 2019-05-09 stsp
5894 9c57c4c6 2024-02-13 thomas *new_te = NULL;
5895 036813ee 2019-05-09 stsp
5896 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5897 036813ee 2019-05-09 stsp if (*new_te == NULL)
5898 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5899 036813ee 2019-05-09 stsp
5900 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5901 102b254e 2020-10-19 stsp if (err)
5902 036813ee 2019-05-09 stsp goto done;
5903 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5904 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5905 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5906 036813ee 2019-05-09 stsp goto done;
5907 036813ee 2019-05-09 stsp }
5908 036813ee 2019-05-09 stsp
5909 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5910 036813ee 2019-05-09 stsp
5911 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5912 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5913 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5914 5f8a88c6 2019-08-03 stsp else
5915 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5916 036813ee 2019-05-09 stsp done:
5917 102b254e 2020-10-19 stsp free(ct_name);
5918 036813ee 2019-05-09 stsp if (err && *new_te) {
5919 56e0773d 2019-11-28 stsp free(*new_te);
5920 036813ee 2019-05-09 stsp *new_te = NULL;
5921 036813ee 2019-05-09 stsp }
5922 036813ee 2019-05-09 stsp return err;
5923 036813ee 2019-05-09 stsp }
5924 036813ee 2019-05-09 stsp
5925 036813ee 2019-05-09 stsp static const struct got_error *
5926 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5927 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5928 036813ee 2019-05-09 stsp {
5929 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5930 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5931 036813ee 2019-05-09 stsp
5932 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5933 036813ee 2019-05-09 stsp if (err)
5934 036813ee 2019-05-09 stsp return err;
5935 036813ee 2019-05-09 stsp if (new_pe == NULL)
5936 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5937 036813ee 2019-05-09 stsp return NULL;
5938 afa376bf 2019-05-09 stsp }
5939 afa376bf 2019-05-09 stsp
5940 afa376bf 2019-05-09 stsp static const struct got_error *
5941 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5942 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5943 afa376bf 2019-05-09 stsp {
5944 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5945 5f8a88c6 2019-08-03 stsp unsigned char status;
5946 ae1e948a 2021-09-28 thomas
5947 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5948 ae1e948a 2021-09-28 thomas return NULL;
5949 5f8a88c6 2019-08-03 stsp
5950 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5951 afa376bf 2019-05-09 stsp ct_path++;
5952 5f8a88c6 2019-08-03 stsp
5953 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5954 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5955 5f8a88c6 2019-08-03 stsp else
5956 5f8a88c6 2019-08-03 stsp status = ct->status;
5957 5f8a88c6 2019-08-03 stsp
5958 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5959 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5960 036813ee 2019-05-09 stsp }
5961 036813ee 2019-05-09 stsp
5962 036813ee 2019-05-09 stsp static const struct got_error *
5963 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5964 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5965 44d03001 2019-05-09 stsp {
5966 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5967 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5968 44d03001 2019-05-09 stsp char *te_path;
5969 44d03001 2019-05-09 stsp
5970 44d03001 2019-05-09 stsp *modified = 0;
5971 44d03001 2019-05-09 stsp
5972 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5973 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5974 44d03001 2019-05-09 stsp te->name) == -1)
5975 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5976 44d03001 2019-05-09 stsp
5977 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5978 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5979 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5980 44d03001 2019-05-09 stsp strlen(te_path));
5981 62d463ca 2020-10-20 naddy if (*modified)
5982 44d03001 2019-05-09 stsp break;
5983 44d03001 2019-05-09 stsp }
5984 44d03001 2019-05-09 stsp
5985 44d03001 2019-05-09 stsp free(te_path);
5986 44d03001 2019-05-09 stsp return err;
5987 44d03001 2019-05-09 stsp }
5988 44d03001 2019-05-09 stsp
5989 44d03001 2019-05-09 stsp static const struct got_error *
5990 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5991 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5992 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5993 036813ee 2019-05-09 stsp {
5994 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5995 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5996 036813ee 2019-05-09 stsp
5997 036813ee 2019-05-09 stsp *ctp = NULL;
5998 036813ee 2019-05-09 stsp
5999 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6000 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6001 036813ee 2019-05-09 stsp char *ct_name = NULL;
6002 036813ee 2019-05-09 stsp int path_matches;
6003 036813ee 2019-05-09 stsp
6004 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6005 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
6006 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
6007 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
6008 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6009 5f8a88c6 2019-08-03 stsp continue;
6010 5f8a88c6 2019-08-03 stsp } else {
6011 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
6012 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
6013 5f8a88c6 2019-08-03 stsp continue;
6014 5f8a88c6 2019-08-03 stsp }
6015 036813ee 2019-05-09 stsp
6016 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6017 036813ee 2019-05-09 stsp continue;
6018 036813ee 2019-05-09 stsp
6019 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6020 62d463ca 2020-10-20 naddy if (err)
6021 036813ee 2019-05-09 stsp return err;
6022 036813ee 2019-05-09 stsp if (!path_matches)
6023 036813ee 2019-05-09 stsp continue;
6024 036813ee 2019-05-09 stsp
6025 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
6026 d34b633e 2020-10-19 stsp if (err)
6027 d34b633e 2020-10-19 stsp return err;
6028 d34b633e 2020-10-19 stsp
6029 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
6030 d34b633e 2020-10-19 stsp free(ct_name);
6031 036813ee 2019-05-09 stsp continue;
6032 d34b633e 2020-10-19 stsp }
6033 d34b633e 2020-10-19 stsp free(ct_name);
6034 036813ee 2019-05-09 stsp
6035 036813ee 2019-05-09 stsp *ctp = ct;
6036 036813ee 2019-05-09 stsp break;
6037 036813ee 2019-05-09 stsp }
6038 2b496619 2019-07-10 stsp
6039 2b496619 2019-07-10 stsp return err;
6040 2b496619 2019-07-10 stsp }
6041 2b496619 2019-07-10 stsp
6042 2b496619 2019-07-10 stsp static const struct got_error *
6043 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6044 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
6045 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
6046 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
6047 2b496619 2019-07-10 stsp struct got_repository *repo)
6048 2b496619 2019-07-10 stsp {
6049 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
6050 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
6051 2b496619 2019-07-10 stsp char *subtree_path;
6052 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
6053 ba580f68 2020-03-22 stsp int nentries;
6054 2b496619 2019-07-10 stsp
6055 2b496619 2019-07-10 stsp *new_tep = NULL;
6056 2b496619 2019-07-10 stsp
6057 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6058 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6059 2b496619 2019-07-10 stsp child_path) == -1)
6060 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6061 036813ee 2019-05-09 stsp
6062 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6063 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6064 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6065 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6066 56e0773d 2019-11-28 stsp
6067 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6068 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6069 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6070 2b496619 2019-07-10 stsp goto done;
6071 2b496619 2019-07-10 stsp }
6072 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6073 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6074 2b496619 2019-07-10 stsp if (err) {
6075 56e0773d 2019-11-28 stsp free(new_te);
6076 2b496619 2019-07-10 stsp goto done;
6077 2b496619 2019-07-10 stsp }
6078 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6079 2b496619 2019-07-10 stsp done:
6080 56e0773d 2019-11-28 stsp free(id);
6081 2b496619 2019-07-10 stsp free(subtree_path);
6082 2b496619 2019-07-10 stsp if (err == NULL)
6083 2b496619 2019-07-10 stsp *new_tep = new_te;
6084 036813ee 2019-05-09 stsp return err;
6085 036813ee 2019-05-09 stsp }
6086 036813ee 2019-05-09 stsp
6087 036813ee 2019-05-09 stsp static const struct got_error *
6088 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6089 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6090 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6091 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6092 036813ee 2019-05-09 stsp struct got_repository *repo)
6093 036813ee 2019-05-09 stsp {
6094 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6095 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6096 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6097 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6098 036813ee 2019-05-09 stsp
6099 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6100 ba580f68 2020-03-22 stsp *nentries = 0;
6101 036813ee 2019-05-09 stsp
6102 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6103 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6104 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6105 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6106 036813ee 2019-05-09 stsp
6107 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6108 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6109 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6110 036813ee 2019-05-09 stsp continue;
6111 036813ee 2019-05-09 stsp
6112 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6113 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6114 036813ee 2019-05-09 stsp continue;
6115 036813ee 2019-05-09 stsp
6116 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6117 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6118 036813ee 2019-05-09 stsp if (err)
6119 036813ee 2019-05-09 stsp goto done;
6120 036813ee 2019-05-09 stsp
6121 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6122 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6123 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6124 036813ee 2019-05-09 stsp if (err)
6125 036813ee 2019-05-09 stsp goto done;
6126 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6127 afa376bf 2019-05-09 stsp if (err)
6128 afa376bf 2019-05-09 stsp goto done;
6129 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6130 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6131 2b496619 2019-07-10 stsp if (err)
6132 2f51b5b3 2019-05-09 stsp goto done;
6133 ba580f68 2020-03-22 stsp (*nentries)++;
6134 2b496619 2019-07-10 stsp } else {
6135 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6136 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6137 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6138 2b496619 2019-07-10 stsp == NULL) {
6139 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6140 2b496619 2019-07-10 stsp child_path, path_base_tree,
6141 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6142 2b496619 2019-07-10 stsp repo);
6143 2b496619 2019-07-10 stsp if (err)
6144 2b496619 2019-07-10 stsp goto done;
6145 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6146 2b496619 2019-07-10 stsp if (err)
6147 2b496619 2019-07-10 stsp goto done;
6148 ba580f68 2020-03-22 stsp (*nentries)++;
6149 9ba0479c 2019-05-10 stsp }
6150 ed175427 2019-05-09 stsp }
6151 2f51b5b3 2019-05-09 stsp }
6152 2f51b5b3 2019-05-09 stsp
6153 2f51b5b3 2019-05-09 stsp if (base_tree) {
6154 56e0773d 2019-11-28 stsp int i, nbase_entries;
6155 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6156 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6157 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6158 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6159 63c5ca5d 2019-08-24 stsp
6160 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6161 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6162 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6163 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6164 63c5ca5d 2019-08-24 stsp if (err)
6165 63c5ca5d 2019-08-24 stsp goto done;
6166 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6167 63c5ca5d 2019-08-24 stsp if (err)
6168 63c5ca5d 2019-08-24 stsp goto done;
6169 ba580f68 2020-03-22 stsp (*nentries)++;
6170 63c5ca5d 2019-08-24 stsp continue;
6171 63c5ca5d 2019-08-24 stsp }
6172 2f51b5b3 2019-05-09 stsp
6173 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6174 44d03001 2019-05-09 stsp int modified;
6175 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6176 036813ee 2019-05-09 stsp if (err)
6177 036813ee 2019-05-09 stsp goto done;
6178 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6179 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6180 2f51b5b3 2019-05-09 stsp if (err)
6181 2f51b5b3 2019-05-09 stsp goto done;
6182 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6183 44d03001 2019-05-09 stsp if (modified) {
6184 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6185 ba580f68 2020-03-22 stsp int nsubentries;
6186 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6187 ba580f68 2020-03-22 stsp &nsubentries, te,
6188 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6189 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6190 44d03001 2019-05-09 stsp if (err)
6191 44d03001 2019-05-09 stsp goto done;
6192 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6193 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6194 ba580f68 2020-03-22 stsp free(new_id);
6195 ba580f68 2020-03-22 stsp continue;
6196 ba580f68 2020-03-22 stsp }
6197 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6198 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6199 56e0773d 2019-11-28 stsp free(new_id);
6200 44d03001 2019-05-09 stsp }
6201 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6202 036813ee 2019-05-09 stsp if (err)
6203 036813ee 2019-05-09 stsp goto done;
6204 ba580f68 2020-03-22 stsp (*nentries)++;
6205 2f51b5b3 2019-05-09 stsp continue;
6206 036813ee 2019-05-09 stsp }
6207 2f51b5b3 2019-05-09 stsp
6208 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6209 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6210 f66c734c 2019-09-22 stsp if (err)
6211 f66c734c 2019-09-22 stsp goto done;
6212 2f51b5b3 2019-05-09 stsp if (ct) {
6213 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6214 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6215 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6216 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6217 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6218 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6219 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6220 2f51b5b3 2019-05-09 stsp if (err)
6221 2f51b5b3 2019-05-09 stsp goto done;
6222 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6223 2f51b5b3 2019-05-09 stsp if (err)
6224 2f51b5b3 2019-05-09 stsp goto done;
6225 ba580f68 2020-03-22 stsp (*nentries)++;
6226 2f51b5b3 2019-05-09 stsp }
6227 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6228 b416585c 2019-05-13 stsp status_arg);
6229 afa376bf 2019-05-09 stsp if (err)
6230 afa376bf 2019-05-09 stsp goto done;
6231 2f51b5b3 2019-05-09 stsp } else {
6232 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6233 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6234 2f51b5b3 2019-05-09 stsp if (err)
6235 2f51b5b3 2019-05-09 stsp goto done;
6236 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6237 2f51b5b3 2019-05-09 stsp if (err)
6238 2f51b5b3 2019-05-09 stsp goto done;
6239 ba580f68 2020-03-22 stsp (*nentries)++;
6240 2f51b5b3 2019-05-09 stsp }
6241 ca2503ea 2019-05-09 stsp }
6242 ed175427 2019-05-09 stsp }
6243 0b5cc0d6 2019-05-09 stsp
6244 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6245 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6246 ed175427 2019-05-09 stsp done:
6247 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6248 2e1fa222 2020-07-23 stsp return err;
6249 2e1fa222 2020-07-23 stsp }
6250 2e1fa222 2020-07-23 stsp
6251 2e1fa222 2020-07-23 stsp static const struct got_error *
6252 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6253 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6254 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6255 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6256 ebf99748 2019-05-09 stsp {
6257 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6258 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6259 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6260 ebf99748 2019-05-09 stsp
6261 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6262 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6263 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6264 ebf99748 2019-05-09 stsp
6265 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6266 437adc9d 2020-12-10 yzhong
6267 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6268 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6269 437adc9d 2020-12-10 yzhong if (err)
6270 437adc9d 2020-12-10 yzhong goto done;
6271 437adc9d 2020-12-10 yzhong
6272 ebf99748 2019-05-09 stsp if (ie) {
6273 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6274 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6275 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6276 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6277 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6278 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6279 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6280 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6281 437adc9d 2020-12-10 yzhong
6282 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6283 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6284 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6285 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6286 72fd46fa 2019-09-06 stsp !have_staged_files);
6287 ebf99748 2019-05-09 stsp } else
6288 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6289 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6290 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6291 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6292 72fd46fa 2019-09-06 stsp !have_staged_files);
6293 ebf99748 2019-05-09 stsp } else {
6294 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6295 ebf99748 2019-05-09 stsp if (err)
6296 437adc9d 2020-12-10 yzhong goto done;
6297 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6298 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6299 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6300 3969253a 2020-03-07 stsp if (err) {
6301 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6302 437adc9d 2020-12-10 yzhong goto done;
6303 3969253a 2020-03-07 stsp }
6304 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6305 3969253a 2020-03-07 stsp if (err) {
6306 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6307 437adc9d 2020-12-10 yzhong goto done;
6308 3969253a 2020-03-07 stsp }
6309 ebf99748 2019-05-09 stsp }
6310 437adc9d 2020-12-10 yzhong free(relpath);
6311 437adc9d 2020-12-10 yzhong relpath = NULL;
6312 ebf99748 2019-05-09 stsp }
6313 437adc9d 2020-12-10 yzhong done:
6314 437adc9d 2020-12-10 yzhong free(relpath);
6315 d56d26ce 2019-05-10 stsp return err;
6316 d56d26ce 2019-05-10 stsp }
6317 735ef5ac 2019-08-03 stsp
6318 d56d26ce 2019-05-10 stsp
6319 d56d26ce 2019-05-10 stsp static const struct got_error *
6320 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6321 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6322 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6323 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6324 735ef5ac 2019-08-03 stsp int ood_errcode)
6325 d56d26ce 2019-05-10 stsp {
6326 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6327 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6328 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6329 1a36436d 2019-06-10 stsp
6330 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6331 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6332 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6333 1a36436d 2019-06-10 stsp return NULL;
6334 9bead371 2019-07-28 stsp /*
6335 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6336 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6337 9bead371 2019-07-28 stsp */
6338 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6339 945f9229 2022-04-16 thomas if (err)
6340 945f9229 2022-04-16 thomas goto done;
6341 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6342 9bead371 2019-07-28 stsp if (err) {
6343 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6344 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6345 1a36436d 2019-06-10 stsp goto done;
6346 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6347 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6348 1a36436d 2019-06-10 stsp } else {
6349 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6350 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6351 945f9229 2022-04-16 thomas if (err)
6352 945f9229 2022-04-16 thomas goto done;
6353 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6354 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6355 1a36436d 2019-06-10 stsp goto done;
6356 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6357 1a36436d 2019-06-10 stsp }
6358 1a36436d 2019-06-10 stsp done:
6359 1a36436d 2019-06-10 stsp free(id);
6360 945f9229 2022-04-16 thomas if (commit)
6361 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6362 1a36436d 2019-06-10 stsp return err;
6363 ebf99748 2019-05-09 stsp }
6364 ebf99748 2019-05-09 stsp
6365 ef20f542 2022-06-26 thomas static const struct got_error *
6366 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6367 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6368 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6369 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6370 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6371 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6372 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6373 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6374 afa376bf 2019-05-09 stsp struct got_repository *repo)
6375 c4296144 2019-05-09 stsp {
6376 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6377 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6378 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6379 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6380 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6381 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6382 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6383 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6384 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6385 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6386 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6387 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6388 892eab0a 2022-07-22 thomas time_t timestamp;
6389 c4296144 2019-05-09 stsp
6390 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6391 c4296144 2019-05-09 stsp
6392 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6393 675c7539 2019-05-09 stsp
6394 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6395 588edf97 2019-05-10 stsp if (err)
6396 588edf97 2019-05-10 stsp goto done;
6397 de18fc63 2019-05-09 stsp
6398 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6399 588edf97 2019-05-10 stsp if (err)
6400 588edf97 2019-05-10 stsp goto done;
6401 588edf97 2019-05-10 stsp
6402 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6403 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6404 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6405 33ad4cbe 2019-05-12 jcs if (err)
6406 33ad4cbe 2019-05-12 jcs goto done;
6407 33ad4cbe 2019-05-12 jcs }
6408 c4296144 2019-05-09 stsp
6409 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6410 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6411 33ad4cbe 2019-05-12 jcs goto done;
6412 33ad4cbe 2019-05-12 jcs }
6413 33ad4cbe 2019-05-12 jcs
6414 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6415 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6416 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6417 cf066bf8 2019-05-09 stsp char *ondisk_path;
6418 cf066bf8 2019-05-09 stsp
6419 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6420 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6421 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6422 5f8a88c6 2019-08-03 stsp continue;
6423 5f8a88c6 2019-08-03 stsp
6424 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6425 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6426 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6427 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6428 cf066bf8 2019-05-09 stsp continue;
6429 cf066bf8 2019-05-09 stsp
6430 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6431 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6432 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6433 cf066bf8 2019-05-09 stsp goto done;
6434 cf066bf8 2019-05-09 stsp }
6435 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6436 2e1fa222 2020-07-23 stsp free(ondisk_path);
6437 2e1fa222 2020-07-23 stsp if (err)
6438 cf066bf8 2019-05-09 stsp goto done;
6439 cf066bf8 2019-05-09 stsp }
6440 036813ee 2019-05-09 stsp
6441 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6442 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6443 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6444 036813ee 2019-05-09 stsp if (err)
6445 036813ee 2019-05-09 stsp goto done;
6446 036813ee 2019-05-09 stsp
6447 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6448 de18fc63 2019-05-09 stsp if (err)
6449 de18fc63 2019-05-09 stsp goto done;
6450 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6451 10604dce 2021-09-24 thomas nparents++;
6452 10604dce 2021-09-24 thomas if (parent_id2) {
6453 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6454 10604dce 2021-09-24 thomas if (err)
6455 10604dce 2021-09-24 thomas goto done;
6456 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6457 10604dce 2021-09-24 thomas nparents++;
6458 10604dce 2021-09-24 thomas }
6459 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6460 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6461 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6462 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6463 33ad4cbe 2019-05-12 jcs free(logmsg);
6464 9d40349a 2019-05-09 stsp if (err)
6465 9d40349a 2019-05-09 stsp goto done;
6466 9d40349a 2019-05-09 stsp
6467 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6468 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6469 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6470 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6471 09f5bd90 2019-05-09 stsp goto done;
6472 09f5bd90 2019-05-09 stsp }
6473 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6474 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6475 09f5bd90 2019-05-09 stsp if (err)
6476 09f5bd90 2019-05-09 stsp goto done;
6477 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6478 ebf99748 2019-05-09 stsp if (err)
6479 ebf99748 2019-05-09 stsp goto done;
6480 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6481 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6482 09f5bd90 2019-05-09 stsp goto done;
6483 09f5bd90 2019-05-09 stsp }
6484 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6485 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6486 f2c16586 2019-05-09 stsp if (err)
6487 f2c16586 2019-05-09 stsp goto done;
6488 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6489 f2c16586 2019-05-09 stsp if (err)
6490 f2c16586 2019-05-09 stsp goto done;
6491 f2c16586 2019-05-09 stsp
6492 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6493 09f5bd90 2019-05-09 stsp if (err)
6494 09f5bd90 2019-05-09 stsp goto done;
6495 09f5bd90 2019-05-09 stsp
6496 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6497 ebf99748 2019-05-09 stsp if (err)
6498 ebf99748 2019-05-09 stsp goto done;
6499 c4296144 2019-05-09 stsp done:
6500 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6501 588edf97 2019-05-10 stsp if (head_tree)
6502 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6503 588edf97 2019-05-10 stsp if (head_commit)
6504 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6505 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6506 2f17228e 2019-05-12 stsp if (head_ref2) {
6507 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6508 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6509 2f17228e 2019-05-12 stsp err = unlockerr;
6510 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6511 39cd0ff6 2019-07-12 stsp }
6512 39cd0ff6 2019-07-12 stsp return err;
6513 39cd0ff6 2019-07-12 stsp }
6514 5c1e53bc 2019-07-28 stsp
6515 5c1e53bc 2019-07-28 stsp static const struct got_error *
6516 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6517 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6518 5c1e53bc 2019-07-28 stsp {
6519 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6520 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6521 39cd0ff6 2019-07-12 stsp
6522 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6523 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6524 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6525 5c1e53bc 2019-07-28 stsp
6526 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6527 5c1e53bc 2019-07-28 stsp ct_path++;
6528 5c1e53bc 2019-07-28 stsp
6529 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6530 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6531 5c1e53bc 2019-07-28 stsp break;
6532 5c1e53bc 2019-07-28 stsp }
6533 5c1e53bc 2019-07-28 stsp
6534 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6535 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6536 5c1e53bc 2019-07-28 stsp
6537 5c1e53bc 2019-07-28 stsp return NULL;
6538 5c1e53bc 2019-07-28 stsp }
6539 5c1e53bc 2019-07-28 stsp
6540 f0b75401 2019-08-03 stsp static const struct got_error *
6541 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6542 f0b75401 2019-08-03 stsp {
6543 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6544 f0b75401 2019-08-03 stsp
6545 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6546 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6547 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6548 f0b75401 2019-08-03 stsp }
6549 f0b75401 2019-08-03 stsp
6550 f0b75401 2019-08-03 stsp return NULL;
6551 f0b75401 2019-08-03 stsp }
6552 f0b75401 2019-08-03 stsp
6553 5f8a88c6 2019-08-03 stsp static const struct got_error *
6554 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6555 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6556 5f8a88c6 2019-08-03 stsp {
6557 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6558 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6559 5f8a88c6 2019-08-03 stsp
6560 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6561 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6562 5f8a88c6 2019-08-03 stsp continue;
6563 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6564 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6565 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6566 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6567 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6568 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6569 5f8a88c6 2019-08-03 stsp }
6570 5f8a88c6 2019-08-03 stsp
6571 5f8a88c6 2019-08-03 stsp return NULL;
6572 5f8a88c6 2019-08-03 stsp }
6573 5f8a88c6 2019-08-03 stsp
6574 39cd0ff6 2019-07-12 stsp const struct got_error *
6575 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6576 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6577 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6578 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6579 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6580 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6581 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6582 39cd0ff6 2019-07-12 stsp {
6583 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6584 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6585 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6586 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6587 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6588 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6589 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6590 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6591 ef899790 2022-11-01 thomas char *diff_path = NULL;
6592 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6593 39cd0ff6 2019-07-12 stsp
6594 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6595 39cd0ff6 2019-07-12 stsp
6596 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6597 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6598 39cd0ff6 2019-07-12 stsp
6599 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6600 39cd0ff6 2019-07-12 stsp if (err)
6601 39cd0ff6 2019-07-12 stsp goto done;
6602 39cd0ff6 2019-07-12 stsp
6603 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6604 39cd0ff6 2019-07-12 stsp if (err)
6605 39cd0ff6 2019-07-12 stsp goto done;
6606 39cd0ff6 2019-07-12 stsp
6607 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6608 39cd0ff6 2019-07-12 stsp if (err)
6609 39cd0ff6 2019-07-12 stsp goto done;
6610 39cd0ff6 2019-07-12 stsp
6611 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6612 39cd0ff6 2019-07-12 stsp if (err)
6613 39cd0ff6 2019-07-12 stsp goto done;
6614 39cd0ff6 2019-07-12 stsp
6615 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6616 5f8a88c6 2019-08-03 stsp &have_staged_files);
6617 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6618 5f8a88c6 2019-08-03 stsp goto done;
6619 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6620 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6621 5f8a88c6 2019-08-03 stsp if (err)
6622 5f8a88c6 2019-08-03 stsp goto done;
6623 5f8a88c6 2019-08-03 stsp }
6624 5f8a88c6 2019-08-03 stsp
6625 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6626 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6627 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6628 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6629 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6630 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6631 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6632 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6633 ef899790 2022-11-01 thomas if (show_diff) {
6634 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6635 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6636 ef899790 2022-11-01 thomas if (err)
6637 ef899790 2022-11-01 thomas goto done;
6638 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6639 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6640 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6641 ef899790 2022-11-01 thomas goto done;
6642 ef899790 2022-11-01 thomas }
6643 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6644 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6645 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6646 ef899790 2022-11-01 thomas goto done;
6647 ef899790 2022-11-01 thomas }
6648 ef899790 2022-11-01 thomas }
6649 ef899790 2022-11-01 thomas
6650 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6651 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6652 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6653 5c1e53bc 2019-07-28 stsp if (err)
6654 5c1e53bc 2019-07-28 stsp goto done;
6655 5c1e53bc 2019-07-28 stsp }
6656 39cd0ff6 2019-07-12 stsp
6657 ef899790 2022-11-01 thomas if (show_diff) {
6658 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6659 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6660 ef899790 2022-11-01 thomas goto done;
6661 ef899790 2022-11-01 thomas }
6662 ef899790 2022-11-01 thomas }
6663 ef899790 2022-11-01 thomas
6664 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6665 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6666 39cd0ff6 2019-07-12 stsp goto done;
6667 5c1e53bc 2019-07-28 stsp }
6668 5c1e53bc 2019-07-28 stsp
6669 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6670 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6671 5c1e53bc 2019-07-28 stsp if (err)
6672 5c1e53bc 2019-07-28 stsp goto done;
6673 39cd0ff6 2019-07-12 stsp }
6674 f0b75401 2019-08-03 stsp
6675 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6676 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6677 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6678 f0b75401 2019-08-03 stsp
6679 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6680 f0b75401 2019-08-03 stsp ct_path++;
6681 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6682 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6683 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6684 b50cabdf 2019-07-12 stsp if (err)
6685 b50cabdf 2019-07-12 stsp goto done;
6686 f0b75401 2019-08-03 stsp
6687 b50cabdf 2019-07-12 stsp }
6688 b50cabdf 2019-07-12 stsp
6689 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6690 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6691 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6692 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6693 39cd0ff6 2019-07-12 stsp if (err)
6694 39cd0ff6 2019-07-12 stsp goto done;
6695 39cd0ff6 2019-07-12 stsp
6696 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6697 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6698 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6699 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6700 39cd0ff6 2019-07-12 stsp err = sync_err;
6701 39cd0ff6 2019-07-12 stsp done:
6702 39cd0ff6 2019-07-12 stsp if (fileindex)
6703 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6704 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6705 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6706 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6707 39cd0ff6 2019-07-12 stsp err = unlockerr;
6708 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6709 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6710 21c2d8be 2023-01-10 thomas
6711 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6712 39cd0ff6 2019-07-12 stsp }
6713 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6714 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6715 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6716 ef899790 2022-11-01 thomas free(diff_path);
6717 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6718 ef899790 2022-11-01 thomas err == NULL)
6719 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6720 c4296144 2019-05-09 stsp return err;
6721 8656d6c4 2019-05-20 stsp }
6722 8656d6c4 2019-05-20 stsp
6723 8656d6c4 2019-05-20 stsp const char *
6724 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6725 8656d6c4 2019-05-20 stsp {
6726 8656d6c4 2019-05-20 stsp return ct->path;
6727 8656d6c4 2019-05-20 stsp }
6728 8656d6c4 2019-05-20 stsp
6729 8656d6c4 2019-05-20 stsp unsigned int
6730 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6731 8656d6c4 2019-05-20 stsp {
6732 8656d6c4 2019-05-20 stsp return ct->status;
6733 818c7501 2019-07-11 stsp }
6734 818c7501 2019-07-11 stsp
6735 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6736 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6737 818c7501 2019-07-11 stsp struct got_repository *repo;
6738 818c7501 2019-07-11 stsp };
6739 818c7501 2019-07-11 stsp
6740 818c7501 2019-07-11 stsp static const struct got_error *
6741 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6742 818c7501 2019-07-11 stsp {
6743 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6744 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6745 818c7501 2019-07-11 stsp unsigned char status;
6746 818c7501 2019-07-11 stsp struct stat sb;
6747 818c7501 2019-07-11 stsp char *ondisk_path;
6748 818c7501 2019-07-11 stsp
6749 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6750 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6751 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6752 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6753 818c7501 2019-07-11 stsp
6754 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6755 818c7501 2019-07-11 stsp == -1)
6756 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6757 818c7501 2019-07-11 stsp
6758 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6759 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6760 818c7501 2019-07-11 stsp free(ondisk_path);
6761 818c7501 2019-07-11 stsp if (err)
6762 818c7501 2019-07-11 stsp return err;
6763 818c7501 2019-07-11 stsp
6764 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6765 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6766 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6767 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6768 818c7501 2019-07-11 stsp
6769 818c7501 2019-07-11 stsp return NULL;
6770 818c7501 2019-07-11 stsp }
6771 818c7501 2019-07-11 stsp
6772 818c7501 2019-07-11 stsp const struct got_error *
6773 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6774 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6775 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6776 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6777 818c7501 2019-07-11 stsp {
6778 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6779 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6780 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6781 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6782 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6783 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6784 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6785 818c7501 2019-07-11 stsp
6786 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6787 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6788 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6789 818c7501 2019-07-11 stsp
6790 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6791 818c7501 2019-07-11 stsp if (err)
6792 818c7501 2019-07-11 stsp return err;
6793 818c7501 2019-07-11 stsp
6794 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6795 818c7501 2019-07-11 stsp if (err)
6796 818c7501 2019-07-11 stsp goto done;
6797 818c7501 2019-07-11 stsp
6798 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6799 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6800 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6801 818c7501 2019-07-11 stsp &ok_arg);
6802 818c7501 2019-07-11 stsp if (err)
6803 818c7501 2019-07-11 stsp goto done;
6804 818c7501 2019-07-11 stsp
6805 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6806 818c7501 2019-07-11 stsp if (err)
6807 818c7501 2019-07-11 stsp goto done;
6808 818c7501 2019-07-11 stsp
6809 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6810 818c7501 2019-07-11 stsp if (err)
6811 818c7501 2019-07-11 stsp goto done;
6812 818c7501 2019-07-11 stsp
6813 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6814 818c7501 2019-07-11 stsp if (err)
6815 818c7501 2019-07-11 stsp goto done;
6816 818c7501 2019-07-11 stsp
6817 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6818 818c7501 2019-07-11 stsp 0);
6819 e51d7b55 2020-01-04 stsp if (err)
6820 e51d7b55 2020-01-04 stsp goto done;
6821 e51d7b55 2020-01-04 stsp
6822 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6823 818c7501 2019-07-11 stsp if (err)
6824 818c7501 2019-07-11 stsp goto done;
6825 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6826 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6827 e51d7b55 2020-01-04 stsp goto done;
6828 e51d7b55 2020-01-04 stsp }
6829 818c7501 2019-07-11 stsp
6830 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6831 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6832 818c7501 2019-07-11 stsp if (err)
6833 818c7501 2019-07-11 stsp goto done;
6834 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6835 818c7501 2019-07-11 stsp if (err)
6836 818c7501 2019-07-11 stsp goto done;
6837 818c7501 2019-07-11 stsp
6838 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6839 818c7501 2019-07-11 stsp
6840 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6841 818c7501 2019-07-11 stsp if (err)
6842 818c7501 2019-07-11 stsp goto done;
6843 818c7501 2019-07-11 stsp
6844 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6845 818c7501 2019-07-11 stsp if (err)
6846 818c7501 2019-07-11 stsp goto done;
6847 818c7501 2019-07-11 stsp
6848 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6849 818c7501 2019-07-11 stsp worktree->base_commit_id);
6850 818c7501 2019-07-11 stsp if (err)
6851 818c7501 2019-07-11 stsp goto done;
6852 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6853 818c7501 2019-07-11 stsp if (err)
6854 818c7501 2019-07-11 stsp goto done;
6855 818c7501 2019-07-11 stsp
6856 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6857 818c7501 2019-07-11 stsp if (err)
6858 818c7501 2019-07-11 stsp goto done;
6859 818c7501 2019-07-11 stsp done:
6860 818c7501 2019-07-11 stsp free(fileindex_path);
6861 818c7501 2019-07-11 stsp free(tmp_branch_name);
6862 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6863 818c7501 2019-07-11 stsp free(branch_ref_name);
6864 818c7501 2019-07-11 stsp if (branch_ref)
6865 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6866 818c7501 2019-07-11 stsp if (wt_branch)
6867 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6868 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6869 818c7501 2019-07-11 stsp if (err) {
6870 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6871 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6872 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6873 818c7501 2019-07-11 stsp }
6874 818c7501 2019-07-11 stsp if (*tmp_branch) {
6875 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6876 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6877 818c7501 2019-07-11 stsp }
6878 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6879 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6880 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6881 3e3a69f1 2019-07-25 stsp }
6882 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6883 818c7501 2019-07-11 stsp }
6884 818c7501 2019-07-11 stsp return err;
6885 818c7501 2019-07-11 stsp }
6886 818c7501 2019-07-11 stsp
6887 818c7501 2019-07-11 stsp const struct got_error *
6888 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6889 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6890 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6891 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6892 818c7501 2019-07-11 stsp {
6893 818c7501 2019-07-11 stsp const struct got_error *err;
6894 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6895 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6896 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6897 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6898 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6899 818c7501 2019-07-11 stsp
6900 818c7501 2019-07-11 stsp *commit_id = NULL;
6901 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6902 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6903 3e3a69f1 2019-07-25 stsp *branch = NULL;
6904 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6905 3e3a69f1 2019-07-25 stsp
6906 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6907 3e3a69f1 2019-07-25 stsp if (err)
6908 3e3a69f1 2019-07-25 stsp return err;
6909 818c7501 2019-07-11 stsp
6910 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6911 3e3a69f1 2019-07-25 stsp if (err)
6912 f032f1f7 2019-08-04 stsp goto done;
6913 f032f1f7 2019-08-04 stsp
6914 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6915 f032f1f7 2019-08-04 stsp &have_staged_files);
6916 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6917 f032f1f7 2019-08-04 stsp goto done;
6918 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6919 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6920 3e3a69f1 2019-07-25 stsp goto done;
6921 f032f1f7 2019-08-04 stsp }
6922 3e3a69f1 2019-07-25 stsp
6923 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6924 818c7501 2019-07-11 stsp if (err)
6925 f032f1f7 2019-08-04 stsp goto done;
6926 818c7501 2019-07-11 stsp
6927 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6928 818c7501 2019-07-11 stsp if (err)
6929 818c7501 2019-07-11 stsp goto done;
6930 818c7501 2019-07-11 stsp
6931 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6932 818c7501 2019-07-11 stsp if (err)
6933 818c7501 2019-07-11 stsp goto done;
6934 818c7501 2019-07-11 stsp
6935 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6936 818c7501 2019-07-11 stsp if (err)
6937 818c7501 2019-07-11 stsp goto done;
6938 818c7501 2019-07-11 stsp
6939 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6940 818c7501 2019-07-11 stsp if (err)
6941 818c7501 2019-07-11 stsp goto done;
6942 818c7501 2019-07-11 stsp
6943 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6944 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6945 818c7501 2019-07-11 stsp if (err)
6946 818c7501 2019-07-11 stsp goto done;
6947 818c7501 2019-07-11 stsp
6948 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6949 818c7501 2019-07-11 stsp if (err)
6950 818c7501 2019-07-11 stsp goto done;
6951 818c7501 2019-07-11 stsp
6952 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6953 818c7501 2019-07-11 stsp if (err)
6954 818c7501 2019-07-11 stsp goto done;
6955 818c7501 2019-07-11 stsp
6956 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6957 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6958 818c7501 2019-07-11 stsp if (err)
6959 818c7501 2019-07-11 stsp goto done;
6960 818c7501 2019-07-11 stsp
6961 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6962 818c7501 2019-07-11 stsp if (err)
6963 818c7501 2019-07-11 stsp goto done;
6964 818c7501 2019-07-11 stsp done:
6965 818c7501 2019-07-11 stsp free(commit_ref_name);
6966 818c7501 2019-07-11 stsp free(branch_ref_name);
6967 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6968 818c7501 2019-07-11 stsp if (commit_ref)
6969 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6970 818c7501 2019-07-11 stsp if (branch_ref)
6971 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6972 818c7501 2019-07-11 stsp if (err) {
6973 818c7501 2019-07-11 stsp free(*commit_id);
6974 818c7501 2019-07-11 stsp *commit_id = NULL;
6975 818c7501 2019-07-11 stsp if (*tmp_branch) {
6976 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6977 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6978 818c7501 2019-07-11 stsp }
6979 818c7501 2019-07-11 stsp if (*new_base_branch) {
6980 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6981 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6982 818c7501 2019-07-11 stsp }
6983 818c7501 2019-07-11 stsp if (*branch) {
6984 818c7501 2019-07-11 stsp got_ref_close(*branch);
6985 818c7501 2019-07-11 stsp *branch = NULL;
6986 818c7501 2019-07-11 stsp }
6987 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6988 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6989 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6990 3e3a69f1 2019-07-25 stsp }
6991 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6992 818c7501 2019-07-11 stsp }
6993 818c7501 2019-07-11 stsp return err;
6994 c4296144 2019-05-09 stsp }
6995 818c7501 2019-07-11 stsp
6996 818c7501 2019-07-11 stsp const struct got_error *
6997 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6998 818c7501 2019-07-11 stsp {
6999 818c7501 2019-07-11 stsp const struct got_error *err;
7000 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
7001 818c7501 2019-07-11 stsp
7002 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7003 818c7501 2019-07-11 stsp if (err)
7004 818c7501 2019-07-11 stsp return err;
7005 818c7501 2019-07-11 stsp
7006 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7007 818c7501 2019-07-11 stsp free(tmp_branch_name);
7008 818c7501 2019-07-11 stsp return NULL;
7009 cd634f2d 2024-03-30 thomas }
7010 cd634f2d 2024-03-30 thomas
7011 cd634f2d 2024-03-30 thomas const struct got_error *
7012 cd634f2d 2024-03-30 thomas got_worktree_rebase_info(char **new_base_branch_name, char **branch_name,
7013 cd634f2d 2024-03-30 thomas struct got_worktree *worktree, struct got_repository *repo)
7014 cd634f2d 2024-03-30 thomas {
7015 cd634f2d 2024-03-30 thomas const struct got_error *err;
7016 cd634f2d 2024-03-30 thomas char *new_base_branch_ref_name = NULL;
7017 cd634f2d 2024-03-30 thomas char *branch_ref_name = NULL;
7018 cd634f2d 2024-03-30 thomas struct got_reference *branch_ref = NULL, *branch = NULL;
7019 cd634f2d 2024-03-30 thomas struct got_reference *new_base_branch = NULL;
7020 cd634f2d 2024-03-30 thomas
7021 cd634f2d 2024-03-30 thomas *new_base_branch_name = NULL;
7022 cd634f2d 2024-03-30 thomas *branch_name = NULL;
7023 cd634f2d 2024-03-30 thomas
7024 cd634f2d 2024-03-30 thomas err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7025 cd634f2d 2024-03-30 thomas if (err)
7026 cd634f2d 2024-03-30 thomas goto done;
7027 cd634f2d 2024-03-30 thomas
7028 cd634f2d 2024-03-30 thomas err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7029 cd634f2d 2024-03-30 thomas if (err)
7030 cd634f2d 2024-03-30 thomas goto done;
7031 cd634f2d 2024-03-30 thomas
7032 cd634f2d 2024-03-30 thomas err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
7033 cd634f2d 2024-03-30 thomas if (err)
7034 cd634f2d 2024-03-30 thomas goto done;
7035 cd634f2d 2024-03-30 thomas
7036 cd634f2d 2024-03-30 thomas err = got_ref_open(&branch, repo,
7037 cd634f2d 2024-03-30 thomas got_ref_get_symref_target(branch_ref), 0);
7038 cd634f2d 2024-03-30 thomas if (err)
7039 cd634f2d 2024-03-30 thomas goto done;
7040 cd634f2d 2024-03-30 thomas
7041 cd634f2d 2024-03-30 thomas err = got_ref_open(&new_base_branch, repo,
7042 cd634f2d 2024-03-30 thomas new_base_branch_ref_name, 0);
7043 cd634f2d 2024-03-30 thomas if (err)
7044 cd634f2d 2024-03-30 thomas goto done;
7045 cd634f2d 2024-03-30 thomas
7046 cd634f2d 2024-03-30 thomas if (!got_ref_is_symbolic(new_base_branch)) {
7047 cd634f2d 2024-03-30 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7048 cd634f2d 2024-03-30 thomas "%s is not a symbolic reference",
7049 cd634f2d 2024-03-30 thomas got_ref_get_name(branch_ref));
7050 cd634f2d 2024-03-30 thomas goto done;
7051 cd634f2d 2024-03-30 thomas }
7052 cd634f2d 2024-03-30 thomas
7053 cd634f2d 2024-03-30 thomas *new_base_branch_name = strdup(got_ref_get_symref_target(
7054 cd634f2d 2024-03-30 thomas new_base_branch));
7055 cd634f2d 2024-03-30 thomas if (*new_base_branch_name == NULL) {
7056 cd634f2d 2024-03-30 thomas err = got_error_from_errno("strdup");
7057 cd634f2d 2024-03-30 thomas goto done;
7058 cd634f2d 2024-03-30 thomas }
7059 cd634f2d 2024-03-30 thomas *branch_name = strdup(got_ref_get_name(branch));
7060 cd634f2d 2024-03-30 thomas if (*branch_name == NULL) {
7061 cd634f2d 2024-03-30 thomas err = got_error_from_errno("strdup");
7062 cd634f2d 2024-03-30 thomas goto done;
7063 cd634f2d 2024-03-30 thomas }
7064 cd634f2d 2024-03-30 thomas done:
7065 cd634f2d 2024-03-30 thomas free(branch_ref_name);
7066 cd634f2d 2024-03-30 thomas if (branch_ref)
7067 cd634f2d 2024-03-30 thomas got_ref_close(branch_ref);
7068 cd634f2d 2024-03-30 thomas if (new_base_branch)
7069 cd634f2d 2024-03-30 thomas got_ref_close(new_base_branch);
7070 cd634f2d 2024-03-30 thomas if (branch)
7071 cd634f2d 2024-03-30 thomas got_ref_close(branch);
7072 cd634f2d 2024-03-30 thomas return err;
7073 818c7501 2019-07-11 stsp }
7074 818c7501 2019-07-11 stsp
7075 818c7501 2019-07-11 stsp static const struct got_error *
7076 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7077 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7078 818c7501 2019-07-11 stsp {
7079 0ebf8283 2019-07-24 stsp *logmsg = arg;
7080 818c7501 2019-07-11 stsp return NULL;
7081 818c7501 2019-07-11 stsp }
7082 818c7501 2019-07-11 stsp
7083 818c7501 2019-07-11 stsp static const struct got_error *
7084 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7085 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7086 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7087 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7088 818c7501 2019-07-11 stsp {
7089 818c7501 2019-07-11 stsp return NULL;
7090 01757395 2019-07-12 stsp }
7091 01757395 2019-07-12 stsp
7092 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
7093 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
7094 01757395 2019-07-12 stsp void *progress_arg;
7095 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
7096 01757395 2019-07-12 stsp };
7097 01757395 2019-07-12 stsp
7098 01757395 2019-07-12 stsp static const struct got_error *
7099 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
7100 01757395 2019-07-12 stsp {
7101 01757395 2019-07-12 stsp const struct got_error *err;
7102 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
7103 01757395 2019-07-12 stsp char *p;
7104 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
7105 01757395 2019-07-12 stsp
7106 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
7107 01757395 2019-07-12 stsp if (err)
7108 01757395 2019-07-12 stsp return err;
7109 01757395 2019-07-12 stsp
7110 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
7111 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
7112 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
7113 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
7114 01757395 2019-07-12 stsp return NULL;
7115 01757395 2019-07-12 stsp
7116 01757395 2019-07-12 stsp p = strdup(path);
7117 01757395 2019-07-12 stsp if (p == NULL)
7118 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7119 01757395 2019-07-12 stsp
7120 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7121 01757395 2019-07-12 stsp if (err || new == NULL)
7122 01757395 2019-07-12 stsp free(p);
7123 01757395 2019-07-12 stsp return err;
7124 818c7501 2019-07-11 stsp }
7125 818c7501 2019-07-11 stsp
7126 0ebf8283 2019-07-24 stsp static const struct got_error *
7127 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7128 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7129 818c7501 2019-07-11 stsp {
7130 818c7501 2019-07-11 stsp const struct got_error *err;
7131 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7132 818c7501 2019-07-11 stsp
7133 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7134 818c7501 2019-07-11 stsp if (err) {
7135 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7136 818c7501 2019-07-11 stsp goto done;
7137 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7138 818c7501 2019-07-11 stsp if (err)
7139 818c7501 2019-07-11 stsp goto done;
7140 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7141 818c7501 2019-07-11 stsp if (err)
7142 818c7501 2019-07-11 stsp goto done;
7143 de05890f 2020-03-05 stsp } else if (is_rebase) {
7144 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7145 818c7501 2019-07-11 stsp int cmp;
7146 818c7501 2019-07-11 stsp
7147 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7148 818c7501 2019-07-11 stsp if (err)
7149 818c7501 2019-07-11 stsp goto done;
7150 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7151 818c7501 2019-07-11 stsp free(stored_id);
7152 818c7501 2019-07-11 stsp if (cmp != 0) {
7153 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7154 818c7501 2019-07-11 stsp goto done;
7155 818c7501 2019-07-11 stsp }
7156 818c7501 2019-07-11 stsp }
7157 0ebf8283 2019-07-24 stsp done:
7158 0ebf8283 2019-07-24 stsp if (commit_ref)
7159 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7160 0ebf8283 2019-07-24 stsp return err;
7161 0ebf8283 2019-07-24 stsp }
7162 0ebf8283 2019-07-24 stsp
7163 0ebf8283 2019-07-24 stsp static const struct got_error *
7164 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7165 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7166 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7167 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7168 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7169 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7170 0ebf8283 2019-07-24 stsp {
7171 0ebf8283 2019-07-24 stsp const struct got_error *err;
7172 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7173 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7174 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7175 818c7501 2019-07-11 stsp
7176 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7177 0ebf8283 2019-07-24 stsp
7178 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7179 0ebf8283 2019-07-24 stsp if (err)
7180 0ebf8283 2019-07-24 stsp return err;
7181 0ebf8283 2019-07-24 stsp
7182 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7183 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7184 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7185 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7186 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7187 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7188 818c7501 2019-07-11 stsp if (commit_ref)
7189 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7190 818c7501 2019-07-11 stsp return err;
7191 818c7501 2019-07-11 stsp }
7192 818c7501 2019-07-11 stsp
7193 818c7501 2019-07-11 stsp const struct got_error *
7194 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7195 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7196 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7197 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7198 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7199 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7200 818c7501 2019-07-11 stsp {
7201 0ebf8283 2019-07-24 stsp const struct got_error *err;
7202 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7203 0ebf8283 2019-07-24 stsp
7204 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7205 0ebf8283 2019-07-24 stsp if (err)
7206 0ebf8283 2019-07-24 stsp return err;
7207 0ebf8283 2019-07-24 stsp
7208 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7209 0ebf8283 2019-07-24 stsp if (err)
7210 0ebf8283 2019-07-24 stsp goto done;
7211 0ebf8283 2019-07-24 stsp
7212 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7213 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7214 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7215 0ebf8283 2019-07-24 stsp done:
7216 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7217 0ebf8283 2019-07-24 stsp return err;
7218 0ebf8283 2019-07-24 stsp }
7219 0ebf8283 2019-07-24 stsp
7220 0ebf8283 2019-07-24 stsp const struct got_error *
7221 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7222 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7223 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7224 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7225 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7226 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7227 0ebf8283 2019-07-24 stsp {
7228 0ebf8283 2019-07-24 stsp const struct got_error *err;
7229 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7230 0ebf8283 2019-07-24 stsp
7231 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7232 0ebf8283 2019-07-24 stsp if (err)
7233 0ebf8283 2019-07-24 stsp return err;
7234 0ebf8283 2019-07-24 stsp
7235 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7236 0ebf8283 2019-07-24 stsp if (err)
7237 0ebf8283 2019-07-24 stsp goto done;
7238 0ebf8283 2019-07-24 stsp
7239 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7240 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7241 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7242 0ebf8283 2019-07-24 stsp done:
7243 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7244 0ebf8283 2019-07-24 stsp return err;
7245 0ebf8283 2019-07-24 stsp }
7246 0ebf8283 2019-07-24 stsp
7247 0ebf8283 2019-07-24 stsp static const struct got_error *
7248 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7249 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7250 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7251 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
7252 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
7253 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7254 0ebf8283 2019-07-24 stsp {
7255 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7256 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7257 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7258 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7259 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7260 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7261 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7262 818c7501 2019-07-11 stsp
7263 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7264 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7265 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7266 a0e95631 2019-07-12 stsp
7267 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7268 818c7501 2019-07-11 stsp
7269 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7270 a0e95631 2019-07-12 stsp if (err)
7271 3e3a69f1 2019-07-25 stsp return err;
7272 a0e95631 2019-07-12 stsp
7273 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7274 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7275 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7276 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7277 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7278 01757395 2019-07-12 stsp /*
7279 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7280 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7281 6c13b005 2021-09-02 stsp *
7282 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7283 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7284 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7285 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7286 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7287 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7288 01757395 2019-07-12 stsp */
7289 01757395 2019-07-12 stsp if (merged_paths) {
7290 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7291 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7292 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7293 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7294 f2a9dc41 2019-12-13 tracey 0);
7295 01757395 2019-07-12 stsp if (err)
7296 01757395 2019-07-12 stsp goto done;
7297 01757395 2019-07-12 stsp }
7298 01757395 2019-07-12 stsp } else {
7299 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7300 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7301 01757395 2019-07-12 stsp if (err)
7302 01757395 2019-07-12 stsp goto done;
7303 01757395 2019-07-12 stsp }
7304 a0e95631 2019-07-12 stsp
7305 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7306 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7307 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7308 a0e95631 2019-07-12 stsp if (err)
7309 a0e95631 2019-07-12 stsp goto done;
7310 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7311 a0e95631 2019-07-12 stsp goto done;
7312 ff0d2220 2019-07-11 stsp }
7313 818c7501 2019-07-11 stsp
7314 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7315 edd02c5e 2019-07-11 stsp if (err)
7316 edd02c5e 2019-07-11 stsp goto done;
7317 edd02c5e 2019-07-11 stsp
7318 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7319 818c7501 2019-07-11 stsp if (err)
7320 818c7501 2019-07-11 stsp goto done;
7321 818c7501 2019-07-11 stsp
7322 5943eee2 2019-08-13 stsp if (new_logmsg) {
7323 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7324 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7325 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7326 5943eee2 2019-08-13 stsp goto done;
7327 5943eee2 2019-08-13 stsp }
7328 5943eee2 2019-08-13 stsp } else {
7329 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7330 5943eee2 2019-08-13 stsp if (err)
7331 5943eee2 2019-08-13 stsp goto done;
7332 5943eee2 2019-08-13 stsp }
7333 0ebf8283 2019-07-24 stsp
7334 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7335 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7336 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7337 8db00f97 2022-07-22 thomas committer ? committer :
7338 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7339 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7340 a0e95631 2019-07-12 stsp if (err)
7341 a0e95631 2019-07-12 stsp goto done;
7342 a0e95631 2019-07-12 stsp
7343 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7344 a0e95631 2019-07-12 stsp if (err)
7345 a0e95631 2019-07-12 stsp goto done;
7346 a0e95631 2019-07-12 stsp
7347 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7348 a0e95631 2019-07-12 stsp if (err)
7349 a0e95631 2019-07-12 stsp goto done;
7350 a0e95631 2019-07-12 stsp
7351 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7352 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7353 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7354 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7355 a0e95631 2019-07-12 stsp err = sync_err;
7356 818c7501 2019-07-11 stsp done:
7357 a0e95631 2019-07-12 stsp free(fileindex_path);
7358 a0e95631 2019-07-12 stsp free(head_commit_id);
7359 a0e95631 2019-07-12 stsp if (head_ref)
7360 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7361 818c7501 2019-07-11 stsp if (err) {
7362 818c7501 2019-07-11 stsp free(*new_commit_id);
7363 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7364 818c7501 2019-07-11 stsp }
7365 818c7501 2019-07-11 stsp return err;
7366 818c7501 2019-07-11 stsp }
7367 818c7501 2019-07-11 stsp
7368 818c7501 2019-07-11 stsp const struct got_error *
7369 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7370 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7371 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7372 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7373 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7374 be94c032 2023-02-20 thomas struct got_repository *repo)
7375 0ebf8283 2019-07-24 stsp {
7376 0ebf8283 2019-07-24 stsp const struct got_error *err;
7377 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7378 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7379 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7380 0ebf8283 2019-07-24 stsp
7381 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7382 0ebf8283 2019-07-24 stsp if (err)
7383 0ebf8283 2019-07-24 stsp return err;
7384 0ebf8283 2019-07-24 stsp
7385 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7386 0ebf8283 2019-07-24 stsp if (err)
7387 0ebf8283 2019-07-24 stsp goto done;
7388 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7389 0ebf8283 2019-07-24 stsp if (err)
7390 0ebf8283 2019-07-24 stsp goto done;
7391 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7392 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7393 0ebf8283 2019-07-24 stsp goto done;
7394 0ebf8283 2019-07-24 stsp }
7395 0ebf8283 2019-07-24 stsp
7396 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7397 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7398 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7399 0ebf8283 2019-07-24 stsp done:
7400 0ebf8283 2019-07-24 stsp if (commit_ref)
7401 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7402 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7403 0ebf8283 2019-07-24 stsp free(commit_id);
7404 0ebf8283 2019-07-24 stsp return err;
7405 0ebf8283 2019-07-24 stsp }
7406 0ebf8283 2019-07-24 stsp
7407 0ebf8283 2019-07-24 stsp const struct got_error *
7408 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7409 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7410 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7411 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7412 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7413 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7414 0ebf8283 2019-07-24 stsp {
7415 0ebf8283 2019-07-24 stsp const struct got_error *err;
7416 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7417 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7418 0ebf8283 2019-07-24 stsp
7419 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7420 0ebf8283 2019-07-24 stsp if (err)
7421 0ebf8283 2019-07-24 stsp return err;
7422 0ebf8283 2019-07-24 stsp
7423 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7424 0ebf8283 2019-07-24 stsp if (err)
7425 0ebf8283 2019-07-24 stsp goto done;
7426 0ebf8283 2019-07-24 stsp
7427 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7428 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7429 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7430 0ebf8283 2019-07-24 stsp done:
7431 0ebf8283 2019-07-24 stsp if (commit_ref)
7432 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7433 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7434 0ebf8283 2019-07-24 stsp return err;
7435 0ebf8283 2019-07-24 stsp }
7436 0ebf8283 2019-07-24 stsp
7437 0ebf8283 2019-07-24 stsp const struct got_error *
7438 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7439 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7440 818c7501 2019-07-11 stsp {
7441 3e3a69f1 2019-07-25 stsp if (fileindex)
7442 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7443 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7444 69844fba 2019-07-11 stsp }
7445 69844fba 2019-07-11 stsp
7446 69844fba 2019-07-11 stsp static const struct got_error *
7447 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7448 69844fba 2019-07-11 stsp {
7449 69844fba 2019-07-11 stsp const struct got_error *err;
7450 69844fba 2019-07-11 stsp struct got_reference *ref;
7451 69844fba 2019-07-11 stsp
7452 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7453 69844fba 2019-07-11 stsp if (err) {
7454 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7455 69844fba 2019-07-11 stsp return NULL;
7456 69844fba 2019-07-11 stsp return err;
7457 69844fba 2019-07-11 stsp }
7458 69844fba 2019-07-11 stsp
7459 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7460 69844fba 2019-07-11 stsp got_ref_close(ref);
7461 69844fba 2019-07-11 stsp return err;
7462 818c7501 2019-07-11 stsp }
7463 818c7501 2019-07-11 stsp
7464 69844fba 2019-07-11 stsp static const struct got_error *
7465 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7466 69844fba 2019-07-11 stsp {
7467 69844fba 2019-07-11 stsp const struct got_error *err;
7468 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7469 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7470 69844fba 2019-07-11 stsp
7471 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7472 69844fba 2019-07-11 stsp if (err)
7473 69844fba 2019-07-11 stsp goto done;
7474 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7475 69844fba 2019-07-11 stsp if (err)
7476 69844fba 2019-07-11 stsp goto done;
7477 69844fba 2019-07-11 stsp
7478 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7479 69844fba 2019-07-11 stsp if (err)
7480 69844fba 2019-07-11 stsp goto done;
7481 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7482 69844fba 2019-07-11 stsp if (err)
7483 69844fba 2019-07-11 stsp goto done;
7484 69844fba 2019-07-11 stsp
7485 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7486 69844fba 2019-07-11 stsp if (err)
7487 69844fba 2019-07-11 stsp goto done;
7488 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7489 69844fba 2019-07-11 stsp if (err)
7490 69844fba 2019-07-11 stsp goto done;
7491 69844fba 2019-07-11 stsp
7492 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7493 69844fba 2019-07-11 stsp if (err)
7494 69844fba 2019-07-11 stsp goto done;
7495 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7496 69844fba 2019-07-11 stsp if (err)
7497 69844fba 2019-07-11 stsp goto done;
7498 69844fba 2019-07-11 stsp
7499 69844fba 2019-07-11 stsp done:
7500 69844fba 2019-07-11 stsp free(tmp_branch_name);
7501 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7502 69844fba 2019-07-11 stsp free(branch_ref_name);
7503 69844fba 2019-07-11 stsp free(commit_ref_name);
7504 e600f124 2021-03-21 stsp return err;
7505 e600f124 2021-03-21 stsp }
7506 e600f124 2021-03-21 stsp
7507 ef20f542 2022-06-26 thomas static const struct got_error *
7508 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7509 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7510 e600f124 2021-03-21 stsp {
7511 e600f124 2021-03-21 stsp const struct got_error *err;
7512 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7513 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7514 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7515 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7516 e600f124 2021-03-21 stsp char *refname = NULL;
7517 e600f124 2021-03-21 stsp
7518 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7519 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7520 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7521 e600f124 2021-03-21 stsp branch_name += 11;
7522 e600f124 2021-03-21 stsp
7523 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7524 e600f124 2021-03-21 stsp if (err)
7525 e600f124 2021-03-21 stsp return err;
7526 e600f124 2021-03-21 stsp
7527 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7528 e600f124 2021-03-21 stsp new_id_str) == -1) {
7529 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7530 e600f124 2021-03-21 stsp goto done;
7531 e600f124 2021-03-21 stsp }
7532 e600f124 2021-03-21 stsp
7533 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7534 e600f124 2021-03-21 stsp if (err)
7535 e600f124 2021-03-21 stsp goto done;
7536 e600f124 2021-03-21 stsp
7537 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7538 e600f124 2021-03-21 stsp if (err)
7539 e600f124 2021-03-21 stsp goto done;
7540 e600f124 2021-03-21 stsp
7541 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7542 e600f124 2021-03-21 stsp done:
7543 e600f124 2021-03-21 stsp free(new_id_str);
7544 e600f124 2021-03-21 stsp free(refname);
7545 e600f124 2021-03-21 stsp free(old_commit_id);
7546 e600f124 2021-03-21 stsp if (ref)
7547 e600f124 2021-03-21 stsp got_ref_close(ref);
7548 69844fba 2019-07-11 stsp return err;
7549 69844fba 2019-07-11 stsp }
7550 69844fba 2019-07-11 stsp
7551 818c7501 2019-07-11 stsp const struct got_error *
7552 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7553 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7554 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7555 f08f28be 2023-02-03 thomas int create_backup)
7556 818c7501 2019-07-11 stsp {
7557 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7558 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7559 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7560 818c7501 2019-07-11 stsp
7561 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7562 818c7501 2019-07-11 stsp if (err)
7563 818c7501 2019-07-11 stsp return err;
7564 e600f124 2021-03-21 stsp
7565 e600f124 2021-03-21 stsp if (create_backup) {
7566 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7567 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7568 e600f124 2021-03-21 stsp if (err)
7569 e600f124 2021-03-21 stsp goto done;
7570 e600f124 2021-03-21 stsp }
7571 818c7501 2019-07-11 stsp
7572 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7573 818c7501 2019-07-11 stsp if (err)
7574 818c7501 2019-07-11 stsp goto done;
7575 818c7501 2019-07-11 stsp
7576 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7577 818c7501 2019-07-11 stsp if (err)
7578 818c7501 2019-07-11 stsp goto done;
7579 818c7501 2019-07-11 stsp
7580 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7581 818c7501 2019-07-11 stsp if (err)
7582 818c7501 2019-07-11 stsp goto done;
7583 818c7501 2019-07-11 stsp
7584 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7585 a615e0e7 2020-12-16 stsp if (err)
7586 a615e0e7 2020-12-16 stsp goto done;
7587 a615e0e7 2020-12-16 stsp
7588 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7589 a615e0e7 2020-12-16 stsp if (err)
7590 a615e0e7 2020-12-16 stsp goto done;
7591 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7592 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7593 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7594 a615e0e7 2020-12-16 stsp err = sync_err;
7595 818c7501 2019-07-11 stsp done:
7596 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7597 a615e0e7 2020-12-16 stsp free(fileindex_path);
7598 818c7501 2019-07-11 stsp free(new_head_commit_id);
7599 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7600 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7601 818c7501 2019-07-11 stsp err = unlockerr;
7602 818c7501 2019-07-11 stsp return err;
7603 818c7501 2019-07-11 stsp }
7604 8641a332 2023-04-14 thomas
7605 8641a332 2023-04-14 thomas static const struct got_error *
7606 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7607 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7608 8641a332 2023-04-14 thomas struct got_repository *repo)
7609 8641a332 2023-04-14 thomas {
7610 8641a332 2023-04-14 thomas const struct got_error *err;
7611 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7612 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7613 8641a332 2023-04-14 thomas
7614 8641a332 2023-04-14 thomas if (id1) {
7615 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7616 8641a332 2023-04-14 thomas if (err)
7617 8641a332 2023-04-14 thomas goto done;
7618 8641a332 2023-04-14 thomas
7619 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7620 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7621 8641a332 2023-04-14 thomas if (err)
7622 8641a332 2023-04-14 thomas goto done;
7623 8641a332 2023-04-14 thomas }
7624 818c7501 2019-07-11 stsp
7625 8641a332 2023-04-14 thomas if (id2) {
7626 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7627 8641a332 2023-04-14 thomas if (err)
7628 8641a332 2023-04-14 thomas goto done;
7629 8641a332 2023-04-14 thomas
7630 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7631 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7632 8641a332 2023-04-14 thomas if (err)
7633 8641a332 2023-04-14 thomas goto done;
7634 8641a332 2023-04-14 thomas }
7635 8641a332 2023-04-14 thomas
7636 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7637 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7638 8641a332 2023-04-14 thomas if (err)
7639 8641a332 2023-04-14 thomas goto done;
7640 8641a332 2023-04-14 thomas done:
7641 8641a332 2023-04-14 thomas if (commit1)
7642 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7643 8641a332 2023-04-14 thomas if (commit2)
7644 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7645 8641a332 2023-04-14 thomas if (tree1)
7646 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7647 8641a332 2023-04-14 thomas if (tree2)
7648 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7649 8641a332 2023-04-14 thomas return err;
7650 8641a332 2023-04-14 thomas }
7651 8641a332 2023-04-14 thomas
7652 8641a332 2023-04-14 thomas static const struct got_error *
7653 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7654 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7655 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7656 8641a332 2023-04-14 thomas {
7657 8641a332 2023-04-14 thomas const struct got_error *err;
7658 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7659 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7660 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7661 8641a332 2023-04-14 thomas
7662 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7663 8641a332 2023-04-14 thomas
7664 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7665 8641a332 2023-04-14 thomas if (err)
7666 8641a332 2023-04-14 thomas goto done;
7667 8641a332 2023-04-14 thomas
7668 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7669 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7670 8641a332 2023-04-14 thomas
7671 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7672 8641a332 2023-04-14 thomas continue;
7673 8641a332 2023-04-14 thomas
7674 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7675 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7676 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7677 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7678 8641a332 2023-04-14 thomas goto done;
7679 8641a332 2023-04-14 thomas }
7680 8641a332 2023-04-14 thomas } else {
7681 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7682 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7683 8641a332 2023-04-14 thomas goto done;
7684 8641a332 2023-04-14 thomas }
7685 8641a332 2023-04-14 thomas
7686 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7687 8641a332 2023-04-14 thomas path_prefix, abspath);
7688 8641a332 2023-04-14 thomas if (err)
7689 8641a332 2023-04-14 thomas goto done;
7690 8641a332 2023-04-14 thomas free(abspath);
7691 8641a332 2023-04-14 thomas abspath = NULL;
7692 8641a332 2023-04-14 thomas }
7693 8641a332 2023-04-14 thomas
7694 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7695 8641a332 2023-04-14 thomas if (err)
7696 8641a332 2023-04-14 thomas goto done;
7697 8641a332 2023-04-14 thomas wt_path = NULL;
7698 8641a332 2023-04-14 thomas }
7699 8641a332 2023-04-14 thomas
7700 8641a332 2023-04-14 thomas done:
7701 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7702 8641a332 2023-04-14 thomas free(abspath);
7703 8641a332 2023-04-14 thomas free(wt_path);
7704 8641a332 2023-04-14 thomas return err;
7705 8641a332 2023-04-14 thomas }
7706 8641a332 2023-04-14 thomas
7707 8641a332 2023-04-14 thomas static const struct got_error *
7708 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7709 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7710 8641a332 2023-04-14 thomas struct got_repository *repo)
7711 8641a332 2023-04-14 thomas {
7712 8641a332 2023-04-14 thomas const struct got_error *err;
7713 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7714 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7715 8641a332 2023-04-14 thomas
7716 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7717 8641a332 2023-04-14 thomas if (err)
7718 8641a332 2023-04-14 thomas goto done;
7719 8641a332 2023-04-14 thomas
7720 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7721 8641a332 2023-04-14 thomas
7722 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7723 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7724 8641a332 2023-04-14 thomas if (err)
7725 8641a332 2023-04-14 thomas goto done;
7726 8641a332 2023-04-14 thomas done:
7727 8641a332 2023-04-14 thomas if (commit)
7728 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7729 8641a332 2023-04-14 thomas return err;
7730 8641a332 2023-04-14 thomas }
7731 8641a332 2023-04-14 thomas
7732 818c7501 2019-07-11 stsp const struct got_error *
7733 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7734 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7735 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7736 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7737 818c7501 2019-07-11 stsp {
7738 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7739 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7740 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7741 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7742 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7743 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7744 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7745 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7746 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7747 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7748 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7749 818c7501 2019-07-11 stsp
7750 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7751 8641a332 2023-04-14 thomas
7752 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7753 818c7501 2019-07-11 stsp if (err)
7754 818c7501 2019-07-11 stsp return err;
7755 8641a332 2023-04-14 thomas
7756 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7757 8641a332 2023-04-14 thomas if (err)
7758 8641a332 2023-04-14 thomas goto done;
7759 8641a332 2023-04-14 thomas
7760 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7761 8641a332 2023-04-14 thomas if (err)
7762 8641a332 2023-04-14 thomas goto done;
7763 8641a332 2023-04-14 thomas
7764 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7765 8641a332 2023-04-14 thomas if (err)
7766 8641a332 2023-04-14 thomas goto done;
7767 945f9229 2022-04-16 thomas
7768 8641a332 2023-04-14 thomas /*
7769 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7770 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7771 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7772 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7773 8641a332 2023-04-14 thomas */
7774 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7775 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7776 8641a332 2023-04-14 thomas if (err)
7777 8641a332 2023-04-14 thomas goto done;
7778 8641a332 2023-04-14 thomas
7779 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7780 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7781 818c7501 2019-07-11 stsp if (err)
7782 818c7501 2019-07-11 stsp goto done;
7783 818c7501 2019-07-11 stsp
7784 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7785 818c7501 2019-07-11 stsp if (err)
7786 818c7501 2019-07-11 stsp goto done;
7787 818c7501 2019-07-11 stsp
7788 818c7501 2019-07-11 stsp /*
7789 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7790 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7791 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7792 818c7501 2019-07-11 stsp */
7793 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7794 818c7501 2019-07-11 stsp if (err)
7795 818c7501 2019-07-11 stsp goto done;
7796 818c7501 2019-07-11 stsp
7797 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7798 25a102ed 2023-04-14 thomas if (err)
7799 25a102ed 2023-04-14 thomas goto done;
7800 25a102ed 2023-04-14 thomas
7801 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7802 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7803 818c7501 2019-07-11 stsp if (err)
7804 818c7501 2019-07-11 stsp goto done;
7805 818c7501 2019-07-11 stsp
7806 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7807 945f9229 2022-04-16 thomas worktree->path_prefix);
7808 ca355955 2019-07-12 stsp if (err)
7809 ca355955 2019-07-12 stsp goto done;
7810 ca355955 2019-07-12 stsp
7811 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7812 ca355955 2019-07-12 stsp if (err)
7813 ca355955 2019-07-12 stsp goto done;
7814 ca355955 2019-07-12 stsp
7815 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7816 818c7501 2019-07-11 stsp if (err)
7817 818c7501 2019-07-11 stsp goto done;
7818 818c7501 2019-07-11 stsp
7819 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7820 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7821 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7822 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7823 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7824 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7825 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7826 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7827 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7828 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7829 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7830 55bd499d 2019-07-12 stsp if (err)
7831 1f1abb7e 2019-08-08 stsp goto sync;
7832 55bd499d 2019-07-12 stsp
7833 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7834 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7835 ca355955 2019-07-12 stsp sync:
7836 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7837 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7838 ca355955 2019-07-12 stsp err = sync_err;
7839 818c7501 2019-07-11 stsp done:
7840 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7841 818c7501 2019-07-11 stsp got_ref_close(resolved);
7842 a3a2faf2 2019-07-12 stsp free(tree_id);
7843 818c7501 2019-07-11 stsp free(commit_id);
7844 8641a332 2023-04-14 thomas free(merged_commit_id);
7845 945f9229 2022-04-16 thomas if (commit)
7846 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7847 0ebf8283 2019-07-24 stsp if (fileindex)
7848 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7849 0ebf8283 2019-07-24 stsp free(fileindex_path);
7850 8641a332 2023-04-14 thomas free(commit_ref_name);
7851 8641a332 2023-04-14 thomas if (commit_ref)
7852 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7853 0ebf8283 2019-07-24 stsp
7854 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7855 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7856 0ebf8283 2019-07-24 stsp err = unlockerr;
7857 0ebf8283 2019-07-24 stsp return err;
7858 0ebf8283 2019-07-24 stsp }
7859 0ebf8283 2019-07-24 stsp
7860 0ebf8283 2019-07-24 stsp const struct got_error *
7861 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7862 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7863 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7864 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7865 0ebf8283 2019-07-24 stsp {
7866 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7867 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7868 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7869 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7870 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7871 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7872 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7873 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7874 0ebf8283 2019-07-24 stsp
7875 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7876 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7877 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7878 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7879 0ebf8283 2019-07-24 stsp
7880 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7881 0ebf8283 2019-07-24 stsp if (err)
7882 0ebf8283 2019-07-24 stsp return err;
7883 0ebf8283 2019-07-24 stsp
7884 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7885 0ebf8283 2019-07-24 stsp if (err)
7886 0ebf8283 2019-07-24 stsp goto done;
7887 0ebf8283 2019-07-24 stsp
7888 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7889 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7890 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7891 0ebf8283 2019-07-24 stsp &ok_arg);
7892 0ebf8283 2019-07-24 stsp if (err)
7893 0ebf8283 2019-07-24 stsp goto done;
7894 0ebf8283 2019-07-24 stsp
7895 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7896 0ebf8283 2019-07-24 stsp if (err)
7897 0ebf8283 2019-07-24 stsp goto done;
7898 0ebf8283 2019-07-24 stsp
7899 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7900 0ebf8283 2019-07-24 stsp if (err)
7901 0ebf8283 2019-07-24 stsp goto done;
7902 0ebf8283 2019-07-24 stsp
7903 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7904 0ebf8283 2019-07-24 stsp worktree);
7905 0ebf8283 2019-07-24 stsp if (err)
7906 0ebf8283 2019-07-24 stsp goto done;
7907 0ebf8283 2019-07-24 stsp
7908 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7909 0ebf8283 2019-07-24 stsp 0);
7910 0ebf8283 2019-07-24 stsp if (err)
7911 0ebf8283 2019-07-24 stsp goto done;
7912 0ebf8283 2019-07-24 stsp
7913 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7914 0ebf8283 2019-07-24 stsp if (err)
7915 0ebf8283 2019-07-24 stsp goto done;
7916 0ebf8283 2019-07-24 stsp
7917 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7918 0ebf8283 2019-07-24 stsp if (err)
7919 0ebf8283 2019-07-24 stsp goto done;
7920 0ebf8283 2019-07-24 stsp
7921 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7922 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7923 0ebf8283 2019-07-24 stsp if (err)
7924 0ebf8283 2019-07-24 stsp goto done;
7925 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7926 0ebf8283 2019-07-24 stsp if (err)
7927 0ebf8283 2019-07-24 stsp goto done;
7928 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7929 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7930 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7931 0ebf8283 2019-07-24 stsp goto done;
7932 0ebf8283 2019-07-24 stsp }
7933 0ebf8283 2019-07-24 stsp
7934 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7935 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7936 0ebf8283 2019-07-24 stsp if (err)
7937 0ebf8283 2019-07-24 stsp goto done;
7938 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7939 0ebf8283 2019-07-24 stsp if (err)
7940 0ebf8283 2019-07-24 stsp goto done;
7941 0ebf8283 2019-07-24 stsp
7942 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7943 0ebf8283 2019-07-24 stsp if (err)
7944 0ebf8283 2019-07-24 stsp goto done;
7945 0ebf8283 2019-07-24 stsp done:
7946 0ebf8283 2019-07-24 stsp free(fileindex_path);
7947 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7948 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7949 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7950 0ebf8283 2019-07-24 stsp if (wt_branch)
7951 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7952 0ebf8283 2019-07-24 stsp if (err) {
7953 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7954 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7955 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7956 0ebf8283 2019-07-24 stsp }
7957 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7958 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7959 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7960 0ebf8283 2019-07-24 stsp }
7961 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7962 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7963 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7964 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7965 3e3a69f1 2019-07-25 stsp }
7966 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7967 0ebf8283 2019-07-24 stsp }
7968 0ebf8283 2019-07-24 stsp return err;
7969 0ebf8283 2019-07-24 stsp }
7970 0ebf8283 2019-07-24 stsp
7971 0ebf8283 2019-07-24 stsp const struct got_error *
7972 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7973 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7974 0ebf8283 2019-07-24 stsp {
7975 3e3a69f1 2019-07-25 stsp if (fileindex)
7976 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7977 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7978 0ebf8283 2019-07-24 stsp }
7979 0ebf8283 2019-07-24 stsp
7980 0ebf8283 2019-07-24 stsp const struct got_error *
7981 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7982 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7983 0ebf8283 2019-07-24 stsp {
7984 0ebf8283 2019-07-24 stsp const struct got_error *err;
7985 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7986 0ebf8283 2019-07-24 stsp
7987 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7988 0ebf8283 2019-07-24 stsp if (err)
7989 0ebf8283 2019-07-24 stsp return err;
7990 0ebf8283 2019-07-24 stsp
7991 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7992 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7993 0ebf8283 2019-07-24 stsp return NULL;
7994 0ebf8283 2019-07-24 stsp }
7995 0ebf8283 2019-07-24 stsp
7996 0ebf8283 2019-07-24 stsp const struct got_error *
7997 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7998 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7999 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
8000 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8001 0ebf8283 2019-07-24 stsp {
8002 0ebf8283 2019-07-24 stsp const struct got_error *err;
8003 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
8004 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
8005 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
8006 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
8007 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
8008 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
8009 0ebf8283 2019-07-24 stsp
8010 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8011 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8012 d4b2e62d 2024-03-30 thomas *branch_ref = NULL;
8013 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8014 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8015 0ebf8283 2019-07-24 stsp
8016 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
8017 3e3a69f1 2019-07-25 stsp if (err)
8018 3e3a69f1 2019-07-25 stsp return err;
8019 3e3a69f1 2019-07-25 stsp
8020 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8021 3e3a69f1 2019-07-25 stsp if (err)
8022 f032f1f7 2019-08-04 stsp goto done;
8023 f032f1f7 2019-08-04 stsp
8024 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8025 f032f1f7 2019-08-04 stsp &have_staged_files);
8026 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
8027 f032f1f7 2019-08-04 stsp goto done;
8028 f032f1f7 2019-08-04 stsp if (have_staged_files) {
8029 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8030 3e3a69f1 2019-07-25 stsp goto done;
8031 f032f1f7 2019-08-04 stsp }
8032 3e3a69f1 2019-07-25 stsp
8033 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8034 0ebf8283 2019-07-24 stsp if (err)
8035 f032f1f7 2019-08-04 stsp goto done;
8036 0ebf8283 2019-07-24 stsp
8037 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8038 0ebf8283 2019-07-24 stsp if (err)
8039 0ebf8283 2019-07-24 stsp goto done;
8040 0ebf8283 2019-07-24 stsp
8041 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8042 0ebf8283 2019-07-24 stsp if (err)
8043 0ebf8283 2019-07-24 stsp goto done;
8044 0ebf8283 2019-07-24 stsp
8045 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8046 0ebf8283 2019-07-24 stsp worktree);
8047 0ebf8283 2019-07-24 stsp if (err)
8048 0ebf8283 2019-07-24 stsp goto done;
8049 0ebf8283 2019-07-24 stsp
8050 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
8051 0ebf8283 2019-07-24 stsp if (err)
8052 0ebf8283 2019-07-24 stsp goto done;
8053 0ebf8283 2019-07-24 stsp
8054 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8055 0ebf8283 2019-07-24 stsp if (err)
8056 0ebf8283 2019-07-24 stsp goto done;
8057 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
8058 0ebf8283 2019-07-24 stsp if (err)
8059 0ebf8283 2019-07-24 stsp goto done;
8060 0ebf8283 2019-07-24 stsp
8061 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
8062 0ebf8283 2019-07-24 stsp if (err)
8063 0ebf8283 2019-07-24 stsp goto done;
8064 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8065 0ebf8283 2019-07-24 stsp if (err)
8066 0ebf8283 2019-07-24 stsp goto done;
8067 0ebf8283 2019-07-24 stsp
8068 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8069 0ebf8283 2019-07-24 stsp if (err)
8070 0ebf8283 2019-07-24 stsp goto done;
8071 0ebf8283 2019-07-24 stsp done:
8072 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8073 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8074 3e3a69f1 2019-07-25 stsp free(fileindex_path);
8075 0ebf8283 2019-07-24 stsp if (commit_ref)
8076 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
8077 0ebf8283 2019-07-24 stsp if (base_commit_ref)
8078 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
8079 0ebf8283 2019-07-24 stsp if (err) {
8080 0ebf8283 2019-07-24 stsp free(*commit_id);
8081 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8082 0ebf8283 2019-07-24 stsp free(*base_commit_id);
8083 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8084 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
8085 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
8086 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8087 0ebf8283 2019-07-24 stsp }
8088 d4b2e62d 2024-03-30 thomas if (*branch_ref) {
8089 d4b2e62d 2024-03-30 thomas got_ref_close(*branch_ref);
8090 d4b2e62d 2024-03-30 thomas *branch_ref = NULL;
8091 d4b2e62d 2024-03-30 thomas }
8092 3e3a69f1 2019-07-25 stsp if (*fileindex) {
8093 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
8094 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8095 3e3a69f1 2019-07-25 stsp }
8096 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
8097 cd634f2d 2024-03-30 thomas }
8098 cd634f2d 2024-03-30 thomas return err;
8099 cd634f2d 2024-03-30 thomas }
8100 cd634f2d 2024-03-30 thomas
8101 cd634f2d 2024-03-30 thomas const struct got_error *
8102 cd634f2d 2024-03-30 thomas got_worktree_histedit_info(char **branch_name,
8103 cd634f2d 2024-03-30 thomas struct got_worktree *worktree, struct got_repository *repo)
8104 cd634f2d 2024-03-30 thomas {
8105 cd634f2d 2024-03-30 thomas const struct got_error *err;
8106 cd634f2d 2024-03-30 thomas struct got_reference *branch_ref = NULL;
8107 cd634f2d 2024-03-30 thomas char *branch_ref_name = NULL;
8108 cd634f2d 2024-03-30 thomas
8109 cd634f2d 2024-03-30 thomas *branch_name = NULL;
8110 cd634f2d 2024-03-30 thomas
8111 cd634f2d 2024-03-30 thomas err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8112 cd634f2d 2024-03-30 thomas if (err)
8113 cd634f2d 2024-03-30 thomas goto done;
8114 cd634f2d 2024-03-30 thomas
8115 cd634f2d 2024-03-30 thomas err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
8116 cd634f2d 2024-03-30 thomas if (err)
8117 cd634f2d 2024-03-30 thomas goto done;
8118 cd634f2d 2024-03-30 thomas
8119 cd634f2d 2024-03-30 thomas if (!got_ref_is_symbolic(branch_ref)) {
8120 cd634f2d 2024-03-30 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8121 cd634f2d 2024-03-30 thomas "%s is not a symbolic reference",
8122 cd634f2d 2024-03-30 thomas got_ref_get_name(branch_ref));
8123 cd634f2d 2024-03-30 thomas goto done;
8124 cd634f2d 2024-03-30 thomas }
8125 cd634f2d 2024-03-30 thomas
8126 cd634f2d 2024-03-30 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8127 cd634f2d 2024-03-30 thomas if (*branch_name == NULL) {
8128 cd634f2d 2024-03-30 thomas err = got_error_from_errno("strdup");
8129 cd634f2d 2024-03-30 thomas goto done;
8130 0ebf8283 2019-07-24 stsp }
8131 cd634f2d 2024-03-30 thomas done:
8132 cd634f2d 2024-03-30 thomas free(branch_ref_name);
8133 cd634f2d 2024-03-30 thomas if (branch_ref)
8134 cd634f2d 2024-03-30 thomas got_ref_close(branch_ref);
8135 cd634f2d 2024-03-30 thomas if (err) {
8136 cd634f2d 2024-03-30 thomas free(*branch_name);
8137 cd634f2d 2024-03-30 thomas *branch_name = NULL;
8138 cd634f2d 2024-03-30 thomas }
8139 0ebf8283 2019-07-24 stsp return err;
8140 0ebf8283 2019-07-24 stsp }
8141 0ebf8283 2019-07-24 stsp
8142 0ebf8283 2019-07-24 stsp static const struct got_error *
8143 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8144 0ebf8283 2019-07-24 stsp {
8145 0ebf8283 2019-07-24 stsp const struct got_error *err;
8146 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8147 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
8148 0ebf8283 2019-07-24 stsp
8149 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8150 0ebf8283 2019-07-24 stsp if (err)
8151 0ebf8283 2019-07-24 stsp goto done;
8152 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
8153 0ebf8283 2019-07-24 stsp if (err)
8154 0ebf8283 2019-07-24 stsp goto done;
8155 0ebf8283 2019-07-24 stsp
8156 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8157 0ebf8283 2019-07-24 stsp worktree);
8158 0ebf8283 2019-07-24 stsp if (err)
8159 0ebf8283 2019-07-24 stsp goto done;
8160 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
8161 0ebf8283 2019-07-24 stsp if (err)
8162 0ebf8283 2019-07-24 stsp goto done;
8163 0ebf8283 2019-07-24 stsp
8164 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8165 0ebf8283 2019-07-24 stsp if (err)
8166 0ebf8283 2019-07-24 stsp goto done;
8167 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8168 0ebf8283 2019-07-24 stsp if (err)
8169 0ebf8283 2019-07-24 stsp goto done;
8170 0ebf8283 2019-07-24 stsp
8171 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8172 0ebf8283 2019-07-24 stsp if (err)
8173 0ebf8283 2019-07-24 stsp goto done;
8174 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8175 0ebf8283 2019-07-24 stsp if (err)
8176 0ebf8283 2019-07-24 stsp goto done;
8177 0ebf8283 2019-07-24 stsp done:
8178 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8179 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8180 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8181 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8182 0ebf8283 2019-07-24 stsp return err;
8183 0ebf8283 2019-07-24 stsp }
8184 0ebf8283 2019-07-24 stsp
8185 0ebf8283 2019-07-24 stsp const struct got_error *
8186 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8187 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8188 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8189 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8190 0ebf8283 2019-07-24 stsp {
8191 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8192 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8193 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8194 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8195 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8196 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8197 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8198 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8199 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8200 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8201 0ebf8283 2019-07-24 stsp
8202 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8203 8641a332 2023-04-14 thomas
8204 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8205 0ebf8283 2019-07-24 stsp if (err)
8206 0ebf8283 2019-07-24 stsp return err;
8207 945f9229 2022-04-16 thomas
8208 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8209 8641a332 2023-04-14 thomas if (err)
8210 8641a332 2023-04-14 thomas goto done;
8211 8641a332 2023-04-14 thomas
8212 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8213 8641a332 2023-04-14 thomas if (err) {
8214 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
8215 8641a332 2023-04-14 thomas goto done;
8216 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
8217 8641a332 2023-04-14 thomas commit_ref = NULL;
8218 8641a332 2023-04-14 thomas }
8219 8641a332 2023-04-14 thomas
8220 8641a332 2023-04-14 thomas if (commit_ref) {
8221 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8222 8641a332 2023-04-14 thomas if (err)
8223 8641a332 2023-04-14 thomas goto done;
8224 8641a332 2023-04-14 thomas
8225 8641a332 2023-04-14 thomas /*
8226 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8227 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8228 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
8229 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
8230 8641a332 2023-04-14 thomas */
8231 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8232 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8233 8641a332 2023-04-14 thomas if (err)
8234 8641a332 2023-04-14 thomas goto done;
8235 8641a332 2023-04-14 thomas }
8236 8641a332 2023-04-14 thomas
8237 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8238 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8239 0ebf8283 2019-07-24 stsp if (err)
8240 0ebf8283 2019-07-24 stsp goto done;
8241 0ebf8283 2019-07-24 stsp
8242 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8243 0ebf8283 2019-07-24 stsp if (err)
8244 0ebf8283 2019-07-24 stsp goto done;
8245 0ebf8283 2019-07-24 stsp
8246 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8247 0ebf8283 2019-07-24 stsp if (err)
8248 0ebf8283 2019-07-24 stsp goto done;
8249 0ebf8283 2019-07-24 stsp
8250 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
8251 25a102ed 2023-04-14 thomas worktree->base_commit_id);
8252 25a102ed 2023-04-14 thomas if (err)
8253 25a102ed 2023-04-14 thomas goto done;
8254 25a102ed 2023-04-14 thomas
8255 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8256 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8257 0ebf8283 2019-07-24 stsp if (err)
8258 0ebf8283 2019-07-24 stsp goto done;
8259 0ebf8283 2019-07-24 stsp
8260 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8261 0ebf8283 2019-07-24 stsp if (err)
8262 0ebf8283 2019-07-24 stsp goto done;
8263 0ebf8283 2019-07-24 stsp
8264 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8265 0ebf8283 2019-07-24 stsp if (err)
8266 0ebf8283 2019-07-24 stsp goto done;
8267 0ebf8283 2019-07-24 stsp
8268 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8269 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8270 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8271 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8272 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8273 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8274 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8275 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
8276 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8277 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8278 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8279 0ebf8283 2019-07-24 stsp if (err)
8280 1f1abb7e 2019-08-08 stsp goto sync;
8281 0ebf8283 2019-07-24 stsp
8282 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8283 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8284 0ebf8283 2019-07-24 stsp sync:
8285 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8286 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8287 0ebf8283 2019-07-24 stsp err = sync_err;
8288 0ebf8283 2019-07-24 stsp done:
8289 8641a332 2023-04-14 thomas if (resolved)
8290 8641a332 2023-04-14 thomas got_ref_close(resolved);
8291 8641a332 2023-04-14 thomas if (commit_ref)
8292 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8293 8641a332 2023-04-14 thomas free(merged_commit_id);
8294 0ebf8283 2019-07-24 stsp free(tree_id);
8295 818c7501 2019-07-11 stsp free(fileindex_path);
8296 8641a332 2023-04-14 thomas free(commit_ref_name);
8297 818c7501 2019-07-11 stsp
8298 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8299 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8300 818c7501 2019-07-11 stsp err = unlockerr;
8301 818c7501 2019-07-11 stsp return err;
8302 818c7501 2019-07-11 stsp }
8303 0ebf8283 2019-07-24 stsp
8304 0ebf8283 2019-07-24 stsp const struct got_error *
8305 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8306 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8307 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8308 0ebf8283 2019-07-24 stsp {
8309 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8310 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8311 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8312 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8313 0ebf8283 2019-07-24 stsp
8314 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8315 0ebf8283 2019-07-24 stsp if (err)
8316 0ebf8283 2019-07-24 stsp return err;
8317 0ebf8283 2019-07-24 stsp
8318 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8319 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8320 e600f124 2021-03-21 stsp if (err)
8321 e600f124 2021-03-21 stsp goto done;
8322 e600f124 2021-03-21 stsp
8323 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8324 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8325 0ebf8283 2019-07-24 stsp if (err)
8326 0ebf8283 2019-07-24 stsp goto done;
8327 0ebf8283 2019-07-24 stsp
8328 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8329 0ebf8283 2019-07-24 stsp if (err)
8330 0ebf8283 2019-07-24 stsp goto done;
8331 0ebf8283 2019-07-24 stsp
8332 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8333 0ebf8283 2019-07-24 stsp if (err)
8334 0ebf8283 2019-07-24 stsp goto done;
8335 0ebf8283 2019-07-24 stsp
8336 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8337 0ebf8283 2019-07-24 stsp if (err)
8338 0ebf8283 2019-07-24 stsp goto done;
8339 0ebf8283 2019-07-24 stsp
8340 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8341 a615e0e7 2020-12-16 stsp if (err)
8342 a615e0e7 2020-12-16 stsp goto done;
8343 a615e0e7 2020-12-16 stsp
8344 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8345 a615e0e7 2020-12-16 stsp if (err)
8346 a615e0e7 2020-12-16 stsp goto done;
8347 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8348 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8349 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8350 a615e0e7 2020-12-16 stsp err = sync_err;
8351 0ebf8283 2019-07-24 stsp done:
8352 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8353 a615e0e7 2020-12-16 stsp free(fileindex_path);
8354 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8355 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8356 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8357 0ebf8283 2019-07-24 stsp err = unlockerr;
8358 0ebf8283 2019-07-24 stsp return err;
8359 0ebf8283 2019-07-24 stsp }
8360 0ebf8283 2019-07-24 stsp
8361 0ebf8283 2019-07-24 stsp const struct got_error *
8362 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8363 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8364 0ebf8283 2019-07-24 stsp {
8365 0ebf8283 2019-07-24 stsp const struct got_error *err;
8366 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8367 0ebf8283 2019-07-24 stsp
8368 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8369 0ebf8283 2019-07-24 stsp if (err)
8370 0ebf8283 2019-07-24 stsp return err;
8371 0ebf8283 2019-07-24 stsp
8372 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8373 0ebf8283 2019-07-24 stsp if (err)
8374 0ebf8283 2019-07-24 stsp goto done;
8375 0ebf8283 2019-07-24 stsp
8376 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8377 0ebf8283 2019-07-24 stsp done:
8378 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8379 2822a352 2019-10-15 stsp return err;
8380 2822a352 2019-10-15 stsp }
8381 2822a352 2019-10-15 stsp
8382 2822a352 2019-10-15 stsp const struct got_error *
8383 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8384 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8385 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8386 2822a352 2019-10-15 stsp struct got_repository *repo)
8387 2822a352 2019-10-15 stsp {
8388 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8389 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8390 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8391 2822a352 2019-10-15 stsp
8392 2822a352 2019-10-15 stsp *fileindex = NULL;
8393 2822a352 2019-10-15 stsp *branch_ref = NULL;
8394 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8395 2822a352 2019-10-15 stsp
8396 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8397 2822a352 2019-10-15 stsp if (err)
8398 2822a352 2019-10-15 stsp return err;
8399 2822a352 2019-10-15 stsp
8400 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8401 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8402 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8403 2822a352 2019-10-15 stsp "update -b or different branch name required");
8404 2822a352 2019-10-15 stsp goto done;
8405 2822a352 2019-10-15 stsp }
8406 2822a352 2019-10-15 stsp
8407 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8408 2822a352 2019-10-15 stsp if (err)
8409 2822a352 2019-10-15 stsp goto done;
8410 2822a352 2019-10-15 stsp
8411 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8412 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8413 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8414 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8415 2822a352 2019-10-15 stsp &ok_arg);
8416 2822a352 2019-10-15 stsp if (err)
8417 2822a352 2019-10-15 stsp goto done;
8418 2822a352 2019-10-15 stsp
8419 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8420 2822a352 2019-10-15 stsp if (err)
8421 2822a352 2019-10-15 stsp goto done;
8422 2822a352 2019-10-15 stsp
8423 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8424 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8425 2822a352 2019-10-15 stsp done:
8426 2822a352 2019-10-15 stsp if (err) {
8427 2822a352 2019-10-15 stsp if (*branch_ref) {
8428 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8429 2822a352 2019-10-15 stsp *branch_ref = NULL;
8430 2822a352 2019-10-15 stsp }
8431 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8432 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8433 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8434 2822a352 2019-10-15 stsp }
8435 2822a352 2019-10-15 stsp if (*fileindex) {
8436 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8437 2822a352 2019-10-15 stsp *fileindex = NULL;
8438 2822a352 2019-10-15 stsp }
8439 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8440 2822a352 2019-10-15 stsp }
8441 0cb83759 2019-08-03 stsp return err;
8442 0cb83759 2019-08-03 stsp }
8443 0cb83759 2019-08-03 stsp
8444 2822a352 2019-10-15 stsp const struct got_error *
8445 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8446 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8447 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8448 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8449 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8450 2822a352 2019-10-15 stsp {
8451 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8452 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8453 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8454 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8455 2822a352 2019-10-15 stsp
8456 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8457 2822a352 2019-10-15 stsp if (err)
8458 2822a352 2019-10-15 stsp goto done;
8459 2822a352 2019-10-15 stsp
8460 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8461 2822a352 2019-10-15 stsp if (err)
8462 2822a352 2019-10-15 stsp goto done;
8463 2822a352 2019-10-15 stsp
8464 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8465 945f9229 2022-04-16 thomas if (err)
8466 945f9229 2022-04-16 thomas goto done;
8467 945f9229 2022-04-16 thomas
8468 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8469 2822a352 2019-10-15 stsp worktree->path_prefix);
8470 2822a352 2019-10-15 stsp if (err)
8471 2822a352 2019-10-15 stsp goto done;
8472 2822a352 2019-10-15 stsp
8473 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8474 2822a352 2019-10-15 stsp if (err)
8475 2822a352 2019-10-15 stsp goto done;
8476 2822a352 2019-10-15 stsp
8477 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8478 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8479 2822a352 2019-10-15 stsp if (err)
8480 2822a352 2019-10-15 stsp goto sync;
8481 2822a352 2019-10-15 stsp
8482 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8483 2822a352 2019-10-15 stsp if (err)
8484 2822a352 2019-10-15 stsp goto sync;
8485 2822a352 2019-10-15 stsp
8486 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8487 6e210706 2021-01-22 stsp if (err)
8488 6e210706 2021-01-22 stsp goto sync;
8489 6e210706 2021-01-22 stsp
8490 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8491 2822a352 2019-10-15 stsp sync:
8492 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8493 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8494 2822a352 2019-10-15 stsp err = sync_err;
8495 2822a352 2019-10-15 stsp
8496 2822a352 2019-10-15 stsp done:
8497 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8498 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8499 2822a352 2019-10-15 stsp err = unlockerr;
8500 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8501 2822a352 2019-10-15 stsp
8502 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8503 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8504 2822a352 2019-10-15 stsp err = unlockerr;
8505 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8506 2822a352 2019-10-15 stsp
8507 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8508 2822a352 2019-10-15 stsp free(fileindex_path);
8509 2822a352 2019-10-15 stsp free(tree_id);
8510 945f9229 2022-04-16 thomas if (commit)
8511 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8512 2822a352 2019-10-15 stsp
8513 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8514 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8515 2822a352 2019-10-15 stsp err = unlockerr;
8516 2822a352 2019-10-15 stsp return err;
8517 2822a352 2019-10-15 stsp }
8518 2822a352 2019-10-15 stsp
8519 2822a352 2019-10-15 stsp const struct got_error *
8520 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8521 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8522 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8523 2822a352 2019-10-15 stsp {
8524 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8525 8b692cd0 2019-10-21 stsp
8526 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8527 8b692cd0 2019-10-21 stsp
8528 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8529 8b692cd0 2019-10-21 stsp
8530 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8531 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8532 8b692cd0 2019-10-21 stsp err = unlockerr;
8533 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8534 8b692cd0 2019-10-21 stsp
8535 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8536 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8537 8b692cd0 2019-10-21 stsp err = unlockerr;
8538 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8539 10604dce 2021-09-24 thomas
8540 10604dce 2021-09-24 thomas return err;
8541 10604dce 2021-09-24 thomas }
8542 10604dce 2021-09-24 thomas
8543 10604dce 2021-09-24 thomas const struct got_error *
8544 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8545 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8546 10604dce 2021-09-24 thomas {
8547 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8548 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8549 10604dce 2021-09-24 thomas
8550 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8551 10604dce 2021-09-24 thomas if (err)
8552 10604dce 2021-09-24 thomas goto done;
8553 8b692cd0 2019-10-21 stsp
8554 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8555 10604dce 2021-09-24 thomas
8556 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8557 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8558 10604dce 2021-09-24 thomas err = sync_err;
8559 10604dce 2021-09-24 thomas done:
8560 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8561 10604dce 2021-09-24 thomas free(fileindex_path);
8562 8b692cd0 2019-10-21 stsp return err;
8563 2822a352 2019-10-15 stsp }
8564 2822a352 2019-10-15 stsp
8565 10604dce 2021-09-24 thomas static const struct got_error *
8566 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8567 10604dce 2021-09-24 thomas {
8568 10604dce 2021-09-24 thomas const struct got_error *err;
8569 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8570 10604dce 2021-09-24 thomas
8571 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8572 10604dce 2021-09-24 thomas if (err)
8573 10604dce 2021-09-24 thomas goto done;
8574 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8575 10604dce 2021-09-24 thomas if (err)
8576 10604dce 2021-09-24 thomas goto done;
8577 10604dce 2021-09-24 thomas
8578 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8579 10604dce 2021-09-24 thomas if (err)
8580 10604dce 2021-09-24 thomas goto done;
8581 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8582 10604dce 2021-09-24 thomas if (err)
8583 10604dce 2021-09-24 thomas goto done;
8584 10604dce 2021-09-24 thomas
8585 10604dce 2021-09-24 thomas done:
8586 10604dce 2021-09-24 thomas free(branch_refname);
8587 10604dce 2021-09-24 thomas free(commit_refname);
8588 10604dce 2021-09-24 thomas return err;
8589 10604dce 2021-09-24 thomas }
8590 10604dce 2021-09-24 thomas
8591 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8592 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8593 10604dce 2021-09-24 thomas const char *branch_name;
8594 10604dce 2021-09-24 thomas };
8595 10604dce 2021-09-24 thomas
8596 10604dce 2021-09-24 thomas static const struct got_error *
8597 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8598 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8599 10604dce 2021-09-24 thomas {
8600 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8601 10604dce 2021-09-24 thomas
8602 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8603 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8604 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8605 10604dce 2021-09-24 thomas
8606 10604dce 2021-09-24 thomas return NULL;
8607 10604dce 2021-09-24 thomas }
8608 10604dce 2021-09-24 thomas
8609 10604dce 2021-09-24 thomas
8610 10604dce 2021-09-24 thomas const struct got_error *
8611 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8612 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8613 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8614 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8615 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8616 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8617 10604dce 2021-09-24 thomas {
8618 10604dce 2021-09-24 thomas const struct got_error *err;
8619 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8620 6ebb2263 2023-07-26 thomas struct check_mixed_commits_args cma;
8621 10604dce 2021-09-24 thomas
8622 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8623 10604dce 2021-09-24 thomas if (err)
8624 10604dce 2021-09-24 thomas goto done;
8625 10604dce 2021-09-24 thomas
8626 6ebb2263 2023-07-26 thomas cma.worktree = worktree;
8627 6ebb2263 2023-07-26 thomas cma.cancel_cb = cancel_cb;
8628 6ebb2263 2023-07-26 thomas cma.cancel_arg = cancel_arg;
8629 6ebb2263 2023-07-26 thomas
8630 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8631 d1c85b42 2023-07-26 thomas &cma);
8632 10604dce 2021-09-24 thomas if (err)
8633 10604dce 2021-09-24 thomas goto done;
8634 10604dce 2021-09-24 thomas
8635 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8636 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8637 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8638 10604dce 2021-09-24 thomas done:
8639 10604dce 2021-09-24 thomas free(fileindex_path);
8640 10604dce 2021-09-24 thomas return err;
8641 10604dce 2021-09-24 thomas }
8642 10604dce 2021-09-24 thomas
8643 10604dce 2021-09-24 thomas const struct got_error *
8644 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8645 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8646 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8647 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8648 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8649 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8650 ae1e948a 2021-09-28 thomas
8651 10604dce 2021-09-24 thomas {
8652 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8653 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8654 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8655 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8656 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8657 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8658 10604dce 2021-09-24 thomas int have_staged_files = 0;
8659 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8660 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8661 10604dce 2021-09-24 thomas
8662 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8663 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8664 10604dce 2021-09-24 thomas
8665 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8666 10604dce 2021-09-24 thomas
8667 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8668 10604dce 2021-09-24 thomas if (err)
8669 10604dce 2021-09-24 thomas goto done;
8670 10604dce 2021-09-24 thomas
8671 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8672 10604dce 2021-09-24 thomas if (err)
8673 10604dce 2021-09-24 thomas goto done;
8674 10604dce 2021-09-24 thomas
8675 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8676 10604dce 2021-09-24 thomas if (err)
8677 10604dce 2021-09-24 thomas goto done;
8678 10604dce 2021-09-24 thomas
8679 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8680 10604dce 2021-09-24 thomas &have_staged_files);
8681 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8682 10604dce 2021-09-24 thomas goto done;
8683 10604dce 2021-09-24 thomas if (have_staged_files) {
8684 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8685 10604dce 2021-09-24 thomas goto done;
8686 10604dce 2021-09-24 thomas }
8687 10604dce 2021-09-24 thomas
8688 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8689 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8690 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8691 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8692 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8693 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8694 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8695 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8696 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8697 10604dce 2021-09-24 thomas if (err)
8698 10604dce 2021-09-24 thomas goto done;
8699 10604dce 2021-09-24 thomas
8700 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8701 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8702 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8703 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8704 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8705 10604dce 2021-09-24 thomas if (err)
8706 10604dce 2021-09-24 thomas goto done;
8707 10604dce 2021-09-24 thomas
8708 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8709 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8710 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8711 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8712 10604dce 2021-09-24 thomas err = sync_err;
8713 10604dce 2021-09-24 thomas done:
8714 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8715 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8716 21c2d8be 2023-01-10 thomas
8717 10604dce 2021-09-24 thomas free_commitable(ct);
8718 10604dce 2021-09-24 thomas }
8719 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8720 10604dce 2021-09-24 thomas free(fileindex_path);
8721 10604dce 2021-09-24 thomas return err;
8722 10604dce 2021-09-24 thomas }
8723 10604dce 2021-09-24 thomas
8724 10604dce 2021-09-24 thomas const struct got_error *
8725 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8726 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8727 10604dce 2021-09-24 thomas {
8728 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8729 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8730 10604dce 2021-09-24 thomas
8731 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8732 10604dce 2021-09-24 thomas if (err)
8733 10604dce 2021-09-24 thomas goto done;
8734 10604dce 2021-09-24 thomas
8735 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8736 10604dce 2021-09-24 thomas if (err)
8737 10604dce 2021-09-24 thomas goto done;
8738 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8739 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8740 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8741 10604dce 2021-09-24 thomas err = sync_err;
8742 10604dce 2021-09-24 thomas done:
8743 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8744 10604dce 2021-09-24 thomas free(fileindex_path);
8745 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8746 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8747 10604dce 2021-09-24 thomas err = unlockerr;
8748 10604dce 2021-09-24 thomas return err;
8749 10604dce 2021-09-24 thomas }
8750 10604dce 2021-09-24 thomas
8751 10604dce 2021-09-24 thomas const struct got_error *
8752 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8753 10604dce 2021-09-24 thomas struct got_repository *repo)
8754 10604dce 2021-09-24 thomas {
8755 10604dce 2021-09-24 thomas const struct got_error *err;
8756 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8757 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8758 10604dce 2021-09-24 thomas
8759 10604dce 2021-09-24 thomas *in_progress = 0;
8760 10604dce 2021-09-24 thomas
8761 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8762 10604dce 2021-09-24 thomas if (err)
8763 10604dce 2021-09-24 thomas return err;
8764 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8765 dfe86d1f 2021-09-24 thomas free(branch_refname);
8766 10604dce 2021-09-24 thomas if (err) {
8767 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8768 10604dce 2021-09-24 thomas return err;
8769 10604dce 2021-09-24 thomas } else
8770 10604dce 2021-09-24 thomas *in_progress = 1;
8771 10604dce 2021-09-24 thomas
8772 10604dce 2021-09-24 thomas return NULL;
8773 10604dce 2021-09-24 thomas }
8774 10604dce 2021-09-24 thomas
8775 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8776 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8777 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8778 10604dce 2021-09-24 thomas {
8779 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8780 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8781 2b72f32d 2023-06-22 thomas struct got_reference *wt_branch = NULL;
8782 2b72f32d 2023-06-22 thomas struct got_object_id *wt_branch_tip = NULL;
8783 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8784 10604dce 2021-09-24 thomas
8785 10604dce 2021-09-24 thomas *fileindex = NULL;
8786 10604dce 2021-09-24 thomas
8787 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8788 10604dce 2021-09-24 thomas if (err)
8789 10604dce 2021-09-24 thomas return err;
8790 10604dce 2021-09-24 thomas
8791 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8792 10604dce 2021-09-24 thomas if (err)
8793 10604dce 2021-09-24 thomas goto done;
8794 10604dce 2021-09-24 thomas
8795 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8796 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8797 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8798 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8799 10604dce 2021-09-24 thomas &ok_arg);
8800 10604dce 2021-09-24 thomas if (err)
8801 10604dce 2021-09-24 thomas goto done;
8802 10604dce 2021-09-24 thomas
8803 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8804 10604dce 2021-09-24 thomas 0);
8805 10604dce 2021-09-24 thomas if (err)
8806 10604dce 2021-09-24 thomas goto done;
8807 10604dce 2021-09-24 thomas
8808 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8809 10604dce 2021-09-24 thomas if (err)
8810 10604dce 2021-09-24 thomas goto done;
8811 10604dce 2021-09-24 thomas
8812 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8813 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8814 10604dce 2021-09-24 thomas goto done;
8815 10604dce 2021-09-24 thomas }
8816 2b72f32d 2023-06-22 thomas
8817 2b72f32d 2023-06-22 thomas done:
8818 2b72f32d 2023-06-22 thomas free(fileindex_path);
8819 2b72f32d 2023-06-22 thomas if (wt_branch)
8820 2b72f32d 2023-06-22 thomas got_ref_close(wt_branch);
8821 2b72f32d 2023-06-22 thomas free(wt_branch_tip);
8822 2b72f32d 2023-06-22 thomas if (err) {
8823 2b72f32d 2023-06-22 thomas if (*fileindex) {
8824 2b72f32d 2023-06-22 thomas got_fileindex_free(*fileindex);
8825 2b72f32d 2023-06-22 thomas *fileindex = NULL;
8826 2b72f32d 2023-06-22 thomas }
8827 2b72f32d 2023-06-22 thomas lock_worktree(worktree, LOCK_SH);
8828 2b72f32d 2023-06-22 thomas }
8829 2b72f32d 2023-06-22 thomas return err;
8830 2b72f32d 2023-06-22 thomas }
8831 10604dce 2021-09-24 thomas
8832 2b72f32d 2023-06-22 thomas const struct got_error *got_worktree_merge_write_refs(
8833 2b72f32d 2023-06-22 thomas struct got_worktree *worktree, struct got_reference *branch,
8834 2b72f32d 2023-06-22 thomas struct got_repository *repo)
8835 2b72f32d 2023-06-22 thomas {
8836 2b72f32d 2023-06-22 thomas const struct got_error *err = NULL;
8837 2b72f32d 2023-06-22 thomas char *branch_refname = NULL, *commit_refname = NULL;
8838 2b72f32d 2023-06-22 thomas struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8839 2b72f32d 2023-06-22 thomas struct got_object_id *branch_tip = NULL;
8840 2b72f32d 2023-06-22 thomas
8841 2b72f32d 2023-06-22 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8842 2b72f32d 2023-06-22 thomas if (err)
8843 2b72f32d 2023-06-22 thomas return err;
8844 2b72f32d 2023-06-22 thomas
8845 2b72f32d 2023-06-22 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8846 2b72f32d 2023-06-22 thomas if (err)
8847 2b72f32d 2023-06-22 thomas return err;
8848 2b72f32d 2023-06-22 thomas
8849 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8850 10604dce 2021-09-24 thomas if (err)
8851 10604dce 2021-09-24 thomas goto done;
8852 10604dce 2021-09-24 thomas
8853 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8854 10604dce 2021-09-24 thomas if (err)
8855 10604dce 2021-09-24 thomas goto done;
8856 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8857 10604dce 2021-09-24 thomas if (err)
8858 10604dce 2021-09-24 thomas goto done;
8859 10604dce 2021-09-24 thomas
8860 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8861 10604dce 2021-09-24 thomas if (err)
8862 10604dce 2021-09-24 thomas goto done;
8863 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8864 10604dce 2021-09-24 thomas if (err)
8865 10604dce 2021-09-24 thomas goto done;
8866 10604dce 2021-09-24 thomas
8867 10604dce 2021-09-24 thomas done:
8868 10604dce 2021-09-24 thomas free(branch_refname);
8869 10604dce 2021-09-24 thomas free(commit_refname);
8870 10604dce 2021-09-24 thomas if (branch_ref)
8871 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8872 10604dce 2021-09-24 thomas if (commit_ref)
8873 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8874 2b72f32d 2023-06-22 thomas free(branch_tip);
8875 10604dce 2021-09-24 thomas return err;
8876 10604dce 2021-09-24 thomas }
8877 10604dce 2021-09-24 thomas
8878 10604dce 2021-09-24 thomas const struct got_error *
8879 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8880 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8881 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8882 10604dce 2021-09-24 thomas {
8883 10604dce 2021-09-24 thomas const struct got_error *err;
8884 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8885 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8886 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8887 10604dce 2021-09-24 thomas int have_staged_files = 0;
8888 10604dce 2021-09-24 thomas
8889 10604dce 2021-09-24 thomas *branch_name = NULL;
8890 10604dce 2021-09-24 thomas *branch_tip = NULL;
8891 10604dce 2021-09-24 thomas *fileindex = NULL;
8892 10604dce 2021-09-24 thomas
8893 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8894 10604dce 2021-09-24 thomas if (err)
8895 10604dce 2021-09-24 thomas return err;
8896 10604dce 2021-09-24 thomas
8897 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8898 10604dce 2021-09-24 thomas if (err)
8899 10604dce 2021-09-24 thomas goto done;
8900 10604dce 2021-09-24 thomas
8901 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8902 10604dce 2021-09-24 thomas &have_staged_files);
8903 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8904 10604dce 2021-09-24 thomas goto done;
8905 10604dce 2021-09-24 thomas if (have_staged_files) {
8906 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8907 10604dce 2021-09-24 thomas goto done;
8908 10604dce 2021-09-24 thomas }
8909 10604dce 2021-09-24 thomas
8910 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8911 10604dce 2021-09-24 thomas if (err)
8912 10604dce 2021-09-24 thomas goto done;
8913 10604dce 2021-09-24 thomas
8914 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8915 10604dce 2021-09-24 thomas if (err)
8916 10604dce 2021-09-24 thomas goto done;
8917 10604dce 2021-09-24 thomas
8918 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8919 10604dce 2021-09-24 thomas if (err)
8920 10604dce 2021-09-24 thomas goto done;
8921 10604dce 2021-09-24 thomas
8922 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8923 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8924 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8925 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8926 10604dce 2021-09-24 thomas goto done;
8927 10604dce 2021-09-24 thomas }
8928 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8929 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8930 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8931 10604dce 2021-09-24 thomas goto done;
8932 10604dce 2021-09-24 thomas }
8933 10604dce 2021-09-24 thomas
8934 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8935 10604dce 2021-09-24 thomas if (err)
8936 10604dce 2021-09-24 thomas goto done;
8937 10604dce 2021-09-24 thomas
8938 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8939 10604dce 2021-09-24 thomas if (err)
8940 10604dce 2021-09-24 thomas goto done;
8941 10604dce 2021-09-24 thomas done:
8942 10604dce 2021-09-24 thomas free(commit_refname);
8943 10604dce 2021-09-24 thomas free(branch_refname);
8944 10604dce 2021-09-24 thomas free(fileindex_path);
8945 10604dce 2021-09-24 thomas if (commit_ref)
8946 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8947 10604dce 2021-09-24 thomas if (branch_ref)
8948 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8949 10604dce 2021-09-24 thomas if (err) {
8950 10604dce 2021-09-24 thomas if (*branch_name) {
8951 10604dce 2021-09-24 thomas free(*branch_name);
8952 10604dce 2021-09-24 thomas *branch_name = NULL;
8953 10604dce 2021-09-24 thomas }
8954 10604dce 2021-09-24 thomas free(*branch_tip);
8955 10604dce 2021-09-24 thomas *branch_tip = NULL;
8956 10604dce 2021-09-24 thomas if (*fileindex) {
8957 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8958 10604dce 2021-09-24 thomas *fileindex = NULL;
8959 10604dce 2021-09-24 thomas }
8960 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8961 10604dce 2021-09-24 thomas }
8962 10604dce 2021-09-24 thomas return err;
8963 10604dce 2021-09-24 thomas }
8964 10604dce 2021-09-24 thomas
8965 10604dce 2021-09-24 thomas const struct got_error *
8966 cd634f2d 2024-03-30 thomas got_worktree_merge_info(char **branch_name, struct got_worktree *worktree,
8967 cd634f2d 2024-03-30 thomas struct got_repository *repo)
8968 cd634f2d 2024-03-30 thomas {
8969 cd634f2d 2024-03-30 thomas const struct got_error *err;
8970 cd634f2d 2024-03-30 thomas char *branch_refname = NULL;
8971 cd634f2d 2024-03-30 thomas struct got_reference *branch_ref = NULL;
8972 cd634f2d 2024-03-30 thomas
8973 cd634f2d 2024-03-30 thomas *branch_name = NULL;
8974 cd634f2d 2024-03-30 thomas
8975 cd634f2d 2024-03-30 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8976 cd634f2d 2024-03-30 thomas if (err)
8977 cd634f2d 2024-03-30 thomas goto done;
8978 cd634f2d 2024-03-30 thomas
8979 cd634f2d 2024-03-30 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8980 cd634f2d 2024-03-30 thomas if (err)
8981 cd634f2d 2024-03-30 thomas goto done;
8982 cd634f2d 2024-03-30 thomas
8983 cd634f2d 2024-03-30 thomas if (!got_ref_is_symbolic(branch_ref)) {
8984 cd634f2d 2024-03-30 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8985 cd634f2d 2024-03-30 thomas "%s is not a symbolic reference",
8986 cd634f2d 2024-03-30 thomas got_ref_get_name(branch_ref));
8987 cd634f2d 2024-03-30 thomas goto done;
8988 cd634f2d 2024-03-30 thomas }
8989 cd634f2d 2024-03-30 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8990 cd634f2d 2024-03-30 thomas if (*branch_name == NULL) {
8991 cd634f2d 2024-03-30 thomas err = got_error_from_errno("strdup");
8992 cd634f2d 2024-03-30 thomas goto done;
8993 cd634f2d 2024-03-30 thomas }
8994 cd634f2d 2024-03-30 thomas
8995 cd634f2d 2024-03-30 thomas done:
8996 cd634f2d 2024-03-30 thomas free(branch_refname);
8997 cd634f2d 2024-03-30 thomas if (branch_ref)
8998 cd634f2d 2024-03-30 thomas got_ref_close(branch_ref);
8999 cd634f2d 2024-03-30 thomas if (err) {
9000 cd634f2d 2024-03-30 thomas if (*branch_name) {
9001 cd634f2d 2024-03-30 thomas free(*branch_name);
9002 cd634f2d 2024-03-30 thomas *branch_name = NULL;
9003 cd634f2d 2024-03-30 thomas }
9004 cd634f2d 2024-03-30 thomas }
9005 cd634f2d 2024-03-30 thomas return err;
9006 cd634f2d 2024-03-30 thomas }
9007 cd634f2d 2024-03-30 thomas
9008 cd634f2d 2024-03-30 thomas const struct got_error *
9009 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
9010 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
9011 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9012 10604dce 2021-09-24 thomas {
9013 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
9014 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9015 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
9016 10604dce 2021-09-24 thomas struct revert_file_args rfa;
9017 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
9018 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
9019 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
9020 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
9021 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
9022 8641a332 2023-04-14 thomas
9023 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
9024 8641a332 2023-04-14 thomas
9025 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
9026 8641a332 2023-04-14 thomas if (err)
9027 8641a332 2023-04-14 thomas goto done;
9028 8641a332 2023-04-14 thomas
9029 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
9030 8641a332 2023-04-14 thomas if (err)
9031 8641a332 2023-04-14 thomas goto done;
9032 8641a332 2023-04-14 thomas
9033 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
9034 8641a332 2023-04-14 thomas if (err)
9035 8641a332 2023-04-14 thomas goto done;
9036 8641a332 2023-04-14 thomas
9037 8641a332 2023-04-14 thomas /*
9038 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
9039 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
9040 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
9041 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
9042 8641a332 2023-04-14 thomas */
9043 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
9044 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
9045 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
9046 8641a332 2023-04-14 thomas if (err)
9047 8641a332 2023-04-14 thomas goto done;
9048 10604dce 2021-09-24 thomas
9049 8641a332 2023-04-14 thomas
9050 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
9051 945f9229 2022-04-16 thomas worktree->base_commit_id);
9052 945f9229 2022-04-16 thomas if (err)
9053 945f9229 2022-04-16 thomas goto done;
9054 945f9229 2022-04-16 thomas
9055 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
9056 945f9229 2022-04-16 thomas worktree->path_prefix);
9057 10604dce 2021-09-24 thomas if (err)
9058 10604dce 2021-09-24 thomas goto done;
9059 10604dce 2021-09-24 thomas
9060 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
9061 10604dce 2021-09-24 thomas if (err)
9062 10604dce 2021-09-24 thomas goto done;
9063 10604dce 2021-09-24 thomas
9064 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
9065 10604dce 2021-09-24 thomas if (err)
9066 10604dce 2021-09-24 thomas goto done;
9067 10604dce 2021-09-24 thomas
9068 10604dce 2021-09-24 thomas rfa.worktree = worktree;
9069 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
9070 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
9071 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
9072 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
9073 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
9074 10604dce 2021-09-24 thomas rfa.repo = repo;
9075 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
9076 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
9077 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
9078 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
9079 10604dce 2021-09-24 thomas if (err)
9080 10604dce 2021-09-24 thomas goto sync;
9081 10604dce 2021-09-24 thomas
9082 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
9083 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
9084 10604dce 2021-09-24 thomas sync:
9085 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
9086 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
9087 10604dce 2021-09-24 thomas err = sync_err;
9088 10604dce 2021-09-24 thomas done:
9089 10604dce 2021-09-24 thomas free(tree_id);
9090 8641a332 2023-04-14 thomas free(merged_commit_id);
9091 945f9229 2022-04-16 thomas if (commit)
9092 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9093 10604dce 2021-09-24 thomas if (fileindex)
9094 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
9095 10604dce 2021-09-24 thomas free(fileindex_path);
9096 8641a332 2023-04-14 thomas if (commit_ref)
9097 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
9098 8641a332 2023-04-14 thomas free(commit_ref_name);
9099 10604dce 2021-09-24 thomas
9100 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
9101 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
9102 10604dce 2021-09-24 thomas err = unlockerr;
9103 10604dce 2021-09-24 thomas return err;
9104 10604dce 2021-09-24 thomas }
9105 10604dce 2021-09-24 thomas
9106 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
9107 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
9108 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9109 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9110 2db2652d 2019-08-07 stsp struct got_repository *repo;
9111 2db2652d 2019-08-07 stsp int have_changes;
9112 2db2652d 2019-08-07 stsp };
9113 2db2652d 2019-08-07 stsp
9114 ef20f542 2022-06-26 thomas static const struct got_error *
9115 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
9116 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9117 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9118 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9119 735ef5ac 2019-08-03 stsp {
9120 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
9121 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
9122 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
9123 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
9124 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
9125 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
9126 8b13ce36 2019-08-08 stsp
9127 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
9128 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
9129 8b13ce36 2019-08-08 stsp return NULL;
9130 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
9131 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
9132 735ef5ac 2019-08-03 stsp
9133 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9134 735ef5ac 2019-08-03 stsp if (ie == NULL)
9135 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9136 735ef5ac 2019-08-03 stsp
9137 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
9138 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
9139 735ef5ac 2019-08-03 stsp relpath) == -1)
9140 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
9141 735ef5ac 2019-08-03 stsp
9142 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
9143 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
9144 43010591 2023-02-17 thomas &base_commit_id, ie);
9145 735ef5ac 2019-08-03 stsp }
9146 735ef5ac 2019-08-03 stsp
9147 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
9148 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
9149 3aa5969e 2019-08-06 stsp goto done;
9150 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
9151 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
9152 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
9153 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9154 735ef5ac 2019-08-03 stsp goto done;
9155 3aa5969e 2019-08-06 stsp }
9156 735ef5ac 2019-08-03 stsp
9157 2db2652d 2019-08-07 stsp a->have_changes = 1;
9158 2db2652d 2019-08-07 stsp
9159 735ef5ac 2019-08-03 stsp p = in_repo_path;
9160 735ef5ac 2019-08-03 stsp while (p[0] == '/')
9161 735ef5ac 2019-08-03 stsp p++;
9162 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
9163 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
9164 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
9165 735ef5ac 2019-08-03 stsp done:
9166 735ef5ac 2019-08-03 stsp free(in_repo_path);
9167 dc424a06 2019-08-07 stsp return err;
9168 dc424a06 2019-08-07 stsp }
9169 dc424a06 2019-08-07 stsp
9170 2db2652d 2019-08-07 stsp struct stage_path_arg {
9171 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9172 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9173 2db2652d 2019-08-07 stsp struct got_repository *repo;
9174 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
9175 2db2652d 2019-08-07 stsp void *status_arg;
9176 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
9177 2db2652d 2019-08-07 stsp void *patch_arg;
9178 7b5dc508 2019-10-28 stsp int staged_something;
9179 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
9180 2db2652d 2019-08-07 stsp };
9181 2db2652d 2019-08-07 stsp
9182 2db2652d 2019-08-07 stsp static const struct got_error *
9183 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
9184 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9185 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9186 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9187 0cb83759 2019-08-03 stsp {
9188 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
9189 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
9190 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
9191 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
9192 0cb83759 2019-08-03 stsp uint32_t stage;
9193 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
9194 0aeb8099 2020-07-23 stsp struct stat sb;
9195 8b13ce36 2019-08-08 stsp
9196 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
9197 8b13ce36 2019-08-08 stsp return NULL;
9198 0cb83759 2019-08-03 stsp
9199 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9200 d3e7c587 2019-08-03 stsp if (ie == NULL)
9201 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9202 0cb83759 2019-08-03 stsp
9203 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9204 2db2652d 2019-08-07 stsp relpath)== -1)
9205 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
9206 0cb83759 2019-08-03 stsp
9207 0cb83759 2019-08-03 stsp switch (status) {
9208 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9209 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9210 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9211 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9212 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9213 0aeb8099 2020-07-23 stsp break;
9214 0aeb8099 2020-07-23 stsp }
9215 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9216 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9217 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9218 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9219 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9220 dc424a06 2019-08-07 stsp if (err)
9221 dc424a06 2019-08-07 stsp break;
9222 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9223 dc424a06 2019-08-07 stsp break;
9224 dc424a06 2019-08-07 stsp } else {
9225 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9226 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9227 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9228 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9229 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9230 dc424a06 2019-08-07 stsp break;
9231 dc424a06 2019-08-07 stsp }
9232 dc424a06 2019-08-07 stsp }
9233 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9234 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9235 0cb83759 2019-08-03 stsp if (err)
9236 dc424a06 2019-08-07 stsp break;
9237 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9238 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9239 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9240 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9241 0cb83759 2019-08-03 stsp else
9242 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9243 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9244 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9245 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9246 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9247 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9248 35213c7c 2020-07-23 stsp ssize_t target_len;
9249 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9250 35213c7c 2020-07-23 stsp sizeof(target_path));
9251 35213c7c 2020-07-23 stsp if (target_len == -1) {
9252 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9253 35213c7c 2020-07-23 stsp ondisk_path);
9254 35213c7c 2020-07-23 stsp break;
9255 35213c7c 2020-07-23 stsp }
9256 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9257 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9258 ad10f64e 2023-07-19 thomas a->worktree->root_path,
9259 ad10f64e 2023-07-19 thomas a->worktree->meta_dir);
9260 35213c7c 2020-07-23 stsp if (err)
9261 35213c7c 2020-07-23 stsp break;
9262 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9263 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9264 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9265 35213c7c 2020-07-23 stsp break;
9266 35213c7c 2020-07-23 stsp }
9267 35213c7c 2020-07-23 stsp }
9268 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9269 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9270 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9271 35213c7c 2020-07-23 stsp else
9272 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9273 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9274 0aeb8099 2020-07-23 stsp } else {
9275 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9276 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9277 0aeb8099 2020-07-23 stsp }
9278 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9279 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9280 dc424a06 2019-08-07 stsp break;
9281 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9282 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9283 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9284 f289c82c 2022-06-13 thomas if (err)
9285 f289c82c 2022-06-13 thomas break;
9286 f289c82c 2022-06-13 thomas /*
9287 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
9288 f289c82c 2022-06-13 thomas * implicitly unstage the file.
9289 f289c82c 2022-06-13 thomas */
9290 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9291 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
9292 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
9293 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
9294 f289c82c 2022-06-13 thomas }
9295 0cb83759 2019-08-03 stsp break;
9296 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9297 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9298 d3e7c587 2019-08-03 stsp break;
9299 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9300 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9301 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9302 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9303 dc424a06 2019-08-07 stsp if (err)
9304 dc424a06 2019-08-07 stsp break;
9305 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9306 88f33a19 2019-08-08 stsp break;
9307 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9308 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9309 dc424a06 2019-08-07 stsp break;
9310 88f33a19 2019-08-08 stsp }
9311 dc424a06 2019-08-07 stsp }
9312 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9313 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9314 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9315 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9316 dc424a06 2019-08-07 stsp break;
9317 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9318 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9319 12463d8b 2019-12-13 stsp de_name);
9320 0cb83759 2019-08-03 stsp break;
9321 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9322 d3e7c587 2019-08-03 stsp break;
9323 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9324 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9325 ebf48fd5 2019-08-03 stsp break;
9326 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9327 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9328 2a06fe5f 2019-08-24 stsp break;
9329 0cb83759 2019-08-03 stsp default:
9330 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9331 537ac44b 2019-08-03 stsp break;
9332 0cb83759 2019-08-03 stsp }
9333 dc424a06 2019-08-07 stsp
9334 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9335 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9336 dc424a06 2019-08-07 stsp free(path_content);
9337 2db2652d 2019-08-07 stsp free(ondisk_path);
9338 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9339 0ebf8283 2019-07-24 stsp return err;
9340 0ebf8283 2019-07-24 stsp }
9341 0cb83759 2019-08-03 stsp
9342 0cb83759 2019-08-03 stsp const struct got_error *
9343 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9344 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9345 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9346 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9347 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9348 0cb83759 2019-08-03 stsp {
9349 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9350 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9351 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9352 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9353 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9354 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9355 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9356 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9357 0cb83759 2019-08-03 stsp
9358 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9359 0cb83759 2019-08-03 stsp if (err)
9360 0cb83759 2019-08-03 stsp return err;
9361 0cb83759 2019-08-03 stsp
9362 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9363 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9364 735ef5ac 2019-08-03 stsp if (err)
9365 735ef5ac 2019-08-03 stsp goto done;
9366 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9367 735ef5ac 2019-08-03 stsp if (err)
9368 735ef5ac 2019-08-03 stsp goto done;
9369 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9370 0cb83759 2019-08-03 stsp if (err)
9371 0cb83759 2019-08-03 stsp goto done;
9372 0cb83759 2019-08-03 stsp
9373 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9374 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9375 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9376 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9377 2db2652d 2019-08-07 stsp oka.repo = repo;
9378 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9379 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9380 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9381 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9382 735ef5ac 2019-08-03 stsp if (err)
9383 735ef5ac 2019-08-03 stsp goto done;
9384 735ef5ac 2019-08-03 stsp }
9385 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9386 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9387 2db2652d 2019-08-07 stsp goto done;
9388 2db2652d 2019-08-07 stsp }
9389 735ef5ac 2019-08-03 stsp
9390 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9391 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9392 2db2652d 2019-08-07 stsp spa.repo = repo;
9393 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9394 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9395 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9396 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9397 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9398 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9399 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9400 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9401 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9402 42005733 2019-08-03 stsp if (err)
9403 2db2652d 2019-08-07 stsp goto done;
9404 7b5dc508 2019-10-28 stsp }
9405 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9406 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9407 7b5dc508 2019-10-28 stsp goto done;
9408 0cb83759 2019-08-03 stsp }
9409 0cb83759 2019-08-03 stsp
9410 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9411 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9412 0cb83759 2019-08-03 stsp err = sync_err;
9413 0cb83759 2019-08-03 stsp done:
9414 735ef5ac 2019-08-03 stsp if (head_ref)
9415 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9416 735ef5ac 2019-08-03 stsp free(head_commit_id);
9417 0cb83759 2019-08-03 stsp free(fileindex_path);
9418 0cb83759 2019-08-03 stsp if (fileindex)
9419 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9420 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9421 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9422 0cb83759 2019-08-03 stsp err = unlockerr;
9423 0cb83759 2019-08-03 stsp return err;
9424 0cb83759 2019-08-03 stsp }
9425 ad493afc 2019-08-03 stsp
9426 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9427 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9428 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9429 ad493afc 2019-08-03 stsp struct got_repository *repo;
9430 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9431 ad493afc 2019-08-03 stsp void *progress_arg;
9432 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9433 2e1f37b0 2019-08-08 stsp void *patch_arg;
9434 ad493afc 2019-08-03 stsp };
9435 2e1f37b0 2019-08-08 stsp
9436 2e1f37b0 2019-08-08 stsp static const struct got_error *
9437 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9438 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9439 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9440 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9441 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9442 2e1f37b0 2019-08-08 stsp {
9443 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9444 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9445 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9446 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9447 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9448 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9449 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9450 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9451 2e1f37b0 2019-08-08 stsp
9452 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9453 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9454 2e1f37b0 2019-08-08 stsp
9455 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9456 2e1f37b0 2019-08-08 stsp if (err)
9457 2e1f37b0 2019-08-08 stsp return err;
9458 f4ae6ddb 2022-07-01 thomas
9459 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9460 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9461 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9462 f4ae6ddb 2022-07-01 thomas goto done;
9463 f4ae6ddb 2022-07-01 thomas }
9464 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9465 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9466 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9467 f4ae6ddb 2022-07-01 thomas goto done;
9468 f4ae6ddb 2022-07-01 thomas }
9469 f4ae6ddb 2022-07-01 thomas
9470 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9471 2e1f37b0 2019-08-08 stsp if (err)
9472 2e1f37b0 2019-08-08 stsp goto done;
9473 2e1f37b0 2019-08-08 stsp
9474 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9475 2e1f37b0 2019-08-08 stsp if (err)
9476 2e1f37b0 2019-08-08 stsp goto done;
9477 2e1f37b0 2019-08-08 stsp
9478 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9479 2e1f37b0 2019-08-08 stsp if (err)
9480 2e1f37b0 2019-08-08 stsp goto done;
9481 2e1f37b0 2019-08-08 stsp
9482 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9483 f4ae6ddb 2022-07-01 thomas fd2);
9484 2e1f37b0 2019-08-08 stsp if (err)
9485 2e1f37b0 2019-08-08 stsp goto done;
9486 2e1f37b0 2019-08-08 stsp
9487 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9488 2e1f37b0 2019-08-08 stsp if (err)
9489 2e1f37b0 2019-08-08 stsp goto done;
9490 ad493afc 2019-08-03 stsp
9491 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9492 2e1f37b0 2019-08-08 stsp if (err)
9493 2e1f37b0 2019-08-08 stsp goto done;
9494 2e1f37b0 2019-08-08 stsp
9495 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9496 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9497 2e1f37b0 2019-08-08 stsp if (err)
9498 2e1f37b0 2019-08-08 stsp goto done;
9499 2e1f37b0 2019-08-08 stsp
9500 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9501 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9502 2e1f37b0 2019-08-08 stsp if (err)
9503 2e1f37b0 2019-08-08 stsp goto done;
9504 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9505 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9506 2e1f37b0 2019-08-08 stsp if (err)
9507 2e1f37b0 2019-08-08 stsp goto done;
9508 2e1f37b0 2019-08-08 stsp
9509 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9510 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9511 2e1f37b0 2019-08-08 stsp goto done;
9512 2e1f37b0 2019-08-08 stsp }
9513 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9514 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9515 2e1f37b0 2019-08-08 stsp goto done;
9516 2e1f37b0 2019-08-08 stsp }
9517 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9518 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9519 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9520 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9521 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9522 fe621944 2020-11-10 stsp nchanges++;
9523 fe621944 2020-11-10 stsp }
9524 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9525 2e1f37b0 2019-08-08 stsp int choice;
9526 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9527 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9528 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9529 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9530 2e1f37b0 2019-08-08 stsp if (err)
9531 2e1f37b0 2019-08-08 stsp goto done;
9532 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9533 2e1f37b0 2019-08-08 stsp have_content = 1;
9534 19e4b907 2019-08-08 stsp else
9535 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9536 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9537 2e1f37b0 2019-08-08 stsp break;
9538 2e1f37b0 2019-08-08 stsp }
9539 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9540 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9541 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9542 2e1f37b0 2019-08-08 stsp done:
9543 2e1f37b0 2019-08-08 stsp free(label1);
9544 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9545 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9546 2e1f37b0 2019-08-08 stsp if (blob)
9547 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9548 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9549 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9550 2e1f37b0 2019-08-08 stsp if (staged_blob)
9551 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9552 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9553 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9554 fe621944 2020-11-10 stsp err = free_err;
9555 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9556 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9557 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9558 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9559 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9560 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9561 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9562 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9563 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9564 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9565 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9566 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9567 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9568 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9569 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9570 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9571 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9572 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9573 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9574 2e1f37b0 2019-08-08 stsp }
9575 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9576 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9577 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9578 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9579 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9580 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9581 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9582 2e1f37b0 2019-08-08 stsp }
9583 2e1f37b0 2019-08-08 stsp free(path1);
9584 2e1f37b0 2019-08-08 stsp free(path2);
9585 fda8017d 2020-07-23 stsp return err;
9586 fda8017d 2020-07-23 stsp }
9587 fda8017d 2020-07-23 stsp
9588 fda8017d 2020-07-23 stsp static const struct got_error *
9589 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9590 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9591 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9592 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9593 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9594 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9595 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9596 fda8017d 2020-07-23 stsp {
9597 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9598 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9599 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9600 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9601 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9602 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9603 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9604 fda8017d 2020-07-23 stsp struct stat sb;
9605 fda8017d 2020-07-23 stsp
9606 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9607 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9608 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9609 fda8017d 2020-07-23 stsp if (err)
9610 fda8017d 2020-07-23 stsp return err;
9611 fda8017d 2020-07-23 stsp
9612 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9613 fda8017d 2020-07-23 stsp return NULL;
9614 fda8017d 2020-07-23 stsp
9615 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9616 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9617 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9618 fda8017d 2020-07-23 stsp if (err)
9619 fda8017d 2020-07-23 stsp goto done;
9620 fda8017d 2020-07-23 stsp }
9621 fda8017d 2020-07-23 stsp
9622 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9623 fda8017d 2020-07-23 stsp if (f == NULL) {
9624 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9625 fda8017d 2020-07-23 stsp path_unstaged_content);
9626 fda8017d 2020-07-23 stsp goto done;
9627 fda8017d 2020-07-23 stsp }
9628 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9629 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9630 fda8017d 2020-07-23 stsp goto done;
9631 fda8017d 2020-07-23 stsp }
9632 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9633 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9634 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9635 fda8017d 2020-07-23 stsp size_t r;
9636 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9637 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9638 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9639 fda8017d 2020-07-23 stsp goto done;
9640 fda8017d 2020-07-23 stsp }
9641 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9642 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9643 fda8017d 2020-07-23 stsp goto done;
9644 fda8017d 2020-07-23 stsp }
9645 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9646 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9647 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9648 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9649 fda8017d 2020-07-23 stsp progress_arg);
9650 fda8017d 2020-07-23 stsp } else {
9651 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9652 67a66647 2021-05-31 stsp
9653 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9654 67a66647 2021-05-31 stsp if (err)
9655 67a66647 2021-05-31 stsp return err;
9656 67a66647 2021-05-31 stsp
9657 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9658 67a66647 2021-05-31 stsp parent) == -1) {
9659 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9660 67a66647 2021-05-31 stsp base_path = NULL;
9661 67a66647 2021-05-31 stsp goto done;
9662 67a66647 2021-05-31 stsp }
9663 67a66647 2021-05-31 stsp
9664 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9665 fc2a50f2 2022-11-01 thomas base_path, "");
9666 67a66647 2021-05-31 stsp if (err)
9667 67a66647 2021-05-31 stsp goto done;
9668 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9669 67a66647 2021-05-31 stsp blob_base);
9670 67a66647 2021-05-31 stsp if (err)
9671 67a66647 2021-05-31 stsp goto done;
9672 67a66647 2021-05-31 stsp
9673 b6b86fd1 2022-08-30 thomas /*
9674 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9675 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9676 eec2f5a9 2021-06-03 stsp */
9677 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9678 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9679 eec2f5a9 2021-06-03 stsp ondisk_path);
9680 eec2f5a9 2021-06-03 stsp if (err)
9681 eec2f5a9 2021-06-03 stsp goto done;
9682 eec2f5a9 2021-06-03 stsp } else {
9683 eec2f5a9 2021-06-03 stsp int fd;
9684 06340621 2021-12-31 thomas fd = open(ondisk_path,
9685 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9686 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9687 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9688 eec2f5a9 2021-06-03 stsp goto done;
9689 eec2f5a9 2021-06-03 stsp }
9690 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9691 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9692 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9693 eec2f5a9 2021-06-03 stsp close(fd);
9694 eec2f5a9 2021-06-03 stsp goto done;
9695 eec2f5a9 2021-06-03 stsp }
9696 eec2f5a9 2021-06-03 stsp }
9697 eec2f5a9 2021-06-03 stsp
9698 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9699 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9700 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9701 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9702 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9703 fda8017d 2020-07-23 stsp }
9704 fda8017d 2020-07-23 stsp if (err)
9705 fda8017d 2020-07-23 stsp goto done;
9706 fda8017d 2020-07-23 stsp
9707 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9708 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9709 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9710 2ac8aa02 2020-11-09 stsp } else {
9711 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9712 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9713 2ac8aa02 2020-11-09 stsp }
9714 fda8017d 2020-07-23 stsp done:
9715 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9716 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9717 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9718 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9719 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9720 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9721 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9722 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9723 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9724 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9725 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9726 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9727 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9728 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9729 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9730 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9731 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9732 67a66647 2021-05-31 stsp free(blob_base_path);
9733 67a66647 2021-05-31 stsp free(parent);
9734 67a66647 2021-05-31 stsp free(base_path);
9735 2e1f37b0 2019-08-08 stsp return err;
9736 2e1f37b0 2019-08-08 stsp }
9737 2e1f37b0 2019-08-08 stsp
9738 ad493afc 2019-08-03 stsp static const struct got_error *
9739 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9740 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9741 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9742 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9743 ad493afc 2019-08-03 stsp {
9744 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9745 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9746 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9747 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9748 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9749 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9750 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9751 9bc94a15 2019-08-03 stsp struct stat sb;
9752 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9753 ad493afc 2019-08-03 stsp
9754 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9755 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9756 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9757 2e1f37b0 2019-08-08 stsp return NULL;
9758 2e1f37b0 2019-08-08 stsp
9759 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9760 ad493afc 2019-08-03 stsp if (ie == NULL)
9761 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9762 9bc94a15 2019-08-03 stsp
9763 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9764 9bc94a15 2019-08-03 stsp == -1)
9765 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9766 ad493afc 2019-08-03 stsp
9767 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9768 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9769 f69721c3 2019-10-21 stsp if (err)
9770 f69721c3 2019-10-21 stsp goto done;
9771 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9772 f69721c3 2019-10-21 stsp id_str) == -1) {
9773 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9774 f4ae6ddb 2022-07-01 thomas goto done;
9775 f4ae6ddb 2022-07-01 thomas }
9776 f4ae6ddb 2022-07-01 thomas
9777 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9778 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9779 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9780 f4ae6ddb 2022-07-01 thomas goto done;
9781 f4ae6ddb 2022-07-01 thomas }
9782 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9783 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9784 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9785 f69721c3 2019-10-21 stsp goto done;
9786 f69721c3 2019-10-21 stsp }
9787 f69721c3 2019-10-21 stsp
9788 ad493afc 2019-08-03 stsp switch (staged_status) {
9789 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9790 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9791 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9792 ad493afc 2019-08-03 stsp if (err)
9793 ad493afc 2019-08-03 stsp break;
9794 ad493afc 2019-08-03 stsp /* fall through */
9795 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9796 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9797 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9798 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9799 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9800 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9801 2e1f37b0 2019-08-08 stsp if (err)
9802 2e1f37b0 2019-08-08 stsp break;
9803 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9804 2e1f37b0 2019-08-08 stsp break;
9805 2e1f37b0 2019-08-08 stsp } else {
9806 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9807 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9808 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9809 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9810 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9811 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9812 2e1f37b0 2019-08-08 stsp }
9813 2e1f37b0 2019-08-08 stsp }
9814 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9815 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9816 ad493afc 2019-08-03 stsp if (err)
9817 ad493afc 2019-08-03 stsp break;
9818 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9819 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9820 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9821 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9822 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9823 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9824 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9825 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9826 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9827 ea7786be 2020-07-23 stsp break;
9828 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9829 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9830 36bf999c 2020-07-23 stsp char *staged_target;
9831 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9832 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9833 36bf999c 2020-07-23 stsp if (err)
9834 36bf999c 2020-07-23 stsp goto done;
9835 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9836 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9837 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9838 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9839 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9840 36bf999c 2020-07-23 stsp free(staged_target);
9841 dfe9fba0 2020-07-23 stsp } else {
9842 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9843 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9844 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9845 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9846 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9847 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9848 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9849 dfe9fba0 2020-07-23 stsp }
9850 ea7786be 2020-07-23 stsp break;
9851 ea7786be 2020-07-23 stsp default:
9852 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9853 ea7786be 2020-07-23 stsp break;
9854 ea7786be 2020-07-23 stsp }
9855 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9856 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9857 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9858 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9859 2ac8aa02 2020-11-09 stsp }
9860 ad493afc 2019-08-03 stsp break;
9861 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9862 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9863 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9864 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9865 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9866 2e1f37b0 2019-08-08 stsp if (err)
9867 2e1f37b0 2019-08-08 stsp break;
9868 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9869 2e1f37b0 2019-08-08 stsp break;
9870 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9871 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9872 2e1f37b0 2019-08-08 stsp break;
9873 2e1f37b0 2019-08-08 stsp }
9874 2e1f37b0 2019-08-08 stsp }
9875 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9876 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9877 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9878 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9879 9bc94a15 2019-08-03 stsp if (err)
9880 9bc94a15 2019-08-03 stsp break;
9881 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9882 ad493afc 2019-08-03 stsp break;
9883 ad493afc 2019-08-03 stsp }
9884 f69721c3 2019-10-21 stsp done:
9885 ad493afc 2019-08-03 stsp free(ondisk_path);
9886 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9887 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9888 ad493afc 2019-08-03 stsp if (blob_base)
9889 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9890 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9891 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9892 ad493afc 2019-08-03 stsp if (blob_staged)
9893 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9894 f69721c3 2019-10-21 stsp free(id_str);
9895 f69721c3 2019-10-21 stsp free(label_orig);
9896 ad493afc 2019-08-03 stsp return err;
9897 ad493afc 2019-08-03 stsp }
9898 ad493afc 2019-08-03 stsp
9899 ad493afc 2019-08-03 stsp const struct got_error *
9900 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9901 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9902 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9903 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9904 ad493afc 2019-08-03 stsp struct got_repository *repo)
9905 ad493afc 2019-08-03 stsp {
9906 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9907 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9908 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9909 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9910 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9911 ad493afc 2019-08-03 stsp
9912 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9913 ad493afc 2019-08-03 stsp if (err)
9914 ad493afc 2019-08-03 stsp return err;
9915 ad493afc 2019-08-03 stsp
9916 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9917 ad493afc 2019-08-03 stsp if (err)
9918 ad493afc 2019-08-03 stsp goto done;
9919 ad493afc 2019-08-03 stsp
9920 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9921 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9922 ad493afc 2019-08-03 stsp upa.repo = repo;
9923 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9924 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9925 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9926 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9927 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9928 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9929 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9930 ad493afc 2019-08-03 stsp if (err)
9931 ad493afc 2019-08-03 stsp goto done;
9932 ad493afc 2019-08-03 stsp }
9933 ad493afc 2019-08-03 stsp
9934 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9935 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9936 ad493afc 2019-08-03 stsp err = sync_err;
9937 ad493afc 2019-08-03 stsp done:
9938 ad493afc 2019-08-03 stsp free(fileindex_path);
9939 ad493afc 2019-08-03 stsp if (fileindex)
9940 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9941 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9942 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9943 ad493afc 2019-08-03 stsp err = unlockerr;
9944 ad493afc 2019-08-03 stsp return err;
9945 ad493afc 2019-08-03 stsp }
9946 b2118c49 2020-07-28 stsp
9947 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9948 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9949 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9950 b2118c49 2020-07-28 stsp void *info_arg;
9951 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9952 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9953 b2118c49 2020-07-28 stsp void *cancel_arg;
9954 b2118c49 2020-07-28 stsp };
9955 b2118c49 2020-07-28 stsp
9956 b2118c49 2020-07-28 stsp static const struct got_error *
9957 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9958 b2118c49 2020-07-28 stsp {
9959 eb6fb9cd 2023-07-26 thomas const struct got_error *err;
9960 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9961 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9962 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9963 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9964 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9965 b2118c49 2020-07-28 stsp int stage;
9966 b2118c49 2020-07-28 stsp
9967 eb6fb9cd 2023-07-26 thomas if (a->cancel_cb) {
9968 eb6fb9cd 2023-07-26 thomas err = a->cancel_cb(a->cancel_arg);
9969 eb6fb9cd 2023-07-26 thomas if (err)
9970 eb6fb9cd 2023-07-26 thomas return err;
9971 eb6fb9cd 2023-07-26 thomas }
9972 b2118c49 2020-07-28 stsp
9973 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9974 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9975 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9976 b2118c49 2020-07-28 stsp break;
9977 b2118c49 2020-07-28 stsp }
9978 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9979 b2118c49 2020-07-28 stsp return NULL;
9980 b2118c49 2020-07-28 stsp
9981 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9982 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9983 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9984 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9985 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9986 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9987 43010591 2023-02-17 thomas &staged_blob_id, ie);
9988 b2118c49 2020-07-28 stsp }
9989 b2118c49 2020-07-28 stsp
9990 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9991 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9992 b2118c49 2020-07-28 stsp
9993 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9994 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9995 b2118c49 2020-07-28 stsp }
9996 b2118c49 2020-07-28 stsp
9997 b2118c49 2020-07-28 stsp const struct got_error *
9998 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9999 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
10000 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
10001 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
10002 b2118c49 2020-07-28 stsp
10003 b2118c49 2020-07-28 stsp {
10004 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
10005 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
10006 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
10007 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
10008 b2118c49 2020-07-28 stsp
10009 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
10010 b2118c49 2020-07-28 stsp if (err)
10011 b2118c49 2020-07-28 stsp return err;
10012 b2118c49 2020-07-28 stsp
10013 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
10014 b2118c49 2020-07-28 stsp if (err)
10015 b2118c49 2020-07-28 stsp goto done;
10016 b2118c49 2020-07-28 stsp
10017 b2118c49 2020-07-28 stsp arg.worktree = worktree;
10018 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
10019 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
10020 b2118c49 2020-07-28 stsp arg.paths = paths;
10021 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
10022 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
10023 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
10024 b2118c49 2020-07-28 stsp &arg);
10025 b2118c49 2020-07-28 stsp done:
10026 b2118c49 2020-07-28 stsp free(fileindex_path);
10027 b2118c49 2020-07-28 stsp if (fileindex)
10028 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
10029 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
10030 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
10031 b2118c49 2020-07-28 stsp err = unlockerr;
10032 b2118c49 2020-07-28 stsp return err;
10033 b2118c49 2020-07-28 stsp }
10034 814624e7 2022-03-22 thomas
10035 814624e7 2022-03-22 thomas static const struct got_error *
10036 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
10037 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
10038 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
10039 814624e7 2022-03-22 thomas {
10040 814624e7 2022-03-22 thomas const struct got_error *err;
10041 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
10042 814624e7 2022-03-22 thomas struct stat sb;
10043 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
10044 814624e7 2022-03-22 thomas
10045 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
10046 814624e7 2022-03-22 thomas if (err)
10047 814624e7 2022-03-22 thomas return err;
10048 814624e7 2022-03-22 thomas
10049 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
10050 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
10051 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
10052 814624e7 2022-03-22 thomas
10053 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
10054 814624e7 2022-03-22 thomas if (ie) {
10055 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
10056 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
10057 12de5570 2022-05-01 thomas repo);
10058 814624e7 2022-03-22 thomas if (err)
10059 814624e7 2022-03-22 thomas goto done;
10060 814624e7 2022-03-22 thomas } else {
10061 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
10062 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
10063 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
10064 814624e7 2022-03-22 thomas if (errno != ENOENT) {
10065 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
10066 814624e7 2022-03-22 thomas ondisk_path);
10067 814624e7 2022-03-22 thomas goto done;
10068 814624e7 2022-03-22 thomas }
10069 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
10070 814624e7 2022-03-22 thomas }
10071 814624e7 2022-03-22 thomas }
10072 814624e7 2022-03-22 thomas
10073 814624e7 2022-03-22 thomas done:
10074 814624e7 2022-03-22 thomas free(ondisk_path);
10075 814624e7 2022-03-22 thomas return err;
10076 814624e7 2022-03-22 thomas }
10077 814624e7 2022-03-22 thomas
10078 814624e7 2022-03-22 thomas static const struct got_error *
10079 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
10080 814624e7 2022-03-22 thomas unsigned char staged_status)
10081 814624e7 2022-03-22 thomas {
10082 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
10083 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
10084 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
10085 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
10086 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
10087 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
10088 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
10089 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
10090 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
10091 814624e7 2022-03-22 thomas return NULL;
10092 814624e7 2022-03-22 thomas }
10093 814624e7 2022-03-22 thomas
10094 814624e7 2022-03-22 thomas static const struct got_error *
10095 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
10096 814624e7 2022-03-22 thomas {
10097 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
10098 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
10099 814624e7 2022-03-22 thomas return NULL;
10100 814624e7 2022-03-22 thomas }
10101 814624e7 2022-03-22 thomas
10102 814624e7 2022-03-22 thomas static const struct got_error *
10103 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
10104 814624e7 2022-03-22 thomas unsigned char staged_status)
10105 814624e7 2022-03-22 thomas {
10106 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
10107 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
10108 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
10109 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
10110 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
10111 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
10112 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
10113 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
10114 814624e7 2022-03-22 thomas return NULL;
10115 814624e7 2022-03-22 thomas }
10116 814624e7 2022-03-22 thomas
10117 814624e7 2022-03-22 thomas const struct got_error *
10118 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
10119 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
10120 814624e7 2022-03-22 thomas {
10121 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
10122 814624e7 2022-03-22 thomas }
10123 814624e7 2022-03-22 thomas
10124 814624e7 2022-03-22 thomas const struct got_error *
10125 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
10126 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
10127 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
10128 814624e7 2022-03-22 thomas {
10129 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
10130 814624e7 2022-03-22 thomas int file_renamed = 0;
10131 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
10132 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
10133 814624e7 2022-03-22 thomas
10134 814624e7 2022-03-22 thomas *oldpath = NULL;
10135 814624e7 2022-03-22 thomas *newpath = NULL;
10136 814624e7 2022-03-22 thomas
10137 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
10138 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
10139 814624e7 2022-03-22 thomas if (err)
10140 814624e7 2022-03-22 thomas goto done;
10141 814624e7 2022-03-22 thomas
10142 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
10143 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
10144 814624e7 2022-03-22 thomas if (err)
10145 814624e7 2022-03-22 thomas goto done;
10146 814624e7 2022-03-22 thomas
10147 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
10148 814624e7 2022-03-22 thomas file_renamed = 1;
10149 814624e7 2022-03-22 thomas
10150 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
10151 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
10152 814624e7 2022-03-22 thomas else if (file_renamed) {
10153 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
10154 814624e7 2022-03-22 thomas if (err == NULL)
10155 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
10156 814624e7 2022-03-22 thomas } else if (old == NULL)
10157 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
10158 814624e7 2022-03-22 thomas else
10159 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
10160 814624e7 2022-03-22 thomas
10161 814624e7 2022-03-22 thomas done:
10162 814624e7 2022-03-22 thomas if (err) {
10163 814624e7 2022-03-22 thomas free(*oldpath);
10164 814624e7 2022-03-22 thomas *oldpath = NULL;
10165 814624e7 2022-03-22 thomas free(*newpath);
10166 814624e7 2022-03-22 thomas *newpath = NULL;
10167 814624e7 2022-03-22 thomas }
10168 814624e7 2022-03-22 thomas return err;
10169 814624e7 2022-03-22 thomas }
10170 814624e7 2022-03-22 thomas
10171 814624e7 2022-03-22 thomas const struct got_error *
10172 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10173 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10174 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
10175 814624e7 2022-03-22 thomas {
10176 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
10177 5e22b737 2022-05-31 thomas
10178 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
10179 5e22b737 2022-05-31 thomas saa.worktree = worktree;
10180 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
10181 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
10182 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
10183 5e22b737 2022-05-31 thomas saa.repo = repo;
10184 5e22b737 2022-05-31 thomas
10185 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
10186 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
10187 814624e7 2022-03-22 thomas }
10188 5e22b737 2022-05-31 thomas
10189 5e22b737 2022-05-31 thomas const struct got_error *
10190 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10191 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
10192 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
10193 5e22b737 2022-05-31 thomas {
10194 8ec7352a 2023-06-01 thomas const struct got_error *err;
10195 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
10196 8ec7352a 2023-06-01 thomas char *ondisk_status_path;
10197 5e22b737 2022-05-31 thomas
10198 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
10199 5e22b737 2022-05-31 thomas sda.worktree = worktree;
10200 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
10201 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
10202 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
10203 5e22b737 2022-05-31 thomas sda.repo = repo;
10204 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
10205 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
10206 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
10207 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
10208 8ec7352a 2023-06-01 thomas if (asprintf(&ondisk_status_path, "%s/%s",
10209 8ec7352a 2023-06-01 thomas got_worktree_get_root_path(worktree), path) == -1)
10210 8ec7352a 2023-06-01 thomas return got_error_from_errno("asprintf");
10211 8ec7352a 2023-06-01 thomas sda.status_path = ondisk_status_path;
10212 8ec7352a 2023-06-01 thomas sda.status_path_len = strlen(ondisk_status_path);
10213 8ec7352a 2023-06-01 thomas
10214 8ec7352a 2023-06-01 thomas err = worktree_status(worktree, path, fileindex, repo,
10215 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10216 8ec7352a 2023-06-01 thomas free(ondisk_status_path);
10217 8ec7352a 2023-06-01 thomas return err;
10218 5e22b737 2022-05-31 thomas }
10219 5e22b737 2022-05-31 thomas
10220 5e22b737 2022-05-31 thomas const struct got_error *
10221 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
10222 36832a8e 2022-05-31 thomas const char *fileindex_path)
10223 5e22b737 2022-05-31 thomas {
10224 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
10225 5e22b737 2022-05-31 thomas
10226 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
10227 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
10228 5e22b737 2022-05-31 thomas
10229 5e22b737 2022-05-31 thomas return err;
10230 5e22b737 2022-05-31 thomas }