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 2021-01-02 naddy char *line = NULL;
1559 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1560 9bdd68dd 2021-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 2021-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 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
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 6841da00 2019-08-08 stsp }
3781 6841da00 2019-08-08 stsp return err;
3782 249b637c 2023-02-20 stsp }
3783 249b637c 2023-02-20 stsp
3784 249b637c 2023-02-20 stsp static int
3785 249b637c 2023-02-20 stsp match_path(const char *pattern, size_t pattern_len, const char *path,
3786 249b637c 2023-02-20 stsp int flags)
3787 249b637c 2023-02-20 stsp {
3788 249b637c 2023-02-20 stsp char buf[PATH_MAX];
3789 249b637c 2023-02-20 stsp
3790 249b637c 2023-02-20 stsp /*
3791 249b637c 2023-02-20 stsp * Trailing slashes signify directories.
3792 249b637c 2023-02-20 stsp * Append a * to make such patterns conform to fnmatch rules.
3793 249b637c 2023-02-20 stsp */
3794 249b637c 2023-02-20 stsp if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3795 249b637c 2023-02-20 stsp if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3796 249b637c 2023-02-20 stsp return FNM_NOMATCH; /* XXX */
3797 249b637c 2023-02-20 stsp
3798 249b637c 2023-02-20 stsp return fnmatch(buf, path, flags);
3799 249b637c 2023-02-20 stsp }
3800 249b637c 2023-02-20 stsp
3801 249b637c 2023-02-20 stsp return fnmatch(pattern, path, flags);
3802 6841da00 2019-08-08 stsp }
3803 6841da00 2019-08-08 stsp
3804 336075a4 2022-06-25 op static int
3805 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3806 6841da00 2019-08-08 stsp {
3807 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3808 bd8de430 2019-10-04 stsp
3809 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3810 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3811 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3812 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3813 bd8de430 2019-10-04 stsp
3814 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3815 249b637c 2023-02-20 stsp const char *p;
3816 6841da00 2019-08-08 stsp
3817 249b637c 2023-02-20 stsp if (pi->path_len < 3 ||
3818 249b637c 2023-02-20 stsp strncmp(pi->path, "**/", 3) != 0)
3819 bd8de430 2019-10-04 stsp continue;
3820 bd8de430 2019-10-04 stsp p = path;
3821 bd8de430 2019-10-04 stsp while (*p) {
3822 249b637c 2023-02-20 stsp if (match_path(pi->path + 3,
3823 249b637c 2023-02-20 stsp pi->path_len - 3, p,
3824 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3825 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3826 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3827 bd8de430 2019-10-04 stsp p++;
3828 bd8de430 2019-10-04 stsp while (*p == '/')
3829 bd8de430 2019-10-04 stsp p++;
3830 bd8de430 2019-10-04 stsp continue;
3831 bd8de430 2019-10-04 stsp }
3832 bd8de430 2019-10-04 stsp return 1;
3833 bd8de430 2019-10-04 stsp }
3834 bd8de430 2019-10-04 stsp }
3835 bd8de430 2019-10-04 stsp }
3836 bd8de430 2019-10-04 stsp
3837 6841da00 2019-08-08 stsp /*
3838 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3839 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3840 6841da00 2019-08-08 stsp * ignores backwards.
3841 6841da00 2019-08-08 stsp */
3842 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3843 6841da00 2019-08-08 stsp while (pe) {
3844 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3845 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3846 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3847 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3848 249b637c 2023-02-20 stsp int flags = FNM_LEADING_DIR;
3849 249b637c 2023-02-20 stsp if (strstr(pi->path, "/**/") == NULL)
3850 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3851 249b637c 2023-02-20 stsp if (match_path(pi->path, pi->path_len,
3852 249b637c 2023-02-20 stsp path, flags))
3853 6841da00 2019-08-08 stsp continue;
3854 6841da00 2019-08-08 stsp return 1;
3855 6841da00 2019-08-08 stsp }
3856 6841da00 2019-08-08 stsp }
3857 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3858 6841da00 2019-08-08 stsp }
3859 6841da00 2019-08-08 stsp
3860 6841da00 2019-08-08 stsp return 0;
3861 6841da00 2019-08-08 stsp }
3862 6841da00 2019-08-08 stsp
3863 6841da00 2019-08-08 stsp static const struct got_error *
3864 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3865 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3866 6841da00 2019-08-08 stsp {
3867 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3868 6841da00 2019-08-08 stsp char *ignorespath;
3869 886cec17 2019-12-15 stsp int fd = -1;
3870 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3871 6841da00 2019-08-08 stsp
3872 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3873 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3874 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3875 6841da00 2019-08-08 stsp
3876 886cec17 2019-12-15 stsp if (dirfd != -1) {
3877 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3878 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3879 886cec17 2019-12-15 stsp if (fd == -1) {
3880 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3881 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3882 886cec17 2019-12-15 stsp ignorespath);
3883 886cec17 2019-12-15 stsp } else {
3884 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3885 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3886 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3887 886cec17 2019-12-15 stsp ignorespath);
3888 886cec17 2019-12-15 stsp else {
3889 886cec17 2019-12-15 stsp fd = -1;
3890 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3891 886cec17 2019-12-15 stsp }
3892 886cec17 2019-12-15 stsp }
3893 886cec17 2019-12-15 stsp } else {
3894 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3895 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3896 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3897 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3898 886cec17 2019-12-15 stsp ignorespath);
3899 886cec17 2019-12-15 stsp } else
3900 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3901 886cec17 2019-12-15 stsp }
3902 6841da00 2019-08-08 stsp
3903 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3904 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3905 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3906 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3907 6841da00 2019-08-08 stsp free(ignorespath);
3908 6841da00 2019-08-08 stsp return err;
3909 f8d1f275 2019-02-04 stsp }
3910 f8d1f275 2019-02-04 stsp
3911 f8d1f275 2019-02-04 stsp static const struct got_error *
3912 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3913 62da3196 2021-10-01 stsp int dirfd)
3914 f8d1f275 2019-02-04 stsp {
3915 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3916 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3917 f8d1f275 2019-02-04 stsp char *path = NULL;
3918 62da3196 2021-10-01 stsp
3919 62da3196 2021-10-01 stsp if (ignore != NULL)
3920 62da3196 2021-10-01 stsp *ignore = 0;
3921 f8d1f275 2019-02-04 stsp
3922 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
3923 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
3924 f6b8c3c2 2023-07-24 stsp if (err)
3925 f6b8c3c2 2023-07-24 stsp return err;
3926 f6b8c3c2 2023-07-24 stsp }
3927 0584f854 2019-04-06 stsp
3928 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3929 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3930 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3931 f8d1f275 2019-02-04 stsp } else {
3932 f8d1f275 2019-02-04 stsp path = de->d_name;
3933 f8d1f275 2019-02-04 stsp }
3934 f8d1f275 2019-02-04 stsp
3935 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3936 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3937 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3938 62da3196 2021-10-01 stsp *ignore = 1;
3939 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3940 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3941 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3942 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3943 f8d1f275 2019-02-04 stsp if (parent_path[0])
3944 f8d1f275 2019-02-04 stsp free(path);
3945 b72f483a 2019-02-05 stsp return err;
3946 f8d1f275 2019-02-04 stsp }
3947 f8d1f275 2019-02-04 stsp
3948 347d1d3e 2019-07-12 stsp static const struct got_error *
3949 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3950 3143d852 2020-06-25 stsp {
3951 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3952 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3953 3143d852 2020-06-25 stsp
3954 3143d852 2020-06-25 stsp if (a->no_ignores)
3955 3143d852 2020-06-25 stsp return NULL;
3956 3143d852 2020-06-25 stsp
3957 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3958 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3959 3143d852 2020-06-25 stsp if (err)
3960 3143d852 2020-06-25 stsp return err;
3961 3143d852 2020-06-25 stsp
3962 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3963 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3964 3143d852 2020-06-25 stsp
3965 3143d852 2020-06-25 stsp return err;
3966 3143d852 2020-06-25 stsp }
3967 3143d852 2020-06-25 stsp
3968 3143d852 2020-06-25 stsp static const struct got_error *
3969 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3970 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3971 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3972 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3973 abb4604f 2019-07-27 stsp {
3974 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3975 abb4604f 2019-07-27 stsp struct stat sb;
3976 ff56836b 2021-07-08 stsp
3977 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3978 abb4604f 2019-07-27 stsp if (ie)
3979 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3980 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3981 abb4604f 2019-07-27 stsp
3982 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3983 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3984 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3985 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3986 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3987 abb4604f 2019-07-27 stsp }
3988 abb4604f 2019-07-27 stsp
3989 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3990 916237f3 2022-02-11 stsp return NULL;
3991 916237f3 2022-02-11 stsp
3992 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3993 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3994 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3995 abb4604f 2019-07-27 stsp
3996 abb4604f 2019-07-27 stsp return NULL;
3997 3143d852 2020-06-25 stsp }
3998 3143d852 2020-06-25 stsp
3999 3143d852 2020-06-25 stsp static const struct got_error *
4000 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
4001 3143d852 2020-06-25 stsp const char *root_path, const char *path)
4002 3143d852 2020-06-25 stsp {
4003 3143d852 2020-06-25 stsp const struct got_error *err;
4004 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
4005 3143d852 2020-06-25 stsp
4006 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4007 3143d852 2020-06-25 stsp ".cvsignore");
4008 3143d852 2020-06-25 stsp if (err)
4009 3143d852 2020-06-25 stsp return err;
4010 3143d852 2020-06-25 stsp
4011 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
4012 3143d852 2020-06-25 stsp ".gitignore");
4013 3143d852 2020-06-25 stsp if (err)
4014 3143d852 2020-06-25 stsp return err;
4015 3143d852 2020-06-25 stsp
4016 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
4017 3143d852 2020-06-25 stsp if (err) {
4018 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
4019 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
4020 3143d852 2020-06-25 stsp return err;
4021 3143d852 2020-06-25 stsp }
4022 3143d852 2020-06-25 stsp for (;;) {
4023 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4024 3143d852 2020-06-25 stsp ".cvsignore");
4025 3143d852 2020-06-25 stsp if (err)
4026 3143d852 2020-06-25 stsp break;
4027 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
4028 3143d852 2020-06-25 stsp ".gitignore");
4029 3143d852 2020-06-25 stsp if (err)
4030 3143d852 2020-06-25 stsp break;
4031 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
4032 3143d852 2020-06-25 stsp if (err) {
4033 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
4034 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
4035 3143d852 2020-06-25 stsp break;
4036 3143d852 2020-06-25 stsp }
4037 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
4038 ff56836b 2021-07-08 stsp break;
4039 b737c85a 2020-06-26 stsp free(parent_path);
4040 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
4041 b737c85a 2020-06-26 stsp next_parent_path = NULL;
4042 3143d852 2020-06-25 stsp }
4043 3143d852 2020-06-25 stsp
4044 b737c85a 2020-06-26 stsp free(parent_path);
4045 b737c85a 2020-06-26 stsp free(next_parent_path);
4046 3143d852 2020-06-25 stsp return err;
4047 abb4604f 2019-07-27 stsp }
4048 31cf15ec 2023-04-12 stsp
4049 31cf15ec 2023-04-12 stsp struct find_missing_children_args {
4050 31cf15ec 2023-04-12 stsp const char *parent_path;
4051 31cf15ec 2023-04-12 stsp size_t parent_len;
4052 31cf15ec 2023-04-12 stsp struct got_pathlist_head *children;
4053 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb;
4054 31cf15ec 2023-04-12 stsp void *cancel_arg;
4055 31cf15ec 2023-04-12 stsp };
4056 31cf15ec 2023-04-12 stsp
4057 abb4604f 2019-07-27 stsp static const struct got_error *
4058 31cf15ec 2023-04-12 stsp find_missing_children(void *arg, struct got_fileindex_entry *ie)
4059 31cf15ec 2023-04-12 stsp {
4060 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
4061 31cf15ec 2023-04-12 stsp struct find_missing_children_args *a = arg;
4062 31cf15ec 2023-04-12 stsp
4063 31cf15ec 2023-04-12 stsp if (a->cancel_cb) {
4064 31cf15ec 2023-04-12 stsp err = a->cancel_cb(a->cancel_arg);
4065 31cf15ec 2023-04-12 stsp if (err)
4066 31cf15ec 2023-04-12 stsp return err;
4067 31cf15ec 2023-04-12 stsp }
4068 31cf15ec 2023-04-12 stsp
4069 31cf15ec 2023-04-12 stsp if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4070 31cf15ec 2023-04-12 stsp err = got_pathlist_append(a->children, ie->path, NULL);
4071 31cf15ec 2023-04-12 stsp
4072 31cf15ec 2023-04-12 stsp return err;
4073 31cf15ec 2023-04-12 stsp }
4074 31cf15ec 2023-04-12 stsp
4075 31cf15ec 2023-04-12 stsp static const struct got_error *
4076 31cf15ec 2023-04-12 stsp report_children(struct got_pathlist_head *children,
4077 31cf15ec 2023-04-12 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4078 31cf15ec 2023-04-12 stsp struct got_repository *repo, int is_root_dir, int report_unchanged,
4079 31cf15ec 2023-04-12 stsp struct got_pathlist_head *ignores, int no_ignores,
4080 31cf15ec 2023-04-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4081 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4082 31cf15ec 2023-04-12 stsp {
4083 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
4084 31cf15ec 2023-04-12 stsp struct got_pathlist_entry *pe;
4085 31cf15ec 2023-04-12 stsp char *ondisk_path = NULL;
4086 31cf15ec 2023-04-12 stsp
4087 31cf15ec 2023-04-12 stsp TAILQ_FOREACH(pe, children, entry) {
4088 31cf15ec 2023-04-12 stsp if (cancel_cb) {
4089 31cf15ec 2023-04-12 stsp err = cancel_cb(cancel_arg);
4090 31cf15ec 2023-04-12 stsp if (err)
4091 31cf15ec 2023-04-12 stsp break;
4092 31cf15ec 2023-04-12 stsp }
4093 31cf15ec 2023-04-12 stsp
4094 31cf15ec 2023-04-12 stsp if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4095 31cf15ec 2023-04-12 stsp !is_root_dir ? "/" : "", pe->path) == -1) {
4096 31cf15ec 2023-04-12 stsp err = got_error_from_errno("asprintf");
4097 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
4098 31cf15ec 2023-04-12 stsp break;
4099 31cf15ec 2023-04-12 stsp }
4100 31cf15ec 2023-04-12 stsp
4101 31cf15ec 2023-04-12 stsp err = report_single_file_status(pe->path, ondisk_path,
4102 31cf15ec 2023-04-12 stsp fileindex, status_cb, status_arg, repo, report_unchanged,
4103 31cf15ec 2023-04-12 stsp ignores, no_ignores);
4104 31cf15ec 2023-04-12 stsp if (err)
4105 31cf15ec 2023-04-12 stsp break;
4106 31cf15ec 2023-04-12 stsp
4107 31cf15ec 2023-04-12 stsp free(ondisk_path);
4108 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
4109 31cf15ec 2023-04-12 stsp }
4110 31cf15ec 2023-04-12 stsp
4111 31cf15ec 2023-04-12 stsp free(ondisk_path);
4112 31cf15ec 2023-04-12 stsp return err;
4113 31cf15ec 2023-04-12 stsp }
4114 31cf15ec 2023-04-12 stsp
4115 31cf15ec 2023-04-12 stsp static const struct got_error *
4116 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
4117 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4118 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4119 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4120 f2a9dc41 2019-12-13 tracey int report_unchanged)
4121 f8d1f275 2019-02-04 stsp {
4122 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
4123 6fc93f37 2019-12-13 stsp int fd = -1;
4124 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
4125 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
4126 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
4127 31cf15ec 2023-04-12 stsp struct got_pathlist_head ignores, missing_children;
4128 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
4129 3143d852 2020-06-25 stsp
4130 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
4131 31cf15ec 2023-04-12 stsp TAILQ_INIT(&missing_children);
4132 f8d1f275 2019-02-04 stsp
4133 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
4134 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
4135 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
4136 8dc303cc 2019-07-27 stsp
4137 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4138 a84c0d30 2022-03-12 stsp if (ie) {
4139 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
4140 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
4141 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
4142 a84c0d30 2022-03-12 stsp goto done;
4143 31cf15ec 2023-04-12 stsp } else {
4144 31cf15ec 2023-04-12 stsp struct find_missing_children_args fmca;
4145 31cf15ec 2023-04-12 stsp fmca.parent_path = path;
4146 31cf15ec 2023-04-12 stsp fmca.parent_len = strlen(path);
4147 31cf15ec 2023-04-12 stsp fmca.children = &missing_children;
4148 31cf15ec 2023-04-12 stsp fmca.cancel_cb = cancel_cb;
4149 31cf15ec 2023-04-12 stsp fmca.cancel_arg = cancel_arg;
4150 31cf15ec 2023-04-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
4151 31cf15ec 2023-04-12 stsp find_missing_children, &fmca);
4152 31cf15ec 2023-04-12 stsp if (err)
4153 31cf15ec 2023-04-12 stsp goto done;
4154 a84c0d30 2022-03-12 stsp }
4155 a84c0d30 2022-03-12 stsp
4156 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4157 6fc93f37 2019-12-13 stsp if (fd == -1) {
4158 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4159 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
4160 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4161 ff56836b 2021-07-08 stsp else {
4162 ff56836b 2021-07-08 stsp if (!no_ignores) {
4163 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4164 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4165 ff56836b 2021-07-08 stsp if (err)
4166 ff56836b 2021-07-08 stsp goto done;
4167 ff56836b 2021-07-08 stsp }
4168 31cf15ec 2023-04-12 stsp if (TAILQ_EMPTY(&missing_children)) {
4169 31cf15ec 2023-04-12 stsp err = report_single_file_status(path,
4170 31cf15ec 2023-04-12 stsp ondisk_path, fileindex,
4171 31cf15ec 2023-04-12 stsp status_cb, status_arg, repo,
4172 31cf15ec 2023-04-12 stsp report_unchanged, &ignores, no_ignores);
4173 31cf15ec 2023-04-12 stsp if (err)
4174 31cf15ec 2023-04-12 stsp goto done;
4175 31cf15ec 2023-04-12 stsp } else {
4176 31cf15ec 2023-04-12 stsp err = report_children(&missing_children,
4177 31cf15ec 2023-04-12 stsp worktree, fileindex, repo,
4178 31cf15ec 2023-04-12 stsp (path[0] == '\0'), report_unchanged,
4179 31cf15ec 2023-04-12 stsp &ignores, no_ignores,
4180 31cf15ec 2023-04-12 stsp status_cb, status_arg,
4181 31cf15ec 2023-04-12 stsp cancel_cb, cancel_arg);
4182 46108e23 2023-04-12 stsp if (err)
4183 46108e23 2023-04-12 stsp goto done;
4184 31cf15ec 2023-04-12 stsp }
4185 ff56836b 2021-07-08 stsp }
4186 abb4604f 2019-07-27 stsp } else {
4187 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4188 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4189 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4190 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4191 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4192 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4193 abb4604f 2019-07-27 stsp arg.status_path = path;
4194 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4195 abb4604f 2019-07-27 stsp arg.repo = repo;
4196 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4197 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4198 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4199 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4200 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4201 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4202 022fae89 2019-12-06 tracey if (!no_ignores) {
4203 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4204 3143d852 2020-06-25 stsp worktree->root_path, path);
4205 3143d852 2020-06-25 stsp if (err)
4206 3143d852 2020-06-25 stsp goto done;
4207 022fae89 2019-12-06 tracey }
4208 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4209 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4210 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4211 927df6b7 2019-02-10 stsp }
4212 3143d852 2020-06-25 stsp done:
4213 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4214 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4215 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4216 927df6b7 2019-02-10 stsp free(ondisk_path);
4217 347d1d3e 2019-07-12 stsp return err;
4218 347d1d3e 2019-07-12 stsp }
4219 347d1d3e 2019-07-12 stsp
4220 347d1d3e 2019-07-12 stsp const struct got_error *
4221 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4222 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4223 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4224 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4225 347d1d3e 2019-07-12 stsp {
4226 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4227 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4228 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4229 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4230 347d1d3e 2019-07-12 stsp
4231 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4232 347d1d3e 2019-07-12 stsp if (err)
4233 347d1d3e 2019-07-12 stsp return err;
4234 347d1d3e 2019-07-12 stsp
4235 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4236 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4237 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4238 f6343036 2021-06-22 stsp no_ignores, 0);
4239 72ea6654 2019-07-27 stsp if (err)
4240 72ea6654 2019-07-27 stsp break;
4241 72ea6654 2019-07-27 stsp }
4242 f8d1f275 2019-02-04 stsp free(fileindex_path);
4243 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4244 f8d1f275 2019-02-04 stsp return err;
4245 f8d1f275 2019-02-04 stsp }
4246 6c7ab921 2019-03-18 stsp
4247 6c7ab921 2019-03-18 stsp const struct got_error *
4248 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4249 6c7ab921 2019-03-18 stsp const char *arg)
4250 6c7ab921 2019-03-18 stsp {
4251 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4252 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4253 6c7ab921 2019-03-18 stsp size_t len;
4254 00bb5ea0 2020-07-23 stsp struct stat sb;
4255 01740607 2020-11-04 stsp char *abspath = NULL;
4256 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4257 6c7ab921 2019-03-18 stsp
4258 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4259 6c7ab921 2019-03-18 stsp
4260 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4261 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4262 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4263 00bb5ea0 2020-07-23 stsp
4264 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4265 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4266 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4267 00bb5ea0 2020-07-23 stsp goto done;
4268 00bb5ea0 2020-07-23 stsp }
4269 727173c3 2020-11-06 stsp sb.st_mode = 0;
4270 00bb5ea0 2020-07-23 stsp }
4271 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4272 00bb5ea0 2020-07-23 stsp /*
4273 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4274 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4275 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4276 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4277 00bb5ea0 2020-07-23 stsp */
4278 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4279 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4280 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4281 00bb5ea0 2020-07-23 stsp goto done;
4282 00bb5ea0 2020-07-23 stsp }
4283 00bb5ea0 2020-07-23 stsp
4284 00bb5ea0 2020-07-23 stsp }
4285 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4286 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4287 00bb5ea0 2020-07-23 stsp if (err)
4288 d0710d08 2019-07-22 stsp goto done;
4289 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4290 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4291 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4292 00bb5ea0 2020-07-23 stsp goto done;
4293 00bb5ea0 2020-07-23 stsp }
4294 00bb5ea0 2020-07-23 stsp } else {
4295 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4296 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4297 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4298 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4299 00bb5ea0 2020-07-23 stsp goto done;
4300 00bb5ea0 2020-07-23 stsp }
4301 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4302 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4303 01740607 2020-11-04 stsp goto done;
4304 01740607 2020-11-04 stsp }
4305 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4306 01740607 2020-11-04 stsp sizeof(canonpath));
4307 01740607 2020-11-04 stsp if (err)
4308 00bb5ea0 2020-07-23 stsp goto done;
4309 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4310 01740607 2020-11-04 stsp if (resolved == NULL) {
4311 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4312 01740607 2020-11-04 stsp goto done;
4313 00bb5ea0 2020-07-23 stsp }
4314 d0710d08 2019-07-22 stsp }
4315 d0710d08 2019-07-22 stsp }
4316 6c7ab921 2019-03-18 stsp
4317 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4318 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4319 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4320 6c7ab921 2019-03-18 stsp goto done;
4321 6c7ab921 2019-03-18 stsp }
4322 6c7ab921 2019-03-18 stsp
4323 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4324 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4325 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4326 f6d88e1a 2019-05-29 stsp if (err)
4327 f6d88e1a 2019-05-29 stsp goto done;
4328 f6d88e1a 2019-05-29 stsp } else {
4329 f6d88e1a 2019-05-29 stsp path = strdup("");
4330 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4331 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4332 f6d88e1a 2019-05-29 stsp goto done;
4333 f6d88e1a 2019-05-29 stsp }
4334 6c7ab921 2019-03-18 stsp }
4335 6c7ab921 2019-03-18 stsp
4336 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4337 6c7ab921 2019-03-18 stsp len = strlen(path);
4338 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4339 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4340 6c7ab921 2019-03-18 stsp len--;
4341 6c7ab921 2019-03-18 stsp }
4342 6c7ab921 2019-03-18 stsp done:
4343 01740607 2020-11-04 stsp free(abspath);
4344 6c7ab921 2019-03-18 stsp free(resolved);
4345 d0710d08 2019-07-22 stsp free(cwd);
4346 6c7ab921 2019-03-18 stsp if (err == NULL)
4347 6c7ab921 2019-03-18 stsp *wt_path = path;
4348 6c7ab921 2019-03-18 stsp else
4349 6c7ab921 2019-03-18 stsp free(path);
4350 d00136be 2019-03-26 stsp return err;
4351 d00136be 2019-03-26 stsp }
4352 d00136be 2019-03-26 stsp
4353 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4354 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4355 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4356 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4357 4e68cba3 2019-11-23 stsp void *progress_arg;
4358 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4359 4e68cba3 2019-11-23 stsp };
4360 4e68cba3 2019-11-23 stsp
4361 b88936d3 2023-06-21 stsp static int
4362 b88936d3 2023-06-21 stsp add_noop_status(unsigned char status)
4363 b88936d3 2023-06-21 stsp {
4364 b88936d3 2023-06-21 stsp return (status == GOT_STATUS_ADD ||
4365 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODIFY ||
4366 b88936d3 2023-06-21 stsp status == GOT_STATUS_CONFLICT ||
4367 b88936d3 2023-06-21 stsp status == GOT_STATUS_MODE_CHANGE ||
4368 b88936d3 2023-06-21 stsp status == GOT_STATUS_NO_CHANGE);
4369 b88936d3 2023-06-21 stsp }
4370 b88936d3 2023-06-21 stsp
4371 4e68cba3 2019-11-23 stsp static const struct got_error *
4372 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4373 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4374 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4375 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4376 07f5b47a 2019-06-02 stsp {
4377 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4378 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4379 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4380 6d022e97 2019-08-04 stsp struct stat sb;
4381 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4382 07f5b47a 2019-06-02 stsp
4383 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4384 dbb83fbd 2019-12-12 stsp relpath) == -1)
4385 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4386 dbb83fbd 2019-12-12 stsp
4387 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4388 6d022e97 2019-08-04 stsp if (ie) {
4389 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4390 12463d8b 2019-12-13 stsp de_name, a->repo);
4391 6d022e97 2019-08-04 stsp if (err)
4392 dbb83fbd 2019-12-12 stsp goto done;
4393 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4394 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE &&
4395 b88936d3 2023-06-21 stsp add_noop_status(status))
4396 dbb83fbd 2019-12-12 stsp goto done;
4397 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4398 dbb83fbd 2019-12-12 stsp if (err)
4399 dbb83fbd 2019-12-12 stsp goto done;
4400 6d022e97 2019-08-04 stsp }
4401 07f5b47a 2019-06-02 stsp
4402 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4403 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4404 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4405 9b4603c0 2022-01-31 stsp else
4406 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4407 dbb83fbd 2019-12-12 stsp goto done;
4408 4e68cba3 2019-11-23 stsp }
4409 07f5b47a 2019-06-02 stsp
4410 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4411 4e68cba3 2019-11-23 stsp if (err)
4412 4e68cba3 2019-11-23 stsp goto done;
4413 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4414 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4415 3969253a 2020-03-07 stsp if (err) {
4416 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4417 3969253a 2020-03-07 stsp goto done;
4418 3969253a 2020-03-07 stsp }
4419 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4420 07f5b47a 2019-06-02 stsp if (err) {
4421 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4422 4e68cba3 2019-11-23 stsp goto done;
4423 07f5b47a 2019-06-02 stsp }
4424 4e68cba3 2019-11-23 stsp done:
4425 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4426 dbb83fbd 2019-12-12 stsp if (err)
4427 dbb83fbd 2019-12-12 stsp return err;
4428 b88936d3 2023-06-21 stsp if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4429 dbb83fbd 2019-12-12 stsp return NULL;
4430 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4431 07f5b47a 2019-06-02 stsp }
4432 07f5b47a 2019-06-02 stsp
4433 d00136be 2019-03-26 stsp const struct got_error *
4434 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4435 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4436 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4437 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4438 d00136be 2019-03-26 stsp {
4439 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4440 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4441 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4442 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4443 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4444 d00136be 2019-03-26 stsp
4445 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4446 d00136be 2019-03-26 stsp if (err)
4447 d00136be 2019-03-26 stsp return err;
4448 d00136be 2019-03-26 stsp
4449 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4450 d00136be 2019-03-26 stsp if (err)
4451 d00136be 2019-03-26 stsp goto done;
4452 d00136be 2019-03-26 stsp
4453 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4454 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4455 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4456 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4457 4e68cba3 2019-11-23 stsp saa.repo = repo;
4458 4e68cba3 2019-11-23 stsp
4459 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4460 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4461 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4462 1dd54920 2019-05-11 stsp if (err)
4463 af12c6b9 2019-06-04 stsp break;
4464 1dd54920 2019-05-11 stsp }
4465 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4466 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4467 af12c6b9 2019-06-04 stsp err = sync_err;
4468 d00136be 2019-03-26 stsp done:
4469 fb399478 2019-07-12 stsp free(fileindex_path);
4470 d00136be 2019-03-26 stsp if (fileindex)
4471 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4472 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4473 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4474 d00136be 2019-03-26 stsp err = unlockerr;
4475 6c7ab921 2019-03-18 stsp return err;
4476 6c7ab921 2019-03-18 stsp }
4477 17ed4618 2019-06-02 stsp
4478 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4479 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4480 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4481 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4482 f2a9dc41 2019-12-13 tracey void *progress_arg;
4483 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4484 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4485 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4486 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4487 7f4e5320 2023-05-29 stsp const char *status_path;
4488 7f4e5320 2023-05-29 stsp size_t status_path_len;
4489 766841c2 2020-08-13 stsp const char *status_codes;
4490 f2a9dc41 2019-12-13 tracey };
4491 f2a9dc41 2019-12-13 tracey
4492 f2a9dc41 2019-12-13 tracey static const struct got_error *
4493 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4494 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4495 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4496 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4497 17ed4618 2019-06-02 stsp {
4498 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4499 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4500 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4501 17ed4618 2019-06-02 stsp struct stat sb;
4502 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4503 4e12cd97 2022-01-25 stsp
4504 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4505 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4506 4e12cd97 2022-01-25 stsp return NULL;
4507 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4508 4e12cd97 2022-01-25 stsp }
4509 17ed4618 2019-06-02 stsp
4510 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4511 17ed4618 2019-06-02 stsp if (ie == NULL)
4512 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4513 2ec1f75b 2019-03-26 stsp
4514 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4515 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4516 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4517 9acbc4fa 2019-08-03 stsp return NULL;
4518 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4519 9acbc4fa 2019-08-03 stsp }
4520 9acbc4fa 2019-08-03 stsp
4521 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4522 f2a9dc41 2019-12-13 tracey relpath) == -1)
4523 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4524 f2a9dc41 2019-12-13 tracey
4525 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4526 12463d8b 2019-12-13 stsp a->repo);
4527 17ed4618 2019-06-02 stsp if (err)
4528 f2a9dc41 2019-12-13 tracey goto done;
4529 17ed4618 2019-06-02 stsp
4530 766841c2 2020-08-13 stsp if (a->status_codes) {
4531 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4532 766841c2 2020-08-13 stsp int i;
4533 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4534 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4535 766841c2 2020-08-13 stsp break;
4536 766841c2 2020-08-13 stsp }
4537 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4538 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4539 766841c2 2020-08-13 stsp free(ondisk_path);
4540 766841c2 2020-08-13 stsp return NULL;
4541 766841c2 2020-08-13 stsp }
4542 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4543 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4544 766841c2 2020-08-13 stsp static char msg[64];
4545 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4546 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4547 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4548 766841c2 2020-08-13 stsp goto done;
4549 766841c2 2020-08-13 stsp }
4550 766841c2 2020-08-13 stsp }
4551 766841c2 2020-08-13 stsp
4552 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4553 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4554 f2a9dc41 2019-12-13 tracey goto done;
4555 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4556 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4557 f2a9dc41 2019-12-13 tracey goto done;
4558 f2a9dc41 2019-12-13 tracey }
4559 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4560 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4561 4e12cd97 2022-01-25 stsp goto done;
4562 4e12cd97 2022-01-25 stsp }
4563 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4564 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4565 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4566 f2a9dc41 2019-12-13 tracey goto done;
4567 f2a9dc41 2019-12-13 tracey }
4568 17ed4618 2019-06-02 stsp }
4569 17ed4618 2019-06-02 stsp
4570 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4571 20a2ad1c 2020-10-20 stsp size_t root_len;
4572 20a2ad1c 2020-10-20 stsp
4573 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4574 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4575 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4576 12463d8b 2019-12-13 stsp ondisk_path);
4577 12463d8b 2019-12-13 stsp goto done;
4578 12463d8b 2019-12-13 stsp }
4579 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4580 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4581 12463d8b 2019-12-13 stsp goto done;
4582 15341bfd 2020-03-05 tracey }
4583 15341bfd 2020-03-05 tracey
4584 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4585 20a2ad1c 2020-10-20 stsp do {
4586 20a2ad1c 2020-10-20 stsp char *parent;
4587 7f4e5320 2023-05-29 stsp
4588 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4589 20a2ad1c 2020-10-20 stsp if (err)
4590 2513f20a 2020-10-20 stsp goto done;
4591 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4592 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4593 7f4e5320 2023-05-29 stsp if (got_path_cmp(ondisk_path, a->status_path,
4594 7f4e5320 2023-05-29 stsp strlen(ondisk_path), a->status_path_len) != 0 &&
4595 7f4e5320 2023-05-29 stsp !got_path_is_child(ondisk_path, a->status_path,
4596 7f4e5320 2023-05-29 stsp a->status_path_len))
4597 7f4e5320 2023-05-29 stsp break;
4598 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4599 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4600 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4601 20a2ad1c 2020-10-20 stsp ondisk_path);
4602 15341bfd 2020-03-05 tracey break;
4603 15341bfd 2020-03-05 tracey }
4604 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4605 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4606 f2a9dc41 2019-12-13 tracey }
4607 17ed4618 2019-06-02 stsp
4608 f6635657 2023-08-25 stsp if (got_fileindex_entry_has_blob(ie))
4609 f6635657 2023-08-25 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4610 f6635657 2023-08-25 stsp else
4611 f6635657 2023-08-25 stsp got_fileindex_entry_remove(a->fileindex, ie);
4612 f2a9dc41 2019-12-13 tracey done:
4613 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4614 f2a9dc41 2019-12-13 tracey if (err)
4615 f2a9dc41 2019-12-13 tracey return err;
4616 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4617 f2a9dc41 2019-12-13 tracey return NULL;
4618 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4619 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4620 17ed4618 2019-06-02 stsp }
4621 17ed4618 2019-06-02 stsp
4622 2ec1f75b 2019-03-26 stsp const struct got_error *
4623 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4624 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4625 766841c2 2020-08-13 stsp const char *status_codes,
4626 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4627 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4628 2ec1f75b 2019-03-26 stsp {
4629 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4630 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4631 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4632 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4633 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4634 2ec1f75b 2019-03-26 stsp
4635 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4636 2ec1f75b 2019-03-26 stsp if (err)
4637 2ec1f75b 2019-03-26 stsp return err;
4638 2ec1f75b 2019-03-26 stsp
4639 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4640 2ec1f75b 2019-03-26 stsp if (err)
4641 2ec1f75b 2019-03-26 stsp goto done;
4642 f2a9dc41 2019-12-13 tracey
4643 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4644 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4645 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4646 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4647 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4648 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4649 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4650 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4651 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4652 2ec1f75b 2019-03-26 stsp
4653 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4654 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
4655 7f4e5320 2023-05-29 stsp
4656 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s%s%s",
4657 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree),
4658 7f4e5320 2023-05-29 stsp pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4659 7f4e5320 2023-05-29 stsp err = got_error_from_errno("asprintf");
4660 7f4e5320 2023-05-29 stsp goto done;
4661 7f4e5320 2023-05-29 stsp }
4662 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
4663 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
4664 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4665 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4666 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
4667 17ed4618 2019-06-02 stsp if (err)
4668 af12c6b9 2019-06-04 stsp break;
4669 2ec1f75b 2019-03-26 stsp }
4670 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4671 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4672 af12c6b9 2019-06-04 stsp err = sync_err;
4673 2ec1f75b 2019-03-26 stsp done:
4674 fb399478 2019-07-12 stsp free(fileindex_path);
4675 2ec1f75b 2019-03-26 stsp if (fileindex)
4676 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4677 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4678 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4679 2ec1f75b 2019-03-26 stsp err = unlockerr;
4680 33aa809d 2019-08-08 stsp return err;
4681 33aa809d 2019-08-08 stsp }
4682 33aa809d 2019-08-08 stsp
4683 33aa809d 2019-08-08 stsp static const struct got_error *
4684 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4685 33aa809d 2019-08-08 stsp {
4686 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4687 33aa809d 2019-08-08 stsp char *line = NULL;
4688 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4689 33aa809d 2019-08-08 stsp ssize_t linelen;
4690 33aa809d 2019-08-08 stsp
4691 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4692 33aa809d 2019-08-08 stsp if (linelen == -1) {
4693 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4694 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4695 33aa809d 2019-08-08 stsp goto done;
4696 33aa809d 2019-08-08 stsp }
4697 33aa809d 2019-08-08 stsp return NULL;
4698 33aa809d 2019-08-08 stsp }
4699 33aa809d 2019-08-08 stsp if (outfile) {
4700 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4701 33aa809d 2019-08-08 stsp if (n != linelen) {
4702 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4703 33aa809d 2019-08-08 stsp goto done;
4704 33aa809d 2019-08-08 stsp }
4705 33aa809d 2019-08-08 stsp }
4706 33aa809d 2019-08-08 stsp if (rejectfile) {
4707 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4708 33aa809d 2019-08-08 stsp if (n != linelen)
4709 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4710 33aa809d 2019-08-08 stsp }
4711 33aa809d 2019-08-08 stsp done:
4712 33aa809d 2019-08-08 stsp free(line);
4713 2ec1f75b 2019-03-26 stsp return err;
4714 2ec1f75b 2019-03-26 stsp }
4715 1f1abb7e 2019-08-08 stsp
4716 33aa809d 2019-08-08 stsp static const struct got_error *
4717 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4718 33aa809d 2019-08-08 stsp {
4719 33aa809d 2019-08-08 stsp char *line = NULL;
4720 33aa809d 2019-08-08 stsp size_t linesize = 0;
4721 33aa809d 2019-08-08 stsp ssize_t linelen;
4722 33aa809d 2019-08-08 stsp
4723 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4724 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4725 500467ff 2019-09-25 hiltjo if (ferror(f))
4726 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4727 500467ff 2019-09-25 hiltjo return NULL;
4728 500467ff 2019-09-25 hiltjo }
4729 33aa809d 2019-08-08 stsp free(line);
4730 33aa809d 2019-08-08 stsp return NULL;
4731 33aa809d 2019-08-08 stsp }
4732 33aa809d 2019-08-08 stsp
4733 33aa809d 2019-08-08 stsp static const struct got_error *
4734 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4735 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4736 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4737 abc59930 2021-09-05 naddy {
4738 33aa809d 2019-08-08 stsp const struct got_error *err;
4739 33aa809d 2019-08-08 stsp
4740 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4741 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4742 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4743 33aa809d 2019-08-08 stsp if (err)
4744 33aa809d 2019-08-08 stsp return err;
4745 33aa809d 2019-08-08 stsp (*line_cur1)++;
4746 33aa809d 2019-08-08 stsp }
4747 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4748 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4749 33aa809d 2019-08-08 stsp if (rejectfile)
4750 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4751 33aa809d 2019-08-08 stsp else
4752 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4753 33aa809d 2019-08-08 stsp if (err)
4754 33aa809d 2019-08-08 stsp return err;
4755 33aa809d 2019-08-08 stsp (*line_cur2)++;
4756 33aa809d 2019-08-08 stsp }
4757 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4758 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4759 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4760 33aa809d 2019-08-08 stsp if (err)
4761 33aa809d 2019-08-08 stsp return err;
4762 33aa809d 2019-08-08 stsp (*line_cur2)++;
4763 33aa809d 2019-08-08 stsp }
4764 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4765 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4766 33aa809d 2019-08-08 stsp if (rejectfile)
4767 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4768 33aa809d 2019-08-08 stsp else
4769 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4770 33aa809d 2019-08-08 stsp if (err)
4771 33aa809d 2019-08-08 stsp return err;
4772 33aa809d 2019-08-08 stsp (*line_cur1)++;
4773 33aa809d 2019-08-08 stsp }
4774 f1e81a05 2019-08-10 stsp
4775 f1e81a05 2019-08-10 stsp return NULL;
4776 f1e81a05 2019-08-10 stsp }
4777 f1e81a05 2019-08-10 stsp
4778 f1e81a05 2019-08-10 stsp static const struct got_error *
4779 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4780 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4781 f1e81a05 2019-08-10 stsp {
4782 f1e81a05 2019-08-10 stsp const struct got_error *err;
4783 f1e81a05 2019-08-10 stsp
4784 f1e81a05 2019-08-10 stsp if (outfile) {
4785 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4786 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4787 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4788 f1e81a05 2019-08-10 stsp if (err)
4789 f1e81a05 2019-08-10 stsp return err;
4790 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4791 f1e81a05 2019-08-10 stsp }
4792 f1e81a05 2019-08-10 stsp }
4793 f1e81a05 2019-08-10 stsp if (rejectfile) {
4794 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4795 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4796 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4797 f1e81a05 2019-08-10 stsp if (err)
4798 f1e81a05 2019-08-10 stsp return err;
4799 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4800 f1e81a05 2019-08-10 stsp }
4801 33aa809d 2019-08-08 stsp }
4802 33aa809d 2019-08-08 stsp
4803 33aa809d 2019-08-08 stsp return NULL;
4804 33aa809d 2019-08-08 stsp }
4805 33aa809d 2019-08-08 stsp
4806 33aa809d 2019-08-08 stsp static const struct got_error *
4807 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4808 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4809 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4810 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4811 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4812 33aa809d 2019-08-08 stsp {
4813 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4814 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4815 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4816 33aa809d 2019-08-08 stsp FILE *hunkfile;
4817 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4818 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4819 fe621944 2020-11-10 stsp int rc;
4820 33aa809d 2019-08-08 stsp
4821 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4822 33aa809d 2019-08-08 stsp
4823 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4824 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4825 fe621944 2020-11-10 stsp start_old = cc.left.start;
4826 fe621944 2020-11-10 stsp end_old = cc.left.end;
4827 fe621944 2020-11-10 stsp start_new = cc.right.start;
4828 fe621944 2020-11-10 stsp end_new = cc.right.end;
4829 33aa809d 2019-08-08 stsp
4830 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4831 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4832 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4833 33aa809d 2019-08-08 stsp
4834 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4835 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4836 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4837 33aa809d 2019-08-08 stsp
4838 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4839 fe621944 2020-11-10 stsp if (diff_state == NULL)
4840 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4841 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4842 fe621944 2020-11-10 stsp
4843 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4844 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4845 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4846 33aa809d 2019-08-08 stsp goto done;
4847 33aa809d 2019-08-08 stsp }
4848 fe621944 2020-11-10 stsp
4849 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4850 fe621944 2020-11-10 stsp diff_result, &cc);
4851 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4852 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4853 33aa809d 2019-08-08 stsp goto done;
4854 33aa809d 2019-08-08 stsp }
4855 fe621944 2020-11-10 stsp
4856 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4857 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4858 33aa809d 2019-08-08 stsp goto done;
4859 33aa809d 2019-08-08 stsp }
4860 33aa809d 2019-08-08 stsp
4861 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4862 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4863 33aa809d 2019-08-08 stsp if (err)
4864 33aa809d 2019-08-08 stsp goto done;
4865 33aa809d 2019-08-08 stsp
4866 33aa809d 2019-08-08 stsp switch (*choice) {
4867 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4868 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4869 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4870 33aa809d 2019-08-08 stsp break;
4871 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4872 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4873 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4874 33aa809d 2019-08-08 stsp break;
4875 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4876 33aa809d 2019-08-08 stsp break;
4877 33aa809d 2019-08-08 stsp default:
4878 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4879 33aa809d 2019-08-08 stsp break;
4880 33aa809d 2019-08-08 stsp }
4881 33aa809d 2019-08-08 stsp done:
4882 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4883 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4884 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4885 33aa809d 2019-08-08 stsp return err;
4886 33aa809d 2019-08-08 stsp }
4887 33aa809d 2019-08-08 stsp
4888 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4889 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4890 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4891 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4892 1f1abb7e 2019-08-08 stsp void *progress_arg;
4893 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4894 33aa809d 2019-08-08 stsp void *patch_arg;
4895 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4896 f259c4c1 2021-09-24 stsp int unlink_added_files;
4897 af179be7 2023-04-14 stsp struct got_pathlist_head *added_files_to_unlink;
4898 1f1abb7e 2019-08-08 stsp };
4899 a129376b 2019-03-28 stsp
4900 e20a8b6f 2019-06-04 stsp static const struct got_error *
4901 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4902 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4903 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4904 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4905 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4906 33aa809d 2019-08-08 stsp {
4907 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4908 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4909 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4910 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4911 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4912 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4913 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4914 fe621944 2020-11-10 stsp struct stat sb2;
4915 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4916 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4917 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4918 33aa809d 2019-08-08 stsp
4919 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4920 33aa809d 2019-08-08 stsp
4921 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4922 33aa809d 2019-08-08 stsp if (err)
4923 33aa809d 2019-08-08 stsp return err;
4924 33aa809d 2019-08-08 stsp
4925 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4926 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4927 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4928 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4929 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4930 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4931 fa3cef63 2020-07-23 stsp goto done;
4932 fa3cef63 2020-07-23 stsp }
4933 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4934 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4935 c0df5966 2021-12-31 stsp if (link_len == -1) {
4936 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4937 c0df5966 2021-12-31 stsp path2);
4938 c0df5966 2021-12-31 stsp }
4939 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4940 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4941 12463d8b 2019-12-13 stsp }
4942 12463d8b 2019-12-13 stsp } else {
4943 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4944 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4945 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4946 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4947 fa3cef63 2020-07-23 stsp goto done;
4948 fa3cef63 2020-07-23 stsp }
4949 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4950 fa3cef63 2020-07-23 stsp sizeof(link_target));
4951 fa3cef63 2020-07-23 stsp if (link_len == -1)
4952 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4953 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4954 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4955 12463d8b 2019-12-13 stsp }
4956 1ebedb77 2019-10-19 stsp }
4957 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4958 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4959 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4960 fa3cef63 2020-07-23 stsp goto done;
4961 fa3cef63 2020-07-23 stsp }
4962 1ebedb77 2019-10-19 stsp
4963 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4964 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4965 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4966 fa3cef63 2020-07-23 stsp goto done;
4967 fa3cef63 2020-07-23 stsp }
4968 fa3cef63 2020-07-23 stsp fd2 = -1;
4969 fa3cef63 2020-07-23 stsp } else {
4970 fa3cef63 2020-07-23 stsp size_t n;
4971 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4972 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4973 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4974 fa3cef63 2020-07-23 stsp goto done;
4975 fa3cef63 2020-07-23 stsp }
4976 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4977 fa3cef63 2020-07-23 stsp if (n != link_len) {
4978 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4979 fa3cef63 2020-07-23 stsp goto done;
4980 fa3cef63 2020-07-23 stsp }
4981 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4982 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4983 fa3cef63 2020-07-23 stsp goto done;
4984 fa3cef63 2020-07-23 stsp }
4985 fa3cef63 2020-07-23 stsp rewind(f2);
4986 33aa809d 2019-08-08 stsp }
4987 33aa809d 2019-08-08 stsp
4988 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4989 eb81bc23 2022-06-28 tracey if (fd == -1) {
4990 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4991 eb81bc23 2022-06-28 tracey goto done;
4992 eb81bc23 2022-06-28 tracey }
4993 eb81bc23 2022-06-28 tracey
4994 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4995 33aa809d 2019-08-08 stsp if (err)
4996 33aa809d 2019-08-08 stsp goto done;
4997 33aa809d 2019-08-08 stsp
4998 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4999 33aa809d 2019-08-08 stsp if (err)
5000 33aa809d 2019-08-08 stsp goto done;
5001 33aa809d 2019-08-08 stsp
5002 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5003 33aa809d 2019-08-08 stsp if (err)
5004 33aa809d 2019-08-08 stsp goto done;
5005 33aa809d 2019-08-08 stsp
5006 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5007 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5008 33aa809d 2019-08-08 stsp if (err)
5009 33aa809d 2019-08-08 stsp goto done;
5010 33aa809d 2019-08-08 stsp
5011 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5012 b90054ed 2022-11-01 stsp "");
5013 33aa809d 2019-08-08 stsp if (err)
5014 33aa809d 2019-08-08 stsp goto done;
5015 33aa809d 2019-08-08 stsp
5016 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
5017 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
5018 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
5019 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
5020 fe621944 2020-11-10 stsp
5021 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
5022 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5023 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
5024 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
5025 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
5026 fe621944 2020-11-10 stsp nchanges++;
5027 fe621944 2020-11-10 stsp }
5028 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5029 33aa809d 2019-08-08 stsp int choice;
5030 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
5031 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
5032 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
5033 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
5034 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
5035 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
5036 33aa809d 2019-08-08 stsp if (err)
5037 33aa809d 2019-08-08 stsp goto done;
5038 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
5039 33aa809d 2019-08-08 stsp have_content = 1;
5040 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
5041 33aa809d 2019-08-08 stsp break;
5042 33aa809d 2019-08-08 stsp }
5043 1ebedb77 2019-10-19 stsp if (have_content) {
5044 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5045 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
5046 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
5047 1ebedb77 2019-10-19 stsp if (err)
5048 1ebedb77 2019-10-19 stsp goto done;
5049 1ebedb77 2019-10-19 stsp
5050 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
5051 b2b3fce1 2022-10-29 op mode_t mode;
5052 b2b3fce1 2022-10-29 op
5053 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
5054 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
5055 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
5056 fa3cef63 2020-07-23 stsp goto done;
5057 fa3cef63 2020-07-23 stsp }
5058 1ebedb77 2019-10-19 stsp }
5059 1ebedb77 2019-10-19 stsp }
5060 33aa809d 2019-08-08 stsp done:
5061 33aa809d 2019-08-08 stsp free(id_str);
5062 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5063 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5064 33aa809d 2019-08-08 stsp if (blob)
5065 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
5066 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
5067 fe621944 2020-11-10 stsp if (err == NULL)
5068 fe621944 2020-11-10 stsp err = free_err;
5069 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
5070 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
5071 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
5072 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
5073 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5074 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
5075 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
5076 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
5077 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
5078 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
5079 33aa809d 2019-08-08 stsp if (err || !have_content) {
5080 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5081 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
5082 33aa809d 2019-08-08 stsp free(*path_outfile);
5083 33aa809d 2019-08-08 stsp *path_outfile = NULL;
5084 33aa809d 2019-08-08 stsp }
5085 33aa809d 2019-08-08 stsp free(path1);
5086 33aa809d 2019-08-08 stsp return err;
5087 33aa809d 2019-08-08 stsp }
5088 33aa809d 2019-08-08 stsp
5089 33aa809d 2019-08-08 stsp static const struct got_error *
5090 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
5091 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
5092 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5093 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5094 a129376b 2019-03-28 stsp {
5095 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
5096 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
5097 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
5098 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
5099 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
5100 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
5101 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
5102 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
5103 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
5104 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
5105 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
5106 eb81bc23 2022-06-28 tracey int fd = -1;
5107 a129376b 2019-03-28 stsp
5108 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
5109 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
5110 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
5111 d3bcc3d1 2019-08-08 stsp return NULL;
5112 3d69ad8d 2019-08-17 semarie
5113 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
5114 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
5115 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
5116 d3bcc3d1 2019-08-08 stsp
5117 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5118 65084dad 2019-08-08 stsp if (ie == NULL)
5119 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
5120 a129376b 2019-03-28 stsp
5121 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
5122 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
5123 a129376b 2019-03-28 stsp if (err) {
5124 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
5125 a129376b 2019-03-28 stsp goto done;
5126 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
5127 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
5128 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
5129 e20a8b6f 2019-06-04 stsp goto done;
5130 e20a8b6f 2019-06-04 stsp }
5131 a129376b 2019-03-28 stsp }
5132 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
5133 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
5134 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5135 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5136 a129376b 2019-03-28 stsp goto done;
5137 a129376b 2019-03-28 stsp }
5138 a129376b 2019-03-28 stsp } else {
5139 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
5140 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
5141 a129376b 2019-03-28 stsp if (tree_path == NULL) {
5142 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5143 a129376b 2019-03-28 stsp goto done;
5144 a129376b 2019-03-28 stsp }
5145 a129376b 2019-03-28 stsp } else {
5146 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
5147 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
5148 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5149 a129376b 2019-03-28 stsp goto done;
5150 a129376b 2019-03-28 stsp }
5151 a129376b 2019-03-28 stsp }
5152 a129376b 2019-03-28 stsp }
5153 a129376b 2019-03-28 stsp
5154 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
5155 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
5156 a44927cc 2022-04-07 stsp if (err)
5157 a44927cc 2022-04-07 stsp goto done;
5158 a44927cc 2022-04-07 stsp
5159 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5160 a9fa2909 2019-07-27 stsp if (err) {
5161 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5162 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
5163 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
5164 a9fa2909 2019-07-27 stsp goto done;
5165 a9fa2909 2019-07-27 stsp } else {
5166 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
5167 a9fa2909 2019-07-27 stsp if (err)
5168 a9fa2909 2019-07-27 stsp goto done;
5169 a9fa2909 2019-07-27 stsp
5170 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
5171 1233e6b6 2020-10-19 stsp if (err)
5172 a9fa2909 2019-07-27 stsp goto done;
5173 a9fa2909 2019-07-27 stsp
5174 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
5175 1233e6b6 2020-10-19 stsp free(te_name);
5176 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
5177 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
5178 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5179 a9fa2909 2019-07-27 stsp goto done;
5180 a9fa2909 2019-07-27 stsp }
5181 a129376b 2019-03-28 stsp }
5182 a129376b 2019-03-28 stsp
5183 a129376b 2019-03-28 stsp switch (status) {
5184 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
5185 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5186 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5187 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5188 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5189 33aa809d 2019-08-08 stsp if (err)
5190 33aa809d 2019-08-08 stsp goto done;
5191 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5192 33aa809d 2019-08-08 stsp break;
5193 33aa809d 2019-08-08 stsp }
5194 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5195 1f1abb7e 2019-08-08 stsp ie->path);
5196 1ee397ad 2019-07-12 stsp if (err)
5197 1ee397ad 2019-07-12 stsp goto done;
5198 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5199 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
5200 af179be7 2023-04-14 stsp int do_unlink = a->added_files_to_unlink ? 0 : 1;
5201 af179be7 2023-04-14 stsp
5202 af179be7 2023-04-14 stsp if (a->added_files_to_unlink) {
5203 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
5204 af179be7 2023-04-14 stsp
5205 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, a->added_files_to_unlink,
5206 af179be7 2023-04-14 stsp entry) {
5207 af179be7 2023-04-14 stsp if (got_path_cmp(pe->path, relpath,
5208 af179be7 2023-04-14 stsp pe->path_len, strlen(relpath)))
5209 af179be7 2023-04-14 stsp continue;
5210 af179be7 2023-04-14 stsp do_unlink = 1;
5211 af179be7 2023-04-14 stsp break;
5212 af179be7 2023-04-14 stsp }
5213 f259c4c1 2021-09-24 stsp }
5214 af179be7 2023-04-14 stsp
5215 af179be7 2023-04-14 stsp if (do_unlink) {
5216 af179be7 2023-04-14 stsp if (asprintf(&ondisk_path, "%s/%s",
5217 af179be7 2023-04-14 stsp got_worktree_get_root_path(a->worktree),
5218 af179be7 2023-04-14 stsp relpath) == -1) {
5219 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
5220 af179be7 2023-04-14 stsp goto done;
5221 af179be7 2023-04-14 stsp }
5222 af179be7 2023-04-14 stsp if (unlink(ondisk_path) == -1) {
5223 af179be7 2023-04-14 stsp err = got_error_from_errno2("unlink",
5224 af179be7 2023-04-14 stsp ondisk_path);
5225 af179be7 2023-04-14 stsp break;
5226 af179be7 2023-04-14 stsp }
5227 f259c4c1 2021-09-24 stsp }
5228 f259c4c1 2021-09-24 stsp }
5229 a129376b 2019-03-28 stsp break;
5230 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5231 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5232 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5233 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5234 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5235 33aa809d 2019-08-08 stsp if (err)
5236 33aa809d 2019-08-08 stsp goto done;
5237 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5238 33aa809d 2019-08-08 stsp break;
5239 33aa809d 2019-08-08 stsp }
5240 33aa809d 2019-08-08 stsp /* fall through */
5241 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5242 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5243 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5244 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5245 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5246 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5247 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
5248 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
5249 b4b2adf5 2023-02-09 op else
5250 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
5251 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5252 eb81bc23 2022-06-28 tracey if (fd == -1) {
5253 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5254 eb81bc23 2022-06-28 tracey goto done;
5255 eb81bc23 2022-06-28 tracey }
5256 eb81bc23 2022-06-28 tracey
5257 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5258 a129376b 2019-03-28 stsp if (err)
5259 65084dad 2019-08-08 stsp goto done;
5260 65084dad 2019-08-08 stsp
5261 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5262 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5263 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5264 a129376b 2019-03-28 stsp goto done;
5265 65084dad 2019-08-08 stsp }
5266 65084dad 2019-08-08 stsp
5267 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5268 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5269 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5270 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5271 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5272 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5273 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5274 33aa809d 2019-08-08 stsp break;
5275 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5276 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5277 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5278 369fd7e5 2020-07-23 stsp path_content);
5279 369fd7e5 2020-07-23 stsp break;
5280 369fd7e5 2020-07-23 stsp }
5281 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5282 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5283 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5284 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5285 369fd7e5 2020-07-23 stsp } else {
5286 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5287 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5288 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5289 369fd7e5 2020-07-23 stsp goto done;
5290 369fd7e5 2020-07-23 stsp }
5291 33aa809d 2019-08-08 stsp }
5292 33aa809d 2019-08-08 stsp } else {
5293 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5294 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5295 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5296 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5297 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5298 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5299 2e1fa222 2020-07-23 stsp } else {
5300 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5301 2e1fa222 2020-07-23 stsp ie->path,
5302 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5303 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5304 ef623445 2023-09-16 op 0, 1, 0, 0, NULL, a->repo,
5305 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5306 2e1fa222 2020-07-23 stsp }
5307 a129376b 2019-03-28 stsp if (err)
5308 a129376b 2019-03-28 stsp goto done;
5309 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5310 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5311 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5312 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5313 437adc9d 2020-12-10 yzhong blob->id.sha1,
5314 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5315 2e1fa222 2020-07-23 stsp if (err)
5316 2e1fa222 2020-07-23 stsp goto done;
5317 2e1fa222 2020-07-23 stsp }
5318 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5319 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5320 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5321 33aa809d 2019-08-08 stsp }
5322 a129376b 2019-03-28 stsp }
5323 a129376b 2019-03-28 stsp break;
5324 e20a8b6f 2019-06-04 stsp }
5325 a129376b 2019-03-28 stsp default:
5326 1f1abb7e 2019-08-08 stsp break;
5327 a129376b 2019-03-28 stsp }
5328 a129376b 2019-03-28 stsp done:
5329 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5330 33aa809d 2019-08-08 stsp free(path_content);
5331 e20a8b6f 2019-06-04 stsp free(parent_path);
5332 a129376b 2019-03-28 stsp free(tree_path);
5333 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5334 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5335 a129376b 2019-03-28 stsp if (blob)
5336 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5337 a129376b 2019-03-28 stsp if (tree)
5338 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5339 a129376b 2019-03-28 stsp free(tree_id);
5340 a44927cc 2022-04-07 stsp if (base_commit)
5341 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
5342 e20a8b6f 2019-06-04 stsp return err;
5343 e20a8b6f 2019-06-04 stsp }
5344 e20a8b6f 2019-06-04 stsp
5345 e20a8b6f 2019-06-04 stsp const struct got_error *
5346 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5347 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5348 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5349 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5350 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5351 e20a8b6f 2019-06-04 stsp {
5352 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5353 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5354 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5355 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5356 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5357 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5358 e20a8b6f 2019-06-04 stsp
5359 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5360 e20a8b6f 2019-06-04 stsp if (err)
5361 e20a8b6f 2019-06-04 stsp return err;
5362 e20a8b6f 2019-06-04 stsp
5363 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5364 e20a8b6f 2019-06-04 stsp if (err)
5365 e20a8b6f 2019-06-04 stsp goto done;
5366 e20a8b6f 2019-06-04 stsp
5367 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5368 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5369 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5370 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5371 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5372 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5373 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5374 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
5375 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5376 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5377 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5378 e20a8b6f 2019-06-04 stsp if (err)
5379 af12c6b9 2019-06-04 stsp break;
5380 e20a8b6f 2019-06-04 stsp }
5381 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5382 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5383 e20a8b6f 2019-06-04 stsp err = sync_err;
5384 af12c6b9 2019-06-04 stsp done:
5385 fb399478 2019-07-12 stsp free(fileindex_path);
5386 a129376b 2019-03-28 stsp if (fileindex)
5387 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5388 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5389 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5390 a129376b 2019-03-28 stsp err = unlockerr;
5391 c4296144 2019-05-09 stsp return err;
5392 c4296144 2019-05-09 stsp }
5393 c4296144 2019-05-09 stsp
5394 cf066bf8 2019-05-09 stsp static void
5395 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5396 cf066bf8 2019-05-09 stsp {
5397 24519714 2019-05-09 stsp free(ct->path);
5398 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5399 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5400 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5401 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5402 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5403 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5404 cf066bf8 2019-05-09 stsp free(ct);
5405 cf066bf8 2019-05-09 stsp }
5406 24519714 2019-05-09 stsp
5407 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5408 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5409 24519714 2019-05-09 stsp struct got_repository *repo;
5410 24519714 2019-05-09 stsp struct got_worktree *worktree;
5411 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5412 5f8a88c6 2019-08-03 stsp int have_staged_files;
5413 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5414 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5415 12383673 2023-02-18 mark int commit_conflicts;
5416 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5417 2a47b1e5 2022-11-01 stsp FILE *f1;
5418 2a47b1e5 2022-11-01 stsp FILE *f2;
5419 24519714 2019-05-09 stsp };
5420 2a47b1e5 2022-11-01 stsp
5421 2a47b1e5 2022-11-01 stsp /*
5422 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5423 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5424 2a47b1e5 2022-11-01 stsp */
5425 2a47b1e5 2022-11-01 stsp static const struct got_error *
5426 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5427 2a47b1e5 2022-11-01 stsp const char *abspath)
5428 2a47b1e5 2022-11-01 stsp {
5429 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5430 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5431 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5432 2a47b1e5 2022-11-01 stsp
5433 2a47b1e5 2022-11-01 stsp *fd = -1;
5434 2a47b1e5 2022-11-01 stsp
5435 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5436 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5437 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5438 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5439 2a47b1e5 2022-11-01 stsp } else {
5440 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5441 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5442 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5443 2a47b1e5 2022-11-01 stsp }
5444 2a47b1e5 2022-11-01 stsp
5445 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5446 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5447 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5448 2a47b1e5 2022-11-01 stsp
5449 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5450 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5451 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5452 2a47b1e5 2022-11-01 stsp goto done;
5453 2a47b1e5 2022-11-01 stsp }
5454 2a47b1e5 2022-11-01 stsp
5455 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5456 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5457 2a47b1e5 2022-11-01 stsp goto done;
5458 2a47b1e5 2022-11-01 stsp }
5459 2a47b1e5 2022-11-01 stsp done:
5460 2a47b1e5 2022-11-01 stsp if (err) {
5461 2a47b1e5 2022-11-01 stsp close(*fd);
5462 2a47b1e5 2022-11-01 stsp *fd = -1;
5463 2a47b1e5 2022-11-01 stsp }
5464 2a47b1e5 2022-11-01 stsp return err;
5465 2a47b1e5 2022-11-01 stsp }
5466 2a47b1e5 2022-11-01 stsp
5467 2a47b1e5 2022-11-01 stsp static const struct got_error *
5468 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5469 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5470 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5471 2a47b1e5 2022-11-01 stsp {
5472 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5473 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5474 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5475 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5476 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5477 2a47b1e5 2022-11-01 stsp struct stat sb;
5478 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5479 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5480 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5481 2a47b1e5 2022-11-01 stsp
5482 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5483 4ba5cca9 2022-11-01 stsp
5484 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5485 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5486 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5487 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5488 4ba5cca9 2022-11-01 stsp return NULL;
5489 4ba5cca9 2022-11-01 stsp } else {
5490 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5491 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5492 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5493 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5494 4ba5cca9 2022-11-01 stsp return NULL;
5495 4ba5cca9 2022-11-01 stsp }
5496 2a47b1e5 2022-11-01 stsp
5497 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5498 2a47b1e5 2022-11-01 stsp if (err)
5499 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5500 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5501 2a47b1e5 2022-11-01 stsp if (err)
5502 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5503 2a47b1e5 2022-11-01 stsp
5504 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5505 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5506 2a47b1e5 2022-11-01 stsp if (err)
5507 2a47b1e5 2022-11-01 stsp return err;
5508 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5509 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5510 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5511 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5512 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5513 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5514 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5515 2a47b1e5 2022-11-01 stsp }
5516 2a47b1e5 2022-11-01 stsp
5517 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5518 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5519 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5520 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5521 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5522 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5523 2a47b1e5 2022-11-01 stsp break;
5524 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5525 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5526 2a47b1e5 2022-11-01 stsp break;
5527 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5528 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5529 2a47b1e5 2022-11-01 stsp break;
5530 2a47b1e5 2022-11-01 stsp default:
5531 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5532 2a47b1e5 2022-11-01 stsp }
5533 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5534 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5535 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5536 2a47b1e5 2022-11-01 stsp goto done;
5537 2a47b1e5 2022-11-01 stsp }
5538 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5539 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5540 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5541 2a47b1e5 2022-11-01 stsp goto done;
5542 2a47b1e5 2022-11-01 stsp }
5543 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5544 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5545 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5546 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5547 2a47b1e5 2022-11-01 stsp goto done;
5548 2a47b1e5 2022-11-01 stsp }
5549 2a47b1e5 2022-11-01 stsp
5550 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5551 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5552 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5553 2a47b1e5 2022-11-01 stsp goto done;
5554 2a47b1e5 2022-11-01 stsp }
5555 2a47b1e5 2022-11-01 stsp
5556 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5557 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5558 2a47b1e5 2022-11-01 stsp 8192, fd1);
5559 2a47b1e5 2022-11-01 stsp if (err)
5560 2a47b1e5 2022-11-01 stsp goto done;
5561 2a47b1e5 2022-11-01 stsp }
5562 2a47b1e5 2022-11-01 stsp
5563 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5564 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5565 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5566 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5567 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5568 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5569 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5570 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5571 2a47b1e5 2022-11-01 stsp goto done;
5572 2a47b1e5 2022-11-01 stsp }
5573 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5574 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5575 2a47b1e5 2022-11-01 stsp if (err)
5576 2a47b1e5 2022-11-01 stsp goto done;
5577 2a47b1e5 2022-11-01 stsp }
5578 2a47b1e5 2022-11-01 stsp } else {
5579 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5580 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5581 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5582 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5583 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5584 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5585 2a47b1e5 2022-11-01 stsp goto done;
5586 2a47b1e5 2022-11-01 stsp }
5587 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5588 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5589 2a47b1e5 2022-11-01 stsp if (err)
5590 2a47b1e5 2022-11-01 stsp goto done;
5591 2a47b1e5 2022-11-01 stsp }
5592 2a47b1e5 2022-11-01 stsp }
5593 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5594 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5595 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5596 2a47b1e5 2022-11-01 stsp goto done;
5597 2a47b1e5 2022-11-01 stsp }
5598 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5599 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5600 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5601 2a47b1e5 2022-11-01 stsp goto done;
5602 2a47b1e5 2022-11-01 stsp }
5603 2a47b1e5 2022-11-01 stsp fd = -1;
5604 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5605 2a47b1e5 2022-11-01 stsp }
5606 2a47b1e5 2022-11-01 stsp
5607 2a47b1e5 2022-11-01 stsp if (blob1) {
5608 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5609 2a47b1e5 2022-11-01 stsp f1, blob1);
5610 2a47b1e5 2022-11-01 stsp if (err)
5611 2a47b1e5 2022-11-01 stsp goto done;
5612 2a47b1e5 2022-11-01 stsp }
5613 2a47b1e5 2022-11-01 stsp
5614 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5615 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5616 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5617 2a47b1e5 2022-11-01 stsp done:
5618 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5619 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5620 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5621 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5622 2a47b1e5 2022-11-01 stsp if (blob1)
5623 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5624 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5625 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5626 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5627 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5628 2a47b1e5 2022-11-01 stsp return err;
5629 2a47b1e5 2022-11-01 stsp }
5630 cf066bf8 2019-05-09 stsp
5631 c4296144 2019-05-09 stsp static const struct got_error *
5632 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5633 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5634 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5635 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5636 c4296144 2019-05-09 stsp {
5637 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5638 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5639 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5640 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5641 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5642 768aea60 2019-05-09 stsp struct stat sb;
5643 c4296144 2019-05-09 stsp
5644 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5645 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5646 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5647 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5648 5f8a88c6 2019-08-03 stsp return NULL;
5649 5f8a88c6 2019-08-03 stsp } else {
5650 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5651 12383673 2023-02-18 mark printf("C %s\n", relpath);
5652 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5653 12383673 2023-02-18 mark }
5654 c4296144 2019-05-09 stsp
5655 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5656 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5657 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5658 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5659 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5660 5f8a88c6 2019-08-03 stsp return NULL;
5661 5f8a88c6 2019-08-03 stsp }
5662 0b5cc0d6 2019-05-09 stsp
5663 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5664 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5665 036813ee 2019-05-09 stsp goto done;
5666 036813ee 2019-05-09 stsp }
5667 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5668 036813ee 2019-05-09 stsp parent_path = strdup("");
5669 69960a46 2019-05-09 stsp if (parent_path == NULL)
5670 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5671 69960a46 2019-05-09 stsp } else {
5672 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5673 69960a46 2019-05-09 stsp if (err)
5674 69960a46 2019-05-09 stsp return err;
5675 69960a46 2019-05-09 stsp }
5676 c4296144 2019-05-09 stsp
5677 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5678 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5679 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5680 c4296144 2019-05-09 stsp goto done;
5681 768aea60 2019-05-09 stsp }
5682 768aea60 2019-05-09 stsp
5683 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5684 768aea60 2019-05-09 stsp relpath) == -1) {
5685 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5686 768aea60 2019-05-09 stsp goto done;
5687 768aea60 2019-05-09 stsp }
5688 0aeb8099 2020-07-23 stsp
5689 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5690 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5691 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5692 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5693 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5694 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5695 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5696 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5697 0aeb8099 2020-07-23 stsp break;
5698 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5699 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5700 0aeb8099 2020-07-23 stsp break;
5701 0aeb8099 2020-07-23 stsp default:
5702 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5703 0aeb8099 2020-07-23 stsp goto done;
5704 0aeb8099 2020-07-23 stsp }
5705 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5706 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5707 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5708 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5709 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5710 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5711 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5712 12463d8b 2019-12-13 stsp ct->ondisk_path);
5713 12463d8b 2019-12-13 stsp goto done;
5714 12463d8b 2019-12-13 stsp }
5715 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5716 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5717 768aea60 2019-05-09 stsp goto done;
5718 768aea60 2019-05-09 stsp }
5719 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5720 c4296144 2019-05-09 stsp }
5721 c4296144 2019-05-09 stsp
5722 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5723 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5724 44d03001 2019-05-09 stsp relpath) == -1) {
5725 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5726 44d03001 2019-05-09 stsp goto done;
5727 44d03001 2019-05-09 stsp }
5728 44d03001 2019-05-09 stsp
5729 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5730 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5731 35213c7c 2020-07-23 stsp int is_bad_symlink;
5732 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5733 35213c7c 2020-07-23 stsp ssize_t target_len;
5734 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5735 35213c7c 2020-07-23 stsp sizeof(target_path));
5736 35213c7c 2020-07-23 stsp if (target_len == -1) {
5737 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5738 35213c7c 2020-07-23 stsp ct->ondisk_path);
5739 35213c7c 2020-07-23 stsp goto done;
5740 35213c7c 2020-07-23 stsp }
5741 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5742 df6221c7 2023-07-19 stsp target_len, ct->ondisk_path, a->worktree->root_path,
5743 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
5744 35213c7c 2020-07-23 stsp if (err)
5745 35213c7c 2020-07-23 stsp goto done;
5746 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5747 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5748 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5749 35213c7c 2020-07-23 stsp goto done;
5750 35213c7c 2020-07-23 stsp }
5751 35213c7c 2020-07-23 stsp }
5752 35213c7c 2020-07-23 stsp
5753 35213c7c 2020-07-23 stsp
5754 cf066bf8 2019-05-09 stsp ct->status = status;
5755 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5756 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5757 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5758 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5759 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5760 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5761 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5762 b416585c 2019-05-13 stsp goto done;
5763 b416585c 2019-05-13 stsp }
5764 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5765 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5766 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5767 f0b75401 2019-08-03 stsp goto done;
5768 f0b75401 2019-08-03 stsp }
5769 f0b75401 2019-08-03 stsp }
5770 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5771 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5772 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5773 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5774 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5775 036813ee 2019-05-09 stsp goto done;
5776 036813ee 2019-05-09 stsp }
5777 ca2503ea 2019-05-09 stsp }
5778 24519714 2019-05-09 stsp ct->path = strdup(path);
5779 24519714 2019-05-09 stsp if (ct->path == NULL) {
5780 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5781 24519714 2019-05-09 stsp goto done;
5782 24519714 2019-05-09 stsp }
5783 2a47b1e5 2022-11-01 stsp
5784 c78dbc03 2023-08-25 stsp if (a->diff_outfile) {
5785 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5786 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5787 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5788 2a47b1e5 2022-11-01 stsp if (err)
5789 2a47b1e5 2022-11-01 stsp goto done;
5790 2a47b1e5 2022-11-01 stsp }
5791 c78dbc03 2023-08-25 stsp
5792 c78dbc03 2023-08-25 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5793 c4296144 2019-05-09 stsp done:
5794 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5795 ed175427 2019-05-09 stsp free_commitable(ct);
5796 24519714 2019-05-09 stsp free(parent_path);
5797 036813ee 2019-05-09 stsp free(path);
5798 a129376b 2019-03-28 stsp return err;
5799 a129376b 2019-03-28 stsp }
5800 c4296144 2019-05-09 stsp
5801 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5802 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5803 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5804 036813ee 2019-05-09 stsp struct got_repository *);
5805 ed175427 2019-05-09 stsp
5806 ed175427 2019-05-09 stsp static const struct got_error *
5807 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5808 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5809 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5810 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5811 afa376bf 2019-05-09 stsp struct got_repository *repo)
5812 ed175427 2019-05-09 stsp {
5813 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5814 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5815 036813ee 2019-05-09 stsp char *subpath;
5816 ed175427 2019-05-09 stsp
5817 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5818 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5819 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5820 ed175427 2019-05-09 stsp
5821 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5822 ed175427 2019-05-09 stsp if (err)
5823 ed175427 2019-05-09 stsp return err;
5824 ed175427 2019-05-09 stsp
5825 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5826 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5827 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5828 036813ee 2019-05-09 stsp free(subpath);
5829 036813ee 2019-05-09 stsp return err;
5830 036813ee 2019-05-09 stsp }
5831 ed175427 2019-05-09 stsp
5832 036813ee 2019-05-09 stsp static const struct got_error *
5833 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5834 036813ee 2019-05-09 stsp {
5835 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5836 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5837 ed175427 2019-05-09 stsp
5838 036813ee 2019-05-09 stsp *match = 0;
5839 ed175427 2019-05-09 stsp
5840 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5841 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5842 0f63689d 2019-05-10 stsp return NULL;
5843 036813ee 2019-05-09 stsp }
5844 0b5cc0d6 2019-05-09 stsp
5845 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5846 0f63689d 2019-05-10 stsp if (err)
5847 0f63689d 2019-05-10 stsp return err;
5848 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5849 036813ee 2019-05-09 stsp free(ct_parent_path);
5850 036813ee 2019-05-09 stsp return err;
5851 036813ee 2019-05-09 stsp }
5852 0b5cc0d6 2019-05-09 stsp
5853 768aea60 2019-05-09 stsp static mode_t
5854 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5855 768aea60 2019-05-09 stsp {
5856 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5857 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5858 3d9a4ec4 2020-07-23 stsp
5859 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5860 768aea60 2019-05-09 stsp }
5861 768aea60 2019-05-09 stsp
5862 036813ee 2019-05-09 stsp static const struct got_error *
5863 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5864 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5865 036813ee 2019-05-09 stsp {
5866 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5867 ca2503ea 2019-05-09 stsp
5868 036813ee 2019-05-09 stsp *new_te = NULL;
5869 0b5cc0d6 2019-05-09 stsp
5870 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5871 036813ee 2019-05-09 stsp if (err)
5872 036813ee 2019-05-09 stsp goto done;
5873 0b5cc0d6 2019-05-09 stsp
5874 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5875 036813ee 2019-05-09 stsp
5876 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5877 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5878 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5879 5f8a88c6 2019-08-03 stsp else
5880 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5881 036813ee 2019-05-09 stsp done:
5882 036813ee 2019-05-09 stsp if (err && *new_te) {
5883 56e0773d 2019-11-28 stsp free(*new_te);
5884 036813ee 2019-05-09 stsp *new_te = NULL;
5885 036813ee 2019-05-09 stsp }
5886 036813ee 2019-05-09 stsp return err;
5887 036813ee 2019-05-09 stsp }
5888 036813ee 2019-05-09 stsp
5889 036813ee 2019-05-09 stsp static const struct got_error *
5890 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5891 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5892 036813ee 2019-05-09 stsp {
5893 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5894 102b254e 2020-10-19 stsp char *ct_name = NULL;
5895 036813ee 2019-05-09 stsp
5896 036813ee 2019-05-09 stsp *new_te = NULL;
5897 036813ee 2019-05-09 stsp
5898 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5899 036813ee 2019-05-09 stsp if (*new_te == NULL)
5900 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5901 036813ee 2019-05-09 stsp
5902 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5903 102b254e 2020-10-19 stsp if (err)
5904 036813ee 2019-05-09 stsp goto done;
5905 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5906 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5907 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5908 036813ee 2019-05-09 stsp goto done;
5909 036813ee 2019-05-09 stsp }
5910 036813ee 2019-05-09 stsp
5911 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5912 036813ee 2019-05-09 stsp
5913 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5914 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5915 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5916 5f8a88c6 2019-08-03 stsp else
5917 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5918 036813ee 2019-05-09 stsp done:
5919 102b254e 2020-10-19 stsp free(ct_name);
5920 036813ee 2019-05-09 stsp if (err && *new_te) {
5921 56e0773d 2019-11-28 stsp free(*new_te);
5922 036813ee 2019-05-09 stsp *new_te = NULL;
5923 036813ee 2019-05-09 stsp }
5924 036813ee 2019-05-09 stsp return err;
5925 036813ee 2019-05-09 stsp }
5926 036813ee 2019-05-09 stsp
5927 036813ee 2019-05-09 stsp static const struct got_error *
5928 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5929 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5930 036813ee 2019-05-09 stsp {
5931 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5932 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5933 036813ee 2019-05-09 stsp
5934 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5935 036813ee 2019-05-09 stsp if (err)
5936 036813ee 2019-05-09 stsp return err;
5937 036813ee 2019-05-09 stsp if (new_pe == NULL)
5938 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5939 036813ee 2019-05-09 stsp return NULL;
5940 afa376bf 2019-05-09 stsp }
5941 afa376bf 2019-05-09 stsp
5942 afa376bf 2019-05-09 stsp static const struct got_error *
5943 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5944 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5945 afa376bf 2019-05-09 stsp {
5946 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5947 5f8a88c6 2019-08-03 stsp unsigned char status;
5948 0ff8d236 2021-09-28 stsp
5949 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5950 0ff8d236 2021-09-28 stsp return NULL;
5951 5f8a88c6 2019-08-03 stsp
5952 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5953 afa376bf 2019-05-09 stsp ct_path++;
5954 5f8a88c6 2019-08-03 stsp
5955 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5956 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5957 5f8a88c6 2019-08-03 stsp else
5958 5f8a88c6 2019-08-03 stsp status = ct->status;
5959 5f8a88c6 2019-08-03 stsp
5960 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5961 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5962 036813ee 2019-05-09 stsp }
5963 036813ee 2019-05-09 stsp
5964 036813ee 2019-05-09 stsp static const struct got_error *
5965 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5966 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5967 44d03001 2019-05-09 stsp {
5968 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5969 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5970 44d03001 2019-05-09 stsp char *te_path;
5971 44d03001 2019-05-09 stsp
5972 44d03001 2019-05-09 stsp *modified = 0;
5973 44d03001 2019-05-09 stsp
5974 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5975 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5976 44d03001 2019-05-09 stsp te->name) == -1)
5977 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5978 44d03001 2019-05-09 stsp
5979 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5980 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5981 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5982 44d03001 2019-05-09 stsp strlen(te_path));
5983 62d463ca 2020-10-20 naddy if (*modified)
5984 44d03001 2019-05-09 stsp break;
5985 44d03001 2019-05-09 stsp }
5986 44d03001 2019-05-09 stsp
5987 44d03001 2019-05-09 stsp free(te_path);
5988 44d03001 2019-05-09 stsp return err;
5989 44d03001 2019-05-09 stsp }
5990 44d03001 2019-05-09 stsp
5991 44d03001 2019-05-09 stsp static const struct got_error *
5992 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5993 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5994 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5995 036813ee 2019-05-09 stsp {
5996 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5997 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5998 036813ee 2019-05-09 stsp
5999 036813ee 2019-05-09 stsp *ctp = NULL;
6000 036813ee 2019-05-09 stsp
6001 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6002 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6003 036813ee 2019-05-09 stsp char *ct_name = NULL;
6004 036813ee 2019-05-09 stsp int path_matches;
6005 036813ee 2019-05-09 stsp
6006 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6007 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
6008 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
6009 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
6010 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6011 5f8a88c6 2019-08-03 stsp continue;
6012 5f8a88c6 2019-08-03 stsp } else {
6013 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
6014 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
6015 5f8a88c6 2019-08-03 stsp continue;
6016 5f8a88c6 2019-08-03 stsp }
6017 036813ee 2019-05-09 stsp
6018 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6019 036813ee 2019-05-09 stsp continue;
6020 036813ee 2019-05-09 stsp
6021 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6022 62d463ca 2020-10-20 naddy if (err)
6023 036813ee 2019-05-09 stsp return err;
6024 036813ee 2019-05-09 stsp if (!path_matches)
6025 036813ee 2019-05-09 stsp continue;
6026 036813ee 2019-05-09 stsp
6027 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
6028 d34b633e 2020-10-19 stsp if (err)
6029 d34b633e 2020-10-19 stsp return err;
6030 d34b633e 2020-10-19 stsp
6031 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
6032 d34b633e 2020-10-19 stsp free(ct_name);
6033 036813ee 2019-05-09 stsp continue;
6034 d34b633e 2020-10-19 stsp }
6035 d34b633e 2020-10-19 stsp free(ct_name);
6036 036813ee 2019-05-09 stsp
6037 036813ee 2019-05-09 stsp *ctp = ct;
6038 036813ee 2019-05-09 stsp break;
6039 036813ee 2019-05-09 stsp }
6040 2b496619 2019-07-10 stsp
6041 2b496619 2019-07-10 stsp return err;
6042 2b496619 2019-07-10 stsp }
6043 2b496619 2019-07-10 stsp
6044 2b496619 2019-07-10 stsp static const struct got_error *
6045 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6046 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
6047 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
6048 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
6049 2b496619 2019-07-10 stsp struct got_repository *repo)
6050 2b496619 2019-07-10 stsp {
6051 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
6052 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
6053 2b496619 2019-07-10 stsp char *subtree_path;
6054 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
6055 ba580f68 2020-03-22 stsp int nentries;
6056 2b496619 2019-07-10 stsp
6057 2b496619 2019-07-10 stsp *new_tep = NULL;
6058 2b496619 2019-07-10 stsp
6059 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6060 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
6061 2b496619 2019-07-10 stsp child_path) == -1)
6062 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
6063 036813ee 2019-05-09 stsp
6064 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
6065 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
6066 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
6067 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
6068 56e0773d 2019-11-28 stsp
6069 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6070 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
6071 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
6072 2b496619 2019-07-10 stsp goto done;
6073 2b496619 2019-07-10 stsp }
6074 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
6075 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
6076 2b496619 2019-07-10 stsp if (err) {
6077 56e0773d 2019-11-28 stsp free(new_te);
6078 2b496619 2019-07-10 stsp goto done;
6079 2b496619 2019-07-10 stsp }
6080 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
6081 2b496619 2019-07-10 stsp done:
6082 56e0773d 2019-11-28 stsp free(id);
6083 2b496619 2019-07-10 stsp free(subtree_path);
6084 2b496619 2019-07-10 stsp if (err == NULL)
6085 2b496619 2019-07-10 stsp *new_tep = new_te;
6086 036813ee 2019-05-09 stsp return err;
6087 036813ee 2019-05-09 stsp }
6088 036813ee 2019-05-09 stsp
6089 036813ee 2019-05-09 stsp static const struct got_error *
6090 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
6091 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
6092 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
6093 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6094 036813ee 2019-05-09 stsp struct got_repository *repo)
6095 036813ee 2019-05-09 stsp {
6096 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
6097 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
6098 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
6099 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
6100 036813ee 2019-05-09 stsp
6101 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
6102 ba580f68 2020-03-22 stsp *nentries = 0;
6103 036813ee 2019-05-09 stsp
6104 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
6105 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6106 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6107 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
6108 036813ee 2019-05-09 stsp
6109 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
6110 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
6111 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
6112 036813ee 2019-05-09 stsp continue;
6113 036813ee 2019-05-09 stsp
6114 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6115 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
6116 036813ee 2019-05-09 stsp continue;
6117 036813ee 2019-05-09 stsp
6118 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6119 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
6120 036813ee 2019-05-09 stsp if (err)
6121 036813ee 2019-05-09 stsp goto done;
6122 036813ee 2019-05-09 stsp
6123 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
6124 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
6125 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
6126 036813ee 2019-05-09 stsp if (err)
6127 036813ee 2019-05-09 stsp goto done;
6128 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
6129 afa376bf 2019-05-09 stsp if (err)
6130 afa376bf 2019-05-09 stsp goto done;
6131 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
6132 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6133 2b496619 2019-07-10 stsp if (err)
6134 2f51b5b3 2019-05-09 stsp goto done;
6135 ba580f68 2020-03-22 stsp (*nentries)++;
6136 2b496619 2019-07-10 stsp } else {
6137 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
6138 2b496619 2019-07-10 stsp if (base_tree == NULL ||
6139 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
6140 2b496619 2019-07-10 stsp == NULL) {
6141 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
6142 2b496619 2019-07-10 stsp child_path, path_base_tree,
6143 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
6144 2b496619 2019-07-10 stsp repo);
6145 2b496619 2019-07-10 stsp if (err)
6146 2b496619 2019-07-10 stsp goto done;
6147 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
6148 2b496619 2019-07-10 stsp if (err)
6149 2b496619 2019-07-10 stsp goto done;
6150 ba580f68 2020-03-22 stsp (*nentries)++;
6151 9ba0479c 2019-05-10 stsp }
6152 ed175427 2019-05-09 stsp }
6153 2f51b5b3 2019-05-09 stsp }
6154 2f51b5b3 2019-05-09 stsp
6155 2f51b5b3 2019-05-09 stsp if (base_tree) {
6156 56e0773d 2019-11-28 stsp int i, nbase_entries;
6157 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
6158 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
6159 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
6160 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
6161 63c5ca5d 2019-08-24 stsp
6162 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
6163 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
6164 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
6165 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
6166 63c5ca5d 2019-08-24 stsp if (err)
6167 63c5ca5d 2019-08-24 stsp goto done;
6168 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
6169 63c5ca5d 2019-08-24 stsp if (err)
6170 63c5ca5d 2019-08-24 stsp goto done;
6171 ba580f68 2020-03-22 stsp (*nentries)++;
6172 63c5ca5d 2019-08-24 stsp continue;
6173 63c5ca5d 2019-08-24 stsp }
6174 2f51b5b3 2019-05-09 stsp
6175 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
6176 44d03001 2019-05-09 stsp int modified;
6177 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6178 036813ee 2019-05-09 stsp if (err)
6179 036813ee 2019-05-09 stsp goto done;
6180 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
6181 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
6182 2f51b5b3 2019-05-09 stsp if (err)
6183 2f51b5b3 2019-05-09 stsp goto done;
6184 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
6185 44d03001 2019-05-09 stsp if (modified) {
6186 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
6187 ba580f68 2020-03-22 stsp int nsubentries;
6188 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
6189 ba580f68 2020-03-22 stsp &nsubentries, te,
6190 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
6191 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
6192 44d03001 2019-05-09 stsp if (err)
6193 44d03001 2019-05-09 stsp goto done;
6194 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6195 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6196 ba580f68 2020-03-22 stsp free(new_id);
6197 ba580f68 2020-03-22 stsp continue;
6198 ba580f68 2020-03-22 stsp }
6199 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6200 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6201 56e0773d 2019-11-28 stsp free(new_id);
6202 44d03001 2019-05-09 stsp }
6203 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6204 036813ee 2019-05-09 stsp if (err)
6205 036813ee 2019-05-09 stsp goto done;
6206 ba580f68 2020-03-22 stsp (*nentries)++;
6207 2f51b5b3 2019-05-09 stsp continue;
6208 036813ee 2019-05-09 stsp }
6209 2f51b5b3 2019-05-09 stsp
6210 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6211 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6212 f66c734c 2019-09-22 stsp if (err)
6213 f66c734c 2019-09-22 stsp goto done;
6214 2f51b5b3 2019-05-09 stsp if (ct) {
6215 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6216 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6217 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6218 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
6219 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6220 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6221 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6222 2f51b5b3 2019-05-09 stsp if (err)
6223 2f51b5b3 2019-05-09 stsp goto done;
6224 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6225 2f51b5b3 2019-05-09 stsp if (err)
6226 2f51b5b3 2019-05-09 stsp goto done;
6227 ba580f68 2020-03-22 stsp (*nentries)++;
6228 2f51b5b3 2019-05-09 stsp }
6229 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6230 b416585c 2019-05-13 stsp status_arg);
6231 afa376bf 2019-05-09 stsp if (err)
6232 afa376bf 2019-05-09 stsp goto done;
6233 2f51b5b3 2019-05-09 stsp } else {
6234 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6235 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6236 2f51b5b3 2019-05-09 stsp if (err)
6237 2f51b5b3 2019-05-09 stsp goto done;
6238 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6239 2f51b5b3 2019-05-09 stsp if (err)
6240 2f51b5b3 2019-05-09 stsp goto done;
6241 ba580f68 2020-03-22 stsp (*nentries)++;
6242 2f51b5b3 2019-05-09 stsp }
6243 ca2503ea 2019-05-09 stsp }
6244 ed175427 2019-05-09 stsp }
6245 0b5cc0d6 2019-05-09 stsp
6246 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6247 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6248 ed175427 2019-05-09 stsp done:
6249 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6250 2e1fa222 2020-07-23 stsp return err;
6251 2e1fa222 2020-07-23 stsp }
6252 2e1fa222 2020-07-23 stsp
6253 2e1fa222 2020-07-23 stsp static const struct got_error *
6254 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6255 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6256 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6257 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6258 ebf99748 2019-05-09 stsp {
6259 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6260 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6261 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6262 ebf99748 2019-05-09 stsp
6263 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6264 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6265 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6266 ebf99748 2019-05-09 stsp
6267 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6268 437adc9d 2020-12-10 yzhong
6269 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6270 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6271 437adc9d 2020-12-10 yzhong if (err)
6272 437adc9d 2020-12-10 yzhong goto done;
6273 437adc9d 2020-12-10 yzhong
6274 ebf99748 2019-05-09 stsp if (ie) {
6275 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6276 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6277 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6278 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6279 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6280 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6281 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6282 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6283 437adc9d 2020-12-10 yzhong
6284 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6285 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6286 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6287 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6288 72fd46fa 2019-09-06 stsp !have_staged_files);
6289 ebf99748 2019-05-09 stsp } else
6290 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6291 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6292 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6293 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6294 72fd46fa 2019-09-06 stsp !have_staged_files);
6295 ebf99748 2019-05-09 stsp } else {
6296 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6297 ebf99748 2019-05-09 stsp if (err)
6298 437adc9d 2020-12-10 yzhong goto done;
6299 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6300 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6301 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6302 3969253a 2020-03-07 stsp if (err) {
6303 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6304 437adc9d 2020-12-10 yzhong goto done;
6305 3969253a 2020-03-07 stsp }
6306 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6307 3969253a 2020-03-07 stsp if (err) {
6308 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6309 437adc9d 2020-12-10 yzhong goto done;
6310 3969253a 2020-03-07 stsp }
6311 ebf99748 2019-05-09 stsp }
6312 437adc9d 2020-12-10 yzhong free(relpath);
6313 437adc9d 2020-12-10 yzhong relpath = NULL;
6314 ebf99748 2019-05-09 stsp }
6315 437adc9d 2020-12-10 yzhong done:
6316 437adc9d 2020-12-10 yzhong free(relpath);
6317 d56d26ce 2019-05-10 stsp return err;
6318 d56d26ce 2019-05-10 stsp }
6319 735ef5ac 2019-08-03 stsp
6320 d56d26ce 2019-05-10 stsp
6321 d56d26ce 2019-05-10 stsp static const struct got_error *
6322 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6323 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6324 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6325 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6326 735ef5ac 2019-08-03 stsp int ood_errcode)
6327 d56d26ce 2019-05-10 stsp {
6328 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6329 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6330 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6331 1a36436d 2019-06-10 stsp
6332 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6333 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6334 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6335 1a36436d 2019-06-10 stsp return NULL;
6336 9bead371 2019-07-28 stsp /*
6337 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6338 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6339 9bead371 2019-07-28 stsp */
6340 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6341 a44927cc 2022-04-07 stsp if (err)
6342 a44927cc 2022-04-07 stsp goto done;
6343 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6344 9bead371 2019-07-28 stsp if (err) {
6345 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6346 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6347 1a36436d 2019-06-10 stsp goto done;
6348 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6349 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6350 1a36436d 2019-06-10 stsp } else {
6351 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6352 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6353 a44927cc 2022-04-07 stsp if (err)
6354 a44927cc 2022-04-07 stsp goto done;
6355 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6356 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6357 1a36436d 2019-06-10 stsp goto done;
6358 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6359 1a36436d 2019-06-10 stsp }
6360 1a36436d 2019-06-10 stsp done:
6361 1a36436d 2019-06-10 stsp free(id);
6362 a44927cc 2022-04-07 stsp if (commit)
6363 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6364 1a36436d 2019-06-10 stsp return err;
6365 ebf99748 2019-05-09 stsp }
6366 ebf99748 2019-05-09 stsp
6367 336075a4 2022-06-25 op static const struct got_error *
6368 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6369 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6370 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
6371 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
6372 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
6373 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
6374 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6375 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6376 afa376bf 2019-05-09 stsp struct got_repository *repo)
6377 c4296144 2019-05-09 stsp {
6378 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6379 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6380 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6381 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6382 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6383 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6384 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6385 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6386 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6387 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6388 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6389 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6390 f8399b8f 2022-07-22 stsp time_t timestamp;
6391 c4296144 2019-05-09 stsp
6392 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6393 c4296144 2019-05-09 stsp
6394 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6395 675c7539 2019-05-09 stsp
6396 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6397 588edf97 2019-05-10 stsp if (err)
6398 588edf97 2019-05-10 stsp goto done;
6399 de18fc63 2019-05-09 stsp
6400 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6401 588edf97 2019-05-10 stsp if (err)
6402 588edf97 2019-05-10 stsp goto done;
6403 588edf97 2019-05-10 stsp
6404 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6405 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6406 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6407 33ad4cbe 2019-05-12 jcs if (err)
6408 33ad4cbe 2019-05-12 jcs goto done;
6409 33ad4cbe 2019-05-12 jcs }
6410 c4296144 2019-05-09 stsp
6411 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6412 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6413 33ad4cbe 2019-05-12 jcs goto done;
6414 33ad4cbe 2019-05-12 jcs }
6415 33ad4cbe 2019-05-12 jcs
6416 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6417 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6418 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6419 cf066bf8 2019-05-09 stsp char *ondisk_path;
6420 cf066bf8 2019-05-09 stsp
6421 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6422 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6423 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6424 5f8a88c6 2019-08-03 stsp continue;
6425 5f8a88c6 2019-08-03 stsp
6426 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6427 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6428 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6429 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6430 cf066bf8 2019-05-09 stsp continue;
6431 cf066bf8 2019-05-09 stsp
6432 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6433 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6434 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6435 cf066bf8 2019-05-09 stsp goto done;
6436 cf066bf8 2019-05-09 stsp }
6437 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6438 2e1fa222 2020-07-23 stsp free(ondisk_path);
6439 2e1fa222 2020-07-23 stsp if (err)
6440 cf066bf8 2019-05-09 stsp goto done;
6441 cf066bf8 2019-05-09 stsp }
6442 036813ee 2019-05-09 stsp
6443 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6444 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6445 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6446 036813ee 2019-05-09 stsp if (err)
6447 036813ee 2019-05-09 stsp goto done;
6448 036813ee 2019-05-09 stsp
6449 2b706956 2023-04-17 stsp err = got_object_qid_alloc(&pid, head_commit_id);
6450 de18fc63 2019-05-09 stsp if (err)
6451 de18fc63 2019-05-09 stsp goto done;
6452 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6453 f259c4c1 2021-09-24 stsp nparents++;
6454 f259c4c1 2021-09-24 stsp if (parent_id2) {
6455 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6456 f259c4c1 2021-09-24 stsp if (err)
6457 f259c4c1 2021-09-24 stsp goto done;
6458 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6459 f259c4c1 2021-09-24 stsp nparents++;
6460 f259c4c1 2021-09-24 stsp }
6461 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6462 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6463 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6464 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6465 33ad4cbe 2019-05-12 jcs free(logmsg);
6466 9d40349a 2019-05-09 stsp if (err)
6467 9d40349a 2019-05-09 stsp goto done;
6468 9d40349a 2019-05-09 stsp
6469 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6470 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6471 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6472 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6473 09f5bd90 2019-05-09 stsp goto done;
6474 09f5bd90 2019-05-09 stsp }
6475 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6476 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6477 09f5bd90 2019-05-09 stsp if (err)
6478 09f5bd90 2019-05-09 stsp goto done;
6479 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6480 ebf99748 2019-05-09 stsp if (err)
6481 ebf99748 2019-05-09 stsp goto done;
6482 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6483 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6484 09f5bd90 2019-05-09 stsp goto done;
6485 09f5bd90 2019-05-09 stsp }
6486 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6487 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6488 f2c16586 2019-05-09 stsp if (err)
6489 f2c16586 2019-05-09 stsp goto done;
6490 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6491 f2c16586 2019-05-09 stsp if (err)
6492 f2c16586 2019-05-09 stsp goto done;
6493 f2c16586 2019-05-09 stsp
6494 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6495 09f5bd90 2019-05-09 stsp if (err)
6496 09f5bd90 2019-05-09 stsp goto done;
6497 09f5bd90 2019-05-09 stsp
6498 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6499 ebf99748 2019-05-09 stsp if (err)
6500 ebf99748 2019-05-09 stsp goto done;
6501 c4296144 2019-05-09 stsp done:
6502 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6503 588edf97 2019-05-10 stsp if (head_tree)
6504 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6505 588edf97 2019-05-10 stsp if (head_commit)
6506 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6507 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6508 2f17228e 2019-05-12 stsp if (head_ref2) {
6509 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6510 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6511 2f17228e 2019-05-12 stsp err = unlockerr;
6512 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6513 39cd0ff6 2019-07-12 stsp }
6514 39cd0ff6 2019-07-12 stsp return err;
6515 39cd0ff6 2019-07-12 stsp }
6516 5c1e53bc 2019-07-28 stsp
6517 5c1e53bc 2019-07-28 stsp static const struct got_error *
6518 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6519 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6520 5c1e53bc 2019-07-28 stsp {
6521 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6522 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6523 39cd0ff6 2019-07-12 stsp
6524 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6525 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6526 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6527 5c1e53bc 2019-07-28 stsp
6528 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6529 5c1e53bc 2019-07-28 stsp ct_path++;
6530 5c1e53bc 2019-07-28 stsp
6531 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6532 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6533 5c1e53bc 2019-07-28 stsp break;
6534 5c1e53bc 2019-07-28 stsp }
6535 5c1e53bc 2019-07-28 stsp
6536 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6537 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6538 5c1e53bc 2019-07-28 stsp
6539 5c1e53bc 2019-07-28 stsp return NULL;
6540 5c1e53bc 2019-07-28 stsp }
6541 5c1e53bc 2019-07-28 stsp
6542 f0b75401 2019-08-03 stsp static const struct got_error *
6543 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6544 f0b75401 2019-08-03 stsp {
6545 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6546 f0b75401 2019-08-03 stsp
6547 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6548 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6549 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6550 f0b75401 2019-08-03 stsp }
6551 f0b75401 2019-08-03 stsp
6552 f0b75401 2019-08-03 stsp return NULL;
6553 f0b75401 2019-08-03 stsp }
6554 f0b75401 2019-08-03 stsp
6555 5f8a88c6 2019-08-03 stsp static const struct got_error *
6556 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6557 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6558 5f8a88c6 2019-08-03 stsp {
6559 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6560 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6561 5f8a88c6 2019-08-03 stsp
6562 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6563 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6564 5f8a88c6 2019-08-03 stsp continue;
6565 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6566 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6567 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6568 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6569 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6570 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6571 5f8a88c6 2019-08-03 stsp }
6572 5f8a88c6 2019-08-03 stsp
6573 5f8a88c6 2019-08-03 stsp return NULL;
6574 5f8a88c6 2019-08-03 stsp }
6575 5f8a88c6 2019-08-03 stsp
6576 39cd0ff6 2019-07-12 stsp const struct got_error *
6577 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6578 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6579 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6580 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6581 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6582 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6583 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6584 39cd0ff6 2019-07-12 stsp {
6585 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6586 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6587 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6588 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6589 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6590 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6591 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6592 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6593 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6594 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6595 39cd0ff6 2019-07-12 stsp
6596 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6597 39cd0ff6 2019-07-12 stsp
6598 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6599 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6600 39cd0ff6 2019-07-12 stsp
6601 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6602 39cd0ff6 2019-07-12 stsp if (err)
6603 39cd0ff6 2019-07-12 stsp goto done;
6604 39cd0ff6 2019-07-12 stsp
6605 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6606 39cd0ff6 2019-07-12 stsp if (err)
6607 39cd0ff6 2019-07-12 stsp goto done;
6608 39cd0ff6 2019-07-12 stsp
6609 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6610 39cd0ff6 2019-07-12 stsp if (err)
6611 39cd0ff6 2019-07-12 stsp goto done;
6612 39cd0ff6 2019-07-12 stsp
6613 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6614 39cd0ff6 2019-07-12 stsp if (err)
6615 39cd0ff6 2019-07-12 stsp goto done;
6616 39cd0ff6 2019-07-12 stsp
6617 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6618 5f8a88c6 2019-08-03 stsp &have_staged_files);
6619 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6620 5f8a88c6 2019-08-03 stsp goto done;
6621 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6622 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6623 5f8a88c6 2019-08-03 stsp if (err)
6624 5f8a88c6 2019-08-03 stsp goto done;
6625 5f8a88c6 2019-08-03 stsp }
6626 5f8a88c6 2019-08-03 stsp
6627 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6628 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6629 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6630 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6631 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6632 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6633 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6634 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6635 2a47b1e5 2022-11-01 stsp if (show_diff) {
6636 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6637 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6638 2a47b1e5 2022-11-01 stsp if (err)
6639 2a47b1e5 2022-11-01 stsp goto done;
6640 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6641 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6642 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6643 2a47b1e5 2022-11-01 stsp goto done;
6644 2a47b1e5 2022-11-01 stsp }
6645 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6646 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6647 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6648 2a47b1e5 2022-11-01 stsp goto done;
6649 2a47b1e5 2022-11-01 stsp }
6650 2a47b1e5 2022-11-01 stsp }
6651 2a47b1e5 2022-11-01 stsp
6652 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6653 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6654 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6655 5c1e53bc 2019-07-28 stsp if (err)
6656 5c1e53bc 2019-07-28 stsp goto done;
6657 5c1e53bc 2019-07-28 stsp }
6658 39cd0ff6 2019-07-12 stsp
6659 2a47b1e5 2022-11-01 stsp if (show_diff) {
6660 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6661 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6662 2a47b1e5 2022-11-01 stsp goto done;
6663 2a47b1e5 2022-11-01 stsp }
6664 2a47b1e5 2022-11-01 stsp }
6665 2a47b1e5 2022-11-01 stsp
6666 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6667 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6668 39cd0ff6 2019-07-12 stsp goto done;
6669 5c1e53bc 2019-07-28 stsp }
6670 5c1e53bc 2019-07-28 stsp
6671 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6672 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6673 5c1e53bc 2019-07-28 stsp if (err)
6674 5c1e53bc 2019-07-28 stsp goto done;
6675 39cd0ff6 2019-07-12 stsp }
6676 f0b75401 2019-08-03 stsp
6677 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6678 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6679 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6680 f0b75401 2019-08-03 stsp
6681 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6682 f0b75401 2019-08-03 stsp ct_path++;
6683 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6684 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6685 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6686 b50cabdf 2019-07-12 stsp if (err)
6687 b50cabdf 2019-07-12 stsp goto done;
6688 f0b75401 2019-08-03 stsp
6689 b50cabdf 2019-07-12 stsp }
6690 b50cabdf 2019-07-12 stsp
6691 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6692 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6693 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6694 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6695 39cd0ff6 2019-07-12 stsp if (err)
6696 39cd0ff6 2019-07-12 stsp goto done;
6697 39cd0ff6 2019-07-12 stsp
6698 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6699 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6700 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6701 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6702 39cd0ff6 2019-07-12 stsp err = sync_err;
6703 39cd0ff6 2019-07-12 stsp done:
6704 39cd0ff6 2019-07-12 stsp if (fileindex)
6705 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6706 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6707 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6708 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6709 39cd0ff6 2019-07-12 stsp err = unlockerr;
6710 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6711 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6712 d8bacb93 2023-01-10 mark
6713 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6714 39cd0ff6 2019-07-12 stsp }
6715 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6716 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6717 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6718 2a47b1e5 2022-11-01 stsp free(diff_path);
6719 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6720 2a47b1e5 2022-11-01 stsp err == NULL)
6721 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6722 c4296144 2019-05-09 stsp return err;
6723 8656d6c4 2019-05-20 stsp }
6724 8656d6c4 2019-05-20 stsp
6725 8656d6c4 2019-05-20 stsp const char *
6726 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6727 8656d6c4 2019-05-20 stsp {
6728 8656d6c4 2019-05-20 stsp return ct->path;
6729 8656d6c4 2019-05-20 stsp }
6730 8656d6c4 2019-05-20 stsp
6731 8656d6c4 2019-05-20 stsp unsigned int
6732 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6733 8656d6c4 2019-05-20 stsp {
6734 8656d6c4 2019-05-20 stsp return ct->status;
6735 818c7501 2019-07-11 stsp }
6736 818c7501 2019-07-11 stsp
6737 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6738 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6739 818c7501 2019-07-11 stsp struct got_repository *repo;
6740 818c7501 2019-07-11 stsp };
6741 818c7501 2019-07-11 stsp
6742 818c7501 2019-07-11 stsp static const struct got_error *
6743 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6744 818c7501 2019-07-11 stsp {
6745 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6746 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6747 818c7501 2019-07-11 stsp unsigned char status;
6748 818c7501 2019-07-11 stsp struct stat sb;
6749 818c7501 2019-07-11 stsp char *ondisk_path;
6750 818c7501 2019-07-11 stsp
6751 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6752 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6753 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6754 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6755 818c7501 2019-07-11 stsp
6756 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6757 818c7501 2019-07-11 stsp == -1)
6758 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6759 818c7501 2019-07-11 stsp
6760 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6761 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6762 818c7501 2019-07-11 stsp free(ondisk_path);
6763 818c7501 2019-07-11 stsp if (err)
6764 818c7501 2019-07-11 stsp return err;
6765 818c7501 2019-07-11 stsp
6766 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6767 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6768 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6769 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6770 818c7501 2019-07-11 stsp
6771 818c7501 2019-07-11 stsp return NULL;
6772 818c7501 2019-07-11 stsp }
6773 818c7501 2019-07-11 stsp
6774 818c7501 2019-07-11 stsp const struct got_error *
6775 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6776 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6777 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6778 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6779 818c7501 2019-07-11 stsp {
6780 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6781 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6782 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6783 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6784 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6785 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6786 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6787 818c7501 2019-07-11 stsp
6788 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6789 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6790 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6791 818c7501 2019-07-11 stsp
6792 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6793 818c7501 2019-07-11 stsp if (err)
6794 818c7501 2019-07-11 stsp return err;
6795 818c7501 2019-07-11 stsp
6796 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6797 818c7501 2019-07-11 stsp if (err)
6798 818c7501 2019-07-11 stsp goto done;
6799 818c7501 2019-07-11 stsp
6800 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6801 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6802 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6803 818c7501 2019-07-11 stsp &ok_arg);
6804 818c7501 2019-07-11 stsp if (err)
6805 818c7501 2019-07-11 stsp goto done;
6806 818c7501 2019-07-11 stsp
6807 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6808 818c7501 2019-07-11 stsp if (err)
6809 818c7501 2019-07-11 stsp goto done;
6810 818c7501 2019-07-11 stsp
6811 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6812 818c7501 2019-07-11 stsp if (err)
6813 818c7501 2019-07-11 stsp goto done;
6814 818c7501 2019-07-11 stsp
6815 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6816 818c7501 2019-07-11 stsp if (err)
6817 818c7501 2019-07-11 stsp goto done;
6818 818c7501 2019-07-11 stsp
6819 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6820 818c7501 2019-07-11 stsp 0);
6821 e51d7b55 2020-01-04 stsp if (err)
6822 e51d7b55 2020-01-04 stsp goto done;
6823 e51d7b55 2020-01-04 stsp
6824 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6825 818c7501 2019-07-11 stsp if (err)
6826 818c7501 2019-07-11 stsp goto done;
6827 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6828 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6829 e51d7b55 2020-01-04 stsp goto done;
6830 e51d7b55 2020-01-04 stsp }
6831 818c7501 2019-07-11 stsp
6832 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6833 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6834 818c7501 2019-07-11 stsp if (err)
6835 818c7501 2019-07-11 stsp goto done;
6836 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6837 818c7501 2019-07-11 stsp if (err)
6838 818c7501 2019-07-11 stsp goto done;
6839 818c7501 2019-07-11 stsp
6840 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6841 818c7501 2019-07-11 stsp
6842 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6843 818c7501 2019-07-11 stsp if (err)
6844 818c7501 2019-07-11 stsp goto done;
6845 818c7501 2019-07-11 stsp
6846 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6847 818c7501 2019-07-11 stsp if (err)
6848 818c7501 2019-07-11 stsp goto done;
6849 818c7501 2019-07-11 stsp
6850 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6851 818c7501 2019-07-11 stsp worktree->base_commit_id);
6852 818c7501 2019-07-11 stsp if (err)
6853 818c7501 2019-07-11 stsp goto done;
6854 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6855 818c7501 2019-07-11 stsp if (err)
6856 818c7501 2019-07-11 stsp goto done;
6857 818c7501 2019-07-11 stsp
6858 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6859 818c7501 2019-07-11 stsp if (err)
6860 818c7501 2019-07-11 stsp goto done;
6861 818c7501 2019-07-11 stsp done:
6862 818c7501 2019-07-11 stsp free(fileindex_path);
6863 818c7501 2019-07-11 stsp free(tmp_branch_name);
6864 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6865 818c7501 2019-07-11 stsp free(branch_ref_name);
6866 818c7501 2019-07-11 stsp if (branch_ref)
6867 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6868 818c7501 2019-07-11 stsp if (wt_branch)
6869 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6870 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6871 818c7501 2019-07-11 stsp if (err) {
6872 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6873 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6874 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6875 818c7501 2019-07-11 stsp }
6876 818c7501 2019-07-11 stsp if (*tmp_branch) {
6877 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6878 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6879 818c7501 2019-07-11 stsp }
6880 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6881 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6882 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6883 3e3a69f1 2019-07-25 stsp }
6884 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6885 818c7501 2019-07-11 stsp }
6886 818c7501 2019-07-11 stsp return err;
6887 818c7501 2019-07-11 stsp }
6888 818c7501 2019-07-11 stsp
6889 818c7501 2019-07-11 stsp const struct got_error *
6890 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6891 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6892 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6893 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6894 818c7501 2019-07-11 stsp {
6895 818c7501 2019-07-11 stsp const struct got_error *err;
6896 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6897 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6898 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6899 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6900 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6901 818c7501 2019-07-11 stsp
6902 818c7501 2019-07-11 stsp *commit_id = NULL;
6903 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6904 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6905 3e3a69f1 2019-07-25 stsp *branch = NULL;
6906 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6907 3e3a69f1 2019-07-25 stsp
6908 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6909 3e3a69f1 2019-07-25 stsp if (err)
6910 3e3a69f1 2019-07-25 stsp return err;
6911 818c7501 2019-07-11 stsp
6912 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6913 3e3a69f1 2019-07-25 stsp if (err)
6914 f032f1f7 2019-08-04 stsp goto done;
6915 f032f1f7 2019-08-04 stsp
6916 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6917 f032f1f7 2019-08-04 stsp &have_staged_files);
6918 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6919 f032f1f7 2019-08-04 stsp goto done;
6920 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6921 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6922 3e3a69f1 2019-07-25 stsp goto done;
6923 f032f1f7 2019-08-04 stsp }
6924 3e3a69f1 2019-07-25 stsp
6925 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6926 818c7501 2019-07-11 stsp if (err)
6927 f032f1f7 2019-08-04 stsp goto done;
6928 818c7501 2019-07-11 stsp
6929 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6930 818c7501 2019-07-11 stsp if (err)
6931 818c7501 2019-07-11 stsp goto done;
6932 818c7501 2019-07-11 stsp
6933 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6934 818c7501 2019-07-11 stsp if (err)
6935 818c7501 2019-07-11 stsp goto done;
6936 818c7501 2019-07-11 stsp
6937 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6938 818c7501 2019-07-11 stsp if (err)
6939 818c7501 2019-07-11 stsp goto done;
6940 818c7501 2019-07-11 stsp
6941 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6942 818c7501 2019-07-11 stsp if (err)
6943 818c7501 2019-07-11 stsp goto done;
6944 818c7501 2019-07-11 stsp
6945 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6946 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6947 818c7501 2019-07-11 stsp if (err)
6948 818c7501 2019-07-11 stsp goto done;
6949 818c7501 2019-07-11 stsp
6950 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6951 818c7501 2019-07-11 stsp if (err)
6952 818c7501 2019-07-11 stsp goto done;
6953 818c7501 2019-07-11 stsp
6954 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6955 818c7501 2019-07-11 stsp if (err)
6956 818c7501 2019-07-11 stsp goto done;
6957 818c7501 2019-07-11 stsp
6958 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6959 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6960 818c7501 2019-07-11 stsp if (err)
6961 818c7501 2019-07-11 stsp goto done;
6962 818c7501 2019-07-11 stsp
6963 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6964 818c7501 2019-07-11 stsp if (err)
6965 818c7501 2019-07-11 stsp goto done;
6966 818c7501 2019-07-11 stsp done:
6967 818c7501 2019-07-11 stsp free(commit_ref_name);
6968 818c7501 2019-07-11 stsp free(branch_ref_name);
6969 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6970 818c7501 2019-07-11 stsp if (commit_ref)
6971 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6972 818c7501 2019-07-11 stsp if (branch_ref)
6973 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6974 818c7501 2019-07-11 stsp if (err) {
6975 818c7501 2019-07-11 stsp free(*commit_id);
6976 818c7501 2019-07-11 stsp *commit_id = NULL;
6977 818c7501 2019-07-11 stsp if (*tmp_branch) {
6978 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6979 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6980 818c7501 2019-07-11 stsp }
6981 818c7501 2019-07-11 stsp if (*new_base_branch) {
6982 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6983 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6984 818c7501 2019-07-11 stsp }
6985 818c7501 2019-07-11 stsp if (*branch) {
6986 818c7501 2019-07-11 stsp got_ref_close(*branch);
6987 818c7501 2019-07-11 stsp *branch = NULL;
6988 818c7501 2019-07-11 stsp }
6989 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6990 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6991 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6992 3e3a69f1 2019-07-25 stsp }
6993 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6994 818c7501 2019-07-11 stsp }
6995 818c7501 2019-07-11 stsp return err;
6996 c4296144 2019-05-09 stsp }
6997 818c7501 2019-07-11 stsp
6998 818c7501 2019-07-11 stsp const struct got_error *
6999 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7000 818c7501 2019-07-11 stsp {
7001 818c7501 2019-07-11 stsp const struct got_error *err;
7002 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
7003 818c7501 2019-07-11 stsp
7004 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7005 818c7501 2019-07-11 stsp if (err)
7006 818c7501 2019-07-11 stsp return err;
7007 818c7501 2019-07-11 stsp
7008 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7009 818c7501 2019-07-11 stsp free(tmp_branch_name);
7010 818c7501 2019-07-11 stsp return NULL;
7011 818c7501 2019-07-11 stsp }
7012 818c7501 2019-07-11 stsp
7013 818c7501 2019-07-11 stsp static const struct got_error *
7014 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7015 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7016 818c7501 2019-07-11 stsp {
7017 0ebf8283 2019-07-24 stsp *logmsg = arg;
7018 818c7501 2019-07-11 stsp return NULL;
7019 818c7501 2019-07-11 stsp }
7020 818c7501 2019-07-11 stsp
7021 818c7501 2019-07-11 stsp static const struct got_error *
7022 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7023 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7024 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7025 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7026 818c7501 2019-07-11 stsp {
7027 818c7501 2019-07-11 stsp return NULL;
7028 01757395 2019-07-12 stsp }
7029 01757395 2019-07-12 stsp
7030 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
7031 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
7032 01757395 2019-07-12 stsp void *progress_arg;
7033 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
7034 01757395 2019-07-12 stsp };
7035 01757395 2019-07-12 stsp
7036 01757395 2019-07-12 stsp static const struct got_error *
7037 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
7038 01757395 2019-07-12 stsp {
7039 01757395 2019-07-12 stsp const struct got_error *err;
7040 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
7041 01757395 2019-07-12 stsp char *p;
7042 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
7043 01757395 2019-07-12 stsp
7044 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
7045 01757395 2019-07-12 stsp if (err)
7046 01757395 2019-07-12 stsp return err;
7047 01757395 2019-07-12 stsp
7048 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
7049 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
7050 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
7051 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
7052 01757395 2019-07-12 stsp return NULL;
7053 01757395 2019-07-12 stsp
7054 01757395 2019-07-12 stsp p = strdup(path);
7055 01757395 2019-07-12 stsp if (p == NULL)
7056 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
7057 01757395 2019-07-12 stsp
7058 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7059 01757395 2019-07-12 stsp if (err || new == NULL)
7060 01757395 2019-07-12 stsp free(p);
7061 01757395 2019-07-12 stsp return err;
7062 818c7501 2019-07-11 stsp }
7063 818c7501 2019-07-11 stsp
7064 0ebf8283 2019-07-24 stsp static const struct got_error *
7065 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7066 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
7067 818c7501 2019-07-11 stsp {
7068 818c7501 2019-07-11 stsp const struct got_error *err;
7069 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
7070 818c7501 2019-07-11 stsp
7071 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7072 818c7501 2019-07-11 stsp if (err) {
7073 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
7074 818c7501 2019-07-11 stsp goto done;
7075 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7076 818c7501 2019-07-11 stsp if (err)
7077 818c7501 2019-07-11 stsp goto done;
7078 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
7079 818c7501 2019-07-11 stsp if (err)
7080 818c7501 2019-07-11 stsp goto done;
7081 de05890f 2020-03-05 stsp } else if (is_rebase) {
7082 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
7083 818c7501 2019-07-11 stsp int cmp;
7084 818c7501 2019-07-11 stsp
7085 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
7086 818c7501 2019-07-11 stsp if (err)
7087 818c7501 2019-07-11 stsp goto done;
7088 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
7089 818c7501 2019-07-11 stsp free(stored_id);
7090 818c7501 2019-07-11 stsp if (cmp != 0) {
7091 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7092 818c7501 2019-07-11 stsp goto done;
7093 818c7501 2019-07-11 stsp }
7094 818c7501 2019-07-11 stsp }
7095 0ebf8283 2019-07-24 stsp done:
7096 0ebf8283 2019-07-24 stsp if (commit_ref)
7097 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7098 0ebf8283 2019-07-24 stsp return err;
7099 0ebf8283 2019-07-24 stsp }
7100 0ebf8283 2019-07-24 stsp
7101 0ebf8283 2019-07-24 stsp static const struct got_error *
7102 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
7103 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
7104 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7105 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
7106 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7107 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7108 0ebf8283 2019-07-24 stsp {
7109 0ebf8283 2019-07-24 stsp const struct got_error *err;
7110 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7111 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
7112 3e3a69f1 2019-07-25 stsp char *fileindex_path;
7113 818c7501 2019-07-11 stsp
7114 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7115 0ebf8283 2019-07-24 stsp
7116 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7117 0ebf8283 2019-07-24 stsp if (err)
7118 0ebf8283 2019-07-24 stsp return err;
7119 0ebf8283 2019-07-24 stsp
7120 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
7121 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
7122 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
7123 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
7124 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
7125 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
7126 818c7501 2019-07-11 stsp if (commit_ref)
7127 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
7128 818c7501 2019-07-11 stsp return err;
7129 818c7501 2019-07-11 stsp }
7130 818c7501 2019-07-11 stsp
7131 818c7501 2019-07-11 stsp const struct got_error *
7132 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7133 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7134 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7135 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7136 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7137 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7138 818c7501 2019-07-11 stsp {
7139 0ebf8283 2019-07-24 stsp const struct got_error *err;
7140 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7141 0ebf8283 2019-07-24 stsp
7142 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7143 0ebf8283 2019-07-24 stsp if (err)
7144 0ebf8283 2019-07-24 stsp return err;
7145 0ebf8283 2019-07-24 stsp
7146 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7147 0ebf8283 2019-07-24 stsp if (err)
7148 0ebf8283 2019-07-24 stsp goto done;
7149 0ebf8283 2019-07-24 stsp
7150 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7151 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7152 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7153 0ebf8283 2019-07-24 stsp done:
7154 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7155 0ebf8283 2019-07-24 stsp return err;
7156 0ebf8283 2019-07-24 stsp }
7157 0ebf8283 2019-07-24 stsp
7158 0ebf8283 2019-07-24 stsp const struct got_error *
7159 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7160 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7161 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7162 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
7163 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7164 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7165 0ebf8283 2019-07-24 stsp {
7166 0ebf8283 2019-07-24 stsp const struct got_error *err;
7167 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7168 0ebf8283 2019-07-24 stsp
7169 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7170 0ebf8283 2019-07-24 stsp if (err)
7171 0ebf8283 2019-07-24 stsp return err;
7172 0ebf8283 2019-07-24 stsp
7173 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7174 0ebf8283 2019-07-24 stsp if (err)
7175 0ebf8283 2019-07-24 stsp goto done;
7176 0ebf8283 2019-07-24 stsp
7177 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7178 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
7179 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
7180 0ebf8283 2019-07-24 stsp done:
7181 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7182 0ebf8283 2019-07-24 stsp return err;
7183 0ebf8283 2019-07-24 stsp }
7184 0ebf8283 2019-07-24 stsp
7185 0ebf8283 2019-07-24 stsp static const struct got_error *
7186 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
7187 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7188 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7189 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
7190 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
7191 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7192 0ebf8283 2019-07-24 stsp {
7193 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7194 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7195 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7196 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7197 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7198 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7199 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7200 818c7501 2019-07-11 stsp
7201 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7202 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7203 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7204 a0e95631 2019-07-12 stsp
7205 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7206 818c7501 2019-07-11 stsp
7207 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7208 a0e95631 2019-07-12 stsp if (err)
7209 3e3a69f1 2019-07-25 stsp return err;
7210 a0e95631 2019-07-12 stsp
7211 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7212 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7213 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7214 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7215 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
7216 01757395 2019-07-12 stsp /*
7217 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7218 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7219 6c13b005 2021-09-02 stsp *
7220 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7221 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7222 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7223 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7224 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7225 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7226 01757395 2019-07-12 stsp */
7227 01757395 2019-07-12 stsp if (merged_paths) {
7228 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7229 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7230 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7231 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7232 f2a9dc41 2019-12-13 tracey 0);
7233 01757395 2019-07-12 stsp if (err)
7234 01757395 2019-07-12 stsp goto done;
7235 01757395 2019-07-12 stsp }
7236 01757395 2019-07-12 stsp } else {
7237 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7238 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7239 01757395 2019-07-12 stsp if (err)
7240 01757395 2019-07-12 stsp goto done;
7241 01757395 2019-07-12 stsp }
7242 a0e95631 2019-07-12 stsp
7243 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7244 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7245 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7246 a0e95631 2019-07-12 stsp if (err)
7247 a0e95631 2019-07-12 stsp goto done;
7248 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7249 a0e95631 2019-07-12 stsp goto done;
7250 ff0d2220 2019-07-11 stsp }
7251 818c7501 2019-07-11 stsp
7252 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7253 edd02c5e 2019-07-11 stsp if (err)
7254 edd02c5e 2019-07-11 stsp goto done;
7255 edd02c5e 2019-07-11 stsp
7256 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7257 818c7501 2019-07-11 stsp if (err)
7258 818c7501 2019-07-11 stsp goto done;
7259 818c7501 2019-07-11 stsp
7260 5943eee2 2019-08-13 stsp if (new_logmsg) {
7261 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7262 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7263 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7264 5943eee2 2019-08-13 stsp goto done;
7265 5943eee2 2019-08-13 stsp }
7266 5943eee2 2019-08-13 stsp } else {
7267 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7268 5943eee2 2019-08-13 stsp if (err)
7269 5943eee2 2019-08-13 stsp goto done;
7270 5943eee2 2019-08-13 stsp }
7271 0ebf8283 2019-07-24 stsp
7272 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7273 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7274 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
7275 50e7a649 2022-07-22 stsp committer ? committer :
7276 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
7277 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7278 a0e95631 2019-07-12 stsp if (err)
7279 a0e95631 2019-07-12 stsp goto done;
7280 a0e95631 2019-07-12 stsp
7281 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7282 a0e95631 2019-07-12 stsp if (err)
7283 a0e95631 2019-07-12 stsp goto done;
7284 a0e95631 2019-07-12 stsp
7285 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7286 a0e95631 2019-07-12 stsp if (err)
7287 a0e95631 2019-07-12 stsp goto done;
7288 a0e95631 2019-07-12 stsp
7289 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7290 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7291 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7292 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7293 a0e95631 2019-07-12 stsp err = sync_err;
7294 818c7501 2019-07-11 stsp done:
7295 a0e95631 2019-07-12 stsp free(fileindex_path);
7296 a0e95631 2019-07-12 stsp free(head_commit_id);
7297 a0e95631 2019-07-12 stsp if (head_ref)
7298 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7299 818c7501 2019-07-11 stsp if (err) {
7300 818c7501 2019-07-11 stsp free(*new_commit_id);
7301 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7302 818c7501 2019-07-11 stsp }
7303 818c7501 2019-07-11 stsp return err;
7304 818c7501 2019-07-11 stsp }
7305 818c7501 2019-07-11 stsp
7306 818c7501 2019-07-11 stsp const struct got_error *
7307 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7308 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7309 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7310 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7311 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
7312 12383673 2023-02-18 mark struct got_repository *repo)
7313 0ebf8283 2019-07-24 stsp {
7314 0ebf8283 2019-07-24 stsp const struct got_error *err;
7315 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7316 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7317 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7318 0ebf8283 2019-07-24 stsp
7319 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7320 0ebf8283 2019-07-24 stsp if (err)
7321 0ebf8283 2019-07-24 stsp return err;
7322 0ebf8283 2019-07-24 stsp
7323 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7324 0ebf8283 2019-07-24 stsp if (err)
7325 0ebf8283 2019-07-24 stsp goto done;
7326 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7327 0ebf8283 2019-07-24 stsp if (err)
7328 0ebf8283 2019-07-24 stsp goto done;
7329 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7330 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7331 0ebf8283 2019-07-24 stsp goto done;
7332 0ebf8283 2019-07-24 stsp }
7333 0ebf8283 2019-07-24 stsp
7334 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7335 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7336 12383673 2023-02-18 mark NULL, allow_conflict, repo);
7337 0ebf8283 2019-07-24 stsp done:
7338 0ebf8283 2019-07-24 stsp if (commit_ref)
7339 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7340 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7341 0ebf8283 2019-07-24 stsp free(commit_id);
7342 0ebf8283 2019-07-24 stsp return err;
7343 0ebf8283 2019-07-24 stsp }
7344 0ebf8283 2019-07-24 stsp
7345 0ebf8283 2019-07-24 stsp const struct got_error *
7346 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7347 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7348 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7349 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7350 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7351 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7352 0ebf8283 2019-07-24 stsp {
7353 0ebf8283 2019-07-24 stsp const struct got_error *err;
7354 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7355 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7356 0ebf8283 2019-07-24 stsp
7357 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7358 0ebf8283 2019-07-24 stsp if (err)
7359 0ebf8283 2019-07-24 stsp return err;
7360 0ebf8283 2019-07-24 stsp
7361 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7362 0ebf8283 2019-07-24 stsp if (err)
7363 0ebf8283 2019-07-24 stsp goto done;
7364 0ebf8283 2019-07-24 stsp
7365 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7366 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7367 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
7368 0ebf8283 2019-07-24 stsp done:
7369 0ebf8283 2019-07-24 stsp if (commit_ref)
7370 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7371 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7372 0ebf8283 2019-07-24 stsp return err;
7373 0ebf8283 2019-07-24 stsp }
7374 0ebf8283 2019-07-24 stsp
7375 0ebf8283 2019-07-24 stsp const struct got_error *
7376 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7377 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7378 818c7501 2019-07-11 stsp {
7379 3e3a69f1 2019-07-25 stsp if (fileindex)
7380 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7381 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7382 69844fba 2019-07-11 stsp }
7383 69844fba 2019-07-11 stsp
7384 69844fba 2019-07-11 stsp static const struct got_error *
7385 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7386 69844fba 2019-07-11 stsp {
7387 69844fba 2019-07-11 stsp const struct got_error *err;
7388 69844fba 2019-07-11 stsp struct got_reference *ref;
7389 69844fba 2019-07-11 stsp
7390 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7391 69844fba 2019-07-11 stsp if (err) {
7392 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7393 69844fba 2019-07-11 stsp return NULL;
7394 69844fba 2019-07-11 stsp return err;
7395 69844fba 2019-07-11 stsp }
7396 69844fba 2019-07-11 stsp
7397 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7398 69844fba 2019-07-11 stsp got_ref_close(ref);
7399 69844fba 2019-07-11 stsp return err;
7400 818c7501 2019-07-11 stsp }
7401 818c7501 2019-07-11 stsp
7402 69844fba 2019-07-11 stsp static const struct got_error *
7403 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7404 69844fba 2019-07-11 stsp {
7405 69844fba 2019-07-11 stsp const struct got_error *err;
7406 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7407 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7408 69844fba 2019-07-11 stsp
7409 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7410 69844fba 2019-07-11 stsp if (err)
7411 69844fba 2019-07-11 stsp goto done;
7412 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7413 69844fba 2019-07-11 stsp if (err)
7414 69844fba 2019-07-11 stsp goto done;
7415 69844fba 2019-07-11 stsp
7416 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7417 69844fba 2019-07-11 stsp if (err)
7418 69844fba 2019-07-11 stsp goto done;
7419 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7420 69844fba 2019-07-11 stsp if (err)
7421 69844fba 2019-07-11 stsp goto done;
7422 69844fba 2019-07-11 stsp
7423 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7424 69844fba 2019-07-11 stsp if (err)
7425 69844fba 2019-07-11 stsp goto done;
7426 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7427 69844fba 2019-07-11 stsp if (err)
7428 69844fba 2019-07-11 stsp goto done;
7429 69844fba 2019-07-11 stsp
7430 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7431 69844fba 2019-07-11 stsp if (err)
7432 69844fba 2019-07-11 stsp goto done;
7433 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7434 69844fba 2019-07-11 stsp if (err)
7435 69844fba 2019-07-11 stsp goto done;
7436 69844fba 2019-07-11 stsp
7437 69844fba 2019-07-11 stsp done:
7438 69844fba 2019-07-11 stsp free(tmp_branch_name);
7439 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7440 69844fba 2019-07-11 stsp free(branch_ref_name);
7441 69844fba 2019-07-11 stsp free(commit_ref_name);
7442 e600f124 2021-03-21 stsp return err;
7443 e600f124 2021-03-21 stsp }
7444 e600f124 2021-03-21 stsp
7445 336075a4 2022-06-25 op static const struct got_error *
7446 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7447 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7448 e600f124 2021-03-21 stsp {
7449 e600f124 2021-03-21 stsp const struct got_error *err;
7450 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7451 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7452 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7453 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7454 e600f124 2021-03-21 stsp char *refname = NULL;
7455 e600f124 2021-03-21 stsp
7456 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7457 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7458 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7459 e600f124 2021-03-21 stsp branch_name += 11;
7460 e600f124 2021-03-21 stsp
7461 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7462 e600f124 2021-03-21 stsp if (err)
7463 e600f124 2021-03-21 stsp return err;
7464 e600f124 2021-03-21 stsp
7465 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7466 e600f124 2021-03-21 stsp new_id_str) == -1) {
7467 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7468 e600f124 2021-03-21 stsp goto done;
7469 e600f124 2021-03-21 stsp }
7470 e600f124 2021-03-21 stsp
7471 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7472 e600f124 2021-03-21 stsp if (err)
7473 e600f124 2021-03-21 stsp goto done;
7474 e600f124 2021-03-21 stsp
7475 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7476 e600f124 2021-03-21 stsp if (err)
7477 e600f124 2021-03-21 stsp goto done;
7478 e600f124 2021-03-21 stsp
7479 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7480 e600f124 2021-03-21 stsp done:
7481 e600f124 2021-03-21 stsp free(new_id_str);
7482 e600f124 2021-03-21 stsp free(refname);
7483 e600f124 2021-03-21 stsp free(old_commit_id);
7484 e600f124 2021-03-21 stsp if (ref)
7485 e600f124 2021-03-21 stsp got_ref_close(ref);
7486 69844fba 2019-07-11 stsp return err;
7487 69844fba 2019-07-11 stsp }
7488 69844fba 2019-07-11 stsp
7489 818c7501 2019-07-11 stsp const struct got_error *
7490 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7491 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7492 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7493 ef85a376 2023-02-01 mark int create_backup)
7494 818c7501 2019-07-11 stsp {
7495 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7496 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7497 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7498 818c7501 2019-07-11 stsp
7499 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7500 818c7501 2019-07-11 stsp if (err)
7501 818c7501 2019-07-11 stsp return err;
7502 e600f124 2021-03-21 stsp
7503 e600f124 2021-03-21 stsp if (create_backup) {
7504 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7505 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7506 e600f124 2021-03-21 stsp if (err)
7507 e600f124 2021-03-21 stsp goto done;
7508 e600f124 2021-03-21 stsp }
7509 818c7501 2019-07-11 stsp
7510 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7511 818c7501 2019-07-11 stsp if (err)
7512 818c7501 2019-07-11 stsp goto done;
7513 818c7501 2019-07-11 stsp
7514 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7515 818c7501 2019-07-11 stsp if (err)
7516 818c7501 2019-07-11 stsp goto done;
7517 818c7501 2019-07-11 stsp
7518 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7519 818c7501 2019-07-11 stsp if (err)
7520 818c7501 2019-07-11 stsp goto done;
7521 818c7501 2019-07-11 stsp
7522 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7523 a615e0e7 2020-12-16 stsp if (err)
7524 a615e0e7 2020-12-16 stsp goto done;
7525 a615e0e7 2020-12-16 stsp
7526 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7527 a615e0e7 2020-12-16 stsp if (err)
7528 a615e0e7 2020-12-16 stsp goto done;
7529 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7530 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7531 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7532 a615e0e7 2020-12-16 stsp err = sync_err;
7533 818c7501 2019-07-11 stsp done:
7534 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7535 a615e0e7 2020-12-16 stsp free(fileindex_path);
7536 818c7501 2019-07-11 stsp free(new_head_commit_id);
7537 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7538 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7539 818c7501 2019-07-11 stsp err = unlockerr;
7540 818c7501 2019-07-11 stsp return err;
7541 818c7501 2019-07-11 stsp }
7542 af179be7 2023-04-14 stsp
7543 af179be7 2023-04-14 stsp static const struct got_error *
7544 af179be7 2023-04-14 stsp get_paths_changed_between_commits(struct got_pathlist_head *paths,
7545 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7546 af179be7 2023-04-14 stsp struct got_repository *repo)
7547 af179be7 2023-04-14 stsp {
7548 af179be7 2023-04-14 stsp const struct got_error *err;
7549 af179be7 2023-04-14 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7550 af179be7 2023-04-14 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7551 af179be7 2023-04-14 stsp
7552 af179be7 2023-04-14 stsp if (id1) {
7553 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit1, repo, id1);
7554 af179be7 2023-04-14 stsp if (err)
7555 af179be7 2023-04-14 stsp goto done;
7556 af179be7 2023-04-14 stsp
7557 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree1, repo,
7558 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit1));
7559 af179be7 2023-04-14 stsp if (err)
7560 af179be7 2023-04-14 stsp goto done;
7561 af179be7 2023-04-14 stsp }
7562 818c7501 2019-07-11 stsp
7563 af179be7 2023-04-14 stsp if (id2) {
7564 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit2, repo, id2);
7565 af179be7 2023-04-14 stsp if (err)
7566 af179be7 2023-04-14 stsp goto done;
7567 af179be7 2023-04-14 stsp
7568 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree2, repo,
7569 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit2));
7570 af179be7 2023-04-14 stsp if (err)
7571 af179be7 2023-04-14 stsp goto done;
7572 af179be7 2023-04-14 stsp }
7573 af179be7 2023-04-14 stsp
7574 af179be7 2023-04-14 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7575 af179be7 2023-04-14 stsp got_diff_tree_collect_changed_paths, paths, 0);
7576 af179be7 2023-04-14 stsp if (err)
7577 af179be7 2023-04-14 stsp goto done;
7578 af179be7 2023-04-14 stsp done:
7579 af179be7 2023-04-14 stsp if (commit1)
7580 af179be7 2023-04-14 stsp got_object_commit_close(commit1);
7581 af179be7 2023-04-14 stsp if (commit2)
7582 af179be7 2023-04-14 stsp got_object_commit_close(commit2);
7583 af179be7 2023-04-14 stsp if (tree1)
7584 af179be7 2023-04-14 stsp got_object_tree_close(tree1);
7585 af179be7 2023-04-14 stsp if (tree2)
7586 af179be7 2023-04-14 stsp got_object_tree_close(tree2);
7587 af179be7 2023-04-14 stsp return err;
7588 af179be7 2023-04-14 stsp }
7589 af179be7 2023-04-14 stsp
7590 af179be7 2023-04-14 stsp static const struct got_error *
7591 af179be7 2023-04-14 stsp get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7592 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7593 af179be7 2023-04-14 stsp const char *path_prefix, struct got_repository *repo)
7594 af179be7 2023-04-14 stsp {
7595 af179be7 2023-04-14 stsp const struct got_error *err;
7596 af179be7 2023-04-14 stsp struct got_pathlist_head merged_paths;
7597 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
7598 af179be7 2023-04-14 stsp char *abspath = NULL, *wt_path = NULL;
7599 af179be7 2023-04-14 stsp
7600 af179be7 2023-04-14 stsp TAILQ_INIT(&merged_paths);
7601 af179be7 2023-04-14 stsp
7602 af179be7 2023-04-14 stsp err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7603 af179be7 2023-04-14 stsp if (err)
7604 af179be7 2023-04-14 stsp goto done;
7605 af179be7 2023-04-14 stsp
7606 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, &merged_paths, entry) {
7607 af179be7 2023-04-14 stsp struct got_diff_changed_path *change = pe->data;
7608 af179be7 2023-04-14 stsp
7609 af179be7 2023-04-14 stsp if (change->status != GOT_STATUS_ADD)
7610 af179be7 2023-04-14 stsp continue;
7611 af179be7 2023-04-14 stsp
7612 af179be7 2023-04-14 stsp if (got_path_is_root_dir(path_prefix)) {
7613 af179be7 2023-04-14 stsp wt_path = strdup(pe->path);
7614 af179be7 2023-04-14 stsp if (wt_path == NULL) {
7615 af179be7 2023-04-14 stsp err = got_error_from_errno("strdup");
7616 af179be7 2023-04-14 stsp goto done;
7617 af179be7 2023-04-14 stsp }
7618 af179be7 2023-04-14 stsp } else {
7619 af179be7 2023-04-14 stsp if (asprintf(&abspath, "/%s", pe->path) == -1) {
7620 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
7621 af179be7 2023-04-14 stsp goto done;
7622 af179be7 2023-04-14 stsp }
7623 af179be7 2023-04-14 stsp
7624 af179be7 2023-04-14 stsp err = got_path_skip_common_ancestor(&wt_path,
7625 af179be7 2023-04-14 stsp path_prefix, abspath);
7626 af179be7 2023-04-14 stsp if (err)
7627 af179be7 2023-04-14 stsp goto done;
7628 af179be7 2023-04-14 stsp free(abspath);
7629 af179be7 2023-04-14 stsp abspath = NULL;
7630 af179be7 2023-04-14 stsp }
7631 af179be7 2023-04-14 stsp
7632 af179be7 2023-04-14 stsp err = got_pathlist_append(added_paths, wt_path, NULL);
7633 af179be7 2023-04-14 stsp if (err)
7634 af179be7 2023-04-14 stsp goto done;
7635 af179be7 2023-04-14 stsp wt_path = NULL;
7636 af179be7 2023-04-14 stsp }
7637 af179be7 2023-04-14 stsp
7638 af179be7 2023-04-14 stsp done:
7639 af179be7 2023-04-14 stsp got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7640 af179be7 2023-04-14 stsp free(abspath);
7641 af179be7 2023-04-14 stsp free(wt_path);
7642 af179be7 2023-04-14 stsp return err;
7643 af179be7 2023-04-14 stsp }
7644 af179be7 2023-04-14 stsp
7645 af179be7 2023-04-14 stsp static const struct got_error *
7646 af179be7 2023-04-14 stsp get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7647 af179be7 2023-04-14 stsp struct got_object_id *id, const char *path_prefix,
7648 af179be7 2023-04-14 stsp struct got_repository *repo)
7649 af179be7 2023-04-14 stsp {
7650 af179be7 2023-04-14 stsp const struct got_error *err;
7651 af179be7 2023-04-14 stsp struct got_commit_object *commit = NULL;
7652 af179be7 2023-04-14 stsp struct got_object_qid *pid;
7653 af179be7 2023-04-14 stsp
7654 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit, repo, id);
7655 af179be7 2023-04-14 stsp if (err)
7656 af179be7 2023-04-14 stsp goto done;
7657 af179be7 2023-04-14 stsp
7658 af179be7 2023-04-14 stsp pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7659 af179be7 2023-04-14 stsp
7660 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(added_paths,
7661 af179be7 2023-04-14 stsp pid ? &pid->id : NULL, id, path_prefix, repo);
7662 af179be7 2023-04-14 stsp if (err)
7663 af179be7 2023-04-14 stsp goto done;
7664 af179be7 2023-04-14 stsp done:
7665 af179be7 2023-04-14 stsp if (commit)
7666 af179be7 2023-04-14 stsp got_object_commit_close(commit);
7667 af179be7 2023-04-14 stsp return err;
7668 af179be7 2023-04-14 stsp }
7669 af179be7 2023-04-14 stsp
7670 818c7501 2019-07-11 stsp const struct got_error *
7671 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7672 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7673 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7674 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7675 818c7501 2019-07-11 stsp {
7676 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7677 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7678 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7679 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
7680 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7681 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7682 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
7683 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
7684 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7685 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7686 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
7687 818c7501 2019-07-11 stsp
7688 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
7689 af179be7 2023-04-14 stsp
7690 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7691 818c7501 2019-07-11 stsp if (err)
7692 818c7501 2019-07-11 stsp return err;
7693 af179be7 2023-04-14 stsp
7694 af179be7 2023-04-14 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7695 af179be7 2023-04-14 stsp if (err)
7696 af179be7 2023-04-14 stsp goto done;
7697 af179be7 2023-04-14 stsp
7698 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7699 af179be7 2023-04-14 stsp if (err)
7700 af179be7 2023-04-14 stsp goto done;
7701 af179be7 2023-04-14 stsp
7702 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7703 af179be7 2023-04-14 stsp if (err)
7704 af179be7 2023-04-14 stsp goto done;
7705 a44927cc 2022-04-07 stsp
7706 af179be7 2023-04-14 stsp /*
7707 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
7708 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
7709 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
7710 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
7711 af179be7 2023-04-14 stsp */
7712 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7713 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
7714 af179be7 2023-04-14 stsp if (err)
7715 af179be7 2023-04-14 stsp goto done;
7716 af179be7 2023-04-14 stsp
7717 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7718 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7719 818c7501 2019-07-11 stsp if (err)
7720 818c7501 2019-07-11 stsp goto done;
7721 818c7501 2019-07-11 stsp
7722 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7723 818c7501 2019-07-11 stsp if (err)
7724 818c7501 2019-07-11 stsp goto done;
7725 818c7501 2019-07-11 stsp
7726 818c7501 2019-07-11 stsp /*
7727 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7728 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7729 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7730 818c7501 2019-07-11 stsp */
7731 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7732 818c7501 2019-07-11 stsp if (err)
7733 818c7501 2019-07-11 stsp goto done;
7734 818c7501 2019-07-11 stsp
7735 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7736 1b093d84 2023-04-12 stsp if (err)
7737 1b093d84 2023-04-12 stsp goto done;
7738 1b093d84 2023-04-12 stsp
7739 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
7740 1b093d84 2023-04-12 stsp worktree->base_commit_id);
7741 818c7501 2019-07-11 stsp if (err)
7742 818c7501 2019-07-11 stsp goto done;
7743 818c7501 2019-07-11 stsp
7744 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7745 a44927cc 2022-04-07 stsp worktree->path_prefix);
7746 ca355955 2019-07-12 stsp if (err)
7747 ca355955 2019-07-12 stsp goto done;
7748 ca355955 2019-07-12 stsp
7749 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7750 ca355955 2019-07-12 stsp if (err)
7751 ca355955 2019-07-12 stsp goto done;
7752 ca355955 2019-07-12 stsp
7753 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7754 818c7501 2019-07-11 stsp if (err)
7755 818c7501 2019-07-11 stsp goto done;
7756 818c7501 2019-07-11 stsp
7757 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7758 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7759 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7760 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7761 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7762 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7763 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7764 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
7765 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
7766 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7767 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7768 55bd499d 2019-07-12 stsp if (err)
7769 1f1abb7e 2019-08-08 stsp goto sync;
7770 55bd499d 2019-07-12 stsp
7771 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7772 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7773 ca355955 2019-07-12 stsp sync:
7774 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7775 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7776 ca355955 2019-07-12 stsp err = sync_err;
7777 818c7501 2019-07-11 stsp done:
7778 af179be7 2023-04-14 stsp got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7779 818c7501 2019-07-11 stsp got_ref_close(resolved);
7780 a3a2faf2 2019-07-12 stsp free(tree_id);
7781 818c7501 2019-07-11 stsp free(commit_id);
7782 af179be7 2023-04-14 stsp free(merged_commit_id);
7783 a44927cc 2022-04-07 stsp if (commit)
7784 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7785 0ebf8283 2019-07-24 stsp if (fileindex)
7786 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7787 0ebf8283 2019-07-24 stsp free(fileindex_path);
7788 af179be7 2023-04-14 stsp free(commit_ref_name);
7789 af179be7 2023-04-14 stsp if (commit_ref)
7790 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
7791 0ebf8283 2019-07-24 stsp
7792 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7793 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7794 0ebf8283 2019-07-24 stsp err = unlockerr;
7795 0ebf8283 2019-07-24 stsp return err;
7796 0ebf8283 2019-07-24 stsp }
7797 0ebf8283 2019-07-24 stsp
7798 0ebf8283 2019-07-24 stsp const struct got_error *
7799 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7800 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7801 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7802 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7803 0ebf8283 2019-07-24 stsp {
7804 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7805 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7806 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7807 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7808 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7809 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7810 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7811 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7812 0ebf8283 2019-07-24 stsp
7813 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7814 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7815 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7816 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7817 0ebf8283 2019-07-24 stsp
7818 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7819 0ebf8283 2019-07-24 stsp if (err)
7820 0ebf8283 2019-07-24 stsp return err;
7821 0ebf8283 2019-07-24 stsp
7822 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7823 0ebf8283 2019-07-24 stsp if (err)
7824 0ebf8283 2019-07-24 stsp goto done;
7825 0ebf8283 2019-07-24 stsp
7826 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7827 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7828 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7829 0ebf8283 2019-07-24 stsp &ok_arg);
7830 0ebf8283 2019-07-24 stsp if (err)
7831 0ebf8283 2019-07-24 stsp goto done;
7832 0ebf8283 2019-07-24 stsp
7833 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7834 0ebf8283 2019-07-24 stsp if (err)
7835 0ebf8283 2019-07-24 stsp goto done;
7836 0ebf8283 2019-07-24 stsp
7837 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7838 0ebf8283 2019-07-24 stsp if (err)
7839 0ebf8283 2019-07-24 stsp goto done;
7840 0ebf8283 2019-07-24 stsp
7841 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7842 0ebf8283 2019-07-24 stsp worktree);
7843 0ebf8283 2019-07-24 stsp if (err)
7844 0ebf8283 2019-07-24 stsp goto done;
7845 0ebf8283 2019-07-24 stsp
7846 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7847 0ebf8283 2019-07-24 stsp 0);
7848 0ebf8283 2019-07-24 stsp if (err)
7849 0ebf8283 2019-07-24 stsp goto done;
7850 0ebf8283 2019-07-24 stsp
7851 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7852 0ebf8283 2019-07-24 stsp if (err)
7853 0ebf8283 2019-07-24 stsp goto done;
7854 0ebf8283 2019-07-24 stsp
7855 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7856 0ebf8283 2019-07-24 stsp if (err)
7857 0ebf8283 2019-07-24 stsp goto done;
7858 0ebf8283 2019-07-24 stsp
7859 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7860 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7861 0ebf8283 2019-07-24 stsp if (err)
7862 0ebf8283 2019-07-24 stsp goto done;
7863 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7864 0ebf8283 2019-07-24 stsp if (err)
7865 0ebf8283 2019-07-24 stsp goto done;
7866 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7867 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7868 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7869 0ebf8283 2019-07-24 stsp goto done;
7870 0ebf8283 2019-07-24 stsp }
7871 0ebf8283 2019-07-24 stsp
7872 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7873 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7874 0ebf8283 2019-07-24 stsp if (err)
7875 0ebf8283 2019-07-24 stsp goto done;
7876 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7877 0ebf8283 2019-07-24 stsp if (err)
7878 0ebf8283 2019-07-24 stsp goto done;
7879 0ebf8283 2019-07-24 stsp
7880 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7881 0ebf8283 2019-07-24 stsp if (err)
7882 0ebf8283 2019-07-24 stsp goto done;
7883 0ebf8283 2019-07-24 stsp done:
7884 0ebf8283 2019-07-24 stsp free(fileindex_path);
7885 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7886 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7887 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7888 0ebf8283 2019-07-24 stsp if (wt_branch)
7889 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7890 0ebf8283 2019-07-24 stsp if (err) {
7891 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7892 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7893 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7894 0ebf8283 2019-07-24 stsp }
7895 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7896 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7897 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7898 0ebf8283 2019-07-24 stsp }
7899 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7900 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7901 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7902 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7903 3e3a69f1 2019-07-25 stsp }
7904 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7905 0ebf8283 2019-07-24 stsp }
7906 0ebf8283 2019-07-24 stsp return err;
7907 0ebf8283 2019-07-24 stsp }
7908 0ebf8283 2019-07-24 stsp
7909 0ebf8283 2019-07-24 stsp const struct got_error *
7910 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7911 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7912 0ebf8283 2019-07-24 stsp {
7913 3e3a69f1 2019-07-25 stsp if (fileindex)
7914 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7915 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7916 0ebf8283 2019-07-24 stsp }
7917 0ebf8283 2019-07-24 stsp
7918 0ebf8283 2019-07-24 stsp const struct got_error *
7919 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7920 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7921 0ebf8283 2019-07-24 stsp {
7922 0ebf8283 2019-07-24 stsp const struct got_error *err;
7923 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7924 0ebf8283 2019-07-24 stsp
7925 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7926 0ebf8283 2019-07-24 stsp if (err)
7927 0ebf8283 2019-07-24 stsp return err;
7928 0ebf8283 2019-07-24 stsp
7929 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7930 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7931 0ebf8283 2019-07-24 stsp return NULL;
7932 0ebf8283 2019-07-24 stsp }
7933 0ebf8283 2019-07-24 stsp
7934 0ebf8283 2019-07-24 stsp const struct got_error *
7935 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7936 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7937 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7938 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7939 0ebf8283 2019-07-24 stsp {
7940 0ebf8283 2019-07-24 stsp const struct got_error *err;
7941 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7942 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7943 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7944 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7945 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7946 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7947 0ebf8283 2019-07-24 stsp
7948 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7949 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7950 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7951 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7952 0ebf8283 2019-07-24 stsp
7953 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7954 3e3a69f1 2019-07-25 stsp if (err)
7955 3e3a69f1 2019-07-25 stsp return err;
7956 3e3a69f1 2019-07-25 stsp
7957 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7958 3e3a69f1 2019-07-25 stsp if (err)
7959 f032f1f7 2019-08-04 stsp goto done;
7960 f032f1f7 2019-08-04 stsp
7961 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7962 f032f1f7 2019-08-04 stsp &have_staged_files);
7963 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7964 f032f1f7 2019-08-04 stsp goto done;
7965 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7966 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7967 3e3a69f1 2019-07-25 stsp goto done;
7968 f032f1f7 2019-08-04 stsp }
7969 3e3a69f1 2019-07-25 stsp
7970 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7971 0ebf8283 2019-07-24 stsp if (err)
7972 f032f1f7 2019-08-04 stsp goto done;
7973 0ebf8283 2019-07-24 stsp
7974 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7975 0ebf8283 2019-07-24 stsp if (err)
7976 0ebf8283 2019-07-24 stsp goto done;
7977 0ebf8283 2019-07-24 stsp
7978 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7979 0ebf8283 2019-07-24 stsp if (err)
7980 0ebf8283 2019-07-24 stsp goto done;
7981 0ebf8283 2019-07-24 stsp
7982 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7983 0ebf8283 2019-07-24 stsp worktree);
7984 0ebf8283 2019-07-24 stsp if (err)
7985 0ebf8283 2019-07-24 stsp goto done;
7986 0ebf8283 2019-07-24 stsp
7987 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7988 0ebf8283 2019-07-24 stsp if (err)
7989 0ebf8283 2019-07-24 stsp goto done;
7990 0ebf8283 2019-07-24 stsp
7991 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7992 0ebf8283 2019-07-24 stsp if (err)
7993 0ebf8283 2019-07-24 stsp goto done;
7994 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7995 0ebf8283 2019-07-24 stsp if (err)
7996 0ebf8283 2019-07-24 stsp goto done;
7997 0ebf8283 2019-07-24 stsp
7998 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7999 0ebf8283 2019-07-24 stsp if (err)
8000 0ebf8283 2019-07-24 stsp goto done;
8001 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8002 0ebf8283 2019-07-24 stsp if (err)
8003 0ebf8283 2019-07-24 stsp goto done;
8004 0ebf8283 2019-07-24 stsp
8005 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8006 0ebf8283 2019-07-24 stsp if (err)
8007 0ebf8283 2019-07-24 stsp goto done;
8008 0ebf8283 2019-07-24 stsp done:
8009 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8010 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8011 3e3a69f1 2019-07-25 stsp free(fileindex_path);
8012 0ebf8283 2019-07-24 stsp if (commit_ref)
8013 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
8014 0ebf8283 2019-07-24 stsp if (base_commit_ref)
8015 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
8016 0ebf8283 2019-07-24 stsp if (err) {
8017 0ebf8283 2019-07-24 stsp free(*commit_id);
8018 0ebf8283 2019-07-24 stsp *commit_id = NULL;
8019 0ebf8283 2019-07-24 stsp free(*base_commit_id);
8020 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
8021 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
8022 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
8023 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
8024 0ebf8283 2019-07-24 stsp }
8025 3e3a69f1 2019-07-25 stsp if (*fileindex) {
8026 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
8027 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
8028 3e3a69f1 2019-07-25 stsp }
8029 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
8030 0ebf8283 2019-07-24 stsp }
8031 0ebf8283 2019-07-24 stsp return err;
8032 0ebf8283 2019-07-24 stsp }
8033 0ebf8283 2019-07-24 stsp
8034 0ebf8283 2019-07-24 stsp static const struct got_error *
8035 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8036 0ebf8283 2019-07-24 stsp {
8037 0ebf8283 2019-07-24 stsp const struct got_error *err;
8038 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8039 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
8040 0ebf8283 2019-07-24 stsp
8041 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8042 0ebf8283 2019-07-24 stsp if (err)
8043 0ebf8283 2019-07-24 stsp goto done;
8044 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
8045 0ebf8283 2019-07-24 stsp if (err)
8046 0ebf8283 2019-07-24 stsp goto done;
8047 0ebf8283 2019-07-24 stsp
8048 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8049 0ebf8283 2019-07-24 stsp worktree);
8050 0ebf8283 2019-07-24 stsp if (err)
8051 0ebf8283 2019-07-24 stsp goto done;
8052 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
8053 0ebf8283 2019-07-24 stsp if (err)
8054 0ebf8283 2019-07-24 stsp goto done;
8055 0ebf8283 2019-07-24 stsp
8056 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8057 0ebf8283 2019-07-24 stsp if (err)
8058 0ebf8283 2019-07-24 stsp goto done;
8059 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
8060 0ebf8283 2019-07-24 stsp if (err)
8061 0ebf8283 2019-07-24 stsp goto done;
8062 0ebf8283 2019-07-24 stsp
8063 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8064 0ebf8283 2019-07-24 stsp if (err)
8065 0ebf8283 2019-07-24 stsp goto done;
8066 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8067 0ebf8283 2019-07-24 stsp if (err)
8068 0ebf8283 2019-07-24 stsp goto done;
8069 0ebf8283 2019-07-24 stsp done:
8070 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
8071 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
8072 0ebf8283 2019-07-24 stsp free(branch_ref_name);
8073 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8074 0ebf8283 2019-07-24 stsp return err;
8075 0ebf8283 2019-07-24 stsp }
8076 0ebf8283 2019-07-24 stsp
8077 0ebf8283 2019-07-24 stsp const struct got_error *
8078 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
8079 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8080 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
8081 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8082 0ebf8283 2019-07-24 stsp {
8083 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8084 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8085 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
8086 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8087 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8088 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8089 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8090 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
8091 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
8092 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8093 0ebf8283 2019-07-24 stsp
8094 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8095 af179be7 2023-04-14 stsp
8096 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
8097 0ebf8283 2019-07-24 stsp if (err)
8098 0ebf8283 2019-07-24 stsp return err;
8099 a44927cc 2022-04-07 stsp
8100 af179be7 2023-04-14 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8101 af179be7 2023-04-14 stsp if (err)
8102 af179be7 2023-04-14 stsp goto done;
8103 af179be7 2023-04-14 stsp
8104 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8105 af179be7 2023-04-14 stsp if (err) {
8106 af179be7 2023-04-14 stsp if (err->code != GOT_ERR_NOT_REF)
8107 af179be7 2023-04-14 stsp goto done;
8108 af179be7 2023-04-14 stsp /* Can happen on early abort due to invalid histedit script. */
8109 af179be7 2023-04-14 stsp commit_ref = NULL;
8110 af179be7 2023-04-14 stsp }
8111 af179be7 2023-04-14 stsp
8112 af179be7 2023-04-14 stsp if (commit_ref) {
8113 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8114 af179be7 2023-04-14 stsp if (err)
8115 af179be7 2023-04-14 stsp goto done;
8116 af179be7 2023-04-14 stsp
8117 af179be7 2023-04-14 stsp /*
8118 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8119 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8120 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files added by the
8121 af179be7 2023-04-14 stsp * user during conflict resolution or during histedit -e.
8122 af179be7 2023-04-14 stsp */
8123 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8124 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8125 af179be7 2023-04-14 stsp if (err)
8126 af179be7 2023-04-14 stsp goto done;
8127 af179be7 2023-04-14 stsp }
8128 af179be7 2023-04-14 stsp
8129 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8130 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
8131 0ebf8283 2019-07-24 stsp if (err)
8132 0ebf8283 2019-07-24 stsp goto done;
8133 0ebf8283 2019-07-24 stsp
8134 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8135 0ebf8283 2019-07-24 stsp if (err)
8136 0ebf8283 2019-07-24 stsp goto done;
8137 0ebf8283 2019-07-24 stsp
8138 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8139 0ebf8283 2019-07-24 stsp if (err)
8140 0ebf8283 2019-07-24 stsp goto done;
8141 0ebf8283 2019-07-24 stsp
8142 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
8143 1b093d84 2023-04-12 stsp worktree->base_commit_id);
8144 1b093d84 2023-04-12 stsp if (err)
8145 1b093d84 2023-04-12 stsp goto done;
8146 1b093d84 2023-04-12 stsp
8147 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8148 0ebf8283 2019-07-24 stsp worktree->path_prefix);
8149 0ebf8283 2019-07-24 stsp if (err)
8150 0ebf8283 2019-07-24 stsp goto done;
8151 0ebf8283 2019-07-24 stsp
8152 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8153 0ebf8283 2019-07-24 stsp if (err)
8154 0ebf8283 2019-07-24 stsp goto done;
8155 0ebf8283 2019-07-24 stsp
8156 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
8157 0ebf8283 2019-07-24 stsp if (err)
8158 0ebf8283 2019-07-24 stsp goto done;
8159 0ebf8283 2019-07-24 stsp
8160 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
8161 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
8162 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
8163 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
8164 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
8165 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
8166 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
8167 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
8168 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8169 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8170 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8171 0ebf8283 2019-07-24 stsp if (err)
8172 1f1abb7e 2019-08-08 stsp goto sync;
8173 0ebf8283 2019-07-24 stsp
8174 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8175 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8176 0ebf8283 2019-07-24 stsp sync:
8177 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8178 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
8179 0ebf8283 2019-07-24 stsp err = sync_err;
8180 0ebf8283 2019-07-24 stsp done:
8181 af179be7 2023-04-14 stsp if (resolved)
8182 af179be7 2023-04-14 stsp got_ref_close(resolved);
8183 af179be7 2023-04-14 stsp if (commit_ref)
8184 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8185 af179be7 2023-04-14 stsp free(merged_commit_id);
8186 0ebf8283 2019-07-24 stsp free(tree_id);
8187 818c7501 2019-07-11 stsp free(fileindex_path);
8188 af179be7 2023-04-14 stsp free(commit_ref_name);
8189 818c7501 2019-07-11 stsp
8190 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8191 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
8192 818c7501 2019-07-11 stsp err = unlockerr;
8193 818c7501 2019-07-11 stsp return err;
8194 818c7501 2019-07-11 stsp }
8195 0ebf8283 2019-07-24 stsp
8196 0ebf8283 2019-07-24 stsp const struct got_error *
8197 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8198 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8199 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8200 0ebf8283 2019-07-24 stsp {
8201 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8202 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8203 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8204 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8205 0ebf8283 2019-07-24 stsp
8206 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8207 0ebf8283 2019-07-24 stsp if (err)
8208 0ebf8283 2019-07-24 stsp return err;
8209 0ebf8283 2019-07-24 stsp
8210 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8211 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8212 e600f124 2021-03-21 stsp if (err)
8213 e600f124 2021-03-21 stsp goto done;
8214 e600f124 2021-03-21 stsp
8215 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8216 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8217 0ebf8283 2019-07-24 stsp if (err)
8218 0ebf8283 2019-07-24 stsp goto done;
8219 0ebf8283 2019-07-24 stsp
8220 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8221 0ebf8283 2019-07-24 stsp if (err)
8222 0ebf8283 2019-07-24 stsp goto done;
8223 0ebf8283 2019-07-24 stsp
8224 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8225 0ebf8283 2019-07-24 stsp if (err)
8226 0ebf8283 2019-07-24 stsp goto done;
8227 0ebf8283 2019-07-24 stsp
8228 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8229 0ebf8283 2019-07-24 stsp if (err)
8230 0ebf8283 2019-07-24 stsp goto done;
8231 0ebf8283 2019-07-24 stsp
8232 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8233 a615e0e7 2020-12-16 stsp if (err)
8234 a615e0e7 2020-12-16 stsp goto done;
8235 a615e0e7 2020-12-16 stsp
8236 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8237 a615e0e7 2020-12-16 stsp if (err)
8238 a615e0e7 2020-12-16 stsp goto done;
8239 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8240 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8241 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8242 a615e0e7 2020-12-16 stsp err = sync_err;
8243 0ebf8283 2019-07-24 stsp done:
8244 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8245 a615e0e7 2020-12-16 stsp free(fileindex_path);
8246 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8247 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8248 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8249 0ebf8283 2019-07-24 stsp err = unlockerr;
8250 0ebf8283 2019-07-24 stsp return err;
8251 0ebf8283 2019-07-24 stsp }
8252 0ebf8283 2019-07-24 stsp
8253 0ebf8283 2019-07-24 stsp const struct got_error *
8254 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8255 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8256 0ebf8283 2019-07-24 stsp {
8257 0ebf8283 2019-07-24 stsp const struct got_error *err;
8258 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8259 0ebf8283 2019-07-24 stsp
8260 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8261 0ebf8283 2019-07-24 stsp if (err)
8262 0ebf8283 2019-07-24 stsp return err;
8263 0ebf8283 2019-07-24 stsp
8264 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8265 0ebf8283 2019-07-24 stsp if (err)
8266 0ebf8283 2019-07-24 stsp goto done;
8267 0ebf8283 2019-07-24 stsp
8268 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8269 0ebf8283 2019-07-24 stsp done:
8270 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8271 2822a352 2019-10-15 stsp return err;
8272 2822a352 2019-10-15 stsp }
8273 2822a352 2019-10-15 stsp
8274 2822a352 2019-10-15 stsp const struct got_error *
8275 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8276 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8277 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8278 2822a352 2019-10-15 stsp struct got_repository *repo)
8279 2822a352 2019-10-15 stsp {
8280 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8281 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8282 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8283 2822a352 2019-10-15 stsp
8284 2822a352 2019-10-15 stsp *fileindex = NULL;
8285 2822a352 2019-10-15 stsp *branch_ref = NULL;
8286 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8287 2822a352 2019-10-15 stsp
8288 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8289 2822a352 2019-10-15 stsp if (err)
8290 2822a352 2019-10-15 stsp return err;
8291 2822a352 2019-10-15 stsp
8292 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8293 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8294 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8295 2822a352 2019-10-15 stsp "update -b or different branch name required");
8296 2822a352 2019-10-15 stsp goto done;
8297 2822a352 2019-10-15 stsp }
8298 2822a352 2019-10-15 stsp
8299 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8300 2822a352 2019-10-15 stsp if (err)
8301 2822a352 2019-10-15 stsp goto done;
8302 2822a352 2019-10-15 stsp
8303 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8304 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8305 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8306 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8307 2822a352 2019-10-15 stsp &ok_arg);
8308 2822a352 2019-10-15 stsp if (err)
8309 2822a352 2019-10-15 stsp goto done;
8310 2822a352 2019-10-15 stsp
8311 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8312 2822a352 2019-10-15 stsp if (err)
8313 2822a352 2019-10-15 stsp goto done;
8314 2822a352 2019-10-15 stsp
8315 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8316 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8317 2822a352 2019-10-15 stsp done:
8318 2822a352 2019-10-15 stsp if (err) {
8319 2822a352 2019-10-15 stsp if (*branch_ref) {
8320 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8321 2822a352 2019-10-15 stsp *branch_ref = NULL;
8322 2822a352 2019-10-15 stsp }
8323 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8324 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8325 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8326 2822a352 2019-10-15 stsp }
8327 2822a352 2019-10-15 stsp if (*fileindex) {
8328 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8329 2822a352 2019-10-15 stsp *fileindex = NULL;
8330 2822a352 2019-10-15 stsp }
8331 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8332 2822a352 2019-10-15 stsp }
8333 0cb83759 2019-08-03 stsp return err;
8334 0cb83759 2019-08-03 stsp }
8335 0cb83759 2019-08-03 stsp
8336 2822a352 2019-10-15 stsp const struct got_error *
8337 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8338 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8339 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8340 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8341 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8342 2822a352 2019-10-15 stsp {
8343 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8344 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8345 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8346 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8347 2822a352 2019-10-15 stsp
8348 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8349 2822a352 2019-10-15 stsp if (err)
8350 2822a352 2019-10-15 stsp goto done;
8351 2822a352 2019-10-15 stsp
8352 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8353 2822a352 2019-10-15 stsp if (err)
8354 2822a352 2019-10-15 stsp goto done;
8355 2822a352 2019-10-15 stsp
8356 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8357 a44927cc 2022-04-07 stsp if (err)
8358 a44927cc 2022-04-07 stsp goto done;
8359 a44927cc 2022-04-07 stsp
8360 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8361 2822a352 2019-10-15 stsp worktree->path_prefix);
8362 2822a352 2019-10-15 stsp if (err)
8363 2822a352 2019-10-15 stsp goto done;
8364 2822a352 2019-10-15 stsp
8365 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8366 2822a352 2019-10-15 stsp if (err)
8367 2822a352 2019-10-15 stsp goto done;
8368 2822a352 2019-10-15 stsp
8369 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8370 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8371 2822a352 2019-10-15 stsp if (err)
8372 2822a352 2019-10-15 stsp goto sync;
8373 2822a352 2019-10-15 stsp
8374 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8375 2822a352 2019-10-15 stsp if (err)
8376 2822a352 2019-10-15 stsp goto sync;
8377 2822a352 2019-10-15 stsp
8378 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8379 6e210706 2021-01-22 stsp if (err)
8380 6e210706 2021-01-22 stsp goto sync;
8381 6e210706 2021-01-22 stsp
8382 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8383 2822a352 2019-10-15 stsp sync:
8384 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8385 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8386 2822a352 2019-10-15 stsp err = sync_err;
8387 2822a352 2019-10-15 stsp
8388 2822a352 2019-10-15 stsp done:
8389 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8390 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8391 2822a352 2019-10-15 stsp err = unlockerr;
8392 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8393 2822a352 2019-10-15 stsp
8394 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8395 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8396 2822a352 2019-10-15 stsp err = unlockerr;
8397 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8398 2822a352 2019-10-15 stsp
8399 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8400 2822a352 2019-10-15 stsp free(fileindex_path);
8401 2822a352 2019-10-15 stsp free(tree_id);
8402 a44927cc 2022-04-07 stsp if (commit)
8403 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8404 2822a352 2019-10-15 stsp
8405 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8406 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8407 2822a352 2019-10-15 stsp err = unlockerr;
8408 2822a352 2019-10-15 stsp return err;
8409 2822a352 2019-10-15 stsp }
8410 2822a352 2019-10-15 stsp
8411 2822a352 2019-10-15 stsp const struct got_error *
8412 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8413 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8414 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8415 2822a352 2019-10-15 stsp {
8416 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8417 8b692cd0 2019-10-21 stsp
8418 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8419 8b692cd0 2019-10-21 stsp
8420 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8421 8b692cd0 2019-10-21 stsp
8422 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8423 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8424 8b692cd0 2019-10-21 stsp err = unlockerr;
8425 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8426 8b692cd0 2019-10-21 stsp
8427 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8428 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8429 8b692cd0 2019-10-21 stsp err = unlockerr;
8430 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8431 f259c4c1 2021-09-24 stsp
8432 f259c4c1 2021-09-24 stsp return err;
8433 f259c4c1 2021-09-24 stsp }
8434 f259c4c1 2021-09-24 stsp
8435 f259c4c1 2021-09-24 stsp const struct got_error *
8436 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
8437 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
8438 f259c4c1 2021-09-24 stsp {
8439 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
8440 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8441 f259c4c1 2021-09-24 stsp
8442 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8443 f259c4c1 2021-09-24 stsp if (err)
8444 f259c4c1 2021-09-24 stsp goto done;
8445 8b692cd0 2019-10-21 stsp
8446 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8447 f259c4c1 2021-09-24 stsp
8448 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
8449 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8450 f259c4c1 2021-09-24 stsp err = sync_err;
8451 f259c4c1 2021-09-24 stsp done:
8452 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8453 f259c4c1 2021-09-24 stsp free(fileindex_path);
8454 8b692cd0 2019-10-21 stsp return err;
8455 2822a352 2019-10-15 stsp }
8456 2822a352 2019-10-15 stsp
8457 f259c4c1 2021-09-24 stsp static const struct got_error *
8458 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8459 f259c4c1 2021-09-24 stsp {
8460 f259c4c1 2021-09-24 stsp const struct got_error *err;
8461 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8462 f259c4c1 2021-09-24 stsp
8463 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8464 f259c4c1 2021-09-24 stsp if (err)
8465 f259c4c1 2021-09-24 stsp goto done;
8466 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
8467 f259c4c1 2021-09-24 stsp if (err)
8468 f259c4c1 2021-09-24 stsp goto done;
8469 f259c4c1 2021-09-24 stsp
8470 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8471 f259c4c1 2021-09-24 stsp if (err)
8472 f259c4c1 2021-09-24 stsp goto done;
8473 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
8474 f259c4c1 2021-09-24 stsp if (err)
8475 f259c4c1 2021-09-24 stsp goto done;
8476 f259c4c1 2021-09-24 stsp
8477 f259c4c1 2021-09-24 stsp done:
8478 f259c4c1 2021-09-24 stsp free(branch_refname);
8479 f259c4c1 2021-09-24 stsp free(commit_refname);
8480 f259c4c1 2021-09-24 stsp return err;
8481 f259c4c1 2021-09-24 stsp }
8482 f259c4c1 2021-09-24 stsp
8483 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
8484 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
8485 f259c4c1 2021-09-24 stsp const char *branch_name;
8486 f259c4c1 2021-09-24 stsp };
8487 f259c4c1 2021-09-24 stsp
8488 f259c4c1 2021-09-24 stsp static const struct got_error *
8489 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8490 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
8491 f259c4c1 2021-09-24 stsp {
8492 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
8493 f259c4c1 2021-09-24 stsp
8494 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8495 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
8496 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
8497 f259c4c1 2021-09-24 stsp
8498 f259c4c1 2021-09-24 stsp return NULL;
8499 f259c4c1 2021-09-24 stsp }
8500 f259c4c1 2021-09-24 stsp
8501 f259c4c1 2021-09-24 stsp
8502 f259c4c1 2021-09-24 stsp const struct got_error *
8503 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
8504 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
8505 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
8506 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
8507 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8508 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8509 f259c4c1 2021-09-24 stsp {
8510 f259c4c1 2021-09-24 stsp const struct got_error *err;
8511 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8512 cdbfe5d2 2023-07-24 stsp struct check_mixed_commits_args cma;
8513 f259c4c1 2021-09-24 stsp
8514 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8515 f259c4c1 2021-09-24 stsp if (err)
8516 f259c4c1 2021-09-24 stsp goto done;
8517 f259c4c1 2021-09-24 stsp
8518 cdbfe5d2 2023-07-24 stsp cma.worktree = worktree;
8519 cdbfe5d2 2023-07-24 stsp cma.cancel_cb = cancel_cb;
8520 cdbfe5d2 2023-07-24 stsp cma.cancel_arg = cancel_arg;
8521 cdbfe5d2 2023-07-24 stsp
8522 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8523 e07c1782 2023-07-24 op &cma);
8524 f259c4c1 2021-09-24 stsp if (err)
8525 f259c4c1 2021-09-24 stsp goto done;
8526 f259c4c1 2021-09-24 stsp
8527 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8528 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
8529 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
8530 f259c4c1 2021-09-24 stsp done:
8531 f259c4c1 2021-09-24 stsp free(fileindex_path);
8532 f259c4c1 2021-09-24 stsp return err;
8533 f259c4c1 2021-09-24 stsp }
8534 f259c4c1 2021-09-24 stsp
8535 f259c4c1 2021-09-24 stsp const struct got_error *
8536 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
8537 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8538 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
8539 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
8540 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
8541 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
8542 0ff8d236 2021-09-28 stsp
8543 f259c4c1 2021-09-24 stsp {
8544 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
8545 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
8546 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
8547 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
8548 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
8549 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
8550 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8551 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
8552 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8553 f259c4c1 2021-09-24 stsp
8554 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
8555 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
8556 f259c4c1 2021-09-24 stsp
8557 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
8558 f259c4c1 2021-09-24 stsp
8559 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8560 f259c4c1 2021-09-24 stsp if (err)
8561 f259c4c1 2021-09-24 stsp goto done;
8562 f259c4c1 2021-09-24 stsp
8563 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8564 f259c4c1 2021-09-24 stsp if (err)
8565 f259c4c1 2021-09-24 stsp goto done;
8566 f259c4c1 2021-09-24 stsp
8567 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8568 f259c4c1 2021-09-24 stsp if (err)
8569 f259c4c1 2021-09-24 stsp goto done;
8570 f259c4c1 2021-09-24 stsp
8571 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8572 f259c4c1 2021-09-24 stsp &have_staged_files);
8573 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8574 f259c4c1 2021-09-24 stsp goto done;
8575 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8576 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8577 f259c4c1 2021-09-24 stsp goto done;
8578 f259c4c1 2021-09-24 stsp }
8579 f259c4c1 2021-09-24 stsp
8580 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
8581 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
8582 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
8583 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
8584 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
8585 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8586 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8587 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8588 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8589 f259c4c1 2021-09-24 stsp if (err)
8590 f259c4c1 2021-09-24 stsp goto done;
8591 f259c4c1 2021-09-24 stsp
8592 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8593 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8594 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8595 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8596 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8597 f259c4c1 2021-09-24 stsp if (err)
8598 f259c4c1 2021-09-24 stsp goto done;
8599 f259c4c1 2021-09-24 stsp
8600 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8601 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8602 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8603 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8604 f259c4c1 2021-09-24 stsp err = sync_err;
8605 f259c4c1 2021-09-24 stsp done:
8606 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8607 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8608 d8bacb93 2023-01-10 mark
8609 f259c4c1 2021-09-24 stsp free_commitable(ct);
8610 f259c4c1 2021-09-24 stsp }
8611 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8612 f259c4c1 2021-09-24 stsp free(fileindex_path);
8613 f259c4c1 2021-09-24 stsp return err;
8614 f259c4c1 2021-09-24 stsp }
8615 f259c4c1 2021-09-24 stsp
8616 f259c4c1 2021-09-24 stsp const struct got_error *
8617 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8618 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8619 f259c4c1 2021-09-24 stsp {
8620 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8621 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8622 f259c4c1 2021-09-24 stsp
8623 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8624 f259c4c1 2021-09-24 stsp if (err)
8625 f259c4c1 2021-09-24 stsp goto done;
8626 f259c4c1 2021-09-24 stsp
8627 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8628 f259c4c1 2021-09-24 stsp if (err)
8629 f259c4c1 2021-09-24 stsp goto done;
8630 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8631 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8632 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8633 f259c4c1 2021-09-24 stsp err = sync_err;
8634 f259c4c1 2021-09-24 stsp done:
8635 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8636 f259c4c1 2021-09-24 stsp free(fileindex_path);
8637 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8638 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8639 f259c4c1 2021-09-24 stsp err = unlockerr;
8640 f259c4c1 2021-09-24 stsp return err;
8641 f259c4c1 2021-09-24 stsp }
8642 f259c4c1 2021-09-24 stsp
8643 f259c4c1 2021-09-24 stsp const struct got_error *
8644 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8645 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8646 f259c4c1 2021-09-24 stsp {
8647 f259c4c1 2021-09-24 stsp const struct got_error *err;
8648 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8649 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8650 f259c4c1 2021-09-24 stsp
8651 f259c4c1 2021-09-24 stsp *in_progress = 0;
8652 f259c4c1 2021-09-24 stsp
8653 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8654 f259c4c1 2021-09-24 stsp if (err)
8655 f259c4c1 2021-09-24 stsp return err;
8656 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8657 793fcac3 2021-09-24 stsp free(branch_refname);
8658 f259c4c1 2021-09-24 stsp if (err) {
8659 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8660 f259c4c1 2021-09-24 stsp return err;
8661 f259c4c1 2021-09-24 stsp } else
8662 f259c4c1 2021-09-24 stsp *in_progress = 1;
8663 f259c4c1 2021-09-24 stsp
8664 f259c4c1 2021-09-24 stsp return NULL;
8665 f259c4c1 2021-09-24 stsp }
8666 f259c4c1 2021-09-24 stsp
8667 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8668 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8669 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8670 f259c4c1 2021-09-24 stsp {
8671 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8672 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8673 179f9db0 2023-06-20 falsifian struct got_reference *wt_branch = NULL;
8674 179f9db0 2023-06-20 falsifian struct got_object_id *wt_branch_tip = NULL;
8675 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8676 f259c4c1 2021-09-24 stsp
8677 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8678 f259c4c1 2021-09-24 stsp
8679 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8680 f259c4c1 2021-09-24 stsp if (err)
8681 f259c4c1 2021-09-24 stsp return err;
8682 f259c4c1 2021-09-24 stsp
8683 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8684 f259c4c1 2021-09-24 stsp if (err)
8685 f259c4c1 2021-09-24 stsp goto done;
8686 f259c4c1 2021-09-24 stsp
8687 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8688 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8689 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8690 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8691 f259c4c1 2021-09-24 stsp &ok_arg);
8692 f259c4c1 2021-09-24 stsp if (err)
8693 f259c4c1 2021-09-24 stsp goto done;
8694 f259c4c1 2021-09-24 stsp
8695 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8696 f259c4c1 2021-09-24 stsp 0);
8697 f259c4c1 2021-09-24 stsp if (err)
8698 f259c4c1 2021-09-24 stsp goto done;
8699 f259c4c1 2021-09-24 stsp
8700 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8701 f259c4c1 2021-09-24 stsp if (err)
8702 f259c4c1 2021-09-24 stsp goto done;
8703 f259c4c1 2021-09-24 stsp
8704 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8705 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8706 f259c4c1 2021-09-24 stsp goto done;
8707 f259c4c1 2021-09-24 stsp }
8708 179f9db0 2023-06-20 falsifian
8709 179f9db0 2023-06-20 falsifian done:
8710 179f9db0 2023-06-20 falsifian free(fileindex_path);
8711 179f9db0 2023-06-20 falsifian if (wt_branch)
8712 179f9db0 2023-06-20 falsifian got_ref_close(wt_branch);
8713 179f9db0 2023-06-20 falsifian free(wt_branch_tip);
8714 179f9db0 2023-06-20 falsifian if (err) {
8715 179f9db0 2023-06-20 falsifian if (*fileindex) {
8716 179f9db0 2023-06-20 falsifian got_fileindex_free(*fileindex);
8717 179f9db0 2023-06-20 falsifian *fileindex = NULL;
8718 179f9db0 2023-06-20 falsifian }
8719 179f9db0 2023-06-20 falsifian lock_worktree(worktree, LOCK_SH);
8720 179f9db0 2023-06-20 falsifian }
8721 179f9db0 2023-06-20 falsifian return err;
8722 179f9db0 2023-06-20 falsifian }
8723 f259c4c1 2021-09-24 stsp
8724 179f9db0 2023-06-20 falsifian const struct got_error *got_worktree_merge_write_refs(
8725 179f9db0 2023-06-20 falsifian struct got_worktree *worktree, struct got_reference *branch,
8726 179f9db0 2023-06-20 falsifian struct got_repository *repo)
8727 179f9db0 2023-06-20 falsifian {
8728 179f9db0 2023-06-20 falsifian const struct got_error *err = NULL;
8729 179f9db0 2023-06-20 falsifian char *branch_refname = NULL, *commit_refname = NULL;
8730 179f9db0 2023-06-20 falsifian struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8731 179f9db0 2023-06-20 falsifian struct got_object_id *branch_tip = NULL;
8732 179f9db0 2023-06-20 falsifian
8733 179f9db0 2023-06-20 falsifian err = get_merge_branch_ref_name(&branch_refname, worktree);
8734 179f9db0 2023-06-20 falsifian if (err)
8735 179f9db0 2023-06-20 falsifian return err;
8736 179f9db0 2023-06-20 falsifian
8737 179f9db0 2023-06-20 falsifian err = get_merge_commit_ref_name(&commit_refname, worktree);
8738 179f9db0 2023-06-20 falsifian if (err)
8739 179f9db0 2023-06-20 falsifian return err;
8740 179f9db0 2023-06-20 falsifian
8741 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8742 f259c4c1 2021-09-24 stsp if (err)
8743 f259c4c1 2021-09-24 stsp goto done;
8744 f259c4c1 2021-09-24 stsp
8745 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8746 f259c4c1 2021-09-24 stsp if (err)
8747 f259c4c1 2021-09-24 stsp goto done;
8748 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8749 f259c4c1 2021-09-24 stsp if (err)
8750 f259c4c1 2021-09-24 stsp goto done;
8751 f259c4c1 2021-09-24 stsp
8752 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8753 f259c4c1 2021-09-24 stsp if (err)
8754 f259c4c1 2021-09-24 stsp goto done;
8755 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8756 f259c4c1 2021-09-24 stsp if (err)
8757 f259c4c1 2021-09-24 stsp goto done;
8758 f259c4c1 2021-09-24 stsp
8759 f259c4c1 2021-09-24 stsp done:
8760 f259c4c1 2021-09-24 stsp free(branch_refname);
8761 f259c4c1 2021-09-24 stsp free(commit_refname);
8762 f259c4c1 2021-09-24 stsp if (branch_ref)
8763 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8764 f259c4c1 2021-09-24 stsp if (commit_ref)
8765 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8766 179f9db0 2023-06-20 falsifian free(branch_tip);
8767 f259c4c1 2021-09-24 stsp return err;
8768 f259c4c1 2021-09-24 stsp }
8769 f259c4c1 2021-09-24 stsp
8770 f259c4c1 2021-09-24 stsp const struct got_error *
8771 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8772 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8773 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8774 f259c4c1 2021-09-24 stsp {
8775 f259c4c1 2021-09-24 stsp const struct got_error *err;
8776 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8777 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8778 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8779 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8780 f259c4c1 2021-09-24 stsp
8781 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8782 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8783 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8784 f259c4c1 2021-09-24 stsp
8785 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8786 f259c4c1 2021-09-24 stsp if (err)
8787 f259c4c1 2021-09-24 stsp return err;
8788 f259c4c1 2021-09-24 stsp
8789 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8790 f259c4c1 2021-09-24 stsp if (err)
8791 f259c4c1 2021-09-24 stsp goto done;
8792 f259c4c1 2021-09-24 stsp
8793 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8794 f259c4c1 2021-09-24 stsp &have_staged_files);
8795 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8796 f259c4c1 2021-09-24 stsp goto done;
8797 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8798 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8799 f259c4c1 2021-09-24 stsp goto done;
8800 f259c4c1 2021-09-24 stsp }
8801 f259c4c1 2021-09-24 stsp
8802 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8803 f259c4c1 2021-09-24 stsp if (err)
8804 f259c4c1 2021-09-24 stsp goto done;
8805 f259c4c1 2021-09-24 stsp
8806 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8807 f259c4c1 2021-09-24 stsp if (err)
8808 f259c4c1 2021-09-24 stsp goto done;
8809 f259c4c1 2021-09-24 stsp
8810 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8811 f259c4c1 2021-09-24 stsp if (err)
8812 f259c4c1 2021-09-24 stsp goto done;
8813 f259c4c1 2021-09-24 stsp
8814 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8815 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8816 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8817 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8818 f259c4c1 2021-09-24 stsp goto done;
8819 f259c4c1 2021-09-24 stsp }
8820 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8821 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8822 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8823 f259c4c1 2021-09-24 stsp goto done;
8824 f259c4c1 2021-09-24 stsp }
8825 f259c4c1 2021-09-24 stsp
8826 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8827 f259c4c1 2021-09-24 stsp if (err)
8828 f259c4c1 2021-09-24 stsp goto done;
8829 f259c4c1 2021-09-24 stsp
8830 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8831 f259c4c1 2021-09-24 stsp if (err)
8832 f259c4c1 2021-09-24 stsp goto done;
8833 f259c4c1 2021-09-24 stsp done:
8834 f259c4c1 2021-09-24 stsp free(commit_refname);
8835 f259c4c1 2021-09-24 stsp free(branch_refname);
8836 f259c4c1 2021-09-24 stsp free(fileindex_path);
8837 f259c4c1 2021-09-24 stsp if (commit_ref)
8838 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8839 f259c4c1 2021-09-24 stsp if (branch_ref)
8840 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8841 f259c4c1 2021-09-24 stsp if (err) {
8842 f259c4c1 2021-09-24 stsp if (*branch_name) {
8843 f259c4c1 2021-09-24 stsp free(*branch_name);
8844 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8845 f259c4c1 2021-09-24 stsp }
8846 f259c4c1 2021-09-24 stsp free(*branch_tip);
8847 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8848 f259c4c1 2021-09-24 stsp if (*fileindex) {
8849 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8850 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8851 f259c4c1 2021-09-24 stsp }
8852 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8853 f259c4c1 2021-09-24 stsp }
8854 f259c4c1 2021-09-24 stsp return err;
8855 f259c4c1 2021-09-24 stsp }
8856 f259c4c1 2021-09-24 stsp
8857 f259c4c1 2021-09-24 stsp const struct got_error *
8858 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8859 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8860 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8861 f259c4c1 2021-09-24 stsp {
8862 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8863 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8864 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8865 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8866 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8867 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8868 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8869 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8870 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8871 af179be7 2023-04-14 stsp
8872 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8873 af179be7 2023-04-14 stsp
8874 af179be7 2023-04-14 stsp err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8875 af179be7 2023-04-14 stsp if (err)
8876 af179be7 2023-04-14 stsp goto done;
8877 af179be7 2023-04-14 stsp
8878 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8879 af179be7 2023-04-14 stsp if (err)
8880 af179be7 2023-04-14 stsp goto done;
8881 af179be7 2023-04-14 stsp
8882 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8883 af179be7 2023-04-14 stsp if (err)
8884 af179be7 2023-04-14 stsp goto done;
8885 af179be7 2023-04-14 stsp
8886 af179be7 2023-04-14 stsp /*
8887 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8888 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8889 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
8890 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
8891 af179be7 2023-04-14 stsp */
8892 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(&added_paths,
8893 af179be7 2023-04-14 stsp got_worktree_get_base_commit_id(worktree), merged_commit_id,
8894 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8895 af179be7 2023-04-14 stsp if (err)
8896 af179be7 2023-04-14 stsp goto done;
8897 f259c4c1 2021-09-24 stsp
8898 af179be7 2023-04-14 stsp
8899 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8900 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8901 a44927cc 2022-04-07 stsp if (err)
8902 a44927cc 2022-04-07 stsp goto done;
8903 a44927cc 2022-04-07 stsp
8904 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8905 a44927cc 2022-04-07 stsp worktree->path_prefix);
8906 f259c4c1 2021-09-24 stsp if (err)
8907 f259c4c1 2021-09-24 stsp goto done;
8908 f259c4c1 2021-09-24 stsp
8909 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8910 f259c4c1 2021-09-24 stsp if (err)
8911 f259c4c1 2021-09-24 stsp goto done;
8912 f259c4c1 2021-09-24 stsp
8913 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8914 f259c4c1 2021-09-24 stsp if (err)
8915 f259c4c1 2021-09-24 stsp goto done;
8916 f259c4c1 2021-09-24 stsp
8917 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8918 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8919 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8920 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8921 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8922 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8923 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8924 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8925 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8926 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8927 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8928 f259c4c1 2021-09-24 stsp if (err)
8929 f259c4c1 2021-09-24 stsp goto sync;
8930 f259c4c1 2021-09-24 stsp
8931 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8932 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8933 f259c4c1 2021-09-24 stsp sync:
8934 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8935 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8936 f259c4c1 2021-09-24 stsp err = sync_err;
8937 f259c4c1 2021-09-24 stsp done:
8938 f259c4c1 2021-09-24 stsp free(tree_id);
8939 af179be7 2023-04-14 stsp free(merged_commit_id);
8940 a44927cc 2022-04-07 stsp if (commit)
8941 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8942 f259c4c1 2021-09-24 stsp if (fileindex)
8943 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8944 f259c4c1 2021-09-24 stsp free(fileindex_path);
8945 af179be7 2023-04-14 stsp if (commit_ref)
8946 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8947 af179be7 2023-04-14 stsp free(commit_ref_name);
8948 f259c4c1 2021-09-24 stsp
8949 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8950 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8951 f259c4c1 2021-09-24 stsp err = unlockerr;
8952 f259c4c1 2021-09-24 stsp return err;
8953 f259c4c1 2021-09-24 stsp }
8954 f259c4c1 2021-09-24 stsp
8955 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8956 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8957 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8958 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8959 2db2652d 2019-08-07 stsp struct got_repository *repo;
8960 2db2652d 2019-08-07 stsp int have_changes;
8961 2db2652d 2019-08-07 stsp };
8962 2db2652d 2019-08-07 stsp
8963 336075a4 2022-06-25 op static const struct got_error *
8964 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8965 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8966 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8967 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8968 735ef5ac 2019-08-03 stsp {
8969 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8970 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8971 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8972 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8973 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8974 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8975 8b13ce36 2019-08-08 stsp
8976 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8977 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8978 8b13ce36 2019-08-08 stsp return NULL;
8979 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8980 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8981 735ef5ac 2019-08-03 stsp
8982 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8983 735ef5ac 2019-08-03 stsp if (ie == NULL)
8984 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8985 735ef5ac 2019-08-03 stsp
8986 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8987 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8988 735ef5ac 2019-08-03 stsp relpath) == -1)
8989 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8990 735ef5ac 2019-08-03 stsp
8991 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8992 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8993 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8994 735ef5ac 2019-08-03 stsp }
8995 735ef5ac 2019-08-03 stsp
8996 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8997 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8998 3aa5969e 2019-08-06 stsp goto done;
8999 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
9000 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
9001 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
9002 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9003 735ef5ac 2019-08-03 stsp goto done;
9004 3aa5969e 2019-08-06 stsp }
9005 735ef5ac 2019-08-03 stsp
9006 2db2652d 2019-08-07 stsp a->have_changes = 1;
9007 2db2652d 2019-08-07 stsp
9008 735ef5ac 2019-08-03 stsp p = in_repo_path;
9009 735ef5ac 2019-08-03 stsp while (p[0] == '/')
9010 735ef5ac 2019-08-03 stsp p++;
9011 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
9012 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
9013 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
9014 735ef5ac 2019-08-03 stsp done:
9015 735ef5ac 2019-08-03 stsp free(in_repo_path);
9016 dc424a06 2019-08-07 stsp return err;
9017 dc424a06 2019-08-07 stsp }
9018 dc424a06 2019-08-07 stsp
9019 2db2652d 2019-08-07 stsp struct stage_path_arg {
9020 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
9021 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
9022 2db2652d 2019-08-07 stsp struct got_repository *repo;
9023 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
9024 2db2652d 2019-08-07 stsp void *status_arg;
9025 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
9026 2db2652d 2019-08-07 stsp void *patch_arg;
9027 7b5dc508 2019-10-28 stsp int staged_something;
9028 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
9029 2db2652d 2019-08-07 stsp };
9030 2db2652d 2019-08-07 stsp
9031 2db2652d 2019-08-07 stsp static const struct got_error *
9032 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
9033 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
9034 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9035 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9036 0cb83759 2019-08-03 stsp {
9037 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
9038 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
9039 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
9040 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
9041 0cb83759 2019-08-03 stsp uint32_t stage;
9042 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
9043 0aeb8099 2020-07-23 stsp struct stat sb;
9044 8b13ce36 2019-08-08 stsp
9045 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
9046 8b13ce36 2019-08-08 stsp return NULL;
9047 0cb83759 2019-08-03 stsp
9048 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9049 d3e7c587 2019-08-03 stsp if (ie == NULL)
9050 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9051 0cb83759 2019-08-03 stsp
9052 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9053 2db2652d 2019-08-07 stsp relpath)== -1)
9054 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
9055 0cb83759 2019-08-03 stsp
9056 0cb83759 2019-08-03 stsp switch (status) {
9057 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
9058 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
9059 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
9060 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
9061 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
9062 0aeb8099 2020-07-23 stsp break;
9063 0aeb8099 2020-07-23 stsp }
9064 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9065 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
9066 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9067 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9068 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
9069 dc424a06 2019-08-07 stsp if (err)
9070 dc424a06 2019-08-07 stsp break;
9071 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
9072 dc424a06 2019-08-07 stsp break;
9073 dc424a06 2019-08-07 stsp } else {
9074 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
9075 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
9076 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
9077 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
9078 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
9079 dc424a06 2019-08-07 stsp break;
9080 dc424a06 2019-08-07 stsp }
9081 dc424a06 2019-08-07 stsp }
9082 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
9083 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
9084 0cb83759 2019-08-03 stsp if (err)
9085 dc424a06 2019-08-07 stsp break;
9086 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9087 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
9088 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9089 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
9090 0cb83759 2019-08-03 stsp else
9091 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
9092 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9093 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
9094 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
9095 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
9096 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
9097 35213c7c 2020-07-23 stsp ssize_t target_len;
9098 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
9099 35213c7c 2020-07-23 stsp sizeof(target_path));
9100 35213c7c 2020-07-23 stsp if (target_len == -1) {
9101 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
9102 35213c7c 2020-07-23 stsp ondisk_path);
9103 35213c7c 2020-07-23 stsp break;
9104 35213c7c 2020-07-23 stsp }
9105 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
9106 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
9107 df6221c7 2023-07-19 stsp a->worktree->root_path,
9108 df6221c7 2023-07-19 stsp a->worktree->meta_dir);
9109 35213c7c 2020-07-23 stsp if (err)
9110 35213c7c 2020-07-23 stsp break;
9111 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
9112 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
9113 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
9114 35213c7c 2020-07-23 stsp break;
9115 35213c7c 2020-07-23 stsp }
9116 35213c7c 2020-07-23 stsp }
9117 35213c7c 2020-07-23 stsp if (is_bad_symlink)
9118 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9119 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
9120 35213c7c 2020-07-23 stsp else
9121 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9122 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
9123 0aeb8099 2020-07-23 stsp } else {
9124 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
9125 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
9126 0aeb8099 2020-07-23 stsp }
9127 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9128 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9129 dc424a06 2019-08-07 stsp break;
9130 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9131 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
9132 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
9133 9fdde394 2022-06-04 op if (err)
9134 9fdde394 2022-06-04 op break;
9135 9fdde394 2022-06-04 op /*
9136 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
9137 9fdde394 2022-06-04 op * implicitly unstage the file.
9138 9fdde394 2022-06-04 op */
9139 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9140 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
9141 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
9142 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
9143 9fdde394 2022-06-04 op }
9144 0cb83759 2019-08-03 stsp break;
9145 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
9146 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
9147 d3e7c587 2019-08-03 stsp break;
9148 2db2652d 2019-08-07 stsp if (a->patch_cb) {
9149 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
9150 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
9151 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
9152 dc424a06 2019-08-07 stsp if (err)
9153 dc424a06 2019-08-07 stsp break;
9154 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9155 88f33a19 2019-08-08 stsp break;
9156 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9157 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9158 dc424a06 2019-08-07 stsp break;
9159 88f33a19 2019-08-08 stsp }
9160 dc424a06 2019-08-07 stsp }
9161 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
9162 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
9163 7b5dc508 2019-10-28 stsp a->staged_something = 1;
9164 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
9165 dc424a06 2019-08-07 stsp break;
9166 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9167 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9168 12463d8b 2019-12-13 stsp de_name);
9169 0cb83759 2019-08-03 stsp break;
9170 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
9171 d3e7c587 2019-08-03 stsp break;
9172 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
9173 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9174 ebf48fd5 2019-08-03 stsp break;
9175 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
9176 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
9177 2a06fe5f 2019-08-24 stsp break;
9178 0cb83759 2019-08-03 stsp default:
9179 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9180 537ac44b 2019-08-03 stsp break;
9181 0cb83759 2019-08-03 stsp }
9182 dc424a06 2019-08-07 stsp
9183 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
9184 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
9185 dc424a06 2019-08-07 stsp free(path_content);
9186 2db2652d 2019-08-07 stsp free(ondisk_path);
9187 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
9188 0ebf8283 2019-07-24 stsp return err;
9189 0ebf8283 2019-07-24 stsp }
9190 0cb83759 2019-08-03 stsp
9191 0cb83759 2019-08-03 stsp const struct got_error *
9192 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
9193 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
9194 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
9195 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9196 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
9197 0cb83759 2019-08-03 stsp {
9198 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9199 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9200 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9201 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9202 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9203 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9204 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9205 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9206 0cb83759 2019-08-03 stsp
9207 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9208 0cb83759 2019-08-03 stsp if (err)
9209 0cb83759 2019-08-03 stsp return err;
9210 0cb83759 2019-08-03 stsp
9211 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9212 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9213 735ef5ac 2019-08-03 stsp if (err)
9214 735ef5ac 2019-08-03 stsp goto done;
9215 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9216 735ef5ac 2019-08-03 stsp if (err)
9217 735ef5ac 2019-08-03 stsp goto done;
9218 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9219 0cb83759 2019-08-03 stsp if (err)
9220 0cb83759 2019-08-03 stsp goto done;
9221 0cb83759 2019-08-03 stsp
9222 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9223 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9224 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9225 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9226 2db2652d 2019-08-07 stsp oka.repo = repo;
9227 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9228 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9229 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9230 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
9231 735ef5ac 2019-08-03 stsp if (err)
9232 735ef5ac 2019-08-03 stsp goto done;
9233 735ef5ac 2019-08-03 stsp }
9234 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9235 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9236 2db2652d 2019-08-07 stsp goto done;
9237 2db2652d 2019-08-07 stsp }
9238 735ef5ac 2019-08-03 stsp
9239 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9240 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9241 2db2652d 2019-08-07 stsp spa.repo = repo;
9242 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9243 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9244 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9245 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9246 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9247 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9248 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9249 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9250 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
9251 42005733 2019-08-03 stsp if (err)
9252 2db2652d 2019-08-07 stsp goto done;
9253 7b5dc508 2019-10-28 stsp }
9254 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9255 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9256 7b5dc508 2019-10-28 stsp goto done;
9257 0cb83759 2019-08-03 stsp }
9258 0cb83759 2019-08-03 stsp
9259 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9260 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9261 0cb83759 2019-08-03 stsp err = sync_err;
9262 0cb83759 2019-08-03 stsp done:
9263 735ef5ac 2019-08-03 stsp if (head_ref)
9264 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9265 735ef5ac 2019-08-03 stsp free(head_commit_id);
9266 0cb83759 2019-08-03 stsp free(fileindex_path);
9267 0cb83759 2019-08-03 stsp if (fileindex)
9268 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9269 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9270 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9271 0cb83759 2019-08-03 stsp err = unlockerr;
9272 0cb83759 2019-08-03 stsp return err;
9273 0cb83759 2019-08-03 stsp }
9274 ad493afc 2019-08-03 stsp
9275 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9276 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9277 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9278 ad493afc 2019-08-03 stsp struct got_repository *repo;
9279 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9280 ad493afc 2019-08-03 stsp void *progress_arg;
9281 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9282 2e1f37b0 2019-08-08 stsp void *patch_arg;
9283 ad493afc 2019-08-03 stsp };
9284 2e1f37b0 2019-08-08 stsp
9285 2e1f37b0 2019-08-08 stsp static const struct got_error *
9286 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9287 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9288 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9289 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9290 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9291 2e1f37b0 2019-08-08 stsp {
9292 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9293 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9294 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9295 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9296 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9297 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9298 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9299 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9300 2e1f37b0 2019-08-08 stsp
9301 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9302 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9303 2e1f37b0 2019-08-08 stsp
9304 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9305 2e1f37b0 2019-08-08 stsp if (err)
9306 2e1f37b0 2019-08-08 stsp return err;
9307 eb81bc23 2022-06-28 tracey
9308 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9309 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9310 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9311 eb81bc23 2022-06-28 tracey goto done;
9312 eb81bc23 2022-06-28 tracey }
9313 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9314 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9315 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9316 eb81bc23 2022-06-28 tracey goto done;
9317 eb81bc23 2022-06-28 tracey }
9318 eb81bc23 2022-06-28 tracey
9319 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9320 2e1f37b0 2019-08-08 stsp if (err)
9321 2e1f37b0 2019-08-08 stsp goto done;
9322 2e1f37b0 2019-08-08 stsp
9323 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9324 2e1f37b0 2019-08-08 stsp if (err)
9325 2e1f37b0 2019-08-08 stsp goto done;
9326 2e1f37b0 2019-08-08 stsp
9327 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9328 2e1f37b0 2019-08-08 stsp if (err)
9329 2e1f37b0 2019-08-08 stsp goto done;
9330 2e1f37b0 2019-08-08 stsp
9331 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9332 eb81bc23 2022-06-28 tracey fd2);
9333 2e1f37b0 2019-08-08 stsp if (err)
9334 2e1f37b0 2019-08-08 stsp goto done;
9335 2e1f37b0 2019-08-08 stsp
9336 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9337 2e1f37b0 2019-08-08 stsp if (err)
9338 2e1f37b0 2019-08-08 stsp goto done;
9339 ad493afc 2019-08-03 stsp
9340 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9341 2e1f37b0 2019-08-08 stsp if (err)
9342 2e1f37b0 2019-08-08 stsp goto done;
9343 2e1f37b0 2019-08-08 stsp
9344 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9345 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9346 2e1f37b0 2019-08-08 stsp if (err)
9347 2e1f37b0 2019-08-08 stsp goto done;
9348 2e1f37b0 2019-08-08 stsp
9349 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9350 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
9351 2e1f37b0 2019-08-08 stsp if (err)
9352 2e1f37b0 2019-08-08 stsp goto done;
9353 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9354 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
9355 2e1f37b0 2019-08-08 stsp if (err)
9356 2e1f37b0 2019-08-08 stsp goto done;
9357 2e1f37b0 2019-08-08 stsp
9358 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9359 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9360 2e1f37b0 2019-08-08 stsp goto done;
9361 2e1f37b0 2019-08-08 stsp }
9362 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9363 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9364 2e1f37b0 2019-08-08 stsp goto done;
9365 2e1f37b0 2019-08-08 stsp }
9366 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9367 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9368 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
9369 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9370 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9371 fe621944 2020-11-10 stsp nchanges++;
9372 fe621944 2020-11-10 stsp }
9373 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9374 2e1f37b0 2019-08-08 stsp int choice;
9375 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9376 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9377 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9378 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9379 2e1f37b0 2019-08-08 stsp if (err)
9380 2e1f37b0 2019-08-08 stsp goto done;
9381 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9382 2e1f37b0 2019-08-08 stsp have_content = 1;
9383 19e4b907 2019-08-08 stsp else
9384 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9385 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9386 2e1f37b0 2019-08-08 stsp break;
9387 2e1f37b0 2019-08-08 stsp }
9388 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9389 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9390 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9391 2e1f37b0 2019-08-08 stsp done:
9392 2e1f37b0 2019-08-08 stsp free(label1);
9393 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9394 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9395 2e1f37b0 2019-08-08 stsp if (blob)
9396 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9397 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9398 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9399 2e1f37b0 2019-08-08 stsp if (staged_blob)
9400 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9401 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9402 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9403 fe621944 2020-11-10 stsp err = free_err;
9404 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9405 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9406 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9407 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9408 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9409 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9410 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9411 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9412 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9413 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9414 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9415 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9416 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9417 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9418 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9419 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9420 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9421 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9422 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9423 2e1f37b0 2019-08-08 stsp }
9424 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9425 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9426 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9427 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9428 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9429 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9430 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9431 2e1f37b0 2019-08-08 stsp }
9432 2e1f37b0 2019-08-08 stsp free(path1);
9433 2e1f37b0 2019-08-08 stsp free(path2);
9434 fda8017d 2020-07-23 stsp return err;
9435 fda8017d 2020-07-23 stsp }
9436 fda8017d 2020-07-23 stsp
9437 fda8017d 2020-07-23 stsp static const struct got_error *
9438 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9439 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9440 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9441 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9442 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9443 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9444 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9445 fda8017d 2020-07-23 stsp {
9446 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9447 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9448 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9449 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9450 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9451 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9452 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9453 fda8017d 2020-07-23 stsp struct stat sb;
9454 fda8017d 2020-07-23 stsp
9455 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9456 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9457 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9458 fda8017d 2020-07-23 stsp if (err)
9459 fda8017d 2020-07-23 stsp return err;
9460 fda8017d 2020-07-23 stsp
9461 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9462 fda8017d 2020-07-23 stsp return NULL;
9463 fda8017d 2020-07-23 stsp
9464 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9465 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9466 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9467 fda8017d 2020-07-23 stsp if (err)
9468 fda8017d 2020-07-23 stsp goto done;
9469 fda8017d 2020-07-23 stsp }
9470 fda8017d 2020-07-23 stsp
9471 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
9472 fda8017d 2020-07-23 stsp if (f == NULL) {
9473 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9474 fda8017d 2020-07-23 stsp path_unstaged_content);
9475 fda8017d 2020-07-23 stsp goto done;
9476 fda8017d 2020-07-23 stsp }
9477 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9478 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9479 fda8017d 2020-07-23 stsp goto done;
9480 fda8017d 2020-07-23 stsp }
9481 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9482 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9483 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9484 fda8017d 2020-07-23 stsp size_t r;
9485 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9486 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9487 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9488 fda8017d 2020-07-23 stsp goto done;
9489 fda8017d 2020-07-23 stsp }
9490 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9491 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9492 fda8017d 2020-07-23 stsp goto done;
9493 fda8017d 2020-07-23 stsp }
9494 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9495 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9496 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9497 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9498 fda8017d 2020-07-23 stsp progress_arg);
9499 fda8017d 2020-07-23 stsp } else {
9500 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9501 67a66647 2021-05-31 stsp
9502 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9503 67a66647 2021-05-31 stsp if (err)
9504 67a66647 2021-05-31 stsp return err;
9505 67a66647 2021-05-31 stsp
9506 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9507 67a66647 2021-05-31 stsp parent) == -1) {
9508 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9509 67a66647 2021-05-31 stsp base_path = NULL;
9510 67a66647 2021-05-31 stsp goto done;
9511 67a66647 2021-05-31 stsp }
9512 67a66647 2021-05-31 stsp
9513 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
9514 b90054ed 2022-11-01 stsp base_path, "");
9515 67a66647 2021-05-31 stsp if (err)
9516 67a66647 2021-05-31 stsp goto done;
9517 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9518 67a66647 2021-05-31 stsp blob_base);
9519 67a66647 2021-05-31 stsp if (err)
9520 67a66647 2021-05-31 stsp goto done;
9521 67a66647 2021-05-31 stsp
9522 5e91dae4 2022-08-30 stsp /*
9523 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9524 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9525 eec2f5a9 2021-06-03 stsp */
9526 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9527 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9528 eec2f5a9 2021-06-03 stsp ondisk_path);
9529 eec2f5a9 2021-06-03 stsp if (err)
9530 eec2f5a9 2021-06-03 stsp goto done;
9531 eec2f5a9 2021-06-03 stsp } else {
9532 eec2f5a9 2021-06-03 stsp int fd;
9533 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
9534 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9535 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9536 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9537 eec2f5a9 2021-06-03 stsp goto done;
9538 eec2f5a9 2021-06-03 stsp }
9539 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9540 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9541 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9542 eec2f5a9 2021-06-03 stsp close(fd);
9543 eec2f5a9 2021-06-03 stsp goto done;
9544 eec2f5a9 2021-06-03 stsp }
9545 eec2f5a9 2021-06-03 stsp }
9546 eec2f5a9 2021-06-03 stsp
9547 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9548 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9549 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9550 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9551 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9552 fda8017d 2020-07-23 stsp }
9553 fda8017d 2020-07-23 stsp if (err)
9554 fda8017d 2020-07-23 stsp goto done;
9555 fda8017d 2020-07-23 stsp
9556 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9557 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9558 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9559 2ac8aa02 2020-11-09 stsp } else {
9560 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9561 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9562 2ac8aa02 2020-11-09 stsp }
9563 fda8017d 2020-07-23 stsp done:
9564 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9565 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9566 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9567 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9568 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9569 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9570 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9571 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9572 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9573 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9574 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9575 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9576 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9577 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9578 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9579 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9580 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9581 67a66647 2021-05-31 stsp free(blob_base_path);
9582 67a66647 2021-05-31 stsp free(parent);
9583 67a66647 2021-05-31 stsp free(base_path);
9584 2e1f37b0 2019-08-08 stsp return err;
9585 2e1f37b0 2019-08-08 stsp }
9586 2e1f37b0 2019-08-08 stsp
9587 ad493afc 2019-08-03 stsp static const struct got_error *
9588 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9589 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9590 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9591 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9592 ad493afc 2019-08-03 stsp {
9593 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9594 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9595 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9596 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9597 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9598 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9599 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9600 9bc94a15 2019-08-03 stsp struct stat sb;
9601 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9602 ad493afc 2019-08-03 stsp
9603 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9604 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9605 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9606 2e1f37b0 2019-08-08 stsp return NULL;
9607 2e1f37b0 2019-08-08 stsp
9608 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9609 ad493afc 2019-08-03 stsp if (ie == NULL)
9610 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9611 9bc94a15 2019-08-03 stsp
9612 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9613 9bc94a15 2019-08-03 stsp == -1)
9614 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9615 ad493afc 2019-08-03 stsp
9616 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9617 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9618 f69721c3 2019-10-21 stsp if (err)
9619 f69721c3 2019-10-21 stsp goto done;
9620 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9621 f69721c3 2019-10-21 stsp id_str) == -1) {
9622 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9623 eb81bc23 2022-06-28 tracey goto done;
9624 eb81bc23 2022-06-28 tracey }
9625 eb81bc23 2022-06-28 tracey
9626 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9627 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9628 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9629 eb81bc23 2022-06-28 tracey goto done;
9630 eb81bc23 2022-06-28 tracey }
9631 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9632 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9633 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9634 f69721c3 2019-10-21 stsp goto done;
9635 f69721c3 2019-10-21 stsp }
9636 f69721c3 2019-10-21 stsp
9637 ad493afc 2019-08-03 stsp switch (staged_status) {
9638 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9639 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9640 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9641 ad493afc 2019-08-03 stsp if (err)
9642 ad493afc 2019-08-03 stsp break;
9643 ad493afc 2019-08-03 stsp /* fall through */
9644 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9645 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9646 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9647 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9648 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9649 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9650 2e1f37b0 2019-08-08 stsp if (err)
9651 2e1f37b0 2019-08-08 stsp break;
9652 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9653 2e1f37b0 2019-08-08 stsp break;
9654 2e1f37b0 2019-08-08 stsp } else {
9655 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9656 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9657 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9658 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9659 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9660 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9661 2e1f37b0 2019-08-08 stsp }
9662 2e1f37b0 2019-08-08 stsp }
9663 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9664 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9665 ad493afc 2019-08-03 stsp if (err)
9666 ad493afc 2019-08-03 stsp break;
9667 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9668 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9669 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9670 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9671 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9672 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9673 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9674 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9675 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9676 ea7786be 2020-07-23 stsp break;
9677 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9678 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9679 36bf999c 2020-07-23 stsp char *staged_target;
9680 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9681 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9682 36bf999c 2020-07-23 stsp if (err)
9683 36bf999c 2020-07-23 stsp goto done;
9684 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9685 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9686 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9687 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9688 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9689 36bf999c 2020-07-23 stsp free(staged_target);
9690 dfe9fba0 2020-07-23 stsp } else {
9691 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9692 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9693 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9694 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9695 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9696 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9697 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9698 dfe9fba0 2020-07-23 stsp }
9699 ea7786be 2020-07-23 stsp break;
9700 ea7786be 2020-07-23 stsp default:
9701 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9702 ea7786be 2020-07-23 stsp break;
9703 ea7786be 2020-07-23 stsp }
9704 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9705 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9706 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9707 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9708 2ac8aa02 2020-11-09 stsp }
9709 ad493afc 2019-08-03 stsp break;
9710 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9711 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9712 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9713 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9714 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9715 2e1f37b0 2019-08-08 stsp if (err)
9716 2e1f37b0 2019-08-08 stsp break;
9717 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9718 2e1f37b0 2019-08-08 stsp break;
9719 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9720 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9721 2e1f37b0 2019-08-08 stsp break;
9722 2e1f37b0 2019-08-08 stsp }
9723 2e1f37b0 2019-08-08 stsp }
9724 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9725 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9726 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9727 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9728 9bc94a15 2019-08-03 stsp if (err)
9729 9bc94a15 2019-08-03 stsp break;
9730 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9731 ad493afc 2019-08-03 stsp break;
9732 ad493afc 2019-08-03 stsp }
9733 f69721c3 2019-10-21 stsp done:
9734 ad493afc 2019-08-03 stsp free(ondisk_path);
9735 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9736 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9737 ad493afc 2019-08-03 stsp if (blob_base)
9738 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9739 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9740 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9741 ad493afc 2019-08-03 stsp if (blob_staged)
9742 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9743 f69721c3 2019-10-21 stsp free(id_str);
9744 f69721c3 2019-10-21 stsp free(label_orig);
9745 ad493afc 2019-08-03 stsp return err;
9746 ad493afc 2019-08-03 stsp }
9747 ad493afc 2019-08-03 stsp
9748 ad493afc 2019-08-03 stsp const struct got_error *
9749 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9750 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9751 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9752 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9753 ad493afc 2019-08-03 stsp struct got_repository *repo)
9754 ad493afc 2019-08-03 stsp {
9755 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9756 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9757 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9758 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9759 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9760 ad493afc 2019-08-03 stsp
9761 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9762 ad493afc 2019-08-03 stsp if (err)
9763 ad493afc 2019-08-03 stsp return err;
9764 ad493afc 2019-08-03 stsp
9765 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9766 ad493afc 2019-08-03 stsp if (err)
9767 ad493afc 2019-08-03 stsp goto done;
9768 ad493afc 2019-08-03 stsp
9769 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9770 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9771 ad493afc 2019-08-03 stsp upa.repo = repo;
9772 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9773 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9774 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9775 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9776 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9777 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9778 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9779 ad493afc 2019-08-03 stsp if (err)
9780 ad493afc 2019-08-03 stsp goto done;
9781 ad493afc 2019-08-03 stsp }
9782 ad493afc 2019-08-03 stsp
9783 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9784 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9785 ad493afc 2019-08-03 stsp err = sync_err;
9786 ad493afc 2019-08-03 stsp done:
9787 ad493afc 2019-08-03 stsp free(fileindex_path);
9788 ad493afc 2019-08-03 stsp if (fileindex)
9789 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9790 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9791 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9792 ad493afc 2019-08-03 stsp err = unlockerr;
9793 ad493afc 2019-08-03 stsp return err;
9794 ad493afc 2019-08-03 stsp }
9795 b2118c49 2020-07-28 stsp
9796 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9797 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9798 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9799 b2118c49 2020-07-28 stsp void *info_arg;
9800 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9801 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9802 b2118c49 2020-07-28 stsp void *cancel_arg;
9803 b2118c49 2020-07-28 stsp };
9804 b2118c49 2020-07-28 stsp
9805 b2118c49 2020-07-28 stsp static const struct got_error *
9806 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9807 b2118c49 2020-07-28 stsp {
9808 f6b8c3c2 2023-07-24 stsp const struct got_error *err;
9809 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9810 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9811 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9812 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9813 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9814 b2118c49 2020-07-28 stsp int stage;
9815 b2118c49 2020-07-28 stsp
9816 f6b8c3c2 2023-07-24 stsp if (a->cancel_cb) {
9817 f6b8c3c2 2023-07-24 stsp err = a->cancel_cb(a->cancel_arg);
9818 f6b8c3c2 2023-07-24 stsp if (err)
9819 f6b8c3c2 2023-07-24 stsp return err;
9820 f6b8c3c2 2023-07-24 stsp }
9821 b2118c49 2020-07-28 stsp
9822 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9823 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9824 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9825 b2118c49 2020-07-28 stsp break;
9826 b2118c49 2020-07-28 stsp }
9827 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9828 b2118c49 2020-07-28 stsp return NULL;
9829 b2118c49 2020-07-28 stsp
9830 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9831 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9832 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9833 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9834 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9835 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9836 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9837 b2118c49 2020-07-28 stsp }
9838 b2118c49 2020-07-28 stsp
9839 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9840 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9841 b2118c49 2020-07-28 stsp
9842 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9843 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9844 b2118c49 2020-07-28 stsp }
9845 b2118c49 2020-07-28 stsp
9846 b2118c49 2020-07-28 stsp const struct got_error *
9847 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9848 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9849 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9850 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9851 b2118c49 2020-07-28 stsp
9852 b2118c49 2020-07-28 stsp {
9853 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9854 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9855 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9856 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9857 b2118c49 2020-07-28 stsp
9858 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9859 b2118c49 2020-07-28 stsp if (err)
9860 b2118c49 2020-07-28 stsp return err;
9861 b2118c49 2020-07-28 stsp
9862 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9863 b2118c49 2020-07-28 stsp if (err)
9864 b2118c49 2020-07-28 stsp goto done;
9865 b2118c49 2020-07-28 stsp
9866 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9867 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9868 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9869 b2118c49 2020-07-28 stsp arg.paths = paths;
9870 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9871 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9872 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9873 b2118c49 2020-07-28 stsp &arg);
9874 b2118c49 2020-07-28 stsp done:
9875 b2118c49 2020-07-28 stsp free(fileindex_path);
9876 b2118c49 2020-07-28 stsp if (fileindex)
9877 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9878 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9879 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9880 b2118c49 2020-07-28 stsp err = unlockerr;
9881 b2118c49 2020-07-28 stsp return err;
9882 b2118c49 2020-07-28 stsp }
9883 78f5ac24 2022-03-19 op
9884 78f5ac24 2022-03-19 op static const struct got_error *
9885 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9886 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9887 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9888 78f5ac24 2022-03-19 op {
9889 78f5ac24 2022-03-19 op const struct got_error *err;
9890 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9891 78f5ac24 2022-03-19 op struct stat sb;
9892 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9893 78f5ac24 2022-03-19 op
9894 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9895 78f5ac24 2022-03-19 op if (err)
9896 78f5ac24 2022-03-19 op return err;
9897 78f5ac24 2022-03-19 op
9898 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9899 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9900 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9901 78f5ac24 2022-03-19 op
9902 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9903 78f5ac24 2022-03-19 op if (ie) {
9904 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9905 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9906 a05fb460 2022-04-23 op repo);
9907 78f5ac24 2022-03-19 op if (err)
9908 78f5ac24 2022-03-19 op goto done;
9909 78f5ac24 2022-03-19 op } else {
9910 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9911 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9912 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9913 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9914 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9915 78f5ac24 2022-03-19 op ondisk_path);
9916 78f5ac24 2022-03-19 op goto done;
9917 78f5ac24 2022-03-19 op }
9918 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9919 78f5ac24 2022-03-19 op }
9920 78f5ac24 2022-03-19 op }
9921 78f5ac24 2022-03-19 op
9922 78f5ac24 2022-03-19 op done:
9923 78f5ac24 2022-03-19 op free(ondisk_path);
9924 78f5ac24 2022-03-19 op return err;
9925 78f5ac24 2022-03-19 op }
9926 78f5ac24 2022-03-19 op
9927 78f5ac24 2022-03-19 op static const struct got_error *
9928 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9929 78f5ac24 2022-03-19 op unsigned char staged_status)
9930 78f5ac24 2022-03-19 op {
9931 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9932 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9933 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9934 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9935 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9936 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9937 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9938 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9939 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9940 78f5ac24 2022-03-19 op return NULL;
9941 78f5ac24 2022-03-19 op }
9942 78f5ac24 2022-03-19 op
9943 78f5ac24 2022-03-19 op static const struct got_error *
9944 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9945 78f5ac24 2022-03-19 op {
9946 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9947 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9948 78f5ac24 2022-03-19 op return NULL;
9949 78f5ac24 2022-03-19 op }
9950 78f5ac24 2022-03-19 op
9951 78f5ac24 2022-03-19 op static const struct got_error *
9952 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9953 78f5ac24 2022-03-19 op unsigned char staged_status)
9954 78f5ac24 2022-03-19 op {
9955 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9956 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9957 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9958 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9959 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9960 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9961 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9962 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9963 78f5ac24 2022-03-19 op return NULL;
9964 78f5ac24 2022-03-19 op }
9965 78f5ac24 2022-03-19 op
9966 78f5ac24 2022-03-19 op const struct got_error *
9967 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9968 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9969 78f5ac24 2022-03-19 op {
9970 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9971 78f5ac24 2022-03-19 op }
9972 78f5ac24 2022-03-19 op
9973 78f5ac24 2022-03-19 op const struct got_error *
9974 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9975 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9976 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9977 78f5ac24 2022-03-19 op {
9978 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9979 78f5ac24 2022-03-19 op int file_renamed = 0;
9980 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9981 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9982 78f5ac24 2022-03-19 op
9983 78f5ac24 2022-03-19 op *oldpath = NULL;
9984 78f5ac24 2022-03-19 op *newpath = NULL;
9985 78f5ac24 2022-03-19 op
9986 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9987 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9988 78f5ac24 2022-03-19 op if (err)
9989 78f5ac24 2022-03-19 op goto done;
9990 78f5ac24 2022-03-19 op
9991 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9992 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9993 78f5ac24 2022-03-19 op if (err)
9994 78f5ac24 2022-03-19 op goto done;
9995 78f5ac24 2022-03-19 op
9996 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9997 78f5ac24 2022-03-19 op file_renamed = 1;
9998 78f5ac24 2022-03-19 op
9999 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
10000 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
10001 78f5ac24 2022-03-19 op else if (file_renamed) {
10002 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
10003 78f5ac24 2022-03-19 op if (err == NULL)
10004 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
10005 78f5ac24 2022-03-19 op } else if (old == NULL)
10006 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
10007 78f5ac24 2022-03-19 op else
10008 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
10009 78f5ac24 2022-03-19 op
10010 78f5ac24 2022-03-19 op done:
10011 78f5ac24 2022-03-19 op if (err) {
10012 78f5ac24 2022-03-19 op free(*oldpath);
10013 78f5ac24 2022-03-19 op *oldpath = NULL;
10014 78f5ac24 2022-03-19 op free(*newpath);
10015 78f5ac24 2022-03-19 op *newpath = NULL;
10016 78f5ac24 2022-03-19 op }
10017 78f5ac24 2022-03-19 op return err;
10018 78f5ac24 2022-03-19 op }
10019 78f5ac24 2022-03-19 op
10020 78f5ac24 2022-03-19 op const struct got_error *
10021 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10022 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
10023 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
10024 78f5ac24 2022-03-19 op {
10025 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
10026 f2dd7807 2022-05-17 op
10027 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
10028 f2dd7807 2022-05-17 op saa.worktree = worktree;
10029 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
10030 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
10031 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
10032 f2dd7807 2022-05-17 op saa.repo = repo;
10033 f2dd7807 2022-05-17 op
10034 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
10035 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
10036 78f5ac24 2022-03-19 op }
10037 f2dd7807 2022-05-17 op
10038 f2dd7807 2022-05-17 op const struct got_error *
10039 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10040 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
10041 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
10042 f2dd7807 2022-05-17 op {
10043 7f4e5320 2023-05-29 stsp const struct got_error *err;
10044 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
10045 7f4e5320 2023-05-29 stsp char *ondisk_status_path;
10046 f2dd7807 2022-05-17 op
10047 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
10048 f2dd7807 2022-05-17 op sda.worktree = worktree;
10049 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
10050 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
10051 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
10052 f2dd7807 2022-05-17 op sda.repo = repo;
10053 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
10054 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
10055 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
10056 f2dd7807 2022-05-17 op sda.status_codes = NULL;
10057 7f4e5320 2023-05-29 stsp if (asprintf(&ondisk_status_path, "%s/%s",
10058 7f4e5320 2023-05-29 stsp got_worktree_get_root_path(worktree), path) == -1)
10059 7f4e5320 2023-05-29 stsp return got_error_from_errno("asprintf");
10060 7f4e5320 2023-05-29 stsp sda.status_path = ondisk_status_path;
10061 7f4e5320 2023-05-29 stsp sda.status_path_len = strlen(ondisk_status_path);
10062 7f4e5320 2023-05-29 stsp
10063 7f4e5320 2023-05-29 stsp err = worktree_status(worktree, path, fileindex, repo,
10064 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10065 7f4e5320 2023-05-29 stsp free(ondisk_status_path);
10066 7f4e5320 2023-05-29 stsp return err;
10067 f2dd7807 2022-05-17 op }
10068 f2dd7807 2022-05-17 op
10069 f2dd7807 2022-05-17 op const struct got_error *
10070 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
10071 4bcdc895 2022-05-17 op const char *fileindex_path)
10072 f2dd7807 2022-05-17 op {
10073 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
10074 f2dd7807 2022-05-17 op
10075 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
10076 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
10077 f2dd7807 2022-05-17 op
10078 f2dd7807 2022-05-17 op return err;
10079 f2dd7807 2022-05-17 op }