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 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 86c3caaf 2018-03-09 stsp
20 d1f6d47b 2019-02-04 stsp #include <dirent.h>
21 12ce7a6c 2019-08-12 stsp #include <limits.h>
22 f5c49f82 2019-01-06 stsp #include <stddef.h>
23 86c3caaf 2018-03-09 stsp #include <string.h>
24 86c3caaf 2018-03-09 stsp #include <stdio.h>
25 86c3caaf 2018-03-09 stsp #include <stdlib.h>
26 303e14b5 2019-09-23 stsp #include <time.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <zlib.h>
31 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
32 512f0d0e 2019-01-02 stsp #include <libgen.h>
33 dd038bc6 2021-09-21 thomas.ad
34 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
35 86c3caaf 2018-03-09 stsp
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
57 9d31a1d8 2018-03-11 stsp
58 9d31a1d8 2018-03-11 stsp #ifndef MIN
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 9d31a1d8 2018-03-11 stsp #endif
61 f69721c3 2019-10-21 stsp
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 a2c162eb 2022-10-30 thomas
65 a2c162eb 2022-10-30 thomas static mode_t apply_umask(mode_t);
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
118 09fe317a 2018-03-11 stsp }
119 09fe317a 2018-03-11 stsp
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
122 024e9686 2019-05-14 stsp {
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
125 024e9686 2019-05-14 stsp
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
130 024e9686 2019-05-14 stsp } else {
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
134 024e9686 2019-05-14 stsp }
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
138 024e9686 2019-05-14 stsp }
139 024e9686 2019-05-14 stsp
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
143 86c3caaf 2018-03-09 stsp {
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
154 65596e15 2018-12-24 stsp
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
157 0c48fee2 2019-03-11 stsp goto done;
158 0c48fee2 2019-03-11 stsp }
159 0c48fee2 2019-03-11 stsp
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
168 86c3caaf 2018-03-09 stsp
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
172 0bb8a95e 2018-03-12 stsp }
173 577ec78f 2018-03-11 stsp
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
183 86c3caaf 2018-03-09 stsp goto done;
184 86c3caaf 2018-03-09 stsp }
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
187 86c3caaf 2018-03-09 stsp goto done;
188 86c3caaf 2018-03-09 stsp }
189 86c3caaf 2018-03-09 stsp
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 056e7441 2018-03-11 stsp if (err)
193 056e7441 2018-03-11 stsp goto done;
194 056e7441 2018-03-11 stsp
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 99724ed4 2018-03-10 stsp if (err)
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
202 65596e15 2018-12-24 stsp if (err)
203 65596e15 2018-12-24 stsp goto done;
204 65596e15 2018-12-24 stsp
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp goto done;
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 99724ed4 2018-03-10 stsp if (err)
211 86c3caaf 2018-03-09 stsp goto done;
212 86c3caaf 2018-03-09 stsp
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
216 99724ed4 2018-03-10 stsp if (err)
217 86c3caaf 2018-03-09 stsp goto done;
218 86c3caaf 2018-03-09 stsp
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
222 ec22038e 2019-03-10 stsp if (err)
223 ec22038e 2019-03-10 stsp goto done;
224 ec22038e 2019-03-10 stsp
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
229 ec22038e 2019-03-10 stsp goto done;
230 ec22038e 2019-03-10 stsp }
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp }
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 577ec78f 2018-03-11 stsp if (err)
238 577ec78f 2018-03-11 stsp goto done;
239 577ec78f 2018-03-11 stsp
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 1451e70d 2018-03-10 stsp goto done;
244 1451e70d 2018-03-10 stsp }
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 99724ed4 2018-03-10 stsp if (err)
247 1451e70d 2018-03-10 stsp goto done;
248 1451e70d 2018-03-10 stsp
249 86c3caaf 2018-03-09 stsp done:
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
257 86c3caaf 2018-03-09 stsp }
258 86c3caaf 2018-03-09 stsp
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
262 e5dc7198 2018-12-29 stsp {
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
264 e5dc7198 2018-12-29 stsp
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
268 e5dc7198 2018-12-29 stsp }
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
273 86c3caaf 2018-03-09 stsp }
274 86c3caaf 2018-03-09 stsp
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
277 86c3caaf 2018-03-09 stsp {
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
279 024e9686 2019-05-14 stsp }
280 024e9686 2019-05-14 stsp
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
284 024e9686 2019-05-14 stsp {
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
287 024e9686 2019-05-14 stsp
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
292 024e9686 2019-05-14 stsp goto done;
293 024e9686 2019-05-14 stsp }
294 024e9686 2019-05-14 stsp
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
298 024e9686 2019-05-14 stsp goto done;
299 024e9686 2019-05-14 stsp }
300 024e9686 2019-05-14 stsp
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
302 024e9686 2019-05-14 stsp if (err)
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
307 024e9686 2019-05-14 stsp done:
308 024e9686 2019-05-14 stsp free(path_got);
309 024e9686 2019-05-14 stsp if (err)
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
312 507dc3bb 2018-12-29 stsp }
313 507dc3bb 2018-12-29 stsp
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
316 507dc3bb 2018-12-29 stsp {
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
318 86c3caaf 2018-03-09 stsp }
319 86c3caaf 2018-03-09 stsp
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
323 507dc3bb 2018-12-29 stsp {
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
328 507dc3bb 2018-12-29 stsp
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
333 507dc3bb 2018-12-29 stsp goto done;
334 507dc3bb 2018-12-29 stsp }
335 507dc3bb 2018-12-29 stsp
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
337 507dc3bb 2018-12-29 stsp if (err)
338 507dc3bb 2018-12-29 stsp return err;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 507dc3bb 2018-12-29 stsp goto done;
343 507dc3bb 2018-12-29 stsp }
344 507dc3bb 2018-12-29 stsp
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
347 507dc3bb 2018-12-29 stsp if (err)
348 507dc3bb 2018-12-29 stsp goto done;
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
357 507dc3bb 2018-12-29 stsp goto done;
358 507dc3bb 2018-12-29 stsp }
359 507dc3bb 2018-12-29 stsp done:
360 507dc3bb 2018-12-29 stsp if (obj)
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
365 50b0790e 2020-09-11 stsp }
366 50b0790e 2020-09-11 stsp
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
369 50b0790e 2020-09-11 stsp {
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
371 507dc3bb 2018-12-29 stsp }
372 507dc3bb 2018-12-29 stsp
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
375 86c3caaf 2018-03-09 stsp {
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
381 21908da4 2019-01-13 stsp }
382 21908da4 2019-01-13 stsp
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
385 4a1ddfc2 2019-01-12 stsp {
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
388 4a1ddfc2 2019-01-12 stsp
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
391 4a1ddfc2 2019-01-12 stsp
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 ddcd8544 2019-03-11 stsp }
402 ddcd8544 2019-03-11 stsp }
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
405 68c76935 2019-02-19 stsp }
406 68c76935 2019-02-19 stsp
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
409 68c76935 2019-02-19 stsp {
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
414 68c76935 2019-02-19 stsp
415 68c76935 2019-02-19 stsp *same = 1;
416 68c76935 2019-02-19 stsp
417 230a42bd 2019-05-11 jcs for (;;) {
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
421 68c76935 2019-02-19 stsp break;
422 68c76935 2019-02-19 stsp }
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp break;
427 68c76935 2019-02-19 stsp }
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
430 68c76935 2019-02-19 stsp *same = 0;
431 68c76935 2019-02-19 stsp break;
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
434 68c76935 2019-02-19 stsp *same = 0;
435 68c76935 2019-02-19 stsp break;
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 68c76935 2019-02-19 stsp *same = 0;
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp } else {
442 68c76935 2019-02-19 stsp *same = 0;
443 68c76935 2019-02-19 stsp break;
444 68c76935 2019-02-19 stsp }
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp
447 68c76935 2019-02-19 stsp return err;
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
452 68c76935 2019-02-19 stsp {
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
455 68c76935 2019-02-19 stsp
456 68c76935 2019-02-19 stsp *same = 1;
457 68c76935 2019-02-19 stsp
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
461 68c76935 2019-02-19 stsp
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
465 68c76935 2019-02-19 stsp
466 68c76935 2019-02-19 stsp if (size1 != size2) {
467 68c76935 2019-02-19 stsp *same = 0;
468 68c76935 2019-02-19 stsp return NULL;
469 68c76935 2019-02-19 stsp }
470 68c76935 2019-02-19 stsp
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
477 24f136e0 2021-11-20 thomas }
478 24f136e0 2021-11-20 thomas
479 24f136e0 2021-11-20 thomas static const struct got_error *
480 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
481 24f136e0 2021-11-20 thomas {
482 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
483 24f136e0 2021-11-20 thomas size_t flen;
484 24f136e0 2021-11-20 thomas ssize_t outlen;
485 24f136e0 2021-11-20 thomas
486 24f136e0 2021-11-20 thomas *outsize = 0;
487 24f136e0 2021-11-20 thomas
488 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
489 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
490 24f136e0 2021-11-20 thomas
491 24f136e0 2021-11-20 thomas for (;;) {
492 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
493 24f136e0 2021-11-20 thomas if (flen == 0) {
494 24f136e0 2021-11-20 thomas if (ferror(f))
495 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
496 24f136e0 2021-11-20 thomas if (feof(f))
497 24f136e0 2021-11-20 thomas break;
498 24f136e0 2021-11-20 thomas }
499 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
500 24f136e0 2021-11-20 thomas if (outlen == -1)
501 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
502 24f136e0 2021-11-20 thomas if (outlen != flen)
503 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
504 24f136e0 2021-11-20 thomas *outsize += outlen;
505 24f136e0 2021-11-20 thomas }
506 24f136e0 2021-11-20 thomas
507 24f136e0 2021-11-20 thomas return NULL;
508 24f136e0 2021-11-20 thomas }
509 24f136e0 2021-11-20 thomas
510 24f136e0 2021-11-20 thomas static const struct got_error *
511 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
512 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 24f136e0 2021-11-20 thomas const char *ondisk_path)
515 24f136e0 2021-11-20 thomas {
516 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
517 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
518 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
522 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
523 24f136e0 2021-11-20 thomas
524 24f136e0 2021-11-20 thomas *overlapcnt = 0;
525 24f136e0 2021-11-20 thomas
526 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 24f136e0 2021-11-20 thomas if (err)
528 24f136e0 2021-11-20 thomas return err;
529 24f136e0 2021-11-20 thomas
530 24f136e0 2021-11-20 thomas if (same_content)
531 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
532 24f136e0 2021-11-20 thomas
533 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
534 24f136e0 2021-11-20 thomas if (err)
535 24f136e0 2021-11-20 thomas return err;
536 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
537 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
538 24f136e0 2021-11-20 thomas if (err)
539 24f136e0 2021-11-20 thomas return err;
540 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
541 24f136e0 2021-11-20 thomas
542 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
543 24f136e0 2021-11-20 thomas *overlapcnt = 1;
544 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
546 24f136e0 2021-11-20 thomas goto done;
547 24f136e0 2021-11-20 thomas }
548 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
550 24f136e0 2021-11-20 thomas goto done;
551 24f136e0 2021-11-20 thomas }
552 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
554 24f136e0 2021-11-20 thomas goto done;
555 24f136e0 2021-11-20 thomas }
556 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 fc2a50f2 2022-11-01 thomas base_path_orig, "");
558 24f136e0 2021-11-20 thomas if (err)
559 24f136e0 2021-11-20 thomas goto done;
560 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
562 24f136e0 2021-11-20 thomas if (err)
563 24f136e0 2021-11-20 thomas goto done;
564 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
566 24f136e0 2021-11-20 thomas if (err)
567 24f136e0 2021-11-20 thomas goto done;
568 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 24f136e0 2021-11-20 thomas if (err)
570 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 24f136e0 2021-11-20 thomas if (err)
573 24f136e0 2021-11-20 thomas goto done;
574 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 24f136e0 2021-11-20 thomas if (err)
576 24f136e0 2021-11-20 thomas goto done;
577 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
578 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
579 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
580 24f136e0 2021-11-20 thomas goto done;
581 24f136e0 2021-11-20 thomas }
582 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
585 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
586 24f136e0 2021-11-20 thomas path_deriv) < 0) {
587 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas }
590 24f136e0 2021-11-20 thomas if (size_orig > 0) {
591 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
593 24f136e0 2021-11-20 thomas label_orig ? " " : "",
594 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
595 24f136e0 2021-11-20 thomas path_orig) < 0) {
596 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
597 24f136e0 2021-11-20 thomas goto done;
598 24f136e0 2021-11-20 thomas }
599 24f136e0 2021-11-20 thomas }
600 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
602 24f136e0 2021-11-20 thomas path_deriv2,
603 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
604 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
605 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
606 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
607 24f136e0 2021-11-20 thomas goto done;
608 24f136e0 2021-11-20 thomas }
609 24f136e0 2021-11-20 thomas } else if (changed_deriv)
610 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 24f136e0 2021-11-20 thomas else if (changed_deriv2)
612 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 24f136e0 2021-11-20 thomas done:
614 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 24f136e0 2021-11-20 thomas err == NULL)
616 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
617 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
619 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
621 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
623 24f136e0 2021-11-20 thomas free(path_orig);
624 24f136e0 2021-11-20 thomas free(path_deriv);
625 24f136e0 2021-11-20 thomas free(path_deriv2);
626 24f136e0 2021-11-20 thomas free(base_path_orig);
627 24f136e0 2021-11-20 thomas free(base_path_deriv);
628 24f136e0 2021-11-20 thomas free(base_path_deriv2);
629 24f136e0 2021-11-20 thomas return err;
630 6353ad76 2019-02-08 stsp }
631 6353ad76 2019-02-08 stsp
632 6353ad76 2019-02-08 stsp /*
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
638 6353ad76 2019-02-08 stsp */
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
646 6353ad76 2019-02-08 stsp {
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
653 6353ad76 2019-02-08 stsp
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
655 234035bc 2019-06-01 stsp
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
657 3524bbf9 2020-10-20 stsp if (err)
658 3524bbf9 2020-10-20 stsp return err;
659 af54ae4a 2019-02-19 stsp
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
662 3524bbf9 2020-10-20 stsp goto done;
663 3524bbf9 2020-10-20 stsp }
664 af54ae4a 2019-02-19 stsp
665 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
666 6353ad76 2019-02-08 stsp if (err)
667 6353ad76 2019-02-08 stsp goto done;
668 3b9f0f87 2020-07-23 stsp
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 24f136e0 2021-11-20 thomas if (err) {
672 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
673 24f136e0 2021-11-20 thomas goto done;
674 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 24f136e0 2021-11-20 thomas ondisk_path);
677 24f136e0 2021-11-20 thomas if (err)
678 24f136e0 2021-11-20 thomas goto done;
679 24f136e0 2021-11-20 thomas }
680 6353ad76 2019-02-08 stsp
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 1ee397ad 2019-07-12 stsp if (err)
684 1ee397ad 2019-07-12 stsp goto done;
685 6353ad76 2019-02-08 stsp
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
688 816dc654 2019-02-16 stsp goto done;
689 68c76935 2019-02-19 stsp }
690 68c76935 2019-02-19 stsp
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
694 db590691 2021-06-02 stsp goto done;
695 db590691 2021-06-02 stsp }
696 db590691 2021-06-02 stsp merged_fd = -1;
697 db590691 2021-06-02 stsp
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
701 db590691 2021-06-02 stsp f_merged);
702 68c76935 2019-02-19 stsp if (err)
703 68c76935 2019-02-19 stsp goto done;
704 816dc654 2019-02-16 stsp }
705 6353ad76 2019-02-08 stsp
706 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
708 70a0c8ec 2019-02-20 stsp goto done;
709 70a0c8ec 2019-02-20 stsp }
710 70a0c8ec 2019-02-20 stsp
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
714 6353ad76 2019-02-08 stsp goto done;
715 6353ad76 2019-02-08 stsp }
716 6353ad76 2019-02-08 stsp done:
717 fdcb7daf 2019-12-15 stsp if (err) {
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
720 3b9f0f87 2020-07-23 stsp }
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
729 af57b12a 2020-07-23 stsp }
730 af57b12a 2020-07-23 stsp
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
734 af57b12a 2020-07-23 stsp {
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
742 af57b12a 2020-07-23 stsp }
743 af57b12a 2020-07-23 stsp
744 af57b12a 2020-07-23 stsp /*
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
749 993e2a1b 2020-07-23 stsp *
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
755 11cc08c1 2020-07-23 stsp */
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
760 11cc08c1 2020-07-23 stsp {
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
764 11cc08c1 2020-07-23 stsp
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
766 11cc08c1 2020-07-23 stsp if (err)
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
768 11cc08c1 2020-07-23 stsp
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp goto done;
773 11cc08c1 2020-07-23 stsp }
774 11cc08c1 2020-07-23 stsp
775 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
776 11cc08c1 2020-07-23 stsp if (err)
777 3818e3c4 2020-11-01 naddy goto done;
778 3818e3c4 2020-11-01 naddy
779 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
781 11cc08c1 2020-07-23 stsp goto done;
782 3818e3c4 2020-11-01 naddy }
783 11cc08c1 2020-07-23 stsp
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
794 11cc08c1 2020-07-23 stsp goto done;
795 11cc08c1 2020-07-23 stsp }
796 11cc08c1 2020-07-23 stsp
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
799 11cc08c1 2020-07-23 stsp goto done;
800 11cc08c1 2020-07-23 stsp }
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
803 11cc08c1 2020-07-23 stsp goto done;
804 11cc08c1 2020-07-23 stsp }
805 11cc08c1 2020-07-23 stsp done:
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
812 11cc08c1 2020-07-23 stsp }
813 d219f183 2020-07-23 stsp
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
820 11cc08c1 2020-07-23 stsp
821 11cc08c1 2020-07-23 stsp /*
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
826 af57b12a 2020-07-23 stsp */
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
833 af57b12a 2020-07-23 stsp {
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
841 af57b12a 2020-07-23 stsp
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
850 af57b12a 2020-07-23 stsp goto done;
851 af57b12a 2020-07-23 stsp }
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
853 af57b12a 2020-07-23 stsp
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 526a746f 2020-07-23 stsp if (err)
857 526a746f 2020-07-23 stsp goto done;
858 526a746f 2020-07-23 stsp }
859 af57b12a 2020-07-23 stsp
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
864 11cc08c1 2020-07-23 stsp
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 11cc08c1 2020-07-23 stsp path);
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp path);
881 11cc08c1 2020-07-23 stsp } else {
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
886 11cc08c1 2020-07-23 stsp if (err)
887 11cc08c1 2020-07-23 stsp goto done;
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 11cc08c1 2020-07-23 stsp path);
890 11cc08c1 2020-07-23 stsp }
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
895 af57b12a 2020-07-23 stsp if (err)
896 af57b12a 2020-07-23 stsp goto done;
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 ea7786be 2020-07-23 stsp path);
904 ea7786be 2020-07-23 stsp } else {
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
908 ea7786be 2020-07-23 stsp if (err)
909 ea7786be 2020-07-23 stsp goto done;
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 ea7786be 2020-07-23 stsp path);
912 ea7786be 2020-07-23 stsp }
913 6353ad76 2019-02-08 stsp }
914 11cc08c1 2020-07-23 stsp
915 af57b12a 2020-07-23 stsp done:
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
918 eec2f5a9 2021-06-03 stsp }
919 eec2f5a9 2021-06-03 stsp
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
922 eec2f5a9 2021-06-03 stsp {
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
926 eec2f5a9 2021-06-03 stsp size_t n;
927 eec2f5a9 2021-06-03 stsp FILE *f;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
937 eec2f5a9 2021-06-03 stsp goto done;
938 eec2f5a9 2021-06-03 stsp }
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
942 eec2f5a9 2021-06-03 stsp goto done;
943 eec2f5a9 2021-06-03 stsp }
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
946 eec2f5a9 2021-06-03 stsp goto done;
947 eec2f5a9 2021-06-03 stsp }
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
950 eec2f5a9 2021-06-03 stsp goto done;
951 eec2f5a9 2021-06-03 stsp }
952 eec2f5a9 2021-06-03 stsp done:
953 eec2f5a9 2021-06-03 stsp if (err)
954 eec2f5a9 2021-06-03 stsp fclose(f);
955 eec2f5a9 2021-06-03 stsp else
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
958 14c901f1 2019-08-08 stsp }
959 14c901f1 2019-08-08 stsp
960 14c901f1 2019-08-08 stsp /*
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
964 14c901f1 2019-08-08 stsp */
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
972 14c901f1 2019-08-08 stsp {
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
978 14c901f1 2019-08-08 stsp
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
980 14c901f1 2019-08-08 stsp
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
982 ed6b5030 2020-10-20 stsp if (err)
983 ed6b5030 2020-10-20 stsp return err;
984 14c901f1 2019-08-08 stsp
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
990 67a66647 2021-05-31 stsp goto done;
991 67a66647 2021-05-31 stsp }
992 67a66647 2021-05-31 stsp
993 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
994 fc2a50f2 2022-11-01 thomas base_path, "");
995 67a66647 2021-05-31 stsp if (err)
996 67a66647 2021-05-31 stsp goto done;
997 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
998 67a66647 2021-05-31 stsp blob_orig);
999 67a66647 2021-05-31 stsp if (err)
1000 67a66647 2021-05-31 stsp goto done;
1001 67a66647 2021-05-31 stsp free(base_path);
1002 db590691 2021-06-02 stsp } else {
1003 db590691 2021-06-02 stsp /*
1004 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1005 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1006 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1007 db590691 2021-06-02 stsp */
1008 db590691 2021-06-02 stsp f_orig = got_opentemp();
1009 db590691 2021-06-02 stsp if (f_orig == NULL) {
1010 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1011 db590691 2021-06-02 stsp goto done;
1012 db590691 2021-06-02 stsp }
1013 67a66647 2021-05-31 stsp }
1014 67a66647 2021-05-31 stsp
1015 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1017 14c901f1 2019-08-08 stsp base_path = NULL;
1018 14c901f1 2019-08-08 stsp goto done;
1019 14c901f1 2019-08-08 stsp }
1020 14c901f1 2019-08-08 stsp
1021 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1022 14c901f1 2019-08-08 stsp if (err)
1023 14c901f1 2019-08-08 stsp goto done;
1024 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 14c901f1 2019-08-08 stsp blob_deriv);
1026 14c901f1 2019-08-08 stsp if (err)
1027 14c901f1 2019-08-08 stsp goto done;
1028 14c901f1 2019-08-08 stsp
1029 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1030 14c901f1 2019-08-08 stsp if (err)
1031 14c901f1 2019-08-08 stsp goto done;
1032 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1033 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1035 14c901f1 2019-08-08 stsp goto done;
1036 eec2f5a9 2021-06-03 stsp }
1037 eec2f5a9 2021-06-03 stsp
1038 b6b86fd1 2022-08-30 thomas /*
1039 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1040 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1041 eec2f5a9 2021-06-03 stsp */
1042 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1043 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 eec2f5a9 2021-06-03 stsp if (err)
1045 eec2f5a9 2021-06-03 stsp goto done;
1046 eec2f5a9 2021-06-03 stsp } else {
1047 eec2f5a9 2021-06-03 stsp int fd;
1048 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1050 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1051 eec2f5a9 2021-06-03 stsp goto done;
1052 eec2f5a9 2021-06-03 stsp }
1053 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1054 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1055 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1056 eec2f5a9 2021-06-03 stsp close(fd);
1057 eec2f5a9 2021-06-03 stsp goto done;
1058 eec2f5a9 2021-06-03 stsp }
1059 14c901f1 2019-08-08 stsp }
1060 14c901f1 2019-08-08 stsp
1061 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 14c901f1 2019-08-08 stsp done:
1065 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1067 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1071 14c901f1 2019-08-08 stsp free(base_path);
1072 67a66647 2021-05-31 stsp if (blob_orig_path) {
1073 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1074 67a66647 2021-05-31 stsp free(blob_orig_path);
1075 67a66647 2021-05-31 stsp }
1076 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1077 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1078 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1079 14c901f1 2019-08-08 stsp }
1080 6353ad76 2019-02-08 stsp free(id_str);
1081 818c7501 2019-07-11 stsp free(label_deriv);
1082 ed6b5030 2020-10-20 stsp free(parent);
1083 4a1ddfc2 2019-01-12 stsp return err;
1084 4a1ddfc2 2019-01-12 stsp }
1085 4a1ddfc2 2019-01-12 stsp
1086 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1087 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1090 13d9040b 2019-03-27 stsp {
1091 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1092 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1093 13d9040b 2019-03-27 stsp
1094 65b05cec 2020-07-23 stsp *new_iep = NULL;
1095 65b05cec 2020-07-23 stsp
1096 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1097 054041d0 2020-06-24 stsp if (err)
1098 054041d0 2020-06-24 stsp return err;
1099 054041d0 2020-06-24 stsp
1100 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1102 054041d0 2020-06-24 stsp if (err)
1103 054041d0 2020-06-24 stsp goto done;
1104 054041d0 2020-06-24 stsp
1105 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1106 054041d0 2020-06-24 stsp done:
1107 054041d0 2020-06-24 stsp if (err)
1108 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1109 65b05cec 2020-07-23 stsp else
1110 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1111 13d9040b 2019-03-27 stsp return err;
1112 1ebedb77 2019-10-19 stsp }
1113 1ebedb77 2019-10-19 stsp
1114 1ebedb77 2019-10-19 stsp static mode_t
1115 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1116 1ebedb77 2019-10-19 stsp {
1117 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1118 1ebedb77 2019-10-19 stsp
1119 1ebedb77 2019-10-19 stsp if (executable) {
1120 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1121 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1122 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1123 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1124 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1125 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1126 1ebedb77 2019-10-19 stsp }
1127 1ebedb77 2019-10-19 stsp
1128 a2c162eb 2022-10-30 thomas return st_mode;
1129 a2c162eb 2022-10-30 thomas }
1130 a2c162eb 2022-10-30 thomas
1131 a2c162eb 2022-10-30 thomas static mode_t
1132 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
1133 a2c162eb 2022-10-30 thomas {
1134 a2c162eb 2022-10-30 thomas mode_t um;
1135 a2c162eb 2022-10-30 thomas
1136 a2c162eb 2022-10-30 thomas um = umask(000);
1137 a2c162eb 2022-10-30 thomas umask(um);
1138 a2c162eb 2022-10-30 thomas return mode & ~um;
1139 13d9040b 2019-03-27 stsp }
1140 13d9040b 2019-03-27 stsp
1141 8ba819a3 2020-07-23 stsp /* forward declaration */
1142 13d9040b 2019-03-27 stsp static const struct got_error *
1143 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1145 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1146 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1147 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1148 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1149 8ba819a3 2020-07-23 stsp
1150 5a1fbc73 2020-07-23 stsp /*
1151 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1152 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1153 5a1fbc73 2020-07-23 stsp */
1154 8ba819a3 2020-07-23 stsp static const struct got_error *
1155 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1157 5a1fbc73 2020-07-23 stsp {
1158 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1159 5a1fbc73 2020-07-23 stsp ssize_t elen;
1160 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1161 5a1fbc73 2020-07-23 stsp int fd;
1162 5a1fbc73 2020-07-23 stsp
1163 c6e8a826 2021-04-05 stsp *did_something = 0;
1164 c6e8a826 2021-04-05 stsp
1165 5a1fbc73 2020-07-23 stsp /*
1166 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1167 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1168 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1169 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1170 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1171 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1172 5a1fbc73 2020-07-23 stsp */
1173 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1175 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1176 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1177 5a1fbc73 2020-07-23 stsp
1178 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1179 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 5a1fbc73 2020-07-23 stsp if (elen == -1)
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1182 5a1fbc73 2020-07-23 stsp
1183 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1184 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1185 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1186 5a1fbc73 2020-07-23 stsp }
1187 5a1fbc73 2020-07-23 stsp
1188 c6e8a826 2021-04-05 stsp *did_something = 1;
1189 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1190 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1191 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1192 5a1fbc73 2020-07-23 stsp return err;
1193 3c1ec782 2020-07-23 stsp }
1194 3c1ec782 2020-07-23 stsp
1195 3c1ec782 2020-07-23 stsp static const struct got_error *
1196 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1198 3c1ec782 2020-07-23 stsp {
1199 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1200 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1201 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1202 3c1ec782 2020-07-23 stsp
1203 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1204 3c1ec782 2020-07-23 stsp
1205 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1206 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1207 3c1ec782 2020-07-23 stsp return NULL;
1208 3c1ec782 2020-07-23 stsp }
1209 3c1ec782 2020-07-23 stsp
1210 3c1ec782 2020-07-23 stsp /*
1211 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1212 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1213 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1214 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1215 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1216 3c1ec782 2020-07-23 stsp */
1217 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1218 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1219 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1220 ce031e9e 2020-10-20 stsp if (err)
1221 ce031e9e 2020-10-20 stsp return err;
1222 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 ce031e9e 2020-10-20 stsp free(parent);
1224 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1225 ce031e9e 2020-10-20 stsp }
1226 ce031e9e 2020-10-20 stsp free(parent);
1227 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1228 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1229 3c1ec782 2020-07-23 stsp free(abspath);
1230 3c1ec782 2020-07-23 stsp return err;
1231 3c1ec782 2020-07-23 stsp }
1232 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1233 3c1ec782 2020-07-23 stsp free(abspath);
1234 3c1ec782 2020-07-23 stsp if (err)
1235 3c1ec782 2020-07-23 stsp return err;
1236 3c1ec782 2020-07-23 stsp } else {
1237 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1238 3c1ec782 2020-07-23 stsp if (err)
1239 3c1ec782 2020-07-23 stsp return err;
1240 3c1ec782 2020-07-23 stsp }
1241 3c1ec782 2020-07-23 stsp
1242 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1243 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1245 3c1ec782 2020-07-23 stsp return NULL;
1246 3c1ec782 2020-07-23 stsp }
1247 3c1ec782 2020-07-23 stsp
1248 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1249 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1251 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1252 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1254 3c1ec782 2020-07-23 stsp
1255 3c1ec782 2020-07-23 stsp free(path_got);
1256 3c1ec782 2020-07-23 stsp return NULL;
1257 5a1fbc73 2020-07-23 stsp }
1258 5a1fbc73 2020-07-23 stsp
1259 5a1fbc73 2020-07-23 stsp static const struct got_error *
1260 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1263 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1264 ace90326 2021-09-27 thomas struct got_repository *repo,
1265 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1266 9d31a1d8 2018-03-11 stsp {
1267 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1268 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1269 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1270 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1272 8ba819a3 2020-07-23 stsp
1273 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1274 2e1fa222 2020-07-23 stsp
1275 9e39ca77 2022-07-21 thomas /*
1276 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1277 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1278 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1279 8ba819a3 2020-07-23 stsp * in the blob object.
1280 8ba819a3 2020-07-23 stsp */
1281 8ba819a3 2020-07-23 stsp do {
1282 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1283 3521b466 2022-07-21 thomas if (err)
1284 3521b466 2022-07-21 thomas return err;
1285 3521b466 2022-07-21 thomas
1286 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1287 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1288 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1289 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1290 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1291 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1293 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1294 3b9f0f87 2020-07-23 stsp progress_arg);
1295 8ba819a3 2020-07-23 stsp }
1296 8ba819a3 2020-07-23 stsp if (len > 0) {
1297 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1298 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1299 8ba819a3 2020-07-23 stsp len - hdrlen);
1300 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1301 8ba819a3 2020-07-23 stsp hdrlen = 0;
1302 8ba819a3 2020-07-23 stsp }
1303 8ba819a3 2020-07-23 stsp } while (len != 0);
1304 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1305 8ba819a3 2020-07-23 stsp
1306 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1308 3c1ec782 2020-07-23 stsp if (err)
1309 3c1ec782 2020-07-23 stsp return err;
1310 8ba819a3 2020-07-23 stsp
1311 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1312 906c123b 2020-07-23 stsp /* install as a regular file */
1313 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1314 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1315 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1317 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1318 9e39ca77 2022-07-21 thomas return err;
1319 906c123b 2020-07-23 stsp }
1320 906c123b 2020-07-23 stsp
1321 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1322 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1323 c6e8a826 2021-04-05 stsp int symlink_replaced;
1324 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1325 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1326 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1327 9e39ca77 2022-07-21 thomas return err;
1328 c90c8ce3 2020-07-23 stsp }
1329 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1330 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1331 5a1fbc73 2020-07-23 stsp if (err)
1332 9e39ca77 2022-07-21 thomas return err;
1333 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1334 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1335 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1336 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1337 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1338 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1339 c6e8a826 2021-04-05 stsp } else {
1340 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1341 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1342 c6e8a826 2021-04-05 stsp }
1343 f35fa46a 2020-07-23 stsp }
1344 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1345 f35fa46a 2020-07-23 stsp }
1346 f35fa46a 2020-07-23 stsp
1347 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1348 f4994adc 2020-10-20 stsp char *parent;
1349 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1350 f4994adc 2020-10-20 stsp if (err)
1351 9e39ca77 2022-07-21 thomas return err;
1352 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1353 f4994adc 2020-10-20 stsp free(parent);
1354 f35fa46a 2020-07-23 stsp if (err)
1355 9e39ca77 2022-07-21 thomas return err;
1356 f35fa46a 2020-07-23 stsp /*
1357 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1358 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1359 f35fa46a 2020-07-23 stsp */
1360 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1361 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1362 9e39ca77 2022-07-21 thomas return err;
1363 f35fa46a 2020-07-23 stsp }
1364 f35fa46a 2020-07-23 stsp }
1365 f35fa46a 2020-07-23 stsp
1366 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1367 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1368 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1369 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1370 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1371 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1372 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1374 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1375 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1376 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1377 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1378 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1379 8ba819a3 2020-07-23 stsp } else {
1380 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1381 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1382 8ba819a3 2020-07-23 stsp }
1383 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1384 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1386 8ba819a3 2020-07-23 stsp return err;
1387 8ba819a3 2020-07-23 stsp }
1388 8ba819a3 2020-07-23 stsp
1389 8ba819a3 2020-07-23 stsp static const struct got_error *
1390 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1392 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1393 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1394 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1395 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1396 8ba819a3 2020-07-23 stsp {
1397 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1398 507dc3bb 2018-12-29 stsp int fd = -1;
1399 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1400 507dc3bb 2018-12-29 stsp int update = 0;
1401 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1402 a2c162eb 2022-10-30 thomas mode_t mode;
1403 8ba819a3 2020-07-23 stsp
1404 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1407 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1408 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1409 f5375317 2020-10-20 stsp char *parent;
1410 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1411 f5375317 2020-10-20 stsp if (err)
1412 f5375317 2020-10-20 stsp return err;
1413 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1414 f5375317 2020-10-20 stsp free(parent);
1415 21908da4 2019-01-13 stsp if (err)
1416 21908da4 2019-01-13 stsp return err;
1417 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1418 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1419 a2c162eb 2022-10-30 thomas mode);
1420 21908da4 2019-01-13 stsp if (fd == -1)
1421 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1422 230a42bd 2019-05-11 jcs ondisk_path);
1423 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1424 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1425 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1426 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1427 3b9f0f87 2020-07-23 stsp goto done;
1428 3b9f0f87 2020-07-23 stsp }
1429 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1432 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1433 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1434 507dc3bb 2018-12-29 stsp goto done;
1435 d70b8e30 2018-12-27 stsp } else {
1436 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1437 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1438 507dc3bb 2018-12-29 stsp if (err)
1439 507dc3bb 2018-12-29 stsp goto done;
1440 507dc3bb 2018-12-29 stsp update = 1;
1441 a2c162eb 2022-10-30 thomas
1442 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1443 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1444 a2c162eb 2022-10-30 thomas tmppath);
1445 a2c162eb 2022-10-30 thomas goto done;
1446 a2c162eb 2022-10-30 thomas }
1447 9d31a1d8 2018-03-11 stsp }
1448 507dc3bb 2018-12-29 stsp } else
1449 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1450 3818e3c4 2020-11-01 naddy }
1451 3818e3c4 2020-11-01 naddy
1452 bd6aa359 2020-07-23 stsp if (progress_cb) {
1453 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1454 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1455 bd6aa359 2020-07-23 stsp path);
1456 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1457 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1458 bd6aa359 2020-07-23 stsp path);
1459 bd6aa359 2020-07-23 stsp else
1460 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1461 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1462 bd6aa359 2020-07-23 stsp if (err)
1463 bd6aa359 2020-07-23 stsp goto done;
1464 bd6aa359 2020-07-23 stsp }
1465 d7b62c98 2018-12-27 stsp
1466 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1467 9d31a1d8 2018-03-11 stsp do {
1468 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1470 9d31a1d8 2018-03-11 stsp if (err)
1471 9d31a1d8 2018-03-11 stsp break;
1472 9d31a1d8 2018-03-11 stsp if (len > 0) {
1473 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1474 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1476 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1477 b87c6f83 2018-12-24 stsp goto done;
1478 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1479 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1480 b87c6f83 2018-12-24 stsp goto done;
1481 9d31a1d8 2018-03-11 stsp }
1482 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1483 9d31a1d8 2018-03-11 stsp }
1484 9d31a1d8 2018-03-11 stsp } while (len != 0);
1485 9d31a1d8 2018-03-11 stsp
1486 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1488 816dc654 2019-02-16 stsp goto done;
1489 816dc654 2019-02-16 stsp }
1490 9d31a1d8 2018-03-11 stsp
1491 507dc3bb 2018-12-29 stsp if (update) {
1492 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1494 f6d8c0ac 2020-11-09 stsp goto done;
1495 f6d8c0ac 2020-11-09 stsp }
1496 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1497 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1498 230a42bd 2019-05-11 jcs ondisk_path);
1499 68ed9ba5 2019-02-10 stsp goto done;
1500 68ed9ba5 2019-02-10 stsp }
1501 63df146d 2020-11-09 stsp free(tmppath);
1502 63df146d 2020-11-09 stsp tmppath = NULL;
1503 507dc3bb 2018-12-29 stsp }
1504 507dc3bb 2018-12-29 stsp
1505 9d31a1d8 2018-03-11 stsp done:
1506 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1507 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1508 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1510 507dc3bb 2018-12-29 stsp free(tmppath);
1511 6353ad76 2019-02-08 stsp return err;
1512 6353ad76 2019-02-08 stsp }
1513 6353ad76 2019-02-08 stsp
1514 be94c032 2023-02-20 thomas /*
1515 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1517 be94c032 2023-02-20 thomas */
1518 6353ad76 2019-02-08 stsp static const struct got_error *
1519 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1520 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1521 be94c032 2023-02-20 thomas FILE *ondisk_file)
1522 7154f6ce 2019-03-27 stsp {
1523 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1524 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1525 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1528 7154f6ce 2019-03-27 stsp };
1529 be94c032 2023-02-20 thomas FILE *f = NULL, *f1 = NULL;
1530 7154f6ce 2019-03-27 stsp int i = 0;
1531 9bdd68dd 2020-01-02 naddy char *line = NULL;
1532 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1533 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1534 be94c032 2023-02-20 thomas off_t sz1 = 0;
1535 7154f6ce 2019-03-27 stsp
1536 be94c032 2023-02-20 thomas if (*status == GOT_STATUS_MODIFY) {
1537 be94c032 2023-02-20 thomas f = got_opentemp();
1538 be94c032 2023-02-20 thomas if (f == NULL)
1539 be94c032 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1540 be94c032 2023-02-20 thomas
1541 be94c032 2023-02-20 thomas f1 = got_opentemp();
1542 be94c032 2023-02-20 thomas if (f1 == NULL) {
1543 be94c032 2023-02-20 thomas err = got_error_from_errno("got_opentemp");
1544 be94c032 2023-02-20 thomas goto done;
1545 be94c032 2023-02-20 thomas }
1546 be94c032 2023-02-20 thomas
1547 be94c032 2023-02-20 thomas if (blob) {
1548 be94c032 2023-02-20 thomas err = got_object_blob_dump_to_file(&sz1, NULL, NULL,
1549 be94c032 2023-02-20 thomas f1, blob);
1550 be94c032 2023-02-20 thomas if (err)
1551 be94c032 2023-02-20 thomas goto done;
1552 be94c032 2023-02-20 thomas }
1553 be94c032 2023-02-20 thomas
1554 be94c032 2023-02-20 thomas err = got_diff_blob_file(blob, f1, sz1, NULL, ondisk_file, 1,
1555 be94c032 2023-02-20 thomas sb, path, GOT_DIFF_ALGORITHM_MYERS, 0, 0, 0, NULL, f);
1556 be94c032 2023-02-20 thomas if (err)
1557 be94c032 2023-02-20 thomas goto done;
1558 be94c032 2023-02-20 thomas
1559 be94c032 2023-02-20 thomas if (fflush(f) == EOF) {
1560 be94c032 2023-02-20 thomas err = got_error_from_errno("fflush");
1561 be94c032 2023-02-20 thomas goto done;
1562 be94c032 2023-02-20 thomas }
1563 be94c032 2023-02-20 thomas if (fseeko(f, 0L, SEEK_SET) == -1) {
1564 be94c032 2023-02-20 thomas err = got_error_from_errno("fseek");
1565 be94c032 2023-02-20 thomas goto done;
1566 be94c032 2023-02-20 thomas }
1567 be94c032 2023-02-20 thomas }
1568 be94c032 2023-02-20 thomas
1569 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1570 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1571 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1572 7154f6ce 2019-03-27 stsp if (feof(f))
1573 7154f6ce 2019-03-27 stsp break;
1574 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1575 7154f6ce 2019-03-27 stsp break;
1576 7154f6ce 2019-03-27 stsp }
1577 7154f6ce 2019-03-27 stsp
1578 be94c032 2023-02-20 thomas if (*line == '+' &&
1579 be94c032 2023-02-20 thomas strncmp(line + 1, markers[i], strlen(markers[i])) == 0) {
1580 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1581 19332e6d 2019-05-13 stsp == 0)
1582 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1583 7154f6ce 2019-03-27 stsp else
1584 7154f6ce 2019-03-27 stsp i++;
1585 7154f6ce 2019-03-27 stsp }
1586 7154f6ce 2019-03-27 stsp }
1587 be94c032 2023-02-20 thomas
1588 be94c032 2023-02-20 thomas done:
1589 9bdd68dd 2020-01-02 naddy free(line);
1590 be94c032 2023-02-20 thomas if (f != NULL && fclose(f) == EOF && err == NULL)
1591 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1592 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1593 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1594 7154f6ce 2019-03-27 stsp
1595 7154f6ce 2019-03-27 stsp return err;
1596 e2b1e152 2019-07-13 stsp }
1597 e2b1e152 2019-07-13 stsp
1598 e2b1e152 2019-07-13 stsp static int
1599 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1600 1ebedb77 2019-10-19 stsp {
1601 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1602 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1603 1ebedb77 2019-10-19 stsp }
1604 1ebedb77 2019-10-19 stsp
1605 1ebedb77 2019-10-19 stsp static int
1606 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1607 e2b1e152 2019-07-13 stsp {
1608 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1609 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1610 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1611 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1612 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1613 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1614 c363b2c1 2019-08-03 stsp }
1615 c363b2c1 2019-08-03 stsp
1616 c363b2c1 2019-08-03 stsp static unsigned char
1617 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1618 c363b2c1 2019-08-03 stsp {
1619 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1620 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1621 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1622 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1623 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1624 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1625 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1626 c363b2c1 2019-08-03 stsp default:
1627 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1628 a919d5c4 2020-07-23 stsp }
1629 a919d5c4 2020-07-23 stsp }
1630 a919d5c4 2020-07-23 stsp
1631 a919d5c4 2020-07-23 stsp static const struct got_error *
1632 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1633 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1634 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1635 a919d5c4 2020-07-23 stsp {
1636 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1637 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1638 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1639 a919d5c4 2020-07-23 stsp ssize_t elen;
1640 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1641 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1642 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1643 a919d5c4 2020-07-23 stsp
1644 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1645 a919d5c4 2020-07-23 stsp
1646 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1647 a919d5c4 2020-07-23 stsp do {
1648 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1649 a919d5c4 2020-07-23 stsp if (err)
1650 a919d5c4 2020-07-23 stsp return err;
1651 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1652 a919d5c4 2020-07-23 stsp /*
1653 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1654 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1655 a919d5c4 2020-07-23 stsp */
1656 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1657 a919d5c4 2020-07-23 stsp }
1658 a919d5c4 2020-07-23 stsp if (len > 0) {
1659 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1660 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1661 a919d5c4 2020-07-23 stsp len - hdrlen);
1662 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1663 a919d5c4 2020-07-23 stsp hdrlen = 0;
1664 a919d5c4 2020-07-23 stsp }
1665 a919d5c4 2020-07-23 stsp } while (len != 0);
1666 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1667 a919d5c4 2020-07-23 stsp
1668 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1669 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1670 a919d5c4 2020-07-23 stsp if (elen == -1)
1671 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1672 a919d5c4 2020-07-23 stsp } else {
1673 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1674 a919d5c4 2020-07-23 stsp if (elen == -1)
1675 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1676 c363b2c1 2019-08-03 stsp }
1677 a919d5c4 2020-07-23 stsp
1678 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1679 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1680 a919d5c4 2020-07-23 stsp
1681 a919d5c4 2020-07-23 stsp return NULL;
1682 7154f6ce 2019-03-27 stsp }
1683 7154f6ce 2019-03-27 stsp
1684 7154f6ce 2019-03-27 stsp static const struct got_error *
1685 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1686 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1687 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1688 6353ad76 2019-02-08 stsp {
1689 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1690 6353ad76 2019-02-08 stsp struct got_object_id id;
1691 6353ad76 2019-02-08 stsp size_t hdrlen;
1692 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1693 6353ad76 2019-02-08 stsp FILE *f = NULL;
1694 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1695 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1696 6353ad76 2019-02-08 stsp size_t flen, blen;
1697 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1698 6353ad76 2019-02-08 stsp
1699 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1700 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1701 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1702 6353ad76 2019-02-08 stsp
1703 7f91a133 2019-12-13 stsp /*
1704 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1705 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1706 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1707 7f91a133 2019-12-13 stsp */
1708 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1709 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1710 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1711 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1712 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1713 882ef1b9 2019-12-13 stsp else
1714 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1715 882ef1b9 2019-12-13 stsp goto done;
1716 882ef1b9 2019-12-13 stsp }
1717 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1718 3d35a492 2019-12-13 stsp goto done;
1719 3d35a492 2019-12-13 stsp }
1720 7f91a133 2019-12-13 stsp } else {
1721 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1722 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1723 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1724 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1725 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1726 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1727 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1728 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1729 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1730 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1731 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1732 3d35a492 2019-12-13 stsp else
1733 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1734 3d35a492 2019-12-13 stsp goto done;
1735 3d35a492 2019-12-13 stsp }
1736 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1737 1338848f 2019-12-13 stsp goto done;
1738 a378724f 2019-02-10 stsp }
1739 a378724f 2019-02-10 stsp }
1740 6353ad76 2019-02-08 stsp
1741 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1742 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1743 1338848f 2019-12-13 stsp goto done;
1744 3f148bc6 2019-07-27 stsp }
1745 efdd40df 2019-07-27 stsp
1746 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1747 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1748 1338848f 2019-12-13 stsp goto done;
1749 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1750 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1751 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1752 1338848f 2019-12-13 stsp goto done;
1753 d00136be 2019-03-26 stsp }
1754 b8f41171 2019-02-10 stsp
1755 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1756 f179e66d 2020-07-23 stsp goto done;
1757 f179e66d 2020-07-23 stsp
1758 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1759 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1760 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1761 1338848f 2019-12-13 stsp goto done;
1762 f179e66d 2020-07-23 stsp }
1763 6353ad76 2019-02-08 stsp
1764 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1765 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1766 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1767 c363b2c1 2019-08-03 stsp else
1768 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1769 c363b2c1 2019-08-03 stsp
1770 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1771 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1772 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1773 f4ae6ddb 2022-07-01 thomas goto done;
1774 f4ae6ddb 2022-07-01 thomas }
1775 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1776 6353ad76 2019-02-08 stsp if (err)
1777 1338848f 2019-12-13 stsp goto done;
1778 6353ad76 2019-02-08 stsp
1779 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1780 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1781 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1782 a919d5c4 2020-07-23 stsp goto done;
1783 a919d5c4 2020-07-23 stsp }
1784 a919d5c4 2020-07-23 stsp
1785 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1786 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1787 ab0d4361 2019-12-13 stsp if (fd == -1) {
1788 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1789 ab0d4361 2019-12-13 stsp goto done;
1790 ab0d4361 2019-12-13 stsp }
1791 3d35a492 2019-12-13 stsp }
1792 3d35a492 2019-12-13 stsp
1793 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1794 6353ad76 2019-02-08 stsp if (f == NULL) {
1795 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1796 6353ad76 2019-02-08 stsp goto done;
1797 6353ad76 2019-02-08 stsp }
1798 1338848f 2019-12-13 stsp fd = -1;
1799 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1800 656b1f76 2019-05-11 jcs for (;;) {
1801 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1802 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1803 6353ad76 2019-02-08 stsp if (err)
1804 7154f6ce 2019-03-27 stsp goto done;
1805 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1806 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1807 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1808 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1809 7154f6ce 2019-03-27 stsp goto done;
1810 80c5c120 2019-02-19 stsp }
1811 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1812 6353ad76 2019-02-08 stsp if (flen != 0)
1813 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1814 6353ad76 2019-02-08 stsp break;
1815 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1816 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1817 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1818 6353ad76 2019-02-08 stsp break;
1819 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1820 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1821 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1822 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1823 6353ad76 2019-02-08 stsp break;
1824 6353ad76 2019-02-08 stsp }
1825 6353ad76 2019-02-08 stsp } else {
1826 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1827 6353ad76 2019-02-08 stsp break;
1828 6353ad76 2019-02-08 stsp }
1829 6353ad76 2019-02-08 stsp hdrlen = 0;
1830 6353ad76 2019-02-08 stsp }
1831 7154f6ce 2019-03-27 stsp
1832 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1833 7154f6ce 2019-03-27 stsp rewind(f);
1834 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1835 be94c032 2023-02-20 thomas sb, f);
1836 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1837 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1838 6353ad76 2019-02-08 stsp done:
1839 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1840 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1841 6353ad76 2019-02-08 stsp if (blob)
1842 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1843 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1844 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1845 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1846 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1847 9d31a1d8 2018-03-11 stsp return err;
1848 e2b1e152 2019-07-13 stsp }
1849 e2b1e152 2019-07-13 stsp
1850 e2b1e152 2019-07-13 stsp /*
1851 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1852 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1853 e2b1e152 2019-07-13 stsp */
1854 e2b1e152 2019-07-13 stsp static const struct got_error *
1855 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1856 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1857 e2b1e152 2019-07-13 stsp {
1858 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1859 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1860 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1861 e2b1e152 2019-07-13 stsp
1862 e2b1e152 2019-07-13 stsp return NULL;
1863 9d31a1d8 2018-03-11 stsp }
1864 9d31a1d8 2018-03-11 stsp
1865 9d31a1d8 2018-03-11 stsp static const struct got_error *
1866 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1867 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1868 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1869 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1870 0584f854 2019-04-06 stsp void *progress_arg)
1871 9d31a1d8 2018-03-11 stsp {
1872 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1873 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1874 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1875 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1876 b8f41171 2019-02-10 stsp struct stat sb;
1877 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1878 9d31a1d8 2018-03-11 stsp
1879 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1880 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1881 6353ad76 2019-02-08 stsp
1882 abb4604f 2019-07-27 stsp if (ie) {
1883 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1884 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1885 a76c42e6 2019-08-03 stsp goto done;
1886 a76c42e6 2019-08-03 stsp }
1887 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1888 7f91a133 2019-12-13 stsp repo);
1889 abb4604f 2019-07-27 stsp if (err)
1890 abb4604f 2019-07-27 stsp goto done;
1891 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1892 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1893 c90c8ce3 2020-07-23 stsp } else {
1894 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1895 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1896 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1897 e6f4ba31 2021-09-24 thomas ondisk_path);
1898 e6f4ba31 2021-09-24 thomas goto done;
1899 e6f4ba31 2021-09-24 thomas }
1900 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1901 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1902 e6f4ba31 2021-09-24 thomas } else {
1903 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1904 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1905 e6f4ba31 2021-09-24 thomas else
1906 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1907 e6f4ba31 2021-09-24 thomas }
1908 c90c8ce3 2020-07-23 stsp }
1909 6353ad76 2019-02-08 stsp
1910 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1911 2c41dce7 2021-06-27 stsp if (ie)
1912 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1913 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1914 b8f41171 2019-02-10 stsp goto done;
1915 b8f41171 2019-02-10 stsp }
1916 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1917 a769b60b 2021-06-27 stsp if (ie)
1918 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1919 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1920 5036ab18 2020-04-18 stsp path);
1921 5036ab18 2020-04-18 stsp goto done;
1922 5036ab18 2020-04-18 stsp }
1923 b8f41171 2019-02-10 stsp
1924 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1925 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1926 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1927 c6e8a826 2021-04-05 stsp /*
1928 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1929 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1930 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1931 c6e8a826 2021-04-05 stsp * updating contents of this file.
1932 c6e8a826 2021-04-05 stsp */
1933 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1934 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1935 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1936 c6e8a826 2021-04-05 stsp /* Same commit. */
1937 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1938 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1939 e2b1e152 2019-07-13 stsp if (err)
1940 e2b1e152 2019-07-13 stsp goto done;
1941 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1942 b8f41171 2019-02-10 stsp path);
1943 6353ad76 2019-02-08 stsp goto done;
1944 a378724f 2019-02-10 stsp }
1945 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1946 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1947 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1948 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1949 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1950 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1951 0f58026f 2021-04-05 stsp if (err)
1952 0f58026f 2021-04-05 stsp goto done;
1953 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1954 0f58026f 2021-04-05 stsp path);
1955 b8f41171 2019-02-10 stsp goto done;
1956 e2b1e152 2019-07-13 stsp }
1957 9d31a1d8 2018-03-11 stsp }
1958 9d31a1d8 2018-03-11 stsp
1959 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1960 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1961 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1962 f4ae6ddb 2022-07-01 thomas goto done;
1963 f4ae6ddb 2022-07-01 thomas }
1964 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1965 8da9e5f4 2019-01-12 stsp if (err)
1966 6353ad76 2019-02-08 stsp goto done;
1967 512f0d0e 2019-01-02 stsp
1968 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1969 234035bc 2019-06-01 stsp int update_timestamps;
1970 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1971 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1972 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1973 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1974 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1975 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1976 f4ae6ddb 2022-07-01 thomas goto done;
1977 f4ae6ddb 2022-07-01 thomas }
1978 234035bc 2019-06-01 stsp struct got_object_id id2;
1979 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
1980 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1981 f4ae6ddb 2022-07-01 thomas fd2);
1982 234035bc 2019-06-01 stsp if (err)
1983 f69721c3 2019-10-21 stsp goto done;
1984 f69721c3 2019-10-21 stsp }
1985 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1986 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1987 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1988 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1989 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1990 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1991 f69721c3 2019-10-21 stsp goto done;
1992 f69721c3 2019-10-21 stsp }
1993 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1994 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1995 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1996 234035bc 2019-06-01 stsp goto done;
1997 f69721c3 2019-10-21 stsp }
1998 234035bc 2019-06-01 stsp }
1999 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2000 36bf999c 2020-07-23 stsp char *link_target;
2001 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2002 36bf999c 2020-07-23 stsp if (err)
2003 36bf999c 2020-07-23 stsp goto done;
2004 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2005 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2006 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2007 36bf999c 2020-07-23 stsp free(link_target);
2008 993e2a1b 2020-07-23 stsp } else {
2009 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2010 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2011 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2012 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2013 993e2a1b 2020-07-23 stsp }
2014 f69721c3 2019-10-21 stsp free(label_orig);
2015 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2016 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2017 f4ae6ddb 2022-07-01 thomas goto done;
2018 f4ae6ddb 2022-07-01 thomas }
2019 234035bc 2019-06-01 stsp if (blob2)
2020 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2021 909d120e 2019-09-22 stsp if (err)
2022 909d120e 2019-09-22 stsp goto done;
2023 234035bc 2019-06-01 stsp /*
2024 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2025 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2026 234035bc 2019-06-01 stsp * unmodified files again.
2027 234035bc 2019-06-01 stsp */
2028 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2029 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2030 234035bc 2019-06-01 stsp update_timestamps);
2031 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2032 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2033 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2034 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2035 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2036 1ee397ad 2019-07-12 stsp if (err)
2037 1ee397ad 2019-07-12 stsp goto done;
2038 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2039 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2040 13d9040b 2019-03-27 stsp if (err)
2041 13d9040b 2019-03-27 stsp goto done;
2042 13d9040b 2019-03-27 stsp } else {
2043 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2044 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2045 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2046 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2047 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2048 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2049 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2050 2e1fa222 2020-07-23 stsp } else {
2051 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2052 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2053 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2054 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2055 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2056 2e1fa222 2020-07-23 stsp }
2057 13d9040b 2019-03-27 stsp if (err)
2058 13d9040b 2019-03-27 stsp goto done;
2059 65b05cec 2020-07-23 stsp
2060 054041d0 2020-06-24 stsp if (ie) {
2061 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2062 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2063 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2064 054041d0 2020-06-24 stsp } else {
2065 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2066 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2067 054041d0 2020-06-24 stsp &blob->id);
2068 054041d0 2020-06-24 stsp }
2069 13d9040b 2019-03-27 stsp if (err)
2070 13d9040b 2019-03-27 stsp goto done;
2071 65b05cec 2020-07-23 stsp
2072 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2073 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2074 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2075 2e1fa222 2020-07-23 stsp }
2076 13d9040b 2019-03-27 stsp }
2077 f4ae6ddb 2022-07-01 thomas
2078 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2079 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2080 f4ae6ddb 2022-07-01 thomas goto done;
2081 f4ae6ddb 2022-07-01 thomas }
2082 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2083 6353ad76 2019-02-08 stsp done:
2084 6353ad76 2019-02-08 stsp free(ondisk_path);
2085 8da9e5f4 2019-01-12 stsp return err;
2086 90285c3b 2019-01-08 stsp }
2087 90285c3b 2019-01-08 stsp
2088 90285c3b 2019-01-08 stsp static const struct got_error *
2089 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2090 90285c3b 2019-01-08 stsp {
2091 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2092 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2093 90285c3b 2019-01-08 stsp
2094 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2095 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2096 90285c3b 2019-01-08 stsp
2097 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2098 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2099 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2100 c97665ae 2019-01-13 stsp } else {
2101 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2102 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2103 1c4cdd89 2021-06-20 stsp if (err)
2104 1c4cdd89 2021-06-20 stsp goto done;
2105 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2106 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2107 fddefe3b 2020-10-20 stsp free(ondisk_path);
2108 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2109 1c4cdd89 2021-06-20 stsp parent = NULL;
2110 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2111 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2112 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2113 fddefe3b 2020-10-20 stsp ondisk_path);
2114 90285c3b 2019-01-08 stsp break;
2115 90285c3b 2019-01-08 stsp }
2116 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2117 1c4cdd89 2021-06-20 stsp if (err)
2118 1c4cdd89 2021-06-20 stsp break;
2119 1c4cdd89 2021-06-20 stsp }
2120 90285c3b 2019-01-08 stsp }
2121 1c4cdd89 2021-06-20 stsp done:
2122 90285c3b 2019-01-08 stsp free(ondisk_path);
2123 1c4cdd89 2021-06-20 stsp free(parent);
2124 90285c3b 2019-01-08 stsp return err;
2125 512f0d0e 2019-01-02 stsp }
2126 512f0d0e 2019-01-02 stsp
2127 708d8e67 2019-03-27 stsp static const struct got_error *
2128 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2129 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2130 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2131 708d8e67 2019-03-27 stsp {
2132 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2133 708d8e67 2019-03-27 stsp unsigned char status;
2134 708d8e67 2019-03-27 stsp struct stat sb;
2135 708d8e67 2019-03-27 stsp char *ondisk_path;
2136 708d8e67 2019-03-27 stsp
2137 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2138 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2139 a76c42e6 2019-08-03 stsp
2140 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2141 708d8e67 2019-03-27 stsp == -1)
2142 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2143 708d8e67 2019-03-27 stsp
2144 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2145 708d8e67 2019-03-27 stsp if (err)
2146 5a58a424 2020-06-23 stsp goto done;
2147 708d8e67 2019-03-27 stsp
2148 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2149 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2150 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2151 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2152 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2153 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2154 993e2a1b 2020-07-23 stsp goto done;
2155 993e2a1b 2020-07-23 stsp }
2156 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2157 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2158 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2159 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2160 993e2a1b 2020-07-23 stsp if (err)
2161 993e2a1b 2020-07-23 stsp goto done;
2162 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2163 993e2a1b 2020-07-23 stsp ie->path);
2164 993e2a1b 2020-07-23 stsp goto done;
2165 993e2a1b 2020-07-23 stsp }
2166 993e2a1b 2020-07-23 stsp
2167 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2168 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2169 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2170 1ee397ad 2019-07-12 stsp if (err)
2171 5a58a424 2020-06-23 stsp goto done;
2172 fc6346c4 2019-03-27 stsp /*
2173 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2174 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2175 fc6346c4 2019-03-27 stsp */
2176 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2177 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2178 fc6346c4 2019-03-27 stsp } else {
2179 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2180 1ee397ad 2019-07-12 stsp if (err)
2181 5a58a424 2020-06-23 stsp goto done;
2182 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2183 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2184 fc6346c4 2019-03-27 stsp if (err)
2185 5a58a424 2020-06-23 stsp goto done;
2186 fc6346c4 2019-03-27 stsp }
2187 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2188 708d8e67 2019-03-27 stsp }
2189 5a58a424 2020-06-23 stsp done:
2190 5a58a424 2020-06-23 stsp free(ondisk_path);
2191 fc6346c4 2019-03-27 stsp return err;
2192 708d8e67 2019-03-27 stsp }
2193 708d8e67 2019-03-27 stsp
2194 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2195 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2196 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2197 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2198 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2199 8da9e5f4 2019-01-12 stsp void *progress_arg;
2200 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2201 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2202 8da9e5f4 2019-01-12 stsp };
2203 8da9e5f4 2019-01-12 stsp
2204 512f0d0e 2019-01-02 stsp static const struct got_error *
2205 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2206 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2207 512f0d0e 2019-01-02 stsp {
2208 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2209 0584f854 2019-04-06 stsp
2210 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2211 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2212 512f0d0e 2019-01-02 stsp
2213 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2214 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2215 8da9e5f4 2019-01-12 stsp }
2216 512f0d0e 2019-01-02 stsp
2217 8da9e5f4 2019-01-12 stsp static const struct got_error *
2218 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2219 8da9e5f4 2019-01-12 stsp {
2220 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2221 7a9df742 2019-01-08 stsp
2222 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2223 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2224 0584f854 2019-04-06 stsp
2225 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2226 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2227 9d31a1d8 2018-03-11 stsp }
2228 9d31a1d8 2018-03-11 stsp
2229 9d31a1d8 2018-03-11 stsp static const struct got_error *
2230 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2231 9d31a1d8 2018-03-11 stsp {
2232 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2233 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2234 8da9e5f4 2019-01-12 stsp char *path;
2235 9d31a1d8 2018-03-11 stsp
2236 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2237 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2238 0584f854 2019-04-06 stsp
2239 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2240 63c5ca5d 2019-08-24 stsp return NULL;
2241 63c5ca5d 2019-08-24 stsp
2242 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2243 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2244 8da9e5f4 2019-01-12 stsp == -1)
2245 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2246 9d31a1d8 2018-03-11 stsp
2247 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2248 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2249 8da9e5f4 2019-01-12 stsp else
2250 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2251 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2252 9d31a1d8 2018-03-11 stsp
2253 8da9e5f4 2019-01-12 stsp free(path);
2254 0cd1c46a 2019-03-11 stsp return err;
2255 0cd1c46a 2019-03-11 stsp }
2256 0cd1c46a 2019-03-11 stsp
2257 b2118c49 2020-07-28 stsp const struct got_error *
2258 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2259 b2118c49 2020-07-28 stsp {
2260 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2261 b2118c49 2020-07-28 stsp
2262 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2263 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2264 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2265 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2266 b2118c49 2020-07-28 stsp }
2267 b2118c49 2020-07-28 stsp
2268 b2118c49 2020-07-28 stsp return NULL;
2269 b2118c49 2020-07-28 stsp }
2270 b2118c49 2020-07-28 stsp
2271 818c7501 2019-07-11 stsp static const struct got_error *
2272 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2273 0cd1c46a 2019-03-11 stsp {
2274 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2275 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2276 0cd1c46a 2019-03-11 stsp
2277 517bab73 2019-03-11 stsp *refname = NULL;
2278 517bab73 2019-03-11 stsp
2279 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2280 b2118c49 2020-07-28 stsp if (err)
2281 b2118c49 2020-07-28 stsp return err;
2282 0cd1c46a 2019-03-11 stsp
2283 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2284 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2285 0647c563 2019-03-11 stsp *refname = NULL;
2286 0cd1c46a 2019-03-11 stsp }
2287 517bab73 2019-03-11 stsp free(uuidstr);
2288 517bab73 2019-03-11 stsp return err;
2289 517bab73 2019-03-11 stsp }
2290 517bab73 2019-03-11 stsp
2291 818c7501 2019-07-11 stsp const struct got_error *
2292 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2293 f6cd0243 2023-01-28 thomas const char *prefix)
2294 f6cd0243 2023-01-28 thomas {
2295 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2296 f6cd0243 2023-01-28 thomas }
2297 f6cd0243 2023-01-28 thomas
2298 f6cd0243 2023-01-28 thomas const struct got_error *
2299 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2300 818c7501 2019-07-11 stsp {
2301 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2302 818c7501 2019-07-11 stsp }
2303 818c7501 2019-07-11 stsp
2304 818c7501 2019-07-11 stsp static const struct got_error *
2305 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2306 818c7501 2019-07-11 stsp {
2307 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2308 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2309 818c7501 2019-07-11 stsp }
2310 818c7501 2019-07-11 stsp
2311 818c7501 2019-07-11 stsp static const struct got_error *
2312 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2313 818c7501 2019-07-11 stsp {
2314 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2315 818c7501 2019-07-11 stsp }
2316 818c7501 2019-07-11 stsp
2317 818c7501 2019-07-11 stsp static const struct got_error *
2318 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2319 818c7501 2019-07-11 stsp {
2320 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2321 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2322 818c7501 2019-07-11 stsp }
2323 818c7501 2019-07-11 stsp
2324 818c7501 2019-07-11 stsp static const struct got_error *
2325 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2326 818c7501 2019-07-11 stsp {
2327 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2328 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2329 0ebf8283 2019-07-24 stsp }
2330 0ebf8283 2019-07-24 stsp
2331 0ebf8283 2019-07-24 stsp static const struct got_error *
2332 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2333 0ebf8283 2019-07-24 stsp {
2334 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2335 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2336 0ebf8283 2019-07-24 stsp }
2337 0ebf8283 2019-07-24 stsp
2338 0ebf8283 2019-07-24 stsp static const struct got_error *
2339 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2340 0ebf8283 2019-07-24 stsp {
2341 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2342 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2343 0ebf8283 2019-07-24 stsp }
2344 0ebf8283 2019-07-24 stsp
2345 0ebf8283 2019-07-24 stsp static const struct got_error *
2346 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2347 0ebf8283 2019-07-24 stsp {
2348 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2349 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2350 818c7501 2019-07-11 stsp }
2351 818c7501 2019-07-11 stsp
2352 0ebf8283 2019-07-24 stsp static const struct got_error *
2353 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2354 0ebf8283 2019-07-24 stsp {
2355 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2356 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2357 0ebf8283 2019-07-24 stsp }
2358 818c7501 2019-07-11 stsp
2359 0ebf8283 2019-07-24 stsp const struct got_error *
2360 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2361 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2362 0ebf8283 2019-07-24 stsp {
2363 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2364 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2365 0ebf8283 2019-07-24 stsp *path = NULL;
2366 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2367 0ebf8283 2019-07-24 stsp }
2368 0ebf8283 2019-07-24 stsp return NULL;
2369 10604dce 2021-09-24 thomas }
2370 10604dce 2021-09-24 thomas
2371 10604dce 2021-09-24 thomas static const struct got_error *
2372 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2373 10604dce 2021-09-24 thomas {
2374 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2375 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2376 0ebf8283 2019-07-24 stsp }
2377 0ebf8283 2019-07-24 stsp
2378 10604dce 2021-09-24 thomas static const struct got_error *
2379 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2380 10604dce 2021-09-24 thomas {
2381 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2382 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2383 10604dce 2021-09-24 thomas }
2384 10604dce 2021-09-24 thomas
2385 517bab73 2019-03-11 stsp /*
2386 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2387 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2388 517bab73 2019-03-11 stsp */
2389 517bab73 2019-03-11 stsp static const struct got_error *
2390 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2391 517bab73 2019-03-11 stsp {
2392 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2393 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2394 517bab73 2019-03-11 stsp char *refname;
2395 517bab73 2019-03-11 stsp
2396 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2397 517bab73 2019-03-11 stsp if (err)
2398 517bab73 2019-03-11 stsp return err;
2399 0cd1c46a 2019-03-11 stsp
2400 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2401 0cd1c46a 2019-03-11 stsp if (err)
2402 0cd1c46a 2019-03-11 stsp goto done;
2403 0cd1c46a 2019-03-11 stsp
2404 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2405 0cd1c46a 2019-03-11 stsp done:
2406 0cd1c46a 2019-03-11 stsp free(refname);
2407 0cd1c46a 2019-03-11 stsp if (ref)
2408 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2409 3e3a69f1 2019-07-25 stsp return err;
2410 3e3a69f1 2019-07-25 stsp }
2411 3e3a69f1 2019-07-25 stsp
2412 3e3a69f1 2019-07-25 stsp static const struct got_error *
2413 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2414 3e3a69f1 2019-07-25 stsp {
2415 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2416 3e3a69f1 2019-07-25 stsp
2417 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2418 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2419 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2420 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2421 3e3a69f1 2019-07-25 stsp }
2422 9d31a1d8 2018-03-11 stsp return err;
2423 9d31a1d8 2018-03-11 stsp }
2424 9d31a1d8 2018-03-11 stsp
2425 3e3a69f1 2019-07-25 stsp
2426 ebf99748 2019-05-09 stsp static const struct got_error *
2427 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2428 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2429 ebf99748 2019-05-09 stsp {
2430 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2431 ebf99748 2019-05-09 stsp FILE *index = NULL;
2432 0cd1c46a 2019-03-11 stsp
2433 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2434 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2435 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2436 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2437 ebf99748 2019-05-09 stsp
2438 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2439 3e3a69f1 2019-07-25 stsp if (err)
2440 ebf99748 2019-05-09 stsp goto done;
2441 ebf99748 2019-05-09 stsp
2442 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2443 ebf99748 2019-05-09 stsp if (index == NULL) {
2444 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2445 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2446 ebf99748 2019-05-09 stsp } else {
2447 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2448 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2449 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2450 ebf99748 2019-05-09 stsp }
2451 ebf99748 2019-05-09 stsp done:
2452 ebf99748 2019-05-09 stsp if (err) {
2453 ebf99748 2019-05-09 stsp free(*fileindex_path);
2454 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2455 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2456 ebf99748 2019-05-09 stsp *fileindex = NULL;
2457 ebf99748 2019-05-09 stsp }
2458 ebf99748 2019-05-09 stsp return err;
2459 ebf99748 2019-05-09 stsp }
2460 c932eeeb 2019-05-22 stsp
2461 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2462 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2463 c932eeeb 2019-05-22 stsp const char *path;
2464 c932eeeb 2019-05-22 stsp size_t path_len;
2465 c932eeeb 2019-05-22 stsp const char *entry_name;
2466 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2467 a484d721 2019-06-10 stsp void *progress_arg;
2468 c932eeeb 2019-05-22 stsp };
2469 ebf99748 2019-05-09 stsp
2470 c932eeeb 2019-05-22 stsp static const struct got_error *
2471 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2472 c932eeeb 2019-05-22 stsp {
2473 1ee397ad 2019-07-12 stsp const struct got_error *err;
2474 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2475 c932eeeb 2019-05-22 stsp
2476 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2477 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2478 c932eeeb 2019-05-22 stsp return NULL;
2479 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2480 102ff934 2019-06-11 stsp return NULL;
2481 102ff934 2019-06-11 stsp
2482 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2483 a769b60b 2021-06-27 stsp return NULL;
2484 a769b60b 2021-06-27 stsp
2485 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2486 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2487 c932eeeb 2019-05-22 stsp return NULL;
2488 c932eeeb 2019-05-22 stsp
2489 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2490 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2491 edd02c5e 2019-07-11 stsp ie->path);
2492 1ee397ad 2019-07-12 stsp if (err)
2493 1ee397ad 2019-07-12 stsp return err;
2494 1ee397ad 2019-07-12 stsp }
2495 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2496 c932eeeb 2019-05-22 stsp return NULL;
2497 a615e0e7 2020-12-16 stsp }
2498 a615e0e7 2020-12-16 stsp
2499 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2500 a615e0e7 2020-12-16 stsp static const struct got_error *
2501 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2502 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2503 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2504 a615e0e7 2020-12-16 stsp {
2505 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2506 a615e0e7 2020-12-16 stsp
2507 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2508 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2509 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2510 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2511 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2512 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2513 a615e0e7 2020-12-16 stsp
2514 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2515 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2516 9c6338c4 2019-06-02 stsp }
2517 9c6338c4 2019-06-02 stsp
2518 9c6338c4 2019-06-02 stsp static const struct got_error *
2519 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2520 9c6338c4 2019-06-02 stsp {
2521 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2522 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2523 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2524 867630bb 2020-01-17 stsp struct timespec timeout;
2525 9c6338c4 2019-06-02 stsp
2526 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2527 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2528 9c6338c4 2019-06-02 stsp if (err)
2529 9c6338c4 2019-06-02 stsp goto done;
2530 9c6338c4 2019-06-02 stsp
2531 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2532 9c6338c4 2019-06-02 stsp if (err)
2533 9c6338c4 2019-06-02 stsp goto done;
2534 9c6338c4 2019-06-02 stsp
2535 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2536 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2537 9c6338c4 2019-06-02 stsp fileindex_path);
2538 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2539 9c6338c4 2019-06-02 stsp }
2540 867630bb 2020-01-17 stsp
2541 867630bb 2020-01-17 stsp /*
2542 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2543 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2544 867630bb 2020-01-17 stsp * was recorded in the file index.
2545 867630bb 2020-01-17 stsp */
2546 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2547 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2548 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2549 9c6338c4 2019-06-02 stsp done:
2550 9c6338c4 2019-06-02 stsp if (new_index)
2551 9c6338c4 2019-06-02 stsp fclose(new_index);
2552 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2553 9c6338c4 2019-06-02 stsp return err;
2554 c932eeeb 2019-05-22 stsp }
2555 6ced4176 2019-07-12 stsp
2556 6ced4176 2019-07-12 stsp static const struct got_error *
2557 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2558 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2559 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2560 945f9229 2022-04-16 thomas struct got_repository *repo)
2561 6ced4176 2019-07-12 stsp {
2562 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2563 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2564 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2565 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2566 6ced4176 2019-07-12 stsp
2567 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2568 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2569 6ced4176 2019-07-12 stsp *tree_id = NULL;
2570 6ced4176 2019-07-12 stsp
2571 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2572 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2573 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2574 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2575 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2576 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2577 6ced4176 2019-07-12 stsp goto done;
2578 6ced4176 2019-07-12 stsp }
2579 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2580 945f9229 2022-04-16 thomas worktree->path_prefix);
2581 6ced4176 2019-07-12 stsp if (err)
2582 6ced4176 2019-07-12 stsp goto done;
2583 6ced4176 2019-07-12 stsp return NULL;
2584 6ced4176 2019-07-12 stsp }
2585 6ced4176 2019-07-12 stsp
2586 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2587 6ced4176 2019-07-12 stsp
2588 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2589 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2590 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2591 6ced4176 2019-07-12 stsp goto done;
2592 6ced4176 2019-07-12 stsp }
2593 6ced4176 2019-07-12 stsp
2594 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2595 6ced4176 2019-07-12 stsp if (err)
2596 6ced4176 2019-07-12 stsp goto done;
2597 6ced4176 2019-07-12 stsp
2598 6ced4176 2019-07-12 stsp free(in_repo_path);
2599 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2600 6ced4176 2019-07-12 stsp
2601 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2602 6ced4176 2019-07-12 stsp if (err)
2603 6ced4176 2019-07-12 stsp goto done;
2604 c932eeeb 2019-05-22 stsp
2605 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2606 6ced4176 2019-07-12 stsp /* Check out a single file. */
2607 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2608 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2609 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2610 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2611 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2612 6ced4176 2019-07-12 stsp goto done;
2613 6ced4176 2019-07-12 stsp }
2614 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2615 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2616 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2617 6ced4176 2019-07-12 stsp goto done;
2618 6ced4176 2019-07-12 stsp }
2619 6ced4176 2019-07-12 stsp } else {
2620 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2621 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2622 6ced4176 2019-07-12 stsp if (err)
2623 6ced4176 2019-07-12 stsp return err;
2624 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2625 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2626 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2627 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2628 6ced4176 2019-07-12 stsp goto done;
2629 6ced4176 2019-07-12 stsp }
2630 6ced4176 2019-07-12 stsp }
2631 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2632 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2633 6ced4176 2019-07-12 stsp } else {
2634 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2635 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2636 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2637 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2638 6ced4176 2019-07-12 stsp goto done;
2639 6ced4176 2019-07-12 stsp }
2640 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2641 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2642 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2643 6ced4176 2019-07-12 stsp goto done;
2644 6ced4176 2019-07-12 stsp }
2645 6ced4176 2019-07-12 stsp }
2646 6ced4176 2019-07-12 stsp done:
2647 6ced4176 2019-07-12 stsp free(id);
2648 6ced4176 2019-07-12 stsp free(in_repo_path);
2649 6ced4176 2019-07-12 stsp if (err) {
2650 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2651 6ced4176 2019-07-12 stsp free(*tree_relpath);
2652 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2653 6ced4176 2019-07-12 stsp free(*tree_id);
2654 6ced4176 2019-07-12 stsp *tree_id = NULL;
2655 6ced4176 2019-07-12 stsp }
2656 07ed472d 2019-07-12 stsp return err;
2657 07ed472d 2019-07-12 stsp }
2658 07ed472d 2019-07-12 stsp
2659 07ed472d 2019-07-12 stsp static const struct got_error *
2660 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2661 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2662 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2663 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2664 07ed472d 2019-07-12 stsp {
2665 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2666 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2667 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2668 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2669 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2670 07ed472d 2019-07-12 stsp
2671 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2672 7f47418f 2019-12-20 stsp if (err) {
2673 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2674 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2675 7f47418f 2019-12-20 stsp goto done;
2676 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2677 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2678 7f47418f 2019-12-20 stsp if (err)
2679 7f47418f 2019-12-20 stsp return err;
2680 7f47418f 2019-12-20 stsp }
2681 07ed472d 2019-07-12 stsp
2682 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2683 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2684 07ed472d 2019-07-12 stsp if (err)
2685 07ed472d 2019-07-12 stsp goto done;
2686 07ed472d 2019-07-12 stsp
2687 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2688 07ed472d 2019-07-12 stsp if (err)
2689 07ed472d 2019-07-12 stsp goto done;
2690 07ed472d 2019-07-12 stsp
2691 07ed472d 2019-07-12 stsp if (entry_name &&
2692 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2693 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2694 07ed472d 2019-07-12 stsp goto done;
2695 07ed472d 2019-07-12 stsp }
2696 07ed472d 2019-07-12 stsp
2697 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2698 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2699 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2700 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2701 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2702 07ed472d 2019-07-12 stsp arg.repo = repo;
2703 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2704 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2705 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2706 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2707 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2708 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2709 07ed472d 2019-07-12 stsp done:
2710 07ed472d 2019-07-12 stsp if (tree)
2711 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2712 07ed472d 2019-07-12 stsp if (commit)
2713 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2714 6ced4176 2019-07-12 stsp return err;
2715 6ced4176 2019-07-12 stsp }
2716 6ced4176 2019-07-12 stsp
2717 9d31a1d8 2018-03-11 stsp const struct got_error *
2718 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2719 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2720 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2721 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2722 9d31a1d8 2018-03-11 stsp {
2723 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2724 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2725 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2726 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2727 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2728 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2729 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2730 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2731 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2732 f2ea84fa 2019-07-27 stsp int entry_type;
2733 f2ea84fa 2019-07-27 stsp char *relpath;
2734 f2ea84fa 2019-07-27 stsp char *entry_name;
2735 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2736 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2737 9d31a1d8 2018-03-11 stsp
2738 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2739 f2ea84fa 2019-07-27 stsp
2740 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2741 9d31a1d8 2018-03-11 stsp if (err)
2742 9d31a1d8 2018-03-11 stsp return err;
2743 945f9229 2022-04-16 thomas
2744 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2745 945f9229 2022-04-16 thomas worktree->base_commit_id);
2746 945f9229 2022-04-16 thomas if (err)
2747 945f9229 2022-04-16 thomas goto done;
2748 93a30277 2018-12-24 stsp
2749 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2750 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2751 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2752 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2753 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2754 f2ea84fa 2019-07-27 stsp goto done;
2755 f2ea84fa 2019-07-27 stsp }
2756 6ced4176 2019-07-12 stsp
2757 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2758 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2759 945f9229 2022-04-16 thomas worktree, repo);
2760 f2ea84fa 2019-07-27 stsp if (err) {
2761 f2ea84fa 2019-07-27 stsp free(tpd);
2762 6ced4176 2019-07-12 stsp goto done;
2763 6ced4176 2019-07-12 stsp }
2764 f2ea84fa 2019-07-27 stsp
2765 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2766 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2767 f2ea84fa 2019-07-27 stsp if (err) {
2768 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2769 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2770 f2ea84fa 2019-07-27 stsp free(tpd);
2771 f2ea84fa 2019-07-27 stsp goto done;
2772 f2ea84fa 2019-07-27 stsp }
2773 f2ea84fa 2019-07-27 stsp } else
2774 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2775 f2ea84fa 2019-07-27 stsp
2776 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2777 6ced4176 2019-07-12 stsp }
2778 6ced4176 2019-07-12 stsp
2779 51514078 2018-12-25 stsp /*
2780 51514078 2018-12-25 stsp * Read the file index.
2781 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2782 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2783 51514078 2018-12-25 stsp */
2784 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2785 8da9e5f4 2019-01-12 stsp if (err)
2786 c4cdcb68 2019-04-03 stsp goto done;
2787 c4cdcb68 2019-04-03 stsp
2788 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2789 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2790 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2791 f2ea84fa 2019-07-27 stsp
2792 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2793 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2794 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2795 f2ea84fa 2019-07-27 stsp if (err)
2796 f2ea84fa 2019-07-27 stsp break;
2797 f2ea84fa 2019-07-27 stsp
2798 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2799 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2800 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2801 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2802 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2803 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2804 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2805 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2806 f2ea84fa 2019-07-27 stsp if (err)
2807 f2ea84fa 2019-07-27 stsp break;
2808 f2ea84fa 2019-07-27 stsp
2809 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2810 711d9cd9 2019-07-12 stsp }
2811 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2812 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2813 af12c6b9 2019-06-04 stsp err = sync_err;
2814 9d31a1d8 2018-03-11 stsp done:
2815 9c6338c4 2019-06-02 stsp free(fileindex_path);
2816 edfa7d7f 2018-09-11 stsp if (tree)
2817 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2818 9d31a1d8 2018-03-11 stsp if (commit)
2819 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2820 5ade8233 2019-07-12 stsp if (fileindex)
2821 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2822 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2823 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2824 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2825 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2826 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2827 f2ea84fa 2019-07-27 stsp free(tpd);
2828 f2ea84fa 2019-07-27 stsp }
2829 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2830 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2831 9d31a1d8 2018-03-11 stsp err = unlockerr;
2832 f8d1f275 2019-02-04 stsp return err;
2833 f8d1f275 2019-02-04 stsp }
2834 f8d1f275 2019-02-04 stsp
2835 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2836 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2837 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2838 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2839 234035bc 2019-06-01 stsp void *progress_arg;
2840 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2841 234035bc 2019-06-01 stsp void *cancel_arg;
2842 f69721c3 2019-10-21 stsp const char *label_orig;
2843 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2844 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2845 234035bc 2019-06-01 stsp };
2846 234035bc 2019-06-01 stsp
2847 234035bc 2019-06-01 stsp static const struct got_error *
2848 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2849 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2850 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2851 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2852 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2853 234035bc 2019-06-01 stsp {
2854 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2855 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2856 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2857 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2858 234035bc 2019-06-01 stsp struct stat sb;
2859 234035bc 2019-06-01 stsp unsigned char status;
2860 234035bc 2019-06-01 stsp int local_changes_subsumed;
2861 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2862 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2863 234035bc 2019-06-01 stsp
2864 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2865 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2866 d6c87207 2019-08-02 stsp strlen(path2));
2867 1ee397ad 2019-07-12 stsp if (ie == NULL)
2868 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2869 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2870 234035bc 2019-06-01 stsp
2871 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2872 234035bc 2019-06-01 stsp path2) == -1)
2873 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2874 234035bc 2019-06-01 stsp
2875 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2876 7f91a133 2019-12-13 stsp repo);
2877 234035bc 2019-06-01 stsp if (err)
2878 234035bc 2019-06-01 stsp goto done;
2879 234035bc 2019-06-01 stsp
2880 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2881 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2882 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2883 234035bc 2019-06-01 stsp goto done;
2884 234035bc 2019-06-01 stsp }
2885 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2886 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2887 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2888 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2889 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2890 234035bc 2019-06-01 stsp goto done;
2891 234035bc 2019-06-01 stsp }
2892 234035bc 2019-06-01 stsp
2893 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2894 36bf999c 2020-07-23 stsp char *link_target2;
2895 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2896 36bf999c 2020-07-23 stsp if (err)
2897 36bf999c 2020-07-23 stsp goto done;
2898 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2899 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2900 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2901 36bf999c 2020-07-23 stsp free(link_target2);
2902 af57b12a 2020-07-23 stsp } else {
2903 1af628f4 2021-06-03 stsp int fd;
2904 1af628f4 2021-06-03 stsp
2905 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2906 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2907 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2908 1af628f4 2021-06-03 stsp goto done;
2909 1af628f4 2021-06-03 stsp }
2910 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2911 1af628f4 2021-06-03 stsp f_orig, blob1);
2912 1af628f4 2021-06-03 stsp if (err)
2913 1af628f4 2021-06-03 stsp goto done;
2914 1af628f4 2021-06-03 stsp
2915 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2916 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2917 1af628f4 2021-06-03 stsp goto done;
2918 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2919 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2920 1af628f4 2021-06-03 stsp if (err)
2921 1af628f4 2021-06-03 stsp goto done;
2922 1af628f4 2021-06-03 stsp
2923 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2924 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2925 1af628f4 2021-06-03 stsp if (fd == -1) {
2926 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2927 1af628f4 2021-06-03 stsp ondisk_path);
2928 1af628f4 2021-06-03 stsp goto done;
2929 1af628f4 2021-06-03 stsp }
2930 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2931 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2932 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2933 1af628f4 2021-06-03 stsp ondisk_path);
2934 1af628f4 2021-06-03 stsp close(fd);
2935 1af628f4 2021-06-03 stsp goto done;
2936 1af628f4 2021-06-03 stsp }
2937 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2938 1af628f4 2021-06-03 stsp if (err)
2939 1af628f4 2021-06-03 stsp goto done;
2940 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2941 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2942 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2943 1af628f4 2021-06-03 stsp goto done;
2944 1af628f4 2021-06-03 stsp }
2945 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2946 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2947 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
2948 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2949 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2950 af57b12a 2020-07-23 stsp }
2951 234035bc 2019-06-01 stsp } else if (blob1) {
2952 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2953 d6c87207 2019-08-02 stsp strlen(path1));
2954 1ee397ad 2019-07-12 stsp if (ie == NULL)
2955 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2956 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2957 2b92fad7 2019-06-02 stsp
2958 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2959 2b92fad7 2019-06-02 stsp path1) == -1)
2960 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2961 2b92fad7 2019-06-02 stsp
2962 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2963 7f91a133 2019-12-13 stsp repo);
2964 2b92fad7 2019-06-02 stsp if (err)
2965 2b92fad7 2019-06-02 stsp goto done;
2966 2b92fad7 2019-06-02 stsp
2967 2b92fad7 2019-06-02 stsp switch (status) {
2968 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2969 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2970 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2971 1ee397ad 2019-07-12 stsp if (err)
2972 1ee397ad 2019-07-12 stsp goto done;
2973 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2974 2b92fad7 2019-06-02 stsp if (err)
2975 2b92fad7 2019-06-02 stsp goto done;
2976 2b92fad7 2019-06-02 stsp if (ie)
2977 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2978 2b92fad7 2019-06-02 stsp break;
2979 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2980 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2981 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2982 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2983 1ee397ad 2019-07-12 stsp if (err)
2984 1ee397ad 2019-07-12 stsp goto done;
2985 2b92fad7 2019-06-02 stsp if (ie)
2986 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2987 2b92fad7 2019-06-02 stsp break;
2988 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2989 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2990 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2991 e8f02263 2022-01-23 thomas off_t blob1_size;
2992 0a22ca1a 2020-09-23 stsp /*
2993 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2994 0a22ca1a 2020-09-23 stsp * exists in the repository.
2995 0a22ca1a 2020-09-23 stsp */
2996 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
2997 e8f02263 2022-01-23 thomas &blob1_size, path1);
2998 0a22ca1a 2020-09-23 stsp if (err)
2999 0a22ca1a 2020-09-23 stsp goto done;
3000 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3001 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3002 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3003 0a22ca1a 2020-09-23 stsp if (err)
3004 0a22ca1a 2020-09-23 stsp goto done;
3005 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3006 0a22ca1a 2020-09-23 stsp path1);
3007 0a22ca1a 2020-09-23 stsp if (err)
3008 0a22ca1a 2020-09-23 stsp goto done;
3009 0a22ca1a 2020-09-23 stsp if (ie)
3010 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3011 0a22ca1a 2020-09-23 stsp ie);
3012 0a22ca1a 2020-09-23 stsp } else {
3013 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3014 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3015 0a22ca1a 2020-09-23 stsp }
3016 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3017 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3018 0a22ca1a 2020-09-23 stsp free(id);
3019 0a22ca1a 2020-09-23 stsp if (err)
3020 0a22ca1a 2020-09-23 stsp goto done;
3021 0a22ca1a 2020-09-23 stsp break;
3022 0a22ca1a 2020-09-23 stsp }
3023 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3024 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3025 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3026 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3027 1ee397ad 2019-07-12 stsp if (err)
3028 1ee397ad 2019-07-12 stsp goto done;
3029 2b92fad7 2019-06-02 stsp break;
3030 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3031 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3032 1ee397ad 2019-07-12 stsp if (err)
3033 1ee397ad 2019-07-12 stsp goto done;
3034 2b92fad7 2019-06-02 stsp break;
3035 2b92fad7 2019-06-02 stsp default:
3036 2b92fad7 2019-06-02 stsp break;
3037 2b92fad7 2019-06-02 stsp }
3038 234035bc 2019-06-01 stsp } else if (blob2) {
3039 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3040 234035bc 2019-06-01 stsp path2) == -1)
3041 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3042 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3043 d6c87207 2019-08-02 stsp strlen(path2));
3044 234035bc 2019-06-01 stsp if (ie) {
3045 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3046 7f91a133 2019-12-13 stsp -1, NULL, repo);
3047 234035bc 2019-06-01 stsp if (err)
3048 234035bc 2019-06-01 stsp goto done;
3049 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3050 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3051 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3052 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3053 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3054 1ee397ad 2019-07-12 stsp status, path2);
3055 234035bc 2019-06-01 stsp goto done;
3056 234035bc 2019-06-01 stsp }
3057 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3058 36bf999c 2020-07-23 stsp char *link_target2;
3059 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3060 36bf999c 2020-07-23 stsp blob2);
3061 36bf999c 2020-07-23 stsp if (err)
3062 36bf999c 2020-07-23 stsp goto done;
3063 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3064 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3065 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3066 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3067 36bf999c 2020-07-23 stsp free(link_target2);
3068 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3069 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3070 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3071 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3072 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3073 526a746f 2020-07-23 stsp a->progress_arg);
3074 dfe9fba0 2020-07-23 stsp } else {
3075 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3076 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3077 526a746f 2020-07-23 stsp }
3078 dfe9fba0 2020-07-23 stsp if (err)
3079 dfe9fba0 2020-07-23 stsp goto done;
3080 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3081 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3082 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3083 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3084 234035bc 2019-06-01 stsp if (err)
3085 234035bc 2019-06-01 stsp goto done;
3086 234035bc 2019-06-01 stsp }
3087 234035bc 2019-06-01 stsp } else {
3088 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3089 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3090 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3091 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3092 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3093 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3094 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3095 2e1fa222 2020-07-23 stsp } else {
3096 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3097 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3098 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3099 2e1fa222 2020-07-23 stsp }
3100 234035bc 2019-06-01 stsp if (err)
3101 234035bc 2019-06-01 stsp goto done;
3102 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3103 234035bc 2019-06-01 stsp if (err)
3104 234035bc 2019-06-01 stsp goto done;
3105 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3106 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3107 3969253a 2020-03-07 stsp if (err) {
3108 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3109 3969253a 2020-03-07 stsp goto done;
3110 3969253a 2020-03-07 stsp }
3111 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3112 2b92fad7 2019-06-02 stsp if (err) {
3113 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3114 2b92fad7 2019-06-02 stsp goto done;
3115 2b92fad7 2019-06-02 stsp }
3116 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3117 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3118 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3119 2e1fa222 2020-07-23 stsp }
3120 234035bc 2019-06-01 stsp }
3121 234035bc 2019-06-01 stsp }
3122 234035bc 2019-06-01 stsp done:
3123 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3124 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3125 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3126 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3127 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3128 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3129 1af628f4 2021-06-03 stsp free(id_str);
3130 54d5be07 2021-06-03 stsp free(label_deriv2);
3131 234035bc 2019-06-01 stsp free(ondisk_path);
3132 234035bc 2019-06-01 stsp return err;
3133 234035bc 2019-06-01 stsp }
3134 234035bc 2019-06-01 stsp
3135 69de9dd4 2021-09-03 stsp static const struct got_error *
3136 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3137 69de9dd4 2021-09-03 stsp {
3138 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3139 69de9dd4 2021-09-03 stsp
3140 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3141 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3142 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3143 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3144 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3145 69de9dd4 2021-09-03 stsp
3146 69de9dd4 2021-09-03 stsp return NULL;
3147 69de9dd4 2021-09-03 stsp }
3148 69de9dd4 2021-09-03 stsp
3149 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3150 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3151 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3152 234035bc 2019-06-01 stsp struct got_repository *repo;
3153 234035bc 2019-06-01 stsp };
3154 234035bc 2019-06-01 stsp
3155 234035bc 2019-06-01 stsp static const struct got_error *
3156 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3157 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3158 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3159 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3160 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3161 234035bc 2019-06-01 stsp {
3162 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3163 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3164 234035bc 2019-06-01 stsp unsigned char status;
3165 234035bc 2019-06-01 stsp struct stat sb;
3166 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3167 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3168 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3169 234035bc 2019-06-01 stsp char *ondisk_path;
3170 234035bc 2019-06-01 stsp
3171 69de9dd4 2021-09-03 stsp if (id == NULL)
3172 69de9dd4 2021-09-03 stsp return NULL;
3173 234035bc 2019-06-01 stsp
3174 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3175 69de9dd4 2021-09-03 stsp if (ie == NULL)
3176 69de9dd4 2021-09-03 stsp return NULL;
3177 69de9dd4 2021-09-03 stsp
3178 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3179 234035bc 2019-06-01 stsp == -1)
3180 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3181 234035bc 2019-06-01 stsp
3182 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3183 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3184 5546d466 2021-09-02 stsp free(ondisk_path);
3185 234035bc 2019-06-01 stsp if (err)
3186 234035bc 2019-06-01 stsp return err;
3187 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3188 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3189 234035bc 2019-06-01 stsp
3190 234035bc 2019-06-01 stsp return NULL;
3191 234035bc 2019-06-01 stsp }
3192 234035bc 2019-06-01 stsp
3193 818c7501 2019-07-11 stsp static const struct got_error *
3194 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3195 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3196 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3197 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3198 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3199 234035bc 2019-06-01 stsp {
3200 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3201 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3202 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3203 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3204 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3205 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3206 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3207 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3208 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3209 234035bc 2019-06-01 stsp
3210 03415a1a 2019-06-02 stsp if (commit_id1) {
3211 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3212 945f9229 2022-04-16 thomas if (err)
3213 945f9229 2022-04-16 thomas goto done;
3214 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3215 03415a1a 2019-06-02 stsp worktree->path_prefix);
3216 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3217 03415a1a 2019-06-02 stsp goto done;
3218 69d57f3d 2020-07-31 stsp }
3219 69d57f3d 2020-07-31 stsp if (tree_id1) {
3220 69d57f3d 2020-07-31 stsp char *id_str;
3221 03415a1a 2019-06-02 stsp
3222 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3223 03415a1a 2019-06-02 stsp if (err)
3224 03415a1a 2019-06-02 stsp goto done;
3225 f69721c3 2019-10-21 stsp
3226 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3227 f69721c3 2019-10-21 stsp if (err)
3228 f69721c3 2019-10-21 stsp goto done;
3229 f69721c3 2019-10-21 stsp
3230 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3231 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3232 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3233 f69721c3 2019-10-21 stsp free(id_str);
3234 f69721c3 2019-10-21 stsp goto done;
3235 f69721c3 2019-10-21 stsp }
3236 f69721c3 2019-10-21 stsp free(id_str);
3237 a0f32f33 2022-06-13 thomas
3238 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3239 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3240 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3241 a0f32f33 2022-06-13 thomas goto done;
3242 a0f32f33 2022-06-13 thomas }
3243 03415a1a 2019-06-02 stsp }
3244 234035bc 2019-06-01 stsp
3245 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3246 945f9229 2022-04-16 thomas if (err)
3247 945f9229 2022-04-16 thomas goto done;
3248 945f9229 2022-04-16 thomas
3249 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3250 234035bc 2019-06-01 stsp worktree->path_prefix);
3251 234035bc 2019-06-01 stsp if (err)
3252 234035bc 2019-06-01 stsp goto done;
3253 234035bc 2019-06-01 stsp
3254 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3255 234035bc 2019-06-01 stsp if (err)
3256 234035bc 2019-06-01 stsp goto done;
3257 234035bc 2019-06-01 stsp
3258 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3259 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3260 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3261 19a6a6b5 2022-07-01 thomas goto done;
3262 19a6a6b5 2022-07-01 thomas }
3263 19a6a6b5 2022-07-01 thomas
3264 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3265 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3266 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3267 a0f32f33 2022-06-13 thomas goto done;
3268 a0f32f33 2022-06-13 thomas }
3269 a0f32f33 2022-06-13 thomas
3270 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3271 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3272 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3273 19a6a6b5 2022-07-01 thomas goto done;
3274 19a6a6b5 2022-07-01 thomas }
3275 19a6a6b5 2022-07-01 thomas
3276 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3277 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3278 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3279 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3280 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3281 69de9dd4 2021-09-03 stsp if (err)
3282 69de9dd4 2021-09-03 stsp goto done;
3283 69de9dd4 2021-09-03 stsp
3284 234035bc 2019-06-01 stsp arg.worktree = worktree;
3285 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3286 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3287 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3288 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3289 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3290 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3291 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3292 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3293 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3294 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3295 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3296 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3297 af12c6b9 2019-06-04 stsp err = sync_err;
3298 234035bc 2019-06-01 stsp done:
3299 945f9229 2022-04-16 thomas if (commit1)
3300 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3301 945f9229 2022-04-16 thomas if (commit2)
3302 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3303 234035bc 2019-06-01 stsp if (tree1)
3304 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3305 234035bc 2019-06-01 stsp if (tree2)
3306 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3307 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3308 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3309 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3310 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3311 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3312 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3313 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3314 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3315 f69721c3 2019-10-21 stsp free(label_orig);
3316 818c7501 2019-07-11 stsp return err;
3317 818c7501 2019-07-11 stsp }
3318 234035bc 2019-06-01 stsp
3319 818c7501 2019-07-11 stsp const struct got_error *
3320 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3321 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3322 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3323 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3324 818c7501 2019-07-11 stsp {
3325 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3326 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3327 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3328 818c7501 2019-07-11 stsp
3329 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3330 818c7501 2019-07-11 stsp if (err)
3331 818c7501 2019-07-11 stsp return err;
3332 818c7501 2019-07-11 stsp
3333 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3334 818c7501 2019-07-11 stsp if (err)
3335 818c7501 2019-07-11 stsp goto done;
3336 818c7501 2019-07-11 stsp
3337 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3338 69de9dd4 2021-09-03 stsp worktree);
3339 818c7501 2019-07-11 stsp if (err)
3340 818c7501 2019-07-11 stsp goto done;
3341 818c7501 2019-07-11 stsp
3342 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3343 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3344 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3345 818c7501 2019-07-11 stsp done:
3346 818c7501 2019-07-11 stsp if (fileindex)
3347 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3348 818c7501 2019-07-11 stsp free(fileindex_path);
3349 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3350 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3351 234035bc 2019-06-01 stsp err = unlockerr;
3352 234035bc 2019-06-01 stsp return err;
3353 234035bc 2019-06-01 stsp }
3354 234035bc 2019-06-01 stsp
3355 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3356 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3357 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3358 927df6b7 2019-02-10 stsp const char *status_path;
3359 927df6b7 2019-02-10 stsp size_t status_path_len;
3360 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3361 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3362 f8d1f275 2019-02-04 stsp void *status_arg;
3363 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3364 f8d1f275 2019-02-04 stsp void *cancel_arg;
3365 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3366 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3367 f2a9dc41 2019-12-13 tracey int report_unchanged;
3368 3143d852 2020-06-25 stsp int no_ignores;
3369 f8d1f275 2019-02-04 stsp };
3370 88d0e355 2019-08-03 stsp
3371 f8d1f275 2019-02-04 stsp static const struct got_error *
3372 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3373 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3374 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3375 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3376 927df6b7 2019-02-10 stsp {
3377 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3378 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3379 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3380 927df6b7 2019-02-10 stsp struct stat sb;
3381 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3382 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3383 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3384 927df6b7 2019-02-10 stsp
3385 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3386 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3387 98eaaa12 2019-08-03 stsp if (err)
3388 98eaaa12 2019-08-03 stsp return err;
3389 98eaaa12 2019-08-03 stsp
3390 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3391 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3392 98eaaa12 2019-08-03 stsp return NULL;
3393 98eaaa12 2019-08-03 stsp
3394 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3395 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3396 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3397 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3398 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3399 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3400 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3401 43010591 2023-02-17 thomas &staged_blob_id, ie);
3402 98eaaa12 2019-08-03 stsp }
3403 98eaaa12 2019-08-03 stsp
3404 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3405 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3406 927df6b7 2019-02-10 stsp }
3407 927df6b7 2019-02-10 stsp
3408 927df6b7 2019-02-10 stsp static const struct got_error *
3409 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3410 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3411 f8d1f275 2019-02-04 stsp {
3412 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3413 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3414 f8d1f275 2019-02-04 stsp char *abspath;
3415 f8d1f275 2019-02-04 stsp
3416 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3417 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3418 0584f854 2019-04-06 stsp
3419 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3420 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3421 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3422 927df6b7 2019-02-10 stsp return NULL;
3423 927df6b7 2019-02-10 stsp
3424 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3425 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3426 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3427 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3428 f8d1f275 2019-02-04 stsp } else {
3429 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3430 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3431 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3432 f8d1f275 2019-02-04 stsp }
3433 f8d1f275 2019-02-04 stsp
3434 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3435 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3436 f8d1f275 2019-02-04 stsp free(abspath);
3437 9d31a1d8 2018-03-11 stsp return err;
3438 9d31a1d8 2018-03-11 stsp }
3439 f8d1f275 2019-02-04 stsp
3440 f8d1f275 2019-02-04 stsp static const struct got_error *
3441 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3442 f8d1f275 2019-02-04 stsp {
3443 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3444 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3445 2ec1f75b 2019-03-26 stsp unsigned char status;
3446 927df6b7 2019-02-10 stsp
3447 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3448 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3449 0584f854 2019-04-06 stsp
3450 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3451 927df6b7 2019-02-10 stsp return NULL;
3452 927df6b7 2019-02-10 stsp
3453 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3454 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3455 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3456 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3457 2ec1f75b 2019-03-26 stsp else
3458 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3459 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3460 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3461 6841da00 2019-08-08 stsp }
3462 6841da00 2019-08-08 stsp
3463 ef20f542 2022-06-26 thomas static void
3464 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3465 6841da00 2019-08-08 stsp {
3466 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3467 6841da00 2019-08-08 stsp
3468 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3469 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3470 21c2d8be 2023-01-10 thomas
3471 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3472 6841da00 2019-08-08 stsp }
3473 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3474 6841da00 2019-08-08 stsp }
3475 6841da00 2019-08-08 stsp
3476 6841da00 2019-08-08 stsp static const struct got_error *
3477 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3478 6841da00 2019-08-08 stsp {
3479 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3480 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3481 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3482 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3483 6841da00 2019-08-08 stsp size_t linesize = 0;
3484 6841da00 2019-08-08 stsp ssize_t linelen;
3485 6841da00 2019-08-08 stsp
3486 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3487 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3488 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3489 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3490 6841da00 2019-08-08 stsp
3491 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3492 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3493 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3494 bd8de430 2019-10-04 stsp
3495 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3496 bd8de430 2019-10-04 stsp if (line[0] == '#')
3497 bd8de430 2019-10-04 stsp continue;
3498 bd8de430 2019-10-04 stsp
3499 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3500 bd8de430 2019-10-04 stsp if (line[0] == '!')
3501 bd8de430 2019-10-04 stsp continue;
3502 bd8de430 2019-10-04 stsp
3503 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3504 6841da00 2019-08-08 stsp line) == -1) {
3505 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3506 6841da00 2019-08-08 stsp goto done;
3507 6841da00 2019-08-08 stsp }
3508 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3509 6841da00 2019-08-08 stsp if (err)
3510 6841da00 2019-08-08 stsp goto done;
3511 6841da00 2019-08-08 stsp }
3512 6841da00 2019-08-08 stsp if (ferror(f)) {
3513 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3514 6841da00 2019-08-08 stsp goto done;
3515 6841da00 2019-08-08 stsp }
3516 6841da00 2019-08-08 stsp
3517 6841da00 2019-08-08 stsp dirpath = strdup(path);
3518 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3519 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3520 6841da00 2019-08-08 stsp goto done;
3521 6841da00 2019-08-08 stsp }
3522 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3523 6841da00 2019-08-08 stsp done:
3524 6841da00 2019-08-08 stsp free(line);
3525 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3526 6841da00 2019-08-08 stsp free(dirpath);
3527 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3528 6841da00 2019-08-08 stsp }
3529 6841da00 2019-08-08 stsp return err;
3530 6841da00 2019-08-08 stsp }
3531 6841da00 2019-08-08 stsp
3532 ef20f542 2022-06-26 thomas static int
3533 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3534 6841da00 2019-08-08 stsp {
3535 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3536 bd8de430 2019-10-04 stsp
3537 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3538 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3539 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3540 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3541 bd8de430 2019-10-04 stsp
3542 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3543 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3544 6841da00 2019-08-08 stsp
3545 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3546 bd8de430 2019-10-04 stsp continue;
3547 bd8de430 2019-10-04 stsp pattern += 3;
3548 bd8de430 2019-10-04 stsp p = path;
3549 bd8de430 2019-10-04 stsp while (*p) {
3550 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3551 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3552 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3553 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3554 bd8de430 2019-10-04 stsp p++;
3555 bd8de430 2019-10-04 stsp while (*p == '/')
3556 bd8de430 2019-10-04 stsp p++;
3557 bd8de430 2019-10-04 stsp continue;
3558 bd8de430 2019-10-04 stsp }
3559 bd8de430 2019-10-04 stsp return 1;
3560 bd8de430 2019-10-04 stsp }
3561 bd8de430 2019-10-04 stsp }
3562 bd8de430 2019-10-04 stsp }
3563 bd8de430 2019-10-04 stsp
3564 6841da00 2019-08-08 stsp /*
3565 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3566 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3567 6841da00 2019-08-08 stsp * ignores backwards.
3568 6841da00 2019-08-08 stsp */
3569 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3570 6841da00 2019-08-08 stsp while (pe) {
3571 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3572 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3573 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3574 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3575 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3576 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3577 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3578 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3579 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3580 6841da00 2019-08-08 stsp continue;
3581 6841da00 2019-08-08 stsp return 1;
3582 6841da00 2019-08-08 stsp }
3583 6841da00 2019-08-08 stsp }
3584 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3585 6841da00 2019-08-08 stsp }
3586 6841da00 2019-08-08 stsp
3587 6841da00 2019-08-08 stsp return 0;
3588 6841da00 2019-08-08 stsp }
3589 6841da00 2019-08-08 stsp
3590 6841da00 2019-08-08 stsp static const struct got_error *
3591 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3592 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3593 6841da00 2019-08-08 stsp {
3594 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3595 6841da00 2019-08-08 stsp char *ignorespath;
3596 886cec17 2019-12-15 stsp int fd = -1;
3597 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3598 6841da00 2019-08-08 stsp
3599 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3600 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3601 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3602 6841da00 2019-08-08 stsp
3603 886cec17 2019-12-15 stsp if (dirfd != -1) {
3604 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3605 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3606 886cec17 2019-12-15 stsp if (fd == -1) {
3607 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3608 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3609 886cec17 2019-12-15 stsp ignorespath);
3610 886cec17 2019-12-15 stsp } else {
3611 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3612 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3613 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3614 886cec17 2019-12-15 stsp ignorespath);
3615 886cec17 2019-12-15 stsp else {
3616 886cec17 2019-12-15 stsp fd = -1;
3617 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3618 886cec17 2019-12-15 stsp }
3619 886cec17 2019-12-15 stsp }
3620 886cec17 2019-12-15 stsp } else {
3621 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3622 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3623 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3624 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3625 886cec17 2019-12-15 stsp ignorespath);
3626 886cec17 2019-12-15 stsp } else
3627 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3628 886cec17 2019-12-15 stsp }
3629 6841da00 2019-08-08 stsp
3630 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3631 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3632 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3633 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3634 6841da00 2019-08-08 stsp free(ignorespath);
3635 6841da00 2019-08-08 stsp return err;
3636 f8d1f275 2019-02-04 stsp }
3637 f8d1f275 2019-02-04 stsp
3638 f8d1f275 2019-02-04 stsp static const struct got_error *
3639 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3640 6092c299 2021-10-04 thomas int dirfd)
3641 f8d1f275 2019-02-04 stsp {
3642 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3643 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3644 f8d1f275 2019-02-04 stsp char *path = NULL;
3645 6092c299 2021-10-04 thomas
3646 6092c299 2021-10-04 thomas if (ignore != NULL)
3647 6092c299 2021-10-04 thomas *ignore = 0;
3648 f8d1f275 2019-02-04 stsp
3649 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3650 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3651 0584f854 2019-04-06 stsp
3652 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3653 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3654 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3655 f8d1f275 2019-02-04 stsp } else {
3656 f8d1f275 2019-02-04 stsp path = de->d_name;
3657 f8d1f275 2019-02-04 stsp }
3658 f8d1f275 2019-02-04 stsp
3659 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3660 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3661 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3662 6092c299 2021-10-04 thomas *ignore = 1;
3663 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3664 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3665 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3666 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3667 f8d1f275 2019-02-04 stsp if (parent_path[0])
3668 f8d1f275 2019-02-04 stsp free(path);
3669 b72f483a 2019-02-05 stsp return err;
3670 f8d1f275 2019-02-04 stsp }
3671 f8d1f275 2019-02-04 stsp
3672 347d1d3e 2019-07-12 stsp static const struct got_error *
3673 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3674 3143d852 2020-06-25 stsp {
3675 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3676 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3677 3143d852 2020-06-25 stsp
3678 3143d852 2020-06-25 stsp if (a->no_ignores)
3679 3143d852 2020-06-25 stsp return NULL;
3680 3143d852 2020-06-25 stsp
3681 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3682 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3683 3143d852 2020-06-25 stsp if (err)
3684 3143d852 2020-06-25 stsp return err;
3685 3143d852 2020-06-25 stsp
3686 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3687 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3688 3143d852 2020-06-25 stsp
3689 3143d852 2020-06-25 stsp return err;
3690 3143d852 2020-06-25 stsp }
3691 3143d852 2020-06-25 stsp
3692 3143d852 2020-06-25 stsp static const struct got_error *
3693 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3694 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3695 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3696 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3697 abb4604f 2019-07-27 stsp {
3698 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3699 abb4604f 2019-07-27 stsp struct stat sb;
3700 ff56836b 2021-07-08 stsp
3701 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3702 abb4604f 2019-07-27 stsp if (ie)
3703 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3704 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3705 abb4604f 2019-07-27 stsp
3706 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3707 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3708 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3709 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3710 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3711 abb4604f 2019-07-27 stsp }
3712 abb4604f 2019-07-27 stsp
3713 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3714 fe1d8685 2022-02-12 thomas return NULL;
3715 fe1d8685 2022-02-12 thomas
3716 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3717 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3718 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3719 abb4604f 2019-07-27 stsp
3720 abb4604f 2019-07-27 stsp return NULL;
3721 3143d852 2020-06-25 stsp }
3722 3143d852 2020-06-25 stsp
3723 3143d852 2020-06-25 stsp static const struct got_error *
3724 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3725 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3726 3143d852 2020-06-25 stsp {
3727 3143d852 2020-06-25 stsp const struct got_error *err;
3728 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3729 3143d852 2020-06-25 stsp
3730 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3731 3143d852 2020-06-25 stsp ".cvsignore");
3732 3143d852 2020-06-25 stsp if (err)
3733 3143d852 2020-06-25 stsp return err;
3734 3143d852 2020-06-25 stsp
3735 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3736 3143d852 2020-06-25 stsp ".gitignore");
3737 3143d852 2020-06-25 stsp if (err)
3738 3143d852 2020-06-25 stsp return err;
3739 3143d852 2020-06-25 stsp
3740 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3741 3143d852 2020-06-25 stsp if (err) {
3742 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3743 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3744 3143d852 2020-06-25 stsp return err;
3745 3143d852 2020-06-25 stsp }
3746 3143d852 2020-06-25 stsp for (;;) {
3747 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3748 3143d852 2020-06-25 stsp ".cvsignore");
3749 3143d852 2020-06-25 stsp if (err)
3750 3143d852 2020-06-25 stsp break;
3751 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3752 3143d852 2020-06-25 stsp ".gitignore");
3753 3143d852 2020-06-25 stsp if (err)
3754 3143d852 2020-06-25 stsp break;
3755 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3756 3143d852 2020-06-25 stsp if (err) {
3757 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3758 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3759 3143d852 2020-06-25 stsp break;
3760 3143d852 2020-06-25 stsp }
3761 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3762 ff56836b 2021-07-08 stsp break;
3763 b737c85a 2020-06-26 stsp free(parent_path);
3764 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3765 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3766 3143d852 2020-06-25 stsp }
3767 3143d852 2020-06-25 stsp
3768 b737c85a 2020-06-26 stsp free(parent_path);
3769 b737c85a 2020-06-26 stsp free(next_parent_path);
3770 3143d852 2020-06-25 stsp return err;
3771 abb4604f 2019-07-27 stsp }
3772 abb4604f 2019-07-27 stsp
3773 abb4604f 2019-07-27 stsp static const struct got_error *
3774 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3775 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3776 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3777 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3778 f2a9dc41 2019-12-13 tracey int report_unchanged)
3779 f8d1f275 2019-02-04 stsp {
3780 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3781 6fc93f37 2019-12-13 stsp int fd = -1;
3782 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3783 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3784 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3785 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3786 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3787 3143d852 2020-06-25 stsp
3788 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3789 f8d1f275 2019-02-04 stsp
3790 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3791 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3792 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3793 8dc303cc 2019-07-27 stsp
3794 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3795 a78810f8 2022-03-13 thomas if (ie) {
3796 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3797 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3798 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3799 a78810f8 2022-03-13 thomas goto done;
3800 a78810f8 2022-03-13 thomas }
3801 a78810f8 2022-03-13 thomas
3802 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3803 6fc93f37 2019-12-13 stsp if (fd == -1) {
3804 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3805 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3806 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3807 ff56836b 2021-07-08 stsp else {
3808 ff56836b 2021-07-08 stsp if (!no_ignores) {
3809 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3810 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3811 ff56836b 2021-07-08 stsp if (err)
3812 ff56836b 2021-07-08 stsp goto done;
3813 ff56836b 2021-07-08 stsp }
3814 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3815 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3816 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3817 ff56836b 2021-07-08 stsp }
3818 abb4604f 2019-07-27 stsp } else {
3819 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3820 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3821 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3822 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3823 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3824 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3825 abb4604f 2019-07-27 stsp arg.status_path = path;
3826 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3827 abb4604f 2019-07-27 stsp arg.repo = repo;
3828 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3829 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3830 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3831 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3832 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3833 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3834 022fae89 2019-12-06 tracey if (!no_ignores) {
3835 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3836 3143d852 2020-06-25 stsp worktree->root_path, path);
3837 3143d852 2020-06-25 stsp if (err)
3838 3143d852 2020-06-25 stsp goto done;
3839 022fae89 2019-12-06 tracey }
3840 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3841 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3842 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3843 927df6b7 2019-02-10 stsp }
3844 3143d852 2020-06-25 stsp done:
3845 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3846 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3847 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3848 927df6b7 2019-02-10 stsp free(ondisk_path);
3849 347d1d3e 2019-07-12 stsp return err;
3850 347d1d3e 2019-07-12 stsp }
3851 347d1d3e 2019-07-12 stsp
3852 347d1d3e 2019-07-12 stsp const struct got_error *
3853 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3854 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3855 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3856 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3857 347d1d3e 2019-07-12 stsp {
3858 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3859 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3860 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3861 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3862 347d1d3e 2019-07-12 stsp
3863 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3864 347d1d3e 2019-07-12 stsp if (err)
3865 347d1d3e 2019-07-12 stsp return err;
3866 347d1d3e 2019-07-12 stsp
3867 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3868 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3869 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3870 f6343036 2021-06-22 stsp no_ignores, 0);
3871 72ea6654 2019-07-27 stsp if (err)
3872 72ea6654 2019-07-27 stsp break;
3873 72ea6654 2019-07-27 stsp }
3874 f8d1f275 2019-02-04 stsp free(fileindex_path);
3875 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3876 f8d1f275 2019-02-04 stsp return err;
3877 f8d1f275 2019-02-04 stsp }
3878 6c7ab921 2019-03-18 stsp
3879 6c7ab921 2019-03-18 stsp const struct got_error *
3880 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3881 6c7ab921 2019-03-18 stsp const char *arg)
3882 6c7ab921 2019-03-18 stsp {
3883 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3884 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3885 6c7ab921 2019-03-18 stsp size_t len;
3886 00bb5ea0 2020-07-23 stsp struct stat sb;
3887 01740607 2020-11-04 stsp char *abspath = NULL;
3888 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3889 6c7ab921 2019-03-18 stsp
3890 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3891 6c7ab921 2019-03-18 stsp
3892 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3893 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3894 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3895 00bb5ea0 2020-07-23 stsp
3896 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3897 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3898 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3899 00bb5ea0 2020-07-23 stsp goto done;
3900 00bb5ea0 2020-07-23 stsp }
3901 727173c3 2020-11-06 stsp sb.st_mode = 0;
3902 00bb5ea0 2020-07-23 stsp }
3903 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3904 00bb5ea0 2020-07-23 stsp /*
3905 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3906 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3907 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3908 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3909 00bb5ea0 2020-07-23 stsp */
3910 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3911 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3912 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3913 00bb5ea0 2020-07-23 stsp goto done;
3914 00bb5ea0 2020-07-23 stsp }
3915 00bb5ea0 2020-07-23 stsp
3916 00bb5ea0 2020-07-23 stsp }
3917 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3918 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3919 00bb5ea0 2020-07-23 stsp if (err)
3920 d0710d08 2019-07-22 stsp goto done;
3921 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3922 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3923 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3924 00bb5ea0 2020-07-23 stsp goto done;
3925 00bb5ea0 2020-07-23 stsp }
3926 00bb5ea0 2020-07-23 stsp } else {
3927 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3928 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3929 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3930 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3931 00bb5ea0 2020-07-23 stsp goto done;
3932 00bb5ea0 2020-07-23 stsp }
3933 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3934 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3935 01740607 2020-11-04 stsp goto done;
3936 01740607 2020-11-04 stsp }
3937 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3938 01740607 2020-11-04 stsp sizeof(canonpath));
3939 01740607 2020-11-04 stsp if (err)
3940 00bb5ea0 2020-07-23 stsp goto done;
3941 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3942 01740607 2020-11-04 stsp if (resolved == NULL) {
3943 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3944 01740607 2020-11-04 stsp goto done;
3945 00bb5ea0 2020-07-23 stsp }
3946 d0710d08 2019-07-22 stsp }
3947 d0710d08 2019-07-22 stsp }
3948 6c7ab921 2019-03-18 stsp
3949 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3950 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3951 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3952 6c7ab921 2019-03-18 stsp goto done;
3953 6c7ab921 2019-03-18 stsp }
3954 6c7ab921 2019-03-18 stsp
3955 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3956 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3957 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3958 f6d88e1a 2019-05-29 stsp if (err)
3959 f6d88e1a 2019-05-29 stsp goto done;
3960 f6d88e1a 2019-05-29 stsp } else {
3961 f6d88e1a 2019-05-29 stsp path = strdup("");
3962 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3963 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3964 f6d88e1a 2019-05-29 stsp goto done;
3965 f6d88e1a 2019-05-29 stsp }
3966 6c7ab921 2019-03-18 stsp }
3967 6c7ab921 2019-03-18 stsp
3968 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3969 6c7ab921 2019-03-18 stsp len = strlen(path);
3970 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3971 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3972 6c7ab921 2019-03-18 stsp len--;
3973 6c7ab921 2019-03-18 stsp }
3974 6c7ab921 2019-03-18 stsp done:
3975 01740607 2020-11-04 stsp free(abspath);
3976 6c7ab921 2019-03-18 stsp free(resolved);
3977 d0710d08 2019-07-22 stsp free(cwd);
3978 6c7ab921 2019-03-18 stsp if (err == NULL)
3979 6c7ab921 2019-03-18 stsp *wt_path = path;
3980 6c7ab921 2019-03-18 stsp else
3981 6c7ab921 2019-03-18 stsp free(path);
3982 d00136be 2019-03-26 stsp return err;
3983 d00136be 2019-03-26 stsp }
3984 d00136be 2019-03-26 stsp
3985 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3986 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3987 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3988 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3989 4e68cba3 2019-11-23 stsp void *progress_arg;
3990 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3991 4e68cba3 2019-11-23 stsp };
3992 4e68cba3 2019-11-23 stsp
3993 4e68cba3 2019-11-23 stsp static const struct got_error *
3994 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3995 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3996 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3997 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3998 07f5b47a 2019-06-02 stsp {
3999 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4000 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4001 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4002 6d022e97 2019-08-04 stsp struct stat sb;
4003 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4004 07f5b47a 2019-06-02 stsp
4005 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4006 dbb83fbd 2019-12-12 stsp relpath) == -1)
4007 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4008 dbb83fbd 2019-12-12 stsp
4009 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4010 6d022e97 2019-08-04 stsp if (ie) {
4011 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4012 12463d8b 2019-12-13 stsp de_name, a->repo);
4013 6d022e97 2019-08-04 stsp if (err)
4014 dbb83fbd 2019-12-12 stsp goto done;
4015 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4016 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4017 dbb83fbd 2019-12-12 stsp goto done;
4018 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4019 dbb83fbd 2019-12-12 stsp if (err)
4020 dbb83fbd 2019-12-12 stsp goto done;
4021 6d022e97 2019-08-04 stsp }
4022 07f5b47a 2019-06-02 stsp
4023 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4024 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4025 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4026 84bf00a6 2022-02-12 thomas else
4027 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4028 dbb83fbd 2019-12-12 stsp goto done;
4029 4e68cba3 2019-11-23 stsp }
4030 07f5b47a 2019-06-02 stsp
4031 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4032 4e68cba3 2019-11-23 stsp if (err)
4033 4e68cba3 2019-11-23 stsp goto done;
4034 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4035 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4036 3969253a 2020-03-07 stsp if (err) {
4037 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4038 3969253a 2020-03-07 stsp goto done;
4039 3969253a 2020-03-07 stsp }
4040 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4041 07f5b47a 2019-06-02 stsp if (err) {
4042 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4043 4e68cba3 2019-11-23 stsp goto done;
4044 07f5b47a 2019-06-02 stsp }
4045 4e68cba3 2019-11-23 stsp done:
4046 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4047 dbb83fbd 2019-12-12 stsp if (err)
4048 dbb83fbd 2019-12-12 stsp return err;
4049 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4050 dbb83fbd 2019-12-12 stsp return NULL;
4051 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4052 07f5b47a 2019-06-02 stsp }
4053 07f5b47a 2019-06-02 stsp
4054 d00136be 2019-03-26 stsp const struct got_error *
4055 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4056 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4057 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4058 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4059 d00136be 2019-03-26 stsp {
4060 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4061 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4062 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4063 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4064 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4065 d00136be 2019-03-26 stsp
4066 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4067 d00136be 2019-03-26 stsp if (err)
4068 d00136be 2019-03-26 stsp return err;
4069 d00136be 2019-03-26 stsp
4070 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4071 d00136be 2019-03-26 stsp if (err)
4072 d00136be 2019-03-26 stsp goto done;
4073 d00136be 2019-03-26 stsp
4074 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4075 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4076 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4077 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4078 4e68cba3 2019-11-23 stsp saa.repo = repo;
4079 4e68cba3 2019-11-23 stsp
4080 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4081 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4082 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4083 1dd54920 2019-05-11 stsp if (err)
4084 af12c6b9 2019-06-04 stsp break;
4085 1dd54920 2019-05-11 stsp }
4086 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4087 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4088 af12c6b9 2019-06-04 stsp err = sync_err;
4089 d00136be 2019-03-26 stsp done:
4090 fb399478 2019-07-12 stsp free(fileindex_path);
4091 d00136be 2019-03-26 stsp if (fileindex)
4092 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4093 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4094 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4095 d00136be 2019-03-26 stsp err = unlockerr;
4096 6c7ab921 2019-03-18 stsp return err;
4097 6c7ab921 2019-03-18 stsp }
4098 17ed4618 2019-06-02 stsp
4099 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4100 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4101 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4102 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4103 f2a9dc41 2019-12-13 tracey void *progress_arg;
4104 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4105 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4106 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4107 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4108 766841c2 2020-08-13 stsp const char *status_codes;
4109 f2a9dc41 2019-12-13 tracey };
4110 f2a9dc41 2019-12-13 tracey
4111 f2a9dc41 2019-12-13 tracey static const struct got_error *
4112 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4113 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4114 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4115 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4116 17ed4618 2019-06-02 stsp {
4117 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4118 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4119 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4120 17ed4618 2019-06-02 stsp struct stat sb;
4121 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4122 d64cc78a 2022-01-25 thomas
4123 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4124 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4125 d64cc78a 2022-01-25 thomas return NULL;
4126 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4127 d64cc78a 2022-01-25 thomas }
4128 17ed4618 2019-06-02 stsp
4129 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4130 17ed4618 2019-06-02 stsp if (ie == NULL)
4131 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4132 2ec1f75b 2019-03-26 stsp
4133 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4134 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4135 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4136 9acbc4fa 2019-08-03 stsp return NULL;
4137 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4138 9acbc4fa 2019-08-03 stsp }
4139 9acbc4fa 2019-08-03 stsp
4140 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4141 f2a9dc41 2019-12-13 tracey relpath) == -1)
4142 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4143 f2a9dc41 2019-12-13 tracey
4144 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4145 12463d8b 2019-12-13 stsp a->repo);
4146 17ed4618 2019-06-02 stsp if (err)
4147 f2a9dc41 2019-12-13 tracey goto done;
4148 17ed4618 2019-06-02 stsp
4149 766841c2 2020-08-13 stsp if (a->status_codes) {
4150 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4151 766841c2 2020-08-13 stsp int i;
4152 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4153 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4154 766841c2 2020-08-13 stsp break;
4155 766841c2 2020-08-13 stsp }
4156 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4157 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4158 766841c2 2020-08-13 stsp free(ondisk_path);
4159 766841c2 2020-08-13 stsp return NULL;
4160 766841c2 2020-08-13 stsp }
4161 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4162 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4163 766841c2 2020-08-13 stsp static char msg[64];
4164 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4165 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4166 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4167 766841c2 2020-08-13 stsp goto done;
4168 766841c2 2020-08-13 stsp }
4169 766841c2 2020-08-13 stsp }
4170 766841c2 2020-08-13 stsp
4171 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4172 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4173 f2a9dc41 2019-12-13 tracey goto done;
4174 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4175 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4176 f2a9dc41 2019-12-13 tracey goto done;
4177 f2a9dc41 2019-12-13 tracey }
4178 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4179 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4180 d64cc78a 2022-01-25 thomas goto done;
4181 d64cc78a 2022-01-25 thomas }
4182 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4183 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4184 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4185 f2a9dc41 2019-12-13 tracey goto done;
4186 f2a9dc41 2019-12-13 tracey }
4187 17ed4618 2019-06-02 stsp }
4188 17ed4618 2019-06-02 stsp
4189 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4190 20a2ad1c 2020-10-20 stsp size_t root_len;
4191 20a2ad1c 2020-10-20 stsp
4192 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4193 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4194 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4195 12463d8b 2019-12-13 stsp ondisk_path);
4196 12463d8b 2019-12-13 stsp goto done;
4197 12463d8b 2019-12-13 stsp }
4198 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4199 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4200 12463d8b 2019-12-13 stsp goto done;
4201 15341bfd 2020-03-05 tracey }
4202 15341bfd 2020-03-05 tracey
4203 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4204 20a2ad1c 2020-10-20 stsp do {
4205 20a2ad1c 2020-10-20 stsp char *parent;
4206 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4207 20a2ad1c 2020-10-20 stsp if (err)
4208 2513f20a 2020-10-20 stsp goto done;
4209 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4210 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4211 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4212 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4213 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4214 20a2ad1c 2020-10-20 stsp ondisk_path);
4215 15341bfd 2020-03-05 tracey break;
4216 15341bfd 2020-03-05 tracey }
4217 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4218 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4219 f2a9dc41 2019-12-13 tracey }
4220 17ed4618 2019-06-02 stsp
4221 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4222 f2a9dc41 2019-12-13 tracey done:
4223 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4224 f2a9dc41 2019-12-13 tracey if (err)
4225 f2a9dc41 2019-12-13 tracey return err;
4226 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4227 f2a9dc41 2019-12-13 tracey return NULL;
4228 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4229 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4230 17ed4618 2019-06-02 stsp }
4231 17ed4618 2019-06-02 stsp
4232 2ec1f75b 2019-03-26 stsp const struct got_error *
4233 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4234 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4235 766841c2 2020-08-13 stsp const char *status_codes,
4236 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4237 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4238 2ec1f75b 2019-03-26 stsp {
4239 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4240 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4241 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4242 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4243 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4244 2ec1f75b 2019-03-26 stsp
4245 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4246 2ec1f75b 2019-03-26 stsp if (err)
4247 2ec1f75b 2019-03-26 stsp return err;
4248 2ec1f75b 2019-03-26 stsp
4249 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4250 2ec1f75b 2019-03-26 stsp if (err)
4251 2ec1f75b 2019-03-26 stsp goto done;
4252 f2a9dc41 2019-12-13 tracey
4253 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4254 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4255 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4256 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4257 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4258 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4259 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4260 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4261 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4262 2ec1f75b 2019-03-26 stsp
4263 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4264 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4265 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4266 17ed4618 2019-06-02 stsp if (err)
4267 af12c6b9 2019-06-04 stsp break;
4268 2ec1f75b 2019-03-26 stsp }
4269 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4270 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4271 af12c6b9 2019-06-04 stsp err = sync_err;
4272 2ec1f75b 2019-03-26 stsp done:
4273 fb399478 2019-07-12 stsp free(fileindex_path);
4274 2ec1f75b 2019-03-26 stsp if (fileindex)
4275 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4276 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4277 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4278 2ec1f75b 2019-03-26 stsp err = unlockerr;
4279 33aa809d 2019-08-08 stsp return err;
4280 33aa809d 2019-08-08 stsp }
4281 33aa809d 2019-08-08 stsp
4282 33aa809d 2019-08-08 stsp static const struct got_error *
4283 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4284 33aa809d 2019-08-08 stsp {
4285 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4286 33aa809d 2019-08-08 stsp char *line = NULL;
4287 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4288 33aa809d 2019-08-08 stsp ssize_t linelen;
4289 33aa809d 2019-08-08 stsp
4290 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4291 33aa809d 2019-08-08 stsp if (linelen == -1) {
4292 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4293 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4294 33aa809d 2019-08-08 stsp goto done;
4295 33aa809d 2019-08-08 stsp }
4296 33aa809d 2019-08-08 stsp return NULL;
4297 33aa809d 2019-08-08 stsp }
4298 33aa809d 2019-08-08 stsp if (outfile) {
4299 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4300 33aa809d 2019-08-08 stsp if (n != linelen) {
4301 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4302 33aa809d 2019-08-08 stsp goto done;
4303 33aa809d 2019-08-08 stsp }
4304 33aa809d 2019-08-08 stsp }
4305 33aa809d 2019-08-08 stsp if (rejectfile) {
4306 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4307 33aa809d 2019-08-08 stsp if (n != linelen)
4308 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4309 33aa809d 2019-08-08 stsp }
4310 33aa809d 2019-08-08 stsp done:
4311 33aa809d 2019-08-08 stsp free(line);
4312 2ec1f75b 2019-03-26 stsp return err;
4313 2ec1f75b 2019-03-26 stsp }
4314 1f1abb7e 2019-08-08 stsp
4315 33aa809d 2019-08-08 stsp static const struct got_error *
4316 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4317 33aa809d 2019-08-08 stsp {
4318 33aa809d 2019-08-08 stsp char *line = NULL;
4319 33aa809d 2019-08-08 stsp size_t linesize = 0;
4320 33aa809d 2019-08-08 stsp ssize_t linelen;
4321 33aa809d 2019-08-08 stsp
4322 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4323 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4324 500467ff 2019-09-25 hiltjo if (ferror(f))
4325 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4326 500467ff 2019-09-25 hiltjo return NULL;
4327 500467ff 2019-09-25 hiltjo }
4328 33aa809d 2019-08-08 stsp free(line);
4329 33aa809d 2019-08-08 stsp return NULL;
4330 33aa809d 2019-08-08 stsp }
4331 33aa809d 2019-08-08 stsp
4332 33aa809d 2019-08-08 stsp static const struct got_error *
4333 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4334 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4335 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4336 abc59930 2021-09-05 naddy {
4337 33aa809d 2019-08-08 stsp const struct got_error *err;
4338 33aa809d 2019-08-08 stsp
4339 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4340 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4341 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4342 33aa809d 2019-08-08 stsp if (err)
4343 33aa809d 2019-08-08 stsp return err;
4344 33aa809d 2019-08-08 stsp (*line_cur1)++;
4345 33aa809d 2019-08-08 stsp }
4346 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4347 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4348 33aa809d 2019-08-08 stsp if (rejectfile)
4349 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4350 33aa809d 2019-08-08 stsp else
4351 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4352 33aa809d 2019-08-08 stsp if (err)
4353 33aa809d 2019-08-08 stsp return err;
4354 33aa809d 2019-08-08 stsp (*line_cur2)++;
4355 33aa809d 2019-08-08 stsp }
4356 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4357 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4358 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4359 33aa809d 2019-08-08 stsp if (err)
4360 33aa809d 2019-08-08 stsp return err;
4361 33aa809d 2019-08-08 stsp (*line_cur2)++;
4362 33aa809d 2019-08-08 stsp }
4363 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4364 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4365 33aa809d 2019-08-08 stsp if (rejectfile)
4366 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4367 33aa809d 2019-08-08 stsp else
4368 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4369 33aa809d 2019-08-08 stsp if (err)
4370 33aa809d 2019-08-08 stsp return err;
4371 33aa809d 2019-08-08 stsp (*line_cur1)++;
4372 33aa809d 2019-08-08 stsp }
4373 f1e81a05 2019-08-10 stsp
4374 f1e81a05 2019-08-10 stsp return NULL;
4375 f1e81a05 2019-08-10 stsp }
4376 f1e81a05 2019-08-10 stsp
4377 f1e81a05 2019-08-10 stsp static const struct got_error *
4378 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4379 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4380 f1e81a05 2019-08-10 stsp {
4381 f1e81a05 2019-08-10 stsp const struct got_error *err;
4382 f1e81a05 2019-08-10 stsp
4383 f1e81a05 2019-08-10 stsp if (outfile) {
4384 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4385 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4386 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4387 f1e81a05 2019-08-10 stsp if (err)
4388 f1e81a05 2019-08-10 stsp return err;
4389 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4390 f1e81a05 2019-08-10 stsp }
4391 f1e81a05 2019-08-10 stsp }
4392 f1e81a05 2019-08-10 stsp if (rejectfile) {
4393 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4394 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4395 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4396 f1e81a05 2019-08-10 stsp if (err)
4397 f1e81a05 2019-08-10 stsp return err;
4398 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4399 f1e81a05 2019-08-10 stsp }
4400 33aa809d 2019-08-08 stsp }
4401 33aa809d 2019-08-08 stsp
4402 33aa809d 2019-08-08 stsp return NULL;
4403 33aa809d 2019-08-08 stsp }
4404 33aa809d 2019-08-08 stsp
4405 33aa809d 2019-08-08 stsp static const struct got_error *
4406 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4407 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4408 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4409 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4410 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4411 33aa809d 2019-08-08 stsp {
4412 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4413 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4414 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4415 33aa809d 2019-08-08 stsp FILE *hunkfile;
4416 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4417 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4418 fe621944 2020-11-10 stsp int rc;
4419 33aa809d 2019-08-08 stsp
4420 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4421 33aa809d 2019-08-08 stsp
4422 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4423 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4424 fe621944 2020-11-10 stsp start_old = cc.left.start;
4425 fe621944 2020-11-10 stsp end_old = cc.left.end;
4426 fe621944 2020-11-10 stsp start_new = cc.right.start;
4427 fe621944 2020-11-10 stsp end_new = cc.right.end;
4428 33aa809d 2019-08-08 stsp
4429 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4430 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4431 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4432 33aa809d 2019-08-08 stsp
4433 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4434 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4435 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4436 33aa809d 2019-08-08 stsp
4437 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4438 fe621944 2020-11-10 stsp if (diff_state == NULL)
4439 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4440 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4441 fe621944 2020-11-10 stsp
4442 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4443 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4444 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4445 33aa809d 2019-08-08 stsp goto done;
4446 33aa809d 2019-08-08 stsp }
4447 fe621944 2020-11-10 stsp
4448 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4449 fe621944 2020-11-10 stsp diff_result, &cc);
4450 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4451 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4452 33aa809d 2019-08-08 stsp goto done;
4453 33aa809d 2019-08-08 stsp }
4454 fe621944 2020-11-10 stsp
4455 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4456 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4457 33aa809d 2019-08-08 stsp goto done;
4458 33aa809d 2019-08-08 stsp }
4459 33aa809d 2019-08-08 stsp
4460 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4461 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4462 33aa809d 2019-08-08 stsp if (err)
4463 33aa809d 2019-08-08 stsp goto done;
4464 33aa809d 2019-08-08 stsp
4465 33aa809d 2019-08-08 stsp switch (*choice) {
4466 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4467 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4468 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4469 33aa809d 2019-08-08 stsp break;
4470 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4471 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4472 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4473 33aa809d 2019-08-08 stsp break;
4474 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4475 33aa809d 2019-08-08 stsp break;
4476 33aa809d 2019-08-08 stsp default:
4477 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4478 33aa809d 2019-08-08 stsp break;
4479 33aa809d 2019-08-08 stsp }
4480 33aa809d 2019-08-08 stsp done:
4481 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4482 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4483 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4484 33aa809d 2019-08-08 stsp return err;
4485 33aa809d 2019-08-08 stsp }
4486 33aa809d 2019-08-08 stsp
4487 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4488 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4489 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4490 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4491 1f1abb7e 2019-08-08 stsp void *progress_arg;
4492 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4493 33aa809d 2019-08-08 stsp void *patch_arg;
4494 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4495 10604dce 2021-09-24 thomas int unlink_added_files;
4496 1f1abb7e 2019-08-08 stsp };
4497 a129376b 2019-03-28 stsp
4498 e20a8b6f 2019-06-04 stsp static const struct got_error *
4499 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4500 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4501 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4502 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4503 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4504 33aa809d 2019-08-08 stsp {
4505 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4506 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4507 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4508 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4509 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4510 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4511 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4512 fe621944 2020-11-10 stsp struct stat sb2;
4513 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4514 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4515 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4516 33aa809d 2019-08-08 stsp
4517 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4518 33aa809d 2019-08-08 stsp
4519 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4520 33aa809d 2019-08-08 stsp if (err)
4521 33aa809d 2019-08-08 stsp return err;
4522 33aa809d 2019-08-08 stsp
4523 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4524 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4525 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4526 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4527 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4528 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4529 fa3cef63 2020-07-23 stsp goto done;
4530 fa3cef63 2020-07-23 stsp }
4531 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4532 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4533 48b4f239 2021-12-31 thomas if (link_len == -1) {
4534 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4535 48b4f239 2021-12-31 thomas path2);
4536 48b4f239 2021-12-31 thomas }
4537 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4538 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4539 12463d8b 2019-12-13 stsp }
4540 12463d8b 2019-12-13 stsp } else {
4541 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4542 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4543 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4544 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4545 fa3cef63 2020-07-23 stsp goto done;
4546 fa3cef63 2020-07-23 stsp }
4547 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4548 fa3cef63 2020-07-23 stsp sizeof(link_target));
4549 fa3cef63 2020-07-23 stsp if (link_len == -1)
4550 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4551 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4552 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4553 12463d8b 2019-12-13 stsp }
4554 1ebedb77 2019-10-19 stsp }
4555 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4556 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4557 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4558 fa3cef63 2020-07-23 stsp goto done;
4559 fa3cef63 2020-07-23 stsp }
4560 1ebedb77 2019-10-19 stsp
4561 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4562 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4563 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4564 fa3cef63 2020-07-23 stsp goto done;
4565 fa3cef63 2020-07-23 stsp }
4566 fa3cef63 2020-07-23 stsp fd2 = -1;
4567 fa3cef63 2020-07-23 stsp } else {
4568 fa3cef63 2020-07-23 stsp size_t n;
4569 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4570 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4571 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4572 fa3cef63 2020-07-23 stsp goto done;
4573 fa3cef63 2020-07-23 stsp }
4574 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4575 fa3cef63 2020-07-23 stsp if (n != link_len) {
4576 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4577 fa3cef63 2020-07-23 stsp goto done;
4578 fa3cef63 2020-07-23 stsp }
4579 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4580 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4581 fa3cef63 2020-07-23 stsp goto done;
4582 fa3cef63 2020-07-23 stsp }
4583 fa3cef63 2020-07-23 stsp rewind(f2);
4584 33aa809d 2019-08-08 stsp }
4585 33aa809d 2019-08-08 stsp
4586 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4587 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4588 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4589 f4ae6ddb 2022-07-01 thomas goto done;
4590 f4ae6ddb 2022-07-01 thomas }
4591 f4ae6ddb 2022-07-01 thomas
4592 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4593 33aa809d 2019-08-08 stsp if (err)
4594 33aa809d 2019-08-08 stsp goto done;
4595 33aa809d 2019-08-08 stsp
4596 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4597 33aa809d 2019-08-08 stsp if (err)
4598 33aa809d 2019-08-08 stsp goto done;
4599 33aa809d 2019-08-08 stsp
4600 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4601 33aa809d 2019-08-08 stsp if (err)
4602 33aa809d 2019-08-08 stsp goto done;
4603 33aa809d 2019-08-08 stsp
4604 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4605 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4606 33aa809d 2019-08-08 stsp if (err)
4607 33aa809d 2019-08-08 stsp goto done;
4608 33aa809d 2019-08-08 stsp
4609 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4610 fc2a50f2 2022-11-01 thomas "");
4611 33aa809d 2019-08-08 stsp if (err)
4612 33aa809d 2019-08-08 stsp goto done;
4613 33aa809d 2019-08-08 stsp
4614 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4615 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4616 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4617 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4618 fe621944 2020-11-10 stsp
4619 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4620 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4621 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4622 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4623 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4624 fe621944 2020-11-10 stsp nchanges++;
4625 fe621944 2020-11-10 stsp }
4626 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4627 33aa809d 2019-08-08 stsp int choice;
4628 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4629 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4630 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4631 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4632 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4633 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4634 33aa809d 2019-08-08 stsp if (err)
4635 33aa809d 2019-08-08 stsp goto done;
4636 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4637 33aa809d 2019-08-08 stsp have_content = 1;
4638 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4639 33aa809d 2019-08-08 stsp break;
4640 33aa809d 2019-08-08 stsp }
4641 1ebedb77 2019-10-19 stsp if (have_content) {
4642 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4643 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4644 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4645 1ebedb77 2019-10-19 stsp if (err)
4646 1ebedb77 2019-10-19 stsp goto done;
4647 1ebedb77 2019-10-19 stsp
4648 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4649 a2c162eb 2022-10-30 thomas mode_t mode;
4650 a2c162eb 2022-10-30 thomas
4651 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4652 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4653 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4654 fa3cef63 2020-07-23 stsp goto done;
4655 fa3cef63 2020-07-23 stsp }
4656 1ebedb77 2019-10-19 stsp }
4657 1ebedb77 2019-10-19 stsp }
4658 33aa809d 2019-08-08 stsp done:
4659 33aa809d 2019-08-08 stsp free(id_str);
4660 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4661 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4662 33aa809d 2019-08-08 stsp if (blob)
4663 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4664 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4665 fe621944 2020-11-10 stsp if (err == NULL)
4666 fe621944 2020-11-10 stsp err = free_err;
4667 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4668 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4669 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4670 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4671 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4672 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4673 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4674 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4675 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4676 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4677 33aa809d 2019-08-08 stsp if (err || !have_content) {
4678 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4679 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4680 33aa809d 2019-08-08 stsp free(*path_outfile);
4681 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4682 33aa809d 2019-08-08 stsp }
4683 33aa809d 2019-08-08 stsp free(path1);
4684 33aa809d 2019-08-08 stsp return err;
4685 33aa809d 2019-08-08 stsp }
4686 33aa809d 2019-08-08 stsp
4687 33aa809d 2019-08-08 stsp static const struct got_error *
4688 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4689 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4690 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4691 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4692 a129376b 2019-03-28 stsp {
4693 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4694 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4695 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4696 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4697 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4698 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4699 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4700 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4701 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4702 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4703 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4704 f4ae6ddb 2022-07-01 thomas int fd = -1;
4705 a129376b 2019-03-28 stsp
4706 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4707 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4708 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4709 d3bcc3d1 2019-08-08 stsp return NULL;
4710 3d69ad8d 2019-08-17 semarie
4711 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4712 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4713 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4714 d3bcc3d1 2019-08-08 stsp
4715 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4716 65084dad 2019-08-08 stsp if (ie == NULL)
4717 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4718 a129376b 2019-03-28 stsp
4719 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4720 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4721 a129376b 2019-03-28 stsp if (err) {
4722 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4723 a129376b 2019-03-28 stsp goto done;
4724 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4725 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4726 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4727 e20a8b6f 2019-06-04 stsp goto done;
4728 e20a8b6f 2019-06-04 stsp }
4729 a129376b 2019-03-28 stsp }
4730 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4731 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4732 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4733 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4734 a129376b 2019-03-28 stsp goto done;
4735 a129376b 2019-03-28 stsp }
4736 a129376b 2019-03-28 stsp } else {
4737 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4738 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4739 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4740 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4741 a129376b 2019-03-28 stsp goto done;
4742 a129376b 2019-03-28 stsp }
4743 a129376b 2019-03-28 stsp } else {
4744 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4745 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4746 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4747 a129376b 2019-03-28 stsp goto done;
4748 a129376b 2019-03-28 stsp }
4749 a129376b 2019-03-28 stsp }
4750 a129376b 2019-03-28 stsp }
4751 a129376b 2019-03-28 stsp
4752 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4753 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4754 945f9229 2022-04-16 thomas if (err)
4755 945f9229 2022-04-16 thomas goto done;
4756 945f9229 2022-04-16 thomas
4757 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4758 a9fa2909 2019-07-27 stsp if (err) {
4759 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4760 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4761 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4762 a9fa2909 2019-07-27 stsp goto done;
4763 a9fa2909 2019-07-27 stsp } else {
4764 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4765 a9fa2909 2019-07-27 stsp if (err)
4766 a9fa2909 2019-07-27 stsp goto done;
4767 a9fa2909 2019-07-27 stsp
4768 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4769 1233e6b6 2020-10-19 stsp if (err)
4770 a9fa2909 2019-07-27 stsp goto done;
4771 a9fa2909 2019-07-27 stsp
4772 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4773 1233e6b6 2020-10-19 stsp free(te_name);
4774 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4775 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4776 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4777 a9fa2909 2019-07-27 stsp goto done;
4778 a9fa2909 2019-07-27 stsp }
4779 a129376b 2019-03-28 stsp }
4780 a129376b 2019-03-28 stsp
4781 a129376b 2019-03-28 stsp switch (status) {
4782 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4783 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4784 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4785 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4786 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4787 33aa809d 2019-08-08 stsp if (err)
4788 33aa809d 2019-08-08 stsp goto done;
4789 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4790 33aa809d 2019-08-08 stsp break;
4791 33aa809d 2019-08-08 stsp }
4792 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4793 1f1abb7e 2019-08-08 stsp ie->path);
4794 1ee397ad 2019-07-12 stsp if (err)
4795 1ee397ad 2019-07-12 stsp goto done;
4796 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4797 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4798 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4799 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4800 10604dce 2021-09-24 thomas relpath) == -1) {
4801 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4802 10604dce 2021-09-24 thomas goto done;
4803 10604dce 2021-09-24 thomas }
4804 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4805 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4806 10604dce 2021-09-24 thomas ondisk_path);
4807 10604dce 2021-09-24 thomas break;
4808 10604dce 2021-09-24 thomas }
4809 10604dce 2021-09-24 thomas }
4810 a129376b 2019-03-28 stsp break;
4811 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4812 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4813 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4814 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4815 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4816 33aa809d 2019-08-08 stsp if (err)
4817 33aa809d 2019-08-08 stsp goto done;
4818 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4819 33aa809d 2019-08-08 stsp break;
4820 33aa809d 2019-08-08 stsp }
4821 33aa809d 2019-08-08 stsp /* fall through */
4822 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4823 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4824 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4825 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4826 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4827 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4828 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
4829 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
4830 43010591 2023-02-17 thomas else
4831 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
4832 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4833 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4834 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4835 f4ae6ddb 2022-07-01 thomas goto done;
4836 f4ae6ddb 2022-07-01 thomas }
4837 f4ae6ddb 2022-07-01 thomas
4838 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4839 a129376b 2019-03-28 stsp if (err)
4840 65084dad 2019-08-08 stsp goto done;
4841 65084dad 2019-08-08 stsp
4842 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4843 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4844 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4845 a129376b 2019-03-28 stsp goto done;
4846 65084dad 2019-08-08 stsp }
4847 65084dad 2019-08-08 stsp
4848 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4849 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4850 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4851 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4852 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4853 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4854 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4855 33aa809d 2019-08-08 stsp break;
4856 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4857 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4858 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4859 369fd7e5 2020-07-23 stsp path_content);
4860 369fd7e5 2020-07-23 stsp break;
4861 369fd7e5 2020-07-23 stsp }
4862 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4863 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4864 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4865 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4866 369fd7e5 2020-07-23 stsp } else {
4867 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4868 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4869 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4870 369fd7e5 2020-07-23 stsp goto done;
4871 369fd7e5 2020-07-23 stsp }
4872 33aa809d 2019-08-08 stsp }
4873 33aa809d 2019-08-08 stsp } else {
4874 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4875 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4876 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4877 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4878 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4879 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4880 2e1fa222 2020-07-23 stsp } else {
4881 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4882 2e1fa222 2020-07-23 stsp ie->path,
4883 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4884 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4885 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4886 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4887 2e1fa222 2020-07-23 stsp }
4888 a129376b 2019-03-28 stsp if (err)
4889 a129376b 2019-03-28 stsp goto done;
4890 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4891 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4892 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4893 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4894 437adc9d 2020-12-10 yzhong blob->id.sha1,
4895 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4896 2e1fa222 2020-07-23 stsp if (err)
4897 2e1fa222 2020-07-23 stsp goto done;
4898 2e1fa222 2020-07-23 stsp }
4899 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4900 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4901 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4902 33aa809d 2019-08-08 stsp }
4903 a129376b 2019-03-28 stsp }
4904 a129376b 2019-03-28 stsp break;
4905 e20a8b6f 2019-06-04 stsp }
4906 a129376b 2019-03-28 stsp default:
4907 1f1abb7e 2019-08-08 stsp break;
4908 a129376b 2019-03-28 stsp }
4909 a129376b 2019-03-28 stsp done:
4910 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4911 33aa809d 2019-08-08 stsp free(path_content);
4912 e20a8b6f 2019-06-04 stsp free(parent_path);
4913 a129376b 2019-03-28 stsp free(tree_path);
4914 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4915 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4916 a129376b 2019-03-28 stsp if (blob)
4917 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4918 a129376b 2019-03-28 stsp if (tree)
4919 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4920 a129376b 2019-03-28 stsp free(tree_id);
4921 945f9229 2022-04-16 thomas if (base_commit)
4922 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4923 e20a8b6f 2019-06-04 stsp return err;
4924 e20a8b6f 2019-06-04 stsp }
4925 e20a8b6f 2019-06-04 stsp
4926 e20a8b6f 2019-06-04 stsp const struct got_error *
4927 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4928 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4929 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4930 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4931 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4932 e20a8b6f 2019-06-04 stsp {
4933 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4934 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4935 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4936 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4937 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4938 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4939 e20a8b6f 2019-06-04 stsp
4940 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4941 e20a8b6f 2019-06-04 stsp if (err)
4942 e20a8b6f 2019-06-04 stsp return err;
4943 e20a8b6f 2019-06-04 stsp
4944 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4945 e20a8b6f 2019-06-04 stsp if (err)
4946 e20a8b6f 2019-06-04 stsp goto done;
4947 e20a8b6f 2019-06-04 stsp
4948 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4949 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4950 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4951 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4952 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4953 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4954 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4955 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4956 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4957 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4958 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4959 e20a8b6f 2019-06-04 stsp if (err)
4960 af12c6b9 2019-06-04 stsp break;
4961 e20a8b6f 2019-06-04 stsp }
4962 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4963 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4964 e20a8b6f 2019-06-04 stsp err = sync_err;
4965 af12c6b9 2019-06-04 stsp done:
4966 fb399478 2019-07-12 stsp free(fileindex_path);
4967 a129376b 2019-03-28 stsp if (fileindex)
4968 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4969 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4970 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4971 a129376b 2019-03-28 stsp err = unlockerr;
4972 c4296144 2019-05-09 stsp return err;
4973 c4296144 2019-05-09 stsp }
4974 c4296144 2019-05-09 stsp
4975 cf066bf8 2019-05-09 stsp static void
4976 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4977 cf066bf8 2019-05-09 stsp {
4978 24519714 2019-05-09 stsp free(ct->path);
4979 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4980 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4981 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4982 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4983 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4984 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4985 cf066bf8 2019-05-09 stsp free(ct);
4986 cf066bf8 2019-05-09 stsp }
4987 24519714 2019-05-09 stsp
4988 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4989 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4990 24519714 2019-05-09 stsp struct got_repository *repo;
4991 24519714 2019-05-09 stsp struct got_worktree *worktree;
4992 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4993 5f8a88c6 2019-08-03 stsp int have_staged_files;
4994 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4995 ef899790 2022-11-01 thomas int diff_header_shown;
4996 be94c032 2023-02-20 thomas int commit_conflicts;
4997 ef899790 2022-11-01 thomas FILE *diff_outfile;
4998 ef899790 2022-11-01 thomas FILE *f1;
4999 ef899790 2022-11-01 thomas FILE *f2;
5000 24519714 2019-05-09 stsp };
5001 ef899790 2022-11-01 thomas
5002 ef899790 2022-11-01 thomas /*
5003 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5004 ef899790 2022-11-01 thomas * it as content to the diff engine.
5005 ef899790 2022-11-01 thomas */
5006 ef899790 2022-11-01 thomas static const struct got_error *
5007 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5008 ef899790 2022-11-01 thomas const char *abspath)
5009 ef899790 2022-11-01 thomas {
5010 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5011 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5012 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5013 ef899790 2022-11-01 thomas
5014 ef899790 2022-11-01 thomas *fd = -1;
5015 ef899790 2022-11-01 thomas
5016 ef899790 2022-11-01 thomas if (dirfd != -1) {
5017 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5018 ef899790 2022-11-01 thomas if (target_len == -1)
5019 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5020 ef899790 2022-11-01 thomas } else {
5021 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5022 ef899790 2022-11-01 thomas if (target_len == -1)
5023 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5024 ef899790 2022-11-01 thomas }
5025 ef899790 2022-11-01 thomas
5026 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5027 ef899790 2022-11-01 thomas if (*fd == -1)
5028 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5029 ef899790 2022-11-01 thomas
5030 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5031 ef899790 2022-11-01 thomas if (outlen == -1) {
5032 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5033 ef899790 2022-11-01 thomas goto done;
5034 ef899790 2022-11-01 thomas }
5035 ef899790 2022-11-01 thomas
5036 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5037 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5038 ef899790 2022-11-01 thomas goto done;
5039 ef899790 2022-11-01 thomas }
5040 ef899790 2022-11-01 thomas done:
5041 ef899790 2022-11-01 thomas if (err) {
5042 ef899790 2022-11-01 thomas close(*fd);
5043 ef899790 2022-11-01 thomas *fd = -1;
5044 ef899790 2022-11-01 thomas }
5045 ef899790 2022-11-01 thomas return err;
5046 ef899790 2022-11-01 thomas }
5047 ef899790 2022-11-01 thomas
5048 ef899790 2022-11-01 thomas static const struct got_error *
5049 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5050 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5051 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5052 ef899790 2022-11-01 thomas {
5053 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5054 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5055 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5056 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5057 ef899790 2022-11-01 thomas char *label1 = NULL;
5058 ef899790 2022-11-01 thomas struct stat sb;
5059 ef899790 2022-11-01 thomas off_t size1 = 0;
5060 ef899790 2022-11-01 thomas int f2_exists = 0;
5061 ef899790 2022-11-01 thomas char *id_str = NULL;
5062 ef899790 2022-11-01 thomas
5063 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5064 d259e491 2022-11-01 thomas
5065 d259e491 2022-11-01 thomas if (diff_staged) {
5066 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5067 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5068 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5069 d259e491 2022-11-01 thomas return NULL;
5070 d259e491 2022-11-01 thomas } else {
5071 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5072 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5073 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5074 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5075 d259e491 2022-11-01 thomas return NULL;
5076 d259e491 2022-11-01 thomas }
5077 ef899790 2022-11-01 thomas
5078 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5079 ef899790 2022-11-01 thomas if (err)
5080 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5081 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5082 ef899790 2022-11-01 thomas if (err)
5083 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5084 ef899790 2022-11-01 thomas
5085 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5086 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5087 ef899790 2022-11-01 thomas if (err)
5088 ef899790 2022-11-01 thomas return err;
5089 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5090 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5091 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5092 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5093 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5094 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5095 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5096 ef899790 2022-11-01 thomas }
5097 ef899790 2022-11-01 thomas
5098 ef899790 2022-11-01 thomas if (diff_staged) {
5099 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5100 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5101 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5102 ef899790 2022-11-01 thomas label1 = ct->path;
5103 ef899790 2022-11-01 thomas label2 = ct->path;
5104 ef899790 2022-11-01 thomas break;
5105 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5106 ef899790 2022-11-01 thomas label2 = ct->path;
5107 ef899790 2022-11-01 thomas break;
5108 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5109 ef899790 2022-11-01 thomas label1 = ct->path;
5110 ef899790 2022-11-01 thomas break;
5111 ef899790 2022-11-01 thomas default:
5112 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5113 ef899790 2022-11-01 thomas }
5114 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5115 ef899790 2022-11-01 thomas if (fd1 == -1) {
5116 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5117 ef899790 2022-11-01 thomas goto done;
5118 ef899790 2022-11-01 thomas }
5119 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5120 ef899790 2022-11-01 thomas if (fd2 == -1) {
5121 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5122 ef899790 2022-11-01 thomas goto done;
5123 ef899790 2022-11-01 thomas }
5124 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5125 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5126 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5127 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5128 ef899790 2022-11-01 thomas goto done;
5129 ef899790 2022-11-01 thomas }
5130 ef899790 2022-11-01 thomas
5131 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5132 ef899790 2022-11-01 thomas if (fd1 == -1) {
5133 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5134 ef899790 2022-11-01 thomas goto done;
5135 ef899790 2022-11-01 thomas }
5136 ef899790 2022-11-01 thomas
5137 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5138 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5139 ef899790 2022-11-01 thomas 8192, fd1);
5140 ef899790 2022-11-01 thomas if (err)
5141 ef899790 2022-11-01 thomas goto done;
5142 ef899790 2022-11-01 thomas }
5143 ef899790 2022-11-01 thomas
5144 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5145 ef899790 2022-11-01 thomas if (dirfd != -1) {
5146 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5147 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5148 ef899790 2022-11-01 thomas if (fd == -1) {
5149 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5150 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5151 ef899790 2022-11-01 thomas ct->ondisk_path);
5152 ef899790 2022-11-01 thomas goto done;
5153 ef899790 2022-11-01 thomas }
5154 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5155 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5156 ef899790 2022-11-01 thomas if (err)
5157 ef899790 2022-11-01 thomas goto done;
5158 ef899790 2022-11-01 thomas }
5159 ef899790 2022-11-01 thomas } else {
5160 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5161 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5162 ef899790 2022-11-01 thomas if (fd == -1) {
5163 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5164 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5165 ef899790 2022-11-01 thomas ct->ondisk_path);
5166 ef899790 2022-11-01 thomas goto done;
5167 ef899790 2022-11-01 thomas }
5168 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5169 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5170 ef899790 2022-11-01 thomas if (err)
5171 ef899790 2022-11-01 thomas goto done;
5172 ef899790 2022-11-01 thomas }
5173 ef899790 2022-11-01 thomas }
5174 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5175 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5176 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5177 ef899790 2022-11-01 thomas goto done;
5178 ef899790 2022-11-01 thomas }
5179 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5180 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5181 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5182 ef899790 2022-11-01 thomas goto done;
5183 ef899790 2022-11-01 thomas }
5184 ef899790 2022-11-01 thomas fd = -1;
5185 ef899790 2022-11-01 thomas f2_exists = 1;
5186 ef899790 2022-11-01 thomas }
5187 ef899790 2022-11-01 thomas
5188 ef899790 2022-11-01 thomas if (blob1) {
5189 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5190 ef899790 2022-11-01 thomas f1, blob1);
5191 ef899790 2022-11-01 thomas if (err)
5192 ef899790 2022-11-01 thomas goto done;
5193 ef899790 2022-11-01 thomas }
5194 ef899790 2022-11-01 thomas
5195 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5196 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5197 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5198 ef899790 2022-11-01 thomas done:
5199 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5200 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5201 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5202 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5203 ef899790 2022-11-01 thomas if (blob1)
5204 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5205 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5206 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5207 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5208 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5209 ef899790 2022-11-01 thomas return err;
5210 ef899790 2022-11-01 thomas }
5211 cf066bf8 2019-05-09 stsp
5212 c4296144 2019-05-09 stsp static const struct got_error *
5213 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5214 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5215 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5216 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5217 c4296144 2019-05-09 stsp {
5218 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5219 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5220 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5221 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5222 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5223 768aea60 2019-05-09 stsp struct stat sb;
5224 c4296144 2019-05-09 stsp
5225 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5226 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5227 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5228 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5229 5f8a88c6 2019-08-03 stsp return NULL;
5230 5f8a88c6 2019-08-03 stsp } else {
5231 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5232 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5233 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5234 be94c032 2023-02-20 thomas }
5235 c4296144 2019-05-09 stsp
5236 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5237 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5238 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5239 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5240 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5241 5f8a88c6 2019-08-03 stsp return NULL;
5242 5f8a88c6 2019-08-03 stsp }
5243 0b5cc0d6 2019-05-09 stsp
5244 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5246 036813ee 2019-05-09 stsp goto done;
5247 036813ee 2019-05-09 stsp }
5248 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5249 036813ee 2019-05-09 stsp parent_path = strdup("");
5250 69960a46 2019-05-09 stsp if (parent_path == NULL)
5251 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5252 69960a46 2019-05-09 stsp } else {
5253 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5254 69960a46 2019-05-09 stsp if (err)
5255 69960a46 2019-05-09 stsp return err;
5256 69960a46 2019-05-09 stsp }
5257 c4296144 2019-05-09 stsp
5258 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5259 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5260 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5261 c4296144 2019-05-09 stsp goto done;
5262 768aea60 2019-05-09 stsp }
5263 768aea60 2019-05-09 stsp
5264 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5265 768aea60 2019-05-09 stsp relpath) == -1) {
5266 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5267 768aea60 2019-05-09 stsp goto done;
5268 768aea60 2019-05-09 stsp }
5269 0aeb8099 2020-07-23 stsp
5270 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5271 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5272 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5273 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5274 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5275 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5276 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5277 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5278 0aeb8099 2020-07-23 stsp break;
5279 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5280 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5281 0aeb8099 2020-07-23 stsp break;
5282 0aeb8099 2020-07-23 stsp default:
5283 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5284 0aeb8099 2020-07-23 stsp goto done;
5285 0aeb8099 2020-07-23 stsp }
5286 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5287 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5288 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5289 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5290 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5291 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5292 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5293 12463d8b 2019-12-13 stsp ct->ondisk_path);
5294 12463d8b 2019-12-13 stsp goto done;
5295 12463d8b 2019-12-13 stsp }
5296 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5297 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5298 768aea60 2019-05-09 stsp goto done;
5299 768aea60 2019-05-09 stsp }
5300 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5301 c4296144 2019-05-09 stsp }
5302 c4296144 2019-05-09 stsp
5303 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5304 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5305 44d03001 2019-05-09 stsp relpath) == -1) {
5306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5307 44d03001 2019-05-09 stsp goto done;
5308 44d03001 2019-05-09 stsp }
5309 44d03001 2019-05-09 stsp
5310 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5311 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5312 35213c7c 2020-07-23 stsp int is_bad_symlink;
5313 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5314 35213c7c 2020-07-23 stsp ssize_t target_len;
5315 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5316 35213c7c 2020-07-23 stsp sizeof(target_path));
5317 35213c7c 2020-07-23 stsp if (target_len == -1) {
5318 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5319 35213c7c 2020-07-23 stsp ct->ondisk_path);
5320 35213c7c 2020-07-23 stsp goto done;
5321 35213c7c 2020-07-23 stsp }
5322 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5323 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5324 35213c7c 2020-07-23 stsp if (err)
5325 35213c7c 2020-07-23 stsp goto done;
5326 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5327 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5328 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5329 35213c7c 2020-07-23 stsp goto done;
5330 35213c7c 2020-07-23 stsp }
5331 35213c7c 2020-07-23 stsp }
5332 35213c7c 2020-07-23 stsp
5333 35213c7c 2020-07-23 stsp
5334 cf066bf8 2019-05-09 stsp ct->status = status;
5335 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5336 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5337 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5338 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5339 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5340 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5341 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5342 b416585c 2019-05-13 stsp goto done;
5343 b416585c 2019-05-13 stsp }
5344 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5345 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5346 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5347 f0b75401 2019-08-03 stsp goto done;
5348 f0b75401 2019-08-03 stsp }
5349 f0b75401 2019-08-03 stsp }
5350 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5351 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5352 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5353 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5354 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5355 036813ee 2019-05-09 stsp goto done;
5356 036813ee 2019-05-09 stsp }
5357 ca2503ea 2019-05-09 stsp }
5358 24519714 2019-05-09 stsp ct->path = strdup(path);
5359 24519714 2019-05-09 stsp if (ct->path == NULL) {
5360 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5361 24519714 2019-05-09 stsp goto done;
5362 24519714 2019-05-09 stsp }
5363 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5364 ef899790 2022-11-01 thomas if (err)
5365 ef899790 2022-11-01 thomas goto done;
5366 ef899790 2022-11-01 thomas
5367 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5368 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5369 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5370 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5371 ef899790 2022-11-01 thomas if (err)
5372 ef899790 2022-11-01 thomas goto done;
5373 ef899790 2022-11-01 thomas }
5374 c4296144 2019-05-09 stsp done:
5375 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5376 ed175427 2019-05-09 stsp free_commitable(ct);
5377 24519714 2019-05-09 stsp free(parent_path);
5378 036813ee 2019-05-09 stsp free(path);
5379 a129376b 2019-03-28 stsp return err;
5380 a129376b 2019-03-28 stsp }
5381 c4296144 2019-05-09 stsp
5382 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5383 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5384 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5385 036813ee 2019-05-09 stsp struct got_repository *);
5386 ed175427 2019-05-09 stsp
5387 ed175427 2019-05-09 stsp static const struct got_error *
5388 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5389 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5390 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5391 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5392 afa376bf 2019-05-09 stsp struct got_repository *repo)
5393 ed175427 2019-05-09 stsp {
5394 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5395 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5396 036813ee 2019-05-09 stsp char *subpath;
5397 ed175427 2019-05-09 stsp
5398 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5399 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5400 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5401 ed175427 2019-05-09 stsp
5402 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5403 ed175427 2019-05-09 stsp if (err)
5404 ed175427 2019-05-09 stsp return err;
5405 ed175427 2019-05-09 stsp
5406 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5407 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5408 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5409 036813ee 2019-05-09 stsp free(subpath);
5410 036813ee 2019-05-09 stsp return err;
5411 036813ee 2019-05-09 stsp }
5412 ed175427 2019-05-09 stsp
5413 036813ee 2019-05-09 stsp static const struct got_error *
5414 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5415 036813ee 2019-05-09 stsp {
5416 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5417 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5418 ed175427 2019-05-09 stsp
5419 036813ee 2019-05-09 stsp *match = 0;
5420 ed175427 2019-05-09 stsp
5421 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5422 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5423 0f63689d 2019-05-10 stsp return NULL;
5424 036813ee 2019-05-09 stsp }
5425 0b5cc0d6 2019-05-09 stsp
5426 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5427 0f63689d 2019-05-10 stsp if (err)
5428 0f63689d 2019-05-10 stsp return err;
5429 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5430 036813ee 2019-05-09 stsp free(ct_parent_path);
5431 036813ee 2019-05-09 stsp return err;
5432 036813ee 2019-05-09 stsp }
5433 0b5cc0d6 2019-05-09 stsp
5434 768aea60 2019-05-09 stsp static mode_t
5435 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5436 768aea60 2019-05-09 stsp {
5437 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5438 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5439 3d9a4ec4 2020-07-23 stsp
5440 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5441 768aea60 2019-05-09 stsp }
5442 768aea60 2019-05-09 stsp
5443 036813ee 2019-05-09 stsp static const struct got_error *
5444 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5445 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5446 036813ee 2019-05-09 stsp {
5447 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5448 ca2503ea 2019-05-09 stsp
5449 036813ee 2019-05-09 stsp *new_te = NULL;
5450 0b5cc0d6 2019-05-09 stsp
5451 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5452 036813ee 2019-05-09 stsp if (err)
5453 036813ee 2019-05-09 stsp goto done;
5454 0b5cc0d6 2019-05-09 stsp
5455 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5456 036813ee 2019-05-09 stsp
5457 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5458 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5459 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5460 5f8a88c6 2019-08-03 stsp else
5461 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5462 036813ee 2019-05-09 stsp done:
5463 036813ee 2019-05-09 stsp if (err && *new_te) {
5464 56e0773d 2019-11-28 stsp free(*new_te);
5465 036813ee 2019-05-09 stsp *new_te = NULL;
5466 036813ee 2019-05-09 stsp }
5467 036813ee 2019-05-09 stsp return err;
5468 036813ee 2019-05-09 stsp }
5469 036813ee 2019-05-09 stsp
5470 036813ee 2019-05-09 stsp static const struct got_error *
5471 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5472 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5473 036813ee 2019-05-09 stsp {
5474 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5475 102b254e 2020-10-19 stsp char *ct_name = NULL;
5476 036813ee 2019-05-09 stsp
5477 036813ee 2019-05-09 stsp *new_te = NULL;
5478 036813ee 2019-05-09 stsp
5479 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5480 036813ee 2019-05-09 stsp if (*new_te == NULL)
5481 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5482 036813ee 2019-05-09 stsp
5483 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5484 102b254e 2020-10-19 stsp if (err)
5485 036813ee 2019-05-09 stsp goto done;
5486 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5487 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5488 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5489 036813ee 2019-05-09 stsp goto done;
5490 036813ee 2019-05-09 stsp }
5491 036813ee 2019-05-09 stsp
5492 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5493 036813ee 2019-05-09 stsp
5494 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5495 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5496 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5497 5f8a88c6 2019-08-03 stsp else
5498 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5499 036813ee 2019-05-09 stsp done:
5500 102b254e 2020-10-19 stsp free(ct_name);
5501 036813ee 2019-05-09 stsp if (err && *new_te) {
5502 56e0773d 2019-11-28 stsp free(*new_te);
5503 036813ee 2019-05-09 stsp *new_te = NULL;
5504 036813ee 2019-05-09 stsp }
5505 036813ee 2019-05-09 stsp return err;
5506 036813ee 2019-05-09 stsp }
5507 036813ee 2019-05-09 stsp
5508 036813ee 2019-05-09 stsp static const struct got_error *
5509 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5510 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5511 036813ee 2019-05-09 stsp {
5512 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5513 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5514 036813ee 2019-05-09 stsp
5515 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5516 036813ee 2019-05-09 stsp if (err)
5517 036813ee 2019-05-09 stsp return err;
5518 036813ee 2019-05-09 stsp if (new_pe == NULL)
5519 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5520 036813ee 2019-05-09 stsp return NULL;
5521 afa376bf 2019-05-09 stsp }
5522 afa376bf 2019-05-09 stsp
5523 afa376bf 2019-05-09 stsp static const struct got_error *
5524 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5525 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5526 afa376bf 2019-05-09 stsp {
5527 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5528 5f8a88c6 2019-08-03 stsp unsigned char status;
5529 ae1e948a 2021-09-28 thomas
5530 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5531 ae1e948a 2021-09-28 thomas return NULL;
5532 5f8a88c6 2019-08-03 stsp
5533 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5534 afa376bf 2019-05-09 stsp ct_path++;
5535 5f8a88c6 2019-08-03 stsp
5536 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5537 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5538 5f8a88c6 2019-08-03 stsp else
5539 5f8a88c6 2019-08-03 stsp status = ct->status;
5540 5f8a88c6 2019-08-03 stsp
5541 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5542 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5543 036813ee 2019-05-09 stsp }
5544 036813ee 2019-05-09 stsp
5545 036813ee 2019-05-09 stsp static const struct got_error *
5546 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5547 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5548 44d03001 2019-05-09 stsp {
5549 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5550 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5551 44d03001 2019-05-09 stsp char *te_path;
5552 44d03001 2019-05-09 stsp
5553 44d03001 2019-05-09 stsp *modified = 0;
5554 44d03001 2019-05-09 stsp
5555 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5556 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5557 44d03001 2019-05-09 stsp te->name) == -1)
5558 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5559 44d03001 2019-05-09 stsp
5560 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5561 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5562 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5563 44d03001 2019-05-09 stsp strlen(te_path));
5564 62d463ca 2020-10-20 naddy if (*modified)
5565 44d03001 2019-05-09 stsp break;
5566 44d03001 2019-05-09 stsp }
5567 44d03001 2019-05-09 stsp
5568 44d03001 2019-05-09 stsp free(te_path);
5569 44d03001 2019-05-09 stsp return err;
5570 44d03001 2019-05-09 stsp }
5571 44d03001 2019-05-09 stsp
5572 44d03001 2019-05-09 stsp static const struct got_error *
5573 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5574 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5575 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5576 036813ee 2019-05-09 stsp {
5577 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5578 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5579 036813ee 2019-05-09 stsp
5580 036813ee 2019-05-09 stsp *ctp = NULL;
5581 036813ee 2019-05-09 stsp
5582 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5583 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5584 036813ee 2019-05-09 stsp char *ct_name = NULL;
5585 036813ee 2019-05-09 stsp int path_matches;
5586 036813ee 2019-05-09 stsp
5587 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5588 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5589 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5590 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5591 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5592 5f8a88c6 2019-08-03 stsp continue;
5593 5f8a88c6 2019-08-03 stsp } else {
5594 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5595 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5596 5f8a88c6 2019-08-03 stsp continue;
5597 5f8a88c6 2019-08-03 stsp }
5598 036813ee 2019-05-09 stsp
5599 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5600 036813ee 2019-05-09 stsp continue;
5601 036813ee 2019-05-09 stsp
5602 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5603 62d463ca 2020-10-20 naddy if (err)
5604 036813ee 2019-05-09 stsp return err;
5605 036813ee 2019-05-09 stsp if (!path_matches)
5606 036813ee 2019-05-09 stsp continue;
5607 036813ee 2019-05-09 stsp
5608 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5609 d34b633e 2020-10-19 stsp if (err)
5610 d34b633e 2020-10-19 stsp return err;
5611 d34b633e 2020-10-19 stsp
5612 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5613 d34b633e 2020-10-19 stsp free(ct_name);
5614 036813ee 2019-05-09 stsp continue;
5615 d34b633e 2020-10-19 stsp }
5616 d34b633e 2020-10-19 stsp free(ct_name);
5617 036813ee 2019-05-09 stsp
5618 036813ee 2019-05-09 stsp *ctp = ct;
5619 036813ee 2019-05-09 stsp break;
5620 036813ee 2019-05-09 stsp }
5621 2b496619 2019-07-10 stsp
5622 2b496619 2019-07-10 stsp return err;
5623 2b496619 2019-07-10 stsp }
5624 2b496619 2019-07-10 stsp
5625 2b496619 2019-07-10 stsp static const struct got_error *
5626 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5627 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5628 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5629 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5630 2b496619 2019-07-10 stsp struct got_repository *repo)
5631 2b496619 2019-07-10 stsp {
5632 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5633 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5634 2b496619 2019-07-10 stsp char *subtree_path;
5635 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5636 ba580f68 2020-03-22 stsp int nentries;
5637 2b496619 2019-07-10 stsp
5638 2b496619 2019-07-10 stsp *new_tep = NULL;
5639 2b496619 2019-07-10 stsp
5640 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5641 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5642 2b496619 2019-07-10 stsp child_path) == -1)
5643 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5644 036813ee 2019-05-09 stsp
5645 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5646 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5647 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5648 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5649 56e0773d 2019-11-28 stsp
5650 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5651 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5652 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5653 2b496619 2019-07-10 stsp goto done;
5654 2b496619 2019-07-10 stsp }
5655 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5656 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5657 2b496619 2019-07-10 stsp if (err) {
5658 56e0773d 2019-11-28 stsp free(new_te);
5659 2b496619 2019-07-10 stsp goto done;
5660 2b496619 2019-07-10 stsp }
5661 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5662 2b496619 2019-07-10 stsp done:
5663 56e0773d 2019-11-28 stsp free(id);
5664 2b496619 2019-07-10 stsp free(subtree_path);
5665 2b496619 2019-07-10 stsp if (err == NULL)
5666 2b496619 2019-07-10 stsp *new_tep = new_te;
5667 036813ee 2019-05-09 stsp return err;
5668 036813ee 2019-05-09 stsp }
5669 036813ee 2019-05-09 stsp
5670 036813ee 2019-05-09 stsp static const struct got_error *
5671 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5672 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5673 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5674 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5675 036813ee 2019-05-09 stsp struct got_repository *repo)
5676 036813ee 2019-05-09 stsp {
5677 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5678 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5679 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5680 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5681 036813ee 2019-05-09 stsp
5682 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5683 ba580f68 2020-03-22 stsp *nentries = 0;
5684 036813ee 2019-05-09 stsp
5685 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5686 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5687 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5688 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5689 036813ee 2019-05-09 stsp
5690 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5691 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5692 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5693 036813ee 2019-05-09 stsp continue;
5694 036813ee 2019-05-09 stsp
5695 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5696 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5697 036813ee 2019-05-09 stsp continue;
5698 036813ee 2019-05-09 stsp
5699 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5700 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5701 036813ee 2019-05-09 stsp if (err)
5702 036813ee 2019-05-09 stsp goto done;
5703 036813ee 2019-05-09 stsp
5704 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5705 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5706 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5707 036813ee 2019-05-09 stsp if (err)
5708 036813ee 2019-05-09 stsp goto done;
5709 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5710 afa376bf 2019-05-09 stsp if (err)
5711 afa376bf 2019-05-09 stsp goto done;
5712 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5713 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5714 2b496619 2019-07-10 stsp if (err)
5715 2f51b5b3 2019-05-09 stsp goto done;
5716 ba580f68 2020-03-22 stsp (*nentries)++;
5717 2b496619 2019-07-10 stsp } else {
5718 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5719 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5720 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5721 2b496619 2019-07-10 stsp == NULL) {
5722 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5723 2b496619 2019-07-10 stsp child_path, path_base_tree,
5724 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5725 2b496619 2019-07-10 stsp repo);
5726 2b496619 2019-07-10 stsp if (err)
5727 2b496619 2019-07-10 stsp goto done;
5728 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5729 2b496619 2019-07-10 stsp if (err)
5730 2b496619 2019-07-10 stsp goto done;
5731 ba580f68 2020-03-22 stsp (*nentries)++;
5732 9ba0479c 2019-05-10 stsp }
5733 ed175427 2019-05-09 stsp }
5734 2f51b5b3 2019-05-09 stsp }
5735 2f51b5b3 2019-05-09 stsp
5736 2f51b5b3 2019-05-09 stsp if (base_tree) {
5737 56e0773d 2019-11-28 stsp int i, nbase_entries;
5738 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5739 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5740 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5741 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5742 63c5ca5d 2019-08-24 stsp
5743 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5744 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5745 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5746 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5747 63c5ca5d 2019-08-24 stsp if (err)
5748 63c5ca5d 2019-08-24 stsp goto done;
5749 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5750 63c5ca5d 2019-08-24 stsp if (err)
5751 63c5ca5d 2019-08-24 stsp goto done;
5752 ba580f68 2020-03-22 stsp (*nentries)++;
5753 63c5ca5d 2019-08-24 stsp continue;
5754 63c5ca5d 2019-08-24 stsp }
5755 2f51b5b3 2019-05-09 stsp
5756 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5757 44d03001 2019-05-09 stsp int modified;
5758 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5759 036813ee 2019-05-09 stsp if (err)
5760 036813ee 2019-05-09 stsp goto done;
5761 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5762 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5763 2f51b5b3 2019-05-09 stsp if (err)
5764 2f51b5b3 2019-05-09 stsp goto done;
5765 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5766 44d03001 2019-05-09 stsp if (modified) {
5767 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5768 ba580f68 2020-03-22 stsp int nsubentries;
5769 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5770 ba580f68 2020-03-22 stsp &nsubentries, te,
5771 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5772 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5773 44d03001 2019-05-09 stsp if (err)
5774 44d03001 2019-05-09 stsp goto done;
5775 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5776 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5777 ba580f68 2020-03-22 stsp free(new_id);
5778 ba580f68 2020-03-22 stsp continue;
5779 ba580f68 2020-03-22 stsp }
5780 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5781 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5782 56e0773d 2019-11-28 stsp free(new_id);
5783 44d03001 2019-05-09 stsp }
5784 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5785 036813ee 2019-05-09 stsp if (err)
5786 036813ee 2019-05-09 stsp goto done;
5787 ba580f68 2020-03-22 stsp (*nentries)++;
5788 2f51b5b3 2019-05-09 stsp continue;
5789 036813ee 2019-05-09 stsp }
5790 2f51b5b3 2019-05-09 stsp
5791 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5792 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5793 f66c734c 2019-09-22 stsp if (err)
5794 f66c734c 2019-09-22 stsp goto done;
5795 2f51b5b3 2019-05-09 stsp if (ct) {
5796 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5797 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5798 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5799 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
5800 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5801 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5802 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5803 2f51b5b3 2019-05-09 stsp if (err)
5804 2f51b5b3 2019-05-09 stsp goto done;
5805 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5806 2f51b5b3 2019-05-09 stsp if (err)
5807 2f51b5b3 2019-05-09 stsp goto done;
5808 ba580f68 2020-03-22 stsp (*nentries)++;
5809 2f51b5b3 2019-05-09 stsp }
5810 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5811 b416585c 2019-05-13 stsp status_arg);
5812 afa376bf 2019-05-09 stsp if (err)
5813 afa376bf 2019-05-09 stsp goto done;
5814 2f51b5b3 2019-05-09 stsp } else {
5815 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5816 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5817 2f51b5b3 2019-05-09 stsp if (err)
5818 2f51b5b3 2019-05-09 stsp goto done;
5819 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5820 2f51b5b3 2019-05-09 stsp if (err)
5821 2f51b5b3 2019-05-09 stsp goto done;
5822 ba580f68 2020-03-22 stsp (*nentries)++;
5823 2f51b5b3 2019-05-09 stsp }
5824 ca2503ea 2019-05-09 stsp }
5825 ed175427 2019-05-09 stsp }
5826 0b5cc0d6 2019-05-09 stsp
5827 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5828 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5829 ed175427 2019-05-09 stsp done:
5830 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5831 2e1fa222 2020-07-23 stsp return err;
5832 2e1fa222 2020-07-23 stsp }
5833 2e1fa222 2020-07-23 stsp
5834 2e1fa222 2020-07-23 stsp static const struct got_error *
5835 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5836 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5837 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5838 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5839 ebf99748 2019-05-09 stsp {
5840 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5841 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5842 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5843 ebf99748 2019-05-09 stsp
5844 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5845 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5846 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5847 ebf99748 2019-05-09 stsp
5848 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5849 437adc9d 2020-12-10 yzhong
5850 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5851 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5852 437adc9d 2020-12-10 yzhong if (err)
5853 437adc9d 2020-12-10 yzhong goto done;
5854 437adc9d 2020-12-10 yzhong
5855 ebf99748 2019-05-09 stsp if (ie) {
5856 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5857 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5858 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5859 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5860 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5861 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5862 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5863 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5864 437adc9d 2020-12-10 yzhong
5865 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5866 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5867 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5868 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5869 72fd46fa 2019-09-06 stsp !have_staged_files);
5870 ebf99748 2019-05-09 stsp } else
5871 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5872 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5873 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5874 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5875 72fd46fa 2019-09-06 stsp !have_staged_files);
5876 ebf99748 2019-05-09 stsp } else {
5877 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5878 ebf99748 2019-05-09 stsp if (err)
5879 437adc9d 2020-12-10 yzhong goto done;
5880 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5881 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5882 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5883 3969253a 2020-03-07 stsp if (err) {
5884 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5885 437adc9d 2020-12-10 yzhong goto done;
5886 3969253a 2020-03-07 stsp }
5887 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5888 3969253a 2020-03-07 stsp if (err) {
5889 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5890 437adc9d 2020-12-10 yzhong goto done;
5891 3969253a 2020-03-07 stsp }
5892 ebf99748 2019-05-09 stsp }
5893 437adc9d 2020-12-10 yzhong free(relpath);
5894 437adc9d 2020-12-10 yzhong relpath = NULL;
5895 ebf99748 2019-05-09 stsp }
5896 437adc9d 2020-12-10 yzhong done:
5897 437adc9d 2020-12-10 yzhong free(relpath);
5898 d56d26ce 2019-05-10 stsp return err;
5899 d56d26ce 2019-05-10 stsp }
5900 735ef5ac 2019-08-03 stsp
5901 d56d26ce 2019-05-10 stsp
5902 d56d26ce 2019-05-10 stsp static const struct got_error *
5903 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5904 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5905 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5906 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5907 735ef5ac 2019-08-03 stsp int ood_errcode)
5908 d56d26ce 2019-05-10 stsp {
5909 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5910 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5911 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5912 1a36436d 2019-06-10 stsp
5913 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5914 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5915 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5916 1a36436d 2019-06-10 stsp return NULL;
5917 9bead371 2019-07-28 stsp /*
5918 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5919 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5920 9bead371 2019-07-28 stsp */
5921 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5922 945f9229 2022-04-16 thomas if (err)
5923 945f9229 2022-04-16 thomas goto done;
5924 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5925 9bead371 2019-07-28 stsp if (err) {
5926 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5927 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5928 1a36436d 2019-06-10 stsp goto done;
5929 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5930 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5931 1a36436d 2019-06-10 stsp } else {
5932 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5933 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5934 945f9229 2022-04-16 thomas if (err)
5935 945f9229 2022-04-16 thomas goto done;
5936 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5937 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5938 1a36436d 2019-06-10 stsp goto done;
5939 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5940 1a36436d 2019-06-10 stsp }
5941 1a36436d 2019-06-10 stsp done:
5942 1a36436d 2019-06-10 stsp free(id);
5943 945f9229 2022-04-16 thomas if (commit)
5944 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5945 1a36436d 2019-06-10 stsp return err;
5946 ebf99748 2019-05-09 stsp }
5947 ebf99748 2019-05-09 stsp
5948 ef20f542 2022-06-26 thomas static const struct got_error *
5949 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5950 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5951 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5952 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5953 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5954 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
5955 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5956 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5957 afa376bf 2019-05-09 stsp struct got_repository *repo)
5958 c4296144 2019-05-09 stsp {
5959 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5960 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5961 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5962 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5963 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5964 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5965 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5966 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5967 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5968 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5969 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5970 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5971 892eab0a 2022-07-22 thomas time_t timestamp;
5972 c4296144 2019-05-09 stsp
5973 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5974 c4296144 2019-05-09 stsp
5975 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5976 675c7539 2019-05-09 stsp
5977 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5978 588edf97 2019-05-10 stsp if (err)
5979 588edf97 2019-05-10 stsp goto done;
5980 de18fc63 2019-05-09 stsp
5981 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5982 588edf97 2019-05-10 stsp if (err)
5983 588edf97 2019-05-10 stsp goto done;
5984 588edf97 2019-05-10 stsp
5985 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5986 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
5987 ef899790 2022-11-01 thomas &logmsg, commit_arg);
5988 33ad4cbe 2019-05-12 jcs if (err)
5989 33ad4cbe 2019-05-12 jcs goto done;
5990 33ad4cbe 2019-05-12 jcs }
5991 c4296144 2019-05-09 stsp
5992 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5993 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5994 33ad4cbe 2019-05-12 jcs goto done;
5995 33ad4cbe 2019-05-12 jcs }
5996 33ad4cbe 2019-05-12 jcs
5997 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5998 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5999 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6000 cf066bf8 2019-05-09 stsp char *ondisk_path;
6001 cf066bf8 2019-05-09 stsp
6002 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6003 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6004 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6005 5f8a88c6 2019-08-03 stsp continue;
6006 5f8a88c6 2019-08-03 stsp
6007 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6008 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6009 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6010 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6011 cf066bf8 2019-05-09 stsp continue;
6012 cf066bf8 2019-05-09 stsp
6013 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6014 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6015 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6016 cf066bf8 2019-05-09 stsp goto done;
6017 cf066bf8 2019-05-09 stsp }
6018 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6019 2e1fa222 2020-07-23 stsp free(ondisk_path);
6020 2e1fa222 2020-07-23 stsp if (err)
6021 cf066bf8 2019-05-09 stsp goto done;
6022 cf066bf8 2019-05-09 stsp }
6023 036813ee 2019-05-09 stsp
6024 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6025 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6026 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6027 036813ee 2019-05-09 stsp if (err)
6028 036813ee 2019-05-09 stsp goto done;
6029 036813ee 2019-05-09 stsp
6030 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6031 de18fc63 2019-05-09 stsp if (err)
6032 de18fc63 2019-05-09 stsp goto done;
6033 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6034 10604dce 2021-09-24 thomas nparents++;
6035 10604dce 2021-09-24 thomas if (parent_id2) {
6036 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6037 10604dce 2021-09-24 thomas if (err)
6038 10604dce 2021-09-24 thomas goto done;
6039 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6040 10604dce 2021-09-24 thomas nparents++;
6041 10604dce 2021-09-24 thomas }
6042 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6043 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6044 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6045 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6046 33ad4cbe 2019-05-12 jcs free(logmsg);
6047 9d40349a 2019-05-09 stsp if (err)
6048 9d40349a 2019-05-09 stsp goto done;
6049 9d40349a 2019-05-09 stsp
6050 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6051 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6052 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6053 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6054 09f5bd90 2019-05-09 stsp goto done;
6055 09f5bd90 2019-05-09 stsp }
6056 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6057 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6058 09f5bd90 2019-05-09 stsp if (err)
6059 09f5bd90 2019-05-09 stsp goto done;
6060 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6061 ebf99748 2019-05-09 stsp if (err)
6062 ebf99748 2019-05-09 stsp goto done;
6063 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6064 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6065 09f5bd90 2019-05-09 stsp goto done;
6066 09f5bd90 2019-05-09 stsp }
6067 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6068 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6069 f2c16586 2019-05-09 stsp if (err)
6070 f2c16586 2019-05-09 stsp goto done;
6071 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6072 f2c16586 2019-05-09 stsp if (err)
6073 f2c16586 2019-05-09 stsp goto done;
6074 f2c16586 2019-05-09 stsp
6075 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6076 09f5bd90 2019-05-09 stsp if (err)
6077 09f5bd90 2019-05-09 stsp goto done;
6078 09f5bd90 2019-05-09 stsp
6079 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6080 ebf99748 2019-05-09 stsp if (err)
6081 ebf99748 2019-05-09 stsp goto done;
6082 c4296144 2019-05-09 stsp done:
6083 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6084 588edf97 2019-05-10 stsp if (head_tree)
6085 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6086 588edf97 2019-05-10 stsp if (head_commit)
6087 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6088 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6089 2f17228e 2019-05-12 stsp if (head_ref2) {
6090 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6091 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6092 2f17228e 2019-05-12 stsp err = unlockerr;
6093 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6094 39cd0ff6 2019-07-12 stsp }
6095 39cd0ff6 2019-07-12 stsp return err;
6096 39cd0ff6 2019-07-12 stsp }
6097 5c1e53bc 2019-07-28 stsp
6098 5c1e53bc 2019-07-28 stsp static const struct got_error *
6099 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6100 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6101 5c1e53bc 2019-07-28 stsp {
6102 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6103 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6104 39cd0ff6 2019-07-12 stsp
6105 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6106 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6107 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6108 5c1e53bc 2019-07-28 stsp
6109 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6110 5c1e53bc 2019-07-28 stsp ct_path++;
6111 5c1e53bc 2019-07-28 stsp
6112 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6113 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6114 5c1e53bc 2019-07-28 stsp break;
6115 5c1e53bc 2019-07-28 stsp }
6116 5c1e53bc 2019-07-28 stsp
6117 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6118 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6119 5c1e53bc 2019-07-28 stsp
6120 5c1e53bc 2019-07-28 stsp return NULL;
6121 5c1e53bc 2019-07-28 stsp }
6122 5c1e53bc 2019-07-28 stsp
6123 f0b75401 2019-08-03 stsp static const struct got_error *
6124 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6125 f0b75401 2019-08-03 stsp {
6126 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6127 f0b75401 2019-08-03 stsp
6128 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6129 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6130 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6131 f0b75401 2019-08-03 stsp }
6132 f0b75401 2019-08-03 stsp
6133 f0b75401 2019-08-03 stsp return NULL;
6134 f0b75401 2019-08-03 stsp }
6135 f0b75401 2019-08-03 stsp
6136 5f8a88c6 2019-08-03 stsp static const struct got_error *
6137 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6138 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6139 5f8a88c6 2019-08-03 stsp {
6140 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6141 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6142 5f8a88c6 2019-08-03 stsp
6143 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6144 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6145 5f8a88c6 2019-08-03 stsp continue;
6146 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6147 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6148 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6149 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6150 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6151 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6152 5f8a88c6 2019-08-03 stsp }
6153 5f8a88c6 2019-08-03 stsp
6154 5f8a88c6 2019-08-03 stsp return NULL;
6155 5f8a88c6 2019-08-03 stsp }
6156 5f8a88c6 2019-08-03 stsp
6157 39cd0ff6 2019-07-12 stsp const struct got_error *
6158 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6159 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6160 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6161 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6162 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6163 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6164 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6165 39cd0ff6 2019-07-12 stsp {
6166 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6167 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6168 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6169 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6170 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6171 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6172 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6173 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6174 ef899790 2022-11-01 thomas char *diff_path = NULL;
6175 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6176 39cd0ff6 2019-07-12 stsp
6177 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6178 39cd0ff6 2019-07-12 stsp
6179 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6180 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6181 39cd0ff6 2019-07-12 stsp
6182 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6183 39cd0ff6 2019-07-12 stsp if (err)
6184 39cd0ff6 2019-07-12 stsp goto done;
6185 39cd0ff6 2019-07-12 stsp
6186 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6187 39cd0ff6 2019-07-12 stsp if (err)
6188 39cd0ff6 2019-07-12 stsp goto done;
6189 39cd0ff6 2019-07-12 stsp
6190 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6191 39cd0ff6 2019-07-12 stsp if (err)
6192 39cd0ff6 2019-07-12 stsp goto done;
6193 39cd0ff6 2019-07-12 stsp
6194 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6195 39cd0ff6 2019-07-12 stsp if (err)
6196 39cd0ff6 2019-07-12 stsp goto done;
6197 39cd0ff6 2019-07-12 stsp
6198 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6199 5f8a88c6 2019-08-03 stsp &have_staged_files);
6200 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6201 5f8a88c6 2019-08-03 stsp goto done;
6202 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6203 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6204 5f8a88c6 2019-08-03 stsp if (err)
6205 5f8a88c6 2019-08-03 stsp goto done;
6206 5f8a88c6 2019-08-03 stsp }
6207 5f8a88c6 2019-08-03 stsp
6208 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6209 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6210 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6211 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6212 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6213 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6214 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6215 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6216 ef899790 2022-11-01 thomas if (show_diff) {
6217 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6218 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6219 ef899790 2022-11-01 thomas if (err)
6220 ef899790 2022-11-01 thomas goto done;
6221 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6222 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6223 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6224 ef899790 2022-11-01 thomas goto done;
6225 ef899790 2022-11-01 thomas }
6226 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6227 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6228 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6229 ef899790 2022-11-01 thomas goto done;
6230 ef899790 2022-11-01 thomas }
6231 ef899790 2022-11-01 thomas }
6232 ef899790 2022-11-01 thomas
6233 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6234 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6235 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6236 5c1e53bc 2019-07-28 stsp if (err)
6237 5c1e53bc 2019-07-28 stsp goto done;
6238 5c1e53bc 2019-07-28 stsp }
6239 39cd0ff6 2019-07-12 stsp
6240 ef899790 2022-11-01 thomas if (show_diff) {
6241 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6242 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6243 ef899790 2022-11-01 thomas goto done;
6244 ef899790 2022-11-01 thomas }
6245 ef899790 2022-11-01 thomas }
6246 ef899790 2022-11-01 thomas
6247 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6248 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6249 39cd0ff6 2019-07-12 stsp goto done;
6250 5c1e53bc 2019-07-28 stsp }
6251 5c1e53bc 2019-07-28 stsp
6252 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6253 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6254 5c1e53bc 2019-07-28 stsp if (err)
6255 5c1e53bc 2019-07-28 stsp goto done;
6256 39cd0ff6 2019-07-12 stsp }
6257 f0b75401 2019-08-03 stsp
6258 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6259 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6260 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6261 f0b75401 2019-08-03 stsp
6262 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6263 f0b75401 2019-08-03 stsp ct_path++;
6264 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6265 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6266 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6267 b50cabdf 2019-07-12 stsp if (err)
6268 b50cabdf 2019-07-12 stsp goto done;
6269 f0b75401 2019-08-03 stsp
6270 b50cabdf 2019-07-12 stsp }
6271 b50cabdf 2019-07-12 stsp
6272 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6273 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6274 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6275 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6276 39cd0ff6 2019-07-12 stsp if (err)
6277 39cd0ff6 2019-07-12 stsp goto done;
6278 39cd0ff6 2019-07-12 stsp
6279 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6280 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6281 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6282 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6283 39cd0ff6 2019-07-12 stsp err = sync_err;
6284 39cd0ff6 2019-07-12 stsp done:
6285 39cd0ff6 2019-07-12 stsp if (fileindex)
6286 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6287 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6288 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6289 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6290 39cd0ff6 2019-07-12 stsp err = unlockerr;
6291 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6292 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6293 21c2d8be 2023-01-10 thomas
6294 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6295 39cd0ff6 2019-07-12 stsp }
6296 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6297 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6298 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6299 ef899790 2022-11-01 thomas free(diff_path);
6300 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6301 ef899790 2022-11-01 thomas err == NULL)
6302 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6303 c4296144 2019-05-09 stsp return err;
6304 8656d6c4 2019-05-20 stsp }
6305 8656d6c4 2019-05-20 stsp
6306 8656d6c4 2019-05-20 stsp const char *
6307 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6308 8656d6c4 2019-05-20 stsp {
6309 8656d6c4 2019-05-20 stsp return ct->path;
6310 8656d6c4 2019-05-20 stsp }
6311 8656d6c4 2019-05-20 stsp
6312 8656d6c4 2019-05-20 stsp unsigned int
6313 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6314 8656d6c4 2019-05-20 stsp {
6315 8656d6c4 2019-05-20 stsp return ct->status;
6316 818c7501 2019-07-11 stsp }
6317 818c7501 2019-07-11 stsp
6318 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6319 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6320 818c7501 2019-07-11 stsp struct got_repository *repo;
6321 818c7501 2019-07-11 stsp };
6322 818c7501 2019-07-11 stsp
6323 818c7501 2019-07-11 stsp static const struct got_error *
6324 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6325 818c7501 2019-07-11 stsp {
6326 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6327 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6328 818c7501 2019-07-11 stsp unsigned char status;
6329 818c7501 2019-07-11 stsp struct stat sb;
6330 818c7501 2019-07-11 stsp char *ondisk_path;
6331 818c7501 2019-07-11 stsp
6332 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6333 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6334 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6335 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6336 818c7501 2019-07-11 stsp
6337 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6338 818c7501 2019-07-11 stsp == -1)
6339 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6340 818c7501 2019-07-11 stsp
6341 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6342 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6343 818c7501 2019-07-11 stsp free(ondisk_path);
6344 818c7501 2019-07-11 stsp if (err)
6345 818c7501 2019-07-11 stsp return err;
6346 818c7501 2019-07-11 stsp
6347 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6348 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6349 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6350 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6351 818c7501 2019-07-11 stsp
6352 818c7501 2019-07-11 stsp return NULL;
6353 818c7501 2019-07-11 stsp }
6354 818c7501 2019-07-11 stsp
6355 818c7501 2019-07-11 stsp const struct got_error *
6356 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6357 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6358 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6359 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6360 818c7501 2019-07-11 stsp {
6361 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6362 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6363 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6364 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6365 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6366 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6367 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6368 818c7501 2019-07-11 stsp
6369 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6370 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6371 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6372 818c7501 2019-07-11 stsp
6373 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6374 818c7501 2019-07-11 stsp if (err)
6375 818c7501 2019-07-11 stsp return err;
6376 818c7501 2019-07-11 stsp
6377 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6378 818c7501 2019-07-11 stsp if (err)
6379 818c7501 2019-07-11 stsp goto done;
6380 818c7501 2019-07-11 stsp
6381 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6382 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6383 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6384 818c7501 2019-07-11 stsp &ok_arg);
6385 818c7501 2019-07-11 stsp if (err)
6386 818c7501 2019-07-11 stsp goto done;
6387 818c7501 2019-07-11 stsp
6388 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6389 818c7501 2019-07-11 stsp if (err)
6390 818c7501 2019-07-11 stsp goto done;
6391 818c7501 2019-07-11 stsp
6392 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6393 818c7501 2019-07-11 stsp if (err)
6394 818c7501 2019-07-11 stsp goto done;
6395 818c7501 2019-07-11 stsp
6396 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6397 818c7501 2019-07-11 stsp if (err)
6398 818c7501 2019-07-11 stsp goto done;
6399 818c7501 2019-07-11 stsp
6400 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6401 818c7501 2019-07-11 stsp 0);
6402 e51d7b55 2020-01-04 stsp if (err)
6403 e51d7b55 2020-01-04 stsp goto done;
6404 e51d7b55 2020-01-04 stsp
6405 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6406 818c7501 2019-07-11 stsp if (err)
6407 818c7501 2019-07-11 stsp goto done;
6408 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6409 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6410 e51d7b55 2020-01-04 stsp goto done;
6411 e51d7b55 2020-01-04 stsp }
6412 818c7501 2019-07-11 stsp
6413 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6414 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6415 818c7501 2019-07-11 stsp if (err)
6416 818c7501 2019-07-11 stsp goto done;
6417 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6418 818c7501 2019-07-11 stsp if (err)
6419 818c7501 2019-07-11 stsp goto done;
6420 818c7501 2019-07-11 stsp
6421 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6422 818c7501 2019-07-11 stsp
6423 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6424 818c7501 2019-07-11 stsp if (err)
6425 818c7501 2019-07-11 stsp goto done;
6426 818c7501 2019-07-11 stsp
6427 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6428 818c7501 2019-07-11 stsp if (err)
6429 818c7501 2019-07-11 stsp goto done;
6430 818c7501 2019-07-11 stsp
6431 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6432 818c7501 2019-07-11 stsp worktree->base_commit_id);
6433 818c7501 2019-07-11 stsp if (err)
6434 818c7501 2019-07-11 stsp goto done;
6435 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6436 818c7501 2019-07-11 stsp if (err)
6437 818c7501 2019-07-11 stsp goto done;
6438 818c7501 2019-07-11 stsp
6439 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6440 818c7501 2019-07-11 stsp if (err)
6441 818c7501 2019-07-11 stsp goto done;
6442 818c7501 2019-07-11 stsp done:
6443 818c7501 2019-07-11 stsp free(fileindex_path);
6444 818c7501 2019-07-11 stsp free(tmp_branch_name);
6445 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6446 818c7501 2019-07-11 stsp free(branch_ref_name);
6447 818c7501 2019-07-11 stsp if (branch_ref)
6448 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6449 818c7501 2019-07-11 stsp if (wt_branch)
6450 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6451 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6452 818c7501 2019-07-11 stsp if (err) {
6453 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6454 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6455 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6456 818c7501 2019-07-11 stsp }
6457 818c7501 2019-07-11 stsp if (*tmp_branch) {
6458 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6459 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6460 818c7501 2019-07-11 stsp }
6461 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6462 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6463 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6464 3e3a69f1 2019-07-25 stsp }
6465 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6466 818c7501 2019-07-11 stsp }
6467 818c7501 2019-07-11 stsp return err;
6468 818c7501 2019-07-11 stsp }
6469 818c7501 2019-07-11 stsp
6470 818c7501 2019-07-11 stsp const struct got_error *
6471 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6472 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6473 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6474 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6475 818c7501 2019-07-11 stsp {
6476 818c7501 2019-07-11 stsp const struct got_error *err;
6477 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6478 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6479 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6480 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6481 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6482 818c7501 2019-07-11 stsp
6483 818c7501 2019-07-11 stsp *commit_id = NULL;
6484 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6485 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6486 3e3a69f1 2019-07-25 stsp *branch = NULL;
6487 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6488 3e3a69f1 2019-07-25 stsp
6489 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6490 3e3a69f1 2019-07-25 stsp if (err)
6491 3e3a69f1 2019-07-25 stsp return err;
6492 818c7501 2019-07-11 stsp
6493 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6494 3e3a69f1 2019-07-25 stsp if (err)
6495 f032f1f7 2019-08-04 stsp goto done;
6496 f032f1f7 2019-08-04 stsp
6497 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6498 f032f1f7 2019-08-04 stsp &have_staged_files);
6499 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6500 f032f1f7 2019-08-04 stsp goto done;
6501 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6502 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6503 3e3a69f1 2019-07-25 stsp goto done;
6504 f032f1f7 2019-08-04 stsp }
6505 3e3a69f1 2019-07-25 stsp
6506 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6507 818c7501 2019-07-11 stsp if (err)
6508 f032f1f7 2019-08-04 stsp goto done;
6509 818c7501 2019-07-11 stsp
6510 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6511 818c7501 2019-07-11 stsp if (err)
6512 818c7501 2019-07-11 stsp goto done;
6513 818c7501 2019-07-11 stsp
6514 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6515 818c7501 2019-07-11 stsp if (err)
6516 818c7501 2019-07-11 stsp goto done;
6517 818c7501 2019-07-11 stsp
6518 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6519 818c7501 2019-07-11 stsp if (err)
6520 818c7501 2019-07-11 stsp goto done;
6521 818c7501 2019-07-11 stsp
6522 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6523 818c7501 2019-07-11 stsp if (err)
6524 818c7501 2019-07-11 stsp goto done;
6525 818c7501 2019-07-11 stsp
6526 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6527 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6528 818c7501 2019-07-11 stsp if (err)
6529 818c7501 2019-07-11 stsp goto done;
6530 818c7501 2019-07-11 stsp
6531 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6532 818c7501 2019-07-11 stsp if (err)
6533 818c7501 2019-07-11 stsp goto done;
6534 818c7501 2019-07-11 stsp
6535 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6536 818c7501 2019-07-11 stsp if (err)
6537 818c7501 2019-07-11 stsp goto done;
6538 818c7501 2019-07-11 stsp
6539 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6540 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6541 818c7501 2019-07-11 stsp if (err)
6542 818c7501 2019-07-11 stsp goto done;
6543 818c7501 2019-07-11 stsp
6544 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6545 818c7501 2019-07-11 stsp if (err)
6546 818c7501 2019-07-11 stsp goto done;
6547 818c7501 2019-07-11 stsp done:
6548 818c7501 2019-07-11 stsp free(commit_ref_name);
6549 818c7501 2019-07-11 stsp free(branch_ref_name);
6550 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6551 818c7501 2019-07-11 stsp if (commit_ref)
6552 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6553 818c7501 2019-07-11 stsp if (branch_ref)
6554 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6555 818c7501 2019-07-11 stsp if (err) {
6556 818c7501 2019-07-11 stsp free(*commit_id);
6557 818c7501 2019-07-11 stsp *commit_id = NULL;
6558 818c7501 2019-07-11 stsp if (*tmp_branch) {
6559 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6560 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6561 818c7501 2019-07-11 stsp }
6562 818c7501 2019-07-11 stsp if (*new_base_branch) {
6563 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6564 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6565 818c7501 2019-07-11 stsp }
6566 818c7501 2019-07-11 stsp if (*branch) {
6567 818c7501 2019-07-11 stsp got_ref_close(*branch);
6568 818c7501 2019-07-11 stsp *branch = NULL;
6569 818c7501 2019-07-11 stsp }
6570 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6571 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6572 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6573 3e3a69f1 2019-07-25 stsp }
6574 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6575 818c7501 2019-07-11 stsp }
6576 818c7501 2019-07-11 stsp return err;
6577 c4296144 2019-05-09 stsp }
6578 818c7501 2019-07-11 stsp
6579 818c7501 2019-07-11 stsp const struct got_error *
6580 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6581 818c7501 2019-07-11 stsp {
6582 818c7501 2019-07-11 stsp const struct got_error *err;
6583 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6584 818c7501 2019-07-11 stsp
6585 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6586 818c7501 2019-07-11 stsp if (err)
6587 818c7501 2019-07-11 stsp return err;
6588 818c7501 2019-07-11 stsp
6589 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6590 818c7501 2019-07-11 stsp free(tmp_branch_name);
6591 818c7501 2019-07-11 stsp return NULL;
6592 818c7501 2019-07-11 stsp }
6593 818c7501 2019-07-11 stsp
6594 818c7501 2019-07-11 stsp static const struct got_error *
6595 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6596 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6597 818c7501 2019-07-11 stsp {
6598 0ebf8283 2019-07-24 stsp *logmsg = arg;
6599 818c7501 2019-07-11 stsp return NULL;
6600 818c7501 2019-07-11 stsp }
6601 818c7501 2019-07-11 stsp
6602 818c7501 2019-07-11 stsp static const struct got_error *
6603 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6604 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6605 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6606 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6607 818c7501 2019-07-11 stsp {
6608 818c7501 2019-07-11 stsp return NULL;
6609 01757395 2019-07-12 stsp }
6610 01757395 2019-07-12 stsp
6611 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6612 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6613 01757395 2019-07-12 stsp void *progress_arg;
6614 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6615 01757395 2019-07-12 stsp };
6616 01757395 2019-07-12 stsp
6617 01757395 2019-07-12 stsp static const struct got_error *
6618 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6619 01757395 2019-07-12 stsp {
6620 01757395 2019-07-12 stsp const struct got_error *err;
6621 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6622 01757395 2019-07-12 stsp char *p;
6623 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6624 01757395 2019-07-12 stsp
6625 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6626 01757395 2019-07-12 stsp if (err)
6627 01757395 2019-07-12 stsp return err;
6628 01757395 2019-07-12 stsp
6629 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6630 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6631 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6632 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6633 01757395 2019-07-12 stsp return NULL;
6634 01757395 2019-07-12 stsp
6635 01757395 2019-07-12 stsp p = strdup(path);
6636 01757395 2019-07-12 stsp if (p == NULL)
6637 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6638 01757395 2019-07-12 stsp
6639 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6640 01757395 2019-07-12 stsp if (err || new == NULL)
6641 01757395 2019-07-12 stsp free(p);
6642 01757395 2019-07-12 stsp return err;
6643 818c7501 2019-07-11 stsp }
6644 818c7501 2019-07-11 stsp
6645 0ebf8283 2019-07-24 stsp static const struct got_error *
6646 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6647 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6648 818c7501 2019-07-11 stsp {
6649 818c7501 2019-07-11 stsp const struct got_error *err;
6650 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6651 818c7501 2019-07-11 stsp
6652 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6653 818c7501 2019-07-11 stsp if (err) {
6654 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6655 818c7501 2019-07-11 stsp goto done;
6656 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6657 818c7501 2019-07-11 stsp if (err)
6658 818c7501 2019-07-11 stsp goto done;
6659 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6660 818c7501 2019-07-11 stsp if (err)
6661 818c7501 2019-07-11 stsp goto done;
6662 de05890f 2020-03-05 stsp } else if (is_rebase) {
6663 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6664 818c7501 2019-07-11 stsp int cmp;
6665 818c7501 2019-07-11 stsp
6666 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6667 818c7501 2019-07-11 stsp if (err)
6668 818c7501 2019-07-11 stsp goto done;
6669 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6670 818c7501 2019-07-11 stsp free(stored_id);
6671 818c7501 2019-07-11 stsp if (cmp != 0) {
6672 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6673 818c7501 2019-07-11 stsp goto done;
6674 818c7501 2019-07-11 stsp }
6675 818c7501 2019-07-11 stsp }
6676 0ebf8283 2019-07-24 stsp done:
6677 0ebf8283 2019-07-24 stsp if (commit_ref)
6678 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6679 0ebf8283 2019-07-24 stsp return err;
6680 0ebf8283 2019-07-24 stsp }
6681 0ebf8283 2019-07-24 stsp
6682 0ebf8283 2019-07-24 stsp static const struct got_error *
6683 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6684 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6685 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6686 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6687 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6688 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6689 0ebf8283 2019-07-24 stsp {
6690 0ebf8283 2019-07-24 stsp const struct got_error *err;
6691 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6692 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6693 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6694 818c7501 2019-07-11 stsp
6695 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6696 0ebf8283 2019-07-24 stsp
6697 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6698 0ebf8283 2019-07-24 stsp if (err)
6699 0ebf8283 2019-07-24 stsp return err;
6700 0ebf8283 2019-07-24 stsp
6701 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6702 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6703 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6704 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6705 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6706 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6707 818c7501 2019-07-11 stsp if (commit_ref)
6708 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6709 818c7501 2019-07-11 stsp return err;
6710 818c7501 2019-07-11 stsp }
6711 818c7501 2019-07-11 stsp
6712 818c7501 2019-07-11 stsp const struct got_error *
6713 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6714 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6715 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6716 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6717 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6718 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6719 818c7501 2019-07-11 stsp {
6720 0ebf8283 2019-07-24 stsp const struct got_error *err;
6721 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6722 0ebf8283 2019-07-24 stsp
6723 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6724 0ebf8283 2019-07-24 stsp if (err)
6725 0ebf8283 2019-07-24 stsp return err;
6726 0ebf8283 2019-07-24 stsp
6727 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6728 0ebf8283 2019-07-24 stsp if (err)
6729 0ebf8283 2019-07-24 stsp goto done;
6730 0ebf8283 2019-07-24 stsp
6731 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6732 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6733 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6734 0ebf8283 2019-07-24 stsp done:
6735 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6736 0ebf8283 2019-07-24 stsp return err;
6737 0ebf8283 2019-07-24 stsp }
6738 0ebf8283 2019-07-24 stsp
6739 0ebf8283 2019-07-24 stsp const struct got_error *
6740 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6741 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6742 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6743 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6744 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6745 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6746 0ebf8283 2019-07-24 stsp {
6747 0ebf8283 2019-07-24 stsp const struct got_error *err;
6748 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6749 0ebf8283 2019-07-24 stsp
6750 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6751 0ebf8283 2019-07-24 stsp if (err)
6752 0ebf8283 2019-07-24 stsp return err;
6753 0ebf8283 2019-07-24 stsp
6754 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6755 0ebf8283 2019-07-24 stsp if (err)
6756 0ebf8283 2019-07-24 stsp goto done;
6757 0ebf8283 2019-07-24 stsp
6758 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6759 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6760 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6761 0ebf8283 2019-07-24 stsp done:
6762 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6763 0ebf8283 2019-07-24 stsp return err;
6764 0ebf8283 2019-07-24 stsp }
6765 0ebf8283 2019-07-24 stsp
6766 0ebf8283 2019-07-24 stsp static const struct got_error *
6767 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6768 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6769 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6770 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6771 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6772 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6773 0ebf8283 2019-07-24 stsp {
6774 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6775 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6776 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6777 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6778 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6779 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6780 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6781 818c7501 2019-07-11 stsp
6782 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6783 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6784 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6785 a0e95631 2019-07-12 stsp
6786 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6787 818c7501 2019-07-11 stsp
6788 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6789 a0e95631 2019-07-12 stsp if (err)
6790 3e3a69f1 2019-07-25 stsp return err;
6791 a0e95631 2019-07-12 stsp
6792 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6793 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6794 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6795 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6796 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
6797 01757395 2019-07-12 stsp /*
6798 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6799 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6800 6c13b005 2021-09-02 stsp *
6801 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6802 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6803 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6804 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6805 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6806 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6807 01757395 2019-07-12 stsp */
6808 01757395 2019-07-12 stsp if (merged_paths) {
6809 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6810 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6811 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6812 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6813 f2a9dc41 2019-12-13 tracey 0);
6814 01757395 2019-07-12 stsp if (err)
6815 01757395 2019-07-12 stsp goto done;
6816 01757395 2019-07-12 stsp }
6817 01757395 2019-07-12 stsp } else {
6818 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6819 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6820 01757395 2019-07-12 stsp if (err)
6821 01757395 2019-07-12 stsp goto done;
6822 01757395 2019-07-12 stsp }
6823 a0e95631 2019-07-12 stsp
6824 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6825 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6826 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6827 a0e95631 2019-07-12 stsp if (err)
6828 a0e95631 2019-07-12 stsp goto done;
6829 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6830 a0e95631 2019-07-12 stsp goto done;
6831 ff0d2220 2019-07-11 stsp }
6832 818c7501 2019-07-11 stsp
6833 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6834 edd02c5e 2019-07-11 stsp if (err)
6835 edd02c5e 2019-07-11 stsp goto done;
6836 edd02c5e 2019-07-11 stsp
6837 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6838 818c7501 2019-07-11 stsp if (err)
6839 818c7501 2019-07-11 stsp goto done;
6840 818c7501 2019-07-11 stsp
6841 5943eee2 2019-08-13 stsp if (new_logmsg) {
6842 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6843 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6844 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6845 5943eee2 2019-08-13 stsp goto done;
6846 5943eee2 2019-08-13 stsp }
6847 5943eee2 2019-08-13 stsp } else {
6848 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6849 5943eee2 2019-08-13 stsp if (err)
6850 5943eee2 2019-08-13 stsp goto done;
6851 5943eee2 2019-08-13 stsp }
6852 0ebf8283 2019-07-24 stsp
6853 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6854 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6855 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6856 8db00f97 2022-07-22 thomas committer ? committer :
6857 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
6858 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6859 a0e95631 2019-07-12 stsp if (err)
6860 a0e95631 2019-07-12 stsp goto done;
6861 a0e95631 2019-07-12 stsp
6862 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6863 a0e95631 2019-07-12 stsp if (err)
6864 a0e95631 2019-07-12 stsp goto done;
6865 a0e95631 2019-07-12 stsp
6866 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6867 a0e95631 2019-07-12 stsp if (err)
6868 a0e95631 2019-07-12 stsp goto done;
6869 a0e95631 2019-07-12 stsp
6870 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6871 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6872 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6873 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6874 a0e95631 2019-07-12 stsp err = sync_err;
6875 818c7501 2019-07-11 stsp done:
6876 a0e95631 2019-07-12 stsp free(fileindex_path);
6877 a0e95631 2019-07-12 stsp free(head_commit_id);
6878 a0e95631 2019-07-12 stsp if (head_ref)
6879 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6880 818c7501 2019-07-11 stsp if (err) {
6881 818c7501 2019-07-11 stsp free(*new_commit_id);
6882 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6883 818c7501 2019-07-11 stsp }
6884 818c7501 2019-07-11 stsp return err;
6885 818c7501 2019-07-11 stsp }
6886 818c7501 2019-07-11 stsp
6887 818c7501 2019-07-11 stsp const struct got_error *
6888 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6889 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6890 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6891 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6892 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
6893 be94c032 2023-02-20 thomas struct got_repository *repo)
6894 0ebf8283 2019-07-24 stsp {
6895 0ebf8283 2019-07-24 stsp const struct got_error *err;
6896 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6897 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6898 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6899 0ebf8283 2019-07-24 stsp
6900 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6901 0ebf8283 2019-07-24 stsp if (err)
6902 0ebf8283 2019-07-24 stsp return err;
6903 0ebf8283 2019-07-24 stsp
6904 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6905 0ebf8283 2019-07-24 stsp if (err)
6906 0ebf8283 2019-07-24 stsp goto done;
6907 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6908 0ebf8283 2019-07-24 stsp if (err)
6909 0ebf8283 2019-07-24 stsp goto done;
6910 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6911 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6912 0ebf8283 2019-07-24 stsp goto done;
6913 0ebf8283 2019-07-24 stsp }
6914 0ebf8283 2019-07-24 stsp
6915 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6916 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6917 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
6918 0ebf8283 2019-07-24 stsp done:
6919 0ebf8283 2019-07-24 stsp if (commit_ref)
6920 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6921 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6922 0ebf8283 2019-07-24 stsp free(commit_id);
6923 0ebf8283 2019-07-24 stsp return err;
6924 0ebf8283 2019-07-24 stsp }
6925 0ebf8283 2019-07-24 stsp
6926 0ebf8283 2019-07-24 stsp const struct got_error *
6927 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6928 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6929 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6930 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6931 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6932 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6933 0ebf8283 2019-07-24 stsp {
6934 0ebf8283 2019-07-24 stsp const struct got_error *err;
6935 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6936 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6937 0ebf8283 2019-07-24 stsp
6938 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6939 0ebf8283 2019-07-24 stsp if (err)
6940 0ebf8283 2019-07-24 stsp return err;
6941 0ebf8283 2019-07-24 stsp
6942 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6943 0ebf8283 2019-07-24 stsp if (err)
6944 0ebf8283 2019-07-24 stsp goto done;
6945 0ebf8283 2019-07-24 stsp
6946 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6947 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6948 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
6949 0ebf8283 2019-07-24 stsp done:
6950 0ebf8283 2019-07-24 stsp if (commit_ref)
6951 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6952 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6953 0ebf8283 2019-07-24 stsp return err;
6954 0ebf8283 2019-07-24 stsp }
6955 0ebf8283 2019-07-24 stsp
6956 0ebf8283 2019-07-24 stsp const struct got_error *
6957 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6958 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6959 818c7501 2019-07-11 stsp {
6960 3e3a69f1 2019-07-25 stsp if (fileindex)
6961 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6962 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6963 69844fba 2019-07-11 stsp }
6964 69844fba 2019-07-11 stsp
6965 69844fba 2019-07-11 stsp static const struct got_error *
6966 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6967 69844fba 2019-07-11 stsp {
6968 69844fba 2019-07-11 stsp const struct got_error *err;
6969 69844fba 2019-07-11 stsp struct got_reference *ref;
6970 69844fba 2019-07-11 stsp
6971 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6972 69844fba 2019-07-11 stsp if (err) {
6973 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6974 69844fba 2019-07-11 stsp return NULL;
6975 69844fba 2019-07-11 stsp return err;
6976 69844fba 2019-07-11 stsp }
6977 69844fba 2019-07-11 stsp
6978 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6979 69844fba 2019-07-11 stsp got_ref_close(ref);
6980 69844fba 2019-07-11 stsp return err;
6981 818c7501 2019-07-11 stsp }
6982 818c7501 2019-07-11 stsp
6983 69844fba 2019-07-11 stsp static const struct got_error *
6984 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6985 69844fba 2019-07-11 stsp {
6986 69844fba 2019-07-11 stsp const struct got_error *err;
6987 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6988 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6989 69844fba 2019-07-11 stsp
6990 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6991 69844fba 2019-07-11 stsp if (err)
6992 69844fba 2019-07-11 stsp goto done;
6993 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6994 69844fba 2019-07-11 stsp if (err)
6995 69844fba 2019-07-11 stsp goto done;
6996 69844fba 2019-07-11 stsp
6997 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6998 69844fba 2019-07-11 stsp if (err)
6999 69844fba 2019-07-11 stsp goto done;
7000 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7001 69844fba 2019-07-11 stsp if (err)
7002 69844fba 2019-07-11 stsp goto done;
7003 69844fba 2019-07-11 stsp
7004 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7005 69844fba 2019-07-11 stsp if (err)
7006 69844fba 2019-07-11 stsp goto done;
7007 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7008 69844fba 2019-07-11 stsp if (err)
7009 69844fba 2019-07-11 stsp goto done;
7010 69844fba 2019-07-11 stsp
7011 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7012 69844fba 2019-07-11 stsp if (err)
7013 69844fba 2019-07-11 stsp goto done;
7014 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7015 69844fba 2019-07-11 stsp if (err)
7016 69844fba 2019-07-11 stsp goto done;
7017 69844fba 2019-07-11 stsp
7018 69844fba 2019-07-11 stsp done:
7019 69844fba 2019-07-11 stsp free(tmp_branch_name);
7020 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7021 69844fba 2019-07-11 stsp free(branch_ref_name);
7022 69844fba 2019-07-11 stsp free(commit_ref_name);
7023 e600f124 2021-03-21 stsp return err;
7024 e600f124 2021-03-21 stsp }
7025 e600f124 2021-03-21 stsp
7026 ef20f542 2022-06-26 thomas static const struct got_error *
7027 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7028 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7029 e600f124 2021-03-21 stsp {
7030 e600f124 2021-03-21 stsp const struct got_error *err;
7031 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7032 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7033 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7034 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7035 e600f124 2021-03-21 stsp char *refname = NULL;
7036 e600f124 2021-03-21 stsp
7037 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7038 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7039 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7040 e600f124 2021-03-21 stsp branch_name += 11;
7041 e600f124 2021-03-21 stsp
7042 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7043 e600f124 2021-03-21 stsp if (err)
7044 e600f124 2021-03-21 stsp return err;
7045 e600f124 2021-03-21 stsp
7046 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7047 e600f124 2021-03-21 stsp new_id_str) == -1) {
7048 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7049 e600f124 2021-03-21 stsp goto done;
7050 e600f124 2021-03-21 stsp }
7051 e600f124 2021-03-21 stsp
7052 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7053 e600f124 2021-03-21 stsp if (err)
7054 e600f124 2021-03-21 stsp goto done;
7055 e600f124 2021-03-21 stsp
7056 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7057 e600f124 2021-03-21 stsp if (err)
7058 e600f124 2021-03-21 stsp goto done;
7059 e600f124 2021-03-21 stsp
7060 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7061 e600f124 2021-03-21 stsp done:
7062 e600f124 2021-03-21 stsp free(new_id_str);
7063 e600f124 2021-03-21 stsp free(refname);
7064 e600f124 2021-03-21 stsp free(old_commit_id);
7065 e600f124 2021-03-21 stsp if (ref)
7066 e600f124 2021-03-21 stsp got_ref_close(ref);
7067 69844fba 2019-07-11 stsp return err;
7068 69844fba 2019-07-11 stsp }
7069 69844fba 2019-07-11 stsp
7070 818c7501 2019-07-11 stsp const struct got_error *
7071 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7072 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7073 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7074 f08f28be 2023-02-03 thomas int create_backup)
7075 818c7501 2019-07-11 stsp {
7076 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7077 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7078 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7079 818c7501 2019-07-11 stsp
7080 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7081 818c7501 2019-07-11 stsp if (err)
7082 818c7501 2019-07-11 stsp return err;
7083 e600f124 2021-03-21 stsp
7084 e600f124 2021-03-21 stsp if (create_backup) {
7085 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7086 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7087 e600f124 2021-03-21 stsp if (err)
7088 e600f124 2021-03-21 stsp goto done;
7089 e600f124 2021-03-21 stsp }
7090 818c7501 2019-07-11 stsp
7091 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7092 818c7501 2019-07-11 stsp if (err)
7093 818c7501 2019-07-11 stsp goto done;
7094 818c7501 2019-07-11 stsp
7095 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7096 818c7501 2019-07-11 stsp if (err)
7097 818c7501 2019-07-11 stsp goto done;
7098 818c7501 2019-07-11 stsp
7099 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7100 818c7501 2019-07-11 stsp if (err)
7101 818c7501 2019-07-11 stsp goto done;
7102 818c7501 2019-07-11 stsp
7103 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7104 a615e0e7 2020-12-16 stsp if (err)
7105 a615e0e7 2020-12-16 stsp goto done;
7106 a615e0e7 2020-12-16 stsp
7107 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7108 a615e0e7 2020-12-16 stsp if (err)
7109 a615e0e7 2020-12-16 stsp goto done;
7110 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7111 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7112 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7113 a615e0e7 2020-12-16 stsp err = sync_err;
7114 818c7501 2019-07-11 stsp done:
7115 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7116 a615e0e7 2020-12-16 stsp free(fileindex_path);
7117 818c7501 2019-07-11 stsp free(new_head_commit_id);
7118 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7119 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7120 818c7501 2019-07-11 stsp err = unlockerr;
7121 818c7501 2019-07-11 stsp return err;
7122 818c7501 2019-07-11 stsp }
7123 818c7501 2019-07-11 stsp
7124 818c7501 2019-07-11 stsp const struct got_error *
7125 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7126 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7127 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7128 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7129 818c7501 2019-07-11 stsp {
7130 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7131 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7132 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7133 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7134 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7135 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7136 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7137 818c7501 2019-07-11 stsp
7138 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7139 818c7501 2019-07-11 stsp if (err)
7140 818c7501 2019-07-11 stsp return err;
7141 818c7501 2019-07-11 stsp
7142 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7143 945f9229 2022-04-16 thomas worktree->base_commit_id);
7144 945f9229 2022-04-16 thomas if (err)
7145 945f9229 2022-04-16 thomas goto done;
7146 945f9229 2022-04-16 thomas
7147 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7148 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7149 818c7501 2019-07-11 stsp if (err)
7150 818c7501 2019-07-11 stsp goto done;
7151 818c7501 2019-07-11 stsp
7152 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7153 818c7501 2019-07-11 stsp if (err)
7154 818c7501 2019-07-11 stsp goto done;
7155 818c7501 2019-07-11 stsp
7156 818c7501 2019-07-11 stsp /*
7157 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7158 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7159 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7160 818c7501 2019-07-11 stsp */
7161 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7162 818c7501 2019-07-11 stsp if (err)
7163 818c7501 2019-07-11 stsp goto done;
7164 818c7501 2019-07-11 stsp
7165 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7166 818c7501 2019-07-11 stsp if (err)
7167 818c7501 2019-07-11 stsp goto done;
7168 818c7501 2019-07-11 stsp
7169 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7170 945f9229 2022-04-16 thomas worktree->path_prefix);
7171 ca355955 2019-07-12 stsp if (err)
7172 ca355955 2019-07-12 stsp goto done;
7173 ca355955 2019-07-12 stsp
7174 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7175 ca355955 2019-07-12 stsp if (err)
7176 ca355955 2019-07-12 stsp goto done;
7177 ca355955 2019-07-12 stsp
7178 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7179 818c7501 2019-07-11 stsp if (err)
7180 818c7501 2019-07-11 stsp goto done;
7181 818c7501 2019-07-11 stsp
7182 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7183 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7184 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7185 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7186 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7187 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7188 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7189 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7190 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7191 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7192 55bd499d 2019-07-12 stsp if (err)
7193 1f1abb7e 2019-08-08 stsp goto sync;
7194 55bd499d 2019-07-12 stsp
7195 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7196 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7197 ca355955 2019-07-12 stsp sync:
7198 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7199 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7200 ca355955 2019-07-12 stsp err = sync_err;
7201 818c7501 2019-07-11 stsp done:
7202 818c7501 2019-07-11 stsp got_ref_close(resolved);
7203 a3a2faf2 2019-07-12 stsp free(tree_id);
7204 818c7501 2019-07-11 stsp free(commit_id);
7205 945f9229 2022-04-16 thomas if (commit)
7206 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7207 0ebf8283 2019-07-24 stsp if (fileindex)
7208 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7209 0ebf8283 2019-07-24 stsp free(fileindex_path);
7210 0ebf8283 2019-07-24 stsp
7211 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7212 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7213 0ebf8283 2019-07-24 stsp err = unlockerr;
7214 0ebf8283 2019-07-24 stsp return err;
7215 0ebf8283 2019-07-24 stsp }
7216 0ebf8283 2019-07-24 stsp
7217 0ebf8283 2019-07-24 stsp const struct got_error *
7218 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7219 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7220 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7221 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7222 0ebf8283 2019-07-24 stsp {
7223 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7224 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7225 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7226 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7227 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7228 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7229 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7230 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7231 0ebf8283 2019-07-24 stsp
7232 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7233 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7234 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7235 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7236 0ebf8283 2019-07-24 stsp
7237 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7238 0ebf8283 2019-07-24 stsp if (err)
7239 0ebf8283 2019-07-24 stsp return err;
7240 0ebf8283 2019-07-24 stsp
7241 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7242 0ebf8283 2019-07-24 stsp if (err)
7243 0ebf8283 2019-07-24 stsp goto done;
7244 0ebf8283 2019-07-24 stsp
7245 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7246 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7247 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7248 0ebf8283 2019-07-24 stsp &ok_arg);
7249 0ebf8283 2019-07-24 stsp if (err)
7250 0ebf8283 2019-07-24 stsp goto done;
7251 0ebf8283 2019-07-24 stsp
7252 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7253 0ebf8283 2019-07-24 stsp if (err)
7254 0ebf8283 2019-07-24 stsp goto done;
7255 0ebf8283 2019-07-24 stsp
7256 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7257 0ebf8283 2019-07-24 stsp if (err)
7258 0ebf8283 2019-07-24 stsp goto done;
7259 0ebf8283 2019-07-24 stsp
7260 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7261 0ebf8283 2019-07-24 stsp worktree);
7262 0ebf8283 2019-07-24 stsp if (err)
7263 0ebf8283 2019-07-24 stsp goto done;
7264 0ebf8283 2019-07-24 stsp
7265 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7266 0ebf8283 2019-07-24 stsp 0);
7267 0ebf8283 2019-07-24 stsp if (err)
7268 0ebf8283 2019-07-24 stsp goto done;
7269 0ebf8283 2019-07-24 stsp
7270 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7271 0ebf8283 2019-07-24 stsp if (err)
7272 0ebf8283 2019-07-24 stsp goto done;
7273 0ebf8283 2019-07-24 stsp
7274 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7275 0ebf8283 2019-07-24 stsp if (err)
7276 0ebf8283 2019-07-24 stsp goto done;
7277 0ebf8283 2019-07-24 stsp
7278 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7279 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7280 0ebf8283 2019-07-24 stsp if (err)
7281 0ebf8283 2019-07-24 stsp goto done;
7282 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7283 0ebf8283 2019-07-24 stsp if (err)
7284 0ebf8283 2019-07-24 stsp goto done;
7285 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7286 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7287 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7288 0ebf8283 2019-07-24 stsp goto done;
7289 0ebf8283 2019-07-24 stsp }
7290 0ebf8283 2019-07-24 stsp
7291 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7292 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7293 0ebf8283 2019-07-24 stsp if (err)
7294 0ebf8283 2019-07-24 stsp goto done;
7295 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7296 0ebf8283 2019-07-24 stsp if (err)
7297 0ebf8283 2019-07-24 stsp goto done;
7298 0ebf8283 2019-07-24 stsp
7299 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7300 0ebf8283 2019-07-24 stsp if (err)
7301 0ebf8283 2019-07-24 stsp goto done;
7302 0ebf8283 2019-07-24 stsp done:
7303 0ebf8283 2019-07-24 stsp free(fileindex_path);
7304 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7305 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7306 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7307 0ebf8283 2019-07-24 stsp if (wt_branch)
7308 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7309 0ebf8283 2019-07-24 stsp if (err) {
7310 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7311 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7312 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7313 0ebf8283 2019-07-24 stsp }
7314 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7315 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7316 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7317 0ebf8283 2019-07-24 stsp }
7318 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7319 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7320 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7321 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7322 3e3a69f1 2019-07-25 stsp }
7323 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7324 0ebf8283 2019-07-24 stsp }
7325 0ebf8283 2019-07-24 stsp return err;
7326 0ebf8283 2019-07-24 stsp }
7327 0ebf8283 2019-07-24 stsp
7328 0ebf8283 2019-07-24 stsp const struct got_error *
7329 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7330 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7331 0ebf8283 2019-07-24 stsp {
7332 3e3a69f1 2019-07-25 stsp if (fileindex)
7333 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7334 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7335 0ebf8283 2019-07-24 stsp }
7336 0ebf8283 2019-07-24 stsp
7337 0ebf8283 2019-07-24 stsp const struct got_error *
7338 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7339 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7340 0ebf8283 2019-07-24 stsp {
7341 0ebf8283 2019-07-24 stsp const struct got_error *err;
7342 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7343 0ebf8283 2019-07-24 stsp
7344 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7345 0ebf8283 2019-07-24 stsp if (err)
7346 0ebf8283 2019-07-24 stsp return err;
7347 0ebf8283 2019-07-24 stsp
7348 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7349 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7350 0ebf8283 2019-07-24 stsp return NULL;
7351 0ebf8283 2019-07-24 stsp }
7352 0ebf8283 2019-07-24 stsp
7353 0ebf8283 2019-07-24 stsp const struct got_error *
7354 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7355 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7356 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7357 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7358 0ebf8283 2019-07-24 stsp {
7359 0ebf8283 2019-07-24 stsp const struct got_error *err;
7360 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7361 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7362 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7363 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7364 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7365 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7366 0ebf8283 2019-07-24 stsp
7367 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7368 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7369 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7370 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7371 0ebf8283 2019-07-24 stsp
7372 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7373 3e3a69f1 2019-07-25 stsp if (err)
7374 3e3a69f1 2019-07-25 stsp return err;
7375 3e3a69f1 2019-07-25 stsp
7376 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7377 3e3a69f1 2019-07-25 stsp if (err)
7378 f032f1f7 2019-08-04 stsp goto done;
7379 f032f1f7 2019-08-04 stsp
7380 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7381 f032f1f7 2019-08-04 stsp &have_staged_files);
7382 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7383 f032f1f7 2019-08-04 stsp goto done;
7384 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7385 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7386 3e3a69f1 2019-07-25 stsp goto done;
7387 f032f1f7 2019-08-04 stsp }
7388 3e3a69f1 2019-07-25 stsp
7389 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7390 0ebf8283 2019-07-24 stsp if (err)
7391 f032f1f7 2019-08-04 stsp goto done;
7392 0ebf8283 2019-07-24 stsp
7393 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7394 0ebf8283 2019-07-24 stsp if (err)
7395 0ebf8283 2019-07-24 stsp goto done;
7396 0ebf8283 2019-07-24 stsp
7397 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7398 0ebf8283 2019-07-24 stsp if (err)
7399 0ebf8283 2019-07-24 stsp goto done;
7400 0ebf8283 2019-07-24 stsp
7401 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7402 0ebf8283 2019-07-24 stsp worktree);
7403 0ebf8283 2019-07-24 stsp if (err)
7404 0ebf8283 2019-07-24 stsp goto done;
7405 0ebf8283 2019-07-24 stsp
7406 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7407 0ebf8283 2019-07-24 stsp if (err)
7408 0ebf8283 2019-07-24 stsp goto done;
7409 0ebf8283 2019-07-24 stsp
7410 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7411 0ebf8283 2019-07-24 stsp if (err)
7412 0ebf8283 2019-07-24 stsp goto done;
7413 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7414 0ebf8283 2019-07-24 stsp if (err)
7415 0ebf8283 2019-07-24 stsp goto done;
7416 0ebf8283 2019-07-24 stsp
7417 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7418 0ebf8283 2019-07-24 stsp if (err)
7419 0ebf8283 2019-07-24 stsp goto done;
7420 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7421 0ebf8283 2019-07-24 stsp if (err)
7422 0ebf8283 2019-07-24 stsp goto done;
7423 0ebf8283 2019-07-24 stsp
7424 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7425 0ebf8283 2019-07-24 stsp if (err)
7426 0ebf8283 2019-07-24 stsp goto done;
7427 0ebf8283 2019-07-24 stsp done:
7428 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7429 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7430 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7431 0ebf8283 2019-07-24 stsp if (commit_ref)
7432 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7433 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7434 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7435 0ebf8283 2019-07-24 stsp if (err) {
7436 0ebf8283 2019-07-24 stsp free(*commit_id);
7437 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7438 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7439 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7440 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7441 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7442 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7443 0ebf8283 2019-07-24 stsp }
7444 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7445 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7446 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7447 3e3a69f1 2019-07-25 stsp }
7448 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7449 0ebf8283 2019-07-24 stsp }
7450 0ebf8283 2019-07-24 stsp return err;
7451 0ebf8283 2019-07-24 stsp }
7452 0ebf8283 2019-07-24 stsp
7453 0ebf8283 2019-07-24 stsp static const struct got_error *
7454 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7455 0ebf8283 2019-07-24 stsp {
7456 0ebf8283 2019-07-24 stsp const struct got_error *err;
7457 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7458 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7459 0ebf8283 2019-07-24 stsp
7460 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7461 0ebf8283 2019-07-24 stsp if (err)
7462 0ebf8283 2019-07-24 stsp goto done;
7463 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7464 0ebf8283 2019-07-24 stsp if (err)
7465 0ebf8283 2019-07-24 stsp goto done;
7466 0ebf8283 2019-07-24 stsp
7467 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7468 0ebf8283 2019-07-24 stsp worktree);
7469 0ebf8283 2019-07-24 stsp if (err)
7470 0ebf8283 2019-07-24 stsp goto done;
7471 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7472 0ebf8283 2019-07-24 stsp if (err)
7473 0ebf8283 2019-07-24 stsp goto done;
7474 0ebf8283 2019-07-24 stsp
7475 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7476 0ebf8283 2019-07-24 stsp if (err)
7477 0ebf8283 2019-07-24 stsp goto done;
7478 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7479 0ebf8283 2019-07-24 stsp if (err)
7480 0ebf8283 2019-07-24 stsp goto done;
7481 0ebf8283 2019-07-24 stsp
7482 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7483 0ebf8283 2019-07-24 stsp if (err)
7484 0ebf8283 2019-07-24 stsp goto done;
7485 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7486 0ebf8283 2019-07-24 stsp if (err)
7487 0ebf8283 2019-07-24 stsp goto done;
7488 0ebf8283 2019-07-24 stsp done:
7489 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7490 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7491 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7492 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7493 0ebf8283 2019-07-24 stsp return err;
7494 0ebf8283 2019-07-24 stsp }
7495 0ebf8283 2019-07-24 stsp
7496 0ebf8283 2019-07-24 stsp const struct got_error *
7497 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7498 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7499 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7500 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7501 0ebf8283 2019-07-24 stsp {
7502 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7503 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7504 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7505 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7506 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7507 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7508 0ebf8283 2019-07-24 stsp
7509 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7510 0ebf8283 2019-07-24 stsp if (err)
7511 0ebf8283 2019-07-24 stsp return err;
7512 0ebf8283 2019-07-24 stsp
7513 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7514 945f9229 2022-04-16 thomas worktree->base_commit_id);
7515 945f9229 2022-04-16 thomas if (err)
7516 945f9229 2022-04-16 thomas goto done;
7517 945f9229 2022-04-16 thomas
7518 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7519 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7520 0ebf8283 2019-07-24 stsp if (err)
7521 0ebf8283 2019-07-24 stsp goto done;
7522 0ebf8283 2019-07-24 stsp
7523 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7524 0ebf8283 2019-07-24 stsp if (err)
7525 0ebf8283 2019-07-24 stsp goto done;
7526 0ebf8283 2019-07-24 stsp
7527 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7528 0ebf8283 2019-07-24 stsp if (err)
7529 0ebf8283 2019-07-24 stsp goto done;
7530 0ebf8283 2019-07-24 stsp
7531 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7532 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7533 0ebf8283 2019-07-24 stsp if (err)
7534 0ebf8283 2019-07-24 stsp goto done;
7535 0ebf8283 2019-07-24 stsp
7536 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7537 0ebf8283 2019-07-24 stsp if (err)
7538 0ebf8283 2019-07-24 stsp goto done;
7539 0ebf8283 2019-07-24 stsp
7540 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7541 0ebf8283 2019-07-24 stsp if (err)
7542 0ebf8283 2019-07-24 stsp goto done;
7543 0ebf8283 2019-07-24 stsp
7544 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7545 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7546 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7547 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7548 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7549 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7550 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7551 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7552 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7553 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7554 0ebf8283 2019-07-24 stsp if (err)
7555 1f1abb7e 2019-08-08 stsp goto sync;
7556 0ebf8283 2019-07-24 stsp
7557 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7558 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7559 0ebf8283 2019-07-24 stsp sync:
7560 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7561 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7562 0ebf8283 2019-07-24 stsp err = sync_err;
7563 0ebf8283 2019-07-24 stsp done:
7564 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7565 0ebf8283 2019-07-24 stsp free(tree_id);
7566 818c7501 2019-07-11 stsp free(fileindex_path);
7567 818c7501 2019-07-11 stsp
7568 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7569 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7570 818c7501 2019-07-11 stsp err = unlockerr;
7571 818c7501 2019-07-11 stsp return err;
7572 818c7501 2019-07-11 stsp }
7573 0ebf8283 2019-07-24 stsp
7574 0ebf8283 2019-07-24 stsp const struct got_error *
7575 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7576 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7577 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7578 0ebf8283 2019-07-24 stsp {
7579 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7580 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7581 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7582 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7583 0ebf8283 2019-07-24 stsp
7584 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7585 0ebf8283 2019-07-24 stsp if (err)
7586 0ebf8283 2019-07-24 stsp return err;
7587 0ebf8283 2019-07-24 stsp
7588 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7589 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7590 e600f124 2021-03-21 stsp if (err)
7591 e600f124 2021-03-21 stsp goto done;
7592 e600f124 2021-03-21 stsp
7593 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7594 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7595 0ebf8283 2019-07-24 stsp if (err)
7596 0ebf8283 2019-07-24 stsp goto done;
7597 0ebf8283 2019-07-24 stsp
7598 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7599 0ebf8283 2019-07-24 stsp if (err)
7600 0ebf8283 2019-07-24 stsp goto done;
7601 0ebf8283 2019-07-24 stsp
7602 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7603 0ebf8283 2019-07-24 stsp if (err)
7604 0ebf8283 2019-07-24 stsp goto done;
7605 0ebf8283 2019-07-24 stsp
7606 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7607 0ebf8283 2019-07-24 stsp if (err)
7608 0ebf8283 2019-07-24 stsp goto done;
7609 0ebf8283 2019-07-24 stsp
7610 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7611 a615e0e7 2020-12-16 stsp if (err)
7612 a615e0e7 2020-12-16 stsp goto done;
7613 a615e0e7 2020-12-16 stsp
7614 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7615 a615e0e7 2020-12-16 stsp if (err)
7616 a615e0e7 2020-12-16 stsp goto done;
7617 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7618 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7619 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7620 a615e0e7 2020-12-16 stsp err = sync_err;
7621 0ebf8283 2019-07-24 stsp done:
7622 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7623 a615e0e7 2020-12-16 stsp free(fileindex_path);
7624 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7625 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7626 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7627 0ebf8283 2019-07-24 stsp err = unlockerr;
7628 0ebf8283 2019-07-24 stsp return err;
7629 0ebf8283 2019-07-24 stsp }
7630 0ebf8283 2019-07-24 stsp
7631 0ebf8283 2019-07-24 stsp const struct got_error *
7632 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7633 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7634 0ebf8283 2019-07-24 stsp {
7635 0ebf8283 2019-07-24 stsp const struct got_error *err;
7636 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7637 0ebf8283 2019-07-24 stsp
7638 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7639 0ebf8283 2019-07-24 stsp if (err)
7640 0ebf8283 2019-07-24 stsp return err;
7641 0ebf8283 2019-07-24 stsp
7642 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7643 0ebf8283 2019-07-24 stsp if (err)
7644 0ebf8283 2019-07-24 stsp goto done;
7645 0ebf8283 2019-07-24 stsp
7646 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7647 0ebf8283 2019-07-24 stsp done:
7648 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7649 2822a352 2019-10-15 stsp return err;
7650 2822a352 2019-10-15 stsp }
7651 2822a352 2019-10-15 stsp
7652 2822a352 2019-10-15 stsp const struct got_error *
7653 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7654 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7655 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7656 2822a352 2019-10-15 stsp struct got_repository *repo)
7657 2822a352 2019-10-15 stsp {
7658 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7659 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7660 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7661 2822a352 2019-10-15 stsp
7662 2822a352 2019-10-15 stsp *fileindex = NULL;
7663 2822a352 2019-10-15 stsp *branch_ref = NULL;
7664 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7665 2822a352 2019-10-15 stsp
7666 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7667 2822a352 2019-10-15 stsp if (err)
7668 2822a352 2019-10-15 stsp return err;
7669 2822a352 2019-10-15 stsp
7670 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7671 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7672 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7673 2822a352 2019-10-15 stsp "update -b or different branch name required");
7674 2822a352 2019-10-15 stsp goto done;
7675 2822a352 2019-10-15 stsp }
7676 2822a352 2019-10-15 stsp
7677 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7678 2822a352 2019-10-15 stsp if (err)
7679 2822a352 2019-10-15 stsp goto done;
7680 2822a352 2019-10-15 stsp
7681 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7682 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7683 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7684 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7685 2822a352 2019-10-15 stsp &ok_arg);
7686 2822a352 2019-10-15 stsp if (err)
7687 2822a352 2019-10-15 stsp goto done;
7688 2822a352 2019-10-15 stsp
7689 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7690 2822a352 2019-10-15 stsp if (err)
7691 2822a352 2019-10-15 stsp goto done;
7692 2822a352 2019-10-15 stsp
7693 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7694 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7695 2822a352 2019-10-15 stsp done:
7696 2822a352 2019-10-15 stsp if (err) {
7697 2822a352 2019-10-15 stsp if (*branch_ref) {
7698 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7699 2822a352 2019-10-15 stsp *branch_ref = NULL;
7700 2822a352 2019-10-15 stsp }
7701 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7702 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7703 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7704 2822a352 2019-10-15 stsp }
7705 2822a352 2019-10-15 stsp if (*fileindex) {
7706 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7707 2822a352 2019-10-15 stsp *fileindex = NULL;
7708 2822a352 2019-10-15 stsp }
7709 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7710 2822a352 2019-10-15 stsp }
7711 0cb83759 2019-08-03 stsp return err;
7712 0cb83759 2019-08-03 stsp }
7713 0cb83759 2019-08-03 stsp
7714 2822a352 2019-10-15 stsp const struct got_error *
7715 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7716 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7717 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7718 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7719 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7720 2822a352 2019-10-15 stsp {
7721 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7722 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7723 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7724 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7725 2822a352 2019-10-15 stsp
7726 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7727 2822a352 2019-10-15 stsp if (err)
7728 2822a352 2019-10-15 stsp goto done;
7729 2822a352 2019-10-15 stsp
7730 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7731 2822a352 2019-10-15 stsp if (err)
7732 2822a352 2019-10-15 stsp goto done;
7733 2822a352 2019-10-15 stsp
7734 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7735 945f9229 2022-04-16 thomas if (err)
7736 945f9229 2022-04-16 thomas goto done;
7737 945f9229 2022-04-16 thomas
7738 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7739 2822a352 2019-10-15 stsp worktree->path_prefix);
7740 2822a352 2019-10-15 stsp if (err)
7741 2822a352 2019-10-15 stsp goto done;
7742 2822a352 2019-10-15 stsp
7743 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7744 2822a352 2019-10-15 stsp if (err)
7745 2822a352 2019-10-15 stsp goto done;
7746 2822a352 2019-10-15 stsp
7747 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7748 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7749 2822a352 2019-10-15 stsp if (err)
7750 2822a352 2019-10-15 stsp goto sync;
7751 2822a352 2019-10-15 stsp
7752 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7753 2822a352 2019-10-15 stsp if (err)
7754 2822a352 2019-10-15 stsp goto sync;
7755 2822a352 2019-10-15 stsp
7756 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7757 6e210706 2021-01-22 stsp if (err)
7758 6e210706 2021-01-22 stsp goto sync;
7759 6e210706 2021-01-22 stsp
7760 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7761 2822a352 2019-10-15 stsp sync:
7762 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7763 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7764 2822a352 2019-10-15 stsp err = sync_err;
7765 2822a352 2019-10-15 stsp
7766 2822a352 2019-10-15 stsp done:
7767 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7768 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7769 2822a352 2019-10-15 stsp err = unlockerr;
7770 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7771 2822a352 2019-10-15 stsp
7772 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7773 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7774 2822a352 2019-10-15 stsp err = unlockerr;
7775 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7776 2822a352 2019-10-15 stsp
7777 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7778 2822a352 2019-10-15 stsp free(fileindex_path);
7779 2822a352 2019-10-15 stsp free(tree_id);
7780 945f9229 2022-04-16 thomas if (commit)
7781 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7782 2822a352 2019-10-15 stsp
7783 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7784 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7785 2822a352 2019-10-15 stsp err = unlockerr;
7786 2822a352 2019-10-15 stsp return err;
7787 2822a352 2019-10-15 stsp }
7788 2822a352 2019-10-15 stsp
7789 2822a352 2019-10-15 stsp const struct got_error *
7790 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7791 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7792 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7793 2822a352 2019-10-15 stsp {
7794 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7795 8b692cd0 2019-10-21 stsp
7796 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7797 8b692cd0 2019-10-21 stsp
7798 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7799 8b692cd0 2019-10-21 stsp
7800 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7801 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7802 8b692cd0 2019-10-21 stsp err = unlockerr;
7803 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7804 8b692cd0 2019-10-21 stsp
7805 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7806 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7807 8b692cd0 2019-10-21 stsp err = unlockerr;
7808 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7809 10604dce 2021-09-24 thomas
7810 10604dce 2021-09-24 thomas return err;
7811 10604dce 2021-09-24 thomas }
7812 10604dce 2021-09-24 thomas
7813 10604dce 2021-09-24 thomas const struct got_error *
7814 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7815 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7816 10604dce 2021-09-24 thomas {
7817 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7818 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7819 10604dce 2021-09-24 thomas
7820 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7821 10604dce 2021-09-24 thomas if (err)
7822 10604dce 2021-09-24 thomas goto done;
7823 8b692cd0 2019-10-21 stsp
7824 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7825 10604dce 2021-09-24 thomas
7826 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7827 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7828 10604dce 2021-09-24 thomas err = sync_err;
7829 10604dce 2021-09-24 thomas done:
7830 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7831 10604dce 2021-09-24 thomas free(fileindex_path);
7832 8b692cd0 2019-10-21 stsp return err;
7833 2822a352 2019-10-15 stsp }
7834 2822a352 2019-10-15 stsp
7835 10604dce 2021-09-24 thomas static const struct got_error *
7836 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7837 10604dce 2021-09-24 thomas {
7838 10604dce 2021-09-24 thomas const struct got_error *err;
7839 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7840 10604dce 2021-09-24 thomas
7841 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7842 10604dce 2021-09-24 thomas if (err)
7843 10604dce 2021-09-24 thomas goto done;
7844 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7845 10604dce 2021-09-24 thomas if (err)
7846 10604dce 2021-09-24 thomas goto done;
7847 10604dce 2021-09-24 thomas
7848 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7849 10604dce 2021-09-24 thomas if (err)
7850 10604dce 2021-09-24 thomas goto done;
7851 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7852 10604dce 2021-09-24 thomas if (err)
7853 10604dce 2021-09-24 thomas goto done;
7854 10604dce 2021-09-24 thomas
7855 10604dce 2021-09-24 thomas done:
7856 10604dce 2021-09-24 thomas free(branch_refname);
7857 10604dce 2021-09-24 thomas free(commit_refname);
7858 10604dce 2021-09-24 thomas return err;
7859 10604dce 2021-09-24 thomas }
7860 10604dce 2021-09-24 thomas
7861 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7862 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7863 10604dce 2021-09-24 thomas const char *branch_name;
7864 10604dce 2021-09-24 thomas };
7865 10604dce 2021-09-24 thomas
7866 10604dce 2021-09-24 thomas static const struct got_error *
7867 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7868 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7869 10604dce 2021-09-24 thomas {
7870 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7871 10604dce 2021-09-24 thomas
7872 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7873 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7874 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7875 10604dce 2021-09-24 thomas
7876 10604dce 2021-09-24 thomas return NULL;
7877 10604dce 2021-09-24 thomas }
7878 10604dce 2021-09-24 thomas
7879 10604dce 2021-09-24 thomas
7880 10604dce 2021-09-24 thomas const struct got_error *
7881 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7882 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7883 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7884 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7885 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7886 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7887 10604dce 2021-09-24 thomas {
7888 10604dce 2021-09-24 thomas const struct got_error *err;
7889 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7890 10604dce 2021-09-24 thomas
7891 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7892 10604dce 2021-09-24 thomas if (err)
7893 10604dce 2021-09-24 thomas goto done;
7894 10604dce 2021-09-24 thomas
7895 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7896 10604dce 2021-09-24 thomas worktree);
7897 10604dce 2021-09-24 thomas if (err)
7898 10604dce 2021-09-24 thomas goto done;
7899 10604dce 2021-09-24 thomas
7900 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7901 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7902 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7903 10604dce 2021-09-24 thomas done:
7904 10604dce 2021-09-24 thomas free(fileindex_path);
7905 10604dce 2021-09-24 thomas return err;
7906 10604dce 2021-09-24 thomas }
7907 10604dce 2021-09-24 thomas
7908 10604dce 2021-09-24 thomas const struct got_error *
7909 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7910 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7911 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7912 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7913 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
7914 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7915 ae1e948a 2021-09-28 thomas
7916 10604dce 2021-09-24 thomas {
7917 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7918 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7919 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7920 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7921 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7922 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7923 10604dce 2021-09-24 thomas int have_staged_files = 0;
7924 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7925 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7926 10604dce 2021-09-24 thomas
7927 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7928 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7929 10604dce 2021-09-24 thomas
7930 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7931 10604dce 2021-09-24 thomas
7932 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7933 10604dce 2021-09-24 thomas if (err)
7934 10604dce 2021-09-24 thomas goto done;
7935 10604dce 2021-09-24 thomas
7936 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7937 10604dce 2021-09-24 thomas if (err)
7938 10604dce 2021-09-24 thomas goto done;
7939 10604dce 2021-09-24 thomas
7940 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7941 10604dce 2021-09-24 thomas if (err)
7942 10604dce 2021-09-24 thomas goto done;
7943 10604dce 2021-09-24 thomas
7944 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7945 10604dce 2021-09-24 thomas &have_staged_files);
7946 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7947 10604dce 2021-09-24 thomas goto done;
7948 10604dce 2021-09-24 thomas if (have_staged_files) {
7949 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7950 10604dce 2021-09-24 thomas goto done;
7951 10604dce 2021-09-24 thomas }
7952 10604dce 2021-09-24 thomas
7953 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7954 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7955 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7956 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7957 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7958 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7959 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7960 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7961 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7962 10604dce 2021-09-24 thomas if (err)
7963 10604dce 2021-09-24 thomas goto done;
7964 10604dce 2021-09-24 thomas
7965 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7966 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7967 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7968 10604dce 2021-09-24 thomas goto done;
7969 10604dce 2021-09-24 thomas }
7970 10604dce 2021-09-24 thomas
7971 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7972 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7973 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7974 10604dce 2021-09-24 thomas
7975 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7976 10604dce 2021-09-24 thomas ct_path++;
7977 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7978 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7979 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7980 10604dce 2021-09-24 thomas if (err)
7981 10604dce 2021-09-24 thomas goto done;
7982 10604dce 2021-09-24 thomas
7983 10604dce 2021-09-24 thomas }
7984 10604dce 2021-09-24 thomas
7985 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7986 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7987 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7988 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
7989 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7990 10604dce 2021-09-24 thomas if (err)
7991 10604dce 2021-09-24 thomas goto done;
7992 10604dce 2021-09-24 thomas
7993 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7994 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7995 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7996 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7997 10604dce 2021-09-24 thomas err = sync_err;
7998 10604dce 2021-09-24 thomas done:
7999 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8000 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8001 21c2d8be 2023-01-10 thomas
8002 10604dce 2021-09-24 thomas free_commitable(ct);
8003 10604dce 2021-09-24 thomas }
8004 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8005 10604dce 2021-09-24 thomas free(fileindex_path);
8006 10604dce 2021-09-24 thomas return err;
8007 10604dce 2021-09-24 thomas }
8008 10604dce 2021-09-24 thomas
8009 10604dce 2021-09-24 thomas const struct got_error *
8010 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8011 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8012 10604dce 2021-09-24 thomas {
8013 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8014 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8015 10604dce 2021-09-24 thomas
8016 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8017 10604dce 2021-09-24 thomas if (err)
8018 10604dce 2021-09-24 thomas goto done;
8019 10604dce 2021-09-24 thomas
8020 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8021 10604dce 2021-09-24 thomas if (err)
8022 10604dce 2021-09-24 thomas goto done;
8023 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8024 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8025 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8026 10604dce 2021-09-24 thomas err = sync_err;
8027 10604dce 2021-09-24 thomas done:
8028 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8029 10604dce 2021-09-24 thomas free(fileindex_path);
8030 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8031 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8032 10604dce 2021-09-24 thomas err = unlockerr;
8033 10604dce 2021-09-24 thomas return err;
8034 10604dce 2021-09-24 thomas }
8035 10604dce 2021-09-24 thomas
8036 10604dce 2021-09-24 thomas const struct got_error *
8037 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8038 10604dce 2021-09-24 thomas struct got_repository *repo)
8039 10604dce 2021-09-24 thomas {
8040 10604dce 2021-09-24 thomas const struct got_error *err;
8041 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8042 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8043 10604dce 2021-09-24 thomas
8044 10604dce 2021-09-24 thomas *in_progress = 0;
8045 10604dce 2021-09-24 thomas
8046 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8047 10604dce 2021-09-24 thomas if (err)
8048 10604dce 2021-09-24 thomas return err;
8049 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8050 dfe86d1f 2021-09-24 thomas free(branch_refname);
8051 10604dce 2021-09-24 thomas if (err) {
8052 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8053 10604dce 2021-09-24 thomas return err;
8054 10604dce 2021-09-24 thomas } else
8055 10604dce 2021-09-24 thomas *in_progress = 1;
8056 10604dce 2021-09-24 thomas
8057 10604dce 2021-09-24 thomas return NULL;
8058 10604dce 2021-09-24 thomas }
8059 10604dce 2021-09-24 thomas
8060 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8061 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8062 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
8063 10604dce 2021-09-24 thomas {
8064 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8065 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8066 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8067 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8068 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
8069 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8070 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8071 10604dce 2021-09-24 thomas
8072 10604dce 2021-09-24 thomas *fileindex = NULL;
8073 10604dce 2021-09-24 thomas
8074 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8075 10604dce 2021-09-24 thomas if (err)
8076 10604dce 2021-09-24 thomas return err;
8077 10604dce 2021-09-24 thomas
8078 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8079 10604dce 2021-09-24 thomas if (err)
8080 10604dce 2021-09-24 thomas goto done;
8081 10604dce 2021-09-24 thomas
8082 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8083 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8084 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8085 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8086 10604dce 2021-09-24 thomas &ok_arg);
8087 10604dce 2021-09-24 thomas if (err)
8088 10604dce 2021-09-24 thomas goto done;
8089 10604dce 2021-09-24 thomas
8090 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8091 10604dce 2021-09-24 thomas if (err)
8092 10604dce 2021-09-24 thomas return err;
8093 10604dce 2021-09-24 thomas
8094 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8095 10604dce 2021-09-24 thomas if (err)
8096 10604dce 2021-09-24 thomas return err;
8097 10604dce 2021-09-24 thomas
8098 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8099 10604dce 2021-09-24 thomas 0);
8100 10604dce 2021-09-24 thomas if (err)
8101 10604dce 2021-09-24 thomas goto done;
8102 10604dce 2021-09-24 thomas
8103 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8104 10604dce 2021-09-24 thomas if (err)
8105 10604dce 2021-09-24 thomas goto done;
8106 10604dce 2021-09-24 thomas
8107 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8108 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8109 10604dce 2021-09-24 thomas goto done;
8110 10604dce 2021-09-24 thomas }
8111 10604dce 2021-09-24 thomas
8112 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8113 10604dce 2021-09-24 thomas if (err)
8114 10604dce 2021-09-24 thomas goto done;
8115 10604dce 2021-09-24 thomas
8116 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8117 10604dce 2021-09-24 thomas if (err)
8118 10604dce 2021-09-24 thomas goto done;
8119 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8120 10604dce 2021-09-24 thomas if (err)
8121 10604dce 2021-09-24 thomas goto done;
8122 10604dce 2021-09-24 thomas
8123 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8124 10604dce 2021-09-24 thomas if (err)
8125 10604dce 2021-09-24 thomas goto done;
8126 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8127 10604dce 2021-09-24 thomas if (err)
8128 10604dce 2021-09-24 thomas goto done;
8129 10604dce 2021-09-24 thomas
8130 10604dce 2021-09-24 thomas done:
8131 10604dce 2021-09-24 thomas free(branch_refname);
8132 10604dce 2021-09-24 thomas free(commit_refname);
8133 10604dce 2021-09-24 thomas free(fileindex_path);
8134 10604dce 2021-09-24 thomas if (branch_ref)
8135 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8136 10604dce 2021-09-24 thomas if (commit_ref)
8137 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8138 10604dce 2021-09-24 thomas if (wt_branch)
8139 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
8140 10604dce 2021-09-24 thomas free(wt_branch_tip);
8141 10604dce 2021-09-24 thomas if (err) {
8142 10604dce 2021-09-24 thomas if (*fileindex) {
8143 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8144 10604dce 2021-09-24 thomas *fileindex = NULL;
8145 10604dce 2021-09-24 thomas }
8146 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8147 10604dce 2021-09-24 thomas }
8148 10604dce 2021-09-24 thomas return err;
8149 10604dce 2021-09-24 thomas }
8150 10604dce 2021-09-24 thomas
8151 10604dce 2021-09-24 thomas const struct got_error *
8152 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8153 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8154 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8155 10604dce 2021-09-24 thomas {
8156 10604dce 2021-09-24 thomas const struct got_error *err;
8157 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8158 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8159 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8160 10604dce 2021-09-24 thomas int have_staged_files = 0;
8161 10604dce 2021-09-24 thomas
8162 10604dce 2021-09-24 thomas *branch_name = NULL;
8163 10604dce 2021-09-24 thomas *branch_tip = NULL;
8164 10604dce 2021-09-24 thomas *fileindex = NULL;
8165 10604dce 2021-09-24 thomas
8166 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8167 10604dce 2021-09-24 thomas if (err)
8168 10604dce 2021-09-24 thomas return err;
8169 10604dce 2021-09-24 thomas
8170 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8171 10604dce 2021-09-24 thomas if (err)
8172 10604dce 2021-09-24 thomas goto done;
8173 10604dce 2021-09-24 thomas
8174 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8175 10604dce 2021-09-24 thomas &have_staged_files);
8176 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8177 10604dce 2021-09-24 thomas goto done;
8178 10604dce 2021-09-24 thomas if (have_staged_files) {
8179 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8180 10604dce 2021-09-24 thomas goto done;
8181 10604dce 2021-09-24 thomas }
8182 10604dce 2021-09-24 thomas
8183 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8184 10604dce 2021-09-24 thomas if (err)
8185 10604dce 2021-09-24 thomas goto done;
8186 10604dce 2021-09-24 thomas
8187 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8188 10604dce 2021-09-24 thomas if (err)
8189 10604dce 2021-09-24 thomas goto done;
8190 10604dce 2021-09-24 thomas
8191 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8192 10604dce 2021-09-24 thomas if (err)
8193 10604dce 2021-09-24 thomas goto done;
8194 10604dce 2021-09-24 thomas
8195 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8196 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8197 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8198 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8199 10604dce 2021-09-24 thomas goto done;
8200 10604dce 2021-09-24 thomas }
8201 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8202 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8203 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8204 10604dce 2021-09-24 thomas goto done;
8205 10604dce 2021-09-24 thomas }
8206 10604dce 2021-09-24 thomas
8207 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8208 10604dce 2021-09-24 thomas if (err)
8209 10604dce 2021-09-24 thomas goto done;
8210 10604dce 2021-09-24 thomas
8211 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8212 10604dce 2021-09-24 thomas if (err)
8213 10604dce 2021-09-24 thomas goto done;
8214 10604dce 2021-09-24 thomas done:
8215 10604dce 2021-09-24 thomas free(commit_refname);
8216 10604dce 2021-09-24 thomas free(branch_refname);
8217 10604dce 2021-09-24 thomas free(fileindex_path);
8218 10604dce 2021-09-24 thomas if (commit_ref)
8219 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8220 10604dce 2021-09-24 thomas if (branch_ref)
8221 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8222 10604dce 2021-09-24 thomas if (err) {
8223 10604dce 2021-09-24 thomas if (*branch_name) {
8224 10604dce 2021-09-24 thomas free(*branch_name);
8225 10604dce 2021-09-24 thomas *branch_name = NULL;
8226 10604dce 2021-09-24 thomas }
8227 10604dce 2021-09-24 thomas free(*branch_tip);
8228 10604dce 2021-09-24 thomas *branch_tip = NULL;
8229 10604dce 2021-09-24 thomas if (*fileindex) {
8230 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8231 10604dce 2021-09-24 thomas *fileindex = NULL;
8232 10604dce 2021-09-24 thomas }
8233 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8234 10604dce 2021-09-24 thomas }
8235 10604dce 2021-09-24 thomas return err;
8236 10604dce 2021-09-24 thomas }
8237 10604dce 2021-09-24 thomas
8238 10604dce 2021-09-24 thomas const struct got_error *
8239 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8240 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8241 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8242 10604dce 2021-09-24 thomas {
8243 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8244 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
8245 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8246 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8247 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8248 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8249 10604dce 2021-09-24 thomas
8250 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8251 945f9229 2022-04-16 thomas worktree->base_commit_id);
8252 945f9229 2022-04-16 thomas if (err)
8253 945f9229 2022-04-16 thomas goto done;
8254 945f9229 2022-04-16 thomas
8255 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8256 945f9229 2022-04-16 thomas worktree->path_prefix);
8257 10604dce 2021-09-24 thomas if (err)
8258 10604dce 2021-09-24 thomas goto done;
8259 10604dce 2021-09-24 thomas
8260 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8261 10604dce 2021-09-24 thomas if (err)
8262 10604dce 2021-09-24 thomas goto done;
8263 10604dce 2021-09-24 thomas
8264 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8265 10604dce 2021-09-24 thomas if (err)
8266 10604dce 2021-09-24 thomas goto done;
8267 10604dce 2021-09-24 thomas
8268 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8269 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8270 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8271 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8272 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8273 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8274 10604dce 2021-09-24 thomas rfa.repo = repo;
8275 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8276 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8277 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8278 10604dce 2021-09-24 thomas if (err)
8279 10604dce 2021-09-24 thomas goto sync;
8280 10604dce 2021-09-24 thomas
8281 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8282 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8283 10604dce 2021-09-24 thomas sync:
8284 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8285 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8286 10604dce 2021-09-24 thomas err = sync_err;
8287 10604dce 2021-09-24 thomas done:
8288 10604dce 2021-09-24 thomas free(tree_id);
8289 10604dce 2021-09-24 thomas free(commit_id);
8290 945f9229 2022-04-16 thomas if (commit)
8291 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8292 10604dce 2021-09-24 thomas if (fileindex)
8293 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8294 10604dce 2021-09-24 thomas free(fileindex_path);
8295 10604dce 2021-09-24 thomas
8296 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8297 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8298 10604dce 2021-09-24 thomas err = unlockerr;
8299 10604dce 2021-09-24 thomas return err;
8300 10604dce 2021-09-24 thomas }
8301 10604dce 2021-09-24 thomas
8302 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8303 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8304 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8305 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8306 2db2652d 2019-08-07 stsp struct got_repository *repo;
8307 2db2652d 2019-08-07 stsp int have_changes;
8308 2db2652d 2019-08-07 stsp };
8309 2db2652d 2019-08-07 stsp
8310 ef20f542 2022-06-26 thomas static const struct got_error *
8311 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8312 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8313 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8314 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8315 735ef5ac 2019-08-03 stsp {
8316 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8317 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8318 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8319 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8320 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8321 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8322 8b13ce36 2019-08-08 stsp
8323 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8324 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8325 8b13ce36 2019-08-08 stsp return NULL;
8326 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8327 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8328 735ef5ac 2019-08-03 stsp
8329 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8330 735ef5ac 2019-08-03 stsp if (ie == NULL)
8331 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8332 735ef5ac 2019-08-03 stsp
8333 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8334 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8335 735ef5ac 2019-08-03 stsp relpath) == -1)
8336 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8337 735ef5ac 2019-08-03 stsp
8338 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8339 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8340 43010591 2023-02-17 thomas &base_commit_id, ie);
8341 735ef5ac 2019-08-03 stsp }
8342 735ef5ac 2019-08-03 stsp
8343 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8344 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8345 3aa5969e 2019-08-06 stsp goto done;
8346 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8347 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8348 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8349 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8350 735ef5ac 2019-08-03 stsp goto done;
8351 3aa5969e 2019-08-06 stsp }
8352 735ef5ac 2019-08-03 stsp
8353 2db2652d 2019-08-07 stsp a->have_changes = 1;
8354 2db2652d 2019-08-07 stsp
8355 735ef5ac 2019-08-03 stsp p = in_repo_path;
8356 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8357 735ef5ac 2019-08-03 stsp p++;
8358 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8359 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8360 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8361 735ef5ac 2019-08-03 stsp done:
8362 735ef5ac 2019-08-03 stsp free(in_repo_path);
8363 dc424a06 2019-08-07 stsp return err;
8364 dc424a06 2019-08-07 stsp }
8365 dc424a06 2019-08-07 stsp
8366 2db2652d 2019-08-07 stsp struct stage_path_arg {
8367 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8368 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8369 2db2652d 2019-08-07 stsp struct got_repository *repo;
8370 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8371 2db2652d 2019-08-07 stsp void *status_arg;
8372 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8373 2db2652d 2019-08-07 stsp void *patch_arg;
8374 7b5dc508 2019-10-28 stsp int staged_something;
8375 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8376 2db2652d 2019-08-07 stsp };
8377 2db2652d 2019-08-07 stsp
8378 2db2652d 2019-08-07 stsp static const struct got_error *
8379 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8380 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8381 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8382 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8383 0cb83759 2019-08-03 stsp {
8384 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8385 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8386 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8387 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8388 0cb83759 2019-08-03 stsp uint32_t stage;
8389 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8390 0aeb8099 2020-07-23 stsp struct stat sb;
8391 8b13ce36 2019-08-08 stsp
8392 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8393 8b13ce36 2019-08-08 stsp return NULL;
8394 0cb83759 2019-08-03 stsp
8395 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8396 d3e7c587 2019-08-03 stsp if (ie == NULL)
8397 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8398 0cb83759 2019-08-03 stsp
8399 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8400 2db2652d 2019-08-07 stsp relpath)== -1)
8401 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8402 0cb83759 2019-08-03 stsp
8403 0cb83759 2019-08-03 stsp switch (status) {
8404 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8405 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8406 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8407 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8408 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8409 0aeb8099 2020-07-23 stsp break;
8410 0aeb8099 2020-07-23 stsp }
8411 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8412 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8413 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8414 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8415 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8416 dc424a06 2019-08-07 stsp if (err)
8417 dc424a06 2019-08-07 stsp break;
8418 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8419 dc424a06 2019-08-07 stsp break;
8420 dc424a06 2019-08-07 stsp } else {
8421 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8422 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8423 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8424 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8425 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8426 dc424a06 2019-08-07 stsp break;
8427 dc424a06 2019-08-07 stsp }
8428 dc424a06 2019-08-07 stsp }
8429 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8430 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8431 0cb83759 2019-08-03 stsp if (err)
8432 dc424a06 2019-08-07 stsp break;
8433 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8434 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8435 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8436 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8437 0cb83759 2019-08-03 stsp else
8438 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8439 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8440 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8441 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8442 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8443 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8444 35213c7c 2020-07-23 stsp ssize_t target_len;
8445 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8446 35213c7c 2020-07-23 stsp sizeof(target_path));
8447 35213c7c 2020-07-23 stsp if (target_len == -1) {
8448 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8449 35213c7c 2020-07-23 stsp ondisk_path);
8450 35213c7c 2020-07-23 stsp break;
8451 35213c7c 2020-07-23 stsp }
8452 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8453 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8454 35213c7c 2020-07-23 stsp a->worktree->root_path);
8455 35213c7c 2020-07-23 stsp if (err)
8456 35213c7c 2020-07-23 stsp break;
8457 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8458 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8459 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8460 35213c7c 2020-07-23 stsp break;
8461 35213c7c 2020-07-23 stsp }
8462 35213c7c 2020-07-23 stsp }
8463 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8464 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8465 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8466 35213c7c 2020-07-23 stsp else
8467 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8468 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8469 0aeb8099 2020-07-23 stsp } else {
8470 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8471 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8472 0aeb8099 2020-07-23 stsp }
8473 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8474 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8475 dc424a06 2019-08-07 stsp break;
8476 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8477 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8478 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8479 f289c82c 2022-06-13 thomas if (err)
8480 f289c82c 2022-06-13 thomas break;
8481 f289c82c 2022-06-13 thomas /*
8482 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8483 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8484 f289c82c 2022-06-13 thomas */
8485 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8486 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8487 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8488 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8489 f289c82c 2022-06-13 thomas }
8490 0cb83759 2019-08-03 stsp break;
8491 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8492 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8493 d3e7c587 2019-08-03 stsp break;
8494 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8495 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8496 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8497 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8498 dc424a06 2019-08-07 stsp if (err)
8499 dc424a06 2019-08-07 stsp break;
8500 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8501 88f33a19 2019-08-08 stsp break;
8502 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8503 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8504 dc424a06 2019-08-07 stsp break;
8505 88f33a19 2019-08-08 stsp }
8506 dc424a06 2019-08-07 stsp }
8507 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8508 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8509 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8510 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8511 dc424a06 2019-08-07 stsp break;
8512 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8513 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8514 12463d8b 2019-12-13 stsp de_name);
8515 0cb83759 2019-08-03 stsp break;
8516 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8517 d3e7c587 2019-08-03 stsp break;
8518 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8519 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8520 ebf48fd5 2019-08-03 stsp break;
8521 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8522 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8523 2a06fe5f 2019-08-24 stsp break;
8524 0cb83759 2019-08-03 stsp default:
8525 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8526 537ac44b 2019-08-03 stsp break;
8527 0cb83759 2019-08-03 stsp }
8528 dc424a06 2019-08-07 stsp
8529 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8530 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8531 dc424a06 2019-08-07 stsp free(path_content);
8532 2db2652d 2019-08-07 stsp free(ondisk_path);
8533 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8534 0ebf8283 2019-07-24 stsp return err;
8535 0ebf8283 2019-07-24 stsp }
8536 0cb83759 2019-08-03 stsp
8537 0cb83759 2019-08-03 stsp const struct got_error *
8538 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8539 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8540 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8541 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8542 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8543 0cb83759 2019-08-03 stsp {
8544 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8545 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8546 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8547 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8548 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8549 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8550 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8551 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8552 0cb83759 2019-08-03 stsp
8553 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8554 0cb83759 2019-08-03 stsp if (err)
8555 0cb83759 2019-08-03 stsp return err;
8556 0cb83759 2019-08-03 stsp
8557 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8558 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8559 735ef5ac 2019-08-03 stsp if (err)
8560 735ef5ac 2019-08-03 stsp goto done;
8561 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8562 735ef5ac 2019-08-03 stsp if (err)
8563 735ef5ac 2019-08-03 stsp goto done;
8564 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8565 0cb83759 2019-08-03 stsp if (err)
8566 0cb83759 2019-08-03 stsp goto done;
8567 0cb83759 2019-08-03 stsp
8568 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8569 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8570 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8571 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8572 2db2652d 2019-08-07 stsp oka.repo = repo;
8573 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8574 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8575 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8576 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8577 735ef5ac 2019-08-03 stsp if (err)
8578 735ef5ac 2019-08-03 stsp goto done;
8579 735ef5ac 2019-08-03 stsp }
8580 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8581 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8582 2db2652d 2019-08-07 stsp goto done;
8583 2db2652d 2019-08-07 stsp }
8584 735ef5ac 2019-08-03 stsp
8585 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8586 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8587 2db2652d 2019-08-07 stsp spa.repo = repo;
8588 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8589 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8590 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8591 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8592 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8593 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8594 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8595 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8596 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8597 42005733 2019-08-03 stsp if (err)
8598 2db2652d 2019-08-07 stsp goto done;
8599 7b5dc508 2019-10-28 stsp }
8600 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8601 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8602 7b5dc508 2019-10-28 stsp goto done;
8603 0cb83759 2019-08-03 stsp }
8604 0cb83759 2019-08-03 stsp
8605 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8606 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8607 0cb83759 2019-08-03 stsp err = sync_err;
8608 0cb83759 2019-08-03 stsp done:
8609 735ef5ac 2019-08-03 stsp if (head_ref)
8610 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8611 735ef5ac 2019-08-03 stsp free(head_commit_id);
8612 0cb83759 2019-08-03 stsp free(fileindex_path);
8613 0cb83759 2019-08-03 stsp if (fileindex)
8614 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8615 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8616 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8617 0cb83759 2019-08-03 stsp err = unlockerr;
8618 0cb83759 2019-08-03 stsp return err;
8619 0cb83759 2019-08-03 stsp }
8620 ad493afc 2019-08-03 stsp
8621 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8622 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8623 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8624 ad493afc 2019-08-03 stsp struct got_repository *repo;
8625 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8626 ad493afc 2019-08-03 stsp void *progress_arg;
8627 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8628 2e1f37b0 2019-08-08 stsp void *patch_arg;
8629 ad493afc 2019-08-03 stsp };
8630 2e1f37b0 2019-08-08 stsp
8631 2e1f37b0 2019-08-08 stsp static const struct got_error *
8632 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8633 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8634 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8635 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8636 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8637 2e1f37b0 2019-08-08 stsp {
8638 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8639 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8640 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8641 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8642 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8643 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8644 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8645 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8646 2e1f37b0 2019-08-08 stsp
8647 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8648 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8649 2e1f37b0 2019-08-08 stsp
8650 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8651 2e1f37b0 2019-08-08 stsp if (err)
8652 2e1f37b0 2019-08-08 stsp return err;
8653 f4ae6ddb 2022-07-01 thomas
8654 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8655 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8656 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8657 f4ae6ddb 2022-07-01 thomas goto done;
8658 f4ae6ddb 2022-07-01 thomas }
8659 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8660 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8661 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8662 f4ae6ddb 2022-07-01 thomas goto done;
8663 f4ae6ddb 2022-07-01 thomas }
8664 f4ae6ddb 2022-07-01 thomas
8665 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8666 2e1f37b0 2019-08-08 stsp if (err)
8667 2e1f37b0 2019-08-08 stsp goto done;
8668 2e1f37b0 2019-08-08 stsp
8669 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8670 2e1f37b0 2019-08-08 stsp if (err)
8671 2e1f37b0 2019-08-08 stsp goto done;
8672 2e1f37b0 2019-08-08 stsp
8673 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8674 2e1f37b0 2019-08-08 stsp if (err)
8675 2e1f37b0 2019-08-08 stsp goto done;
8676 2e1f37b0 2019-08-08 stsp
8677 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8678 f4ae6ddb 2022-07-01 thomas fd2);
8679 2e1f37b0 2019-08-08 stsp if (err)
8680 2e1f37b0 2019-08-08 stsp goto done;
8681 2e1f37b0 2019-08-08 stsp
8682 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8683 2e1f37b0 2019-08-08 stsp if (err)
8684 2e1f37b0 2019-08-08 stsp goto done;
8685 ad493afc 2019-08-03 stsp
8686 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8687 2e1f37b0 2019-08-08 stsp if (err)
8688 2e1f37b0 2019-08-08 stsp goto done;
8689 2e1f37b0 2019-08-08 stsp
8690 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8691 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8692 2e1f37b0 2019-08-08 stsp if (err)
8693 2e1f37b0 2019-08-08 stsp goto done;
8694 2e1f37b0 2019-08-08 stsp
8695 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8696 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
8697 2e1f37b0 2019-08-08 stsp if (err)
8698 2e1f37b0 2019-08-08 stsp goto done;
8699 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8700 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
8701 2e1f37b0 2019-08-08 stsp if (err)
8702 2e1f37b0 2019-08-08 stsp goto done;
8703 2e1f37b0 2019-08-08 stsp
8704 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8705 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8706 2e1f37b0 2019-08-08 stsp goto done;
8707 2e1f37b0 2019-08-08 stsp }
8708 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8709 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8710 2e1f37b0 2019-08-08 stsp goto done;
8711 2e1f37b0 2019-08-08 stsp }
8712 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8713 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8714 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8715 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8716 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8717 fe621944 2020-11-10 stsp nchanges++;
8718 fe621944 2020-11-10 stsp }
8719 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8720 2e1f37b0 2019-08-08 stsp int choice;
8721 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8722 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8723 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8724 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8725 2e1f37b0 2019-08-08 stsp if (err)
8726 2e1f37b0 2019-08-08 stsp goto done;
8727 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8728 2e1f37b0 2019-08-08 stsp have_content = 1;
8729 19e4b907 2019-08-08 stsp else
8730 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8731 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8732 2e1f37b0 2019-08-08 stsp break;
8733 2e1f37b0 2019-08-08 stsp }
8734 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8735 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8736 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8737 2e1f37b0 2019-08-08 stsp done:
8738 2e1f37b0 2019-08-08 stsp free(label1);
8739 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8740 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8741 2e1f37b0 2019-08-08 stsp if (blob)
8742 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8743 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8744 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8745 2e1f37b0 2019-08-08 stsp if (staged_blob)
8746 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8747 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8748 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8749 fe621944 2020-11-10 stsp err = free_err;
8750 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8751 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8752 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8753 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8754 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8755 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8756 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8757 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8758 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8759 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8760 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8761 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8762 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8763 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8764 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8765 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8766 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8767 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8768 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8769 2e1f37b0 2019-08-08 stsp }
8770 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8771 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8772 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8773 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8774 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8775 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8776 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8777 2e1f37b0 2019-08-08 stsp }
8778 2e1f37b0 2019-08-08 stsp free(path1);
8779 2e1f37b0 2019-08-08 stsp free(path2);
8780 fda8017d 2020-07-23 stsp return err;
8781 fda8017d 2020-07-23 stsp }
8782 fda8017d 2020-07-23 stsp
8783 fda8017d 2020-07-23 stsp static const struct got_error *
8784 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8785 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8786 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8787 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8788 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8789 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8790 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8791 fda8017d 2020-07-23 stsp {
8792 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8793 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8794 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8795 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8796 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8797 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8798 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8799 fda8017d 2020-07-23 stsp struct stat sb;
8800 fda8017d 2020-07-23 stsp
8801 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8802 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8803 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8804 fda8017d 2020-07-23 stsp if (err)
8805 fda8017d 2020-07-23 stsp return err;
8806 fda8017d 2020-07-23 stsp
8807 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8808 fda8017d 2020-07-23 stsp return NULL;
8809 fda8017d 2020-07-23 stsp
8810 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8811 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8812 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8813 fda8017d 2020-07-23 stsp if (err)
8814 fda8017d 2020-07-23 stsp goto done;
8815 fda8017d 2020-07-23 stsp }
8816 fda8017d 2020-07-23 stsp
8817 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8818 fda8017d 2020-07-23 stsp if (f == NULL) {
8819 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8820 fda8017d 2020-07-23 stsp path_unstaged_content);
8821 fda8017d 2020-07-23 stsp goto done;
8822 fda8017d 2020-07-23 stsp }
8823 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8824 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8825 fda8017d 2020-07-23 stsp goto done;
8826 fda8017d 2020-07-23 stsp }
8827 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8828 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8829 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8830 fda8017d 2020-07-23 stsp size_t r;
8831 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8832 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8833 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8834 fda8017d 2020-07-23 stsp goto done;
8835 fda8017d 2020-07-23 stsp }
8836 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8837 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8838 fda8017d 2020-07-23 stsp goto done;
8839 fda8017d 2020-07-23 stsp }
8840 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8841 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8842 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8843 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8844 fda8017d 2020-07-23 stsp progress_arg);
8845 fda8017d 2020-07-23 stsp } else {
8846 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8847 67a66647 2021-05-31 stsp
8848 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8849 67a66647 2021-05-31 stsp if (err)
8850 67a66647 2021-05-31 stsp return err;
8851 67a66647 2021-05-31 stsp
8852 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8853 67a66647 2021-05-31 stsp parent) == -1) {
8854 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8855 67a66647 2021-05-31 stsp base_path = NULL;
8856 67a66647 2021-05-31 stsp goto done;
8857 67a66647 2021-05-31 stsp }
8858 67a66647 2021-05-31 stsp
8859 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
8860 fc2a50f2 2022-11-01 thomas base_path, "");
8861 67a66647 2021-05-31 stsp if (err)
8862 67a66647 2021-05-31 stsp goto done;
8863 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8864 67a66647 2021-05-31 stsp blob_base);
8865 67a66647 2021-05-31 stsp if (err)
8866 67a66647 2021-05-31 stsp goto done;
8867 67a66647 2021-05-31 stsp
8868 b6b86fd1 2022-08-30 thomas /*
8869 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8870 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8871 eec2f5a9 2021-06-03 stsp */
8872 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8873 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8874 eec2f5a9 2021-06-03 stsp ondisk_path);
8875 eec2f5a9 2021-06-03 stsp if (err)
8876 eec2f5a9 2021-06-03 stsp goto done;
8877 eec2f5a9 2021-06-03 stsp } else {
8878 eec2f5a9 2021-06-03 stsp int fd;
8879 06340621 2021-12-31 thomas fd = open(ondisk_path,
8880 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8881 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8882 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8883 eec2f5a9 2021-06-03 stsp goto done;
8884 eec2f5a9 2021-06-03 stsp }
8885 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8886 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8887 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8888 eec2f5a9 2021-06-03 stsp close(fd);
8889 eec2f5a9 2021-06-03 stsp goto done;
8890 eec2f5a9 2021-06-03 stsp }
8891 eec2f5a9 2021-06-03 stsp }
8892 eec2f5a9 2021-06-03 stsp
8893 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8894 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8895 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8896 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8897 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8898 fda8017d 2020-07-23 stsp }
8899 fda8017d 2020-07-23 stsp if (err)
8900 fda8017d 2020-07-23 stsp goto done;
8901 fda8017d 2020-07-23 stsp
8902 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8903 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8904 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8905 2ac8aa02 2020-11-09 stsp } else {
8906 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8907 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8908 2ac8aa02 2020-11-09 stsp }
8909 fda8017d 2020-07-23 stsp done:
8910 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8911 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8912 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8913 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8914 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8915 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8916 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8917 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8918 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8919 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8920 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8921 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8922 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8923 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8924 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8925 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8926 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8927 67a66647 2021-05-31 stsp free(blob_base_path);
8928 67a66647 2021-05-31 stsp free(parent);
8929 67a66647 2021-05-31 stsp free(base_path);
8930 2e1f37b0 2019-08-08 stsp return err;
8931 2e1f37b0 2019-08-08 stsp }
8932 2e1f37b0 2019-08-08 stsp
8933 ad493afc 2019-08-03 stsp static const struct got_error *
8934 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8935 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8936 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8937 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8938 ad493afc 2019-08-03 stsp {
8939 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8940 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8941 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8942 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8943 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8944 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8945 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8946 9bc94a15 2019-08-03 stsp struct stat sb;
8947 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8948 ad493afc 2019-08-03 stsp
8949 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8950 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8951 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8952 2e1f37b0 2019-08-08 stsp return NULL;
8953 2e1f37b0 2019-08-08 stsp
8954 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8955 ad493afc 2019-08-03 stsp if (ie == NULL)
8956 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8957 9bc94a15 2019-08-03 stsp
8958 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8959 9bc94a15 2019-08-03 stsp == -1)
8960 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8961 ad493afc 2019-08-03 stsp
8962 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8963 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8964 f69721c3 2019-10-21 stsp if (err)
8965 f69721c3 2019-10-21 stsp goto done;
8966 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8967 f69721c3 2019-10-21 stsp id_str) == -1) {
8968 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8969 f4ae6ddb 2022-07-01 thomas goto done;
8970 f4ae6ddb 2022-07-01 thomas }
8971 f4ae6ddb 2022-07-01 thomas
8972 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8973 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8974 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8975 f4ae6ddb 2022-07-01 thomas goto done;
8976 f4ae6ddb 2022-07-01 thomas }
8977 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8978 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8979 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8980 f69721c3 2019-10-21 stsp goto done;
8981 f69721c3 2019-10-21 stsp }
8982 f69721c3 2019-10-21 stsp
8983 ad493afc 2019-08-03 stsp switch (staged_status) {
8984 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8985 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8986 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
8987 ad493afc 2019-08-03 stsp if (err)
8988 ad493afc 2019-08-03 stsp break;
8989 ad493afc 2019-08-03 stsp /* fall through */
8990 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8991 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8992 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8993 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8994 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8995 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8996 2e1f37b0 2019-08-08 stsp if (err)
8997 2e1f37b0 2019-08-08 stsp break;
8998 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8999 2e1f37b0 2019-08-08 stsp break;
9000 2e1f37b0 2019-08-08 stsp } else {
9001 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9002 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9003 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9004 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9005 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9006 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9007 2e1f37b0 2019-08-08 stsp }
9008 2e1f37b0 2019-08-08 stsp }
9009 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9010 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9011 ad493afc 2019-08-03 stsp if (err)
9012 ad493afc 2019-08-03 stsp break;
9013 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9014 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9015 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9016 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9017 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9018 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9019 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9020 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9021 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9022 ea7786be 2020-07-23 stsp break;
9023 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9024 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9025 36bf999c 2020-07-23 stsp char *staged_target;
9026 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9027 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9028 36bf999c 2020-07-23 stsp if (err)
9029 36bf999c 2020-07-23 stsp goto done;
9030 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9031 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9032 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9033 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9034 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9035 36bf999c 2020-07-23 stsp free(staged_target);
9036 dfe9fba0 2020-07-23 stsp } else {
9037 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9038 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9039 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9040 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9041 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9042 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9043 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9044 dfe9fba0 2020-07-23 stsp }
9045 ea7786be 2020-07-23 stsp break;
9046 ea7786be 2020-07-23 stsp default:
9047 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9048 ea7786be 2020-07-23 stsp break;
9049 ea7786be 2020-07-23 stsp }
9050 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9051 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9052 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9053 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9054 2ac8aa02 2020-11-09 stsp }
9055 ad493afc 2019-08-03 stsp break;
9056 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9057 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9058 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9059 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9060 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9061 2e1f37b0 2019-08-08 stsp if (err)
9062 2e1f37b0 2019-08-08 stsp break;
9063 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9064 2e1f37b0 2019-08-08 stsp break;
9065 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9066 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9067 2e1f37b0 2019-08-08 stsp break;
9068 2e1f37b0 2019-08-08 stsp }
9069 2e1f37b0 2019-08-08 stsp }
9070 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9071 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9072 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9073 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9074 9bc94a15 2019-08-03 stsp if (err)
9075 9bc94a15 2019-08-03 stsp break;
9076 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9077 ad493afc 2019-08-03 stsp break;
9078 ad493afc 2019-08-03 stsp }
9079 f69721c3 2019-10-21 stsp done:
9080 ad493afc 2019-08-03 stsp free(ondisk_path);
9081 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9082 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9083 ad493afc 2019-08-03 stsp if (blob_base)
9084 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9085 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9086 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9087 ad493afc 2019-08-03 stsp if (blob_staged)
9088 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9089 f69721c3 2019-10-21 stsp free(id_str);
9090 f69721c3 2019-10-21 stsp free(label_orig);
9091 ad493afc 2019-08-03 stsp return err;
9092 ad493afc 2019-08-03 stsp }
9093 ad493afc 2019-08-03 stsp
9094 ad493afc 2019-08-03 stsp const struct got_error *
9095 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9096 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9097 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9098 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9099 ad493afc 2019-08-03 stsp struct got_repository *repo)
9100 ad493afc 2019-08-03 stsp {
9101 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9102 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9103 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9104 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9105 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9106 ad493afc 2019-08-03 stsp
9107 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9108 ad493afc 2019-08-03 stsp if (err)
9109 ad493afc 2019-08-03 stsp return err;
9110 ad493afc 2019-08-03 stsp
9111 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9112 ad493afc 2019-08-03 stsp if (err)
9113 ad493afc 2019-08-03 stsp goto done;
9114 ad493afc 2019-08-03 stsp
9115 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9116 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9117 ad493afc 2019-08-03 stsp upa.repo = repo;
9118 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9119 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9120 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9121 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9122 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9123 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9124 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9125 ad493afc 2019-08-03 stsp if (err)
9126 ad493afc 2019-08-03 stsp goto done;
9127 ad493afc 2019-08-03 stsp }
9128 ad493afc 2019-08-03 stsp
9129 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9130 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9131 ad493afc 2019-08-03 stsp err = sync_err;
9132 ad493afc 2019-08-03 stsp done:
9133 ad493afc 2019-08-03 stsp free(fileindex_path);
9134 ad493afc 2019-08-03 stsp if (fileindex)
9135 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9136 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9137 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9138 ad493afc 2019-08-03 stsp err = unlockerr;
9139 ad493afc 2019-08-03 stsp return err;
9140 ad493afc 2019-08-03 stsp }
9141 b2118c49 2020-07-28 stsp
9142 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9143 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9144 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9145 b2118c49 2020-07-28 stsp void *info_arg;
9146 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9147 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9148 b2118c49 2020-07-28 stsp void *cancel_arg;
9149 b2118c49 2020-07-28 stsp };
9150 b2118c49 2020-07-28 stsp
9151 b2118c49 2020-07-28 stsp static const struct got_error *
9152 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9153 b2118c49 2020-07-28 stsp {
9154 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9155 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9156 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9157 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9158 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9159 b2118c49 2020-07-28 stsp int stage;
9160 b2118c49 2020-07-28 stsp
9161 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9162 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9163 b2118c49 2020-07-28 stsp
9164 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9165 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9166 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9167 b2118c49 2020-07-28 stsp break;
9168 b2118c49 2020-07-28 stsp }
9169 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9170 b2118c49 2020-07-28 stsp return NULL;
9171 b2118c49 2020-07-28 stsp
9172 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9173 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9174 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9175 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9176 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9177 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9178 43010591 2023-02-17 thomas &staged_blob_id, ie);
9179 b2118c49 2020-07-28 stsp }
9180 b2118c49 2020-07-28 stsp
9181 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9182 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9183 b2118c49 2020-07-28 stsp
9184 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9185 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9186 b2118c49 2020-07-28 stsp }
9187 b2118c49 2020-07-28 stsp
9188 b2118c49 2020-07-28 stsp const struct got_error *
9189 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9190 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9191 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9192 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9193 b2118c49 2020-07-28 stsp
9194 b2118c49 2020-07-28 stsp {
9195 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9196 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9197 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9198 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9199 b2118c49 2020-07-28 stsp
9200 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9201 b2118c49 2020-07-28 stsp if (err)
9202 b2118c49 2020-07-28 stsp return err;
9203 b2118c49 2020-07-28 stsp
9204 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9205 b2118c49 2020-07-28 stsp if (err)
9206 b2118c49 2020-07-28 stsp goto done;
9207 b2118c49 2020-07-28 stsp
9208 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9209 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9210 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9211 b2118c49 2020-07-28 stsp arg.paths = paths;
9212 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9213 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9214 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9215 b2118c49 2020-07-28 stsp &arg);
9216 b2118c49 2020-07-28 stsp done:
9217 b2118c49 2020-07-28 stsp free(fileindex_path);
9218 b2118c49 2020-07-28 stsp if (fileindex)
9219 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9220 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9221 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9222 b2118c49 2020-07-28 stsp err = unlockerr;
9223 b2118c49 2020-07-28 stsp return err;
9224 b2118c49 2020-07-28 stsp }
9225 814624e7 2022-03-22 thomas
9226 814624e7 2022-03-22 thomas static const struct got_error *
9227 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9228 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9229 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9230 814624e7 2022-03-22 thomas {
9231 814624e7 2022-03-22 thomas const struct got_error *err;
9232 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9233 814624e7 2022-03-22 thomas struct stat sb;
9234 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9235 814624e7 2022-03-22 thomas
9236 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9237 814624e7 2022-03-22 thomas if (err)
9238 814624e7 2022-03-22 thomas return err;
9239 814624e7 2022-03-22 thomas
9240 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9241 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9242 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9243 814624e7 2022-03-22 thomas
9244 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9245 814624e7 2022-03-22 thomas if (ie) {
9246 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9247 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9248 12de5570 2022-05-01 thomas repo);
9249 814624e7 2022-03-22 thomas if (err)
9250 814624e7 2022-03-22 thomas goto done;
9251 814624e7 2022-03-22 thomas } else {
9252 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9253 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9254 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9255 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9256 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9257 814624e7 2022-03-22 thomas ondisk_path);
9258 814624e7 2022-03-22 thomas goto done;
9259 814624e7 2022-03-22 thomas }
9260 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9261 814624e7 2022-03-22 thomas }
9262 814624e7 2022-03-22 thomas }
9263 814624e7 2022-03-22 thomas
9264 814624e7 2022-03-22 thomas done:
9265 814624e7 2022-03-22 thomas free(ondisk_path);
9266 814624e7 2022-03-22 thomas return err;
9267 814624e7 2022-03-22 thomas }
9268 814624e7 2022-03-22 thomas
9269 814624e7 2022-03-22 thomas static const struct got_error *
9270 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9271 814624e7 2022-03-22 thomas unsigned char staged_status)
9272 814624e7 2022-03-22 thomas {
9273 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9274 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9275 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9276 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9277 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9278 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9279 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9280 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9281 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9282 814624e7 2022-03-22 thomas return NULL;
9283 814624e7 2022-03-22 thomas }
9284 814624e7 2022-03-22 thomas
9285 814624e7 2022-03-22 thomas static const struct got_error *
9286 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9287 814624e7 2022-03-22 thomas {
9288 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9289 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9290 814624e7 2022-03-22 thomas return NULL;
9291 814624e7 2022-03-22 thomas }
9292 814624e7 2022-03-22 thomas
9293 814624e7 2022-03-22 thomas static const struct got_error *
9294 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9295 814624e7 2022-03-22 thomas unsigned char staged_status)
9296 814624e7 2022-03-22 thomas {
9297 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9298 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9299 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9300 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9301 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9302 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9303 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9304 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9305 814624e7 2022-03-22 thomas return NULL;
9306 814624e7 2022-03-22 thomas }
9307 814624e7 2022-03-22 thomas
9308 814624e7 2022-03-22 thomas const struct got_error *
9309 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9310 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9311 814624e7 2022-03-22 thomas {
9312 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9313 814624e7 2022-03-22 thomas }
9314 814624e7 2022-03-22 thomas
9315 814624e7 2022-03-22 thomas const struct got_error *
9316 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9317 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9318 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9319 814624e7 2022-03-22 thomas {
9320 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9321 814624e7 2022-03-22 thomas int file_renamed = 0;
9322 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9323 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9324 814624e7 2022-03-22 thomas
9325 814624e7 2022-03-22 thomas *oldpath = NULL;
9326 814624e7 2022-03-22 thomas *newpath = NULL;
9327 814624e7 2022-03-22 thomas
9328 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9329 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9330 814624e7 2022-03-22 thomas if (err)
9331 814624e7 2022-03-22 thomas goto done;
9332 814624e7 2022-03-22 thomas
9333 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9334 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9335 814624e7 2022-03-22 thomas if (err)
9336 814624e7 2022-03-22 thomas goto done;
9337 814624e7 2022-03-22 thomas
9338 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9339 814624e7 2022-03-22 thomas file_renamed = 1;
9340 814624e7 2022-03-22 thomas
9341 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9342 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9343 814624e7 2022-03-22 thomas else if (file_renamed) {
9344 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9345 814624e7 2022-03-22 thomas if (err == NULL)
9346 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9347 814624e7 2022-03-22 thomas } else if (old == NULL)
9348 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9349 814624e7 2022-03-22 thomas else
9350 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9351 814624e7 2022-03-22 thomas
9352 814624e7 2022-03-22 thomas done:
9353 814624e7 2022-03-22 thomas if (err) {
9354 814624e7 2022-03-22 thomas free(*oldpath);
9355 814624e7 2022-03-22 thomas *oldpath = NULL;
9356 814624e7 2022-03-22 thomas free(*newpath);
9357 814624e7 2022-03-22 thomas *newpath = NULL;
9358 814624e7 2022-03-22 thomas }
9359 814624e7 2022-03-22 thomas return err;
9360 814624e7 2022-03-22 thomas }
9361 814624e7 2022-03-22 thomas
9362 814624e7 2022-03-22 thomas const struct got_error *
9363 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9364 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9365 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9366 814624e7 2022-03-22 thomas {
9367 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9368 5e22b737 2022-05-31 thomas
9369 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9370 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9371 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9372 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9373 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9374 5e22b737 2022-05-31 thomas saa.repo = repo;
9375 5e22b737 2022-05-31 thomas
9376 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9377 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9378 814624e7 2022-03-22 thomas }
9379 5e22b737 2022-05-31 thomas
9380 5e22b737 2022-05-31 thomas const struct got_error *
9381 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9382 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9383 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9384 5e22b737 2022-05-31 thomas {
9385 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9386 5e22b737 2022-05-31 thomas
9387 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9388 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9389 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9390 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9391 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9392 5e22b737 2022-05-31 thomas sda.repo = repo;
9393 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9394 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9395 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9396 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9397 5e22b737 2022-05-31 thomas
9398 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9399 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9400 5e22b737 2022-05-31 thomas }
9401 5e22b737 2022-05-31 thomas
9402 5e22b737 2022-05-31 thomas const struct got_error *
9403 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9404 36832a8e 2022-05-31 thomas const char *fileindex_path)
9405 5e22b737 2022-05-31 thomas {
9406 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9407 5e22b737 2022-05-31 thomas
9408 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9409 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9410 5e22b737 2022-05-31 thomas
9411 5e22b737 2022-05-31 thomas return err;
9412 5e22b737 2022-05-31 thomas }