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 588a8092 2023-02-23 thomas #include <sha2.h>
31 9d31a1d8 2018-03-11 stsp #include <zlib.h>
32 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
33 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 dd038bc6 2021-09-21 thomas.ad
35 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
36 86c3caaf 2018-03-09 stsp
37 86c3caaf 2018-03-09 stsp #include "got_error.h"
38 86c3caaf 2018-03-09 stsp #include "got_repository.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 9d31a1d8 2018-03-11 stsp #include "got_object.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 e6209546 2019-08-22 stsp #include "got_cancel.h"
43 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
44 511a516b 2018-05-19 stsp #include "got_opentemp.h"
45 234035bc 2019-06-01 stsp #include "got_diff.h"
46 86c3caaf 2018-03-09 stsp
47 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
48 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
50 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
53 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
54 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
55 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
56 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
57 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
58 9d31a1d8 2018-03-11 stsp
59 9d31a1d8 2018-03-11 stsp #ifndef MIN
60 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 9d31a1d8 2018-03-11 stsp #endif
62 f69721c3 2019-10-21 stsp
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 a2c162eb 2022-10-30 thomas
66 a2c162eb 2022-10-30 thomas static mode_t apply_umask(mode_t);
67 86c3caaf 2018-03-09 stsp
68 99724ed4 2018-03-10 stsp static const struct got_error *
69 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
70 99724ed4 2018-03-10 stsp {
71 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
72 99724ed4 2018-03-10 stsp char *path;
73 99724ed4 2018-03-10 stsp
74 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
75 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
76 99724ed4 2018-03-10 stsp
77 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
78 99724ed4 2018-03-10 stsp free(path);
79 507dc3bb 2018-12-29 stsp return err;
80 507dc3bb 2018-12-29 stsp }
81 507dc3bb 2018-12-29 stsp
82 507dc3bb 2018-12-29 stsp static const struct got_error *
83 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
84 507dc3bb 2018-12-29 stsp {
85 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
86 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
87 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
88 507dc3bb 2018-12-29 stsp char *path = NULL;
89 507dc3bb 2018-12-29 stsp
90 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
91 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
92 507dc3bb 2018-12-29 stsp path = NULL;
93 507dc3bb 2018-12-29 stsp goto done;
94 507dc3bb 2018-12-29 stsp }
95 507dc3bb 2018-12-29 stsp
96 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
97 507dc3bb 2018-12-29 stsp if (err)
98 507dc3bb 2018-12-29 stsp goto done;
99 507dc3bb 2018-12-29 stsp
100 507dc3bb 2018-12-29 stsp if (content) {
101 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
102 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
103 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
104 507dc3bb 2018-12-29 stsp goto done;
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp }
107 507dc3bb 2018-12-29 stsp
108 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
109 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
110 2a57020b 2019-02-20 stsp unlink(tmppath);
111 507dc3bb 2018-12-29 stsp goto done;
112 507dc3bb 2018-12-29 stsp }
113 507dc3bb 2018-12-29 stsp
114 507dc3bb 2018-12-29 stsp done:
115 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
116 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
117 230a42bd 2019-05-11 jcs free(tmppath);
118 09fe317a 2018-03-11 stsp return err;
119 09fe317a 2018-03-11 stsp }
120 09fe317a 2018-03-11 stsp
121 024e9686 2019-05-14 stsp static const struct got_error *
122 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
123 024e9686 2019-05-14 stsp {
124 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
125 024e9686 2019-05-14 stsp char *refstr = NULL;
126 024e9686 2019-05-14 stsp
127 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
128 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
129 024e9686 2019-05-14 stsp if (refstr == NULL)
130 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
131 024e9686 2019-05-14 stsp } else {
132 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
133 024e9686 2019-05-14 stsp if (refstr == NULL)
134 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
135 024e9686 2019-05-14 stsp }
136 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
137 024e9686 2019-05-14 stsp free(refstr);
138 024e9686 2019-05-14 stsp return err;
139 024e9686 2019-05-14 stsp }
140 024e9686 2019-05-14 stsp
141 86c3caaf 2018-03-09 stsp const struct got_error *
142 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
143 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
144 86c3caaf 2018-03-09 stsp {
145 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
146 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
147 ec22038e 2019-03-10 stsp uuid_t uuid;
148 ec22038e 2019-03-10 stsp uint32_t uuid_status;
149 65596e15 2018-12-24 stsp int obj_type;
150 7ac97322 2018-03-11 stsp char *path_got = NULL;
151 1451e70d 2018-03-10 stsp char *formatstr = NULL;
152 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
153 65596e15 2018-12-24 stsp char *basestr = NULL;
154 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
155 65596e15 2018-12-24 stsp
156 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
157 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
158 0c48fee2 2019-03-11 stsp goto done;
159 0c48fee2 2019-03-11 stsp }
160 0c48fee2 2019-03-11 stsp
161 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
162 65596e15 2018-12-24 stsp if (err)
163 65596e15 2018-12-24 stsp return err;
164 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
165 65596e15 2018-12-24 stsp if (err)
166 65596e15 2018-12-24 stsp return err;
167 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
168 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
169 86c3caaf 2018-03-09 stsp
170 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
171 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
172 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
173 0bb8a95e 2018-03-12 stsp }
174 577ec78f 2018-03-11 stsp
175 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
176 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
177 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
178 86c3caaf 2018-03-09 stsp goto done;
179 86c3caaf 2018-03-09 stsp }
180 86c3caaf 2018-03-09 stsp
181 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
182 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
183 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
184 86c3caaf 2018-03-09 stsp goto done;
185 86c3caaf 2018-03-09 stsp }
186 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
188 86c3caaf 2018-03-09 stsp goto done;
189 86c3caaf 2018-03-09 stsp }
190 86c3caaf 2018-03-09 stsp
191 056e7441 2018-03-11 stsp /* Create an empty lock file. */
192 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
193 056e7441 2018-03-11 stsp if (err)
194 056e7441 2018-03-11 stsp goto done;
195 056e7441 2018-03-11 stsp
196 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
197 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
198 99724ed4 2018-03-10 stsp if (err)
199 86c3caaf 2018-03-09 stsp goto done;
200 86c3caaf 2018-03-09 stsp
201 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
202 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
203 65596e15 2018-12-24 stsp if (err)
204 65596e15 2018-12-24 stsp goto done;
205 65596e15 2018-12-24 stsp
206 65596e15 2018-12-24 stsp /* Record our base commit. */
207 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
208 65596e15 2018-12-24 stsp if (err)
209 65596e15 2018-12-24 stsp goto done;
210 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
211 99724ed4 2018-03-10 stsp if (err)
212 86c3caaf 2018-03-09 stsp goto done;
213 86c3caaf 2018-03-09 stsp
214 1451e70d 2018-03-10 stsp /* Store path to repository. */
215 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
216 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
217 99724ed4 2018-03-10 stsp if (err)
218 86c3caaf 2018-03-09 stsp goto done;
219 86c3caaf 2018-03-09 stsp
220 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
221 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
222 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
223 ec22038e 2019-03-10 stsp if (err)
224 ec22038e 2019-03-10 stsp goto done;
225 ec22038e 2019-03-10 stsp
226 ec22038e 2019-03-10 stsp /* Generate UUID. */
227 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
228 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
229 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
230 ec22038e 2019-03-10 stsp goto done;
231 ec22038e 2019-03-10 stsp }
232 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
233 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
234 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
235 ec22038e 2019-03-10 stsp goto done;
236 ec22038e 2019-03-10 stsp }
237 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
238 577ec78f 2018-03-11 stsp if (err)
239 577ec78f 2018-03-11 stsp goto done;
240 577ec78f 2018-03-11 stsp
241 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
242 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
243 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
244 1451e70d 2018-03-10 stsp goto done;
245 1451e70d 2018-03-10 stsp }
246 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
247 99724ed4 2018-03-10 stsp if (err)
248 1451e70d 2018-03-10 stsp goto done;
249 1451e70d 2018-03-10 stsp
250 86c3caaf 2018-03-09 stsp done:
251 65596e15 2018-12-24 stsp free(commit_id);
252 7ac97322 2018-03-11 stsp free(path_got);
253 1451e70d 2018-03-10 stsp free(formatstr);
254 0bb8a95e 2018-03-12 stsp free(absprefix);
255 65596e15 2018-12-24 stsp free(basestr);
256 ec22038e 2019-03-10 stsp free(uuidstr);
257 86c3caaf 2018-03-09 stsp return err;
258 86c3caaf 2018-03-09 stsp }
259 86c3caaf 2018-03-09 stsp
260 247140b2 2019-02-05 stsp const struct got_error *
261 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
262 e5dc7198 2018-12-29 stsp const char *path_prefix)
263 e5dc7198 2018-12-29 stsp {
264 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
265 e5dc7198 2018-12-29 stsp
266 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
267 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
268 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
269 e5dc7198 2018-12-29 stsp }
270 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
271 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
272 e5dc7198 2018-12-29 stsp free(absprefix);
273 e5dc7198 2018-12-29 stsp return NULL;
274 86c3caaf 2018-03-09 stsp }
275 86c3caaf 2018-03-09 stsp
276 bc70eb79 2019-05-09 stsp const char *
277 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 86c3caaf 2018-03-09 stsp {
279 36a38700 2019-05-10 stsp return worktree->head_ref_name;
280 024e9686 2019-05-14 stsp }
281 024e9686 2019-05-14 stsp
282 024e9686 2019-05-14 stsp const struct got_error *
283 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
284 024e9686 2019-05-14 stsp struct got_reference *head_ref)
285 024e9686 2019-05-14 stsp {
286 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
287 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
288 024e9686 2019-05-14 stsp
289 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
290 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
291 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
292 024e9686 2019-05-14 stsp path_got = NULL;
293 024e9686 2019-05-14 stsp goto done;
294 024e9686 2019-05-14 stsp }
295 024e9686 2019-05-14 stsp
296 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
297 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
298 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
299 024e9686 2019-05-14 stsp goto done;
300 024e9686 2019-05-14 stsp }
301 024e9686 2019-05-14 stsp
302 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
303 024e9686 2019-05-14 stsp if (err)
304 024e9686 2019-05-14 stsp goto done;
305 024e9686 2019-05-14 stsp
306 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
307 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
308 024e9686 2019-05-14 stsp done:
309 024e9686 2019-05-14 stsp free(path_got);
310 024e9686 2019-05-14 stsp if (err)
311 024e9686 2019-05-14 stsp free(head_ref_name);
312 024e9686 2019-05-14 stsp return err;
313 507dc3bb 2018-12-29 stsp }
314 507dc3bb 2018-12-29 stsp
315 b72f483a 2019-02-05 stsp struct got_object_id *
316 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 507dc3bb 2018-12-29 stsp {
318 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
319 86c3caaf 2018-03-09 stsp }
320 86c3caaf 2018-03-09 stsp
321 507dc3bb 2018-12-29 stsp const struct got_error *
322 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
323 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
324 507dc3bb 2018-12-29 stsp {
325 507dc3bb 2018-12-29 stsp const struct got_error *err;
326 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
327 507dc3bb 2018-12-29 stsp char *id_str = NULL;
328 507dc3bb 2018-12-29 stsp char *path_got = NULL;
329 507dc3bb 2018-12-29 stsp
330 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
331 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
332 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
333 507dc3bb 2018-12-29 stsp path_got = NULL;
334 507dc3bb 2018-12-29 stsp goto done;
335 507dc3bb 2018-12-29 stsp }
336 507dc3bb 2018-12-29 stsp
337 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
338 507dc3bb 2018-12-29 stsp if (err)
339 507dc3bb 2018-12-29 stsp return err;
340 507dc3bb 2018-12-29 stsp
341 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
342 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
343 507dc3bb 2018-12-29 stsp goto done;
344 507dc3bb 2018-12-29 stsp }
345 507dc3bb 2018-12-29 stsp
346 507dc3bb 2018-12-29 stsp /* Record our base commit. */
347 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
348 507dc3bb 2018-12-29 stsp if (err)
349 507dc3bb 2018-12-29 stsp goto done;
350 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
351 507dc3bb 2018-12-29 stsp if (err)
352 507dc3bb 2018-12-29 stsp goto done;
353 507dc3bb 2018-12-29 stsp
354 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
355 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
356 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
357 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
358 507dc3bb 2018-12-29 stsp goto done;
359 507dc3bb 2018-12-29 stsp }
360 507dc3bb 2018-12-29 stsp done:
361 507dc3bb 2018-12-29 stsp if (obj)
362 507dc3bb 2018-12-29 stsp got_object_close(obj);
363 507dc3bb 2018-12-29 stsp free(id_str);
364 507dc3bb 2018-12-29 stsp free(path_got);
365 507dc3bb 2018-12-29 stsp return err;
366 50b0790e 2020-09-11 stsp }
367 50b0790e 2020-09-11 stsp
368 50b0790e 2020-09-11 stsp const struct got_gotconfig *
369 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
370 50b0790e 2020-09-11 stsp {
371 50b0790e 2020-09-11 stsp return worktree->gotconfig;
372 507dc3bb 2018-12-29 stsp }
373 507dc3bb 2018-12-29 stsp
374 9d31a1d8 2018-03-11 stsp static const struct got_error *
375 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
376 86c3caaf 2018-03-09 stsp {
377 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
378 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
379 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
380 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
381 86c3caaf 2018-03-09 stsp return NULL;
382 21908da4 2019-01-13 stsp }
383 21908da4 2019-01-13 stsp
384 21908da4 2019-01-13 stsp static const struct got_error *
385 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 4a1ddfc2 2019-01-12 stsp {
387 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
388 4a1ddfc2 2019-01-12 stsp char *abspath;
389 4a1ddfc2 2019-01-12 stsp
390 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
391 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
392 4a1ddfc2 2019-01-12 stsp
393 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
394 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
395 ddcd8544 2019-03-11 stsp struct stat sb;
396 ddcd8544 2019-03-11 stsp err = NULL;
397 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
398 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
399 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
400 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
401 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
402 ddcd8544 2019-03-11 stsp }
403 ddcd8544 2019-03-11 stsp }
404 4a1ddfc2 2019-01-12 stsp free(abspath);
405 68c76935 2019-02-19 stsp return err;
406 68c76935 2019-02-19 stsp }
407 68c76935 2019-02-19 stsp
408 68c76935 2019-02-19 stsp static const struct got_error *
409 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 68c76935 2019-02-19 stsp {
411 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
412 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
413 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
414 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
415 68c76935 2019-02-19 stsp
416 68c76935 2019-02-19 stsp *same = 1;
417 68c76935 2019-02-19 stsp
418 230a42bd 2019-05-11 jcs for (;;) {
419 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
420 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
421 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
422 68c76935 2019-02-19 stsp break;
423 68c76935 2019-02-19 stsp }
424 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
425 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
426 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
427 68c76935 2019-02-19 stsp break;
428 68c76935 2019-02-19 stsp }
429 68c76935 2019-02-19 stsp if (flen1 == 0) {
430 68c76935 2019-02-19 stsp if (flen2 != 0)
431 68c76935 2019-02-19 stsp *same = 0;
432 68c76935 2019-02-19 stsp break;
433 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
434 68c76935 2019-02-19 stsp if (flen1 != 0)
435 68c76935 2019-02-19 stsp *same = 0;
436 68c76935 2019-02-19 stsp break;
437 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
438 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
439 68c76935 2019-02-19 stsp *same = 0;
440 68c76935 2019-02-19 stsp break;
441 68c76935 2019-02-19 stsp }
442 68c76935 2019-02-19 stsp } else {
443 68c76935 2019-02-19 stsp *same = 0;
444 68c76935 2019-02-19 stsp break;
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp }
447 68c76935 2019-02-19 stsp
448 68c76935 2019-02-19 stsp return err;
449 68c76935 2019-02-19 stsp }
450 68c76935 2019-02-19 stsp
451 68c76935 2019-02-19 stsp static const struct got_error *
452 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
453 68c76935 2019-02-19 stsp {
454 68c76935 2019-02-19 stsp struct stat sb;
455 68c76935 2019-02-19 stsp size_t size1, size2;
456 68c76935 2019-02-19 stsp
457 68c76935 2019-02-19 stsp *same = 1;
458 68c76935 2019-02-19 stsp
459 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
460 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
461 68c76935 2019-02-19 stsp size1 = sb.st_size;
462 68c76935 2019-02-19 stsp
463 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
464 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
465 68c76935 2019-02-19 stsp size2 = sb.st_size;
466 68c76935 2019-02-19 stsp
467 68c76935 2019-02-19 stsp if (size1 != size2) {
468 68c76935 2019-02-19 stsp *same = 0;
469 68c76935 2019-02-19 stsp return NULL;
470 68c76935 2019-02-19 stsp }
471 68c76935 2019-02-19 stsp
472 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
473 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
474 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
475 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
476 68c76935 2019-02-19 stsp
477 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
478 24f136e0 2021-11-20 thomas }
479 24f136e0 2021-11-20 thomas
480 24f136e0 2021-11-20 thomas static const struct got_error *
481 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
482 24f136e0 2021-11-20 thomas {
483 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
484 24f136e0 2021-11-20 thomas size_t flen;
485 24f136e0 2021-11-20 thomas ssize_t outlen;
486 24f136e0 2021-11-20 thomas
487 24f136e0 2021-11-20 thomas *outsize = 0;
488 24f136e0 2021-11-20 thomas
489 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
490 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
491 24f136e0 2021-11-20 thomas
492 24f136e0 2021-11-20 thomas for (;;) {
493 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
494 24f136e0 2021-11-20 thomas if (flen == 0) {
495 24f136e0 2021-11-20 thomas if (ferror(f))
496 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
497 24f136e0 2021-11-20 thomas if (feof(f))
498 24f136e0 2021-11-20 thomas break;
499 24f136e0 2021-11-20 thomas }
500 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
501 24f136e0 2021-11-20 thomas if (outlen == -1)
502 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
503 24f136e0 2021-11-20 thomas if (outlen != flen)
504 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
505 24f136e0 2021-11-20 thomas *outsize += outlen;
506 24f136e0 2021-11-20 thomas }
507 24f136e0 2021-11-20 thomas
508 24f136e0 2021-11-20 thomas return NULL;
509 24f136e0 2021-11-20 thomas }
510 24f136e0 2021-11-20 thomas
511 24f136e0 2021-11-20 thomas static const struct got_error *
512 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
513 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
514 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
515 24f136e0 2021-11-20 thomas const char *ondisk_path)
516 24f136e0 2021-11-20 thomas {
517 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
518 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
519 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
520 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
521 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
522 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
523 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
524 24f136e0 2021-11-20 thomas
525 24f136e0 2021-11-20 thomas *overlapcnt = 0;
526 24f136e0 2021-11-20 thomas
527 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
528 24f136e0 2021-11-20 thomas if (err)
529 24f136e0 2021-11-20 thomas return err;
530 24f136e0 2021-11-20 thomas
531 24f136e0 2021-11-20 thomas if (same_content)
532 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 24f136e0 2021-11-20 thomas
534 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
535 24f136e0 2021-11-20 thomas if (err)
536 24f136e0 2021-11-20 thomas return err;
537 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
538 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
539 24f136e0 2021-11-20 thomas if (err)
540 24f136e0 2021-11-20 thomas return err;
541 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
542 24f136e0 2021-11-20 thomas
543 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
544 24f136e0 2021-11-20 thomas *overlapcnt = 1;
545 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
546 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
547 24f136e0 2021-11-20 thomas goto done;
548 24f136e0 2021-11-20 thomas }
549 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
550 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
551 24f136e0 2021-11-20 thomas goto done;
552 24f136e0 2021-11-20 thomas }
553 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
554 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
555 24f136e0 2021-11-20 thomas goto done;
556 24f136e0 2021-11-20 thomas }
557 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
558 fc2a50f2 2022-11-01 thomas base_path_orig, "");
559 24f136e0 2021-11-20 thomas if (err)
560 24f136e0 2021-11-20 thomas goto done;
561 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
562 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
563 24f136e0 2021-11-20 thomas if (err)
564 24f136e0 2021-11-20 thomas goto done;
565 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
566 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
567 24f136e0 2021-11-20 thomas if (err)
568 24f136e0 2021-11-20 thomas goto done;
569 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
570 24f136e0 2021-11-20 thomas if (err)
571 24f136e0 2021-11-20 thomas goto done;
572 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
573 24f136e0 2021-11-20 thomas if (err)
574 24f136e0 2021-11-20 thomas goto done;
575 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
576 24f136e0 2021-11-20 thomas if (err)
577 24f136e0 2021-11-20 thomas goto done;
578 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
579 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
580 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
581 24f136e0 2021-11-20 thomas goto done;
582 24f136e0 2021-11-20 thomas }
583 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
584 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
585 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
586 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
587 24f136e0 2021-11-20 thomas path_deriv) < 0) {
588 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
589 24f136e0 2021-11-20 thomas goto done;
590 24f136e0 2021-11-20 thomas }
591 24f136e0 2021-11-20 thomas if (size_orig > 0) {
592 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
593 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
594 24f136e0 2021-11-20 thomas label_orig ? " " : "",
595 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
596 24f136e0 2021-11-20 thomas path_orig) < 0) {
597 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
598 24f136e0 2021-11-20 thomas goto done;
599 24f136e0 2021-11-20 thomas }
600 24f136e0 2021-11-20 thomas }
601 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
602 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
603 24f136e0 2021-11-20 thomas path_deriv2,
604 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
605 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
606 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
607 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
608 24f136e0 2021-11-20 thomas goto done;
609 24f136e0 2021-11-20 thomas }
610 24f136e0 2021-11-20 thomas } else if (changed_deriv)
611 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
612 24f136e0 2021-11-20 thomas else if (changed_deriv2)
613 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
614 24f136e0 2021-11-20 thomas done:
615 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
616 24f136e0 2021-11-20 thomas err == NULL)
617 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
618 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
619 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
620 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
621 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
622 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
623 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
624 24f136e0 2021-11-20 thomas free(path_orig);
625 24f136e0 2021-11-20 thomas free(path_deriv);
626 24f136e0 2021-11-20 thomas free(path_deriv2);
627 24f136e0 2021-11-20 thomas free(base_path_orig);
628 24f136e0 2021-11-20 thomas free(base_path_deriv);
629 24f136e0 2021-11-20 thomas free(base_path_deriv2);
630 24f136e0 2021-11-20 thomas return err;
631 6353ad76 2019-02-08 stsp }
632 6353ad76 2019-02-08 stsp
633 6353ad76 2019-02-08 stsp /*
634 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
635 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
636 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
637 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
638 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
639 6353ad76 2019-02-08 stsp */
640 6353ad76 2019-02-08 stsp static const struct got_error *
641 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
642 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
643 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
644 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
645 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
646 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
647 6353ad76 2019-02-08 stsp {
648 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
649 6353ad76 2019-02-08 stsp int merged_fd = -1;
650 db590691 2021-06-02 stsp FILE *f_merged = NULL;
651 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
652 234035bc 2019-06-01 stsp int overlapcnt = 0;
653 3524bbf9 2020-10-20 stsp char *parent = NULL;
654 6353ad76 2019-02-08 stsp
655 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
656 234035bc 2019-06-01 stsp
657 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
658 3524bbf9 2020-10-20 stsp if (err)
659 3524bbf9 2020-10-20 stsp return err;
660 af54ae4a 2019-02-19 stsp
661 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
662 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
663 3524bbf9 2020-10-20 stsp goto done;
664 3524bbf9 2020-10-20 stsp }
665 af54ae4a 2019-02-19 stsp
666 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
667 6353ad76 2019-02-08 stsp if (err)
668 6353ad76 2019-02-08 stsp goto done;
669 3b9f0f87 2020-07-23 stsp
670 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
671 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
672 24f136e0 2021-11-20 thomas if (err) {
673 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
674 24f136e0 2021-11-20 thomas goto done;
675 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
676 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
677 24f136e0 2021-11-20 thomas ondisk_path);
678 24f136e0 2021-11-20 thomas if (err)
679 24f136e0 2021-11-20 thomas goto done;
680 24f136e0 2021-11-20 thomas }
681 6353ad76 2019-02-08 stsp
682 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
683 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
684 1ee397ad 2019-07-12 stsp if (err)
685 1ee397ad 2019-07-12 stsp goto done;
686 6353ad76 2019-02-08 stsp
687 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
688 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
689 816dc654 2019-02-16 stsp goto done;
690 68c76935 2019-02-19 stsp }
691 68c76935 2019-02-19 stsp
692 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
693 db590691 2021-06-02 stsp if (f_merged == NULL) {
694 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
695 db590691 2021-06-02 stsp goto done;
696 db590691 2021-06-02 stsp }
697 db590691 2021-06-02 stsp merged_fd = -1;
698 db590691 2021-06-02 stsp
699 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
700 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
701 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
702 db590691 2021-06-02 stsp f_merged);
703 68c76935 2019-02-19 stsp if (err)
704 68c76935 2019-02-19 stsp goto done;
705 816dc654 2019-02-16 stsp }
706 6353ad76 2019-02-08 stsp
707 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
708 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
709 70a0c8ec 2019-02-20 stsp goto done;
710 70a0c8ec 2019-02-20 stsp }
711 70a0c8ec 2019-02-20 stsp
712 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
713 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
714 230a42bd 2019-05-11 jcs ondisk_path);
715 6353ad76 2019-02-08 stsp goto done;
716 6353ad76 2019-02-08 stsp }
717 6353ad76 2019-02-08 stsp done:
718 fdcb7daf 2019-12-15 stsp if (err) {
719 fdcb7daf 2019-12-15 stsp if (merged_path)
720 fdcb7daf 2019-12-15 stsp unlink(merged_path);
721 3b9f0f87 2020-07-23 stsp }
722 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
723 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
724 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
725 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
726 6353ad76 2019-02-08 stsp free(merged_path);
727 af54ae4a 2019-02-19 stsp free(base_path);
728 3524bbf9 2020-10-20 stsp free(parent);
729 af57b12a 2020-07-23 stsp return err;
730 af57b12a 2020-07-23 stsp }
731 af57b12a 2020-07-23 stsp
732 af57b12a 2020-07-23 stsp static const struct got_error *
733 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
734 af57b12a 2020-07-23 stsp size_t target_len)
735 af57b12a 2020-07-23 stsp {
736 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
737 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
738 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
739 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
740 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
741 af57b12a 2020-07-23 stsp ondisk_path);
742 af57b12a 2020-07-23 stsp return NULL;
743 af57b12a 2020-07-23 stsp }
744 af57b12a 2020-07-23 stsp
745 af57b12a 2020-07-23 stsp /*
746 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
747 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
748 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
749 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
750 993e2a1b 2020-07-23 stsp *
751 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
752 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
753 993e2a1b 2020-07-23 stsp *
754 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
755 993e2a1b 2020-07-23 stsp * has deleted this symlink.
756 11cc08c1 2020-07-23 stsp */
757 11cc08c1 2020-07-23 stsp static const struct got_error *
758 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
759 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
760 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
761 11cc08c1 2020-07-23 stsp {
762 11cc08c1 2020-07-23 stsp const struct got_error *err;
763 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
764 11cc08c1 2020-07-23 stsp FILE *f = NULL;
765 11cc08c1 2020-07-23 stsp
766 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
767 11cc08c1 2020-07-23 stsp if (err)
768 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
769 11cc08c1 2020-07-23 stsp
770 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
771 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
772 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
773 11cc08c1 2020-07-23 stsp goto done;
774 11cc08c1 2020-07-23 stsp }
775 11cc08c1 2020-07-23 stsp
776 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
777 11cc08c1 2020-07-23 stsp if (err)
778 3818e3c4 2020-11-01 naddy goto done;
779 3818e3c4 2020-11-01 naddy
780 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
781 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
782 11cc08c1 2020-07-23 stsp goto done;
783 3818e3c4 2020-11-01 naddy }
784 11cc08c1 2020-07-23 stsp
785 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
786 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
787 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
788 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
789 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
790 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
791 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
792 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
793 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
794 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
795 11cc08c1 2020-07-23 stsp goto done;
796 11cc08c1 2020-07-23 stsp }
797 11cc08c1 2020-07-23 stsp
798 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
799 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
800 11cc08c1 2020-07-23 stsp goto done;
801 11cc08c1 2020-07-23 stsp }
802 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
803 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
804 11cc08c1 2020-07-23 stsp goto done;
805 11cc08c1 2020-07-23 stsp }
806 11cc08c1 2020-07-23 stsp done:
807 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
808 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
809 11cc08c1 2020-07-23 stsp free(path);
810 11cc08c1 2020-07-23 stsp free(id_str);
811 11cc08c1 2020-07-23 stsp free(label_deriv);
812 11cc08c1 2020-07-23 stsp return err;
813 11cc08c1 2020-07-23 stsp }
814 d219f183 2020-07-23 stsp
815 d219f183 2020-07-23 stsp /* forward declaration */
816 d219f183 2020-07-23 stsp static const struct got_error *
817 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
818 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
819 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
820 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
821 11cc08c1 2020-07-23 stsp
822 11cc08c1 2020-07-23 stsp /*
823 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
824 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
825 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
826 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
827 af57b12a 2020-07-23 stsp */
828 af57b12a 2020-07-23 stsp static const struct got_error *
829 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
830 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
831 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
832 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
833 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
834 af57b12a 2020-07-23 stsp {
835 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
836 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
837 af57b12a 2020-07-23 stsp struct stat sb;
838 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
839 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
840 11cc08c1 2020-07-23 stsp int have_local_change = 0;
841 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
842 af57b12a 2020-07-23 stsp
843 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
844 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
845 af57b12a 2020-07-23 stsp
846 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
847 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
848 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
849 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
850 af57b12a 2020-07-23 stsp ondisk_path);
851 af57b12a 2020-07-23 stsp goto done;
852 af57b12a 2020-07-23 stsp }
853 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
854 af57b12a 2020-07-23 stsp
855 526a746f 2020-07-23 stsp if (blob_orig) {
856 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
857 526a746f 2020-07-23 stsp if (err)
858 526a746f 2020-07-23 stsp goto done;
859 526a746f 2020-07-23 stsp }
860 af57b12a 2020-07-23 stsp
861 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
862 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
863 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
864 11cc08c1 2020-07-23 stsp have_local_change = 1;
865 11cc08c1 2020-07-23 stsp
866 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
867 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
868 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
869 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
870 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
871 11cc08c1 2020-07-23 stsp
872 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
873 11cc08c1 2020-07-23 stsp if (ancestor_target) {
874 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
875 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
876 11cc08c1 2020-07-23 stsp path);
877 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
878 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
879 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
880 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
881 11cc08c1 2020-07-23 stsp path);
882 11cc08c1 2020-07-23 stsp } else {
883 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
884 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
885 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
886 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
887 11cc08c1 2020-07-23 stsp if (err)
888 11cc08c1 2020-07-23 stsp goto done;
889 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
890 11cc08c1 2020-07-23 stsp path);
891 11cc08c1 2020-07-23 stsp }
892 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
893 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
894 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
895 af57b12a 2020-07-23 stsp strlen(deriv_target));
896 af57b12a 2020-07-23 stsp if (err)
897 af57b12a 2020-07-23 stsp goto done;
898 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
899 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
900 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
901 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
902 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
903 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
904 ea7786be 2020-07-23 stsp path);
905 ea7786be 2020-07-23 stsp } else {
906 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
907 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
908 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
909 ea7786be 2020-07-23 stsp if (err)
910 ea7786be 2020-07-23 stsp goto done;
911 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
912 ea7786be 2020-07-23 stsp path);
913 ea7786be 2020-07-23 stsp }
914 6353ad76 2019-02-08 stsp }
915 11cc08c1 2020-07-23 stsp
916 af57b12a 2020-07-23 stsp done:
917 af57b12a 2020-07-23 stsp free(ancestor_target);
918 eec2f5a9 2021-06-03 stsp return err;
919 eec2f5a9 2021-06-03 stsp }
920 eec2f5a9 2021-06-03 stsp
921 eec2f5a9 2021-06-03 stsp static const struct got_error *
922 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 eec2f5a9 2021-06-03 stsp {
924 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
925 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
926 eec2f5a9 2021-06-03 stsp ssize_t target_len;
927 eec2f5a9 2021-06-03 stsp size_t n;
928 eec2f5a9 2021-06-03 stsp FILE *f;
929 eec2f5a9 2021-06-03 stsp
930 eec2f5a9 2021-06-03 stsp *outfile = NULL;
931 eec2f5a9 2021-06-03 stsp
932 eec2f5a9 2021-06-03 stsp f = got_opentemp();
933 eec2f5a9 2021-06-03 stsp if (f == NULL)
934 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
935 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
936 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
937 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
938 eec2f5a9 2021-06-03 stsp goto done;
939 eec2f5a9 2021-06-03 stsp }
940 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
941 eec2f5a9 2021-06-03 stsp if (n != target_len) {
942 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
943 eec2f5a9 2021-06-03 stsp goto done;
944 eec2f5a9 2021-06-03 stsp }
945 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
946 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
947 eec2f5a9 2021-06-03 stsp goto done;
948 eec2f5a9 2021-06-03 stsp }
949 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
950 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
951 eec2f5a9 2021-06-03 stsp goto done;
952 eec2f5a9 2021-06-03 stsp }
953 eec2f5a9 2021-06-03 stsp done:
954 eec2f5a9 2021-06-03 stsp if (err)
955 eec2f5a9 2021-06-03 stsp fclose(f);
956 eec2f5a9 2021-06-03 stsp else
957 eec2f5a9 2021-06-03 stsp *outfile = f;
958 14c901f1 2019-08-08 stsp return err;
959 14c901f1 2019-08-08 stsp }
960 14c901f1 2019-08-08 stsp
961 14c901f1 2019-08-08 stsp /*
962 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
963 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
964 14c901f1 2019-08-08 stsp * acts as the second derived version.
965 14c901f1 2019-08-08 stsp */
966 14c901f1 2019-08-08 stsp static const struct got_error *
967 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
968 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
969 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
970 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
971 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
972 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
973 14c901f1 2019-08-08 stsp {
974 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
975 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
976 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
977 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
978 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
979 14c901f1 2019-08-08 stsp
980 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
981 14c901f1 2019-08-08 stsp
982 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
983 ed6b5030 2020-10-20 stsp if (err)
984 ed6b5030 2020-10-20 stsp return err;
985 14c901f1 2019-08-08 stsp
986 67a66647 2021-05-31 stsp if (blob_orig) {
987 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
988 67a66647 2021-05-31 stsp parent) == -1) {
989 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
990 67a66647 2021-05-31 stsp base_path = NULL;
991 67a66647 2021-05-31 stsp goto done;
992 67a66647 2021-05-31 stsp }
993 67a66647 2021-05-31 stsp
994 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
995 fc2a50f2 2022-11-01 thomas base_path, "");
996 67a66647 2021-05-31 stsp if (err)
997 67a66647 2021-05-31 stsp goto done;
998 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
999 67a66647 2021-05-31 stsp blob_orig);
1000 67a66647 2021-05-31 stsp if (err)
1001 67a66647 2021-05-31 stsp goto done;
1002 67a66647 2021-05-31 stsp free(base_path);
1003 db590691 2021-06-02 stsp } else {
1004 db590691 2021-06-02 stsp /*
1005 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1006 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1007 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1008 db590691 2021-06-02 stsp */
1009 db590691 2021-06-02 stsp f_orig = got_opentemp();
1010 db590691 2021-06-02 stsp if (f_orig == NULL) {
1011 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1012 db590691 2021-06-02 stsp goto done;
1013 db590691 2021-06-02 stsp }
1014 67a66647 2021-05-31 stsp }
1015 67a66647 2021-05-31 stsp
1016 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1017 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1018 14c901f1 2019-08-08 stsp base_path = NULL;
1019 14c901f1 2019-08-08 stsp goto done;
1020 14c901f1 2019-08-08 stsp }
1021 14c901f1 2019-08-08 stsp
1022 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1023 14c901f1 2019-08-08 stsp if (err)
1024 14c901f1 2019-08-08 stsp goto done;
1025 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1026 14c901f1 2019-08-08 stsp blob_deriv);
1027 14c901f1 2019-08-08 stsp if (err)
1028 14c901f1 2019-08-08 stsp goto done;
1029 14c901f1 2019-08-08 stsp
1030 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1031 14c901f1 2019-08-08 stsp if (err)
1032 14c901f1 2019-08-08 stsp goto done;
1033 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1034 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1035 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1036 14c901f1 2019-08-08 stsp goto done;
1037 eec2f5a9 2021-06-03 stsp }
1038 eec2f5a9 2021-06-03 stsp
1039 b6b86fd1 2022-08-30 thomas /*
1040 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1041 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1042 eec2f5a9 2021-06-03 stsp */
1043 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1044 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1045 eec2f5a9 2021-06-03 stsp if (err)
1046 eec2f5a9 2021-06-03 stsp goto done;
1047 eec2f5a9 2021-06-03 stsp } else {
1048 eec2f5a9 2021-06-03 stsp int fd;
1049 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1050 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1051 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1052 eec2f5a9 2021-06-03 stsp goto done;
1053 eec2f5a9 2021-06-03 stsp }
1054 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1055 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1056 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1057 eec2f5a9 2021-06-03 stsp close(fd);
1058 eec2f5a9 2021-06-03 stsp goto done;
1059 eec2f5a9 2021-06-03 stsp }
1060 14c901f1 2019-08-08 stsp }
1061 14c901f1 2019-08-08 stsp
1062 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1063 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1064 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1065 14c901f1 2019-08-08 stsp done:
1066 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1067 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1068 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1069 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1070 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1071 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1072 14c901f1 2019-08-08 stsp free(base_path);
1073 67a66647 2021-05-31 stsp if (blob_orig_path) {
1074 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1075 67a66647 2021-05-31 stsp free(blob_orig_path);
1076 67a66647 2021-05-31 stsp }
1077 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1078 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1079 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1080 14c901f1 2019-08-08 stsp }
1081 6353ad76 2019-02-08 stsp free(id_str);
1082 818c7501 2019-07-11 stsp free(label_deriv);
1083 ed6b5030 2020-10-20 stsp free(parent);
1084 4a1ddfc2 2019-01-12 stsp return err;
1085 4a1ddfc2 2019-01-12 stsp }
1086 4a1ddfc2 2019-01-12 stsp
1087 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1088 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1089 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1090 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1091 13d9040b 2019-03-27 stsp {
1092 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1093 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1094 13d9040b 2019-03-27 stsp
1095 65b05cec 2020-07-23 stsp *new_iep = NULL;
1096 65b05cec 2020-07-23 stsp
1097 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1098 054041d0 2020-06-24 stsp if (err)
1099 054041d0 2020-06-24 stsp return err;
1100 054041d0 2020-06-24 stsp
1101 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1102 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1103 054041d0 2020-06-24 stsp if (err)
1104 054041d0 2020-06-24 stsp goto done;
1105 054041d0 2020-06-24 stsp
1106 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1107 054041d0 2020-06-24 stsp done:
1108 054041d0 2020-06-24 stsp if (err)
1109 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1110 65b05cec 2020-07-23 stsp else
1111 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1112 13d9040b 2019-03-27 stsp return err;
1113 1ebedb77 2019-10-19 stsp }
1114 1ebedb77 2019-10-19 stsp
1115 1ebedb77 2019-10-19 stsp static mode_t
1116 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1117 1ebedb77 2019-10-19 stsp {
1118 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1119 1ebedb77 2019-10-19 stsp
1120 1ebedb77 2019-10-19 stsp if (executable) {
1121 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1122 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1123 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1124 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1125 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1126 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1127 1ebedb77 2019-10-19 stsp }
1128 1ebedb77 2019-10-19 stsp
1129 a2c162eb 2022-10-30 thomas return st_mode;
1130 a2c162eb 2022-10-30 thomas }
1131 a2c162eb 2022-10-30 thomas
1132 a2c162eb 2022-10-30 thomas static mode_t
1133 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
1134 a2c162eb 2022-10-30 thomas {
1135 a2c162eb 2022-10-30 thomas mode_t um;
1136 a2c162eb 2022-10-30 thomas
1137 a2c162eb 2022-10-30 thomas um = umask(000);
1138 a2c162eb 2022-10-30 thomas umask(um);
1139 a2c162eb 2022-10-30 thomas return mode & ~um;
1140 13d9040b 2019-03-27 stsp }
1141 13d9040b 2019-03-27 stsp
1142 8ba819a3 2020-07-23 stsp /* forward declaration */
1143 13d9040b 2019-03-27 stsp static const struct got_error *
1144 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1145 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1146 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1147 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1148 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1149 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1150 8ba819a3 2020-07-23 stsp
1151 5a1fbc73 2020-07-23 stsp /*
1152 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1153 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1154 5a1fbc73 2020-07-23 stsp */
1155 8ba819a3 2020-07-23 stsp static const struct got_error *
1156 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1157 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1158 5a1fbc73 2020-07-23 stsp {
1159 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1160 5a1fbc73 2020-07-23 stsp ssize_t elen;
1161 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1162 5a1fbc73 2020-07-23 stsp int fd;
1163 5a1fbc73 2020-07-23 stsp
1164 c6e8a826 2021-04-05 stsp *did_something = 0;
1165 c6e8a826 2021-04-05 stsp
1166 5a1fbc73 2020-07-23 stsp /*
1167 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1168 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1169 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1170 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1171 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1172 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1173 5a1fbc73 2020-07-23 stsp */
1174 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1175 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1176 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1177 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1178 5a1fbc73 2020-07-23 stsp
1179 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1180 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1181 5a1fbc73 2020-07-23 stsp if (elen == -1)
1182 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1183 5a1fbc73 2020-07-23 stsp
1184 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1185 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1186 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1187 5a1fbc73 2020-07-23 stsp }
1188 5a1fbc73 2020-07-23 stsp
1189 c6e8a826 2021-04-05 stsp *did_something = 1;
1190 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1191 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1192 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1193 5a1fbc73 2020-07-23 stsp return err;
1194 3c1ec782 2020-07-23 stsp }
1195 3c1ec782 2020-07-23 stsp
1196 3c1ec782 2020-07-23 stsp static const struct got_error *
1197 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1198 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1199 3c1ec782 2020-07-23 stsp {
1200 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1201 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1202 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1203 3c1ec782 2020-07-23 stsp
1204 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1205 3c1ec782 2020-07-23 stsp
1206 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1207 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1208 3c1ec782 2020-07-23 stsp return NULL;
1209 3c1ec782 2020-07-23 stsp }
1210 3c1ec782 2020-07-23 stsp
1211 3c1ec782 2020-07-23 stsp /*
1212 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1213 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1214 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1215 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1216 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1217 3c1ec782 2020-07-23 stsp */
1218 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1219 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1220 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1221 ce031e9e 2020-10-20 stsp if (err)
1222 ce031e9e 2020-10-20 stsp return err;
1223 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 ce031e9e 2020-10-20 stsp free(parent);
1225 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1226 ce031e9e 2020-10-20 stsp }
1227 ce031e9e 2020-10-20 stsp free(parent);
1228 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1229 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1230 3c1ec782 2020-07-23 stsp free(abspath);
1231 3c1ec782 2020-07-23 stsp return err;
1232 3c1ec782 2020-07-23 stsp }
1233 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1234 3c1ec782 2020-07-23 stsp free(abspath);
1235 3c1ec782 2020-07-23 stsp if (err)
1236 3c1ec782 2020-07-23 stsp return err;
1237 3c1ec782 2020-07-23 stsp } else {
1238 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1239 3c1ec782 2020-07-23 stsp if (err)
1240 3c1ec782 2020-07-23 stsp return err;
1241 3c1ec782 2020-07-23 stsp }
1242 3c1ec782 2020-07-23 stsp
1243 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1244 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1245 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1246 3c1ec782 2020-07-23 stsp return NULL;
1247 3c1ec782 2020-07-23 stsp }
1248 3c1ec782 2020-07-23 stsp
1249 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1250 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1251 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1252 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1253 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1254 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1255 3c1ec782 2020-07-23 stsp
1256 3c1ec782 2020-07-23 stsp free(path_got);
1257 3c1ec782 2020-07-23 stsp return NULL;
1258 5a1fbc73 2020-07-23 stsp }
1259 5a1fbc73 2020-07-23 stsp
1260 5a1fbc73 2020-07-23 stsp static const struct got_error *
1261 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1262 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1263 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1264 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1265 ace90326 2021-09-27 thomas struct got_repository *repo,
1266 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1267 9d31a1d8 2018-03-11 stsp {
1268 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1269 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1270 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1271 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1272 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1273 8ba819a3 2020-07-23 stsp
1274 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1275 2e1fa222 2020-07-23 stsp
1276 9e39ca77 2022-07-21 thomas /*
1277 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1278 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1279 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1280 8ba819a3 2020-07-23 stsp * in the blob object.
1281 8ba819a3 2020-07-23 stsp */
1282 8ba819a3 2020-07-23 stsp do {
1283 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1284 3521b466 2022-07-21 thomas if (err)
1285 3521b466 2022-07-21 thomas return err;
1286 3521b466 2022-07-21 thomas
1287 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1288 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1289 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1290 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1291 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1292 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1293 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1294 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1295 3b9f0f87 2020-07-23 stsp progress_arg);
1296 8ba819a3 2020-07-23 stsp }
1297 8ba819a3 2020-07-23 stsp if (len > 0) {
1298 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1299 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1300 8ba819a3 2020-07-23 stsp len - hdrlen);
1301 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1302 8ba819a3 2020-07-23 stsp hdrlen = 0;
1303 8ba819a3 2020-07-23 stsp }
1304 8ba819a3 2020-07-23 stsp } while (len != 0);
1305 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1306 8ba819a3 2020-07-23 stsp
1307 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1308 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1309 3c1ec782 2020-07-23 stsp if (err)
1310 3c1ec782 2020-07-23 stsp return err;
1311 8ba819a3 2020-07-23 stsp
1312 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1313 906c123b 2020-07-23 stsp /* install as a regular file */
1314 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1315 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1316 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1317 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1318 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1319 9e39ca77 2022-07-21 thomas return err;
1320 906c123b 2020-07-23 stsp }
1321 906c123b 2020-07-23 stsp
1322 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1323 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1324 c6e8a826 2021-04-05 stsp int symlink_replaced;
1325 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1326 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1327 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1328 9e39ca77 2022-07-21 thomas return err;
1329 c90c8ce3 2020-07-23 stsp }
1330 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1331 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1332 5a1fbc73 2020-07-23 stsp if (err)
1333 9e39ca77 2022-07-21 thomas return err;
1334 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1335 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1336 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1337 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1338 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1339 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1340 c6e8a826 2021-04-05 stsp } else {
1341 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1342 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1343 c6e8a826 2021-04-05 stsp }
1344 f35fa46a 2020-07-23 stsp }
1345 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1346 f35fa46a 2020-07-23 stsp }
1347 f35fa46a 2020-07-23 stsp
1348 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1349 f4994adc 2020-10-20 stsp char *parent;
1350 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1351 f4994adc 2020-10-20 stsp if (err)
1352 9e39ca77 2022-07-21 thomas return err;
1353 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1354 f4994adc 2020-10-20 stsp free(parent);
1355 f35fa46a 2020-07-23 stsp if (err)
1356 9e39ca77 2022-07-21 thomas return err;
1357 f35fa46a 2020-07-23 stsp /*
1358 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1359 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1360 f35fa46a 2020-07-23 stsp */
1361 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1362 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1363 9e39ca77 2022-07-21 thomas return err;
1364 f35fa46a 2020-07-23 stsp }
1365 f35fa46a 2020-07-23 stsp }
1366 f35fa46a 2020-07-23 stsp
1367 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1368 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1369 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1370 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1371 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1372 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1373 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1375 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1376 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1377 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1378 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1379 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1380 8ba819a3 2020-07-23 stsp } else {
1381 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1382 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1383 8ba819a3 2020-07-23 stsp }
1384 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1385 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1386 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1387 8ba819a3 2020-07-23 stsp return err;
1388 8ba819a3 2020-07-23 stsp }
1389 8ba819a3 2020-07-23 stsp
1390 8ba819a3 2020-07-23 stsp static const struct got_error *
1391 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1392 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1393 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1394 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1395 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1396 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1397 8ba819a3 2020-07-23 stsp {
1398 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1399 507dc3bb 2018-12-29 stsp int fd = -1;
1400 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1401 507dc3bb 2018-12-29 stsp int update = 0;
1402 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1403 a2c162eb 2022-10-30 thomas mode_t mode;
1404 8ba819a3 2020-07-23 stsp
1405 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1406 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1407 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1408 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1409 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1410 f5375317 2020-10-20 stsp char *parent;
1411 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1412 f5375317 2020-10-20 stsp if (err)
1413 f5375317 2020-10-20 stsp return err;
1414 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1415 f5375317 2020-10-20 stsp free(parent);
1416 21908da4 2019-01-13 stsp if (err)
1417 21908da4 2019-01-13 stsp return err;
1418 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1419 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1420 a2c162eb 2022-10-30 thomas mode);
1421 21908da4 2019-01-13 stsp if (fd == -1)
1422 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1423 230a42bd 2019-05-11 jcs ondisk_path);
1424 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1425 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1426 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1427 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1428 3b9f0f87 2020-07-23 stsp goto done;
1429 3b9f0f87 2020-07-23 stsp }
1430 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1431 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1432 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1433 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1434 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1435 507dc3bb 2018-12-29 stsp goto done;
1436 d70b8e30 2018-12-27 stsp } else {
1437 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1438 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1439 507dc3bb 2018-12-29 stsp if (err)
1440 507dc3bb 2018-12-29 stsp goto done;
1441 507dc3bb 2018-12-29 stsp update = 1;
1442 a2c162eb 2022-10-30 thomas
1443 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1444 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1445 a2c162eb 2022-10-30 thomas tmppath);
1446 a2c162eb 2022-10-30 thomas goto done;
1447 a2c162eb 2022-10-30 thomas }
1448 9d31a1d8 2018-03-11 stsp }
1449 507dc3bb 2018-12-29 stsp } else
1450 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1451 3818e3c4 2020-11-01 naddy }
1452 3818e3c4 2020-11-01 naddy
1453 bd6aa359 2020-07-23 stsp if (progress_cb) {
1454 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1455 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1456 bd6aa359 2020-07-23 stsp path);
1457 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1458 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1459 bd6aa359 2020-07-23 stsp path);
1460 bd6aa359 2020-07-23 stsp else
1461 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1462 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1463 bd6aa359 2020-07-23 stsp if (err)
1464 bd6aa359 2020-07-23 stsp goto done;
1465 bd6aa359 2020-07-23 stsp }
1466 d7b62c98 2018-12-27 stsp
1467 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1468 9d31a1d8 2018-03-11 stsp do {
1469 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1470 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1471 9d31a1d8 2018-03-11 stsp if (err)
1472 9d31a1d8 2018-03-11 stsp break;
1473 9d31a1d8 2018-03-11 stsp if (len > 0) {
1474 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1475 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1476 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1477 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1478 b87c6f83 2018-12-24 stsp goto done;
1479 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1480 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1481 b87c6f83 2018-12-24 stsp goto done;
1482 9d31a1d8 2018-03-11 stsp }
1483 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1484 9d31a1d8 2018-03-11 stsp }
1485 9d31a1d8 2018-03-11 stsp } while (len != 0);
1486 9d31a1d8 2018-03-11 stsp
1487 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1488 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1489 816dc654 2019-02-16 stsp goto done;
1490 816dc654 2019-02-16 stsp }
1491 9d31a1d8 2018-03-11 stsp
1492 507dc3bb 2018-12-29 stsp if (update) {
1493 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1494 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1495 f6d8c0ac 2020-11-09 stsp goto done;
1496 f6d8c0ac 2020-11-09 stsp }
1497 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1498 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1499 230a42bd 2019-05-11 jcs ondisk_path);
1500 68ed9ba5 2019-02-10 stsp goto done;
1501 68ed9ba5 2019-02-10 stsp }
1502 63df146d 2020-11-09 stsp free(tmppath);
1503 63df146d 2020-11-09 stsp tmppath = NULL;
1504 507dc3bb 2018-12-29 stsp }
1505 507dc3bb 2018-12-29 stsp
1506 9d31a1d8 2018-03-11 stsp done:
1507 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1508 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1509 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1510 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1511 507dc3bb 2018-12-29 stsp free(tmppath);
1512 6353ad76 2019-02-08 stsp return err;
1513 6353ad76 2019-02-08 stsp }
1514 6353ad76 2019-02-08 stsp
1515 be94c032 2023-02-20 thomas /*
1516 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1517 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1518 be94c032 2023-02-20 thomas */
1519 6353ad76 2019-02-08 stsp static const struct got_error *
1520 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1521 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1522 be94c032 2023-02-20 thomas FILE *ondisk_file)
1523 7154f6ce 2019-03-27 stsp {
1524 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1525 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1526 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1528 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1529 7154f6ce 2019-03-27 stsp };
1530 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1531 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1532 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1533 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1534 9bdd68dd 2020-01-02 naddy char *line = NULL;
1535 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1536 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1537 7154f6ce 2019-03-27 stsp
1538 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1539 8de9d8ad 2023-02-20 thomas return NULL;
1540 be94c032 2023-02-20 thomas
1541 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1542 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1543 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1544 be94c032 2023-02-20 thomas
1545 8de9d8ad 2023-02-20 thomas if (blob) {
1546 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1547 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1548 be94c032 2023-02-20 thomas if (err)
1549 be94c032 2023-02-20 thomas goto done;
1550 8de9d8ad 2023-02-20 thomas }
1551 be94c032 2023-02-20 thomas
1552 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1553 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1554 8de9d8ad 2023-02-20 thomas if (err)
1555 8de9d8ad 2023-02-20 thomas goto done;
1556 8de9d8ad 2023-02-20 thomas
1557 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1558 8de9d8ad 2023-02-20 thomas
1559 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1560 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1561 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1562 450d9f6b 2023-02-20 thomas off_t pos;
1563 8de9d8ad 2023-02-20 thomas
1564 8de9d8ad 2023-02-20 thomas /*
1565 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1566 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1567 8de9d8ad 2023-02-20 thomas */
1568 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1569 8de9d8ad 2023-02-20 thomas
1570 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1571 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1572 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1573 7154f6ce 2019-03-27 stsp }
1574 7154f6ce 2019-03-27 stsp
1575 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1576 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1577 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1578 450d9f6b 2023-02-20 thomas goto done;
1579 7154f6ce 2019-03-27 stsp }
1580 8de9d8ad 2023-02-20 thomas
1581 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1582 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1583 450d9f6b 2023-02-20 thomas
1584 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1585 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1586 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1587 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1588 8de9d8ad 2023-02-20 thomas break;
1589 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1590 8de9d8ad 2023-02-20 thomas break;
1591 8de9d8ad 2023-02-20 thomas }
1592 8de9d8ad 2023-02-20 thomas
1593 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1594 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1595 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1596 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1597 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1598 8de9d8ad 2023-02-20 thomas goto done;
1599 8de9d8ad 2023-02-20 thomas } else
1600 8de9d8ad 2023-02-20 thomas i++;
1601 8de9d8ad 2023-02-20 thomas }
1602 8de9d8ad 2023-02-20 thomas ++ln;
1603 8de9d8ad 2023-02-20 thomas }
1604 7154f6ce 2019-03-27 stsp }
1605 be94c032 2023-02-20 thomas
1606 be94c032 2023-02-20 thomas done:
1607 9bdd68dd 2020-01-02 naddy free(line);
1608 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1609 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1610 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1611 8de9d8ad 2023-02-20 thomas if (err == NULL)
1612 8de9d8ad 2023-02-20 thomas err = free_err;
1613 7154f6ce 2019-03-27 stsp
1614 7154f6ce 2019-03-27 stsp return err;
1615 e2b1e152 2019-07-13 stsp }
1616 e2b1e152 2019-07-13 stsp
1617 e2b1e152 2019-07-13 stsp static int
1618 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1619 1ebedb77 2019-10-19 stsp {
1620 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1621 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1622 1ebedb77 2019-10-19 stsp }
1623 1ebedb77 2019-10-19 stsp
1624 1ebedb77 2019-10-19 stsp static int
1625 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1626 e2b1e152 2019-07-13 stsp {
1627 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1628 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1629 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1630 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1631 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1632 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1633 c363b2c1 2019-08-03 stsp }
1634 c363b2c1 2019-08-03 stsp
1635 c363b2c1 2019-08-03 stsp static unsigned char
1636 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1637 c363b2c1 2019-08-03 stsp {
1638 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1639 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1640 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1641 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1642 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1643 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1644 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1645 c363b2c1 2019-08-03 stsp default:
1646 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1647 a919d5c4 2020-07-23 stsp }
1648 a919d5c4 2020-07-23 stsp }
1649 a919d5c4 2020-07-23 stsp
1650 a919d5c4 2020-07-23 stsp static const struct got_error *
1651 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1652 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1653 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1654 a919d5c4 2020-07-23 stsp {
1655 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1656 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1657 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1658 a919d5c4 2020-07-23 stsp ssize_t elen;
1659 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1660 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1661 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1662 a919d5c4 2020-07-23 stsp
1663 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1664 a919d5c4 2020-07-23 stsp
1665 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1666 a919d5c4 2020-07-23 stsp do {
1667 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1668 a919d5c4 2020-07-23 stsp if (err)
1669 a919d5c4 2020-07-23 stsp return err;
1670 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1671 a919d5c4 2020-07-23 stsp /*
1672 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1673 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1674 a919d5c4 2020-07-23 stsp */
1675 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1676 a919d5c4 2020-07-23 stsp }
1677 a919d5c4 2020-07-23 stsp if (len > 0) {
1678 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1679 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1680 a919d5c4 2020-07-23 stsp len - hdrlen);
1681 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1682 a919d5c4 2020-07-23 stsp hdrlen = 0;
1683 a919d5c4 2020-07-23 stsp }
1684 a919d5c4 2020-07-23 stsp } while (len != 0);
1685 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1686 a919d5c4 2020-07-23 stsp
1687 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1688 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1689 a919d5c4 2020-07-23 stsp if (elen == -1)
1690 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1691 a919d5c4 2020-07-23 stsp } else {
1692 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1693 a919d5c4 2020-07-23 stsp if (elen == -1)
1694 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1695 c363b2c1 2019-08-03 stsp }
1696 a919d5c4 2020-07-23 stsp
1697 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1698 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1699 a919d5c4 2020-07-23 stsp
1700 a919d5c4 2020-07-23 stsp return NULL;
1701 7154f6ce 2019-03-27 stsp }
1702 7154f6ce 2019-03-27 stsp
1703 7154f6ce 2019-03-27 stsp static const struct got_error *
1704 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1705 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1706 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1707 6353ad76 2019-02-08 stsp {
1708 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1709 6353ad76 2019-02-08 stsp struct got_object_id id;
1710 6353ad76 2019-02-08 stsp size_t hdrlen;
1711 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1712 6353ad76 2019-02-08 stsp FILE *f = NULL;
1713 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1714 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1715 6353ad76 2019-02-08 stsp size_t flen, blen;
1716 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1717 6353ad76 2019-02-08 stsp
1718 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1719 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1720 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1721 6353ad76 2019-02-08 stsp
1722 7f91a133 2019-12-13 stsp /*
1723 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1724 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1725 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1726 7f91a133 2019-12-13 stsp */
1727 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1728 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1729 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1730 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1731 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1732 882ef1b9 2019-12-13 stsp else
1733 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1734 882ef1b9 2019-12-13 stsp goto done;
1735 882ef1b9 2019-12-13 stsp }
1736 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1737 3d35a492 2019-12-13 stsp goto done;
1738 3d35a492 2019-12-13 stsp }
1739 7f91a133 2019-12-13 stsp } else {
1740 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1741 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1742 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1743 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1744 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1745 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1746 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1747 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1748 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1749 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1750 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1751 3d35a492 2019-12-13 stsp else
1752 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1753 3d35a492 2019-12-13 stsp goto done;
1754 3d35a492 2019-12-13 stsp }
1755 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1756 1338848f 2019-12-13 stsp goto done;
1757 a378724f 2019-02-10 stsp }
1758 a378724f 2019-02-10 stsp }
1759 6353ad76 2019-02-08 stsp
1760 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1761 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1762 1338848f 2019-12-13 stsp goto done;
1763 3f148bc6 2019-07-27 stsp }
1764 efdd40df 2019-07-27 stsp
1765 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1766 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1767 1338848f 2019-12-13 stsp goto done;
1768 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1769 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1770 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1771 1338848f 2019-12-13 stsp goto done;
1772 d00136be 2019-03-26 stsp }
1773 b8f41171 2019-02-10 stsp
1774 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1775 f179e66d 2020-07-23 stsp goto done;
1776 f179e66d 2020-07-23 stsp
1777 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1778 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1779 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1780 1338848f 2019-12-13 stsp goto done;
1781 f179e66d 2020-07-23 stsp }
1782 6353ad76 2019-02-08 stsp
1783 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1784 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1785 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1786 c363b2c1 2019-08-03 stsp else
1787 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1788 c363b2c1 2019-08-03 stsp
1789 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1790 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1791 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1792 f4ae6ddb 2022-07-01 thomas goto done;
1793 f4ae6ddb 2022-07-01 thomas }
1794 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1795 6353ad76 2019-02-08 stsp if (err)
1796 1338848f 2019-12-13 stsp goto done;
1797 6353ad76 2019-02-08 stsp
1798 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1799 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1800 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1801 a919d5c4 2020-07-23 stsp goto done;
1802 a919d5c4 2020-07-23 stsp }
1803 a919d5c4 2020-07-23 stsp
1804 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1805 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1806 ab0d4361 2019-12-13 stsp if (fd == -1) {
1807 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1808 ab0d4361 2019-12-13 stsp goto done;
1809 ab0d4361 2019-12-13 stsp }
1810 3d35a492 2019-12-13 stsp }
1811 3d35a492 2019-12-13 stsp
1812 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1813 6353ad76 2019-02-08 stsp if (f == NULL) {
1814 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1815 6353ad76 2019-02-08 stsp goto done;
1816 6353ad76 2019-02-08 stsp }
1817 1338848f 2019-12-13 stsp fd = -1;
1818 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1819 656b1f76 2019-05-11 jcs for (;;) {
1820 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1821 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1822 6353ad76 2019-02-08 stsp if (err)
1823 7154f6ce 2019-03-27 stsp goto done;
1824 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1825 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1826 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1827 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1828 7154f6ce 2019-03-27 stsp goto done;
1829 80c5c120 2019-02-19 stsp }
1830 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1831 6353ad76 2019-02-08 stsp if (flen != 0)
1832 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1833 6353ad76 2019-02-08 stsp break;
1834 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1835 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1836 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1837 6353ad76 2019-02-08 stsp break;
1838 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1839 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1840 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1841 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1842 6353ad76 2019-02-08 stsp break;
1843 6353ad76 2019-02-08 stsp }
1844 6353ad76 2019-02-08 stsp } else {
1845 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1846 6353ad76 2019-02-08 stsp break;
1847 6353ad76 2019-02-08 stsp }
1848 6353ad76 2019-02-08 stsp hdrlen = 0;
1849 6353ad76 2019-02-08 stsp }
1850 7154f6ce 2019-03-27 stsp
1851 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1852 7154f6ce 2019-03-27 stsp rewind(f);
1853 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1854 be94c032 2023-02-20 thomas sb, f);
1855 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1856 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1857 6353ad76 2019-02-08 stsp done:
1858 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1859 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1860 6353ad76 2019-02-08 stsp if (blob)
1861 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1862 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1863 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1864 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1865 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1866 9d31a1d8 2018-03-11 stsp return err;
1867 e2b1e152 2019-07-13 stsp }
1868 e2b1e152 2019-07-13 stsp
1869 e2b1e152 2019-07-13 stsp /*
1870 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1871 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1872 e2b1e152 2019-07-13 stsp */
1873 e2b1e152 2019-07-13 stsp static const struct got_error *
1874 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1875 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1876 e2b1e152 2019-07-13 stsp {
1877 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1878 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1879 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1880 e2b1e152 2019-07-13 stsp
1881 e2b1e152 2019-07-13 stsp return NULL;
1882 9d31a1d8 2018-03-11 stsp }
1883 9d31a1d8 2018-03-11 stsp
1884 9d31a1d8 2018-03-11 stsp static const struct got_error *
1885 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1886 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1887 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1888 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1889 0584f854 2019-04-06 stsp void *progress_arg)
1890 9d31a1d8 2018-03-11 stsp {
1891 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1892 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1893 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1894 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1895 b8f41171 2019-02-10 stsp struct stat sb;
1896 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1897 9d31a1d8 2018-03-11 stsp
1898 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1899 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1900 6353ad76 2019-02-08 stsp
1901 abb4604f 2019-07-27 stsp if (ie) {
1902 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1903 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1904 a76c42e6 2019-08-03 stsp goto done;
1905 a76c42e6 2019-08-03 stsp }
1906 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1907 7f91a133 2019-12-13 stsp repo);
1908 abb4604f 2019-07-27 stsp if (err)
1909 abb4604f 2019-07-27 stsp goto done;
1910 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1911 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1912 c90c8ce3 2020-07-23 stsp } else {
1913 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1914 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1915 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1916 e6f4ba31 2021-09-24 thomas ondisk_path);
1917 e6f4ba31 2021-09-24 thomas goto done;
1918 e6f4ba31 2021-09-24 thomas }
1919 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1920 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1921 e6f4ba31 2021-09-24 thomas } else {
1922 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1923 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1924 e6f4ba31 2021-09-24 thomas else
1925 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1926 e6f4ba31 2021-09-24 thomas }
1927 c90c8ce3 2020-07-23 stsp }
1928 6353ad76 2019-02-08 stsp
1929 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1930 2c41dce7 2021-06-27 stsp if (ie)
1931 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1932 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1933 b8f41171 2019-02-10 stsp goto done;
1934 b8f41171 2019-02-10 stsp }
1935 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1936 a769b60b 2021-06-27 stsp if (ie)
1937 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1938 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1939 5036ab18 2020-04-18 stsp path);
1940 5036ab18 2020-04-18 stsp goto done;
1941 5036ab18 2020-04-18 stsp }
1942 b8f41171 2019-02-10 stsp
1943 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1944 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1945 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1946 c6e8a826 2021-04-05 stsp /*
1947 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1948 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1949 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1950 c6e8a826 2021-04-05 stsp * updating contents of this file.
1951 c6e8a826 2021-04-05 stsp */
1952 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1953 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1954 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1955 c6e8a826 2021-04-05 stsp /* Same commit. */
1956 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1957 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1958 e2b1e152 2019-07-13 stsp if (err)
1959 e2b1e152 2019-07-13 stsp goto done;
1960 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1961 b8f41171 2019-02-10 stsp path);
1962 6353ad76 2019-02-08 stsp goto done;
1963 a378724f 2019-02-10 stsp }
1964 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1965 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1966 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1967 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1968 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1969 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1970 0f58026f 2021-04-05 stsp if (err)
1971 0f58026f 2021-04-05 stsp goto done;
1972 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1973 0f58026f 2021-04-05 stsp path);
1974 b8f41171 2019-02-10 stsp goto done;
1975 e2b1e152 2019-07-13 stsp }
1976 9d31a1d8 2018-03-11 stsp }
1977 9d31a1d8 2018-03-11 stsp
1978 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1979 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1980 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1981 f4ae6ddb 2022-07-01 thomas goto done;
1982 f4ae6ddb 2022-07-01 thomas }
1983 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1984 8da9e5f4 2019-01-12 stsp if (err)
1985 6353ad76 2019-02-08 stsp goto done;
1986 512f0d0e 2019-01-02 stsp
1987 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1988 234035bc 2019-06-01 stsp int update_timestamps;
1989 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1990 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1991 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1992 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1993 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1994 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1995 f4ae6ddb 2022-07-01 thomas goto done;
1996 f4ae6ddb 2022-07-01 thomas }
1997 234035bc 2019-06-01 stsp struct got_object_id id2;
1998 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
1999 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2000 f4ae6ddb 2022-07-01 thomas fd2);
2001 234035bc 2019-06-01 stsp if (err)
2002 f69721c3 2019-10-21 stsp goto done;
2003 f69721c3 2019-10-21 stsp }
2004 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2005 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2006 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2007 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2008 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2009 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2010 f69721c3 2019-10-21 stsp goto done;
2011 f69721c3 2019-10-21 stsp }
2012 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2013 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2014 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2015 234035bc 2019-06-01 stsp goto done;
2016 f69721c3 2019-10-21 stsp }
2017 234035bc 2019-06-01 stsp }
2018 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2019 36bf999c 2020-07-23 stsp char *link_target;
2020 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2021 36bf999c 2020-07-23 stsp if (err)
2022 36bf999c 2020-07-23 stsp goto done;
2023 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2024 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2025 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2026 36bf999c 2020-07-23 stsp free(link_target);
2027 993e2a1b 2020-07-23 stsp } else {
2028 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2029 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2030 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2031 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2032 993e2a1b 2020-07-23 stsp }
2033 f69721c3 2019-10-21 stsp free(label_orig);
2034 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2035 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2036 f4ae6ddb 2022-07-01 thomas goto done;
2037 f4ae6ddb 2022-07-01 thomas }
2038 234035bc 2019-06-01 stsp if (blob2)
2039 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2040 909d120e 2019-09-22 stsp if (err)
2041 909d120e 2019-09-22 stsp goto done;
2042 234035bc 2019-06-01 stsp /*
2043 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2044 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2045 234035bc 2019-06-01 stsp * unmodified files again.
2046 234035bc 2019-06-01 stsp */
2047 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2048 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2049 234035bc 2019-06-01 stsp update_timestamps);
2050 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2051 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2052 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2053 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2054 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2055 1ee397ad 2019-07-12 stsp if (err)
2056 1ee397ad 2019-07-12 stsp goto done;
2057 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2058 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2059 13d9040b 2019-03-27 stsp if (err)
2060 13d9040b 2019-03-27 stsp goto done;
2061 13d9040b 2019-03-27 stsp } else {
2062 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2063 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2064 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2065 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2066 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2067 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2068 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2069 2e1fa222 2020-07-23 stsp } else {
2070 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2071 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2072 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2073 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2074 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2075 2e1fa222 2020-07-23 stsp }
2076 13d9040b 2019-03-27 stsp if (err)
2077 13d9040b 2019-03-27 stsp goto done;
2078 65b05cec 2020-07-23 stsp
2079 054041d0 2020-06-24 stsp if (ie) {
2080 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2081 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2082 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2083 054041d0 2020-06-24 stsp } else {
2084 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2085 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2086 054041d0 2020-06-24 stsp &blob->id);
2087 054041d0 2020-06-24 stsp }
2088 13d9040b 2019-03-27 stsp if (err)
2089 13d9040b 2019-03-27 stsp goto done;
2090 65b05cec 2020-07-23 stsp
2091 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2092 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2093 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2094 2e1fa222 2020-07-23 stsp }
2095 13d9040b 2019-03-27 stsp }
2096 f4ae6ddb 2022-07-01 thomas
2097 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2098 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2099 f4ae6ddb 2022-07-01 thomas goto done;
2100 f4ae6ddb 2022-07-01 thomas }
2101 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2102 6353ad76 2019-02-08 stsp done:
2103 6353ad76 2019-02-08 stsp free(ondisk_path);
2104 8da9e5f4 2019-01-12 stsp return err;
2105 90285c3b 2019-01-08 stsp }
2106 90285c3b 2019-01-08 stsp
2107 90285c3b 2019-01-08 stsp static const struct got_error *
2108 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2109 90285c3b 2019-01-08 stsp {
2110 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2111 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2112 90285c3b 2019-01-08 stsp
2113 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2114 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2115 90285c3b 2019-01-08 stsp
2116 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2117 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2118 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2119 c97665ae 2019-01-13 stsp } else {
2120 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2121 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2122 1c4cdd89 2021-06-20 stsp if (err)
2123 1c4cdd89 2021-06-20 stsp goto done;
2124 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2125 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2126 fddefe3b 2020-10-20 stsp free(ondisk_path);
2127 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2128 1c4cdd89 2021-06-20 stsp parent = NULL;
2129 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2130 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2131 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2132 fddefe3b 2020-10-20 stsp ondisk_path);
2133 90285c3b 2019-01-08 stsp break;
2134 90285c3b 2019-01-08 stsp }
2135 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2136 1c4cdd89 2021-06-20 stsp if (err)
2137 1c4cdd89 2021-06-20 stsp break;
2138 1c4cdd89 2021-06-20 stsp }
2139 90285c3b 2019-01-08 stsp }
2140 1c4cdd89 2021-06-20 stsp done:
2141 90285c3b 2019-01-08 stsp free(ondisk_path);
2142 1c4cdd89 2021-06-20 stsp free(parent);
2143 90285c3b 2019-01-08 stsp return err;
2144 512f0d0e 2019-01-02 stsp }
2145 512f0d0e 2019-01-02 stsp
2146 708d8e67 2019-03-27 stsp static const struct got_error *
2147 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2149 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2150 708d8e67 2019-03-27 stsp {
2151 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2152 708d8e67 2019-03-27 stsp unsigned char status;
2153 708d8e67 2019-03-27 stsp struct stat sb;
2154 708d8e67 2019-03-27 stsp char *ondisk_path;
2155 708d8e67 2019-03-27 stsp
2156 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2157 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2158 a76c42e6 2019-08-03 stsp
2159 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2160 708d8e67 2019-03-27 stsp == -1)
2161 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2162 708d8e67 2019-03-27 stsp
2163 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2164 708d8e67 2019-03-27 stsp if (err)
2165 5a58a424 2020-06-23 stsp goto done;
2166 708d8e67 2019-03-27 stsp
2167 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2168 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2169 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2170 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2171 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2172 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2173 993e2a1b 2020-07-23 stsp goto done;
2174 993e2a1b 2020-07-23 stsp }
2175 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2176 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2177 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2178 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2179 993e2a1b 2020-07-23 stsp if (err)
2180 993e2a1b 2020-07-23 stsp goto done;
2181 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2182 993e2a1b 2020-07-23 stsp ie->path);
2183 993e2a1b 2020-07-23 stsp goto done;
2184 993e2a1b 2020-07-23 stsp }
2185 993e2a1b 2020-07-23 stsp
2186 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2187 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2188 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2189 1ee397ad 2019-07-12 stsp if (err)
2190 5a58a424 2020-06-23 stsp goto done;
2191 fc6346c4 2019-03-27 stsp /*
2192 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2193 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2194 fc6346c4 2019-03-27 stsp */
2195 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2196 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2197 fc6346c4 2019-03-27 stsp } else {
2198 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2199 1ee397ad 2019-07-12 stsp if (err)
2200 5a58a424 2020-06-23 stsp goto done;
2201 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2202 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2203 fc6346c4 2019-03-27 stsp if (err)
2204 5a58a424 2020-06-23 stsp goto done;
2205 fc6346c4 2019-03-27 stsp }
2206 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2207 708d8e67 2019-03-27 stsp }
2208 5a58a424 2020-06-23 stsp done:
2209 5a58a424 2020-06-23 stsp free(ondisk_path);
2210 fc6346c4 2019-03-27 stsp return err;
2211 708d8e67 2019-03-27 stsp }
2212 708d8e67 2019-03-27 stsp
2213 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2214 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2215 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2216 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2217 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2218 8da9e5f4 2019-01-12 stsp void *progress_arg;
2219 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2220 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2221 8da9e5f4 2019-01-12 stsp };
2222 8da9e5f4 2019-01-12 stsp
2223 512f0d0e 2019-01-02 stsp static const struct got_error *
2224 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2225 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2226 512f0d0e 2019-01-02 stsp {
2227 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2228 0584f854 2019-04-06 stsp
2229 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2230 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2231 512f0d0e 2019-01-02 stsp
2232 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2233 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2234 8da9e5f4 2019-01-12 stsp }
2235 512f0d0e 2019-01-02 stsp
2236 8da9e5f4 2019-01-12 stsp static const struct got_error *
2237 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2238 8da9e5f4 2019-01-12 stsp {
2239 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2240 7a9df742 2019-01-08 stsp
2241 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2243 0584f854 2019-04-06 stsp
2244 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2245 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2246 9d31a1d8 2018-03-11 stsp }
2247 9d31a1d8 2018-03-11 stsp
2248 9d31a1d8 2018-03-11 stsp static const struct got_error *
2249 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2250 9d31a1d8 2018-03-11 stsp {
2251 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2252 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2253 8da9e5f4 2019-01-12 stsp char *path;
2254 9d31a1d8 2018-03-11 stsp
2255 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2257 0584f854 2019-04-06 stsp
2258 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2259 63c5ca5d 2019-08-24 stsp return NULL;
2260 63c5ca5d 2019-08-24 stsp
2261 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2262 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2263 8da9e5f4 2019-01-12 stsp == -1)
2264 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2265 9d31a1d8 2018-03-11 stsp
2266 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2267 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2268 8da9e5f4 2019-01-12 stsp else
2269 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2270 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2271 9d31a1d8 2018-03-11 stsp
2272 8da9e5f4 2019-01-12 stsp free(path);
2273 0cd1c46a 2019-03-11 stsp return err;
2274 0cd1c46a 2019-03-11 stsp }
2275 0cd1c46a 2019-03-11 stsp
2276 b2118c49 2020-07-28 stsp const struct got_error *
2277 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2278 b2118c49 2020-07-28 stsp {
2279 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2280 b2118c49 2020-07-28 stsp
2281 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2282 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2283 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2284 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2285 b2118c49 2020-07-28 stsp }
2286 b2118c49 2020-07-28 stsp
2287 b2118c49 2020-07-28 stsp return NULL;
2288 b2118c49 2020-07-28 stsp }
2289 b2118c49 2020-07-28 stsp
2290 818c7501 2019-07-11 stsp static const struct got_error *
2291 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2292 0cd1c46a 2019-03-11 stsp {
2293 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2294 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2295 0cd1c46a 2019-03-11 stsp
2296 517bab73 2019-03-11 stsp *refname = NULL;
2297 517bab73 2019-03-11 stsp
2298 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2299 b2118c49 2020-07-28 stsp if (err)
2300 b2118c49 2020-07-28 stsp return err;
2301 0cd1c46a 2019-03-11 stsp
2302 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2303 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2304 0647c563 2019-03-11 stsp *refname = NULL;
2305 0cd1c46a 2019-03-11 stsp }
2306 517bab73 2019-03-11 stsp free(uuidstr);
2307 517bab73 2019-03-11 stsp return err;
2308 517bab73 2019-03-11 stsp }
2309 517bab73 2019-03-11 stsp
2310 818c7501 2019-07-11 stsp const struct got_error *
2311 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2312 f6cd0243 2023-01-28 thomas const char *prefix)
2313 f6cd0243 2023-01-28 thomas {
2314 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2315 f6cd0243 2023-01-28 thomas }
2316 f6cd0243 2023-01-28 thomas
2317 f6cd0243 2023-01-28 thomas const struct got_error *
2318 818c7501 2019-07-11 stsp got_worktree_get_base_ref_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, GOT_WORKTREE_BASE_REF_PREFIX);
2321 818c7501 2019-07-11 stsp }
2322 818c7501 2019-07-11 stsp
2323 818c7501 2019-07-11 stsp static const struct got_error *
2324 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2325 818c7501 2019-07-11 stsp {
2326 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2327 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2328 818c7501 2019-07-11 stsp }
2329 818c7501 2019-07-11 stsp
2330 818c7501 2019-07-11 stsp static const struct got_error *
2331 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2332 818c7501 2019-07-11 stsp {
2333 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2334 818c7501 2019-07-11 stsp }
2335 818c7501 2019-07-11 stsp
2336 818c7501 2019-07-11 stsp static const struct got_error *
2337 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2338 818c7501 2019-07-11 stsp {
2339 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2340 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2341 818c7501 2019-07-11 stsp }
2342 818c7501 2019-07-11 stsp
2343 818c7501 2019-07-11 stsp static const struct got_error *
2344 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2345 818c7501 2019-07-11 stsp {
2346 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2347 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2348 0ebf8283 2019-07-24 stsp }
2349 0ebf8283 2019-07-24 stsp
2350 0ebf8283 2019-07-24 stsp static const struct got_error *
2351 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2352 0ebf8283 2019-07-24 stsp {
2353 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2354 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2355 0ebf8283 2019-07-24 stsp }
2356 0ebf8283 2019-07-24 stsp
2357 0ebf8283 2019-07-24 stsp static const struct got_error *
2358 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2359 0ebf8283 2019-07-24 stsp {
2360 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2361 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2362 0ebf8283 2019-07-24 stsp }
2363 0ebf8283 2019-07-24 stsp
2364 0ebf8283 2019-07-24 stsp static const struct got_error *
2365 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2366 0ebf8283 2019-07-24 stsp {
2367 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2368 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2369 818c7501 2019-07-11 stsp }
2370 818c7501 2019-07-11 stsp
2371 0ebf8283 2019-07-24 stsp static const struct got_error *
2372 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2373 0ebf8283 2019-07-24 stsp {
2374 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2375 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2376 0ebf8283 2019-07-24 stsp }
2377 818c7501 2019-07-11 stsp
2378 0ebf8283 2019-07-24 stsp const struct got_error *
2379 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2380 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2381 0ebf8283 2019-07-24 stsp {
2382 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2383 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2384 0ebf8283 2019-07-24 stsp *path = NULL;
2385 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2386 0ebf8283 2019-07-24 stsp }
2387 0ebf8283 2019-07-24 stsp return NULL;
2388 10604dce 2021-09-24 thomas }
2389 10604dce 2021-09-24 thomas
2390 10604dce 2021-09-24 thomas static const struct got_error *
2391 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2392 10604dce 2021-09-24 thomas {
2393 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2394 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2395 0ebf8283 2019-07-24 stsp }
2396 0ebf8283 2019-07-24 stsp
2397 10604dce 2021-09-24 thomas static const struct got_error *
2398 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2399 10604dce 2021-09-24 thomas {
2400 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2401 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2402 10604dce 2021-09-24 thomas }
2403 10604dce 2021-09-24 thomas
2404 517bab73 2019-03-11 stsp /*
2405 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2406 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2407 517bab73 2019-03-11 stsp */
2408 517bab73 2019-03-11 stsp static const struct got_error *
2409 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2410 517bab73 2019-03-11 stsp {
2411 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2412 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2413 517bab73 2019-03-11 stsp char *refname;
2414 517bab73 2019-03-11 stsp
2415 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2416 517bab73 2019-03-11 stsp if (err)
2417 517bab73 2019-03-11 stsp return err;
2418 0cd1c46a 2019-03-11 stsp
2419 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2420 0cd1c46a 2019-03-11 stsp if (err)
2421 0cd1c46a 2019-03-11 stsp goto done;
2422 0cd1c46a 2019-03-11 stsp
2423 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2424 0cd1c46a 2019-03-11 stsp done:
2425 0cd1c46a 2019-03-11 stsp free(refname);
2426 0cd1c46a 2019-03-11 stsp if (ref)
2427 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2428 3e3a69f1 2019-07-25 stsp return err;
2429 3e3a69f1 2019-07-25 stsp }
2430 3e3a69f1 2019-07-25 stsp
2431 3e3a69f1 2019-07-25 stsp static const struct got_error *
2432 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2433 3e3a69f1 2019-07-25 stsp {
2434 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2435 3e3a69f1 2019-07-25 stsp
2436 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2437 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2438 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2439 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2440 3e3a69f1 2019-07-25 stsp }
2441 9d31a1d8 2018-03-11 stsp return err;
2442 9d31a1d8 2018-03-11 stsp }
2443 9d31a1d8 2018-03-11 stsp
2444 3e3a69f1 2019-07-25 stsp
2445 ebf99748 2019-05-09 stsp static const struct got_error *
2446 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2447 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2448 ebf99748 2019-05-09 stsp {
2449 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2450 ebf99748 2019-05-09 stsp FILE *index = NULL;
2451 0cd1c46a 2019-03-11 stsp
2452 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2453 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2454 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2455 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2456 ebf99748 2019-05-09 stsp
2457 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2458 3e3a69f1 2019-07-25 stsp if (err)
2459 ebf99748 2019-05-09 stsp goto done;
2460 ebf99748 2019-05-09 stsp
2461 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2462 ebf99748 2019-05-09 stsp if (index == NULL) {
2463 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2464 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2465 ebf99748 2019-05-09 stsp } else {
2466 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2467 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2468 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2469 ebf99748 2019-05-09 stsp }
2470 ebf99748 2019-05-09 stsp done:
2471 ebf99748 2019-05-09 stsp if (err) {
2472 ebf99748 2019-05-09 stsp free(*fileindex_path);
2473 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2474 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2475 ebf99748 2019-05-09 stsp *fileindex = NULL;
2476 ebf99748 2019-05-09 stsp }
2477 ebf99748 2019-05-09 stsp return err;
2478 ebf99748 2019-05-09 stsp }
2479 c932eeeb 2019-05-22 stsp
2480 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2481 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2482 c932eeeb 2019-05-22 stsp const char *path;
2483 c932eeeb 2019-05-22 stsp size_t path_len;
2484 c932eeeb 2019-05-22 stsp const char *entry_name;
2485 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2486 a484d721 2019-06-10 stsp void *progress_arg;
2487 c932eeeb 2019-05-22 stsp };
2488 ebf99748 2019-05-09 stsp
2489 c932eeeb 2019-05-22 stsp static const struct got_error *
2490 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2491 c932eeeb 2019-05-22 stsp {
2492 1ee397ad 2019-07-12 stsp const struct got_error *err;
2493 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2494 c932eeeb 2019-05-22 stsp
2495 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2496 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2497 c932eeeb 2019-05-22 stsp return NULL;
2498 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2499 102ff934 2019-06-11 stsp return NULL;
2500 102ff934 2019-06-11 stsp
2501 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2502 a769b60b 2021-06-27 stsp return NULL;
2503 a769b60b 2021-06-27 stsp
2504 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2505 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2506 c932eeeb 2019-05-22 stsp return NULL;
2507 c932eeeb 2019-05-22 stsp
2508 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2509 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2510 edd02c5e 2019-07-11 stsp ie->path);
2511 1ee397ad 2019-07-12 stsp if (err)
2512 1ee397ad 2019-07-12 stsp return err;
2513 1ee397ad 2019-07-12 stsp }
2514 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2515 c932eeeb 2019-05-22 stsp return NULL;
2516 a615e0e7 2020-12-16 stsp }
2517 a615e0e7 2020-12-16 stsp
2518 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2519 a615e0e7 2020-12-16 stsp static const struct got_error *
2520 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2521 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2522 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2523 a615e0e7 2020-12-16 stsp {
2524 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2525 a615e0e7 2020-12-16 stsp
2526 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2527 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2528 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2529 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2530 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2531 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2532 a615e0e7 2020-12-16 stsp
2533 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2534 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2535 9c6338c4 2019-06-02 stsp }
2536 9c6338c4 2019-06-02 stsp
2537 9c6338c4 2019-06-02 stsp static const struct got_error *
2538 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2539 9c6338c4 2019-06-02 stsp {
2540 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2541 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2542 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2543 867630bb 2020-01-17 stsp struct timespec timeout;
2544 9c6338c4 2019-06-02 stsp
2545 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2546 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2547 9c6338c4 2019-06-02 stsp if (err)
2548 9c6338c4 2019-06-02 stsp goto done;
2549 9c6338c4 2019-06-02 stsp
2550 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2551 9c6338c4 2019-06-02 stsp if (err)
2552 9c6338c4 2019-06-02 stsp goto done;
2553 9c6338c4 2019-06-02 stsp
2554 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2555 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2556 9c6338c4 2019-06-02 stsp fileindex_path);
2557 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2558 9c6338c4 2019-06-02 stsp }
2559 867630bb 2020-01-17 stsp
2560 867630bb 2020-01-17 stsp /*
2561 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2562 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2563 867630bb 2020-01-17 stsp * was recorded in the file index.
2564 867630bb 2020-01-17 stsp */
2565 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2566 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2567 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2568 9c6338c4 2019-06-02 stsp done:
2569 9c6338c4 2019-06-02 stsp if (new_index)
2570 9c6338c4 2019-06-02 stsp fclose(new_index);
2571 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2572 9c6338c4 2019-06-02 stsp return err;
2573 c932eeeb 2019-05-22 stsp }
2574 6ced4176 2019-07-12 stsp
2575 6ced4176 2019-07-12 stsp static const struct got_error *
2576 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2577 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2578 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2579 945f9229 2022-04-16 thomas struct got_repository *repo)
2580 6ced4176 2019-07-12 stsp {
2581 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2582 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2583 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2584 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2585 6ced4176 2019-07-12 stsp
2586 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2587 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2588 6ced4176 2019-07-12 stsp *tree_id = NULL;
2589 6ced4176 2019-07-12 stsp
2590 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2591 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2592 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2593 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2594 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2595 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2596 6ced4176 2019-07-12 stsp goto done;
2597 6ced4176 2019-07-12 stsp }
2598 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2599 945f9229 2022-04-16 thomas worktree->path_prefix);
2600 6ced4176 2019-07-12 stsp if (err)
2601 6ced4176 2019-07-12 stsp goto done;
2602 6ced4176 2019-07-12 stsp return NULL;
2603 6ced4176 2019-07-12 stsp }
2604 6ced4176 2019-07-12 stsp
2605 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2606 6ced4176 2019-07-12 stsp
2607 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2608 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2609 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2610 6ced4176 2019-07-12 stsp goto done;
2611 6ced4176 2019-07-12 stsp }
2612 6ced4176 2019-07-12 stsp
2613 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2614 6ced4176 2019-07-12 stsp if (err)
2615 6ced4176 2019-07-12 stsp goto done;
2616 6ced4176 2019-07-12 stsp
2617 6ced4176 2019-07-12 stsp free(in_repo_path);
2618 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2619 6ced4176 2019-07-12 stsp
2620 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2621 6ced4176 2019-07-12 stsp if (err)
2622 6ced4176 2019-07-12 stsp goto done;
2623 c932eeeb 2019-05-22 stsp
2624 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2625 6ced4176 2019-07-12 stsp /* Check out a single file. */
2626 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2627 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2628 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2629 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2630 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2631 6ced4176 2019-07-12 stsp goto done;
2632 6ced4176 2019-07-12 stsp }
2633 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2634 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2635 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2636 6ced4176 2019-07-12 stsp goto done;
2637 6ced4176 2019-07-12 stsp }
2638 6ced4176 2019-07-12 stsp } else {
2639 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2640 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2641 6ced4176 2019-07-12 stsp if (err)
2642 6ced4176 2019-07-12 stsp return err;
2643 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2644 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2645 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2646 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2647 6ced4176 2019-07-12 stsp goto done;
2648 6ced4176 2019-07-12 stsp }
2649 6ced4176 2019-07-12 stsp }
2650 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2651 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2652 6ced4176 2019-07-12 stsp } else {
2653 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2654 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2655 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2656 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2657 6ced4176 2019-07-12 stsp goto done;
2658 6ced4176 2019-07-12 stsp }
2659 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2660 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2661 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2662 6ced4176 2019-07-12 stsp goto done;
2663 6ced4176 2019-07-12 stsp }
2664 6ced4176 2019-07-12 stsp }
2665 6ced4176 2019-07-12 stsp done:
2666 6ced4176 2019-07-12 stsp free(id);
2667 6ced4176 2019-07-12 stsp free(in_repo_path);
2668 6ced4176 2019-07-12 stsp if (err) {
2669 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2670 6ced4176 2019-07-12 stsp free(*tree_relpath);
2671 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2672 6ced4176 2019-07-12 stsp free(*tree_id);
2673 6ced4176 2019-07-12 stsp *tree_id = NULL;
2674 6ced4176 2019-07-12 stsp }
2675 07ed472d 2019-07-12 stsp return err;
2676 07ed472d 2019-07-12 stsp }
2677 07ed472d 2019-07-12 stsp
2678 07ed472d 2019-07-12 stsp static const struct got_error *
2679 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2680 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2681 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2682 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2683 07ed472d 2019-07-12 stsp {
2684 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2685 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2686 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2687 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2688 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2689 07ed472d 2019-07-12 stsp
2690 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2691 7f47418f 2019-12-20 stsp if (err) {
2692 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2693 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2694 7f47418f 2019-12-20 stsp goto done;
2695 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2696 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2697 7f47418f 2019-12-20 stsp if (err)
2698 7f47418f 2019-12-20 stsp return err;
2699 7f47418f 2019-12-20 stsp }
2700 07ed472d 2019-07-12 stsp
2701 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2702 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2703 07ed472d 2019-07-12 stsp if (err)
2704 07ed472d 2019-07-12 stsp goto done;
2705 07ed472d 2019-07-12 stsp
2706 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2707 07ed472d 2019-07-12 stsp if (err)
2708 07ed472d 2019-07-12 stsp goto done;
2709 07ed472d 2019-07-12 stsp
2710 07ed472d 2019-07-12 stsp if (entry_name &&
2711 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2712 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2713 07ed472d 2019-07-12 stsp goto done;
2714 07ed472d 2019-07-12 stsp }
2715 07ed472d 2019-07-12 stsp
2716 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2717 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2718 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2719 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2720 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2721 07ed472d 2019-07-12 stsp arg.repo = repo;
2722 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2723 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2724 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2725 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2726 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2727 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2728 07ed472d 2019-07-12 stsp done:
2729 07ed472d 2019-07-12 stsp if (tree)
2730 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2731 07ed472d 2019-07-12 stsp if (commit)
2732 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2733 6ced4176 2019-07-12 stsp return err;
2734 6ced4176 2019-07-12 stsp }
2735 6ced4176 2019-07-12 stsp
2736 9d31a1d8 2018-03-11 stsp const struct got_error *
2737 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2738 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2739 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2740 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2741 9d31a1d8 2018-03-11 stsp {
2742 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2743 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2744 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2745 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2746 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2747 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2748 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2749 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2750 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2751 f2ea84fa 2019-07-27 stsp int entry_type;
2752 f2ea84fa 2019-07-27 stsp char *relpath;
2753 f2ea84fa 2019-07-27 stsp char *entry_name;
2754 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2755 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2756 9d31a1d8 2018-03-11 stsp
2757 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2758 f2ea84fa 2019-07-27 stsp
2759 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2760 9d31a1d8 2018-03-11 stsp if (err)
2761 9d31a1d8 2018-03-11 stsp return err;
2762 945f9229 2022-04-16 thomas
2763 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2764 945f9229 2022-04-16 thomas worktree->base_commit_id);
2765 945f9229 2022-04-16 thomas if (err)
2766 945f9229 2022-04-16 thomas goto done;
2767 93a30277 2018-12-24 stsp
2768 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2769 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2770 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2771 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2772 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2773 f2ea84fa 2019-07-27 stsp goto done;
2774 f2ea84fa 2019-07-27 stsp }
2775 6ced4176 2019-07-12 stsp
2776 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2777 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2778 945f9229 2022-04-16 thomas worktree, repo);
2779 f2ea84fa 2019-07-27 stsp if (err) {
2780 f2ea84fa 2019-07-27 stsp free(tpd);
2781 6ced4176 2019-07-12 stsp goto done;
2782 6ced4176 2019-07-12 stsp }
2783 f2ea84fa 2019-07-27 stsp
2784 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2785 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2786 f2ea84fa 2019-07-27 stsp if (err) {
2787 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2788 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2789 f2ea84fa 2019-07-27 stsp free(tpd);
2790 f2ea84fa 2019-07-27 stsp goto done;
2791 f2ea84fa 2019-07-27 stsp }
2792 f2ea84fa 2019-07-27 stsp } else
2793 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2794 f2ea84fa 2019-07-27 stsp
2795 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2796 6ced4176 2019-07-12 stsp }
2797 6ced4176 2019-07-12 stsp
2798 51514078 2018-12-25 stsp /*
2799 51514078 2018-12-25 stsp * Read the file index.
2800 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2801 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2802 51514078 2018-12-25 stsp */
2803 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2804 8da9e5f4 2019-01-12 stsp if (err)
2805 c4cdcb68 2019-04-03 stsp goto done;
2806 c4cdcb68 2019-04-03 stsp
2807 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2808 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2809 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2810 f2ea84fa 2019-07-27 stsp
2811 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2812 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2813 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2814 f2ea84fa 2019-07-27 stsp if (err)
2815 f2ea84fa 2019-07-27 stsp break;
2816 f2ea84fa 2019-07-27 stsp
2817 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2818 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2819 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2820 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2821 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2822 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2823 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2824 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2825 f2ea84fa 2019-07-27 stsp if (err)
2826 f2ea84fa 2019-07-27 stsp break;
2827 f2ea84fa 2019-07-27 stsp
2828 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2829 711d9cd9 2019-07-12 stsp }
2830 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2831 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2832 af12c6b9 2019-06-04 stsp err = sync_err;
2833 9d31a1d8 2018-03-11 stsp done:
2834 9c6338c4 2019-06-02 stsp free(fileindex_path);
2835 edfa7d7f 2018-09-11 stsp if (tree)
2836 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2837 9d31a1d8 2018-03-11 stsp if (commit)
2838 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2839 5ade8233 2019-07-12 stsp if (fileindex)
2840 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2841 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2842 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2843 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2844 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2845 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2846 f2ea84fa 2019-07-27 stsp free(tpd);
2847 f2ea84fa 2019-07-27 stsp }
2848 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2849 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2850 9d31a1d8 2018-03-11 stsp err = unlockerr;
2851 f8d1f275 2019-02-04 stsp return err;
2852 f8d1f275 2019-02-04 stsp }
2853 f8d1f275 2019-02-04 stsp
2854 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2855 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2856 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2857 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2858 234035bc 2019-06-01 stsp void *progress_arg;
2859 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2860 234035bc 2019-06-01 stsp void *cancel_arg;
2861 f69721c3 2019-10-21 stsp const char *label_orig;
2862 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2863 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2864 234035bc 2019-06-01 stsp };
2865 234035bc 2019-06-01 stsp
2866 234035bc 2019-06-01 stsp static const struct got_error *
2867 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2868 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2869 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2870 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2871 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2872 234035bc 2019-06-01 stsp {
2873 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2874 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2875 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2876 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2877 234035bc 2019-06-01 stsp struct stat sb;
2878 234035bc 2019-06-01 stsp unsigned char status;
2879 234035bc 2019-06-01 stsp int local_changes_subsumed;
2880 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2881 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2882 234035bc 2019-06-01 stsp
2883 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2884 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2885 d6c87207 2019-08-02 stsp strlen(path2));
2886 1ee397ad 2019-07-12 stsp if (ie == NULL)
2887 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2888 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2889 234035bc 2019-06-01 stsp
2890 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2891 234035bc 2019-06-01 stsp path2) == -1)
2892 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2893 234035bc 2019-06-01 stsp
2894 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2895 7f91a133 2019-12-13 stsp repo);
2896 234035bc 2019-06-01 stsp if (err)
2897 234035bc 2019-06-01 stsp goto done;
2898 234035bc 2019-06-01 stsp
2899 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2900 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2901 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2902 234035bc 2019-06-01 stsp goto done;
2903 234035bc 2019-06-01 stsp }
2904 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2905 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2906 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2907 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2908 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2909 234035bc 2019-06-01 stsp goto done;
2910 234035bc 2019-06-01 stsp }
2911 234035bc 2019-06-01 stsp
2912 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2913 36bf999c 2020-07-23 stsp char *link_target2;
2914 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2915 36bf999c 2020-07-23 stsp if (err)
2916 36bf999c 2020-07-23 stsp goto done;
2917 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2918 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2919 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2920 36bf999c 2020-07-23 stsp free(link_target2);
2921 af57b12a 2020-07-23 stsp } else {
2922 1af628f4 2021-06-03 stsp int fd;
2923 1af628f4 2021-06-03 stsp
2924 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2925 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2926 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2927 1af628f4 2021-06-03 stsp goto done;
2928 1af628f4 2021-06-03 stsp }
2929 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2930 1af628f4 2021-06-03 stsp f_orig, blob1);
2931 1af628f4 2021-06-03 stsp if (err)
2932 1af628f4 2021-06-03 stsp goto done;
2933 1af628f4 2021-06-03 stsp
2934 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2935 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2936 1af628f4 2021-06-03 stsp goto done;
2937 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2938 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2939 1af628f4 2021-06-03 stsp if (err)
2940 1af628f4 2021-06-03 stsp goto done;
2941 1af628f4 2021-06-03 stsp
2942 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2943 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2944 1af628f4 2021-06-03 stsp if (fd == -1) {
2945 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2946 1af628f4 2021-06-03 stsp ondisk_path);
2947 1af628f4 2021-06-03 stsp goto done;
2948 1af628f4 2021-06-03 stsp }
2949 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2950 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2951 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2952 1af628f4 2021-06-03 stsp ondisk_path);
2953 1af628f4 2021-06-03 stsp close(fd);
2954 1af628f4 2021-06-03 stsp goto done;
2955 1af628f4 2021-06-03 stsp }
2956 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2957 1af628f4 2021-06-03 stsp if (err)
2958 1af628f4 2021-06-03 stsp goto done;
2959 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2960 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2961 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2962 1af628f4 2021-06-03 stsp goto done;
2963 1af628f4 2021-06-03 stsp }
2964 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2965 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2966 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
2967 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2968 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2969 af57b12a 2020-07-23 stsp }
2970 234035bc 2019-06-01 stsp } else if (blob1) {
2971 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2972 d6c87207 2019-08-02 stsp strlen(path1));
2973 1ee397ad 2019-07-12 stsp if (ie == NULL)
2974 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2975 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2976 2b92fad7 2019-06-02 stsp
2977 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2978 2b92fad7 2019-06-02 stsp path1) == -1)
2979 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2980 2b92fad7 2019-06-02 stsp
2981 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2982 7f91a133 2019-12-13 stsp repo);
2983 2b92fad7 2019-06-02 stsp if (err)
2984 2b92fad7 2019-06-02 stsp goto done;
2985 2b92fad7 2019-06-02 stsp
2986 2b92fad7 2019-06-02 stsp switch (status) {
2987 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2988 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2989 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2990 1ee397ad 2019-07-12 stsp if (err)
2991 1ee397ad 2019-07-12 stsp goto done;
2992 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2993 2b92fad7 2019-06-02 stsp if (err)
2994 2b92fad7 2019-06-02 stsp goto done;
2995 2b92fad7 2019-06-02 stsp if (ie)
2996 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2997 2b92fad7 2019-06-02 stsp break;
2998 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2999 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3000 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3001 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3002 1ee397ad 2019-07-12 stsp if (err)
3003 1ee397ad 2019-07-12 stsp goto done;
3004 2b92fad7 2019-06-02 stsp if (ie)
3005 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3006 2b92fad7 2019-06-02 stsp break;
3007 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3008 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3009 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3010 e8f02263 2022-01-23 thomas off_t blob1_size;
3011 0a22ca1a 2020-09-23 stsp /*
3012 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3013 0a22ca1a 2020-09-23 stsp * exists in the repository.
3014 0a22ca1a 2020-09-23 stsp */
3015 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3016 e8f02263 2022-01-23 thomas &blob1_size, path1);
3017 0a22ca1a 2020-09-23 stsp if (err)
3018 0a22ca1a 2020-09-23 stsp goto done;
3019 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3020 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3021 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3022 0a22ca1a 2020-09-23 stsp if (err)
3023 0a22ca1a 2020-09-23 stsp goto done;
3024 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3025 0a22ca1a 2020-09-23 stsp path1);
3026 0a22ca1a 2020-09-23 stsp if (err)
3027 0a22ca1a 2020-09-23 stsp goto done;
3028 0a22ca1a 2020-09-23 stsp if (ie)
3029 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3030 0a22ca1a 2020-09-23 stsp ie);
3031 0a22ca1a 2020-09-23 stsp } else {
3032 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3033 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3034 0a22ca1a 2020-09-23 stsp }
3035 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3036 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3037 0a22ca1a 2020-09-23 stsp free(id);
3038 0a22ca1a 2020-09-23 stsp if (err)
3039 0a22ca1a 2020-09-23 stsp goto done;
3040 0a22ca1a 2020-09-23 stsp break;
3041 0a22ca1a 2020-09-23 stsp }
3042 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3043 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3044 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3045 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3046 1ee397ad 2019-07-12 stsp if (err)
3047 1ee397ad 2019-07-12 stsp goto done;
3048 2b92fad7 2019-06-02 stsp break;
3049 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3050 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3051 1ee397ad 2019-07-12 stsp if (err)
3052 1ee397ad 2019-07-12 stsp goto done;
3053 2b92fad7 2019-06-02 stsp break;
3054 2b92fad7 2019-06-02 stsp default:
3055 2b92fad7 2019-06-02 stsp break;
3056 2b92fad7 2019-06-02 stsp }
3057 234035bc 2019-06-01 stsp } else if (blob2) {
3058 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3059 234035bc 2019-06-01 stsp path2) == -1)
3060 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3061 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3062 d6c87207 2019-08-02 stsp strlen(path2));
3063 234035bc 2019-06-01 stsp if (ie) {
3064 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3065 7f91a133 2019-12-13 stsp -1, NULL, repo);
3066 234035bc 2019-06-01 stsp if (err)
3067 234035bc 2019-06-01 stsp goto done;
3068 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3069 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3070 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3071 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3072 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3073 1ee397ad 2019-07-12 stsp status, path2);
3074 234035bc 2019-06-01 stsp goto done;
3075 234035bc 2019-06-01 stsp }
3076 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3077 36bf999c 2020-07-23 stsp char *link_target2;
3078 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3079 36bf999c 2020-07-23 stsp blob2);
3080 36bf999c 2020-07-23 stsp if (err)
3081 36bf999c 2020-07-23 stsp goto done;
3082 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3083 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3084 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3085 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3086 36bf999c 2020-07-23 stsp free(link_target2);
3087 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3088 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3089 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3090 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3091 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3092 526a746f 2020-07-23 stsp a->progress_arg);
3093 dfe9fba0 2020-07-23 stsp } else {
3094 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3095 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3096 526a746f 2020-07-23 stsp }
3097 dfe9fba0 2020-07-23 stsp if (err)
3098 dfe9fba0 2020-07-23 stsp goto done;
3099 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3100 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3101 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3102 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3103 234035bc 2019-06-01 stsp if (err)
3104 234035bc 2019-06-01 stsp goto done;
3105 234035bc 2019-06-01 stsp }
3106 234035bc 2019-06-01 stsp } else {
3107 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3108 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3109 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3110 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3111 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3112 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3113 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3114 2e1fa222 2020-07-23 stsp } else {
3115 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3116 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3117 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3118 2e1fa222 2020-07-23 stsp }
3119 234035bc 2019-06-01 stsp if (err)
3120 234035bc 2019-06-01 stsp goto done;
3121 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3122 234035bc 2019-06-01 stsp if (err)
3123 234035bc 2019-06-01 stsp goto done;
3124 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3125 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3126 3969253a 2020-03-07 stsp if (err) {
3127 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3128 3969253a 2020-03-07 stsp goto done;
3129 3969253a 2020-03-07 stsp }
3130 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3131 2b92fad7 2019-06-02 stsp if (err) {
3132 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3133 2b92fad7 2019-06-02 stsp goto done;
3134 2b92fad7 2019-06-02 stsp }
3135 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3136 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3137 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3138 2e1fa222 2020-07-23 stsp }
3139 234035bc 2019-06-01 stsp }
3140 234035bc 2019-06-01 stsp }
3141 234035bc 2019-06-01 stsp done:
3142 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3143 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3144 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3145 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3146 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3147 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3148 1af628f4 2021-06-03 stsp free(id_str);
3149 54d5be07 2021-06-03 stsp free(label_deriv2);
3150 234035bc 2019-06-01 stsp free(ondisk_path);
3151 234035bc 2019-06-01 stsp return err;
3152 234035bc 2019-06-01 stsp }
3153 234035bc 2019-06-01 stsp
3154 69de9dd4 2021-09-03 stsp static const struct got_error *
3155 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3156 69de9dd4 2021-09-03 stsp {
3157 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3158 69de9dd4 2021-09-03 stsp
3159 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3160 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3161 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3162 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3163 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3164 69de9dd4 2021-09-03 stsp
3165 69de9dd4 2021-09-03 stsp return NULL;
3166 69de9dd4 2021-09-03 stsp }
3167 69de9dd4 2021-09-03 stsp
3168 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3169 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3170 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3171 234035bc 2019-06-01 stsp struct got_repository *repo;
3172 234035bc 2019-06-01 stsp };
3173 234035bc 2019-06-01 stsp
3174 234035bc 2019-06-01 stsp static const struct got_error *
3175 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3176 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3177 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3178 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3179 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3180 234035bc 2019-06-01 stsp {
3181 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3182 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3183 234035bc 2019-06-01 stsp unsigned char status;
3184 234035bc 2019-06-01 stsp struct stat sb;
3185 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3186 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3187 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3188 234035bc 2019-06-01 stsp char *ondisk_path;
3189 234035bc 2019-06-01 stsp
3190 69de9dd4 2021-09-03 stsp if (id == NULL)
3191 69de9dd4 2021-09-03 stsp return NULL;
3192 234035bc 2019-06-01 stsp
3193 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3194 69de9dd4 2021-09-03 stsp if (ie == NULL)
3195 69de9dd4 2021-09-03 stsp return NULL;
3196 69de9dd4 2021-09-03 stsp
3197 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3198 234035bc 2019-06-01 stsp == -1)
3199 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3200 234035bc 2019-06-01 stsp
3201 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3202 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3203 5546d466 2021-09-02 stsp free(ondisk_path);
3204 234035bc 2019-06-01 stsp if (err)
3205 234035bc 2019-06-01 stsp return err;
3206 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3207 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3208 234035bc 2019-06-01 stsp
3209 234035bc 2019-06-01 stsp return NULL;
3210 234035bc 2019-06-01 stsp }
3211 234035bc 2019-06-01 stsp
3212 818c7501 2019-07-11 stsp static const struct got_error *
3213 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3214 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3215 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3216 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3217 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3218 234035bc 2019-06-01 stsp {
3219 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3220 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3221 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3222 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3223 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3224 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3225 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3226 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3227 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3228 234035bc 2019-06-01 stsp
3229 03415a1a 2019-06-02 stsp if (commit_id1) {
3230 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3231 945f9229 2022-04-16 thomas if (err)
3232 945f9229 2022-04-16 thomas goto done;
3233 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3234 03415a1a 2019-06-02 stsp worktree->path_prefix);
3235 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3236 03415a1a 2019-06-02 stsp goto done;
3237 69d57f3d 2020-07-31 stsp }
3238 69d57f3d 2020-07-31 stsp if (tree_id1) {
3239 69d57f3d 2020-07-31 stsp char *id_str;
3240 03415a1a 2019-06-02 stsp
3241 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3242 03415a1a 2019-06-02 stsp if (err)
3243 03415a1a 2019-06-02 stsp goto done;
3244 f69721c3 2019-10-21 stsp
3245 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3246 f69721c3 2019-10-21 stsp if (err)
3247 f69721c3 2019-10-21 stsp goto done;
3248 f69721c3 2019-10-21 stsp
3249 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3250 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3251 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3252 f69721c3 2019-10-21 stsp free(id_str);
3253 f69721c3 2019-10-21 stsp goto done;
3254 f69721c3 2019-10-21 stsp }
3255 f69721c3 2019-10-21 stsp free(id_str);
3256 a0f32f33 2022-06-13 thomas
3257 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3258 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3259 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3260 a0f32f33 2022-06-13 thomas goto done;
3261 a0f32f33 2022-06-13 thomas }
3262 03415a1a 2019-06-02 stsp }
3263 234035bc 2019-06-01 stsp
3264 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3265 945f9229 2022-04-16 thomas if (err)
3266 945f9229 2022-04-16 thomas goto done;
3267 945f9229 2022-04-16 thomas
3268 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3269 234035bc 2019-06-01 stsp worktree->path_prefix);
3270 234035bc 2019-06-01 stsp if (err)
3271 234035bc 2019-06-01 stsp goto done;
3272 234035bc 2019-06-01 stsp
3273 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3274 234035bc 2019-06-01 stsp if (err)
3275 234035bc 2019-06-01 stsp goto done;
3276 234035bc 2019-06-01 stsp
3277 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3278 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3279 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3280 19a6a6b5 2022-07-01 thomas goto done;
3281 19a6a6b5 2022-07-01 thomas }
3282 19a6a6b5 2022-07-01 thomas
3283 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3284 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3285 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3286 a0f32f33 2022-06-13 thomas goto done;
3287 a0f32f33 2022-06-13 thomas }
3288 a0f32f33 2022-06-13 thomas
3289 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3290 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3291 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3292 19a6a6b5 2022-07-01 thomas goto done;
3293 19a6a6b5 2022-07-01 thomas }
3294 19a6a6b5 2022-07-01 thomas
3295 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3296 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3297 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3298 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3299 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3300 69de9dd4 2021-09-03 stsp if (err)
3301 69de9dd4 2021-09-03 stsp goto done;
3302 69de9dd4 2021-09-03 stsp
3303 234035bc 2019-06-01 stsp arg.worktree = worktree;
3304 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3305 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3306 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3307 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3308 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3309 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3310 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3311 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3312 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3313 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3314 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3315 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3316 af12c6b9 2019-06-04 stsp err = sync_err;
3317 234035bc 2019-06-01 stsp done:
3318 945f9229 2022-04-16 thomas if (commit1)
3319 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3320 945f9229 2022-04-16 thomas if (commit2)
3321 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3322 234035bc 2019-06-01 stsp if (tree1)
3323 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3324 234035bc 2019-06-01 stsp if (tree2)
3325 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3326 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3327 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3328 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3329 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3330 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3331 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3332 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3333 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3334 f69721c3 2019-10-21 stsp free(label_orig);
3335 818c7501 2019-07-11 stsp return err;
3336 818c7501 2019-07-11 stsp }
3337 234035bc 2019-06-01 stsp
3338 818c7501 2019-07-11 stsp const struct got_error *
3339 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3340 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3341 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3342 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3343 818c7501 2019-07-11 stsp {
3344 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3345 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3346 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3347 818c7501 2019-07-11 stsp
3348 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3349 818c7501 2019-07-11 stsp if (err)
3350 818c7501 2019-07-11 stsp return err;
3351 818c7501 2019-07-11 stsp
3352 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3353 818c7501 2019-07-11 stsp if (err)
3354 818c7501 2019-07-11 stsp goto done;
3355 818c7501 2019-07-11 stsp
3356 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3357 69de9dd4 2021-09-03 stsp worktree);
3358 818c7501 2019-07-11 stsp if (err)
3359 818c7501 2019-07-11 stsp goto done;
3360 818c7501 2019-07-11 stsp
3361 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3362 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3363 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3364 818c7501 2019-07-11 stsp done:
3365 818c7501 2019-07-11 stsp if (fileindex)
3366 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3367 818c7501 2019-07-11 stsp free(fileindex_path);
3368 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3369 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3370 234035bc 2019-06-01 stsp err = unlockerr;
3371 234035bc 2019-06-01 stsp return err;
3372 234035bc 2019-06-01 stsp }
3373 234035bc 2019-06-01 stsp
3374 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3375 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3376 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3377 927df6b7 2019-02-10 stsp const char *status_path;
3378 927df6b7 2019-02-10 stsp size_t status_path_len;
3379 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3380 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3381 f8d1f275 2019-02-04 stsp void *status_arg;
3382 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3383 f8d1f275 2019-02-04 stsp void *cancel_arg;
3384 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3385 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3386 f2a9dc41 2019-12-13 tracey int report_unchanged;
3387 3143d852 2020-06-25 stsp int no_ignores;
3388 f8d1f275 2019-02-04 stsp };
3389 88d0e355 2019-08-03 stsp
3390 f8d1f275 2019-02-04 stsp static const struct got_error *
3391 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3392 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3393 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3394 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3395 927df6b7 2019-02-10 stsp {
3396 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3397 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3398 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3399 927df6b7 2019-02-10 stsp struct stat sb;
3400 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3401 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3402 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3403 927df6b7 2019-02-10 stsp
3404 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3405 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3406 98eaaa12 2019-08-03 stsp if (err)
3407 98eaaa12 2019-08-03 stsp return err;
3408 98eaaa12 2019-08-03 stsp
3409 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3410 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3411 98eaaa12 2019-08-03 stsp return NULL;
3412 98eaaa12 2019-08-03 stsp
3413 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3414 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3415 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3416 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3417 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3418 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3419 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3420 43010591 2023-02-17 thomas &staged_blob_id, ie);
3421 98eaaa12 2019-08-03 stsp }
3422 98eaaa12 2019-08-03 stsp
3423 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3424 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3425 927df6b7 2019-02-10 stsp }
3426 927df6b7 2019-02-10 stsp
3427 927df6b7 2019-02-10 stsp static const struct got_error *
3428 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3429 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3430 f8d1f275 2019-02-04 stsp {
3431 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3432 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3433 f8d1f275 2019-02-04 stsp char *abspath;
3434 f8d1f275 2019-02-04 stsp
3435 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3436 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3437 0584f854 2019-04-06 stsp
3438 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3439 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3440 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3441 927df6b7 2019-02-10 stsp return NULL;
3442 927df6b7 2019-02-10 stsp
3443 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3444 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3445 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3446 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3447 f8d1f275 2019-02-04 stsp } else {
3448 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3449 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3450 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3451 f8d1f275 2019-02-04 stsp }
3452 f8d1f275 2019-02-04 stsp
3453 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3454 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3455 f8d1f275 2019-02-04 stsp free(abspath);
3456 9d31a1d8 2018-03-11 stsp return err;
3457 9d31a1d8 2018-03-11 stsp }
3458 f8d1f275 2019-02-04 stsp
3459 f8d1f275 2019-02-04 stsp static const struct got_error *
3460 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3461 f8d1f275 2019-02-04 stsp {
3462 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3463 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3464 2ec1f75b 2019-03-26 stsp unsigned char status;
3465 927df6b7 2019-02-10 stsp
3466 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3467 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3468 0584f854 2019-04-06 stsp
3469 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3470 927df6b7 2019-02-10 stsp return NULL;
3471 927df6b7 2019-02-10 stsp
3472 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3473 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3474 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3475 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3476 2ec1f75b 2019-03-26 stsp else
3477 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3478 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3479 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3480 6841da00 2019-08-08 stsp }
3481 6841da00 2019-08-08 stsp
3482 ef20f542 2022-06-26 thomas static void
3483 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3484 6841da00 2019-08-08 stsp {
3485 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3486 6841da00 2019-08-08 stsp
3487 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3488 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3489 21c2d8be 2023-01-10 thomas
3490 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3491 6841da00 2019-08-08 stsp }
3492 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3493 6841da00 2019-08-08 stsp }
3494 6841da00 2019-08-08 stsp
3495 6841da00 2019-08-08 stsp static const struct got_error *
3496 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3497 6841da00 2019-08-08 stsp {
3498 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3499 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3500 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3501 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3502 6841da00 2019-08-08 stsp size_t linesize = 0;
3503 6841da00 2019-08-08 stsp ssize_t linelen;
3504 6841da00 2019-08-08 stsp
3505 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3506 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3507 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3508 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3509 6841da00 2019-08-08 stsp
3510 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3511 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3512 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3513 bd8de430 2019-10-04 stsp
3514 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3515 bd8de430 2019-10-04 stsp if (line[0] == '#')
3516 bd8de430 2019-10-04 stsp continue;
3517 bd8de430 2019-10-04 stsp
3518 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3519 bd8de430 2019-10-04 stsp if (line[0] == '!')
3520 bd8de430 2019-10-04 stsp continue;
3521 bd8de430 2019-10-04 stsp
3522 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3523 6841da00 2019-08-08 stsp line) == -1) {
3524 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3525 6841da00 2019-08-08 stsp goto done;
3526 6841da00 2019-08-08 stsp }
3527 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3528 6841da00 2019-08-08 stsp if (err)
3529 6841da00 2019-08-08 stsp goto done;
3530 6841da00 2019-08-08 stsp }
3531 6841da00 2019-08-08 stsp if (ferror(f)) {
3532 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3533 6841da00 2019-08-08 stsp goto done;
3534 6841da00 2019-08-08 stsp }
3535 6841da00 2019-08-08 stsp
3536 6841da00 2019-08-08 stsp dirpath = strdup(path);
3537 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3538 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3539 6841da00 2019-08-08 stsp goto done;
3540 6841da00 2019-08-08 stsp }
3541 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3542 6841da00 2019-08-08 stsp done:
3543 6841da00 2019-08-08 stsp free(line);
3544 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3545 6841da00 2019-08-08 stsp free(dirpath);
3546 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3547 6841da00 2019-08-08 stsp }
3548 6841da00 2019-08-08 stsp return err;
3549 1b5d300f 2023-02-20 thomas }
3550 1b5d300f 2023-02-20 thomas
3551 1b5d300f 2023-02-20 thomas static int
3552 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3553 1b5d300f 2023-02-20 thomas int flags)
3554 1b5d300f 2023-02-20 thomas {
3555 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3556 1b5d300f 2023-02-20 thomas
3557 1b5d300f 2023-02-20 thomas /*
3558 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3559 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3560 1b5d300f 2023-02-20 thomas */
3561 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3562 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3563 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3564 1b5d300f 2023-02-20 thomas
3565 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3566 1b5d300f 2023-02-20 thomas }
3567 1b5d300f 2023-02-20 thomas
3568 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3569 6841da00 2019-08-08 stsp }
3570 6841da00 2019-08-08 stsp
3571 ef20f542 2022-06-26 thomas static int
3572 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3573 6841da00 2019-08-08 stsp {
3574 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3575 bd8de430 2019-10-04 stsp
3576 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3577 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3578 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3579 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3580 bd8de430 2019-10-04 stsp
3581 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3582 1b5d300f 2023-02-20 thomas const char *p;
3583 6841da00 2019-08-08 stsp
3584 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3585 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3586 bd8de430 2019-10-04 stsp continue;
3587 bd8de430 2019-10-04 stsp p = path;
3588 bd8de430 2019-10-04 stsp while (*p) {
3589 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3590 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3591 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3592 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3593 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3594 bd8de430 2019-10-04 stsp p++;
3595 bd8de430 2019-10-04 stsp while (*p == '/')
3596 bd8de430 2019-10-04 stsp p++;
3597 bd8de430 2019-10-04 stsp continue;
3598 bd8de430 2019-10-04 stsp }
3599 bd8de430 2019-10-04 stsp return 1;
3600 bd8de430 2019-10-04 stsp }
3601 bd8de430 2019-10-04 stsp }
3602 bd8de430 2019-10-04 stsp }
3603 bd8de430 2019-10-04 stsp
3604 6841da00 2019-08-08 stsp /*
3605 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3606 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3607 6841da00 2019-08-08 stsp * ignores backwards.
3608 6841da00 2019-08-08 stsp */
3609 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3610 6841da00 2019-08-08 stsp while (pe) {
3611 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3612 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3613 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3614 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3615 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3616 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3617 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3618 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3619 1b5d300f 2023-02-20 thomas path, flags))
3620 6841da00 2019-08-08 stsp continue;
3621 6841da00 2019-08-08 stsp return 1;
3622 6841da00 2019-08-08 stsp }
3623 6841da00 2019-08-08 stsp }
3624 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3625 6841da00 2019-08-08 stsp }
3626 6841da00 2019-08-08 stsp
3627 6841da00 2019-08-08 stsp return 0;
3628 6841da00 2019-08-08 stsp }
3629 6841da00 2019-08-08 stsp
3630 6841da00 2019-08-08 stsp static const struct got_error *
3631 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3632 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3633 6841da00 2019-08-08 stsp {
3634 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3635 6841da00 2019-08-08 stsp char *ignorespath;
3636 886cec17 2019-12-15 stsp int fd = -1;
3637 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3638 6841da00 2019-08-08 stsp
3639 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3640 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3641 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3642 6841da00 2019-08-08 stsp
3643 886cec17 2019-12-15 stsp if (dirfd != -1) {
3644 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3645 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3646 886cec17 2019-12-15 stsp if (fd == -1) {
3647 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3648 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3649 886cec17 2019-12-15 stsp ignorespath);
3650 886cec17 2019-12-15 stsp } else {
3651 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3652 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3653 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3654 886cec17 2019-12-15 stsp ignorespath);
3655 886cec17 2019-12-15 stsp else {
3656 886cec17 2019-12-15 stsp fd = -1;
3657 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3658 886cec17 2019-12-15 stsp }
3659 886cec17 2019-12-15 stsp }
3660 886cec17 2019-12-15 stsp } else {
3661 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3662 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3663 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3664 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3665 886cec17 2019-12-15 stsp ignorespath);
3666 886cec17 2019-12-15 stsp } else
3667 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3668 886cec17 2019-12-15 stsp }
3669 6841da00 2019-08-08 stsp
3670 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3671 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3672 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3673 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3674 6841da00 2019-08-08 stsp free(ignorespath);
3675 6841da00 2019-08-08 stsp return err;
3676 f8d1f275 2019-02-04 stsp }
3677 f8d1f275 2019-02-04 stsp
3678 f8d1f275 2019-02-04 stsp static const struct got_error *
3679 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3680 6092c299 2021-10-04 thomas int dirfd)
3681 f8d1f275 2019-02-04 stsp {
3682 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3683 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3684 f8d1f275 2019-02-04 stsp char *path = NULL;
3685 6092c299 2021-10-04 thomas
3686 6092c299 2021-10-04 thomas if (ignore != NULL)
3687 6092c299 2021-10-04 thomas *ignore = 0;
3688 f8d1f275 2019-02-04 stsp
3689 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3690 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3691 0584f854 2019-04-06 stsp
3692 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3693 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3694 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3695 f8d1f275 2019-02-04 stsp } else {
3696 f8d1f275 2019-02-04 stsp path = de->d_name;
3697 f8d1f275 2019-02-04 stsp }
3698 f8d1f275 2019-02-04 stsp
3699 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3700 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3701 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3702 6092c299 2021-10-04 thomas *ignore = 1;
3703 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3704 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3705 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3706 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3707 f8d1f275 2019-02-04 stsp if (parent_path[0])
3708 f8d1f275 2019-02-04 stsp free(path);
3709 b72f483a 2019-02-05 stsp return err;
3710 f8d1f275 2019-02-04 stsp }
3711 f8d1f275 2019-02-04 stsp
3712 347d1d3e 2019-07-12 stsp static const struct got_error *
3713 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3714 3143d852 2020-06-25 stsp {
3715 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3716 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3717 3143d852 2020-06-25 stsp
3718 3143d852 2020-06-25 stsp if (a->no_ignores)
3719 3143d852 2020-06-25 stsp return NULL;
3720 3143d852 2020-06-25 stsp
3721 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3722 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3723 3143d852 2020-06-25 stsp if (err)
3724 3143d852 2020-06-25 stsp return err;
3725 3143d852 2020-06-25 stsp
3726 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3727 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3728 3143d852 2020-06-25 stsp
3729 3143d852 2020-06-25 stsp return err;
3730 3143d852 2020-06-25 stsp }
3731 3143d852 2020-06-25 stsp
3732 3143d852 2020-06-25 stsp static const struct got_error *
3733 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3734 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3735 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3736 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3737 abb4604f 2019-07-27 stsp {
3738 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3739 abb4604f 2019-07-27 stsp struct stat sb;
3740 ff56836b 2021-07-08 stsp
3741 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3742 abb4604f 2019-07-27 stsp if (ie)
3743 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3744 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3745 abb4604f 2019-07-27 stsp
3746 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3747 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3748 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3749 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3750 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3751 abb4604f 2019-07-27 stsp }
3752 abb4604f 2019-07-27 stsp
3753 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3754 fe1d8685 2022-02-12 thomas return NULL;
3755 fe1d8685 2022-02-12 thomas
3756 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3757 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3758 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3759 abb4604f 2019-07-27 stsp
3760 abb4604f 2019-07-27 stsp return NULL;
3761 3143d852 2020-06-25 stsp }
3762 3143d852 2020-06-25 stsp
3763 3143d852 2020-06-25 stsp static const struct got_error *
3764 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3765 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3766 3143d852 2020-06-25 stsp {
3767 3143d852 2020-06-25 stsp const struct got_error *err;
3768 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3769 3143d852 2020-06-25 stsp
3770 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3771 3143d852 2020-06-25 stsp ".cvsignore");
3772 3143d852 2020-06-25 stsp if (err)
3773 3143d852 2020-06-25 stsp return err;
3774 3143d852 2020-06-25 stsp
3775 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3776 3143d852 2020-06-25 stsp ".gitignore");
3777 3143d852 2020-06-25 stsp if (err)
3778 3143d852 2020-06-25 stsp return err;
3779 3143d852 2020-06-25 stsp
3780 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3781 3143d852 2020-06-25 stsp if (err) {
3782 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3783 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3784 3143d852 2020-06-25 stsp return err;
3785 3143d852 2020-06-25 stsp }
3786 3143d852 2020-06-25 stsp for (;;) {
3787 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3788 3143d852 2020-06-25 stsp ".cvsignore");
3789 3143d852 2020-06-25 stsp if (err)
3790 3143d852 2020-06-25 stsp break;
3791 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3792 3143d852 2020-06-25 stsp ".gitignore");
3793 3143d852 2020-06-25 stsp if (err)
3794 3143d852 2020-06-25 stsp break;
3795 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3796 3143d852 2020-06-25 stsp if (err) {
3797 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3798 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3799 3143d852 2020-06-25 stsp break;
3800 3143d852 2020-06-25 stsp }
3801 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3802 ff56836b 2021-07-08 stsp break;
3803 b737c85a 2020-06-26 stsp free(parent_path);
3804 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3805 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3806 3143d852 2020-06-25 stsp }
3807 3143d852 2020-06-25 stsp
3808 b737c85a 2020-06-26 stsp free(parent_path);
3809 b737c85a 2020-06-26 stsp free(next_parent_path);
3810 3143d852 2020-06-25 stsp return err;
3811 abb4604f 2019-07-27 stsp }
3812 abb4604f 2019-07-27 stsp
3813 abb4604f 2019-07-27 stsp static const struct got_error *
3814 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3815 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3816 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3817 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3818 f2a9dc41 2019-12-13 tracey int report_unchanged)
3819 f8d1f275 2019-02-04 stsp {
3820 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3821 6fc93f37 2019-12-13 stsp int fd = -1;
3822 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3823 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3824 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3825 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3826 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3827 3143d852 2020-06-25 stsp
3828 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3829 f8d1f275 2019-02-04 stsp
3830 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3831 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3832 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3833 8dc303cc 2019-07-27 stsp
3834 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3835 a78810f8 2022-03-13 thomas if (ie) {
3836 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3837 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3838 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3839 a78810f8 2022-03-13 thomas goto done;
3840 a78810f8 2022-03-13 thomas }
3841 a78810f8 2022-03-13 thomas
3842 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3843 6fc93f37 2019-12-13 stsp if (fd == -1) {
3844 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3845 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3846 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3847 ff56836b 2021-07-08 stsp else {
3848 ff56836b 2021-07-08 stsp if (!no_ignores) {
3849 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3850 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3851 ff56836b 2021-07-08 stsp if (err)
3852 ff56836b 2021-07-08 stsp goto done;
3853 ff56836b 2021-07-08 stsp }
3854 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3855 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3856 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3857 ff56836b 2021-07-08 stsp }
3858 abb4604f 2019-07-27 stsp } else {
3859 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3860 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3861 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3862 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3863 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3864 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3865 abb4604f 2019-07-27 stsp arg.status_path = path;
3866 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3867 abb4604f 2019-07-27 stsp arg.repo = repo;
3868 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3869 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3870 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3871 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3872 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3873 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3874 022fae89 2019-12-06 tracey if (!no_ignores) {
3875 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3876 3143d852 2020-06-25 stsp worktree->root_path, path);
3877 3143d852 2020-06-25 stsp if (err)
3878 3143d852 2020-06-25 stsp goto done;
3879 022fae89 2019-12-06 tracey }
3880 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3881 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3882 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3883 927df6b7 2019-02-10 stsp }
3884 3143d852 2020-06-25 stsp done:
3885 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3886 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3887 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3888 927df6b7 2019-02-10 stsp free(ondisk_path);
3889 347d1d3e 2019-07-12 stsp return err;
3890 347d1d3e 2019-07-12 stsp }
3891 347d1d3e 2019-07-12 stsp
3892 347d1d3e 2019-07-12 stsp const struct got_error *
3893 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3894 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3895 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3896 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3897 347d1d3e 2019-07-12 stsp {
3898 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3899 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3900 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3901 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3902 347d1d3e 2019-07-12 stsp
3903 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3904 347d1d3e 2019-07-12 stsp if (err)
3905 347d1d3e 2019-07-12 stsp return err;
3906 347d1d3e 2019-07-12 stsp
3907 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3908 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3909 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3910 f6343036 2021-06-22 stsp no_ignores, 0);
3911 72ea6654 2019-07-27 stsp if (err)
3912 72ea6654 2019-07-27 stsp break;
3913 72ea6654 2019-07-27 stsp }
3914 f8d1f275 2019-02-04 stsp free(fileindex_path);
3915 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3916 f8d1f275 2019-02-04 stsp return err;
3917 f8d1f275 2019-02-04 stsp }
3918 6c7ab921 2019-03-18 stsp
3919 6c7ab921 2019-03-18 stsp const struct got_error *
3920 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3921 6c7ab921 2019-03-18 stsp const char *arg)
3922 6c7ab921 2019-03-18 stsp {
3923 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3924 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3925 6c7ab921 2019-03-18 stsp size_t len;
3926 00bb5ea0 2020-07-23 stsp struct stat sb;
3927 01740607 2020-11-04 stsp char *abspath = NULL;
3928 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3929 6c7ab921 2019-03-18 stsp
3930 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3931 6c7ab921 2019-03-18 stsp
3932 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3933 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3934 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3935 00bb5ea0 2020-07-23 stsp
3936 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3937 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3938 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3939 00bb5ea0 2020-07-23 stsp goto done;
3940 00bb5ea0 2020-07-23 stsp }
3941 727173c3 2020-11-06 stsp sb.st_mode = 0;
3942 00bb5ea0 2020-07-23 stsp }
3943 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3944 00bb5ea0 2020-07-23 stsp /*
3945 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3946 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3947 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3948 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3949 00bb5ea0 2020-07-23 stsp */
3950 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3951 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3952 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3953 00bb5ea0 2020-07-23 stsp goto done;
3954 00bb5ea0 2020-07-23 stsp }
3955 00bb5ea0 2020-07-23 stsp
3956 00bb5ea0 2020-07-23 stsp }
3957 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3958 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3959 00bb5ea0 2020-07-23 stsp if (err)
3960 d0710d08 2019-07-22 stsp goto done;
3961 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3962 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3963 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3964 00bb5ea0 2020-07-23 stsp goto done;
3965 00bb5ea0 2020-07-23 stsp }
3966 00bb5ea0 2020-07-23 stsp } else {
3967 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3968 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3969 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3970 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3971 00bb5ea0 2020-07-23 stsp goto done;
3972 00bb5ea0 2020-07-23 stsp }
3973 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3974 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3975 01740607 2020-11-04 stsp goto done;
3976 01740607 2020-11-04 stsp }
3977 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3978 01740607 2020-11-04 stsp sizeof(canonpath));
3979 01740607 2020-11-04 stsp if (err)
3980 00bb5ea0 2020-07-23 stsp goto done;
3981 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3982 01740607 2020-11-04 stsp if (resolved == NULL) {
3983 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3984 01740607 2020-11-04 stsp goto done;
3985 00bb5ea0 2020-07-23 stsp }
3986 d0710d08 2019-07-22 stsp }
3987 d0710d08 2019-07-22 stsp }
3988 6c7ab921 2019-03-18 stsp
3989 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3990 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3991 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3992 6c7ab921 2019-03-18 stsp goto done;
3993 6c7ab921 2019-03-18 stsp }
3994 6c7ab921 2019-03-18 stsp
3995 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3996 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3997 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3998 f6d88e1a 2019-05-29 stsp if (err)
3999 f6d88e1a 2019-05-29 stsp goto done;
4000 f6d88e1a 2019-05-29 stsp } else {
4001 f6d88e1a 2019-05-29 stsp path = strdup("");
4002 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4003 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4004 f6d88e1a 2019-05-29 stsp goto done;
4005 f6d88e1a 2019-05-29 stsp }
4006 6c7ab921 2019-03-18 stsp }
4007 6c7ab921 2019-03-18 stsp
4008 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4009 6c7ab921 2019-03-18 stsp len = strlen(path);
4010 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4011 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4012 6c7ab921 2019-03-18 stsp len--;
4013 6c7ab921 2019-03-18 stsp }
4014 6c7ab921 2019-03-18 stsp done:
4015 01740607 2020-11-04 stsp free(abspath);
4016 6c7ab921 2019-03-18 stsp free(resolved);
4017 d0710d08 2019-07-22 stsp free(cwd);
4018 6c7ab921 2019-03-18 stsp if (err == NULL)
4019 6c7ab921 2019-03-18 stsp *wt_path = path;
4020 6c7ab921 2019-03-18 stsp else
4021 6c7ab921 2019-03-18 stsp free(path);
4022 d00136be 2019-03-26 stsp return err;
4023 d00136be 2019-03-26 stsp }
4024 d00136be 2019-03-26 stsp
4025 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4026 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4027 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4028 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4029 4e68cba3 2019-11-23 stsp void *progress_arg;
4030 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4031 4e68cba3 2019-11-23 stsp };
4032 4e68cba3 2019-11-23 stsp
4033 4e68cba3 2019-11-23 stsp static const struct got_error *
4034 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4035 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4036 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4037 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4038 07f5b47a 2019-06-02 stsp {
4039 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4040 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4041 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4042 6d022e97 2019-08-04 stsp struct stat sb;
4043 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4044 07f5b47a 2019-06-02 stsp
4045 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4046 dbb83fbd 2019-12-12 stsp relpath) == -1)
4047 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4048 dbb83fbd 2019-12-12 stsp
4049 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4050 6d022e97 2019-08-04 stsp if (ie) {
4051 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4052 12463d8b 2019-12-13 stsp de_name, a->repo);
4053 6d022e97 2019-08-04 stsp if (err)
4054 dbb83fbd 2019-12-12 stsp goto done;
4055 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4056 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4057 dbb83fbd 2019-12-12 stsp goto done;
4058 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4059 dbb83fbd 2019-12-12 stsp if (err)
4060 dbb83fbd 2019-12-12 stsp goto done;
4061 6d022e97 2019-08-04 stsp }
4062 07f5b47a 2019-06-02 stsp
4063 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4064 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4065 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4066 84bf00a6 2022-02-12 thomas else
4067 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4068 dbb83fbd 2019-12-12 stsp goto done;
4069 4e68cba3 2019-11-23 stsp }
4070 07f5b47a 2019-06-02 stsp
4071 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4072 4e68cba3 2019-11-23 stsp if (err)
4073 4e68cba3 2019-11-23 stsp goto done;
4074 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4075 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4076 3969253a 2020-03-07 stsp if (err) {
4077 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4078 3969253a 2020-03-07 stsp goto done;
4079 3969253a 2020-03-07 stsp }
4080 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4081 07f5b47a 2019-06-02 stsp if (err) {
4082 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4083 4e68cba3 2019-11-23 stsp goto done;
4084 07f5b47a 2019-06-02 stsp }
4085 4e68cba3 2019-11-23 stsp done:
4086 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4087 dbb83fbd 2019-12-12 stsp if (err)
4088 dbb83fbd 2019-12-12 stsp return err;
4089 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4090 dbb83fbd 2019-12-12 stsp return NULL;
4091 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4092 07f5b47a 2019-06-02 stsp }
4093 07f5b47a 2019-06-02 stsp
4094 d00136be 2019-03-26 stsp const struct got_error *
4095 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4096 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4097 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4098 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4099 d00136be 2019-03-26 stsp {
4100 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4101 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4102 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4103 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4104 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4105 d00136be 2019-03-26 stsp
4106 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4107 d00136be 2019-03-26 stsp if (err)
4108 d00136be 2019-03-26 stsp return err;
4109 d00136be 2019-03-26 stsp
4110 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4111 d00136be 2019-03-26 stsp if (err)
4112 d00136be 2019-03-26 stsp goto done;
4113 d00136be 2019-03-26 stsp
4114 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4115 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4116 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4117 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4118 4e68cba3 2019-11-23 stsp saa.repo = repo;
4119 4e68cba3 2019-11-23 stsp
4120 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4121 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4122 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4123 1dd54920 2019-05-11 stsp if (err)
4124 af12c6b9 2019-06-04 stsp break;
4125 1dd54920 2019-05-11 stsp }
4126 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4127 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4128 af12c6b9 2019-06-04 stsp err = sync_err;
4129 d00136be 2019-03-26 stsp done:
4130 fb399478 2019-07-12 stsp free(fileindex_path);
4131 d00136be 2019-03-26 stsp if (fileindex)
4132 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4133 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4134 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4135 d00136be 2019-03-26 stsp err = unlockerr;
4136 6c7ab921 2019-03-18 stsp return err;
4137 6c7ab921 2019-03-18 stsp }
4138 17ed4618 2019-06-02 stsp
4139 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4140 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4141 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4142 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4143 f2a9dc41 2019-12-13 tracey void *progress_arg;
4144 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4145 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4146 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4147 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4148 766841c2 2020-08-13 stsp const char *status_codes;
4149 f2a9dc41 2019-12-13 tracey };
4150 f2a9dc41 2019-12-13 tracey
4151 f2a9dc41 2019-12-13 tracey static const struct got_error *
4152 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4153 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4154 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4155 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4156 17ed4618 2019-06-02 stsp {
4157 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4158 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4159 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4160 17ed4618 2019-06-02 stsp struct stat sb;
4161 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4162 d64cc78a 2022-01-25 thomas
4163 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4164 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4165 d64cc78a 2022-01-25 thomas return NULL;
4166 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4167 d64cc78a 2022-01-25 thomas }
4168 17ed4618 2019-06-02 stsp
4169 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4170 17ed4618 2019-06-02 stsp if (ie == NULL)
4171 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4172 2ec1f75b 2019-03-26 stsp
4173 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4174 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4175 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4176 9acbc4fa 2019-08-03 stsp return NULL;
4177 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4178 9acbc4fa 2019-08-03 stsp }
4179 9acbc4fa 2019-08-03 stsp
4180 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4181 f2a9dc41 2019-12-13 tracey relpath) == -1)
4182 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4183 f2a9dc41 2019-12-13 tracey
4184 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4185 12463d8b 2019-12-13 stsp a->repo);
4186 17ed4618 2019-06-02 stsp if (err)
4187 f2a9dc41 2019-12-13 tracey goto done;
4188 17ed4618 2019-06-02 stsp
4189 766841c2 2020-08-13 stsp if (a->status_codes) {
4190 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4191 766841c2 2020-08-13 stsp int i;
4192 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4193 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4194 766841c2 2020-08-13 stsp break;
4195 766841c2 2020-08-13 stsp }
4196 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4197 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4198 766841c2 2020-08-13 stsp free(ondisk_path);
4199 766841c2 2020-08-13 stsp return NULL;
4200 766841c2 2020-08-13 stsp }
4201 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4202 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4203 766841c2 2020-08-13 stsp static char msg[64];
4204 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4205 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4206 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4207 766841c2 2020-08-13 stsp goto done;
4208 766841c2 2020-08-13 stsp }
4209 766841c2 2020-08-13 stsp }
4210 766841c2 2020-08-13 stsp
4211 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4212 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4213 f2a9dc41 2019-12-13 tracey goto done;
4214 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4215 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4216 f2a9dc41 2019-12-13 tracey goto done;
4217 f2a9dc41 2019-12-13 tracey }
4218 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4219 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4220 d64cc78a 2022-01-25 thomas goto done;
4221 d64cc78a 2022-01-25 thomas }
4222 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4223 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4224 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4225 f2a9dc41 2019-12-13 tracey goto done;
4226 f2a9dc41 2019-12-13 tracey }
4227 17ed4618 2019-06-02 stsp }
4228 17ed4618 2019-06-02 stsp
4229 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4230 20a2ad1c 2020-10-20 stsp size_t root_len;
4231 20a2ad1c 2020-10-20 stsp
4232 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4233 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4234 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4235 12463d8b 2019-12-13 stsp ondisk_path);
4236 12463d8b 2019-12-13 stsp goto done;
4237 12463d8b 2019-12-13 stsp }
4238 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4239 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4240 12463d8b 2019-12-13 stsp goto done;
4241 15341bfd 2020-03-05 tracey }
4242 15341bfd 2020-03-05 tracey
4243 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4244 20a2ad1c 2020-10-20 stsp do {
4245 20a2ad1c 2020-10-20 stsp char *parent;
4246 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4247 20a2ad1c 2020-10-20 stsp if (err)
4248 2513f20a 2020-10-20 stsp goto done;
4249 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4250 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4251 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4252 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4253 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4254 20a2ad1c 2020-10-20 stsp ondisk_path);
4255 15341bfd 2020-03-05 tracey break;
4256 15341bfd 2020-03-05 tracey }
4257 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4258 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4259 f2a9dc41 2019-12-13 tracey }
4260 17ed4618 2019-06-02 stsp
4261 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4262 f2a9dc41 2019-12-13 tracey done:
4263 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4264 f2a9dc41 2019-12-13 tracey if (err)
4265 f2a9dc41 2019-12-13 tracey return err;
4266 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4267 f2a9dc41 2019-12-13 tracey return NULL;
4268 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4269 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4270 17ed4618 2019-06-02 stsp }
4271 17ed4618 2019-06-02 stsp
4272 2ec1f75b 2019-03-26 stsp const struct got_error *
4273 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4274 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4275 766841c2 2020-08-13 stsp const char *status_codes,
4276 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4277 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4278 2ec1f75b 2019-03-26 stsp {
4279 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4280 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4281 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4282 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4283 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4284 2ec1f75b 2019-03-26 stsp
4285 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4286 2ec1f75b 2019-03-26 stsp if (err)
4287 2ec1f75b 2019-03-26 stsp return err;
4288 2ec1f75b 2019-03-26 stsp
4289 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4290 2ec1f75b 2019-03-26 stsp if (err)
4291 2ec1f75b 2019-03-26 stsp goto done;
4292 f2a9dc41 2019-12-13 tracey
4293 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4294 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4295 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4296 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4297 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4298 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4299 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4300 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4301 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4302 2ec1f75b 2019-03-26 stsp
4303 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4304 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4305 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4306 17ed4618 2019-06-02 stsp if (err)
4307 af12c6b9 2019-06-04 stsp break;
4308 2ec1f75b 2019-03-26 stsp }
4309 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4310 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4311 af12c6b9 2019-06-04 stsp err = sync_err;
4312 2ec1f75b 2019-03-26 stsp done:
4313 fb399478 2019-07-12 stsp free(fileindex_path);
4314 2ec1f75b 2019-03-26 stsp if (fileindex)
4315 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4316 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4317 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4318 2ec1f75b 2019-03-26 stsp err = unlockerr;
4319 33aa809d 2019-08-08 stsp return err;
4320 33aa809d 2019-08-08 stsp }
4321 33aa809d 2019-08-08 stsp
4322 33aa809d 2019-08-08 stsp static const struct got_error *
4323 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4324 33aa809d 2019-08-08 stsp {
4325 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4326 33aa809d 2019-08-08 stsp char *line = NULL;
4327 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4328 33aa809d 2019-08-08 stsp ssize_t linelen;
4329 33aa809d 2019-08-08 stsp
4330 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4331 33aa809d 2019-08-08 stsp if (linelen == -1) {
4332 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4333 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4334 33aa809d 2019-08-08 stsp goto done;
4335 33aa809d 2019-08-08 stsp }
4336 33aa809d 2019-08-08 stsp return NULL;
4337 33aa809d 2019-08-08 stsp }
4338 33aa809d 2019-08-08 stsp if (outfile) {
4339 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4340 33aa809d 2019-08-08 stsp if (n != linelen) {
4341 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4342 33aa809d 2019-08-08 stsp goto done;
4343 33aa809d 2019-08-08 stsp }
4344 33aa809d 2019-08-08 stsp }
4345 33aa809d 2019-08-08 stsp if (rejectfile) {
4346 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4347 33aa809d 2019-08-08 stsp if (n != linelen)
4348 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4349 33aa809d 2019-08-08 stsp }
4350 33aa809d 2019-08-08 stsp done:
4351 33aa809d 2019-08-08 stsp free(line);
4352 2ec1f75b 2019-03-26 stsp return err;
4353 2ec1f75b 2019-03-26 stsp }
4354 1f1abb7e 2019-08-08 stsp
4355 33aa809d 2019-08-08 stsp static const struct got_error *
4356 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4357 33aa809d 2019-08-08 stsp {
4358 33aa809d 2019-08-08 stsp char *line = NULL;
4359 33aa809d 2019-08-08 stsp size_t linesize = 0;
4360 33aa809d 2019-08-08 stsp ssize_t linelen;
4361 33aa809d 2019-08-08 stsp
4362 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4363 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4364 500467ff 2019-09-25 hiltjo if (ferror(f))
4365 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4366 500467ff 2019-09-25 hiltjo return NULL;
4367 500467ff 2019-09-25 hiltjo }
4368 33aa809d 2019-08-08 stsp free(line);
4369 33aa809d 2019-08-08 stsp return NULL;
4370 33aa809d 2019-08-08 stsp }
4371 33aa809d 2019-08-08 stsp
4372 33aa809d 2019-08-08 stsp static const struct got_error *
4373 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4374 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4375 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4376 abc59930 2021-09-05 naddy {
4377 33aa809d 2019-08-08 stsp const struct got_error *err;
4378 33aa809d 2019-08-08 stsp
4379 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4380 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4381 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4382 33aa809d 2019-08-08 stsp if (err)
4383 33aa809d 2019-08-08 stsp return err;
4384 33aa809d 2019-08-08 stsp (*line_cur1)++;
4385 33aa809d 2019-08-08 stsp }
4386 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4387 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4388 33aa809d 2019-08-08 stsp if (rejectfile)
4389 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4390 33aa809d 2019-08-08 stsp else
4391 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4392 33aa809d 2019-08-08 stsp if (err)
4393 33aa809d 2019-08-08 stsp return err;
4394 33aa809d 2019-08-08 stsp (*line_cur2)++;
4395 33aa809d 2019-08-08 stsp }
4396 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4397 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4398 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4399 33aa809d 2019-08-08 stsp if (err)
4400 33aa809d 2019-08-08 stsp return err;
4401 33aa809d 2019-08-08 stsp (*line_cur2)++;
4402 33aa809d 2019-08-08 stsp }
4403 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4404 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4405 33aa809d 2019-08-08 stsp if (rejectfile)
4406 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4407 33aa809d 2019-08-08 stsp else
4408 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4409 33aa809d 2019-08-08 stsp if (err)
4410 33aa809d 2019-08-08 stsp return err;
4411 33aa809d 2019-08-08 stsp (*line_cur1)++;
4412 33aa809d 2019-08-08 stsp }
4413 f1e81a05 2019-08-10 stsp
4414 f1e81a05 2019-08-10 stsp return NULL;
4415 f1e81a05 2019-08-10 stsp }
4416 f1e81a05 2019-08-10 stsp
4417 f1e81a05 2019-08-10 stsp static const struct got_error *
4418 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4419 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4420 f1e81a05 2019-08-10 stsp {
4421 f1e81a05 2019-08-10 stsp const struct got_error *err;
4422 f1e81a05 2019-08-10 stsp
4423 f1e81a05 2019-08-10 stsp if (outfile) {
4424 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4425 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4426 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4427 f1e81a05 2019-08-10 stsp if (err)
4428 f1e81a05 2019-08-10 stsp return err;
4429 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4430 f1e81a05 2019-08-10 stsp }
4431 f1e81a05 2019-08-10 stsp }
4432 f1e81a05 2019-08-10 stsp if (rejectfile) {
4433 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4434 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4435 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4436 f1e81a05 2019-08-10 stsp if (err)
4437 f1e81a05 2019-08-10 stsp return err;
4438 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4439 f1e81a05 2019-08-10 stsp }
4440 33aa809d 2019-08-08 stsp }
4441 33aa809d 2019-08-08 stsp
4442 33aa809d 2019-08-08 stsp return NULL;
4443 33aa809d 2019-08-08 stsp }
4444 33aa809d 2019-08-08 stsp
4445 33aa809d 2019-08-08 stsp static const struct got_error *
4446 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4447 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4448 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4449 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4450 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4451 33aa809d 2019-08-08 stsp {
4452 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4453 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4454 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4455 33aa809d 2019-08-08 stsp FILE *hunkfile;
4456 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4457 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4458 fe621944 2020-11-10 stsp int rc;
4459 33aa809d 2019-08-08 stsp
4460 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4461 33aa809d 2019-08-08 stsp
4462 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4463 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4464 fe621944 2020-11-10 stsp start_old = cc.left.start;
4465 fe621944 2020-11-10 stsp end_old = cc.left.end;
4466 fe621944 2020-11-10 stsp start_new = cc.right.start;
4467 fe621944 2020-11-10 stsp end_new = cc.right.end;
4468 33aa809d 2019-08-08 stsp
4469 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4470 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4471 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4472 33aa809d 2019-08-08 stsp
4473 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4474 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4475 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4476 33aa809d 2019-08-08 stsp
4477 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4478 fe621944 2020-11-10 stsp if (diff_state == NULL)
4479 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4480 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4481 fe621944 2020-11-10 stsp
4482 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4483 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4484 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4485 33aa809d 2019-08-08 stsp goto done;
4486 33aa809d 2019-08-08 stsp }
4487 fe621944 2020-11-10 stsp
4488 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4489 fe621944 2020-11-10 stsp diff_result, &cc);
4490 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4491 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4492 33aa809d 2019-08-08 stsp goto done;
4493 33aa809d 2019-08-08 stsp }
4494 fe621944 2020-11-10 stsp
4495 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4496 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4497 33aa809d 2019-08-08 stsp goto done;
4498 33aa809d 2019-08-08 stsp }
4499 33aa809d 2019-08-08 stsp
4500 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4501 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4502 33aa809d 2019-08-08 stsp if (err)
4503 33aa809d 2019-08-08 stsp goto done;
4504 33aa809d 2019-08-08 stsp
4505 33aa809d 2019-08-08 stsp switch (*choice) {
4506 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4507 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4508 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4509 33aa809d 2019-08-08 stsp break;
4510 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4511 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4512 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4513 33aa809d 2019-08-08 stsp break;
4514 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4515 33aa809d 2019-08-08 stsp break;
4516 33aa809d 2019-08-08 stsp default:
4517 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4518 33aa809d 2019-08-08 stsp break;
4519 33aa809d 2019-08-08 stsp }
4520 33aa809d 2019-08-08 stsp done:
4521 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4522 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4523 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4524 33aa809d 2019-08-08 stsp return err;
4525 33aa809d 2019-08-08 stsp }
4526 33aa809d 2019-08-08 stsp
4527 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4528 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4529 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4530 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4531 1f1abb7e 2019-08-08 stsp void *progress_arg;
4532 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4533 33aa809d 2019-08-08 stsp void *patch_arg;
4534 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4535 10604dce 2021-09-24 thomas int unlink_added_files;
4536 1f1abb7e 2019-08-08 stsp };
4537 a129376b 2019-03-28 stsp
4538 e20a8b6f 2019-06-04 stsp static const struct got_error *
4539 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4540 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4541 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4542 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4543 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4544 33aa809d 2019-08-08 stsp {
4545 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4546 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4547 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4548 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4549 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4550 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4551 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4552 fe621944 2020-11-10 stsp struct stat sb2;
4553 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4554 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4555 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4556 33aa809d 2019-08-08 stsp
4557 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4558 33aa809d 2019-08-08 stsp
4559 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4560 33aa809d 2019-08-08 stsp if (err)
4561 33aa809d 2019-08-08 stsp return err;
4562 33aa809d 2019-08-08 stsp
4563 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4564 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4565 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4566 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4567 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4568 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4569 fa3cef63 2020-07-23 stsp goto done;
4570 fa3cef63 2020-07-23 stsp }
4571 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4572 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4573 48b4f239 2021-12-31 thomas if (link_len == -1) {
4574 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4575 48b4f239 2021-12-31 thomas path2);
4576 48b4f239 2021-12-31 thomas }
4577 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4578 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4579 12463d8b 2019-12-13 stsp }
4580 12463d8b 2019-12-13 stsp } else {
4581 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4582 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4583 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4584 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4585 fa3cef63 2020-07-23 stsp goto done;
4586 fa3cef63 2020-07-23 stsp }
4587 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4588 fa3cef63 2020-07-23 stsp sizeof(link_target));
4589 fa3cef63 2020-07-23 stsp if (link_len == -1)
4590 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4591 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4592 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4593 12463d8b 2019-12-13 stsp }
4594 1ebedb77 2019-10-19 stsp }
4595 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4596 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4597 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4598 fa3cef63 2020-07-23 stsp goto done;
4599 fa3cef63 2020-07-23 stsp }
4600 1ebedb77 2019-10-19 stsp
4601 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4602 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4603 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4604 fa3cef63 2020-07-23 stsp goto done;
4605 fa3cef63 2020-07-23 stsp }
4606 fa3cef63 2020-07-23 stsp fd2 = -1;
4607 fa3cef63 2020-07-23 stsp } else {
4608 fa3cef63 2020-07-23 stsp size_t n;
4609 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4610 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4611 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4612 fa3cef63 2020-07-23 stsp goto done;
4613 fa3cef63 2020-07-23 stsp }
4614 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4615 fa3cef63 2020-07-23 stsp if (n != link_len) {
4616 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4617 fa3cef63 2020-07-23 stsp goto done;
4618 fa3cef63 2020-07-23 stsp }
4619 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4620 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4621 fa3cef63 2020-07-23 stsp goto done;
4622 fa3cef63 2020-07-23 stsp }
4623 fa3cef63 2020-07-23 stsp rewind(f2);
4624 33aa809d 2019-08-08 stsp }
4625 33aa809d 2019-08-08 stsp
4626 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4627 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4628 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4629 f4ae6ddb 2022-07-01 thomas goto done;
4630 f4ae6ddb 2022-07-01 thomas }
4631 f4ae6ddb 2022-07-01 thomas
4632 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4633 33aa809d 2019-08-08 stsp if (err)
4634 33aa809d 2019-08-08 stsp goto done;
4635 33aa809d 2019-08-08 stsp
4636 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4637 33aa809d 2019-08-08 stsp if (err)
4638 33aa809d 2019-08-08 stsp goto done;
4639 33aa809d 2019-08-08 stsp
4640 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4641 33aa809d 2019-08-08 stsp if (err)
4642 33aa809d 2019-08-08 stsp goto done;
4643 33aa809d 2019-08-08 stsp
4644 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4645 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4646 33aa809d 2019-08-08 stsp if (err)
4647 33aa809d 2019-08-08 stsp goto done;
4648 33aa809d 2019-08-08 stsp
4649 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4650 fc2a50f2 2022-11-01 thomas "");
4651 33aa809d 2019-08-08 stsp if (err)
4652 33aa809d 2019-08-08 stsp goto done;
4653 33aa809d 2019-08-08 stsp
4654 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4655 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4656 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4657 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4658 fe621944 2020-11-10 stsp
4659 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4660 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4661 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4662 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4663 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4664 fe621944 2020-11-10 stsp nchanges++;
4665 fe621944 2020-11-10 stsp }
4666 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4667 33aa809d 2019-08-08 stsp int choice;
4668 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4669 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4670 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4671 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4672 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4673 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4674 33aa809d 2019-08-08 stsp if (err)
4675 33aa809d 2019-08-08 stsp goto done;
4676 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4677 33aa809d 2019-08-08 stsp have_content = 1;
4678 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4679 33aa809d 2019-08-08 stsp break;
4680 33aa809d 2019-08-08 stsp }
4681 1ebedb77 2019-10-19 stsp if (have_content) {
4682 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4683 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4684 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4685 1ebedb77 2019-10-19 stsp if (err)
4686 1ebedb77 2019-10-19 stsp goto done;
4687 1ebedb77 2019-10-19 stsp
4688 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4689 a2c162eb 2022-10-30 thomas mode_t mode;
4690 a2c162eb 2022-10-30 thomas
4691 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4692 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4693 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4694 fa3cef63 2020-07-23 stsp goto done;
4695 fa3cef63 2020-07-23 stsp }
4696 1ebedb77 2019-10-19 stsp }
4697 1ebedb77 2019-10-19 stsp }
4698 33aa809d 2019-08-08 stsp done:
4699 33aa809d 2019-08-08 stsp free(id_str);
4700 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4701 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4702 33aa809d 2019-08-08 stsp if (blob)
4703 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4704 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4705 fe621944 2020-11-10 stsp if (err == NULL)
4706 fe621944 2020-11-10 stsp err = free_err;
4707 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4708 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4709 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4710 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4711 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4712 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4713 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4714 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4715 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4716 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4717 33aa809d 2019-08-08 stsp if (err || !have_content) {
4718 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4719 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4720 33aa809d 2019-08-08 stsp free(*path_outfile);
4721 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4722 33aa809d 2019-08-08 stsp }
4723 33aa809d 2019-08-08 stsp free(path1);
4724 33aa809d 2019-08-08 stsp return err;
4725 33aa809d 2019-08-08 stsp }
4726 33aa809d 2019-08-08 stsp
4727 33aa809d 2019-08-08 stsp static const struct got_error *
4728 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4729 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4730 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4731 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4732 a129376b 2019-03-28 stsp {
4733 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4734 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4735 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4736 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4737 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4738 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4739 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4740 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4741 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4742 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4743 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4744 f4ae6ddb 2022-07-01 thomas int fd = -1;
4745 a129376b 2019-03-28 stsp
4746 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4747 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4748 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4749 d3bcc3d1 2019-08-08 stsp return NULL;
4750 3d69ad8d 2019-08-17 semarie
4751 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4752 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4753 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4754 d3bcc3d1 2019-08-08 stsp
4755 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4756 65084dad 2019-08-08 stsp if (ie == NULL)
4757 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4758 a129376b 2019-03-28 stsp
4759 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4760 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4761 a129376b 2019-03-28 stsp if (err) {
4762 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4763 a129376b 2019-03-28 stsp goto done;
4764 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4765 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4766 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4767 e20a8b6f 2019-06-04 stsp goto done;
4768 e20a8b6f 2019-06-04 stsp }
4769 a129376b 2019-03-28 stsp }
4770 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4771 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4772 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4773 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4774 a129376b 2019-03-28 stsp goto done;
4775 a129376b 2019-03-28 stsp }
4776 a129376b 2019-03-28 stsp } else {
4777 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4778 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4779 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4780 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4781 a129376b 2019-03-28 stsp goto done;
4782 a129376b 2019-03-28 stsp }
4783 a129376b 2019-03-28 stsp } else {
4784 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4785 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4786 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4787 a129376b 2019-03-28 stsp goto done;
4788 a129376b 2019-03-28 stsp }
4789 a129376b 2019-03-28 stsp }
4790 a129376b 2019-03-28 stsp }
4791 a129376b 2019-03-28 stsp
4792 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4793 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4794 945f9229 2022-04-16 thomas if (err)
4795 945f9229 2022-04-16 thomas goto done;
4796 945f9229 2022-04-16 thomas
4797 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4798 a9fa2909 2019-07-27 stsp if (err) {
4799 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4800 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4801 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4802 a9fa2909 2019-07-27 stsp goto done;
4803 a9fa2909 2019-07-27 stsp } else {
4804 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4805 a9fa2909 2019-07-27 stsp if (err)
4806 a9fa2909 2019-07-27 stsp goto done;
4807 a9fa2909 2019-07-27 stsp
4808 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4809 1233e6b6 2020-10-19 stsp if (err)
4810 a9fa2909 2019-07-27 stsp goto done;
4811 a9fa2909 2019-07-27 stsp
4812 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4813 1233e6b6 2020-10-19 stsp free(te_name);
4814 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4815 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4816 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4817 a9fa2909 2019-07-27 stsp goto done;
4818 a9fa2909 2019-07-27 stsp }
4819 a129376b 2019-03-28 stsp }
4820 a129376b 2019-03-28 stsp
4821 a129376b 2019-03-28 stsp switch (status) {
4822 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4823 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4824 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4825 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4826 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4827 33aa809d 2019-08-08 stsp if (err)
4828 33aa809d 2019-08-08 stsp goto done;
4829 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4830 33aa809d 2019-08-08 stsp break;
4831 33aa809d 2019-08-08 stsp }
4832 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4833 1f1abb7e 2019-08-08 stsp ie->path);
4834 1ee397ad 2019-07-12 stsp if (err)
4835 1ee397ad 2019-07-12 stsp goto done;
4836 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4837 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4838 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4839 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4840 10604dce 2021-09-24 thomas relpath) == -1) {
4841 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4842 10604dce 2021-09-24 thomas goto done;
4843 10604dce 2021-09-24 thomas }
4844 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4845 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4846 10604dce 2021-09-24 thomas ondisk_path);
4847 10604dce 2021-09-24 thomas break;
4848 10604dce 2021-09-24 thomas }
4849 10604dce 2021-09-24 thomas }
4850 a129376b 2019-03-28 stsp break;
4851 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4852 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4853 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4854 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4855 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4856 33aa809d 2019-08-08 stsp if (err)
4857 33aa809d 2019-08-08 stsp goto done;
4858 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4859 33aa809d 2019-08-08 stsp break;
4860 33aa809d 2019-08-08 stsp }
4861 33aa809d 2019-08-08 stsp /* fall through */
4862 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4863 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4864 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4865 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4866 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4867 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4868 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
4869 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
4870 43010591 2023-02-17 thomas else
4871 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
4872 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4873 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4874 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4875 f4ae6ddb 2022-07-01 thomas goto done;
4876 f4ae6ddb 2022-07-01 thomas }
4877 f4ae6ddb 2022-07-01 thomas
4878 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4879 a129376b 2019-03-28 stsp if (err)
4880 65084dad 2019-08-08 stsp goto done;
4881 65084dad 2019-08-08 stsp
4882 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4883 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4884 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4885 a129376b 2019-03-28 stsp goto done;
4886 65084dad 2019-08-08 stsp }
4887 65084dad 2019-08-08 stsp
4888 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4889 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4890 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4891 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4892 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4893 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4894 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4895 33aa809d 2019-08-08 stsp break;
4896 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4897 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4898 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4899 369fd7e5 2020-07-23 stsp path_content);
4900 369fd7e5 2020-07-23 stsp break;
4901 369fd7e5 2020-07-23 stsp }
4902 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4903 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4904 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4905 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4906 369fd7e5 2020-07-23 stsp } else {
4907 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4908 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4909 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4910 369fd7e5 2020-07-23 stsp goto done;
4911 369fd7e5 2020-07-23 stsp }
4912 33aa809d 2019-08-08 stsp }
4913 33aa809d 2019-08-08 stsp } else {
4914 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4915 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4916 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4917 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4918 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4919 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4920 2e1fa222 2020-07-23 stsp } else {
4921 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4922 2e1fa222 2020-07-23 stsp ie->path,
4923 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4924 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4925 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4926 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4927 2e1fa222 2020-07-23 stsp }
4928 a129376b 2019-03-28 stsp if (err)
4929 a129376b 2019-03-28 stsp goto done;
4930 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4931 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4932 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4933 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4934 437adc9d 2020-12-10 yzhong blob->id.sha1,
4935 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4936 2e1fa222 2020-07-23 stsp if (err)
4937 2e1fa222 2020-07-23 stsp goto done;
4938 2e1fa222 2020-07-23 stsp }
4939 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4940 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4941 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4942 33aa809d 2019-08-08 stsp }
4943 a129376b 2019-03-28 stsp }
4944 a129376b 2019-03-28 stsp break;
4945 e20a8b6f 2019-06-04 stsp }
4946 a129376b 2019-03-28 stsp default:
4947 1f1abb7e 2019-08-08 stsp break;
4948 a129376b 2019-03-28 stsp }
4949 a129376b 2019-03-28 stsp done:
4950 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4951 33aa809d 2019-08-08 stsp free(path_content);
4952 e20a8b6f 2019-06-04 stsp free(parent_path);
4953 a129376b 2019-03-28 stsp free(tree_path);
4954 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4955 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4956 a129376b 2019-03-28 stsp if (blob)
4957 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4958 a129376b 2019-03-28 stsp if (tree)
4959 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4960 a129376b 2019-03-28 stsp free(tree_id);
4961 945f9229 2022-04-16 thomas if (base_commit)
4962 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4963 e20a8b6f 2019-06-04 stsp return err;
4964 e20a8b6f 2019-06-04 stsp }
4965 e20a8b6f 2019-06-04 stsp
4966 e20a8b6f 2019-06-04 stsp const struct got_error *
4967 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4968 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4969 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4970 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4971 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4972 e20a8b6f 2019-06-04 stsp {
4973 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4974 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4975 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4976 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4977 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4978 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4979 e20a8b6f 2019-06-04 stsp
4980 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4981 e20a8b6f 2019-06-04 stsp if (err)
4982 e20a8b6f 2019-06-04 stsp return err;
4983 e20a8b6f 2019-06-04 stsp
4984 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4985 e20a8b6f 2019-06-04 stsp if (err)
4986 e20a8b6f 2019-06-04 stsp goto done;
4987 e20a8b6f 2019-06-04 stsp
4988 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4989 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4990 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4991 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4992 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4993 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4994 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4995 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4996 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4997 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4998 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4999 e20a8b6f 2019-06-04 stsp if (err)
5000 af12c6b9 2019-06-04 stsp break;
5001 e20a8b6f 2019-06-04 stsp }
5002 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5003 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5004 e20a8b6f 2019-06-04 stsp err = sync_err;
5005 af12c6b9 2019-06-04 stsp done:
5006 fb399478 2019-07-12 stsp free(fileindex_path);
5007 a129376b 2019-03-28 stsp if (fileindex)
5008 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5009 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5010 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5011 a129376b 2019-03-28 stsp err = unlockerr;
5012 c4296144 2019-05-09 stsp return err;
5013 c4296144 2019-05-09 stsp }
5014 c4296144 2019-05-09 stsp
5015 cf066bf8 2019-05-09 stsp static void
5016 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5017 cf066bf8 2019-05-09 stsp {
5018 24519714 2019-05-09 stsp free(ct->path);
5019 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5020 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5021 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5022 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5023 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5024 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5025 cf066bf8 2019-05-09 stsp free(ct);
5026 cf066bf8 2019-05-09 stsp }
5027 24519714 2019-05-09 stsp
5028 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5029 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5030 24519714 2019-05-09 stsp struct got_repository *repo;
5031 24519714 2019-05-09 stsp struct got_worktree *worktree;
5032 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5033 5f8a88c6 2019-08-03 stsp int have_staged_files;
5034 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5035 ef899790 2022-11-01 thomas int diff_header_shown;
5036 be94c032 2023-02-20 thomas int commit_conflicts;
5037 ef899790 2022-11-01 thomas FILE *diff_outfile;
5038 ef899790 2022-11-01 thomas FILE *f1;
5039 ef899790 2022-11-01 thomas FILE *f2;
5040 24519714 2019-05-09 stsp };
5041 ef899790 2022-11-01 thomas
5042 ef899790 2022-11-01 thomas /*
5043 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5044 ef899790 2022-11-01 thomas * it as content to the diff engine.
5045 ef899790 2022-11-01 thomas */
5046 ef899790 2022-11-01 thomas static const struct got_error *
5047 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5048 ef899790 2022-11-01 thomas const char *abspath)
5049 ef899790 2022-11-01 thomas {
5050 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5051 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5052 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5053 ef899790 2022-11-01 thomas
5054 ef899790 2022-11-01 thomas *fd = -1;
5055 ef899790 2022-11-01 thomas
5056 ef899790 2022-11-01 thomas if (dirfd != -1) {
5057 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5058 ef899790 2022-11-01 thomas if (target_len == -1)
5059 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5060 ef899790 2022-11-01 thomas } else {
5061 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5062 ef899790 2022-11-01 thomas if (target_len == -1)
5063 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5064 ef899790 2022-11-01 thomas }
5065 ef899790 2022-11-01 thomas
5066 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5067 ef899790 2022-11-01 thomas if (*fd == -1)
5068 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5069 ef899790 2022-11-01 thomas
5070 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5071 ef899790 2022-11-01 thomas if (outlen == -1) {
5072 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5073 ef899790 2022-11-01 thomas goto done;
5074 ef899790 2022-11-01 thomas }
5075 ef899790 2022-11-01 thomas
5076 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5077 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5078 ef899790 2022-11-01 thomas goto done;
5079 ef899790 2022-11-01 thomas }
5080 ef899790 2022-11-01 thomas done:
5081 ef899790 2022-11-01 thomas if (err) {
5082 ef899790 2022-11-01 thomas close(*fd);
5083 ef899790 2022-11-01 thomas *fd = -1;
5084 ef899790 2022-11-01 thomas }
5085 ef899790 2022-11-01 thomas return err;
5086 ef899790 2022-11-01 thomas }
5087 ef899790 2022-11-01 thomas
5088 ef899790 2022-11-01 thomas static const struct got_error *
5089 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5090 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5091 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5092 ef899790 2022-11-01 thomas {
5093 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5094 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5095 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5096 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5097 ef899790 2022-11-01 thomas char *label1 = NULL;
5098 ef899790 2022-11-01 thomas struct stat sb;
5099 ef899790 2022-11-01 thomas off_t size1 = 0;
5100 ef899790 2022-11-01 thomas int f2_exists = 0;
5101 ef899790 2022-11-01 thomas char *id_str = NULL;
5102 ef899790 2022-11-01 thomas
5103 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5104 d259e491 2022-11-01 thomas
5105 d259e491 2022-11-01 thomas if (diff_staged) {
5106 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5107 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5108 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5109 d259e491 2022-11-01 thomas return NULL;
5110 d259e491 2022-11-01 thomas } else {
5111 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5112 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5113 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5114 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5115 d259e491 2022-11-01 thomas return NULL;
5116 d259e491 2022-11-01 thomas }
5117 ef899790 2022-11-01 thomas
5118 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5119 ef899790 2022-11-01 thomas if (err)
5120 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5121 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5122 ef899790 2022-11-01 thomas if (err)
5123 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5124 ef899790 2022-11-01 thomas
5125 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5126 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5127 ef899790 2022-11-01 thomas if (err)
5128 ef899790 2022-11-01 thomas return err;
5129 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5130 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5131 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5132 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5133 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5134 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5135 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5136 ef899790 2022-11-01 thomas }
5137 ef899790 2022-11-01 thomas
5138 ef899790 2022-11-01 thomas if (diff_staged) {
5139 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5140 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5141 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5142 ef899790 2022-11-01 thomas label1 = ct->path;
5143 ef899790 2022-11-01 thomas label2 = ct->path;
5144 ef899790 2022-11-01 thomas break;
5145 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5146 ef899790 2022-11-01 thomas label2 = ct->path;
5147 ef899790 2022-11-01 thomas break;
5148 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5149 ef899790 2022-11-01 thomas label1 = ct->path;
5150 ef899790 2022-11-01 thomas break;
5151 ef899790 2022-11-01 thomas default:
5152 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5153 ef899790 2022-11-01 thomas }
5154 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5155 ef899790 2022-11-01 thomas if (fd1 == -1) {
5156 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5157 ef899790 2022-11-01 thomas goto done;
5158 ef899790 2022-11-01 thomas }
5159 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5160 ef899790 2022-11-01 thomas if (fd2 == -1) {
5161 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5162 ef899790 2022-11-01 thomas goto done;
5163 ef899790 2022-11-01 thomas }
5164 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5165 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5166 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5167 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5168 ef899790 2022-11-01 thomas goto done;
5169 ef899790 2022-11-01 thomas }
5170 ef899790 2022-11-01 thomas
5171 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5172 ef899790 2022-11-01 thomas if (fd1 == -1) {
5173 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5174 ef899790 2022-11-01 thomas goto done;
5175 ef899790 2022-11-01 thomas }
5176 ef899790 2022-11-01 thomas
5177 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5178 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5179 ef899790 2022-11-01 thomas 8192, fd1);
5180 ef899790 2022-11-01 thomas if (err)
5181 ef899790 2022-11-01 thomas goto done;
5182 ef899790 2022-11-01 thomas }
5183 ef899790 2022-11-01 thomas
5184 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5185 ef899790 2022-11-01 thomas if (dirfd != -1) {
5186 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5187 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5188 ef899790 2022-11-01 thomas if (fd == -1) {
5189 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5190 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5191 ef899790 2022-11-01 thomas ct->ondisk_path);
5192 ef899790 2022-11-01 thomas goto done;
5193 ef899790 2022-11-01 thomas }
5194 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5195 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5196 ef899790 2022-11-01 thomas if (err)
5197 ef899790 2022-11-01 thomas goto done;
5198 ef899790 2022-11-01 thomas }
5199 ef899790 2022-11-01 thomas } else {
5200 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5201 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5202 ef899790 2022-11-01 thomas if (fd == -1) {
5203 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5204 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5205 ef899790 2022-11-01 thomas ct->ondisk_path);
5206 ef899790 2022-11-01 thomas goto done;
5207 ef899790 2022-11-01 thomas }
5208 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5209 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5210 ef899790 2022-11-01 thomas if (err)
5211 ef899790 2022-11-01 thomas goto done;
5212 ef899790 2022-11-01 thomas }
5213 ef899790 2022-11-01 thomas }
5214 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5215 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5216 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5217 ef899790 2022-11-01 thomas goto done;
5218 ef899790 2022-11-01 thomas }
5219 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5220 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5221 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5222 ef899790 2022-11-01 thomas goto done;
5223 ef899790 2022-11-01 thomas }
5224 ef899790 2022-11-01 thomas fd = -1;
5225 ef899790 2022-11-01 thomas f2_exists = 1;
5226 ef899790 2022-11-01 thomas }
5227 ef899790 2022-11-01 thomas
5228 ef899790 2022-11-01 thomas if (blob1) {
5229 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5230 ef899790 2022-11-01 thomas f1, blob1);
5231 ef899790 2022-11-01 thomas if (err)
5232 ef899790 2022-11-01 thomas goto done;
5233 ef899790 2022-11-01 thomas }
5234 ef899790 2022-11-01 thomas
5235 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5236 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5237 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5238 ef899790 2022-11-01 thomas done:
5239 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5240 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5241 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5242 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5243 ef899790 2022-11-01 thomas if (blob1)
5244 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5245 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5246 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5247 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5248 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5249 ef899790 2022-11-01 thomas return err;
5250 ef899790 2022-11-01 thomas }
5251 cf066bf8 2019-05-09 stsp
5252 c4296144 2019-05-09 stsp static const struct got_error *
5253 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5254 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5255 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5256 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5257 c4296144 2019-05-09 stsp {
5258 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5259 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5260 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5261 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5262 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5263 768aea60 2019-05-09 stsp struct stat sb;
5264 c4296144 2019-05-09 stsp
5265 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5266 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5267 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5268 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5269 5f8a88c6 2019-08-03 stsp return NULL;
5270 5f8a88c6 2019-08-03 stsp } else {
5271 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5272 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5273 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5274 be94c032 2023-02-20 thomas }
5275 c4296144 2019-05-09 stsp
5276 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5277 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5278 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5279 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5280 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5281 5f8a88c6 2019-08-03 stsp return NULL;
5282 5f8a88c6 2019-08-03 stsp }
5283 0b5cc0d6 2019-05-09 stsp
5284 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5285 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5286 036813ee 2019-05-09 stsp goto done;
5287 036813ee 2019-05-09 stsp }
5288 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5289 036813ee 2019-05-09 stsp parent_path = strdup("");
5290 69960a46 2019-05-09 stsp if (parent_path == NULL)
5291 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5292 69960a46 2019-05-09 stsp } else {
5293 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5294 69960a46 2019-05-09 stsp if (err)
5295 69960a46 2019-05-09 stsp return err;
5296 69960a46 2019-05-09 stsp }
5297 c4296144 2019-05-09 stsp
5298 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5299 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5300 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5301 c4296144 2019-05-09 stsp goto done;
5302 768aea60 2019-05-09 stsp }
5303 768aea60 2019-05-09 stsp
5304 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5305 768aea60 2019-05-09 stsp relpath) == -1) {
5306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5307 768aea60 2019-05-09 stsp goto done;
5308 768aea60 2019-05-09 stsp }
5309 0aeb8099 2020-07-23 stsp
5310 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5311 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5312 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5313 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5314 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5315 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5316 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5317 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5318 0aeb8099 2020-07-23 stsp break;
5319 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5320 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5321 0aeb8099 2020-07-23 stsp break;
5322 0aeb8099 2020-07-23 stsp default:
5323 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5324 0aeb8099 2020-07-23 stsp goto done;
5325 0aeb8099 2020-07-23 stsp }
5326 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5327 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5328 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5329 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5330 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5331 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5332 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5333 12463d8b 2019-12-13 stsp ct->ondisk_path);
5334 12463d8b 2019-12-13 stsp goto done;
5335 12463d8b 2019-12-13 stsp }
5336 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5337 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5338 768aea60 2019-05-09 stsp goto done;
5339 768aea60 2019-05-09 stsp }
5340 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5341 c4296144 2019-05-09 stsp }
5342 c4296144 2019-05-09 stsp
5343 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5344 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5345 44d03001 2019-05-09 stsp relpath) == -1) {
5346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5347 44d03001 2019-05-09 stsp goto done;
5348 44d03001 2019-05-09 stsp }
5349 44d03001 2019-05-09 stsp
5350 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5351 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5352 35213c7c 2020-07-23 stsp int is_bad_symlink;
5353 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5354 35213c7c 2020-07-23 stsp ssize_t target_len;
5355 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5356 35213c7c 2020-07-23 stsp sizeof(target_path));
5357 35213c7c 2020-07-23 stsp if (target_len == -1) {
5358 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5359 35213c7c 2020-07-23 stsp ct->ondisk_path);
5360 35213c7c 2020-07-23 stsp goto done;
5361 35213c7c 2020-07-23 stsp }
5362 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5363 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5364 35213c7c 2020-07-23 stsp if (err)
5365 35213c7c 2020-07-23 stsp goto done;
5366 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5367 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5368 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5369 35213c7c 2020-07-23 stsp goto done;
5370 35213c7c 2020-07-23 stsp }
5371 35213c7c 2020-07-23 stsp }
5372 35213c7c 2020-07-23 stsp
5373 35213c7c 2020-07-23 stsp
5374 cf066bf8 2019-05-09 stsp ct->status = status;
5375 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5376 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5377 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5378 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5379 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5380 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5381 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5382 b416585c 2019-05-13 stsp goto done;
5383 b416585c 2019-05-13 stsp }
5384 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5385 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5386 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5387 f0b75401 2019-08-03 stsp goto done;
5388 f0b75401 2019-08-03 stsp }
5389 f0b75401 2019-08-03 stsp }
5390 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5391 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5392 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5393 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5394 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5395 036813ee 2019-05-09 stsp goto done;
5396 036813ee 2019-05-09 stsp }
5397 ca2503ea 2019-05-09 stsp }
5398 24519714 2019-05-09 stsp ct->path = strdup(path);
5399 24519714 2019-05-09 stsp if (ct->path == NULL) {
5400 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5401 24519714 2019-05-09 stsp goto done;
5402 24519714 2019-05-09 stsp }
5403 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5404 ef899790 2022-11-01 thomas if (err)
5405 ef899790 2022-11-01 thomas goto done;
5406 ef899790 2022-11-01 thomas
5407 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5408 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5409 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5410 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5411 ef899790 2022-11-01 thomas if (err)
5412 ef899790 2022-11-01 thomas goto done;
5413 ef899790 2022-11-01 thomas }
5414 c4296144 2019-05-09 stsp done:
5415 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5416 ed175427 2019-05-09 stsp free_commitable(ct);
5417 24519714 2019-05-09 stsp free(parent_path);
5418 036813ee 2019-05-09 stsp free(path);
5419 a129376b 2019-03-28 stsp return err;
5420 a129376b 2019-03-28 stsp }
5421 c4296144 2019-05-09 stsp
5422 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5423 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5424 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5425 036813ee 2019-05-09 stsp struct got_repository *);
5426 ed175427 2019-05-09 stsp
5427 ed175427 2019-05-09 stsp static const struct got_error *
5428 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5429 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5430 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5431 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5432 afa376bf 2019-05-09 stsp struct got_repository *repo)
5433 ed175427 2019-05-09 stsp {
5434 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5435 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5436 036813ee 2019-05-09 stsp char *subpath;
5437 ed175427 2019-05-09 stsp
5438 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5439 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5440 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5441 ed175427 2019-05-09 stsp
5442 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5443 ed175427 2019-05-09 stsp if (err)
5444 ed175427 2019-05-09 stsp return err;
5445 ed175427 2019-05-09 stsp
5446 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5447 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5448 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5449 036813ee 2019-05-09 stsp free(subpath);
5450 036813ee 2019-05-09 stsp return err;
5451 036813ee 2019-05-09 stsp }
5452 ed175427 2019-05-09 stsp
5453 036813ee 2019-05-09 stsp static const struct got_error *
5454 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5455 036813ee 2019-05-09 stsp {
5456 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5457 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5458 ed175427 2019-05-09 stsp
5459 036813ee 2019-05-09 stsp *match = 0;
5460 ed175427 2019-05-09 stsp
5461 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5462 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5463 0f63689d 2019-05-10 stsp return NULL;
5464 036813ee 2019-05-09 stsp }
5465 0b5cc0d6 2019-05-09 stsp
5466 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5467 0f63689d 2019-05-10 stsp if (err)
5468 0f63689d 2019-05-10 stsp return err;
5469 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5470 036813ee 2019-05-09 stsp free(ct_parent_path);
5471 036813ee 2019-05-09 stsp return err;
5472 036813ee 2019-05-09 stsp }
5473 0b5cc0d6 2019-05-09 stsp
5474 768aea60 2019-05-09 stsp static mode_t
5475 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5476 768aea60 2019-05-09 stsp {
5477 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5478 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5479 3d9a4ec4 2020-07-23 stsp
5480 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5481 768aea60 2019-05-09 stsp }
5482 768aea60 2019-05-09 stsp
5483 036813ee 2019-05-09 stsp static const struct got_error *
5484 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5485 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5486 036813ee 2019-05-09 stsp {
5487 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5488 ca2503ea 2019-05-09 stsp
5489 036813ee 2019-05-09 stsp *new_te = NULL;
5490 0b5cc0d6 2019-05-09 stsp
5491 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5492 036813ee 2019-05-09 stsp if (err)
5493 036813ee 2019-05-09 stsp goto done;
5494 0b5cc0d6 2019-05-09 stsp
5495 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5496 036813ee 2019-05-09 stsp
5497 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5498 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5499 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5500 5f8a88c6 2019-08-03 stsp else
5501 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5502 036813ee 2019-05-09 stsp done:
5503 036813ee 2019-05-09 stsp if (err && *new_te) {
5504 56e0773d 2019-11-28 stsp free(*new_te);
5505 036813ee 2019-05-09 stsp *new_te = NULL;
5506 036813ee 2019-05-09 stsp }
5507 036813ee 2019-05-09 stsp return err;
5508 036813ee 2019-05-09 stsp }
5509 036813ee 2019-05-09 stsp
5510 036813ee 2019-05-09 stsp static const struct got_error *
5511 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5512 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5513 036813ee 2019-05-09 stsp {
5514 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5515 102b254e 2020-10-19 stsp char *ct_name = NULL;
5516 036813ee 2019-05-09 stsp
5517 036813ee 2019-05-09 stsp *new_te = NULL;
5518 036813ee 2019-05-09 stsp
5519 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5520 036813ee 2019-05-09 stsp if (*new_te == NULL)
5521 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5522 036813ee 2019-05-09 stsp
5523 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5524 102b254e 2020-10-19 stsp if (err)
5525 036813ee 2019-05-09 stsp goto done;
5526 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5527 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5528 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5529 036813ee 2019-05-09 stsp goto done;
5530 036813ee 2019-05-09 stsp }
5531 036813ee 2019-05-09 stsp
5532 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5533 036813ee 2019-05-09 stsp
5534 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5535 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5536 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5537 5f8a88c6 2019-08-03 stsp else
5538 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5539 036813ee 2019-05-09 stsp done:
5540 102b254e 2020-10-19 stsp free(ct_name);
5541 036813ee 2019-05-09 stsp if (err && *new_te) {
5542 56e0773d 2019-11-28 stsp free(*new_te);
5543 036813ee 2019-05-09 stsp *new_te = NULL;
5544 036813ee 2019-05-09 stsp }
5545 036813ee 2019-05-09 stsp return err;
5546 036813ee 2019-05-09 stsp }
5547 036813ee 2019-05-09 stsp
5548 036813ee 2019-05-09 stsp static const struct got_error *
5549 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5550 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5551 036813ee 2019-05-09 stsp {
5552 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5553 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5554 036813ee 2019-05-09 stsp
5555 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5556 036813ee 2019-05-09 stsp if (err)
5557 036813ee 2019-05-09 stsp return err;
5558 036813ee 2019-05-09 stsp if (new_pe == NULL)
5559 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5560 036813ee 2019-05-09 stsp return NULL;
5561 afa376bf 2019-05-09 stsp }
5562 afa376bf 2019-05-09 stsp
5563 afa376bf 2019-05-09 stsp static const struct got_error *
5564 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5565 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5566 afa376bf 2019-05-09 stsp {
5567 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5568 5f8a88c6 2019-08-03 stsp unsigned char status;
5569 ae1e948a 2021-09-28 thomas
5570 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5571 ae1e948a 2021-09-28 thomas return NULL;
5572 5f8a88c6 2019-08-03 stsp
5573 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5574 afa376bf 2019-05-09 stsp ct_path++;
5575 5f8a88c6 2019-08-03 stsp
5576 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5577 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5578 5f8a88c6 2019-08-03 stsp else
5579 5f8a88c6 2019-08-03 stsp status = ct->status;
5580 5f8a88c6 2019-08-03 stsp
5581 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5582 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5583 036813ee 2019-05-09 stsp }
5584 036813ee 2019-05-09 stsp
5585 036813ee 2019-05-09 stsp static const struct got_error *
5586 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5587 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5588 44d03001 2019-05-09 stsp {
5589 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5590 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5591 44d03001 2019-05-09 stsp char *te_path;
5592 44d03001 2019-05-09 stsp
5593 44d03001 2019-05-09 stsp *modified = 0;
5594 44d03001 2019-05-09 stsp
5595 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5596 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5597 44d03001 2019-05-09 stsp te->name) == -1)
5598 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5599 44d03001 2019-05-09 stsp
5600 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5601 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5602 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5603 44d03001 2019-05-09 stsp strlen(te_path));
5604 62d463ca 2020-10-20 naddy if (*modified)
5605 44d03001 2019-05-09 stsp break;
5606 44d03001 2019-05-09 stsp }
5607 44d03001 2019-05-09 stsp
5608 44d03001 2019-05-09 stsp free(te_path);
5609 44d03001 2019-05-09 stsp return err;
5610 44d03001 2019-05-09 stsp }
5611 44d03001 2019-05-09 stsp
5612 44d03001 2019-05-09 stsp static const struct got_error *
5613 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5614 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5615 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5616 036813ee 2019-05-09 stsp {
5617 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5618 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5619 036813ee 2019-05-09 stsp
5620 036813ee 2019-05-09 stsp *ctp = NULL;
5621 036813ee 2019-05-09 stsp
5622 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5623 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5624 036813ee 2019-05-09 stsp char *ct_name = NULL;
5625 036813ee 2019-05-09 stsp int path_matches;
5626 036813ee 2019-05-09 stsp
5627 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5628 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5629 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5630 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5631 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5632 5f8a88c6 2019-08-03 stsp continue;
5633 5f8a88c6 2019-08-03 stsp } else {
5634 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5635 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5636 5f8a88c6 2019-08-03 stsp continue;
5637 5f8a88c6 2019-08-03 stsp }
5638 036813ee 2019-05-09 stsp
5639 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5640 036813ee 2019-05-09 stsp continue;
5641 036813ee 2019-05-09 stsp
5642 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5643 62d463ca 2020-10-20 naddy if (err)
5644 036813ee 2019-05-09 stsp return err;
5645 036813ee 2019-05-09 stsp if (!path_matches)
5646 036813ee 2019-05-09 stsp continue;
5647 036813ee 2019-05-09 stsp
5648 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5649 d34b633e 2020-10-19 stsp if (err)
5650 d34b633e 2020-10-19 stsp return err;
5651 d34b633e 2020-10-19 stsp
5652 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5653 d34b633e 2020-10-19 stsp free(ct_name);
5654 036813ee 2019-05-09 stsp continue;
5655 d34b633e 2020-10-19 stsp }
5656 d34b633e 2020-10-19 stsp free(ct_name);
5657 036813ee 2019-05-09 stsp
5658 036813ee 2019-05-09 stsp *ctp = ct;
5659 036813ee 2019-05-09 stsp break;
5660 036813ee 2019-05-09 stsp }
5661 2b496619 2019-07-10 stsp
5662 2b496619 2019-07-10 stsp return err;
5663 2b496619 2019-07-10 stsp }
5664 2b496619 2019-07-10 stsp
5665 2b496619 2019-07-10 stsp static const struct got_error *
5666 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5667 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5668 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5669 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5670 2b496619 2019-07-10 stsp struct got_repository *repo)
5671 2b496619 2019-07-10 stsp {
5672 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5673 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5674 2b496619 2019-07-10 stsp char *subtree_path;
5675 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5676 ba580f68 2020-03-22 stsp int nentries;
5677 2b496619 2019-07-10 stsp
5678 2b496619 2019-07-10 stsp *new_tep = NULL;
5679 2b496619 2019-07-10 stsp
5680 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5681 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5682 2b496619 2019-07-10 stsp child_path) == -1)
5683 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5684 036813ee 2019-05-09 stsp
5685 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5686 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5687 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5688 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5689 56e0773d 2019-11-28 stsp
5690 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5691 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5692 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5693 2b496619 2019-07-10 stsp goto done;
5694 2b496619 2019-07-10 stsp }
5695 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5696 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5697 2b496619 2019-07-10 stsp if (err) {
5698 56e0773d 2019-11-28 stsp free(new_te);
5699 2b496619 2019-07-10 stsp goto done;
5700 2b496619 2019-07-10 stsp }
5701 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5702 2b496619 2019-07-10 stsp done:
5703 56e0773d 2019-11-28 stsp free(id);
5704 2b496619 2019-07-10 stsp free(subtree_path);
5705 2b496619 2019-07-10 stsp if (err == NULL)
5706 2b496619 2019-07-10 stsp *new_tep = new_te;
5707 036813ee 2019-05-09 stsp return err;
5708 036813ee 2019-05-09 stsp }
5709 036813ee 2019-05-09 stsp
5710 036813ee 2019-05-09 stsp static const struct got_error *
5711 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5712 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5713 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5714 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5715 036813ee 2019-05-09 stsp struct got_repository *repo)
5716 036813ee 2019-05-09 stsp {
5717 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5718 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5719 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5720 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5721 036813ee 2019-05-09 stsp
5722 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5723 ba580f68 2020-03-22 stsp *nentries = 0;
5724 036813ee 2019-05-09 stsp
5725 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5726 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5727 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5728 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5729 036813ee 2019-05-09 stsp
5730 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5731 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5732 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5733 036813ee 2019-05-09 stsp continue;
5734 036813ee 2019-05-09 stsp
5735 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5736 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5737 036813ee 2019-05-09 stsp continue;
5738 036813ee 2019-05-09 stsp
5739 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5740 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5741 036813ee 2019-05-09 stsp if (err)
5742 036813ee 2019-05-09 stsp goto done;
5743 036813ee 2019-05-09 stsp
5744 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5745 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5746 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5747 036813ee 2019-05-09 stsp if (err)
5748 036813ee 2019-05-09 stsp goto done;
5749 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5750 afa376bf 2019-05-09 stsp if (err)
5751 afa376bf 2019-05-09 stsp goto done;
5752 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5753 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5754 2b496619 2019-07-10 stsp if (err)
5755 2f51b5b3 2019-05-09 stsp goto done;
5756 ba580f68 2020-03-22 stsp (*nentries)++;
5757 2b496619 2019-07-10 stsp } else {
5758 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5759 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5760 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5761 2b496619 2019-07-10 stsp == NULL) {
5762 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5763 2b496619 2019-07-10 stsp child_path, path_base_tree,
5764 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5765 2b496619 2019-07-10 stsp repo);
5766 2b496619 2019-07-10 stsp if (err)
5767 2b496619 2019-07-10 stsp goto done;
5768 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5769 2b496619 2019-07-10 stsp if (err)
5770 2b496619 2019-07-10 stsp goto done;
5771 ba580f68 2020-03-22 stsp (*nentries)++;
5772 9ba0479c 2019-05-10 stsp }
5773 ed175427 2019-05-09 stsp }
5774 2f51b5b3 2019-05-09 stsp }
5775 2f51b5b3 2019-05-09 stsp
5776 2f51b5b3 2019-05-09 stsp if (base_tree) {
5777 56e0773d 2019-11-28 stsp int i, nbase_entries;
5778 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5779 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5780 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5781 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5782 63c5ca5d 2019-08-24 stsp
5783 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5784 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5785 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5786 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5787 63c5ca5d 2019-08-24 stsp if (err)
5788 63c5ca5d 2019-08-24 stsp goto done;
5789 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5790 63c5ca5d 2019-08-24 stsp if (err)
5791 63c5ca5d 2019-08-24 stsp goto done;
5792 ba580f68 2020-03-22 stsp (*nentries)++;
5793 63c5ca5d 2019-08-24 stsp continue;
5794 63c5ca5d 2019-08-24 stsp }
5795 2f51b5b3 2019-05-09 stsp
5796 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5797 44d03001 2019-05-09 stsp int modified;
5798 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5799 036813ee 2019-05-09 stsp if (err)
5800 036813ee 2019-05-09 stsp goto done;
5801 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5802 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5803 2f51b5b3 2019-05-09 stsp if (err)
5804 2f51b5b3 2019-05-09 stsp goto done;
5805 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5806 44d03001 2019-05-09 stsp if (modified) {
5807 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5808 ba580f68 2020-03-22 stsp int nsubentries;
5809 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5810 ba580f68 2020-03-22 stsp &nsubentries, te,
5811 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5812 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5813 44d03001 2019-05-09 stsp if (err)
5814 44d03001 2019-05-09 stsp goto done;
5815 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5816 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5817 ba580f68 2020-03-22 stsp free(new_id);
5818 ba580f68 2020-03-22 stsp continue;
5819 ba580f68 2020-03-22 stsp }
5820 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5821 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5822 56e0773d 2019-11-28 stsp free(new_id);
5823 44d03001 2019-05-09 stsp }
5824 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5825 036813ee 2019-05-09 stsp if (err)
5826 036813ee 2019-05-09 stsp goto done;
5827 ba580f68 2020-03-22 stsp (*nentries)++;
5828 2f51b5b3 2019-05-09 stsp continue;
5829 036813ee 2019-05-09 stsp }
5830 2f51b5b3 2019-05-09 stsp
5831 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5832 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5833 f66c734c 2019-09-22 stsp if (err)
5834 f66c734c 2019-09-22 stsp goto done;
5835 2f51b5b3 2019-05-09 stsp if (ct) {
5836 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5837 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5838 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5839 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
5840 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5841 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5842 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5843 2f51b5b3 2019-05-09 stsp if (err)
5844 2f51b5b3 2019-05-09 stsp goto done;
5845 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5846 2f51b5b3 2019-05-09 stsp if (err)
5847 2f51b5b3 2019-05-09 stsp goto done;
5848 ba580f68 2020-03-22 stsp (*nentries)++;
5849 2f51b5b3 2019-05-09 stsp }
5850 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5851 b416585c 2019-05-13 stsp status_arg);
5852 afa376bf 2019-05-09 stsp if (err)
5853 afa376bf 2019-05-09 stsp goto done;
5854 2f51b5b3 2019-05-09 stsp } else {
5855 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5856 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5857 2f51b5b3 2019-05-09 stsp if (err)
5858 2f51b5b3 2019-05-09 stsp goto done;
5859 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5860 2f51b5b3 2019-05-09 stsp if (err)
5861 2f51b5b3 2019-05-09 stsp goto done;
5862 ba580f68 2020-03-22 stsp (*nentries)++;
5863 2f51b5b3 2019-05-09 stsp }
5864 ca2503ea 2019-05-09 stsp }
5865 ed175427 2019-05-09 stsp }
5866 0b5cc0d6 2019-05-09 stsp
5867 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5868 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5869 ed175427 2019-05-09 stsp done:
5870 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5871 2e1fa222 2020-07-23 stsp return err;
5872 2e1fa222 2020-07-23 stsp }
5873 2e1fa222 2020-07-23 stsp
5874 2e1fa222 2020-07-23 stsp static const struct got_error *
5875 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5876 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5877 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5878 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5879 ebf99748 2019-05-09 stsp {
5880 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5881 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5882 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5883 ebf99748 2019-05-09 stsp
5884 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5885 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5886 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5887 ebf99748 2019-05-09 stsp
5888 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5889 437adc9d 2020-12-10 yzhong
5890 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5891 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5892 437adc9d 2020-12-10 yzhong if (err)
5893 437adc9d 2020-12-10 yzhong goto done;
5894 437adc9d 2020-12-10 yzhong
5895 ebf99748 2019-05-09 stsp if (ie) {
5896 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5897 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5898 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5899 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5900 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5901 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5902 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5903 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5904 437adc9d 2020-12-10 yzhong
5905 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5906 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5907 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5908 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5909 72fd46fa 2019-09-06 stsp !have_staged_files);
5910 ebf99748 2019-05-09 stsp } else
5911 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5912 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5913 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5914 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5915 72fd46fa 2019-09-06 stsp !have_staged_files);
5916 ebf99748 2019-05-09 stsp } else {
5917 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5918 ebf99748 2019-05-09 stsp if (err)
5919 437adc9d 2020-12-10 yzhong goto done;
5920 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5921 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5922 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5923 3969253a 2020-03-07 stsp if (err) {
5924 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5925 437adc9d 2020-12-10 yzhong goto done;
5926 3969253a 2020-03-07 stsp }
5927 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5928 3969253a 2020-03-07 stsp if (err) {
5929 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5930 437adc9d 2020-12-10 yzhong goto done;
5931 3969253a 2020-03-07 stsp }
5932 ebf99748 2019-05-09 stsp }
5933 437adc9d 2020-12-10 yzhong free(relpath);
5934 437adc9d 2020-12-10 yzhong relpath = NULL;
5935 ebf99748 2019-05-09 stsp }
5936 437adc9d 2020-12-10 yzhong done:
5937 437adc9d 2020-12-10 yzhong free(relpath);
5938 d56d26ce 2019-05-10 stsp return err;
5939 d56d26ce 2019-05-10 stsp }
5940 735ef5ac 2019-08-03 stsp
5941 d56d26ce 2019-05-10 stsp
5942 d56d26ce 2019-05-10 stsp static const struct got_error *
5943 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5944 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5945 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5946 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5947 735ef5ac 2019-08-03 stsp int ood_errcode)
5948 d56d26ce 2019-05-10 stsp {
5949 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5950 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5951 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5952 1a36436d 2019-06-10 stsp
5953 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5954 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5955 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5956 1a36436d 2019-06-10 stsp return NULL;
5957 9bead371 2019-07-28 stsp /*
5958 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5959 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5960 9bead371 2019-07-28 stsp */
5961 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5962 945f9229 2022-04-16 thomas if (err)
5963 945f9229 2022-04-16 thomas goto done;
5964 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5965 9bead371 2019-07-28 stsp if (err) {
5966 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5967 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5968 1a36436d 2019-06-10 stsp goto done;
5969 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5970 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5971 1a36436d 2019-06-10 stsp } else {
5972 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5973 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5974 945f9229 2022-04-16 thomas if (err)
5975 945f9229 2022-04-16 thomas goto done;
5976 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5977 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5978 1a36436d 2019-06-10 stsp goto done;
5979 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5980 1a36436d 2019-06-10 stsp }
5981 1a36436d 2019-06-10 stsp done:
5982 1a36436d 2019-06-10 stsp free(id);
5983 945f9229 2022-04-16 thomas if (commit)
5984 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5985 1a36436d 2019-06-10 stsp return err;
5986 ebf99748 2019-05-09 stsp }
5987 ebf99748 2019-05-09 stsp
5988 ef20f542 2022-06-26 thomas static const struct got_error *
5989 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5990 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5991 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5992 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5993 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5994 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
5995 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5996 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5997 afa376bf 2019-05-09 stsp struct got_repository *repo)
5998 c4296144 2019-05-09 stsp {
5999 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6000 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6001 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6002 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6003 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6004 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6005 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6006 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6007 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6008 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6009 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6010 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6011 892eab0a 2022-07-22 thomas time_t timestamp;
6012 c4296144 2019-05-09 stsp
6013 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6014 c4296144 2019-05-09 stsp
6015 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6016 675c7539 2019-05-09 stsp
6017 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6018 588edf97 2019-05-10 stsp if (err)
6019 588edf97 2019-05-10 stsp goto done;
6020 de18fc63 2019-05-09 stsp
6021 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6022 588edf97 2019-05-10 stsp if (err)
6023 588edf97 2019-05-10 stsp goto done;
6024 588edf97 2019-05-10 stsp
6025 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6026 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6027 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6028 33ad4cbe 2019-05-12 jcs if (err)
6029 33ad4cbe 2019-05-12 jcs goto done;
6030 33ad4cbe 2019-05-12 jcs }
6031 c4296144 2019-05-09 stsp
6032 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6033 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6034 33ad4cbe 2019-05-12 jcs goto done;
6035 33ad4cbe 2019-05-12 jcs }
6036 33ad4cbe 2019-05-12 jcs
6037 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6038 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6039 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6040 cf066bf8 2019-05-09 stsp char *ondisk_path;
6041 cf066bf8 2019-05-09 stsp
6042 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6043 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6044 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6045 5f8a88c6 2019-08-03 stsp continue;
6046 5f8a88c6 2019-08-03 stsp
6047 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6048 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6049 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6050 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6051 cf066bf8 2019-05-09 stsp continue;
6052 cf066bf8 2019-05-09 stsp
6053 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6054 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6055 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6056 cf066bf8 2019-05-09 stsp goto done;
6057 cf066bf8 2019-05-09 stsp }
6058 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6059 2e1fa222 2020-07-23 stsp free(ondisk_path);
6060 2e1fa222 2020-07-23 stsp if (err)
6061 cf066bf8 2019-05-09 stsp goto done;
6062 cf066bf8 2019-05-09 stsp }
6063 036813ee 2019-05-09 stsp
6064 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6065 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6066 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6067 036813ee 2019-05-09 stsp if (err)
6068 036813ee 2019-05-09 stsp goto done;
6069 036813ee 2019-05-09 stsp
6070 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6071 de18fc63 2019-05-09 stsp if (err)
6072 de18fc63 2019-05-09 stsp goto done;
6073 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6074 10604dce 2021-09-24 thomas nparents++;
6075 10604dce 2021-09-24 thomas if (parent_id2) {
6076 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6077 10604dce 2021-09-24 thomas if (err)
6078 10604dce 2021-09-24 thomas goto done;
6079 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6080 10604dce 2021-09-24 thomas nparents++;
6081 10604dce 2021-09-24 thomas }
6082 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6083 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6084 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6085 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6086 33ad4cbe 2019-05-12 jcs free(logmsg);
6087 9d40349a 2019-05-09 stsp if (err)
6088 9d40349a 2019-05-09 stsp goto done;
6089 9d40349a 2019-05-09 stsp
6090 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6091 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6092 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6093 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6094 09f5bd90 2019-05-09 stsp goto done;
6095 09f5bd90 2019-05-09 stsp }
6096 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6097 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6098 09f5bd90 2019-05-09 stsp if (err)
6099 09f5bd90 2019-05-09 stsp goto done;
6100 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6101 ebf99748 2019-05-09 stsp if (err)
6102 ebf99748 2019-05-09 stsp goto done;
6103 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6104 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6105 09f5bd90 2019-05-09 stsp goto done;
6106 09f5bd90 2019-05-09 stsp }
6107 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6108 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6109 f2c16586 2019-05-09 stsp if (err)
6110 f2c16586 2019-05-09 stsp goto done;
6111 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6112 f2c16586 2019-05-09 stsp if (err)
6113 f2c16586 2019-05-09 stsp goto done;
6114 f2c16586 2019-05-09 stsp
6115 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6116 09f5bd90 2019-05-09 stsp if (err)
6117 09f5bd90 2019-05-09 stsp goto done;
6118 09f5bd90 2019-05-09 stsp
6119 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6120 ebf99748 2019-05-09 stsp if (err)
6121 ebf99748 2019-05-09 stsp goto done;
6122 c4296144 2019-05-09 stsp done:
6123 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6124 588edf97 2019-05-10 stsp if (head_tree)
6125 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6126 588edf97 2019-05-10 stsp if (head_commit)
6127 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6128 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6129 2f17228e 2019-05-12 stsp if (head_ref2) {
6130 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6131 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6132 2f17228e 2019-05-12 stsp err = unlockerr;
6133 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6134 39cd0ff6 2019-07-12 stsp }
6135 39cd0ff6 2019-07-12 stsp return err;
6136 39cd0ff6 2019-07-12 stsp }
6137 5c1e53bc 2019-07-28 stsp
6138 5c1e53bc 2019-07-28 stsp static const struct got_error *
6139 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6140 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6141 5c1e53bc 2019-07-28 stsp {
6142 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6143 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6144 39cd0ff6 2019-07-12 stsp
6145 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6146 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6147 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6148 5c1e53bc 2019-07-28 stsp
6149 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6150 5c1e53bc 2019-07-28 stsp ct_path++;
6151 5c1e53bc 2019-07-28 stsp
6152 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6153 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6154 5c1e53bc 2019-07-28 stsp break;
6155 5c1e53bc 2019-07-28 stsp }
6156 5c1e53bc 2019-07-28 stsp
6157 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6158 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6159 5c1e53bc 2019-07-28 stsp
6160 5c1e53bc 2019-07-28 stsp return NULL;
6161 5c1e53bc 2019-07-28 stsp }
6162 5c1e53bc 2019-07-28 stsp
6163 f0b75401 2019-08-03 stsp static const struct got_error *
6164 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6165 f0b75401 2019-08-03 stsp {
6166 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6167 f0b75401 2019-08-03 stsp
6168 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6169 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6170 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6171 f0b75401 2019-08-03 stsp }
6172 f0b75401 2019-08-03 stsp
6173 f0b75401 2019-08-03 stsp return NULL;
6174 f0b75401 2019-08-03 stsp }
6175 f0b75401 2019-08-03 stsp
6176 5f8a88c6 2019-08-03 stsp static const struct got_error *
6177 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6178 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6179 5f8a88c6 2019-08-03 stsp {
6180 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6181 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6182 5f8a88c6 2019-08-03 stsp
6183 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6184 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6185 5f8a88c6 2019-08-03 stsp continue;
6186 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6187 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6188 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6189 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6190 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6191 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6192 5f8a88c6 2019-08-03 stsp }
6193 5f8a88c6 2019-08-03 stsp
6194 5f8a88c6 2019-08-03 stsp return NULL;
6195 5f8a88c6 2019-08-03 stsp }
6196 5f8a88c6 2019-08-03 stsp
6197 39cd0ff6 2019-07-12 stsp const struct got_error *
6198 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6199 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6200 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6201 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6202 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6203 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6204 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6205 39cd0ff6 2019-07-12 stsp {
6206 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6207 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6208 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6209 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6210 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6211 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6212 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6213 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6214 ef899790 2022-11-01 thomas char *diff_path = NULL;
6215 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6216 39cd0ff6 2019-07-12 stsp
6217 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6218 39cd0ff6 2019-07-12 stsp
6219 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6220 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6221 39cd0ff6 2019-07-12 stsp
6222 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6223 39cd0ff6 2019-07-12 stsp if (err)
6224 39cd0ff6 2019-07-12 stsp goto done;
6225 39cd0ff6 2019-07-12 stsp
6226 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6227 39cd0ff6 2019-07-12 stsp if (err)
6228 39cd0ff6 2019-07-12 stsp goto done;
6229 39cd0ff6 2019-07-12 stsp
6230 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6231 39cd0ff6 2019-07-12 stsp if (err)
6232 39cd0ff6 2019-07-12 stsp goto done;
6233 39cd0ff6 2019-07-12 stsp
6234 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6235 39cd0ff6 2019-07-12 stsp if (err)
6236 39cd0ff6 2019-07-12 stsp goto done;
6237 39cd0ff6 2019-07-12 stsp
6238 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6239 5f8a88c6 2019-08-03 stsp &have_staged_files);
6240 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6241 5f8a88c6 2019-08-03 stsp goto done;
6242 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6243 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6244 5f8a88c6 2019-08-03 stsp if (err)
6245 5f8a88c6 2019-08-03 stsp goto done;
6246 5f8a88c6 2019-08-03 stsp }
6247 5f8a88c6 2019-08-03 stsp
6248 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6249 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6250 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6251 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6252 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6253 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6254 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6255 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6256 ef899790 2022-11-01 thomas if (show_diff) {
6257 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6258 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6259 ef899790 2022-11-01 thomas if (err)
6260 ef899790 2022-11-01 thomas goto done;
6261 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6262 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6263 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6264 ef899790 2022-11-01 thomas goto done;
6265 ef899790 2022-11-01 thomas }
6266 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6267 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6268 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6269 ef899790 2022-11-01 thomas goto done;
6270 ef899790 2022-11-01 thomas }
6271 ef899790 2022-11-01 thomas }
6272 ef899790 2022-11-01 thomas
6273 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6274 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6275 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6276 5c1e53bc 2019-07-28 stsp if (err)
6277 5c1e53bc 2019-07-28 stsp goto done;
6278 5c1e53bc 2019-07-28 stsp }
6279 39cd0ff6 2019-07-12 stsp
6280 ef899790 2022-11-01 thomas if (show_diff) {
6281 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6282 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6283 ef899790 2022-11-01 thomas goto done;
6284 ef899790 2022-11-01 thomas }
6285 ef899790 2022-11-01 thomas }
6286 ef899790 2022-11-01 thomas
6287 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6288 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6289 39cd0ff6 2019-07-12 stsp goto done;
6290 5c1e53bc 2019-07-28 stsp }
6291 5c1e53bc 2019-07-28 stsp
6292 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6293 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6294 5c1e53bc 2019-07-28 stsp if (err)
6295 5c1e53bc 2019-07-28 stsp goto done;
6296 39cd0ff6 2019-07-12 stsp }
6297 f0b75401 2019-08-03 stsp
6298 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6299 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6300 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6301 f0b75401 2019-08-03 stsp
6302 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6303 f0b75401 2019-08-03 stsp ct_path++;
6304 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6305 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6306 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6307 b50cabdf 2019-07-12 stsp if (err)
6308 b50cabdf 2019-07-12 stsp goto done;
6309 f0b75401 2019-08-03 stsp
6310 b50cabdf 2019-07-12 stsp }
6311 b50cabdf 2019-07-12 stsp
6312 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6313 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6314 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6315 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6316 39cd0ff6 2019-07-12 stsp if (err)
6317 39cd0ff6 2019-07-12 stsp goto done;
6318 39cd0ff6 2019-07-12 stsp
6319 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6320 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6321 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6322 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6323 39cd0ff6 2019-07-12 stsp err = sync_err;
6324 39cd0ff6 2019-07-12 stsp done:
6325 39cd0ff6 2019-07-12 stsp if (fileindex)
6326 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6327 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6328 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6329 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6330 39cd0ff6 2019-07-12 stsp err = unlockerr;
6331 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6332 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6333 21c2d8be 2023-01-10 thomas
6334 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6335 39cd0ff6 2019-07-12 stsp }
6336 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6337 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6338 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6339 ef899790 2022-11-01 thomas free(diff_path);
6340 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6341 ef899790 2022-11-01 thomas err == NULL)
6342 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6343 c4296144 2019-05-09 stsp return err;
6344 8656d6c4 2019-05-20 stsp }
6345 8656d6c4 2019-05-20 stsp
6346 8656d6c4 2019-05-20 stsp const char *
6347 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6348 8656d6c4 2019-05-20 stsp {
6349 8656d6c4 2019-05-20 stsp return ct->path;
6350 8656d6c4 2019-05-20 stsp }
6351 8656d6c4 2019-05-20 stsp
6352 8656d6c4 2019-05-20 stsp unsigned int
6353 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6354 8656d6c4 2019-05-20 stsp {
6355 8656d6c4 2019-05-20 stsp return ct->status;
6356 818c7501 2019-07-11 stsp }
6357 818c7501 2019-07-11 stsp
6358 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6359 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6360 818c7501 2019-07-11 stsp struct got_repository *repo;
6361 818c7501 2019-07-11 stsp };
6362 818c7501 2019-07-11 stsp
6363 818c7501 2019-07-11 stsp static const struct got_error *
6364 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6365 818c7501 2019-07-11 stsp {
6366 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6367 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6368 818c7501 2019-07-11 stsp unsigned char status;
6369 818c7501 2019-07-11 stsp struct stat sb;
6370 818c7501 2019-07-11 stsp char *ondisk_path;
6371 818c7501 2019-07-11 stsp
6372 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6373 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6374 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6375 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6376 818c7501 2019-07-11 stsp
6377 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6378 818c7501 2019-07-11 stsp == -1)
6379 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6380 818c7501 2019-07-11 stsp
6381 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6382 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6383 818c7501 2019-07-11 stsp free(ondisk_path);
6384 818c7501 2019-07-11 stsp if (err)
6385 818c7501 2019-07-11 stsp return err;
6386 818c7501 2019-07-11 stsp
6387 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6388 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6389 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6390 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6391 818c7501 2019-07-11 stsp
6392 818c7501 2019-07-11 stsp return NULL;
6393 818c7501 2019-07-11 stsp }
6394 818c7501 2019-07-11 stsp
6395 818c7501 2019-07-11 stsp const struct got_error *
6396 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6397 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6398 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6399 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6400 818c7501 2019-07-11 stsp {
6401 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6402 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6403 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6404 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6405 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6406 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6407 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6408 818c7501 2019-07-11 stsp
6409 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6410 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6411 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6412 818c7501 2019-07-11 stsp
6413 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6414 818c7501 2019-07-11 stsp if (err)
6415 818c7501 2019-07-11 stsp return err;
6416 818c7501 2019-07-11 stsp
6417 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
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 ok_arg.worktree = worktree;
6422 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6423 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6424 818c7501 2019-07-11 stsp &ok_arg);
6425 818c7501 2019-07-11 stsp if (err)
6426 818c7501 2019-07-11 stsp goto done;
6427 818c7501 2019-07-11 stsp
6428 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6429 818c7501 2019-07-11 stsp if (err)
6430 818c7501 2019-07-11 stsp goto done;
6431 818c7501 2019-07-11 stsp
6432 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6433 818c7501 2019-07-11 stsp if (err)
6434 818c7501 2019-07-11 stsp goto done;
6435 818c7501 2019-07-11 stsp
6436 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6437 818c7501 2019-07-11 stsp if (err)
6438 818c7501 2019-07-11 stsp goto done;
6439 818c7501 2019-07-11 stsp
6440 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6441 818c7501 2019-07-11 stsp 0);
6442 e51d7b55 2020-01-04 stsp if (err)
6443 e51d7b55 2020-01-04 stsp goto done;
6444 e51d7b55 2020-01-04 stsp
6445 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6446 818c7501 2019-07-11 stsp if (err)
6447 818c7501 2019-07-11 stsp goto done;
6448 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6449 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6450 e51d7b55 2020-01-04 stsp goto done;
6451 e51d7b55 2020-01-04 stsp }
6452 818c7501 2019-07-11 stsp
6453 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6454 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6455 818c7501 2019-07-11 stsp if (err)
6456 818c7501 2019-07-11 stsp goto done;
6457 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6458 818c7501 2019-07-11 stsp if (err)
6459 818c7501 2019-07-11 stsp goto done;
6460 818c7501 2019-07-11 stsp
6461 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6462 818c7501 2019-07-11 stsp
6463 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6464 818c7501 2019-07-11 stsp if (err)
6465 818c7501 2019-07-11 stsp goto done;
6466 818c7501 2019-07-11 stsp
6467 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6468 818c7501 2019-07-11 stsp if (err)
6469 818c7501 2019-07-11 stsp goto done;
6470 818c7501 2019-07-11 stsp
6471 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6472 818c7501 2019-07-11 stsp worktree->base_commit_id);
6473 818c7501 2019-07-11 stsp if (err)
6474 818c7501 2019-07-11 stsp goto done;
6475 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6476 818c7501 2019-07-11 stsp if (err)
6477 818c7501 2019-07-11 stsp goto done;
6478 818c7501 2019-07-11 stsp
6479 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6480 818c7501 2019-07-11 stsp if (err)
6481 818c7501 2019-07-11 stsp goto done;
6482 818c7501 2019-07-11 stsp done:
6483 818c7501 2019-07-11 stsp free(fileindex_path);
6484 818c7501 2019-07-11 stsp free(tmp_branch_name);
6485 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6486 818c7501 2019-07-11 stsp free(branch_ref_name);
6487 818c7501 2019-07-11 stsp if (branch_ref)
6488 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6489 818c7501 2019-07-11 stsp if (wt_branch)
6490 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6491 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6492 818c7501 2019-07-11 stsp if (err) {
6493 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6494 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6495 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6496 818c7501 2019-07-11 stsp }
6497 818c7501 2019-07-11 stsp if (*tmp_branch) {
6498 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6499 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6500 818c7501 2019-07-11 stsp }
6501 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6502 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6503 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6504 3e3a69f1 2019-07-25 stsp }
6505 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6506 818c7501 2019-07-11 stsp }
6507 818c7501 2019-07-11 stsp return err;
6508 818c7501 2019-07-11 stsp }
6509 818c7501 2019-07-11 stsp
6510 818c7501 2019-07-11 stsp const struct got_error *
6511 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6512 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6513 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6514 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6515 818c7501 2019-07-11 stsp {
6516 818c7501 2019-07-11 stsp const struct got_error *err;
6517 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6518 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6519 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6520 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6521 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6522 818c7501 2019-07-11 stsp
6523 818c7501 2019-07-11 stsp *commit_id = NULL;
6524 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6525 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6526 3e3a69f1 2019-07-25 stsp *branch = NULL;
6527 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6528 3e3a69f1 2019-07-25 stsp
6529 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6530 3e3a69f1 2019-07-25 stsp if (err)
6531 3e3a69f1 2019-07-25 stsp return err;
6532 818c7501 2019-07-11 stsp
6533 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6534 3e3a69f1 2019-07-25 stsp if (err)
6535 f032f1f7 2019-08-04 stsp goto done;
6536 f032f1f7 2019-08-04 stsp
6537 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6538 f032f1f7 2019-08-04 stsp &have_staged_files);
6539 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6540 f032f1f7 2019-08-04 stsp goto done;
6541 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6542 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6543 3e3a69f1 2019-07-25 stsp goto done;
6544 f032f1f7 2019-08-04 stsp }
6545 3e3a69f1 2019-07-25 stsp
6546 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6547 818c7501 2019-07-11 stsp if (err)
6548 f032f1f7 2019-08-04 stsp goto done;
6549 818c7501 2019-07-11 stsp
6550 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6551 818c7501 2019-07-11 stsp if (err)
6552 818c7501 2019-07-11 stsp goto done;
6553 818c7501 2019-07-11 stsp
6554 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6555 818c7501 2019-07-11 stsp if (err)
6556 818c7501 2019-07-11 stsp goto done;
6557 818c7501 2019-07-11 stsp
6558 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6559 818c7501 2019-07-11 stsp if (err)
6560 818c7501 2019-07-11 stsp goto done;
6561 818c7501 2019-07-11 stsp
6562 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6563 818c7501 2019-07-11 stsp if (err)
6564 818c7501 2019-07-11 stsp goto done;
6565 818c7501 2019-07-11 stsp
6566 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6567 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6568 818c7501 2019-07-11 stsp if (err)
6569 818c7501 2019-07-11 stsp goto done;
6570 818c7501 2019-07-11 stsp
6571 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6572 818c7501 2019-07-11 stsp if (err)
6573 818c7501 2019-07-11 stsp goto done;
6574 818c7501 2019-07-11 stsp
6575 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6576 818c7501 2019-07-11 stsp if (err)
6577 818c7501 2019-07-11 stsp goto done;
6578 818c7501 2019-07-11 stsp
6579 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6580 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6581 818c7501 2019-07-11 stsp if (err)
6582 818c7501 2019-07-11 stsp goto done;
6583 818c7501 2019-07-11 stsp
6584 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6585 818c7501 2019-07-11 stsp if (err)
6586 818c7501 2019-07-11 stsp goto done;
6587 818c7501 2019-07-11 stsp done:
6588 818c7501 2019-07-11 stsp free(commit_ref_name);
6589 818c7501 2019-07-11 stsp free(branch_ref_name);
6590 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6591 818c7501 2019-07-11 stsp if (commit_ref)
6592 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6593 818c7501 2019-07-11 stsp if (branch_ref)
6594 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6595 818c7501 2019-07-11 stsp if (err) {
6596 818c7501 2019-07-11 stsp free(*commit_id);
6597 818c7501 2019-07-11 stsp *commit_id = NULL;
6598 818c7501 2019-07-11 stsp if (*tmp_branch) {
6599 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6600 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6601 818c7501 2019-07-11 stsp }
6602 818c7501 2019-07-11 stsp if (*new_base_branch) {
6603 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6604 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6605 818c7501 2019-07-11 stsp }
6606 818c7501 2019-07-11 stsp if (*branch) {
6607 818c7501 2019-07-11 stsp got_ref_close(*branch);
6608 818c7501 2019-07-11 stsp *branch = NULL;
6609 818c7501 2019-07-11 stsp }
6610 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6611 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6612 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6613 3e3a69f1 2019-07-25 stsp }
6614 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6615 818c7501 2019-07-11 stsp }
6616 818c7501 2019-07-11 stsp return err;
6617 c4296144 2019-05-09 stsp }
6618 818c7501 2019-07-11 stsp
6619 818c7501 2019-07-11 stsp const struct got_error *
6620 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6621 818c7501 2019-07-11 stsp {
6622 818c7501 2019-07-11 stsp const struct got_error *err;
6623 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6624 818c7501 2019-07-11 stsp
6625 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6626 818c7501 2019-07-11 stsp if (err)
6627 818c7501 2019-07-11 stsp return err;
6628 818c7501 2019-07-11 stsp
6629 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6630 818c7501 2019-07-11 stsp free(tmp_branch_name);
6631 818c7501 2019-07-11 stsp return NULL;
6632 818c7501 2019-07-11 stsp }
6633 818c7501 2019-07-11 stsp
6634 818c7501 2019-07-11 stsp static const struct got_error *
6635 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6636 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6637 818c7501 2019-07-11 stsp {
6638 0ebf8283 2019-07-24 stsp *logmsg = arg;
6639 818c7501 2019-07-11 stsp return NULL;
6640 818c7501 2019-07-11 stsp }
6641 818c7501 2019-07-11 stsp
6642 818c7501 2019-07-11 stsp static const struct got_error *
6643 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6644 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6645 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6646 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6647 818c7501 2019-07-11 stsp {
6648 818c7501 2019-07-11 stsp return NULL;
6649 01757395 2019-07-12 stsp }
6650 01757395 2019-07-12 stsp
6651 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6652 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6653 01757395 2019-07-12 stsp void *progress_arg;
6654 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6655 01757395 2019-07-12 stsp };
6656 01757395 2019-07-12 stsp
6657 01757395 2019-07-12 stsp static const struct got_error *
6658 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6659 01757395 2019-07-12 stsp {
6660 01757395 2019-07-12 stsp const struct got_error *err;
6661 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6662 01757395 2019-07-12 stsp char *p;
6663 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6664 01757395 2019-07-12 stsp
6665 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6666 01757395 2019-07-12 stsp if (err)
6667 01757395 2019-07-12 stsp return err;
6668 01757395 2019-07-12 stsp
6669 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6670 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6671 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6672 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6673 01757395 2019-07-12 stsp return NULL;
6674 01757395 2019-07-12 stsp
6675 01757395 2019-07-12 stsp p = strdup(path);
6676 01757395 2019-07-12 stsp if (p == NULL)
6677 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6678 01757395 2019-07-12 stsp
6679 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6680 01757395 2019-07-12 stsp if (err || new == NULL)
6681 01757395 2019-07-12 stsp free(p);
6682 01757395 2019-07-12 stsp return err;
6683 818c7501 2019-07-11 stsp }
6684 818c7501 2019-07-11 stsp
6685 0ebf8283 2019-07-24 stsp static const struct got_error *
6686 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6687 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6688 818c7501 2019-07-11 stsp {
6689 818c7501 2019-07-11 stsp const struct got_error *err;
6690 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6691 818c7501 2019-07-11 stsp
6692 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6693 818c7501 2019-07-11 stsp if (err) {
6694 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6695 818c7501 2019-07-11 stsp goto done;
6696 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6697 818c7501 2019-07-11 stsp if (err)
6698 818c7501 2019-07-11 stsp goto done;
6699 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6700 818c7501 2019-07-11 stsp if (err)
6701 818c7501 2019-07-11 stsp goto done;
6702 de05890f 2020-03-05 stsp } else if (is_rebase) {
6703 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6704 818c7501 2019-07-11 stsp int cmp;
6705 818c7501 2019-07-11 stsp
6706 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6707 818c7501 2019-07-11 stsp if (err)
6708 818c7501 2019-07-11 stsp goto done;
6709 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6710 818c7501 2019-07-11 stsp free(stored_id);
6711 818c7501 2019-07-11 stsp if (cmp != 0) {
6712 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6713 818c7501 2019-07-11 stsp goto done;
6714 818c7501 2019-07-11 stsp }
6715 818c7501 2019-07-11 stsp }
6716 0ebf8283 2019-07-24 stsp done:
6717 0ebf8283 2019-07-24 stsp if (commit_ref)
6718 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6719 0ebf8283 2019-07-24 stsp return err;
6720 0ebf8283 2019-07-24 stsp }
6721 0ebf8283 2019-07-24 stsp
6722 0ebf8283 2019-07-24 stsp static const struct got_error *
6723 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6724 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6725 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6726 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6727 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6728 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6729 0ebf8283 2019-07-24 stsp {
6730 0ebf8283 2019-07-24 stsp const struct got_error *err;
6731 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6732 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6733 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6734 818c7501 2019-07-11 stsp
6735 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6736 0ebf8283 2019-07-24 stsp
6737 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6738 0ebf8283 2019-07-24 stsp if (err)
6739 0ebf8283 2019-07-24 stsp return err;
6740 0ebf8283 2019-07-24 stsp
6741 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6742 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6743 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6744 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6745 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6746 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6747 818c7501 2019-07-11 stsp if (commit_ref)
6748 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6749 818c7501 2019-07-11 stsp return err;
6750 818c7501 2019-07-11 stsp }
6751 818c7501 2019-07-11 stsp
6752 818c7501 2019-07-11 stsp const struct got_error *
6753 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6754 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6755 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6756 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6757 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6758 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6759 818c7501 2019-07-11 stsp {
6760 0ebf8283 2019-07-24 stsp const struct got_error *err;
6761 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6762 0ebf8283 2019-07-24 stsp
6763 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6764 0ebf8283 2019-07-24 stsp if (err)
6765 0ebf8283 2019-07-24 stsp return err;
6766 0ebf8283 2019-07-24 stsp
6767 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6768 0ebf8283 2019-07-24 stsp if (err)
6769 0ebf8283 2019-07-24 stsp goto done;
6770 0ebf8283 2019-07-24 stsp
6771 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6772 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6773 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6774 0ebf8283 2019-07-24 stsp done:
6775 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6776 0ebf8283 2019-07-24 stsp return err;
6777 0ebf8283 2019-07-24 stsp }
6778 0ebf8283 2019-07-24 stsp
6779 0ebf8283 2019-07-24 stsp const struct got_error *
6780 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6781 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6782 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6783 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6784 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6785 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6786 0ebf8283 2019-07-24 stsp {
6787 0ebf8283 2019-07-24 stsp const struct got_error *err;
6788 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6789 0ebf8283 2019-07-24 stsp
6790 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6791 0ebf8283 2019-07-24 stsp if (err)
6792 0ebf8283 2019-07-24 stsp return err;
6793 0ebf8283 2019-07-24 stsp
6794 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6795 0ebf8283 2019-07-24 stsp if (err)
6796 0ebf8283 2019-07-24 stsp goto done;
6797 0ebf8283 2019-07-24 stsp
6798 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6799 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6800 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6801 0ebf8283 2019-07-24 stsp done:
6802 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6803 0ebf8283 2019-07-24 stsp return err;
6804 0ebf8283 2019-07-24 stsp }
6805 0ebf8283 2019-07-24 stsp
6806 0ebf8283 2019-07-24 stsp static const struct got_error *
6807 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6808 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6809 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6810 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6811 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6812 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6813 0ebf8283 2019-07-24 stsp {
6814 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6815 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6816 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6817 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6818 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6819 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6820 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6821 818c7501 2019-07-11 stsp
6822 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6823 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6824 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6825 a0e95631 2019-07-12 stsp
6826 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6827 818c7501 2019-07-11 stsp
6828 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6829 a0e95631 2019-07-12 stsp if (err)
6830 3e3a69f1 2019-07-25 stsp return err;
6831 a0e95631 2019-07-12 stsp
6832 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6833 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6834 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6835 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6836 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
6837 01757395 2019-07-12 stsp /*
6838 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6839 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6840 6c13b005 2021-09-02 stsp *
6841 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6842 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6843 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6844 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6845 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6846 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6847 01757395 2019-07-12 stsp */
6848 01757395 2019-07-12 stsp if (merged_paths) {
6849 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6850 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6851 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6852 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6853 f2a9dc41 2019-12-13 tracey 0);
6854 01757395 2019-07-12 stsp if (err)
6855 01757395 2019-07-12 stsp goto done;
6856 01757395 2019-07-12 stsp }
6857 01757395 2019-07-12 stsp } else {
6858 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6859 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6860 01757395 2019-07-12 stsp if (err)
6861 01757395 2019-07-12 stsp goto done;
6862 01757395 2019-07-12 stsp }
6863 a0e95631 2019-07-12 stsp
6864 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6865 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
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 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6870 a0e95631 2019-07-12 stsp goto done;
6871 ff0d2220 2019-07-11 stsp }
6872 818c7501 2019-07-11 stsp
6873 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6874 edd02c5e 2019-07-11 stsp if (err)
6875 edd02c5e 2019-07-11 stsp goto done;
6876 edd02c5e 2019-07-11 stsp
6877 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6878 818c7501 2019-07-11 stsp if (err)
6879 818c7501 2019-07-11 stsp goto done;
6880 818c7501 2019-07-11 stsp
6881 5943eee2 2019-08-13 stsp if (new_logmsg) {
6882 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6883 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6884 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6885 5943eee2 2019-08-13 stsp goto done;
6886 5943eee2 2019-08-13 stsp }
6887 5943eee2 2019-08-13 stsp } else {
6888 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6889 5943eee2 2019-08-13 stsp if (err)
6890 5943eee2 2019-08-13 stsp goto done;
6891 5943eee2 2019-08-13 stsp }
6892 0ebf8283 2019-07-24 stsp
6893 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6894 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6895 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6896 8db00f97 2022-07-22 thomas committer ? committer :
6897 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
6898 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6899 a0e95631 2019-07-12 stsp if (err)
6900 a0e95631 2019-07-12 stsp goto done;
6901 a0e95631 2019-07-12 stsp
6902 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6903 a0e95631 2019-07-12 stsp if (err)
6904 a0e95631 2019-07-12 stsp goto done;
6905 a0e95631 2019-07-12 stsp
6906 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6907 a0e95631 2019-07-12 stsp if (err)
6908 a0e95631 2019-07-12 stsp goto done;
6909 a0e95631 2019-07-12 stsp
6910 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6911 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6912 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6913 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6914 a0e95631 2019-07-12 stsp err = sync_err;
6915 818c7501 2019-07-11 stsp done:
6916 a0e95631 2019-07-12 stsp free(fileindex_path);
6917 a0e95631 2019-07-12 stsp free(head_commit_id);
6918 a0e95631 2019-07-12 stsp if (head_ref)
6919 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6920 818c7501 2019-07-11 stsp if (err) {
6921 818c7501 2019-07-11 stsp free(*new_commit_id);
6922 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6923 818c7501 2019-07-11 stsp }
6924 818c7501 2019-07-11 stsp return err;
6925 818c7501 2019-07-11 stsp }
6926 818c7501 2019-07-11 stsp
6927 818c7501 2019-07-11 stsp const struct got_error *
6928 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6929 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6930 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6931 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6932 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
6933 be94c032 2023-02-20 thomas struct got_repository *repo)
6934 0ebf8283 2019-07-24 stsp {
6935 0ebf8283 2019-07-24 stsp const struct got_error *err;
6936 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6937 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6938 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6939 0ebf8283 2019-07-24 stsp
6940 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6941 0ebf8283 2019-07-24 stsp if (err)
6942 0ebf8283 2019-07-24 stsp return err;
6943 0ebf8283 2019-07-24 stsp
6944 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6945 0ebf8283 2019-07-24 stsp if (err)
6946 0ebf8283 2019-07-24 stsp goto done;
6947 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6948 0ebf8283 2019-07-24 stsp if (err)
6949 0ebf8283 2019-07-24 stsp goto done;
6950 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6951 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6952 0ebf8283 2019-07-24 stsp goto done;
6953 0ebf8283 2019-07-24 stsp }
6954 0ebf8283 2019-07-24 stsp
6955 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6956 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6957 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
6958 0ebf8283 2019-07-24 stsp done:
6959 0ebf8283 2019-07-24 stsp if (commit_ref)
6960 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6961 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6962 0ebf8283 2019-07-24 stsp free(commit_id);
6963 0ebf8283 2019-07-24 stsp return err;
6964 0ebf8283 2019-07-24 stsp }
6965 0ebf8283 2019-07-24 stsp
6966 0ebf8283 2019-07-24 stsp const struct got_error *
6967 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6968 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6969 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6970 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6971 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6972 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6973 0ebf8283 2019-07-24 stsp {
6974 0ebf8283 2019-07-24 stsp const struct got_error *err;
6975 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6976 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6977 0ebf8283 2019-07-24 stsp
6978 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6979 0ebf8283 2019-07-24 stsp if (err)
6980 0ebf8283 2019-07-24 stsp return err;
6981 0ebf8283 2019-07-24 stsp
6982 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6983 0ebf8283 2019-07-24 stsp if (err)
6984 0ebf8283 2019-07-24 stsp goto done;
6985 0ebf8283 2019-07-24 stsp
6986 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6987 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6988 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
6989 0ebf8283 2019-07-24 stsp done:
6990 0ebf8283 2019-07-24 stsp if (commit_ref)
6991 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6992 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6993 0ebf8283 2019-07-24 stsp return err;
6994 0ebf8283 2019-07-24 stsp }
6995 0ebf8283 2019-07-24 stsp
6996 0ebf8283 2019-07-24 stsp const struct got_error *
6997 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6998 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6999 818c7501 2019-07-11 stsp {
7000 3e3a69f1 2019-07-25 stsp if (fileindex)
7001 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7002 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7003 69844fba 2019-07-11 stsp }
7004 69844fba 2019-07-11 stsp
7005 69844fba 2019-07-11 stsp static const struct got_error *
7006 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7007 69844fba 2019-07-11 stsp {
7008 69844fba 2019-07-11 stsp const struct got_error *err;
7009 69844fba 2019-07-11 stsp struct got_reference *ref;
7010 69844fba 2019-07-11 stsp
7011 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7012 69844fba 2019-07-11 stsp if (err) {
7013 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7014 69844fba 2019-07-11 stsp return NULL;
7015 69844fba 2019-07-11 stsp return err;
7016 69844fba 2019-07-11 stsp }
7017 69844fba 2019-07-11 stsp
7018 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7019 69844fba 2019-07-11 stsp got_ref_close(ref);
7020 69844fba 2019-07-11 stsp return err;
7021 818c7501 2019-07-11 stsp }
7022 818c7501 2019-07-11 stsp
7023 69844fba 2019-07-11 stsp static const struct got_error *
7024 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7025 69844fba 2019-07-11 stsp {
7026 69844fba 2019-07-11 stsp const struct got_error *err;
7027 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7028 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7029 69844fba 2019-07-11 stsp
7030 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7031 69844fba 2019-07-11 stsp if (err)
7032 69844fba 2019-07-11 stsp goto done;
7033 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7034 69844fba 2019-07-11 stsp if (err)
7035 69844fba 2019-07-11 stsp goto done;
7036 69844fba 2019-07-11 stsp
7037 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7038 69844fba 2019-07-11 stsp if (err)
7039 69844fba 2019-07-11 stsp goto done;
7040 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7041 69844fba 2019-07-11 stsp if (err)
7042 69844fba 2019-07-11 stsp goto done;
7043 69844fba 2019-07-11 stsp
7044 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7045 69844fba 2019-07-11 stsp if (err)
7046 69844fba 2019-07-11 stsp goto done;
7047 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7048 69844fba 2019-07-11 stsp if (err)
7049 69844fba 2019-07-11 stsp goto done;
7050 69844fba 2019-07-11 stsp
7051 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7052 69844fba 2019-07-11 stsp if (err)
7053 69844fba 2019-07-11 stsp goto done;
7054 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7055 69844fba 2019-07-11 stsp if (err)
7056 69844fba 2019-07-11 stsp goto done;
7057 69844fba 2019-07-11 stsp
7058 69844fba 2019-07-11 stsp done:
7059 69844fba 2019-07-11 stsp free(tmp_branch_name);
7060 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7061 69844fba 2019-07-11 stsp free(branch_ref_name);
7062 69844fba 2019-07-11 stsp free(commit_ref_name);
7063 e600f124 2021-03-21 stsp return err;
7064 e600f124 2021-03-21 stsp }
7065 e600f124 2021-03-21 stsp
7066 ef20f542 2022-06-26 thomas static const struct got_error *
7067 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7068 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7069 e600f124 2021-03-21 stsp {
7070 e600f124 2021-03-21 stsp const struct got_error *err;
7071 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7072 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7073 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7074 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7075 e600f124 2021-03-21 stsp char *refname = NULL;
7076 e600f124 2021-03-21 stsp
7077 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7078 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7079 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7080 e600f124 2021-03-21 stsp branch_name += 11;
7081 e600f124 2021-03-21 stsp
7082 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7083 e600f124 2021-03-21 stsp if (err)
7084 e600f124 2021-03-21 stsp return err;
7085 e600f124 2021-03-21 stsp
7086 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7087 e600f124 2021-03-21 stsp new_id_str) == -1) {
7088 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7089 e600f124 2021-03-21 stsp goto done;
7090 e600f124 2021-03-21 stsp }
7091 e600f124 2021-03-21 stsp
7092 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7093 e600f124 2021-03-21 stsp if (err)
7094 e600f124 2021-03-21 stsp goto done;
7095 e600f124 2021-03-21 stsp
7096 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7097 e600f124 2021-03-21 stsp if (err)
7098 e600f124 2021-03-21 stsp goto done;
7099 e600f124 2021-03-21 stsp
7100 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7101 e600f124 2021-03-21 stsp done:
7102 e600f124 2021-03-21 stsp free(new_id_str);
7103 e600f124 2021-03-21 stsp free(refname);
7104 e600f124 2021-03-21 stsp free(old_commit_id);
7105 e600f124 2021-03-21 stsp if (ref)
7106 e600f124 2021-03-21 stsp got_ref_close(ref);
7107 69844fba 2019-07-11 stsp return err;
7108 69844fba 2019-07-11 stsp }
7109 69844fba 2019-07-11 stsp
7110 818c7501 2019-07-11 stsp const struct got_error *
7111 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7112 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7113 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7114 f08f28be 2023-02-03 thomas int create_backup)
7115 818c7501 2019-07-11 stsp {
7116 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7117 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7118 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7119 818c7501 2019-07-11 stsp
7120 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7121 818c7501 2019-07-11 stsp if (err)
7122 818c7501 2019-07-11 stsp return err;
7123 e600f124 2021-03-21 stsp
7124 e600f124 2021-03-21 stsp if (create_backup) {
7125 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7126 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7127 e600f124 2021-03-21 stsp if (err)
7128 e600f124 2021-03-21 stsp goto done;
7129 e600f124 2021-03-21 stsp }
7130 818c7501 2019-07-11 stsp
7131 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7132 818c7501 2019-07-11 stsp if (err)
7133 818c7501 2019-07-11 stsp goto done;
7134 818c7501 2019-07-11 stsp
7135 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7136 818c7501 2019-07-11 stsp if (err)
7137 818c7501 2019-07-11 stsp goto done;
7138 818c7501 2019-07-11 stsp
7139 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7140 818c7501 2019-07-11 stsp if (err)
7141 818c7501 2019-07-11 stsp goto done;
7142 818c7501 2019-07-11 stsp
7143 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7144 a615e0e7 2020-12-16 stsp if (err)
7145 a615e0e7 2020-12-16 stsp goto done;
7146 a615e0e7 2020-12-16 stsp
7147 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7148 a615e0e7 2020-12-16 stsp if (err)
7149 a615e0e7 2020-12-16 stsp goto done;
7150 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7151 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7152 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7153 a615e0e7 2020-12-16 stsp err = sync_err;
7154 818c7501 2019-07-11 stsp done:
7155 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7156 a615e0e7 2020-12-16 stsp free(fileindex_path);
7157 818c7501 2019-07-11 stsp free(new_head_commit_id);
7158 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7159 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7160 818c7501 2019-07-11 stsp err = unlockerr;
7161 818c7501 2019-07-11 stsp return err;
7162 818c7501 2019-07-11 stsp }
7163 818c7501 2019-07-11 stsp
7164 818c7501 2019-07-11 stsp const struct got_error *
7165 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7166 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7167 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7168 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7169 818c7501 2019-07-11 stsp {
7170 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7171 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7172 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7173 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7174 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7175 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7176 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7177 818c7501 2019-07-11 stsp
7178 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7179 818c7501 2019-07-11 stsp if (err)
7180 818c7501 2019-07-11 stsp return err;
7181 818c7501 2019-07-11 stsp
7182 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7183 945f9229 2022-04-16 thomas worktree->base_commit_id);
7184 945f9229 2022-04-16 thomas if (err)
7185 945f9229 2022-04-16 thomas goto done;
7186 945f9229 2022-04-16 thomas
7187 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7188 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7189 818c7501 2019-07-11 stsp if (err)
7190 818c7501 2019-07-11 stsp goto done;
7191 818c7501 2019-07-11 stsp
7192 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7193 818c7501 2019-07-11 stsp if (err)
7194 818c7501 2019-07-11 stsp goto done;
7195 818c7501 2019-07-11 stsp
7196 818c7501 2019-07-11 stsp /*
7197 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7198 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7199 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7200 818c7501 2019-07-11 stsp */
7201 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7202 818c7501 2019-07-11 stsp if (err)
7203 818c7501 2019-07-11 stsp goto done;
7204 818c7501 2019-07-11 stsp
7205 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7206 818c7501 2019-07-11 stsp if (err)
7207 818c7501 2019-07-11 stsp goto done;
7208 818c7501 2019-07-11 stsp
7209 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7210 945f9229 2022-04-16 thomas worktree->path_prefix);
7211 ca355955 2019-07-12 stsp if (err)
7212 ca355955 2019-07-12 stsp goto done;
7213 ca355955 2019-07-12 stsp
7214 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7215 ca355955 2019-07-12 stsp if (err)
7216 ca355955 2019-07-12 stsp goto done;
7217 ca355955 2019-07-12 stsp
7218 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7219 818c7501 2019-07-11 stsp if (err)
7220 818c7501 2019-07-11 stsp goto done;
7221 818c7501 2019-07-11 stsp
7222 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7223 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7224 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7225 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7226 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7227 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7228 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7229 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7230 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7231 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7232 55bd499d 2019-07-12 stsp if (err)
7233 1f1abb7e 2019-08-08 stsp goto sync;
7234 55bd499d 2019-07-12 stsp
7235 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7236 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7237 ca355955 2019-07-12 stsp sync:
7238 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7239 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7240 ca355955 2019-07-12 stsp err = sync_err;
7241 818c7501 2019-07-11 stsp done:
7242 818c7501 2019-07-11 stsp got_ref_close(resolved);
7243 a3a2faf2 2019-07-12 stsp free(tree_id);
7244 818c7501 2019-07-11 stsp free(commit_id);
7245 945f9229 2022-04-16 thomas if (commit)
7246 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7247 0ebf8283 2019-07-24 stsp if (fileindex)
7248 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7249 0ebf8283 2019-07-24 stsp free(fileindex_path);
7250 0ebf8283 2019-07-24 stsp
7251 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7252 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7253 0ebf8283 2019-07-24 stsp err = unlockerr;
7254 0ebf8283 2019-07-24 stsp return err;
7255 0ebf8283 2019-07-24 stsp }
7256 0ebf8283 2019-07-24 stsp
7257 0ebf8283 2019-07-24 stsp const struct got_error *
7258 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7259 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7260 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7261 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7262 0ebf8283 2019-07-24 stsp {
7263 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7264 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7265 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7266 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7267 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7268 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7269 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7270 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7271 0ebf8283 2019-07-24 stsp
7272 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7273 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7274 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7275 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7276 0ebf8283 2019-07-24 stsp
7277 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7278 0ebf8283 2019-07-24 stsp if (err)
7279 0ebf8283 2019-07-24 stsp return err;
7280 0ebf8283 2019-07-24 stsp
7281 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7282 0ebf8283 2019-07-24 stsp if (err)
7283 0ebf8283 2019-07-24 stsp goto done;
7284 0ebf8283 2019-07-24 stsp
7285 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7286 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7287 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7288 0ebf8283 2019-07-24 stsp &ok_arg);
7289 0ebf8283 2019-07-24 stsp if (err)
7290 0ebf8283 2019-07-24 stsp goto done;
7291 0ebf8283 2019-07-24 stsp
7292 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7293 0ebf8283 2019-07-24 stsp if (err)
7294 0ebf8283 2019-07-24 stsp goto done;
7295 0ebf8283 2019-07-24 stsp
7296 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7297 0ebf8283 2019-07-24 stsp if (err)
7298 0ebf8283 2019-07-24 stsp goto done;
7299 0ebf8283 2019-07-24 stsp
7300 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7301 0ebf8283 2019-07-24 stsp worktree);
7302 0ebf8283 2019-07-24 stsp if (err)
7303 0ebf8283 2019-07-24 stsp goto done;
7304 0ebf8283 2019-07-24 stsp
7305 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7306 0ebf8283 2019-07-24 stsp 0);
7307 0ebf8283 2019-07-24 stsp if (err)
7308 0ebf8283 2019-07-24 stsp goto done;
7309 0ebf8283 2019-07-24 stsp
7310 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7311 0ebf8283 2019-07-24 stsp if (err)
7312 0ebf8283 2019-07-24 stsp goto done;
7313 0ebf8283 2019-07-24 stsp
7314 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7315 0ebf8283 2019-07-24 stsp if (err)
7316 0ebf8283 2019-07-24 stsp goto done;
7317 0ebf8283 2019-07-24 stsp
7318 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7319 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7320 0ebf8283 2019-07-24 stsp if (err)
7321 0ebf8283 2019-07-24 stsp goto done;
7322 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7323 0ebf8283 2019-07-24 stsp if (err)
7324 0ebf8283 2019-07-24 stsp goto done;
7325 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7326 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7327 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7328 0ebf8283 2019-07-24 stsp goto done;
7329 0ebf8283 2019-07-24 stsp }
7330 0ebf8283 2019-07-24 stsp
7331 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7332 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7333 0ebf8283 2019-07-24 stsp if (err)
7334 0ebf8283 2019-07-24 stsp goto done;
7335 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7336 0ebf8283 2019-07-24 stsp if (err)
7337 0ebf8283 2019-07-24 stsp goto done;
7338 0ebf8283 2019-07-24 stsp
7339 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7340 0ebf8283 2019-07-24 stsp if (err)
7341 0ebf8283 2019-07-24 stsp goto done;
7342 0ebf8283 2019-07-24 stsp done:
7343 0ebf8283 2019-07-24 stsp free(fileindex_path);
7344 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7345 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7346 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7347 0ebf8283 2019-07-24 stsp if (wt_branch)
7348 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7349 0ebf8283 2019-07-24 stsp if (err) {
7350 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7351 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7352 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7353 0ebf8283 2019-07-24 stsp }
7354 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7355 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7356 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7357 0ebf8283 2019-07-24 stsp }
7358 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7359 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7360 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7361 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7362 3e3a69f1 2019-07-25 stsp }
7363 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7364 0ebf8283 2019-07-24 stsp }
7365 0ebf8283 2019-07-24 stsp return err;
7366 0ebf8283 2019-07-24 stsp }
7367 0ebf8283 2019-07-24 stsp
7368 0ebf8283 2019-07-24 stsp const struct got_error *
7369 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7370 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7371 0ebf8283 2019-07-24 stsp {
7372 3e3a69f1 2019-07-25 stsp if (fileindex)
7373 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7374 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7375 0ebf8283 2019-07-24 stsp }
7376 0ebf8283 2019-07-24 stsp
7377 0ebf8283 2019-07-24 stsp const struct got_error *
7378 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7379 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7380 0ebf8283 2019-07-24 stsp {
7381 0ebf8283 2019-07-24 stsp const struct got_error *err;
7382 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7383 0ebf8283 2019-07-24 stsp
7384 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7385 0ebf8283 2019-07-24 stsp if (err)
7386 0ebf8283 2019-07-24 stsp return err;
7387 0ebf8283 2019-07-24 stsp
7388 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7389 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7390 0ebf8283 2019-07-24 stsp return NULL;
7391 0ebf8283 2019-07-24 stsp }
7392 0ebf8283 2019-07-24 stsp
7393 0ebf8283 2019-07-24 stsp const struct got_error *
7394 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7395 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7396 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7397 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7398 0ebf8283 2019-07-24 stsp {
7399 0ebf8283 2019-07-24 stsp const struct got_error *err;
7400 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7401 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7402 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7403 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7404 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7405 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7406 0ebf8283 2019-07-24 stsp
7407 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7408 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7409 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7410 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7411 0ebf8283 2019-07-24 stsp
7412 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7413 3e3a69f1 2019-07-25 stsp if (err)
7414 3e3a69f1 2019-07-25 stsp return err;
7415 3e3a69f1 2019-07-25 stsp
7416 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7417 3e3a69f1 2019-07-25 stsp if (err)
7418 f032f1f7 2019-08-04 stsp goto done;
7419 f032f1f7 2019-08-04 stsp
7420 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7421 f032f1f7 2019-08-04 stsp &have_staged_files);
7422 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7423 f032f1f7 2019-08-04 stsp goto done;
7424 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7425 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7426 3e3a69f1 2019-07-25 stsp goto done;
7427 f032f1f7 2019-08-04 stsp }
7428 3e3a69f1 2019-07-25 stsp
7429 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7430 0ebf8283 2019-07-24 stsp if (err)
7431 f032f1f7 2019-08-04 stsp goto done;
7432 0ebf8283 2019-07-24 stsp
7433 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7434 0ebf8283 2019-07-24 stsp if (err)
7435 0ebf8283 2019-07-24 stsp goto done;
7436 0ebf8283 2019-07-24 stsp
7437 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7438 0ebf8283 2019-07-24 stsp if (err)
7439 0ebf8283 2019-07-24 stsp goto done;
7440 0ebf8283 2019-07-24 stsp
7441 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7442 0ebf8283 2019-07-24 stsp worktree);
7443 0ebf8283 2019-07-24 stsp if (err)
7444 0ebf8283 2019-07-24 stsp goto done;
7445 0ebf8283 2019-07-24 stsp
7446 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7447 0ebf8283 2019-07-24 stsp if (err)
7448 0ebf8283 2019-07-24 stsp goto done;
7449 0ebf8283 2019-07-24 stsp
7450 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7451 0ebf8283 2019-07-24 stsp if (err)
7452 0ebf8283 2019-07-24 stsp goto done;
7453 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7454 0ebf8283 2019-07-24 stsp if (err)
7455 0ebf8283 2019-07-24 stsp goto done;
7456 0ebf8283 2019-07-24 stsp
7457 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7458 0ebf8283 2019-07-24 stsp if (err)
7459 0ebf8283 2019-07-24 stsp goto done;
7460 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7461 0ebf8283 2019-07-24 stsp if (err)
7462 0ebf8283 2019-07-24 stsp goto done;
7463 0ebf8283 2019-07-24 stsp
7464 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7465 0ebf8283 2019-07-24 stsp if (err)
7466 0ebf8283 2019-07-24 stsp goto done;
7467 0ebf8283 2019-07-24 stsp done:
7468 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7469 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7470 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7471 0ebf8283 2019-07-24 stsp if (commit_ref)
7472 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7473 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7474 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7475 0ebf8283 2019-07-24 stsp if (err) {
7476 0ebf8283 2019-07-24 stsp free(*commit_id);
7477 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7478 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7479 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7480 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7481 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7482 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7483 0ebf8283 2019-07-24 stsp }
7484 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7485 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7486 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7487 3e3a69f1 2019-07-25 stsp }
7488 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7489 0ebf8283 2019-07-24 stsp }
7490 0ebf8283 2019-07-24 stsp return err;
7491 0ebf8283 2019-07-24 stsp }
7492 0ebf8283 2019-07-24 stsp
7493 0ebf8283 2019-07-24 stsp static const struct got_error *
7494 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7495 0ebf8283 2019-07-24 stsp {
7496 0ebf8283 2019-07-24 stsp const struct got_error *err;
7497 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7498 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7499 0ebf8283 2019-07-24 stsp
7500 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7501 0ebf8283 2019-07-24 stsp if (err)
7502 0ebf8283 2019-07-24 stsp goto done;
7503 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7504 0ebf8283 2019-07-24 stsp if (err)
7505 0ebf8283 2019-07-24 stsp goto done;
7506 0ebf8283 2019-07-24 stsp
7507 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7508 0ebf8283 2019-07-24 stsp worktree);
7509 0ebf8283 2019-07-24 stsp if (err)
7510 0ebf8283 2019-07-24 stsp goto done;
7511 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7512 0ebf8283 2019-07-24 stsp if (err)
7513 0ebf8283 2019-07-24 stsp goto done;
7514 0ebf8283 2019-07-24 stsp
7515 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7516 0ebf8283 2019-07-24 stsp if (err)
7517 0ebf8283 2019-07-24 stsp goto done;
7518 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7519 0ebf8283 2019-07-24 stsp if (err)
7520 0ebf8283 2019-07-24 stsp goto done;
7521 0ebf8283 2019-07-24 stsp
7522 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7523 0ebf8283 2019-07-24 stsp if (err)
7524 0ebf8283 2019-07-24 stsp goto done;
7525 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7526 0ebf8283 2019-07-24 stsp if (err)
7527 0ebf8283 2019-07-24 stsp goto done;
7528 0ebf8283 2019-07-24 stsp done:
7529 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7530 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7531 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7532 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7533 0ebf8283 2019-07-24 stsp return err;
7534 0ebf8283 2019-07-24 stsp }
7535 0ebf8283 2019-07-24 stsp
7536 0ebf8283 2019-07-24 stsp const struct got_error *
7537 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7538 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7539 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7540 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7541 0ebf8283 2019-07-24 stsp {
7542 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7543 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7544 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7545 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7546 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7547 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7548 0ebf8283 2019-07-24 stsp
7549 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7550 0ebf8283 2019-07-24 stsp if (err)
7551 0ebf8283 2019-07-24 stsp return err;
7552 0ebf8283 2019-07-24 stsp
7553 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7554 945f9229 2022-04-16 thomas worktree->base_commit_id);
7555 945f9229 2022-04-16 thomas if (err)
7556 945f9229 2022-04-16 thomas goto done;
7557 945f9229 2022-04-16 thomas
7558 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7559 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7560 0ebf8283 2019-07-24 stsp if (err)
7561 0ebf8283 2019-07-24 stsp goto done;
7562 0ebf8283 2019-07-24 stsp
7563 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7564 0ebf8283 2019-07-24 stsp if (err)
7565 0ebf8283 2019-07-24 stsp goto done;
7566 0ebf8283 2019-07-24 stsp
7567 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7568 0ebf8283 2019-07-24 stsp if (err)
7569 0ebf8283 2019-07-24 stsp goto done;
7570 0ebf8283 2019-07-24 stsp
7571 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7572 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7573 0ebf8283 2019-07-24 stsp if (err)
7574 0ebf8283 2019-07-24 stsp goto done;
7575 0ebf8283 2019-07-24 stsp
7576 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7577 0ebf8283 2019-07-24 stsp if (err)
7578 0ebf8283 2019-07-24 stsp goto done;
7579 0ebf8283 2019-07-24 stsp
7580 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7581 0ebf8283 2019-07-24 stsp if (err)
7582 0ebf8283 2019-07-24 stsp goto done;
7583 0ebf8283 2019-07-24 stsp
7584 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7585 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7586 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7587 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7588 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7589 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7590 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7591 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7592 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7593 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7594 0ebf8283 2019-07-24 stsp if (err)
7595 1f1abb7e 2019-08-08 stsp goto sync;
7596 0ebf8283 2019-07-24 stsp
7597 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7598 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7599 0ebf8283 2019-07-24 stsp sync:
7600 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7601 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7602 0ebf8283 2019-07-24 stsp err = sync_err;
7603 0ebf8283 2019-07-24 stsp done:
7604 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7605 0ebf8283 2019-07-24 stsp free(tree_id);
7606 818c7501 2019-07-11 stsp free(fileindex_path);
7607 818c7501 2019-07-11 stsp
7608 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7609 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7610 818c7501 2019-07-11 stsp err = unlockerr;
7611 818c7501 2019-07-11 stsp return err;
7612 818c7501 2019-07-11 stsp }
7613 0ebf8283 2019-07-24 stsp
7614 0ebf8283 2019-07-24 stsp const struct got_error *
7615 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7616 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7617 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7618 0ebf8283 2019-07-24 stsp {
7619 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7620 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7621 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7622 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7623 0ebf8283 2019-07-24 stsp
7624 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7625 0ebf8283 2019-07-24 stsp if (err)
7626 0ebf8283 2019-07-24 stsp return err;
7627 0ebf8283 2019-07-24 stsp
7628 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7629 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7630 e600f124 2021-03-21 stsp if (err)
7631 e600f124 2021-03-21 stsp goto done;
7632 e600f124 2021-03-21 stsp
7633 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7634 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7635 0ebf8283 2019-07-24 stsp if (err)
7636 0ebf8283 2019-07-24 stsp goto done;
7637 0ebf8283 2019-07-24 stsp
7638 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7639 0ebf8283 2019-07-24 stsp if (err)
7640 0ebf8283 2019-07-24 stsp goto done;
7641 0ebf8283 2019-07-24 stsp
7642 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, 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 = got_worktree_set_head_ref(worktree, resolved);
7647 0ebf8283 2019-07-24 stsp if (err)
7648 0ebf8283 2019-07-24 stsp goto done;
7649 0ebf8283 2019-07-24 stsp
7650 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7651 a615e0e7 2020-12-16 stsp if (err)
7652 a615e0e7 2020-12-16 stsp goto done;
7653 a615e0e7 2020-12-16 stsp
7654 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7655 a615e0e7 2020-12-16 stsp if (err)
7656 a615e0e7 2020-12-16 stsp goto done;
7657 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7658 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7659 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7660 a615e0e7 2020-12-16 stsp err = sync_err;
7661 0ebf8283 2019-07-24 stsp done:
7662 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7663 a615e0e7 2020-12-16 stsp free(fileindex_path);
7664 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7665 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7666 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7667 0ebf8283 2019-07-24 stsp err = unlockerr;
7668 0ebf8283 2019-07-24 stsp return err;
7669 0ebf8283 2019-07-24 stsp }
7670 0ebf8283 2019-07-24 stsp
7671 0ebf8283 2019-07-24 stsp const struct got_error *
7672 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7673 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7674 0ebf8283 2019-07-24 stsp {
7675 0ebf8283 2019-07-24 stsp const struct got_error *err;
7676 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7677 0ebf8283 2019-07-24 stsp
7678 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7679 0ebf8283 2019-07-24 stsp if (err)
7680 0ebf8283 2019-07-24 stsp return err;
7681 0ebf8283 2019-07-24 stsp
7682 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7683 0ebf8283 2019-07-24 stsp if (err)
7684 0ebf8283 2019-07-24 stsp goto done;
7685 0ebf8283 2019-07-24 stsp
7686 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7687 0ebf8283 2019-07-24 stsp done:
7688 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7689 2822a352 2019-10-15 stsp return err;
7690 2822a352 2019-10-15 stsp }
7691 2822a352 2019-10-15 stsp
7692 2822a352 2019-10-15 stsp const struct got_error *
7693 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7694 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7695 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7696 2822a352 2019-10-15 stsp struct got_repository *repo)
7697 2822a352 2019-10-15 stsp {
7698 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7699 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7700 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7701 2822a352 2019-10-15 stsp
7702 2822a352 2019-10-15 stsp *fileindex = NULL;
7703 2822a352 2019-10-15 stsp *branch_ref = NULL;
7704 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7705 2822a352 2019-10-15 stsp
7706 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7707 2822a352 2019-10-15 stsp if (err)
7708 2822a352 2019-10-15 stsp return err;
7709 2822a352 2019-10-15 stsp
7710 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7711 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7712 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7713 2822a352 2019-10-15 stsp "update -b or different branch name required");
7714 2822a352 2019-10-15 stsp goto done;
7715 2822a352 2019-10-15 stsp }
7716 2822a352 2019-10-15 stsp
7717 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7718 2822a352 2019-10-15 stsp if (err)
7719 2822a352 2019-10-15 stsp goto done;
7720 2822a352 2019-10-15 stsp
7721 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7722 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7723 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7724 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7725 2822a352 2019-10-15 stsp &ok_arg);
7726 2822a352 2019-10-15 stsp if (err)
7727 2822a352 2019-10-15 stsp goto done;
7728 2822a352 2019-10-15 stsp
7729 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7730 2822a352 2019-10-15 stsp if (err)
7731 2822a352 2019-10-15 stsp goto done;
7732 2822a352 2019-10-15 stsp
7733 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7734 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7735 2822a352 2019-10-15 stsp done:
7736 2822a352 2019-10-15 stsp if (err) {
7737 2822a352 2019-10-15 stsp if (*branch_ref) {
7738 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7739 2822a352 2019-10-15 stsp *branch_ref = NULL;
7740 2822a352 2019-10-15 stsp }
7741 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7742 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7743 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7744 2822a352 2019-10-15 stsp }
7745 2822a352 2019-10-15 stsp if (*fileindex) {
7746 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7747 2822a352 2019-10-15 stsp *fileindex = NULL;
7748 2822a352 2019-10-15 stsp }
7749 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7750 2822a352 2019-10-15 stsp }
7751 0cb83759 2019-08-03 stsp return err;
7752 0cb83759 2019-08-03 stsp }
7753 0cb83759 2019-08-03 stsp
7754 2822a352 2019-10-15 stsp const struct got_error *
7755 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7756 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7757 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7758 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7759 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7760 2822a352 2019-10-15 stsp {
7761 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7762 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7763 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7764 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7765 2822a352 2019-10-15 stsp
7766 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7767 2822a352 2019-10-15 stsp if (err)
7768 2822a352 2019-10-15 stsp goto done;
7769 2822a352 2019-10-15 stsp
7770 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7771 2822a352 2019-10-15 stsp if (err)
7772 2822a352 2019-10-15 stsp goto done;
7773 2822a352 2019-10-15 stsp
7774 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7775 945f9229 2022-04-16 thomas if (err)
7776 945f9229 2022-04-16 thomas goto done;
7777 945f9229 2022-04-16 thomas
7778 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7779 2822a352 2019-10-15 stsp worktree->path_prefix);
7780 2822a352 2019-10-15 stsp if (err)
7781 2822a352 2019-10-15 stsp goto done;
7782 2822a352 2019-10-15 stsp
7783 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7784 2822a352 2019-10-15 stsp if (err)
7785 2822a352 2019-10-15 stsp goto done;
7786 2822a352 2019-10-15 stsp
7787 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7788 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7789 2822a352 2019-10-15 stsp if (err)
7790 2822a352 2019-10-15 stsp goto sync;
7791 2822a352 2019-10-15 stsp
7792 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7793 2822a352 2019-10-15 stsp if (err)
7794 2822a352 2019-10-15 stsp goto sync;
7795 2822a352 2019-10-15 stsp
7796 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7797 6e210706 2021-01-22 stsp if (err)
7798 6e210706 2021-01-22 stsp goto sync;
7799 6e210706 2021-01-22 stsp
7800 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7801 2822a352 2019-10-15 stsp sync:
7802 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7803 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7804 2822a352 2019-10-15 stsp err = sync_err;
7805 2822a352 2019-10-15 stsp
7806 2822a352 2019-10-15 stsp done:
7807 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7808 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7809 2822a352 2019-10-15 stsp err = unlockerr;
7810 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7811 2822a352 2019-10-15 stsp
7812 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7813 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7814 2822a352 2019-10-15 stsp err = unlockerr;
7815 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7816 2822a352 2019-10-15 stsp
7817 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7818 2822a352 2019-10-15 stsp free(fileindex_path);
7819 2822a352 2019-10-15 stsp free(tree_id);
7820 945f9229 2022-04-16 thomas if (commit)
7821 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7822 2822a352 2019-10-15 stsp
7823 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7824 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7825 2822a352 2019-10-15 stsp err = unlockerr;
7826 2822a352 2019-10-15 stsp return err;
7827 2822a352 2019-10-15 stsp }
7828 2822a352 2019-10-15 stsp
7829 2822a352 2019-10-15 stsp const struct got_error *
7830 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7831 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7832 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7833 2822a352 2019-10-15 stsp {
7834 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7835 8b692cd0 2019-10-21 stsp
7836 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7837 8b692cd0 2019-10-21 stsp
7838 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7839 8b692cd0 2019-10-21 stsp
7840 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7841 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7842 8b692cd0 2019-10-21 stsp err = unlockerr;
7843 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7844 8b692cd0 2019-10-21 stsp
7845 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7846 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7847 8b692cd0 2019-10-21 stsp err = unlockerr;
7848 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7849 10604dce 2021-09-24 thomas
7850 10604dce 2021-09-24 thomas return err;
7851 10604dce 2021-09-24 thomas }
7852 10604dce 2021-09-24 thomas
7853 10604dce 2021-09-24 thomas const struct got_error *
7854 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7855 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7856 10604dce 2021-09-24 thomas {
7857 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7858 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7859 10604dce 2021-09-24 thomas
7860 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7861 10604dce 2021-09-24 thomas if (err)
7862 10604dce 2021-09-24 thomas goto done;
7863 8b692cd0 2019-10-21 stsp
7864 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7865 10604dce 2021-09-24 thomas
7866 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7867 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7868 10604dce 2021-09-24 thomas err = sync_err;
7869 10604dce 2021-09-24 thomas done:
7870 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7871 10604dce 2021-09-24 thomas free(fileindex_path);
7872 8b692cd0 2019-10-21 stsp return err;
7873 2822a352 2019-10-15 stsp }
7874 2822a352 2019-10-15 stsp
7875 10604dce 2021-09-24 thomas static const struct got_error *
7876 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7877 10604dce 2021-09-24 thomas {
7878 10604dce 2021-09-24 thomas const struct got_error *err;
7879 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7880 10604dce 2021-09-24 thomas
7881 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7882 10604dce 2021-09-24 thomas if (err)
7883 10604dce 2021-09-24 thomas goto done;
7884 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7885 10604dce 2021-09-24 thomas if (err)
7886 10604dce 2021-09-24 thomas goto done;
7887 10604dce 2021-09-24 thomas
7888 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7889 10604dce 2021-09-24 thomas if (err)
7890 10604dce 2021-09-24 thomas goto done;
7891 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
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 done:
7896 10604dce 2021-09-24 thomas free(branch_refname);
7897 10604dce 2021-09-24 thomas free(commit_refname);
7898 10604dce 2021-09-24 thomas return err;
7899 10604dce 2021-09-24 thomas }
7900 10604dce 2021-09-24 thomas
7901 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7902 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7903 10604dce 2021-09-24 thomas const char *branch_name;
7904 10604dce 2021-09-24 thomas };
7905 10604dce 2021-09-24 thomas
7906 10604dce 2021-09-24 thomas static const struct got_error *
7907 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7908 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
7909 10604dce 2021-09-24 thomas {
7910 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7911 10604dce 2021-09-24 thomas
7912 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7913 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7914 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7915 10604dce 2021-09-24 thomas
7916 10604dce 2021-09-24 thomas return NULL;
7917 10604dce 2021-09-24 thomas }
7918 10604dce 2021-09-24 thomas
7919 10604dce 2021-09-24 thomas
7920 10604dce 2021-09-24 thomas const struct got_error *
7921 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7922 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7923 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7924 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7925 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7926 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7927 10604dce 2021-09-24 thomas {
7928 10604dce 2021-09-24 thomas const struct got_error *err;
7929 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7930 10604dce 2021-09-24 thomas
7931 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7932 10604dce 2021-09-24 thomas if (err)
7933 10604dce 2021-09-24 thomas goto done;
7934 10604dce 2021-09-24 thomas
7935 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7936 10604dce 2021-09-24 thomas worktree);
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 = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7941 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7942 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7943 10604dce 2021-09-24 thomas done:
7944 10604dce 2021-09-24 thomas free(fileindex_path);
7945 10604dce 2021-09-24 thomas return err;
7946 10604dce 2021-09-24 thomas }
7947 10604dce 2021-09-24 thomas
7948 10604dce 2021-09-24 thomas const struct got_error *
7949 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7950 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7951 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7952 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7953 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
7954 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7955 ae1e948a 2021-09-28 thomas
7956 10604dce 2021-09-24 thomas {
7957 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7958 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7959 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7960 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7961 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7962 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7963 10604dce 2021-09-24 thomas int have_staged_files = 0;
7964 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7965 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7966 10604dce 2021-09-24 thomas
7967 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7968 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7969 10604dce 2021-09-24 thomas
7970 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7971 10604dce 2021-09-24 thomas
7972 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7973 10604dce 2021-09-24 thomas if (err)
7974 10604dce 2021-09-24 thomas goto done;
7975 10604dce 2021-09-24 thomas
7976 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7977 10604dce 2021-09-24 thomas if (err)
7978 10604dce 2021-09-24 thomas goto done;
7979 10604dce 2021-09-24 thomas
7980 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7981 10604dce 2021-09-24 thomas if (err)
7982 10604dce 2021-09-24 thomas goto done;
7983 10604dce 2021-09-24 thomas
7984 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7985 10604dce 2021-09-24 thomas &have_staged_files);
7986 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7987 10604dce 2021-09-24 thomas goto done;
7988 10604dce 2021-09-24 thomas if (have_staged_files) {
7989 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7990 10604dce 2021-09-24 thomas goto done;
7991 10604dce 2021-09-24 thomas }
7992 10604dce 2021-09-24 thomas
7993 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7994 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7995 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7996 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7997 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7998 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7999 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8000 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8001 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8002 10604dce 2021-09-24 thomas if (err)
8003 10604dce 2021-09-24 thomas goto done;
8004 10604dce 2021-09-24 thomas
8005 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
8006 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8007 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
8008 10604dce 2021-09-24 thomas goto done;
8009 10604dce 2021-09-24 thomas }
8010 10604dce 2021-09-24 thomas
8011 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8012 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8013 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
8014 10604dce 2021-09-24 thomas
8015 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
8016 10604dce 2021-09-24 thomas ct_path++;
8017 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
8018 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8019 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8020 10604dce 2021-09-24 thomas if (err)
8021 10604dce 2021-09-24 thomas goto done;
8022 10604dce 2021-09-24 thomas
8023 10604dce 2021-09-24 thomas }
8024 10604dce 2021-09-24 thomas
8025 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8026 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8027 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8028 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8029 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8030 10604dce 2021-09-24 thomas if (err)
8031 10604dce 2021-09-24 thomas goto done;
8032 10604dce 2021-09-24 thomas
8033 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8034 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8035 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8036 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8037 10604dce 2021-09-24 thomas err = sync_err;
8038 10604dce 2021-09-24 thomas done:
8039 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8040 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8041 21c2d8be 2023-01-10 thomas
8042 10604dce 2021-09-24 thomas free_commitable(ct);
8043 10604dce 2021-09-24 thomas }
8044 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8045 10604dce 2021-09-24 thomas free(fileindex_path);
8046 10604dce 2021-09-24 thomas return err;
8047 10604dce 2021-09-24 thomas }
8048 10604dce 2021-09-24 thomas
8049 10604dce 2021-09-24 thomas const struct got_error *
8050 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8051 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8052 10604dce 2021-09-24 thomas {
8053 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8054 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8055 10604dce 2021-09-24 thomas
8056 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8057 10604dce 2021-09-24 thomas if (err)
8058 10604dce 2021-09-24 thomas goto done;
8059 10604dce 2021-09-24 thomas
8060 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8061 10604dce 2021-09-24 thomas if (err)
8062 10604dce 2021-09-24 thomas goto done;
8063 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8064 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8065 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8066 10604dce 2021-09-24 thomas err = sync_err;
8067 10604dce 2021-09-24 thomas done:
8068 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8069 10604dce 2021-09-24 thomas free(fileindex_path);
8070 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8071 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8072 10604dce 2021-09-24 thomas err = unlockerr;
8073 10604dce 2021-09-24 thomas return err;
8074 10604dce 2021-09-24 thomas }
8075 10604dce 2021-09-24 thomas
8076 10604dce 2021-09-24 thomas const struct got_error *
8077 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8078 10604dce 2021-09-24 thomas struct got_repository *repo)
8079 10604dce 2021-09-24 thomas {
8080 10604dce 2021-09-24 thomas const struct got_error *err;
8081 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8082 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8083 10604dce 2021-09-24 thomas
8084 10604dce 2021-09-24 thomas *in_progress = 0;
8085 10604dce 2021-09-24 thomas
8086 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8087 10604dce 2021-09-24 thomas if (err)
8088 10604dce 2021-09-24 thomas return err;
8089 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8090 dfe86d1f 2021-09-24 thomas free(branch_refname);
8091 10604dce 2021-09-24 thomas if (err) {
8092 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8093 10604dce 2021-09-24 thomas return err;
8094 10604dce 2021-09-24 thomas } else
8095 10604dce 2021-09-24 thomas *in_progress = 1;
8096 10604dce 2021-09-24 thomas
8097 10604dce 2021-09-24 thomas return NULL;
8098 10604dce 2021-09-24 thomas }
8099 10604dce 2021-09-24 thomas
8100 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8101 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8102 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
8103 10604dce 2021-09-24 thomas {
8104 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8105 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8106 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8107 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8108 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
8109 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8110 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8111 10604dce 2021-09-24 thomas
8112 10604dce 2021-09-24 thomas *fileindex = NULL;
8113 10604dce 2021-09-24 thomas
8114 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8115 10604dce 2021-09-24 thomas if (err)
8116 10604dce 2021-09-24 thomas return err;
8117 10604dce 2021-09-24 thomas
8118 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8119 10604dce 2021-09-24 thomas if (err)
8120 10604dce 2021-09-24 thomas goto done;
8121 10604dce 2021-09-24 thomas
8122 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8123 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8124 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8125 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8126 10604dce 2021-09-24 thomas &ok_arg);
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 err = get_merge_branch_ref_name(&branch_refname, worktree);
8131 10604dce 2021-09-24 thomas if (err)
8132 10604dce 2021-09-24 thomas return err;
8133 10604dce 2021-09-24 thomas
8134 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8135 10604dce 2021-09-24 thomas if (err)
8136 10604dce 2021-09-24 thomas return err;
8137 10604dce 2021-09-24 thomas
8138 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8139 10604dce 2021-09-24 thomas 0);
8140 10604dce 2021-09-24 thomas if (err)
8141 10604dce 2021-09-24 thomas goto done;
8142 10604dce 2021-09-24 thomas
8143 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8144 10604dce 2021-09-24 thomas if (err)
8145 10604dce 2021-09-24 thomas goto done;
8146 10604dce 2021-09-24 thomas
8147 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8148 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8149 10604dce 2021-09-24 thomas goto done;
8150 10604dce 2021-09-24 thomas }
8151 10604dce 2021-09-24 thomas
8152 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8153 10604dce 2021-09-24 thomas if (err)
8154 10604dce 2021-09-24 thomas goto done;
8155 10604dce 2021-09-24 thomas
8156 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8157 10604dce 2021-09-24 thomas if (err)
8158 10604dce 2021-09-24 thomas goto done;
8159 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8160 10604dce 2021-09-24 thomas if (err)
8161 10604dce 2021-09-24 thomas goto done;
8162 10604dce 2021-09-24 thomas
8163 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8164 10604dce 2021-09-24 thomas if (err)
8165 10604dce 2021-09-24 thomas goto done;
8166 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8167 10604dce 2021-09-24 thomas if (err)
8168 10604dce 2021-09-24 thomas goto done;
8169 10604dce 2021-09-24 thomas
8170 10604dce 2021-09-24 thomas done:
8171 10604dce 2021-09-24 thomas free(branch_refname);
8172 10604dce 2021-09-24 thomas free(commit_refname);
8173 10604dce 2021-09-24 thomas free(fileindex_path);
8174 10604dce 2021-09-24 thomas if (branch_ref)
8175 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8176 10604dce 2021-09-24 thomas if (commit_ref)
8177 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8178 10604dce 2021-09-24 thomas if (wt_branch)
8179 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
8180 10604dce 2021-09-24 thomas free(wt_branch_tip);
8181 10604dce 2021-09-24 thomas if (err) {
8182 10604dce 2021-09-24 thomas if (*fileindex) {
8183 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8184 10604dce 2021-09-24 thomas *fileindex = NULL;
8185 10604dce 2021-09-24 thomas }
8186 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8187 10604dce 2021-09-24 thomas }
8188 10604dce 2021-09-24 thomas return err;
8189 10604dce 2021-09-24 thomas }
8190 10604dce 2021-09-24 thomas
8191 10604dce 2021-09-24 thomas const struct got_error *
8192 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8193 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8194 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8195 10604dce 2021-09-24 thomas {
8196 10604dce 2021-09-24 thomas const struct got_error *err;
8197 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8198 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8199 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8200 10604dce 2021-09-24 thomas int have_staged_files = 0;
8201 10604dce 2021-09-24 thomas
8202 10604dce 2021-09-24 thomas *branch_name = NULL;
8203 10604dce 2021-09-24 thomas *branch_tip = NULL;
8204 10604dce 2021-09-24 thomas *fileindex = NULL;
8205 10604dce 2021-09-24 thomas
8206 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8207 10604dce 2021-09-24 thomas if (err)
8208 10604dce 2021-09-24 thomas return err;
8209 10604dce 2021-09-24 thomas
8210 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8211 10604dce 2021-09-24 thomas if (err)
8212 10604dce 2021-09-24 thomas goto done;
8213 10604dce 2021-09-24 thomas
8214 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8215 10604dce 2021-09-24 thomas &have_staged_files);
8216 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8217 10604dce 2021-09-24 thomas goto done;
8218 10604dce 2021-09-24 thomas if (have_staged_files) {
8219 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8220 10604dce 2021-09-24 thomas goto done;
8221 10604dce 2021-09-24 thomas }
8222 10604dce 2021-09-24 thomas
8223 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8224 10604dce 2021-09-24 thomas if (err)
8225 10604dce 2021-09-24 thomas goto done;
8226 10604dce 2021-09-24 thomas
8227 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8228 10604dce 2021-09-24 thomas if (err)
8229 10604dce 2021-09-24 thomas goto done;
8230 10604dce 2021-09-24 thomas
8231 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8232 10604dce 2021-09-24 thomas if (err)
8233 10604dce 2021-09-24 thomas goto done;
8234 10604dce 2021-09-24 thomas
8235 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8236 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8237 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8238 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8239 10604dce 2021-09-24 thomas goto done;
8240 10604dce 2021-09-24 thomas }
8241 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8242 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8243 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8244 10604dce 2021-09-24 thomas goto done;
8245 10604dce 2021-09-24 thomas }
8246 10604dce 2021-09-24 thomas
8247 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8248 10604dce 2021-09-24 thomas if (err)
8249 10604dce 2021-09-24 thomas goto done;
8250 10604dce 2021-09-24 thomas
8251 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8252 10604dce 2021-09-24 thomas if (err)
8253 10604dce 2021-09-24 thomas goto done;
8254 10604dce 2021-09-24 thomas done:
8255 10604dce 2021-09-24 thomas free(commit_refname);
8256 10604dce 2021-09-24 thomas free(branch_refname);
8257 10604dce 2021-09-24 thomas free(fileindex_path);
8258 10604dce 2021-09-24 thomas if (commit_ref)
8259 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8260 10604dce 2021-09-24 thomas if (branch_ref)
8261 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8262 10604dce 2021-09-24 thomas if (err) {
8263 10604dce 2021-09-24 thomas if (*branch_name) {
8264 10604dce 2021-09-24 thomas free(*branch_name);
8265 10604dce 2021-09-24 thomas *branch_name = NULL;
8266 10604dce 2021-09-24 thomas }
8267 10604dce 2021-09-24 thomas free(*branch_tip);
8268 10604dce 2021-09-24 thomas *branch_tip = NULL;
8269 10604dce 2021-09-24 thomas if (*fileindex) {
8270 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8271 10604dce 2021-09-24 thomas *fileindex = NULL;
8272 10604dce 2021-09-24 thomas }
8273 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8274 10604dce 2021-09-24 thomas }
8275 10604dce 2021-09-24 thomas return err;
8276 10604dce 2021-09-24 thomas }
8277 10604dce 2021-09-24 thomas
8278 10604dce 2021-09-24 thomas const struct got_error *
8279 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8280 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8281 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8282 10604dce 2021-09-24 thomas {
8283 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8284 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
8285 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8286 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8287 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8288 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8289 10604dce 2021-09-24 thomas
8290 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8291 945f9229 2022-04-16 thomas worktree->base_commit_id);
8292 945f9229 2022-04-16 thomas if (err)
8293 945f9229 2022-04-16 thomas goto done;
8294 945f9229 2022-04-16 thomas
8295 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8296 945f9229 2022-04-16 thomas worktree->path_prefix);
8297 10604dce 2021-09-24 thomas if (err)
8298 10604dce 2021-09-24 thomas goto done;
8299 10604dce 2021-09-24 thomas
8300 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8301 10604dce 2021-09-24 thomas if (err)
8302 10604dce 2021-09-24 thomas goto done;
8303 10604dce 2021-09-24 thomas
8304 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8305 10604dce 2021-09-24 thomas if (err)
8306 10604dce 2021-09-24 thomas goto done;
8307 10604dce 2021-09-24 thomas
8308 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8309 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8310 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8311 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8312 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8313 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8314 10604dce 2021-09-24 thomas rfa.repo = repo;
8315 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8316 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8317 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8318 10604dce 2021-09-24 thomas if (err)
8319 10604dce 2021-09-24 thomas goto sync;
8320 10604dce 2021-09-24 thomas
8321 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8322 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8323 10604dce 2021-09-24 thomas sync:
8324 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8325 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8326 10604dce 2021-09-24 thomas err = sync_err;
8327 10604dce 2021-09-24 thomas done:
8328 10604dce 2021-09-24 thomas free(tree_id);
8329 10604dce 2021-09-24 thomas free(commit_id);
8330 945f9229 2022-04-16 thomas if (commit)
8331 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8332 10604dce 2021-09-24 thomas if (fileindex)
8333 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8334 10604dce 2021-09-24 thomas free(fileindex_path);
8335 10604dce 2021-09-24 thomas
8336 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8337 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8338 10604dce 2021-09-24 thomas err = unlockerr;
8339 10604dce 2021-09-24 thomas return err;
8340 10604dce 2021-09-24 thomas }
8341 10604dce 2021-09-24 thomas
8342 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8343 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8344 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8345 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8346 2db2652d 2019-08-07 stsp struct got_repository *repo;
8347 2db2652d 2019-08-07 stsp int have_changes;
8348 2db2652d 2019-08-07 stsp };
8349 2db2652d 2019-08-07 stsp
8350 ef20f542 2022-06-26 thomas static const struct got_error *
8351 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8352 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8353 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8354 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8355 735ef5ac 2019-08-03 stsp {
8356 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8357 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8358 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8359 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8360 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8361 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8362 8b13ce36 2019-08-08 stsp
8363 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8364 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8365 8b13ce36 2019-08-08 stsp return NULL;
8366 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8367 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8368 735ef5ac 2019-08-03 stsp
8369 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8370 735ef5ac 2019-08-03 stsp if (ie == NULL)
8371 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8372 735ef5ac 2019-08-03 stsp
8373 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8374 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8375 735ef5ac 2019-08-03 stsp relpath) == -1)
8376 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8377 735ef5ac 2019-08-03 stsp
8378 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8379 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8380 43010591 2023-02-17 thomas &base_commit_id, ie);
8381 735ef5ac 2019-08-03 stsp }
8382 735ef5ac 2019-08-03 stsp
8383 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8384 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8385 3aa5969e 2019-08-06 stsp goto done;
8386 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8387 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8388 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8389 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8390 735ef5ac 2019-08-03 stsp goto done;
8391 3aa5969e 2019-08-06 stsp }
8392 735ef5ac 2019-08-03 stsp
8393 2db2652d 2019-08-07 stsp a->have_changes = 1;
8394 2db2652d 2019-08-07 stsp
8395 735ef5ac 2019-08-03 stsp p = in_repo_path;
8396 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8397 735ef5ac 2019-08-03 stsp p++;
8398 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8399 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8400 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8401 735ef5ac 2019-08-03 stsp done:
8402 735ef5ac 2019-08-03 stsp free(in_repo_path);
8403 dc424a06 2019-08-07 stsp return err;
8404 dc424a06 2019-08-07 stsp }
8405 dc424a06 2019-08-07 stsp
8406 2db2652d 2019-08-07 stsp struct stage_path_arg {
8407 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8408 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8409 2db2652d 2019-08-07 stsp struct got_repository *repo;
8410 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8411 2db2652d 2019-08-07 stsp void *status_arg;
8412 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8413 2db2652d 2019-08-07 stsp void *patch_arg;
8414 7b5dc508 2019-10-28 stsp int staged_something;
8415 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8416 2db2652d 2019-08-07 stsp };
8417 2db2652d 2019-08-07 stsp
8418 2db2652d 2019-08-07 stsp static const struct got_error *
8419 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8420 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8421 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8422 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8423 0cb83759 2019-08-03 stsp {
8424 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8425 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8426 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8427 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8428 0cb83759 2019-08-03 stsp uint32_t stage;
8429 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8430 0aeb8099 2020-07-23 stsp struct stat sb;
8431 8b13ce36 2019-08-08 stsp
8432 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8433 8b13ce36 2019-08-08 stsp return NULL;
8434 0cb83759 2019-08-03 stsp
8435 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8436 d3e7c587 2019-08-03 stsp if (ie == NULL)
8437 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8438 0cb83759 2019-08-03 stsp
8439 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8440 2db2652d 2019-08-07 stsp relpath)== -1)
8441 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8442 0cb83759 2019-08-03 stsp
8443 0cb83759 2019-08-03 stsp switch (status) {
8444 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8445 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8446 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8447 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8448 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8449 0aeb8099 2020-07-23 stsp break;
8450 0aeb8099 2020-07-23 stsp }
8451 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8452 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8453 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8454 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8455 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8456 dc424a06 2019-08-07 stsp if (err)
8457 dc424a06 2019-08-07 stsp break;
8458 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8459 dc424a06 2019-08-07 stsp break;
8460 dc424a06 2019-08-07 stsp } else {
8461 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8462 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8463 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8464 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8465 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8466 dc424a06 2019-08-07 stsp break;
8467 dc424a06 2019-08-07 stsp }
8468 dc424a06 2019-08-07 stsp }
8469 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8470 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8471 0cb83759 2019-08-03 stsp if (err)
8472 dc424a06 2019-08-07 stsp break;
8473 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8474 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8475 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8476 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8477 0cb83759 2019-08-03 stsp else
8478 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8479 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8480 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8481 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8482 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8483 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8484 35213c7c 2020-07-23 stsp ssize_t target_len;
8485 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8486 35213c7c 2020-07-23 stsp sizeof(target_path));
8487 35213c7c 2020-07-23 stsp if (target_len == -1) {
8488 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8489 35213c7c 2020-07-23 stsp ondisk_path);
8490 35213c7c 2020-07-23 stsp break;
8491 35213c7c 2020-07-23 stsp }
8492 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8493 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8494 35213c7c 2020-07-23 stsp a->worktree->root_path);
8495 35213c7c 2020-07-23 stsp if (err)
8496 35213c7c 2020-07-23 stsp break;
8497 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8498 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8499 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8500 35213c7c 2020-07-23 stsp break;
8501 35213c7c 2020-07-23 stsp }
8502 35213c7c 2020-07-23 stsp }
8503 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8504 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8505 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8506 35213c7c 2020-07-23 stsp else
8507 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8508 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8509 0aeb8099 2020-07-23 stsp } else {
8510 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8511 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8512 0aeb8099 2020-07-23 stsp }
8513 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8514 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8515 dc424a06 2019-08-07 stsp break;
8516 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8517 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8518 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8519 f289c82c 2022-06-13 thomas if (err)
8520 f289c82c 2022-06-13 thomas break;
8521 f289c82c 2022-06-13 thomas /*
8522 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8523 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8524 f289c82c 2022-06-13 thomas */
8525 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8526 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8527 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8528 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8529 f289c82c 2022-06-13 thomas }
8530 0cb83759 2019-08-03 stsp break;
8531 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8532 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8533 d3e7c587 2019-08-03 stsp break;
8534 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8535 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8536 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8537 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8538 dc424a06 2019-08-07 stsp if (err)
8539 dc424a06 2019-08-07 stsp break;
8540 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8541 88f33a19 2019-08-08 stsp break;
8542 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8543 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8544 dc424a06 2019-08-07 stsp break;
8545 88f33a19 2019-08-08 stsp }
8546 dc424a06 2019-08-07 stsp }
8547 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8548 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8549 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8550 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8551 dc424a06 2019-08-07 stsp break;
8552 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8553 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8554 12463d8b 2019-12-13 stsp de_name);
8555 0cb83759 2019-08-03 stsp break;
8556 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8557 d3e7c587 2019-08-03 stsp break;
8558 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8559 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8560 ebf48fd5 2019-08-03 stsp break;
8561 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8562 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8563 2a06fe5f 2019-08-24 stsp break;
8564 0cb83759 2019-08-03 stsp default:
8565 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8566 537ac44b 2019-08-03 stsp break;
8567 0cb83759 2019-08-03 stsp }
8568 dc424a06 2019-08-07 stsp
8569 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8570 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8571 dc424a06 2019-08-07 stsp free(path_content);
8572 2db2652d 2019-08-07 stsp free(ondisk_path);
8573 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8574 0ebf8283 2019-07-24 stsp return err;
8575 0ebf8283 2019-07-24 stsp }
8576 0cb83759 2019-08-03 stsp
8577 0cb83759 2019-08-03 stsp const struct got_error *
8578 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8579 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8580 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8581 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8582 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8583 0cb83759 2019-08-03 stsp {
8584 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8585 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8586 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8587 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8588 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8589 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8590 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8591 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8592 0cb83759 2019-08-03 stsp
8593 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8594 0cb83759 2019-08-03 stsp if (err)
8595 0cb83759 2019-08-03 stsp return err;
8596 0cb83759 2019-08-03 stsp
8597 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8598 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8599 735ef5ac 2019-08-03 stsp if (err)
8600 735ef5ac 2019-08-03 stsp goto done;
8601 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8602 735ef5ac 2019-08-03 stsp if (err)
8603 735ef5ac 2019-08-03 stsp goto done;
8604 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8605 0cb83759 2019-08-03 stsp if (err)
8606 0cb83759 2019-08-03 stsp goto done;
8607 0cb83759 2019-08-03 stsp
8608 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8609 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8610 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8611 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8612 2db2652d 2019-08-07 stsp oka.repo = repo;
8613 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8614 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8615 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8616 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8617 735ef5ac 2019-08-03 stsp if (err)
8618 735ef5ac 2019-08-03 stsp goto done;
8619 735ef5ac 2019-08-03 stsp }
8620 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8621 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8622 2db2652d 2019-08-07 stsp goto done;
8623 2db2652d 2019-08-07 stsp }
8624 735ef5ac 2019-08-03 stsp
8625 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8626 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8627 2db2652d 2019-08-07 stsp spa.repo = repo;
8628 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8629 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8630 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8631 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8632 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8633 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8634 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8635 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8636 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8637 42005733 2019-08-03 stsp if (err)
8638 2db2652d 2019-08-07 stsp goto done;
8639 7b5dc508 2019-10-28 stsp }
8640 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8641 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8642 7b5dc508 2019-10-28 stsp goto done;
8643 0cb83759 2019-08-03 stsp }
8644 0cb83759 2019-08-03 stsp
8645 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8646 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8647 0cb83759 2019-08-03 stsp err = sync_err;
8648 0cb83759 2019-08-03 stsp done:
8649 735ef5ac 2019-08-03 stsp if (head_ref)
8650 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8651 735ef5ac 2019-08-03 stsp free(head_commit_id);
8652 0cb83759 2019-08-03 stsp free(fileindex_path);
8653 0cb83759 2019-08-03 stsp if (fileindex)
8654 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8655 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8656 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8657 0cb83759 2019-08-03 stsp err = unlockerr;
8658 0cb83759 2019-08-03 stsp return err;
8659 0cb83759 2019-08-03 stsp }
8660 ad493afc 2019-08-03 stsp
8661 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8662 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8663 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8664 ad493afc 2019-08-03 stsp struct got_repository *repo;
8665 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8666 ad493afc 2019-08-03 stsp void *progress_arg;
8667 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8668 2e1f37b0 2019-08-08 stsp void *patch_arg;
8669 ad493afc 2019-08-03 stsp };
8670 2e1f37b0 2019-08-08 stsp
8671 2e1f37b0 2019-08-08 stsp static const struct got_error *
8672 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8673 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8674 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8675 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8676 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8677 2e1f37b0 2019-08-08 stsp {
8678 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8679 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8680 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8681 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8682 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8683 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8684 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8685 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8686 2e1f37b0 2019-08-08 stsp
8687 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8688 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8689 2e1f37b0 2019-08-08 stsp
8690 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8691 2e1f37b0 2019-08-08 stsp if (err)
8692 2e1f37b0 2019-08-08 stsp return err;
8693 f4ae6ddb 2022-07-01 thomas
8694 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8695 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8696 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8697 f4ae6ddb 2022-07-01 thomas goto done;
8698 f4ae6ddb 2022-07-01 thomas }
8699 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8700 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8701 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8702 f4ae6ddb 2022-07-01 thomas goto done;
8703 f4ae6ddb 2022-07-01 thomas }
8704 f4ae6ddb 2022-07-01 thomas
8705 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8706 2e1f37b0 2019-08-08 stsp if (err)
8707 2e1f37b0 2019-08-08 stsp goto done;
8708 2e1f37b0 2019-08-08 stsp
8709 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8710 2e1f37b0 2019-08-08 stsp if (err)
8711 2e1f37b0 2019-08-08 stsp goto done;
8712 2e1f37b0 2019-08-08 stsp
8713 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8714 2e1f37b0 2019-08-08 stsp if (err)
8715 2e1f37b0 2019-08-08 stsp goto done;
8716 2e1f37b0 2019-08-08 stsp
8717 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8718 f4ae6ddb 2022-07-01 thomas fd2);
8719 2e1f37b0 2019-08-08 stsp if (err)
8720 2e1f37b0 2019-08-08 stsp goto done;
8721 2e1f37b0 2019-08-08 stsp
8722 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8723 2e1f37b0 2019-08-08 stsp if (err)
8724 2e1f37b0 2019-08-08 stsp goto done;
8725 ad493afc 2019-08-03 stsp
8726 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8727 2e1f37b0 2019-08-08 stsp if (err)
8728 2e1f37b0 2019-08-08 stsp goto done;
8729 2e1f37b0 2019-08-08 stsp
8730 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8731 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8732 2e1f37b0 2019-08-08 stsp if (err)
8733 2e1f37b0 2019-08-08 stsp goto done;
8734 2e1f37b0 2019-08-08 stsp
8735 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8736 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
8737 2e1f37b0 2019-08-08 stsp if (err)
8738 2e1f37b0 2019-08-08 stsp goto done;
8739 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8740 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
8741 2e1f37b0 2019-08-08 stsp if (err)
8742 2e1f37b0 2019-08-08 stsp goto done;
8743 2e1f37b0 2019-08-08 stsp
8744 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8745 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8746 2e1f37b0 2019-08-08 stsp goto done;
8747 2e1f37b0 2019-08-08 stsp }
8748 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8749 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8750 2e1f37b0 2019-08-08 stsp goto done;
8751 2e1f37b0 2019-08-08 stsp }
8752 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8753 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8754 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8755 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8756 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8757 fe621944 2020-11-10 stsp nchanges++;
8758 fe621944 2020-11-10 stsp }
8759 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8760 2e1f37b0 2019-08-08 stsp int choice;
8761 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8762 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8763 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8764 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8765 2e1f37b0 2019-08-08 stsp if (err)
8766 2e1f37b0 2019-08-08 stsp goto done;
8767 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8768 2e1f37b0 2019-08-08 stsp have_content = 1;
8769 19e4b907 2019-08-08 stsp else
8770 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8771 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8772 2e1f37b0 2019-08-08 stsp break;
8773 2e1f37b0 2019-08-08 stsp }
8774 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8775 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8776 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8777 2e1f37b0 2019-08-08 stsp done:
8778 2e1f37b0 2019-08-08 stsp free(label1);
8779 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8780 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8781 2e1f37b0 2019-08-08 stsp if (blob)
8782 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8783 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8784 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8785 2e1f37b0 2019-08-08 stsp if (staged_blob)
8786 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8787 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8788 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8789 fe621944 2020-11-10 stsp err = free_err;
8790 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8791 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8792 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8793 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8794 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8795 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8796 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8797 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8798 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8799 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8800 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8801 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8802 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8803 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8804 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8805 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8806 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8807 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8808 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8809 2e1f37b0 2019-08-08 stsp }
8810 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8811 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8812 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8813 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8814 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8815 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8816 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8817 2e1f37b0 2019-08-08 stsp }
8818 2e1f37b0 2019-08-08 stsp free(path1);
8819 2e1f37b0 2019-08-08 stsp free(path2);
8820 fda8017d 2020-07-23 stsp return err;
8821 fda8017d 2020-07-23 stsp }
8822 fda8017d 2020-07-23 stsp
8823 fda8017d 2020-07-23 stsp static const struct got_error *
8824 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8825 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8826 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8827 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8828 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8829 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8830 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8831 fda8017d 2020-07-23 stsp {
8832 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8833 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8834 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8835 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8836 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8837 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8838 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8839 fda8017d 2020-07-23 stsp struct stat sb;
8840 fda8017d 2020-07-23 stsp
8841 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8842 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8843 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8844 fda8017d 2020-07-23 stsp if (err)
8845 fda8017d 2020-07-23 stsp return err;
8846 fda8017d 2020-07-23 stsp
8847 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8848 fda8017d 2020-07-23 stsp return NULL;
8849 fda8017d 2020-07-23 stsp
8850 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8851 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8852 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8853 fda8017d 2020-07-23 stsp if (err)
8854 fda8017d 2020-07-23 stsp goto done;
8855 fda8017d 2020-07-23 stsp }
8856 fda8017d 2020-07-23 stsp
8857 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8858 fda8017d 2020-07-23 stsp if (f == NULL) {
8859 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8860 fda8017d 2020-07-23 stsp path_unstaged_content);
8861 fda8017d 2020-07-23 stsp goto done;
8862 fda8017d 2020-07-23 stsp }
8863 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8864 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8865 fda8017d 2020-07-23 stsp goto done;
8866 fda8017d 2020-07-23 stsp }
8867 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8868 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8869 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8870 fda8017d 2020-07-23 stsp size_t r;
8871 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8872 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8873 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8874 fda8017d 2020-07-23 stsp goto done;
8875 fda8017d 2020-07-23 stsp }
8876 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8877 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8878 fda8017d 2020-07-23 stsp goto done;
8879 fda8017d 2020-07-23 stsp }
8880 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8881 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8882 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8883 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8884 fda8017d 2020-07-23 stsp progress_arg);
8885 fda8017d 2020-07-23 stsp } else {
8886 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8887 67a66647 2021-05-31 stsp
8888 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8889 67a66647 2021-05-31 stsp if (err)
8890 67a66647 2021-05-31 stsp return err;
8891 67a66647 2021-05-31 stsp
8892 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8893 67a66647 2021-05-31 stsp parent) == -1) {
8894 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8895 67a66647 2021-05-31 stsp base_path = NULL;
8896 67a66647 2021-05-31 stsp goto done;
8897 67a66647 2021-05-31 stsp }
8898 67a66647 2021-05-31 stsp
8899 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
8900 fc2a50f2 2022-11-01 thomas base_path, "");
8901 67a66647 2021-05-31 stsp if (err)
8902 67a66647 2021-05-31 stsp goto done;
8903 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8904 67a66647 2021-05-31 stsp blob_base);
8905 67a66647 2021-05-31 stsp if (err)
8906 67a66647 2021-05-31 stsp goto done;
8907 67a66647 2021-05-31 stsp
8908 b6b86fd1 2022-08-30 thomas /*
8909 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8910 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8911 eec2f5a9 2021-06-03 stsp */
8912 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8913 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8914 eec2f5a9 2021-06-03 stsp ondisk_path);
8915 eec2f5a9 2021-06-03 stsp if (err)
8916 eec2f5a9 2021-06-03 stsp goto done;
8917 eec2f5a9 2021-06-03 stsp } else {
8918 eec2f5a9 2021-06-03 stsp int fd;
8919 06340621 2021-12-31 thomas fd = open(ondisk_path,
8920 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8921 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8922 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8923 eec2f5a9 2021-06-03 stsp goto done;
8924 eec2f5a9 2021-06-03 stsp }
8925 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8926 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8927 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8928 eec2f5a9 2021-06-03 stsp close(fd);
8929 eec2f5a9 2021-06-03 stsp goto done;
8930 eec2f5a9 2021-06-03 stsp }
8931 eec2f5a9 2021-06-03 stsp }
8932 eec2f5a9 2021-06-03 stsp
8933 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8934 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8935 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8936 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8937 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8938 fda8017d 2020-07-23 stsp }
8939 fda8017d 2020-07-23 stsp if (err)
8940 fda8017d 2020-07-23 stsp goto done;
8941 fda8017d 2020-07-23 stsp
8942 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8943 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8944 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8945 2ac8aa02 2020-11-09 stsp } else {
8946 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8947 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8948 2ac8aa02 2020-11-09 stsp }
8949 fda8017d 2020-07-23 stsp done:
8950 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8951 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8952 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8953 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8954 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8955 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8956 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8957 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8958 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8959 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8960 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8961 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8962 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8963 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8964 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8965 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8966 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8967 67a66647 2021-05-31 stsp free(blob_base_path);
8968 67a66647 2021-05-31 stsp free(parent);
8969 67a66647 2021-05-31 stsp free(base_path);
8970 2e1f37b0 2019-08-08 stsp return err;
8971 2e1f37b0 2019-08-08 stsp }
8972 2e1f37b0 2019-08-08 stsp
8973 ad493afc 2019-08-03 stsp static const struct got_error *
8974 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8975 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8976 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8977 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8978 ad493afc 2019-08-03 stsp {
8979 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8980 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8981 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8982 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8983 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8984 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8985 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8986 9bc94a15 2019-08-03 stsp struct stat sb;
8987 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8988 ad493afc 2019-08-03 stsp
8989 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8990 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8991 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8992 2e1f37b0 2019-08-08 stsp return NULL;
8993 2e1f37b0 2019-08-08 stsp
8994 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8995 ad493afc 2019-08-03 stsp if (ie == NULL)
8996 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8997 9bc94a15 2019-08-03 stsp
8998 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8999 9bc94a15 2019-08-03 stsp == -1)
9000 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9001 ad493afc 2019-08-03 stsp
9002 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9003 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9004 f69721c3 2019-10-21 stsp if (err)
9005 f69721c3 2019-10-21 stsp goto done;
9006 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9007 f69721c3 2019-10-21 stsp id_str) == -1) {
9008 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9009 f4ae6ddb 2022-07-01 thomas goto done;
9010 f4ae6ddb 2022-07-01 thomas }
9011 f4ae6ddb 2022-07-01 thomas
9012 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9013 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9014 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9015 f4ae6ddb 2022-07-01 thomas goto done;
9016 f4ae6ddb 2022-07-01 thomas }
9017 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9018 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9019 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9020 f69721c3 2019-10-21 stsp goto done;
9021 f69721c3 2019-10-21 stsp }
9022 f69721c3 2019-10-21 stsp
9023 ad493afc 2019-08-03 stsp switch (staged_status) {
9024 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9025 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9026 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9027 ad493afc 2019-08-03 stsp if (err)
9028 ad493afc 2019-08-03 stsp break;
9029 ad493afc 2019-08-03 stsp /* fall through */
9030 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9031 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9032 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9033 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9034 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9035 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9036 2e1f37b0 2019-08-08 stsp if (err)
9037 2e1f37b0 2019-08-08 stsp break;
9038 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9039 2e1f37b0 2019-08-08 stsp break;
9040 2e1f37b0 2019-08-08 stsp } else {
9041 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9042 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9043 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9044 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9045 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9046 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9047 2e1f37b0 2019-08-08 stsp }
9048 2e1f37b0 2019-08-08 stsp }
9049 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9050 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9051 ad493afc 2019-08-03 stsp if (err)
9052 ad493afc 2019-08-03 stsp break;
9053 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9054 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9055 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9056 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9057 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9058 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9059 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9060 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9061 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9062 ea7786be 2020-07-23 stsp break;
9063 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9064 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9065 36bf999c 2020-07-23 stsp char *staged_target;
9066 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9067 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9068 36bf999c 2020-07-23 stsp if (err)
9069 36bf999c 2020-07-23 stsp goto done;
9070 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9071 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9072 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9073 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9074 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9075 36bf999c 2020-07-23 stsp free(staged_target);
9076 dfe9fba0 2020-07-23 stsp } else {
9077 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9078 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9079 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9080 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9081 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9082 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9083 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9084 dfe9fba0 2020-07-23 stsp }
9085 ea7786be 2020-07-23 stsp break;
9086 ea7786be 2020-07-23 stsp default:
9087 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9088 ea7786be 2020-07-23 stsp break;
9089 ea7786be 2020-07-23 stsp }
9090 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9091 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9092 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9093 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9094 2ac8aa02 2020-11-09 stsp }
9095 ad493afc 2019-08-03 stsp break;
9096 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9097 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9098 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9099 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9100 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9101 2e1f37b0 2019-08-08 stsp if (err)
9102 2e1f37b0 2019-08-08 stsp break;
9103 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9104 2e1f37b0 2019-08-08 stsp break;
9105 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9106 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9107 2e1f37b0 2019-08-08 stsp break;
9108 2e1f37b0 2019-08-08 stsp }
9109 2e1f37b0 2019-08-08 stsp }
9110 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9111 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9112 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9113 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9114 9bc94a15 2019-08-03 stsp if (err)
9115 9bc94a15 2019-08-03 stsp break;
9116 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9117 ad493afc 2019-08-03 stsp break;
9118 ad493afc 2019-08-03 stsp }
9119 f69721c3 2019-10-21 stsp done:
9120 ad493afc 2019-08-03 stsp free(ondisk_path);
9121 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9122 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9123 ad493afc 2019-08-03 stsp if (blob_base)
9124 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9125 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9126 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9127 ad493afc 2019-08-03 stsp if (blob_staged)
9128 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9129 f69721c3 2019-10-21 stsp free(id_str);
9130 f69721c3 2019-10-21 stsp free(label_orig);
9131 ad493afc 2019-08-03 stsp return err;
9132 ad493afc 2019-08-03 stsp }
9133 ad493afc 2019-08-03 stsp
9134 ad493afc 2019-08-03 stsp const struct got_error *
9135 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9136 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9137 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9138 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9139 ad493afc 2019-08-03 stsp struct got_repository *repo)
9140 ad493afc 2019-08-03 stsp {
9141 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9142 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9143 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9144 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9145 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9146 ad493afc 2019-08-03 stsp
9147 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9148 ad493afc 2019-08-03 stsp if (err)
9149 ad493afc 2019-08-03 stsp return err;
9150 ad493afc 2019-08-03 stsp
9151 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9152 ad493afc 2019-08-03 stsp if (err)
9153 ad493afc 2019-08-03 stsp goto done;
9154 ad493afc 2019-08-03 stsp
9155 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9156 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9157 ad493afc 2019-08-03 stsp upa.repo = repo;
9158 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9159 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9160 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9161 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9162 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9163 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9164 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9165 ad493afc 2019-08-03 stsp if (err)
9166 ad493afc 2019-08-03 stsp goto done;
9167 ad493afc 2019-08-03 stsp }
9168 ad493afc 2019-08-03 stsp
9169 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9170 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9171 ad493afc 2019-08-03 stsp err = sync_err;
9172 ad493afc 2019-08-03 stsp done:
9173 ad493afc 2019-08-03 stsp free(fileindex_path);
9174 ad493afc 2019-08-03 stsp if (fileindex)
9175 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9176 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9177 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9178 ad493afc 2019-08-03 stsp err = unlockerr;
9179 ad493afc 2019-08-03 stsp return err;
9180 ad493afc 2019-08-03 stsp }
9181 b2118c49 2020-07-28 stsp
9182 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9183 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9184 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9185 b2118c49 2020-07-28 stsp void *info_arg;
9186 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9187 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9188 b2118c49 2020-07-28 stsp void *cancel_arg;
9189 b2118c49 2020-07-28 stsp };
9190 b2118c49 2020-07-28 stsp
9191 b2118c49 2020-07-28 stsp static const struct got_error *
9192 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9193 b2118c49 2020-07-28 stsp {
9194 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9195 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9196 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9197 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9198 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9199 b2118c49 2020-07-28 stsp int stage;
9200 b2118c49 2020-07-28 stsp
9201 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9202 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9203 b2118c49 2020-07-28 stsp
9204 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9205 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9206 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9207 b2118c49 2020-07-28 stsp break;
9208 b2118c49 2020-07-28 stsp }
9209 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9210 b2118c49 2020-07-28 stsp return NULL;
9211 b2118c49 2020-07-28 stsp
9212 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9213 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9214 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9215 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9216 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9217 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9218 43010591 2023-02-17 thomas &staged_blob_id, ie);
9219 b2118c49 2020-07-28 stsp }
9220 b2118c49 2020-07-28 stsp
9221 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9222 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9223 b2118c49 2020-07-28 stsp
9224 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9225 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9226 b2118c49 2020-07-28 stsp }
9227 b2118c49 2020-07-28 stsp
9228 b2118c49 2020-07-28 stsp const struct got_error *
9229 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9230 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9231 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9232 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9233 b2118c49 2020-07-28 stsp
9234 b2118c49 2020-07-28 stsp {
9235 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9236 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9237 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9238 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9239 b2118c49 2020-07-28 stsp
9240 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9241 b2118c49 2020-07-28 stsp if (err)
9242 b2118c49 2020-07-28 stsp return err;
9243 b2118c49 2020-07-28 stsp
9244 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9245 b2118c49 2020-07-28 stsp if (err)
9246 b2118c49 2020-07-28 stsp goto done;
9247 b2118c49 2020-07-28 stsp
9248 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9249 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9250 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9251 b2118c49 2020-07-28 stsp arg.paths = paths;
9252 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9253 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9254 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9255 b2118c49 2020-07-28 stsp &arg);
9256 b2118c49 2020-07-28 stsp done:
9257 b2118c49 2020-07-28 stsp free(fileindex_path);
9258 b2118c49 2020-07-28 stsp if (fileindex)
9259 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9260 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9261 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9262 b2118c49 2020-07-28 stsp err = unlockerr;
9263 b2118c49 2020-07-28 stsp return err;
9264 b2118c49 2020-07-28 stsp }
9265 814624e7 2022-03-22 thomas
9266 814624e7 2022-03-22 thomas static const struct got_error *
9267 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9268 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9269 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9270 814624e7 2022-03-22 thomas {
9271 814624e7 2022-03-22 thomas const struct got_error *err;
9272 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9273 814624e7 2022-03-22 thomas struct stat sb;
9274 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9275 814624e7 2022-03-22 thomas
9276 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9277 814624e7 2022-03-22 thomas if (err)
9278 814624e7 2022-03-22 thomas return err;
9279 814624e7 2022-03-22 thomas
9280 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9281 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9282 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9283 814624e7 2022-03-22 thomas
9284 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9285 814624e7 2022-03-22 thomas if (ie) {
9286 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9287 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9288 12de5570 2022-05-01 thomas repo);
9289 814624e7 2022-03-22 thomas if (err)
9290 814624e7 2022-03-22 thomas goto done;
9291 814624e7 2022-03-22 thomas } else {
9292 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9293 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9294 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9295 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9296 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9297 814624e7 2022-03-22 thomas ondisk_path);
9298 814624e7 2022-03-22 thomas goto done;
9299 814624e7 2022-03-22 thomas }
9300 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9301 814624e7 2022-03-22 thomas }
9302 814624e7 2022-03-22 thomas }
9303 814624e7 2022-03-22 thomas
9304 814624e7 2022-03-22 thomas done:
9305 814624e7 2022-03-22 thomas free(ondisk_path);
9306 814624e7 2022-03-22 thomas return err;
9307 814624e7 2022-03-22 thomas }
9308 814624e7 2022-03-22 thomas
9309 814624e7 2022-03-22 thomas static const struct got_error *
9310 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9311 814624e7 2022-03-22 thomas unsigned char staged_status)
9312 814624e7 2022-03-22 thomas {
9313 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9314 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9315 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9316 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9317 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9318 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9319 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9320 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9321 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9322 814624e7 2022-03-22 thomas return NULL;
9323 814624e7 2022-03-22 thomas }
9324 814624e7 2022-03-22 thomas
9325 814624e7 2022-03-22 thomas static const struct got_error *
9326 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9327 814624e7 2022-03-22 thomas {
9328 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9329 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9330 814624e7 2022-03-22 thomas return NULL;
9331 814624e7 2022-03-22 thomas }
9332 814624e7 2022-03-22 thomas
9333 814624e7 2022-03-22 thomas static const struct got_error *
9334 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9335 814624e7 2022-03-22 thomas unsigned char staged_status)
9336 814624e7 2022-03-22 thomas {
9337 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9338 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9339 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9340 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9341 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9342 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9343 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9344 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9345 814624e7 2022-03-22 thomas return NULL;
9346 814624e7 2022-03-22 thomas }
9347 814624e7 2022-03-22 thomas
9348 814624e7 2022-03-22 thomas const struct got_error *
9349 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9350 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9351 814624e7 2022-03-22 thomas {
9352 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9353 814624e7 2022-03-22 thomas }
9354 814624e7 2022-03-22 thomas
9355 814624e7 2022-03-22 thomas const struct got_error *
9356 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9357 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9358 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9359 814624e7 2022-03-22 thomas {
9360 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9361 814624e7 2022-03-22 thomas int file_renamed = 0;
9362 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9363 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9364 814624e7 2022-03-22 thomas
9365 814624e7 2022-03-22 thomas *oldpath = NULL;
9366 814624e7 2022-03-22 thomas *newpath = NULL;
9367 814624e7 2022-03-22 thomas
9368 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9369 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9370 814624e7 2022-03-22 thomas if (err)
9371 814624e7 2022-03-22 thomas goto done;
9372 814624e7 2022-03-22 thomas
9373 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9374 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9375 814624e7 2022-03-22 thomas if (err)
9376 814624e7 2022-03-22 thomas goto done;
9377 814624e7 2022-03-22 thomas
9378 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9379 814624e7 2022-03-22 thomas file_renamed = 1;
9380 814624e7 2022-03-22 thomas
9381 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9382 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9383 814624e7 2022-03-22 thomas else if (file_renamed) {
9384 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9385 814624e7 2022-03-22 thomas if (err == NULL)
9386 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9387 814624e7 2022-03-22 thomas } else if (old == NULL)
9388 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9389 814624e7 2022-03-22 thomas else
9390 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9391 814624e7 2022-03-22 thomas
9392 814624e7 2022-03-22 thomas done:
9393 814624e7 2022-03-22 thomas if (err) {
9394 814624e7 2022-03-22 thomas free(*oldpath);
9395 814624e7 2022-03-22 thomas *oldpath = NULL;
9396 814624e7 2022-03-22 thomas free(*newpath);
9397 814624e7 2022-03-22 thomas *newpath = NULL;
9398 814624e7 2022-03-22 thomas }
9399 814624e7 2022-03-22 thomas return err;
9400 814624e7 2022-03-22 thomas }
9401 814624e7 2022-03-22 thomas
9402 814624e7 2022-03-22 thomas const struct got_error *
9403 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9404 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9405 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9406 814624e7 2022-03-22 thomas {
9407 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9408 5e22b737 2022-05-31 thomas
9409 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9410 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9411 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9412 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9413 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9414 5e22b737 2022-05-31 thomas saa.repo = repo;
9415 5e22b737 2022-05-31 thomas
9416 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9417 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9418 814624e7 2022-03-22 thomas }
9419 5e22b737 2022-05-31 thomas
9420 5e22b737 2022-05-31 thomas const struct got_error *
9421 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9422 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9423 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9424 5e22b737 2022-05-31 thomas {
9425 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9426 5e22b737 2022-05-31 thomas
9427 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9428 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9429 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9430 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9431 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9432 5e22b737 2022-05-31 thomas sda.repo = repo;
9433 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9434 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9435 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9436 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9437 5e22b737 2022-05-31 thomas
9438 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9439 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9440 5e22b737 2022-05-31 thomas }
9441 5e22b737 2022-05-31 thomas
9442 5e22b737 2022-05-31 thomas const struct got_error *
9443 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9444 36832a8e 2022-05-31 thomas const char *fileindex_path)
9445 5e22b737 2022-05-31 thomas {
9446 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9447 5e22b737 2022-05-31 thomas
9448 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9449 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9450 5e22b737 2022-05-31 thomas
9451 5e22b737 2022-05-31 thomas return err;
9452 5e22b737 2022-05-31 thomas }