Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 5822e79e 2023-02-23 op #include <sha2.h>
33 9d31a1d8 2018-03-11 stsp #include <zlib.h>
34 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
35 512f0d0e 2019-01-02 stsp #include <libgen.h>
36 ec22038e 2019-03-10 stsp #include <uuid.h>
37 7154f6ce 2019-03-27 stsp #include <util.h>
38 86c3caaf 2018-03-09 stsp
39 86c3caaf 2018-03-09 stsp #include "got_error.h"
40 86c3caaf 2018-03-09 stsp #include "got_repository.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 9d31a1d8 2018-03-11 stsp #include "got_object.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
46 511a516b 2018-05-19 stsp #include "got_opentemp.h"
47 234035bc 2019-06-01 stsp #include "got_diff.h"
48 86c3caaf 2018-03-09 stsp
49 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
50 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
52 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
54 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
55 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
56 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
57 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
58 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
59 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
60 9d31a1d8 2018-03-11 stsp
61 9d31a1d8 2018-03-11 stsp #ifndef MIN
62 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 9d31a1d8 2018-03-11 stsp #endif
64 f69721c3 2019-10-21 stsp
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
66 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 b2b3fce1 2022-10-29 op
68 d556241a 2023-07-03 jrick static mode_t
69 d556241a 2023-07-03 jrick apply_umask(mode_t mode)
70 d556241a 2023-07-03 jrick {
71 d556241a 2023-07-03 jrick mode_t um;
72 d556241a 2023-07-03 jrick
73 d556241a 2023-07-03 jrick um = umask(000);
74 d556241a 2023-07-03 jrick umask(um);
75 d556241a 2023-07-03 jrick return mode & ~um;
76 d556241a 2023-07-03 jrick }
77 86c3caaf 2018-03-09 stsp
78 99724ed4 2018-03-10 stsp static const struct got_error *
79 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
80 99724ed4 2018-03-10 stsp {
81 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
82 99724ed4 2018-03-10 stsp char *path;
83 99724ed4 2018-03-10 stsp
84 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
85 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
86 99724ed4 2018-03-10 stsp
87 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
88 99724ed4 2018-03-10 stsp free(path);
89 507dc3bb 2018-12-29 stsp return err;
90 507dc3bb 2018-12-29 stsp }
91 507dc3bb 2018-12-29 stsp
92 507dc3bb 2018-12-29 stsp static const struct got_error *
93 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
94 507dc3bb 2018-12-29 stsp {
95 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
96 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
97 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
98 507dc3bb 2018-12-29 stsp char *path = NULL;
99 507dc3bb 2018-12-29 stsp
100 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
102 507dc3bb 2018-12-29 stsp path = NULL;
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp
106 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, path, "");
107 507dc3bb 2018-12-29 stsp if (err)
108 507dc3bb 2018-12-29 stsp goto done;
109 507dc3bb 2018-12-29 stsp
110 507dc3bb 2018-12-29 stsp if (content) {
111 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
112 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
113 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
114 507dc3bb 2018-12-29 stsp goto done;
115 507dc3bb 2018-12-29 stsp }
116 507dc3bb 2018-12-29 stsp }
117 507dc3bb 2018-12-29 stsp
118 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
119 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
120 2a57020b 2019-02-20 stsp unlink(tmppath);
121 507dc3bb 2018-12-29 stsp goto done;
122 507dc3bb 2018-12-29 stsp }
123 507dc3bb 2018-12-29 stsp
124 507dc3bb 2018-12-29 stsp done:
125 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
126 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
127 230a42bd 2019-05-11 jcs free(tmppath);
128 09fe317a 2018-03-11 stsp return err;
129 09fe317a 2018-03-11 stsp }
130 09fe317a 2018-03-11 stsp
131 024e9686 2019-05-14 stsp static const struct got_error *
132 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
133 024e9686 2019-05-14 stsp {
134 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
135 024e9686 2019-05-14 stsp char *refstr = NULL;
136 024e9686 2019-05-14 stsp
137 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
138 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
139 024e9686 2019-05-14 stsp if (refstr == NULL)
140 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
141 024e9686 2019-05-14 stsp } else {
142 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
143 024e9686 2019-05-14 stsp if (refstr == NULL)
144 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
145 024e9686 2019-05-14 stsp }
146 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
147 024e9686 2019-05-14 stsp free(refstr);
148 024e9686 2019-05-14 stsp return err;
149 024e9686 2019-05-14 stsp }
150 024e9686 2019-05-14 stsp
151 86c3caaf 2018-03-09 stsp const struct got_error *
152 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
153 df6221c7 2023-07-19 stsp const char *prefix, const char *meta_dir, struct got_repository *repo)
154 86c3caaf 2018-03-09 stsp {
155 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
156 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
157 ec22038e 2019-03-10 stsp uuid_t uuid;
158 ec22038e 2019-03-10 stsp uint32_t uuid_status;
159 65596e15 2018-12-24 stsp int obj_type;
160 7ac97322 2018-03-11 stsp char *path_got = NULL;
161 1451e70d 2018-03-10 stsp char *formatstr = NULL;
162 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
163 65596e15 2018-12-24 stsp char *basestr = NULL;
164 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
165 65596e15 2018-12-24 stsp
166 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
167 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
168 0c48fee2 2019-03-11 stsp goto done;
169 0c48fee2 2019-03-11 stsp }
170 0c48fee2 2019-03-11 stsp
171 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
172 65596e15 2018-12-24 stsp if (err)
173 65596e15 2018-12-24 stsp return err;
174 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
175 65596e15 2018-12-24 stsp if (err)
176 65596e15 2018-12-24 stsp return err;
177 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
178 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
179 86c3caaf 2018-03-09 stsp
180 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
181 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
182 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
183 0bb8a95e 2018-03-12 stsp }
184 577ec78f 2018-03-11 stsp
185 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
186 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
188 86c3caaf 2018-03-09 stsp goto done;
189 86c3caaf 2018-03-09 stsp }
190 86c3caaf 2018-03-09 stsp
191 df6221c7 2023-07-19 stsp /* Create .got/.cvg directory (may already exist). */
192 df6221c7 2023-07-19 stsp if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
193 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
194 86c3caaf 2018-03-09 stsp goto done;
195 86c3caaf 2018-03-09 stsp }
196 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
197 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp }
200 86c3caaf 2018-03-09 stsp
201 056e7441 2018-03-11 stsp /* Create an empty lock file. */
202 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
203 056e7441 2018-03-11 stsp if (err)
204 056e7441 2018-03-11 stsp goto done;
205 056e7441 2018-03-11 stsp
206 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
207 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
208 99724ed4 2018-03-10 stsp if (err)
209 86c3caaf 2018-03-09 stsp goto done;
210 86c3caaf 2018-03-09 stsp
211 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
212 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
213 65596e15 2018-12-24 stsp if (err)
214 65596e15 2018-12-24 stsp goto done;
215 65596e15 2018-12-24 stsp
216 65596e15 2018-12-24 stsp /* Record our base commit. */
217 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
218 65596e15 2018-12-24 stsp if (err)
219 65596e15 2018-12-24 stsp goto done;
220 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
221 99724ed4 2018-03-10 stsp if (err)
222 86c3caaf 2018-03-09 stsp goto done;
223 86c3caaf 2018-03-09 stsp
224 1451e70d 2018-03-10 stsp /* Store path to repository. */
225 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
226 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
227 99724ed4 2018-03-10 stsp if (err)
228 86c3caaf 2018-03-09 stsp goto done;
229 86c3caaf 2018-03-09 stsp
230 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
231 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
232 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
233 ec22038e 2019-03-10 stsp if (err)
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp
236 ec22038e 2019-03-10 stsp /* Generate UUID. */
237 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
238 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
239 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
240 ec22038e 2019-03-10 stsp goto done;
241 ec22038e 2019-03-10 stsp }
242 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
243 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
244 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
245 ec22038e 2019-03-10 stsp goto done;
246 ec22038e 2019-03-10 stsp }
247 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
248 577ec78f 2018-03-11 stsp if (err)
249 577ec78f 2018-03-11 stsp goto done;
250 577ec78f 2018-03-11 stsp
251 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
252 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
253 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
254 1451e70d 2018-03-10 stsp goto done;
255 1451e70d 2018-03-10 stsp }
256 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
257 99724ed4 2018-03-10 stsp if (err)
258 1451e70d 2018-03-10 stsp goto done;
259 1451e70d 2018-03-10 stsp
260 86c3caaf 2018-03-09 stsp done:
261 65596e15 2018-12-24 stsp free(commit_id);
262 7ac97322 2018-03-11 stsp free(path_got);
263 1451e70d 2018-03-10 stsp free(formatstr);
264 0bb8a95e 2018-03-12 stsp free(absprefix);
265 65596e15 2018-12-24 stsp free(basestr);
266 ec22038e 2019-03-10 stsp free(uuidstr);
267 86c3caaf 2018-03-09 stsp return err;
268 86c3caaf 2018-03-09 stsp }
269 86c3caaf 2018-03-09 stsp
270 247140b2 2019-02-05 stsp const struct got_error *
271 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
272 e5dc7198 2018-12-29 stsp const char *path_prefix)
273 e5dc7198 2018-12-29 stsp {
274 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
275 e5dc7198 2018-12-29 stsp
276 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
277 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
278 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
279 e5dc7198 2018-12-29 stsp }
280 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
281 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
282 e5dc7198 2018-12-29 stsp free(absprefix);
283 e5dc7198 2018-12-29 stsp return NULL;
284 86c3caaf 2018-03-09 stsp }
285 86c3caaf 2018-03-09 stsp
286 bc70eb79 2019-05-09 stsp const char *
287 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
288 86c3caaf 2018-03-09 stsp {
289 36a38700 2019-05-10 stsp return worktree->head_ref_name;
290 024e9686 2019-05-14 stsp }
291 024e9686 2019-05-14 stsp
292 024e9686 2019-05-14 stsp const struct got_error *
293 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
294 024e9686 2019-05-14 stsp struct got_reference *head_ref)
295 024e9686 2019-05-14 stsp {
296 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
297 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
298 024e9686 2019-05-14 stsp
299 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
300 df6221c7 2023-07-19 stsp worktree->meta_dir) == -1) {
301 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
302 024e9686 2019-05-14 stsp path_got = NULL;
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp }
305 024e9686 2019-05-14 stsp
306 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
307 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
308 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
309 024e9686 2019-05-14 stsp goto done;
310 024e9686 2019-05-14 stsp }
311 024e9686 2019-05-14 stsp
312 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
313 024e9686 2019-05-14 stsp if (err)
314 024e9686 2019-05-14 stsp goto done;
315 024e9686 2019-05-14 stsp
316 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
317 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
318 024e9686 2019-05-14 stsp done:
319 024e9686 2019-05-14 stsp free(path_got);
320 024e9686 2019-05-14 stsp if (err)
321 024e9686 2019-05-14 stsp free(head_ref_name);
322 024e9686 2019-05-14 stsp return err;
323 507dc3bb 2018-12-29 stsp }
324 507dc3bb 2018-12-29 stsp
325 b72f483a 2019-02-05 stsp struct got_object_id *
326 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
327 507dc3bb 2018-12-29 stsp {
328 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
329 86c3caaf 2018-03-09 stsp }
330 86c3caaf 2018-03-09 stsp
331 507dc3bb 2018-12-29 stsp const struct got_error *
332 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
333 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
334 507dc3bb 2018-12-29 stsp {
335 507dc3bb 2018-12-29 stsp const struct got_error *err;
336 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
337 507dc3bb 2018-12-29 stsp char *id_str = NULL;
338 507dc3bb 2018-12-29 stsp char *path_got = NULL;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
341 df6221c7 2023-07-19 stsp worktree->meta_dir) == -1) {
342 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
343 507dc3bb 2018-12-29 stsp path_got = NULL;
344 507dc3bb 2018-12-29 stsp goto done;
345 507dc3bb 2018-12-29 stsp }
346 507dc3bb 2018-12-29 stsp
347 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
348 507dc3bb 2018-12-29 stsp if (err)
349 507dc3bb 2018-12-29 stsp return err;
350 507dc3bb 2018-12-29 stsp
351 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
352 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
353 507dc3bb 2018-12-29 stsp goto done;
354 507dc3bb 2018-12-29 stsp }
355 507dc3bb 2018-12-29 stsp
356 507dc3bb 2018-12-29 stsp /* Record our base commit. */
357 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
358 507dc3bb 2018-12-29 stsp if (err)
359 507dc3bb 2018-12-29 stsp goto done;
360 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
361 507dc3bb 2018-12-29 stsp if (err)
362 507dc3bb 2018-12-29 stsp goto done;
363 507dc3bb 2018-12-29 stsp
364 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
365 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
366 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
367 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
368 507dc3bb 2018-12-29 stsp goto done;
369 507dc3bb 2018-12-29 stsp }
370 507dc3bb 2018-12-29 stsp done:
371 507dc3bb 2018-12-29 stsp if (obj)
372 507dc3bb 2018-12-29 stsp got_object_close(obj);
373 507dc3bb 2018-12-29 stsp free(id_str);
374 507dc3bb 2018-12-29 stsp free(path_got);
375 507dc3bb 2018-12-29 stsp return err;
376 50b0790e 2020-09-11 stsp }
377 50b0790e 2020-09-11 stsp
378 50b0790e 2020-09-11 stsp const struct got_gotconfig *
379 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
380 50b0790e 2020-09-11 stsp {
381 50b0790e 2020-09-11 stsp return worktree->gotconfig;
382 507dc3bb 2018-12-29 stsp }
383 507dc3bb 2018-12-29 stsp
384 9d31a1d8 2018-03-11 stsp static const struct got_error *
385 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
386 86c3caaf 2018-03-09 stsp {
387 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
388 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
389 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
390 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
391 86c3caaf 2018-03-09 stsp return NULL;
392 21908da4 2019-01-13 stsp }
393 21908da4 2019-01-13 stsp
394 21908da4 2019-01-13 stsp static const struct got_error *
395 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
396 4a1ddfc2 2019-01-12 stsp {
397 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
398 4a1ddfc2 2019-01-12 stsp char *abspath;
399 6a390967 2023-07-14 stsp
400 6a390967 2023-07-14 stsp /* We only accept worktree-relative paths here. */
401 6a390967 2023-07-14 stsp if (got_path_is_absolute(path)) {
402 6a390967 2023-07-14 stsp return got_error_fmt(GOT_ERR_BAD_PATH,
403 6a390967 2023-07-14 stsp "%s does not accept absolute paths: %s",
404 6a390967 2023-07-14 stsp __func__, path);
405 6a390967 2023-07-14 stsp }
406 4a1ddfc2 2019-01-12 stsp
407 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
408 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
409 4a1ddfc2 2019-01-12 stsp
410 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
411 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
412 ddcd8544 2019-03-11 stsp struct stat sb;
413 ddcd8544 2019-03-11 stsp err = NULL;
414 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
415 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
416 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
417 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
418 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
419 ddcd8544 2019-03-11 stsp }
420 ddcd8544 2019-03-11 stsp }
421 4a1ddfc2 2019-01-12 stsp free(abspath);
422 68c76935 2019-02-19 stsp return err;
423 68c76935 2019-02-19 stsp }
424 68c76935 2019-02-19 stsp
425 68c76935 2019-02-19 stsp static const struct got_error *
426 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
427 68c76935 2019-02-19 stsp {
428 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
429 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
430 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
431 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
432 68c76935 2019-02-19 stsp
433 68c76935 2019-02-19 stsp *same = 1;
434 68c76935 2019-02-19 stsp
435 230a42bd 2019-05-11 jcs for (;;) {
436 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
437 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
438 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
442 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
443 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
444 68c76935 2019-02-19 stsp break;
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp if (flen1 == 0) {
447 68c76935 2019-02-19 stsp if (flen2 != 0)
448 68c76935 2019-02-19 stsp *same = 0;
449 68c76935 2019-02-19 stsp break;
450 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
451 68c76935 2019-02-19 stsp if (flen1 != 0)
452 68c76935 2019-02-19 stsp *same = 0;
453 68c76935 2019-02-19 stsp break;
454 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
455 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
456 68c76935 2019-02-19 stsp *same = 0;
457 68c76935 2019-02-19 stsp break;
458 68c76935 2019-02-19 stsp }
459 68c76935 2019-02-19 stsp } else {
460 68c76935 2019-02-19 stsp *same = 0;
461 68c76935 2019-02-19 stsp break;
462 68c76935 2019-02-19 stsp }
463 68c76935 2019-02-19 stsp }
464 68c76935 2019-02-19 stsp
465 68c76935 2019-02-19 stsp return err;
466 68c76935 2019-02-19 stsp }
467 68c76935 2019-02-19 stsp
468 68c76935 2019-02-19 stsp static const struct got_error *
469 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
470 68c76935 2019-02-19 stsp {
471 68c76935 2019-02-19 stsp struct stat sb;
472 68c76935 2019-02-19 stsp size_t size1, size2;
473 68c76935 2019-02-19 stsp
474 68c76935 2019-02-19 stsp *same = 1;
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
477 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
478 68c76935 2019-02-19 stsp size1 = sb.st_size;
479 68c76935 2019-02-19 stsp
480 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
481 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
482 68c76935 2019-02-19 stsp size2 = sb.st_size;
483 68c76935 2019-02-19 stsp
484 68c76935 2019-02-19 stsp if (size1 != size2) {
485 68c76935 2019-02-19 stsp *same = 0;
486 68c76935 2019-02-19 stsp return NULL;
487 68c76935 2019-02-19 stsp }
488 68c76935 2019-02-19 stsp
489 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
490 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
491 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
492 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
493 68c76935 2019-02-19 stsp
494 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
495 0e039681 2021-11-15 stsp }
496 0e039681 2021-11-15 stsp
497 0e039681 2021-11-15 stsp static const struct got_error *
498 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
499 0e039681 2021-11-15 stsp {
500 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
501 0e039681 2021-11-15 stsp size_t flen;
502 0e039681 2021-11-15 stsp ssize_t outlen;
503 0e039681 2021-11-15 stsp
504 0e039681 2021-11-15 stsp *outsize = 0;
505 0e039681 2021-11-15 stsp
506 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
507 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
508 0e039681 2021-11-15 stsp
509 0e039681 2021-11-15 stsp for (;;) {
510 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
511 0e039681 2021-11-15 stsp if (flen == 0) {
512 0e039681 2021-11-15 stsp if (ferror(f))
513 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
514 0e039681 2021-11-15 stsp if (feof(f))
515 0e039681 2021-11-15 stsp break;
516 0e039681 2021-11-15 stsp }
517 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
518 0e039681 2021-11-15 stsp if (outlen == -1)
519 0e039681 2021-11-15 stsp return got_error_from_errno("write");
520 0e039681 2021-11-15 stsp if (outlen != flen)
521 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
522 0e039681 2021-11-15 stsp *outsize += outlen;
523 0e039681 2021-11-15 stsp }
524 0e039681 2021-11-15 stsp
525 0e039681 2021-11-15 stsp return NULL;
526 0e039681 2021-11-15 stsp }
527 0e039681 2021-11-15 stsp
528 0e039681 2021-11-15 stsp static const struct got_error *
529 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
530 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
531 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
532 0e039681 2021-11-15 stsp const char *ondisk_path)
533 0e039681 2021-11-15 stsp {
534 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
535 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
536 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
537 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
538 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
539 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
540 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
541 0e039681 2021-11-15 stsp
542 0e039681 2021-11-15 stsp *overlapcnt = 0;
543 0e039681 2021-11-15 stsp
544 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
545 0e039681 2021-11-15 stsp if (err)
546 0e039681 2021-11-15 stsp return err;
547 0e039681 2021-11-15 stsp
548 0e039681 2021-11-15 stsp if (same_content)
549 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
550 0e039681 2021-11-15 stsp
551 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
552 0e039681 2021-11-15 stsp if (err)
553 0e039681 2021-11-15 stsp return err;
554 0e039681 2021-11-15 stsp changed_deriv = !same_content;
555 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
556 0e039681 2021-11-15 stsp if (err)
557 0e039681 2021-11-15 stsp return err;
558 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
559 0e039681 2021-11-15 stsp
560 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
561 0e039681 2021-11-15 stsp *overlapcnt = 1;
562 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
563 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
564 0e039681 2021-11-15 stsp goto done;
565 0e039681 2021-11-15 stsp }
566 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
567 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
568 0e039681 2021-11-15 stsp goto done;
569 0e039681 2021-11-15 stsp }
570 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
571 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
572 0e039681 2021-11-15 stsp goto done;
573 0e039681 2021-11-15 stsp }
574 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
575 b90054ed 2022-11-01 stsp base_path_orig, "");
576 0e039681 2021-11-15 stsp if (err)
577 0e039681 2021-11-15 stsp goto done;
578 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
579 b90054ed 2022-11-01 stsp base_path_deriv, "");
580 0e039681 2021-11-15 stsp if (err)
581 0e039681 2021-11-15 stsp goto done;
582 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
583 b90054ed 2022-11-01 stsp base_path_deriv2, "");
584 0e039681 2021-11-15 stsp if (err)
585 0e039681 2021-11-15 stsp goto done;
586 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
587 0e039681 2021-11-15 stsp if (err)
588 0e039681 2021-11-15 stsp goto done;
589 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
590 0e039681 2021-11-15 stsp if (err)
591 0e039681 2021-11-15 stsp goto done;
592 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
593 0e039681 2021-11-15 stsp if (err)
594 0e039681 2021-11-15 stsp goto done;
595 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
596 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
597 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
598 0e039681 2021-11-15 stsp goto done;
599 0e039681 2021-11-15 stsp }
600 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
601 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
602 0e039681 2021-11-15 stsp label_deriv ? " " : "",
603 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
604 0e039681 2021-11-15 stsp path_deriv) < 0) {
605 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
606 0e039681 2021-11-15 stsp goto done;
607 0e039681 2021-11-15 stsp }
608 0e039681 2021-11-15 stsp if (size_orig > 0) {
609 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
610 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
611 0e039681 2021-11-15 stsp label_orig ? " " : "",
612 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
613 0e039681 2021-11-15 stsp path_orig) < 0) {
614 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
615 0e039681 2021-11-15 stsp goto done;
616 0e039681 2021-11-15 stsp }
617 0e039681 2021-11-15 stsp }
618 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
619 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
620 0e039681 2021-11-15 stsp path_deriv2,
621 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
622 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
623 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
624 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
625 0e039681 2021-11-15 stsp goto done;
626 0e039681 2021-11-15 stsp }
627 0e039681 2021-11-15 stsp } else if (changed_deriv)
628 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
629 0e039681 2021-11-15 stsp else if (changed_deriv2)
630 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
631 0e039681 2021-11-15 stsp done:
632 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
633 0e039681 2021-11-15 stsp err == NULL)
634 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
635 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
636 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
637 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
638 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
639 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
640 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
641 0e039681 2021-11-15 stsp free(path_orig);
642 0e039681 2021-11-15 stsp free(path_deriv);
643 0e039681 2021-11-15 stsp free(path_deriv2);
644 0e039681 2021-11-15 stsp free(base_path_orig);
645 0e039681 2021-11-15 stsp free(base_path_deriv);
646 0e039681 2021-11-15 stsp free(base_path_deriv2);
647 0e039681 2021-11-15 stsp return err;
648 6353ad76 2019-02-08 stsp }
649 6353ad76 2019-02-08 stsp
650 6353ad76 2019-02-08 stsp /*
651 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
652 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
653 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
654 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
655 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
656 6353ad76 2019-02-08 stsp */
657 6353ad76 2019-02-08 stsp static const struct got_error *
658 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
659 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
660 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
661 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
662 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
663 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
664 6353ad76 2019-02-08 stsp {
665 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
666 6353ad76 2019-02-08 stsp int merged_fd = -1;
667 db590691 2021-06-02 stsp FILE *f_merged = NULL;
668 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
669 234035bc 2019-06-01 stsp int overlapcnt = 0;
670 3524bbf9 2020-10-20 stsp char *parent = NULL;
671 6353ad76 2019-02-08 stsp
672 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
673 234035bc 2019-06-01 stsp
674 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
675 3524bbf9 2020-10-20 stsp if (err)
676 3524bbf9 2020-10-20 stsp return err;
677 af54ae4a 2019-02-19 stsp
678 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
679 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
680 3524bbf9 2020-10-20 stsp goto done;
681 3524bbf9 2020-10-20 stsp }
682 af54ae4a 2019-02-19 stsp
683 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
684 6353ad76 2019-02-08 stsp if (err)
685 6353ad76 2019-02-08 stsp goto done;
686 3b9f0f87 2020-07-23 stsp
687 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
688 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
689 0e039681 2021-11-15 stsp if (err) {
690 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
691 0e039681 2021-11-15 stsp goto done;
692 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
693 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
694 0e039681 2021-11-15 stsp ondisk_path);
695 0e039681 2021-11-15 stsp if (err)
696 0e039681 2021-11-15 stsp goto done;
697 0e039681 2021-11-15 stsp }
698 6353ad76 2019-02-08 stsp
699 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
700 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
701 1ee397ad 2019-07-12 stsp if (err)
702 1ee397ad 2019-07-12 stsp goto done;
703 6353ad76 2019-02-08 stsp
704 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
706 816dc654 2019-02-16 stsp goto done;
707 68c76935 2019-02-19 stsp }
708 68c76935 2019-02-19 stsp
709 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
710 db590691 2021-06-02 stsp if (f_merged == NULL) {
711 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
712 db590691 2021-06-02 stsp goto done;
713 db590691 2021-06-02 stsp }
714 db590691 2021-06-02 stsp merged_fd = -1;
715 db590691 2021-06-02 stsp
716 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
717 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
718 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
719 db590691 2021-06-02 stsp f_merged);
720 68c76935 2019-02-19 stsp if (err)
721 68c76935 2019-02-19 stsp goto done;
722 816dc654 2019-02-16 stsp }
723 6353ad76 2019-02-08 stsp
724 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
725 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
726 70a0c8ec 2019-02-20 stsp goto done;
727 70a0c8ec 2019-02-20 stsp }
728 70a0c8ec 2019-02-20 stsp
729 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
730 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
731 230a42bd 2019-05-11 jcs ondisk_path);
732 6353ad76 2019-02-08 stsp goto done;
733 6353ad76 2019-02-08 stsp }
734 6353ad76 2019-02-08 stsp done:
735 fdcb7daf 2019-12-15 stsp if (err) {
736 fdcb7daf 2019-12-15 stsp if (merged_path)
737 fdcb7daf 2019-12-15 stsp unlink(merged_path);
738 3b9f0f87 2020-07-23 stsp }
739 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
740 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
741 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
742 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
743 6353ad76 2019-02-08 stsp free(merged_path);
744 af54ae4a 2019-02-19 stsp free(base_path);
745 3524bbf9 2020-10-20 stsp free(parent);
746 af57b12a 2020-07-23 stsp return err;
747 af57b12a 2020-07-23 stsp }
748 af57b12a 2020-07-23 stsp
749 af57b12a 2020-07-23 stsp static const struct got_error *
750 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
751 af57b12a 2020-07-23 stsp size_t target_len)
752 af57b12a 2020-07-23 stsp {
753 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
754 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
755 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
756 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
757 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
758 af57b12a 2020-07-23 stsp ondisk_path);
759 af57b12a 2020-07-23 stsp return NULL;
760 af57b12a 2020-07-23 stsp }
761 af57b12a 2020-07-23 stsp
762 af57b12a 2020-07-23 stsp /*
763 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
764 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
765 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
766 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
767 993e2a1b 2020-07-23 stsp *
768 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
769 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
770 993e2a1b 2020-07-23 stsp *
771 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
772 993e2a1b 2020-07-23 stsp * has deleted this symlink.
773 11cc08c1 2020-07-23 stsp */
774 11cc08c1 2020-07-23 stsp static const struct got_error *
775 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
776 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
777 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
778 11cc08c1 2020-07-23 stsp {
779 11cc08c1 2020-07-23 stsp const struct got_error *err;
780 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
781 11cc08c1 2020-07-23 stsp FILE *f = NULL;
782 11cc08c1 2020-07-23 stsp
783 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
784 11cc08c1 2020-07-23 stsp if (err)
785 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
786 11cc08c1 2020-07-23 stsp
787 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
788 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
789 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
790 11cc08c1 2020-07-23 stsp goto done;
791 11cc08c1 2020-07-23 stsp }
792 11cc08c1 2020-07-23 stsp
793 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
794 11cc08c1 2020-07-23 stsp if (err)
795 3818e3c4 2020-11-01 naddy goto done;
796 3818e3c4 2020-11-01 naddy
797 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
798 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
799 11cc08c1 2020-07-23 stsp goto done;
800 3818e3c4 2020-11-01 naddy }
801 11cc08c1 2020-07-23 stsp
802 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
803 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
804 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
805 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
806 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
807 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
808 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
809 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
810 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
811 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
812 11cc08c1 2020-07-23 stsp goto done;
813 11cc08c1 2020-07-23 stsp }
814 11cc08c1 2020-07-23 stsp
815 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
816 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
817 11cc08c1 2020-07-23 stsp goto done;
818 11cc08c1 2020-07-23 stsp }
819 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
820 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
821 11cc08c1 2020-07-23 stsp goto done;
822 11cc08c1 2020-07-23 stsp }
823 11cc08c1 2020-07-23 stsp done:
824 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
825 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
826 11cc08c1 2020-07-23 stsp free(path);
827 11cc08c1 2020-07-23 stsp free(id_str);
828 11cc08c1 2020-07-23 stsp free(label_deriv);
829 11cc08c1 2020-07-23 stsp return err;
830 11cc08c1 2020-07-23 stsp }
831 d219f183 2020-07-23 stsp
832 d219f183 2020-07-23 stsp /* forward declaration */
833 d219f183 2020-07-23 stsp static const struct got_error *
834 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
835 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
836 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
837 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
838 11cc08c1 2020-07-23 stsp
839 11cc08c1 2020-07-23 stsp /*
840 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
841 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
842 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
843 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
844 af57b12a 2020-07-23 stsp */
845 af57b12a 2020-07-23 stsp static const struct got_error *
846 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
847 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
848 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
849 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
850 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
851 af57b12a 2020-07-23 stsp {
852 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
853 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
854 af57b12a 2020-07-23 stsp struct stat sb;
855 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
856 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
857 11cc08c1 2020-07-23 stsp int have_local_change = 0;
858 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
859 af57b12a 2020-07-23 stsp
860 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
861 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
862 af57b12a 2020-07-23 stsp
863 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
864 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
865 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
866 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
867 af57b12a 2020-07-23 stsp ondisk_path);
868 af57b12a 2020-07-23 stsp goto done;
869 af57b12a 2020-07-23 stsp }
870 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
871 af57b12a 2020-07-23 stsp
872 526a746f 2020-07-23 stsp if (blob_orig) {
873 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
874 526a746f 2020-07-23 stsp if (err)
875 526a746f 2020-07-23 stsp goto done;
876 526a746f 2020-07-23 stsp }
877 af57b12a 2020-07-23 stsp
878 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
879 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
880 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
881 11cc08c1 2020-07-23 stsp have_local_change = 1;
882 11cc08c1 2020-07-23 stsp
883 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
884 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
885 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
886 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
887 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
888 11cc08c1 2020-07-23 stsp
889 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
890 11cc08c1 2020-07-23 stsp if (ancestor_target) {
891 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
892 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
893 11cc08c1 2020-07-23 stsp path);
894 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
895 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
896 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
897 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
898 11cc08c1 2020-07-23 stsp path);
899 11cc08c1 2020-07-23 stsp } else {
900 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
901 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
902 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
903 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
904 11cc08c1 2020-07-23 stsp if (err)
905 11cc08c1 2020-07-23 stsp goto done;
906 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
907 11cc08c1 2020-07-23 stsp path);
908 11cc08c1 2020-07-23 stsp }
909 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
910 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
911 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
912 af57b12a 2020-07-23 stsp strlen(deriv_target));
913 af57b12a 2020-07-23 stsp if (err)
914 af57b12a 2020-07-23 stsp goto done;
915 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
916 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
917 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
918 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
919 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
920 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
921 ea7786be 2020-07-23 stsp path);
922 ea7786be 2020-07-23 stsp } else {
923 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
924 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
925 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
926 ea7786be 2020-07-23 stsp if (err)
927 ea7786be 2020-07-23 stsp goto done;
928 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
929 ea7786be 2020-07-23 stsp path);
930 ea7786be 2020-07-23 stsp }
931 6353ad76 2019-02-08 stsp }
932 11cc08c1 2020-07-23 stsp
933 af57b12a 2020-07-23 stsp done:
934 af57b12a 2020-07-23 stsp free(ancestor_target);
935 eec2f5a9 2021-06-03 stsp return err;
936 eec2f5a9 2021-06-03 stsp }
937 eec2f5a9 2021-06-03 stsp
938 eec2f5a9 2021-06-03 stsp static const struct got_error *
939 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
940 eec2f5a9 2021-06-03 stsp {
941 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
942 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
943 eec2f5a9 2021-06-03 stsp ssize_t target_len;
944 eec2f5a9 2021-06-03 stsp size_t n;
945 eec2f5a9 2021-06-03 stsp FILE *f;
946 eec2f5a9 2021-06-03 stsp
947 eec2f5a9 2021-06-03 stsp *outfile = NULL;
948 eec2f5a9 2021-06-03 stsp
949 eec2f5a9 2021-06-03 stsp f = got_opentemp();
950 eec2f5a9 2021-06-03 stsp if (f == NULL)
951 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
952 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
953 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
954 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
955 eec2f5a9 2021-06-03 stsp goto done;
956 eec2f5a9 2021-06-03 stsp }
957 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
958 eec2f5a9 2021-06-03 stsp if (n != target_len) {
959 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
960 eec2f5a9 2021-06-03 stsp goto done;
961 eec2f5a9 2021-06-03 stsp }
962 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
963 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
964 eec2f5a9 2021-06-03 stsp goto done;
965 eec2f5a9 2021-06-03 stsp }
966 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
967 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
968 eec2f5a9 2021-06-03 stsp goto done;
969 eec2f5a9 2021-06-03 stsp }
970 eec2f5a9 2021-06-03 stsp done:
971 eec2f5a9 2021-06-03 stsp if (err)
972 eec2f5a9 2021-06-03 stsp fclose(f);
973 eec2f5a9 2021-06-03 stsp else
974 eec2f5a9 2021-06-03 stsp *outfile = f;
975 14c901f1 2019-08-08 stsp return err;
976 14c901f1 2019-08-08 stsp }
977 14c901f1 2019-08-08 stsp
978 14c901f1 2019-08-08 stsp /*
979 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
980 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
981 14c901f1 2019-08-08 stsp * acts as the second derived version.
982 14c901f1 2019-08-08 stsp */
983 14c901f1 2019-08-08 stsp static const struct got_error *
984 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
985 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
986 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
987 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
988 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
989 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
990 14c901f1 2019-08-08 stsp {
991 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
992 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
993 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
994 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
995 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
996 14c901f1 2019-08-08 stsp
997 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
998 14c901f1 2019-08-08 stsp
999 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1000 ed6b5030 2020-10-20 stsp if (err)
1001 ed6b5030 2020-10-20 stsp return err;
1002 14c901f1 2019-08-08 stsp
1003 67a66647 2021-05-31 stsp if (blob_orig) {
1004 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1005 67a66647 2021-05-31 stsp parent) == -1) {
1006 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1007 67a66647 2021-05-31 stsp base_path = NULL;
1008 67a66647 2021-05-31 stsp goto done;
1009 67a66647 2021-05-31 stsp }
1010 67a66647 2021-05-31 stsp
1011 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
1012 b90054ed 2022-11-01 stsp base_path, "");
1013 67a66647 2021-05-31 stsp if (err)
1014 67a66647 2021-05-31 stsp goto done;
1015 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1016 67a66647 2021-05-31 stsp blob_orig);
1017 67a66647 2021-05-31 stsp if (err)
1018 67a66647 2021-05-31 stsp goto done;
1019 67a66647 2021-05-31 stsp free(base_path);
1020 db590691 2021-06-02 stsp } else {
1021 db590691 2021-06-02 stsp /*
1022 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1023 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1024 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1025 db590691 2021-06-02 stsp */
1026 db590691 2021-06-02 stsp f_orig = got_opentemp();
1027 db590691 2021-06-02 stsp if (f_orig == NULL) {
1028 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1029 db590691 2021-06-02 stsp goto done;
1030 db590691 2021-06-02 stsp }
1031 67a66647 2021-05-31 stsp }
1032 67a66647 2021-05-31 stsp
1033 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1034 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1035 14c901f1 2019-08-08 stsp base_path = NULL;
1036 14c901f1 2019-08-08 stsp goto done;
1037 14c901f1 2019-08-08 stsp }
1038 14c901f1 2019-08-08 stsp
1039 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1040 14c901f1 2019-08-08 stsp if (err)
1041 14c901f1 2019-08-08 stsp goto done;
1042 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1043 14c901f1 2019-08-08 stsp blob_deriv);
1044 14c901f1 2019-08-08 stsp if (err)
1045 14c901f1 2019-08-08 stsp goto done;
1046 14c901f1 2019-08-08 stsp
1047 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1048 14c901f1 2019-08-08 stsp if (err)
1049 14c901f1 2019-08-08 stsp goto done;
1050 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1051 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1052 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1053 14c901f1 2019-08-08 stsp goto done;
1054 eec2f5a9 2021-06-03 stsp }
1055 eec2f5a9 2021-06-03 stsp
1056 5e91dae4 2022-08-30 stsp /*
1057 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1058 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1059 eec2f5a9 2021-06-03 stsp */
1060 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1061 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1062 eec2f5a9 2021-06-03 stsp if (err)
1063 eec2f5a9 2021-06-03 stsp goto done;
1064 eec2f5a9 2021-06-03 stsp } else {
1065 eec2f5a9 2021-06-03 stsp int fd;
1066 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1067 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1069 eec2f5a9 2021-06-03 stsp goto done;
1070 eec2f5a9 2021-06-03 stsp }
1071 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1072 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1073 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1074 eec2f5a9 2021-06-03 stsp close(fd);
1075 eec2f5a9 2021-06-03 stsp goto done;
1076 eec2f5a9 2021-06-03 stsp }
1077 14c901f1 2019-08-08 stsp }
1078 14c901f1 2019-08-08 stsp
1079 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1080 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1081 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1082 14c901f1 2019-08-08 stsp done:
1083 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1084 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1085 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1086 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1087 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1088 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1089 14c901f1 2019-08-08 stsp free(base_path);
1090 67a66647 2021-05-31 stsp if (blob_orig_path) {
1091 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1092 67a66647 2021-05-31 stsp free(blob_orig_path);
1093 67a66647 2021-05-31 stsp }
1094 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1095 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1096 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1097 14c901f1 2019-08-08 stsp }
1098 6353ad76 2019-02-08 stsp free(id_str);
1099 818c7501 2019-07-11 stsp free(label_deriv);
1100 ed6b5030 2020-10-20 stsp free(parent);
1101 4a1ddfc2 2019-01-12 stsp return err;
1102 4a1ddfc2 2019-01-12 stsp }
1103 4a1ddfc2 2019-01-12 stsp
1104 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1105 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1106 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1107 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1108 13d9040b 2019-03-27 stsp {
1109 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1110 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1111 13d9040b 2019-03-27 stsp
1112 65b05cec 2020-07-23 stsp *new_iep = NULL;
1113 65b05cec 2020-07-23 stsp
1114 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1115 054041d0 2020-06-24 stsp if (err)
1116 054041d0 2020-06-24 stsp return err;
1117 054041d0 2020-06-24 stsp
1118 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1119 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1120 054041d0 2020-06-24 stsp if (err)
1121 054041d0 2020-06-24 stsp goto done;
1122 054041d0 2020-06-24 stsp
1123 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1124 054041d0 2020-06-24 stsp done:
1125 054041d0 2020-06-24 stsp if (err)
1126 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1127 65b05cec 2020-07-23 stsp else
1128 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1129 13d9040b 2019-03-27 stsp return err;
1130 1ebedb77 2019-10-19 stsp }
1131 1ebedb77 2019-10-19 stsp
1132 1ebedb77 2019-10-19 stsp static mode_t
1133 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1134 1ebedb77 2019-10-19 stsp {
1135 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1136 1ebedb77 2019-10-19 stsp
1137 1ebedb77 2019-10-19 stsp if (executable) {
1138 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1139 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1140 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1141 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1142 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1143 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1144 1ebedb77 2019-10-19 stsp }
1145 1ebedb77 2019-10-19 stsp
1146 b2b3fce1 2022-10-29 op return st_mode;
1147 13d9040b 2019-03-27 stsp }
1148 13d9040b 2019-03-27 stsp
1149 8ba819a3 2020-07-23 stsp /* forward declaration */
1150 13d9040b 2019-03-27 stsp static const struct got_error *
1151 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1152 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1153 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1154 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1155 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1156 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1157 8ba819a3 2020-07-23 stsp
1158 5a1fbc73 2020-07-23 stsp /*
1159 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1160 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1161 5a1fbc73 2020-07-23 stsp */
1162 8ba819a3 2020-07-23 stsp static const struct got_error *
1163 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1164 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1165 5a1fbc73 2020-07-23 stsp {
1166 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1167 5a1fbc73 2020-07-23 stsp ssize_t elen;
1168 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1169 5a1fbc73 2020-07-23 stsp int fd;
1170 5a1fbc73 2020-07-23 stsp
1171 c6e8a826 2021-04-05 stsp *did_something = 0;
1172 c6e8a826 2021-04-05 stsp
1173 5a1fbc73 2020-07-23 stsp /*
1174 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1175 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1176 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1177 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1178 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1179 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1180 5a1fbc73 2020-07-23 stsp */
1181 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1182 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1183 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1184 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1185 5a1fbc73 2020-07-23 stsp
1186 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1187 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1188 5a1fbc73 2020-07-23 stsp if (elen == -1)
1189 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1190 5a1fbc73 2020-07-23 stsp
1191 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1192 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1193 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1194 5a1fbc73 2020-07-23 stsp }
1195 5a1fbc73 2020-07-23 stsp
1196 c6e8a826 2021-04-05 stsp *did_something = 1;
1197 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1198 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1199 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1200 5a1fbc73 2020-07-23 stsp return err;
1201 3c1ec782 2020-07-23 stsp }
1202 3c1ec782 2020-07-23 stsp
1203 3c1ec782 2020-07-23 stsp static const struct got_error *
1204 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1205 df6221c7 2023-07-19 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path,
1206 df6221c7 2023-07-19 stsp const char *meta_dir)
1207 3c1ec782 2020-07-23 stsp {
1208 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1209 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1210 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1211 3c1ec782 2020-07-23 stsp
1212 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1213 3c1ec782 2020-07-23 stsp
1214 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1215 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1216 3c1ec782 2020-07-23 stsp return NULL;
1217 3c1ec782 2020-07-23 stsp }
1218 3c1ec782 2020-07-23 stsp
1219 3c1ec782 2020-07-23 stsp /*
1220 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1221 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1222 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1223 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1224 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1225 3c1ec782 2020-07-23 stsp */
1226 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1227 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1228 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1229 ce031e9e 2020-10-20 stsp if (err)
1230 ce031e9e 2020-10-20 stsp return err;
1231 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1232 ce031e9e 2020-10-20 stsp free(parent);
1233 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1234 ce031e9e 2020-10-20 stsp }
1235 ce031e9e 2020-10-20 stsp free(parent);
1236 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1237 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1238 3c1ec782 2020-07-23 stsp free(abspath);
1239 3c1ec782 2020-07-23 stsp return err;
1240 3c1ec782 2020-07-23 stsp }
1241 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1242 3c1ec782 2020-07-23 stsp free(abspath);
1243 3c1ec782 2020-07-23 stsp if (err)
1244 3c1ec782 2020-07-23 stsp return err;
1245 3c1ec782 2020-07-23 stsp } else {
1246 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1247 3c1ec782 2020-07-23 stsp if (err)
1248 3c1ec782 2020-07-23 stsp return err;
1249 3c1ec782 2020-07-23 stsp }
1250 3c1ec782 2020-07-23 stsp
1251 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1252 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1253 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1254 3c1ec782 2020-07-23 stsp return NULL;
1255 3c1ec782 2020-07-23 stsp }
1256 3c1ec782 2020-07-23 stsp
1257 df6221c7 2023-07-19 stsp /* Do not allow symlinks pointing into the meta directory. */
1258 df6221c7 2023-07-19 stsp if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1259 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1260 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1261 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1262 3c1ec782 2020-07-23 stsp
1263 3c1ec782 2020-07-23 stsp free(path_got);
1264 3c1ec782 2020-07-23 stsp return NULL;
1265 5a1fbc73 2020-07-23 stsp }
1266 5a1fbc73 2020-07-23 stsp
1267 5a1fbc73 2020-07-23 stsp static const struct got_error *
1268 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1269 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1270 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1271 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1272 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1273 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1274 9d31a1d8 2018-03-11 stsp {
1275 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1276 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1277 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1278 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1279 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1280 8ba819a3 2020-07-23 stsp
1281 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1282 2e1fa222 2020-07-23 stsp
1283 3c29341b 2022-07-21 florian /*
1284 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1285 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1286 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1287 8ba819a3 2020-07-23 stsp * in the blob object.
1288 8ba819a3 2020-07-23 stsp */
1289 8ba819a3 2020-07-23 stsp do {
1290 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1291 538b6881 2022-07-21 florian if (err)
1292 538b6881 2022-07-21 florian return err;
1293 538b6881 2022-07-21 florian
1294 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1295 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1296 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1297 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1298 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1299 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1301 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1302 3b9f0f87 2020-07-23 stsp progress_arg);
1303 8ba819a3 2020-07-23 stsp }
1304 8ba819a3 2020-07-23 stsp if (len > 0) {
1305 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1306 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1307 8ba819a3 2020-07-23 stsp len - hdrlen);
1308 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1309 8ba819a3 2020-07-23 stsp hdrlen = 0;
1310 8ba819a3 2020-07-23 stsp }
1311 8ba819a3 2020-07-23 stsp } while (len != 0);
1312 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1313 8ba819a3 2020-07-23 stsp
1314 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1315 df6221c7 2023-07-19 stsp ondisk_path, worktree->root_path, worktree->meta_dir);
1316 3c1ec782 2020-07-23 stsp if (err)
1317 3c1ec782 2020-07-23 stsp return err;
1318 8ba819a3 2020-07-23 stsp
1319 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1320 906c123b 2020-07-23 stsp /* install as a regular file */
1321 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1322 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1323 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1324 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1325 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1326 3c29341b 2022-07-21 florian 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 3c29341b 2022-07-21 florian 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 3c29341b 2022-07-21 florian 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 3c29341b 2022-07-21 florian 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 3c29341b 2022-07-21 florian return err;
1360 6a390967 2023-07-14 stsp err = got_path_mkdir(parent);
1361 f4994adc 2020-10-20 stsp free(parent);
1362 f35fa46a 2020-07-23 stsp if (err)
1363 3c29341b 2022-07-21 florian 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 6a390967 2023-07-14 stsp if (progress_cb) {
1371 6a390967 2023-07-14 stsp err = (*progress_cb)(progress_arg,
1372 6a390967 2023-07-14 stsp reverting_versioned_file ?
1373 6a390967 2023-07-14 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD,
1374 6a390967 2023-07-14 stsp path);
1375 6a390967 2023-07-14 stsp }
1376 3c29341b 2022-07-21 florian 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 3b9f0f87 2020-07-23 stsp path_is_unversioned, 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 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1409 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1410 8ba819a3 2020-07-23 stsp {
1411 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1412 507dc3bb 2018-12-29 stsp int fd = -1;
1413 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1414 507dc3bb 2018-12-29 stsp int update = 0;
1415 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1416 b2b3fce1 2022-10-29 op mode_t mode;
1417 8ba819a3 2020-07-23 stsp
1418 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1419 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1420 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1421 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1422 07fa9365 2023-03-10 stsp if (errno == ENOENT || errno == ENOTDIR) {
1423 f5375317 2020-10-20 stsp char *parent;
1424 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1425 f5375317 2020-10-20 stsp if (err)
1426 f5375317 2020-10-20 stsp return err;
1427 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1428 07fa9365 2023-03-10 stsp if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1429 07fa9365 2023-03-10 stsp err = got_error_path(path, err->code);
1430 f5375317 2020-10-20 stsp free(parent);
1431 21908da4 2019-01-13 stsp if (err)
1432 21908da4 2019-01-13 stsp return err;
1433 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1434 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1435 b2b3fce1 2022-10-29 op mode);
1436 21908da4 2019-01-13 stsp if (fd == -1)
1437 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1438 230a42bd 2019-05-11 jcs ondisk_path);
1439 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1440 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1441 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1442 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1443 3b9f0f87 2020-07-23 stsp goto done;
1444 3b9f0f87 2020-07-23 stsp }
1445 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1446 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1447 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1448 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1449 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1450 507dc3bb 2018-12-29 stsp goto done;
1451 d70b8e30 2018-12-27 stsp } else {
1452 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1453 b90054ed 2022-11-01 stsp ondisk_path, "");
1454 507dc3bb 2018-12-29 stsp if (err)
1455 507dc3bb 2018-12-29 stsp goto done;
1456 507dc3bb 2018-12-29 stsp update = 1;
1457 b2b3fce1 2022-10-29 op
1458 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1459 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1460 b2b3fce1 2022-10-29 op tmppath);
1461 b2b3fce1 2022-10-29 op goto done;
1462 b2b3fce1 2022-10-29 op }
1463 9d31a1d8 2018-03-11 stsp }
1464 507dc3bb 2018-12-29 stsp } else
1465 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1466 3818e3c4 2020-11-01 naddy }
1467 3818e3c4 2020-11-01 naddy
1468 bd6aa359 2020-07-23 stsp if (progress_cb) {
1469 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1470 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1471 bd6aa359 2020-07-23 stsp path);
1472 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1473 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1474 bd6aa359 2020-07-23 stsp path);
1475 bd6aa359 2020-07-23 stsp else
1476 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1477 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1478 bd6aa359 2020-07-23 stsp if (err)
1479 bd6aa359 2020-07-23 stsp goto done;
1480 bd6aa359 2020-07-23 stsp }
1481 d7b62c98 2018-12-27 stsp
1482 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1483 9d31a1d8 2018-03-11 stsp do {
1484 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1485 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1486 9d31a1d8 2018-03-11 stsp if (err)
1487 9d31a1d8 2018-03-11 stsp break;
1488 9d31a1d8 2018-03-11 stsp if (len > 0) {
1489 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1490 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1491 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1492 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1493 b87c6f83 2018-12-24 stsp goto done;
1494 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1495 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1496 b87c6f83 2018-12-24 stsp goto done;
1497 9d31a1d8 2018-03-11 stsp }
1498 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1499 9d31a1d8 2018-03-11 stsp }
1500 9d31a1d8 2018-03-11 stsp } while (len != 0);
1501 9d31a1d8 2018-03-11 stsp
1502 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1503 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1504 816dc654 2019-02-16 stsp goto done;
1505 816dc654 2019-02-16 stsp }
1506 9d31a1d8 2018-03-11 stsp
1507 507dc3bb 2018-12-29 stsp if (update) {
1508 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1509 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1510 f6d8c0ac 2020-11-09 stsp goto done;
1511 f6d8c0ac 2020-11-09 stsp }
1512 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1513 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1514 230a42bd 2019-05-11 jcs ondisk_path);
1515 68ed9ba5 2019-02-10 stsp goto done;
1516 68ed9ba5 2019-02-10 stsp }
1517 63df146d 2020-11-09 stsp free(tmppath);
1518 63df146d 2020-11-09 stsp tmppath = NULL;
1519 507dc3bb 2018-12-29 stsp }
1520 507dc3bb 2018-12-29 stsp
1521 9d31a1d8 2018-03-11 stsp done:
1522 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1523 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1524 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1525 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1526 507dc3bb 2018-12-29 stsp free(tmppath);
1527 6353ad76 2019-02-08 stsp return err;
1528 6353ad76 2019-02-08 stsp }
1529 6353ad76 2019-02-08 stsp
1530 12383673 2023-02-18 mark /*
1531 12383673 2023-02-18 mark * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1532 12383673 2023-02-18 mark * conflict marker is found in newly added lines only.
1533 12383673 2023-02-18 mark */
1534 6353ad76 2019-02-08 stsp static const struct got_error *
1535 12383673 2023-02-18 mark get_modified_file_content_status(unsigned char *status,
1536 12383673 2023-02-18 mark struct got_blob_object *blob, const char *path, struct stat *sb,
1537 12383673 2023-02-18 mark FILE *ondisk_file)
1538 7154f6ce 2019-03-27 stsp {
1539 d952957d 2023-02-19 mark const struct got_error *err, *free_err;
1540 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1541 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1542 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1543 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1544 7154f6ce 2019-03-27 stsp };
1545 d952957d 2023-02-19 mark FILE *f1 = NULL;
1546 d952957d 2023-02-19 mark struct got_diffreg_result *diffreg_result = NULL;
1547 d952957d 2023-02-19 mark struct diff_result *r;
1548 d952957d 2023-02-19 mark int nchunks_parsed, n, i = 0, ln = 0;
1549 9bdd68dd 2020-01-02 naddy char *line = NULL;
1550 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1551 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1552 7154f6ce 2019-03-27 stsp
1553 d952957d 2023-02-19 mark if (*status != GOT_STATUS_MODIFY)
1554 d952957d 2023-02-19 mark return NULL;
1555 12383673 2023-02-18 mark
1556 d952957d 2023-02-19 mark f1 = got_opentemp();
1557 d952957d 2023-02-19 mark if (f1 == NULL)
1558 d952957d 2023-02-19 mark return got_error_from_errno("got_opentemp");
1559 12383673 2023-02-18 mark
1560 d952957d 2023-02-19 mark if (blob) {
1561 d952957d 2023-02-19 mark got_object_blob_rewind(blob);
1562 d952957d 2023-02-19 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1563 12383673 2023-02-18 mark if (err)
1564 12383673 2023-02-18 mark goto done;
1565 d952957d 2023-02-19 mark }
1566 12383673 2023-02-18 mark
1567 d952957d 2023-02-19 mark err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1568 d952957d 2023-02-19 mark 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1569 d952957d 2023-02-19 mark if (err)
1570 d952957d 2023-02-19 mark goto done;
1571 d952957d 2023-02-19 mark
1572 d952957d 2023-02-19 mark r = diffreg_result->result;
1573 d952957d 2023-02-19 mark
1574 d952957d 2023-02-19 mark for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1575 d952957d 2023-02-19 mark struct diff_chunk *c;
1576 d952957d 2023-02-19 mark struct diff_chunk_context cc = {};
1577 7783f73c 2023-02-20 mark off_t pos;
1578 d952957d 2023-02-19 mark
1579 d952957d 2023-02-19 mark /*
1580 d952957d 2023-02-19 mark * We can optimise a little by advancing straight
1581 d952957d 2023-02-19 mark * to the next chunk if this one has no added lines.
1582 d952957d 2023-02-19 mark */
1583 d952957d 2023-02-19 mark c = diff_chunk_get(r, n);
1584 d952957d 2023-02-19 mark
1585 45e6b2f4 2023-02-20 mark if (diff_chunk_type(c) != CHUNK_PLUS) {
1586 d952957d 2023-02-19 mark nchunks_parsed = 1;
1587 7783f73c 2023-02-20 mark continue; /* removed or unchanged lines */
1588 7154f6ce 2019-03-27 stsp }
1589 7154f6ce 2019-03-27 stsp
1590 7783f73c 2023-02-20 mark pos = diff_chunk_get_right_start_pos(c);
1591 7783f73c 2023-02-20 mark if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1592 7783f73c 2023-02-20 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1593 7783f73c 2023-02-20 mark goto done;
1594 7154f6ce 2019-03-27 stsp }
1595 d952957d 2023-02-19 mark
1596 7783f73c 2023-02-20 mark diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1597 7783f73c 2023-02-20 mark ln = cc.right.start;
1598 7783f73c 2023-02-20 mark
1599 d952957d 2023-02-19 mark while (ln < cc.right.end) {
1600 d952957d 2023-02-19 mark linelen = getline(&line, &linesize, ondisk_file);
1601 d952957d 2023-02-19 mark if (linelen == -1) {
1602 d952957d 2023-02-19 mark if (feof(ondisk_file))
1603 d952957d 2023-02-19 mark break;
1604 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1605 d952957d 2023-02-19 mark break;
1606 d952957d 2023-02-19 mark }
1607 d952957d 2023-02-19 mark
1608 d952957d 2023-02-19 mark if (line && strncmp(line, markers[i],
1609 d952957d 2023-02-19 mark strlen(markers[i])) == 0) {
1610 d952957d 2023-02-19 mark if (strcmp(markers[i],
1611 d952957d 2023-02-19 mark GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1612 d952957d 2023-02-19 mark *status = GOT_STATUS_CONFLICT;
1613 d952957d 2023-02-19 mark goto done;
1614 d952957d 2023-02-19 mark } else
1615 d952957d 2023-02-19 mark i++;
1616 d952957d 2023-02-19 mark }
1617 d952957d 2023-02-19 mark ++ln;
1618 d952957d 2023-02-19 mark }
1619 7154f6ce 2019-03-27 stsp }
1620 12383673 2023-02-18 mark
1621 12383673 2023-02-18 mark done:
1622 9bdd68dd 2020-01-02 naddy free(line);
1623 12383673 2023-02-18 mark if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1624 12383673 2023-02-18 mark err = got_error_from_errno("fclose");
1625 d952957d 2023-02-19 mark free_err = got_diffreg_result_free(diffreg_result);
1626 d952957d 2023-02-19 mark if (err == NULL)
1627 d952957d 2023-02-19 mark err = free_err;
1628 7154f6ce 2019-03-27 stsp
1629 7154f6ce 2019-03-27 stsp return err;
1630 e2b1e152 2019-07-13 stsp }
1631 e2b1e152 2019-07-13 stsp
1632 e2b1e152 2019-07-13 stsp static int
1633 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1634 1ebedb77 2019-10-19 stsp {
1635 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1636 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1637 1ebedb77 2019-10-19 stsp }
1638 1ebedb77 2019-10-19 stsp
1639 1ebedb77 2019-10-19 stsp static int
1640 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1641 e2b1e152 2019-07-13 stsp {
1642 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1643 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1644 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1645 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1646 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1647 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1648 c363b2c1 2019-08-03 stsp }
1649 c363b2c1 2019-08-03 stsp
1650 c363b2c1 2019-08-03 stsp static unsigned char
1651 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1652 c363b2c1 2019-08-03 stsp {
1653 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1654 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1655 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1656 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1657 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1658 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1659 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1660 c363b2c1 2019-08-03 stsp default:
1661 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1662 a919d5c4 2020-07-23 stsp }
1663 a919d5c4 2020-07-23 stsp }
1664 a919d5c4 2020-07-23 stsp
1665 a919d5c4 2020-07-23 stsp static const struct got_error *
1666 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1667 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1668 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1669 a919d5c4 2020-07-23 stsp {
1670 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1671 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1672 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1673 a919d5c4 2020-07-23 stsp ssize_t elen;
1674 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1675 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1676 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1677 a919d5c4 2020-07-23 stsp
1678 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1679 a919d5c4 2020-07-23 stsp
1680 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1681 a919d5c4 2020-07-23 stsp do {
1682 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1683 a919d5c4 2020-07-23 stsp if (err)
1684 a919d5c4 2020-07-23 stsp return err;
1685 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1686 a919d5c4 2020-07-23 stsp /*
1687 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1688 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1689 a919d5c4 2020-07-23 stsp */
1690 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1691 a919d5c4 2020-07-23 stsp }
1692 a919d5c4 2020-07-23 stsp if (len > 0) {
1693 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1694 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1695 a919d5c4 2020-07-23 stsp len - hdrlen);
1696 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1697 a919d5c4 2020-07-23 stsp hdrlen = 0;
1698 a919d5c4 2020-07-23 stsp }
1699 a919d5c4 2020-07-23 stsp } while (len != 0);
1700 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1701 a919d5c4 2020-07-23 stsp
1702 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1703 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1704 a919d5c4 2020-07-23 stsp if (elen == -1)
1705 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1706 a919d5c4 2020-07-23 stsp } else {
1707 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1708 a919d5c4 2020-07-23 stsp if (elen == -1)
1709 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1710 c363b2c1 2019-08-03 stsp }
1711 a919d5c4 2020-07-23 stsp
1712 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1713 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1714 a919d5c4 2020-07-23 stsp
1715 a919d5c4 2020-07-23 stsp return NULL;
1716 7154f6ce 2019-03-27 stsp }
1717 7154f6ce 2019-03-27 stsp
1718 7154f6ce 2019-03-27 stsp static const struct got_error *
1719 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1720 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1721 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1722 6353ad76 2019-02-08 stsp {
1723 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1724 6353ad76 2019-02-08 stsp struct got_object_id id;
1725 6353ad76 2019-02-08 stsp size_t hdrlen;
1726 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1727 6353ad76 2019-02-08 stsp FILE *f = NULL;
1728 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1729 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1730 6353ad76 2019-02-08 stsp size_t flen, blen;
1731 2c4740ad 2023-02-12 mark unsigned char staged_status;
1732 6353ad76 2019-02-08 stsp
1733 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
1734 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1735 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1736 6353ad76 2019-02-08 stsp
1737 7f91a133 2019-12-13 stsp /*
1738 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1739 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1740 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1741 7f91a133 2019-12-13 stsp */
1742 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1743 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1744 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1745 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1746 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1747 882ef1b9 2019-12-13 stsp else
1748 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1749 882ef1b9 2019-12-13 stsp goto done;
1750 882ef1b9 2019-12-13 stsp }
1751 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1752 3d35a492 2019-12-13 stsp goto done;
1753 3d35a492 2019-12-13 stsp }
1754 7f91a133 2019-12-13 stsp } else {
1755 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1756 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1757 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1758 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1759 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1760 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1761 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1762 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1763 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1764 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1765 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1766 3d35a492 2019-12-13 stsp else
1767 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1768 3d35a492 2019-12-13 stsp goto done;
1769 3d35a492 2019-12-13 stsp }
1770 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1771 1338848f 2019-12-13 stsp goto done;
1772 a378724f 2019-02-10 stsp }
1773 a378724f 2019-02-10 stsp }
1774 6353ad76 2019-02-08 stsp
1775 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1776 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1777 1338848f 2019-12-13 stsp goto done;
1778 3f148bc6 2019-07-27 stsp }
1779 efdd40df 2019-07-27 stsp
1780 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1781 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1782 1338848f 2019-12-13 stsp goto done;
1783 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1784 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1785 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1786 1338848f 2019-12-13 stsp goto done;
1787 d00136be 2019-03-26 stsp }
1788 b8f41171 2019-02-10 stsp
1789 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1790 f179e66d 2020-07-23 stsp goto done;
1791 f179e66d 2020-07-23 stsp
1792 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1793 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1794 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1795 1338848f 2019-12-13 stsp goto done;
1796 f179e66d 2020-07-23 stsp }
1797 6353ad76 2019-02-08 stsp
1798 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1799 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1800 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1801 c363b2c1 2019-08-03 stsp else
1802 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1803 c363b2c1 2019-08-03 stsp
1804 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1805 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1806 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1807 eb81bc23 2022-06-28 tracey goto done;
1808 eb81bc23 2022-06-28 tracey }
1809 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1810 6353ad76 2019-02-08 stsp if (err)
1811 1338848f 2019-12-13 stsp goto done;
1812 6353ad76 2019-02-08 stsp
1813 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1814 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1815 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1816 a919d5c4 2020-07-23 stsp goto done;
1817 a919d5c4 2020-07-23 stsp }
1818 a919d5c4 2020-07-23 stsp
1819 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1820 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1821 ab0d4361 2019-12-13 stsp if (fd == -1) {
1822 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1823 ab0d4361 2019-12-13 stsp goto done;
1824 ab0d4361 2019-12-13 stsp }
1825 3d35a492 2019-12-13 stsp }
1826 3d35a492 2019-12-13 stsp
1827 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1828 6353ad76 2019-02-08 stsp if (f == NULL) {
1829 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1830 6353ad76 2019-02-08 stsp goto done;
1831 6353ad76 2019-02-08 stsp }
1832 1338848f 2019-12-13 stsp fd = -1;
1833 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1834 656b1f76 2019-05-11 jcs for (;;) {
1835 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1836 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1837 6353ad76 2019-02-08 stsp if (err)
1838 7154f6ce 2019-03-27 stsp goto done;
1839 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1840 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1841 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1842 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1843 7154f6ce 2019-03-27 stsp goto done;
1844 80c5c120 2019-02-19 stsp }
1845 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1846 6353ad76 2019-02-08 stsp if (flen != 0)
1847 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1848 6353ad76 2019-02-08 stsp break;
1849 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1850 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1851 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1852 6353ad76 2019-02-08 stsp break;
1853 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1854 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1855 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1856 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1857 6353ad76 2019-02-08 stsp break;
1858 6353ad76 2019-02-08 stsp }
1859 6353ad76 2019-02-08 stsp } else {
1860 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1861 6353ad76 2019-02-08 stsp break;
1862 6353ad76 2019-02-08 stsp }
1863 6353ad76 2019-02-08 stsp hdrlen = 0;
1864 6353ad76 2019-02-08 stsp }
1865 7154f6ce 2019-03-27 stsp
1866 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1867 7154f6ce 2019-03-27 stsp rewind(f);
1868 12383673 2023-02-18 mark err = get_modified_file_content_status(status, blob, ie->path,
1869 12383673 2023-02-18 mark sb, f);
1870 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1871 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1872 6353ad76 2019-02-08 stsp done:
1873 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1874 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1875 6353ad76 2019-02-08 stsp if (blob)
1876 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1877 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1878 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1879 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1880 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1881 9d31a1d8 2018-03-11 stsp return err;
1882 e2b1e152 2019-07-13 stsp }
1883 e2b1e152 2019-07-13 stsp
1884 e2b1e152 2019-07-13 stsp /*
1885 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1886 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1887 e2b1e152 2019-07-13 stsp */
1888 e2b1e152 2019-07-13 stsp static const struct got_error *
1889 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1890 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1891 e2b1e152 2019-07-13 stsp {
1892 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1893 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1894 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1895 e2b1e152 2019-07-13 stsp
1896 e2b1e152 2019-07-13 stsp return NULL;
1897 9d31a1d8 2018-03-11 stsp }
1898 9d31a1d8 2018-03-11 stsp
1899 07fa9365 2023-03-10 stsp static const struct got_error *remove_ondisk_file(const char *, const char *);
1900 07fa9365 2023-03-10 stsp
1901 9d31a1d8 2018-03-11 stsp static const struct got_error *
1902 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1903 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1904 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1905 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1906 0584f854 2019-04-06 stsp void *progress_arg)
1907 9d31a1d8 2018-03-11 stsp {
1908 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1909 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1910 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1911 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1912 b8f41171 2019-02-10 stsp struct stat sb;
1913 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1914 9d31a1d8 2018-03-11 stsp
1915 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1916 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1917 6353ad76 2019-02-08 stsp
1918 abb4604f 2019-07-27 stsp if (ie) {
1919 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1920 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1921 a76c42e6 2019-08-03 stsp goto done;
1922 a76c42e6 2019-08-03 stsp }
1923 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1924 7f91a133 2019-12-13 stsp repo);
1925 abb4604f 2019-07-27 stsp if (err)
1926 abb4604f 2019-07-27 stsp goto done;
1927 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1928 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1929 c90c8ce3 2020-07-23 stsp } else {
1930 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1931 07fa9365 2023-03-10 stsp if (errno != ENOENT && errno != ENOTDIR) {
1932 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1933 f6764181 2021-09-24 stsp ondisk_path);
1934 f6764181 2021-09-24 stsp goto done;
1935 f6764181 2021-09-24 stsp }
1936 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1937 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1938 f6764181 2021-09-24 stsp } else {
1939 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1940 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1941 f6764181 2021-09-24 stsp else
1942 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1943 f6764181 2021-09-24 stsp }
1944 c90c8ce3 2020-07-23 stsp }
1945 6353ad76 2019-02-08 stsp
1946 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1947 2c41dce7 2021-06-27 stsp if (ie)
1948 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1949 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1950 b8f41171 2019-02-10 stsp goto done;
1951 b8f41171 2019-02-10 stsp }
1952 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1953 a769b60b 2021-06-27 stsp if (ie)
1954 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1955 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1956 5036ab18 2020-04-18 stsp path);
1957 5036ab18 2020-04-18 stsp goto done;
1958 07fa9365 2023-03-10 stsp }
1959 07fa9365 2023-03-10 stsp
1960 07fa9365 2023-03-10 stsp if (S_ISDIR(te->mode)) { /* file changing into a directory */
1961 07fa9365 2023-03-10 stsp if (status == GOT_STATUS_UNVERSIONED) {
1962 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, status, path);
1963 07fa9365 2023-03-10 stsp } else if (status != GOT_STATUS_NO_CHANGE &&
1964 07fa9365 2023-03-10 stsp status != GOT_STATUS_DELETE &&
1965 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1966 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1967 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg,
1968 07fa9365 2023-03-10 stsp GOT_STATUS_CANNOT_DELETE, path);
1969 07fa9365 2023-03-10 stsp } else if (ie) {
1970 07fa9365 2023-03-10 stsp if (status != GOT_STATUS_DELETE &&
1971 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1972 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1973 07fa9365 2023-03-10 stsp err = remove_ondisk_file(worktree->root_path,
1974 07fa9365 2023-03-10 stsp ie->path);
1975 07fa9365 2023-03-10 stsp if (err && !(err->code == GOT_ERR_ERRNO &&
1976 07fa9365 2023-03-10 stsp errno == ENOENT))
1977 07fa9365 2023-03-10 stsp goto done;
1978 07fa9365 2023-03-10 stsp }
1979 07fa9365 2023-03-10 stsp got_fileindex_entry_remove(fileindex, ie);
1980 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1981 07fa9365 2023-03-10 stsp ie->path);
1982 07fa9365 2023-03-10 stsp }
1983 07fa9365 2023-03-10 stsp goto done; /* nothing else to do */
1984 5036ab18 2020-04-18 stsp }
1985 b8f41171 2019-02-10 stsp
1986 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1987 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1988 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1989 c6e8a826 2021-04-05 stsp /*
1990 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1991 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1992 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1993 c6e8a826 2021-04-05 stsp * updating contents of this file.
1994 c6e8a826 2021-04-05 stsp */
1995 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1996 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1997 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1998 c6e8a826 2021-04-05 stsp /* Same commit. */
1999 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2000 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2001 e2b1e152 2019-07-13 stsp if (err)
2002 e2b1e152 2019-07-13 stsp goto done;
2003 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2004 b8f41171 2019-02-10 stsp path);
2005 6353ad76 2019-02-08 stsp goto done;
2006 a378724f 2019-02-10 stsp }
2007 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
2008 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
2009 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
2010 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2011 fd785a9a 2023-04-12 stsp if (got_fileindex_entry_has_commit(ie)) {
2012 fd785a9a 2023-04-12 stsp /* Update the base commit ID of this file. */
2013 fd785a9a 2023-04-12 stsp memcpy(ie->commit_sha1,
2014 fd785a9a 2023-04-12 stsp worktree->base_commit_id->sha1,
2015 fd785a9a 2023-04-12 stsp sizeof(ie->commit_sha1));
2016 fd785a9a 2023-04-12 stsp }
2017 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2018 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2019 0f58026f 2021-04-05 stsp if (err)
2020 0f58026f 2021-04-05 stsp goto done;
2021 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2022 0f58026f 2021-04-05 stsp path);
2023 b8f41171 2019-02-10 stsp goto done;
2024 e2b1e152 2019-07-13 stsp }
2025 9d31a1d8 2018-03-11 stsp }
2026 9d31a1d8 2018-03-11 stsp
2027 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
2028 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
2029 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2030 eb81bc23 2022-06-28 tracey goto done;
2031 eb81bc23 2022-06-28 tracey }
2032 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2033 8da9e5f4 2019-01-12 stsp if (err)
2034 6353ad76 2019-02-08 stsp goto done;
2035 512f0d0e 2019-01-02 stsp
2036 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2037 234035bc 2019-06-01 stsp int update_timestamps;
2038 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2039 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2040 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2041 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
2042 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
2043 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2044 eb81bc23 2022-06-28 tracey goto done;
2045 eb81bc23 2022-06-28 tracey }
2046 234035bc 2019-06-01 stsp struct got_object_id id2;
2047 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
2048 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2049 eb81bc23 2022-06-28 tracey fd2);
2050 234035bc 2019-06-01 stsp if (err)
2051 f69721c3 2019-10-21 stsp goto done;
2052 f69721c3 2019-10-21 stsp }
2053 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2054 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2055 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2056 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2057 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2058 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2059 f69721c3 2019-10-21 stsp goto done;
2060 f69721c3 2019-10-21 stsp }
2061 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2062 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2063 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2064 234035bc 2019-06-01 stsp goto done;
2065 f69721c3 2019-10-21 stsp }
2066 234035bc 2019-06-01 stsp }
2067 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2068 36bf999c 2020-07-23 stsp char *link_target;
2069 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2070 36bf999c 2020-07-23 stsp if (err)
2071 36bf999c 2020-07-23 stsp goto done;
2072 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2073 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2074 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2075 36bf999c 2020-07-23 stsp free(link_target);
2076 993e2a1b 2020-07-23 stsp } else {
2077 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2078 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2079 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2080 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2081 993e2a1b 2020-07-23 stsp }
2082 f69721c3 2019-10-21 stsp free(label_orig);
2083 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2084 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2085 eb81bc23 2022-06-28 tracey goto done;
2086 eb81bc23 2022-06-28 tracey }
2087 234035bc 2019-06-01 stsp if (blob2)
2088 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2089 909d120e 2019-09-22 stsp if (err)
2090 909d120e 2019-09-22 stsp goto done;
2091 234035bc 2019-06-01 stsp /*
2092 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2093 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2094 234035bc 2019-06-01 stsp * unmodified files again.
2095 234035bc 2019-06-01 stsp */
2096 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2097 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2098 234035bc 2019-06-01 stsp update_timestamps);
2099 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2100 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2101 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2102 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2103 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2104 1ee397ad 2019-07-12 stsp if (err)
2105 1ee397ad 2019-07-12 stsp goto done;
2106 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2108 13d9040b 2019-03-27 stsp if (err)
2109 13d9040b 2019-03-27 stsp goto done;
2110 13d9040b 2019-03-27 stsp } else {
2111 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2112 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2113 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2114 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2115 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2116 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2117 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2118 2e1fa222 2020-07-23 stsp } else {
2119 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2120 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2121 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2122 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2123 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2124 2e1fa222 2020-07-23 stsp }
2125 13d9040b 2019-03-27 stsp if (err)
2126 13d9040b 2019-03-27 stsp goto done;
2127 65b05cec 2020-07-23 stsp
2128 054041d0 2020-06-24 stsp if (ie) {
2129 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2130 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2131 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2132 054041d0 2020-06-24 stsp } else {
2133 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2134 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2135 054041d0 2020-06-24 stsp &blob->id);
2136 054041d0 2020-06-24 stsp }
2137 13d9040b 2019-03-27 stsp if (err)
2138 13d9040b 2019-03-27 stsp goto done;
2139 65b05cec 2020-07-23 stsp
2140 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2141 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2142 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2143 2e1fa222 2020-07-23 stsp }
2144 13d9040b 2019-03-27 stsp }
2145 eb81bc23 2022-06-28 tracey
2146 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2147 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2148 eb81bc23 2022-06-28 tracey goto done;
2149 eb81bc23 2022-06-28 tracey }
2150 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2151 6353ad76 2019-02-08 stsp done:
2152 6353ad76 2019-02-08 stsp free(ondisk_path);
2153 8da9e5f4 2019-01-12 stsp return err;
2154 90285c3b 2019-01-08 stsp }
2155 90285c3b 2019-01-08 stsp
2156 90285c3b 2019-01-08 stsp static const struct got_error *
2157 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2158 90285c3b 2019-01-08 stsp {
2159 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2160 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2161 90285c3b 2019-01-08 stsp
2162 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2163 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2164 90285c3b 2019-01-08 stsp
2165 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2166 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2167 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2168 c97665ae 2019-01-13 stsp } else {
2169 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2170 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2171 1c4cdd89 2021-06-20 stsp if (err)
2172 1c4cdd89 2021-06-20 stsp goto done;
2173 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2174 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2175 fddefe3b 2020-10-20 stsp free(ondisk_path);
2176 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2177 1c4cdd89 2021-06-20 stsp parent = NULL;
2178 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2179 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2180 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2181 fddefe3b 2020-10-20 stsp ondisk_path);
2182 90285c3b 2019-01-08 stsp break;
2183 90285c3b 2019-01-08 stsp }
2184 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2185 1c4cdd89 2021-06-20 stsp if (err)
2186 1c4cdd89 2021-06-20 stsp break;
2187 1c4cdd89 2021-06-20 stsp }
2188 90285c3b 2019-01-08 stsp }
2189 1c4cdd89 2021-06-20 stsp done:
2190 90285c3b 2019-01-08 stsp free(ondisk_path);
2191 1c4cdd89 2021-06-20 stsp free(parent);
2192 90285c3b 2019-01-08 stsp return err;
2193 512f0d0e 2019-01-02 stsp }
2194 512f0d0e 2019-01-02 stsp
2195 708d8e67 2019-03-27 stsp static const struct got_error *
2196 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2197 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2198 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2199 708d8e67 2019-03-27 stsp {
2200 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2201 708d8e67 2019-03-27 stsp unsigned char status;
2202 708d8e67 2019-03-27 stsp struct stat sb;
2203 708d8e67 2019-03-27 stsp char *ondisk_path;
2204 708d8e67 2019-03-27 stsp
2205 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2206 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2207 a76c42e6 2019-08-03 stsp
2208 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2209 708d8e67 2019-03-27 stsp == -1)
2210 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2211 708d8e67 2019-03-27 stsp
2212 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2213 708d8e67 2019-03-27 stsp if (err)
2214 5a58a424 2020-06-23 stsp goto done;
2215 708d8e67 2019-03-27 stsp
2216 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2217 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2218 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2219 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2220 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2221 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2222 993e2a1b 2020-07-23 stsp goto done;
2223 993e2a1b 2020-07-23 stsp }
2224 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2225 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2226 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2227 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2228 993e2a1b 2020-07-23 stsp if (err)
2229 993e2a1b 2020-07-23 stsp goto done;
2230 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2231 993e2a1b 2020-07-23 stsp ie->path);
2232 993e2a1b 2020-07-23 stsp goto done;
2233 993e2a1b 2020-07-23 stsp }
2234 993e2a1b 2020-07-23 stsp
2235 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2236 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2237 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2238 1ee397ad 2019-07-12 stsp if (err)
2239 5a58a424 2020-06-23 stsp goto done;
2240 fc6346c4 2019-03-27 stsp /*
2241 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2242 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2243 fc6346c4 2019-03-27 stsp */
2244 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2245 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2246 fc6346c4 2019-03-27 stsp } else {
2247 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2248 1ee397ad 2019-07-12 stsp if (err)
2249 5a58a424 2020-06-23 stsp goto done;
2250 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2251 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2252 fc6346c4 2019-03-27 stsp if (err)
2253 5a58a424 2020-06-23 stsp goto done;
2254 fc6346c4 2019-03-27 stsp }
2255 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2256 708d8e67 2019-03-27 stsp }
2257 5a58a424 2020-06-23 stsp done:
2258 5a58a424 2020-06-23 stsp free(ondisk_path);
2259 fc6346c4 2019-03-27 stsp return err;
2260 708d8e67 2019-03-27 stsp }
2261 708d8e67 2019-03-27 stsp
2262 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2263 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2264 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2265 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2266 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2267 8da9e5f4 2019-01-12 stsp void *progress_arg;
2268 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2269 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2270 8da9e5f4 2019-01-12 stsp };
2271 8da9e5f4 2019-01-12 stsp
2272 512f0d0e 2019-01-02 stsp static const struct got_error *
2273 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2274 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2275 512f0d0e 2019-01-02 stsp {
2276 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2277 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2278 0584f854 2019-04-06 stsp
2279 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2280 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2281 f6b8c3c2 2023-07-24 stsp if (err)
2282 f6b8c3c2 2023-07-24 stsp return err;
2283 f6b8c3c2 2023-07-24 stsp }
2284 512f0d0e 2019-01-02 stsp
2285 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2286 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2287 8da9e5f4 2019-01-12 stsp }
2288 512f0d0e 2019-01-02 stsp
2289 8da9e5f4 2019-01-12 stsp static const struct got_error *
2290 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2291 8da9e5f4 2019-01-12 stsp {
2292 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2293 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2294 7a9df742 2019-01-08 stsp
2295 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2296 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2297 f6b8c3c2 2023-07-24 stsp if (err)
2298 f6b8c3c2 2023-07-24 stsp return err;
2299 f6b8c3c2 2023-07-24 stsp }
2300 0584f854 2019-04-06 stsp
2301 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2302 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2303 9d31a1d8 2018-03-11 stsp }
2304 9d31a1d8 2018-03-11 stsp
2305 9d31a1d8 2018-03-11 stsp static const struct got_error *
2306 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2307 9d31a1d8 2018-03-11 stsp {
2308 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2309 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2310 8da9e5f4 2019-01-12 stsp char *path;
2311 9d31a1d8 2018-03-11 stsp
2312 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2313 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2314 f6b8c3c2 2023-07-24 stsp if (err)
2315 f6b8c3c2 2023-07-24 stsp return err;
2316 f6b8c3c2 2023-07-24 stsp }
2317 0584f854 2019-04-06 stsp
2318 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2319 6a390967 2023-07-14 stsp return NULL;
2320 6a390967 2023-07-14 stsp
2321 6a390967 2023-07-14 stsp if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2322 63c5ca5d 2019-08-24 stsp return NULL;
2323 63c5ca5d 2019-08-24 stsp
2324 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2325 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2326 8da9e5f4 2019-01-12 stsp == -1)
2327 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2328 9d31a1d8 2018-03-11 stsp
2329 6a390967 2023-07-14 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2330 6a390967 2023-07-14 stsp a->repo, a->progress_cb, a->progress_arg);
2331 9d31a1d8 2018-03-11 stsp
2332 8da9e5f4 2019-01-12 stsp free(path);
2333 0cd1c46a 2019-03-11 stsp return err;
2334 0cd1c46a 2019-03-11 stsp }
2335 0cd1c46a 2019-03-11 stsp
2336 b2118c49 2020-07-28 stsp const struct got_error *
2337 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2338 b2118c49 2020-07-28 stsp {
2339 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2340 b2118c49 2020-07-28 stsp
2341 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2342 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2343 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2344 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2345 b2118c49 2020-07-28 stsp }
2346 b2118c49 2020-07-28 stsp
2347 b2118c49 2020-07-28 stsp return NULL;
2348 b2118c49 2020-07-28 stsp }
2349 b2118c49 2020-07-28 stsp
2350 818c7501 2019-07-11 stsp static const struct got_error *
2351 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2352 0cd1c46a 2019-03-11 stsp {
2353 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2354 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2355 0cd1c46a 2019-03-11 stsp
2356 517bab73 2019-03-11 stsp *refname = NULL;
2357 517bab73 2019-03-11 stsp
2358 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2359 b2118c49 2020-07-28 stsp if (err)
2360 b2118c49 2020-07-28 stsp return err;
2361 0cd1c46a 2019-03-11 stsp
2362 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2363 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2364 0647c563 2019-03-11 stsp *refname = NULL;
2365 0cd1c46a 2019-03-11 stsp }
2366 517bab73 2019-03-11 stsp free(uuidstr);
2367 517bab73 2019-03-11 stsp return err;
2368 517bab73 2019-03-11 stsp }
2369 517bab73 2019-03-11 stsp
2370 818c7501 2019-07-11 stsp const struct got_error *
2371 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2372 9587e6cc 2023-01-28 mark const char *prefix)
2373 9587e6cc 2023-01-28 mark {
2374 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2375 9587e6cc 2023-01-28 mark }
2376 9587e6cc 2023-01-28 mark
2377 5662d61d 2023-07-03 jrick static const struct got_error *
2378 5662d61d 2023-07-03 jrick get_base_ref_name(char **refname, struct got_worktree *worktree)
2379 818c7501 2019-07-11 stsp {
2380 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2381 818c7501 2019-07-11 stsp }
2382 818c7501 2019-07-11 stsp
2383 818c7501 2019-07-11 stsp static const struct got_error *
2384 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2385 818c7501 2019-07-11 stsp {
2386 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2387 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_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_newbase_symref_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, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2394 818c7501 2019-07-11 stsp }
2395 818c7501 2019-07-11 stsp
2396 818c7501 2019-07-11 stsp static const struct got_error *
2397 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2398 818c7501 2019-07-11 stsp {
2399 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2400 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_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_commit_ref_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_COMMIT_REF_PREFIX);
2408 0ebf8283 2019-07-24 stsp }
2409 0ebf8283 2019-07-24 stsp
2410 0ebf8283 2019-07-24 stsp static const struct got_error *
2411 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2412 0ebf8283 2019-07-24 stsp {
2413 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2414 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_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_branch_symref_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_BRANCH_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_base_commit_ref_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_BASE_COMMIT_REF_PREFIX);
2429 818c7501 2019-07-11 stsp }
2430 818c7501 2019-07-11 stsp
2431 0ebf8283 2019-07-24 stsp static const struct got_error *
2432 0ebf8283 2019-07-24 stsp get_histedit_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_COMMIT_REF_PREFIX);
2436 0ebf8283 2019-07-24 stsp }
2437 818c7501 2019-07-11 stsp
2438 0ebf8283 2019-07-24 stsp const struct got_error *
2439 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2440 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2441 0ebf8283 2019-07-24 stsp {
2442 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2443 df6221c7 2023-07-19 stsp worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2444 0ebf8283 2019-07-24 stsp *path = NULL;
2445 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2446 0ebf8283 2019-07-24 stsp }
2447 0ebf8283 2019-07-24 stsp return NULL;
2448 f259c4c1 2021-09-24 stsp }
2449 f259c4c1 2021-09-24 stsp
2450 f259c4c1 2021-09-24 stsp static const struct got_error *
2451 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2452 f259c4c1 2021-09-24 stsp {
2453 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2454 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2455 0ebf8283 2019-07-24 stsp }
2456 0ebf8283 2019-07-24 stsp
2457 f259c4c1 2021-09-24 stsp static const struct got_error *
2458 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2459 f259c4c1 2021-09-24 stsp {
2460 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2461 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2462 f259c4c1 2021-09-24 stsp }
2463 f259c4c1 2021-09-24 stsp
2464 517bab73 2019-03-11 stsp /*
2465 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2466 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2467 517bab73 2019-03-11 stsp */
2468 517bab73 2019-03-11 stsp static const struct got_error *
2469 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2470 517bab73 2019-03-11 stsp {
2471 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2472 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2473 517bab73 2019-03-11 stsp char *refname;
2474 517bab73 2019-03-11 stsp
2475 5662d61d 2023-07-03 jrick err = get_base_ref_name(&refname, worktree);
2476 517bab73 2019-03-11 stsp if (err)
2477 517bab73 2019-03-11 stsp return err;
2478 0cd1c46a 2019-03-11 stsp
2479 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2480 0cd1c46a 2019-03-11 stsp if (err)
2481 0cd1c46a 2019-03-11 stsp goto done;
2482 0cd1c46a 2019-03-11 stsp
2483 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2484 0cd1c46a 2019-03-11 stsp done:
2485 0cd1c46a 2019-03-11 stsp free(refname);
2486 0cd1c46a 2019-03-11 stsp if (ref)
2487 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2488 3e3a69f1 2019-07-25 stsp return err;
2489 3e3a69f1 2019-07-25 stsp }
2490 3e3a69f1 2019-07-25 stsp
2491 3e3a69f1 2019-07-25 stsp static const struct got_error *
2492 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2493 3e3a69f1 2019-07-25 stsp {
2494 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2495 3e3a69f1 2019-07-25 stsp
2496 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2497 df6221c7 2023-07-19 stsp worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2498 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2499 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2500 3e3a69f1 2019-07-25 stsp }
2501 9d31a1d8 2018-03-11 stsp return err;
2502 9d31a1d8 2018-03-11 stsp }
2503 9d31a1d8 2018-03-11 stsp
2504 3e3a69f1 2019-07-25 stsp
2505 ebf99748 2019-05-09 stsp static const struct got_error *
2506 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2507 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2508 ebf99748 2019-05-09 stsp {
2509 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2510 ebf99748 2019-05-09 stsp FILE *index = NULL;
2511 0cd1c46a 2019-03-11 stsp
2512 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2513 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2514 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2515 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2516 ebf99748 2019-05-09 stsp
2517 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2518 3e3a69f1 2019-07-25 stsp if (err)
2519 ebf99748 2019-05-09 stsp goto done;
2520 ebf99748 2019-05-09 stsp
2521 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2522 ebf99748 2019-05-09 stsp if (index == NULL) {
2523 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2524 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2525 ebf99748 2019-05-09 stsp } else {
2526 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2527 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2528 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2529 ebf99748 2019-05-09 stsp }
2530 ebf99748 2019-05-09 stsp done:
2531 ebf99748 2019-05-09 stsp if (err) {
2532 ebf99748 2019-05-09 stsp free(*fileindex_path);
2533 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2534 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2535 ebf99748 2019-05-09 stsp *fileindex = NULL;
2536 ebf99748 2019-05-09 stsp }
2537 ebf99748 2019-05-09 stsp return err;
2538 ebf99748 2019-05-09 stsp }
2539 c932eeeb 2019-05-22 stsp
2540 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2541 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2542 c932eeeb 2019-05-22 stsp const char *path;
2543 c932eeeb 2019-05-22 stsp size_t path_len;
2544 c932eeeb 2019-05-22 stsp const char *entry_name;
2545 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2546 a484d721 2019-06-10 stsp void *progress_arg;
2547 c932eeeb 2019-05-22 stsp };
2548 ebf99748 2019-05-09 stsp
2549 c932eeeb 2019-05-22 stsp static const struct got_error *
2550 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2551 c932eeeb 2019-05-22 stsp {
2552 1ee397ad 2019-07-12 stsp const struct got_error *err;
2553 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2554 c932eeeb 2019-05-22 stsp
2555 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2556 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2557 c932eeeb 2019-05-22 stsp return NULL;
2558 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2559 102ff934 2019-06-11 stsp return NULL;
2560 102ff934 2019-06-11 stsp
2561 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2562 a769b60b 2021-06-27 stsp return NULL;
2563 a769b60b 2021-06-27 stsp
2564 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2565 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2566 c932eeeb 2019-05-22 stsp return NULL;
2567 c932eeeb 2019-05-22 stsp
2568 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2569 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2570 edd02c5e 2019-07-11 stsp ie->path);
2571 1ee397ad 2019-07-12 stsp if (err)
2572 1ee397ad 2019-07-12 stsp return err;
2573 1ee397ad 2019-07-12 stsp }
2574 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2575 c932eeeb 2019-05-22 stsp return NULL;
2576 a615e0e7 2020-12-16 stsp }
2577 a615e0e7 2020-12-16 stsp
2578 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2579 a615e0e7 2020-12-16 stsp static const struct got_error *
2580 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2581 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2582 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2583 a615e0e7 2020-12-16 stsp {
2584 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2585 a615e0e7 2020-12-16 stsp
2586 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2587 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2588 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2589 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2590 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2591 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2592 a615e0e7 2020-12-16 stsp
2593 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2594 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2595 9c6338c4 2019-06-02 stsp }
2596 9c6338c4 2019-06-02 stsp
2597 9c6338c4 2019-06-02 stsp static const struct got_error *
2598 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2599 9c6338c4 2019-06-02 stsp {
2600 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2601 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2602 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2603 867630bb 2020-01-17 stsp struct timespec timeout;
2604 9c6338c4 2019-06-02 stsp
2605 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2606 b90054ed 2022-11-01 stsp fileindex_path, "");
2607 9c6338c4 2019-06-02 stsp if (err)
2608 9c6338c4 2019-06-02 stsp goto done;
2609 9c6338c4 2019-06-02 stsp
2610 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2611 9c6338c4 2019-06-02 stsp if (err)
2612 9c6338c4 2019-06-02 stsp goto done;
2613 9c6338c4 2019-06-02 stsp
2614 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2615 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2616 9c6338c4 2019-06-02 stsp fileindex_path);
2617 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2618 9c6338c4 2019-06-02 stsp }
2619 867630bb 2020-01-17 stsp
2620 867630bb 2020-01-17 stsp /*
2621 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2622 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2623 867630bb 2020-01-17 stsp * was recorded in the file index.
2624 867630bb 2020-01-17 stsp */
2625 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2626 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2627 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2628 9c6338c4 2019-06-02 stsp done:
2629 9c6338c4 2019-06-02 stsp if (new_index)
2630 9c6338c4 2019-06-02 stsp fclose(new_index);
2631 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2632 9c6338c4 2019-06-02 stsp return err;
2633 c932eeeb 2019-05-22 stsp }
2634 6ced4176 2019-07-12 stsp
2635 6ced4176 2019-07-12 stsp static const struct got_error *
2636 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2637 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2638 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2639 a44927cc 2022-04-07 stsp struct got_repository *repo)
2640 6ced4176 2019-07-12 stsp {
2641 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2642 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2643 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2644 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2645 6ced4176 2019-07-12 stsp
2646 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2647 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2648 6ced4176 2019-07-12 stsp *tree_id = NULL;
2649 6ced4176 2019-07-12 stsp
2650 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2651 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2652 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2653 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2654 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2655 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2656 6ced4176 2019-07-12 stsp goto done;
2657 6ced4176 2019-07-12 stsp }
2658 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2659 a44927cc 2022-04-07 stsp worktree->path_prefix);
2660 6ced4176 2019-07-12 stsp if (err)
2661 6ced4176 2019-07-12 stsp goto done;
2662 6ced4176 2019-07-12 stsp return NULL;
2663 6ced4176 2019-07-12 stsp }
2664 6ced4176 2019-07-12 stsp
2665 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2666 6ced4176 2019-07-12 stsp
2667 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2668 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2669 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2670 6ced4176 2019-07-12 stsp goto done;
2671 6ced4176 2019-07-12 stsp }
2672 6ced4176 2019-07-12 stsp
2673 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2674 6ced4176 2019-07-12 stsp if (err)
2675 6ced4176 2019-07-12 stsp goto done;
2676 6ced4176 2019-07-12 stsp
2677 6ced4176 2019-07-12 stsp free(in_repo_path);
2678 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2679 6ced4176 2019-07-12 stsp
2680 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2681 6ced4176 2019-07-12 stsp if (err)
2682 6ced4176 2019-07-12 stsp goto done;
2683 c932eeeb 2019-05-22 stsp
2684 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2685 6ced4176 2019-07-12 stsp /* Check out a single file. */
2686 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2687 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2688 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2689 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2690 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2691 6ced4176 2019-07-12 stsp goto done;
2692 6ced4176 2019-07-12 stsp }
2693 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2694 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2695 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2696 6ced4176 2019-07-12 stsp goto done;
2697 6ced4176 2019-07-12 stsp }
2698 6ced4176 2019-07-12 stsp } else {
2699 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2700 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2701 6ced4176 2019-07-12 stsp if (err)
2702 6ced4176 2019-07-12 stsp return err;
2703 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2704 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2705 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2706 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2707 6ced4176 2019-07-12 stsp goto done;
2708 6ced4176 2019-07-12 stsp }
2709 6ced4176 2019-07-12 stsp }
2710 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2711 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2712 6ced4176 2019-07-12 stsp } else {
2713 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2714 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2715 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2716 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2717 6ced4176 2019-07-12 stsp goto done;
2718 6ced4176 2019-07-12 stsp }
2719 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2720 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2721 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2722 6ced4176 2019-07-12 stsp goto done;
2723 6ced4176 2019-07-12 stsp }
2724 6ced4176 2019-07-12 stsp }
2725 6ced4176 2019-07-12 stsp done:
2726 6ced4176 2019-07-12 stsp free(id);
2727 6ced4176 2019-07-12 stsp free(in_repo_path);
2728 6ced4176 2019-07-12 stsp if (err) {
2729 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2730 6ced4176 2019-07-12 stsp free(*tree_relpath);
2731 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2732 6ced4176 2019-07-12 stsp free(*tree_id);
2733 6ced4176 2019-07-12 stsp *tree_id = NULL;
2734 6ced4176 2019-07-12 stsp }
2735 07ed472d 2019-07-12 stsp return err;
2736 07ed472d 2019-07-12 stsp }
2737 07ed472d 2019-07-12 stsp
2738 07ed472d 2019-07-12 stsp static const struct got_error *
2739 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2740 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2741 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2742 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2743 07ed472d 2019-07-12 stsp {
2744 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2745 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2746 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2747 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2748 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2749 07ed472d 2019-07-12 stsp
2750 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2751 7f47418f 2019-12-20 stsp if (err) {
2752 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2753 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2754 7f47418f 2019-12-20 stsp goto done;
2755 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2756 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2757 7f47418f 2019-12-20 stsp if (err)
2758 7f47418f 2019-12-20 stsp return err;
2759 7f47418f 2019-12-20 stsp }
2760 07ed472d 2019-07-12 stsp
2761 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2762 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2763 07ed472d 2019-07-12 stsp if (err)
2764 07ed472d 2019-07-12 stsp goto done;
2765 07ed472d 2019-07-12 stsp
2766 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2767 07ed472d 2019-07-12 stsp if (err)
2768 07ed472d 2019-07-12 stsp goto done;
2769 07ed472d 2019-07-12 stsp
2770 07ed472d 2019-07-12 stsp if (entry_name &&
2771 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2772 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2773 07ed472d 2019-07-12 stsp goto done;
2774 07ed472d 2019-07-12 stsp }
2775 07ed472d 2019-07-12 stsp
2776 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2777 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2778 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2779 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2780 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2781 07ed472d 2019-07-12 stsp arg.repo = repo;
2782 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2783 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2784 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2785 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2786 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2787 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2788 07ed472d 2019-07-12 stsp done:
2789 07ed472d 2019-07-12 stsp if (tree)
2790 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2791 07ed472d 2019-07-12 stsp if (commit)
2792 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2793 6ced4176 2019-07-12 stsp return err;
2794 6ced4176 2019-07-12 stsp }
2795 6ced4176 2019-07-12 stsp
2796 9d31a1d8 2018-03-11 stsp const struct got_error *
2797 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2798 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2799 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2800 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2801 9d31a1d8 2018-03-11 stsp {
2802 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2803 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2804 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2805 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2806 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2807 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2808 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2809 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2810 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2811 f2ea84fa 2019-07-27 stsp int entry_type;
2812 f2ea84fa 2019-07-27 stsp char *relpath;
2813 f2ea84fa 2019-07-27 stsp char *entry_name;
2814 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2815 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2816 9d31a1d8 2018-03-11 stsp
2817 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2818 f2ea84fa 2019-07-27 stsp
2819 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2820 9d31a1d8 2018-03-11 stsp if (err)
2821 9d31a1d8 2018-03-11 stsp return err;
2822 a44927cc 2022-04-07 stsp
2823 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2824 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2825 a44927cc 2022-04-07 stsp if (err)
2826 a44927cc 2022-04-07 stsp goto done;
2827 93a30277 2018-12-24 stsp
2828 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2829 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2830 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2831 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2832 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2833 f2ea84fa 2019-07-27 stsp goto done;
2834 f2ea84fa 2019-07-27 stsp }
2835 6ced4176 2019-07-12 stsp
2836 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2837 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2838 a44927cc 2022-04-07 stsp worktree, repo);
2839 f2ea84fa 2019-07-27 stsp if (err) {
2840 f2ea84fa 2019-07-27 stsp free(tpd);
2841 6ced4176 2019-07-12 stsp goto done;
2842 6ced4176 2019-07-12 stsp }
2843 f2ea84fa 2019-07-27 stsp
2844 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2845 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2846 f2ea84fa 2019-07-27 stsp if (err) {
2847 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2848 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2849 f2ea84fa 2019-07-27 stsp free(tpd);
2850 f2ea84fa 2019-07-27 stsp goto done;
2851 f2ea84fa 2019-07-27 stsp }
2852 f2ea84fa 2019-07-27 stsp } else
2853 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2854 f2ea84fa 2019-07-27 stsp
2855 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2856 6ced4176 2019-07-12 stsp }
2857 6ced4176 2019-07-12 stsp
2858 51514078 2018-12-25 stsp /*
2859 51514078 2018-12-25 stsp * Read the file index.
2860 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2861 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2862 51514078 2018-12-25 stsp */
2863 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2864 8da9e5f4 2019-01-12 stsp if (err)
2865 c4cdcb68 2019-04-03 stsp goto done;
2866 c4cdcb68 2019-04-03 stsp
2867 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2868 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2869 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2870 f2ea84fa 2019-07-27 stsp
2871 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2872 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2873 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2874 f2ea84fa 2019-07-27 stsp if (err)
2875 f2ea84fa 2019-07-27 stsp break;
2876 f2ea84fa 2019-07-27 stsp
2877 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2878 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2879 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2880 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2881 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2882 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2883 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2884 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2885 f2ea84fa 2019-07-27 stsp if (err)
2886 f2ea84fa 2019-07-27 stsp break;
2887 f2ea84fa 2019-07-27 stsp
2888 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2889 711d9cd9 2019-07-12 stsp }
2890 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2891 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2892 af12c6b9 2019-06-04 stsp err = sync_err;
2893 9d31a1d8 2018-03-11 stsp done:
2894 9c6338c4 2019-06-02 stsp free(fileindex_path);
2895 edfa7d7f 2018-09-11 stsp if (tree)
2896 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2897 9d31a1d8 2018-03-11 stsp if (commit)
2898 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2899 5ade8233 2019-07-12 stsp if (fileindex)
2900 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2901 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2902 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2903 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2904 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2905 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2906 f2ea84fa 2019-07-27 stsp free(tpd);
2907 f2ea84fa 2019-07-27 stsp }
2908 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2909 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2910 9d31a1d8 2018-03-11 stsp err = unlockerr;
2911 f8d1f275 2019-02-04 stsp return err;
2912 f8d1f275 2019-02-04 stsp }
2913 f8d1f275 2019-02-04 stsp
2914 9a298e5c 2023-03-10 stsp static const struct got_error *
2915 9a298e5c 2023-03-10 stsp add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2916 9a298e5c 2023-03-10 stsp struct got_fileindex_entry *ie, const char *ondisk_path,
2917 9a298e5c 2023-03-10 stsp const char *path2, struct got_blob_object *blob2, mode_t mode2,
2918 9a298e5c 2023-03-10 stsp int restoring_missing_file, int reverting_versioned_file,
2919 9a298e5c 2023-03-10 stsp int path_is_unversioned, int allow_bad_symlinks,
2920 9a298e5c 2023-03-10 stsp struct got_repository *repo,
2921 9a298e5c 2023-03-10 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2922 9a298e5c 2023-03-10 stsp {
2923 9a298e5c 2023-03-10 stsp const struct got_error *err = NULL;
2924 9a298e5c 2023-03-10 stsp int is_bad_symlink = 0;
2925 9a298e5c 2023-03-10 stsp
2926 9a298e5c 2023-03-10 stsp if (S_ISLNK(mode2)) {
2927 9a298e5c 2023-03-10 stsp err = install_symlink(&is_bad_symlink,
2928 9a298e5c 2023-03-10 stsp worktree, ondisk_path, path2, blob2,
2929 9a298e5c 2023-03-10 stsp restoring_missing_file,
2930 9a298e5c 2023-03-10 stsp reverting_versioned_file,
2931 9a298e5c 2023-03-10 stsp path_is_unversioned, allow_bad_symlinks,
2932 9a298e5c 2023-03-10 stsp repo, progress_cb, progress_arg);
2933 9a298e5c 2023-03-10 stsp } else {
2934 9a298e5c 2023-03-10 stsp err = install_blob(worktree, ondisk_path, path2,
2935 9a298e5c 2023-03-10 stsp mode2, GOT_DEFAULT_FILE_MODE, blob2,
2936 9a298e5c 2023-03-10 stsp restoring_missing_file, reverting_versioned_file, 0,
2937 9a298e5c 2023-03-10 stsp path_is_unversioned, repo, progress_cb, progress_arg);
2938 9a298e5c 2023-03-10 stsp }
2939 9a298e5c 2023-03-10 stsp if (err)
2940 9a298e5c 2023-03-10 stsp return err;
2941 9a298e5c 2023-03-10 stsp if (ie == NULL) {
2942 9a298e5c 2023-03-10 stsp /* Adding an unversioned file. */
2943 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_alloc(&ie, path2);
2944 9a298e5c 2023-03-10 stsp if (err)
2945 9a298e5c 2023-03-10 stsp return err;
2946 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2947 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, NULL, NULL, 1);
2948 9a298e5c 2023-03-10 stsp if (err) {
2949 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2950 9a298e5c 2023-03-10 stsp return err;
2951 9a298e5c 2023-03-10 stsp }
2952 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_add(fileindex, ie);
2953 9a298e5c 2023-03-10 stsp if (err) {
2954 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2955 9a298e5c 2023-03-10 stsp return err;
2956 9a298e5c 2023-03-10 stsp }
2957 9a298e5c 2023-03-10 stsp } else {
2958 9a298e5c 2023-03-10 stsp /* Re-adding a locally deleted file. */
2959 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2960 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, ie->blob_sha1,
2961 9a298e5c 2023-03-10 stsp worktree->base_commit_id->sha1, 0);
2962 9a298e5c 2023-03-10 stsp if (err)
2963 9a298e5c 2023-03-10 stsp return err;
2964 9a298e5c 2023-03-10 stsp }
2965 9a298e5c 2023-03-10 stsp
2966 9a298e5c 2023-03-10 stsp if (is_bad_symlink) {
2967 9a298e5c 2023-03-10 stsp got_fileindex_entry_filetype_set(ie,
2968 9a298e5c 2023-03-10 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2969 9a298e5c 2023-03-10 stsp }
2970 9a298e5c 2023-03-10 stsp
2971 9a298e5c 2023-03-10 stsp return NULL;
2972 9a298e5c 2023-03-10 stsp }
2973 9a298e5c 2023-03-10 stsp
2974 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2975 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2976 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2977 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2978 234035bc 2019-06-01 stsp void *progress_arg;
2979 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2980 234035bc 2019-06-01 stsp void *cancel_arg;
2981 f69721c3 2019-10-21 stsp const char *label_orig;
2982 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2983 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2984 234035bc 2019-06-01 stsp };
2985 234035bc 2019-06-01 stsp
2986 234035bc 2019-06-01 stsp static const struct got_error *
2987 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2988 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2989 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2990 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2991 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2992 234035bc 2019-06-01 stsp {
2993 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2994 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2995 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2996 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2997 234035bc 2019-06-01 stsp struct stat sb;
2998 234035bc 2019-06-01 stsp unsigned char status;
2999 234035bc 2019-06-01 stsp int local_changes_subsumed;
3000 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3001 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
3002 234035bc 2019-06-01 stsp
3003 234035bc 2019-06-01 stsp if (blob1 && blob2) {
3004 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3005 d6c87207 2019-08-02 stsp strlen(path2));
3006 1ee397ad 2019-07-12 stsp if (ie == NULL)
3007 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3008 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
3009 234035bc 2019-06-01 stsp
3010 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3011 234035bc 2019-06-01 stsp path2) == -1)
3012 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3013 234035bc 2019-06-01 stsp
3014 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3015 7f91a133 2019-12-13 stsp repo);
3016 234035bc 2019-06-01 stsp if (err)
3017 234035bc 2019-06-01 stsp goto done;
3018 234035bc 2019-06-01 stsp
3019 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3020 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3021 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3022 234035bc 2019-06-01 stsp goto done;
3023 234035bc 2019-06-01 stsp }
3024 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3025 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3026 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3027 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3028 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3029 234035bc 2019-06-01 stsp goto done;
3030 234035bc 2019-06-01 stsp }
3031 234035bc 2019-06-01 stsp
3032 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3033 36bf999c 2020-07-23 stsp char *link_target2;
3034 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3035 36bf999c 2020-07-23 stsp if (err)
3036 36bf999c 2020-07-23 stsp goto done;
3037 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3038 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3039 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3040 36bf999c 2020-07-23 stsp free(link_target2);
3041 af57b12a 2020-07-23 stsp } else {
3042 1af628f4 2021-06-03 stsp int fd;
3043 1af628f4 2021-06-03 stsp
3044 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3045 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3046 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3047 1af628f4 2021-06-03 stsp goto done;
3048 1af628f4 2021-06-03 stsp }
3049 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3050 1af628f4 2021-06-03 stsp f_orig, blob1);
3051 1af628f4 2021-06-03 stsp if (err)
3052 1af628f4 2021-06-03 stsp goto done;
3053 1af628f4 2021-06-03 stsp
3054 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3055 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3056 1af628f4 2021-06-03 stsp goto done;
3057 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3058 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3059 1af628f4 2021-06-03 stsp if (err)
3060 1af628f4 2021-06-03 stsp goto done;
3061 1af628f4 2021-06-03 stsp
3062 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
3063 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3064 1af628f4 2021-06-03 stsp if (fd == -1) {
3065 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3066 1af628f4 2021-06-03 stsp ondisk_path);
3067 1af628f4 2021-06-03 stsp goto done;
3068 1af628f4 2021-06-03 stsp }
3069 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3070 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3071 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3072 1af628f4 2021-06-03 stsp ondisk_path);
3073 1af628f4 2021-06-03 stsp close(fd);
3074 1af628f4 2021-06-03 stsp goto done;
3075 1af628f4 2021-06-03 stsp }
3076 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3077 1af628f4 2021-06-03 stsp if (err)
3078 1af628f4 2021-06-03 stsp goto done;
3079 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3080 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3081 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3082 1af628f4 2021-06-03 stsp goto done;
3083 1af628f4 2021-06-03 stsp }
3084 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3085 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3086 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
3087 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3088 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3089 af57b12a 2020-07-23 stsp }
3090 234035bc 2019-06-01 stsp } else if (blob1) {
3091 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3092 d6c87207 2019-08-02 stsp strlen(path1));
3093 1ee397ad 2019-07-12 stsp if (ie == NULL)
3094 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3095 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3096 2b92fad7 2019-06-02 stsp
3097 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3098 2b92fad7 2019-06-02 stsp path1) == -1)
3099 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3100 2b92fad7 2019-06-02 stsp
3101 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3102 7f91a133 2019-12-13 stsp repo);
3103 2b92fad7 2019-06-02 stsp if (err)
3104 2b92fad7 2019-06-02 stsp goto done;
3105 2b92fad7 2019-06-02 stsp
3106 2b92fad7 2019-06-02 stsp switch (status) {
3107 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3108 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3109 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3110 1ee397ad 2019-07-12 stsp if (err)
3111 1ee397ad 2019-07-12 stsp goto done;
3112 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3113 2b92fad7 2019-06-02 stsp if (err)
3114 2b92fad7 2019-06-02 stsp goto done;
3115 2b92fad7 2019-06-02 stsp if (ie)
3116 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3117 2b92fad7 2019-06-02 stsp break;
3118 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3119 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3120 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3121 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3122 1ee397ad 2019-07-12 stsp if (err)
3123 1ee397ad 2019-07-12 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 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3128 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3129 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3130 72840534 2022-01-19 stsp off_t blob1_size;
3131 0a22ca1a 2020-09-23 stsp /*
3132 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3133 0a22ca1a 2020-09-23 stsp * exists in the repository.
3134 0a22ca1a 2020-09-23 stsp */
3135 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
3136 72840534 2022-01-19 stsp &blob1_size, path1);
3137 0a22ca1a 2020-09-23 stsp if (err)
3138 0a22ca1a 2020-09-23 stsp goto done;
3139 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3140 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3141 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3142 0a22ca1a 2020-09-23 stsp if (err)
3143 0a22ca1a 2020-09-23 stsp goto done;
3144 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3145 0a22ca1a 2020-09-23 stsp path1);
3146 0a22ca1a 2020-09-23 stsp if (err)
3147 0a22ca1a 2020-09-23 stsp goto done;
3148 0a22ca1a 2020-09-23 stsp if (ie)
3149 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3150 0a22ca1a 2020-09-23 stsp ie);
3151 0a22ca1a 2020-09-23 stsp } else {
3152 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3153 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3154 0a22ca1a 2020-09-23 stsp }
3155 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3156 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3157 0a22ca1a 2020-09-23 stsp free(id);
3158 0a22ca1a 2020-09-23 stsp if (err)
3159 0a22ca1a 2020-09-23 stsp goto done;
3160 0a22ca1a 2020-09-23 stsp break;
3161 0a22ca1a 2020-09-23 stsp }
3162 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3163 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3164 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3165 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3166 1ee397ad 2019-07-12 stsp if (err)
3167 1ee397ad 2019-07-12 stsp goto done;
3168 2b92fad7 2019-06-02 stsp break;
3169 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3170 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3171 1ee397ad 2019-07-12 stsp if (err)
3172 1ee397ad 2019-07-12 stsp goto done;
3173 2b92fad7 2019-06-02 stsp break;
3174 2b92fad7 2019-06-02 stsp default:
3175 2b92fad7 2019-06-02 stsp break;
3176 2b92fad7 2019-06-02 stsp }
3177 234035bc 2019-06-01 stsp } else if (blob2) {
3178 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3179 234035bc 2019-06-01 stsp path2) == -1)
3180 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3181 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3182 d6c87207 2019-08-02 stsp strlen(path2));
3183 234035bc 2019-06-01 stsp if (ie) {
3184 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3185 7f91a133 2019-12-13 stsp -1, NULL, repo);
3186 234035bc 2019-06-01 stsp if (err)
3187 234035bc 2019-06-01 stsp goto done;
3188 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3189 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3190 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3191 9a298e5c 2023-03-10 stsp status != GOT_STATUS_ADD &&
3192 9a298e5c 2023-03-10 stsp status != GOT_STATUS_DELETE) {
3193 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3194 1ee397ad 2019-07-12 stsp status, path2);
3195 234035bc 2019-06-01 stsp goto done;
3196 234035bc 2019-06-01 stsp }
3197 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3198 36bf999c 2020-07-23 stsp char *link_target2;
3199 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3200 36bf999c 2020-07-23 stsp blob2);
3201 36bf999c 2020-07-23 stsp if (err)
3202 36bf999c 2020-07-23 stsp goto done;
3203 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3204 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3205 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3206 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3207 36bf999c 2020-07-23 stsp free(link_target2);
3208 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3209 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3210 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3211 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3212 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3213 526a746f 2020-07-23 stsp a->progress_arg);
3214 9a298e5c 2023-03-10 stsp } else if (status != GOT_STATUS_DELETE) {
3215 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3216 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3217 526a746f 2020-07-23 stsp }
3218 dfe9fba0 2020-07-23 stsp if (err)
3219 dfe9fba0 2020-07-23 stsp goto done;
3220 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3221 9a298e5c 2023-03-10 stsp /* Re-add file with content from new blob. */
3222 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, ie,
3223 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3224 9a298e5c 2023-03-10 stsp 0, 0, 0, a->allow_bad_symlinks,
3225 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3226 234035bc 2019-06-01 stsp if (err)
3227 234035bc 2019-06-01 stsp goto done;
3228 234035bc 2019-06-01 stsp }
3229 234035bc 2019-06-01 stsp } else {
3230 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, NULL,
3231 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3232 9a298e5c 2023-03-10 stsp 0, 0, 1, a->allow_bad_symlinks,
3233 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3234 234035bc 2019-06-01 stsp if (err)
3235 2b92fad7 2019-06-02 stsp goto done;
3236 234035bc 2019-06-01 stsp }
3237 234035bc 2019-06-01 stsp }
3238 234035bc 2019-06-01 stsp done:
3239 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3240 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3241 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3242 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3243 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3244 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3245 1af628f4 2021-06-03 stsp free(id_str);
3246 54d5be07 2021-06-03 stsp free(label_deriv2);
3247 234035bc 2019-06-01 stsp free(ondisk_path);
3248 234035bc 2019-06-01 stsp return err;
3249 234035bc 2019-06-01 stsp }
3250 cdbfe5d2 2023-07-24 stsp
3251 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args {
3252 cdbfe5d2 2023-07-24 stsp struct got_worktree *worktree;
3253 cdbfe5d2 2023-07-24 stsp got_cancel_cb cancel_cb;
3254 cdbfe5d2 2023-07-24 stsp void *cancel_arg;
3255 cdbfe5d2 2023-07-24 stsp };
3256 234035bc 2019-06-01 stsp
3257 69de9dd4 2021-09-03 stsp static const struct got_error *
3258 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3259 69de9dd4 2021-09-03 stsp {
3260 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
3261 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args *a = arg;
3262 cdbfe5d2 2023-07-24 stsp
3263 cdbfe5d2 2023-07-24 stsp if (a->cancel_cb) {
3264 cdbfe5d2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3265 cdbfe5d2 2023-07-24 stsp if (err)
3266 cdbfe5d2 2023-07-24 stsp return err;
3267 cdbfe5d2 2023-07-24 stsp }
3268 69de9dd4 2021-09-03 stsp
3269 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3270 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3271 cdbfe5d2 2023-07-24 stsp memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3272 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3273 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3274 69de9dd4 2021-09-03 stsp
3275 69de9dd4 2021-09-03 stsp return NULL;
3276 69de9dd4 2021-09-03 stsp }
3277 69de9dd4 2021-09-03 stsp
3278 c935fd51 2023-07-23 mark const struct got_error *
3279 c935fd51 2023-07-23 mark got_worktree_get_state(char *state, struct got_repository *repo,
3280 cdbfe5d2 2023-07-24 stsp struct got_worktree *worktree,
3281 cdbfe5d2 2023-07-24 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3282 c935fd51 2023-07-23 mark {
3283 c935fd51 2023-07-23 mark const struct got_error *err;
3284 c935fd51 2023-07-23 mark struct got_object_id *base_id, *head_id = NULL;
3285 c935fd51 2023-07-23 mark struct got_reference *head_ref;
3286 c935fd51 2023-07-23 mark struct got_fileindex *fileindex = NULL;
3287 c935fd51 2023-07-23 mark char *fileindex_path = NULL;
3288 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
3289 c935fd51 2023-07-23 mark
3290 c935fd51 2023-07-23 mark if (worktree == NULL)
3291 c935fd51 2023-07-23 mark return got_error(GOT_ERR_NOT_WORKTREE);
3292 c935fd51 2023-07-23 mark
3293 c935fd51 2023-07-23 mark err = got_ref_open(&head_ref, repo,
3294 c935fd51 2023-07-23 mark got_worktree_get_head_ref_name(worktree), 0);
3295 c935fd51 2023-07-23 mark if (err)
3296 c935fd51 2023-07-23 mark return err;
3297 c935fd51 2023-07-23 mark
3298 c935fd51 2023-07-23 mark err = got_ref_resolve(&head_id, repo, head_ref);
3299 c935fd51 2023-07-23 mark if (err)
3300 c935fd51 2023-07-23 mark goto done;
3301 c935fd51 2023-07-23 mark
3302 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_UNKNOWN;
3303 c935fd51 2023-07-23 mark base_id = got_worktree_get_base_commit_id(worktree);
3304 c935fd51 2023-07-23 mark
3305 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
3306 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
3307 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
3308 cdbfe5d2 2023-07-24 stsp
3309 c935fd51 2023-07-23 mark if (got_object_id_cmp(base_id, head_id) == 0) {
3310 c935fd51 2023-07-23 mark err = open_fileindex(&fileindex, &fileindex_path, worktree);
3311 c935fd51 2023-07-23 mark if (err)
3312 c935fd51 2023-07-23 mark goto done;
3313 c935fd51 2023-07-23 mark
3314 c935fd51 2023-07-23 mark err = got_fileindex_for_each_entry_safe(fileindex,
3315 cdbfe5d2 2023-07-24 stsp check_mixed_commits, &cma);
3316 c935fd51 2023-07-23 mark if (err == NULL)
3317 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_UPTODATE;
3318 99301cec 2023-07-24 stsp else if (err->code == GOT_ERR_MIXED_COMMITS) {
3319 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_OUTOFDATE;
3320 c935fd51 2023-07-23 mark err = NULL;
3321 99301cec 2023-07-24 stsp }
3322 99301cec 2023-07-24 stsp } else
3323 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_OUTOFDATE;
3324 c935fd51 2023-07-23 mark
3325 c935fd51 2023-07-23 mark done:
3326 c935fd51 2023-07-23 mark free(head_id);
3327 c935fd51 2023-07-23 mark free(fileindex_path);
3328 c935fd51 2023-07-23 mark got_ref_close(head_ref);
3329 c935fd51 2023-07-23 mark if (fileindex != NULL)
3330 c935fd51 2023-07-23 mark got_fileindex_free(fileindex);
3331 c935fd51 2023-07-23 mark return err;
3332 c935fd51 2023-07-23 mark }
3333 c935fd51 2023-07-23 mark
3334 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3335 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3336 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3337 234035bc 2019-06-01 stsp struct got_repository *repo;
3338 234035bc 2019-06-01 stsp };
3339 234035bc 2019-06-01 stsp
3340 234035bc 2019-06-01 stsp static const struct got_error *
3341 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3342 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3343 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3344 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3345 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3346 234035bc 2019-06-01 stsp {
3347 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3348 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3349 234035bc 2019-06-01 stsp unsigned char status;
3350 234035bc 2019-06-01 stsp struct stat sb;
3351 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3352 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3353 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3354 234035bc 2019-06-01 stsp char *ondisk_path;
3355 234035bc 2019-06-01 stsp
3356 69de9dd4 2021-09-03 stsp if (id == NULL)
3357 69de9dd4 2021-09-03 stsp return NULL;
3358 234035bc 2019-06-01 stsp
3359 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3360 69de9dd4 2021-09-03 stsp if (ie == NULL)
3361 69de9dd4 2021-09-03 stsp return NULL;
3362 69de9dd4 2021-09-03 stsp
3363 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3364 234035bc 2019-06-01 stsp == -1)
3365 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3366 234035bc 2019-06-01 stsp
3367 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3368 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3369 5546d466 2021-09-02 stsp free(ondisk_path);
3370 234035bc 2019-06-01 stsp if (err)
3371 234035bc 2019-06-01 stsp return err;
3372 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3373 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3374 234035bc 2019-06-01 stsp
3375 234035bc 2019-06-01 stsp return NULL;
3376 234035bc 2019-06-01 stsp }
3377 234035bc 2019-06-01 stsp
3378 818c7501 2019-07-11 stsp static const struct got_error *
3379 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3380 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3381 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3382 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3383 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3384 234035bc 2019-06-01 stsp {
3385 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3386 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3387 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3388 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3389 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3390 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3391 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3392 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3393 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3394 234035bc 2019-06-01 stsp
3395 03415a1a 2019-06-02 stsp if (commit_id1) {
3396 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3397 a44927cc 2022-04-07 stsp if (err)
3398 a44927cc 2022-04-07 stsp goto done;
3399 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3400 03415a1a 2019-06-02 stsp worktree->path_prefix);
3401 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3402 03415a1a 2019-06-02 stsp goto done;
3403 69d57f3d 2020-07-31 stsp }
3404 69d57f3d 2020-07-31 stsp if (tree_id1) {
3405 69d57f3d 2020-07-31 stsp char *id_str;
3406 03415a1a 2019-06-02 stsp
3407 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3408 03415a1a 2019-06-02 stsp if (err)
3409 03415a1a 2019-06-02 stsp goto done;
3410 f69721c3 2019-10-21 stsp
3411 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3412 f69721c3 2019-10-21 stsp if (err)
3413 f69721c3 2019-10-21 stsp goto done;
3414 f69721c3 2019-10-21 stsp
3415 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3416 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3417 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3418 f69721c3 2019-10-21 stsp free(id_str);
3419 f69721c3 2019-10-21 stsp goto done;
3420 f69721c3 2019-10-21 stsp }
3421 f69721c3 2019-10-21 stsp free(id_str);
3422 b72706c3 2022-06-01 stsp
3423 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3424 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3425 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3426 b72706c3 2022-06-01 stsp goto done;
3427 b72706c3 2022-06-01 stsp }
3428 03415a1a 2019-06-02 stsp }
3429 234035bc 2019-06-01 stsp
3430 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3431 a44927cc 2022-04-07 stsp if (err)
3432 a44927cc 2022-04-07 stsp goto done;
3433 a44927cc 2022-04-07 stsp
3434 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3435 234035bc 2019-06-01 stsp worktree->path_prefix);
3436 234035bc 2019-06-01 stsp if (err)
3437 234035bc 2019-06-01 stsp goto done;
3438 234035bc 2019-06-01 stsp
3439 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3440 234035bc 2019-06-01 stsp if (err)
3441 234035bc 2019-06-01 stsp goto done;
3442 234035bc 2019-06-01 stsp
3443 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3444 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3445 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3446 f9d37699 2022-06-28 stsp goto done;
3447 f9d37699 2022-06-28 stsp }
3448 f9d37699 2022-06-28 stsp
3449 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3450 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3451 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3452 b72706c3 2022-06-01 stsp goto done;
3453 b72706c3 2022-06-01 stsp }
3454 b72706c3 2022-06-01 stsp
3455 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3456 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3457 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3458 f9d37699 2022-06-28 stsp goto done;
3459 f9d37699 2022-06-28 stsp }
3460 f9d37699 2022-06-28 stsp
3461 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3462 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3463 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3464 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3465 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3466 69de9dd4 2021-09-03 stsp if (err)
3467 69de9dd4 2021-09-03 stsp goto done;
3468 69de9dd4 2021-09-03 stsp
3469 234035bc 2019-06-01 stsp arg.worktree = worktree;
3470 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3471 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3472 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3473 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3474 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3475 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3476 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3477 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3478 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3479 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3480 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3481 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3482 af12c6b9 2019-06-04 stsp err = sync_err;
3483 234035bc 2019-06-01 stsp done:
3484 a44927cc 2022-04-07 stsp if (commit1)
3485 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3486 a44927cc 2022-04-07 stsp if (commit2)
3487 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3488 234035bc 2019-06-01 stsp if (tree1)
3489 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3490 234035bc 2019-06-01 stsp if (tree2)
3491 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3492 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3493 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3494 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3495 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3496 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3497 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3498 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3499 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3500 f69721c3 2019-10-21 stsp free(label_orig);
3501 818c7501 2019-07-11 stsp return err;
3502 818c7501 2019-07-11 stsp }
3503 234035bc 2019-06-01 stsp
3504 818c7501 2019-07-11 stsp const struct got_error *
3505 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3506 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3507 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3508 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3509 818c7501 2019-07-11 stsp {
3510 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3511 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3512 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3513 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
3514 818c7501 2019-07-11 stsp
3515 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3516 818c7501 2019-07-11 stsp if (err)
3517 818c7501 2019-07-11 stsp return err;
3518 818c7501 2019-07-11 stsp
3519 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3520 818c7501 2019-07-11 stsp if (err)
3521 818c7501 2019-07-11 stsp goto done;
3522 818c7501 2019-07-11 stsp
3523 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
3524 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
3525 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
3526 cdbfe5d2 2023-07-24 stsp
3527 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3528 cdbfe5d2 2023-07-24 stsp &cma);
3529 818c7501 2019-07-11 stsp if (err)
3530 818c7501 2019-07-11 stsp goto done;
3531 818c7501 2019-07-11 stsp
3532 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3533 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3534 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3535 818c7501 2019-07-11 stsp done:
3536 818c7501 2019-07-11 stsp if (fileindex)
3537 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3538 818c7501 2019-07-11 stsp free(fileindex_path);
3539 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3540 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3541 234035bc 2019-06-01 stsp err = unlockerr;
3542 234035bc 2019-06-01 stsp return err;
3543 234035bc 2019-06-01 stsp }
3544 234035bc 2019-06-01 stsp
3545 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3546 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3547 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3548 927df6b7 2019-02-10 stsp const char *status_path;
3549 927df6b7 2019-02-10 stsp size_t status_path_len;
3550 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3551 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3552 f8d1f275 2019-02-04 stsp void *status_arg;
3553 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3554 f8d1f275 2019-02-04 stsp void *cancel_arg;
3555 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3556 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3557 f2a9dc41 2019-12-13 tracey int report_unchanged;
3558 3143d852 2020-06-25 stsp int no_ignores;
3559 f8d1f275 2019-02-04 stsp };
3560 88d0e355 2019-08-03 stsp
3561 f8d1f275 2019-02-04 stsp static const struct got_error *
3562 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3563 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3564 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3565 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3566 927df6b7 2019-02-10 stsp {
3567 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3568 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3569 2c4740ad 2023-02-12 mark unsigned char staged_status;
3570 927df6b7 2019-02-10 stsp struct stat sb;
3571 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3572 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3573 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3574 927df6b7 2019-02-10 stsp
3575 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3576 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3577 98eaaa12 2019-08-03 stsp if (err)
3578 98eaaa12 2019-08-03 stsp return err;
3579 98eaaa12 2019-08-03 stsp
3580 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3581 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3582 98eaaa12 2019-08-03 stsp return NULL;
3583 98eaaa12 2019-08-03 stsp
3584 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3585 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3586 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3587 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3588 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3589 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3590 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3591 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3592 98eaaa12 2019-08-03 stsp }
3593 98eaaa12 2019-08-03 stsp
3594 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3595 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3596 927df6b7 2019-02-10 stsp }
3597 927df6b7 2019-02-10 stsp
3598 927df6b7 2019-02-10 stsp static const struct got_error *
3599 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3600 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3601 f8d1f275 2019-02-04 stsp {
3602 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3603 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3604 f8d1f275 2019-02-04 stsp char *abspath;
3605 f8d1f275 2019-02-04 stsp
3606 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3607 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3608 f6b8c3c2 2023-07-24 stsp if (err)
3609 f6b8c3c2 2023-07-24 stsp return err;
3610 f6b8c3c2 2023-07-24 stsp }
3611 0584f854 2019-04-06 stsp
3612 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3613 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3614 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3615 927df6b7 2019-02-10 stsp return NULL;
3616 927df6b7 2019-02-10 stsp
3617 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3618 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3619 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3620 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3621 f8d1f275 2019-02-04 stsp } else {
3622 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3623 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3624 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3625 f8d1f275 2019-02-04 stsp }
3626 f8d1f275 2019-02-04 stsp
3627 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3628 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3629 f8d1f275 2019-02-04 stsp free(abspath);
3630 9d31a1d8 2018-03-11 stsp return err;
3631 9d31a1d8 2018-03-11 stsp }
3632 f8d1f275 2019-02-04 stsp
3633 f8d1f275 2019-02-04 stsp static const struct got_error *
3634 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3635 f8d1f275 2019-02-04 stsp {
3636 f6b8c3c2 2023-07-24 stsp const struct got_error *err;
3637 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3638 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3639 2ec1f75b 2019-03-26 stsp unsigned char status;
3640 927df6b7 2019-02-10 stsp
3641 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3642 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3643 f6b8c3c2 2023-07-24 stsp if (err)
3644 f6b8c3c2 2023-07-24 stsp return err;
3645 f6b8c3c2 2023-07-24 stsp }
3646 0584f854 2019-04-06 stsp
3647 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3648 927df6b7 2019-02-10 stsp return NULL;
3649 927df6b7 2019-02-10 stsp
3650 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3651 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3652 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3653 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3654 2ec1f75b 2019-03-26 stsp else
3655 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3656 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3657 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3658 6841da00 2019-08-08 stsp }
3659 6841da00 2019-08-08 stsp
3660 336075a4 2022-06-25 op static void
3661 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3662 6841da00 2019-08-08 stsp {
3663 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3664 6841da00 2019-08-08 stsp
3665 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3666 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3667 d8bacb93 2023-01-10 mark
3668 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3669 6841da00 2019-08-08 stsp }
3670 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3671 6841da00 2019-08-08 stsp }
3672 6841da00 2019-08-08 stsp
3673 6841da00 2019-08-08 stsp static const struct got_error *
3674 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3675 6841da00 2019-08-08 stsp {
3676 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3677 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3678 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3679 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3680 6841da00 2019-08-08 stsp size_t linesize = 0;
3681 6841da00 2019-08-08 stsp ssize_t linelen;
3682 6841da00 2019-08-08 stsp
3683 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3684 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3685 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3686 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3687 6841da00 2019-08-08 stsp
3688 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3689 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3690 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3691 bd8de430 2019-10-04 stsp
3692 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3693 bd8de430 2019-10-04 stsp if (line[0] == '#')
3694 bd8de430 2019-10-04 stsp continue;
3695 bd8de430 2019-10-04 stsp
3696 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3697 bd8de430 2019-10-04 stsp if (line[0] == '!')
3698 bd8de430 2019-10-04 stsp continue;
3699 bd8de430 2019-10-04 stsp
3700 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3701 6841da00 2019-08-08 stsp line) == -1) {
3702 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3703 6841da00 2019-08-08 stsp goto done;
3704 6841da00 2019-08-08 stsp }
3705 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3706 6841da00 2019-08-08 stsp if (err)
3707 6841da00 2019-08-08 stsp goto done;
3708 6841da00 2019-08-08 stsp }
3709 6841da00 2019-08-08 stsp if (ferror(f)) {
3710 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3711 6841da00 2019-08-08 stsp goto done;
3712 6841da00 2019-08-08 stsp }
3713 6841da00 2019-08-08 stsp
3714 6841da00 2019-08-08 stsp dirpath = strdup(path);
3715 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3716 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3717 6841da00 2019-08-08 stsp goto done;
3718 6841da00 2019-08-08 stsp }
3719 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3720 6841da00 2019-08-08 stsp done:
3721 6841da00 2019-08-08 stsp free(line);
3722 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3723 6841da00 2019-08-08 stsp free(dirpath);
3724 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3725 6841da00 2019-08-08 stsp }
3726 6841da00 2019-08-08 stsp return err;
3727 249b637c 2023-02-20 stsp }
3728 249b637c 2023-02-20 stsp
3729 249b637c 2023-02-20 stsp static int
3730 249b637c 2023-02-20 stsp match_path(const char *pattern, size_t pattern_len, const char *path,
3731 249b637c 2023-02-20 stsp int flags)
3732 249b637c 2023-02-20 stsp {
3733 249b637c 2023-02-20 stsp char buf[PATH_MAX];
3734 249b637c 2023-02-20 stsp
3735 249b637c 2023-02-20 stsp /*
3736 249b637c 2023-02-20 stsp * Trailing slashes signify directories.
3737 249b637c 2023-02-20 stsp * Append a * to make such patterns conform to fnmatch rules.
3738 249b637c 2023-02-20 stsp */
3739 249b637c 2023-02-20 stsp if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3740 249b637c 2023-02-20 stsp if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3741 249b637c 2023-02-20 stsp return FNM_NOMATCH; /* XXX */
3742 249b637c 2023-02-20 stsp
3743 249b637c 2023-02-20 stsp return fnmatch(buf, path, flags);
3744 249b637c 2023-02-20 stsp }
3745 249b637c 2023-02-20 stsp
3746 249b637c 2023-02-20 stsp return fnmatch(pattern, path, flags);
3747 6841da00 2019-08-08 stsp }
3748 6841da00 2019-08-08 stsp
3749 336075a4 2022-06-25 op static int
3750 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3751 6841da00 2019-08-08 stsp {
3752 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3753 bd8de430 2019-10-04 stsp
3754 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3755 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3756 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3757 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3758 bd8de430 2019-10-04 stsp
3759 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3760 249b637c 2023-02-20 stsp const char *p;
3761 6841da00 2019-08-08 stsp
3762 249b637c 2023-02-20 stsp if (pi->path_len < 3 ||
3763 249b637c 2023-02-20 stsp strncmp(pi->path, "**/", 3) != 0)
3764 bd8de430 2019-10-04 stsp continue;
3765 bd8de430 2019-10-04 stsp p = path;
3766 bd8de430 2019-10-04 stsp while (*p) {
3767 249b637c 2023-02-20 stsp if (match_path(pi->path + 3,
3768 249b637c 2023-02-20 stsp pi->path_len - 3, p,
3769 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3770 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3771 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3772 bd8de430 2019-10-04 stsp p++;
3773 bd8de430 2019-10-04 stsp while (*p == '/')
3774 bd8de430 2019-10-04 stsp p++;
3775 bd8de430 2019-10-04 stsp continue;
3776 bd8de430 2019-10-04 stsp }
3777 bd8de430 2019-10-04 stsp return 1;
3778 bd8de430 2019-10-04 stsp }
3779 bd8de430 2019-10-04 stsp }
3780 bd8de430 2019-10-04 stsp }
3781 bd8de430 2019-10-04 stsp
3782 6841da00 2019-08-08 stsp /*
3783 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3784 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3785 6841da00 2019-08-08 stsp * ignores backwards.
3786 6841da00 2019-08-08 stsp */
3787 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3788 6841da00 2019-08-08 stsp while (pe) {
3789 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3790 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3791 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3792 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3793 249b637c 2023-02-20 stsp int flags = FNM_LEADING_DIR;
3794 249b637c 2023-02-20 stsp if (strstr(pi->path, "/**/") == NULL)
3795 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3796 249b637c 2023-02-20 stsp if (match_path(pi->path, pi->path_len,
3797 249b637c 2023-02-20 stsp path, flags))
3798 6841da00 2019-08-08 stsp continue;
3799 6841da00 2019-08-08 stsp return 1;
3800 6841da00 2019-08-08 stsp }
3801 6841da00 2019-08-08 stsp }
3802 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3803 6841da00 2019-08-08 stsp }
3804 6841da00 2019-08-08 stsp
3805 6841da00 2019-08-08 stsp return 0;
3806 6841da00 2019-08-08 stsp }
3807 6841da00 2019-08-08 stsp
3808 6841da00 2019-08-08 stsp static const struct got_error *
3809 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3810 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3811 6841da00 2019-08-08 stsp {
3812 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3813 6841da00 2019-08-08 stsp char *ignorespath;
3814 886cec17 2019-12-15 stsp int fd = -1;
3815 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3816 6841da00 2019-08-08 stsp
3817 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3818 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3819 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3820 6841da00 2019-08-08 stsp
3821 886cec17 2019-12-15 stsp if (dirfd != -1) {
3822 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3823 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3824 886cec17 2019-12-15 stsp if (fd == -1) {
3825 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3826 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3827 886cec17 2019-12-15 stsp ignorespath);
3828 886cec17 2019-12-15 stsp } else {
3829 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3830 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3831 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3832 886cec17 2019-12-15 stsp ignorespath);
3833 886cec17 2019-12-15 stsp else {
3834 886cec17 2019-12-15 stsp fd = -1;
3835 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3836 886cec17 2019-12-15 stsp }
3837 886cec17 2019-12-15 stsp }
3838 886cec17 2019-12-15 stsp } else {
3839 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3840 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3841 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3842 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3843 886cec17 2019-12-15 stsp ignorespath);
3844 886cec17 2019-12-15 stsp } else
3845 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3846 886cec17 2019-12-15 stsp }
3847 6841da00 2019-08-08 stsp
3848 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3849 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3850 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3851 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3852 6841da00 2019-08-08 stsp free(ignorespath);
3853 6841da00 2019-08-08 stsp return err;
3854 f8d1f275 2019-02-04 stsp }
3855 f8d1f275 2019-02-04 stsp
3856 f8d1f275 2019-02-04 stsp static const struct got_error *
3857 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3858 62da3196 2021-10-01 stsp int dirfd)
3859 f8d1f275 2019-02-04 stsp {
3860 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3861 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3862 f8d1f275 2019-02-04 stsp char *path = NULL;
3863 62da3196 2021-10-01 stsp
3864 62da3196 2021-10-01 stsp if (ignore != NULL)
3865 62da3196 2021-10-01 stsp *ignore = 0;
3866 f8d1f275 2019-02-04 stsp
3867 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3868 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3869 f6b8c3c2 2023-07-24 stsp if (err)
3870 f6b8c3c2 2023-07-24 stsp return err;
3871 f6b8c3c2 2023-07-24 stsp }
3872 0584f854 2019-04-06 stsp
3873 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3874 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3875 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3876 f8d1f275 2019-02-04 stsp } else {
3877 f8d1f275 2019-02-04 stsp path = de->d_name;
3878 f8d1f275 2019-02-04 stsp }
3879 f8d1f275 2019-02-04 stsp
3880 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3881 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3882 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3883 62da3196 2021-10-01 stsp *ignore = 1;
3884 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3885 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3886 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3887 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3888 f8d1f275 2019-02-04 stsp if (parent_path[0])
3889 f8d1f275 2019-02-04 stsp free(path);
3890 b72f483a 2019-02-05 stsp return err;
3891 f8d1f275 2019-02-04 stsp }
3892 f8d1f275 2019-02-04 stsp
3893 347d1d3e 2019-07-12 stsp static const struct got_error *
3894 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3895 3143d852 2020-06-25 stsp {
3896 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3897 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3898 3143d852 2020-06-25 stsp
3899 3143d852 2020-06-25 stsp if (a->no_ignores)
3900 3143d852 2020-06-25 stsp return NULL;
3901 3143d852 2020-06-25 stsp
3902 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3903 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3904 3143d852 2020-06-25 stsp if (err)
3905 3143d852 2020-06-25 stsp return err;
3906 3143d852 2020-06-25 stsp
3907 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3908 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3909 3143d852 2020-06-25 stsp
3910 3143d852 2020-06-25 stsp return err;
3911 3143d852 2020-06-25 stsp }
3912 3143d852 2020-06-25 stsp
3913 3143d852 2020-06-25 stsp static const struct got_error *
3914 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3915 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3916 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3917 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3918 abb4604f 2019-07-27 stsp {
3919 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3920 abb4604f 2019-07-27 stsp struct stat sb;
3921 ff56836b 2021-07-08 stsp
3922 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3923 abb4604f 2019-07-27 stsp if (ie)
3924 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3925 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3926 abb4604f 2019-07-27 stsp
3927 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3928 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3929 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3930 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3931 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3932 abb4604f 2019-07-27 stsp }
3933 abb4604f 2019-07-27 stsp
3934 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3935 916237f3 2022-02-11 stsp return NULL;
3936 916237f3 2022-02-11 stsp
3937 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3938 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3939 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3940 abb4604f 2019-07-27 stsp
3941 abb4604f 2019-07-27 stsp return NULL;
3942 3143d852 2020-06-25 stsp }
3943 3143d852 2020-06-25 stsp
3944 3143d852 2020-06-25 stsp static const struct got_error *
3945 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3946 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3947 3143d852 2020-06-25 stsp {
3948 3143d852 2020-06-25 stsp const struct got_error *err;
3949 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3950 3143d852 2020-06-25 stsp
3951 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3952 3143d852 2020-06-25 stsp ".cvsignore");
3953 3143d852 2020-06-25 stsp if (err)
3954 3143d852 2020-06-25 stsp return err;
3955 3143d852 2020-06-25 stsp
3956 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3957 3143d852 2020-06-25 stsp ".gitignore");
3958 3143d852 2020-06-25 stsp if (err)
3959 3143d852 2020-06-25 stsp return err;
3960 3143d852 2020-06-25 stsp
3961 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3962 3143d852 2020-06-25 stsp if (err) {
3963 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3964 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3965 3143d852 2020-06-25 stsp return err;
3966 3143d852 2020-06-25 stsp }
3967 3143d852 2020-06-25 stsp for (;;) {
3968 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3969 3143d852 2020-06-25 stsp ".cvsignore");
3970 3143d852 2020-06-25 stsp if (err)
3971 3143d852 2020-06-25 stsp break;
3972 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3973 3143d852 2020-06-25 stsp ".gitignore");
3974 3143d852 2020-06-25 stsp if (err)
3975 3143d852 2020-06-25 stsp break;
3976 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3977 3143d852 2020-06-25 stsp if (err) {
3978 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3979 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3980 3143d852 2020-06-25 stsp break;
3981 3143d852 2020-06-25 stsp }
3982 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3983 ff56836b 2021-07-08 stsp break;
3984 b737c85a 2020-06-26 stsp free(parent_path);
3985 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3986 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3987 3143d852 2020-06-25 stsp }
3988 3143d852 2020-06-25 stsp
3989 b737c85a 2020-06-26 stsp free(parent_path);
3990 b737c85a 2020-06-26 stsp free(next_parent_path);
3991 3143d852 2020-06-25 stsp return err;
3992 abb4604f 2019-07-27 stsp }
3993 31cf15ec 2023-04-12 stsp
3994 31cf15ec 2023-04-12 stsp struct find_missing_children_args {
3995 31cf15ec 2023-04-12 stsp const char *parent_path;
3996 31cf15ec 2023-04-12 stsp size_t parent_len;
3997 31cf15ec 2023-04-12 stsp struct got_pathlist_head *children;
3998 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb;
3999 31cf15ec 2023-04-12 stsp void *cancel_arg;
4000 31cf15ec 2023-04-12 stsp };
4001 31cf15ec 2023-04-12 stsp
4002 abb4604f 2019-07-27 stsp static const struct got_error *
4003 31cf15ec 2023-04-12 stsp find_missing_children(void *arg, struct got_fileindex_entry *ie)
4004 31cf15ec 2023-04-12 stsp {
4005 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
4006 31cf15ec 2023-04-12 stsp struct find_missing_children_args *a = arg;
4007 31cf15ec 2023-04-12 stsp
4008 31cf15ec 2023-04-12 stsp if (a->cancel_cb) {
4009 31cf15ec 2023-04-12 stsp err = a->cancel_cb(a->cancel_arg);
4010 31cf15ec 2023-04-12 stsp if (err)
4011 31cf15ec 2023-04-12 stsp return err;
4012 31cf15ec 2023-04-12 stsp }
4013 31cf15ec 2023-04-12 stsp
4014 31cf15ec 2023-04-12 stsp if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4015 31cf15ec 2023-04-12 stsp err = got_pathlist_append(a->children, ie->path, NULL);
4016 31cf15ec 2023-04-12 stsp
4017 31cf15ec 2023-04-12 stsp return err;
4018 31cf15ec 2023-04-12 stsp }
4019 31cf15ec 2023-04-12 stsp
4020 31cf15ec 2023-04-12 stsp static const struct got_error *
4021 31cf15ec 2023-04-12 stsp report_children(struct got_pathlist_head *children,
4022 31cf15ec 2023-04-12 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4023 31cf15ec 2023-04-12 stsp struct got_repository *repo, int is_root_dir, int report_unchanged,
4024 31cf15ec 2023-04-12 stsp struct got_pathlist_head *ignores, int no_ignores,
4025 31cf15ec 2023-04-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4026 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4027 31cf15ec 2023-04-12 stsp {
4028 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
4029 31cf15ec 2023-04-12 stsp struct got_pathlist_entry *pe;
4030 31cf15ec 2023-04-12 stsp char *ondisk_path = NULL;
4031 31cf15ec 2023-04-12 stsp
4032 31cf15ec 2023-04-12 stsp TAILQ_FOREACH(pe, children, entry) {
4033 31cf15ec 2023-04-12 stsp if (cancel_cb) {
4034 31cf15ec 2023-04-12 stsp err = cancel_cb(cancel_arg);
4035 31cf15ec 2023-04-12 stsp if (err)
4036 31cf15ec 2023-04-12 stsp break;
4037 31cf15ec 2023-04-12 stsp }
4038 31cf15ec 2023-04-12 stsp
4039 31cf15ec 2023-04-12 stsp if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4040 31cf15ec 2023-04-12 stsp !is_root_dir ? "/" : "", pe->path) == -1) {
4041 31cf15ec 2023-04-12 stsp err = got_error_from_errno("asprintf");
4042 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
4043 31cf15ec 2023-04-12 stsp break;
4044 31cf15ec 2023-04-12 stsp }
4045 31cf15ec 2023-04-12 stsp
4046 31cf15ec 2023-04-12 stsp err = report_single_file_status(pe->path, ondisk_path,
4047 31cf15ec 2023-04-12 stsp fileindex, status_cb, status_arg, repo, report_unchanged,
4048 31cf15ec 2023-04-12 stsp ignores, no_ignores);
4049 31cf15ec 2023-04-12 stsp if (err)
4050 31cf15ec 2023-04-12 stsp break;
4051 31cf15ec 2023-04-12 stsp
4052 31cf15ec 2023-04-12 stsp free(ondisk_path);
4053 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
4054 31cf15ec 2023-04-12 stsp }
4055 31cf15ec 2023-04-12 stsp
4056 31cf15ec 2023-04-12 stsp free(ondisk_path);
4057 31cf15ec 2023-04-12 stsp return err;
4058 31cf15ec 2023-04-12 stsp }
4059 31cf15ec 2023-04-12 stsp
4060 31cf15ec 2023-04-12 stsp static const struct got_error *
4061 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4062 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4063 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4064 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4065 f2a9dc41 2019-12-13 tracey int report_unchanged)
4066 f8d1f275 2019-02-04 stsp {
4067 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4068 6fc93f37 2019-12-13 stsp int fd = -1;
4069 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4070 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4071 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4072 31cf15ec 2023-04-12 stsp struct got_pathlist_head ignores, missing_children;
4073 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
4074 3143d852 2020-06-25 stsp
4075 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4076 31cf15ec 2023-04-12 stsp TAILQ_INIT(&missing_children);
4077 f8d1f275 2019-02-04 stsp
4078 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4079 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4080 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4081 8dc303cc 2019-07-27 stsp
4082 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4083 a84c0d30 2022-03-12 stsp if (ie) {
4084 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
4085 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
4086 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
4087 a84c0d30 2022-03-12 stsp goto done;
4088 31cf15ec 2023-04-12 stsp } else {
4089 31cf15ec 2023-04-12 stsp struct find_missing_children_args fmca;
4090 31cf15ec 2023-04-12 stsp fmca.parent_path = path;
4091 31cf15ec 2023-04-12 stsp fmca.parent_len = strlen(path);
4092 31cf15ec 2023-04-12 stsp fmca.children = &missing_children;
4093 31cf15ec 2023-04-12 stsp fmca.cancel_cb = cancel_cb;
4094 31cf15ec 2023-04-12 stsp fmca.cancel_arg = cancel_arg;
4095 31cf15ec 2023-04-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
4096 31cf15ec 2023-04-12 stsp find_missing_children, &fmca);
4097 31cf15ec 2023-04-12 stsp if (err)
4098 31cf15ec 2023-04-12 stsp goto done;
4099 a84c0d30 2022-03-12 stsp }
4100 a84c0d30 2022-03-12 stsp
4101 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4102 6fc93f37 2019-12-13 stsp if (fd == -1) {
4103 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4104 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
4105 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4106 ff56836b 2021-07-08 stsp else {
4107 ff56836b 2021-07-08 stsp if (!no_ignores) {
4108 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4109 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4110 ff56836b 2021-07-08 stsp if (err)
4111 ff56836b 2021-07-08 stsp goto done;
4112 ff56836b 2021-07-08 stsp }
4113 31cf15ec 2023-04-12 stsp if (TAILQ_EMPTY(&missing_children)) {
4114 31cf15ec 2023-04-12 stsp err = report_single_file_status(path,
4115 31cf15ec 2023-04-12 stsp ondisk_path, fileindex,
4116 31cf15ec 2023-04-12 stsp status_cb, status_arg, repo,
4117 31cf15ec 2023-04-12 stsp report_unchanged, &ignores, no_ignores);
4118 31cf15ec 2023-04-12 stsp if (err)
4119 31cf15ec 2023-04-12 stsp goto done;
4120 31cf15ec 2023-04-12 stsp } else {
4121 31cf15ec 2023-04-12 stsp err = report_children(&missing_children,
4122 31cf15ec 2023-04-12 stsp worktree, fileindex, repo,
4123 31cf15ec 2023-04-12 stsp (path[0] == '\0'), report_unchanged,
4124 31cf15ec 2023-04-12 stsp &ignores, no_ignores,
4125 31cf15ec 2023-04-12 stsp status_cb, status_arg,
4126 31cf15ec 2023-04-12 stsp cancel_cb, cancel_arg);
4127 46108e23 2023-04-12 stsp if (err)
4128 46108e23 2023-04-12 stsp goto done;
4129 31cf15ec 2023-04-12 stsp }
4130 ff56836b 2021-07-08 stsp }
4131 abb4604f 2019-07-27 stsp } else {
4132 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4133 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4134 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4135 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4136 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4137 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4138 abb4604f 2019-07-27 stsp arg.status_path = path;
4139 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4140 abb4604f 2019-07-27 stsp arg.repo = repo;
4141 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4142 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4143 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4144 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4145 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4146 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4147 022fae89 2019-12-06 tracey if (!no_ignores) {
4148 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4149 3143d852 2020-06-25 stsp worktree->root_path, path);
4150 3143d852 2020-06-25 stsp if (err)
4151 3143d852 2020-06-25 stsp goto done;
4152 022fae89 2019-12-06 tracey }
4153 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4154 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4155 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4156 927df6b7 2019-02-10 stsp }
4157 3143d852 2020-06-25 stsp done:
4158 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4159 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4160 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4161 927df6b7 2019-02-10 stsp free(ondisk_path);
4162 347d1d3e 2019-07-12 stsp return err;
4163 347d1d3e 2019-07-12 stsp }
4164 347d1d3e 2019-07-12 stsp
4165 347d1d3e 2019-07-12 stsp const struct got_error *
4166 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4167 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4168 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4169 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4170 347d1d3e 2019-07-12 stsp {
4171 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4172 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4173 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4174 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4175 347d1d3e 2019-07-12 stsp
4176 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4177 347d1d3e 2019-07-12 stsp if (err)
4178 347d1d3e 2019-07-12 stsp return err;
4179 347d1d3e 2019-07-12 stsp
4180 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4181 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4182 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4183 f6343036 2021-06-22 stsp no_ignores, 0);
4184 72ea6654 2019-07-27 stsp if (err)
4185 72ea6654 2019-07-27 stsp break;
4186 72ea6654 2019-07-27 stsp }
4187 f8d1f275 2019-02-04 stsp free(fileindex_path);
4188 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4189 f8d1f275 2019-02-04 stsp return err;
4190 f8d1f275 2019-02-04 stsp }
4191 6c7ab921 2019-03-18 stsp
4192 6c7ab921 2019-03-18 stsp const struct got_error *
4193 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4194 6c7ab921 2019-03-18 stsp const char *arg)
4195 6c7ab921 2019-03-18 stsp {
4196 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4197 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4198 6c7ab921 2019-03-18 stsp size_t len;
4199 00bb5ea0 2020-07-23 stsp struct stat sb;
4200 01740607 2020-11-04 stsp char *abspath = NULL;
4201 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4202 6c7ab921 2019-03-18 stsp
4203 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4204 6c7ab921 2019-03-18 stsp
4205 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4206 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4207 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4208 00bb5ea0 2020-07-23 stsp
4209 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4210 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4211 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4212 00bb5ea0 2020-07-23 stsp goto done;
4213 00bb5ea0 2020-07-23 stsp }
4214 727173c3 2020-11-06 stsp sb.st_mode = 0;
4215 00bb5ea0 2020-07-23 stsp }
4216 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4217 00bb5ea0 2020-07-23 stsp /*
4218 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4219 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4220 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4221 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4222 00bb5ea0 2020-07-23 stsp */
4223 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4224 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4225 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4226 00bb5ea0 2020-07-23 stsp goto done;
4227 00bb5ea0 2020-07-23 stsp }
4228 00bb5ea0 2020-07-23 stsp
4229 00bb5ea0 2020-07-23 stsp }
4230 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4231 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4232 00bb5ea0 2020-07-23 stsp if (err)
4233 d0710d08 2019-07-22 stsp goto done;
4234 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4235 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4236 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4237 00bb5ea0 2020-07-23 stsp goto done;
4238 00bb5ea0 2020-07-23 stsp }
4239 00bb5ea0 2020-07-23 stsp } else {
4240 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4241 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4242 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4243 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4244 00bb5ea0 2020-07-23 stsp goto done;
4245 00bb5ea0 2020-07-23 stsp }
4246 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4247 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4248 01740607 2020-11-04 stsp goto done;
4249 01740607 2020-11-04 stsp }
4250 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4251 01740607 2020-11-04 stsp sizeof(canonpath));
4252 01740607 2020-11-04 stsp if (err)
4253 00bb5ea0 2020-07-23 stsp goto done;
4254 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4255 01740607 2020-11-04 stsp if (resolved == NULL) {
4256 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4257 01740607 2020-11-04 stsp goto done;
4258 00bb5ea0 2020-07-23 stsp }
4259 d0710d08 2019-07-22 stsp }
4260 d0710d08 2019-07-22 stsp }
4261 6c7ab921 2019-03-18 stsp
4262 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4263 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4264 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4265 6c7ab921 2019-03-18 stsp goto done;
4266 6c7ab921 2019-03-18 stsp }
4267 6c7ab921 2019-03-18 stsp
4268 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4269 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4270 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4271 f6d88e1a 2019-05-29 stsp if (err)
4272 f6d88e1a 2019-05-29 stsp goto done;
4273 f6d88e1a 2019-05-29 stsp } else {
4274 f6d88e1a 2019-05-29 stsp path = strdup("");
4275 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4276 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4277 f6d88e1a 2019-05-29 stsp goto done;
4278 f6d88e1a 2019-05-29 stsp }
4279 6c7ab921 2019-03-18 stsp }
4280 6c7ab921 2019-03-18 stsp
4281 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4282 6c7ab921 2019-03-18 stsp len = strlen(path);
4283 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4284 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4285 6c7ab921 2019-03-18 stsp len--;
4286 6c7ab921 2019-03-18 stsp }
4287 6c7ab921 2019-03-18 stsp done:
4288 01740607 2020-11-04 stsp free(abspath);
4289 6c7ab921 2019-03-18 stsp free(resolved);
4290 d0710d08 2019-07-22 stsp free(cwd);
4291 6c7ab921 2019-03-18 stsp if (err == NULL)
4292 6c7ab921 2019-03-18 stsp *wt_path = path;
4293 6c7ab921 2019-03-18 stsp else
4294 6c7ab921 2019-03-18 stsp free(path);
4295 d00136be 2019-03-26 stsp return err;
4296 d00136be 2019-03-26 stsp }
4297 d00136be 2019-03-26 stsp
4298 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4299 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4300 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4301 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4302 4e68cba3 2019-11-23 stsp void *progress_arg;
4303 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4304 4e68cba3 2019-11-23 stsp };
4305 4e68cba3 2019-11-23 stsp
4306 b88936d3 2023-06-21 stsp static int
4307 b88936d3 2023-06-21 stsp add_noop_status(unsigned char status)
4308 b88936d3 2023-06-21 stsp {
4309 b88936d3 2023-06-21 stsp return (status == GOT_STATUS_ADD ||
4310 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODIFY ||
4311 b88936d3 2023-06-21 stsp status == GOT_STATUS_CONFLICT ||
4312 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODE_CHANGE ||
4313 b88936d3 2023-06-21 stsp status == GOT_STATUS_NO_CHANGE);
4314 b88936d3 2023-06-21 stsp }
4315 b88936d3 2023-06-21 stsp
4316 4e68cba3 2019-11-23 stsp static const struct got_error *
4317 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4318 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4319 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4320 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4321 07f5b47a 2019-06-02 stsp {
4322 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4323 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4324 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4325 6d022e97 2019-08-04 stsp struct stat sb;
4326 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4327 07f5b47a 2019-06-02 stsp
4328 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4329 dbb83fbd 2019-12-12 stsp relpath) == -1)
4330 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4331 dbb83fbd 2019-12-12 stsp
4332 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4333 6d022e97 2019-08-04 stsp if (ie) {
4334 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4335 12463d8b 2019-12-13 stsp de_name, a->repo);
4336 6d022e97 2019-08-04 stsp if (err)
4337 dbb83fbd 2019-12-12 stsp goto done;
4338 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4339 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE &&
4340 b88936d3 2023-06-21 stsp add_noop_status(status))
4341 dbb83fbd 2019-12-12 stsp goto done;
4342 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4343 dbb83fbd 2019-12-12 stsp if (err)
4344 dbb83fbd 2019-12-12 stsp goto done;
4345 6d022e97 2019-08-04 stsp }
4346 07f5b47a 2019-06-02 stsp
4347 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4348 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4349 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4350 9b4603c0 2022-01-31 stsp else
4351 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4352 dbb83fbd 2019-12-12 stsp goto done;
4353 4e68cba3 2019-11-23 stsp }
4354 07f5b47a 2019-06-02 stsp
4355 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4356 4e68cba3 2019-11-23 stsp if (err)
4357 4e68cba3 2019-11-23 stsp goto done;
4358 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4359 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4360 3969253a 2020-03-07 stsp if (err) {
4361 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4362 3969253a 2020-03-07 stsp goto done;
4363 3969253a 2020-03-07 stsp }
4364 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4365 07f5b47a 2019-06-02 stsp if (err) {
4366 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4367 4e68cba3 2019-11-23 stsp goto done;
4368 07f5b47a 2019-06-02 stsp }
4369 4e68cba3 2019-11-23 stsp done:
4370 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4371 dbb83fbd 2019-12-12 stsp if (err)
4372 dbb83fbd 2019-12-12 stsp return err;
4373 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4374 dbb83fbd 2019-12-12 stsp return NULL;
4375 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4376 07f5b47a 2019-06-02 stsp }
4377 07f5b47a 2019-06-02 stsp
4378 d00136be 2019-03-26 stsp const struct got_error *
4379 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4380 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4381 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4382 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4383 d00136be 2019-03-26 stsp {
4384 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4385 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4386 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4387 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4388 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4389 d00136be 2019-03-26 stsp
4390 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4391 d00136be 2019-03-26 stsp if (err)
4392 d00136be 2019-03-26 stsp return err;
4393 d00136be 2019-03-26 stsp
4394 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4395 d00136be 2019-03-26 stsp if (err)
4396 d00136be 2019-03-26 stsp goto done;
4397 d00136be 2019-03-26 stsp
4398 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4399 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4400 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4401 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4402 4e68cba3 2019-11-23 stsp saa.repo = repo;
4403 4e68cba3 2019-11-23 stsp
4404 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4405 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4406 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4407 1dd54920 2019-05-11 stsp if (err)
4408 af12c6b9 2019-06-04 stsp break;
4409 1dd54920 2019-05-11 stsp }
4410 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4411 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4412 af12c6b9 2019-06-04 stsp err = sync_err;
4413 d00136be 2019-03-26 stsp done:
4414 fb399478 2019-07-12 stsp free(fileindex_path);
4415 d00136be 2019-03-26 stsp if (fileindex)
4416 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4417 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4418 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4419 d00136be 2019-03-26 stsp err = unlockerr;
4420 6c7ab921 2019-03-18 stsp return err;
4421 6c7ab921 2019-03-18 stsp }
4422 17ed4618 2019-06-02 stsp
4423 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4424 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4425 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4426 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4427 f2a9dc41 2019-12-13 tracey void *progress_arg;
4428 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4429 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4430 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4431 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4432 7f4e5320 2023-05-29 stsp const char *status_path;
4433 7f4e5320 2023-05-29 stsp size_t status_path_len;
4434 766841c2 2020-08-13 stsp const char *status_codes;
4435 f2a9dc41 2019-12-13 tracey };
4436 f2a9dc41 2019-12-13 tracey
4437 f2a9dc41 2019-12-13 tracey static const struct got_error *
4438 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4439 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4440 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4441 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4442 17ed4618 2019-06-02 stsp {
4443 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4444 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4445 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4446 17ed4618 2019-06-02 stsp struct stat sb;
4447 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4448 4e12cd97 2022-01-25 stsp
4449 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4450 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4451 4e12cd97 2022-01-25 stsp return NULL;
4452 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4453 4e12cd97 2022-01-25 stsp }
4454 17ed4618 2019-06-02 stsp
4455 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4456 17ed4618 2019-06-02 stsp if (ie == NULL)
4457 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4458 2ec1f75b 2019-03-26 stsp
4459 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4460 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4461 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4462 9acbc4fa 2019-08-03 stsp return NULL;
4463 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4464 9acbc4fa 2019-08-03 stsp }
4465 9acbc4fa 2019-08-03 stsp
4466 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4467 f2a9dc41 2019-12-13 tracey relpath) == -1)
4468 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4469 f2a9dc41 2019-12-13 tracey
4470 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4471 12463d8b 2019-12-13 stsp a->repo);
4472 17ed4618 2019-06-02 stsp if (err)
4473 f2a9dc41 2019-12-13 tracey goto done;
4474 17ed4618 2019-06-02 stsp
4475 766841c2 2020-08-13 stsp if (a->status_codes) {
4476 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4477 766841c2 2020-08-13 stsp int i;
4478 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4479 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4480 766841c2 2020-08-13 stsp break;
4481 766841c2 2020-08-13 stsp }
4482 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4483 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4484 766841c2 2020-08-13 stsp free(ondisk_path);
4485 766841c2 2020-08-13 stsp return NULL;
4486 766841c2 2020-08-13 stsp }
4487 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4488 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4489 766841c2 2020-08-13 stsp static char msg[64];
4490 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4491 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4492 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4493 766841c2 2020-08-13 stsp goto done;
4494 766841c2 2020-08-13 stsp }
4495 766841c2 2020-08-13 stsp }
4496 766841c2 2020-08-13 stsp
4497 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4498 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4499 f2a9dc41 2019-12-13 tracey goto done;
4500 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4501 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4502 f2a9dc41 2019-12-13 tracey goto done;
4503 f2a9dc41 2019-12-13 tracey }
4504 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4505 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4506 4e12cd97 2022-01-25 stsp goto done;
4507 4e12cd97 2022-01-25 stsp }
4508 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4509 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4510 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4511 f2a9dc41 2019-12-13 tracey goto done;
4512 f2a9dc41 2019-12-13 tracey }
4513 17ed4618 2019-06-02 stsp }
4514 17ed4618 2019-06-02 stsp
4515 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4516 20a2ad1c 2020-10-20 stsp size_t root_len;
4517 20a2ad1c 2020-10-20 stsp
4518 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4519 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4520 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4521 12463d8b 2019-12-13 stsp ondisk_path);
4522 12463d8b 2019-12-13 stsp goto done;
4523 12463d8b 2019-12-13 stsp }
4524 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4525 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4526 12463d8b 2019-12-13 stsp goto done;
4527 15341bfd 2020-03-05 tracey }
4528 15341bfd 2020-03-05 tracey
4529 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4530 20a2ad1c 2020-10-20 stsp do {
4531 20a2ad1c 2020-10-20 stsp char *parent;
4532 7f4e5320 2023-05-29 stsp
4533 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4534 20a2ad1c 2020-10-20 stsp if (err)
4535 2513f20a 2020-10-20 stsp goto done;
4536 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4537 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4538 7f4e5320 2023-05-29 stsp if (got_path_cmp(ondisk_path, a->status_path,
4539 7f4e5320 2023-05-29 stsp strlen(ondisk_path), a->status_path_len) != 0 &&
4540 7f4e5320 2023-05-29 stsp !got_path_is_child(ondisk_path, a->status_path,
4541 7f4e5320 2023-05-29 stsp a->status_path_len))
4542 7f4e5320 2023-05-29 stsp break;
4543 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4544 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4545 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4546 20a2ad1c 2020-10-20 stsp ondisk_path);
4547 15341bfd 2020-03-05 tracey break;
4548 15341bfd 2020-03-05 tracey }
4549 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4550 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4551 f2a9dc41 2019-12-13 tracey }
4552 17ed4618 2019-06-02 stsp
4553 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4554 f2a9dc41 2019-12-13 tracey done:
4555 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4556 f2a9dc41 2019-12-13 tracey if (err)
4557 f2a9dc41 2019-12-13 tracey return err;
4558 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4559 f2a9dc41 2019-12-13 tracey return NULL;
4560 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4561 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4562 17ed4618 2019-06-02 stsp }
4563 17ed4618 2019-06-02 stsp
4564 2ec1f75b 2019-03-26 stsp const struct got_error *
4565 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4566 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4567 766841c2 2020-08-13 stsp const char *status_codes,
4568 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4569 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4570 2ec1f75b 2019-03-26 stsp {
4571 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4572 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4573 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4574 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4575 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4576 2ec1f75b 2019-03-26 stsp
4577 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4578 2ec1f75b 2019-03-26 stsp if (err)
4579 2ec1f75b 2019-03-26 stsp return err;
4580 2ec1f75b 2019-03-26 stsp
4581 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4582 2ec1f75b 2019-03-26 stsp if (err)
4583 2ec1f75b 2019-03-26 stsp goto done;
4584 f2a9dc41 2019-12-13 tracey
4585 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4586 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4587 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4588 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4589 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4590 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4591 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4592 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4593 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4594 2ec1f75b 2019-03-26 stsp
4595 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4596 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
4597 7f4e5320 2023-05-29 stsp
4598 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s%s%s",
4599 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree),
4600 7f4e5320 2023-05-29 stsp pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4601 7f4e5320 2023-05-29 stsp err = got_error_from_errno("asprintf");
4602 7f4e5320 2023-05-29 stsp goto done;
4603 7f4e5320 2023-05-29 stsp }
4604 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
4605 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
4606 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4607 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4608 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
4609 17ed4618 2019-06-02 stsp if (err)
4610 af12c6b9 2019-06-04 stsp break;
4611 2ec1f75b 2019-03-26 stsp }
4612 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4613 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4614 af12c6b9 2019-06-04 stsp err = sync_err;
4615 2ec1f75b 2019-03-26 stsp done:
4616 fb399478 2019-07-12 stsp free(fileindex_path);
4617 2ec1f75b 2019-03-26 stsp if (fileindex)
4618 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4619 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4620 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4621 2ec1f75b 2019-03-26 stsp err = unlockerr;
4622 33aa809d 2019-08-08 stsp return err;
4623 33aa809d 2019-08-08 stsp }
4624 33aa809d 2019-08-08 stsp
4625 33aa809d 2019-08-08 stsp static const struct got_error *
4626 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4627 33aa809d 2019-08-08 stsp {
4628 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4629 33aa809d 2019-08-08 stsp char *line = NULL;
4630 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4631 33aa809d 2019-08-08 stsp ssize_t linelen;
4632 33aa809d 2019-08-08 stsp
4633 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4634 33aa809d 2019-08-08 stsp if (linelen == -1) {
4635 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4636 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4637 33aa809d 2019-08-08 stsp goto done;
4638 33aa809d 2019-08-08 stsp }
4639 33aa809d 2019-08-08 stsp return NULL;
4640 33aa809d 2019-08-08 stsp }
4641 33aa809d 2019-08-08 stsp if (outfile) {
4642 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4643 33aa809d 2019-08-08 stsp if (n != linelen) {
4644 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4645 33aa809d 2019-08-08 stsp goto done;
4646 33aa809d 2019-08-08 stsp }
4647 33aa809d 2019-08-08 stsp }
4648 33aa809d 2019-08-08 stsp if (rejectfile) {
4649 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4650 33aa809d 2019-08-08 stsp if (n != linelen)
4651 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4652 33aa809d 2019-08-08 stsp }
4653 33aa809d 2019-08-08 stsp done:
4654 33aa809d 2019-08-08 stsp free(line);
4655 2ec1f75b 2019-03-26 stsp return err;
4656 2ec1f75b 2019-03-26 stsp }
4657 1f1abb7e 2019-08-08 stsp
4658 33aa809d 2019-08-08 stsp static const struct got_error *
4659 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4660 33aa809d 2019-08-08 stsp {
4661 33aa809d 2019-08-08 stsp char *line = NULL;
4662 33aa809d 2019-08-08 stsp size_t linesize = 0;
4663 33aa809d 2019-08-08 stsp ssize_t linelen;
4664 33aa809d 2019-08-08 stsp
4665 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4666 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4667 500467ff 2019-09-25 hiltjo if (ferror(f))
4668 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4669 500467ff 2019-09-25 hiltjo return NULL;
4670 500467ff 2019-09-25 hiltjo }
4671 33aa809d 2019-08-08 stsp free(line);
4672 33aa809d 2019-08-08 stsp return NULL;
4673 33aa809d 2019-08-08 stsp }
4674 33aa809d 2019-08-08 stsp
4675 33aa809d 2019-08-08 stsp static const struct got_error *
4676 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4677 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4678 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4679 abc59930 2021-09-05 naddy {
4680 33aa809d 2019-08-08 stsp const struct got_error *err;
4681 33aa809d 2019-08-08 stsp
4682 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4683 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4684 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4685 33aa809d 2019-08-08 stsp if (err)
4686 33aa809d 2019-08-08 stsp return err;
4687 33aa809d 2019-08-08 stsp (*line_cur1)++;
4688 33aa809d 2019-08-08 stsp }
4689 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4690 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4691 33aa809d 2019-08-08 stsp if (rejectfile)
4692 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4693 33aa809d 2019-08-08 stsp else
4694 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4695 33aa809d 2019-08-08 stsp if (err)
4696 33aa809d 2019-08-08 stsp return err;
4697 33aa809d 2019-08-08 stsp (*line_cur2)++;
4698 33aa809d 2019-08-08 stsp }
4699 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4700 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4701 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4702 33aa809d 2019-08-08 stsp if (err)
4703 33aa809d 2019-08-08 stsp return err;
4704 33aa809d 2019-08-08 stsp (*line_cur2)++;
4705 33aa809d 2019-08-08 stsp }
4706 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4707 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4708 33aa809d 2019-08-08 stsp if (rejectfile)
4709 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4710 33aa809d 2019-08-08 stsp else
4711 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4712 33aa809d 2019-08-08 stsp if (err)
4713 33aa809d 2019-08-08 stsp return err;
4714 33aa809d 2019-08-08 stsp (*line_cur1)++;
4715 33aa809d 2019-08-08 stsp }
4716 f1e81a05 2019-08-10 stsp
4717 f1e81a05 2019-08-10 stsp return NULL;
4718 f1e81a05 2019-08-10 stsp }
4719 f1e81a05 2019-08-10 stsp
4720 f1e81a05 2019-08-10 stsp static const struct got_error *
4721 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4722 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4723 f1e81a05 2019-08-10 stsp {
4724 f1e81a05 2019-08-10 stsp const struct got_error *err;
4725 f1e81a05 2019-08-10 stsp
4726 f1e81a05 2019-08-10 stsp if (outfile) {
4727 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4728 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4729 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4730 f1e81a05 2019-08-10 stsp if (err)
4731 f1e81a05 2019-08-10 stsp return err;
4732 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4733 f1e81a05 2019-08-10 stsp }
4734 f1e81a05 2019-08-10 stsp }
4735 f1e81a05 2019-08-10 stsp if (rejectfile) {
4736 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4737 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4738 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4739 f1e81a05 2019-08-10 stsp if (err)
4740 f1e81a05 2019-08-10 stsp return err;
4741 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4742 f1e81a05 2019-08-10 stsp }
4743 33aa809d 2019-08-08 stsp }
4744 33aa809d 2019-08-08 stsp
4745 33aa809d 2019-08-08 stsp return NULL;
4746 33aa809d 2019-08-08 stsp }
4747 33aa809d 2019-08-08 stsp
4748 33aa809d 2019-08-08 stsp static const struct got_error *
4749 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4750 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4751 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4752 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4753 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4754 33aa809d 2019-08-08 stsp {
4755 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4756 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4757 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4758 33aa809d 2019-08-08 stsp FILE *hunkfile;
4759 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4760 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4761 fe621944 2020-11-10 stsp int rc;
4762 33aa809d 2019-08-08 stsp
4763 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4764 33aa809d 2019-08-08 stsp
4765 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4766 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4767 fe621944 2020-11-10 stsp start_old = cc.left.start;
4768 fe621944 2020-11-10 stsp end_old = cc.left.end;
4769 fe621944 2020-11-10 stsp start_new = cc.right.start;
4770 fe621944 2020-11-10 stsp end_new = cc.right.end;
4771 33aa809d 2019-08-08 stsp
4772 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4773 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4774 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4775 33aa809d 2019-08-08 stsp
4776 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4777 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4778 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4779 33aa809d 2019-08-08 stsp
4780 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4781 fe621944 2020-11-10 stsp if (diff_state == NULL)
4782 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4783 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4784 fe621944 2020-11-10 stsp
4785 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4786 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4787 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4788 33aa809d 2019-08-08 stsp goto done;
4789 33aa809d 2019-08-08 stsp }
4790 fe621944 2020-11-10 stsp
4791 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4792 fe621944 2020-11-10 stsp diff_result, &cc);
4793 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4794 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4795 33aa809d 2019-08-08 stsp goto done;
4796 33aa809d 2019-08-08 stsp }
4797 fe621944 2020-11-10 stsp
4798 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4799 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4800 33aa809d 2019-08-08 stsp goto done;
4801 33aa809d 2019-08-08 stsp }
4802 33aa809d 2019-08-08 stsp
4803 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4804 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4805 33aa809d 2019-08-08 stsp if (err)
4806 33aa809d 2019-08-08 stsp goto done;
4807 33aa809d 2019-08-08 stsp
4808 33aa809d 2019-08-08 stsp switch (*choice) {
4809 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4810 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4811 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4812 33aa809d 2019-08-08 stsp break;
4813 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4814 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4815 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4816 33aa809d 2019-08-08 stsp break;
4817 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4818 33aa809d 2019-08-08 stsp break;
4819 33aa809d 2019-08-08 stsp default:
4820 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4821 33aa809d 2019-08-08 stsp break;
4822 33aa809d 2019-08-08 stsp }
4823 33aa809d 2019-08-08 stsp done:
4824 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4825 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4826 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4827 33aa809d 2019-08-08 stsp return err;
4828 33aa809d 2019-08-08 stsp }
4829 33aa809d 2019-08-08 stsp
4830 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4831 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4832 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4833 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4834 1f1abb7e 2019-08-08 stsp void *progress_arg;
4835 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4836 33aa809d 2019-08-08 stsp void *patch_arg;
4837 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4838 f259c4c1 2021-09-24 stsp int unlink_added_files;
4839 af179be7 2023-04-14 stsp struct got_pathlist_head *added_files_to_unlink;
4840 1f1abb7e 2019-08-08 stsp };
4841 a129376b 2019-03-28 stsp
4842 e20a8b6f 2019-06-04 stsp static const struct got_error *
4843 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4844 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4845 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4846 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4847 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4848 33aa809d 2019-08-08 stsp {
4849 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4850 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4851 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4852 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4853 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4854 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4855 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4856 fe621944 2020-11-10 stsp struct stat sb2;
4857 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4858 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4859 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4860 33aa809d 2019-08-08 stsp
4861 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4862 33aa809d 2019-08-08 stsp
4863 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4864 33aa809d 2019-08-08 stsp if (err)
4865 33aa809d 2019-08-08 stsp return err;
4866 33aa809d 2019-08-08 stsp
4867 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4868 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4869 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4870 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4871 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4872 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4873 fa3cef63 2020-07-23 stsp goto done;
4874 fa3cef63 2020-07-23 stsp }
4875 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4876 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4877 c0df5966 2021-12-31 stsp if (link_len == -1) {
4878 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4879 c0df5966 2021-12-31 stsp path2);
4880 c0df5966 2021-12-31 stsp }
4881 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4882 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4883 12463d8b 2019-12-13 stsp }
4884 12463d8b 2019-12-13 stsp } else {
4885 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4886 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4887 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4888 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4889 fa3cef63 2020-07-23 stsp goto done;
4890 fa3cef63 2020-07-23 stsp }
4891 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4892 fa3cef63 2020-07-23 stsp sizeof(link_target));
4893 fa3cef63 2020-07-23 stsp if (link_len == -1)
4894 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4895 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4896 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4897 12463d8b 2019-12-13 stsp }
4898 1ebedb77 2019-10-19 stsp }
4899 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4900 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4901 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4902 fa3cef63 2020-07-23 stsp goto done;
4903 fa3cef63 2020-07-23 stsp }
4904 1ebedb77 2019-10-19 stsp
4905 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4906 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4907 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4908 fa3cef63 2020-07-23 stsp goto done;
4909 fa3cef63 2020-07-23 stsp }
4910 fa3cef63 2020-07-23 stsp fd2 = -1;
4911 fa3cef63 2020-07-23 stsp } else {
4912 fa3cef63 2020-07-23 stsp size_t n;
4913 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4914 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4915 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4916 fa3cef63 2020-07-23 stsp goto done;
4917 fa3cef63 2020-07-23 stsp }
4918 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4919 fa3cef63 2020-07-23 stsp if (n != link_len) {
4920 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4921 fa3cef63 2020-07-23 stsp goto done;
4922 fa3cef63 2020-07-23 stsp }
4923 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4924 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4925 fa3cef63 2020-07-23 stsp goto done;
4926 fa3cef63 2020-07-23 stsp }
4927 fa3cef63 2020-07-23 stsp rewind(f2);
4928 33aa809d 2019-08-08 stsp }
4929 33aa809d 2019-08-08 stsp
4930 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4931 eb81bc23 2022-06-28 tracey if (fd == -1) {
4932 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4933 eb81bc23 2022-06-28 tracey goto done;
4934 eb81bc23 2022-06-28 tracey }
4935 eb81bc23 2022-06-28 tracey
4936 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4937 33aa809d 2019-08-08 stsp if (err)
4938 33aa809d 2019-08-08 stsp goto done;
4939 33aa809d 2019-08-08 stsp
4940 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4941 33aa809d 2019-08-08 stsp if (err)
4942 33aa809d 2019-08-08 stsp goto done;
4943 33aa809d 2019-08-08 stsp
4944 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4945 33aa809d 2019-08-08 stsp if (err)
4946 33aa809d 2019-08-08 stsp goto done;
4947 33aa809d 2019-08-08 stsp
4948 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4949 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4950 33aa809d 2019-08-08 stsp if (err)
4951 33aa809d 2019-08-08 stsp goto done;
4952 33aa809d 2019-08-08 stsp
4953 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4954 b90054ed 2022-11-01 stsp "");
4955 33aa809d 2019-08-08 stsp if (err)
4956 33aa809d 2019-08-08 stsp goto done;
4957 33aa809d 2019-08-08 stsp
4958 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4959 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4960 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4961 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4962 fe621944 2020-11-10 stsp
4963 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4964 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4965 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4966 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4967 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4968 fe621944 2020-11-10 stsp nchanges++;
4969 fe621944 2020-11-10 stsp }
4970 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4971 33aa809d 2019-08-08 stsp int choice;
4972 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4973 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4974 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4975 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4976 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4977 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4978 33aa809d 2019-08-08 stsp if (err)
4979 33aa809d 2019-08-08 stsp goto done;
4980 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4981 33aa809d 2019-08-08 stsp have_content = 1;
4982 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4983 33aa809d 2019-08-08 stsp break;
4984 33aa809d 2019-08-08 stsp }
4985 1ebedb77 2019-10-19 stsp if (have_content) {
4986 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4987 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4988 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4989 1ebedb77 2019-10-19 stsp if (err)
4990 1ebedb77 2019-10-19 stsp goto done;
4991 1ebedb77 2019-10-19 stsp
4992 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4993 b2b3fce1 2022-10-29 op mode_t mode;
4994 b2b3fce1 2022-10-29 op
4995 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4996 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4997 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4998 fa3cef63 2020-07-23 stsp goto done;
4999 fa3cef63 2020-07-23 stsp }
5000 1ebedb77 2019-10-19 stsp }
5001 1ebedb77 2019-10-19 stsp }
5002 33aa809d 2019-08-08 stsp done:
5003 33aa809d 2019-08-08 stsp free(id_str);
5004 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5005 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5006 33aa809d 2019-08-08 stsp if (blob)
5007 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5008 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5009 fe621944 2020-11-10 stsp if (err == NULL)
5010 fe621944 2020-11-10 stsp err = free_err;
5011 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5012 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5013 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5014 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5015 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5016 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5017 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5018 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5019 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5020 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5021 33aa809d 2019-08-08 stsp if (err || !have_content) {
5022 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5023 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5024 33aa809d 2019-08-08 stsp free(*path_outfile);
5025 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5026 33aa809d 2019-08-08 stsp }
5027 33aa809d 2019-08-08 stsp free(path1);
5028 33aa809d 2019-08-08 stsp return err;
5029 33aa809d 2019-08-08 stsp }
5030 33aa809d 2019-08-08 stsp
5031 33aa809d 2019-08-08 stsp static const struct got_error *
5032 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5033 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5034 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5035 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5036 a129376b 2019-03-28 stsp {
5037 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5038 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5039 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5040 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5041 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
5042 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5043 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5044 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5045 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5046 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5047 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5048 eb81bc23 2022-06-28 tracey int fd = -1;
5049 a129376b 2019-03-28 stsp
5050 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5051 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5052 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5053 d3bcc3d1 2019-08-08 stsp return NULL;
5054 3d69ad8d 2019-08-17 semarie
5055 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5056 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5057 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5058 d3bcc3d1 2019-08-08 stsp
5059 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5060 65084dad 2019-08-08 stsp if (ie == NULL)
5061 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5062 a129376b 2019-03-28 stsp
5063 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5064 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5065 a129376b 2019-03-28 stsp if (err) {
5066 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5067 a129376b 2019-03-28 stsp goto done;
5068 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5069 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5070 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5071 e20a8b6f 2019-06-04 stsp goto done;
5072 e20a8b6f 2019-06-04 stsp }
5073 a129376b 2019-03-28 stsp }
5074 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5075 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5076 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5077 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5078 a129376b 2019-03-28 stsp goto done;
5079 a129376b 2019-03-28 stsp }
5080 a129376b 2019-03-28 stsp } else {
5081 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5082 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5083 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5084 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5085 a129376b 2019-03-28 stsp goto done;
5086 a129376b 2019-03-28 stsp }
5087 a129376b 2019-03-28 stsp } else {
5088 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5089 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5090 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5091 a129376b 2019-03-28 stsp goto done;
5092 a129376b 2019-03-28 stsp }
5093 a129376b 2019-03-28 stsp }
5094 a129376b 2019-03-28 stsp }
5095 a129376b 2019-03-28 stsp
5096 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
5097 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
5098 a44927cc 2022-04-07 stsp if (err)
5099 a44927cc 2022-04-07 stsp goto done;
5100 a44927cc 2022-04-07 stsp
5101 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5102 a9fa2909 2019-07-27 stsp if (err) {
5103 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5104 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5105 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5106 a9fa2909 2019-07-27 stsp goto done;
5107 a9fa2909 2019-07-27 stsp } else {
5108 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5109 a9fa2909 2019-07-27 stsp if (err)
5110 a9fa2909 2019-07-27 stsp goto done;
5111 a9fa2909 2019-07-27 stsp
5112 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5113 1233e6b6 2020-10-19 stsp if (err)
5114 a9fa2909 2019-07-27 stsp goto done;
5115 a9fa2909 2019-07-27 stsp
5116 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5117 1233e6b6 2020-10-19 stsp free(te_name);
5118 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5119 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5120 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5121 a9fa2909 2019-07-27 stsp goto done;
5122 a9fa2909 2019-07-27 stsp }
5123 a129376b 2019-03-28 stsp }
5124 a129376b 2019-03-28 stsp
5125 a129376b 2019-03-28 stsp switch (status) {
5126 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5127 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5128 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5129 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5130 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5131 33aa809d 2019-08-08 stsp if (err)
5132 33aa809d 2019-08-08 stsp goto done;
5133 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5134 33aa809d 2019-08-08 stsp break;
5135 33aa809d 2019-08-08 stsp }
5136 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5137 1f1abb7e 2019-08-08 stsp ie->path);
5138 1ee397ad 2019-07-12 stsp if (err)
5139 1ee397ad 2019-07-12 stsp goto done;
5140 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5141 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
5142 af179be7 2023-04-14 stsp int do_unlink = a->added_files_to_unlink ? 0 : 1;
5143 af179be7 2023-04-14 stsp
5144 af179be7 2023-04-14 stsp if (a->added_files_to_unlink) {
5145 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
5146 af179be7 2023-04-14 stsp
5147 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, a->added_files_to_unlink,
5148 af179be7 2023-04-14 stsp entry) {
5149 af179be7 2023-04-14 stsp if (got_path_cmp(pe->path, relpath,
5150 af179be7 2023-04-14 stsp pe->path_len, strlen(relpath)))
5151 af179be7 2023-04-14 stsp continue;
5152 af179be7 2023-04-14 stsp do_unlink = 1;
5153 af179be7 2023-04-14 stsp break;
5154 af179be7 2023-04-14 stsp }
5155 f259c4c1 2021-09-24 stsp }
5156 af179be7 2023-04-14 stsp
5157 af179be7 2023-04-14 stsp if (do_unlink) {
5158 af179be7 2023-04-14 stsp if (asprintf(&ondisk_path, "%s/%s",
5159 af179be7 2023-04-14 stsp got_worktree_get_root_path(a->worktree),
5160 af179be7 2023-04-14 stsp relpath) == -1) {
5161 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
5162 af179be7 2023-04-14 stsp goto done;
5163 af179be7 2023-04-14 stsp }
5164 af179be7 2023-04-14 stsp if (unlink(ondisk_path) == -1) {
5165 af179be7 2023-04-14 stsp err = got_error_from_errno2("unlink",
5166 af179be7 2023-04-14 stsp ondisk_path);
5167 af179be7 2023-04-14 stsp break;
5168 af179be7 2023-04-14 stsp }
5169 f259c4c1 2021-09-24 stsp }
5170 f259c4c1 2021-09-24 stsp }
5171 a129376b 2019-03-28 stsp break;
5172 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5173 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5174 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5175 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5176 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5177 33aa809d 2019-08-08 stsp if (err)
5178 33aa809d 2019-08-08 stsp goto done;
5179 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5180 33aa809d 2019-08-08 stsp break;
5181 33aa809d 2019-08-08 stsp }
5182 33aa809d 2019-08-08 stsp /* fall through */
5183 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5184 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5185 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5186 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5187 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5188 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5189 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
5190 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
5191 b4b2adf5 2023-02-09 op else
5192 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
5193 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5194 eb81bc23 2022-06-28 tracey if (fd == -1) {
5195 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5196 eb81bc23 2022-06-28 tracey goto done;
5197 eb81bc23 2022-06-28 tracey }
5198 eb81bc23 2022-06-28 tracey
5199 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5200 a129376b 2019-03-28 stsp if (err)
5201 65084dad 2019-08-08 stsp goto done;
5202 65084dad 2019-08-08 stsp
5203 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5204 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5205 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5206 a129376b 2019-03-28 stsp goto done;
5207 65084dad 2019-08-08 stsp }
5208 65084dad 2019-08-08 stsp
5209 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5210 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5211 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5212 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5213 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5214 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5215 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5216 33aa809d 2019-08-08 stsp break;
5217 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5218 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5219 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5220 369fd7e5 2020-07-23 stsp path_content);
5221 369fd7e5 2020-07-23 stsp break;
5222 369fd7e5 2020-07-23 stsp }
5223 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5224 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5225 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5226 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5227 369fd7e5 2020-07-23 stsp } else {
5228 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5229 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5230 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5231 369fd7e5 2020-07-23 stsp goto done;
5232 369fd7e5 2020-07-23 stsp }
5233 33aa809d 2019-08-08 stsp }
5234 33aa809d 2019-08-08 stsp } else {
5235 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5236 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5237 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5238 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5239 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5240 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5241 2e1fa222 2020-07-23 stsp } else {
5242 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5243 2e1fa222 2020-07-23 stsp ie->path,
5244 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5245 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5246 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5247 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5248 2e1fa222 2020-07-23 stsp }
5249 a129376b 2019-03-28 stsp if (err)
5250 a129376b 2019-03-28 stsp goto done;
5251 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5252 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5253 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5254 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5255 437adc9d 2020-12-10 yzhong blob->id.sha1,
5256 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5257 2e1fa222 2020-07-23 stsp if (err)
5258 2e1fa222 2020-07-23 stsp goto done;
5259 2e1fa222 2020-07-23 stsp }
5260 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5261 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5262 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5263 33aa809d 2019-08-08 stsp }
5264 a129376b 2019-03-28 stsp }
5265 a129376b 2019-03-28 stsp break;
5266 e20a8b6f 2019-06-04 stsp }
5267 a129376b 2019-03-28 stsp default:
5268 1f1abb7e 2019-08-08 stsp break;
5269 a129376b 2019-03-28 stsp }
5270 a129376b 2019-03-28 stsp done:
5271 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5272 33aa809d 2019-08-08 stsp free(path_content);
5273 e20a8b6f 2019-06-04 stsp free(parent_path);
5274 a129376b 2019-03-28 stsp free(tree_path);
5275 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5276 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5277 a129376b 2019-03-28 stsp if (blob)
5278 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5279 a129376b 2019-03-28 stsp if (tree)
5280 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5281 a129376b 2019-03-28 stsp free(tree_id);
5282 a44927cc 2022-04-07 stsp if (base_commit)
5283 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
5284 e20a8b6f 2019-06-04 stsp return err;
5285 e20a8b6f 2019-06-04 stsp }
5286 e20a8b6f 2019-06-04 stsp
5287 e20a8b6f 2019-06-04 stsp const struct got_error *
5288 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5289 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5290 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5291 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5292 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5293 e20a8b6f 2019-06-04 stsp {
5294 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5295 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5296 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5297 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5298 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5299 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5300 e20a8b6f 2019-06-04 stsp
5301 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5302 e20a8b6f 2019-06-04 stsp if (err)
5303 e20a8b6f 2019-06-04 stsp return err;
5304 e20a8b6f 2019-06-04 stsp
5305 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5306 e20a8b6f 2019-06-04 stsp if (err)
5307 e20a8b6f 2019-06-04 stsp goto done;
5308 e20a8b6f 2019-06-04 stsp
5309 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5310 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5311 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5312 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5313 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5314 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5315 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5316 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
5317 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5318 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5319 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5320 e20a8b6f 2019-06-04 stsp if (err)
5321 af12c6b9 2019-06-04 stsp break;
5322 e20a8b6f 2019-06-04 stsp }
5323 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5324 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5325 e20a8b6f 2019-06-04 stsp err = sync_err;
5326 af12c6b9 2019-06-04 stsp done:
5327 fb399478 2019-07-12 stsp free(fileindex_path);
5328 a129376b 2019-03-28 stsp if (fileindex)
5329 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5330 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5331 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5332 a129376b 2019-03-28 stsp err = unlockerr;
5333 c4296144 2019-05-09 stsp return err;
5334 c4296144 2019-05-09 stsp }
5335 c4296144 2019-05-09 stsp
5336 cf066bf8 2019-05-09 stsp static void
5337 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5338 cf066bf8 2019-05-09 stsp {
5339 24519714 2019-05-09 stsp free(ct->path);
5340 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5341 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5342 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5343 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5344 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5345 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5346 cf066bf8 2019-05-09 stsp free(ct);
5347 cf066bf8 2019-05-09 stsp }
5348 24519714 2019-05-09 stsp
5349 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5350 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5351 24519714 2019-05-09 stsp struct got_repository *repo;
5352 24519714 2019-05-09 stsp struct got_worktree *worktree;
5353 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5354 5f8a88c6 2019-08-03 stsp int have_staged_files;
5355 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5356 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5357 12383673 2023-02-18 mark int commit_conflicts;
5358 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5359 2a47b1e5 2022-11-01 stsp FILE *f1;
5360 2a47b1e5 2022-11-01 stsp FILE *f2;
5361 24519714 2019-05-09 stsp };
5362 2a47b1e5 2022-11-01 stsp
5363 2a47b1e5 2022-11-01 stsp /*
5364 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5365 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5366 2a47b1e5 2022-11-01 stsp */
5367 2a47b1e5 2022-11-01 stsp static const struct got_error *
5368 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5369 2a47b1e5 2022-11-01 stsp const char *abspath)
5370 2a47b1e5 2022-11-01 stsp {
5371 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5372 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5373 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5374 2a47b1e5 2022-11-01 stsp
5375 2a47b1e5 2022-11-01 stsp *fd = -1;
5376 2a47b1e5 2022-11-01 stsp
5377 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5378 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5379 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5380 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5381 2a47b1e5 2022-11-01 stsp } else {
5382 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5383 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5384 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5385 2a47b1e5 2022-11-01 stsp }
5386 2a47b1e5 2022-11-01 stsp
5387 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5388 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5389 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5390 2a47b1e5 2022-11-01 stsp
5391 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5392 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5393 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5394 2a47b1e5 2022-11-01 stsp goto done;
5395 2a47b1e5 2022-11-01 stsp }
5396 2a47b1e5 2022-11-01 stsp
5397 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5398 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5399 2a47b1e5 2022-11-01 stsp goto done;
5400 2a47b1e5 2022-11-01 stsp }
5401 2a47b1e5 2022-11-01 stsp done:
5402 2a47b1e5 2022-11-01 stsp if (err) {
5403 2a47b1e5 2022-11-01 stsp close(*fd);
5404 2a47b1e5 2022-11-01 stsp *fd = -1;
5405 2a47b1e5 2022-11-01 stsp }
5406 2a47b1e5 2022-11-01 stsp return err;
5407 2a47b1e5 2022-11-01 stsp }
5408 2a47b1e5 2022-11-01 stsp
5409 2a47b1e5 2022-11-01 stsp static const struct got_error *
5410 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5411 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5412 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5413 2a47b1e5 2022-11-01 stsp {
5414 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5415 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5416 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5417 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5418 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5419 2a47b1e5 2022-11-01 stsp struct stat sb;
5420 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5421 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5422 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5423 2a47b1e5 2022-11-01 stsp
5424 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5425 4ba5cca9 2022-11-01 stsp
5426 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5427 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5428 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5429 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5430 4ba5cca9 2022-11-01 stsp return NULL;
5431 4ba5cca9 2022-11-01 stsp } else {
5432 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5433 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5434 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5435 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5436 4ba5cca9 2022-11-01 stsp return NULL;
5437 4ba5cca9 2022-11-01 stsp }
5438 2a47b1e5 2022-11-01 stsp
5439 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5440 2a47b1e5 2022-11-01 stsp if (err)
5441 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5442 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5443 2a47b1e5 2022-11-01 stsp if (err)
5444 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5445 2a47b1e5 2022-11-01 stsp
5446 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5447 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5448 2a47b1e5 2022-11-01 stsp if (err)
5449 2a47b1e5 2022-11-01 stsp return err;
5450 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5451 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5452 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5453 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5454 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5455 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5456 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5457 2a47b1e5 2022-11-01 stsp }
5458 2a47b1e5 2022-11-01 stsp
5459 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5460 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5461 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5462 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5463 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5464 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5465 2a47b1e5 2022-11-01 stsp break;
5466 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5467 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5468 2a47b1e5 2022-11-01 stsp break;
5469 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5470 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5471 2a47b1e5 2022-11-01 stsp break;
5472 2a47b1e5 2022-11-01 stsp default:
5473 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5474 2a47b1e5 2022-11-01 stsp }
5475 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5476 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5477 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5478 2a47b1e5 2022-11-01 stsp goto done;
5479 2a47b1e5 2022-11-01 stsp }
5480 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5481 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5482 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5483 2a47b1e5 2022-11-01 stsp goto done;
5484 2a47b1e5 2022-11-01 stsp }
5485 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5486 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5487 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5488 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5489 2a47b1e5 2022-11-01 stsp goto done;
5490 2a47b1e5 2022-11-01 stsp }
5491 2a47b1e5 2022-11-01 stsp
5492 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5493 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5494 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5495 2a47b1e5 2022-11-01 stsp goto done;
5496 2a47b1e5 2022-11-01 stsp }
5497 2a47b1e5 2022-11-01 stsp
5498 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5499 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5500 2a47b1e5 2022-11-01 stsp 8192, fd1);
5501 2a47b1e5 2022-11-01 stsp if (err)
5502 2a47b1e5 2022-11-01 stsp goto done;
5503 2a47b1e5 2022-11-01 stsp }
5504 2a47b1e5 2022-11-01 stsp
5505 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5506 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5507 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5508 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5509 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5510 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5511 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5512 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5513 2a47b1e5 2022-11-01 stsp goto done;
5514 2a47b1e5 2022-11-01 stsp }
5515 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5516 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5517 2a47b1e5 2022-11-01 stsp if (err)
5518 2a47b1e5 2022-11-01 stsp goto done;
5519 2a47b1e5 2022-11-01 stsp }
5520 2a47b1e5 2022-11-01 stsp } else {
5521 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5522 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5523 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5524 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5525 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5526 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5527 2a47b1e5 2022-11-01 stsp goto done;
5528 2a47b1e5 2022-11-01 stsp }
5529 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5530 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5531 2a47b1e5 2022-11-01 stsp if (err)
5532 2a47b1e5 2022-11-01 stsp goto done;
5533 2a47b1e5 2022-11-01 stsp }
5534 2a47b1e5 2022-11-01 stsp }
5535 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5536 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5537 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5538 2a47b1e5 2022-11-01 stsp goto done;
5539 2a47b1e5 2022-11-01 stsp }
5540 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5541 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5542 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5543 2a47b1e5 2022-11-01 stsp goto done;
5544 2a47b1e5 2022-11-01 stsp }
5545 2a47b1e5 2022-11-01 stsp fd = -1;
5546 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5547 2a47b1e5 2022-11-01 stsp }
5548 2a47b1e5 2022-11-01 stsp
5549 2a47b1e5 2022-11-01 stsp if (blob1) {
5550 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5551 2a47b1e5 2022-11-01 stsp f1, blob1);
5552 2a47b1e5 2022-11-01 stsp if (err)
5553 2a47b1e5 2022-11-01 stsp goto done;
5554 2a47b1e5 2022-11-01 stsp }
5555 2a47b1e5 2022-11-01 stsp
5556 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5557 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5558 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5559 2a47b1e5 2022-11-01 stsp done:
5560 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5561 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5562 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5563 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5564 2a47b1e5 2022-11-01 stsp if (blob1)
5565 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5566 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5567 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5568 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5569 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5570 2a47b1e5 2022-11-01 stsp return err;
5571 2a47b1e5 2022-11-01 stsp }
5572 cf066bf8 2019-05-09 stsp
5573 c4296144 2019-05-09 stsp static const struct got_error *
5574 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5575 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5576 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5577 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5578 c4296144 2019-05-09 stsp {
5579 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5580 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5581 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5582 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5583 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5584 768aea60 2019-05-09 stsp struct stat sb;
5585 c4296144 2019-05-09 stsp
5586 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5587 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5588 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5589 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5590 5f8a88c6 2019-08-03 stsp return NULL;
5591 5f8a88c6 2019-08-03 stsp } else {
5592 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5593 12383673 2023-02-18 mark printf("C %s\n", relpath);
5594 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5595 12383673 2023-02-18 mark }
5596 c4296144 2019-05-09 stsp
5597 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5598 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5599 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5600 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5601 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5602 5f8a88c6 2019-08-03 stsp return NULL;
5603 5f8a88c6 2019-08-03 stsp }
5604 0b5cc0d6 2019-05-09 stsp
5605 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5606 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5607 036813ee 2019-05-09 stsp goto done;
5608 036813ee 2019-05-09 stsp }
5609 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5610 036813ee 2019-05-09 stsp parent_path = strdup("");
5611 69960a46 2019-05-09 stsp if (parent_path == NULL)
5612 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5613 69960a46 2019-05-09 stsp } else {
5614 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5615 69960a46 2019-05-09 stsp if (err)
5616 69960a46 2019-05-09 stsp return err;
5617 69960a46 2019-05-09 stsp }
5618 c4296144 2019-05-09 stsp
5619 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5620 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5621 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5622 c4296144 2019-05-09 stsp goto done;
5623 768aea60 2019-05-09 stsp }
5624 768aea60 2019-05-09 stsp
5625 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5626 768aea60 2019-05-09 stsp relpath) == -1) {
5627 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5628 768aea60 2019-05-09 stsp goto done;
5629 768aea60 2019-05-09 stsp }
5630 0aeb8099 2020-07-23 stsp
5631 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5632 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5633 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5634 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5635 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5636 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5637 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5638 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5639 0aeb8099 2020-07-23 stsp break;
5640 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5641 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5642 0aeb8099 2020-07-23 stsp break;
5643 0aeb8099 2020-07-23 stsp default:
5644 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5645 0aeb8099 2020-07-23 stsp goto done;
5646 0aeb8099 2020-07-23 stsp }
5647 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5648 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5649 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5650 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5651 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5652 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5653 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5654 12463d8b 2019-12-13 stsp ct->ondisk_path);
5655 12463d8b 2019-12-13 stsp goto done;
5656 12463d8b 2019-12-13 stsp }
5657 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5658 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5659 768aea60 2019-05-09 stsp goto done;
5660 768aea60 2019-05-09 stsp }
5661 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5662 c4296144 2019-05-09 stsp }
5663 c4296144 2019-05-09 stsp
5664 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5665 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5666 44d03001 2019-05-09 stsp relpath) == -1) {
5667 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5668 44d03001 2019-05-09 stsp goto done;
5669 44d03001 2019-05-09 stsp }
5670 44d03001 2019-05-09 stsp
5671 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5672 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5673 35213c7c 2020-07-23 stsp int is_bad_symlink;
5674 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5675 35213c7c 2020-07-23 stsp ssize_t target_len;
5676 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5677 35213c7c 2020-07-23 stsp sizeof(target_path));
5678 35213c7c 2020-07-23 stsp if (target_len == -1) {
5679 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5680 35213c7c 2020-07-23 stsp ct->ondisk_path);
5681 35213c7c 2020-07-23 stsp goto done;
5682 35213c7c 2020-07-23 stsp }
5683 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5684 df6221c7 2023-07-19 stsp target_len, ct->ondisk_path, a->worktree->root_path,
5685 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
5686 35213c7c 2020-07-23 stsp if (err)
5687 35213c7c 2020-07-23 stsp goto done;
5688 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5689 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5690 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5691 35213c7c 2020-07-23 stsp goto done;
5692 35213c7c 2020-07-23 stsp }
5693 35213c7c 2020-07-23 stsp }
5694 35213c7c 2020-07-23 stsp
5695 35213c7c 2020-07-23 stsp
5696 cf066bf8 2019-05-09 stsp ct->status = status;
5697 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5698 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5699 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5700 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5701 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5702 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5703 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5704 b416585c 2019-05-13 stsp goto done;
5705 b416585c 2019-05-13 stsp }
5706 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5707 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5708 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5709 f0b75401 2019-08-03 stsp goto done;
5710 f0b75401 2019-08-03 stsp }
5711 f0b75401 2019-08-03 stsp }
5712 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5713 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5714 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5715 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5716 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5717 036813ee 2019-05-09 stsp goto done;
5718 036813ee 2019-05-09 stsp }
5719 ca2503ea 2019-05-09 stsp }
5720 24519714 2019-05-09 stsp ct->path = strdup(path);
5721 24519714 2019-05-09 stsp if (ct->path == NULL) {
5722 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5723 24519714 2019-05-09 stsp goto done;
5724 24519714 2019-05-09 stsp }
5725 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5726 2a47b1e5 2022-11-01 stsp if (err)
5727 2a47b1e5 2022-11-01 stsp goto done;
5728 2a47b1e5 2022-11-01 stsp
5729 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5730 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5731 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5732 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5733 2a47b1e5 2022-11-01 stsp if (err)
5734 2a47b1e5 2022-11-01 stsp goto done;
5735 2a47b1e5 2022-11-01 stsp }
5736 c4296144 2019-05-09 stsp done:
5737 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5738 ed175427 2019-05-09 stsp free_commitable(ct);
5739 24519714 2019-05-09 stsp free(parent_path);
5740 036813ee 2019-05-09 stsp free(path);
5741 a129376b 2019-03-28 stsp return err;
5742 a129376b 2019-03-28 stsp }
5743 c4296144 2019-05-09 stsp
5744 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5745 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5746 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5747 036813ee 2019-05-09 stsp struct got_repository *);
5748 ed175427 2019-05-09 stsp
5749 ed175427 2019-05-09 stsp static const struct got_error *
5750 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5751 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5752 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5753 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5754 afa376bf 2019-05-09 stsp struct got_repository *repo)
5755 ed175427 2019-05-09 stsp {
5756 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5757 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5758 036813ee 2019-05-09 stsp char *subpath;
5759 ed175427 2019-05-09 stsp
5760 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5761 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5762 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5763 ed175427 2019-05-09 stsp
5764 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5765 ed175427 2019-05-09 stsp if (err)
5766 ed175427 2019-05-09 stsp return err;
5767 ed175427 2019-05-09 stsp
5768 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5769 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5770 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5771 036813ee 2019-05-09 stsp free(subpath);
5772 036813ee 2019-05-09 stsp return err;
5773 036813ee 2019-05-09 stsp }
5774 ed175427 2019-05-09 stsp
5775 036813ee 2019-05-09 stsp static const struct got_error *
5776 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5777 036813ee 2019-05-09 stsp {
5778 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5779 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5780 ed175427 2019-05-09 stsp
5781 036813ee 2019-05-09 stsp *match = 0;
5782 ed175427 2019-05-09 stsp
5783 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5784 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5785 0f63689d 2019-05-10 stsp return NULL;
5786 036813ee 2019-05-09 stsp }
5787 0b5cc0d6 2019-05-09 stsp
5788 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5789 0f63689d 2019-05-10 stsp if (err)
5790 0f63689d 2019-05-10 stsp return err;
5791 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5792 036813ee 2019-05-09 stsp free(ct_parent_path);
5793 036813ee 2019-05-09 stsp return err;
5794 036813ee 2019-05-09 stsp }
5795 0b5cc0d6 2019-05-09 stsp
5796 768aea60 2019-05-09 stsp static mode_t
5797 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5798 768aea60 2019-05-09 stsp {
5799 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5800 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5801 3d9a4ec4 2020-07-23 stsp
5802 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5803 768aea60 2019-05-09 stsp }
5804 768aea60 2019-05-09 stsp
5805 036813ee 2019-05-09 stsp static const struct got_error *
5806 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5807 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5808 036813ee 2019-05-09 stsp {
5809 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5810 ca2503ea 2019-05-09 stsp
5811 036813ee 2019-05-09 stsp *new_te = NULL;
5812 0b5cc0d6 2019-05-09 stsp
5813 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5814 036813ee 2019-05-09 stsp if (err)
5815 036813ee 2019-05-09 stsp goto done;
5816 0b5cc0d6 2019-05-09 stsp
5817 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5818 036813ee 2019-05-09 stsp
5819 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5820 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5821 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5822 5f8a88c6 2019-08-03 stsp else
5823 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5824 036813ee 2019-05-09 stsp done:
5825 036813ee 2019-05-09 stsp if (err && *new_te) {
5826 56e0773d 2019-11-28 stsp free(*new_te);
5827 036813ee 2019-05-09 stsp *new_te = NULL;
5828 036813ee 2019-05-09 stsp }
5829 036813ee 2019-05-09 stsp return err;
5830 036813ee 2019-05-09 stsp }
5831 036813ee 2019-05-09 stsp
5832 036813ee 2019-05-09 stsp static const struct got_error *
5833 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5834 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5835 036813ee 2019-05-09 stsp {
5836 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5837 102b254e 2020-10-19 stsp char *ct_name = NULL;
5838 036813ee 2019-05-09 stsp
5839 036813ee 2019-05-09 stsp *new_te = NULL;
5840 036813ee 2019-05-09 stsp
5841 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5842 036813ee 2019-05-09 stsp if (*new_te == NULL)
5843 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5844 036813ee 2019-05-09 stsp
5845 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5846 102b254e 2020-10-19 stsp if (err)
5847 036813ee 2019-05-09 stsp goto done;
5848 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5849 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5850 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5851 036813ee 2019-05-09 stsp goto done;
5852 036813ee 2019-05-09 stsp }
5853 036813ee 2019-05-09 stsp
5854 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5855 036813ee 2019-05-09 stsp
5856 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5857 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5858 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5859 5f8a88c6 2019-08-03 stsp else
5860 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5861 036813ee 2019-05-09 stsp done:
5862 102b254e 2020-10-19 stsp free(ct_name);
5863 036813ee 2019-05-09 stsp if (err && *new_te) {
5864 56e0773d 2019-11-28 stsp free(*new_te);
5865 036813ee 2019-05-09 stsp *new_te = NULL;
5866 036813ee 2019-05-09 stsp }
5867 036813ee 2019-05-09 stsp return err;
5868 036813ee 2019-05-09 stsp }
5869 036813ee 2019-05-09 stsp
5870 036813ee 2019-05-09 stsp static const struct got_error *
5871 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5872 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5873 036813ee 2019-05-09 stsp {
5874 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5875 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5876 036813ee 2019-05-09 stsp
5877 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5878 036813ee 2019-05-09 stsp if (err)
5879 036813ee 2019-05-09 stsp return err;
5880 036813ee 2019-05-09 stsp if (new_pe == NULL)
5881 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5882 036813ee 2019-05-09 stsp return NULL;
5883 afa376bf 2019-05-09 stsp }
5884 afa376bf 2019-05-09 stsp
5885 afa376bf 2019-05-09 stsp static const struct got_error *
5886 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5887 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5888 afa376bf 2019-05-09 stsp {
5889 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5890 5f8a88c6 2019-08-03 stsp unsigned char status;
5891 0ff8d236 2021-09-28 stsp
5892 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5893 0ff8d236 2021-09-28 stsp return NULL;
5894 5f8a88c6 2019-08-03 stsp
5895 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5896 afa376bf 2019-05-09 stsp ct_path++;
5897 5f8a88c6 2019-08-03 stsp
5898 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5899 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5900 5f8a88c6 2019-08-03 stsp else
5901 5f8a88c6 2019-08-03 stsp status = ct->status;
5902 5f8a88c6 2019-08-03 stsp
5903 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5904 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5905 036813ee 2019-05-09 stsp }
5906 036813ee 2019-05-09 stsp
5907 036813ee 2019-05-09 stsp static const struct got_error *
5908 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5909 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5910 44d03001 2019-05-09 stsp {
5911 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5912 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5913 44d03001 2019-05-09 stsp char *te_path;
5914 44d03001 2019-05-09 stsp
5915 44d03001 2019-05-09 stsp *modified = 0;
5916 44d03001 2019-05-09 stsp
5917 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5918 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5919 44d03001 2019-05-09 stsp te->name) == -1)
5920 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5921 44d03001 2019-05-09 stsp
5922 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5923 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5924 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5925 44d03001 2019-05-09 stsp strlen(te_path));
5926 62d463ca 2020-10-20 naddy if (*modified)
5927 44d03001 2019-05-09 stsp break;
5928 44d03001 2019-05-09 stsp }
5929 44d03001 2019-05-09 stsp
5930 44d03001 2019-05-09 stsp free(te_path);
5931 44d03001 2019-05-09 stsp return err;
5932 44d03001 2019-05-09 stsp }
5933 44d03001 2019-05-09 stsp
5934 44d03001 2019-05-09 stsp static const struct got_error *
5935 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5936 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5937 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5938 036813ee 2019-05-09 stsp {
5939 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5940 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5941 036813ee 2019-05-09 stsp
5942 036813ee 2019-05-09 stsp *ctp = NULL;
5943 036813ee 2019-05-09 stsp
5944 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5945 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5946 036813ee 2019-05-09 stsp char *ct_name = NULL;
5947 036813ee 2019-05-09 stsp int path_matches;
5948 036813ee 2019-05-09 stsp
5949 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5950 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5951 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5952 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5953 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5954 5f8a88c6 2019-08-03 stsp continue;
5955 5f8a88c6 2019-08-03 stsp } else {
5956 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5957 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5958 5f8a88c6 2019-08-03 stsp continue;
5959 5f8a88c6 2019-08-03 stsp }
5960 036813ee 2019-05-09 stsp
5961 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5962 036813ee 2019-05-09 stsp continue;
5963 036813ee 2019-05-09 stsp
5964 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5965 62d463ca 2020-10-20 naddy if (err)
5966 036813ee 2019-05-09 stsp return err;
5967 036813ee 2019-05-09 stsp if (!path_matches)
5968 036813ee 2019-05-09 stsp continue;
5969 036813ee 2019-05-09 stsp
5970 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5971 d34b633e 2020-10-19 stsp if (err)
5972 d34b633e 2020-10-19 stsp return err;
5973 d34b633e 2020-10-19 stsp
5974 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5975 d34b633e 2020-10-19 stsp free(ct_name);
5976 036813ee 2019-05-09 stsp continue;
5977 d34b633e 2020-10-19 stsp }
5978 d34b633e 2020-10-19 stsp free(ct_name);
5979 036813ee 2019-05-09 stsp
5980 036813ee 2019-05-09 stsp *ctp = ct;
5981 036813ee 2019-05-09 stsp break;
5982 036813ee 2019-05-09 stsp }
5983 2b496619 2019-07-10 stsp
5984 2b496619 2019-07-10 stsp return err;
5985 2b496619 2019-07-10 stsp }
5986 2b496619 2019-07-10 stsp
5987 2b496619 2019-07-10 stsp static const struct got_error *
5988 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5989 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5990 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5991 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5992 2b496619 2019-07-10 stsp struct got_repository *repo)
5993 2b496619 2019-07-10 stsp {
5994 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5995 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5996 2b496619 2019-07-10 stsp char *subtree_path;
5997 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5998 ba580f68 2020-03-22 stsp int nentries;
5999 2b496619 2019-07-10 stsp
6000 2b496619 2019-07-10 stsp *new_tep = NULL;
6001 2b496619 2019-07-10 stsp
6002 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6003 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6004 2b496619 2019-07-10 stsp child_path) == -1)
6005 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6006 036813ee 2019-05-09 stsp
6007 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6008 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6009 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6010 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6011 56e0773d 2019-11-28 stsp
6012 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6013 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6014 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6015 2b496619 2019-07-10 stsp goto done;
6016 2b496619 2019-07-10 stsp }
6017 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6018 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6019 2b496619 2019-07-10 stsp if (err) {
6020 56e0773d 2019-11-28 stsp free(new_te);
6021 2b496619 2019-07-10 stsp goto done;
6022 2b496619 2019-07-10 stsp }
6023 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6024 2b496619 2019-07-10 stsp done:
6025 56e0773d 2019-11-28 stsp free(id);
6026 2b496619 2019-07-10 stsp free(subtree_path);
6027 2b496619 2019-07-10 stsp if (err == NULL)
6028 2b496619 2019-07-10 stsp *new_tep = new_te;
6029 036813ee 2019-05-09 stsp return err;
6030 036813ee 2019-05-09 stsp }
6031 036813ee 2019-05-09 stsp
6032 036813ee 2019-05-09 stsp static const struct got_error *
6033 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6034 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6035 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6036 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6037 036813ee 2019-05-09 stsp struct got_repository *repo)
6038 036813ee 2019-05-09 stsp {
6039 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6040 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6041 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6042 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6043 036813ee 2019-05-09 stsp
6044 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6045 ba580f68 2020-03-22 stsp *nentries = 0;
6046 036813ee 2019-05-09 stsp
6047 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6048 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6049 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6050 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6051 036813ee 2019-05-09 stsp
6052 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6053 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6054 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6055 036813ee 2019-05-09 stsp continue;
6056 036813ee 2019-05-09 stsp
6057 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6058 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6059 036813ee 2019-05-09 stsp continue;
6060 036813ee 2019-05-09 stsp
6061 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6062 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6063 036813ee 2019-05-09 stsp if (err)
6064 036813ee 2019-05-09 stsp goto done;
6065 036813ee 2019-05-09 stsp
6066 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6067 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6068 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6069 036813ee 2019-05-09 stsp if (err)
6070 036813ee 2019-05-09 stsp goto done;
6071 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6072 afa376bf 2019-05-09 stsp if (err)
6073 afa376bf 2019-05-09 stsp goto done;
6074 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6075 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6076 2b496619 2019-07-10 stsp if (err)
6077 2f51b5b3 2019-05-09 stsp goto done;
6078 ba580f68 2020-03-22 stsp (*nentries)++;
6079 2b496619 2019-07-10 stsp } else {
6080 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6081 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6082 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6083 2b496619 2019-07-10 stsp == NULL) {
6084 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6085 2b496619 2019-07-10 stsp child_path, path_base_tree,
6086 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6087 2b496619 2019-07-10 stsp repo);
6088 2b496619 2019-07-10 stsp if (err)
6089 2b496619 2019-07-10 stsp goto done;
6090 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6091 2b496619 2019-07-10 stsp if (err)
6092 2b496619 2019-07-10 stsp goto done;
6093 ba580f68 2020-03-22 stsp (*nentries)++;
6094 9ba0479c 2019-05-10 stsp }
6095 ed175427 2019-05-09 stsp }
6096 2f51b5b3 2019-05-09 stsp }
6097 2f51b5b3 2019-05-09 stsp
6098 2f51b5b3 2019-05-09 stsp if (base_tree) {
6099 56e0773d 2019-11-28 stsp int i, nbase_entries;
6100 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6101 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6102 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6103 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6104 63c5ca5d 2019-08-24 stsp
6105 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6106 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6107 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6108 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6109 63c5ca5d 2019-08-24 stsp if (err)
6110 63c5ca5d 2019-08-24 stsp goto done;
6111 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6112 63c5ca5d 2019-08-24 stsp if (err)
6113 63c5ca5d 2019-08-24 stsp goto done;
6114 ba580f68 2020-03-22 stsp (*nentries)++;
6115 63c5ca5d 2019-08-24 stsp continue;
6116 63c5ca5d 2019-08-24 stsp }
6117 2f51b5b3 2019-05-09 stsp
6118 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6119 44d03001 2019-05-09 stsp int modified;
6120 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6121 036813ee 2019-05-09 stsp if (err)
6122 036813ee 2019-05-09 stsp goto done;
6123 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6124 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6125 2f51b5b3 2019-05-09 stsp if (err)
6126 2f51b5b3 2019-05-09 stsp goto done;
6127 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6128 44d03001 2019-05-09 stsp if (modified) {
6129 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6130 ba580f68 2020-03-22 stsp int nsubentries;
6131 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6132 ba580f68 2020-03-22 stsp &nsubentries, te,
6133 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6134 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6135 44d03001 2019-05-09 stsp if (err)
6136 44d03001 2019-05-09 stsp goto done;
6137 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6138 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6139 ba580f68 2020-03-22 stsp free(new_id);
6140 ba580f68 2020-03-22 stsp continue;
6141 ba580f68 2020-03-22 stsp }
6142 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6143 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6144 56e0773d 2019-11-28 stsp free(new_id);
6145 44d03001 2019-05-09 stsp }
6146 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6147 036813ee 2019-05-09 stsp if (err)
6148 036813ee 2019-05-09 stsp goto done;
6149 ba580f68 2020-03-22 stsp (*nentries)++;
6150 2f51b5b3 2019-05-09 stsp continue;
6151 036813ee 2019-05-09 stsp }
6152 2f51b5b3 2019-05-09 stsp
6153 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6154 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6155 f66c734c 2019-09-22 stsp if (err)
6156 f66c734c 2019-09-22 stsp goto done;
6157 2f51b5b3 2019-05-09 stsp if (ct) {
6158 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6159 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6160 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6161 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
6162 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6163 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6164 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6165 2f51b5b3 2019-05-09 stsp if (err)
6166 2f51b5b3 2019-05-09 stsp goto done;
6167 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6168 2f51b5b3 2019-05-09 stsp if (err)
6169 2f51b5b3 2019-05-09 stsp goto done;
6170 ba580f68 2020-03-22 stsp (*nentries)++;
6171 2f51b5b3 2019-05-09 stsp }
6172 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6173 b416585c 2019-05-13 stsp status_arg);
6174 afa376bf 2019-05-09 stsp if (err)
6175 afa376bf 2019-05-09 stsp goto done;
6176 2f51b5b3 2019-05-09 stsp } else {
6177 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6178 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6179 2f51b5b3 2019-05-09 stsp if (err)
6180 2f51b5b3 2019-05-09 stsp goto done;
6181 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6182 2f51b5b3 2019-05-09 stsp if (err)
6183 2f51b5b3 2019-05-09 stsp goto done;
6184 ba580f68 2020-03-22 stsp (*nentries)++;
6185 2f51b5b3 2019-05-09 stsp }
6186 ca2503ea 2019-05-09 stsp }
6187 ed175427 2019-05-09 stsp }
6188 0b5cc0d6 2019-05-09 stsp
6189 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6190 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6191 ed175427 2019-05-09 stsp done:
6192 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6193 2e1fa222 2020-07-23 stsp return err;
6194 2e1fa222 2020-07-23 stsp }
6195 2e1fa222 2020-07-23 stsp
6196 2e1fa222 2020-07-23 stsp static const struct got_error *
6197 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6198 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6199 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6200 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6201 ebf99748 2019-05-09 stsp {
6202 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6203 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6204 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6205 ebf99748 2019-05-09 stsp
6206 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6207 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6208 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6209 ebf99748 2019-05-09 stsp
6210 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6211 437adc9d 2020-12-10 yzhong
6212 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6213 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6214 437adc9d 2020-12-10 yzhong if (err)
6215 437adc9d 2020-12-10 yzhong goto done;
6216 437adc9d 2020-12-10 yzhong
6217 ebf99748 2019-05-09 stsp if (ie) {
6218 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6219 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6220 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6221 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6222 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6223 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6224 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6225 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6226 437adc9d 2020-12-10 yzhong
6227 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6228 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6229 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6230 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6231 72fd46fa 2019-09-06 stsp !have_staged_files);
6232 ebf99748 2019-05-09 stsp } else
6233 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6234 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6235 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6236 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6237 72fd46fa 2019-09-06 stsp !have_staged_files);
6238 ebf99748 2019-05-09 stsp } else {
6239 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6240 ebf99748 2019-05-09 stsp if (err)
6241 437adc9d 2020-12-10 yzhong goto done;
6242 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6243 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6244 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6245 3969253a 2020-03-07 stsp if (err) {
6246 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6247 437adc9d 2020-12-10 yzhong goto done;
6248 3969253a 2020-03-07 stsp }
6249 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6250 3969253a 2020-03-07 stsp if (err) {
6251 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6252 437adc9d 2020-12-10 yzhong goto done;
6253 3969253a 2020-03-07 stsp }
6254 ebf99748 2019-05-09 stsp }
6255 437adc9d 2020-12-10 yzhong free(relpath);
6256 437adc9d 2020-12-10 yzhong relpath = NULL;
6257 ebf99748 2019-05-09 stsp }
6258 437adc9d 2020-12-10 yzhong done:
6259 437adc9d 2020-12-10 yzhong free(relpath);
6260 d56d26ce 2019-05-10 stsp return err;
6261 d56d26ce 2019-05-10 stsp }
6262 735ef5ac 2019-08-03 stsp
6263 d56d26ce 2019-05-10 stsp
6264 d56d26ce 2019-05-10 stsp static const struct got_error *
6265 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6266 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6267 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6268 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6269 735ef5ac 2019-08-03 stsp int ood_errcode)
6270 d56d26ce 2019-05-10 stsp {
6271 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6272 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6273 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6274 1a36436d 2019-06-10 stsp
6275 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6276 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6277 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6278 1a36436d 2019-06-10 stsp return NULL;
6279 9bead371 2019-07-28 stsp /*
6280 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6281 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6282 9bead371 2019-07-28 stsp */
6283 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6284 a44927cc 2022-04-07 stsp if (err)
6285 a44927cc 2022-04-07 stsp goto done;
6286 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6287 9bead371 2019-07-28 stsp if (err) {
6288 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6289 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6290 1a36436d 2019-06-10 stsp goto done;
6291 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6292 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6293 1a36436d 2019-06-10 stsp } else {
6294 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6295 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6296 a44927cc 2022-04-07 stsp if (err)
6297 a44927cc 2022-04-07 stsp goto done;
6298 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6299 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6300 1a36436d 2019-06-10 stsp goto done;
6301 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6302 1a36436d 2019-06-10 stsp }
6303 1a36436d 2019-06-10 stsp done:
6304 1a36436d 2019-06-10 stsp free(id);
6305 a44927cc 2022-04-07 stsp if (commit)
6306 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6307 1a36436d 2019-06-10 stsp return err;
6308 ebf99748 2019-05-09 stsp }
6309 ebf99748 2019-05-09 stsp
6310 336075a4 2022-06-25 op static const struct got_error *
6311 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6312 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6313 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
6314 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
6315 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
6316 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
6317 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6318 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6319 afa376bf 2019-05-09 stsp struct got_repository *repo)
6320 c4296144 2019-05-09 stsp {
6321 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6322 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6323 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6324 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6325 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6326 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6327 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6328 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6329 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6330 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6331 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6332 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6333 f8399b8f 2022-07-22 stsp time_t timestamp;
6334 c4296144 2019-05-09 stsp
6335 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6336 c4296144 2019-05-09 stsp
6337 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6338 675c7539 2019-05-09 stsp
6339 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6340 588edf97 2019-05-10 stsp if (err)
6341 588edf97 2019-05-10 stsp goto done;
6342 de18fc63 2019-05-09 stsp
6343 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6344 588edf97 2019-05-10 stsp if (err)
6345 588edf97 2019-05-10 stsp goto done;
6346 588edf97 2019-05-10 stsp
6347 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6348 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6349 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6350 33ad4cbe 2019-05-12 jcs if (err)
6351 33ad4cbe 2019-05-12 jcs goto done;
6352 33ad4cbe 2019-05-12 jcs }
6353 c4296144 2019-05-09 stsp
6354 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6355 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6356 33ad4cbe 2019-05-12 jcs goto done;
6357 33ad4cbe 2019-05-12 jcs }
6358 33ad4cbe 2019-05-12 jcs
6359 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6360 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6361 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6362 cf066bf8 2019-05-09 stsp char *ondisk_path;
6363 cf066bf8 2019-05-09 stsp
6364 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6365 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6366 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6367 5f8a88c6 2019-08-03 stsp continue;
6368 5f8a88c6 2019-08-03 stsp
6369 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6370 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6371 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6372 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6373 cf066bf8 2019-05-09 stsp continue;
6374 cf066bf8 2019-05-09 stsp
6375 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6376 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6377 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6378 cf066bf8 2019-05-09 stsp goto done;
6379 cf066bf8 2019-05-09 stsp }
6380 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6381 2e1fa222 2020-07-23 stsp free(ondisk_path);
6382 2e1fa222 2020-07-23 stsp if (err)
6383 cf066bf8 2019-05-09 stsp goto done;
6384 cf066bf8 2019-05-09 stsp }
6385 036813ee 2019-05-09 stsp
6386 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6387 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6388 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6389 036813ee 2019-05-09 stsp if (err)
6390 036813ee 2019-05-09 stsp goto done;
6391 036813ee 2019-05-09 stsp
6392 2b706956 2023-04-17 stsp err = got_object_qid_alloc(&pid, head_commit_id);
6393 de18fc63 2019-05-09 stsp if (err)
6394 de18fc63 2019-05-09 stsp goto done;
6395 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6396 f259c4c1 2021-09-24 stsp nparents++;
6397 f259c4c1 2021-09-24 stsp if (parent_id2) {
6398 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6399 f259c4c1 2021-09-24 stsp if (err)
6400 f259c4c1 2021-09-24 stsp goto done;
6401 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6402 f259c4c1 2021-09-24 stsp nparents++;
6403 f259c4c1 2021-09-24 stsp }
6404 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6405 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6406 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6407 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6408 33ad4cbe 2019-05-12 jcs free(logmsg);
6409 9d40349a 2019-05-09 stsp if (err)
6410 9d40349a 2019-05-09 stsp goto done;
6411 9d40349a 2019-05-09 stsp
6412 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6413 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6414 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6415 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6416 09f5bd90 2019-05-09 stsp goto done;
6417 09f5bd90 2019-05-09 stsp }
6418 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6419 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6420 09f5bd90 2019-05-09 stsp if (err)
6421 09f5bd90 2019-05-09 stsp goto done;
6422 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6423 ebf99748 2019-05-09 stsp if (err)
6424 ebf99748 2019-05-09 stsp goto done;
6425 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6426 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6427 09f5bd90 2019-05-09 stsp goto done;
6428 09f5bd90 2019-05-09 stsp }
6429 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6430 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6431 f2c16586 2019-05-09 stsp if (err)
6432 f2c16586 2019-05-09 stsp goto done;
6433 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6434 f2c16586 2019-05-09 stsp if (err)
6435 f2c16586 2019-05-09 stsp goto done;
6436 f2c16586 2019-05-09 stsp
6437 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6438 09f5bd90 2019-05-09 stsp if (err)
6439 09f5bd90 2019-05-09 stsp goto done;
6440 09f5bd90 2019-05-09 stsp
6441 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6442 ebf99748 2019-05-09 stsp if (err)
6443 ebf99748 2019-05-09 stsp goto done;
6444 c4296144 2019-05-09 stsp done:
6445 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6446 588edf97 2019-05-10 stsp if (head_tree)
6447 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6448 588edf97 2019-05-10 stsp if (head_commit)
6449 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6450 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6451 2f17228e 2019-05-12 stsp if (head_ref2) {
6452 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6453 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6454 2f17228e 2019-05-12 stsp err = unlockerr;
6455 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6456 39cd0ff6 2019-07-12 stsp }
6457 39cd0ff6 2019-07-12 stsp return err;
6458 39cd0ff6 2019-07-12 stsp }
6459 5c1e53bc 2019-07-28 stsp
6460 5c1e53bc 2019-07-28 stsp static const struct got_error *
6461 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6462 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6463 5c1e53bc 2019-07-28 stsp {
6464 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6465 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6466 39cd0ff6 2019-07-12 stsp
6467 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6468 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6469 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6470 5c1e53bc 2019-07-28 stsp
6471 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6472 5c1e53bc 2019-07-28 stsp ct_path++;
6473 5c1e53bc 2019-07-28 stsp
6474 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6475 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6476 5c1e53bc 2019-07-28 stsp break;
6477 5c1e53bc 2019-07-28 stsp }
6478 5c1e53bc 2019-07-28 stsp
6479 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6480 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6481 5c1e53bc 2019-07-28 stsp
6482 5c1e53bc 2019-07-28 stsp return NULL;
6483 5c1e53bc 2019-07-28 stsp }
6484 5c1e53bc 2019-07-28 stsp
6485 f0b75401 2019-08-03 stsp static const struct got_error *
6486 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6487 f0b75401 2019-08-03 stsp {
6488 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6489 f0b75401 2019-08-03 stsp
6490 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6491 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6492 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6493 f0b75401 2019-08-03 stsp }
6494 f0b75401 2019-08-03 stsp
6495 f0b75401 2019-08-03 stsp return NULL;
6496 f0b75401 2019-08-03 stsp }
6497 f0b75401 2019-08-03 stsp
6498 5f8a88c6 2019-08-03 stsp static const struct got_error *
6499 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6500 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6501 5f8a88c6 2019-08-03 stsp {
6502 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6503 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6504 5f8a88c6 2019-08-03 stsp
6505 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6506 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6507 5f8a88c6 2019-08-03 stsp continue;
6508 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6509 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6510 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6511 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6512 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6513 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6514 5f8a88c6 2019-08-03 stsp }
6515 5f8a88c6 2019-08-03 stsp
6516 5f8a88c6 2019-08-03 stsp return NULL;
6517 5f8a88c6 2019-08-03 stsp }
6518 5f8a88c6 2019-08-03 stsp
6519 39cd0ff6 2019-07-12 stsp const struct got_error *
6520 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6521 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6522 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6523 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6524 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6525 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6526 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6527 39cd0ff6 2019-07-12 stsp {
6528 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6529 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6530 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6531 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6532 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6533 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6534 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6535 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6536 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6537 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6538 39cd0ff6 2019-07-12 stsp
6539 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6540 39cd0ff6 2019-07-12 stsp
6541 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6542 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6543 39cd0ff6 2019-07-12 stsp
6544 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6545 39cd0ff6 2019-07-12 stsp if (err)
6546 39cd0ff6 2019-07-12 stsp goto done;
6547 39cd0ff6 2019-07-12 stsp
6548 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6549 39cd0ff6 2019-07-12 stsp if (err)
6550 39cd0ff6 2019-07-12 stsp goto done;
6551 39cd0ff6 2019-07-12 stsp
6552 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6553 39cd0ff6 2019-07-12 stsp if (err)
6554 39cd0ff6 2019-07-12 stsp goto done;
6555 39cd0ff6 2019-07-12 stsp
6556 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6557 39cd0ff6 2019-07-12 stsp if (err)
6558 39cd0ff6 2019-07-12 stsp goto done;
6559 39cd0ff6 2019-07-12 stsp
6560 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6561 5f8a88c6 2019-08-03 stsp &have_staged_files);
6562 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6563 5f8a88c6 2019-08-03 stsp goto done;
6564 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6565 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6566 5f8a88c6 2019-08-03 stsp if (err)
6567 5f8a88c6 2019-08-03 stsp goto done;
6568 5f8a88c6 2019-08-03 stsp }
6569 5f8a88c6 2019-08-03 stsp
6570 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6571 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6572 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6573 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6574 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6575 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6576 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6577 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6578 2a47b1e5 2022-11-01 stsp if (show_diff) {
6579 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6580 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6581 2a47b1e5 2022-11-01 stsp if (err)
6582 2a47b1e5 2022-11-01 stsp goto done;
6583 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6584 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6585 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6586 2a47b1e5 2022-11-01 stsp goto done;
6587 2a47b1e5 2022-11-01 stsp }
6588 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6589 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6590 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6591 2a47b1e5 2022-11-01 stsp goto done;
6592 2a47b1e5 2022-11-01 stsp }
6593 2a47b1e5 2022-11-01 stsp }
6594 2a47b1e5 2022-11-01 stsp
6595 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6596 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6597 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6598 5c1e53bc 2019-07-28 stsp if (err)
6599 5c1e53bc 2019-07-28 stsp goto done;
6600 5c1e53bc 2019-07-28 stsp }
6601 39cd0ff6 2019-07-12 stsp
6602 2a47b1e5 2022-11-01 stsp if (show_diff) {
6603 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6604 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6605 2a47b1e5 2022-11-01 stsp goto done;
6606 2a47b1e5 2022-11-01 stsp }
6607 2a47b1e5 2022-11-01 stsp }
6608 2a47b1e5 2022-11-01 stsp
6609 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6610 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6611 39cd0ff6 2019-07-12 stsp goto done;
6612 5c1e53bc 2019-07-28 stsp }
6613 5c1e53bc 2019-07-28 stsp
6614 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6615 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6616 5c1e53bc 2019-07-28 stsp if (err)
6617 5c1e53bc 2019-07-28 stsp goto done;
6618 39cd0ff6 2019-07-12 stsp }
6619 f0b75401 2019-08-03 stsp
6620 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6621 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6622 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6623 f0b75401 2019-08-03 stsp
6624 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6625 f0b75401 2019-08-03 stsp ct_path++;
6626 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6627 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6628 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6629 b50cabdf 2019-07-12 stsp if (err)
6630 b50cabdf 2019-07-12 stsp goto done;
6631 f0b75401 2019-08-03 stsp
6632 b50cabdf 2019-07-12 stsp }
6633 b50cabdf 2019-07-12 stsp
6634 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6635 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6636 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6637 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6638 39cd0ff6 2019-07-12 stsp if (err)
6639 39cd0ff6 2019-07-12 stsp goto done;
6640 39cd0ff6 2019-07-12 stsp
6641 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6642 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6643 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6644 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6645 39cd0ff6 2019-07-12 stsp err = sync_err;
6646 39cd0ff6 2019-07-12 stsp done:
6647 39cd0ff6 2019-07-12 stsp if (fileindex)
6648 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6649 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6650 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6651 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6652 39cd0ff6 2019-07-12 stsp err = unlockerr;
6653 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6654 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6655 d8bacb93 2023-01-10 mark
6656 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6657 39cd0ff6 2019-07-12 stsp }
6658 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6659 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6660 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6661 2a47b1e5 2022-11-01 stsp free(diff_path);
6662 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6663 2a47b1e5 2022-11-01 stsp err == NULL)
6664 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6665 c4296144 2019-05-09 stsp return err;
6666 8656d6c4 2019-05-20 stsp }
6667 8656d6c4 2019-05-20 stsp
6668 8656d6c4 2019-05-20 stsp const char *
6669 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6670 8656d6c4 2019-05-20 stsp {
6671 8656d6c4 2019-05-20 stsp return ct->path;
6672 8656d6c4 2019-05-20 stsp }
6673 8656d6c4 2019-05-20 stsp
6674 8656d6c4 2019-05-20 stsp unsigned int
6675 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6676 8656d6c4 2019-05-20 stsp {
6677 8656d6c4 2019-05-20 stsp return ct->status;
6678 818c7501 2019-07-11 stsp }
6679 818c7501 2019-07-11 stsp
6680 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6681 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6682 818c7501 2019-07-11 stsp struct got_repository *repo;
6683 818c7501 2019-07-11 stsp };
6684 818c7501 2019-07-11 stsp
6685 818c7501 2019-07-11 stsp static const struct got_error *
6686 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6687 818c7501 2019-07-11 stsp {
6688 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6689 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6690 818c7501 2019-07-11 stsp unsigned char status;
6691 818c7501 2019-07-11 stsp struct stat sb;
6692 818c7501 2019-07-11 stsp char *ondisk_path;
6693 818c7501 2019-07-11 stsp
6694 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6695 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6696 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6697 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6698 818c7501 2019-07-11 stsp
6699 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6700 818c7501 2019-07-11 stsp == -1)
6701 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6702 818c7501 2019-07-11 stsp
6703 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6704 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6705 818c7501 2019-07-11 stsp free(ondisk_path);
6706 818c7501 2019-07-11 stsp if (err)
6707 818c7501 2019-07-11 stsp return err;
6708 818c7501 2019-07-11 stsp
6709 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6710 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6711 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6712 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6713 818c7501 2019-07-11 stsp
6714 818c7501 2019-07-11 stsp return NULL;
6715 818c7501 2019-07-11 stsp }
6716 818c7501 2019-07-11 stsp
6717 818c7501 2019-07-11 stsp const struct got_error *
6718 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6719 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6720 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6721 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6722 818c7501 2019-07-11 stsp {
6723 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6724 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6725 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6726 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6727 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6728 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6729 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6730 818c7501 2019-07-11 stsp
6731 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6732 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6733 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6734 818c7501 2019-07-11 stsp
6735 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6736 818c7501 2019-07-11 stsp if (err)
6737 818c7501 2019-07-11 stsp return err;
6738 818c7501 2019-07-11 stsp
6739 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6740 818c7501 2019-07-11 stsp if (err)
6741 818c7501 2019-07-11 stsp goto done;
6742 818c7501 2019-07-11 stsp
6743 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6744 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6745 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6746 818c7501 2019-07-11 stsp &ok_arg);
6747 818c7501 2019-07-11 stsp if (err)
6748 818c7501 2019-07-11 stsp goto done;
6749 818c7501 2019-07-11 stsp
6750 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6751 818c7501 2019-07-11 stsp if (err)
6752 818c7501 2019-07-11 stsp goto done;
6753 818c7501 2019-07-11 stsp
6754 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6755 818c7501 2019-07-11 stsp if (err)
6756 818c7501 2019-07-11 stsp goto done;
6757 818c7501 2019-07-11 stsp
6758 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6759 818c7501 2019-07-11 stsp if (err)
6760 818c7501 2019-07-11 stsp goto done;
6761 818c7501 2019-07-11 stsp
6762 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6763 818c7501 2019-07-11 stsp 0);
6764 e51d7b55 2020-01-04 stsp if (err)
6765 e51d7b55 2020-01-04 stsp goto done;
6766 e51d7b55 2020-01-04 stsp
6767 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6768 818c7501 2019-07-11 stsp if (err)
6769 818c7501 2019-07-11 stsp goto done;
6770 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6771 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6772 e51d7b55 2020-01-04 stsp goto done;
6773 e51d7b55 2020-01-04 stsp }
6774 818c7501 2019-07-11 stsp
6775 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6776 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6777 818c7501 2019-07-11 stsp if (err)
6778 818c7501 2019-07-11 stsp goto done;
6779 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6780 818c7501 2019-07-11 stsp if (err)
6781 818c7501 2019-07-11 stsp goto done;
6782 818c7501 2019-07-11 stsp
6783 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6784 818c7501 2019-07-11 stsp
6785 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6786 818c7501 2019-07-11 stsp if (err)
6787 818c7501 2019-07-11 stsp goto done;
6788 818c7501 2019-07-11 stsp
6789 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6790 818c7501 2019-07-11 stsp if (err)
6791 818c7501 2019-07-11 stsp goto done;
6792 818c7501 2019-07-11 stsp
6793 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6794 818c7501 2019-07-11 stsp worktree->base_commit_id);
6795 818c7501 2019-07-11 stsp if (err)
6796 818c7501 2019-07-11 stsp goto done;
6797 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6798 818c7501 2019-07-11 stsp if (err)
6799 818c7501 2019-07-11 stsp goto done;
6800 818c7501 2019-07-11 stsp
6801 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6802 818c7501 2019-07-11 stsp if (err)
6803 818c7501 2019-07-11 stsp goto done;
6804 818c7501 2019-07-11 stsp done:
6805 818c7501 2019-07-11 stsp free(fileindex_path);
6806 818c7501 2019-07-11 stsp free(tmp_branch_name);
6807 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6808 818c7501 2019-07-11 stsp free(branch_ref_name);
6809 818c7501 2019-07-11 stsp if (branch_ref)
6810 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6811 818c7501 2019-07-11 stsp if (wt_branch)
6812 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6813 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6814 818c7501 2019-07-11 stsp if (err) {
6815 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6816 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6817 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6818 818c7501 2019-07-11 stsp }
6819 818c7501 2019-07-11 stsp if (*tmp_branch) {
6820 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6821 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6822 818c7501 2019-07-11 stsp }
6823 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6824 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6825 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6826 3e3a69f1 2019-07-25 stsp }
6827 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6828 818c7501 2019-07-11 stsp }
6829 818c7501 2019-07-11 stsp return err;
6830 818c7501 2019-07-11 stsp }
6831 818c7501 2019-07-11 stsp
6832 818c7501 2019-07-11 stsp const struct got_error *
6833 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6834 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6835 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6836 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6837 818c7501 2019-07-11 stsp {
6838 818c7501 2019-07-11 stsp const struct got_error *err;
6839 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6840 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6841 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6842 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6843 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6844 818c7501 2019-07-11 stsp
6845 818c7501 2019-07-11 stsp *commit_id = NULL;
6846 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6847 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6848 3e3a69f1 2019-07-25 stsp *branch = NULL;
6849 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6850 3e3a69f1 2019-07-25 stsp
6851 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6852 3e3a69f1 2019-07-25 stsp if (err)
6853 3e3a69f1 2019-07-25 stsp return err;
6854 818c7501 2019-07-11 stsp
6855 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6856 3e3a69f1 2019-07-25 stsp if (err)
6857 f032f1f7 2019-08-04 stsp goto done;
6858 f032f1f7 2019-08-04 stsp
6859 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6860 f032f1f7 2019-08-04 stsp &have_staged_files);
6861 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6862 f032f1f7 2019-08-04 stsp goto done;
6863 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6864 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6865 3e3a69f1 2019-07-25 stsp goto done;
6866 f032f1f7 2019-08-04 stsp }
6867 3e3a69f1 2019-07-25 stsp
6868 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6869 818c7501 2019-07-11 stsp if (err)
6870 f032f1f7 2019-08-04 stsp goto done;
6871 818c7501 2019-07-11 stsp
6872 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6873 818c7501 2019-07-11 stsp if (err)
6874 818c7501 2019-07-11 stsp goto done;
6875 818c7501 2019-07-11 stsp
6876 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6877 818c7501 2019-07-11 stsp if (err)
6878 818c7501 2019-07-11 stsp goto done;
6879 818c7501 2019-07-11 stsp
6880 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6881 818c7501 2019-07-11 stsp if (err)
6882 818c7501 2019-07-11 stsp goto done;
6883 818c7501 2019-07-11 stsp
6884 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6885 818c7501 2019-07-11 stsp if (err)
6886 818c7501 2019-07-11 stsp goto done;
6887 818c7501 2019-07-11 stsp
6888 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6889 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6890 818c7501 2019-07-11 stsp if (err)
6891 818c7501 2019-07-11 stsp goto done;
6892 818c7501 2019-07-11 stsp
6893 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6894 818c7501 2019-07-11 stsp if (err)
6895 818c7501 2019-07-11 stsp goto done;
6896 818c7501 2019-07-11 stsp
6897 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6898 818c7501 2019-07-11 stsp if (err)
6899 818c7501 2019-07-11 stsp goto done;
6900 818c7501 2019-07-11 stsp
6901 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6902 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6903 818c7501 2019-07-11 stsp if (err)
6904 818c7501 2019-07-11 stsp goto done;
6905 818c7501 2019-07-11 stsp
6906 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6907 818c7501 2019-07-11 stsp if (err)
6908 818c7501 2019-07-11 stsp goto done;
6909 818c7501 2019-07-11 stsp done:
6910 818c7501 2019-07-11 stsp free(commit_ref_name);
6911 818c7501 2019-07-11 stsp free(branch_ref_name);
6912 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6913 818c7501 2019-07-11 stsp if (commit_ref)
6914 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6915 818c7501 2019-07-11 stsp if (branch_ref)
6916 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6917 818c7501 2019-07-11 stsp if (err) {
6918 818c7501 2019-07-11 stsp free(*commit_id);
6919 818c7501 2019-07-11 stsp *commit_id = NULL;
6920 818c7501 2019-07-11 stsp if (*tmp_branch) {
6921 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6922 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6923 818c7501 2019-07-11 stsp }
6924 818c7501 2019-07-11 stsp if (*new_base_branch) {
6925 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6926 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6927 818c7501 2019-07-11 stsp }
6928 818c7501 2019-07-11 stsp if (*branch) {
6929 818c7501 2019-07-11 stsp got_ref_close(*branch);
6930 818c7501 2019-07-11 stsp *branch = NULL;
6931 818c7501 2019-07-11 stsp }
6932 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6933 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6934 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6935 3e3a69f1 2019-07-25 stsp }
6936 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6937 818c7501 2019-07-11 stsp }
6938 818c7501 2019-07-11 stsp return err;
6939 c4296144 2019-05-09 stsp }
6940 818c7501 2019-07-11 stsp
6941 818c7501 2019-07-11 stsp const struct got_error *
6942 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6943 818c7501 2019-07-11 stsp {
6944 818c7501 2019-07-11 stsp const struct got_error *err;
6945 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6946 818c7501 2019-07-11 stsp
6947 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6948 818c7501 2019-07-11 stsp if (err)
6949 818c7501 2019-07-11 stsp return err;
6950 818c7501 2019-07-11 stsp
6951 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6952 818c7501 2019-07-11 stsp free(tmp_branch_name);
6953 818c7501 2019-07-11 stsp return NULL;
6954 818c7501 2019-07-11 stsp }
6955 818c7501 2019-07-11 stsp
6956 818c7501 2019-07-11 stsp static const struct got_error *
6957 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6958 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6959 818c7501 2019-07-11 stsp {
6960 0ebf8283 2019-07-24 stsp *logmsg = arg;
6961 818c7501 2019-07-11 stsp return NULL;
6962 818c7501 2019-07-11 stsp }
6963 818c7501 2019-07-11 stsp
6964 818c7501 2019-07-11 stsp static const struct got_error *
6965 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6966 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6967 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6968 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6969 818c7501 2019-07-11 stsp {
6970 818c7501 2019-07-11 stsp return NULL;
6971 01757395 2019-07-12 stsp }
6972 01757395 2019-07-12 stsp
6973 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6974 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6975 01757395 2019-07-12 stsp void *progress_arg;
6976 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6977 01757395 2019-07-12 stsp };
6978 01757395 2019-07-12 stsp
6979 01757395 2019-07-12 stsp static const struct got_error *
6980 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6981 01757395 2019-07-12 stsp {
6982 01757395 2019-07-12 stsp const struct got_error *err;
6983 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6984 01757395 2019-07-12 stsp char *p;
6985 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6986 01757395 2019-07-12 stsp
6987 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6988 01757395 2019-07-12 stsp if (err)
6989 01757395 2019-07-12 stsp return err;
6990 01757395 2019-07-12 stsp
6991 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6992 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6993 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6994 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6995 01757395 2019-07-12 stsp return NULL;
6996 01757395 2019-07-12 stsp
6997 01757395 2019-07-12 stsp p = strdup(path);
6998 01757395 2019-07-12 stsp if (p == NULL)
6999 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7000 01757395 2019-07-12 stsp
7001 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7002 01757395 2019-07-12 stsp if (err || new == NULL)
7003 01757395 2019-07-12 stsp free(p);
7004 01757395 2019-07-12 stsp return err;
7005 818c7501 2019-07-11 stsp }
7006 818c7501 2019-07-11 stsp
7007 0ebf8283 2019-07-24 stsp static const struct got_error *
7008 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7009 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7010 818c7501 2019-07-11 stsp {
7011 818c7501 2019-07-11 stsp const struct got_error *err;
7012 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7013 818c7501 2019-07-11 stsp
7014 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7015 818c7501 2019-07-11 stsp if (err) {
7016 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7017 818c7501 2019-07-11 stsp goto done;
7018 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7019 818c7501 2019-07-11 stsp if (err)
7020 818c7501 2019-07-11 stsp goto done;
7021 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7022 818c7501 2019-07-11 stsp if (err)
7023 818c7501 2019-07-11 stsp goto done;
7024 de05890f 2020-03-05 stsp } else if (is_rebase) {
7025 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7026 818c7501 2019-07-11 stsp int cmp;
7027 818c7501 2019-07-11 stsp
7028 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7029 818c7501 2019-07-11 stsp if (err)
7030 818c7501 2019-07-11 stsp goto done;
7031 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7032 818c7501 2019-07-11 stsp free(stored_id);
7033 818c7501 2019-07-11 stsp if (cmp != 0) {
7034 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7035 818c7501 2019-07-11 stsp goto done;
7036 818c7501 2019-07-11 stsp }
7037 818c7501 2019-07-11 stsp }
7038 0ebf8283 2019-07-24 stsp done:
7039 0ebf8283 2019-07-24 stsp if (commit_ref)
7040 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7041 0ebf8283 2019-07-24 stsp return err;
7042 0ebf8283 2019-07-24 stsp }
7043 0ebf8283 2019-07-24 stsp
7044 0ebf8283 2019-07-24 stsp static const struct got_error *
7045 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7046 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7047 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7048 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7049 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7050 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7051 0ebf8283 2019-07-24 stsp {
7052 0ebf8283 2019-07-24 stsp const struct got_error *err;
7053 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7054 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7055 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7056 818c7501 2019-07-11 stsp
7057 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7058 0ebf8283 2019-07-24 stsp
7059 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7060 0ebf8283 2019-07-24 stsp if (err)
7061 0ebf8283 2019-07-24 stsp return err;
7062 0ebf8283 2019-07-24 stsp
7063 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7064 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7065 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7066 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7067 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7068 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7069 818c7501 2019-07-11 stsp if (commit_ref)
7070 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7071 818c7501 2019-07-11 stsp return err;
7072 818c7501 2019-07-11 stsp }
7073 818c7501 2019-07-11 stsp
7074 818c7501 2019-07-11 stsp const struct got_error *
7075 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7076 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7077 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7078 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7079 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7080 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7081 818c7501 2019-07-11 stsp {
7082 0ebf8283 2019-07-24 stsp const struct got_error *err;
7083 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7084 0ebf8283 2019-07-24 stsp
7085 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7086 0ebf8283 2019-07-24 stsp if (err)
7087 0ebf8283 2019-07-24 stsp return err;
7088 0ebf8283 2019-07-24 stsp
7089 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7090 0ebf8283 2019-07-24 stsp if (err)
7091 0ebf8283 2019-07-24 stsp goto done;
7092 0ebf8283 2019-07-24 stsp
7093 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7094 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7095 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7096 0ebf8283 2019-07-24 stsp done:
7097 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7098 0ebf8283 2019-07-24 stsp return err;
7099 0ebf8283 2019-07-24 stsp }
7100 0ebf8283 2019-07-24 stsp
7101 0ebf8283 2019-07-24 stsp const struct got_error *
7102 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7103 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7104 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7105 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7106 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7107 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7108 0ebf8283 2019-07-24 stsp {
7109 0ebf8283 2019-07-24 stsp const struct got_error *err;
7110 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7111 0ebf8283 2019-07-24 stsp
7112 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7113 0ebf8283 2019-07-24 stsp if (err)
7114 0ebf8283 2019-07-24 stsp return err;
7115 0ebf8283 2019-07-24 stsp
7116 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7117 0ebf8283 2019-07-24 stsp if (err)
7118 0ebf8283 2019-07-24 stsp goto done;
7119 0ebf8283 2019-07-24 stsp
7120 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7121 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7122 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7123 0ebf8283 2019-07-24 stsp done:
7124 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7125 0ebf8283 2019-07-24 stsp return err;
7126 0ebf8283 2019-07-24 stsp }
7127 0ebf8283 2019-07-24 stsp
7128 0ebf8283 2019-07-24 stsp static const struct got_error *
7129 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7130 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7131 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7132 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
7133 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
7134 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7135 0ebf8283 2019-07-24 stsp {
7136 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7137 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7138 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7139 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7140 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7141 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7142 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7143 818c7501 2019-07-11 stsp
7144 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7145 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7146 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7147 a0e95631 2019-07-12 stsp
7148 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7149 818c7501 2019-07-11 stsp
7150 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7151 a0e95631 2019-07-12 stsp if (err)
7152 3e3a69f1 2019-07-25 stsp return err;
7153 a0e95631 2019-07-12 stsp
7154 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7155 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7156 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7157 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7158 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
7159 01757395 2019-07-12 stsp /*
7160 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7161 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7162 6c13b005 2021-09-02 stsp *
7163 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7164 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7165 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7166 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7167 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7168 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7169 01757395 2019-07-12 stsp */
7170 01757395 2019-07-12 stsp if (merged_paths) {
7171 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7172 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7173 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7174 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7175 f2a9dc41 2019-12-13 tracey 0);
7176 01757395 2019-07-12 stsp if (err)
7177 01757395 2019-07-12 stsp goto done;
7178 01757395 2019-07-12 stsp }
7179 01757395 2019-07-12 stsp } else {
7180 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7181 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7182 01757395 2019-07-12 stsp if (err)
7183 01757395 2019-07-12 stsp goto done;
7184 01757395 2019-07-12 stsp }
7185 a0e95631 2019-07-12 stsp
7186 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7187 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7188 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7189 a0e95631 2019-07-12 stsp if (err)
7190 a0e95631 2019-07-12 stsp goto done;
7191 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7192 a0e95631 2019-07-12 stsp goto done;
7193 ff0d2220 2019-07-11 stsp }
7194 818c7501 2019-07-11 stsp
7195 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7196 edd02c5e 2019-07-11 stsp if (err)
7197 edd02c5e 2019-07-11 stsp goto done;
7198 edd02c5e 2019-07-11 stsp
7199 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7200 818c7501 2019-07-11 stsp if (err)
7201 818c7501 2019-07-11 stsp goto done;
7202 818c7501 2019-07-11 stsp
7203 5943eee2 2019-08-13 stsp if (new_logmsg) {
7204 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7205 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7206 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7207 5943eee2 2019-08-13 stsp goto done;
7208 5943eee2 2019-08-13 stsp }
7209 5943eee2 2019-08-13 stsp } else {
7210 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7211 5943eee2 2019-08-13 stsp if (err)
7212 5943eee2 2019-08-13 stsp goto done;
7213 5943eee2 2019-08-13 stsp }
7214 0ebf8283 2019-07-24 stsp
7215 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7216 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7217 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
7218 50e7a649 2022-07-22 stsp committer ? committer :
7219 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
7220 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7221 a0e95631 2019-07-12 stsp if (err)
7222 a0e95631 2019-07-12 stsp goto done;
7223 a0e95631 2019-07-12 stsp
7224 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7225 a0e95631 2019-07-12 stsp if (err)
7226 a0e95631 2019-07-12 stsp goto done;
7227 a0e95631 2019-07-12 stsp
7228 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7229 a0e95631 2019-07-12 stsp if (err)
7230 a0e95631 2019-07-12 stsp goto done;
7231 a0e95631 2019-07-12 stsp
7232 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7233 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7234 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7235 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7236 a0e95631 2019-07-12 stsp err = sync_err;
7237 818c7501 2019-07-11 stsp done:
7238 a0e95631 2019-07-12 stsp free(fileindex_path);
7239 a0e95631 2019-07-12 stsp free(head_commit_id);
7240 a0e95631 2019-07-12 stsp if (head_ref)
7241 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7242 818c7501 2019-07-11 stsp if (err) {
7243 818c7501 2019-07-11 stsp free(*new_commit_id);
7244 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7245 818c7501 2019-07-11 stsp }
7246 818c7501 2019-07-11 stsp return err;
7247 818c7501 2019-07-11 stsp }
7248 818c7501 2019-07-11 stsp
7249 818c7501 2019-07-11 stsp const struct got_error *
7250 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7251 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7252 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7253 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7254 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
7255 12383673 2023-02-18 mark struct got_repository *repo)
7256 0ebf8283 2019-07-24 stsp {
7257 0ebf8283 2019-07-24 stsp const struct got_error *err;
7258 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7259 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7260 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7261 0ebf8283 2019-07-24 stsp
7262 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7263 0ebf8283 2019-07-24 stsp if (err)
7264 0ebf8283 2019-07-24 stsp return err;
7265 0ebf8283 2019-07-24 stsp
7266 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7267 0ebf8283 2019-07-24 stsp if (err)
7268 0ebf8283 2019-07-24 stsp goto done;
7269 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7270 0ebf8283 2019-07-24 stsp if (err)
7271 0ebf8283 2019-07-24 stsp goto done;
7272 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7273 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7274 0ebf8283 2019-07-24 stsp goto done;
7275 0ebf8283 2019-07-24 stsp }
7276 0ebf8283 2019-07-24 stsp
7277 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7278 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7279 12383673 2023-02-18 mark NULL, allow_conflict, repo);
7280 0ebf8283 2019-07-24 stsp done:
7281 0ebf8283 2019-07-24 stsp if (commit_ref)
7282 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7283 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7284 0ebf8283 2019-07-24 stsp free(commit_id);
7285 0ebf8283 2019-07-24 stsp return err;
7286 0ebf8283 2019-07-24 stsp }
7287 0ebf8283 2019-07-24 stsp
7288 0ebf8283 2019-07-24 stsp const struct got_error *
7289 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7290 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7291 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7292 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7293 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7294 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7295 0ebf8283 2019-07-24 stsp {
7296 0ebf8283 2019-07-24 stsp const struct got_error *err;
7297 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7298 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7299 0ebf8283 2019-07-24 stsp
7300 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7301 0ebf8283 2019-07-24 stsp if (err)
7302 0ebf8283 2019-07-24 stsp return err;
7303 0ebf8283 2019-07-24 stsp
7304 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7305 0ebf8283 2019-07-24 stsp if (err)
7306 0ebf8283 2019-07-24 stsp goto done;
7307 0ebf8283 2019-07-24 stsp
7308 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7309 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7310 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
7311 0ebf8283 2019-07-24 stsp done:
7312 0ebf8283 2019-07-24 stsp if (commit_ref)
7313 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7314 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7315 0ebf8283 2019-07-24 stsp return err;
7316 0ebf8283 2019-07-24 stsp }
7317 0ebf8283 2019-07-24 stsp
7318 0ebf8283 2019-07-24 stsp const struct got_error *
7319 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7320 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7321 818c7501 2019-07-11 stsp {
7322 3e3a69f1 2019-07-25 stsp if (fileindex)
7323 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7324 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7325 69844fba 2019-07-11 stsp }
7326 69844fba 2019-07-11 stsp
7327 69844fba 2019-07-11 stsp static const struct got_error *
7328 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7329 69844fba 2019-07-11 stsp {
7330 69844fba 2019-07-11 stsp const struct got_error *err;
7331 69844fba 2019-07-11 stsp struct got_reference *ref;
7332 69844fba 2019-07-11 stsp
7333 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7334 69844fba 2019-07-11 stsp if (err) {
7335 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7336 69844fba 2019-07-11 stsp return NULL;
7337 69844fba 2019-07-11 stsp return err;
7338 69844fba 2019-07-11 stsp }
7339 69844fba 2019-07-11 stsp
7340 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7341 69844fba 2019-07-11 stsp got_ref_close(ref);
7342 69844fba 2019-07-11 stsp return err;
7343 818c7501 2019-07-11 stsp }
7344 818c7501 2019-07-11 stsp
7345 69844fba 2019-07-11 stsp static const struct got_error *
7346 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7347 69844fba 2019-07-11 stsp {
7348 69844fba 2019-07-11 stsp const struct got_error *err;
7349 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7350 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7351 69844fba 2019-07-11 stsp
7352 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7353 69844fba 2019-07-11 stsp if (err)
7354 69844fba 2019-07-11 stsp goto done;
7355 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7356 69844fba 2019-07-11 stsp if (err)
7357 69844fba 2019-07-11 stsp goto done;
7358 69844fba 2019-07-11 stsp
7359 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7360 69844fba 2019-07-11 stsp if (err)
7361 69844fba 2019-07-11 stsp goto done;
7362 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7363 69844fba 2019-07-11 stsp if (err)
7364 69844fba 2019-07-11 stsp goto done;
7365 69844fba 2019-07-11 stsp
7366 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7367 69844fba 2019-07-11 stsp if (err)
7368 69844fba 2019-07-11 stsp goto done;
7369 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7370 69844fba 2019-07-11 stsp if (err)
7371 69844fba 2019-07-11 stsp goto done;
7372 69844fba 2019-07-11 stsp
7373 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7374 69844fba 2019-07-11 stsp if (err)
7375 69844fba 2019-07-11 stsp goto done;
7376 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7377 69844fba 2019-07-11 stsp if (err)
7378 69844fba 2019-07-11 stsp goto done;
7379 69844fba 2019-07-11 stsp
7380 69844fba 2019-07-11 stsp done:
7381 69844fba 2019-07-11 stsp free(tmp_branch_name);
7382 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7383 69844fba 2019-07-11 stsp free(branch_ref_name);
7384 69844fba 2019-07-11 stsp free(commit_ref_name);
7385 e600f124 2021-03-21 stsp return err;
7386 e600f124 2021-03-21 stsp }
7387 e600f124 2021-03-21 stsp
7388 336075a4 2022-06-25 op static const struct got_error *
7389 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7390 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7391 e600f124 2021-03-21 stsp {
7392 e600f124 2021-03-21 stsp const struct got_error *err;
7393 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7394 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7395 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7396 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7397 e600f124 2021-03-21 stsp char *refname = NULL;
7398 e600f124 2021-03-21 stsp
7399 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7400 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7401 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7402 e600f124 2021-03-21 stsp branch_name += 11;
7403 e600f124 2021-03-21 stsp
7404 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7405 e600f124 2021-03-21 stsp if (err)
7406 e600f124 2021-03-21 stsp return err;
7407 e600f124 2021-03-21 stsp
7408 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7409 e600f124 2021-03-21 stsp new_id_str) == -1) {
7410 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7411 e600f124 2021-03-21 stsp goto done;
7412 e600f124 2021-03-21 stsp }
7413 e600f124 2021-03-21 stsp
7414 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7415 e600f124 2021-03-21 stsp if (err)
7416 e600f124 2021-03-21 stsp goto done;
7417 e600f124 2021-03-21 stsp
7418 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7419 e600f124 2021-03-21 stsp if (err)
7420 e600f124 2021-03-21 stsp goto done;
7421 e600f124 2021-03-21 stsp
7422 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7423 e600f124 2021-03-21 stsp done:
7424 e600f124 2021-03-21 stsp free(new_id_str);
7425 e600f124 2021-03-21 stsp free(refname);
7426 e600f124 2021-03-21 stsp free(old_commit_id);
7427 e600f124 2021-03-21 stsp if (ref)
7428 e600f124 2021-03-21 stsp got_ref_close(ref);
7429 69844fba 2019-07-11 stsp return err;
7430 69844fba 2019-07-11 stsp }
7431 69844fba 2019-07-11 stsp
7432 818c7501 2019-07-11 stsp const struct got_error *
7433 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7434 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7435 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7436 ef85a376 2023-02-01 mark int create_backup)
7437 818c7501 2019-07-11 stsp {
7438 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7439 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7440 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7441 818c7501 2019-07-11 stsp
7442 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7443 818c7501 2019-07-11 stsp if (err)
7444 818c7501 2019-07-11 stsp return err;
7445 e600f124 2021-03-21 stsp
7446 e600f124 2021-03-21 stsp if (create_backup) {
7447 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7448 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7449 e600f124 2021-03-21 stsp if (err)
7450 e600f124 2021-03-21 stsp goto done;
7451 e600f124 2021-03-21 stsp }
7452 818c7501 2019-07-11 stsp
7453 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7454 818c7501 2019-07-11 stsp if (err)
7455 818c7501 2019-07-11 stsp goto done;
7456 818c7501 2019-07-11 stsp
7457 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7458 818c7501 2019-07-11 stsp if (err)
7459 818c7501 2019-07-11 stsp goto done;
7460 818c7501 2019-07-11 stsp
7461 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7462 818c7501 2019-07-11 stsp if (err)
7463 818c7501 2019-07-11 stsp goto done;
7464 818c7501 2019-07-11 stsp
7465 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7466 a615e0e7 2020-12-16 stsp if (err)
7467 a615e0e7 2020-12-16 stsp goto done;
7468 a615e0e7 2020-12-16 stsp
7469 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7470 a615e0e7 2020-12-16 stsp if (err)
7471 a615e0e7 2020-12-16 stsp goto done;
7472 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7473 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7474 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7475 a615e0e7 2020-12-16 stsp err = sync_err;
7476 818c7501 2019-07-11 stsp done:
7477 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7478 a615e0e7 2020-12-16 stsp free(fileindex_path);
7479 818c7501 2019-07-11 stsp free(new_head_commit_id);
7480 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7481 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7482 818c7501 2019-07-11 stsp err = unlockerr;
7483 818c7501 2019-07-11 stsp return err;
7484 818c7501 2019-07-11 stsp }
7485 af179be7 2023-04-14 stsp
7486 af179be7 2023-04-14 stsp static const struct got_error *
7487 af179be7 2023-04-14 stsp get_paths_changed_between_commits(struct got_pathlist_head *paths,
7488 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7489 af179be7 2023-04-14 stsp struct got_repository *repo)
7490 af179be7 2023-04-14 stsp {
7491 af179be7 2023-04-14 stsp const struct got_error *err;
7492 af179be7 2023-04-14 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7493 af179be7 2023-04-14 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7494 af179be7 2023-04-14 stsp
7495 af179be7 2023-04-14 stsp if (id1) {
7496 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit1, repo, id1);
7497 af179be7 2023-04-14 stsp if (err)
7498 af179be7 2023-04-14 stsp goto done;
7499 af179be7 2023-04-14 stsp
7500 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree1, repo,
7501 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit1));
7502 af179be7 2023-04-14 stsp if (err)
7503 af179be7 2023-04-14 stsp goto done;
7504 af179be7 2023-04-14 stsp }
7505 818c7501 2019-07-11 stsp
7506 af179be7 2023-04-14 stsp if (id2) {
7507 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit2, repo, id2);
7508 af179be7 2023-04-14 stsp if (err)
7509 af179be7 2023-04-14 stsp goto done;
7510 af179be7 2023-04-14 stsp
7511 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree2, repo,
7512 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit2));
7513 af179be7 2023-04-14 stsp if (err)
7514 af179be7 2023-04-14 stsp goto done;
7515 af179be7 2023-04-14 stsp }
7516 af179be7 2023-04-14 stsp
7517 af179be7 2023-04-14 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7518 af179be7 2023-04-14 stsp got_diff_tree_collect_changed_paths, paths, 0);
7519 af179be7 2023-04-14 stsp if (err)
7520 af179be7 2023-04-14 stsp goto done;
7521 af179be7 2023-04-14 stsp done:
7522 af179be7 2023-04-14 stsp if (commit1)
7523 af179be7 2023-04-14 stsp got_object_commit_close(commit1);
7524 af179be7 2023-04-14 stsp if (commit2)
7525 af179be7 2023-04-14 stsp got_object_commit_close(commit2);
7526 af179be7 2023-04-14 stsp if (tree1)
7527 af179be7 2023-04-14 stsp got_object_tree_close(tree1);
7528 af179be7 2023-04-14 stsp if (tree2)
7529 af179be7 2023-04-14 stsp got_object_tree_close(tree2);
7530 af179be7 2023-04-14 stsp return err;
7531 af179be7 2023-04-14 stsp }
7532 af179be7 2023-04-14 stsp
7533 af179be7 2023-04-14 stsp static const struct got_error *
7534 af179be7 2023-04-14 stsp get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7535 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7536 af179be7 2023-04-14 stsp const char *path_prefix, struct got_repository *repo)
7537 af179be7 2023-04-14 stsp {
7538 af179be7 2023-04-14 stsp const struct got_error *err;
7539 af179be7 2023-04-14 stsp struct got_pathlist_head merged_paths;
7540 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
7541 af179be7 2023-04-14 stsp char *abspath = NULL, *wt_path = NULL;
7542 af179be7 2023-04-14 stsp
7543 af179be7 2023-04-14 stsp TAILQ_INIT(&merged_paths);
7544 af179be7 2023-04-14 stsp
7545 af179be7 2023-04-14 stsp err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7546 af179be7 2023-04-14 stsp if (err)
7547 af179be7 2023-04-14 stsp goto done;
7548 af179be7 2023-04-14 stsp
7549 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, &merged_paths, entry) {
7550 af179be7 2023-04-14 stsp struct got_diff_changed_path *change = pe->data;
7551 af179be7 2023-04-14 stsp
7552 af179be7 2023-04-14 stsp if (change->status != GOT_STATUS_ADD)
7553 af179be7 2023-04-14 stsp continue;
7554 af179be7 2023-04-14 stsp
7555 af179be7 2023-04-14 stsp if (got_path_is_root_dir(path_prefix)) {
7556 af179be7 2023-04-14 stsp wt_path = strdup(pe->path);
7557 af179be7 2023-04-14 stsp if (wt_path == NULL) {
7558 af179be7 2023-04-14 stsp err = got_error_from_errno("strdup");
7559 af179be7 2023-04-14 stsp goto done;
7560 af179be7 2023-04-14 stsp }
7561 af179be7 2023-04-14 stsp } else {
7562 af179be7 2023-04-14 stsp if (asprintf(&abspath, "/%s", pe->path) == -1) {
7563 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
7564 af179be7 2023-04-14 stsp goto done;
7565 af179be7 2023-04-14 stsp }
7566 af179be7 2023-04-14 stsp
7567 af179be7 2023-04-14 stsp err = got_path_skip_common_ancestor(&wt_path,
7568 af179be7 2023-04-14 stsp path_prefix, abspath);
7569 af179be7 2023-04-14 stsp if (err)
7570 af179be7 2023-04-14 stsp goto done;
7571 af179be7 2023-04-14 stsp free(abspath);
7572 af179be7 2023-04-14 stsp abspath = NULL;
7573 af179be7 2023-04-14 stsp }
7574 af179be7 2023-04-14 stsp
7575 af179be7 2023-04-14 stsp err = got_pathlist_append(added_paths, wt_path, NULL);
7576 af179be7 2023-04-14 stsp if (err)
7577 af179be7 2023-04-14 stsp goto done;
7578 af179be7 2023-04-14 stsp wt_path = NULL;
7579 af179be7 2023-04-14 stsp }
7580 af179be7 2023-04-14 stsp
7581 af179be7 2023-04-14 stsp done:
7582 af179be7 2023-04-14 stsp got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7583 af179be7 2023-04-14 stsp free(abspath);
7584 af179be7 2023-04-14 stsp free(wt_path);
7585 af179be7 2023-04-14 stsp return err;
7586 af179be7 2023-04-14 stsp }
7587 af179be7 2023-04-14 stsp
7588 af179be7 2023-04-14 stsp static const struct got_error *
7589 af179be7 2023-04-14 stsp get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7590 af179be7 2023-04-14 stsp struct got_object_id *id, const char *path_prefix,
7591 af179be7 2023-04-14 stsp struct got_repository *repo)
7592 af179be7 2023-04-14 stsp {
7593 af179be7 2023-04-14 stsp const struct got_error *err;
7594 af179be7 2023-04-14 stsp struct got_commit_object *commit = NULL;
7595 af179be7 2023-04-14 stsp struct got_object_qid *pid;
7596 af179be7 2023-04-14 stsp
7597 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit, repo, id);
7598 af179be7 2023-04-14 stsp if (err)
7599 af179be7 2023-04-14 stsp goto done;
7600 af179be7 2023-04-14 stsp
7601 af179be7 2023-04-14 stsp pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7602 af179be7 2023-04-14 stsp
7603 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(added_paths,
7604 af179be7 2023-04-14 stsp pid ? &pid->id : NULL, id, path_prefix, repo);
7605 af179be7 2023-04-14 stsp if (err)
7606 af179be7 2023-04-14 stsp goto done;
7607 af179be7 2023-04-14 stsp done:
7608 af179be7 2023-04-14 stsp if (commit)
7609 af179be7 2023-04-14 stsp got_object_commit_close(commit);
7610 af179be7 2023-04-14 stsp return err;
7611 af179be7 2023-04-14 stsp }
7612 af179be7 2023-04-14 stsp
7613 818c7501 2019-07-11 stsp const struct got_error *
7614 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7615 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7616 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7617 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7618 818c7501 2019-07-11 stsp {
7619 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7620 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7621 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7622 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
7623 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7624 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7625 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
7626 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
7627 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7628 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7629 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
7630 818c7501 2019-07-11 stsp
7631 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
7632 af179be7 2023-04-14 stsp
7633 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7634 818c7501 2019-07-11 stsp if (err)
7635 818c7501 2019-07-11 stsp return err;
7636 af179be7 2023-04-14 stsp
7637 af179be7 2023-04-14 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7638 af179be7 2023-04-14 stsp if (err)
7639 af179be7 2023-04-14 stsp goto done;
7640 af179be7 2023-04-14 stsp
7641 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7642 af179be7 2023-04-14 stsp if (err)
7643 af179be7 2023-04-14 stsp goto done;
7644 af179be7 2023-04-14 stsp
7645 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7646 af179be7 2023-04-14 stsp if (err)
7647 af179be7 2023-04-14 stsp goto done;
7648 a44927cc 2022-04-07 stsp
7649 af179be7 2023-04-14 stsp /*
7650 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
7651 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
7652 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
7653 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
7654 af179be7 2023-04-14 stsp */
7655 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7656 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
7657 af179be7 2023-04-14 stsp if (err)
7658 af179be7 2023-04-14 stsp goto done;
7659 af179be7 2023-04-14 stsp
7660 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7661 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7662 818c7501 2019-07-11 stsp if (err)
7663 818c7501 2019-07-11 stsp goto done;
7664 818c7501 2019-07-11 stsp
7665 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7666 818c7501 2019-07-11 stsp if (err)
7667 818c7501 2019-07-11 stsp goto done;
7668 818c7501 2019-07-11 stsp
7669 818c7501 2019-07-11 stsp /*
7670 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7671 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7672 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7673 818c7501 2019-07-11 stsp */
7674 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7675 818c7501 2019-07-11 stsp if (err)
7676 818c7501 2019-07-11 stsp goto done;
7677 818c7501 2019-07-11 stsp
7678 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7679 1b093d84 2023-04-12 stsp if (err)
7680 1b093d84 2023-04-12 stsp goto done;
7681 1b093d84 2023-04-12 stsp
7682 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
7683 1b093d84 2023-04-12 stsp worktree->base_commit_id);
7684 818c7501 2019-07-11 stsp if (err)
7685 818c7501 2019-07-11 stsp goto done;
7686 818c7501 2019-07-11 stsp
7687 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7688 a44927cc 2022-04-07 stsp worktree->path_prefix);
7689 ca355955 2019-07-12 stsp if (err)
7690 ca355955 2019-07-12 stsp goto done;
7691 ca355955 2019-07-12 stsp
7692 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7693 ca355955 2019-07-12 stsp if (err)
7694 ca355955 2019-07-12 stsp goto done;
7695 ca355955 2019-07-12 stsp
7696 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7697 818c7501 2019-07-11 stsp if (err)
7698 818c7501 2019-07-11 stsp goto done;
7699 818c7501 2019-07-11 stsp
7700 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7701 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7702 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7703 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7704 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7705 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7706 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7707 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
7708 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
7709 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7710 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7711 55bd499d 2019-07-12 stsp if (err)
7712 1f1abb7e 2019-08-08 stsp goto sync;
7713 55bd499d 2019-07-12 stsp
7714 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7715 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7716 ca355955 2019-07-12 stsp sync:
7717 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7718 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7719 ca355955 2019-07-12 stsp err = sync_err;
7720 818c7501 2019-07-11 stsp done:
7721 af179be7 2023-04-14 stsp got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7722 818c7501 2019-07-11 stsp got_ref_close(resolved);
7723 a3a2faf2 2019-07-12 stsp free(tree_id);
7724 818c7501 2019-07-11 stsp free(commit_id);
7725 af179be7 2023-04-14 stsp free(merged_commit_id);
7726 a44927cc 2022-04-07 stsp if (commit)
7727 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7728 0ebf8283 2019-07-24 stsp if (fileindex)
7729 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7730 0ebf8283 2019-07-24 stsp free(fileindex_path);
7731 af179be7 2023-04-14 stsp free(commit_ref_name);
7732 af179be7 2023-04-14 stsp if (commit_ref)
7733 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
7734 0ebf8283 2019-07-24 stsp
7735 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7736 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7737 0ebf8283 2019-07-24 stsp err = unlockerr;
7738 0ebf8283 2019-07-24 stsp return err;
7739 0ebf8283 2019-07-24 stsp }
7740 0ebf8283 2019-07-24 stsp
7741 0ebf8283 2019-07-24 stsp const struct got_error *
7742 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7743 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7744 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7745 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7746 0ebf8283 2019-07-24 stsp {
7747 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7748 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7749 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7750 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7751 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7752 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7753 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7754 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7755 0ebf8283 2019-07-24 stsp
7756 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7757 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7758 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7759 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7760 0ebf8283 2019-07-24 stsp
7761 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7762 0ebf8283 2019-07-24 stsp if (err)
7763 0ebf8283 2019-07-24 stsp return err;
7764 0ebf8283 2019-07-24 stsp
7765 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7766 0ebf8283 2019-07-24 stsp if (err)
7767 0ebf8283 2019-07-24 stsp goto done;
7768 0ebf8283 2019-07-24 stsp
7769 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7770 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7771 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7772 0ebf8283 2019-07-24 stsp &ok_arg);
7773 0ebf8283 2019-07-24 stsp if (err)
7774 0ebf8283 2019-07-24 stsp goto done;
7775 0ebf8283 2019-07-24 stsp
7776 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7777 0ebf8283 2019-07-24 stsp if (err)
7778 0ebf8283 2019-07-24 stsp goto done;
7779 0ebf8283 2019-07-24 stsp
7780 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7781 0ebf8283 2019-07-24 stsp if (err)
7782 0ebf8283 2019-07-24 stsp goto done;
7783 0ebf8283 2019-07-24 stsp
7784 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7785 0ebf8283 2019-07-24 stsp worktree);
7786 0ebf8283 2019-07-24 stsp if (err)
7787 0ebf8283 2019-07-24 stsp goto done;
7788 0ebf8283 2019-07-24 stsp
7789 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7790 0ebf8283 2019-07-24 stsp 0);
7791 0ebf8283 2019-07-24 stsp if (err)
7792 0ebf8283 2019-07-24 stsp goto done;
7793 0ebf8283 2019-07-24 stsp
7794 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7795 0ebf8283 2019-07-24 stsp if (err)
7796 0ebf8283 2019-07-24 stsp goto done;
7797 0ebf8283 2019-07-24 stsp
7798 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7799 0ebf8283 2019-07-24 stsp if (err)
7800 0ebf8283 2019-07-24 stsp goto done;
7801 0ebf8283 2019-07-24 stsp
7802 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7803 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7804 0ebf8283 2019-07-24 stsp if (err)
7805 0ebf8283 2019-07-24 stsp goto done;
7806 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7807 0ebf8283 2019-07-24 stsp if (err)
7808 0ebf8283 2019-07-24 stsp goto done;
7809 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7810 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7811 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7812 0ebf8283 2019-07-24 stsp goto done;
7813 0ebf8283 2019-07-24 stsp }
7814 0ebf8283 2019-07-24 stsp
7815 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7816 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7817 0ebf8283 2019-07-24 stsp if (err)
7818 0ebf8283 2019-07-24 stsp goto done;
7819 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7820 0ebf8283 2019-07-24 stsp if (err)
7821 0ebf8283 2019-07-24 stsp goto done;
7822 0ebf8283 2019-07-24 stsp
7823 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7824 0ebf8283 2019-07-24 stsp if (err)
7825 0ebf8283 2019-07-24 stsp goto done;
7826 0ebf8283 2019-07-24 stsp done:
7827 0ebf8283 2019-07-24 stsp free(fileindex_path);
7828 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7829 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7830 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7831 0ebf8283 2019-07-24 stsp if (wt_branch)
7832 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7833 0ebf8283 2019-07-24 stsp if (err) {
7834 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7835 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7836 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7837 0ebf8283 2019-07-24 stsp }
7838 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7839 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7840 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7841 0ebf8283 2019-07-24 stsp }
7842 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7843 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7844 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7845 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7846 3e3a69f1 2019-07-25 stsp }
7847 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7848 0ebf8283 2019-07-24 stsp }
7849 0ebf8283 2019-07-24 stsp return err;
7850 0ebf8283 2019-07-24 stsp }
7851 0ebf8283 2019-07-24 stsp
7852 0ebf8283 2019-07-24 stsp const struct got_error *
7853 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7854 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7855 0ebf8283 2019-07-24 stsp {
7856 3e3a69f1 2019-07-25 stsp if (fileindex)
7857 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7858 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7859 0ebf8283 2019-07-24 stsp }
7860 0ebf8283 2019-07-24 stsp
7861 0ebf8283 2019-07-24 stsp const struct got_error *
7862 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7863 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7864 0ebf8283 2019-07-24 stsp {
7865 0ebf8283 2019-07-24 stsp const struct got_error *err;
7866 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7867 0ebf8283 2019-07-24 stsp
7868 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7869 0ebf8283 2019-07-24 stsp if (err)
7870 0ebf8283 2019-07-24 stsp return err;
7871 0ebf8283 2019-07-24 stsp
7872 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7873 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7874 0ebf8283 2019-07-24 stsp return NULL;
7875 0ebf8283 2019-07-24 stsp }
7876 0ebf8283 2019-07-24 stsp
7877 0ebf8283 2019-07-24 stsp const struct got_error *
7878 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7879 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7880 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7881 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7882 0ebf8283 2019-07-24 stsp {
7883 0ebf8283 2019-07-24 stsp const struct got_error *err;
7884 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7885 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7886 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7887 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7888 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7889 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7890 0ebf8283 2019-07-24 stsp
7891 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7892 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7893 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7894 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7895 0ebf8283 2019-07-24 stsp
7896 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7897 3e3a69f1 2019-07-25 stsp if (err)
7898 3e3a69f1 2019-07-25 stsp return err;
7899 3e3a69f1 2019-07-25 stsp
7900 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7901 3e3a69f1 2019-07-25 stsp if (err)
7902 f032f1f7 2019-08-04 stsp goto done;
7903 f032f1f7 2019-08-04 stsp
7904 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7905 f032f1f7 2019-08-04 stsp &have_staged_files);
7906 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7907 f032f1f7 2019-08-04 stsp goto done;
7908 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7909 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7910 3e3a69f1 2019-07-25 stsp goto done;
7911 f032f1f7 2019-08-04 stsp }
7912 3e3a69f1 2019-07-25 stsp
7913 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7914 0ebf8283 2019-07-24 stsp if (err)
7915 f032f1f7 2019-08-04 stsp goto done;
7916 0ebf8283 2019-07-24 stsp
7917 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
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 = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7922 0ebf8283 2019-07-24 stsp if (err)
7923 0ebf8283 2019-07-24 stsp goto done;
7924 0ebf8283 2019-07-24 stsp
7925 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7926 0ebf8283 2019-07-24 stsp worktree);
7927 0ebf8283 2019-07-24 stsp if (err)
7928 0ebf8283 2019-07-24 stsp goto done;
7929 0ebf8283 2019-07-24 stsp
7930 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7931 0ebf8283 2019-07-24 stsp if (err)
7932 0ebf8283 2019-07-24 stsp goto done;
7933 0ebf8283 2019-07-24 stsp
7934 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7935 0ebf8283 2019-07-24 stsp if (err)
7936 0ebf8283 2019-07-24 stsp goto done;
7937 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7938 0ebf8283 2019-07-24 stsp if (err)
7939 0ebf8283 2019-07-24 stsp goto done;
7940 0ebf8283 2019-07-24 stsp
7941 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7942 0ebf8283 2019-07-24 stsp if (err)
7943 0ebf8283 2019-07-24 stsp goto done;
7944 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7945 0ebf8283 2019-07-24 stsp if (err)
7946 0ebf8283 2019-07-24 stsp goto done;
7947 0ebf8283 2019-07-24 stsp
7948 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7949 0ebf8283 2019-07-24 stsp if (err)
7950 0ebf8283 2019-07-24 stsp goto done;
7951 0ebf8283 2019-07-24 stsp done:
7952 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7953 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7954 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7955 0ebf8283 2019-07-24 stsp if (commit_ref)
7956 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7957 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7958 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7959 0ebf8283 2019-07-24 stsp if (err) {
7960 0ebf8283 2019-07-24 stsp free(*commit_id);
7961 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7962 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7963 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7964 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7965 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7966 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7967 0ebf8283 2019-07-24 stsp }
7968 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7969 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7970 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7971 3e3a69f1 2019-07-25 stsp }
7972 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7973 0ebf8283 2019-07-24 stsp }
7974 0ebf8283 2019-07-24 stsp return err;
7975 0ebf8283 2019-07-24 stsp }
7976 0ebf8283 2019-07-24 stsp
7977 0ebf8283 2019-07-24 stsp static const struct got_error *
7978 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7979 0ebf8283 2019-07-24 stsp {
7980 0ebf8283 2019-07-24 stsp const struct got_error *err;
7981 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7982 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7983 0ebf8283 2019-07-24 stsp
7984 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7985 0ebf8283 2019-07-24 stsp if (err)
7986 0ebf8283 2019-07-24 stsp goto done;
7987 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7988 0ebf8283 2019-07-24 stsp if (err)
7989 0ebf8283 2019-07-24 stsp goto done;
7990 0ebf8283 2019-07-24 stsp
7991 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7992 0ebf8283 2019-07-24 stsp worktree);
7993 0ebf8283 2019-07-24 stsp if (err)
7994 0ebf8283 2019-07-24 stsp goto done;
7995 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7996 0ebf8283 2019-07-24 stsp if (err)
7997 0ebf8283 2019-07-24 stsp goto done;
7998 0ebf8283 2019-07-24 stsp
7999 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8000 0ebf8283 2019-07-24 stsp if (err)
8001 0ebf8283 2019-07-24 stsp goto done;
8002 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8003 0ebf8283 2019-07-24 stsp if (err)
8004 0ebf8283 2019-07-24 stsp goto done;
8005 0ebf8283 2019-07-24 stsp
8006 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8007 0ebf8283 2019-07-24 stsp if (err)
8008 0ebf8283 2019-07-24 stsp goto done;
8009 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8010 0ebf8283 2019-07-24 stsp if (err)
8011 0ebf8283 2019-07-24 stsp goto done;
8012 0ebf8283 2019-07-24 stsp done:
8013 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8014 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8015 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8016 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8017 0ebf8283 2019-07-24 stsp return err;
8018 0ebf8283 2019-07-24 stsp }
8019 0ebf8283 2019-07-24 stsp
8020 0ebf8283 2019-07-24 stsp const struct got_error *
8021 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8022 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8023 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8024 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8025 0ebf8283 2019-07-24 stsp {
8026 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8027 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8028 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8029 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8030 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8031 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8032 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8033 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8034 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8035 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8036 0ebf8283 2019-07-24 stsp
8037 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8038 af179be7 2023-04-14 stsp
8039 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8040 0ebf8283 2019-07-24 stsp if (err)
8041 0ebf8283 2019-07-24 stsp return err;
8042 a44927cc 2022-04-07 stsp
8043 af179be7 2023-04-14 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8044 af179be7 2023-04-14 stsp if (err)
8045 af179be7 2023-04-14 stsp goto done;
8046 af179be7 2023-04-14 stsp
8047 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8048 af179be7 2023-04-14 stsp if (err) {
8049 af179be7 2023-04-14 stsp if (err->code != GOT_ERR_NOT_REF)
8050 af179be7 2023-04-14 stsp goto done;
8051 af179be7 2023-04-14 stsp /* Can happen on early abort due to invalid histedit script. */
8052 af179be7 2023-04-14 stsp commit_ref = NULL;
8053 af179be7 2023-04-14 stsp }
8054 af179be7 2023-04-14 stsp
8055 af179be7 2023-04-14 stsp if (commit_ref) {
8056 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8057 af179be7 2023-04-14 stsp if (err)
8058 af179be7 2023-04-14 stsp goto done;
8059 af179be7 2023-04-14 stsp
8060 af179be7 2023-04-14 stsp /*
8061 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8062 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8063 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files added by the
8064 af179be7 2023-04-14 stsp * user during conflict resolution or during histedit -e.
8065 af179be7 2023-04-14 stsp */
8066 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8067 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8068 af179be7 2023-04-14 stsp if (err)
8069 af179be7 2023-04-14 stsp goto done;
8070 af179be7 2023-04-14 stsp }
8071 af179be7 2023-04-14 stsp
8072 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8073 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8074 0ebf8283 2019-07-24 stsp if (err)
8075 0ebf8283 2019-07-24 stsp goto done;
8076 0ebf8283 2019-07-24 stsp
8077 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8078 0ebf8283 2019-07-24 stsp if (err)
8079 0ebf8283 2019-07-24 stsp goto done;
8080 0ebf8283 2019-07-24 stsp
8081 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8082 0ebf8283 2019-07-24 stsp if (err)
8083 0ebf8283 2019-07-24 stsp goto done;
8084 0ebf8283 2019-07-24 stsp
8085 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
8086 1b093d84 2023-04-12 stsp worktree->base_commit_id);
8087 1b093d84 2023-04-12 stsp if (err)
8088 1b093d84 2023-04-12 stsp goto done;
8089 1b093d84 2023-04-12 stsp
8090 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8091 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8092 0ebf8283 2019-07-24 stsp if (err)
8093 0ebf8283 2019-07-24 stsp goto done;
8094 0ebf8283 2019-07-24 stsp
8095 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8096 0ebf8283 2019-07-24 stsp if (err)
8097 0ebf8283 2019-07-24 stsp goto done;
8098 0ebf8283 2019-07-24 stsp
8099 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8100 0ebf8283 2019-07-24 stsp if (err)
8101 0ebf8283 2019-07-24 stsp goto done;
8102 0ebf8283 2019-07-24 stsp
8103 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8104 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8105 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8106 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8107 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8108 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8109 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8110 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
8111 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8112 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8113 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8114 0ebf8283 2019-07-24 stsp if (err)
8115 1f1abb7e 2019-08-08 stsp goto sync;
8116 0ebf8283 2019-07-24 stsp
8117 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8118 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8119 0ebf8283 2019-07-24 stsp sync:
8120 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8121 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8122 0ebf8283 2019-07-24 stsp err = sync_err;
8123 0ebf8283 2019-07-24 stsp done:
8124 af179be7 2023-04-14 stsp if (resolved)
8125 af179be7 2023-04-14 stsp got_ref_close(resolved);
8126 af179be7 2023-04-14 stsp if (commit_ref)
8127 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8128 af179be7 2023-04-14 stsp free(merged_commit_id);
8129 0ebf8283 2019-07-24 stsp free(tree_id);
8130 818c7501 2019-07-11 stsp free(fileindex_path);
8131 af179be7 2023-04-14 stsp free(commit_ref_name);
8132 818c7501 2019-07-11 stsp
8133 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8134 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8135 818c7501 2019-07-11 stsp err = unlockerr;
8136 818c7501 2019-07-11 stsp return err;
8137 818c7501 2019-07-11 stsp }
8138 0ebf8283 2019-07-24 stsp
8139 0ebf8283 2019-07-24 stsp const struct got_error *
8140 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8141 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8142 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8143 0ebf8283 2019-07-24 stsp {
8144 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8145 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8146 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8147 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8148 0ebf8283 2019-07-24 stsp
8149 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8150 0ebf8283 2019-07-24 stsp if (err)
8151 0ebf8283 2019-07-24 stsp return err;
8152 0ebf8283 2019-07-24 stsp
8153 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8154 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8155 e600f124 2021-03-21 stsp if (err)
8156 e600f124 2021-03-21 stsp goto done;
8157 e600f124 2021-03-21 stsp
8158 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8159 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8160 0ebf8283 2019-07-24 stsp if (err)
8161 0ebf8283 2019-07-24 stsp goto done;
8162 0ebf8283 2019-07-24 stsp
8163 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8164 0ebf8283 2019-07-24 stsp if (err)
8165 0ebf8283 2019-07-24 stsp goto done;
8166 0ebf8283 2019-07-24 stsp
8167 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, 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 = got_worktree_set_head_ref(worktree, resolved);
8172 0ebf8283 2019-07-24 stsp if (err)
8173 0ebf8283 2019-07-24 stsp goto done;
8174 0ebf8283 2019-07-24 stsp
8175 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8176 a615e0e7 2020-12-16 stsp if (err)
8177 a615e0e7 2020-12-16 stsp goto done;
8178 a615e0e7 2020-12-16 stsp
8179 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8180 a615e0e7 2020-12-16 stsp if (err)
8181 a615e0e7 2020-12-16 stsp goto done;
8182 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8183 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8184 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8185 a615e0e7 2020-12-16 stsp err = sync_err;
8186 0ebf8283 2019-07-24 stsp done:
8187 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8188 a615e0e7 2020-12-16 stsp free(fileindex_path);
8189 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8190 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8191 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8192 0ebf8283 2019-07-24 stsp err = unlockerr;
8193 0ebf8283 2019-07-24 stsp return err;
8194 0ebf8283 2019-07-24 stsp }
8195 0ebf8283 2019-07-24 stsp
8196 0ebf8283 2019-07-24 stsp const struct got_error *
8197 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8198 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8199 0ebf8283 2019-07-24 stsp {
8200 0ebf8283 2019-07-24 stsp const struct got_error *err;
8201 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8202 0ebf8283 2019-07-24 stsp
8203 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8204 0ebf8283 2019-07-24 stsp if (err)
8205 0ebf8283 2019-07-24 stsp return err;
8206 0ebf8283 2019-07-24 stsp
8207 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8208 0ebf8283 2019-07-24 stsp if (err)
8209 0ebf8283 2019-07-24 stsp goto done;
8210 0ebf8283 2019-07-24 stsp
8211 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8212 0ebf8283 2019-07-24 stsp done:
8213 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8214 2822a352 2019-10-15 stsp return err;
8215 2822a352 2019-10-15 stsp }
8216 2822a352 2019-10-15 stsp
8217 2822a352 2019-10-15 stsp const struct got_error *
8218 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8219 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8220 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8221 2822a352 2019-10-15 stsp struct got_repository *repo)
8222 2822a352 2019-10-15 stsp {
8223 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8224 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8225 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8226 2822a352 2019-10-15 stsp
8227 2822a352 2019-10-15 stsp *fileindex = NULL;
8228 2822a352 2019-10-15 stsp *branch_ref = NULL;
8229 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8230 2822a352 2019-10-15 stsp
8231 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8232 2822a352 2019-10-15 stsp if (err)
8233 2822a352 2019-10-15 stsp return err;
8234 2822a352 2019-10-15 stsp
8235 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8236 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8237 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8238 2822a352 2019-10-15 stsp "update -b or different branch name required");
8239 2822a352 2019-10-15 stsp goto done;
8240 2822a352 2019-10-15 stsp }
8241 2822a352 2019-10-15 stsp
8242 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8243 2822a352 2019-10-15 stsp if (err)
8244 2822a352 2019-10-15 stsp goto done;
8245 2822a352 2019-10-15 stsp
8246 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8247 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8248 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8249 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8250 2822a352 2019-10-15 stsp &ok_arg);
8251 2822a352 2019-10-15 stsp if (err)
8252 2822a352 2019-10-15 stsp goto done;
8253 2822a352 2019-10-15 stsp
8254 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8255 2822a352 2019-10-15 stsp if (err)
8256 2822a352 2019-10-15 stsp goto done;
8257 2822a352 2019-10-15 stsp
8258 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8259 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8260 2822a352 2019-10-15 stsp done:
8261 2822a352 2019-10-15 stsp if (err) {
8262 2822a352 2019-10-15 stsp if (*branch_ref) {
8263 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8264 2822a352 2019-10-15 stsp *branch_ref = NULL;
8265 2822a352 2019-10-15 stsp }
8266 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8267 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8268 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8269 2822a352 2019-10-15 stsp }
8270 2822a352 2019-10-15 stsp if (*fileindex) {
8271 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8272 2822a352 2019-10-15 stsp *fileindex = NULL;
8273 2822a352 2019-10-15 stsp }
8274 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8275 2822a352 2019-10-15 stsp }
8276 0cb83759 2019-08-03 stsp return err;
8277 0cb83759 2019-08-03 stsp }
8278 0cb83759 2019-08-03 stsp
8279 2822a352 2019-10-15 stsp const struct got_error *
8280 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8281 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8282 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8283 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8284 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8285 2822a352 2019-10-15 stsp {
8286 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8287 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8288 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8289 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8290 2822a352 2019-10-15 stsp
8291 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8292 2822a352 2019-10-15 stsp if (err)
8293 2822a352 2019-10-15 stsp goto done;
8294 2822a352 2019-10-15 stsp
8295 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8296 2822a352 2019-10-15 stsp if (err)
8297 2822a352 2019-10-15 stsp goto done;
8298 2822a352 2019-10-15 stsp
8299 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8300 a44927cc 2022-04-07 stsp if (err)
8301 a44927cc 2022-04-07 stsp goto done;
8302 a44927cc 2022-04-07 stsp
8303 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8304 2822a352 2019-10-15 stsp worktree->path_prefix);
8305 2822a352 2019-10-15 stsp if (err)
8306 2822a352 2019-10-15 stsp goto done;
8307 2822a352 2019-10-15 stsp
8308 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8309 2822a352 2019-10-15 stsp if (err)
8310 2822a352 2019-10-15 stsp goto done;
8311 2822a352 2019-10-15 stsp
8312 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8313 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8314 2822a352 2019-10-15 stsp if (err)
8315 2822a352 2019-10-15 stsp goto sync;
8316 2822a352 2019-10-15 stsp
8317 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8318 2822a352 2019-10-15 stsp if (err)
8319 2822a352 2019-10-15 stsp goto sync;
8320 2822a352 2019-10-15 stsp
8321 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8322 6e210706 2021-01-22 stsp if (err)
8323 6e210706 2021-01-22 stsp goto sync;
8324 6e210706 2021-01-22 stsp
8325 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8326 2822a352 2019-10-15 stsp sync:
8327 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8328 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8329 2822a352 2019-10-15 stsp err = sync_err;
8330 2822a352 2019-10-15 stsp
8331 2822a352 2019-10-15 stsp done:
8332 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8333 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8334 2822a352 2019-10-15 stsp err = unlockerr;
8335 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8336 2822a352 2019-10-15 stsp
8337 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8338 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8339 2822a352 2019-10-15 stsp err = unlockerr;
8340 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8341 2822a352 2019-10-15 stsp
8342 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8343 2822a352 2019-10-15 stsp free(fileindex_path);
8344 2822a352 2019-10-15 stsp free(tree_id);
8345 a44927cc 2022-04-07 stsp if (commit)
8346 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8347 2822a352 2019-10-15 stsp
8348 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8349 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8350 2822a352 2019-10-15 stsp err = unlockerr;
8351 2822a352 2019-10-15 stsp return err;
8352 2822a352 2019-10-15 stsp }
8353 2822a352 2019-10-15 stsp
8354 2822a352 2019-10-15 stsp const struct got_error *
8355 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8356 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8357 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8358 2822a352 2019-10-15 stsp {
8359 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8360 8b692cd0 2019-10-21 stsp
8361 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8362 8b692cd0 2019-10-21 stsp
8363 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8364 8b692cd0 2019-10-21 stsp
8365 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8366 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8367 8b692cd0 2019-10-21 stsp err = unlockerr;
8368 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8369 8b692cd0 2019-10-21 stsp
8370 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8371 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8372 8b692cd0 2019-10-21 stsp err = unlockerr;
8373 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8374 f259c4c1 2021-09-24 stsp
8375 f259c4c1 2021-09-24 stsp return err;
8376 f259c4c1 2021-09-24 stsp }
8377 f259c4c1 2021-09-24 stsp
8378 f259c4c1 2021-09-24 stsp const struct got_error *
8379 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
8380 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
8381 f259c4c1 2021-09-24 stsp {
8382 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
8383 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8384 f259c4c1 2021-09-24 stsp
8385 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8386 f259c4c1 2021-09-24 stsp if (err)
8387 f259c4c1 2021-09-24 stsp goto done;
8388 8b692cd0 2019-10-21 stsp
8389 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8390 f259c4c1 2021-09-24 stsp
8391 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
8392 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8393 f259c4c1 2021-09-24 stsp err = sync_err;
8394 f259c4c1 2021-09-24 stsp done:
8395 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8396 f259c4c1 2021-09-24 stsp free(fileindex_path);
8397 8b692cd0 2019-10-21 stsp return err;
8398 2822a352 2019-10-15 stsp }
8399 2822a352 2019-10-15 stsp
8400 f259c4c1 2021-09-24 stsp static const struct got_error *
8401 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8402 f259c4c1 2021-09-24 stsp {
8403 f259c4c1 2021-09-24 stsp const struct got_error *err;
8404 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8405 f259c4c1 2021-09-24 stsp
8406 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8407 f259c4c1 2021-09-24 stsp if (err)
8408 f259c4c1 2021-09-24 stsp goto done;
8409 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
8410 f259c4c1 2021-09-24 stsp if (err)
8411 f259c4c1 2021-09-24 stsp goto done;
8412 f259c4c1 2021-09-24 stsp
8413 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8414 f259c4c1 2021-09-24 stsp if (err)
8415 f259c4c1 2021-09-24 stsp goto done;
8416 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
8417 f259c4c1 2021-09-24 stsp if (err)
8418 f259c4c1 2021-09-24 stsp goto done;
8419 f259c4c1 2021-09-24 stsp
8420 f259c4c1 2021-09-24 stsp done:
8421 f259c4c1 2021-09-24 stsp free(branch_refname);
8422 f259c4c1 2021-09-24 stsp free(commit_refname);
8423 f259c4c1 2021-09-24 stsp return err;
8424 f259c4c1 2021-09-24 stsp }
8425 f259c4c1 2021-09-24 stsp
8426 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
8427 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
8428 f259c4c1 2021-09-24 stsp const char *branch_name;
8429 f259c4c1 2021-09-24 stsp };
8430 f259c4c1 2021-09-24 stsp
8431 f259c4c1 2021-09-24 stsp static const struct got_error *
8432 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8433 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
8434 f259c4c1 2021-09-24 stsp {
8435 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
8436 f259c4c1 2021-09-24 stsp
8437 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8438 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
8439 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
8440 f259c4c1 2021-09-24 stsp
8441 f259c4c1 2021-09-24 stsp return NULL;
8442 f259c4c1 2021-09-24 stsp }
8443 f259c4c1 2021-09-24 stsp
8444 f259c4c1 2021-09-24 stsp
8445 f259c4c1 2021-09-24 stsp const struct got_error *
8446 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
8447 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
8448 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
8449 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
8450 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8451 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8452 f259c4c1 2021-09-24 stsp {
8453 f259c4c1 2021-09-24 stsp const struct got_error *err;
8454 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8455 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
8456 f259c4c1 2021-09-24 stsp
8457 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8458 f259c4c1 2021-09-24 stsp if (err)
8459 f259c4c1 2021-09-24 stsp goto done;
8460 f259c4c1 2021-09-24 stsp
8461 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
8462 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
8463 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
8464 cdbfe5d2 2023-07-24 stsp
8465 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8466 f259c4c1 2021-09-24 stsp worktree);
8467 f259c4c1 2021-09-24 stsp if (err)
8468 f259c4c1 2021-09-24 stsp goto done;
8469 f259c4c1 2021-09-24 stsp
8470 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8471 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
8472 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
8473 f259c4c1 2021-09-24 stsp done:
8474 f259c4c1 2021-09-24 stsp free(fileindex_path);
8475 f259c4c1 2021-09-24 stsp return err;
8476 f259c4c1 2021-09-24 stsp }
8477 f259c4c1 2021-09-24 stsp
8478 f259c4c1 2021-09-24 stsp const struct got_error *
8479 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
8480 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8481 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
8482 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
8483 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
8484 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
8485 0ff8d236 2021-09-28 stsp
8486 f259c4c1 2021-09-24 stsp {
8487 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
8488 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
8489 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
8490 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
8491 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
8492 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
8493 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8494 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
8495 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8496 f259c4c1 2021-09-24 stsp
8497 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
8498 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
8499 f259c4c1 2021-09-24 stsp
8500 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
8501 f259c4c1 2021-09-24 stsp
8502 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8503 f259c4c1 2021-09-24 stsp if (err)
8504 f259c4c1 2021-09-24 stsp goto done;
8505 f259c4c1 2021-09-24 stsp
8506 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8507 f259c4c1 2021-09-24 stsp if (err)
8508 f259c4c1 2021-09-24 stsp goto done;
8509 f259c4c1 2021-09-24 stsp
8510 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8511 f259c4c1 2021-09-24 stsp if (err)
8512 f259c4c1 2021-09-24 stsp goto done;
8513 f259c4c1 2021-09-24 stsp
8514 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8515 f259c4c1 2021-09-24 stsp &have_staged_files);
8516 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8517 f259c4c1 2021-09-24 stsp goto done;
8518 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8519 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8520 f259c4c1 2021-09-24 stsp goto done;
8521 f259c4c1 2021-09-24 stsp }
8522 f259c4c1 2021-09-24 stsp
8523 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
8524 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
8525 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
8526 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
8527 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
8528 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8529 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8530 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8531 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8532 f259c4c1 2021-09-24 stsp if (err)
8533 f259c4c1 2021-09-24 stsp goto done;
8534 f259c4c1 2021-09-24 stsp
8535 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8536 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8537 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8538 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8539 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8540 f259c4c1 2021-09-24 stsp if (err)
8541 f259c4c1 2021-09-24 stsp goto done;
8542 f259c4c1 2021-09-24 stsp
8543 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8544 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8545 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8546 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8547 f259c4c1 2021-09-24 stsp err = sync_err;
8548 f259c4c1 2021-09-24 stsp done:
8549 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8550 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8551 d8bacb93 2023-01-10 mark
8552 f259c4c1 2021-09-24 stsp free_commitable(ct);
8553 f259c4c1 2021-09-24 stsp }
8554 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8555 f259c4c1 2021-09-24 stsp free(fileindex_path);
8556 f259c4c1 2021-09-24 stsp return err;
8557 f259c4c1 2021-09-24 stsp }
8558 f259c4c1 2021-09-24 stsp
8559 f259c4c1 2021-09-24 stsp const struct got_error *
8560 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8561 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8562 f259c4c1 2021-09-24 stsp {
8563 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8564 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8565 f259c4c1 2021-09-24 stsp
8566 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8567 f259c4c1 2021-09-24 stsp if (err)
8568 f259c4c1 2021-09-24 stsp goto done;
8569 f259c4c1 2021-09-24 stsp
8570 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8571 f259c4c1 2021-09-24 stsp if (err)
8572 f259c4c1 2021-09-24 stsp goto done;
8573 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8574 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8575 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8576 f259c4c1 2021-09-24 stsp err = sync_err;
8577 f259c4c1 2021-09-24 stsp done:
8578 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8579 f259c4c1 2021-09-24 stsp free(fileindex_path);
8580 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8581 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8582 f259c4c1 2021-09-24 stsp err = unlockerr;
8583 f259c4c1 2021-09-24 stsp return err;
8584 f259c4c1 2021-09-24 stsp }
8585 f259c4c1 2021-09-24 stsp
8586 f259c4c1 2021-09-24 stsp const struct got_error *
8587 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8588 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8589 f259c4c1 2021-09-24 stsp {
8590 f259c4c1 2021-09-24 stsp const struct got_error *err;
8591 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8592 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8593 f259c4c1 2021-09-24 stsp
8594 f259c4c1 2021-09-24 stsp *in_progress = 0;
8595 f259c4c1 2021-09-24 stsp
8596 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8597 f259c4c1 2021-09-24 stsp if (err)
8598 f259c4c1 2021-09-24 stsp return err;
8599 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8600 793fcac3 2021-09-24 stsp free(branch_refname);
8601 f259c4c1 2021-09-24 stsp if (err) {
8602 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8603 f259c4c1 2021-09-24 stsp return err;
8604 f259c4c1 2021-09-24 stsp } else
8605 f259c4c1 2021-09-24 stsp *in_progress = 1;
8606 f259c4c1 2021-09-24 stsp
8607 f259c4c1 2021-09-24 stsp return NULL;
8608 f259c4c1 2021-09-24 stsp }
8609 f259c4c1 2021-09-24 stsp
8610 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8611 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8612 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8613 f259c4c1 2021-09-24 stsp {
8614 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8615 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8616 179f9db0 2023-06-20 falsifian struct got_reference *wt_branch = NULL;
8617 179f9db0 2023-06-20 falsifian struct got_object_id *wt_branch_tip = NULL;
8618 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8619 f259c4c1 2021-09-24 stsp
8620 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8621 f259c4c1 2021-09-24 stsp
8622 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8623 f259c4c1 2021-09-24 stsp if (err)
8624 f259c4c1 2021-09-24 stsp return err;
8625 f259c4c1 2021-09-24 stsp
8626 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8627 f259c4c1 2021-09-24 stsp if (err)
8628 f259c4c1 2021-09-24 stsp goto done;
8629 f259c4c1 2021-09-24 stsp
8630 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8631 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8632 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8633 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8634 f259c4c1 2021-09-24 stsp &ok_arg);
8635 f259c4c1 2021-09-24 stsp if (err)
8636 f259c4c1 2021-09-24 stsp goto done;
8637 f259c4c1 2021-09-24 stsp
8638 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8639 f259c4c1 2021-09-24 stsp 0);
8640 f259c4c1 2021-09-24 stsp if (err)
8641 f259c4c1 2021-09-24 stsp goto done;
8642 f259c4c1 2021-09-24 stsp
8643 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8644 f259c4c1 2021-09-24 stsp if (err)
8645 f259c4c1 2021-09-24 stsp goto done;
8646 f259c4c1 2021-09-24 stsp
8647 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8648 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8649 f259c4c1 2021-09-24 stsp goto done;
8650 f259c4c1 2021-09-24 stsp }
8651 179f9db0 2023-06-20 falsifian
8652 179f9db0 2023-06-20 falsifian done:
8653 179f9db0 2023-06-20 falsifian free(fileindex_path);
8654 179f9db0 2023-06-20 falsifian if (wt_branch)
8655 179f9db0 2023-06-20 falsifian got_ref_close(wt_branch);
8656 179f9db0 2023-06-20 falsifian free(wt_branch_tip);
8657 179f9db0 2023-06-20 falsifian if (err) {
8658 179f9db0 2023-06-20 falsifian if (*fileindex) {
8659 179f9db0 2023-06-20 falsifian got_fileindex_free(*fileindex);
8660 179f9db0 2023-06-20 falsifian *fileindex = NULL;
8661 179f9db0 2023-06-20 falsifian }
8662 179f9db0 2023-06-20 falsifian lock_worktree(worktree, LOCK_SH);
8663 179f9db0 2023-06-20 falsifian }
8664 179f9db0 2023-06-20 falsifian return err;
8665 179f9db0 2023-06-20 falsifian }
8666 f259c4c1 2021-09-24 stsp
8667 179f9db0 2023-06-20 falsifian const struct got_error *got_worktree_merge_write_refs(
8668 179f9db0 2023-06-20 falsifian struct got_worktree *worktree, struct got_reference *branch,
8669 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8670 179f9db0 2023-06-20 falsifian {
8671 179f9db0 2023-06-20 falsifian const struct got_error *err = NULL;
8672 179f9db0 2023-06-20 falsifian char *branch_refname = NULL, *commit_refname = NULL;
8673 179f9db0 2023-06-20 falsifian struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8674 179f9db0 2023-06-20 falsifian struct got_object_id *branch_tip = NULL;
8675 179f9db0 2023-06-20 falsifian
8676 179f9db0 2023-06-20 falsifian err = get_merge_branch_ref_name(&branch_refname, worktree);
8677 179f9db0 2023-06-20 falsifian if (err)
8678 179f9db0 2023-06-20 falsifian return err;
8679 179f9db0 2023-06-20 falsifian
8680 179f9db0 2023-06-20 falsifian err = get_merge_commit_ref_name(&commit_refname, worktree);
8681 179f9db0 2023-06-20 falsifian if (err)
8682 179f9db0 2023-06-20 falsifian return err;
8683 179f9db0 2023-06-20 falsifian
8684 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8685 f259c4c1 2021-09-24 stsp if (err)
8686 f259c4c1 2021-09-24 stsp goto done;
8687 f259c4c1 2021-09-24 stsp
8688 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8689 f259c4c1 2021-09-24 stsp if (err)
8690 f259c4c1 2021-09-24 stsp goto done;
8691 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8692 f259c4c1 2021-09-24 stsp if (err)
8693 f259c4c1 2021-09-24 stsp goto done;
8694 f259c4c1 2021-09-24 stsp
8695 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8696 f259c4c1 2021-09-24 stsp if (err)
8697 f259c4c1 2021-09-24 stsp goto done;
8698 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8699 f259c4c1 2021-09-24 stsp if (err)
8700 f259c4c1 2021-09-24 stsp goto done;
8701 f259c4c1 2021-09-24 stsp
8702 f259c4c1 2021-09-24 stsp done:
8703 f259c4c1 2021-09-24 stsp free(branch_refname);
8704 f259c4c1 2021-09-24 stsp free(commit_refname);
8705 f259c4c1 2021-09-24 stsp if (branch_ref)
8706 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8707 f259c4c1 2021-09-24 stsp if (commit_ref)
8708 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8709 179f9db0 2023-06-20 falsifian free(branch_tip);
8710 f259c4c1 2021-09-24 stsp return err;
8711 f259c4c1 2021-09-24 stsp }
8712 f259c4c1 2021-09-24 stsp
8713 f259c4c1 2021-09-24 stsp const struct got_error *
8714 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8715 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8716 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8717 f259c4c1 2021-09-24 stsp {
8718 f259c4c1 2021-09-24 stsp const struct got_error *err;
8719 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8720 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8721 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8722 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8723 f259c4c1 2021-09-24 stsp
8724 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8725 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8726 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8727 f259c4c1 2021-09-24 stsp
8728 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8729 f259c4c1 2021-09-24 stsp if (err)
8730 f259c4c1 2021-09-24 stsp return err;
8731 f259c4c1 2021-09-24 stsp
8732 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8733 f259c4c1 2021-09-24 stsp if (err)
8734 f259c4c1 2021-09-24 stsp goto done;
8735 f259c4c1 2021-09-24 stsp
8736 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8737 f259c4c1 2021-09-24 stsp &have_staged_files);
8738 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8739 f259c4c1 2021-09-24 stsp goto done;
8740 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8741 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8742 f259c4c1 2021-09-24 stsp goto done;
8743 f259c4c1 2021-09-24 stsp }
8744 f259c4c1 2021-09-24 stsp
8745 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8746 f259c4c1 2021-09-24 stsp if (err)
8747 f259c4c1 2021-09-24 stsp goto done;
8748 f259c4c1 2021-09-24 stsp
8749 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8750 f259c4c1 2021-09-24 stsp if (err)
8751 f259c4c1 2021-09-24 stsp goto done;
8752 f259c4c1 2021-09-24 stsp
8753 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8754 f259c4c1 2021-09-24 stsp if (err)
8755 f259c4c1 2021-09-24 stsp goto done;
8756 f259c4c1 2021-09-24 stsp
8757 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8758 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8759 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8760 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8761 f259c4c1 2021-09-24 stsp goto done;
8762 f259c4c1 2021-09-24 stsp }
8763 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8764 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8765 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8766 f259c4c1 2021-09-24 stsp goto done;
8767 f259c4c1 2021-09-24 stsp }
8768 f259c4c1 2021-09-24 stsp
8769 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8770 f259c4c1 2021-09-24 stsp if (err)
8771 f259c4c1 2021-09-24 stsp goto done;
8772 f259c4c1 2021-09-24 stsp
8773 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8774 f259c4c1 2021-09-24 stsp if (err)
8775 f259c4c1 2021-09-24 stsp goto done;
8776 f259c4c1 2021-09-24 stsp done:
8777 f259c4c1 2021-09-24 stsp free(commit_refname);
8778 f259c4c1 2021-09-24 stsp free(branch_refname);
8779 f259c4c1 2021-09-24 stsp free(fileindex_path);
8780 f259c4c1 2021-09-24 stsp if (commit_ref)
8781 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8782 f259c4c1 2021-09-24 stsp if (branch_ref)
8783 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8784 f259c4c1 2021-09-24 stsp if (err) {
8785 f259c4c1 2021-09-24 stsp if (*branch_name) {
8786 f259c4c1 2021-09-24 stsp free(*branch_name);
8787 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8788 f259c4c1 2021-09-24 stsp }
8789 f259c4c1 2021-09-24 stsp free(*branch_tip);
8790 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8791 f259c4c1 2021-09-24 stsp if (*fileindex) {
8792 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8793 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8794 f259c4c1 2021-09-24 stsp }
8795 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8796 f259c4c1 2021-09-24 stsp }
8797 f259c4c1 2021-09-24 stsp return err;
8798 f259c4c1 2021-09-24 stsp }
8799 f259c4c1 2021-09-24 stsp
8800 f259c4c1 2021-09-24 stsp const struct got_error *
8801 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8802 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8803 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8804 f259c4c1 2021-09-24 stsp {
8805 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8806 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8807 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8808 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8809 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8810 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8811 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8812 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8813 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8814 af179be7 2023-04-14 stsp
8815 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8816 af179be7 2023-04-14 stsp
8817 af179be7 2023-04-14 stsp err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8818 af179be7 2023-04-14 stsp if (err)
8819 af179be7 2023-04-14 stsp goto done;
8820 af179be7 2023-04-14 stsp
8821 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8822 af179be7 2023-04-14 stsp if (err)
8823 af179be7 2023-04-14 stsp goto done;
8824 af179be7 2023-04-14 stsp
8825 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8826 af179be7 2023-04-14 stsp if (err)
8827 af179be7 2023-04-14 stsp goto done;
8828 af179be7 2023-04-14 stsp
8829 af179be7 2023-04-14 stsp /*
8830 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8831 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8832 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
8833 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
8834 af179be7 2023-04-14 stsp */
8835 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(&added_paths,
8836 af179be7 2023-04-14 stsp got_worktree_get_base_commit_id(worktree), merged_commit_id,
8837 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8838 af179be7 2023-04-14 stsp if (err)
8839 af179be7 2023-04-14 stsp goto done;
8840 f259c4c1 2021-09-24 stsp
8841 af179be7 2023-04-14 stsp
8842 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8843 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8844 a44927cc 2022-04-07 stsp if (err)
8845 a44927cc 2022-04-07 stsp goto done;
8846 a44927cc 2022-04-07 stsp
8847 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8848 a44927cc 2022-04-07 stsp worktree->path_prefix);
8849 f259c4c1 2021-09-24 stsp if (err)
8850 f259c4c1 2021-09-24 stsp goto done;
8851 f259c4c1 2021-09-24 stsp
8852 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8853 f259c4c1 2021-09-24 stsp if (err)
8854 f259c4c1 2021-09-24 stsp goto done;
8855 f259c4c1 2021-09-24 stsp
8856 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8857 f259c4c1 2021-09-24 stsp if (err)
8858 f259c4c1 2021-09-24 stsp goto done;
8859 f259c4c1 2021-09-24 stsp
8860 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8861 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8862 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8863 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8864 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8865 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8866 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8867 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8868 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8869 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8870 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8871 f259c4c1 2021-09-24 stsp if (err)
8872 f259c4c1 2021-09-24 stsp goto sync;
8873 f259c4c1 2021-09-24 stsp
8874 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8875 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8876 f259c4c1 2021-09-24 stsp sync:
8877 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8878 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8879 f259c4c1 2021-09-24 stsp err = sync_err;
8880 f259c4c1 2021-09-24 stsp done:
8881 f259c4c1 2021-09-24 stsp free(tree_id);
8882 af179be7 2023-04-14 stsp free(merged_commit_id);
8883 a44927cc 2022-04-07 stsp if (commit)
8884 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8885 f259c4c1 2021-09-24 stsp if (fileindex)
8886 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8887 f259c4c1 2021-09-24 stsp free(fileindex_path);
8888 af179be7 2023-04-14 stsp if (commit_ref)
8889 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8890 af179be7 2023-04-14 stsp free(commit_ref_name);
8891 f259c4c1 2021-09-24 stsp
8892 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8893 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8894 f259c4c1 2021-09-24 stsp err = unlockerr;
8895 f259c4c1 2021-09-24 stsp return err;
8896 f259c4c1 2021-09-24 stsp }
8897 f259c4c1 2021-09-24 stsp
8898 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8899 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8900 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8901 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8902 2db2652d 2019-08-07 stsp struct got_repository *repo;
8903 2db2652d 2019-08-07 stsp int have_changes;
8904 2db2652d 2019-08-07 stsp };
8905 2db2652d 2019-08-07 stsp
8906 336075a4 2022-06-25 op static const struct got_error *
8907 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8908 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8909 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8910 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8911 735ef5ac 2019-08-03 stsp {
8912 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8913 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8914 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8915 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8916 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8917 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8918 8b13ce36 2019-08-08 stsp
8919 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8920 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8921 8b13ce36 2019-08-08 stsp return NULL;
8922 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8923 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8924 735ef5ac 2019-08-03 stsp
8925 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8926 735ef5ac 2019-08-03 stsp if (ie == NULL)
8927 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8928 735ef5ac 2019-08-03 stsp
8929 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8930 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8931 735ef5ac 2019-08-03 stsp relpath) == -1)
8932 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8933 735ef5ac 2019-08-03 stsp
8934 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8935 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8936 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8937 735ef5ac 2019-08-03 stsp }
8938 735ef5ac 2019-08-03 stsp
8939 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8940 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8941 3aa5969e 2019-08-06 stsp goto done;
8942 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8943 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8944 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8945 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8946 735ef5ac 2019-08-03 stsp goto done;
8947 3aa5969e 2019-08-06 stsp }
8948 735ef5ac 2019-08-03 stsp
8949 2db2652d 2019-08-07 stsp a->have_changes = 1;
8950 2db2652d 2019-08-07 stsp
8951 735ef5ac 2019-08-03 stsp p = in_repo_path;
8952 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8953 735ef5ac 2019-08-03 stsp p++;
8954 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8955 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8956 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8957 735ef5ac 2019-08-03 stsp done:
8958 735ef5ac 2019-08-03 stsp free(in_repo_path);
8959 dc424a06 2019-08-07 stsp return err;
8960 dc424a06 2019-08-07 stsp }
8961 dc424a06 2019-08-07 stsp
8962 2db2652d 2019-08-07 stsp struct stage_path_arg {
8963 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8964 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8965 2db2652d 2019-08-07 stsp struct got_repository *repo;
8966 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8967 2db2652d 2019-08-07 stsp void *status_arg;
8968 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8969 2db2652d 2019-08-07 stsp void *patch_arg;
8970 7b5dc508 2019-10-28 stsp int staged_something;
8971 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8972 2db2652d 2019-08-07 stsp };
8973 2db2652d 2019-08-07 stsp
8974 2db2652d 2019-08-07 stsp static const struct got_error *
8975 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8976 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8977 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8978 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8979 0cb83759 2019-08-03 stsp {
8980 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8981 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8982 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8983 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8984 0cb83759 2019-08-03 stsp uint32_t stage;
8985 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8986 0aeb8099 2020-07-23 stsp struct stat sb;
8987 8b13ce36 2019-08-08 stsp
8988 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8989 8b13ce36 2019-08-08 stsp return NULL;
8990 0cb83759 2019-08-03 stsp
8991 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8992 d3e7c587 2019-08-03 stsp if (ie == NULL)
8993 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8994 0cb83759 2019-08-03 stsp
8995 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8996 2db2652d 2019-08-07 stsp relpath)== -1)
8997 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8998 0cb83759 2019-08-03 stsp
8999 0cb83759 2019-08-03 stsp switch (status) {
9000 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9001 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9002 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9003 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9004 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9005 0aeb8099 2020-07-23 stsp break;
9006 0aeb8099 2020-07-23 stsp }
9007 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9008 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9009 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9010 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9011 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9012 dc424a06 2019-08-07 stsp if (err)
9013 dc424a06 2019-08-07 stsp break;
9014 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9015 dc424a06 2019-08-07 stsp break;
9016 dc424a06 2019-08-07 stsp } else {
9017 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9018 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9019 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9020 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9021 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9022 dc424a06 2019-08-07 stsp break;
9023 dc424a06 2019-08-07 stsp }
9024 dc424a06 2019-08-07 stsp }
9025 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9026 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9027 0cb83759 2019-08-03 stsp if (err)
9028 dc424a06 2019-08-07 stsp break;
9029 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9030 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9031 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9032 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9033 0cb83759 2019-08-03 stsp else
9034 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9035 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9036 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9037 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9038 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9039 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9040 35213c7c 2020-07-23 stsp ssize_t target_len;
9041 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9042 35213c7c 2020-07-23 stsp sizeof(target_path));
9043 35213c7c 2020-07-23 stsp if (target_len == -1) {
9044 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9045 35213c7c 2020-07-23 stsp ondisk_path);
9046 35213c7c 2020-07-23 stsp break;
9047 35213c7c 2020-07-23 stsp }
9048 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9049 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9050 df6221c7 2023-07-19 stsp a->worktree->root_path,
9051 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
9052 35213c7c 2020-07-23 stsp if (err)
9053 35213c7c 2020-07-23 stsp break;
9054 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9055 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9056 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9057 35213c7c 2020-07-23 stsp break;
9058 35213c7c 2020-07-23 stsp }
9059 35213c7c 2020-07-23 stsp }
9060 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9061 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9062 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9063 35213c7c 2020-07-23 stsp else
9064 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9065 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9066 0aeb8099 2020-07-23 stsp } else {
9067 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9068 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9069 0aeb8099 2020-07-23 stsp }
9070 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9071 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9072 dc424a06 2019-08-07 stsp break;
9073 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9074 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9075 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9076 9fdde394 2022-06-04 op if (err)
9077 9fdde394 2022-06-04 op break;
9078 9fdde394 2022-06-04 op /*
9079 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
9080 9fdde394 2022-06-04 op * implicitly unstage the file.
9081 9fdde394 2022-06-04 op */
9082 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9083 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
9084 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
9085 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
9086 9fdde394 2022-06-04 op }
9087 0cb83759 2019-08-03 stsp break;
9088 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9089 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9090 d3e7c587 2019-08-03 stsp break;
9091 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9092 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9093 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9094 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9095 dc424a06 2019-08-07 stsp if (err)
9096 dc424a06 2019-08-07 stsp break;
9097 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9098 88f33a19 2019-08-08 stsp break;
9099 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9100 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9101 dc424a06 2019-08-07 stsp break;
9102 88f33a19 2019-08-08 stsp }
9103 dc424a06 2019-08-07 stsp }
9104 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9105 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9106 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9107 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9108 dc424a06 2019-08-07 stsp break;
9109 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9110 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9111 12463d8b 2019-12-13 stsp de_name);
9112 0cb83759 2019-08-03 stsp break;
9113 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9114 d3e7c587 2019-08-03 stsp break;
9115 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9116 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9117 ebf48fd5 2019-08-03 stsp break;
9118 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9119 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9120 2a06fe5f 2019-08-24 stsp break;
9121 0cb83759 2019-08-03 stsp default:
9122 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9123 537ac44b 2019-08-03 stsp break;
9124 0cb83759 2019-08-03 stsp }
9125 dc424a06 2019-08-07 stsp
9126 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9127 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9128 dc424a06 2019-08-07 stsp free(path_content);
9129 2db2652d 2019-08-07 stsp free(ondisk_path);
9130 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9131 0ebf8283 2019-07-24 stsp return err;
9132 0ebf8283 2019-07-24 stsp }
9133 0cb83759 2019-08-03 stsp
9134 0cb83759 2019-08-03 stsp const struct got_error *
9135 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9136 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9137 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9138 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9139 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9140 0cb83759 2019-08-03 stsp {
9141 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9142 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9143 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9144 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9145 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9146 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9147 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9148 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9149 0cb83759 2019-08-03 stsp
9150 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9151 0cb83759 2019-08-03 stsp if (err)
9152 0cb83759 2019-08-03 stsp return err;
9153 0cb83759 2019-08-03 stsp
9154 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9155 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9156 735ef5ac 2019-08-03 stsp if (err)
9157 735ef5ac 2019-08-03 stsp goto done;
9158 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9159 735ef5ac 2019-08-03 stsp if (err)
9160 735ef5ac 2019-08-03 stsp goto done;
9161 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9162 0cb83759 2019-08-03 stsp if (err)
9163 0cb83759 2019-08-03 stsp goto done;
9164 0cb83759 2019-08-03 stsp
9165 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9166 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9167 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9168 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9169 2db2652d 2019-08-07 stsp oka.repo = repo;
9170 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9171 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9172 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9173 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
9174 735ef5ac 2019-08-03 stsp if (err)
9175 735ef5ac 2019-08-03 stsp goto done;
9176 735ef5ac 2019-08-03 stsp }
9177 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9178 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9179 2db2652d 2019-08-07 stsp goto done;
9180 2db2652d 2019-08-07 stsp }
9181 735ef5ac 2019-08-03 stsp
9182 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9183 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9184 2db2652d 2019-08-07 stsp spa.repo = repo;
9185 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9186 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9187 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9188 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9189 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9190 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9191 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9192 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9193 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
9194 42005733 2019-08-03 stsp if (err)
9195 2db2652d 2019-08-07 stsp goto done;
9196 7b5dc508 2019-10-28 stsp }
9197 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9198 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9199 7b5dc508 2019-10-28 stsp goto done;
9200 0cb83759 2019-08-03 stsp }
9201 0cb83759 2019-08-03 stsp
9202 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9203 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9204 0cb83759 2019-08-03 stsp err = sync_err;
9205 0cb83759 2019-08-03 stsp done:
9206 735ef5ac 2019-08-03 stsp if (head_ref)
9207 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9208 735ef5ac 2019-08-03 stsp free(head_commit_id);
9209 0cb83759 2019-08-03 stsp free(fileindex_path);
9210 0cb83759 2019-08-03 stsp if (fileindex)
9211 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9212 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9213 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9214 0cb83759 2019-08-03 stsp err = unlockerr;
9215 0cb83759 2019-08-03 stsp return err;
9216 0cb83759 2019-08-03 stsp }
9217 ad493afc 2019-08-03 stsp
9218 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9219 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9220 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9221 ad493afc 2019-08-03 stsp struct got_repository *repo;
9222 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9223 ad493afc 2019-08-03 stsp void *progress_arg;
9224 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9225 2e1f37b0 2019-08-08 stsp void *patch_arg;
9226 ad493afc 2019-08-03 stsp };
9227 2e1f37b0 2019-08-08 stsp
9228 2e1f37b0 2019-08-08 stsp static const struct got_error *
9229 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9230 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9231 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9232 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9233 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9234 2e1f37b0 2019-08-08 stsp {
9235 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9236 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9237 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9238 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9239 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9240 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9241 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9242 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9243 2e1f37b0 2019-08-08 stsp
9244 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9245 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9246 2e1f37b0 2019-08-08 stsp
9247 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9248 2e1f37b0 2019-08-08 stsp if (err)
9249 2e1f37b0 2019-08-08 stsp return err;
9250 eb81bc23 2022-06-28 tracey
9251 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9252 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9253 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9254 eb81bc23 2022-06-28 tracey goto done;
9255 eb81bc23 2022-06-28 tracey }
9256 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9257 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9258 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9259 eb81bc23 2022-06-28 tracey goto done;
9260 eb81bc23 2022-06-28 tracey }
9261 eb81bc23 2022-06-28 tracey
9262 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9263 2e1f37b0 2019-08-08 stsp if (err)
9264 2e1f37b0 2019-08-08 stsp goto done;
9265 2e1f37b0 2019-08-08 stsp
9266 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9267 2e1f37b0 2019-08-08 stsp if (err)
9268 2e1f37b0 2019-08-08 stsp goto done;
9269 2e1f37b0 2019-08-08 stsp
9270 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9271 2e1f37b0 2019-08-08 stsp if (err)
9272 2e1f37b0 2019-08-08 stsp goto done;
9273 2e1f37b0 2019-08-08 stsp
9274 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9275 eb81bc23 2022-06-28 tracey fd2);
9276 2e1f37b0 2019-08-08 stsp if (err)
9277 2e1f37b0 2019-08-08 stsp goto done;
9278 2e1f37b0 2019-08-08 stsp
9279 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9280 2e1f37b0 2019-08-08 stsp if (err)
9281 2e1f37b0 2019-08-08 stsp goto done;
9282 ad493afc 2019-08-03 stsp
9283 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9284 2e1f37b0 2019-08-08 stsp if (err)
9285 2e1f37b0 2019-08-08 stsp goto done;
9286 2e1f37b0 2019-08-08 stsp
9287 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9288 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9289 2e1f37b0 2019-08-08 stsp if (err)
9290 2e1f37b0 2019-08-08 stsp goto done;
9291 2e1f37b0 2019-08-08 stsp
9292 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9293 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
9294 2e1f37b0 2019-08-08 stsp if (err)
9295 2e1f37b0 2019-08-08 stsp goto done;
9296 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9297 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
9298 2e1f37b0 2019-08-08 stsp if (err)
9299 2e1f37b0 2019-08-08 stsp goto done;
9300 2e1f37b0 2019-08-08 stsp
9301 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9302 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9303 2e1f37b0 2019-08-08 stsp goto done;
9304 2e1f37b0 2019-08-08 stsp }
9305 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9306 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9307 2e1f37b0 2019-08-08 stsp goto done;
9308 2e1f37b0 2019-08-08 stsp }
9309 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9310 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9311 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
9312 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9313 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9314 fe621944 2020-11-10 stsp nchanges++;
9315 fe621944 2020-11-10 stsp }
9316 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9317 2e1f37b0 2019-08-08 stsp int choice;
9318 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9319 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9320 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9321 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9322 2e1f37b0 2019-08-08 stsp if (err)
9323 2e1f37b0 2019-08-08 stsp goto done;
9324 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9325 2e1f37b0 2019-08-08 stsp have_content = 1;
9326 19e4b907 2019-08-08 stsp else
9327 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9328 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9329 2e1f37b0 2019-08-08 stsp break;
9330 2e1f37b0 2019-08-08 stsp }
9331 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9332 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9333 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9334 2e1f37b0 2019-08-08 stsp done:
9335 2e1f37b0 2019-08-08 stsp free(label1);
9336 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9337 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9338 2e1f37b0 2019-08-08 stsp if (blob)
9339 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9340 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9341 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9342 2e1f37b0 2019-08-08 stsp if (staged_blob)
9343 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9344 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9345 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9346 fe621944 2020-11-10 stsp err = free_err;
9347 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9348 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9349 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9350 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9351 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9352 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9353 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9354 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9355 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9356 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9357 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9358 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9359 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9360 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9361 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9362 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9363 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9364 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9365 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9366 2e1f37b0 2019-08-08 stsp }
9367 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9368 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9369 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9370 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9371 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9372 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9373 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9374 2e1f37b0 2019-08-08 stsp }
9375 2e1f37b0 2019-08-08 stsp free(path1);
9376 2e1f37b0 2019-08-08 stsp free(path2);
9377 fda8017d 2020-07-23 stsp return err;
9378 fda8017d 2020-07-23 stsp }
9379 fda8017d 2020-07-23 stsp
9380 fda8017d 2020-07-23 stsp static const struct got_error *
9381 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9382 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9383 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9384 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9385 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9386 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9387 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9388 fda8017d 2020-07-23 stsp {
9389 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9390 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9391 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9392 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9393 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9394 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9395 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9396 fda8017d 2020-07-23 stsp struct stat sb;
9397 fda8017d 2020-07-23 stsp
9398 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9399 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9400 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9401 fda8017d 2020-07-23 stsp if (err)
9402 fda8017d 2020-07-23 stsp return err;
9403 fda8017d 2020-07-23 stsp
9404 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9405 fda8017d 2020-07-23 stsp return NULL;
9406 fda8017d 2020-07-23 stsp
9407 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9408 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9409 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9410 fda8017d 2020-07-23 stsp if (err)
9411 fda8017d 2020-07-23 stsp goto done;
9412 fda8017d 2020-07-23 stsp }
9413 fda8017d 2020-07-23 stsp
9414 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
9415 fda8017d 2020-07-23 stsp if (f == NULL) {
9416 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9417 fda8017d 2020-07-23 stsp path_unstaged_content);
9418 fda8017d 2020-07-23 stsp goto done;
9419 fda8017d 2020-07-23 stsp }
9420 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9421 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9422 fda8017d 2020-07-23 stsp goto done;
9423 fda8017d 2020-07-23 stsp }
9424 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9425 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9426 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9427 fda8017d 2020-07-23 stsp size_t r;
9428 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9429 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9430 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9431 fda8017d 2020-07-23 stsp goto done;
9432 fda8017d 2020-07-23 stsp }
9433 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9434 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9435 fda8017d 2020-07-23 stsp goto done;
9436 fda8017d 2020-07-23 stsp }
9437 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9438 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9439 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9440 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9441 fda8017d 2020-07-23 stsp progress_arg);
9442 fda8017d 2020-07-23 stsp } else {
9443 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9444 67a66647 2021-05-31 stsp
9445 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9446 67a66647 2021-05-31 stsp if (err)
9447 67a66647 2021-05-31 stsp return err;
9448 67a66647 2021-05-31 stsp
9449 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9450 67a66647 2021-05-31 stsp parent) == -1) {
9451 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9452 67a66647 2021-05-31 stsp base_path = NULL;
9453 67a66647 2021-05-31 stsp goto done;
9454 67a66647 2021-05-31 stsp }
9455 67a66647 2021-05-31 stsp
9456 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
9457 b90054ed 2022-11-01 stsp base_path, "");
9458 67a66647 2021-05-31 stsp if (err)
9459 67a66647 2021-05-31 stsp goto done;
9460 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9461 67a66647 2021-05-31 stsp blob_base);
9462 67a66647 2021-05-31 stsp if (err)
9463 67a66647 2021-05-31 stsp goto done;
9464 67a66647 2021-05-31 stsp
9465 5e91dae4 2022-08-30 stsp /*
9466 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9467 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9468 eec2f5a9 2021-06-03 stsp */
9469 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9470 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9471 eec2f5a9 2021-06-03 stsp ondisk_path);
9472 eec2f5a9 2021-06-03 stsp if (err)
9473 eec2f5a9 2021-06-03 stsp goto done;
9474 eec2f5a9 2021-06-03 stsp } else {
9475 eec2f5a9 2021-06-03 stsp int fd;
9476 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
9477 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9478 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9479 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9480 eec2f5a9 2021-06-03 stsp goto done;
9481 eec2f5a9 2021-06-03 stsp }
9482 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9483 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9484 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9485 eec2f5a9 2021-06-03 stsp close(fd);
9486 eec2f5a9 2021-06-03 stsp goto done;
9487 eec2f5a9 2021-06-03 stsp }
9488 eec2f5a9 2021-06-03 stsp }
9489 eec2f5a9 2021-06-03 stsp
9490 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9491 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9492 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9493 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9494 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9495 fda8017d 2020-07-23 stsp }
9496 fda8017d 2020-07-23 stsp if (err)
9497 fda8017d 2020-07-23 stsp goto done;
9498 fda8017d 2020-07-23 stsp
9499 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9500 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9501 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9502 2ac8aa02 2020-11-09 stsp } else {
9503 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9504 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9505 2ac8aa02 2020-11-09 stsp }
9506 fda8017d 2020-07-23 stsp done:
9507 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9508 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9509 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9510 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9511 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9512 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9513 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9514 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9515 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9516 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9517 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9518 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9519 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9520 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9521 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9522 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9523 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9524 67a66647 2021-05-31 stsp free(blob_base_path);
9525 67a66647 2021-05-31 stsp free(parent);
9526 67a66647 2021-05-31 stsp free(base_path);
9527 2e1f37b0 2019-08-08 stsp return err;
9528 2e1f37b0 2019-08-08 stsp }
9529 2e1f37b0 2019-08-08 stsp
9530 ad493afc 2019-08-03 stsp static const struct got_error *
9531 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9532 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9533 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9534 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9535 ad493afc 2019-08-03 stsp {
9536 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9537 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9538 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9539 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9540 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9541 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9542 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9543 9bc94a15 2019-08-03 stsp struct stat sb;
9544 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9545 ad493afc 2019-08-03 stsp
9546 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9547 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9548 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9549 2e1f37b0 2019-08-08 stsp return NULL;
9550 2e1f37b0 2019-08-08 stsp
9551 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9552 ad493afc 2019-08-03 stsp if (ie == NULL)
9553 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9554 9bc94a15 2019-08-03 stsp
9555 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9556 9bc94a15 2019-08-03 stsp == -1)
9557 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9558 ad493afc 2019-08-03 stsp
9559 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9560 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9561 f69721c3 2019-10-21 stsp if (err)
9562 f69721c3 2019-10-21 stsp goto done;
9563 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9564 f69721c3 2019-10-21 stsp id_str) == -1) {
9565 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9566 eb81bc23 2022-06-28 tracey goto done;
9567 eb81bc23 2022-06-28 tracey }
9568 eb81bc23 2022-06-28 tracey
9569 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9570 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9571 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9572 eb81bc23 2022-06-28 tracey goto done;
9573 eb81bc23 2022-06-28 tracey }
9574 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9575 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9576 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9577 f69721c3 2019-10-21 stsp goto done;
9578 f69721c3 2019-10-21 stsp }
9579 f69721c3 2019-10-21 stsp
9580 ad493afc 2019-08-03 stsp switch (staged_status) {
9581 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9582 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9583 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9584 ad493afc 2019-08-03 stsp if (err)
9585 ad493afc 2019-08-03 stsp break;
9586 ad493afc 2019-08-03 stsp /* fall through */
9587 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9588 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9589 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9590 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9591 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9592 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9593 2e1f37b0 2019-08-08 stsp if (err)
9594 2e1f37b0 2019-08-08 stsp break;
9595 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9596 2e1f37b0 2019-08-08 stsp break;
9597 2e1f37b0 2019-08-08 stsp } else {
9598 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9599 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9600 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9601 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9602 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9603 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9604 2e1f37b0 2019-08-08 stsp }
9605 2e1f37b0 2019-08-08 stsp }
9606 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9607 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9608 ad493afc 2019-08-03 stsp if (err)
9609 ad493afc 2019-08-03 stsp break;
9610 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9611 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9612 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9613 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9614 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9615 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9616 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9617 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9618 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9619 ea7786be 2020-07-23 stsp break;
9620 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9621 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9622 36bf999c 2020-07-23 stsp char *staged_target;
9623 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9624 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9625 36bf999c 2020-07-23 stsp if (err)
9626 36bf999c 2020-07-23 stsp goto done;
9627 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9628 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9629 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9630 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9631 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9632 36bf999c 2020-07-23 stsp free(staged_target);
9633 dfe9fba0 2020-07-23 stsp } else {
9634 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9635 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9636 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9637 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9638 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9639 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9640 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9641 dfe9fba0 2020-07-23 stsp }
9642 ea7786be 2020-07-23 stsp break;
9643 ea7786be 2020-07-23 stsp default:
9644 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9645 ea7786be 2020-07-23 stsp break;
9646 ea7786be 2020-07-23 stsp }
9647 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9648 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9649 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9650 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9651 2ac8aa02 2020-11-09 stsp }
9652 ad493afc 2019-08-03 stsp break;
9653 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9654 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9655 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9656 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9657 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9658 2e1f37b0 2019-08-08 stsp if (err)
9659 2e1f37b0 2019-08-08 stsp break;
9660 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9661 2e1f37b0 2019-08-08 stsp break;
9662 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9663 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9664 2e1f37b0 2019-08-08 stsp break;
9665 2e1f37b0 2019-08-08 stsp }
9666 2e1f37b0 2019-08-08 stsp }
9667 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9668 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9669 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9670 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9671 9bc94a15 2019-08-03 stsp if (err)
9672 9bc94a15 2019-08-03 stsp break;
9673 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9674 ad493afc 2019-08-03 stsp break;
9675 ad493afc 2019-08-03 stsp }
9676 f69721c3 2019-10-21 stsp done:
9677 ad493afc 2019-08-03 stsp free(ondisk_path);
9678 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9679 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9680 ad493afc 2019-08-03 stsp if (blob_base)
9681 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9682 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9683 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9684 ad493afc 2019-08-03 stsp if (blob_staged)
9685 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9686 f69721c3 2019-10-21 stsp free(id_str);
9687 f69721c3 2019-10-21 stsp free(label_orig);
9688 ad493afc 2019-08-03 stsp return err;
9689 ad493afc 2019-08-03 stsp }
9690 ad493afc 2019-08-03 stsp
9691 ad493afc 2019-08-03 stsp const struct got_error *
9692 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9693 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9694 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9695 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9696 ad493afc 2019-08-03 stsp struct got_repository *repo)
9697 ad493afc 2019-08-03 stsp {
9698 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9699 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9700 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9701 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9702 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9703 ad493afc 2019-08-03 stsp
9704 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9705 ad493afc 2019-08-03 stsp if (err)
9706 ad493afc 2019-08-03 stsp return err;
9707 ad493afc 2019-08-03 stsp
9708 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9709 ad493afc 2019-08-03 stsp if (err)
9710 ad493afc 2019-08-03 stsp goto done;
9711 ad493afc 2019-08-03 stsp
9712 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9713 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9714 ad493afc 2019-08-03 stsp upa.repo = repo;
9715 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9716 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9717 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9718 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9719 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9720 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9721 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9722 ad493afc 2019-08-03 stsp if (err)
9723 ad493afc 2019-08-03 stsp goto done;
9724 ad493afc 2019-08-03 stsp }
9725 ad493afc 2019-08-03 stsp
9726 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9727 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9728 ad493afc 2019-08-03 stsp err = sync_err;
9729 ad493afc 2019-08-03 stsp done:
9730 ad493afc 2019-08-03 stsp free(fileindex_path);
9731 ad493afc 2019-08-03 stsp if (fileindex)
9732 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9733 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9734 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9735 ad493afc 2019-08-03 stsp err = unlockerr;
9736 ad493afc 2019-08-03 stsp return err;
9737 ad493afc 2019-08-03 stsp }
9738 b2118c49 2020-07-28 stsp
9739 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9740 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9741 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9742 b2118c49 2020-07-28 stsp void *info_arg;
9743 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9744 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9745 b2118c49 2020-07-28 stsp void *cancel_arg;
9746 b2118c49 2020-07-28 stsp };
9747 b2118c49 2020-07-28 stsp
9748 b2118c49 2020-07-28 stsp static const struct got_error *
9749 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9750 b2118c49 2020-07-28 stsp {
9751 f6b8c3c2 2023-07-24 stsp const struct got_error *err;
9752 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9753 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9754 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9755 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9756 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9757 b2118c49 2020-07-28 stsp int stage;
9758 b2118c49 2020-07-28 stsp
9759 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
9760 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
9761 f6b8c3c2 2023-07-24 stsp if (err)
9762 f6b8c3c2 2023-07-24 stsp return err;
9763 f6b8c3c2 2023-07-24 stsp }
9764 b2118c49 2020-07-28 stsp
9765 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9766 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9767 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9768 b2118c49 2020-07-28 stsp break;
9769 b2118c49 2020-07-28 stsp }
9770 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9771 b2118c49 2020-07-28 stsp return NULL;
9772 b2118c49 2020-07-28 stsp
9773 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9774 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9775 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9776 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9777 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9778 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9779 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9780 b2118c49 2020-07-28 stsp }
9781 b2118c49 2020-07-28 stsp
9782 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9783 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9784 b2118c49 2020-07-28 stsp
9785 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9786 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9787 b2118c49 2020-07-28 stsp }
9788 b2118c49 2020-07-28 stsp
9789 b2118c49 2020-07-28 stsp const struct got_error *
9790 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9791 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9792 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9793 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9794 b2118c49 2020-07-28 stsp
9795 b2118c49 2020-07-28 stsp {
9796 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9797 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9798 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9799 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9800 b2118c49 2020-07-28 stsp
9801 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9802 b2118c49 2020-07-28 stsp if (err)
9803 b2118c49 2020-07-28 stsp return err;
9804 b2118c49 2020-07-28 stsp
9805 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9806 b2118c49 2020-07-28 stsp if (err)
9807 b2118c49 2020-07-28 stsp goto done;
9808 b2118c49 2020-07-28 stsp
9809 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9810 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9811 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9812 b2118c49 2020-07-28 stsp arg.paths = paths;
9813 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9814 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9815 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9816 b2118c49 2020-07-28 stsp &arg);
9817 b2118c49 2020-07-28 stsp done:
9818 b2118c49 2020-07-28 stsp free(fileindex_path);
9819 b2118c49 2020-07-28 stsp if (fileindex)
9820 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9821 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9822 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9823 b2118c49 2020-07-28 stsp err = unlockerr;
9824 b2118c49 2020-07-28 stsp return err;
9825 b2118c49 2020-07-28 stsp }
9826 78f5ac24 2022-03-19 op
9827 78f5ac24 2022-03-19 op static const struct got_error *
9828 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9829 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9830 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9831 78f5ac24 2022-03-19 op {
9832 78f5ac24 2022-03-19 op const struct got_error *err;
9833 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9834 78f5ac24 2022-03-19 op struct stat sb;
9835 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9836 78f5ac24 2022-03-19 op
9837 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9838 78f5ac24 2022-03-19 op if (err)
9839 78f5ac24 2022-03-19 op return err;
9840 78f5ac24 2022-03-19 op
9841 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9842 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9843 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9844 78f5ac24 2022-03-19 op
9845 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9846 78f5ac24 2022-03-19 op if (ie) {
9847 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9848 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9849 a05fb460 2022-04-23 op repo);
9850 78f5ac24 2022-03-19 op if (err)
9851 78f5ac24 2022-03-19 op goto done;
9852 78f5ac24 2022-03-19 op } else {
9853 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9854 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9855 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9856 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9857 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9858 78f5ac24 2022-03-19 op ondisk_path);
9859 78f5ac24 2022-03-19 op goto done;
9860 78f5ac24 2022-03-19 op }
9861 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9862 78f5ac24 2022-03-19 op }
9863 78f5ac24 2022-03-19 op }
9864 78f5ac24 2022-03-19 op
9865 78f5ac24 2022-03-19 op done:
9866 78f5ac24 2022-03-19 op free(ondisk_path);
9867 78f5ac24 2022-03-19 op return err;
9868 78f5ac24 2022-03-19 op }
9869 78f5ac24 2022-03-19 op
9870 78f5ac24 2022-03-19 op static const struct got_error *
9871 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9872 78f5ac24 2022-03-19 op unsigned char staged_status)
9873 78f5ac24 2022-03-19 op {
9874 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9875 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9876 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9877 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9878 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9879 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9880 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9881 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9882 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9883 78f5ac24 2022-03-19 op return NULL;
9884 78f5ac24 2022-03-19 op }
9885 78f5ac24 2022-03-19 op
9886 78f5ac24 2022-03-19 op static const struct got_error *
9887 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9888 78f5ac24 2022-03-19 op {
9889 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9890 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9891 78f5ac24 2022-03-19 op return NULL;
9892 78f5ac24 2022-03-19 op }
9893 78f5ac24 2022-03-19 op
9894 78f5ac24 2022-03-19 op static const struct got_error *
9895 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9896 78f5ac24 2022-03-19 op unsigned char staged_status)
9897 78f5ac24 2022-03-19 op {
9898 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9899 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9900 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9901 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9902 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9903 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9904 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9905 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9906 78f5ac24 2022-03-19 op return NULL;
9907 78f5ac24 2022-03-19 op }
9908 78f5ac24 2022-03-19 op
9909 78f5ac24 2022-03-19 op const struct got_error *
9910 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9911 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9912 78f5ac24 2022-03-19 op {
9913 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9914 78f5ac24 2022-03-19 op }
9915 78f5ac24 2022-03-19 op
9916 78f5ac24 2022-03-19 op const struct got_error *
9917 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9918 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9919 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9920 78f5ac24 2022-03-19 op {
9921 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9922 78f5ac24 2022-03-19 op int file_renamed = 0;
9923 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9924 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9925 78f5ac24 2022-03-19 op
9926 78f5ac24 2022-03-19 op *oldpath = NULL;
9927 78f5ac24 2022-03-19 op *newpath = NULL;
9928 78f5ac24 2022-03-19 op
9929 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9930 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9931 78f5ac24 2022-03-19 op if (err)
9932 78f5ac24 2022-03-19 op goto done;
9933 78f5ac24 2022-03-19 op
9934 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9935 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9936 78f5ac24 2022-03-19 op if (err)
9937 78f5ac24 2022-03-19 op goto done;
9938 78f5ac24 2022-03-19 op
9939 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9940 78f5ac24 2022-03-19 op file_renamed = 1;
9941 78f5ac24 2022-03-19 op
9942 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9943 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9944 78f5ac24 2022-03-19 op else if (file_renamed) {
9945 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9946 78f5ac24 2022-03-19 op if (err == NULL)
9947 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9948 78f5ac24 2022-03-19 op } else if (old == NULL)
9949 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9950 78f5ac24 2022-03-19 op else
9951 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9952 78f5ac24 2022-03-19 op
9953 78f5ac24 2022-03-19 op done:
9954 78f5ac24 2022-03-19 op if (err) {
9955 78f5ac24 2022-03-19 op free(*oldpath);
9956 78f5ac24 2022-03-19 op *oldpath = NULL;
9957 78f5ac24 2022-03-19 op free(*newpath);
9958 78f5ac24 2022-03-19 op *newpath = NULL;
9959 78f5ac24 2022-03-19 op }
9960 78f5ac24 2022-03-19 op return err;
9961 78f5ac24 2022-03-19 op }
9962 78f5ac24 2022-03-19 op
9963 78f5ac24 2022-03-19 op const struct got_error *
9964 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9965 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9966 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9967 78f5ac24 2022-03-19 op {
9968 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9969 f2dd7807 2022-05-17 op
9970 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9971 f2dd7807 2022-05-17 op saa.worktree = worktree;
9972 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9973 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9974 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9975 f2dd7807 2022-05-17 op saa.repo = repo;
9976 f2dd7807 2022-05-17 op
9977 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9978 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9979 78f5ac24 2022-03-19 op }
9980 f2dd7807 2022-05-17 op
9981 f2dd7807 2022-05-17 op const struct got_error *
9982 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9983 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9984 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9985 f2dd7807 2022-05-17 op {
9986 7f4e5320 2023-05-29 stsp const struct got_error *err;
9987 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9988 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
9989 f2dd7807 2022-05-17 op
9990 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9991 f2dd7807 2022-05-17 op sda.worktree = worktree;
9992 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9993 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9994 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9995 f2dd7807 2022-05-17 op sda.repo = repo;
9996 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9997 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9998 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9999 f2dd7807 2022-05-17 op sda.status_codes = NULL;
10000 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s/%s",
10001 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree), path) == -1)
10002 7f4e5320 2023-05-29 stsp return got_error_from_errno("asprintf");
10003 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
10004 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
10005 7f4e5320 2023-05-29 stsp
10006 7f4e5320 2023-05-29 stsp err = worktree_status(worktree, path, fileindex, repo,
10007 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10008 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
10009 7f4e5320 2023-05-29 stsp return err;
10010 f2dd7807 2022-05-17 op }
10011 f2dd7807 2022-05-17 op
10012 f2dd7807 2022-05-17 op const struct got_error *
10013 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
10014 4bcdc895 2022-05-17 op const char *fileindex_path)
10015 f2dd7807 2022-05-17 op {
10016 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
10017 f2dd7807 2022-05-17 op
10018 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
10019 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
10020 f2dd7807 2022-05-17 op
10021 f2dd7807 2022-05-17 op return err;
10022 f2dd7807 2022-05-17 op }