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 ef623445 2023-09-16 op int wt_fd, const char *path, struct got_object_id *blob_id,
1108 ef623445 2023-09-16 op int update_timestamps)
1109 13d9040b 2019-03-27 stsp {
1110 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1111 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1112 13d9040b 2019-03-27 stsp
1113 65b05cec 2020-07-23 stsp *new_iep = NULL;
1114 65b05cec 2020-07-23 stsp
1115 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1116 054041d0 2020-06-24 stsp if (err)
1117 054041d0 2020-06-24 stsp return err;
1118 054041d0 2020-06-24 stsp
1119 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1120 ef623445 2023-09-16 op blob_id->sha1, base_commit_id->sha1, update_timestamps);
1121 054041d0 2020-06-24 stsp if (err)
1122 054041d0 2020-06-24 stsp goto done;
1123 054041d0 2020-06-24 stsp
1124 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1125 054041d0 2020-06-24 stsp done:
1126 054041d0 2020-06-24 stsp if (err)
1127 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1128 65b05cec 2020-07-23 stsp else
1129 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1130 13d9040b 2019-03-27 stsp return err;
1131 1ebedb77 2019-10-19 stsp }
1132 1ebedb77 2019-10-19 stsp
1133 1ebedb77 2019-10-19 stsp static mode_t
1134 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1135 1ebedb77 2019-10-19 stsp {
1136 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1137 1ebedb77 2019-10-19 stsp
1138 1ebedb77 2019-10-19 stsp if (executable) {
1139 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1140 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1141 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1142 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1143 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1144 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1145 1ebedb77 2019-10-19 stsp }
1146 1ebedb77 2019-10-19 stsp
1147 b2b3fce1 2022-10-29 op return st_mode;
1148 13d9040b 2019-03-27 stsp }
1149 13d9040b 2019-03-27 stsp
1150 8ba819a3 2020-07-23 stsp /* forward declaration */
1151 13d9040b 2019-03-27 stsp static const struct got_error *
1152 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1153 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1154 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1155 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1156 ef623445 2023-09-16 op int path_is_unversioned, int *update_timestamps,
1157 ef623445 2023-09-16 op struct got_repository *repo,
1158 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1159 8ba819a3 2020-07-23 stsp
1160 5a1fbc73 2020-07-23 stsp /*
1161 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1162 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1163 5a1fbc73 2020-07-23 stsp */
1164 8ba819a3 2020-07-23 stsp static const struct got_error *
1165 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1166 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1167 5a1fbc73 2020-07-23 stsp {
1168 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1169 5a1fbc73 2020-07-23 stsp ssize_t elen;
1170 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1171 5a1fbc73 2020-07-23 stsp int fd;
1172 5a1fbc73 2020-07-23 stsp
1173 c6e8a826 2021-04-05 stsp *did_something = 0;
1174 c6e8a826 2021-04-05 stsp
1175 5a1fbc73 2020-07-23 stsp /*
1176 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1177 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1178 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1179 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1180 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1181 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1182 5a1fbc73 2020-07-23 stsp */
1183 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1184 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1185 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1186 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1187 5a1fbc73 2020-07-23 stsp
1188 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1189 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1190 5a1fbc73 2020-07-23 stsp if (elen == -1)
1191 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1192 5a1fbc73 2020-07-23 stsp
1193 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1194 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1195 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1196 5a1fbc73 2020-07-23 stsp }
1197 5a1fbc73 2020-07-23 stsp
1198 c6e8a826 2021-04-05 stsp *did_something = 1;
1199 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1200 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1201 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1202 5a1fbc73 2020-07-23 stsp return err;
1203 3c1ec782 2020-07-23 stsp }
1204 3c1ec782 2020-07-23 stsp
1205 3c1ec782 2020-07-23 stsp static const struct got_error *
1206 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1207 df6221c7 2023-07-19 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path,
1208 df6221c7 2023-07-19 stsp const char *meta_dir)
1209 3c1ec782 2020-07-23 stsp {
1210 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1211 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1212 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1213 3c1ec782 2020-07-23 stsp
1214 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1215 3c1ec782 2020-07-23 stsp
1216 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1217 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1218 3c1ec782 2020-07-23 stsp return NULL;
1219 3c1ec782 2020-07-23 stsp }
1220 3c1ec782 2020-07-23 stsp
1221 3c1ec782 2020-07-23 stsp /*
1222 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1223 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1224 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1225 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1226 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1227 3c1ec782 2020-07-23 stsp */
1228 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1229 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1230 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1231 ce031e9e 2020-10-20 stsp if (err)
1232 ce031e9e 2020-10-20 stsp return err;
1233 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1234 ce031e9e 2020-10-20 stsp free(parent);
1235 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1236 ce031e9e 2020-10-20 stsp }
1237 ce031e9e 2020-10-20 stsp free(parent);
1238 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1239 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1240 3c1ec782 2020-07-23 stsp free(abspath);
1241 3c1ec782 2020-07-23 stsp return err;
1242 3c1ec782 2020-07-23 stsp }
1243 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1244 3c1ec782 2020-07-23 stsp free(abspath);
1245 3c1ec782 2020-07-23 stsp if (err)
1246 3c1ec782 2020-07-23 stsp return err;
1247 3c1ec782 2020-07-23 stsp } else {
1248 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1249 3c1ec782 2020-07-23 stsp if (err)
1250 3c1ec782 2020-07-23 stsp return err;
1251 3c1ec782 2020-07-23 stsp }
1252 3c1ec782 2020-07-23 stsp
1253 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1254 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1255 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1256 3c1ec782 2020-07-23 stsp return NULL;
1257 3c1ec782 2020-07-23 stsp }
1258 3c1ec782 2020-07-23 stsp
1259 df6221c7 2023-07-19 stsp /* Do not allow symlinks pointing into the meta directory. */
1260 df6221c7 2023-07-19 stsp if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1261 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1262 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1263 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1264 3c1ec782 2020-07-23 stsp
1265 3c1ec782 2020-07-23 stsp free(path_got);
1266 3c1ec782 2020-07-23 stsp return NULL;
1267 5a1fbc73 2020-07-23 stsp }
1268 5a1fbc73 2020-07-23 stsp
1269 5a1fbc73 2020-07-23 stsp static const struct got_error *
1270 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1271 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1272 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1273 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1274 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1275 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1276 9d31a1d8 2018-03-11 stsp {
1277 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1278 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1279 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1280 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1281 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1282 8ba819a3 2020-07-23 stsp
1283 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1284 2e1fa222 2020-07-23 stsp
1285 3c29341b 2022-07-21 florian /*
1286 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1287 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1288 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1289 8ba819a3 2020-07-23 stsp * in the blob object.
1290 8ba819a3 2020-07-23 stsp */
1291 8ba819a3 2020-07-23 stsp do {
1292 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1293 538b6881 2022-07-21 florian if (err)
1294 538b6881 2022-07-21 florian return err;
1295 538b6881 2022-07-21 florian
1296 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1297 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1298 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1299 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1300 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1301 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1302 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1303 ef623445 2023-09-16 op 1, path_is_unversioned, NULL, repo, progress_cb,
1304 3b9f0f87 2020-07-23 stsp progress_arg);
1305 8ba819a3 2020-07-23 stsp }
1306 8ba819a3 2020-07-23 stsp if (len > 0) {
1307 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1308 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1309 8ba819a3 2020-07-23 stsp len - hdrlen);
1310 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1311 8ba819a3 2020-07-23 stsp hdrlen = 0;
1312 8ba819a3 2020-07-23 stsp }
1313 8ba819a3 2020-07-23 stsp } while (len != 0);
1314 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1315 8ba819a3 2020-07-23 stsp
1316 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1317 df6221c7 2023-07-19 stsp ondisk_path, worktree->root_path, worktree->meta_dir);
1318 3c1ec782 2020-07-23 stsp if (err)
1319 3c1ec782 2020-07-23 stsp return err;
1320 8ba819a3 2020-07-23 stsp
1321 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1322 906c123b 2020-07-23 stsp /* install as a regular file */
1323 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1324 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1325 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1326 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1327 ef623445 2023-09-16 op path_is_unversioned, NULL, repo,
1328 ef623445 2023-09-16 op progress_cb, progress_arg);
1329 3c29341b 2022-07-21 florian return err;
1330 906c123b 2020-07-23 stsp }
1331 906c123b 2020-07-23 stsp
1332 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1333 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1334 c6e8a826 2021-04-05 stsp int symlink_replaced;
1335 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1336 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1337 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1338 3c29341b 2022-07-21 florian return err;
1339 c90c8ce3 2020-07-23 stsp }
1340 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1341 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1342 5a1fbc73 2020-07-23 stsp if (err)
1343 3c29341b 2022-07-21 florian return err;
1344 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1345 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1346 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1347 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1348 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1349 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1350 c6e8a826 2021-04-05 stsp } else {
1351 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1352 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1353 c6e8a826 2021-04-05 stsp }
1354 f35fa46a 2020-07-23 stsp }
1355 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1356 f35fa46a 2020-07-23 stsp }
1357 f35fa46a 2020-07-23 stsp
1358 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1359 f4994adc 2020-10-20 stsp char *parent;
1360 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1361 f4994adc 2020-10-20 stsp if (err)
1362 3c29341b 2022-07-21 florian return err;
1363 6a390967 2023-07-14 stsp err = got_path_mkdir(parent);
1364 f4994adc 2020-10-20 stsp free(parent);
1365 f35fa46a 2020-07-23 stsp if (err)
1366 3c29341b 2022-07-21 florian return err;
1367 f35fa46a 2020-07-23 stsp /*
1368 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1369 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1370 f35fa46a 2020-07-23 stsp */
1371 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1372 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1373 6a390967 2023-07-14 stsp if (progress_cb) {
1374 6a390967 2023-07-14 stsp err = (*progress_cb)(progress_arg,
1375 6a390967 2023-07-14 stsp reverting_versioned_file ?
1376 6a390967 2023-07-14 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD,
1377 6a390967 2023-07-14 stsp path);
1378 6a390967 2023-07-14 stsp }
1379 3c29341b 2022-07-21 florian return err;
1380 f35fa46a 2020-07-23 stsp }
1381 f35fa46a 2020-07-23 stsp }
1382 f35fa46a 2020-07-23 stsp
1383 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1384 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1385 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1386 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1387 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1388 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1389 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1390 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1391 ef623445 2023-09-16 op path_is_unversioned, NULL, repo,
1392 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1393 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1394 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1395 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1396 8ba819a3 2020-07-23 stsp } else {
1397 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1398 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1399 8ba819a3 2020-07-23 stsp }
1400 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1401 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1402 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1403 8ba819a3 2020-07-23 stsp return err;
1404 8ba819a3 2020-07-23 stsp }
1405 8ba819a3 2020-07-23 stsp
1406 8ba819a3 2020-07-23 stsp static const struct got_error *
1407 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1408 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1409 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1410 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1411 ef623445 2023-09-16 op int path_is_unversioned, int *update_timestamps,
1412 ef623445 2023-09-16 op struct got_repository *repo,
1413 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1414 8ba819a3 2020-07-23 stsp {
1415 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1416 507dc3bb 2018-12-29 stsp int fd = -1;
1417 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1418 507dc3bb 2018-12-29 stsp int update = 0;
1419 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1420 b2b3fce1 2022-10-29 op mode_t mode;
1421 ef623445 2023-09-16 op
1422 ef623445 2023-09-16 op if (update_timestamps)
1423 ef623445 2023-09-16 op *update_timestamps = 1;
1424 8ba819a3 2020-07-23 stsp
1425 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1426 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1427 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1428 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1429 07fa9365 2023-03-10 stsp if (errno == ENOENT || errno == ENOTDIR) {
1430 f5375317 2020-10-20 stsp char *parent;
1431 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1432 f5375317 2020-10-20 stsp if (err)
1433 f5375317 2020-10-20 stsp return err;
1434 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1435 07fa9365 2023-03-10 stsp if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1436 07fa9365 2023-03-10 stsp err = got_error_path(path, err->code);
1437 f5375317 2020-10-20 stsp free(parent);
1438 21908da4 2019-01-13 stsp if (err)
1439 21908da4 2019-01-13 stsp return err;
1440 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1441 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1442 b2b3fce1 2022-10-29 op mode);
1443 21908da4 2019-01-13 stsp if (fd == -1)
1444 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1445 230a42bd 2019-05-11 jcs ondisk_path);
1446 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1447 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1448 ef623445 2023-09-16 op if (update_timestamps)
1449 ef623445 2023-09-16 op *update_timestamps = 0;
1450 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1451 ef623445 2023-09-16 op GOT_STATUS_EXISTS, path);
1452 3b9f0f87 2020-07-23 stsp goto done;
1453 3b9f0f87 2020-07-23 stsp }
1454 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1455 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1456 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1457 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1458 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1459 507dc3bb 2018-12-29 stsp goto done;
1460 d70b8e30 2018-12-27 stsp } else {
1461 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1462 b90054ed 2022-11-01 stsp ondisk_path, "");
1463 507dc3bb 2018-12-29 stsp if (err)
1464 507dc3bb 2018-12-29 stsp goto done;
1465 507dc3bb 2018-12-29 stsp update = 1;
1466 b2b3fce1 2022-10-29 op
1467 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1468 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1469 b2b3fce1 2022-10-29 op tmppath);
1470 b2b3fce1 2022-10-29 op goto done;
1471 b2b3fce1 2022-10-29 op }
1472 9d31a1d8 2018-03-11 stsp }
1473 507dc3bb 2018-12-29 stsp } else
1474 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1475 3818e3c4 2020-11-01 naddy }
1476 3818e3c4 2020-11-01 naddy
1477 bd6aa359 2020-07-23 stsp if (progress_cb) {
1478 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1479 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1480 bd6aa359 2020-07-23 stsp path);
1481 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1482 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1483 bd6aa359 2020-07-23 stsp path);
1484 bd6aa359 2020-07-23 stsp else
1485 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1486 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1487 bd6aa359 2020-07-23 stsp if (err)
1488 bd6aa359 2020-07-23 stsp goto done;
1489 bd6aa359 2020-07-23 stsp }
1490 d7b62c98 2018-12-27 stsp
1491 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1492 9d31a1d8 2018-03-11 stsp do {
1493 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1494 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1495 9d31a1d8 2018-03-11 stsp if (err)
1496 9d31a1d8 2018-03-11 stsp break;
1497 9d31a1d8 2018-03-11 stsp if (len > 0) {
1498 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1499 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1500 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1501 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1502 b87c6f83 2018-12-24 stsp goto done;
1503 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1504 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1505 b87c6f83 2018-12-24 stsp goto done;
1506 9d31a1d8 2018-03-11 stsp }
1507 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1508 9d31a1d8 2018-03-11 stsp }
1509 9d31a1d8 2018-03-11 stsp } while (len != 0);
1510 9d31a1d8 2018-03-11 stsp
1511 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1512 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1513 816dc654 2019-02-16 stsp goto done;
1514 816dc654 2019-02-16 stsp }
1515 9d31a1d8 2018-03-11 stsp
1516 507dc3bb 2018-12-29 stsp if (update) {
1517 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1518 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1519 f6d8c0ac 2020-11-09 stsp goto done;
1520 f6d8c0ac 2020-11-09 stsp }
1521 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1522 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1523 230a42bd 2019-05-11 jcs ondisk_path);
1524 68ed9ba5 2019-02-10 stsp goto done;
1525 68ed9ba5 2019-02-10 stsp }
1526 63df146d 2020-11-09 stsp free(tmppath);
1527 63df146d 2020-11-09 stsp tmppath = NULL;
1528 507dc3bb 2018-12-29 stsp }
1529 507dc3bb 2018-12-29 stsp
1530 9d31a1d8 2018-03-11 stsp done:
1531 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1532 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1533 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1534 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1535 507dc3bb 2018-12-29 stsp free(tmppath);
1536 6353ad76 2019-02-08 stsp return err;
1537 6353ad76 2019-02-08 stsp }
1538 6353ad76 2019-02-08 stsp
1539 12383673 2023-02-18 mark /*
1540 12383673 2023-02-18 mark * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1541 12383673 2023-02-18 mark * conflict marker is found in newly added lines only.
1542 12383673 2023-02-18 mark */
1543 6353ad76 2019-02-08 stsp static const struct got_error *
1544 12383673 2023-02-18 mark get_modified_file_content_status(unsigned char *status,
1545 12383673 2023-02-18 mark struct got_blob_object *blob, const char *path, struct stat *sb,
1546 12383673 2023-02-18 mark FILE *ondisk_file)
1547 7154f6ce 2019-03-27 stsp {
1548 d952957d 2023-02-19 mark const struct got_error *err, *free_err;
1549 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1550 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1551 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1552 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1553 7154f6ce 2019-03-27 stsp };
1554 d952957d 2023-02-19 mark FILE *f1 = NULL;
1555 d952957d 2023-02-19 mark struct got_diffreg_result *diffreg_result = NULL;
1556 d952957d 2023-02-19 mark struct diff_result *r;
1557 d952957d 2023-02-19 mark int nchunks_parsed, n, i = 0, ln = 0;
1558 9bdd68dd 2020-01-02 naddy char *line = NULL;
1559 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1560 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1561 7154f6ce 2019-03-27 stsp
1562 d952957d 2023-02-19 mark if (*status != GOT_STATUS_MODIFY)
1563 d952957d 2023-02-19 mark return NULL;
1564 12383673 2023-02-18 mark
1565 d952957d 2023-02-19 mark f1 = got_opentemp();
1566 d952957d 2023-02-19 mark if (f1 == NULL)
1567 d952957d 2023-02-19 mark return got_error_from_errno("got_opentemp");
1568 12383673 2023-02-18 mark
1569 d952957d 2023-02-19 mark if (blob) {
1570 d952957d 2023-02-19 mark got_object_blob_rewind(blob);
1571 d952957d 2023-02-19 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1572 12383673 2023-02-18 mark if (err)
1573 12383673 2023-02-18 mark goto done;
1574 d952957d 2023-02-19 mark }
1575 12383673 2023-02-18 mark
1576 d952957d 2023-02-19 mark err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1577 d952957d 2023-02-19 mark 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1578 d952957d 2023-02-19 mark if (err)
1579 d952957d 2023-02-19 mark goto done;
1580 d952957d 2023-02-19 mark
1581 d952957d 2023-02-19 mark r = diffreg_result->result;
1582 d952957d 2023-02-19 mark
1583 d952957d 2023-02-19 mark for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1584 d952957d 2023-02-19 mark struct diff_chunk *c;
1585 d952957d 2023-02-19 mark struct diff_chunk_context cc = {};
1586 7783f73c 2023-02-20 mark off_t pos;
1587 d952957d 2023-02-19 mark
1588 d952957d 2023-02-19 mark /*
1589 d952957d 2023-02-19 mark * We can optimise a little by advancing straight
1590 d952957d 2023-02-19 mark * to the next chunk if this one has no added lines.
1591 d952957d 2023-02-19 mark */
1592 d952957d 2023-02-19 mark c = diff_chunk_get(r, n);
1593 d952957d 2023-02-19 mark
1594 45e6b2f4 2023-02-20 mark if (diff_chunk_type(c) != CHUNK_PLUS) {
1595 d952957d 2023-02-19 mark nchunks_parsed = 1;
1596 7783f73c 2023-02-20 mark continue; /* removed or unchanged lines */
1597 7154f6ce 2019-03-27 stsp }
1598 7154f6ce 2019-03-27 stsp
1599 7783f73c 2023-02-20 mark pos = diff_chunk_get_right_start_pos(c);
1600 7783f73c 2023-02-20 mark if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1601 7783f73c 2023-02-20 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1602 7783f73c 2023-02-20 mark goto done;
1603 7154f6ce 2019-03-27 stsp }
1604 d952957d 2023-02-19 mark
1605 7783f73c 2023-02-20 mark diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1606 7783f73c 2023-02-20 mark ln = cc.right.start;
1607 7783f73c 2023-02-20 mark
1608 d952957d 2023-02-19 mark while (ln < cc.right.end) {
1609 d952957d 2023-02-19 mark linelen = getline(&line, &linesize, ondisk_file);
1610 d952957d 2023-02-19 mark if (linelen == -1) {
1611 d952957d 2023-02-19 mark if (feof(ondisk_file))
1612 d952957d 2023-02-19 mark break;
1613 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1614 d952957d 2023-02-19 mark break;
1615 d952957d 2023-02-19 mark }
1616 d952957d 2023-02-19 mark
1617 d952957d 2023-02-19 mark if (line && strncmp(line, markers[i],
1618 d952957d 2023-02-19 mark strlen(markers[i])) == 0) {
1619 d952957d 2023-02-19 mark if (strcmp(markers[i],
1620 d952957d 2023-02-19 mark GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1621 d952957d 2023-02-19 mark *status = GOT_STATUS_CONFLICT;
1622 d952957d 2023-02-19 mark goto done;
1623 d952957d 2023-02-19 mark } else
1624 d952957d 2023-02-19 mark i++;
1625 d952957d 2023-02-19 mark }
1626 d952957d 2023-02-19 mark ++ln;
1627 d952957d 2023-02-19 mark }
1628 7154f6ce 2019-03-27 stsp }
1629 12383673 2023-02-18 mark
1630 12383673 2023-02-18 mark done:
1631 9bdd68dd 2020-01-02 naddy free(line);
1632 12383673 2023-02-18 mark if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1633 12383673 2023-02-18 mark err = got_error_from_errno("fclose");
1634 d952957d 2023-02-19 mark free_err = got_diffreg_result_free(diffreg_result);
1635 d952957d 2023-02-19 mark if (err == NULL)
1636 d952957d 2023-02-19 mark err = free_err;
1637 7154f6ce 2019-03-27 stsp
1638 7154f6ce 2019-03-27 stsp return err;
1639 e2b1e152 2019-07-13 stsp }
1640 e2b1e152 2019-07-13 stsp
1641 e2b1e152 2019-07-13 stsp static int
1642 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1643 1ebedb77 2019-10-19 stsp {
1644 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1645 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1646 1ebedb77 2019-10-19 stsp }
1647 1ebedb77 2019-10-19 stsp
1648 1ebedb77 2019-10-19 stsp static int
1649 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1650 e2b1e152 2019-07-13 stsp {
1651 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1652 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1653 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1654 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1655 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1656 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1657 c363b2c1 2019-08-03 stsp }
1658 c363b2c1 2019-08-03 stsp
1659 c363b2c1 2019-08-03 stsp static unsigned char
1660 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1661 c363b2c1 2019-08-03 stsp {
1662 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1663 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1664 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1665 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1666 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1667 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1668 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1669 c363b2c1 2019-08-03 stsp default:
1670 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1671 a919d5c4 2020-07-23 stsp }
1672 a919d5c4 2020-07-23 stsp }
1673 a919d5c4 2020-07-23 stsp
1674 a919d5c4 2020-07-23 stsp static const struct got_error *
1675 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1676 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1677 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1678 a919d5c4 2020-07-23 stsp {
1679 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1680 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1681 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1682 a919d5c4 2020-07-23 stsp ssize_t elen;
1683 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1684 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1685 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1686 a919d5c4 2020-07-23 stsp
1687 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1688 a919d5c4 2020-07-23 stsp
1689 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1690 a919d5c4 2020-07-23 stsp do {
1691 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1692 a919d5c4 2020-07-23 stsp if (err)
1693 a919d5c4 2020-07-23 stsp return err;
1694 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1695 a919d5c4 2020-07-23 stsp /*
1696 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1697 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1698 a919d5c4 2020-07-23 stsp */
1699 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1700 a919d5c4 2020-07-23 stsp }
1701 a919d5c4 2020-07-23 stsp if (len > 0) {
1702 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1703 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1704 a919d5c4 2020-07-23 stsp len - hdrlen);
1705 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1706 a919d5c4 2020-07-23 stsp hdrlen = 0;
1707 a919d5c4 2020-07-23 stsp }
1708 a919d5c4 2020-07-23 stsp } while (len != 0);
1709 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1710 a919d5c4 2020-07-23 stsp
1711 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1712 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1713 a919d5c4 2020-07-23 stsp if (elen == -1)
1714 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1715 a919d5c4 2020-07-23 stsp } else {
1716 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1717 a919d5c4 2020-07-23 stsp if (elen == -1)
1718 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1719 c363b2c1 2019-08-03 stsp }
1720 a919d5c4 2020-07-23 stsp
1721 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1722 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1723 a919d5c4 2020-07-23 stsp
1724 a919d5c4 2020-07-23 stsp return NULL;
1725 7154f6ce 2019-03-27 stsp }
1726 7154f6ce 2019-03-27 stsp
1727 7154f6ce 2019-03-27 stsp static const struct got_error *
1728 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1729 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1730 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1731 6353ad76 2019-02-08 stsp {
1732 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1733 6353ad76 2019-02-08 stsp struct got_object_id id;
1734 6353ad76 2019-02-08 stsp size_t hdrlen;
1735 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1736 6353ad76 2019-02-08 stsp FILE *f = NULL;
1737 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1738 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1739 6353ad76 2019-02-08 stsp size_t flen, blen;
1740 2c4740ad 2023-02-12 mark unsigned char staged_status;
1741 6353ad76 2019-02-08 stsp
1742 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
1743 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1744 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1745 6353ad76 2019-02-08 stsp
1746 7f91a133 2019-12-13 stsp /*
1747 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1748 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1749 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1750 7f91a133 2019-12-13 stsp */
1751 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1752 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1753 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1754 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1755 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1756 882ef1b9 2019-12-13 stsp else
1757 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1758 882ef1b9 2019-12-13 stsp goto done;
1759 882ef1b9 2019-12-13 stsp }
1760 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1761 3d35a492 2019-12-13 stsp goto done;
1762 3d35a492 2019-12-13 stsp }
1763 7f91a133 2019-12-13 stsp } else {
1764 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1765 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1766 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1767 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1768 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1769 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1770 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1771 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1772 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1773 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1774 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1775 3d35a492 2019-12-13 stsp else
1776 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1777 3d35a492 2019-12-13 stsp goto done;
1778 3d35a492 2019-12-13 stsp }
1779 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1780 1338848f 2019-12-13 stsp goto done;
1781 a378724f 2019-02-10 stsp }
1782 a378724f 2019-02-10 stsp }
1783 6353ad76 2019-02-08 stsp
1784 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1785 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1786 1338848f 2019-12-13 stsp goto done;
1787 3f148bc6 2019-07-27 stsp }
1788 efdd40df 2019-07-27 stsp
1789 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1790 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1791 1338848f 2019-12-13 stsp goto done;
1792 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1793 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1794 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1795 1338848f 2019-12-13 stsp goto done;
1796 d00136be 2019-03-26 stsp }
1797 b8f41171 2019-02-10 stsp
1798 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1799 f179e66d 2020-07-23 stsp goto done;
1800 f179e66d 2020-07-23 stsp
1801 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1802 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1803 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1804 1338848f 2019-12-13 stsp goto done;
1805 f179e66d 2020-07-23 stsp }
1806 6353ad76 2019-02-08 stsp
1807 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1808 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1809 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1810 c363b2c1 2019-08-03 stsp else
1811 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1812 c363b2c1 2019-08-03 stsp
1813 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1814 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1815 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1816 eb81bc23 2022-06-28 tracey goto done;
1817 eb81bc23 2022-06-28 tracey }
1818 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1819 6353ad76 2019-02-08 stsp if (err)
1820 1338848f 2019-12-13 stsp goto done;
1821 6353ad76 2019-02-08 stsp
1822 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1823 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1824 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1825 a919d5c4 2020-07-23 stsp goto done;
1826 a919d5c4 2020-07-23 stsp }
1827 a919d5c4 2020-07-23 stsp
1828 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1829 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1830 ab0d4361 2019-12-13 stsp if (fd == -1) {
1831 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1832 ab0d4361 2019-12-13 stsp goto done;
1833 ab0d4361 2019-12-13 stsp }
1834 3d35a492 2019-12-13 stsp }
1835 3d35a492 2019-12-13 stsp
1836 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1837 6353ad76 2019-02-08 stsp if (f == NULL) {
1838 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1839 6353ad76 2019-02-08 stsp goto done;
1840 6353ad76 2019-02-08 stsp }
1841 1338848f 2019-12-13 stsp fd = -1;
1842 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1843 656b1f76 2019-05-11 jcs for (;;) {
1844 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1845 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1846 6353ad76 2019-02-08 stsp if (err)
1847 7154f6ce 2019-03-27 stsp goto done;
1848 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1849 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1850 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1851 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1852 7154f6ce 2019-03-27 stsp goto done;
1853 80c5c120 2019-02-19 stsp }
1854 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1855 6353ad76 2019-02-08 stsp if (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 } else if (flen == 0) {
1859 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1860 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1861 6353ad76 2019-02-08 stsp break;
1862 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1863 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1864 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1865 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1866 6353ad76 2019-02-08 stsp break;
1867 6353ad76 2019-02-08 stsp }
1868 6353ad76 2019-02-08 stsp } else {
1869 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1870 6353ad76 2019-02-08 stsp break;
1871 6353ad76 2019-02-08 stsp }
1872 6353ad76 2019-02-08 stsp hdrlen = 0;
1873 6353ad76 2019-02-08 stsp }
1874 7154f6ce 2019-03-27 stsp
1875 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1876 7154f6ce 2019-03-27 stsp rewind(f);
1877 12383673 2023-02-18 mark err = get_modified_file_content_status(status, blob, ie->path,
1878 12383673 2023-02-18 mark sb, f);
1879 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1880 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1881 6353ad76 2019-02-08 stsp done:
1882 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1883 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1884 6353ad76 2019-02-08 stsp if (blob)
1885 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1886 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1887 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1888 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1889 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1890 9d31a1d8 2018-03-11 stsp return err;
1891 e2b1e152 2019-07-13 stsp }
1892 e2b1e152 2019-07-13 stsp
1893 e2b1e152 2019-07-13 stsp /*
1894 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1895 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1896 e2b1e152 2019-07-13 stsp */
1897 e2b1e152 2019-07-13 stsp static const struct got_error *
1898 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1899 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1900 e2b1e152 2019-07-13 stsp {
1901 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1902 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1903 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1904 e2b1e152 2019-07-13 stsp
1905 e2b1e152 2019-07-13 stsp return NULL;
1906 9d31a1d8 2018-03-11 stsp }
1907 9d31a1d8 2018-03-11 stsp
1908 07fa9365 2023-03-10 stsp static const struct got_error *remove_ondisk_file(const char *, const char *);
1909 07fa9365 2023-03-10 stsp
1910 9d31a1d8 2018-03-11 stsp static const struct got_error *
1911 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1912 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1913 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1914 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1915 0584f854 2019-04-06 stsp void *progress_arg)
1916 9d31a1d8 2018-03-11 stsp {
1917 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1918 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1919 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1920 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1921 b8f41171 2019-02-10 stsp struct stat sb;
1922 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1923 ef623445 2023-09-16 op int update_timestamps;
1924 9d31a1d8 2018-03-11 stsp
1925 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1926 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1927 6353ad76 2019-02-08 stsp
1928 abb4604f 2019-07-27 stsp if (ie) {
1929 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1930 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1931 a76c42e6 2019-08-03 stsp goto done;
1932 a76c42e6 2019-08-03 stsp }
1933 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1934 7f91a133 2019-12-13 stsp repo);
1935 abb4604f 2019-07-27 stsp if (err)
1936 abb4604f 2019-07-27 stsp goto done;
1937 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1938 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1939 c90c8ce3 2020-07-23 stsp } else {
1940 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1941 07fa9365 2023-03-10 stsp if (errno != ENOENT && errno != ENOTDIR) {
1942 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1943 f6764181 2021-09-24 stsp ondisk_path);
1944 f6764181 2021-09-24 stsp goto done;
1945 f6764181 2021-09-24 stsp }
1946 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1947 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1948 f6764181 2021-09-24 stsp } else {
1949 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1950 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1951 f6764181 2021-09-24 stsp else
1952 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1953 f6764181 2021-09-24 stsp }
1954 c90c8ce3 2020-07-23 stsp }
1955 6353ad76 2019-02-08 stsp
1956 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1957 2c41dce7 2021-06-27 stsp if (ie)
1958 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1959 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1960 b8f41171 2019-02-10 stsp goto done;
1961 b8f41171 2019-02-10 stsp }
1962 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1963 a769b60b 2021-06-27 stsp if (ie)
1964 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1965 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1966 5036ab18 2020-04-18 stsp path);
1967 5036ab18 2020-04-18 stsp goto done;
1968 07fa9365 2023-03-10 stsp }
1969 07fa9365 2023-03-10 stsp
1970 07fa9365 2023-03-10 stsp if (S_ISDIR(te->mode)) { /* file changing into a directory */
1971 07fa9365 2023-03-10 stsp if (status == GOT_STATUS_UNVERSIONED) {
1972 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, status, path);
1973 07fa9365 2023-03-10 stsp } else if (status != GOT_STATUS_NO_CHANGE &&
1974 07fa9365 2023-03-10 stsp status != GOT_STATUS_DELETE &&
1975 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1976 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1977 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg,
1978 07fa9365 2023-03-10 stsp GOT_STATUS_CANNOT_DELETE, path);
1979 07fa9365 2023-03-10 stsp } else if (ie) {
1980 07fa9365 2023-03-10 stsp if (status != GOT_STATUS_DELETE &&
1981 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1982 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1983 07fa9365 2023-03-10 stsp err = remove_ondisk_file(worktree->root_path,
1984 07fa9365 2023-03-10 stsp ie->path);
1985 07fa9365 2023-03-10 stsp if (err && !(err->code == GOT_ERR_ERRNO &&
1986 07fa9365 2023-03-10 stsp errno == ENOENT))
1987 07fa9365 2023-03-10 stsp goto done;
1988 07fa9365 2023-03-10 stsp }
1989 07fa9365 2023-03-10 stsp got_fileindex_entry_remove(fileindex, ie);
1990 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1991 07fa9365 2023-03-10 stsp ie->path);
1992 07fa9365 2023-03-10 stsp }
1993 07fa9365 2023-03-10 stsp goto done; /* nothing else to do */
1994 5036ab18 2020-04-18 stsp }
1995 b8f41171 2019-02-10 stsp
1996 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1997 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1998 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1999 c6e8a826 2021-04-05 stsp /*
2000 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
2001 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
2002 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
2003 c6e8a826 2021-04-05 stsp * updating contents of this file.
2004 c6e8a826 2021-04-05 stsp */
2005 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
2006 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2007 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
2008 c6e8a826 2021-04-05 stsp /* Same commit. */
2009 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2010 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2011 e2b1e152 2019-07-13 stsp if (err)
2012 e2b1e152 2019-07-13 stsp goto done;
2013 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2014 b8f41171 2019-02-10 stsp path);
2015 6353ad76 2019-02-08 stsp goto done;
2016 a378724f 2019-02-10 stsp }
2017 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
2018 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
2019 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
2020 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2021 fd785a9a 2023-04-12 stsp if (got_fileindex_entry_has_commit(ie)) {
2022 fd785a9a 2023-04-12 stsp /* Update the base commit ID of this file. */
2023 fd785a9a 2023-04-12 stsp memcpy(ie->commit_sha1,
2024 fd785a9a 2023-04-12 stsp worktree->base_commit_id->sha1,
2025 fd785a9a 2023-04-12 stsp sizeof(ie->commit_sha1));
2026 fd785a9a 2023-04-12 stsp }
2027 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2028 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2029 0f58026f 2021-04-05 stsp if (err)
2030 0f58026f 2021-04-05 stsp goto done;
2031 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2032 0f58026f 2021-04-05 stsp path);
2033 b8f41171 2019-02-10 stsp goto done;
2034 e2b1e152 2019-07-13 stsp }
2035 9d31a1d8 2018-03-11 stsp }
2036 9d31a1d8 2018-03-11 stsp
2037 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
2038 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
2039 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2040 eb81bc23 2022-06-28 tracey goto done;
2041 eb81bc23 2022-06-28 tracey }
2042 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2043 8da9e5f4 2019-01-12 stsp if (err)
2044 6353ad76 2019-02-08 stsp goto done;
2045 512f0d0e 2019-01-02 stsp
2046 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2047 234035bc 2019-06-01 stsp int update_timestamps;
2048 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2049 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2050 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2051 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
2052 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
2053 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2054 eb81bc23 2022-06-28 tracey goto done;
2055 eb81bc23 2022-06-28 tracey }
2056 234035bc 2019-06-01 stsp struct got_object_id id2;
2057 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
2058 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2059 eb81bc23 2022-06-28 tracey fd2);
2060 234035bc 2019-06-01 stsp if (err)
2061 f69721c3 2019-10-21 stsp goto done;
2062 f69721c3 2019-10-21 stsp }
2063 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2064 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2065 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2066 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2067 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2068 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2069 f69721c3 2019-10-21 stsp goto done;
2070 f69721c3 2019-10-21 stsp }
2071 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2072 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2073 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2074 234035bc 2019-06-01 stsp goto done;
2075 f69721c3 2019-10-21 stsp }
2076 234035bc 2019-06-01 stsp }
2077 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2078 36bf999c 2020-07-23 stsp char *link_target;
2079 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2080 36bf999c 2020-07-23 stsp if (err)
2081 36bf999c 2020-07-23 stsp goto done;
2082 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2083 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2084 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2085 36bf999c 2020-07-23 stsp free(link_target);
2086 993e2a1b 2020-07-23 stsp } else {
2087 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2088 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2089 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2090 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2091 993e2a1b 2020-07-23 stsp }
2092 f69721c3 2019-10-21 stsp free(label_orig);
2093 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2094 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2095 eb81bc23 2022-06-28 tracey goto done;
2096 eb81bc23 2022-06-28 tracey }
2097 234035bc 2019-06-01 stsp if (blob2)
2098 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2099 909d120e 2019-09-22 stsp if (err)
2100 909d120e 2019-09-22 stsp goto done;
2101 234035bc 2019-06-01 stsp /*
2102 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2103 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2104 234035bc 2019-06-01 stsp * unmodified files again.
2105 234035bc 2019-06-01 stsp */
2106 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2108 234035bc 2019-06-01 stsp update_timestamps);
2109 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2110 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2111 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2112 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2113 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2114 1ee397ad 2019-07-12 stsp if (err)
2115 1ee397ad 2019-07-12 stsp goto done;
2116 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2117 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2118 13d9040b 2019-03-27 stsp if (err)
2119 13d9040b 2019-03-27 stsp goto done;
2120 13d9040b 2019-03-27 stsp } else {
2121 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2122 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2123 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2124 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2125 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2126 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2127 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2128 2e1fa222 2020-07-23 stsp } else {
2129 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2130 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2131 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2132 ef623445 2023-09-16 op status == GOT_STATUS_UNVERSIONED,
2133 ef623445 2023-09-16 op &update_timestamps,
2134 ef623445 2023-09-16 op repo, progress_cb, progress_arg);
2135 2e1fa222 2020-07-23 stsp }
2136 13d9040b 2019-03-27 stsp if (err)
2137 13d9040b 2019-03-27 stsp goto done;
2138 65b05cec 2020-07-23 stsp
2139 054041d0 2020-06-24 stsp if (ie) {
2140 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2141 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2142 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2143 054041d0 2020-06-24 stsp } else {
2144 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2145 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2146 ef623445 2023-09-16 op &blob->id, update_timestamps);
2147 054041d0 2020-06-24 stsp }
2148 13d9040b 2019-03-27 stsp if (err)
2149 13d9040b 2019-03-27 stsp goto done;
2150 65b05cec 2020-07-23 stsp
2151 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2152 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2153 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2154 2e1fa222 2020-07-23 stsp }
2155 13d9040b 2019-03-27 stsp }
2156 eb81bc23 2022-06-28 tracey
2157 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2158 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2159 eb81bc23 2022-06-28 tracey goto done;
2160 eb81bc23 2022-06-28 tracey }
2161 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2162 6353ad76 2019-02-08 stsp done:
2163 6353ad76 2019-02-08 stsp free(ondisk_path);
2164 8da9e5f4 2019-01-12 stsp return err;
2165 90285c3b 2019-01-08 stsp }
2166 90285c3b 2019-01-08 stsp
2167 90285c3b 2019-01-08 stsp static const struct got_error *
2168 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2169 90285c3b 2019-01-08 stsp {
2170 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2171 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2172 90285c3b 2019-01-08 stsp
2173 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2174 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2175 90285c3b 2019-01-08 stsp
2176 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2177 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2178 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2179 c97665ae 2019-01-13 stsp } else {
2180 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2181 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2182 1c4cdd89 2021-06-20 stsp if (err)
2183 1c4cdd89 2021-06-20 stsp goto done;
2184 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2185 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2186 fddefe3b 2020-10-20 stsp free(ondisk_path);
2187 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2188 1c4cdd89 2021-06-20 stsp parent = NULL;
2189 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2190 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2191 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2192 fddefe3b 2020-10-20 stsp ondisk_path);
2193 90285c3b 2019-01-08 stsp break;
2194 90285c3b 2019-01-08 stsp }
2195 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2196 1c4cdd89 2021-06-20 stsp if (err)
2197 1c4cdd89 2021-06-20 stsp break;
2198 1c4cdd89 2021-06-20 stsp }
2199 90285c3b 2019-01-08 stsp }
2200 1c4cdd89 2021-06-20 stsp done:
2201 90285c3b 2019-01-08 stsp free(ondisk_path);
2202 1c4cdd89 2021-06-20 stsp free(parent);
2203 90285c3b 2019-01-08 stsp return err;
2204 512f0d0e 2019-01-02 stsp }
2205 512f0d0e 2019-01-02 stsp
2206 708d8e67 2019-03-27 stsp static const struct got_error *
2207 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2208 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2209 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2210 708d8e67 2019-03-27 stsp {
2211 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2212 708d8e67 2019-03-27 stsp unsigned char status;
2213 708d8e67 2019-03-27 stsp struct stat sb;
2214 708d8e67 2019-03-27 stsp char *ondisk_path;
2215 708d8e67 2019-03-27 stsp
2216 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2217 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2218 a76c42e6 2019-08-03 stsp
2219 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2220 708d8e67 2019-03-27 stsp == -1)
2221 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2222 708d8e67 2019-03-27 stsp
2223 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2224 708d8e67 2019-03-27 stsp if (err)
2225 5a58a424 2020-06-23 stsp goto done;
2226 708d8e67 2019-03-27 stsp
2227 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2228 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2229 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2230 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2231 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2232 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2233 993e2a1b 2020-07-23 stsp goto done;
2234 993e2a1b 2020-07-23 stsp }
2235 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2236 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2237 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2238 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2239 993e2a1b 2020-07-23 stsp if (err)
2240 993e2a1b 2020-07-23 stsp goto done;
2241 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2242 993e2a1b 2020-07-23 stsp ie->path);
2243 993e2a1b 2020-07-23 stsp goto done;
2244 993e2a1b 2020-07-23 stsp }
2245 993e2a1b 2020-07-23 stsp
2246 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2247 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2248 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2249 1ee397ad 2019-07-12 stsp if (err)
2250 5a58a424 2020-06-23 stsp goto done;
2251 fc6346c4 2019-03-27 stsp /*
2252 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2253 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2254 fc6346c4 2019-03-27 stsp */
2255 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2256 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2257 fc6346c4 2019-03-27 stsp } else {
2258 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2259 1ee397ad 2019-07-12 stsp if (err)
2260 5a58a424 2020-06-23 stsp goto done;
2261 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2262 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2263 fc6346c4 2019-03-27 stsp if (err)
2264 5a58a424 2020-06-23 stsp goto done;
2265 fc6346c4 2019-03-27 stsp }
2266 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2267 708d8e67 2019-03-27 stsp }
2268 5a58a424 2020-06-23 stsp done:
2269 5a58a424 2020-06-23 stsp free(ondisk_path);
2270 fc6346c4 2019-03-27 stsp return err;
2271 708d8e67 2019-03-27 stsp }
2272 708d8e67 2019-03-27 stsp
2273 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2274 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2275 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2276 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2277 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2278 8da9e5f4 2019-01-12 stsp void *progress_arg;
2279 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2280 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2281 8da9e5f4 2019-01-12 stsp };
2282 8da9e5f4 2019-01-12 stsp
2283 512f0d0e 2019-01-02 stsp static const struct got_error *
2284 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2285 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2286 512f0d0e 2019-01-02 stsp {
2287 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2288 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2289 0584f854 2019-04-06 stsp
2290 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2291 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2292 f6b8c3c2 2023-07-24 stsp if (err)
2293 f6b8c3c2 2023-07-24 stsp return err;
2294 f6b8c3c2 2023-07-24 stsp }
2295 512f0d0e 2019-01-02 stsp
2296 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2297 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2298 8da9e5f4 2019-01-12 stsp }
2299 512f0d0e 2019-01-02 stsp
2300 8da9e5f4 2019-01-12 stsp static const struct got_error *
2301 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2302 8da9e5f4 2019-01-12 stsp {
2303 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2304 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2305 7a9df742 2019-01-08 stsp
2306 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2307 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2308 f6b8c3c2 2023-07-24 stsp if (err)
2309 f6b8c3c2 2023-07-24 stsp return err;
2310 f6b8c3c2 2023-07-24 stsp }
2311 0584f854 2019-04-06 stsp
2312 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2313 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2314 9d31a1d8 2018-03-11 stsp }
2315 9d31a1d8 2018-03-11 stsp
2316 9d31a1d8 2018-03-11 stsp static const struct got_error *
2317 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2318 9d31a1d8 2018-03-11 stsp {
2319 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
2320 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2321 8da9e5f4 2019-01-12 stsp char *path;
2322 9d31a1d8 2018-03-11 stsp
2323 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
2324 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
2325 f6b8c3c2 2023-07-24 stsp if (err)
2326 f6b8c3c2 2023-07-24 stsp return err;
2327 f6b8c3c2 2023-07-24 stsp }
2328 0584f854 2019-04-06 stsp
2329 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2330 6a390967 2023-07-14 stsp return NULL;
2331 6a390967 2023-07-14 stsp
2332 6a390967 2023-07-14 stsp if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2333 63c5ca5d 2019-08-24 stsp return NULL;
2334 63c5ca5d 2019-08-24 stsp
2335 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2336 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2337 8da9e5f4 2019-01-12 stsp == -1)
2338 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2339 9d31a1d8 2018-03-11 stsp
2340 6a390967 2023-07-14 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2341 6a390967 2023-07-14 stsp a->repo, a->progress_cb, a->progress_arg);
2342 9d31a1d8 2018-03-11 stsp
2343 8da9e5f4 2019-01-12 stsp free(path);
2344 0cd1c46a 2019-03-11 stsp return err;
2345 0cd1c46a 2019-03-11 stsp }
2346 0cd1c46a 2019-03-11 stsp
2347 b2118c49 2020-07-28 stsp const struct got_error *
2348 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2349 b2118c49 2020-07-28 stsp {
2350 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2351 b2118c49 2020-07-28 stsp
2352 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2353 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2354 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2355 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2356 b2118c49 2020-07-28 stsp }
2357 b2118c49 2020-07-28 stsp
2358 b2118c49 2020-07-28 stsp return NULL;
2359 b2118c49 2020-07-28 stsp }
2360 b2118c49 2020-07-28 stsp
2361 818c7501 2019-07-11 stsp static const struct got_error *
2362 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2363 0cd1c46a 2019-03-11 stsp {
2364 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2365 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2366 0cd1c46a 2019-03-11 stsp
2367 517bab73 2019-03-11 stsp *refname = NULL;
2368 517bab73 2019-03-11 stsp
2369 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2370 b2118c49 2020-07-28 stsp if (err)
2371 b2118c49 2020-07-28 stsp return err;
2372 0cd1c46a 2019-03-11 stsp
2373 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2374 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2375 0647c563 2019-03-11 stsp *refname = NULL;
2376 0cd1c46a 2019-03-11 stsp }
2377 517bab73 2019-03-11 stsp free(uuidstr);
2378 517bab73 2019-03-11 stsp return err;
2379 517bab73 2019-03-11 stsp }
2380 517bab73 2019-03-11 stsp
2381 818c7501 2019-07-11 stsp const struct got_error *
2382 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2383 9587e6cc 2023-01-28 mark const char *prefix)
2384 9587e6cc 2023-01-28 mark {
2385 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2386 9587e6cc 2023-01-28 mark }
2387 9587e6cc 2023-01-28 mark
2388 5662d61d 2023-07-03 jrick static const struct got_error *
2389 5662d61d 2023-07-03 jrick get_base_ref_name(char **refname, struct got_worktree *worktree)
2390 818c7501 2019-07-11 stsp {
2391 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2392 818c7501 2019-07-11 stsp }
2393 818c7501 2019-07-11 stsp
2394 818c7501 2019-07-11 stsp static const struct got_error *
2395 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2396 818c7501 2019-07-11 stsp {
2397 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2398 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2399 818c7501 2019-07-11 stsp }
2400 818c7501 2019-07-11 stsp
2401 818c7501 2019-07-11 stsp static const struct got_error *
2402 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2403 818c7501 2019-07-11 stsp {
2404 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2405 818c7501 2019-07-11 stsp }
2406 818c7501 2019-07-11 stsp
2407 818c7501 2019-07-11 stsp static const struct got_error *
2408 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2409 818c7501 2019-07-11 stsp {
2410 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2411 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2412 818c7501 2019-07-11 stsp }
2413 818c7501 2019-07-11 stsp
2414 818c7501 2019-07-11 stsp static const struct got_error *
2415 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2416 818c7501 2019-07-11 stsp {
2417 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2418 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2419 0ebf8283 2019-07-24 stsp }
2420 0ebf8283 2019-07-24 stsp
2421 0ebf8283 2019-07-24 stsp static const struct got_error *
2422 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2423 0ebf8283 2019-07-24 stsp {
2424 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2425 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2426 0ebf8283 2019-07-24 stsp }
2427 0ebf8283 2019-07-24 stsp
2428 0ebf8283 2019-07-24 stsp static const struct got_error *
2429 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2430 0ebf8283 2019-07-24 stsp {
2431 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2432 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2433 0ebf8283 2019-07-24 stsp }
2434 0ebf8283 2019-07-24 stsp
2435 0ebf8283 2019-07-24 stsp static const struct got_error *
2436 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2437 0ebf8283 2019-07-24 stsp {
2438 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2439 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2440 818c7501 2019-07-11 stsp }
2441 818c7501 2019-07-11 stsp
2442 0ebf8283 2019-07-24 stsp static const struct got_error *
2443 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2444 0ebf8283 2019-07-24 stsp {
2445 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2446 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2447 0ebf8283 2019-07-24 stsp }
2448 818c7501 2019-07-11 stsp
2449 0ebf8283 2019-07-24 stsp const struct got_error *
2450 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2451 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2452 0ebf8283 2019-07-24 stsp {
2453 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2454 df6221c7 2023-07-19 stsp worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2455 0ebf8283 2019-07-24 stsp *path = NULL;
2456 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2457 0ebf8283 2019-07-24 stsp }
2458 0ebf8283 2019-07-24 stsp return NULL;
2459 f259c4c1 2021-09-24 stsp }
2460 f259c4c1 2021-09-24 stsp
2461 f259c4c1 2021-09-24 stsp static const struct got_error *
2462 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2463 f259c4c1 2021-09-24 stsp {
2464 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2465 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2466 0ebf8283 2019-07-24 stsp }
2467 0ebf8283 2019-07-24 stsp
2468 f259c4c1 2021-09-24 stsp static const struct got_error *
2469 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2470 f259c4c1 2021-09-24 stsp {
2471 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2472 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2473 f259c4c1 2021-09-24 stsp }
2474 f259c4c1 2021-09-24 stsp
2475 517bab73 2019-03-11 stsp /*
2476 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2477 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2478 517bab73 2019-03-11 stsp */
2479 517bab73 2019-03-11 stsp static const struct got_error *
2480 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2481 517bab73 2019-03-11 stsp {
2482 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2483 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2484 517bab73 2019-03-11 stsp char *refname;
2485 517bab73 2019-03-11 stsp
2486 5662d61d 2023-07-03 jrick err = get_base_ref_name(&refname, worktree);
2487 517bab73 2019-03-11 stsp if (err)
2488 517bab73 2019-03-11 stsp return err;
2489 0cd1c46a 2019-03-11 stsp
2490 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2491 0cd1c46a 2019-03-11 stsp if (err)
2492 0cd1c46a 2019-03-11 stsp goto done;
2493 0cd1c46a 2019-03-11 stsp
2494 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2495 0cd1c46a 2019-03-11 stsp done:
2496 0cd1c46a 2019-03-11 stsp free(refname);
2497 0cd1c46a 2019-03-11 stsp if (ref)
2498 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2499 3e3a69f1 2019-07-25 stsp return err;
2500 3e3a69f1 2019-07-25 stsp }
2501 3e3a69f1 2019-07-25 stsp
2502 3e3a69f1 2019-07-25 stsp static const struct got_error *
2503 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2504 3e3a69f1 2019-07-25 stsp {
2505 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2506 3e3a69f1 2019-07-25 stsp
2507 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2508 df6221c7 2023-07-19 stsp worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2509 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2510 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2511 3e3a69f1 2019-07-25 stsp }
2512 9d31a1d8 2018-03-11 stsp return err;
2513 9d31a1d8 2018-03-11 stsp }
2514 9d31a1d8 2018-03-11 stsp
2515 3e3a69f1 2019-07-25 stsp
2516 ebf99748 2019-05-09 stsp static const struct got_error *
2517 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2518 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2519 ebf99748 2019-05-09 stsp {
2520 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2521 ebf99748 2019-05-09 stsp FILE *index = NULL;
2522 0cd1c46a 2019-03-11 stsp
2523 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2524 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2525 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2526 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2527 ebf99748 2019-05-09 stsp
2528 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2529 3e3a69f1 2019-07-25 stsp if (err)
2530 ebf99748 2019-05-09 stsp goto done;
2531 ebf99748 2019-05-09 stsp
2532 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2533 ebf99748 2019-05-09 stsp if (index == NULL) {
2534 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2535 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2536 ebf99748 2019-05-09 stsp } else {
2537 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2538 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2539 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2540 ebf99748 2019-05-09 stsp }
2541 ebf99748 2019-05-09 stsp done:
2542 ebf99748 2019-05-09 stsp if (err) {
2543 ebf99748 2019-05-09 stsp free(*fileindex_path);
2544 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2545 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2546 ebf99748 2019-05-09 stsp *fileindex = NULL;
2547 ebf99748 2019-05-09 stsp }
2548 ebf99748 2019-05-09 stsp return err;
2549 ebf99748 2019-05-09 stsp }
2550 c932eeeb 2019-05-22 stsp
2551 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2552 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2553 c932eeeb 2019-05-22 stsp const char *path;
2554 c932eeeb 2019-05-22 stsp size_t path_len;
2555 c932eeeb 2019-05-22 stsp const char *entry_name;
2556 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2557 a484d721 2019-06-10 stsp void *progress_arg;
2558 c932eeeb 2019-05-22 stsp };
2559 ebf99748 2019-05-09 stsp
2560 c932eeeb 2019-05-22 stsp static const struct got_error *
2561 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2562 c932eeeb 2019-05-22 stsp {
2563 1ee397ad 2019-07-12 stsp const struct got_error *err;
2564 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2565 c932eeeb 2019-05-22 stsp
2566 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2567 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2568 c932eeeb 2019-05-22 stsp return NULL;
2569 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2570 102ff934 2019-06-11 stsp return NULL;
2571 102ff934 2019-06-11 stsp
2572 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2573 a769b60b 2021-06-27 stsp return NULL;
2574 a769b60b 2021-06-27 stsp
2575 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2576 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2577 c932eeeb 2019-05-22 stsp return NULL;
2578 c932eeeb 2019-05-22 stsp
2579 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2580 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2581 edd02c5e 2019-07-11 stsp ie->path);
2582 1ee397ad 2019-07-12 stsp if (err)
2583 1ee397ad 2019-07-12 stsp return err;
2584 1ee397ad 2019-07-12 stsp }
2585 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2586 c932eeeb 2019-05-22 stsp return NULL;
2587 a615e0e7 2020-12-16 stsp }
2588 a615e0e7 2020-12-16 stsp
2589 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2590 a615e0e7 2020-12-16 stsp static const struct got_error *
2591 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2592 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2593 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2594 a615e0e7 2020-12-16 stsp {
2595 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2596 a615e0e7 2020-12-16 stsp
2597 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2598 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2599 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2600 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2601 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2602 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2603 a615e0e7 2020-12-16 stsp
2604 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2605 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2606 9c6338c4 2019-06-02 stsp }
2607 9c6338c4 2019-06-02 stsp
2608 9c6338c4 2019-06-02 stsp static const struct got_error *
2609 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2610 9c6338c4 2019-06-02 stsp {
2611 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2612 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2613 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2614 867630bb 2020-01-17 stsp struct timespec timeout;
2615 9c6338c4 2019-06-02 stsp
2616 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2617 b90054ed 2022-11-01 stsp fileindex_path, "");
2618 9c6338c4 2019-06-02 stsp if (err)
2619 9c6338c4 2019-06-02 stsp goto done;
2620 9c6338c4 2019-06-02 stsp
2621 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2622 9c6338c4 2019-06-02 stsp if (err)
2623 9c6338c4 2019-06-02 stsp goto done;
2624 9c6338c4 2019-06-02 stsp
2625 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2626 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2627 9c6338c4 2019-06-02 stsp fileindex_path);
2628 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2629 9c6338c4 2019-06-02 stsp }
2630 867630bb 2020-01-17 stsp
2631 867630bb 2020-01-17 stsp /*
2632 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2633 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2634 867630bb 2020-01-17 stsp * was recorded in the file index.
2635 867630bb 2020-01-17 stsp */
2636 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2637 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2638 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2639 9c6338c4 2019-06-02 stsp done:
2640 9c6338c4 2019-06-02 stsp if (new_index)
2641 9c6338c4 2019-06-02 stsp fclose(new_index);
2642 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2643 9c6338c4 2019-06-02 stsp return err;
2644 c932eeeb 2019-05-22 stsp }
2645 6ced4176 2019-07-12 stsp
2646 6ced4176 2019-07-12 stsp static const struct got_error *
2647 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2648 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2649 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2650 a44927cc 2022-04-07 stsp struct got_repository *repo)
2651 6ced4176 2019-07-12 stsp {
2652 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2653 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2654 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2655 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2656 6ced4176 2019-07-12 stsp
2657 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2658 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2659 6ced4176 2019-07-12 stsp *tree_id = NULL;
2660 6ced4176 2019-07-12 stsp
2661 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2662 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2663 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2664 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2665 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2666 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2667 6ced4176 2019-07-12 stsp goto done;
2668 6ced4176 2019-07-12 stsp }
2669 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2670 a44927cc 2022-04-07 stsp worktree->path_prefix);
2671 6ced4176 2019-07-12 stsp if (err)
2672 6ced4176 2019-07-12 stsp goto done;
2673 6ced4176 2019-07-12 stsp return NULL;
2674 6ced4176 2019-07-12 stsp }
2675 6ced4176 2019-07-12 stsp
2676 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2677 6ced4176 2019-07-12 stsp
2678 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2679 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2680 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2681 6ced4176 2019-07-12 stsp goto done;
2682 6ced4176 2019-07-12 stsp }
2683 6ced4176 2019-07-12 stsp
2684 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2685 6ced4176 2019-07-12 stsp if (err)
2686 6ced4176 2019-07-12 stsp goto done;
2687 6ced4176 2019-07-12 stsp
2688 6ced4176 2019-07-12 stsp free(in_repo_path);
2689 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2690 6ced4176 2019-07-12 stsp
2691 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2692 6ced4176 2019-07-12 stsp if (err)
2693 6ced4176 2019-07-12 stsp goto done;
2694 c932eeeb 2019-05-22 stsp
2695 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2696 6ced4176 2019-07-12 stsp /* Check out a single file. */
2697 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2698 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2699 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2700 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2701 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2702 6ced4176 2019-07-12 stsp goto done;
2703 6ced4176 2019-07-12 stsp }
2704 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2705 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2706 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2707 6ced4176 2019-07-12 stsp goto done;
2708 6ced4176 2019-07-12 stsp }
2709 6ced4176 2019-07-12 stsp } else {
2710 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2711 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2712 6ced4176 2019-07-12 stsp if (err)
2713 6ced4176 2019-07-12 stsp return err;
2714 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2715 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2716 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2717 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2718 6ced4176 2019-07-12 stsp goto done;
2719 6ced4176 2019-07-12 stsp }
2720 6ced4176 2019-07-12 stsp }
2721 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2722 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2723 6ced4176 2019-07-12 stsp } else {
2724 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2725 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2726 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2727 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2728 6ced4176 2019-07-12 stsp goto done;
2729 6ced4176 2019-07-12 stsp }
2730 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2731 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2732 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2733 6ced4176 2019-07-12 stsp goto done;
2734 6ced4176 2019-07-12 stsp }
2735 6ced4176 2019-07-12 stsp }
2736 6ced4176 2019-07-12 stsp done:
2737 6ced4176 2019-07-12 stsp free(id);
2738 6ced4176 2019-07-12 stsp free(in_repo_path);
2739 6ced4176 2019-07-12 stsp if (err) {
2740 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2741 6ced4176 2019-07-12 stsp free(*tree_relpath);
2742 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2743 6ced4176 2019-07-12 stsp free(*tree_id);
2744 6ced4176 2019-07-12 stsp *tree_id = NULL;
2745 6ced4176 2019-07-12 stsp }
2746 07ed472d 2019-07-12 stsp return err;
2747 07ed472d 2019-07-12 stsp }
2748 07ed472d 2019-07-12 stsp
2749 07ed472d 2019-07-12 stsp static const struct got_error *
2750 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2751 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2752 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2753 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2754 07ed472d 2019-07-12 stsp {
2755 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2756 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2757 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2758 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2759 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2760 07ed472d 2019-07-12 stsp
2761 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2762 7f47418f 2019-12-20 stsp if (err) {
2763 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2764 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2765 7f47418f 2019-12-20 stsp goto done;
2766 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2767 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2768 7f47418f 2019-12-20 stsp if (err)
2769 7f47418f 2019-12-20 stsp return err;
2770 7f47418f 2019-12-20 stsp }
2771 07ed472d 2019-07-12 stsp
2772 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2773 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2774 07ed472d 2019-07-12 stsp if (err)
2775 07ed472d 2019-07-12 stsp goto done;
2776 07ed472d 2019-07-12 stsp
2777 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2778 07ed472d 2019-07-12 stsp if (err)
2779 07ed472d 2019-07-12 stsp goto done;
2780 07ed472d 2019-07-12 stsp
2781 07ed472d 2019-07-12 stsp if (entry_name &&
2782 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2783 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2784 07ed472d 2019-07-12 stsp goto done;
2785 07ed472d 2019-07-12 stsp }
2786 07ed472d 2019-07-12 stsp
2787 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2788 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2789 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2790 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2791 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2792 07ed472d 2019-07-12 stsp arg.repo = repo;
2793 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2794 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2795 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2796 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2797 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2798 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2799 07ed472d 2019-07-12 stsp done:
2800 07ed472d 2019-07-12 stsp if (tree)
2801 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2802 07ed472d 2019-07-12 stsp if (commit)
2803 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2804 6ced4176 2019-07-12 stsp return err;
2805 6ced4176 2019-07-12 stsp }
2806 6ced4176 2019-07-12 stsp
2807 9d31a1d8 2018-03-11 stsp const struct got_error *
2808 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2809 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2810 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2811 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2812 9d31a1d8 2018-03-11 stsp {
2813 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2814 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2815 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2816 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2817 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2818 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2819 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2820 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2821 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2822 f2ea84fa 2019-07-27 stsp int entry_type;
2823 f2ea84fa 2019-07-27 stsp char *relpath;
2824 f2ea84fa 2019-07-27 stsp char *entry_name;
2825 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2826 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2827 9d31a1d8 2018-03-11 stsp
2828 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2829 f2ea84fa 2019-07-27 stsp
2830 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2831 9d31a1d8 2018-03-11 stsp if (err)
2832 9d31a1d8 2018-03-11 stsp return err;
2833 a44927cc 2022-04-07 stsp
2834 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2835 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2836 a44927cc 2022-04-07 stsp if (err)
2837 a44927cc 2022-04-07 stsp goto done;
2838 93a30277 2018-12-24 stsp
2839 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2840 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2841 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2842 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2843 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2844 f2ea84fa 2019-07-27 stsp goto done;
2845 f2ea84fa 2019-07-27 stsp }
2846 6ced4176 2019-07-12 stsp
2847 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2848 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2849 a44927cc 2022-04-07 stsp worktree, repo);
2850 f2ea84fa 2019-07-27 stsp if (err) {
2851 f2ea84fa 2019-07-27 stsp free(tpd);
2852 6ced4176 2019-07-12 stsp goto done;
2853 6ced4176 2019-07-12 stsp }
2854 f2ea84fa 2019-07-27 stsp
2855 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2856 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2857 f2ea84fa 2019-07-27 stsp if (err) {
2858 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2859 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2860 f2ea84fa 2019-07-27 stsp free(tpd);
2861 f2ea84fa 2019-07-27 stsp goto done;
2862 f2ea84fa 2019-07-27 stsp }
2863 f2ea84fa 2019-07-27 stsp } else
2864 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2865 f2ea84fa 2019-07-27 stsp
2866 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2867 6ced4176 2019-07-12 stsp }
2868 6ced4176 2019-07-12 stsp
2869 51514078 2018-12-25 stsp /*
2870 51514078 2018-12-25 stsp * Read the file index.
2871 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2872 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2873 51514078 2018-12-25 stsp */
2874 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2875 8da9e5f4 2019-01-12 stsp if (err)
2876 c4cdcb68 2019-04-03 stsp goto done;
2877 c4cdcb68 2019-04-03 stsp
2878 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2879 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2880 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2881 f2ea84fa 2019-07-27 stsp
2882 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2883 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2884 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2885 f2ea84fa 2019-07-27 stsp if (err)
2886 f2ea84fa 2019-07-27 stsp break;
2887 f2ea84fa 2019-07-27 stsp
2888 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2889 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2890 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2891 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2892 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2893 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2894 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2895 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2896 f2ea84fa 2019-07-27 stsp if (err)
2897 f2ea84fa 2019-07-27 stsp break;
2898 f2ea84fa 2019-07-27 stsp
2899 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2900 711d9cd9 2019-07-12 stsp }
2901 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2902 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2903 af12c6b9 2019-06-04 stsp err = sync_err;
2904 9d31a1d8 2018-03-11 stsp done:
2905 9c6338c4 2019-06-02 stsp free(fileindex_path);
2906 edfa7d7f 2018-09-11 stsp if (tree)
2907 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2908 9d31a1d8 2018-03-11 stsp if (commit)
2909 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2910 5ade8233 2019-07-12 stsp if (fileindex)
2911 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2912 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2913 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2914 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2915 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2916 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2917 f2ea84fa 2019-07-27 stsp free(tpd);
2918 f2ea84fa 2019-07-27 stsp }
2919 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2920 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2921 9d31a1d8 2018-03-11 stsp err = unlockerr;
2922 f8d1f275 2019-02-04 stsp return err;
2923 f8d1f275 2019-02-04 stsp }
2924 f8d1f275 2019-02-04 stsp
2925 9a298e5c 2023-03-10 stsp static const struct got_error *
2926 9a298e5c 2023-03-10 stsp add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2927 9a298e5c 2023-03-10 stsp struct got_fileindex_entry *ie, const char *ondisk_path,
2928 9a298e5c 2023-03-10 stsp const char *path2, struct got_blob_object *blob2, mode_t mode2,
2929 9a298e5c 2023-03-10 stsp int restoring_missing_file, int reverting_versioned_file,
2930 9a298e5c 2023-03-10 stsp int path_is_unversioned, int allow_bad_symlinks,
2931 9a298e5c 2023-03-10 stsp struct got_repository *repo,
2932 9a298e5c 2023-03-10 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2933 9a298e5c 2023-03-10 stsp {
2934 9a298e5c 2023-03-10 stsp const struct got_error *err = NULL;
2935 9a298e5c 2023-03-10 stsp int is_bad_symlink = 0;
2936 9a298e5c 2023-03-10 stsp
2937 9a298e5c 2023-03-10 stsp if (S_ISLNK(mode2)) {
2938 9a298e5c 2023-03-10 stsp err = install_symlink(&is_bad_symlink,
2939 9a298e5c 2023-03-10 stsp worktree, ondisk_path, path2, blob2,
2940 9a298e5c 2023-03-10 stsp restoring_missing_file,
2941 9a298e5c 2023-03-10 stsp reverting_versioned_file,
2942 9a298e5c 2023-03-10 stsp path_is_unversioned, allow_bad_symlinks,
2943 9a298e5c 2023-03-10 stsp repo, progress_cb, progress_arg);
2944 9a298e5c 2023-03-10 stsp } else {
2945 9a298e5c 2023-03-10 stsp err = install_blob(worktree, ondisk_path, path2,
2946 9a298e5c 2023-03-10 stsp mode2, GOT_DEFAULT_FILE_MODE, blob2,
2947 9a298e5c 2023-03-10 stsp restoring_missing_file, reverting_versioned_file, 0,
2948 ef623445 2023-09-16 op path_is_unversioned, NULL, repo,
2949 ef623445 2023-09-16 op progress_cb, progress_arg);
2950 9a298e5c 2023-03-10 stsp }
2951 9a298e5c 2023-03-10 stsp if (err)
2952 9a298e5c 2023-03-10 stsp return err;
2953 9a298e5c 2023-03-10 stsp if (ie == NULL) {
2954 9a298e5c 2023-03-10 stsp /* Adding an unversioned file. */
2955 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_alloc(&ie, path2);
2956 9a298e5c 2023-03-10 stsp if (err)
2957 9a298e5c 2023-03-10 stsp return err;
2958 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2959 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, NULL, NULL, 1);
2960 9a298e5c 2023-03-10 stsp if (err) {
2961 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2962 9a298e5c 2023-03-10 stsp return err;
2963 9a298e5c 2023-03-10 stsp }
2964 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_add(fileindex, ie);
2965 9a298e5c 2023-03-10 stsp if (err) {
2966 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2967 9a298e5c 2023-03-10 stsp return err;
2968 9a298e5c 2023-03-10 stsp }
2969 9a298e5c 2023-03-10 stsp } else {
2970 9a298e5c 2023-03-10 stsp /* Re-adding a locally deleted file. */
2971 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2972 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, ie->blob_sha1,
2973 9a298e5c 2023-03-10 stsp worktree->base_commit_id->sha1, 0);
2974 9a298e5c 2023-03-10 stsp if (err)
2975 9a298e5c 2023-03-10 stsp return err;
2976 9a298e5c 2023-03-10 stsp }
2977 9a298e5c 2023-03-10 stsp
2978 9a298e5c 2023-03-10 stsp if (is_bad_symlink) {
2979 9a298e5c 2023-03-10 stsp got_fileindex_entry_filetype_set(ie,
2980 9a298e5c 2023-03-10 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2981 9a298e5c 2023-03-10 stsp }
2982 9a298e5c 2023-03-10 stsp
2983 9a298e5c 2023-03-10 stsp return NULL;
2984 9a298e5c 2023-03-10 stsp }
2985 9a298e5c 2023-03-10 stsp
2986 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2987 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2988 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2989 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2990 234035bc 2019-06-01 stsp void *progress_arg;
2991 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2992 234035bc 2019-06-01 stsp void *cancel_arg;
2993 f69721c3 2019-10-21 stsp const char *label_orig;
2994 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2995 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2996 234035bc 2019-06-01 stsp };
2997 234035bc 2019-06-01 stsp
2998 234035bc 2019-06-01 stsp static const struct got_error *
2999 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
3000 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3001 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3002 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3003 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3004 234035bc 2019-06-01 stsp {
3005 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
3006 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
3007 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
3008 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
3009 234035bc 2019-06-01 stsp struct stat sb;
3010 234035bc 2019-06-01 stsp unsigned char status;
3011 234035bc 2019-06-01 stsp int local_changes_subsumed;
3012 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3013 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
3014 c2ba7aa6 2023-07-26 stsp struct got_object_id *id = NULL;
3015 234035bc 2019-06-01 stsp
3016 234035bc 2019-06-01 stsp if (blob1 && blob2) {
3017 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3018 d6c87207 2019-08-02 stsp strlen(path2));
3019 1ee397ad 2019-07-12 stsp if (ie == NULL)
3020 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3021 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
3022 234035bc 2019-06-01 stsp
3023 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3024 234035bc 2019-06-01 stsp path2) == -1)
3025 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3026 234035bc 2019-06-01 stsp
3027 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3028 7f91a133 2019-12-13 stsp repo);
3029 234035bc 2019-06-01 stsp if (err)
3030 234035bc 2019-06-01 stsp goto done;
3031 234035bc 2019-06-01 stsp
3032 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3033 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3034 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3035 234035bc 2019-06-01 stsp goto done;
3036 234035bc 2019-06-01 stsp }
3037 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3038 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3039 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3040 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3041 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3042 234035bc 2019-06-01 stsp goto done;
3043 234035bc 2019-06-01 stsp }
3044 234035bc 2019-06-01 stsp
3045 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3046 36bf999c 2020-07-23 stsp char *link_target2;
3047 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3048 36bf999c 2020-07-23 stsp if (err)
3049 36bf999c 2020-07-23 stsp goto done;
3050 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3051 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3052 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3053 36bf999c 2020-07-23 stsp free(link_target2);
3054 af57b12a 2020-07-23 stsp } else {
3055 1af628f4 2021-06-03 stsp int fd;
3056 1af628f4 2021-06-03 stsp
3057 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3058 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3059 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3060 1af628f4 2021-06-03 stsp goto done;
3061 1af628f4 2021-06-03 stsp }
3062 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3063 1af628f4 2021-06-03 stsp f_orig, blob1);
3064 1af628f4 2021-06-03 stsp if (err)
3065 1af628f4 2021-06-03 stsp goto done;
3066 1af628f4 2021-06-03 stsp
3067 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3068 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3069 1af628f4 2021-06-03 stsp goto done;
3070 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3071 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3072 1af628f4 2021-06-03 stsp if (err)
3073 1af628f4 2021-06-03 stsp goto done;
3074 1af628f4 2021-06-03 stsp
3075 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
3076 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3077 1af628f4 2021-06-03 stsp if (fd == -1) {
3078 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3079 1af628f4 2021-06-03 stsp ondisk_path);
3080 1af628f4 2021-06-03 stsp goto done;
3081 1af628f4 2021-06-03 stsp }
3082 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3083 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3084 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3085 1af628f4 2021-06-03 stsp ondisk_path);
3086 1af628f4 2021-06-03 stsp close(fd);
3087 1af628f4 2021-06-03 stsp goto done;
3088 1af628f4 2021-06-03 stsp }
3089 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3090 1af628f4 2021-06-03 stsp if (err)
3091 1af628f4 2021-06-03 stsp goto done;
3092 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3093 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3094 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3095 1af628f4 2021-06-03 stsp goto done;
3096 1af628f4 2021-06-03 stsp }
3097 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3098 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3099 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
3100 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3101 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3102 af57b12a 2020-07-23 stsp }
3103 234035bc 2019-06-01 stsp } else if (blob1) {
3104 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3105 d6c87207 2019-08-02 stsp strlen(path1));
3106 1ee397ad 2019-07-12 stsp if (ie == NULL)
3107 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3108 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3109 2b92fad7 2019-06-02 stsp
3110 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3111 2b92fad7 2019-06-02 stsp path1) == -1)
3112 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3113 2b92fad7 2019-06-02 stsp
3114 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3115 7f91a133 2019-12-13 stsp repo);
3116 2b92fad7 2019-06-02 stsp if (err)
3117 2b92fad7 2019-06-02 stsp goto done;
3118 2b92fad7 2019-06-02 stsp
3119 2b92fad7 2019-06-02 stsp switch (status) {
3120 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3121 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3122 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3123 1ee397ad 2019-07-12 stsp if (err)
3124 1ee397ad 2019-07-12 stsp goto done;
3125 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3126 2b92fad7 2019-06-02 stsp if (err)
3127 2b92fad7 2019-06-02 stsp goto done;
3128 2b92fad7 2019-06-02 stsp if (ie)
3129 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3130 2b92fad7 2019-06-02 stsp break;
3131 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3132 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3133 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3134 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3135 1ee397ad 2019-07-12 stsp if (err)
3136 1ee397ad 2019-07-12 stsp goto done;
3137 2b92fad7 2019-06-02 stsp if (ie)
3138 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3139 2b92fad7 2019-06-02 stsp break;
3140 c2ba7aa6 2023-07-26 stsp case GOT_STATUS_MODIFY: {
3141 c2ba7aa6 2023-07-26 stsp FILE *blob1_f;
3142 c2ba7aa6 2023-07-26 stsp off_t blob1_size;
3143 c2ba7aa6 2023-07-26 stsp int obj_type;
3144 c2ba7aa6 2023-07-26 stsp /*
3145 c2ba7aa6 2023-07-26 stsp * Delete the file only if its content already
3146 c2ba7aa6 2023-07-26 stsp * exists in the repository.
3147 c2ba7aa6 2023-07-26 stsp */
3148 c2ba7aa6 2023-07-26 stsp err = got_object_blob_file_create(&id, &blob1_f,
3149 c2ba7aa6 2023-07-26 stsp &blob1_size, path1);
3150 c2ba7aa6 2023-07-26 stsp if (err)
3151 c2ba7aa6 2023-07-26 stsp goto done;
3152 c2ba7aa6 2023-07-26 stsp if (fclose(blob1_f) == EOF) {
3153 c2ba7aa6 2023-07-26 stsp err = got_error_from_errno("fclose");
3154 c2ba7aa6 2023-07-26 stsp goto done;
3155 c2ba7aa6 2023-07-26 stsp }
3156 c2ba7aa6 2023-07-26 stsp
3157 c2ba7aa6 2023-07-26 stsp /* Implied existence check. */
3158 c2ba7aa6 2023-07-26 stsp err = got_object_get_type(&obj_type, repo, id);
3159 c2ba7aa6 2023-07-26 stsp if (err) {
3160 c2ba7aa6 2023-07-26 stsp if (err->code != GOT_ERR_NO_OBJ)
3161 c2ba7aa6 2023-07-26 stsp goto done;
3162 c2ba7aa6 2023-07-26 stsp err = (*a->progress_cb)(a->progress_arg,
3163 c2ba7aa6 2023-07-26 stsp GOT_STATUS_CANNOT_DELETE, path1);
3164 c2ba7aa6 2023-07-26 stsp goto done;
3165 c2ba7aa6 2023-07-26 stsp } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3166 c2ba7aa6 2023-07-26 stsp err = (*a->progress_cb)(a->progress_arg,
3167 c2ba7aa6 2023-07-26 stsp GOT_STATUS_CANNOT_DELETE, path1);
3168 c2ba7aa6 2023-07-26 stsp goto done;
3169 c2ba7aa6 2023-07-26 stsp }
3170 c2ba7aa6 2023-07-26 stsp err = (*a->progress_cb)(a->progress_arg,
3171 c2ba7aa6 2023-07-26 stsp GOT_STATUS_DELETE, path1);
3172 c2ba7aa6 2023-07-26 stsp if (err)
3173 c2ba7aa6 2023-07-26 stsp goto done;
3174 c2ba7aa6 2023-07-26 stsp err = remove_ondisk_file(a->worktree->root_path,
3175 c2ba7aa6 2023-07-26 stsp path1);
3176 c2ba7aa6 2023-07-26 stsp if (err)
3177 c2ba7aa6 2023-07-26 stsp goto done;
3178 c2ba7aa6 2023-07-26 stsp if (ie)
3179 c2ba7aa6 2023-07-26 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3180 c2ba7aa6 2023-07-26 stsp break;
3181 c2ba7aa6 2023-07-26 stsp }
3182 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3183 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3184 72840534 2022-01-19 stsp off_t blob1_size;
3185 0a22ca1a 2020-09-23 stsp /*
3186 c2ba7aa6 2023-07-26 stsp * Delete the file only if its content already
3187 0a22ca1a 2020-09-23 stsp * exists in the repository.
3188 0a22ca1a 2020-09-23 stsp */
3189 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
3190 72840534 2022-01-19 stsp &blob1_size, path1);
3191 0a22ca1a 2020-09-23 stsp if (err)
3192 0a22ca1a 2020-09-23 stsp goto done;
3193 c2ba7aa6 2023-07-26 stsp if (fclose(blob1_f) == EOF) {
3194 c2ba7aa6 2023-07-26 stsp err = got_error_from_errno("fclose");
3195 c2ba7aa6 2023-07-26 stsp goto done;
3196 c2ba7aa6 2023-07-26 stsp }
3197 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3198 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3199 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3200 0a22ca1a 2020-09-23 stsp if (err)
3201 0a22ca1a 2020-09-23 stsp goto done;
3202 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3203 0a22ca1a 2020-09-23 stsp path1);
3204 0a22ca1a 2020-09-23 stsp if (err)
3205 0a22ca1a 2020-09-23 stsp goto done;
3206 0a22ca1a 2020-09-23 stsp if (ie)
3207 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3208 0a22ca1a 2020-09-23 stsp ie);
3209 0a22ca1a 2020-09-23 stsp } else {
3210 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3211 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3212 0a22ca1a 2020-09-23 stsp }
3213 0a22ca1a 2020-09-23 stsp if (err)
3214 0a22ca1a 2020-09-23 stsp goto done;
3215 0a22ca1a 2020-09-23 stsp break;
3216 0a22ca1a 2020-09-23 stsp }
3217 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3218 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3219 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3220 1ee397ad 2019-07-12 stsp if (err)
3221 1ee397ad 2019-07-12 stsp goto done;
3222 2b92fad7 2019-06-02 stsp break;
3223 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3224 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3225 1ee397ad 2019-07-12 stsp if (err)
3226 1ee397ad 2019-07-12 stsp goto done;
3227 2b92fad7 2019-06-02 stsp break;
3228 2b92fad7 2019-06-02 stsp default:
3229 2b92fad7 2019-06-02 stsp break;
3230 2b92fad7 2019-06-02 stsp }
3231 234035bc 2019-06-01 stsp } else if (blob2) {
3232 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3233 234035bc 2019-06-01 stsp path2) == -1)
3234 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3235 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3236 d6c87207 2019-08-02 stsp strlen(path2));
3237 234035bc 2019-06-01 stsp if (ie) {
3238 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3239 7f91a133 2019-12-13 stsp -1, NULL, repo);
3240 234035bc 2019-06-01 stsp if (err)
3241 234035bc 2019-06-01 stsp goto done;
3242 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3243 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3244 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3245 9a298e5c 2023-03-10 stsp status != GOT_STATUS_ADD &&
3246 9a298e5c 2023-03-10 stsp status != GOT_STATUS_DELETE) {
3247 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3248 1ee397ad 2019-07-12 stsp status, path2);
3249 234035bc 2019-06-01 stsp goto done;
3250 234035bc 2019-06-01 stsp }
3251 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3252 36bf999c 2020-07-23 stsp char *link_target2;
3253 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3254 36bf999c 2020-07-23 stsp blob2);
3255 36bf999c 2020-07-23 stsp if (err)
3256 36bf999c 2020-07-23 stsp goto done;
3257 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3258 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3259 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3260 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3261 36bf999c 2020-07-23 stsp free(link_target2);
3262 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3263 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3264 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3265 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3266 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3267 526a746f 2020-07-23 stsp a->progress_arg);
3268 9a298e5c 2023-03-10 stsp } else if (status != GOT_STATUS_DELETE) {
3269 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3270 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3271 526a746f 2020-07-23 stsp }
3272 dfe9fba0 2020-07-23 stsp if (err)
3273 dfe9fba0 2020-07-23 stsp goto done;
3274 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3275 9a298e5c 2023-03-10 stsp /* Re-add file with content from new blob. */
3276 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, ie,
3277 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3278 9a298e5c 2023-03-10 stsp 0, 0, 0, a->allow_bad_symlinks,
3279 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3280 234035bc 2019-06-01 stsp if (err)
3281 234035bc 2019-06-01 stsp goto done;
3282 234035bc 2019-06-01 stsp }
3283 234035bc 2019-06-01 stsp } else {
3284 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, NULL,
3285 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3286 9a298e5c 2023-03-10 stsp 0, 0, 1, a->allow_bad_symlinks,
3287 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3288 234035bc 2019-06-01 stsp if (err)
3289 2b92fad7 2019-06-02 stsp goto done;
3290 234035bc 2019-06-01 stsp }
3291 234035bc 2019-06-01 stsp }
3292 234035bc 2019-06-01 stsp done:
3293 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3294 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3295 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3296 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3297 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3298 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3299 1af628f4 2021-06-03 stsp free(id_str);
3300 c2ba7aa6 2023-07-26 stsp free(id);
3301 54d5be07 2021-06-03 stsp free(label_deriv2);
3302 234035bc 2019-06-01 stsp free(ondisk_path);
3303 234035bc 2019-06-01 stsp return err;
3304 234035bc 2019-06-01 stsp }
3305 cdbfe5d2 2023-07-24 stsp
3306 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args {
3307 cdbfe5d2 2023-07-24 stsp struct got_worktree *worktree;
3308 cdbfe5d2 2023-07-24 stsp got_cancel_cb cancel_cb;
3309 cdbfe5d2 2023-07-24 stsp void *cancel_arg;
3310 cdbfe5d2 2023-07-24 stsp };
3311 234035bc 2019-06-01 stsp
3312 69de9dd4 2021-09-03 stsp static const struct got_error *
3313 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3314 69de9dd4 2021-09-03 stsp {
3315 f6b8c3c2 2023-07-24 stsp const struct got_error *err = NULL;
3316 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args *a = arg;
3317 cdbfe5d2 2023-07-24 stsp
3318 cdbfe5d2 2023-07-24 stsp if (a->cancel_cb) {
3319 cdbfe5d2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3320 cdbfe5d2 2023-07-24 stsp if (err)
3321 cdbfe5d2 2023-07-24 stsp return err;
3322 cdbfe5d2 2023-07-24 stsp }
3323 69de9dd4 2021-09-03 stsp
3324 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3325 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3326 cdbfe5d2 2023-07-24 stsp memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3327 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3328 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3329 69de9dd4 2021-09-03 stsp
3330 69de9dd4 2021-09-03 stsp return NULL;
3331 69de9dd4 2021-09-03 stsp }
3332 69de9dd4 2021-09-03 stsp
3333 c935fd51 2023-07-23 mark const struct got_error *
3334 c935fd51 2023-07-23 mark got_worktree_get_state(char *state, struct got_repository *repo,
3335 cdbfe5d2 2023-07-24 stsp struct got_worktree *worktree,
3336 cdbfe5d2 2023-07-24 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3337 c935fd51 2023-07-23 mark {
3338 c935fd51 2023-07-23 mark const struct got_error *err;
3339 c935fd51 2023-07-23 mark struct got_object_id *base_id, *head_id = NULL;
3340 c935fd51 2023-07-23 mark struct got_reference *head_ref;
3341 c935fd51 2023-07-23 mark struct got_fileindex *fileindex = NULL;
3342 c935fd51 2023-07-23 mark char *fileindex_path = NULL;
3343 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
3344 c935fd51 2023-07-23 mark
3345 c935fd51 2023-07-23 mark if (worktree == NULL)
3346 c935fd51 2023-07-23 mark return got_error(GOT_ERR_NOT_WORKTREE);
3347 c935fd51 2023-07-23 mark
3348 c935fd51 2023-07-23 mark err = got_ref_open(&head_ref, repo,
3349 c935fd51 2023-07-23 mark got_worktree_get_head_ref_name(worktree), 0);
3350 c935fd51 2023-07-23 mark if (err)
3351 c935fd51 2023-07-23 mark return err;
3352 c935fd51 2023-07-23 mark
3353 c935fd51 2023-07-23 mark err = got_ref_resolve(&head_id, repo, head_ref);
3354 c935fd51 2023-07-23 mark if (err)
3355 c935fd51 2023-07-23 mark goto done;
3356 c935fd51 2023-07-23 mark
3357 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_UNKNOWN;
3358 c935fd51 2023-07-23 mark base_id = got_worktree_get_base_commit_id(worktree);
3359 c935fd51 2023-07-23 mark
3360 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
3361 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
3362 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
3363 cdbfe5d2 2023-07-24 stsp
3364 c935fd51 2023-07-23 mark if (got_object_id_cmp(base_id, head_id) == 0) {
3365 c935fd51 2023-07-23 mark err = open_fileindex(&fileindex, &fileindex_path, worktree);
3366 c935fd51 2023-07-23 mark if (err)
3367 c935fd51 2023-07-23 mark goto done;
3368 c935fd51 2023-07-23 mark
3369 c935fd51 2023-07-23 mark err = got_fileindex_for_each_entry_safe(fileindex,
3370 cdbfe5d2 2023-07-24 stsp check_mixed_commits, &cma);
3371 c935fd51 2023-07-23 mark if (err == NULL)
3372 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_UPTODATE;
3373 99301cec 2023-07-24 stsp else if (err->code == GOT_ERR_MIXED_COMMITS) {
3374 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_OUTOFDATE;
3375 c935fd51 2023-07-23 mark err = NULL;
3376 99301cec 2023-07-24 stsp }
3377 99301cec 2023-07-24 stsp } else
3378 99301cec 2023-07-24 stsp *state = GOT_WORKTREE_STATE_OUTOFDATE;
3379 c935fd51 2023-07-23 mark
3380 c935fd51 2023-07-23 mark done:
3381 c935fd51 2023-07-23 mark free(head_id);
3382 c935fd51 2023-07-23 mark free(fileindex_path);
3383 c935fd51 2023-07-23 mark got_ref_close(head_ref);
3384 c935fd51 2023-07-23 mark if (fileindex != NULL)
3385 c935fd51 2023-07-23 mark got_fileindex_free(fileindex);
3386 c935fd51 2023-07-23 mark return err;
3387 c935fd51 2023-07-23 mark }
3388 c935fd51 2023-07-23 mark
3389 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3390 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3391 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3392 234035bc 2019-06-01 stsp struct got_repository *repo;
3393 234035bc 2019-06-01 stsp };
3394 234035bc 2019-06-01 stsp
3395 234035bc 2019-06-01 stsp static const struct got_error *
3396 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3397 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3398 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3399 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3400 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3401 234035bc 2019-06-01 stsp {
3402 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3403 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3404 234035bc 2019-06-01 stsp unsigned char status;
3405 234035bc 2019-06-01 stsp struct stat sb;
3406 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3407 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3408 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3409 234035bc 2019-06-01 stsp char *ondisk_path;
3410 234035bc 2019-06-01 stsp
3411 69de9dd4 2021-09-03 stsp if (id == NULL)
3412 69de9dd4 2021-09-03 stsp return NULL;
3413 234035bc 2019-06-01 stsp
3414 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3415 69de9dd4 2021-09-03 stsp if (ie == NULL)
3416 69de9dd4 2021-09-03 stsp return NULL;
3417 69de9dd4 2021-09-03 stsp
3418 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3419 234035bc 2019-06-01 stsp == -1)
3420 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3421 234035bc 2019-06-01 stsp
3422 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3423 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3424 5546d466 2021-09-02 stsp free(ondisk_path);
3425 234035bc 2019-06-01 stsp if (err)
3426 234035bc 2019-06-01 stsp return err;
3427 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3428 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3429 234035bc 2019-06-01 stsp
3430 234035bc 2019-06-01 stsp return NULL;
3431 234035bc 2019-06-01 stsp }
3432 234035bc 2019-06-01 stsp
3433 818c7501 2019-07-11 stsp static const struct got_error *
3434 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3435 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3436 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3437 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3438 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3439 234035bc 2019-06-01 stsp {
3440 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3441 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3442 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3443 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3444 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3445 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3446 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3447 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3448 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3449 234035bc 2019-06-01 stsp
3450 03415a1a 2019-06-02 stsp if (commit_id1) {
3451 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3452 a44927cc 2022-04-07 stsp if (err)
3453 a44927cc 2022-04-07 stsp goto done;
3454 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3455 03415a1a 2019-06-02 stsp worktree->path_prefix);
3456 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3457 03415a1a 2019-06-02 stsp goto done;
3458 69d57f3d 2020-07-31 stsp }
3459 69d57f3d 2020-07-31 stsp if (tree_id1) {
3460 69d57f3d 2020-07-31 stsp char *id_str;
3461 03415a1a 2019-06-02 stsp
3462 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3463 03415a1a 2019-06-02 stsp if (err)
3464 03415a1a 2019-06-02 stsp goto done;
3465 f69721c3 2019-10-21 stsp
3466 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3467 f69721c3 2019-10-21 stsp if (err)
3468 f69721c3 2019-10-21 stsp goto done;
3469 f69721c3 2019-10-21 stsp
3470 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3471 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3472 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3473 f69721c3 2019-10-21 stsp free(id_str);
3474 f69721c3 2019-10-21 stsp goto done;
3475 f69721c3 2019-10-21 stsp }
3476 f69721c3 2019-10-21 stsp free(id_str);
3477 b72706c3 2022-06-01 stsp
3478 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3479 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3480 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3481 b72706c3 2022-06-01 stsp goto done;
3482 b72706c3 2022-06-01 stsp }
3483 03415a1a 2019-06-02 stsp }
3484 234035bc 2019-06-01 stsp
3485 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3486 a44927cc 2022-04-07 stsp if (err)
3487 a44927cc 2022-04-07 stsp goto done;
3488 a44927cc 2022-04-07 stsp
3489 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3490 234035bc 2019-06-01 stsp worktree->path_prefix);
3491 234035bc 2019-06-01 stsp if (err)
3492 234035bc 2019-06-01 stsp goto done;
3493 234035bc 2019-06-01 stsp
3494 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3495 234035bc 2019-06-01 stsp if (err)
3496 234035bc 2019-06-01 stsp goto done;
3497 234035bc 2019-06-01 stsp
3498 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3499 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3500 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3501 f9d37699 2022-06-28 stsp goto done;
3502 f9d37699 2022-06-28 stsp }
3503 f9d37699 2022-06-28 stsp
3504 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3505 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3506 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3507 b72706c3 2022-06-01 stsp goto done;
3508 b72706c3 2022-06-01 stsp }
3509 b72706c3 2022-06-01 stsp
3510 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3511 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3512 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3513 f9d37699 2022-06-28 stsp goto done;
3514 f9d37699 2022-06-28 stsp }
3515 f9d37699 2022-06-28 stsp
3516 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3517 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3518 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3519 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3520 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3521 69de9dd4 2021-09-03 stsp if (err)
3522 69de9dd4 2021-09-03 stsp goto done;
3523 69de9dd4 2021-09-03 stsp
3524 234035bc 2019-06-01 stsp arg.worktree = worktree;
3525 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3526 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3527 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3528 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3529 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3530 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3531 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3532 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3533 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3534 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3535 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3536 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3537 af12c6b9 2019-06-04 stsp err = sync_err;
3538 234035bc 2019-06-01 stsp done:
3539 a44927cc 2022-04-07 stsp if (commit1)
3540 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3541 a44927cc 2022-04-07 stsp if (commit2)
3542 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3543 234035bc 2019-06-01 stsp if (tree1)
3544 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3545 234035bc 2019-06-01 stsp if (tree2)
3546 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3547 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3548 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3549 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3550 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3551 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3552 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3553 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3554 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3555 f69721c3 2019-10-21 stsp free(label_orig);
3556 818c7501 2019-07-11 stsp return err;
3557 818c7501 2019-07-11 stsp }
3558 234035bc 2019-06-01 stsp
3559 818c7501 2019-07-11 stsp const struct got_error *
3560 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3561 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3562 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3563 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3564 818c7501 2019-07-11 stsp {
3565 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3566 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3567 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3568 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
3569 818c7501 2019-07-11 stsp
3570 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3571 818c7501 2019-07-11 stsp if (err)
3572 818c7501 2019-07-11 stsp return err;
3573 818c7501 2019-07-11 stsp
3574 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3575 818c7501 2019-07-11 stsp if (err)
3576 818c7501 2019-07-11 stsp goto done;
3577 818c7501 2019-07-11 stsp
3578 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
3579 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
3580 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
3581 cdbfe5d2 2023-07-24 stsp
3582 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3583 cdbfe5d2 2023-07-24 stsp &cma);
3584 818c7501 2019-07-11 stsp if (err)
3585 818c7501 2019-07-11 stsp goto done;
3586 818c7501 2019-07-11 stsp
3587 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3588 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3589 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3590 818c7501 2019-07-11 stsp done:
3591 818c7501 2019-07-11 stsp if (fileindex)
3592 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3593 818c7501 2019-07-11 stsp free(fileindex_path);
3594 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3595 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3596 234035bc 2019-06-01 stsp err = unlockerr;
3597 234035bc 2019-06-01 stsp return err;
3598 234035bc 2019-06-01 stsp }
3599 234035bc 2019-06-01 stsp
3600 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3601 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3602 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3603 927df6b7 2019-02-10 stsp const char *status_path;
3604 927df6b7 2019-02-10 stsp size_t status_path_len;
3605 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3606 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3607 f8d1f275 2019-02-04 stsp void *status_arg;
3608 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3609 f8d1f275 2019-02-04 stsp void *cancel_arg;
3610 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3611 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3612 f2a9dc41 2019-12-13 tracey int report_unchanged;
3613 3143d852 2020-06-25 stsp int no_ignores;
3614 f8d1f275 2019-02-04 stsp };
3615 88d0e355 2019-08-03 stsp
3616 f8d1f275 2019-02-04 stsp static const struct got_error *
3617 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3618 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3619 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3620 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3621 927df6b7 2019-02-10 stsp {
3622 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3623 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3624 2c4740ad 2023-02-12 mark unsigned char staged_status;
3625 927df6b7 2019-02-10 stsp struct stat sb;
3626 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3627 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3628 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3629 927df6b7 2019-02-10 stsp
3630 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3631 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3632 98eaaa12 2019-08-03 stsp if (err)
3633 98eaaa12 2019-08-03 stsp return err;
3634 98eaaa12 2019-08-03 stsp
3635 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3636 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3637 98eaaa12 2019-08-03 stsp return NULL;
3638 98eaaa12 2019-08-03 stsp
3639 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3640 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3641 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3642 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3643 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3644 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3645 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3646 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3647 98eaaa12 2019-08-03 stsp }
3648 98eaaa12 2019-08-03 stsp
3649 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3650 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3651 927df6b7 2019-02-10 stsp }
3652 927df6b7 2019-02-10 stsp
3653 927df6b7 2019-02-10 stsp static const struct got_error *
3654 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3655 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3656 f8d1f275 2019-02-04 stsp {
3657 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3658 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3659 f8d1f275 2019-02-04 stsp char *abspath;
3660 f8d1f275 2019-02-04 stsp
3661 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3662 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3663 f6b8c3c2 2023-07-24 stsp if (err)
3664 f6b8c3c2 2023-07-24 stsp return err;
3665 f6b8c3c2 2023-07-24 stsp }
3666 0584f854 2019-04-06 stsp
3667 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3668 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3669 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3670 927df6b7 2019-02-10 stsp return NULL;
3671 927df6b7 2019-02-10 stsp
3672 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3673 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3674 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3675 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3676 f8d1f275 2019-02-04 stsp } else {
3677 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3678 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3679 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3680 f8d1f275 2019-02-04 stsp }
3681 f8d1f275 2019-02-04 stsp
3682 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3683 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3684 f8d1f275 2019-02-04 stsp free(abspath);
3685 9d31a1d8 2018-03-11 stsp return err;
3686 9d31a1d8 2018-03-11 stsp }
3687 f8d1f275 2019-02-04 stsp
3688 f8d1f275 2019-02-04 stsp static const struct got_error *
3689 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3690 f8d1f275 2019-02-04 stsp {
3691 f6b8c3c2 2023-07-24 stsp const struct got_error *err;
3692 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3693 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3694 2ec1f75b 2019-03-26 stsp unsigned char status;
3695 927df6b7 2019-02-10 stsp
3696 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3697 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3698 f6b8c3c2 2023-07-24 stsp if (err)
3699 f6b8c3c2 2023-07-24 stsp return err;
3700 f6b8c3c2 2023-07-24 stsp }
3701 0584f854 2019-04-06 stsp
3702 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3703 927df6b7 2019-02-10 stsp return NULL;
3704 927df6b7 2019-02-10 stsp
3705 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3706 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3707 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3708 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3709 2ec1f75b 2019-03-26 stsp else
3710 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3711 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3712 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3713 6841da00 2019-08-08 stsp }
3714 6841da00 2019-08-08 stsp
3715 336075a4 2022-06-25 op static void
3716 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3717 6841da00 2019-08-08 stsp {
3718 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3719 6841da00 2019-08-08 stsp
3720 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3721 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3722 d8bacb93 2023-01-10 mark
3723 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3724 6841da00 2019-08-08 stsp }
3725 8fd0d196 2023-12-08 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3726 6841da00 2019-08-08 stsp }
3727 6841da00 2019-08-08 stsp
3728 6841da00 2019-08-08 stsp static const struct got_error *
3729 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3730 6841da00 2019-08-08 stsp {
3731 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3732 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3733 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3734 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3735 6841da00 2019-08-08 stsp size_t linesize = 0;
3736 6841da00 2019-08-08 stsp ssize_t linelen;
3737 6841da00 2019-08-08 stsp
3738 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3739 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3740 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3741 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3742 6841da00 2019-08-08 stsp
3743 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3744 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3745 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3746 bd8de430 2019-10-04 stsp
3747 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3748 bd8de430 2019-10-04 stsp if (line[0] == '#')
3749 bd8de430 2019-10-04 stsp continue;
3750 bd8de430 2019-10-04 stsp
3751 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3752 bd8de430 2019-10-04 stsp if (line[0] == '!')
3753 bd8de430 2019-10-04 stsp continue;
3754 bd8de430 2019-10-04 stsp
3755 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3756 6841da00 2019-08-08 stsp line) == -1) {
3757 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3758 6841da00 2019-08-08 stsp goto done;
3759 6841da00 2019-08-08 stsp }
3760 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3761 6841da00 2019-08-08 stsp if (err)
3762 6841da00 2019-08-08 stsp goto done;
3763 6841da00 2019-08-08 stsp }
3764 6841da00 2019-08-08 stsp if (ferror(f)) {
3765 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3766 6841da00 2019-08-08 stsp goto done;
3767 6841da00 2019-08-08 stsp }
3768 6841da00 2019-08-08 stsp
3769 6841da00 2019-08-08 stsp dirpath = strdup(path);
3770 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3771 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3772 6841da00 2019-08-08 stsp goto done;
3773 6841da00 2019-08-08 stsp }
3774 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3775 6841da00 2019-08-08 stsp done:
3776 6841da00 2019-08-08 stsp free(line);
3777 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3778 6841da00 2019-08-08 stsp free(dirpath);
3779 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3780 8fd0d196 2023-12-08 mark free(ignorelist);
3781 6841da00 2019-08-08 stsp }
3782 6841da00 2019-08-08 stsp return err;
3783 249b637c 2023-02-20 stsp }
3784 249b637c 2023-02-20 stsp
3785 249b637c 2023-02-20 stsp static int
3786 249b637c 2023-02-20 stsp match_path(const char *pattern, size_t pattern_len, const char *path,
3787 249b637c 2023-02-20 stsp int flags)
3788 249b637c 2023-02-20 stsp {
3789 249b637c 2023-02-20 stsp char buf[PATH_MAX];
3790 249b637c 2023-02-20 stsp
3791 249b637c 2023-02-20 stsp /*
3792 249b637c 2023-02-20 stsp * Trailing slashes signify directories.
3793 249b637c 2023-02-20 stsp * Append a * to make such patterns conform to fnmatch rules.
3794 249b637c 2023-02-20 stsp */
3795 249b637c 2023-02-20 stsp if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3796 249b637c 2023-02-20 stsp if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3797 249b637c 2023-02-20 stsp return FNM_NOMATCH; /* XXX */
3798 249b637c 2023-02-20 stsp
3799 249b637c 2023-02-20 stsp return fnmatch(buf, path, flags);
3800 249b637c 2023-02-20 stsp }
3801 249b637c 2023-02-20 stsp
3802 249b637c 2023-02-20 stsp return fnmatch(pattern, path, flags);
3803 6841da00 2019-08-08 stsp }
3804 6841da00 2019-08-08 stsp
3805 336075a4 2022-06-25 op static int
3806 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3807 6841da00 2019-08-08 stsp {
3808 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3809 bd8de430 2019-10-04 stsp
3810 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3811 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3812 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3813 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3814 bd8de430 2019-10-04 stsp
3815 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3816 249b637c 2023-02-20 stsp const char *p;
3817 6841da00 2019-08-08 stsp
3818 249b637c 2023-02-20 stsp if (pi->path_len < 3 ||
3819 249b637c 2023-02-20 stsp strncmp(pi->path, "**/", 3) != 0)
3820 bd8de430 2019-10-04 stsp continue;
3821 bd8de430 2019-10-04 stsp p = path;
3822 bd8de430 2019-10-04 stsp while (*p) {
3823 249b637c 2023-02-20 stsp if (match_path(pi->path + 3,
3824 249b637c 2023-02-20 stsp pi->path_len - 3, p,
3825 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3826 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3827 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3828 bd8de430 2019-10-04 stsp p++;
3829 bd8de430 2019-10-04 stsp while (*p == '/')
3830 bd8de430 2019-10-04 stsp p++;
3831 bd8de430 2019-10-04 stsp continue;
3832 bd8de430 2019-10-04 stsp }
3833 bd8de430 2019-10-04 stsp return 1;
3834 bd8de430 2019-10-04 stsp }
3835 bd8de430 2019-10-04 stsp }
3836 bd8de430 2019-10-04 stsp }
3837 bd8de430 2019-10-04 stsp
3838 6841da00 2019-08-08 stsp /*
3839 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3840 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3841 6841da00 2019-08-08 stsp * ignores backwards.
3842 6841da00 2019-08-08 stsp */
3843 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3844 6841da00 2019-08-08 stsp while (pe) {
3845 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3846 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3847 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3848 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3849 249b637c 2023-02-20 stsp int flags = FNM_LEADING_DIR;
3850 249b637c 2023-02-20 stsp if (strstr(pi->path, "/**/") == NULL)
3851 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3852 249b637c 2023-02-20 stsp if (match_path(pi->path, pi->path_len,
3853 249b637c 2023-02-20 stsp path, flags))
3854 6841da00 2019-08-08 stsp continue;
3855 6841da00 2019-08-08 stsp return 1;
3856 6841da00 2019-08-08 stsp }
3857 6841da00 2019-08-08 stsp }
3858 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3859 6841da00 2019-08-08 stsp }
3860 6841da00 2019-08-08 stsp
3861 6841da00 2019-08-08 stsp return 0;
3862 6841da00 2019-08-08 stsp }
3863 6841da00 2019-08-08 stsp
3864 6841da00 2019-08-08 stsp static const struct got_error *
3865 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3866 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3867 6841da00 2019-08-08 stsp {
3868 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3869 6841da00 2019-08-08 stsp char *ignorespath;
3870 886cec17 2019-12-15 stsp int fd = -1;
3871 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3872 6841da00 2019-08-08 stsp
3873 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3874 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3875 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3876 6841da00 2019-08-08 stsp
3877 886cec17 2019-12-15 stsp if (dirfd != -1) {
3878 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3879 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3880 886cec17 2019-12-15 stsp if (fd == -1) {
3881 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3882 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3883 886cec17 2019-12-15 stsp ignorespath);
3884 886cec17 2019-12-15 stsp } else {
3885 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3886 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3887 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3888 886cec17 2019-12-15 stsp ignorespath);
3889 886cec17 2019-12-15 stsp else {
3890 886cec17 2019-12-15 stsp fd = -1;
3891 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3892 886cec17 2019-12-15 stsp }
3893 886cec17 2019-12-15 stsp }
3894 886cec17 2019-12-15 stsp } else {
3895 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3896 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3897 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3898 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3899 886cec17 2019-12-15 stsp ignorespath);
3900 886cec17 2019-12-15 stsp } else
3901 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3902 886cec17 2019-12-15 stsp }
3903 6841da00 2019-08-08 stsp
3904 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3905 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3906 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3907 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3908 6841da00 2019-08-08 stsp free(ignorespath);
3909 6841da00 2019-08-08 stsp return err;
3910 f8d1f275 2019-02-04 stsp }
3911 f8d1f275 2019-02-04 stsp
3912 f8d1f275 2019-02-04 stsp static const struct got_error *
3913 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3914 62da3196 2021-10-01 stsp int dirfd)
3915 f8d1f275 2019-02-04 stsp {
3916 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3917 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3918 f8d1f275 2019-02-04 stsp char *path = NULL;
3919 62da3196 2021-10-01 stsp
3920 62da3196 2021-10-01 stsp if (ignore != NULL)
3921 62da3196 2021-10-01 stsp *ignore = 0;
3922 f8d1f275 2019-02-04 stsp
3923 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3924 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3925 f6b8c3c2 2023-07-24 stsp if (err)
3926 f6b8c3c2 2023-07-24 stsp return err;
3927 f6b8c3c2 2023-07-24 stsp }
3928 0584f854 2019-04-06 stsp
3929 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3930 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3931 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3932 f8d1f275 2019-02-04 stsp } else {
3933 f8d1f275 2019-02-04 stsp path = de->d_name;
3934 f8d1f275 2019-02-04 stsp }
3935 f8d1f275 2019-02-04 stsp
3936 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3937 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3938 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3939 62da3196 2021-10-01 stsp *ignore = 1;
3940 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3941 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3942 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3943 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3944 f8d1f275 2019-02-04 stsp if (parent_path[0])
3945 f8d1f275 2019-02-04 stsp free(path);
3946 b72f483a 2019-02-05 stsp return err;
3947 f8d1f275 2019-02-04 stsp }
3948 f8d1f275 2019-02-04 stsp
3949 347d1d3e 2019-07-12 stsp static const struct got_error *
3950 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3951 3143d852 2020-06-25 stsp {
3952 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3953 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3954 3143d852 2020-06-25 stsp
3955 3143d852 2020-06-25 stsp if (a->no_ignores)
3956 3143d852 2020-06-25 stsp return NULL;
3957 3143d852 2020-06-25 stsp
3958 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3959 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3960 3143d852 2020-06-25 stsp if (err)
3961 3143d852 2020-06-25 stsp return err;
3962 3143d852 2020-06-25 stsp
3963 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3964 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3965 3143d852 2020-06-25 stsp
3966 3143d852 2020-06-25 stsp return err;
3967 3143d852 2020-06-25 stsp }
3968 3143d852 2020-06-25 stsp
3969 3143d852 2020-06-25 stsp static const struct got_error *
3970 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3971 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3972 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3973 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3974 abb4604f 2019-07-27 stsp {
3975 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3976 abb4604f 2019-07-27 stsp struct stat sb;
3977 ff56836b 2021-07-08 stsp
3978 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3979 abb4604f 2019-07-27 stsp if (ie)
3980 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3981 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3982 abb4604f 2019-07-27 stsp
3983 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3984 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3985 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3986 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3987 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3988 abb4604f 2019-07-27 stsp }
3989 abb4604f 2019-07-27 stsp
3990 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3991 916237f3 2022-02-11 stsp return NULL;
3992 916237f3 2022-02-11 stsp
3993 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3994 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3995 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3996 abb4604f 2019-07-27 stsp
3997 abb4604f 2019-07-27 stsp return NULL;
3998 3143d852 2020-06-25 stsp }
3999 3143d852 2020-06-25 stsp
4000 3143d852 2020-06-25 stsp static const struct got_error *
4001 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
4002 3143d852 2020-06-25 stsp const char *root_path, const char *path)
4003 3143d852 2020-06-25 stsp {
4004 3143d852 2020-06-25 stsp const struct got_error *err;
4005 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
4006 3143d852 2020-06-25 stsp
4007 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4008 3143d852 2020-06-25 stsp ".cvsignore");
4009 3143d852 2020-06-25 stsp if (err)
4010 3143d852 2020-06-25 stsp return err;
4011 3143d852 2020-06-25 stsp
4012 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4013 3143d852 2020-06-25 stsp ".gitignore");
4014 3143d852 2020-06-25 stsp if (err)
4015 3143d852 2020-06-25 stsp return err;
4016 3143d852 2020-06-25 stsp
4017 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
4018 3143d852 2020-06-25 stsp if (err) {
4019 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
4020 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
4021 3143d852 2020-06-25 stsp return err;
4022 3143d852 2020-06-25 stsp }
4023 3143d852 2020-06-25 stsp for (;;) {
4024 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4025 3143d852 2020-06-25 stsp ".cvsignore");
4026 3143d852 2020-06-25 stsp if (err)
4027 3143d852 2020-06-25 stsp break;
4028 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4029 3143d852 2020-06-25 stsp ".gitignore");
4030 3143d852 2020-06-25 stsp if (err)
4031 3143d852 2020-06-25 stsp break;
4032 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
4033 3143d852 2020-06-25 stsp if (err) {
4034 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
4035 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
4036 3143d852 2020-06-25 stsp break;
4037 3143d852 2020-06-25 stsp }
4038 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
4039 ff56836b 2021-07-08 stsp break;
4040 b737c85a 2020-06-26 stsp free(parent_path);
4041 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
4042 b737c85a 2020-06-26 stsp next_parent_path = NULL;
4043 3143d852 2020-06-25 stsp }
4044 3143d852 2020-06-25 stsp
4045 b737c85a 2020-06-26 stsp free(parent_path);
4046 b737c85a 2020-06-26 stsp free(next_parent_path);
4047 3143d852 2020-06-25 stsp return err;
4048 abb4604f 2019-07-27 stsp }
4049 31cf15ec 2023-04-12 stsp
4050 31cf15ec 2023-04-12 stsp struct find_missing_children_args {
4051 31cf15ec 2023-04-12 stsp const char *parent_path;
4052 31cf15ec 2023-04-12 stsp size_t parent_len;
4053 31cf15ec 2023-04-12 stsp struct got_pathlist_head *children;
4054 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb;
4055 31cf15ec 2023-04-12 stsp void *cancel_arg;
4056 31cf15ec 2023-04-12 stsp };
4057 31cf15ec 2023-04-12 stsp
4058 abb4604f 2019-07-27 stsp static const struct got_error *
4059 31cf15ec 2023-04-12 stsp find_missing_children(void *arg, struct got_fileindex_entry *ie)
4060 31cf15ec 2023-04-12 stsp {
4061 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
4062 31cf15ec 2023-04-12 stsp struct find_missing_children_args *a = arg;
4063 31cf15ec 2023-04-12 stsp
4064 31cf15ec 2023-04-12 stsp if (a->cancel_cb) {
4065 31cf15ec 2023-04-12 stsp err = a->cancel_cb(a->cancel_arg);
4066 31cf15ec 2023-04-12 stsp if (err)
4067 31cf15ec 2023-04-12 stsp return err;
4068 31cf15ec 2023-04-12 stsp }
4069 31cf15ec 2023-04-12 stsp
4070 31cf15ec 2023-04-12 stsp if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4071 31cf15ec 2023-04-12 stsp err = got_pathlist_append(a->children, ie->path, NULL);
4072 31cf15ec 2023-04-12 stsp
4073 31cf15ec 2023-04-12 stsp return err;
4074 31cf15ec 2023-04-12 stsp }
4075 31cf15ec 2023-04-12 stsp
4076 31cf15ec 2023-04-12 stsp static const struct got_error *
4077 31cf15ec 2023-04-12 stsp report_children(struct got_pathlist_head *children,
4078 31cf15ec 2023-04-12 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4079 31cf15ec 2023-04-12 stsp struct got_repository *repo, int is_root_dir, int report_unchanged,
4080 31cf15ec 2023-04-12 stsp struct got_pathlist_head *ignores, int no_ignores,
4081 31cf15ec 2023-04-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4082 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4083 31cf15ec 2023-04-12 stsp {
4084 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
4085 31cf15ec 2023-04-12 stsp struct got_pathlist_entry *pe;
4086 31cf15ec 2023-04-12 stsp char *ondisk_path = NULL;
4087 31cf15ec 2023-04-12 stsp
4088 31cf15ec 2023-04-12 stsp TAILQ_FOREACH(pe, children, entry) {
4089 31cf15ec 2023-04-12 stsp if (cancel_cb) {
4090 31cf15ec 2023-04-12 stsp err = cancel_cb(cancel_arg);
4091 31cf15ec 2023-04-12 stsp if (err)
4092 31cf15ec 2023-04-12 stsp break;
4093 31cf15ec 2023-04-12 stsp }
4094 31cf15ec 2023-04-12 stsp
4095 31cf15ec 2023-04-12 stsp if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4096 31cf15ec 2023-04-12 stsp !is_root_dir ? "/" : "", pe->path) == -1) {
4097 31cf15ec 2023-04-12 stsp err = got_error_from_errno("asprintf");
4098 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
4099 31cf15ec 2023-04-12 stsp break;
4100 31cf15ec 2023-04-12 stsp }
4101 31cf15ec 2023-04-12 stsp
4102 31cf15ec 2023-04-12 stsp err = report_single_file_status(pe->path, ondisk_path,
4103 31cf15ec 2023-04-12 stsp fileindex, status_cb, status_arg, repo, report_unchanged,
4104 31cf15ec 2023-04-12 stsp ignores, no_ignores);
4105 31cf15ec 2023-04-12 stsp if (err)
4106 31cf15ec 2023-04-12 stsp break;
4107 31cf15ec 2023-04-12 stsp
4108 31cf15ec 2023-04-12 stsp free(ondisk_path);
4109 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
4110 31cf15ec 2023-04-12 stsp }
4111 31cf15ec 2023-04-12 stsp
4112 31cf15ec 2023-04-12 stsp free(ondisk_path);
4113 31cf15ec 2023-04-12 stsp return err;
4114 31cf15ec 2023-04-12 stsp }
4115 31cf15ec 2023-04-12 stsp
4116 31cf15ec 2023-04-12 stsp static const struct got_error *
4117 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4118 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4119 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4120 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4121 f2a9dc41 2019-12-13 tracey int report_unchanged)
4122 f8d1f275 2019-02-04 stsp {
4123 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4124 6fc93f37 2019-12-13 stsp int fd = -1;
4125 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4126 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4127 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4128 31cf15ec 2023-04-12 stsp struct got_pathlist_head ignores, missing_children;
4129 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
4130 3143d852 2020-06-25 stsp
4131 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4132 31cf15ec 2023-04-12 stsp TAILQ_INIT(&missing_children);
4133 f8d1f275 2019-02-04 stsp
4134 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4135 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4136 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4137 8dc303cc 2019-07-27 stsp
4138 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4139 a84c0d30 2022-03-12 stsp if (ie) {
4140 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
4141 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
4142 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
4143 a84c0d30 2022-03-12 stsp goto done;
4144 31cf15ec 2023-04-12 stsp } else {
4145 31cf15ec 2023-04-12 stsp struct find_missing_children_args fmca;
4146 31cf15ec 2023-04-12 stsp fmca.parent_path = path;
4147 31cf15ec 2023-04-12 stsp fmca.parent_len = strlen(path);
4148 31cf15ec 2023-04-12 stsp fmca.children = &missing_children;
4149 31cf15ec 2023-04-12 stsp fmca.cancel_cb = cancel_cb;
4150 31cf15ec 2023-04-12 stsp fmca.cancel_arg = cancel_arg;
4151 31cf15ec 2023-04-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
4152 31cf15ec 2023-04-12 stsp find_missing_children, &fmca);
4153 31cf15ec 2023-04-12 stsp if (err)
4154 31cf15ec 2023-04-12 stsp goto done;
4155 a84c0d30 2022-03-12 stsp }
4156 a84c0d30 2022-03-12 stsp
4157 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4158 6fc93f37 2019-12-13 stsp if (fd == -1) {
4159 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4160 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
4161 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4162 ff56836b 2021-07-08 stsp else {
4163 ff56836b 2021-07-08 stsp if (!no_ignores) {
4164 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4165 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4166 ff56836b 2021-07-08 stsp if (err)
4167 ff56836b 2021-07-08 stsp goto done;
4168 ff56836b 2021-07-08 stsp }
4169 31cf15ec 2023-04-12 stsp if (TAILQ_EMPTY(&missing_children)) {
4170 31cf15ec 2023-04-12 stsp err = report_single_file_status(path,
4171 31cf15ec 2023-04-12 stsp ondisk_path, fileindex,
4172 31cf15ec 2023-04-12 stsp status_cb, status_arg, repo,
4173 31cf15ec 2023-04-12 stsp report_unchanged, &ignores, no_ignores);
4174 31cf15ec 2023-04-12 stsp if (err)
4175 31cf15ec 2023-04-12 stsp goto done;
4176 31cf15ec 2023-04-12 stsp } else {
4177 31cf15ec 2023-04-12 stsp err = report_children(&missing_children,
4178 31cf15ec 2023-04-12 stsp worktree, fileindex, repo,
4179 31cf15ec 2023-04-12 stsp (path[0] == '\0'), report_unchanged,
4180 31cf15ec 2023-04-12 stsp &ignores, no_ignores,
4181 31cf15ec 2023-04-12 stsp status_cb, status_arg,
4182 31cf15ec 2023-04-12 stsp cancel_cb, cancel_arg);
4183 46108e23 2023-04-12 stsp if (err)
4184 46108e23 2023-04-12 stsp goto done;
4185 31cf15ec 2023-04-12 stsp }
4186 ff56836b 2021-07-08 stsp }
4187 abb4604f 2019-07-27 stsp } else {
4188 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4189 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4190 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4191 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4192 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4193 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4194 abb4604f 2019-07-27 stsp arg.status_path = path;
4195 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4196 abb4604f 2019-07-27 stsp arg.repo = repo;
4197 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4198 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4199 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4200 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4201 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4202 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4203 022fae89 2019-12-06 tracey if (!no_ignores) {
4204 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4205 3143d852 2020-06-25 stsp worktree->root_path, path);
4206 3143d852 2020-06-25 stsp if (err)
4207 3143d852 2020-06-25 stsp goto done;
4208 022fae89 2019-12-06 tracey }
4209 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4210 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4211 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4212 927df6b7 2019-02-10 stsp }
4213 3143d852 2020-06-25 stsp done:
4214 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4215 9e83097f 2023-12-05 mark got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4216 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4217 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4218 927df6b7 2019-02-10 stsp free(ondisk_path);
4219 347d1d3e 2019-07-12 stsp return err;
4220 347d1d3e 2019-07-12 stsp }
4221 347d1d3e 2019-07-12 stsp
4222 347d1d3e 2019-07-12 stsp const struct got_error *
4223 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4224 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4225 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4226 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4227 347d1d3e 2019-07-12 stsp {
4228 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4229 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4230 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4231 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4232 347d1d3e 2019-07-12 stsp
4233 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4234 347d1d3e 2019-07-12 stsp if (err)
4235 347d1d3e 2019-07-12 stsp return err;
4236 347d1d3e 2019-07-12 stsp
4237 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4238 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4239 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4240 f6343036 2021-06-22 stsp no_ignores, 0);
4241 72ea6654 2019-07-27 stsp if (err)
4242 72ea6654 2019-07-27 stsp break;
4243 72ea6654 2019-07-27 stsp }
4244 f8d1f275 2019-02-04 stsp free(fileindex_path);
4245 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4246 f8d1f275 2019-02-04 stsp return err;
4247 f8d1f275 2019-02-04 stsp }
4248 6c7ab921 2019-03-18 stsp
4249 6c7ab921 2019-03-18 stsp const struct got_error *
4250 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4251 6c7ab921 2019-03-18 stsp const char *arg)
4252 6c7ab921 2019-03-18 stsp {
4253 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4254 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4255 6c7ab921 2019-03-18 stsp size_t len;
4256 00bb5ea0 2020-07-23 stsp struct stat sb;
4257 01740607 2020-11-04 stsp char *abspath = NULL;
4258 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4259 6c7ab921 2019-03-18 stsp
4260 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4261 6c7ab921 2019-03-18 stsp
4262 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4263 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4264 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4265 00bb5ea0 2020-07-23 stsp
4266 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4267 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4268 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4269 00bb5ea0 2020-07-23 stsp goto done;
4270 00bb5ea0 2020-07-23 stsp }
4271 727173c3 2020-11-06 stsp sb.st_mode = 0;
4272 00bb5ea0 2020-07-23 stsp }
4273 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4274 00bb5ea0 2020-07-23 stsp /*
4275 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4276 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4277 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4278 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4279 00bb5ea0 2020-07-23 stsp */
4280 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4281 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4282 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4283 00bb5ea0 2020-07-23 stsp goto done;
4284 00bb5ea0 2020-07-23 stsp }
4285 00bb5ea0 2020-07-23 stsp
4286 00bb5ea0 2020-07-23 stsp }
4287 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4288 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4289 00bb5ea0 2020-07-23 stsp if (err)
4290 d0710d08 2019-07-22 stsp goto done;
4291 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4292 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4293 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4294 00bb5ea0 2020-07-23 stsp goto done;
4295 00bb5ea0 2020-07-23 stsp }
4296 00bb5ea0 2020-07-23 stsp } else {
4297 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4298 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4299 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4300 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4301 00bb5ea0 2020-07-23 stsp goto done;
4302 00bb5ea0 2020-07-23 stsp }
4303 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4304 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4305 01740607 2020-11-04 stsp goto done;
4306 01740607 2020-11-04 stsp }
4307 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4308 01740607 2020-11-04 stsp sizeof(canonpath));
4309 01740607 2020-11-04 stsp if (err)
4310 00bb5ea0 2020-07-23 stsp goto done;
4311 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4312 01740607 2020-11-04 stsp if (resolved == NULL) {
4313 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4314 01740607 2020-11-04 stsp goto done;
4315 00bb5ea0 2020-07-23 stsp }
4316 d0710d08 2019-07-22 stsp }
4317 d0710d08 2019-07-22 stsp }
4318 6c7ab921 2019-03-18 stsp
4319 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4320 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4321 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4322 6c7ab921 2019-03-18 stsp goto done;
4323 6c7ab921 2019-03-18 stsp }
4324 6c7ab921 2019-03-18 stsp
4325 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4326 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4327 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4328 f6d88e1a 2019-05-29 stsp if (err)
4329 f6d88e1a 2019-05-29 stsp goto done;
4330 f6d88e1a 2019-05-29 stsp } else {
4331 f6d88e1a 2019-05-29 stsp path = strdup("");
4332 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4333 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4334 f6d88e1a 2019-05-29 stsp goto done;
4335 f6d88e1a 2019-05-29 stsp }
4336 6c7ab921 2019-03-18 stsp }
4337 6c7ab921 2019-03-18 stsp
4338 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4339 6c7ab921 2019-03-18 stsp len = strlen(path);
4340 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4341 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4342 6c7ab921 2019-03-18 stsp len--;
4343 6c7ab921 2019-03-18 stsp }
4344 6c7ab921 2019-03-18 stsp done:
4345 01740607 2020-11-04 stsp free(abspath);
4346 6c7ab921 2019-03-18 stsp free(resolved);
4347 d0710d08 2019-07-22 stsp free(cwd);
4348 6c7ab921 2019-03-18 stsp if (err == NULL)
4349 6c7ab921 2019-03-18 stsp *wt_path = path;
4350 6c7ab921 2019-03-18 stsp else
4351 6c7ab921 2019-03-18 stsp free(path);
4352 d00136be 2019-03-26 stsp return err;
4353 d00136be 2019-03-26 stsp }
4354 d00136be 2019-03-26 stsp
4355 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4356 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4357 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4358 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4359 4e68cba3 2019-11-23 stsp void *progress_arg;
4360 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4361 4e68cba3 2019-11-23 stsp };
4362 4e68cba3 2019-11-23 stsp
4363 b88936d3 2023-06-21 stsp static int
4364 b88936d3 2023-06-21 stsp add_noop_status(unsigned char status)
4365 b88936d3 2023-06-21 stsp {
4366 b88936d3 2023-06-21 stsp return (status == GOT_STATUS_ADD ||
4367 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODIFY ||
4368 b88936d3 2023-06-21 stsp status == GOT_STATUS_CONFLICT ||
4369 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODE_CHANGE ||
4370 b88936d3 2023-06-21 stsp status == GOT_STATUS_NO_CHANGE);
4371 b88936d3 2023-06-21 stsp }
4372 b88936d3 2023-06-21 stsp
4373 4e68cba3 2019-11-23 stsp static const struct got_error *
4374 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4375 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4376 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4377 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4378 07f5b47a 2019-06-02 stsp {
4379 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4380 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4381 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4382 6d022e97 2019-08-04 stsp struct stat sb;
4383 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4384 07f5b47a 2019-06-02 stsp
4385 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4386 dbb83fbd 2019-12-12 stsp relpath) == -1)
4387 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4388 dbb83fbd 2019-12-12 stsp
4389 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4390 6d022e97 2019-08-04 stsp if (ie) {
4391 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4392 12463d8b 2019-12-13 stsp de_name, a->repo);
4393 6d022e97 2019-08-04 stsp if (err)
4394 dbb83fbd 2019-12-12 stsp goto done;
4395 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4396 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE &&
4397 b88936d3 2023-06-21 stsp add_noop_status(status))
4398 dbb83fbd 2019-12-12 stsp goto done;
4399 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4400 dbb83fbd 2019-12-12 stsp if (err)
4401 dbb83fbd 2019-12-12 stsp goto done;
4402 6d022e97 2019-08-04 stsp }
4403 07f5b47a 2019-06-02 stsp
4404 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4405 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4406 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4407 9b4603c0 2022-01-31 stsp else
4408 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4409 dbb83fbd 2019-12-12 stsp goto done;
4410 4e68cba3 2019-11-23 stsp }
4411 07f5b47a 2019-06-02 stsp
4412 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4413 4e68cba3 2019-11-23 stsp if (err)
4414 4e68cba3 2019-11-23 stsp goto done;
4415 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4416 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4417 3969253a 2020-03-07 stsp if (err) {
4418 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4419 3969253a 2020-03-07 stsp goto done;
4420 3969253a 2020-03-07 stsp }
4421 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4422 07f5b47a 2019-06-02 stsp if (err) {
4423 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4424 4e68cba3 2019-11-23 stsp goto done;
4425 07f5b47a 2019-06-02 stsp }
4426 4e68cba3 2019-11-23 stsp done:
4427 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4428 dbb83fbd 2019-12-12 stsp if (err)
4429 dbb83fbd 2019-12-12 stsp return err;
4430 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4431 dbb83fbd 2019-12-12 stsp return NULL;
4432 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4433 07f5b47a 2019-06-02 stsp }
4434 07f5b47a 2019-06-02 stsp
4435 d00136be 2019-03-26 stsp const struct got_error *
4436 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4437 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4438 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4439 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4440 d00136be 2019-03-26 stsp {
4441 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4442 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4443 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4444 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4445 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4446 d00136be 2019-03-26 stsp
4447 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4448 d00136be 2019-03-26 stsp if (err)
4449 d00136be 2019-03-26 stsp return err;
4450 d00136be 2019-03-26 stsp
4451 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4452 d00136be 2019-03-26 stsp if (err)
4453 d00136be 2019-03-26 stsp goto done;
4454 d00136be 2019-03-26 stsp
4455 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4456 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4457 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4458 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4459 4e68cba3 2019-11-23 stsp saa.repo = repo;
4460 4e68cba3 2019-11-23 stsp
4461 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4462 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4463 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4464 1dd54920 2019-05-11 stsp if (err)
4465 af12c6b9 2019-06-04 stsp break;
4466 1dd54920 2019-05-11 stsp }
4467 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4468 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4469 af12c6b9 2019-06-04 stsp err = sync_err;
4470 d00136be 2019-03-26 stsp done:
4471 fb399478 2019-07-12 stsp free(fileindex_path);
4472 d00136be 2019-03-26 stsp if (fileindex)
4473 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4474 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4475 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4476 d00136be 2019-03-26 stsp err = unlockerr;
4477 6c7ab921 2019-03-18 stsp return err;
4478 6c7ab921 2019-03-18 stsp }
4479 17ed4618 2019-06-02 stsp
4480 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4481 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4482 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4483 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4484 f2a9dc41 2019-12-13 tracey void *progress_arg;
4485 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4486 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4487 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4488 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4489 7f4e5320 2023-05-29 stsp const char *status_path;
4490 7f4e5320 2023-05-29 stsp size_t status_path_len;
4491 766841c2 2020-08-13 stsp const char *status_codes;
4492 f2a9dc41 2019-12-13 tracey };
4493 f2a9dc41 2019-12-13 tracey
4494 f2a9dc41 2019-12-13 tracey static const struct got_error *
4495 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4496 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4497 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4498 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4499 17ed4618 2019-06-02 stsp {
4500 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4501 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4502 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4503 17ed4618 2019-06-02 stsp struct stat sb;
4504 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4505 4e12cd97 2022-01-25 stsp
4506 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4507 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4508 4e12cd97 2022-01-25 stsp return NULL;
4509 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4510 4e12cd97 2022-01-25 stsp }
4511 17ed4618 2019-06-02 stsp
4512 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4513 17ed4618 2019-06-02 stsp if (ie == NULL)
4514 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4515 2ec1f75b 2019-03-26 stsp
4516 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4517 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4518 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4519 9acbc4fa 2019-08-03 stsp return NULL;
4520 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4521 9acbc4fa 2019-08-03 stsp }
4522 9acbc4fa 2019-08-03 stsp
4523 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4524 f2a9dc41 2019-12-13 tracey relpath) == -1)
4525 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4526 f2a9dc41 2019-12-13 tracey
4527 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4528 12463d8b 2019-12-13 stsp a->repo);
4529 17ed4618 2019-06-02 stsp if (err)
4530 f2a9dc41 2019-12-13 tracey goto done;
4531 17ed4618 2019-06-02 stsp
4532 766841c2 2020-08-13 stsp if (a->status_codes) {
4533 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4534 766841c2 2020-08-13 stsp int i;
4535 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4536 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4537 766841c2 2020-08-13 stsp break;
4538 766841c2 2020-08-13 stsp }
4539 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4540 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4541 766841c2 2020-08-13 stsp free(ondisk_path);
4542 766841c2 2020-08-13 stsp return NULL;
4543 766841c2 2020-08-13 stsp }
4544 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4545 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4546 766841c2 2020-08-13 stsp static char msg[64];
4547 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4548 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4549 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4550 766841c2 2020-08-13 stsp goto done;
4551 766841c2 2020-08-13 stsp }
4552 766841c2 2020-08-13 stsp }
4553 766841c2 2020-08-13 stsp
4554 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4555 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4556 f2a9dc41 2019-12-13 tracey goto done;
4557 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4558 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4559 f2a9dc41 2019-12-13 tracey goto done;
4560 f2a9dc41 2019-12-13 tracey }
4561 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4562 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4563 4e12cd97 2022-01-25 stsp goto done;
4564 4e12cd97 2022-01-25 stsp }
4565 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4566 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4567 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4568 f2a9dc41 2019-12-13 tracey goto done;
4569 f2a9dc41 2019-12-13 tracey }
4570 17ed4618 2019-06-02 stsp }
4571 17ed4618 2019-06-02 stsp
4572 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4573 20a2ad1c 2020-10-20 stsp size_t root_len;
4574 20a2ad1c 2020-10-20 stsp
4575 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4576 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4577 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4578 12463d8b 2019-12-13 stsp ondisk_path);
4579 12463d8b 2019-12-13 stsp goto done;
4580 12463d8b 2019-12-13 stsp }
4581 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4582 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4583 12463d8b 2019-12-13 stsp goto done;
4584 15341bfd 2020-03-05 tracey }
4585 15341bfd 2020-03-05 tracey
4586 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4587 20a2ad1c 2020-10-20 stsp do {
4588 20a2ad1c 2020-10-20 stsp char *parent;
4589 7f4e5320 2023-05-29 stsp
4590 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4591 20a2ad1c 2020-10-20 stsp if (err)
4592 2513f20a 2020-10-20 stsp goto done;
4593 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4594 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4595 7f4e5320 2023-05-29 stsp if (got_path_cmp(ondisk_path, a->status_path,
4596 7f4e5320 2023-05-29 stsp strlen(ondisk_path), a->status_path_len) != 0 &&
4597 7f4e5320 2023-05-29 stsp !got_path_is_child(ondisk_path, a->status_path,
4598 7f4e5320 2023-05-29 stsp a->status_path_len))
4599 7f4e5320 2023-05-29 stsp break;
4600 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4601 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4602 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4603 20a2ad1c 2020-10-20 stsp ondisk_path);
4604 15341bfd 2020-03-05 tracey break;
4605 15341bfd 2020-03-05 tracey }
4606 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4607 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4608 f2a9dc41 2019-12-13 tracey }
4609 17ed4618 2019-06-02 stsp
4610 f6635657 2023-08-25 stsp if (got_fileindex_entry_has_blob(ie))
4611 f6635657 2023-08-25 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4612 f6635657 2023-08-25 stsp else
4613 f6635657 2023-08-25 stsp got_fileindex_entry_remove(a->fileindex, ie);
4614 f2a9dc41 2019-12-13 tracey done:
4615 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4616 f2a9dc41 2019-12-13 tracey if (err)
4617 f2a9dc41 2019-12-13 tracey return err;
4618 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4619 f2a9dc41 2019-12-13 tracey return NULL;
4620 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4621 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4622 17ed4618 2019-06-02 stsp }
4623 17ed4618 2019-06-02 stsp
4624 2ec1f75b 2019-03-26 stsp const struct got_error *
4625 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4626 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4627 766841c2 2020-08-13 stsp const char *status_codes,
4628 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4629 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4630 2ec1f75b 2019-03-26 stsp {
4631 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4632 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4633 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4634 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4635 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4636 2ec1f75b 2019-03-26 stsp
4637 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4638 2ec1f75b 2019-03-26 stsp if (err)
4639 2ec1f75b 2019-03-26 stsp return err;
4640 2ec1f75b 2019-03-26 stsp
4641 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4642 2ec1f75b 2019-03-26 stsp if (err)
4643 2ec1f75b 2019-03-26 stsp goto done;
4644 f2a9dc41 2019-12-13 tracey
4645 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4646 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4647 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4648 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4649 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4650 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4651 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4652 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4653 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4654 2ec1f75b 2019-03-26 stsp
4655 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4656 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
4657 7f4e5320 2023-05-29 stsp
4658 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s%s%s",
4659 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree),
4660 7f4e5320 2023-05-29 stsp pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4661 7f4e5320 2023-05-29 stsp err = got_error_from_errno("asprintf");
4662 7f4e5320 2023-05-29 stsp goto done;
4663 7f4e5320 2023-05-29 stsp }
4664 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
4665 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
4666 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4667 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4668 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
4669 17ed4618 2019-06-02 stsp if (err)
4670 af12c6b9 2019-06-04 stsp break;
4671 2ec1f75b 2019-03-26 stsp }
4672 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4673 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4674 af12c6b9 2019-06-04 stsp err = sync_err;
4675 2ec1f75b 2019-03-26 stsp done:
4676 fb399478 2019-07-12 stsp free(fileindex_path);
4677 2ec1f75b 2019-03-26 stsp if (fileindex)
4678 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4679 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4680 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4681 2ec1f75b 2019-03-26 stsp err = unlockerr;
4682 33aa809d 2019-08-08 stsp return err;
4683 33aa809d 2019-08-08 stsp }
4684 33aa809d 2019-08-08 stsp
4685 33aa809d 2019-08-08 stsp static const struct got_error *
4686 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4687 33aa809d 2019-08-08 stsp {
4688 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4689 33aa809d 2019-08-08 stsp char *line = NULL;
4690 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4691 33aa809d 2019-08-08 stsp ssize_t linelen;
4692 33aa809d 2019-08-08 stsp
4693 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4694 33aa809d 2019-08-08 stsp if (linelen == -1) {
4695 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4696 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4697 33aa809d 2019-08-08 stsp goto done;
4698 33aa809d 2019-08-08 stsp }
4699 33aa809d 2019-08-08 stsp return NULL;
4700 33aa809d 2019-08-08 stsp }
4701 33aa809d 2019-08-08 stsp if (outfile) {
4702 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4703 33aa809d 2019-08-08 stsp if (n != linelen) {
4704 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4705 33aa809d 2019-08-08 stsp goto done;
4706 33aa809d 2019-08-08 stsp }
4707 33aa809d 2019-08-08 stsp }
4708 33aa809d 2019-08-08 stsp if (rejectfile) {
4709 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4710 33aa809d 2019-08-08 stsp if (n != linelen)
4711 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4712 33aa809d 2019-08-08 stsp }
4713 33aa809d 2019-08-08 stsp done:
4714 33aa809d 2019-08-08 stsp free(line);
4715 2ec1f75b 2019-03-26 stsp return err;
4716 2ec1f75b 2019-03-26 stsp }
4717 1f1abb7e 2019-08-08 stsp
4718 33aa809d 2019-08-08 stsp static const struct got_error *
4719 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4720 33aa809d 2019-08-08 stsp {
4721 33aa809d 2019-08-08 stsp char *line = NULL;
4722 33aa809d 2019-08-08 stsp size_t linesize = 0;
4723 33aa809d 2019-08-08 stsp ssize_t linelen;
4724 33aa809d 2019-08-08 stsp
4725 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4726 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4727 500467ff 2019-09-25 hiltjo if (ferror(f))
4728 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4729 500467ff 2019-09-25 hiltjo return NULL;
4730 500467ff 2019-09-25 hiltjo }
4731 33aa809d 2019-08-08 stsp free(line);
4732 33aa809d 2019-08-08 stsp return NULL;
4733 33aa809d 2019-08-08 stsp }
4734 33aa809d 2019-08-08 stsp
4735 33aa809d 2019-08-08 stsp static const struct got_error *
4736 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4737 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4738 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4739 abc59930 2021-09-05 naddy {
4740 33aa809d 2019-08-08 stsp const struct got_error *err;
4741 33aa809d 2019-08-08 stsp
4742 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4743 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4744 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4745 33aa809d 2019-08-08 stsp if (err)
4746 33aa809d 2019-08-08 stsp return err;
4747 33aa809d 2019-08-08 stsp (*line_cur1)++;
4748 33aa809d 2019-08-08 stsp }
4749 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4750 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4751 33aa809d 2019-08-08 stsp if (rejectfile)
4752 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4753 33aa809d 2019-08-08 stsp else
4754 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4755 33aa809d 2019-08-08 stsp if (err)
4756 33aa809d 2019-08-08 stsp return err;
4757 33aa809d 2019-08-08 stsp (*line_cur2)++;
4758 33aa809d 2019-08-08 stsp }
4759 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4760 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4761 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4762 33aa809d 2019-08-08 stsp if (err)
4763 33aa809d 2019-08-08 stsp return err;
4764 33aa809d 2019-08-08 stsp (*line_cur2)++;
4765 33aa809d 2019-08-08 stsp }
4766 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4767 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4768 33aa809d 2019-08-08 stsp if (rejectfile)
4769 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4770 33aa809d 2019-08-08 stsp else
4771 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4772 33aa809d 2019-08-08 stsp if (err)
4773 33aa809d 2019-08-08 stsp return err;
4774 33aa809d 2019-08-08 stsp (*line_cur1)++;
4775 33aa809d 2019-08-08 stsp }
4776 f1e81a05 2019-08-10 stsp
4777 f1e81a05 2019-08-10 stsp return NULL;
4778 f1e81a05 2019-08-10 stsp }
4779 f1e81a05 2019-08-10 stsp
4780 f1e81a05 2019-08-10 stsp static const struct got_error *
4781 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4782 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4783 f1e81a05 2019-08-10 stsp {
4784 f1e81a05 2019-08-10 stsp const struct got_error *err;
4785 f1e81a05 2019-08-10 stsp
4786 f1e81a05 2019-08-10 stsp if (outfile) {
4787 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4788 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4789 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4790 f1e81a05 2019-08-10 stsp if (err)
4791 f1e81a05 2019-08-10 stsp return err;
4792 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4793 f1e81a05 2019-08-10 stsp }
4794 f1e81a05 2019-08-10 stsp }
4795 f1e81a05 2019-08-10 stsp if (rejectfile) {
4796 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4797 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4798 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4799 f1e81a05 2019-08-10 stsp if (err)
4800 f1e81a05 2019-08-10 stsp return err;
4801 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4802 f1e81a05 2019-08-10 stsp }
4803 33aa809d 2019-08-08 stsp }
4804 33aa809d 2019-08-08 stsp
4805 33aa809d 2019-08-08 stsp return NULL;
4806 33aa809d 2019-08-08 stsp }
4807 33aa809d 2019-08-08 stsp
4808 33aa809d 2019-08-08 stsp static const struct got_error *
4809 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4810 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4811 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4812 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4813 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4814 33aa809d 2019-08-08 stsp {
4815 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4816 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4817 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4818 33aa809d 2019-08-08 stsp FILE *hunkfile;
4819 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4820 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4821 fe621944 2020-11-10 stsp int rc;
4822 33aa809d 2019-08-08 stsp
4823 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4824 33aa809d 2019-08-08 stsp
4825 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4826 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4827 fe621944 2020-11-10 stsp start_old = cc.left.start;
4828 fe621944 2020-11-10 stsp end_old = cc.left.end;
4829 fe621944 2020-11-10 stsp start_new = cc.right.start;
4830 fe621944 2020-11-10 stsp end_new = cc.right.end;
4831 33aa809d 2019-08-08 stsp
4832 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4833 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4834 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4835 33aa809d 2019-08-08 stsp
4836 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4837 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4838 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4839 33aa809d 2019-08-08 stsp
4840 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4841 fe621944 2020-11-10 stsp if (diff_state == NULL)
4842 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4843 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4844 fe621944 2020-11-10 stsp
4845 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4846 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4847 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4848 33aa809d 2019-08-08 stsp goto done;
4849 33aa809d 2019-08-08 stsp }
4850 fe621944 2020-11-10 stsp
4851 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4852 fe621944 2020-11-10 stsp diff_result, &cc);
4853 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4854 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4855 33aa809d 2019-08-08 stsp goto done;
4856 33aa809d 2019-08-08 stsp }
4857 fe621944 2020-11-10 stsp
4858 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4859 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4860 33aa809d 2019-08-08 stsp goto done;
4861 33aa809d 2019-08-08 stsp }
4862 33aa809d 2019-08-08 stsp
4863 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4864 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4865 33aa809d 2019-08-08 stsp if (err)
4866 33aa809d 2019-08-08 stsp goto done;
4867 33aa809d 2019-08-08 stsp
4868 33aa809d 2019-08-08 stsp switch (*choice) {
4869 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4870 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4871 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4872 33aa809d 2019-08-08 stsp break;
4873 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4874 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4875 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4876 33aa809d 2019-08-08 stsp break;
4877 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4878 33aa809d 2019-08-08 stsp break;
4879 33aa809d 2019-08-08 stsp default:
4880 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4881 33aa809d 2019-08-08 stsp break;
4882 33aa809d 2019-08-08 stsp }
4883 33aa809d 2019-08-08 stsp done:
4884 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4885 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4886 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4887 33aa809d 2019-08-08 stsp return err;
4888 33aa809d 2019-08-08 stsp }
4889 33aa809d 2019-08-08 stsp
4890 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4891 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4892 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4893 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4894 1f1abb7e 2019-08-08 stsp void *progress_arg;
4895 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4896 33aa809d 2019-08-08 stsp void *patch_arg;
4897 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4898 f259c4c1 2021-09-24 stsp int unlink_added_files;
4899 af179be7 2023-04-14 stsp struct got_pathlist_head *added_files_to_unlink;
4900 1f1abb7e 2019-08-08 stsp };
4901 a129376b 2019-03-28 stsp
4902 e20a8b6f 2019-06-04 stsp static const struct got_error *
4903 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4904 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4905 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4906 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4907 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4908 33aa809d 2019-08-08 stsp {
4909 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4910 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4911 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4912 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4913 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4914 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4915 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4916 fe621944 2020-11-10 stsp struct stat sb2;
4917 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4918 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4919 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4920 33aa809d 2019-08-08 stsp
4921 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4922 33aa809d 2019-08-08 stsp
4923 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4924 33aa809d 2019-08-08 stsp if (err)
4925 33aa809d 2019-08-08 stsp return err;
4926 33aa809d 2019-08-08 stsp
4927 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4928 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4929 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4930 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4931 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4932 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4933 fa3cef63 2020-07-23 stsp goto done;
4934 fa3cef63 2020-07-23 stsp }
4935 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4936 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4937 c0df5966 2021-12-31 stsp if (link_len == -1) {
4938 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4939 c0df5966 2021-12-31 stsp path2);
4940 c0df5966 2021-12-31 stsp }
4941 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4942 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4943 12463d8b 2019-12-13 stsp }
4944 12463d8b 2019-12-13 stsp } else {
4945 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4946 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4947 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4948 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4949 fa3cef63 2020-07-23 stsp goto done;
4950 fa3cef63 2020-07-23 stsp }
4951 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4952 fa3cef63 2020-07-23 stsp sizeof(link_target));
4953 fa3cef63 2020-07-23 stsp if (link_len == -1)
4954 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4955 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4956 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4957 12463d8b 2019-12-13 stsp }
4958 1ebedb77 2019-10-19 stsp }
4959 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4960 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4961 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4962 fa3cef63 2020-07-23 stsp goto done;
4963 fa3cef63 2020-07-23 stsp }
4964 1ebedb77 2019-10-19 stsp
4965 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4966 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4967 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4968 fa3cef63 2020-07-23 stsp goto done;
4969 fa3cef63 2020-07-23 stsp }
4970 fa3cef63 2020-07-23 stsp fd2 = -1;
4971 fa3cef63 2020-07-23 stsp } else {
4972 fa3cef63 2020-07-23 stsp size_t n;
4973 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4974 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4975 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4976 fa3cef63 2020-07-23 stsp goto done;
4977 fa3cef63 2020-07-23 stsp }
4978 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4979 fa3cef63 2020-07-23 stsp if (n != link_len) {
4980 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4981 fa3cef63 2020-07-23 stsp goto done;
4982 fa3cef63 2020-07-23 stsp }
4983 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4984 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4985 fa3cef63 2020-07-23 stsp goto done;
4986 fa3cef63 2020-07-23 stsp }
4987 fa3cef63 2020-07-23 stsp rewind(f2);
4988 33aa809d 2019-08-08 stsp }
4989 33aa809d 2019-08-08 stsp
4990 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4991 eb81bc23 2022-06-28 tracey if (fd == -1) {
4992 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4993 eb81bc23 2022-06-28 tracey goto done;
4994 eb81bc23 2022-06-28 tracey }
4995 eb81bc23 2022-06-28 tracey
4996 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4997 33aa809d 2019-08-08 stsp if (err)
4998 33aa809d 2019-08-08 stsp goto done;
4999 33aa809d 2019-08-08 stsp
5000 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
5001 33aa809d 2019-08-08 stsp if (err)
5002 33aa809d 2019-08-08 stsp goto done;
5003 33aa809d 2019-08-08 stsp
5004 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5005 33aa809d 2019-08-08 stsp if (err)
5006 33aa809d 2019-08-08 stsp goto done;
5007 33aa809d 2019-08-08 stsp
5008 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5009 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5010 33aa809d 2019-08-08 stsp if (err)
5011 33aa809d 2019-08-08 stsp goto done;
5012 33aa809d 2019-08-08 stsp
5013 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5014 b90054ed 2022-11-01 stsp "");
5015 33aa809d 2019-08-08 stsp if (err)
5016 33aa809d 2019-08-08 stsp goto done;
5017 33aa809d 2019-08-08 stsp
5018 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5019 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
5020 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5021 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
5022 fe621944 2020-11-10 stsp
5023 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
5024 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5025 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
5026 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
5027 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
5028 fe621944 2020-11-10 stsp nchanges++;
5029 fe621944 2020-11-10 stsp }
5030 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5031 33aa809d 2019-08-08 stsp int choice;
5032 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
5033 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
5034 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
5035 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
5036 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
5037 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
5038 33aa809d 2019-08-08 stsp if (err)
5039 33aa809d 2019-08-08 stsp goto done;
5040 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5041 33aa809d 2019-08-08 stsp have_content = 1;
5042 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5043 33aa809d 2019-08-08 stsp break;
5044 33aa809d 2019-08-08 stsp }
5045 1ebedb77 2019-10-19 stsp if (have_content) {
5046 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5047 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
5048 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
5049 1ebedb77 2019-10-19 stsp if (err)
5050 1ebedb77 2019-10-19 stsp goto done;
5051 1ebedb77 2019-10-19 stsp
5052 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
5053 b2b3fce1 2022-10-29 op mode_t mode;
5054 b2b3fce1 2022-10-29 op
5055 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
5056 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
5057 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
5058 fa3cef63 2020-07-23 stsp goto done;
5059 fa3cef63 2020-07-23 stsp }
5060 1ebedb77 2019-10-19 stsp }
5061 1ebedb77 2019-10-19 stsp }
5062 33aa809d 2019-08-08 stsp done:
5063 33aa809d 2019-08-08 stsp free(id_str);
5064 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5065 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5066 33aa809d 2019-08-08 stsp if (blob)
5067 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5068 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5069 fe621944 2020-11-10 stsp if (err == NULL)
5070 fe621944 2020-11-10 stsp err = free_err;
5071 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5072 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5073 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5074 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5075 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5076 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5077 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5078 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5079 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5080 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5081 33aa809d 2019-08-08 stsp if (err || !have_content) {
5082 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5083 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5084 33aa809d 2019-08-08 stsp free(*path_outfile);
5085 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5086 33aa809d 2019-08-08 stsp }
5087 33aa809d 2019-08-08 stsp free(path1);
5088 33aa809d 2019-08-08 stsp return err;
5089 33aa809d 2019-08-08 stsp }
5090 33aa809d 2019-08-08 stsp
5091 33aa809d 2019-08-08 stsp static const struct got_error *
5092 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5093 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5094 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5095 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5096 a129376b 2019-03-28 stsp {
5097 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5098 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5099 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5100 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5101 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
5102 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5103 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5104 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5105 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5106 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5107 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5108 eb81bc23 2022-06-28 tracey int fd = -1;
5109 a129376b 2019-03-28 stsp
5110 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5111 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5112 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5113 d3bcc3d1 2019-08-08 stsp return NULL;
5114 3d69ad8d 2019-08-17 semarie
5115 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5116 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5117 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5118 d3bcc3d1 2019-08-08 stsp
5119 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5120 65084dad 2019-08-08 stsp if (ie == NULL)
5121 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5122 a129376b 2019-03-28 stsp
5123 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5124 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5125 a129376b 2019-03-28 stsp if (err) {
5126 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5127 a129376b 2019-03-28 stsp goto done;
5128 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5129 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5130 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5131 e20a8b6f 2019-06-04 stsp goto done;
5132 e20a8b6f 2019-06-04 stsp }
5133 a129376b 2019-03-28 stsp }
5134 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5135 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5136 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5137 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5138 a129376b 2019-03-28 stsp goto done;
5139 a129376b 2019-03-28 stsp }
5140 a129376b 2019-03-28 stsp } else {
5141 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5142 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5143 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5144 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5145 a129376b 2019-03-28 stsp goto done;
5146 a129376b 2019-03-28 stsp }
5147 a129376b 2019-03-28 stsp } else {
5148 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5149 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5150 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5151 a129376b 2019-03-28 stsp goto done;
5152 a129376b 2019-03-28 stsp }
5153 a129376b 2019-03-28 stsp }
5154 a129376b 2019-03-28 stsp }
5155 a129376b 2019-03-28 stsp
5156 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
5157 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
5158 a44927cc 2022-04-07 stsp if (err)
5159 a44927cc 2022-04-07 stsp goto done;
5160 a44927cc 2022-04-07 stsp
5161 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5162 a9fa2909 2019-07-27 stsp if (err) {
5163 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5164 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5165 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5166 a9fa2909 2019-07-27 stsp goto done;
5167 a9fa2909 2019-07-27 stsp } else {
5168 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5169 a9fa2909 2019-07-27 stsp if (err)
5170 a9fa2909 2019-07-27 stsp goto done;
5171 a9fa2909 2019-07-27 stsp
5172 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5173 1233e6b6 2020-10-19 stsp if (err)
5174 a9fa2909 2019-07-27 stsp goto done;
5175 a9fa2909 2019-07-27 stsp
5176 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5177 1233e6b6 2020-10-19 stsp free(te_name);
5178 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5179 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5180 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5181 a9fa2909 2019-07-27 stsp goto done;
5182 a9fa2909 2019-07-27 stsp }
5183 a129376b 2019-03-28 stsp }
5184 a129376b 2019-03-28 stsp
5185 a129376b 2019-03-28 stsp switch (status) {
5186 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5187 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5188 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5189 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5190 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5191 33aa809d 2019-08-08 stsp if (err)
5192 33aa809d 2019-08-08 stsp goto done;
5193 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5194 33aa809d 2019-08-08 stsp break;
5195 33aa809d 2019-08-08 stsp }
5196 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5197 1f1abb7e 2019-08-08 stsp ie->path);
5198 1ee397ad 2019-07-12 stsp if (err)
5199 1ee397ad 2019-07-12 stsp goto done;
5200 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5201 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
5202 af179be7 2023-04-14 stsp int do_unlink = a->added_files_to_unlink ? 0 : 1;
5203 af179be7 2023-04-14 stsp
5204 af179be7 2023-04-14 stsp if (a->added_files_to_unlink) {
5205 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
5206 af179be7 2023-04-14 stsp
5207 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, a->added_files_to_unlink,
5208 af179be7 2023-04-14 stsp entry) {
5209 af179be7 2023-04-14 stsp if (got_path_cmp(pe->path, relpath,
5210 af179be7 2023-04-14 stsp pe->path_len, strlen(relpath)))
5211 af179be7 2023-04-14 stsp continue;
5212 af179be7 2023-04-14 stsp do_unlink = 1;
5213 af179be7 2023-04-14 stsp break;
5214 af179be7 2023-04-14 stsp }
5215 f259c4c1 2021-09-24 stsp }
5216 af179be7 2023-04-14 stsp
5217 af179be7 2023-04-14 stsp if (do_unlink) {
5218 af179be7 2023-04-14 stsp if (asprintf(&ondisk_path, "%s/%s",
5219 af179be7 2023-04-14 stsp got_worktree_get_root_path(a->worktree),
5220 af179be7 2023-04-14 stsp relpath) == -1) {
5221 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
5222 af179be7 2023-04-14 stsp goto done;
5223 af179be7 2023-04-14 stsp }
5224 af179be7 2023-04-14 stsp if (unlink(ondisk_path) == -1) {
5225 af179be7 2023-04-14 stsp err = got_error_from_errno2("unlink",
5226 af179be7 2023-04-14 stsp ondisk_path);
5227 af179be7 2023-04-14 stsp break;
5228 af179be7 2023-04-14 stsp }
5229 f259c4c1 2021-09-24 stsp }
5230 f259c4c1 2021-09-24 stsp }
5231 a129376b 2019-03-28 stsp break;
5232 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5233 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5234 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5235 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5236 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5237 33aa809d 2019-08-08 stsp if (err)
5238 33aa809d 2019-08-08 stsp goto done;
5239 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5240 33aa809d 2019-08-08 stsp break;
5241 33aa809d 2019-08-08 stsp }
5242 33aa809d 2019-08-08 stsp /* fall through */
5243 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5244 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5245 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5246 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5247 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5248 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5249 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
5250 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
5251 b4b2adf5 2023-02-09 op else
5252 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
5253 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5254 eb81bc23 2022-06-28 tracey if (fd == -1) {
5255 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5256 eb81bc23 2022-06-28 tracey goto done;
5257 eb81bc23 2022-06-28 tracey }
5258 eb81bc23 2022-06-28 tracey
5259 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5260 a129376b 2019-03-28 stsp if (err)
5261 65084dad 2019-08-08 stsp goto done;
5262 65084dad 2019-08-08 stsp
5263 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5264 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5265 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5266 a129376b 2019-03-28 stsp goto done;
5267 65084dad 2019-08-08 stsp }
5268 65084dad 2019-08-08 stsp
5269 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5270 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5271 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5272 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5273 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5274 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5275 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5276 33aa809d 2019-08-08 stsp break;
5277 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5278 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5279 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5280 369fd7e5 2020-07-23 stsp path_content);
5281 369fd7e5 2020-07-23 stsp break;
5282 369fd7e5 2020-07-23 stsp }
5283 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5284 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5285 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5286 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5287 369fd7e5 2020-07-23 stsp } else {
5288 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5289 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5290 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5291 369fd7e5 2020-07-23 stsp goto done;
5292 369fd7e5 2020-07-23 stsp }
5293 33aa809d 2019-08-08 stsp }
5294 33aa809d 2019-08-08 stsp } else {
5295 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5296 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5297 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5298 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5299 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5300 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5301 2e1fa222 2020-07-23 stsp } else {
5302 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5303 2e1fa222 2020-07-23 stsp ie->path,
5304 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5305 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5306 ef623445 2023-09-16 op 0, 1, 0, 0, NULL, a->repo,
5307 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5308 2e1fa222 2020-07-23 stsp }
5309 a129376b 2019-03-28 stsp if (err)
5310 a129376b 2019-03-28 stsp goto done;
5311 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5312 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5313 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5314 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5315 437adc9d 2020-12-10 yzhong blob->id.sha1,
5316 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5317 2e1fa222 2020-07-23 stsp if (err)
5318 2e1fa222 2020-07-23 stsp goto done;
5319 2e1fa222 2020-07-23 stsp }
5320 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5321 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5322 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5323 33aa809d 2019-08-08 stsp }
5324 a129376b 2019-03-28 stsp }
5325 a129376b 2019-03-28 stsp break;
5326 e20a8b6f 2019-06-04 stsp }
5327 a129376b 2019-03-28 stsp default:
5328 1f1abb7e 2019-08-08 stsp break;
5329 a129376b 2019-03-28 stsp }
5330 a129376b 2019-03-28 stsp done:
5331 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5332 33aa809d 2019-08-08 stsp free(path_content);
5333 e20a8b6f 2019-06-04 stsp free(parent_path);
5334 a129376b 2019-03-28 stsp free(tree_path);
5335 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5336 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5337 a129376b 2019-03-28 stsp if (blob)
5338 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5339 a129376b 2019-03-28 stsp if (tree)
5340 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5341 a129376b 2019-03-28 stsp free(tree_id);
5342 a44927cc 2022-04-07 stsp if (base_commit)
5343 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
5344 e20a8b6f 2019-06-04 stsp return err;
5345 e20a8b6f 2019-06-04 stsp }
5346 e20a8b6f 2019-06-04 stsp
5347 e20a8b6f 2019-06-04 stsp const struct got_error *
5348 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5349 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5350 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5351 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5352 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5353 e20a8b6f 2019-06-04 stsp {
5354 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5355 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5356 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5357 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5358 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5359 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5360 e20a8b6f 2019-06-04 stsp
5361 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5362 e20a8b6f 2019-06-04 stsp if (err)
5363 e20a8b6f 2019-06-04 stsp return err;
5364 e20a8b6f 2019-06-04 stsp
5365 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5366 e20a8b6f 2019-06-04 stsp if (err)
5367 e20a8b6f 2019-06-04 stsp goto done;
5368 e20a8b6f 2019-06-04 stsp
5369 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5370 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5371 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5372 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5373 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5374 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5375 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5376 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
5377 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5378 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5379 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5380 e20a8b6f 2019-06-04 stsp if (err)
5381 af12c6b9 2019-06-04 stsp break;
5382 e20a8b6f 2019-06-04 stsp }
5383 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5384 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5385 e20a8b6f 2019-06-04 stsp err = sync_err;
5386 af12c6b9 2019-06-04 stsp done:
5387 fb399478 2019-07-12 stsp free(fileindex_path);
5388 a129376b 2019-03-28 stsp if (fileindex)
5389 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5390 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5391 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5392 a129376b 2019-03-28 stsp err = unlockerr;
5393 c4296144 2019-05-09 stsp return err;
5394 c4296144 2019-05-09 stsp }
5395 c4296144 2019-05-09 stsp
5396 cf066bf8 2019-05-09 stsp static void
5397 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5398 cf066bf8 2019-05-09 stsp {
5399 24519714 2019-05-09 stsp free(ct->path);
5400 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5401 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5402 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5403 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5404 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5405 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5406 cf066bf8 2019-05-09 stsp free(ct);
5407 cf066bf8 2019-05-09 stsp }
5408 24519714 2019-05-09 stsp
5409 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5410 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5411 24519714 2019-05-09 stsp struct got_repository *repo;
5412 24519714 2019-05-09 stsp struct got_worktree *worktree;
5413 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5414 5f8a88c6 2019-08-03 stsp int have_staged_files;
5415 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5416 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5417 12383673 2023-02-18 mark int commit_conflicts;
5418 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5419 2a47b1e5 2022-11-01 stsp FILE *f1;
5420 2a47b1e5 2022-11-01 stsp FILE *f2;
5421 24519714 2019-05-09 stsp };
5422 2a47b1e5 2022-11-01 stsp
5423 2a47b1e5 2022-11-01 stsp /*
5424 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5425 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5426 2a47b1e5 2022-11-01 stsp */
5427 2a47b1e5 2022-11-01 stsp static const struct got_error *
5428 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5429 2a47b1e5 2022-11-01 stsp const char *abspath)
5430 2a47b1e5 2022-11-01 stsp {
5431 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5432 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5433 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5434 2a47b1e5 2022-11-01 stsp
5435 2a47b1e5 2022-11-01 stsp *fd = -1;
5436 2a47b1e5 2022-11-01 stsp
5437 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5438 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5439 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5440 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5441 2a47b1e5 2022-11-01 stsp } else {
5442 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5443 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5444 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5445 2a47b1e5 2022-11-01 stsp }
5446 2a47b1e5 2022-11-01 stsp
5447 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5448 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5449 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5450 2a47b1e5 2022-11-01 stsp
5451 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5452 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5453 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5454 2a47b1e5 2022-11-01 stsp goto done;
5455 2a47b1e5 2022-11-01 stsp }
5456 2a47b1e5 2022-11-01 stsp
5457 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5458 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5459 2a47b1e5 2022-11-01 stsp goto done;
5460 2a47b1e5 2022-11-01 stsp }
5461 2a47b1e5 2022-11-01 stsp done:
5462 2a47b1e5 2022-11-01 stsp if (err) {
5463 2a47b1e5 2022-11-01 stsp close(*fd);
5464 2a47b1e5 2022-11-01 stsp *fd = -1;
5465 2a47b1e5 2022-11-01 stsp }
5466 2a47b1e5 2022-11-01 stsp return err;
5467 2a47b1e5 2022-11-01 stsp }
5468 2a47b1e5 2022-11-01 stsp
5469 2a47b1e5 2022-11-01 stsp static const struct got_error *
5470 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5471 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5472 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5473 2a47b1e5 2022-11-01 stsp {
5474 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5475 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5476 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5477 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5478 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5479 2a47b1e5 2022-11-01 stsp struct stat sb;
5480 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5481 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5482 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5483 2a47b1e5 2022-11-01 stsp
5484 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5485 4ba5cca9 2022-11-01 stsp
5486 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5487 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5488 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5489 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5490 4ba5cca9 2022-11-01 stsp return NULL;
5491 4ba5cca9 2022-11-01 stsp } else {
5492 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5493 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5494 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5495 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5496 4ba5cca9 2022-11-01 stsp return NULL;
5497 4ba5cca9 2022-11-01 stsp }
5498 2a47b1e5 2022-11-01 stsp
5499 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5500 2a47b1e5 2022-11-01 stsp if (err)
5501 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5502 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5503 2a47b1e5 2022-11-01 stsp if (err)
5504 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5505 2a47b1e5 2022-11-01 stsp
5506 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5507 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5508 2a47b1e5 2022-11-01 stsp if (err)
5509 2a47b1e5 2022-11-01 stsp return err;
5510 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5511 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5512 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5513 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5514 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5515 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5516 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5517 2a47b1e5 2022-11-01 stsp }
5518 2a47b1e5 2022-11-01 stsp
5519 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5520 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5521 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5522 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5523 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5524 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5525 2a47b1e5 2022-11-01 stsp break;
5526 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5527 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5528 2a47b1e5 2022-11-01 stsp break;
5529 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5530 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5531 2a47b1e5 2022-11-01 stsp break;
5532 2a47b1e5 2022-11-01 stsp default:
5533 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5534 2a47b1e5 2022-11-01 stsp }
5535 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5536 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5537 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5538 2a47b1e5 2022-11-01 stsp goto done;
5539 2a47b1e5 2022-11-01 stsp }
5540 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5541 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5542 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5543 2a47b1e5 2022-11-01 stsp goto done;
5544 2a47b1e5 2022-11-01 stsp }
5545 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5546 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5547 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5548 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5549 2a47b1e5 2022-11-01 stsp goto done;
5550 2a47b1e5 2022-11-01 stsp }
5551 2a47b1e5 2022-11-01 stsp
5552 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5553 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5554 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5555 2a47b1e5 2022-11-01 stsp goto done;
5556 2a47b1e5 2022-11-01 stsp }
5557 2a47b1e5 2022-11-01 stsp
5558 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5559 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5560 2a47b1e5 2022-11-01 stsp 8192, fd1);
5561 2a47b1e5 2022-11-01 stsp if (err)
5562 2a47b1e5 2022-11-01 stsp goto done;
5563 2a47b1e5 2022-11-01 stsp }
5564 2a47b1e5 2022-11-01 stsp
5565 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5566 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5567 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5568 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5569 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5570 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5571 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5572 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5573 2a47b1e5 2022-11-01 stsp goto done;
5574 2a47b1e5 2022-11-01 stsp }
5575 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5576 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5577 2a47b1e5 2022-11-01 stsp if (err)
5578 2a47b1e5 2022-11-01 stsp goto done;
5579 2a47b1e5 2022-11-01 stsp }
5580 2a47b1e5 2022-11-01 stsp } else {
5581 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5582 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5583 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5584 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5585 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5586 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5587 2a47b1e5 2022-11-01 stsp goto done;
5588 2a47b1e5 2022-11-01 stsp }
5589 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5590 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5591 2a47b1e5 2022-11-01 stsp if (err)
5592 2a47b1e5 2022-11-01 stsp goto done;
5593 2a47b1e5 2022-11-01 stsp }
5594 2a47b1e5 2022-11-01 stsp }
5595 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5596 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5597 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5598 2a47b1e5 2022-11-01 stsp goto done;
5599 2a47b1e5 2022-11-01 stsp }
5600 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5601 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5602 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5603 2a47b1e5 2022-11-01 stsp goto done;
5604 2a47b1e5 2022-11-01 stsp }
5605 2a47b1e5 2022-11-01 stsp fd = -1;
5606 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5607 2a47b1e5 2022-11-01 stsp }
5608 2a47b1e5 2022-11-01 stsp
5609 2a47b1e5 2022-11-01 stsp if (blob1) {
5610 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5611 2a47b1e5 2022-11-01 stsp f1, blob1);
5612 2a47b1e5 2022-11-01 stsp if (err)
5613 2a47b1e5 2022-11-01 stsp goto done;
5614 2a47b1e5 2022-11-01 stsp }
5615 2a47b1e5 2022-11-01 stsp
5616 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5617 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5618 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5619 2a47b1e5 2022-11-01 stsp done:
5620 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5621 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5622 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5623 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5624 2a47b1e5 2022-11-01 stsp if (blob1)
5625 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5626 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5627 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5628 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5629 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5630 2a47b1e5 2022-11-01 stsp return err;
5631 2a47b1e5 2022-11-01 stsp }
5632 cf066bf8 2019-05-09 stsp
5633 c4296144 2019-05-09 stsp static const struct got_error *
5634 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5635 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5636 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5637 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5638 c4296144 2019-05-09 stsp {
5639 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5640 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5641 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5642 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5643 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5644 768aea60 2019-05-09 stsp struct stat sb;
5645 c4296144 2019-05-09 stsp
5646 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5647 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5648 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5649 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5650 5f8a88c6 2019-08-03 stsp return NULL;
5651 5f8a88c6 2019-08-03 stsp } else {
5652 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5653 12383673 2023-02-18 mark printf("C %s\n", relpath);
5654 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5655 12383673 2023-02-18 mark }
5656 c4296144 2019-05-09 stsp
5657 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5658 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5659 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5660 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5661 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5662 5f8a88c6 2019-08-03 stsp return NULL;
5663 5f8a88c6 2019-08-03 stsp }
5664 0b5cc0d6 2019-05-09 stsp
5665 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5666 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5667 036813ee 2019-05-09 stsp goto done;
5668 036813ee 2019-05-09 stsp }
5669 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5670 036813ee 2019-05-09 stsp parent_path = strdup("");
5671 69960a46 2019-05-09 stsp if (parent_path == NULL)
5672 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5673 69960a46 2019-05-09 stsp } else {
5674 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5675 69960a46 2019-05-09 stsp if (err)
5676 69960a46 2019-05-09 stsp return err;
5677 69960a46 2019-05-09 stsp }
5678 c4296144 2019-05-09 stsp
5679 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5680 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5681 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5682 c4296144 2019-05-09 stsp goto done;
5683 768aea60 2019-05-09 stsp }
5684 768aea60 2019-05-09 stsp
5685 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5686 768aea60 2019-05-09 stsp relpath) == -1) {
5687 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5688 768aea60 2019-05-09 stsp goto done;
5689 768aea60 2019-05-09 stsp }
5690 0aeb8099 2020-07-23 stsp
5691 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5692 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5693 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5694 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5695 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5696 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5697 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5698 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5699 0aeb8099 2020-07-23 stsp break;
5700 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5701 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5702 0aeb8099 2020-07-23 stsp break;
5703 0aeb8099 2020-07-23 stsp default:
5704 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5705 0aeb8099 2020-07-23 stsp goto done;
5706 0aeb8099 2020-07-23 stsp }
5707 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5708 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5709 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5710 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5711 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5712 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5713 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5714 12463d8b 2019-12-13 stsp ct->ondisk_path);
5715 12463d8b 2019-12-13 stsp goto done;
5716 12463d8b 2019-12-13 stsp }
5717 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5718 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5719 768aea60 2019-05-09 stsp goto done;
5720 768aea60 2019-05-09 stsp }
5721 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5722 c4296144 2019-05-09 stsp }
5723 c4296144 2019-05-09 stsp
5724 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5725 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5726 44d03001 2019-05-09 stsp relpath) == -1) {
5727 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5728 44d03001 2019-05-09 stsp goto done;
5729 44d03001 2019-05-09 stsp }
5730 44d03001 2019-05-09 stsp
5731 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5732 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5733 35213c7c 2020-07-23 stsp int is_bad_symlink;
5734 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5735 35213c7c 2020-07-23 stsp ssize_t target_len;
5736 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5737 35213c7c 2020-07-23 stsp sizeof(target_path));
5738 35213c7c 2020-07-23 stsp if (target_len == -1) {
5739 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5740 35213c7c 2020-07-23 stsp ct->ondisk_path);
5741 35213c7c 2020-07-23 stsp goto done;
5742 35213c7c 2020-07-23 stsp }
5743 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5744 df6221c7 2023-07-19 stsp target_len, ct->ondisk_path, a->worktree->root_path,
5745 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
5746 35213c7c 2020-07-23 stsp if (err)
5747 35213c7c 2020-07-23 stsp goto done;
5748 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5749 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5750 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5751 35213c7c 2020-07-23 stsp goto done;
5752 35213c7c 2020-07-23 stsp }
5753 35213c7c 2020-07-23 stsp }
5754 35213c7c 2020-07-23 stsp
5755 35213c7c 2020-07-23 stsp
5756 cf066bf8 2019-05-09 stsp ct->status = status;
5757 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5758 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5759 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5760 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5761 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5762 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5763 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5764 b416585c 2019-05-13 stsp goto done;
5765 b416585c 2019-05-13 stsp }
5766 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5767 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5768 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5769 f0b75401 2019-08-03 stsp goto done;
5770 f0b75401 2019-08-03 stsp }
5771 f0b75401 2019-08-03 stsp }
5772 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5773 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5774 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5775 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5776 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5777 036813ee 2019-05-09 stsp goto done;
5778 036813ee 2019-05-09 stsp }
5779 ca2503ea 2019-05-09 stsp }
5780 24519714 2019-05-09 stsp ct->path = strdup(path);
5781 24519714 2019-05-09 stsp if (ct->path == NULL) {
5782 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5783 24519714 2019-05-09 stsp goto done;
5784 24519714 2019-05-09 stsp }
5785 2a47b1e5 2022-11-01 stsp
5786 c78dbc03 2023-08-25 stsp if (a->diff_outfile) {
5787 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5788 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5789 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5790 2a47b1e5 2022-11-01 stsp if (err)
5791 2a47b1e5 2022-11-01 stsp goto done;
5792 2a47b1e5 2022-11-01 stsp }
5793 c78dbc03 2023-08-25 stsp
5794 c78dbc03 2023-08-25 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5795 c4296144 2019-05-09 stsp done:
5796 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5797 ed175427 2019-05-09 stsp free_commitable(ct);
5798 24519714 2019-05-09 stsp free(parent_path);
5799 036813ee 2019-05-09 stsp free(path);
5800 a129376b 2019-03-28 stsp return err;
5801 a129376b 2019-03-28 stsp }
5802 c4296144 2019-05-09 stsp
5803 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5804 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5805 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5806 036813ee 2019-05-09 stsp struct got_repository *);
5807 ed175427 2019-05-09 stsp
5808 ed175427 2019-05-09 stsp static const struct got_error *
5809 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5810 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5811 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5812 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5813 afa376bf 2019-05-09 stsp struct got_repository *repo)
5814 ed175427 2019-05-09 stsp {
5815 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5816 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5817 036813ee 2019-05-09 stsp char *subpath;
5818 ed175427 2019-05-09 stsp
5819 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5820 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5821 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5822 ed175427 2019-05-09 stsp
5823 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5824 ed175427 2019-05-09 stsp if (err)
5825 ed175427 2019-05-09 stsp return err;
5826 ed175427 2019-05-09 stsp
5827 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5828 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5829 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5830 036813ee 2019-05-09 stsp free(subpath);
5831 036813ee 2019-05-09 stsp return err;
5832 036813ee 2019-05-09 stsp }
5833 ed175427 2019-05-09 stsp
5834 036813ee 2019-05-09 stsp static const struct got_error *
5835 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5836 036813ee 2019-05-09 stsp {
5837 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5838 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5839 ed175427 2019-05-09 stsp
5840 036813ee 2019-05-09 stsp *match = 0;
5841 ed175427 2019-05-09 stsp
5842 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5843 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5844 0f63689d 2019-05-10 stsp return NULL;
5845 036813ee 2019-05-09 stsp }
5846 0b5cc0d6 2019-05-09 stsp
5847 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5848 0f63689d 2019-05-10 stsp if (err)
5849 0f63689d 2019-05-10 stsp return err;
5850 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5851 036813ee 2019-05-09 stsp free(ct_parent_path);
5852 036813ee 2019-05-09 stsp return err;
5853 036813ee 2019-05-09 stsp }
5854 0b5cc0d6 2019-05-09 stsp
5855 768aea60 2019-05-09 stsp static mode_t
5856 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5857 768aea60 2019-05-09 stsp {
5858 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5859 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5860 3d9a4ec4 2020-07-23 stsp
5861 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5862 768aea60 2019-05-09 stsp }
5863 768aea60 2019-05-09 stsp
5864 036813ee 2019-05-09 stsp static const struct got_error *
5865 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5866 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5867 036813ee 2019-05-09 stsp {
5868 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5869 ca2503ea 2019-05-09 stsp
5870 036813ee 2019-05-09 stsp *new_te = NULL;
5871 0b5cc0d6 2019-05-09 stsp
5872 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5873 036813ee 2019-05-09 stsp if (err)
5874 036813ee 2019-05-09 stsp goto done;
5875 0b5cc0d6 2019-05-09 stsp
5876 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5877 036813ee 2019-05-09 stsp
5878 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5879 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5880 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5881 5f8a88c6 2019-08-03 stsp else
5882 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5883 036813ee 2019-05-09 stsp done:
5884 036813ee 2019-05-09 stsp if (err && *new_te) {
5885 56e0773d 2019-11-28 stsp free(*new_te);
5886 036813ee 2019-05-09 stsp *new_te = NULL;
5887 036813ee 2019-05-09 stsp }
5888 036813ee 2019-05-09 stsp return err;
5889 036813ee 2019-05-09 stsp }
5890 036813ee 2019-05-09 stsp
5891 036813ee 2019-05-09 stsp static const struct got_error *
5892 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5893 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5894 036813ee 2019-05-09 stsp {
5895 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5896 102b254e 2020-10-19 stsp char *ct_name = NULL;
5897 036813ee 2019-05-09 stsp
5898 036813ee 2019-05-09 stsp *new_te = NULL;
5899 036813ee 2019-05-09 stsp
5900 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5901 036813ee 2019-05-09 stsp if (*new_te == NULL)
5902 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5903 036813ee 2019-05-09 stsp
5904 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5905 102b254e 2020-10-19 stsp if (err)
5906 036813ee 2019-05-09 stsp goto done;
5907 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5908 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5909 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5910 036813ee 2019-05-09 stsp goto done;
5911 036813ee 2019-05-09 stsp }
5912 036813ee 2019-05-09 stsp
5913 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5914 036813ee 2019-05-09 stsp
5915 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5916 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5917 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5918 5f8a88c6 2019-08-03 stsp else
5919 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5920 036813ee 2019-05-09 stsp done:
5921 102b254e 2020-10-19 stsp free(ct_name);
5922 036813ee 2019-05-09 stsp if (err && *new_te) {
5923 56e0773d 2019-11-28 stsp free(*new_te);
5924 036813ee 2019-05-09 stsp *new_te = NULL;
5925 036813ee 2019-05-09 stsp }
5926 036813ee 2019-05-09 stsp return err;
5927 036813ee 2019-05-09 stsp }
5928 036813ee 2019-05-09 stsp
5929 036813ee 2019-05-09 stsp static const struct got_error *
5930 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5931 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5932 036813ee 2019-05-09 stsp {
5933 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5934 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5935 036813ee 2019-05-09 stsp
5936 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5937 036813ee 2019-05-09 stsp if (err)
5938 036813ee 2019-05-09 stsp return err;
5939 036813ee 2019-05-09 stsp if (new_pe == NULL)
5940 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5941 036813ee 2019-05-09 stsp return NULL;
5942 afa376bf 2019-05-09 stsp }
5943 afa376bf 2019-05-09 stsp
5944 afa376bf 2019-05-09 stsp static const struct got_error *
5945 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5946 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5947 afa376bf 2019-05-09 stsp {
5948 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5949 5f8a88c6 2019-08-03 stsp unsigned char status;
5950 0ff8d236 2021-09-28 stsp
5951 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5952 0ff8d236 2021-09-28 stsp return NULL;
5953 5f8a88c6 2019-08-03 stsp
5954 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5955 afa376bf 2019-05-09 stsp ct_path++;
5956 5f8a88c6 2019-08-03 stsp
5957 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5958 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5959 5f8a88c6 2019-08-03 stsp else
5960 5f8a88c6 2019-08-03 stsp status = ct->status;
5961 5f8a88c6 2019-08-03 stsp
5962 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5963 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5964 036813ee 2019-05-09 stsp }
5965 036813ee 2019-05-09 stsp
5966 036813ee 2019-05-09 stsp static const struct got_error *
5967 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5968 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5969 44d03001 2019-05-09 stsp {
5970 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5971 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5972 44d03001 2019-05-09 stsp char *te_path;
5973 44d03001 2019-05-09 stsp
5974 44d03001 2019-05-09 stsp *modified = 0;
5975 44d03001 2019-05-09 stsp
5976 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5977 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5978 44d03001 2019-05-09 stsp te->name) == -1)
5979 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5980 44d03001 2019-05-09 stsp
5981 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5982 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5983 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5984 44d03001 2019-05-09 stsp strlen(te_path));
5985 62d463ca 2020-10-20 naddy if (*modified)
5986 44d03001 2019-05-09 stsp break;
5987 44d03001 2019-05-09 stsp }
5988 44d03001 2019-05-09 stsp
5989 44d03001 2019-05-09 stsp free(te_path);
5990 44d03001 2019-05-09 stsp return err;
5991 44d03001 2019-05-09 stsp }
5992 44d03001 2019-05-09 stsp
5993 44d03001 2019-05-09 stsp static const struct got_error *
5994 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5995 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5996 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5997 036813ee 2019-05-09 stsp {
5998 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5999 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6000 036813ee 2019-05-09 stsp
6001 036813ee 2019-05-09 stsp *ctp = NULL;
6002 036813ee 2019-05-09 stsp
6003 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6004 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6005 036813ee 2019-05-09 stsp char *ct_name = NULL;
6006 036813ee 2019-05-09 stsp int path_matches;
6007 036813ee 2019-05-09 stsp
6008 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6009 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
6010 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
6011 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
6012 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6013 5f8a88c6 2019-08-03 stsp continue;
6014 5f8a88c6 2019-08-03 stsp } else {
6015 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
6016 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
6017 5f8a88c6 2019-08-03 stsp continue;
6018 5f8a88c6 2019-08-03 stsp }
6019 036813ee 2019-05-09 stsp
6020 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6021 036813ee 2019-05-09 stsp continue;
6022 036813ee 2019-05-09 stsp
6023 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6024 62d463ca 2020-10-20 naddy if (err)
6025 036813ee 2019-05-09 stsp return err;
6026 036813ee 2019-05-09 stsp if (!path_matches)
6027 036813ee 2019-05-09 stsp continue;
6028 036813ee 2019-05-09 stsp
6029 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
6030 d34b633e 2020-10-19 stsp if (err)
6031 d34b633e 2020-10-19 stsp return err;
6032 d34b633e 2020-10-19 stsp
6033 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
6034 d34b633e 2020-10-19 stsp free(ct_name);
6035 036813ee 2019-05-09 stsp continue;
6036 d34b633e 2020-10-19 stsp }
6037 d34b633e 2020-10-19 stsp free(ct_name);
6038 036813ee 2019-05-09 stsp
6039 036813ee 2019-05-09 stsp *ctp = ct;
6040 036813ee 2019-05-09 stsp break;
6041 036813ee 2019-05-09 stsp }
6042 2b496619 2019-07-10 stsp
6043 2b496619 2019-07-10 stsp return err;
6044 2b496619 2019-07-10 stsp }
6045 2b496619 2019-07-10 stsp
6046 2b496619 2019-07-10 stsp static const struct got_error *
6047 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6048 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
6049 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
6050 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
6051 2b496619 2019-07-10 stsp struct got_repository *repo)
6052 2b496619 2019-07-10 stsp {
6053 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
6054 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
6055 2b496619 2019-07-10 stsp char *subtree_path;
6056 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
6057 ba580f68 2020-03-22 stsp int nentries;
6058 2b496619 2019-07-10 stsp
6059 2b496619 2019-07-10 stsp *new_tep = NULL;
6060 2b496619 2019-07-10 stsp
6061 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6062 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6063 2b496619 2019-07-10 stsp child_path) == -1)
6064 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6065 036813ee 2019-05-09 stsp
6066 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6067 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6068 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6069 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6070 56e0773d 2019-11-28 stsp
6071 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6072 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6073 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6074 2b496619 2019-07-10 stsp goto done;
6075 2b496619 2019-07-10 stsp }
6076 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6077 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6078 2b496619 2019-07-10 stsp if (err) {
6079 56e0773d 2019-11-28 stsp free(new_te);
6080 2b496619 2019-07-10 stsp goto done;
6081 2b496619 2019-07-10 stsp }
6082 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6083 2b496619 2019-07-10 stsp done:
6084 56e0773d 2019-11-28 stsp free(id);
6085 2b496619 2019-07-10 stsp free(subtree_path);
6086 2b496619 2019-07-10 stsp if (err == NULL)
6087 2b496619 2019-07-10 stsp *new_tep = new_te;
6088 036813ee 2019-05-09 stsp return err;
6089 036813ee 2019-05-09 stsp }
6090 036813ee 2019-05-09 stsp
6091 036813ee 2019-05-09 stsp static const struct got_error *
6092 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6093 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6094 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6095 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6096 036813ee 2019-05-09 stsp struct got_repository *repo)
6097 036813ee 2019-05-09 stsp {
6098 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6099 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6100 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6101 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6102 036813ee 2019-05-09 stsp
6103 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6104 ba580f68 2020-03-22 stsp *nentries = 0;
6105 036813ee 2019-05-09 stsp
6106 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6107 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6108 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6109 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6110 036813ee 2019-05-09 stsp
6111 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6112 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6113 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6114 036813ee 2019-05-09 stsp continue;
6115 036813ee 2019-05-09 stsp
6116 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6117 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6118 036813ee 2019-05-09 stsp continue;
6119 036813ee 2019-05-09 stsp
6120 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6121 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6122 036813ee 2019-05-09 stsp if (err)
6123 036813ee 2019-05-09 stsp goto done;
6124 036813ee 2019-05-09 stsp
6125 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6126 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6127 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6128 036813ee 2019-05-09 stsp if (err)
6129 036813ee 2019-05-09 stsp goto done;
6130 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6131 afa376bf 2019-05-09 stsp if (err)
6132 afa376bf 2019-05-09 stsp goto done;
6133 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6134 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6135 2b496619 2019-07-10 stsp if (err)
6136 2f51b5b3 2019-05-09 stsp goto done;
6137 ba580f68 2020-03-22 stsp (*nentries)++;
6138 2b496619 2019-07-10 stsp } else {
6139 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6140 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6141 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6142 2b496619 2019-07-10 stsp == NULL) {
6143 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6144 2b496619 2019-07-10 stsp child_path, path_base_tree,
6145 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6146 2b496619 2019-07-10 stsp repo);
6147 2b496619 2019-07-10 stsp if (err)
6148 2b496619 2019-07-10 stsp goto done;
6149 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6150 2b496619 2019-07-10 stsp if (err)
6151 2b496619 2019-07-10 stsp goto done;
6152 ba580f68 2020-03-22 stsp (*nentries)++;
6153 9ba0479c 2019-05-10 stsp }
6154 ed175427 2019-05-09 stsp }
6155 2f51b5b3 2019-05-09 stsp }
6156 2f51b5b3 2019-05-09 stsp
6157 2f51b5b3 2019-05-09 stsp if (base_tree) {
6158 56e0773d 2019-11-28 stsp int i, nbase_entries;
6159 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6160 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6161 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6162 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6163 63c5ca5d 2019-08-24 stsp
6164 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6165 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6166 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6167 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6168 63c5ca5d 2019-08-24 stsp if (err)
6169 63c5ca5d 2019-08-24 stsp goto done;
6170 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6171 63c5ca5d 2019-08-24 stsp if (err)
6172 63c5ca5d 2019-08-24 stsp goto done;
6173 ba580f68 2020-03-22 stsp (*nentries)++;
6174 63c5ca5d 2019-08-24 stsp continue;
6175 63c5ca5d 2019-08-24 stsp }
6176 2f51b5b3 2019-05-09 stsp
6177 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6178 44d03001 2019-05-09 stsp int modified;
6179 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6180 036813ee 2019-05-09 stsp if (err)
6181 036813ee 2019-05-09 stsp goto done;
6182 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6183 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6184 2f51b5b3 2019-05-09 stsp if (err)
6185 2f51b5b3 2019-05-09 stsp goto done;
6186 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6187 44d03001 2019-05-09 stsp if (modified) {
6188 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6189 ba580f68 2020-03-22 stsp int nsubentries;
6190 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6191 ba580f68 2020-03-22 stsp &nsubentries, te,
6192 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6193 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6194 44d03001 2019-05-09 stsp if (err)
6195 44d03001 2019-05-09 stsp goto done;
6196 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6197 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6198 ba580f68 2020-03-22 stsp free(new_id);
6199 ba580f68 2020-03-22 stsp continue;
6200 ba580f68 2020-03-22 stsp }
6201 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6202 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6203 56e0773d 2019-11-28 stsp free(new_id);
6204 44d03001 2019-05-09 stsp }
6205 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6206 036813ee 2019-05-09 stsp if (err)
6207 036813ee 2019-05-09 stsp goto done;
6208 ba580f68 2020-03-22 stsp (*nentries)++;
6209 2f51b5b3 2019-05-09 stsp continue;
6210 036813ee 2019-05-09 stsp }
6211 2f51b5b3 2019-05-09 stsp
6212 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6213 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6214 f66c734c 2019-09-22 stsp if (err)
6215 f66c734c 2019-09-22 stsp goto done;
6216 2f51b5b3 2019-05-09 stsp if (ct) {
6217 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6218 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6219 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6220 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
6221 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6222 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6223 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6224 2f51b5b3 2019-05-09 stsp if (err)
6225 2f51b5b3 2019-05-09 stsp goto done;
6226 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6227 2f51b5b3 2019-05-09 stsp if (err)
6228 2f51b5b3 2019-05-09 stsp goto done;
6229 ba580f68 2020-03-22 stsp (*nentries)++;
6230 2f51b5b3 2019-05-09 stsp }
6231 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6232 b416585c 2019-05-13 stsp status_arg);
6233 afa376bf 2019-05-09 stsp if (err)
6234 afa376bf 2019-05-09 stsp goto done;
6235 2f51b5b3 2019-05-09 stsp } else {
6236 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6237 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6238 2f51b5b3 2019-05-09 stsp if (err)
6239 2f51b5b3 2019-05-09 stsp goto done;
6240 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6241 2f51b5b3 2019-05-09 stsp if (err)
6242 2f51b5b3 2019-05-09 stsp goto done;
6243 ba580f68 2020-03-22 stsp (*nentries)++;
6244 2f51b5b3 2019-05-09 stsp }
6245 ca2503ea 2019-05-09 stsp }
6246 ed175427 2019-05-09 stsp }
6247 0b5cc0d6 2019-05-09 stsp
6248 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6249 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6250 ed175427 2019-05-09 stsp done:
6251 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6252 2e1fa222 2020-07-23 stsp return err;
6253 2e1fa222 2020-07-23 stsp }
6254 2e1fa222 2020-07-23 stsp
6255 2e1fa222 2020-07-23 stsp static const struct got_error *
6256 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6257 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6258 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6259 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6260 ebf99748 2019-05-09 stsp {
6261 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6262 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6263 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6264 ebf99748 2019-05-09 stsp
6265 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6266 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6267 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6268 ebf99748 2019-05-09 stsp
6269 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6270 437adc9d 2020-12-10 yzhong
6271 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6272 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6273 437adc9d 2020-12-10 yzhong if (err)
6274 437adc9d 2020-12-10 yzhong goto done;
6275 437adc9d 2020-12-10 yzhong
6276 ebf99748 2019-05-09 stsp if (ie) {
6277 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6278 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6279 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6280 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6281 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6282 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6283 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6284 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6285 437adc9d 2020-12-10 yzhong
6286 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6287 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6288 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6289 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6290 72fd46fa 2019-09-06 stsp !have_staged_files);
6291 ebf99748 2019-05-09 stsp } else
6292 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6293 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6294 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6295 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6296 72fd46fa 2019-09-06 stsp !have_staged_files);
6297 ebf99748 2019-05-09 stsp } else {
6298 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6299 ebf99748 2019-05-09 stsp if (err)
6300 437adc9d 2020-12-10 yzhong goto done;
6301 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6302 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6303 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6304 3969253a 2020-03-07 stsp if (err) {
6305 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6306 437adc9d 2020-12-10 yzhong goto done;
6307 3969253a 2020-03-07 stsp }
6308 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6309 3969253a 2020-03-07 stsp if (err) {
6310 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6311 437adc9d 2020-12-10 yzhong goto done;
6312 3969253a 2020-03-07 stsp }
6313 ebf99748 2019-05-09 stsp }
6314 437adc9d 2020-12-10 yzhong free(relpath);
6315 437adc9d 2020-12-10 yzhong relpath = NULL;
6316 ebf99748 2019-05-09 stsp }
6317 437adc9d 2020-12-10 yzhong done:
6318 437adc9d 2020-12-10 yzhong free(relpath);
6319 d56d26ce 2019-05-10 stsp return err;
6320 d56d26ce 2019-05-10 stsp }
6321 735ef5ac 2019-08-03 stsp
6322 d56d26ce 2019-05-10 stsp
6323 d56d26ce 2019-05-10 stsp static const struct got_error *
6324 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6325 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6326 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6327 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6328 735ef5ac 2019-08-03 stsp int ood_errcode)
6329 d56d26ce 2019-05-10 stsp {
6330 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6331 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6332 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6333 1a36436d 2019-06-10 stsp
6334 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6335 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6336 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6337 1a36436d 2019-06-10 stsp return NULL;
6338 9bead371 2019-07-28 stsp /*
6339 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6340 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6341 9bead371 2019-07-28 stsp */
6342 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6343 a44927cc 2022-04-07 stsp if (err)
6344 a44927cc 2022-04-07 stsp goto done;
6345 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6346 9bead371 2019-07-28 stsp if (err) {
6347 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6348 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6349 1a36436d 2019-06-10 stsp goto done;
6350 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6351 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6352 1a36436d 2019-06-10 stsp } else {
6353 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6354 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6355 a44927cc 2022-04-07 stsp if (err)
6356 a44927cc 2022-04-07 stsp goto done;
6357 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6358 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6359 1a36436d 2019-06-10 stsp goto done;
6360 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6361 1a36436d 2019-06-10 stsp }
6362 1a36436d 2019-06-10 stsp done:
6363 1a36436d 2019-06-10 stsp free(id);
6364 a44927cc 2022-04-07 stsp if (commit)
6365 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6366 1a36436d 2019-06-10 stsp return err;
6367 ebf99748 2019-05-09 stsp }
6368 ebf99748 2019-05-09 stsp
6369 336075a4 2022-06-25 op static const struct got_error *
6370 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6371 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6372 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
6373 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
6374 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
6375 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
6376 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6377 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6378 afa376bf 2019-05-09 stsp struct got_repository *repo)
6379 c4296144 2019-05-09 stsp {
6380 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6381 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6382 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6383 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6384 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6385 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6386 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6387 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6388 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6389 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6390 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6391 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6392 f8399b8f 2022-07-22 stsp time_t timestamp;
6393 c4296144 2019-05-09 stsp
6394 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6395 c4296144 2019-05-09 stsp
6396 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6397 675c7539 2019-05-09 stsp
6398 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6399 588edf97 2019-05-10 stsp if (err)
6400 588edf97 2019-05-10 stsp goto done;
6401 de18fc63 2019-05-09 stsp
6402 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6403 588edf97 2019-05-10 stsp if (err)
6404 588edf97 2019-05-10 stsp goto done;
6405 588edf97 2019-05-10 stsp
6406 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6407 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6408 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6409 33ad4cbe 2019-05-12 jcs if (err)
6410 33ad4cbe 2019-05-12 jcs goto done;
6411 33ad4cbe 2019-05-12 jcs }
6412 c4296144 2019-05-09 stsp
6413 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6414 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6415 33ad4cbe 2019-05-12 jcs goto done;
6416 33ad4cbe 2019-05-12 jcs }
6417 33ad4cbe 2019-05-12 jcs
6418 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6419 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6420 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6421 cf066bf8 2019-05-09 stsp char *ondisk_path;
6422 cf066bf8 2019-05-09 stsp
6423 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6424 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6425 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6426 5f8a88c6 2019-08-03 stsp continue;
6427 5f8a88c6 2019-08-03 stsp
6428 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6429 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6430 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6431 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6432 cf066bf8 2019-05-09 stsp continue;
6433 cf066bf8 2019-05-09 stsp
6434 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6435 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6436 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6437 cf066bf8 2019-05-09 stsp goto done;
6438 cf066bf8 2019-05-09 stsp }
6439 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6440 2e1fa222 2020-07-23 stsp free(ondisk_path);
6441 2e1fa222 2020-07-23 stsp if (err)
6442 cf066bf8 2019-05-09 stsp goto done;
6443 cf066bf8 2019-05-09 stsp }
6444 036813ee 2019-05-09 stsp
6445 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6446 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6447 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6448 036813ee 2019-05-09 stsp if (err)
6449 036813ee 2019-05-09 stsp goto done;
6450 036813ee 2019-05-09 stsp
6451 2b706956 2023-04-17 stsp err = got_object_qid_alloc(&pid, head_commit_id);
6452 de18fc63 2019-05-09 stsp if (err)
6453 de18fc63 2019-05-09 stsp goto done;
6454 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6455 f259c4c1 2021-09-24 stsp nparents++;
6456 f259c4c1 2021-09-24 stsp if (parent_id2) {
6457 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6458 f259c4c1 2021-09-24 stsp if (err)
6459 f259c4c1 2021-09-24 stsp goto done;
6460 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6461 f259c4c1 2021-09-24 stsp nparents++;
6462 f259c4c1 2021-09-24 stsp }
6463 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6464 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6465 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6466 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6467 33ad4cbe 2019-05-12 jcs free(logmsg);
6468 9d40349a 2019-05-09 stsp if (err)
6469 9d40349a 2019-05-09 stsp goto done;
6470 9d40349a 2019-05-09 stsp
6471 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6472 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6473 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6474 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6475 09f5bd90 2019-05-09 stsp goto done;
6476 09f5bd90 2019-05-09 stsp }
6477 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6478 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6479 09f5bd90 2019-05-09 stsp if (err)
6480 09f5bd90 2019-05-09 stsp goto done;
6481 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6482 ebf99748 2019-05-09 stsp if (err)
6483 ebf99748 2019-05-09 stsp goto done;
6484 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6485 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6486 09f5bd90 2019-05-09 stsp goto done;
6487 09f5bd90 2019-05-09 stsp }
6488 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6489 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6490 f2c16586 2019-05-09 stsp if (err)
6491 f2c16586 2019-05-09 stsp goto done;
6492 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6493 f2c16586 2019-05-09 stsp if (err)
6494 f2c16586 2019-05-09 stsp goto done;
6495 f2c16586 2019-05-09 stsp
6496 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6497 09f5bd90 2019-05-09 stsp if (err)
6498 09f5bd90 2019-05-09 stsp goto done;
6499 09f5bd90 2019-05-09 stsp
6500 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6501 ebf99748 2019-05-09 stsp if (err)
6502 ebf99748 2019-05-09 stsp goto done;
6503 c4296144 2019-05-09 stsp done:
6504 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6505 588edf97 2019-05-10 stsp if (head_tree)
6506 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6507 588edf97 2019-05-10 stsp if (head_commit)
6508 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6509 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6510 2f17228e 2019-05-12 stsp if (head_ref2) {
6511 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6512 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6513 2f17228e 2019-05-12 stsp err = unlockerr;
6514 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6515 39cd0ff6 2019-07-12 stsp }
6516 39cd0ff6 2019-07-12 stsp return err;
6517 39cd0ff6 2019-07-12 stsp }
6518 5c1e53bc 2019-07-28 stsp
6519 5c1e53bc 2019-07-28 stsp static const struct got_error *
6520 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6521 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6522 5c1e53bc 2019-07-28 stsp {
6523 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6524 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6525 39cd0ff6 2019-07-12 stsp
6526 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6527 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6528 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6529 5c1e53bc 2019-07-28 stsp
6530 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6531 5c1e53bc 2019-07-28 stsp ct_path++;
6532 5c1e53bc 2019-07-28 stsp
6533 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6534 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6535 5c1e53bc 2019-07-28 stsp break;
6536 5c1e53bc 2019-07-28 stsp }
6537 5c1e53bc 2019-07-28 stsp
6538 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6539 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6540 5c1e53bc 2019-07-28 stsp
6541 5c1e53bc 2019-07-28 stsp return NULL;
6542 5c1e53bc 2019-07-28 stsp }
6543 5c1e53bc 2019-07-28 stsp
6544 f0b75401 2019-08-03 stsp static const struct got_error *
6545 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6546 f0b75401 2019-08-03 stsp {
6547 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6548 f0b75401 2019-08-03 stsp
6549 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6550 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6551 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6552 f0b75401 2019-08-03 stsp }
6553 f0b75401 2019-08-03 stsp
6554 f0b75401 2019-08-03 stsp return NULL;
6555 f0b75401 2019-08-03 stsp }
6556 f0b75401 2019-08-03 stsp
6557 5f8a88c6 2019-08-03 stsp static const struct got_error *
6558 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6559 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6560 5f8a88c6 2019-08-03 stsp {
6561 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6562 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6563 5f8a88c6 2019-08-03 stsp
6564 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6565 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6566 5f8a88c6 2019-08-03 stsp continue;
6567 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6568 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6569 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6570 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6571 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6572 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6573 5f8a88c6 2019-08-03 stsp }
6574 5f8a88c6 2019-08-03 stsp
6575 5f8a88c6 2019-08-03 stsp return NULL;
6576 5f8a88c6 2019-08-03 stsp }
6577 5f8a88c6 2019-08-03 stsp
6578 39cd0ff6 2019-07-12 stsp const struct got_error *
6579 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6580 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6581 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6582 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6583 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6584 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6585 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6586 39cd0ff6 2019-07-12 stsp {
6587 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6588 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6589 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6590 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6591 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6592 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6593 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6594 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6595 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6596 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6597 39cd0ff6 2019-07-12 stsp
6598 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6599 39cd0ff6 2019-07-12 stsp
6600 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6601 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6602 39cd0ff6 2019-07-12 stsp
6603 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6604 39cd0ff6 2019-07-12 stsp if (err)
6605 39cd0ff6 2019-07-12 stsp goto done;
6606 39cd0ff6 2019-07-12 stsp
6607 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6608 39cd0ff6 2019-07-12 stsp if (err)
6609 39cd0ff6 2019-07-12 stsp goto done;
6610 39cd0ff6 2019-07-12 stsp
6611 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6612 39cd0ff6 2019-07-12 stsp if (err)
6613 39cd0ff6 2019-07-12 stsp goto done;
6614 39cd0ff6 2019-07-12 stsp
6615 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6616 39cd0ff6 2019-07-12 stsp if (err)
6617 39cd0ff6 2019-07-12 stsp goto done;
6618 39cd0ff6 2019-07-12 stsp
6619 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6620 5f8a88c6 2019-08-03 stsp &have_staged_files);
6621 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6622 5f8a88c6 2019-08-03 stsp goto done;
6623 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6624 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6625 5f8a88c6 2019-08-03 stsp if (err)
6626 5f8a88c6 2019-08-03 stsp goto done;
6627 5f8a88c6 2019-08-03 stsp }
6628 5f8a88c6 2019-08-03 stsp
6629 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6630 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6631 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6632 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6633 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6634 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6635 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6636 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6637 2a47b1e5 2022-11-01 stsp if (show_diff) {
6638 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6639 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6640 2a47b1e5 2022-11-01 stsp if (err)
6641 2a47b1e5 2022-11-01 stsp goto done;
6642 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6643 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6644 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6645 2a47b1e5 2022-11-01 stsp goto done;
6646 2a47b1e5 2022-11-01 stsp }
6647 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6648 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6649 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6650 2a47b1e5 2022-11-01 stsp goto done;
6651 2a47b1e5 2022-11-01 stsp }
6652 2a47b1e5 2022-11-01 stsp }
6653 2a47b1e5 2022-11-01 stsp
6654 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6655 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6656 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6657 5c1e53bc 2019-07-28 stsp if (err)
6658 5c1e53bc 2019-07-28 stsp goto done;
6659 5c1e53bc 2019-07-28 stsp }
6660 39cd0ff6 2019-07-12 stsp
6661 2a47b1e5 2022-11-01 stsp if (show_diff) {
6662 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6663 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6664 2a47b1e5 2022-11-01 stsp goto done;
6665 2a47b1e5 2022-11-01 stsp }
6666 2a47b1e5 2022-11-01 stsp }
6667 2a47b1e5 2022-11-01 stsp
6668 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6669 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6670 39cd0ff6 2019-07-12 stsp goto done;
6671 5c1e53bc 2019-07-28 stsp }
6672 5c1e53bc 2019-07-28 stsp
6673 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6674 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6675 5c1e53bc 2019-07-28 stsp if (err)
6676 5c1e53bc 2019-07-28 stsp goto done;
6677 39cd0ff6 2019-07-12 stsp }
6678 f0b75401 2019-08-03 stsp
6679 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6680 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6681 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6682 f0b75401 2019-08-03 stsp
6683 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6684 f0b75401 2019-08-03 stsp ct_path++;
6685 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6686 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6687 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6688 b50cabdf 2019-07-12 stsp if (err)
6689 b50cabdf 2019-07-12 stsp goto done;
6690 f0b75401 2019-08-03 stsp
6691 b50cabdf 2019-07-12 stsp }
6692 b50cabdf 2019-07-12 stsp
6693 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6694 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6695 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6696 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6697 39cd0ff6 2019-07-12 stsp if (err)
6698 39cd0ff6 2019-07-12 stsp goto done;
6699 39cd0ff6 2019-07-12 stsp
6700 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6701 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6702 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6703 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6704 39cd0ff6 2019-07-12 stsp err = sync_err;
6705 39cd0ff6 2019-07-12 stsp done:
6706 39cd0ff6 2019-07-12 stsp if (fileindex)
6707 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6708 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6709 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6710 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6711 39cd0ff6 2019-07-12 stsp err = unlockerr;
6712 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6713 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6714 d8bacb93 2023-01-10 mark
6715 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6716 39cd0ff6 2019-07-12 stsp }
6717 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6718 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6719 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6720 2a47b1e5 2022-11-01 stsp free(diff_path);
6721 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6722 2a47b1e5 2022-11-01 stsp err == NULL)
6723 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6724 c4296144 2019-05-09 stsp return err;
6725 8656d6c4 2019-05-20 stsp }
6726 8656d6c4 2019-05-20 stsp
6727 8656d6c4 2019-05-20 stsp const char *
6728 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6729 8656d6c4 2019-05-20 stsp {
6730 8656d6c4 2019-05-20 stsp return ct->path;
6731 8656d6c4 2019-05-20 stsp }
6732 8656d6c4 2019-05-20 stsp
6733 8656d6c4 2019-05-20 stsp unsigned int
6734 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6735 8656d6c4 2019-05-20 stsp {
6736 8656d6c4 2019-05-20 stsp return ct->status;
6737 818c7501 2019-07-11 stsp }
6738 818c7501 2019-07-11 stsp
6739 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6740 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6741 818c7501 2019-07-11 stsp struct got_repository *repo;
6742 818c7501 2019-07-11 stsp };
6743 818c7501 2019-07-11 stsp
6744 818c7501 2019-07-11 stsp static const struct got_error *
6745 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6746 818c7501 2019-07-11 stsp {
6747 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6748 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6749 818c7501 2019-07-11 stsp unsigned char status;
6750 818c7501 2019-07-11 stsp struct stat sb;
6751 818c7501 2019-07-11 stsp char *ondisk_path;
6752 818c7501 2019-07-11 stsp
6753 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6754 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6755 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6756 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6757 818c7501 2019-07-11 stsp
6758 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6759 818c7501 2019-07-11 stsp == -1)
6760 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6761 818c7501 2019-07-11 stsp
6762 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6763 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6764 818c7501 2019-07-11 stsp free(ondisk_path);
6765 818c7501 2019-07-11 stsp if (err)
6766 818c7501 2019-07-11 stsp return err;
6767 818c7501 2019-07-11 stsp
6768 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6769 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6770 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6771 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6772 818c7501 2019-07-11 stsp
6773 818c7501 2019-07-11 stsp return NULL;
6774 818c7501 2019-07-11 stsp }
6775 818c7501 2019-07-11 stsp
6776 818c7501 2019-07-11 stsp const struct got_error *
6777 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6778 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6779 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6780 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6781 818c7501 2019-07-11 stsp {
6782 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6783 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6784 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6785 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6786 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6787 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6788 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6789 818c7501 2019-07-11 stsp
6790 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6791 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6792 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6793 818c7501 2019-07-11 stsp
6794 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6795 818c7501 2019-07-11 stsp if (err)
6796 818c7501 2019-07-11 stsp return err;
6797 818c7501 2019-07-11 stsp
6798 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6799 818c7501 2019-07-11 stsp if (err)
6800 818c7501 2019-07-11 stsp goto done;
6801 818c7501 2019-07-11 stsp
6802 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6803 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6804 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6805 818c7501 2019-07-11 stsp &ok_arg);
6806 818c7501 2019-07-11 stsp if (err)
6807 818c7501 2019-07-11 stsp goto done;
6808 818c7501 2019-07-11 stsp
6809 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6810 818c7501 2019-07-11 stsp if (err)
6811 818c7501 2019-07-11 stsp goto done;
6812 818c7501 2019-07-11 stsp
6813 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6814 818c7501 2019-07-11 stsp if (err)
6815 818c7501 2019-07-11 stsp goto done;
6816 818c7501 2019-07-11 stsp
6817 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6818 818c7501 2019-07-11 stsp if (err)
6819 818c7501 2019-07-11 stsp goto done;
6820 818c7501 2019-07-11 stsp
6821 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6822 818c7501 2019-07-11 stsp 0);
6823 e51d7b55 2020-01-04 stsp if (err)
6824 e51d7b55 2020-01-04 stsp goto done;
6825 e51d7b55 2020-01-04 stsp
6826 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6827 818c7501 2019-07-11 stsp if (err)
6828 818c7501 2019-07-11 stsp goto done;
6829 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6830 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6831 e51d7b55 2020-01-04 stsp goto done;
6832 e51d7b55 2020-01-04 stsp }
6833 818c7501 2019-07-11 stsp
6834 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6835 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6836 818c7501 2019-07-11 stsp if (err)
6837 818c7501 2019-07-11 stsp goto done;
6838 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6839 818c7501 2019-07-11 stsp if (err)
6840 818c7501 2019-07-11 stsp goto done;
6841 818c7501 2019-07-11 stsp
6842 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6843 818c7501 2019-07-11 stsp
6844 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6845 818c7501 2019-07-11 stsp if (err)
6846 818c7501 2019-07-11 stsp goto done;
6847 818c7501 2019-07-11 stsp
6848 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6849 818c7501 2019-07-11 stsp if (err)
6850 818c7501 2019-07-11 stsp goto done;
6851 818c7501 2019-07-11 stsp
6852 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6853 818c7501 2019-07-11 stsp worktree->base_commit_id);
6854 818c7501 2019-07-11 stsp if (err)
6855 818c7501 2019-07-11 stsp goto done;
6856 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6857 818c7501 2019-07-11 stsp if (err)
6858 818c7501 2019-07-11 stsp goto done;
6859 818c7501 2019-07-11 stsp
6860 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6861 818c7501 2019-07-11 stsp if (err)
6862 818c7501 2019-07-11 stsp goto done;
6863 818c7501 2019-07-11 stsp done:
6864 818c7501 2019-07-11 stsp free(fileindex_path);
6865 818c7501 2019-07-11 stsp free(tmp_branch_name);
6866 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6867 818c7501 2019-07-11 stsp free(branch_ref_name);
6868 818c7501 2019-07-11 stsp if (branch_ref)
6869 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6870 818c7501 2019-07-11 stsp if (wt_branch)
6871 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6872 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6873 818c7501 2019-07-11 stsp if (err) {
6874 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6875 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6876 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6877 818c7501 2019-07-11 stsp }
6878 818c7501 2019-07-11 stsp if (*tmp_branch) {
6879 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6880 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6881 818c7501 2019-07-11 stsp }
6882 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6883 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6884 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6885 3e3a69f1 2019-07-25 stsp }
6886 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6887 818c7501 2019-07-11 stsp }
6888 818c7501 2019-07-11 stsp return err;
6889 818c7501 2019-07-11 stsp }
6890 818c7501 2019-07-11 stsp
6891 818c7501 2019-07-11 stsp const struct got_error *
6892 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6893 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6894 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6895 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6896 818c7501 2019-07-11 stsp {
6897 818c7501 2019-07-11 stsp const struct got_error *err;
6898 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6899 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6900 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6901 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6902 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6903 818c7501 2019-07-11 stsp
6904 818c7501 2019-07-11 stsp *commit_id = NULL;
6905 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6906 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6907 3e3a69f1 2019-07-25 stsp *branch = NULL;
6908 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6909 3e3a69f1 2019-07-25 stsp
6910 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6911 3e3a69f1 2019-07-25 stsp if (err)
6912 3e3a69f1 2019-07-25 stsp return err;
6913 818c7501 2019-07-11 stsp
6914 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6915 3e3a69f1 2019-07-25 stsp if (err)
6916 f032f1f7 2019-08-04 stsp goto done;
6917 f032f1f7 2019-08-04 stsp
6918 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6919 f032f1f7 2019-08-04 stsp &have_staged_files);
6920 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6921 f032f1f7 2019-08-04 stsp goto done;
6922 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6923 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6924 3e3a69f1 2019-07-25 stsp goto done;
6925 f032f1f7 2019-08-04 stsp }
6926 3e3a69f1 2019-07-25 stsp
6927 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6928 818c7501 2019-07-11 stsp if (err)
6929 f032f1f7 2019-08-04 stsp goto done;
6930 818c7501 2019-07-11 stsp
6931 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6932 818c7501 2019-07-11 stsp if (err)
6933 818c7501 2019-07-11 stsp goto done;
6934 818c7501 2019-07-11 stsp
6935 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6936 818c7501 2019-07-11 stsp if (err)
6937 818c7501 2019-07-11 stsp goto done;
6938 818c7501 2019-07-11 stsp
6939 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6940 818c7501 2019-07-11 stsp if (err)
6941 818c7501 2019-07-11 stsp goto done;
6942 818c7501 2019-07-11 stsp
6943 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6944 818c7501 2019-07-11 stsp if (err)
6945 818c7501 2019-07-11 stsp goto done;
6946 818c7501 2019-07-11 stsp
6947 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6948 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6949 818c7501 2019-07-11 stsp if (err)
6950 818c7501 2019-07-11 stsp goto done;
6951 818c7501 2019-07-11 stsp
6952 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6953 818c7501 2019-07-11 stsp if (err)
6954 818c7501 2019-07-11 stsp goto done;
6955 818c7501 2019-07-11 stsp
6956 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6957 818c7501 2019-07-11 stsp if (err)
6958 818c7501 2019-07-11 stsp goto done;
6959 818c7501 2019-07-11 stsp
6960 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6961 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6962 818c7501 2019-07-11 stsp if (err)
6963 818c7501 2019-07-11 stsp goto done;
6964 818c7501 2019-07-11 stsp
6965 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6966 818c7501 2019-07-11 stsp if (err)
6967 818c7501 2019-07-11 stsp goto done;
6968 818c7501 2019-07-11 stsp done:
6969 818c7501 2019-07-11 stsp free(commit_ref_name);
6970 818c7501 2019-07-11 stsp free(branch_ref_name);
6971 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6972 818c7501 2019-07-11 stsp if (commit_ref)
6973 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6974 818c7501 2019-07-11 stsp if (branch_ref)
6975 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6976 818c7501 2019-07-11 stsp if (err) {
6977 818c7501 2019-07-11 stsp free(*commit_id);
6978 818c7501 2019-07-11 stsp *commit_id = NULL;
6979 818c7501 2019-07-11 stsp if (*tmp_branch) {
6980 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6981 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6982 818c7501 2019-07-11 stsp }
6983 818c7501 2019-07-11 stsp if (*new_base_branch) {
6984 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6985 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6986 818c7501 2019-07-11 stsp }
6987 818c7501 2019-07-11 stsp if (*branch) {
6988 818c7501 2019-07-11 stsp got_ref_close(*branch);
6989 818c7501 2019-07-11 stsp *branch = NULL;
6990 818c7501 2019-07-11 stsp }
6991 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6992 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6993 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6994 3e3a69f1 2019-07-25 stsp }
6995 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6996 818c7501 2019-07-11 stsp }
6997 818c7501 2019-07-11 stsp return err;
6998 c4296144 2019-05-09 stsp }
6999 818c7501 2019-07-11 stsp
7000 818c7501 2019-07-11 stsp const struct got_error *
7001 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7002 818c7501 2019-07-11 stsp {
7003 818c7501 2019-07-11 stsp const struct got_error *err;
7004 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
7005 818c7501 2019-07-11 stsp
7006 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7007 818c7501 2019-07-11 stsp if (err)
7008 818c7501 2019-07-11 stsp return err;
7009 818c7501 2019-07-11 stsp
7010 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7011 818c7501 2019-07-11 stsp free(tmp_branch_name);
7012 818c7501 2019-07-11 stsp return NULL;
7013 818c7501 2019-07-11 stsp }
7014 818c7501 2019-07-11 stsp
7015 818c7501 2019-07-11 stsp static const struct got_error *
7016 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7017 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7018 818c7501 2019-07-11 stsp {
7019 0ebf8283 2019-07-24 stsp *logmsg = arg;
7020 818c7501 2019-07-11 stsp return NULL;
7021 818c7501 2019-07-11 stsp }
7022 818c7501 2019-07-11 stsp
7023 818c7501 2019-07-11 stsp static const struct got_error *
7024 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7025 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7026 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7027 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7028 818c7501 2019-07-11 stsp {
7029 818c7501 2019-07-11 stsp return NULL;
7030 01757395 2019-07-12 stsp }
7031 01757395 2019-07-12 stsp
7032 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
7033 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
7034 01757395 2019-07-12 stsp void *progress_arg;
7035 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
7036 01757395 2019-07-12 stsp };
7037 01757395 2019-07-12 stsp
7038 01757395 2019-07-12 stsp static const struct got_error *
7039 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
7040 01757395 2019-07-12 stsp {
7041 01757395 2019-07-12 stsp const struct got_error *err;
7042 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
7043 01757395 2019-07-12 stsp char *p;
7044 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
7045 01757395 2019-07-12 stsp
7046 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
7047 01757395 2019-07-12 stsp if (err)
7048 01757395 2019-07-12 stsp return err;
7049 01757395 2019-07-12 stsp
7050 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
7051 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
7052 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
7053 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
7054 01757395 2019-07-12 stsp return NULL;
7055 01757395 2019-07-12 stsp
7056 01757395 2019-07-12 stsp p = strdup(path);
7057 01757395 2019-07-12 stsp if (p == NULL)
7058 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7059 01757395 2019-07-12 stsp
7060 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7061 01757395 2019-07-12 stsp if (err || new == NULL)
7062 01757395 2019-07-12 stsp free(p);
7063 01757395 2019-07-12 stsp return err;
7064 818c7501 2019-07-11 stsp }
7065 818c7501 2019-07-11 stsp
7066 0ebf8283 2019-07-24 stsp static const struct got_error *
7067 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7068 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7069 818c7501 2019-07-11 stsp {
7070 818c7501 2019-07-11 stsp const struct got_error *err;
7071 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7072 818c7501 2019-07-11 stsp
7073 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7074 818c7501 2019-07-11 stsp if (err) {
7075 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7076 818c7501 2019-07-11 stsp goto done;
7077 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7078 818c7501 2019-07-11 stsp if (err)
7079 818c7501 2019-07-11 stsp goto done;
7080 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7081 818c7501 2019-07-11 stsp if (err)
7082 818c7501 2019-07-11 stsp goto done;
7083 de05890f 2020-03-05 stsp } else if (is_rebase) {
7084 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7085 818c7501 2019-07-11 stsp int cmp;
7086 818c7501 2019-07-11 stsp
7087 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7088 818c7501 2019-07-11 stsp if (err)
7089 818c7501 2019-07-11 stsp goto done;
7090 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7091 818c7501 2019-07-11 stsp free(stored_id);
7092 818c7501 2019-07-11 stsp if (cmp != 0) {
7093 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7094 818c7501 2019-07-11 stsp goto done;
7095 818c7501 2019-07-11 stsp }
7096 818c7501 2019-07-11 stsp }
7097 0ebf8283 2019-07-24 stsp done:
7098 0ebf8283 2019-07-24 stsp if (commit_ref)
7099 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7100 0ebf8283 2019-07-24 stsp return err;
7101 0ebf8283 2019-07-24 stsp }
7102 0ebf8283 2019-07-24 stsp
7103 0ebf8283 2019-07-24 stsp static const struct got_error *
7104 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7105 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7106 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7107 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7108 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7109 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7110 0ebf8283 2019-07-24 stsp {
7111 0ebf8283 2019-07-24 stsp const struct got_error *err;
7112 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7113 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7114 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7115 818c7501 2019-07-11 stsp
7116 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7117 0ebf8283 2019-07-24 stsp
7118 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7119 0ebf8283 2019-07-24 stsp if (err)
7120 0ebf8283 2019-07-24 stsp return err;
7121 0ebf8283 2019-07-24 stsp
7122 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7123 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7124 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7125 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7126 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7127 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7128 818c7501 2019-07-11 stsp if (commit_ref)
7129 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7130 818c7501 2019-07-11 stsp return err;
7131 818c7501 2019-07-11 stsp }
7132 818c7501 2019-07-11 stsp
7133 818c7501 2019-07-11 stsp const struct got_error *
7134 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7135 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7136 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7137 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7138 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7139 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7140 818c7501 2019-07-11 stsp {
7141 0ebf8283 2019-07-24 stsp const struct got_error *err;
7142 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7143 0ebf8283 2019-07-24 stsp
7144 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7145 0ebf8283 2019-07-24 stsp if (err)
7146 0ebf8283 2019-07-24 stsp return err;
7147 0ebf8283 2019-07-24 stsp
7148 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7149 0ebf8283 2019-07-24 stsp if (err)
7150 0ebf8283 2019-07-24 stsp goto done;
7151 0ebf8283 2019-07-24 stsp
7152 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7153 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7154 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7155 0ebf8283 2019-07-24 stsp done:
7156 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7157 0ebf8283 2019-07-24 stsp return err;
7158 0ebf8283 2019-07-24 stsp }
7159 0ebf8283 2019-07-24 stsp
7160 0ebf8283 2019-07-24 stsp const struct got_error *
7161 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7162 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7163 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7164 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7165 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7166 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7167 0ebf8283 2019-07-24 stsp {
7168 0ebf8283 2019-07-24 stsp const struct got_error *err;
7169 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7170 0ebf8283 2019-07-24 stsp
7171 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7172 0ebf8283 2019-07-24 stsp if (err)
7173 0ebf8283 2019-07-24 stsp return err;
7174 0ebf8283 2019-07-24 stsp
7175 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7176 0ebf8283 2019-07-24 stsp if (err)
7177 0ebf8283 2019-07-24 stsp goto done;
7178 0ebf8283 2019-07-24 stsp
7179 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7180 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7181 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7182 0ebf8283 2019-07-24 stsp done:
7183 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7184 0ebf8283 2019-07-24 stsp return err;
7185 0ebf8283 2019-07-24 stsp }
7186 0ebf8283 2019-07-24 stsp
7187 0ebf8283 2019-07-24 stsp static const struct got_error *
7188 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7189 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7190 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7191 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
7192 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
7193 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7194 0ebf8283 2019-07-24 stsp {
7195 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7196 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7197 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7198 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7199 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7200 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7201 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7202 818c7501 2019-07-11 stsp
7203 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7204 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7205 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7206 a0e95631 2019-07-12 stsp
7207 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7208 818c7501 2019-07-11 stsp
7209 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7210 a0e95631 2019-07-12 stsp if (err)
7211 3e3a69f1 2019-07-25 stsp return err;
7212 a0e95631 2019-07-12 stsp
7213 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7214 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7215 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7216 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7217 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
7218 01757395 2019-07-12 stsp /*
7219 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7220 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7221 6c13b005 2021-09-02 stsp *
7222 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7223 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7224 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7225 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7226 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7227 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7228 01757395 2019-07-12 stsp */
7229 01757395 2019-07-12 stsp if (merged_paths) {
7230 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7231 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7232 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7233 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7234 f2a9dc41 2019-12-13 tracey 0);
7235 01757395 2019-07-12 stsp if (err)
7236 01757395 2019-07-12 stsp goto done;
7237 01757395 2019-07-12 stsp }
7238 01757395 2019-07-12 stsp } else {
7239 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7240 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7241 01757395 2019-07-12 stsp if (err)
7242 01757395 2019-07-12 stsp goto done;
7243 01757395 2019-07-12 stsp }
7244 a0e95631 2019-07-12 stsp
7245 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7246 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7247 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7248 a0e95631 2019-07-12 stsp if (err)
7249 a0e95631 2019-07-12 stsp goto done;
7250 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7251 a0e95631 2019-07-12 stsp goto done;
7252 ff0d2220 2019-07-11 stsp }
7253 818c7501 2019-07-11 stsp
7254 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7255 edd02c5e 2019-07-11 stsp if (err)
7256 edd02c5e 2019-07-11 stsp goto done;
7257 edd02c5e 2019-07-11 stsp
7258 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7259 818c7501 2019-07-11 stsp if (err)
7260 818c7501 2019-07-11 stsp goto done;
7261 818c7501 2019-07-11 stsp
7262 5943eee2 2019-08-13 stsp if (new_logmsg) {
7263 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7264 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7265 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7266 5943eee2 2019-08-13 stsp goto done;
7267 5943eee2 2019-08-13 stsp }
7268 5943eee2 2019-08-13 stsp } else {
7269 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7270 5943eee2 2019-08-13 stsp if (err)
7271 5943eee2 2019-08-13 stsp goto done;
7272 5943eee2 2019-08-13 stsp }
7273 0ebf8283 2019-07-24 stsp
7274 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7275 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7276 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
7277 50e7a649 2022-07-22 stsp committer ? committer :
7278 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
7279 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7280 a0e95631 2019-07-12 stsp if (err)
7281 a0e95631 2019-07-12 stsp goto done;
7282 a0e95631 2019-07-12 stsp
7283 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7284 a0e95631 2019-07-12 stsp if (err)
7285 a0e95631 2019-07-12 stsp goto done;
7286 a0e95631 2019-07-12 stsp
7287 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7288 a0e95631 2019-07-12 stsp if (err)
7289 a0e95631 2019-07-12 stsp goto done;
7290 a0e95631 2019-07-12 stsp
7291 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7292 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7293 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7294 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7295 a0e95631 2019-07-12 stsp err = sync_err;
7296 818c7501 2019-07-11 stsp done:
7297 a0e95631 2019-07-12 stsp free(fileindex_path);
7298 a0e95631 2019-07-12 stsp free(head_commit_id);
7299 a0e95631 2019-07-12 stsp if (head_ref)
7300 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7301 818c7501 2019-07-11 stsp if (err) {
7302 818c7501 2019-07-11 stsp free(*new_commit_id);
7303 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7304 818c7501 2019-07-11 stsp }
7305 818c7501 2019-07-11 stsp return err;
7306 818c7501 2019-07-11 stsp }
7307 818c7501 2019-07-11 stsp
7308 818c7501 2019-07-11 stsp const struct got_error *
7309 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7310 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7311 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7312 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7313 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
7314 12383673 2023-02-18 mark struct got_repository *repo)
7315 0ebf8283 2019-07-24 stsp {
7316 0ebf8283 2019-07-24 stsp const struct got_error *err;
7317 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7318 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7319 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7320 0ebf8283 2019-07-24 stsp
7321 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7322 0ebf8283 2019-07-24 stsp if (err)
7323 0ebf8283 2019-07-24 stsp return err;
7324 0ebf8283 2019-07-24 stsp
7325 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7326 0ebf8283 2019-07-24 stsp if (err)
7327 0ebf8283 2019-07-24 stsp goto done;
7328 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7329 0ebf8283 2019-07-24 stsp if (err)
7330 0ebf8283 2019-07-24 stsp goto done;
7331 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7332 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7333 0ebf8283 2019-07-24 stsp goto done;
7334 0ebf8283 2019-07-24 stsp }
7335 0ebf8283 2019-07-24 stsp
7336 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7337 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7338 12383673 2023-02-18 mark NULL, allow_conflict, repo);
7339 0ebf8283 2019-07-24 stsp done:
7340 0ebf8283 2019-07-24 stsp if (commit_ref)
7341 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7342 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7343 0ebf8283 2019-07-24 stsp free(commit_id);
7344 0ebf8283 2019-07-24 stsp return err;
7345 0ebf8283 2019-07-24 stsp }
7346 0ebf8283 2019-07-24 stsp
7347 0ebf8283 2019-07-24 stsp const struct got_error *
7348 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7349 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7350 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7351 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7352 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7353 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7354 0ebf8283 2019-07-24 stsp {
7355 0ebf8283 2019-07-24 stsp const struct got_error *err;
7356 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7357 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7358 0ebf8283 2019-07-24 stsp
7359 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7360 0ebf8283 2019-07-24 stsp if (err)
7361 0ebf8283 2019-07-24 stsp return err;
7362 0ebf8283 2019-07-24 stsp
7363 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7364 0ebf8283 2019-07-24 stsp if (err)
7365 0ebf8283 2019-07-24 stsp goto done;
7366 0ebf8283 2019-07-24 stsp
7367 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7368 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7369 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
7370 0ebf8283 2019-07-24 stsp done:
7371 0ebf8283 2019-07-24 stsp if (commit_ref)
7372 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7373 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7374 0ebf8283 2019-07-24 stsp return err;
7375 0ebf8283 2019-07-24 stsp }
7376 0ebf8283 2019-07-24 stsp
7377 0ebf8283 2019-07-24 stsp const struct got_error *
7378 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7379 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7380 818c7501 2019-07-11 stsp {
7381 3e3a69f1 2019-07-25 stsp if (fileindex)
7382 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7383 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7384 69844fba 2019-07-11 stsp }
7385 69844fba 2019-07-11 stsp
7386 69844fba 2019-07-11 stsp static const struct got_error *
7387 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7388 69844fba 2019-07-11 stsp {
7389 69844fba 2019-07-11 stsp const struct got_error *err;
7390 69844fba 2019-07-11 stsp struct got_reference *ref;
7391 69844fba 2019-07-11 stsp
7392 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7393 69844fba 2019-07-11 stsp if (err) {
7394 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7395 69844fba 2019-07-11 stsp return NULL;
7396 69844fba 2019-07-11 stsp return err;
7397 69844fba 2019-07-11 stsp }
7398 69844fba 2019-07-11 stsp
7399 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7400 69844fba 2019-07-11 stsp got_ref_close(ref);
7401 69844fba 2019-07-11 stsp return err;
7402 818c7501 2019-07-11 stsp }
7403 818c7501 2019-07-11 stsp
7404 69844fba 2019-07-11 stsp static const struct got_error *
7405 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7406 69844fba 2019-07-11 stsp {
7407 69844fba 2019-07-11 stsp const struct got_error *err;
7408 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7409 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7410 69844fba 2019-07-11 stsp
7411 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7412 69844fba 2019-07-11 stsp if (err)
7413 69844fba 2019-07-11 stsp goto done;
7414 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7415 69844fba 2019-07-11 stsp if (err)
7416 69844fba 2019-07-11 stsp goto done;
7417 69844fba 2019-07-11 stsp
7418 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7419 69844fba 2019-07-11 stsp if (err)
7420 69844fba 2019-07-11 stsp goto done;
7421 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7422 69844fba 2019-07-11 stsp if (err)
7423 69844fba 2019-07-11 stsp goto done;
7424 69844fba 2019-07-11 stsp
7425 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7426 69844fba 2019-07-11 stsp if (err)
7427 69844fba 2019-07-11 stsp goto done;
7428 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7429 69844fba 2019-07-11 stsp if (err)
7430 69844fba 2019-07-11 stsp goto done;
7431 69844fba 2019-07-11 stsp
7432 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7433 69844fba 2019-07-11 stsp if (err)
7434 69844fba 2019-07-11 stsp goto done;
7435 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7436 69844fba 2019-07-11 stsp if (err)
7437 69844fba 2019-07-11 stsp goto done;
7438 69844fba 2019-07-11 stsp
7439 69844fba 2019-07-11 stsp done:
7440 69844fba 2019-07-11 stsp free(tmp_branch_name);
7441 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7442 69844fba 2019-07-11 stsp free(branch_ref_name);
7443 69844fba 2019-07-11 stsp free(commit_ref_name);
7444 e600f124 2021-03-21 stsp return err;
7445 e600f124 2021-03-21 stsp }
7446 e600f124 2021-03-21 stsp
7447 336075a4 2022-06-25 op static const struct got_error *
7448 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7449 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7450 e600f124 2021-03-21 stsp {
7451 e600f124 2021-03-21 stsp const struct got_error *err;
7452 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7453 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7454 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7455 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7456 e600f124 2021-03-21 stsp char *refname = NULL;
7457 e600f124 2021-03-21 stsp
7458 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7459 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7460 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7461 e600f124 2021-03-21 stsp branch_name += 11;
7462 e600f124 2021-03-21 stsp
7463 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7464 e600f124 2021-03-21 stsp if (err)
7465 e600f124 2021-03-21 stsp return err;
7466 e600f124 2021-03-21 stsp
7467 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7468 e600f124 2021-03-21 stsp new_id_str) == -1) {
7469 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7470 e600f124 2021-03-21 stsp goto done;
7471 e600f124 2021-03-21 stsp }
7472 e600f124 2021-03-21 stsp
7473 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7474 e600f124 2021-03-21 stsp if (err)
7475 e600f124 2021-03-21 stsp goto done;
7476 e600f124 2021-03-21 stsp
7477 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7478 e600f124 2021-03-21 stsp if (err)
7479 e600f124 2021-03-21 stsp goto done;
7480 e600f124 2021-03-21 stsp
7481 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7482 e600f124 2021-03-21 stsp done:
7483 e600f124 2021-03-21 stsp free(new_id_str);
7484 e600f124 2021-03-21 stsp free(refname);
7485 e600f124 2021-03-21 stsp free(old_commit_id);
7486 e600f124 2021-03-21 stsp if (ref)
7487 e600f124 2021-03-21 stsp got_ref_close(ref);
7488 69844fba 2019-07-11 stsp return err;
7489 69844fba 2019-07-11 stsp }
7490 69844fba 2019-07-11 stsp
7491 818c7501 2019-07-11 stsp const struct got_error *
7492 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7493 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7494 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7495 ef85a376 2023-02-01 mark int create_backup)
7496 818c7501 2019-07-11 stsp {
7497 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7498 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7499 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7500 818c7501 2019-07-11 stsp
7501 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7502 818c7501 2019-07-11 stsp if (err)
7503 818c7501 2019-07-11 stsp return err;
7504 e600f124 2021-03-21 stsp
7505 e600f124 2021-03-21 stsp if (create_backup) {
7506 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7507 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7508 e600f124 2021-03-21 stsp if (err)
7509 e600f124 2021-03-21 stsp goto done;
7510 e600f124 2021-03-21 stsp }
7511 818c7501 2019-07-11 stsp
7512 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7513 818c7501 2019-07-11 stsp if (err)
7514 818c7501 2019-07-11 stsp goto done;
7515 818c7501 2019-07-11 stsp
7516 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7517 818c7501 2019-07-11 stsp if (err)
7518 818c7501 2019-07-11 stsp goto done;
7519 818c7501 2019-07-11 stsp
7520 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7521 818c7501 2019-07-11 stsp if (err)
7522 818c7501 2019-07-11 stsp goto done;
7523 818c7501 2019-07-11 stsp
7524 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7525 a615e0e7 2020-12-16 stsp if (err)
7526 a615e0e7 2020-12-16 stsp goto done;
7527 a615e0e7 2020-12-16 stsp
7528 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7529 a615e0e7 2020-12-16 stsp if (err)
7530 a615e0e7 2020-12-16 stsp goto done;
7531 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7532 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7533 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7534 a615e0e7 2020-12-16 stsp err = sync_err;
7535 818c7501 2019-07-11 stsp done:
7536 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7537 a615e0e7 2020-12-16 stsp free(fileindex_path);
7538 818c7501 2019-07-11 stsp free(new_head_commit_id);
7539 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7540 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7541 818c7501 2019-07-11 stsp err = unlockerr;
7542 818c7501 2019-07-11 stsp return err;
7543 818c7501 2019-07-11 stsp }
7544 af179be7 2023-04-14 stsp
7545 af179be7 2023-04-14 stsp static const struct got_error *
7546 af179be7 2023-04-14 stsp get_paths_changed_between_commits(struct got_pathlist_head *paths,
7547 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7548 af179be7 2023-04-14 stsp struct got_repository *repo)
7549 af179be7 2023-04-14 stsp {
7550 af179be7 2023-04-14 stsp const struct got_error *err;
7551 af179be7 2023-04-14 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7552 af179be7 2023-04-14 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7553 af179be7 2023-04-14 stsp
7554 af179be7 2023-04-14 stsp if (id1) {
7555 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit1, repo, id1);
7556 af179be7 2023-04-14 stsp if (err)
7557 af179be7 2023-04-14 stsp goto done;
7558 af179be7 2023-04-14 stsp
7559 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree1, repo,
7560 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit1));
7561 af179be7 2023-04-14 stsp if (err)
7562 af179be7 2023-04-14 stsp goto done;
7563 af179be7 2023-04-14 stsp }
7564 818c7501 2019-07-11 stsp
7565 af179be7 2023-04-14 stsp if (id2) {
7566 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit2, repo, id2);
7567 af179be7 2023-04-14 stsp if (err)
7568 af179be7 2023-04-14 stsp goto done;
7569 af179be7 2023-04-14 stsp
7570 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree2, repo,
7571 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit2));
7572 af179be7 2023-04-14 stsp if (err)
7573 af179be7 2023-04-14 stsp goto done;
7574 af179be7 2023-04-14 stsp }
7575 af179be7 2023-04-14 stsp
7576 af179be7 2023-04-14 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7577 af179be7 2023-04-14 stsp got_diff_tree_collect_changed_paths, paths, 0);
7578 af179be7 2023-04-14 stsp if (err)
7579 af179be7 2023-04-14 stsp goto done;
7580 af179be7 2023-04-14 stsp done:
7581 af179be7 2023-04-14 stsp if (commit1)
7582 af179be7 2023-04-14 stsp got_object_commit_close(commit1);
7583 af179be7 2023-04-14 stsp if (commit2)
7584 af179be7 2023-04-14 stsp got_object_commit_close(commit2);
7585 af179be7 2023-04-14 stsp if (tree1)
7586 af179be7 2023-04-14 stsp got_object_tree_close(tree1);
7587 af179be7 2023-04-14 stsp if (tree2)
7588 af179be7 2023-04-14 stsp got_object_tree_close(tree2);
7589 af179be7 2023-04-14 stsp return err;
7590 af179be7 2023-04-14 stsp }
7591 af179be7 2023-04-14 stsp
7592 af179be7 2023-04-14 stsp static const struct got_error *
7593 af179be7 2023-04-14 stsp get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7594 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7595 af179be7 2023-04-14 stsp const char *path_prefix, struct got_repository *repo)
7596 af179be7 2023-04-14 stsp {
7597 af179be7 2023-04-14 stsp const struct got_error *err;
7598 af179be7 2023-04-14 stsp struct got_pathlist_head merged_paths;
7599 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
7600 af179be7 2023-04-14 stsp char *abspath = NULL, *wt_path = NULL;
7601 af179be7 2023-04-14 stsp
7602 af179be7 2023-04-14 stsp TAILQ_INIT(&merged_paths);
7603 af179be7 2023-04-14 stsp
7604 af179be7 2023-04-14 stsp err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7605 af179be7 2023-04-14 stsp if (err)
7606 af179be7 2023-04-14 stsp goto done;
7607 af179be7 2023-04-14 stsp
7608 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, &merged_paths, entry) {
7609 af179be7 2023-04-14 stsp struct got_diff_changed_path *change = pe->data;
7610 af179be7 2023-04-14 stsp
7611 af179be7 2023-04-14 stsp if (change->status != GOT_STATUS_ADD)
7612 af179be7 2023-04-14 stsp continue;
7613 af179be7 2023-04-14 stsp
7614 af179be7 2023-04-14 stsp if (got_path_is_root_dir(path_prefix)) {
7615 af179be7 2023-04-14 stsp wt_path = strdup(pe->path);
7616 af179be7 2023-04-14 stsp if (wt_path == NULL) {
7617 af179be7 2023-04-14 stsp err = got_error_from_errno("strdup");
7618 af179be7 2023-04-14 stsp goto done;
7619 af179be7 2023-04-14 stsp }
7620 af179be7 2023-04-14 stsp } else {
7621 af179be7 2023-04-14 stsp if (asprintf(&abspath, "/%s", pe->path) == -1) {
7622 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
7623 af179be7 2023-04-14 stsp goto done;
7624 af179be7 2023-04-14 stsp }
7625 af179be7 2023-04-14 stsp
7626 af179be7 2023-04-14 stsp err = got_path_skip_common_ancestor(&wt_path,
7627 af179be7 2023-04-14 stsp path_prefix, abspath);
7628 af179be7 2023-04-14 stsp if (err)
7629 af179be7 2023-04-14 stsp goto done;
7630 af179be7 2023-04-14 stsp free(abspath);
7631 af179be7 2023-04-14 stsp abspath = NULL;
7632 af179be7 2023-04-14 stsp }
7633 af179be7 2023-04-14 stsp
7634 af179be7 2023-04-14 stsp err = got_pathlist_append(added_paths, wt_path, NULL);
7635 af179be7 2023-04-14 stsp if (err)
7636 af179be7 2023-04-14 stsp goto done;
7637 af179be7 2023-04-14 stsp wt_path = NULL;
7638 af179be7 2023-04-14 stsp }
7639 af179be7 2023-04-14 stsp
7640 af179be7 2023-04-14 stsp done:
7641 af179be7 2023-04-14 stsp got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7642 af179be7 2023-04-14 stsp free(abspath);
7643 af179be7 2023-04-14 stsp free(wt_path);
7644 af179be7 2023-04-14 stsp return err;
7645 af179be7 2023-04-14 stsp }
7646 af179be7 2023-04-14 stsp
7647 af179be7 2023-04-14 stsp static const struct got_error *
7648 af179be7 2023-04-14 stsp get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7649 af179be7 2023-04-14 stsp struct got_object_id *id, const char *path_prefix,
7650 af179be7 2023-04-14 stsp struct got_repository *repo)
7651 af179be7 2023-04-14 stsp {
7652 af179be7 2023-04-14 stsp const struct got_error *err;
7653 af179be7 2023-04-14 stsp struct got_commit_object *commit = NULL;
7654 af179be7 2023-04-14 stsp struct got_object_qid *pid;
7655 af179be7 2023-04-14 stsp
7656 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit, repo, id);
7657 af179be7 2023-04-14 stsp if (err)
7658 af179be7 2023-04-14 stsp goto done;
7659 af179be7 2023-04-14 stsp
7660 af179be7 2023-04-14 stsp pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7661 af179be7 2023-04-14 stsp
7662 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(added_paths,
7663 af179be7 2023-04-14 stsp pid ? &pid->id : NULL, id, path_prefix, repo);
7664 af179be7 2023-04-14 stsp if (err)
7665 af179be7 2023-04-14 stsp goto done;
7666 af179be7 2023-04-14 stsp done:
7667 af179be7 2023-04-14 stsp if (commit)
7668 af179be7 2023-04-14 stsp got_object_commit_close(commit);
7669 af179be7 2023-04-14 stsp return err;
7670 af179be7 2023-04-14 stsp }
7671 af179be7 2023-04-14 stsp
7672 818c7501 2019-07-11 stsp const struct got_error *
7673 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7674 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7675 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7676 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7677 818c7501 2019-07-11 stsp {
7678 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7679 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7680 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7681 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
7682 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7683 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7684 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
7685 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
7686 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7687 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7688 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
7689 818c7501 2019-07-11 stsp
7690 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
7691 af179be7 2023-04-14 stsp
7692 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7693 818c7501 2019-07-11 stsp if (err)
7694 818c7501 2019-07-11 stsp return err;
7695 af179be7 2023-04-14 stsp
7696 af179be7 2023-04-14 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7697 af179be7 2023-04-14 stsp if (err)
7698 af179be7 2023-04-14 stsp goto done;
7699 af179be7 2023-04-14 stsp
7700 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7701 af179be7 2023-04-14 stsp if (err)
7702 af179be7 2023-04-14 stsp goto done;
7703 af179be7 2023-04-14 stsp
7704 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7705 af179be7 2023-04-14 stsp if (err)
7706 af179be7 2023-04-14 stsp goto done;
7707 a44927cc 2022-04-07 stsp
7708 af179be7 2023-04-14 stsp /*
7709 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
7710 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
7711 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
7712 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
7713 af179be7 2023-04-14 stsp */
7714 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7715 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
7716 af179be7 2023-04-14 stsp if (err)
7717 af179be7 2023-04-14 stsp goto done;
7718 af179be7 2023-04-14 stsp
7719 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7720 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7721 818c7501 2019-07-11 stsp if (err)
7722 818c7501 2019-07-11 stsp goto done;
7723 818c7501 2019-07-11 stsp
7724 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7725 818c7501 2019-07-11 stsp if (err)
7726 818c7501 2019-07-11 stsp goto done;
7727 818c7501 2019-07-11 stsp
7728 818c7501 2019-07-11 stsp /*
7729 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7730 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7731 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7732 818c7501 2019-07-11 stsp */
7733 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7734 818c7501 2019-07-11 stsp if (err)
7735 818c7501 2019-07-11 stsp goto done;
7736 818c7501 2019-07-11 stsp
7737 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7738 1b093d84 2023-04-12 stsp if (err)
7739 1b093d84 2023-04-12 stsp goto done;
7740 1b093d84 2023-04-12 stsp
7741 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
7742 1b093d84 2023-04-12 stsp worktree->base_commit_id);
7743 818c7501 2019-07-11 stsp if (err)
7744 818c7501 2019-07-11 stsp goto done;
7745 818c7501 2019-07-11 stsp
7746 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7747 a44927cc 2022-04-07 stsp worktree->path_prefix);
7748 ca355955 2019-07-12 stsp if (err)
7749 ca355955 2019-07-12 stsp goto done;
7750 ca355955 2019-07-12 stsp
7751 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7752 ca355955 2019-07-12 stsp if (err)
7753 ca355955 2019-07-12 stsp goto done;
7754 ca355955 2019-07-12 stsp
7755 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7756 818c7501 2019-07-11 stsp if (err)
7757 818c7501 2019-07-11 stsp goto done;
7758 818c7501 2019-07-11 stsp
7759 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7760 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7761 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7762 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7763 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7764 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7765 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7766 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
7767 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
7768 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7769 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7770 55bd499d 2019-07-12 stsp if (err)
7771 1f1abb7e 2019-08-08 stsp goto sync;
7772 55bd499d 2019-07-12 stsp
7773 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7774 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7775 ca355955 2019-07-12 stsp sync:
7776 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7777 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7778 ca355955 2019-07-12 stsp err = sync_err;
7779 818c7501 2019-07-11 stsp done:
7780 af179be7 2023-04-14 stsp got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7781 818c7501 2019-07-11 stsp got_ref_close(resolved);
7782 a3a2faf2 2019-07-12 stsp free(tree_id);
7783 818c7501 2019-07-11 stsp free(commit_id);
7784 af179be7 2023-04-14 stsp free(merged_commit_id);
7785 a44927cc 2022-04-07 stsp if (commit)
7786 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7787 0ebf8283 2019-07-24 stsp if (fileindex)
7788 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7789 0ebf8283 2019-07-24 stsp free(fileindex_path);
7790 af179be7 2023-04-14 stsp free(commit_ref_name);
7791 af179be7 2023-04-14 stsp if (commit_ref)
7792 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
7793 0ebf8283 2019-07-24 stsp
7794 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7795 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7796 0ebf8283 2019-07-24 stsp err = unlockerr;
7797 0ebf8283 2019-07-24 stsp return err;
7798 0ebf8283 2019-07-24 stsp }
7799 0ebf8283 2019-07-24 stsp
7800 0ebf8283 2019-07-24 stsp const struct got_error *
7801 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7802 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7803 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7804 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7805 0ebf8283 2019-07-24 stsp {
7806 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7807 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7808 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7809 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7810 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7811 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7812 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7813 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7814 0ebf8283 2019-07-24 stsp
7815 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7816 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7817 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7818 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7819 0ebf8283 2019-07-24 stsp
7820 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7821 0ebf8283 2019-07-24 stsp if (err)
7822 0ebf8283 2019-07-24 stsp return err;
7823 0ebf8283 2019-07-24 stsp
7824 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7825 0ebf8283 2019-07-24 stsp if (err)
7826 0ebf8283 2019-07-24 stsp goto done;
7827 0ebf8283 2019-07-24 stsp
7828 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7829 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7830 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7831 0ebf8283 2019-07-24 stsp &ok_arg);
7832 0ebf8283 2019-07-24 stsp if (err)
7833 0ebf8283 2019-07-24 stsp goto done;
7834 0ebf8283 2019-07-24 stsp
7835 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7836 0ebf8283 2019-07-24 stsp if (err)
7837 0ebf8283 2019-07-24 stsp goto done;
7838 0ebf8283 2019-07-24 stsp
7839 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7840 0ebf8283 2019-07-24 stsp if (err)
7841 0ebf8283 2019-07-24 stsp goto done;
7842 0ebf8283 2019-07-24 stsp
7843 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7844 0ebf8283 2019-07-24 stsp worktree);
7845 0ebf8283 2019-07-24 stsp if (err)
7846 0ebf8283 2019-07-24 stsp goto done;
7847 0ebf8283 2019-07-24 stsp
7848 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7849 0ebf8283 2019-07-24 stsp 0);
7850 0ebf8283 2019-07-24 stsp if (err)
7851 0ebf8283 2019-07-24 stsp goto done;
7852 0ebf8283 2019-07-24 stsp
7853 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7854 0ebf8283 2019-07-24 stsp if (err)
7855 0ebf8283 2019-07-24 stsp goto done;
7856 0ebf8283 2019-07-24 stsp
7857 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7858 0ebf8283 2019-07-24 stsp if (err)
7859 0ebf8283 2019-07-24 stsp goto done;
7860 0ebf8283 2019-07-24 stsp
7861 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7862 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7863 0ebf8283 2019-07-24 stsp if (err)
7864 0ebf8283 2019-07-24 stsp goto done;
7865 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7866 0ebf8283 2019-07-24 stsp if (err)
7867 0ebf8283 2019-07-24 stsp goto done;
7868 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7869 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7870 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7871 0ebf8283 2019-07-24 stsp goto done;
7872 0ebf8283 2019-07-24 stsp }
7873 0ebf8283 2019-07-24 stsp
7874 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7875 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7876 0ebf8283 2019-07-24 stsp if (err)
7877 0ebf8283 2019-07-24 stsp goto done;
7878 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7879 0ebf8283 2019-07-24 stsp if (err)
7880 0ebf8283 2019-07-24 stsp goto done;
7881 0ebf8283 2019-07-24 stsp
7882 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7883 0ebf8283 2019-07-24 stsp if (err)
7884 0ebf8283 2019-07-24 stsp goto done;
7885 0ebf8283 2019-07-24 stsp done:
7886 0ebf8283 2019-07-24 stsp free(fileindex_path);
7887 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7888 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7889 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7890 0ebf8283 2019-07-24 stsp if (wt_branch)
7891 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7892 0ebf8283 2019-07-24 stsp if (err) {
7893 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7894 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7895 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7896 0ebf8283 2019-07-24 stsp }
7897 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7898 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7899 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7900 0ebf8283 2019-07-24 stsp }
7901 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7902 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7903 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7904 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7905 3e3a69f1 2019-07-25 stsp }
7906 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7907 0ebf8283 2019-07-24 stsp }
7908 0ebf8283 2019-07-24 stsp return err;
7909 0ebf8283 2019-07-24 stsp }
7910 0ebf8283 2019-07-24 stsp
7911 0ebf8283 2019-07-24 stsp const struct got_error *
7912 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7913 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7914 0ebf8283 2019-07-24 stsp {
7915 3e3a69f1 2019-07-25 stsp if (fileindex)
7916 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7917 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7918 0ebf8283 2019-07-24 stsp }
7919 0ebf8283 2019-07-24 stsp
7920 0ebf8283 2019-07-24 stsp const struct got_error *
7921 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7922 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7923 0ebf8283 2019-07-24 stsp {
7924 0ebf8283 2019-07-24 stsp const struct got_error *err;
7925 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7926 0ebf8283 2019-07-24 stsp
7927 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7928 0ebf8283 2019-07-24 stsp if (err)
7929 0ebf8283 2019-07-24 stsp return err;
7930 0ebf8283 2019-07-24 stsp
7931 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7932 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7933 0ebf8283 2019-07-24 stsp return NULL;
7934 0ebf8283 2019-07-24 stsp }
7935 0ebf8283 2019-07-24 stsp
7936 0ebf8283 2019-07-24 stsp const struct got_error *
7937 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7938 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7939 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7940 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7941 0ebf8283 2019-07-24 stsp {
7942 0ebf8283 2019-07-24 stsp const struct got_error *err;
7943 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7944 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7945 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7946 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7947 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7948 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7949 0ebf8283 2019-07-24 stsp
7950 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7951 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7952 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7953 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7954 0ebf8283 2019-07-24 stsp
7955 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7956 3e3a69f1 2019-07-25 stsp if (err)
7957 3e3a69f1 2019-07-25 stsp return err;
7958 3e3a69f1 2019-07-25 stsp
7959 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7960 3e3a69f1 2019-07-25 stsp if (err)
7961 f032f1f7 2019-08-04 stsp goto done;
7962 f032f1f7 2019-08-04 stsp
7963 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7964 f032f1f7 2019-08-04 stsp &have_staged_files);
7965 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7966 f032f1f7 2019-08-04 stsp goto done;
7967 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7968 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7969 3e3a69f1 2019-07-25 stsp goto done;
7970 f032f1f7 2019-08-04 stsp }
7971 3e3a69f1 2019-07-25 stsp
7972 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7973 0ebf8283 2019-07-24 stsp if (err)
7974 f032f1f7 2019-08-04 stsp goto done;
7975 0ebf8283 2019-07-24 stsp
7976 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7977 0ebf8283 2019-07-24 stsp if (err)
7978 0ebf8283 2019-07-24 stsp goto done;
7979 0ebf8283 2019-07-24 stsp
7980 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7981 0ebf8283 2019-07-24 stsp if (err)
7982 0ebf8283 2019-07-24 stsp goto done;
7983 0ebf8283 2019-07-24 stsp
7984 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7985 0ebf8283 2019-07-24 stsp worktree);
7986 0ebf8283 2019-07-24 stsp if (err)
7987 0ebf8283 2019-07-24 stsp goto done;
7988 0ebf8283 2019-07-24 stsp
7989 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7990 0ebf8283 2019-07-24 stsp if (err)
7991 0ebf8283 2019-07-24 stsp goto done;
7992 0ebf8283 2019-07-24 stsp
7993 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7994 0ebf8283 2019-07-24 stsp if (err)
7995 0ebf8283 2019-07-24 stsp goto done;
7996 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7997 0ebf8283 2019-07-24 stsp if (err)
7998 0ebf8283 2019-07-24 stsp goto done;
7999 0ebf8283 2019-07-24 stsp
8000 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
8001 0ebf8283 2019-07-24 stsp if (err)
8002 0ebf8283 2019-07-24 stsp goto done;
8003 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8004 0ebf8283 2019-07-24 stsp if (err)
8005 0ebf8283 2019-07-24 stsp goto done;
8006 0ebf8283 2019-07-24 stsp
8007 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8008 0ebf8283 2019-07-24 stsp if (err)
8009 0ebf8283 2019-07-24 stsp goto done;
8010 0ebf8283 2019-07-24 stsp done:
8011 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8012 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8013 3e3a69f1 2019-07-25 stsp free(fileindex_path);
8014 0ebf8283 2019-07-24 stsp if (commit_ref)
8015 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
8016 0ebf8283 2019-07-24 stsp if (base_commit_ref)
8017 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
8018 0ebf8283 2019-07-24 stsp if (err) {
8019 0ebf8283 2019-07-24 stsp free(*commit_id);
8020 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8021 0ebf8283 2019-07-24 stsp free(*base_commit_id);
8022 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8023 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
8024 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
8025 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8026 0ebf8283 2019-07-24 stsp }
8027 3e3a69f1 2019-07-25 stsp if (*fileindex) {
8028 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
8029 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8030 3e3a69f1 2019-07-25 stsp }
8031 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
8032 0ebf8283 2019-07-24 stsp }
8033 0ebf8283 2019-07-24 stsp return err;
8034 0ebf8283 2019-07-24 stsp }
8035 0ebf8283 2019-07-24 stsp
8036 0ebf8283 2019-07-24 stsp static const struct got_error *
8037 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8038 0ebf8283 2019-07-24 stsp {
8039 0ebf8283 2019-07-24 stsp const struct got_error *err;
8040 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8041 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
8042 0ebf8283 2019-07-24 stsp
8043 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8044 0ebf8283 2019-07-24 stsp if (err)
8045 0ebf8283 2019-07-24 stsp goto done;
8046 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
8047 0ebf8283 2019-07-24 stsp if (err)
8048 0ebf8283 2019-07-24 stsp goto done;
8049 0ebf8283 2019-07-24 stsp
8050 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8051 0ebf8283 2019-07-24 stsp worktree);
8052 0ebf8283 2019-07-24 stsp if (err)
8053 0ebf8283 2019-07-24 stsp goto done;
8054 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
8055 0ebf8283 2019-07-24 stsp if (err)
8056 0ebf8283 2019-07-24 stsp goto done;
8057 0ebf8283 2019-07-24 stsp
8058 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8059 0ebf8283 2019-07-24 stsp if (err)
8060 0ebf8283 2019-07-24 stsp goto done;
8061 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8062 0ebf8283 2019-07-24 stsp if (err)
8063 0ebf8283 2019-07-24 stsp goto done;
8064 0ebf8283 2019-07-24 stsp
8065 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8066 0ebf8283 2019-07-24 stsp if (err)
8067 0ebf8283 2019-07-24 stsp goto done;
8068 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8069 0ebf8283 2019-07-24 stsp if (err)
8070 0ebf8283 2019-07-24 stsp goto done;
8071 0ebf8283 2019-07-24 stsp done:
8072 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8073 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8074 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8075 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8076 0ebf8283 2019-07-24 stsp return err;
8077 0ebf8283 2019-07-24 stsp }
8078 0ebf8283 2019-07-24 stsp
8079 0ebf8283 2019-07-24 stsp const struct got_error *
8080 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8081 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8082 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8083 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8084 0ebf8283 2019-07-24 stsp {
8085 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8086 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8087 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8088 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8089 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8090 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8091 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8092 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8093 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8094 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8095 0ebf8283 2019-07-24 stsp
8096 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8097 af179be7 2023-04-14 stsp
8098 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8099 0ebf8283 2019-07-24 stsp if (err)
8100 0ebf8283 2019-07-24 stsp return err;
8101 a44927cc 2022-04-07 stsp
8102 af179be7 2023-04-14 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8103 af179be7 2023-04-14 stsp if (err)
8104 af179be7 2023-04-14 stsp goto done;
8105 af179be7 2023-04-14 stsp
8106 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8107 af179be7 2023-04-14 stsp if (err) {
8108 af179be7 2023-04-14 stsp if (err->code != GOT_ERR_NOT_REF)
8109 af179be7 2023-04-14 stsp goto done;
8110 af179be7 2023-04-14 stsp /* Can happen on early abort due to invalid histedit script. */
8111 af179be7 2023-04-14 stsp commit_ref = NULL;
8112 af179be7 2023-04-14 stsp }
8113 af179be7 2023-04-14 stsp
8114 af179be7 2023-04-14 stsp if (commit_ref) {
8115 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8116 af179be7 2023-04-14 stsp if (err)
8117 af179be7 2023-04-14 stsp goto done;
8118 af179be7 2023-04-14 stsp
8119 af179be7 2023-04-14 stsp /*
8120 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8121 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8122 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files added by the
8123 af179be7 2023-04-14 stsp * user during conflict resolution or during histedit -e.
8124 af179be7 2023-04-14 stsp */
8125 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8126 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8127 af179be7 2023-04-14 stsp if (err)
8128 af179be7 2023-04-14 stsp goto done;
8129 af179be7 2023-04-14 stsp }
8130 af179be7 2023-04-14 stsp
8131 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8132 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8133 0ebf8283 2019-07-24 stsp if (err)
8134 0ebf8283 2019-07-24 stsp goto done;
8135 0ebf8283 2019-07-24 stsp
8136 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8137 0ebf8283 2019-07-24 stsp if (err)
8138 0ebf8283 2019-07-24 stsp goto done;
8139 0ebf8283 2019-07-24 stsp
8140 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8141 0ebf8283 2019-07-24 stsp if (err)
8142 0ebf8283 2019-07-24 stsp goto done;
8143 0ebf8283 2019-07-24 stsp
8144 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
8145 1b093d84 2023-04-12 stsp worktree->base_commit_id);
8146 1b093d84 2023-04-12 stsp if (err)
8147 1b093d84 2023-04-12 stsp goto done;
8148 1b093d84 2023-04-12 stsp
8149 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8150 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8151 0ebf8283 2019-07-24 stsp if (err)
8152 0ebf8283 2019-07-24 stsp goto done;
8153 0ebf8283 2019-07-24 stsp
8154 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8155 0ebf8283 2019-07-24 stsp if (err)
8156 0ebf8283 2019-07-24 stsp goto done;
8157 0ebf8283 2019-07-24 stsp
8158 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8159 0ebf8283 2019-07-24 stsp if (err)
8160 0ebf8283 2019-07-24 stsp goto done;
8161 0ebf8283 2019-07-24 stsp
8162 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8163 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8164 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8165 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8166 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8167 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8168 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8169 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
8170 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8171 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8172 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8173 0ebf8283 2019-07-24 stsp if (err)
8174 1f1abb7e 2019-08-08 stsp goto sync;
8175 0ebf8283 2019-07-24 stsp
8176 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8177 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8178 0ebf8283 2019-07-24 stsp sync:
8179 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8180 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8181 0ebf8283 2019-07-24 stsp err = sync_err;
8182 0ebf8283 2019-07-24 stsp done:
8183 af179be7 2023-04-14 stsp if (resolved)
8184 af179be7 2023-04-14 stsp got_ref_close(resolved);
8185 af179be7 2023-04-14 stsp if (commit_ref)
8186 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8187 af179be7 2023-04-14 stsp free(merged_commit_id);
8188 0ebf8283 2019-07-24 stsp free(tree_id);
8189 818c7501 2019-07-11 stsp free(fileindex_path);
8190 af179be7 2023-04-14 stsp free(commit_ref_name);
8191 818c7501 2019-07-11 stsp
8192 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8193 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8194 818c7501 2019-07-11 stsp err = unlockerr;
8195 818c7501 2019-07-11 stsp return err;
8196 818c7501 2019-07-11 stsp }
8197 0ebf8283 2019-07-24 stsp
8198 0ebf8283 2019-07-24 stsp const struct got_error *
8199 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8200 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8201 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8202 0ebf8283 2019-07-24 stsp {
8203 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8204 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8205 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8206 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8207 0ebf8283 2019-07-24 stsp
8208 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8209 0ebf8283 2019-07-24 stsp if (err)
8210 0ebf8283 2019-07-24 stsp return err;
8211 0ebf8283 2019-07-24 stsp
8212 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8213 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8214 e600f124 2021-03-21 stsp if (err)
8215 e600f124 2021-03-21 stsp goto done;
8216 e600f124 2021-03-21 stsp
8217 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8218 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8219 0ebf8283 2019-07-24 stsp if (err)
8220 0ebf8283 2019-07-24 stsp goto done;
8221 0ebf8283 2019-07-24 stsp
8222 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8223 0ebf8283 2019-07-24 stsp if (err)
8224 0ebf8283 2019-07-24 stsp goto done;
8225 0ebf8283 2019-07-24 stsp
8226 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8227 0ebf8283 2019-07-24 stsp if (err)
8228 0ebf8283 2019-07-24 stsp goto done;
8229 0ebf8283 2019-07-24 stsp
8230 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8231 0ebf8283 2019-07-24 stsp if (err)
8232 0ebf8283 2019-07-24 stsp goto done;
8233 0ebf8283 2019-07-24 stsp
8234 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8235 a615e0e7 2020-12-16 stsp if (err)
8236 a615e0e7 2020-12-16 stsp goto done;
8237 a615e0e7 2020-12-16 stsp
8238 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8239 a615e0e7 2020-12-16 stsp if (err)
8240 a615e0e7 2020-12-16 stsp goto done;
8241 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8242 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8243 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8244 a615e0e7 2020-12-16 stsp err = sync_err;
8245 0ebf8283 2019-07-24 stsp done:
8246 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8247 a615e0e7 2020-12-16 stsp free(fileindex_path);
8248 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8249 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8250 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8251 0ebf8283 2019-07-24 stsp err = unlockerr;
8252 0ebf8283 2019-07-24 stsp return err;
8253 0ebf8283 2019-07-24 stsp }
8254 0ebf8283 2019-07-24 stsp
8255 0ebf8283 2019-07-24 stsp const struct got_error *
8256 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8257 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8258 0ebf8283 2019-07-24 stsp {
8259 0ebf8283 2019-07-24 stsp const struct got_error *err;
8260 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8261 0ebf8283 2019-07-24 stsp
8262 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8263 0ebf8283 2019-07-24 stsp if (err)
8264 0ebf8283 2019-07-24 stsp return err;
8265 0ebf8283 2019-07-24 stsp
8266 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8267 0ebf8283 2019-07-24 stsp if (err)
8268 0ebf8283 2019-07-24 stsp goto done;
8269 0ebf8283 2019-07-24 stsp
8270 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8271 0ebf8283 2019-07-24 stsp done:
8272 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8273 2822a352 2019-10-15 stsp return err;
8274 2822a352 2019-10-15 stsp }
8275 2822a352 2019-10-15 stsp
8276 2822a352 2019-10-15 stsp const struct got_error *
8277 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8278 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8279 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8280 2822a352 2019-10-15 stsp struct got_repository *repo)
8281 2822a352 2019-10-15 stsp {
8282 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8283 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8284 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8285 2822a352 2019-10-15 stsp
8286 2822a352 2019-10-15 stsp *fileindex = NULL;
8287 2822a352 2019-10-15 stsp *branch_ref = NULL;
8288 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8289 2822a352 2019-10-15 stsp
8290 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8291 2822a352 2019-10-15 stsp if (err)
8292 2822a352 2019-10-15 stsp return err;
8293 2822a352 2019-10-15 stsp
8294 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8295 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8296 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8297 2822a352 2019-10-15 stsp "update -b or different branch name required");
8298 2822a352 2019-10-15 stsp goto done;
8299 2822a352 2019-10-15 stsp }
8300 2822a352 2019-10-15 stsp
8301 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8302 2822a352 2019-10-15 stsp if (err)
8303 2822a352 2019-10-15 stsp goto done;
8304 2822a352 2019-10-15 stsp
8305 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8306 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8307 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8308 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8309 2822a352 2019-10-15 stsp &ok_arg);
8310 2822a352 2019-10-15 stsp if (err)
8311 2822a352 2019-10-15 stsp goto done;
8312 2822a352 2019-10-15 stsp
8313 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8314 2822a352 2019-10-15 stsp if (err)
8315 2822a352 2019-10-15 stsp goto done;
8316 2822a352 2019-10-15 stsp
8317 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8318 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8319 2822a352 2019-10-15 stsp done:
8320 2822a352 2019-10-15 stsp if (err) {
8321 2822a352 2019-10-15 stsp if (*branch_ref) {
8322 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8323 2822a352 2019-10-15 stsp *branch_ref = NULL;
8324 2822a352 2019-10-15 stsp }
8325 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8326 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8327 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8328 2822a352 2019-10-15 stsp }
8329 2822a352 2019-10-15 stsp if (*fileindex) {
8330 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8331 2822a352 2019-10-15 stsp *fileindex = NULL;
8332 2822a352 2019-10-15 stsp }
8333 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8334 2822a352 2019-10-15 stsp }
8335 0cb83759 2019-08-03 stsp return err;
8336 0cb83759 2019-08-03 stsp }
8337 0cb83759 2019-08-03 stsp
8338 2822a352 2019-10-15 stsp const struct got_error *
8339 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8340 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8341 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8342 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8343 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8344 2822a352 2019-10-15 stsp {
8345 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8346 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8347 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8348 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8349 2822a352 2019-10-15 stsp
8350 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8351 2822a352 2019-10-15 stsp if (err)
8352 2822a352 2019-10-15 stsp goto done;
8353 2822a352 2019-10-15 stsp
8354 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8355 2822a352 2019-10-15 stsp if (err)
8356 2822a352 2019-10-15 stsp goto done;
8357 2822a352 2019-10-15 stsp
8358 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8359 a44927cc 2022-04-07 stsp if (err)
8360 a44927cc 2022-04-07 stsp goto done;
8361 a44927cc 2022-04-07 stsp
8362 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8363 2822a352 2019-10-15 stsp worktree->path_prefix);
8364 2822a352 2019-10-15 stsp if (err)
8365 2822a352 2019-10-15 stsp goto done;
8366 2822a352 2019-10-15 stsp
8367 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8368 2822a352 2019-10-15 stsp if (err)
8369 2822a352 2019-10-15 stsp goto done;
8370 2822a352 2019-10-15 stsp
8371 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8372 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8373 2822a352 2019-10-15 stsp if (err)
8374 2822a352 2019-10-15 stsp goto sync;
8375 2822a352 2019-10-15 stsp
8376 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8377 2822a352 2019-10-15 stsp if (err)
8378 2822a352 2019-10-15 stsp goto sync;
8379 2822a352 2019-10-15 stsp
8380 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8381 6e210706 2021-01-22 stsp if (err)
8382 6e210706 2021-01-22 stsp goto sync;
8383 6e210706 2021-01-22 stsp
8384 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8385 2822a352 2019-10-15 stsp sync:
8386 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8387 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8388 2822a352 2019-10-15 stsp err = sync_err;
8389 2822a352 2019-10-15 stsp
8390 2822a352 2019-10-15 stsp done:
8391 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8392 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8393 2822a352 2019-10-15 stsp err = unlockerr;
8394 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8395 2822a352 2019-10-15 stsp
8396 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8397 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8398 2822a352 2019-10-15 stsp err = unlockerr;
8399 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8400 2822a352 2019-10-15 stsp
8401 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8402 2822a352 2019-10-15 stsp free(fileindex_path);
8403 2822a352 2019-10-15 stsp free(tree_id);
8404 a44927cc 2022-04-07 stsp if (commit)
8405 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8406 2822a352 2019-10-15 stsp
8407 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8408 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8409 2822a352 2019-10-15 stsp err = unlockerr;
8410 2822a352 2019-10-15 stsp return err;
8411 2822a352 2019-10-15 stsp }
8412 2822a352 2019-10-15 stsp
8413 2822a352 2019-10-15 stsp const struct got_error *
8414 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8415 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8416 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8417 2822a352 2019-10-15 stsp {
8418 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8419 8b692cd0 2019-10-21 stsp
8420 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8421 8b692cd0 2019-10-21 stsp
8422 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8423 8b692cd0 2019-10-21 stsp
8424 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8425 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8426 8b692cd0 2019-10-21 stsp err = unlockerr;
8427 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8428 8b692cd0 2019-10-21 stsp
8429 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8430 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8431 8b692cd0 2019-10-21 stsp err = unlockerr;
8432 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8433 f259c4c1 2021-09-24 stsp
8434 f259c4c1 2021-09-24 stsp return err;
8435 f259c4c1 2021-09-24 stsp }
8436 f259c4c1 2021-09-24 stsp
8437 f259c4c1 2021-09-24 stsp const struct got_error *
8438 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
8439 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
8440 f259c4c1 2021-09-24 stsp {
8441 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
8442 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8443 f259c4c1 2021-09-24 stsp
8444 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8445 f259c4c1 2021-09-24 stsp if (err)
8446 f259c4c1 2021-09-24 stsp goto done;
8447 8b692cd0 2019-10-21 stsp
8448 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8449 f259c4c1 2021-09-24 stsp
8450 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
8451 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8452 f259c4c1 2021-09-24 stsp err = sync_err;
8453 f259c4c1 2021-09-24 stsp done:
8454 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8455 f259c4c1 2021-09-24 stsp free(fileindex_path);
8456 8b692cd0 2019-10-21 stsp return err;
8457 2822a352 2019-10-15 stsp }
8458 2822a352 2019-10-15 stsp
8459 f259c4c1 2021-09-24 stsp static const struct got_error *
8460 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8461 f259c4c1 2021-09-24 stsp {
8462 f259c4c1 2021-09-24 stsp const struct got_error *err;
8463 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8464 f259c4c1 2021-09-24 stsp
8465 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8466 f259c4c1 2021-09-24 stsp if (err)
8467 f259c4c1 2021-09-24 stsp goto done;
8468 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
8469 f259c4c1 2021-09-24 stsp if (err)
8470 f259c4c1 2021-09-24 stsp goto done;
8471 f259c4c1 2021-09-24 stsp
8472 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8473 f259c4c1 2021-09-24 stsp if (err)
8474 f259c4c1 2021-09-24 stsp goto done;
8475 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
8476 f259c4c1 2021-09-24 stsp if (err)
8477 f259c4c1 2021-09-24 stsp goto done;
8478 f259c4c1 2021-09-24 stsp
8479 f259c4c1 2021-09-24 stsp done:
8480 f259c4c1 2021-09-24 stsp free(branch_refname);
8481 f259c4c1 2021-09-24 stsp free(commit_refname);
8482 f259c4c1 2021-09-24 stsp return err;
8483 f259c4c1 2021-09-24 stsp }
8484 f259c4c1 2021-09-24 stsp
8485 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
8486 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
8487 f259c4c1 2021-09-24 stsp const char *branch_name;
8488 f259c4c1 2021-09-24 stsp };
8489 f259c4c1 2021-09-24 stsp
8490 f259c4c1 2021-09-24 stsp static const struct got_error *
8491 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8492 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
8493 f259c4c1 2021-09-24 stsp {
8494 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
8495 f259c4c1 2021-09-24 stsp
8496 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8497 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
8498 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
8499 f259c4c1 2021-09-24 stsp
8500 f259c4c1 2021-09-24 stsp return NULL;
8501 f259c4c1 2021-09-24 stsp }
8502 f259c4c1 2021-09-24 stsp
8503 f259c4c1 2021-09-24 stsp
8504 f259c4c1 2021-09-24 stsp const struct got_error *
8505 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
8506 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
8507 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
8508 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
8509 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8510 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8511 f259c4c1 2021-09-24 stsp {
8512 f259c4c1 2021-09-24 stsp const struct got_error *err;
8513 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8514 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
8515 f259c4c1 2021-09-24 stsp
8516 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8517 f259c4c1 2021-09-24 stsp if (err)
8518 f259c4c1 2021-09-24 stsp goto done;
8519 f259c4c1 2021-09-24 stsp
8520 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
8521 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
8522 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
8523 cdbfe5d2 2023-07-24 stsp
8524 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8525 e07c1782 2023-07-24 op &cma);
8526 f259c4c1 2021-09-24 stsp if (err)
8527 f259c4c1 2021-09-24 stsp goto done;
8528 f259c4c1 2021-09-24 stsp
8529 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8530 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
8531 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
8532 f259c4c1 2021-09-24 stsp done:
8533 f259c4c1 2021-09-24 stsp free(fileindex_path);
8534 f259c4c1 2021-09-24 stsp return err;
8535 f259c4c1 2021-09-24 stsp }
8536 f259c4c1 2021-09-24 stsp
8537 f259c4c1 2021-09-24 stsp const struct got_error *
8538 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
8539 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8540 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
8541 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
8542 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
8543 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
8544 0ff8d236 2021-09-28 stsp
8545 f259c4c1 2021-09-24 stsp {
8546 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
8547 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
8548 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
8549 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
8550 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
8551 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
8552 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8553 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
8554 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8555 f259c4c1 2021-09-24 stsp
8556 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
8557 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
8558 f259c4c1 2021-09-24 stsp
8559 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
8560 f259c4c1 2021-09-24 stsp
8561 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8562 f259c4c1 2021-09-24 stsp if (err)
8563 f259c4c1 2021-09-24 stsp goto done;
8564 f259c4c1 2021-09-24 stsp
8565 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8566 f259c4c1 2021-09-24 stsp if (err)
8567 f259c4c1 2021-09-24 stsp goto done;
8568 f259c4c1 2021-09-24 stsp
8569 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8570 f259c4c1 2021-09-24 stsp if (err)
8571 f259c4c1 2021-09-24 stsp goto done;
8572 f259c4c1 2021-09-24 stsp
8573 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8574 f259c4c1 2021-09-24 stsp &have_staged_files);
8575 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8576 f259c4c1 2021-09-24 stsp goto done;
8577 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8578 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8579 f259c4c1 2021-09-24 stsp goto done;
8580 f259c4c1 2021-09-24 stsp }
8581 f259c4c1 2021-09-24 stsp
8582 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
8583 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
8584 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
8585 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
8586 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
8587 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8588 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8589 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8590 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8591 f259c4c1 2021-09-24 stsp if (err)
8592 f259c4c1 2021-09-24 stsp goto done;
8593 f259c4c1 2021-09-24 stsp
8594 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8595 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8596 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8597 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8598 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8599 f259c4c1 2021-09-24 stsp if (err)
8600 f259c4c1 2021-09-24 stsp goto done;
8601 f259c4c1 2021-09-24 stsp
8602 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8603 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8604 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8605 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8606 f259c4c1 2021-09-24 stsp err = sync_err;
8607 f259c4c1 2021-09-24 stsp done:
8608 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8609 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8610 d8bacb93 2023-01-10 mark
8611 f259c4c1 2021-09-24 stsp free_commitable(ct);
8612 f259c4c1 2021-09-24 stsp }
8613 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8614 f259c4c1 2021-09-24 stsp free(fileindex_path);
8615 f259c4c1 2021-09-24 stsp return err;
8616 f259c4c1 2021-09-24 stsp }
8617 f259c4c1 2021-09-24 stsp
8618 f259c4c1 2021-09-24 stsp const struct got_error *
8619 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8620 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8621 f259c4c1 2021-09-24 stsp {
8622 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8623 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8624 f259c4c1 2021-09-24 stsp
8625 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8626 f259c4c1 2021-09-24 stsp if (err)
8627 f259c4c1 2021-09-24 stsp goto done;
8628 f259c4c1 2021-09-24 stsp
8629 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8630 f259c4c1 2021-09-24 stsp if (err)
8631 f259c4c1 2021-09-24 stsp goto done;
8632 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8633 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8634 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8635 f259c4c1 2021-09-24 stsp err = sync_err;
8636 f259c4c1 2021-09-24 stsp done:
8637 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8638 f259c4c1 2021-09-24 stsp free(fileindex_path);
8639 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8640 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8641 f259c4c1 2021-09-24 stsp err = unlockerr;
8642 f259c4c1 2021-09-24 stsp return err;
8643 f259c4c1 2021-09-24 stsp }
8644 f259c4c1 2021-09-24 stsp
8645 f259c4c1 2021-09-24 stsp const struct got_error *
8646 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8647 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8648 f259c4c1 2021-09-24 stsp {
8649 f259c4c1 2021-09-24 stsp const struct got_error *err;
8650 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8651 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8652 f259c4c1 2021-09-24 stsp
8653 f259c4c1 2021-09-24 stsp *in_progress = 0;
8654 f259c4c1 2021-09-24 stsp
8655 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8656 f259c4c1 2021-09-24 stsp if (err)
8657 f259c4c1 2021-09-24 stsp return err;
8658 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8659 793fcac3 2021-09-24 stsp free(branch_refname);
8660 f259c4c1 2021-09-24 stsp if (err) {
8661 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8662 f259c4c1 2021-09-24 stsp return err;
8663 f259c4c1 2021-09-24 stsp } else
8664 f259c4c1 2021-09-24 stsp *in_progress = 1;
8665 f259c4c1 2021-09-24 stsp
8666 f259c4c1 2021-09-24 stsp return NULL;
8667 f259c4c1 2021-09-24 stsp }
8668 f259c4c1 2021-09-24 stsp
8669 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8670 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8671 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8672 f259c4c1 2021-09-24 stsp {
8673 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8674 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8675 179f9db0 2023-06-20 falsifian struct got_reference *wt_branch = NULL;
8676 179f9db0 2023-06-20 falsifian struct got_object_id *wt_branch_tip = NULL;
8677 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8678 f259c4c1 2021-09-24 stsp
8679 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8680 f259c4c1 2021-09-24 stsp
8681 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8682 f259c4c1 2021-09-24 stsp if (err)
8683 f259c4c1 2021-09-24 stsp return err;
8684 f259c4c1 2021-09-24 stsp
8685 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8686 f259c4c1 2021-09-24 stsp if (err)
8687 f259c4c1 2021-09-24 stsp goto done;
8688 f259c4c1 2021-09-24 stsp
8689 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8690 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8691 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8692 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8693 f259c4c1 2021-09-24 stsp &ok_arg);
8694 f259c4c1 2021-09-24 stsp if (err)
8695 f259c4c1 2021-09-24 stsp goto done;
8696 f259c4c1 2021-09-24 stsp
8697 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8698 f259c4c1 2021-09-24 stsp 0);
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 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8703 f259c4c1 2021-09-24 stsp if (err)
8704 f259c4c1 2021-09-24 stsp goto done;
8705 f259c4c1 2021-09-24 stsp
8706 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8707 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8708 f259c4c1 2021-09-24 stsp goto done;
8709 f259c4c1 2021-09-24 stsp }
8710 179f9db0 2023-06-20 falsifian
8711 179f9db0 2023-06-20 falsifian done:
8712 179f9db0 2023-06-20 falsifian free(fileindex_path);
8713 179f9db0 2023-06-20 falsifian if (wt_branch)
8714 179f9db0 2023-06-20 falsifian got_ref_close(wt_branch);
8715 179f9db0 2023-06-20 falsifian free(wt_branch_tip);
8716 179f9db0 2023-06-20 falsifian if (err) {
8717 179f9db0 2023-06-20 falsifian if (*fileindex) {
8718 179f9db0 2023-06-20 falsifian got_fileindex_free(*fileindex);
8719 179f9db0 2023-06-20 falsifian *fileindex = NULL;
8720 179f9db0 2023-06-20 falsifian }
8721 179f9db0 2023-06-20 falsifian lock_worktree(worktree, LOCK_SH);
8722 179f9db0 2023-06-20 falsifian }
8723 179f9db0 2023-06-20 falsifian return err;
8724 179f9db0 2023-06-20 falsifian }
8725 f259c4c1 2021-09-24 stsp
8726 179f9db0 2023-06-20 falsifian const struct got_error *got_worktree_merge_write_refs(
8727 179f9db0 2023-06-20 falsifian struct got_worktree *worktree, struct got_reference *branch,
8728 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8729 179f9db0 2023-06-20 falsifian {
8730 179f9db0 2023-06-20 falsifian const struct got_error *err = NULL;
8731 179f9db0 2023-06-20 falsifian char *branch_refname = NULL, *commit_refname = NULL;
8732 179f9db0 2023-06-20 falsifian struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8733 179f9db0 2023-06-20 falsifian struct got_object_id *branch_tip = NULL;
8734 179f9db0 2023-06-20 falsifian
8735 179f9db0 2023-06-20 falsifian err = get_merge_branch_ref_name(&branch_refname, worktree);
8736 179f9db0 2023-06-20 falsifian if (err)
8737 179f9db0 2023-06-20 falsifian return err;
8738 179f9db0 2023-06-20 falsifian
8739 179f9db0 2023-06-20 falsifian err = get_merge_commit_ref_name(&commit_refname, worktree);
8740 179f9db0 2023-06-20 falsifian if (err)
8741 179f9db0 2023-06-20 falsifian return err;
8742 179f9db0 2023-06-20 falsifian
8743 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8744 f259c4c1 2021-09-24 stsp if (err)
8745 f259c4c1 2021-09-24 stsp goto done;
8746 f259c4c1 2021-09-24 stsp
8747 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8748 f259c4c1 2021-09-24 stsp if (err)
8749 f259c4c1 2021-09-24 stsp goto done;
8750 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8751 f259c4c1 2021-09-24 stsp if (err)
8752 f259c4c1 2021-09-24 stsp goto done;
8753 f259c4c1 2021-09-24 stsp
8754 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8755 f259c4c1 2021-09-24 stsp if (err)
8756 f259c4c1 2021-09-24 stsp goto done;
8757 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8758 f259c4c1 2021-09-24 stsp if (err)
8759 f259c4c1 2021-09-24 stsp goto done;
8760 f259c4c1 2021-09-24 stsp
8761 f259c4c1 2021-09-24 stsp done:
8762 f259c4c1 2021-09-24 stsp free(branch_refname);
8763 f259c4c1 2021-09-24 stsp free(commit_refname);
8764 f259c4c1 2021-09-24 stsp if (branch_ref)
8765 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8766 f259c4c1 2021-09-24 stsp if (commit_ref)
8767 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8768 179f9db0 2023-06-20 falsifian free(branch_tip);
8769 f259c4c1 2021-09-24 stsp return err;
8770 f259c4c1 2021-09-24 stsp }
8771 f259c4c1 2021-09-24 stsp
8772 f259c4c1 2021-09-24 stsp const struct got_error *
8773 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8774 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8775 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8776 f259c4c1 2021-09-24 stsp {
8777 f259c4c1 2021-09-24 stsp const struct got_error *err;
8778 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8779 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8780 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8781 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8782 f259c4c1 2021-09-24 stsp
8783 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8784 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8785 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8786 f259c4c1 2021-09-24 stsp
8787 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8788 f259c4c1 2021-09-24 stsp if (err)
8789 f259c4c1 2021-09-24 stsp return err;
8790 f259c4c1 2021-09-24 stsp
8791 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8792 f259c4c1 2021-09-24 stsp if (err)
8793 f259c4c1 2021-09-24 stsp goto done;
8794 f259c4c1 2021-09-24 stsp
8795 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8796 f259c4c1 2021-09-24 stsp &have_staged_files);
8797 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8798 f259c4c1 2021-09-24 stsp goto done;
8799 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8800 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8801 f259c4c1 2021-09-24 stsp goto done;
8802 f259c4c1 2021-09-24 stsp }
8803 f259c4c1 2021-09-24 stsp
8804 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8805 f259c4c1 2021-09-24 stsp if (err)
8806 f259c4c1 2021-09-24 stsp goto done;
8807 f259c4c1 2021-09-24 stsp
8808 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8809 f259c4c1 2021-09-24 stsp if (err)
8810 f259c4c1 2021-09-24 stsp goto done;
8811 f259c4c1 2021-09-24 stsp
8812 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8813 f259c4c1 2021-09-24 stsp if (err)
8814 f259c4c1 2021-09-24 stsp goto done;
8815 f259c4c1 2021-09-24 stsp
8816 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8817 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8818 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8819 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8820 f259c4c1 2021-09-24 stsp goto done;
8821 f259c4c1 2021-09-24 stsp }
8822 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8823 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8824 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8825 f259c4c1 2021-09-24 stsp goto done;
8826 f259c4c1 2021-09-24 stsp }
8827 f259c4c1 2021-09-24 stsp
8828 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8829 f259c4c1 2021-09-24 stsp if (err)
8830 f259c4c1 2021-09-24 stsp goto done;
8831 f259c4c1 2021-09-24 stsp
8832 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8833 f259c4c1 2021-09-24 stsp if (err)
8834 f259c4c1 2021-09-24 stsp goto done;
8835 f259c4c1 2021-09-24 stsp done:
8836 f259c4c1 2021-09-24 stsp free(commit_refname);
8837 f259c4c1 2021-09-24 stsp free(branch_refname);
8838 f259c4c1 2021-09-24 stsp free(fileindex_path);
8839 f259c4c1 2021-09-24 stsp if (commit_ref)
8840 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8841 f259c4c1 2021-09-24 stsp if (branch_ref)
8842 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8843 f259c4c1 2021-09-24 stsp if (err) {
8844 f259c4c1 2021-09-24 stsp if (*branch_name) {
8845 f259c4c1 2021-09-24 stsp free(*branch_name);
8846 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8847 f259c4c1 2021-09-24 stsp }
8848 f259c4c1 2021-09-24 stsp free(*branch_tip);
8849 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8850 f259c4c1 2021-09-24 stsp if (*fileindex) {
8851 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8852 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8853 f259c4c1 2021-09-24 stsp }
8854 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8855 f259c4c1 2021-09-24 stsp }
8856 f259c4c1 2021-09-24 stsp return err;
8857 f259c4c1 2021-09-24 stsp }
8858 f259c4c1 2021-09-24 stsp
8859 f259c4c1 2021-09-24 stsp const struct got_error *
8860 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8861 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8862 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8863 f259c4c1 2021-09-24 stsp {
8864 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8865 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8866 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8867 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8868 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8869 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8870 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8871 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8872 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8873 af179be7 2023-04-14 stsp
8874 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8875 af179be7 2023-04-14 stsp
8876 af179be7 2023-04-14 stsp err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8877 af179be7 2023-04-14 stsp if (err)
8878 af179be7 2023-04-14 stsp goto done;
8879 af179be7 2023-04-14 stsp
8880 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8881 af179be7 2023-04-14 stsp if (err)
8882 af179be7 2023-04-14 stsp goto done;
8883 af179be7 2023-04-14 stsp
8884 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8885 af179be7 2023-04-14 stsp if (err)
8886 af179be7 2023-04-14 stsp goto done;
8887 af179be7 2023-04-14 stsp
8888 af179be7 2023-04-14 stsp /*
8889 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8890 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8891 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
8892 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
8893 af179be7 2023-04-14 stsp */
8894 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(&added_paths,
8895 af179be7 2023-04-14 stsp got_worktree_get_base_commit_id(worktree), merged_commit_id,
8896 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8897 af179be7 2023-04-14 stsp if (err)
8898 af179be7 2023-04-14 stsp goto done;
8899 f259c4c1 2021-09-24 stsp
8900 af179be7 2023-04-14 stsp
8901 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8902 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8903 a44927cc 2022-04-07 stsp if (err)
8904 a44927cc 2022-04-07 stsp goto done;
8905 a44927cc 2022-04-07 stsp
8906 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8907 a44927cc 2022-04-07 stsp worktree->path_prefix);
8908 f259c4c1 2021-09-24 stsp if (err)
8909 f259c4c1 2021-09-24 stsp goto done;
8910 f259c4c1 2021-09-24 stsp
8911 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8912 f259c4c1 2021-09-24 stsp if (err)
8913 f259c4c1 2021-09-24 stsp goto done;
8914 f259c4c1 2021-09-24 stsp
8915 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8916 f259c4c1 2021-09-24 stsp if (err)
8917 f259c4c1 2021-09-24 stsp goto done;
8918 f259c4c1 2021-09-24 stsp
8919 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8920 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8921 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8922 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8923 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8924 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8925 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8926 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8927 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8928 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8929 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8930 f259c4c1 2021-09-24 stsp if (err)
8931 f259c4c1 2021-09-24 stsp goto sync;
8932 f259c4c1 2021-09-24 stsp
8933 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8934 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8935 f259c4c1 2021-09-24 stsp sync:
8936 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8937 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8938 f259c4c1 2021-09-24 stsp err = sync_err;
8939 f259c4c1 2021-09-24 stsp done:
8940 f259c4c1 2021-09-24 stsp free(tree_id);
8941 af179be7 2023-04-14 stsp free(merged_commit_id);
8942 a44927cc 2022-04-07 stsp if (commit)
8943 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8944 f259c4c1 2021-09-24 stsp if (fileindex)
8945 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8946 f259c4c1 2021-09-24 stsp free(fileindex_path);
8947 af179be7 2023-04-14 stsp if (commit_ref)
8948 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8949 af179be7 2023-04-14 stsp free(commit_ref_name);
8950 f259c4c1 2021-09-24 stsp
8951 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8952 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8953 f259c4c1 2021-09-24 stsp err = unlockerr;
8954 f259c4c1 2021-09-24 stsp return err;
8955 f259c4c1 2021-09-24 stsp }
8956 f259c4c1 2021-09-24 stsp
8957 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8958 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8959 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8960 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8961 2db2652d 2019-08-07 stsp struct got_repository *repo;
8962 2db2652d 2019-08-07 stsp int have_changes;
8963 2db2652d 2019-08-07 stsp };
8964 2db2652d 2019-08-07 stsp
8965 336075a4 2022-06-25 op static const struct got_error *
8966 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8967 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8968 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8969 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8970 735ef5ac 2019-08-03 stsp {
8971 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8972 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8973 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8974 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8975 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8976 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8977 8b13ce36 2019-08-08 stsp
8978 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8979 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8980 8b13ce36 2019-08-08 stsp return NULL;
8981 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8982 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8983 735ef5ac 2019-08-03 stsp
8984 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8985 735ef5ac 2019-08-03 stsp if (ie == NULL)
8986 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8987 735ef5ac 2019-08-03 stsp
8988 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8989 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8990 735ef5ac 2019-08-03 stsp relpath) == -1)
8991 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8992 735ef5ac 2019-08-03 stsp
8993 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8994 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8995 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8996 735ef5ac 2019-08-03 stsp }
8997 735ef5ac 2019-08-03 stsp
8998 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8999 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
9000 3aa5969e 2019-08-06 stsp goto done;
9001 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
9002 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
9003 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
9004 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9005 735ef5ac 2019-08-03 stsp goto done;
9006 3aa5969e 2019-08-06 stsp }
9007 735ef5ac 2019-08-03 stsp
9008 2db2652d 2019-08-07 stsp a->have_changes = 1;
9009 2db2652d 2019-08-07 stsp
9010 735ef5ac 2019-08-03 stsp p = in_repo_path;
9011 735ef5ac 2019-08-03 stsp while (p[0] == '/')
9012 735ef5ac 2019-08-03 stsp p++;
9013 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
9014 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
9015 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
9016 735ef5ac 2019-08-03 stsp done:
9017 735ef5ac 2019-08-03 stsp free(in_repo_path);
9018 dc424a06 2019-08-07 stsp return err;
9019 dc424a06 2019-08-07 stsp }
9020 dc424a06 2019-08-07 stsp
9021 2db2652d 2019-08-07 stsp struct stage_path_arg {
9022 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9023 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9024 2db2652d 2019-08-07 stsp struct got_repository *repo;
9025 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
9026 2db2652d 2019-08-07 stsp void *status_arg;
9027 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
9028 2db2652d 2019-08-07 stsp void *patch_arg;
9029 7b5dc508 2019-10-28 stsp int staged_something;
9030 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
9031 2db2652d 2019-08-07 stsp };
9032 2db2652d 2019-08-07 stsp
9033 2db2652d 2019-08-07 stsp static const struct got_error *
9034 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
9035 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9036 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9037 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9038 0cb83759 2019-08-03 stsp {
9039 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
9040 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
9041 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
9042 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
9043 0cb83759 2019-08-03 stsp uint32_t stage;
9044 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
9045 0aeb8099 2020-07-23 stsp struct stat sb;
9046 8b13ce36 2019-08-08 stsp
9047 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
9048 8b13ce36 2019-08-08 stsp return NULL;
9049 0cb83759 2019-08-03 stsp
9050 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9051 d3e7c587 2019-08-03 stsp if (ie == NULL)
9052 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9053 0cb83759 2019-08-03 stsp
9054 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9055 2db2652d 2019-08-07 stsp relpath)== -1)
9056 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
9057 0cb83759 2019-08-03 stsp
9058 0cb83759 2019-08-03 stsp switch (status) {
9059 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9060 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9061 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9062 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9063 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9064 0aeb8099 2020-07-23 stsp break;
9065 0aeb8099 2020-07-23 stsp }
9066 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9067 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9068 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9069 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9070 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9071 dc424a06 2019-08-07 stsp if (err)
9072 dc424a06 2019-08-07 stsp break;
9073 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9074 dc424a06 2019-08-07 stsp break;
9075 dc424a06 2019-08-07 stsp } else {
9076 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9077 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9078 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9079 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9080 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9081 dc424a06 2019-08-07 stsp break;
9082 dc424a06 2019-08-07 stsp }
9083 dc424a06 2019-08-07 stsp }
9084 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9085 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9086 0cb83759 2019-08-03 stsp if (err)
9087 dc424a06 2019-08-07 stsp break;
9088 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9089 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9090 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9091 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9092 0cb83759 2019-08-03 stsp else
9093 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9094 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9095 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9096 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9097 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9098 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9099 35213c7c 2020-07-23 stsp ssize_t target_len;
9100 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9101 35213c7c 2020-07-23 stsp sizeof(target_path));
9102 35213c7c 2020-07-23 stsp if (target_len == -1) {
9103 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9104 35213c7c 2020-07-23 stsp ondisk_path);
9105 35213c7c 2020-07-23 stsp break;
9106 35213c7c 2020-07-23 stsp }
9107 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9108 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9109 df6221c7 2023-07-19 stsp a->worktree->root_path,
9110 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
9111 35213c7c 2020-07-23 stsp if (err)
9112 35213c7c 2020-07-23 stsp break;
9113 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9114 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9115 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9116 35213c7c 2020-07-23 stsp break;
9117 35213c7c 2020-07-23 stsp }
9118 35213c7c 2020-07-23 stsp }
9119 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9120 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9121 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9122 35213c7c 2020-07-23 stsp else
9123 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9124 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9125 0aeb8099 2020-07-23 stsp } else {
9126 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9127 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9128 0aeb8099 2020-07-23 stsp }
9129 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9130 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9131 dc424a06 2019-08-07 stsp break;
9132 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9133 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9134 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9135 9fdde394 2022-06-04 op if (err)
9136 9fdde394 2022-06-04 op break;
9137 9fdde394 2022-06-04 op /*
9138 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
9139 9fdde394 2022-06-04 op * implicitly unstage the file.
9140 9fdde394 2022-06-04 op */
9141 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9142 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
9143 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
9144 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
9145 9fdde394 2022-06-04 op }
9146 0cb83759 2019-08-03 stsp break;
9147 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9148 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9149 d3e7c587 2019-08-03 stsp break;
9150 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9151 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9152 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9153 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9154 dc424a06 2019-08-07 stsp if (err)
9155 dc424a06 2019-08-07 stsp break;
9156 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9157 88f33a19 2019-08-08 stsp break;
9158 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9159 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9160 dc424a06 2019-08-07 stsp break;
9161 88f33a19 2019-08-08 stsp }
9162 dc424a06 2019-08-07 stsp }
9163 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9164 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9165 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9166 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9167 dc424a06 2019-08-07 stsp break;
9168 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9169 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9170 12463d8b 2019-12-13 stsp de_name);
9171 0cb83759 2019-08-03 stsp break;
9172 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9173 d3e7c587 2019-08-03 stsp break;
9174 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9175 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9176 ebf48fd5 2019-08-03 stsp break;
9177 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9178 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9179 2a06fe5f 2019-08-24 stsp break;
9180 0cb83759 2019-08-03 stsp default:
9181 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9182 537ac44b 2019-08-03 stsp break;
9183 0cb83759 2019-08-03 stsp }
9184 dc424a06 2019-08-07 stsp
9185 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9186 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9187 dc424a06 2019-08-07 stsp free(path_content);
9188 2db2652d 2019-08-07 stsp free(ondisk_path);
9189 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9190 0ebf8283 2019-07-24 stsp return err;
9191 0ebf8283 2019-07-24 stsp }
9192 0cb83759 2019-08-03 stsp
9193 0cb83759 2019-08-03 stsp const struct got_error *
9194 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9195 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9196 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9197 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9198 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9199 0cb83759 2019-08-03 stsp {
9200 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9201 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9202 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9203 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9204 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9205 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9206 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9207 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9208 0cb83759 2019-08-03 stsp
9209 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9210 0cb83759 2019-08-03 stsp if (err)
9211 0cb83759 2019-08-03 stsp return err;
9212 0cb83759 2019-08-03 stsp
9213 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9214 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9215 735ef5ac 2019-08-03 stsp if (err)
9216 735ef5ac 2019-08-03 stsp goto done;
9217 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9218 735ef5ac 2019-08-03 stsp if (err)
9219 735ef5ac 2019-08-03 stsp goto done;
9220 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9221 0cb83759 2019-08-03 stsp if (err)
9222 0cb83759 2019-08-03 stsp goto done;
9223 0cb83759 2019-08-03 stsp
9224 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9225 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9226 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9227 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9228 2db2652d 2019-08-07 stsp oka.repo = repo;
9229 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9230 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9231 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9232 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
9233 735ef5ac 2019-08-03 stsp if (err)
9234 735ef5ac 2019-08-03 stsp goto done;
9235 735ef5ac 2019-08-03 stsp }
9236 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9237 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9238 2db2652d 2019-08-07 stsp goto done;
9239 2db2652d 2019-08-07 stsp }
9240 735ef5ac 2019-08-03 stsp
9241 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9242 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9243 2db2652d 2019-08-07 stsp spa.repo = repo;
9244 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9245 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9246 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9247 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9248 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9249 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9250 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9251 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9252 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
9253 42005733 2019-08-03 stsp if (err)
9254 2db2652d 2019-08-07 stsp goto done;
9255 7b5dc508 2019-10-28 stsp }
9256 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9257 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9258 7b5dc508 2019-10-28 stsp goto done;
9259 0cb83759 2019-08-03 stsp }
9260 0cb83759 2019-08-03 stsp
9261 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9262 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9263 0cb83759 2019-08-03 stsp err = sync_err;
9264 0cb83759 2019-08-03 stsp done:
9265 735ef5ac 2019-08-03 stsp if (head_ref)
9266 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9267 735ef5ac 2019-08-03 stsp free(head_commit_id);
9268 0cb83759 2019-08-03 stsp free(fileindex_path);
9269 0cb83759 2019-08-03 stsp if (fileindex)
9270 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9271 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9272 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9273 0cb83759 2019-08-03 stsp err = unlockerr;
9274 0cb83759 2019-08-03 stsp return err;
9275 0cb83759 2019-08-03 stsp }
9276 ad493afc 2019-08-03 stsp
9277 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9278 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9279 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9280 ad493afc 2019-08-03 stsp struct got_repository *repo;
9281 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9282 ad493afc 2019-08-03 stsp void *progress_arg;
9283 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9284 2e1f37b0 2019-08-08 stsp void *patch_arg;
9285 ad493afc 2019-08-03 stsp };
9286 2e1f37b0 2019-08-08 stsp
9287 2e1f37b0 2019-08-08 stsp static const struct got_error *
9288 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9289 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9290 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9291 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9292 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9293 2e1f37b0 2019-08-08 stsp {
9294 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9295 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9296 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9297 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9298 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9299 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9300 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9301 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9302 2e1f37b0 2019-08-08 stsp
9303 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9304 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9305 2e1f37b0 2019-08-08 stsp
9306 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9307 2e1f37b0 2019-08-08 stsp if (err)
9308 2e1f37b0 2019-08-08 stsp return err;
9309 eb81bc23 2022-06-28 tracey
9310 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9311 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9312 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9313 eb81bc23 2022-06-28 tracey goto done;
9314 eb81bc23 2022-06-28 tracey }
9315 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9316 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9317 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9318 eb81bc23 2022-06-28 tracey goto done;
9319 eb81bc23 2022-06-28 tracey }
9320 eb81bc23 2022-06-28 tracey
9321 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9322 2e1f37b0 2019-08-08 stsp if (err)
9323 2e1f37b0 2019-08-08 stsp goto done;
9324 2e1f37b0 2019-08-08 stsp
9325 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9326 2e1f37b0 2019-08-08 stsp if (err)
9327 2e1f37b0 2019-08-08 stsp goto done;
9328 2e1f37b0 2019-08-08 stsp
9329 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9330 2e1f37b0 2019-08-08 stsp if (err)
9331 2e1f37b0 2019-08-08 stsp goto done;
9332 2e1f37b0 2019-08-08 stsp
9333 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9334 eb81bc23 2022-06-28 tracey fd2);
9335 2e1f37b0 2019-08-08 stsp if (err)
9336 2e1f37b0 2019-08-08 stsp goto done;
9337 2e1f37b0 2019-08-08 stsp
9338 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9339 2e1f37b0 2019-08-08 stsp if (err)
9340 2e1f37b0 2019-08-08 stsp goto done;
9341 ad493afc 2019-08-03 stsp
9342 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9343 2e1f37b0 2019-08-08 stsp if (err)
9344 2e1f37b0 2019-08-08 stsp goto done;
9345 2e1f37b0 2019-08-08 stsp
9346 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9347 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9348 2e1f37b0 2019-08-08 stsp if (err)
9349 2e1f37b0 2019-08-08 stsp goto done;
9350 2e1f37b0 2019-08-08 stsp
9351 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9352 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
9353 2e1f37b0 2019-08-08 stsp if (err)
9354 2e1f37b0 2019-08-08 stsp goto done;
9355 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9356 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
9357 2e1f37b0 2019-08-08 stsp if (err)
9358 2e1f37b0 2019-08-08 stsp goto done;
9359 2e1f37b0 2019-08-08 stsp
9360 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9361 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9362 2e1f37b0 2019-08-08 stsp goto done;
9363 2e1f37b0 2019-08-08 stsp }
9364 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9365 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9366 2e1f37b0 2019-08-08 stsp goto done;
9367 2e1f37b0 2019-08-08 stsp }
9368 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9369 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9370 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
9371 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9372 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9373 fe621944 2020-11-10 stsp nchanges++;
9374 fe621944 2020-11-10 stsp }
9375 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9376 2e1f37b0 2019-08-08 stsp int choice;
9377 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9378 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9379 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9380 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9381 2e1f37b0 2019-08-08 stsp if (err)
9382 2e1f37b0 2019-08-08 stsp goto done;
9383 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9384 2e1f37b0 2019-08-08 stsp have_content = 1;
9385 19e4b907 2019-08-08 stsp else
9386 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9387 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9388 2e1f37b0 2019-08-08 stsp break;
9389 2e1f37b0 2019-08-08 stsp }
9390 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9391 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9392 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9393 2e1f37b0 2019-08-08 stsp done:
9394 2e1f37b0 2019-08-08 stsp free(label1);
9395 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9396 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9397 2e1f37b0 2019-08-08 stsp if (blob)
9398 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9399 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9400 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9401 2e1f37b0 2019-08-08 stsp if (staged_blob)
9402 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9403 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9404 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9405 fe621944 2020-11-10 stsp err = free_err;
9406 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9407 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9408 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9409 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9410 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9411 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9412 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9413 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9414 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9415 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9416 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9417 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9418 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9419 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9420 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9421 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9422 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9423 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9424 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9425 2e1f37b0 2019-08-08 stsp }
9426 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9427 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9428 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9429 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9430 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9431 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9432 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9433 2e1f37b0 2019-08-08 stsp }
9434 2e1f37b0 2019-08-08 stsp free(path1);
9435 2e1f37b0 2019-08-08 stsp free(path2);
9436 fda8017d 2020-07-23 stsp return err;
9437 fda8017d 2020-07-23 stsp }
9438 fda8017d 2020-07-23 stsp
9439 fda8017d 2020-07-23 stsp static const struct got_error *
9440 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9441 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9442 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9443 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9444 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9445 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9446 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9447 fda8017d 2020-07-23 stsp {
9448 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9449 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9450 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9451 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9452 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9453 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9454 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9455 fda8017d 2020-07-23 stsp struct stat sb;
9456 fda8017d 2020-07-23 stsp
9457 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9458 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9459 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9460 fda8017d 2020-07-23 stsp if (err)
9461 fda8017d 2020-07-23 stsp return err;
9462 fda8017d 2020-07-23 stsp
9463 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9464 fda8017d 2020-07-23 stsp return NULL;
9465 fda8017d 2020-07-23 stsp
9466 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9467 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9468 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9469 fda8017d 2020-07-23 stsp if (err)
9470 fda8017d 2020-07-23 stsp goto done;
9471 fda8017d 2020-07-23 stsp }
9472 fda8017d 2020-07-23 stsp
9473 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
9474 fda8017d 2020-07-23 stsp if (f == NULL) {
9475 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9476 fda8017d 2020-07-23 stsp path_unstaged_content);
9477 fda8017d 2020-07-23 stsp goto done;
9478 fda8017d 2020-07-23 stsp }
9479 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9480 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9481 fda8017d 2020-07-23 stsp goto done;
9482 fda8017d 2020-07-23 stsp }
9483 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9484 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9485 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9486 fda8017d 2020-07-23 stsp size_t r;
9487 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9488 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9489 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9490 fda8017d 2020-07-23 stsp goto done;
9491 fda8017d 2020-07-23 stsp }
9492 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9493 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9494 fda8017d 2020-07-23 stsp goto done;
9495 fda8017d 2020-07-23 stsp }
9496 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9497 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9498 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9499 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9500 fda8017d 2020-07-23 stsp progress_arg);
9501 fda8017d 2020-07-23 stsp } else {
9502 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9503 67a66647 2021-05-31 stsp
9504 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9505 67a66647 2021-05-31 stsp if (err)
9506 67a66647 2021-05-31 stsp return err;
9507 67a66647 2021-05-31 stsp
9508 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9509 67a66647 2021-05-31 stsp parent) == -1) {
9510 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9511 67a66647 2021-05-31 stsp base_path = NULL;
9512 67a66647 2021-05-31 stsp goto done;
9513 67a66647 2021-05-31 stsp }
9514 67a66647 2021-05-31 stsp
9515 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
9516 b90054ed 2022-11-01 stsp base_path, "");
9517 67a66647 2021-05-31 stsp if (err)
9518 67a66647 2021-05-31 stsp goto done;
9519 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9520 67a66647 2021-05-31 stsp blob_base);
9521 67a66647 2021-05-31 stsp if (err)
9522 67a66647 2021-05-31 stsp goto done;
9523 67a66647 2021-05-31 stsp
9524 5e91dae4 2022-08-30 stsp /*
9525 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9526 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9527 eec2f5a9 2021-06-03 stsp */
9528 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9529 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9530 eec2f5a9 2021-06-03 stsp ondisk_path);
9531 eec2f5a9 2021-06-03 stsp if (err)
9532 eec2f5a9 2021-06-03 stsp goto done;
9533 eec2f5a9 2021-06-03 stsp } else {
9534 eec2f5a9 2021-06-03 stsp int fd;
9535 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
9536 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9537 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9538 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9539 eec2f5a9 2021-06-03 stsp goto done;
9540 eec2f5a9 2021-06-03 stsp }
9541 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9542 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9543 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9544 eec2f5a9 2021-06-03 stsp close(fd);
9545 eec2f5a9 2021-06-03 stsp goto done;
9546 eec2f5a9 2021-06-03 stsp }
9547 eec2f5a9 2021-06-03 stsp }
9548 eec2f5a9 2021-06-03 stsp
9549 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9550 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9551 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9552 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9553 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9554 fda8017d 2020-07-23 stsp }
9555 fda8017d 2020-07-23 stsp if (err)
9556 fda8017d 2020-07-23 stsp goto done;
9557 fda8017d 2020-07-23 stsp
9558 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9559 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9560 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9561 2ac8aa02 2020-11-09 stsp } else {
9562 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9563 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9564 2ac8aa02 2020-11-09 stsp }
9565 fda8017d 2020-07-23 stsp done:
9566 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9567 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9568 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9569 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9570 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9571 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9572 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9573 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9574 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9575 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9576 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9577 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9578 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9579 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9580 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9581 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9582 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9583 67a66647 2021-05-31 stsp free(blob_base_path);
9584 67a66647 2021-05-31 stsp free(parent);
9585 67a66647 2021-05-31 stsp free(base_path);
9586 2e1f37b0 2019-08-08 stsp return err;
9587 2e1f37b0 2019-08-08 stsp }
9588 2e1f37b0 2019-08-08 stsp
9589 ad493afc 2019-08-03 stsp static const struct got_error *
9590 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9591 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9592 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9593 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9594 ad493afc 2019-08-03 stsp {
9595 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9596 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9597 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9598 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9599 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9600 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9601 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9602 9bc94a15 2019-08-03 stsp struct stat sb;
9603 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9604 ad493afc 2019-08-03 stsp
9605 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9606 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9607 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9608 2e1f37b0 2019-08-08 stsp return NULL;
9609 2e1f37b0 2019-08-08 stsp
9610 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9611 ad493afc 2019-08-03 stsp if (ie == NULL)
9612 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9613 9bc94a15 2019-08-03 stsp
9614 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9615 9bc94a15 2019-08-03 stsp == -1)
9616 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9617 ad493afc 2019-08-03 stsp
9618 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9619 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9620 f69721c3 2019-10-21 stsp if (err)
9621 f69721c3 2019-10-21 stsp goto done;
9622 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9623 f69721c3 2019-10-21 stsp id_str) == -1) {
9624 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9625 eb81bc23 2022-06-28 tracey goto done;
9626 eb81bc23 2022-06-28 tracey }
9627 eb81bc23 2022-06-28 tracey
9628 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9629 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9630 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9631 eb81bc23 2022-06-28 tracey goto done;
9632 eb81bc23 2022-06-28 tracey }
9633 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9634 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9635 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9636 f69721c3 2019-10-21 stsp goto done;
9637 f69721c3 2019-10-21 stsp }
9638 f69721c3 2019-10-21 stsp
9639 ad493afc 2019-08-03 stsp switch (staged_status) {
9640 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9641 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9642 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9643 ad493afc 2019-08-03 stsp if (err)
9644 ad493afc 2019-08-03 stsp break;
9645 ad493afc 2019-08-03 stsp /* fall through */
9646 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9647 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9648 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9649 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9650 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9651 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9652 2e1f37b0 2019-08-08 stsp if (err)
9653 2e1f37b0 2019-08-08 stsp break;
9654 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9655 2e1f37b0 2019-08-08 stsp break;
9656 2e1f37b0 2019-08-08 stsp } else {
9657 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9658 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9659 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9660 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9661 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9662 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9663 2e1f37b0 2019-08-08 stsp }
9664 2e1f37b0 2019-08-08 stsp }
9665 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9666 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9667 ad493afc 2019-08-03 stsp if (err)
9668 ad493afc 2019-08-03 stsp break;
9669 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9670 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9671 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9672 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9673 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9674 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9675 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9676 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9677 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9678 ea7786be 2020-07-23 stsp break;
9679 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9680 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9681 36bf999c 2020-07-23 stsp char *staged_target;
9682 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9683 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9684 36bf999c 2020-07-23 stsp if (err)
9685 36bf999c 2020-07-23 stsp goto done;
9686 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9687 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9688 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9689 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9690 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9691 36bf999c 2020-07-23 stsp free(staged_target);
9692 dfe9fba0 2020-07-23 stsp } else {
9693 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9694 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9695 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9696 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9697 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9698 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9699 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9700 dfe9fba0 2020-07-23 stsp }
9701 ea7786be 2020-07-23 stsp break;
9702 ea7786be 2020-07-23 stsp default:
9703 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9704 ea7786be 2020-07-23 stsp break;
9705 ea7786be 2020-07-23 stsp }
9706 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9707 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9708 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9709 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9710 2ac8aa02 2020-11-09 stsp }
9711 ad493afc 2019-08-03 stsp break;
9712 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9713 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9714 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9715 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9716 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9717 2e1f37b0 2019-08-08 stsp if (err)
9718 2e1f37b0 2019-08-08 stsp break;
9719 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9720 2e1f37b0 2019-08-08 stsp break;
9721 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9722 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9723 2e1f37b0 2019-08-08 stsp break;
9724 2e1f37b0 2019-08-08 stsp }
9725 2e1f37b0 2019-08-08 stsp }
9726 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9727 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9728 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9729 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9730 9bc94a15 2019-08-03 stsp if (err)
9731 9bc94a15 2019-08-03 stsp break;
9732 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9733 ad493afc 2019-08-03 stsp break;
9734 ad493afc 2019-08-03 stsp }
9735 f69721c3 2019-10-21 stsp done:
9736 ad493afc 2019-08-03 stsp free(ondisk_path);
9737 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9738 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9739 ad493afc 2019-08-03 stsp if (blob_base)
9740 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9741 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9742 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9743 ad493afc 2019-08-03 stsp if (blob_staged)
9744 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9745 f69721c3 2019-10-21 stsp free(id_str);
9746 f69721c3 2019-10-21 stsp free(label_orig);
9747 ad493afc 2019-08-03 stsp return err;
9748 ad493afc 2019-08-03 stsp }
9749 ad493afc 2019-08-03 stsp
9750 ad493afc 2019-08-03 stsp const struct got_error *
9751 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9752 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9753 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9754 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9755 ad493afc 2019-08-03 stsp struct got_repository *repo)
9756 ad493afc 2019-08-03 stsp {
9757 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9758 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9759 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9760 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9761 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9762 ad493afc 2019-08-03 stsp
9763 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9764 ad493afc 2019-08-03 stsp if (err)
9765 ad493afc 2019-08-03 stsp return err;
9766 ad493afc 2019-08-03 stsp
9767 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9768 ad493afc 2019-08-03 stsp if (err)
9769 ad493afc 2019-08-03 stsp goto done;
9770 ad493afc 2019-08-03 stsp
9771 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9772 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9773 ad493afc 2019-08-03 stsp upa.repo = repo;
9774 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9775 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9776 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9777 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9778 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9779 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9780 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9781 ad493afc 2019-08-03 stsp if (err)
9782 ad493afc 2019-08-03 stsp goto done;
9783 ad493afc 2019-08-03 stsp }
9784 ad493afc 2019-08-03 stsp
9785 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9786 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9787 ad493afc 2019-08-03 stsp err = sync_err;
9788 ad493afc 2019-08-03 stsp done:
9789 ad493afc 2019-08-03 stsp free(fileindex_path);
9790 ad493afc 2019-08-03 stsp if (fileindex)
9791 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9792 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9793 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9794 ad493afc 2019-08-03 stsp err = unlockerr;
9795 ad493afc 2019-08-03 stsp return err;
9796 ad493afc 2019-08-03 stsp }
9797 b2118c49 2020-07-28 stsp
9798 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9799 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9800 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9801 b2118c49 2020-07-28 stsp void *info_arg;
9802 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9803 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9804 b2118c49 2020-07-28 stsp void *cancel_arg;
9805 b2118c49 2020-07-28 stsp };
9806 b2118c49 2020-07-28 stsp
9807 b2118c49 2020-07-28 stsp static const struct got_error *
9808 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9809 b2118c49 2020-07-28 stsp {
9810 f6b8c3c2 2023-07-24 stsp const struct got_error *err;
9811 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9812 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9813 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9814 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9815 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9816 b2118c49 2020-07-28 stsp int stage;
9817 b2118c49 2020-07-28 stsp
9818 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
9819 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
9820 f6b8c3c2 2023-07-24 stsp if (err)
9821 f6b8c3c2 2023-07-24 stsp return err;
9822 f6b8c3c2 2023-07-24 stsp }
9823 b2118c49 2020-07-28 stsp
9824 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9825 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9826 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9827 b2118c49 2020-07-28 stsp break;
9828 b2118c49 2020-07-28 stsp }
9829 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9830 b2118c49 2020-07-28 stsp return NULL;
9831 b2118c49 2020-07-28 stsp
9832 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9833 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9834 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9835 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9836 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9837 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9838 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9839 b2118c49 2020-07-28 stsp }
9840 b2118c49 2020-07-28 stsp
9841 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9842 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9843 b2118c49 2020-07-28 stsp
9844 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9845 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9846 b2118c49 2020-07-28 stsp }
9847 b2118c49 2020-07-28 stsp
9848 b2118c49 2020-07-28 stsp const struct got_error *
9849 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9850 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9851 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9852 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9853 b2118c49 2020-07-28 stsp
9854 b2118c49 2020-07-28 stsp {
9855 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9856 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9857 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9858 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9859 b2118c49 2020-07-28 stsp
9860 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9861 b2118c49 2020-07-28 stsp if (err)
9862 b2118c49 2020-07-28 stsp return err;
9863 b2118c49 2020-07-28 stsp
9864 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9865 b2118c49 2020-07-28 stsp if (err)
9866 b2118c49 2020-07-28 stsp goto done;
9867 b2118c49 2020-07-28 stsp
9868 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9869 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9870 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9871 b2118c49 2020-07-28 stsp arg.paths = paths;
9872 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9873 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9874 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9875 b2118c49 2020-07-28 stsp &arg);
9876 b2118c49 2020-07-28 stsp done:
9877 b2118c49 2020-07-28 stsp free(fileindex_path);
9878 b2118c49 2020-07-28 stsp if (fileindex)
9879 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9880 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9881 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9882 b2118c49 2020-07-28 stsp err = unlockerr;
9883 b2118c49 2020-07-28 stsp return err;
9884 b2118c49 2020-07-28 stsp }
9885 78f5ac24 2022-03-19 op
9886 78f5ac24 2022-03-19 op static const struct got_error *
9887 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9888 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9889 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9890 78f5ac24 2022-03-19 op {
9891 78f5ac24 2022-03-19 op const struct got_error *err;
9892 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9893 78f5ac24 2022-03-19 op struct stat sb;
9894 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9895 78f5ac24 2022-03-19 op
9896 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9897 78f5ac24 2022-03-19 op if (err)
9898 78f5ac24 2022-03-19 op return err;
9899 78f5ac24 2022-03-19 op
9900 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9901 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9902 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9903 78f5ac24 2022-03-19 op
9904 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9905 78f5ac24 2022-03-19 op if (ie) {
9906 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9907 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9908 a05fb460 2022-04-23 op repo);
9909 78f5ac24 2022-03-19 op if (err)
9910 78f5ac24 2022-03-19 op goto done;
9911 78f5ac24 2022-03-19 op } else {
9912 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9913 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9914 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9915 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9916 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9917 78f5ac24 2022-03-19 op ondisk_path);
9918 78f5ac24 2022-03-19 op goto done;
9919 78f5ac24 2022-03-19 op }
9920 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9921 78f5ac24 2022-03-19 op }
9922 78f5ac24 2022-03-19 op }
9923 78f5ac24 2022-03-19 op
9924 78f5ac24 2022-03-19 op done:
9925 78f5ac24 2022-03-19 op free(ondisk_path);
9926 78f5ac24 2022-03-19 op return err;
9927 78f5ac24 2022-03-19 op }
9928 78f5ac24 2022-03-19 op
9929 78f5ac24 2022-03-19 op static const struct got_error *
9930 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9931 78f5ac24 2022-03-19 op unsigned char staged_status)
9932 78f5ac24 2022-03-19 op {
9933 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9934 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9935 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9936 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9937 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9938 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9939 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9940 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9941 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9942 78f5ac24 2022-03-19 op return NULL;
9943 78f5ac24 2022-03-19 op }
9944 78f5ac24 2022-03-19 op
9945 78f5ac24 2022-03-19 op static const struct got_error *
9946 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9947 78f5ac24 2022-03-19 op {
9948 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9949 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9950 78f5ac24 2022-03-19 op return NULL;
9951 78f5ac24 2022-03-19 op }
9952 78f5ac24 2022-03-19 op
9953 78f5ac24 2022-03-19 op static const struct got_error *
9954 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9955 78f5ac24 2022-03-19 op unsigned char staged_status)
9956 78f5ac24 2022-03-19 op {
9957 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9958 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9959 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9960 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9961 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9962 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9963 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9964 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9965 78f5ac24 2022-03-19 op return NULL;
9966 78f5ac24 2022-03-19 op }
9967 78f5ac24 2022-03-19 op
9968 78f5ac24 2022-03-19 op const struct got_error *
9969 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9970 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9971 78f5ac24 2022-03-19 op {
9972 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9973 78f5ac24 2022-03-19 op }
9974 78f5ac24 2022-03-19 op
9975 78f5ac24 2022-03-19 op const struct got_error *
9976 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9977 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9978 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9979 78f5ac24 2022-03-19 op {
9980 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9981 78f5ac24 2022-03-19 op int file_renamed = 0;
9982 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9983 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9984 78f5ac24 2022-03-19 op
9985 78f5ac24 2022-03-19 op *oldpath = NULL;
9986 78f5ac24 2022-03-19 op *newpath = NULL;
9987 78f5ac24 2022-03-19 op
9988 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9989 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9990 78f5ac24 2022-03-19 op if (err)
9991 78f5ac24 2022-03-19 op goto done;
9992 78f5ac24 2022-03-19 op
9993 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9994 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9995 78f5ac24 2022-03-19 op if (err)
9996 78f5ac24 2022-03-19 op goto done;
9997 78f5ac24 2022-03-19 op
9998 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9999 78f5ac24 2022-03-19 op file_renamed = 1;
10000 78f5ac24 2022-03-19 op
10001 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
10002 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
10003 78f5ac24 2022-03-19 op else if (file_renamed) {
10004 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
10005 78f5ac24 2022-03-19 op if (err == NULL)
10006 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
10007 78f5ac24 2022-03-19 op } else if (old == NULL)
10008 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
10009 78f5ac24 2022-03-19 op else
10010 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
10011 78f5ac24 2022-03-19 op
10012 78f5ac24 2022-03-19 op done:
10013 78f5ac24 2022-03-19 op if (err) {
10014 78f5ac24 2022-03-19 op free(*oldpath);
10015 78f5ac24 2022-03-19 op *oldpath = NULL;
10016 78f5ac24 2022-03-19 op free(*newpath);
10017 78f5ac24 2022-03-19 op *newpath = NULL;
10018 78f5ac24 2022-03-19 op }
10019 78f5ac24 2022-03-19 op return err;
10020 78f5ac24 2022-03-19 op }
10021 78f5ac24 2022-03-19 op
10022 78f5ac24 2022-03-19 op const struct got_error *
10023 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10024 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
10025 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
10026 78f5ac24 2022-03-19 op {
10027 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
10028 f2dd7807 2022-05-17 op
10029 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
10030 f2dd7807 2022-05-17 op saa.worktree = worktree;
10031 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
10032 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
10033 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
10034 f2dd7807 2022-05-17 op saa.repo = repo;
10035 f2dd7807 2022-05-17 op
10036 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
10037 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
10038 78f5ac24 2022-03-19 op }
10039 f2dd7807 2022-05-17 op
10040 f2dd7807 2022-05-17 op const struct got_error *
10041 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10042 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
10043 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
10044 f2dd7807 2022-05-17 op {
10045 7f4e5320 2023-05-29 stsp const struct got_error *err;
10046 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
10047 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
10048 f2dd7807 2022-05-17 op
10049 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
10050 f2dd7807 2022-05-17 op sda.worktree = worktree;
10051 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
10052 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
10053 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
10054 f2dd7807 2022-05-17 op sda.repo = repo;
10055 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
10056 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
10057 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
10058 f2dd7807 2022-05-17 op sda.status_codes = NULL;
10059 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s/%s",
10060 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree), path) == -1)
10061 7f4e5320 2023-05-29 stsp return got_error_from_errno("asprintf");
10062 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
10063 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
10064 7f4e5320 2023-05-29 stsp
10065 7f4e5320 2023-05-29 stsp err = worktree_status(worktree, path, fileindex, repo,
10066 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10067 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
10068 7f4e5320 2023-05-29 stsp return err;
10069 f2dd7807 2022-05-17 op }
10070 f2dd7807 2022-05-17 op
10071 f2dd7807 2022-05-17 op const struct got_error *
10072 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
10073 4bcdc895 2022-05-17 op const char *fileindex_path)
10074 f2dd7807 2022-05-17 op {
10075 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
10076 f2dd7807 2022-05-17 op
10077 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
10078 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
10079 f2dd7807 2022-05-17 op
10080 f2dd7807 2022-05-17 op return err;
10081 f2dd7807 2022-05-17 op }