Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 86c3caaf 2018-03-09 stsp
20 d1f6d47b 2019-02-04 stsp #include <dirent.h>
21 12ce7a6c 2019-08-12 stsp #include <limits.h>
22 f5c49f82 2019-01-06 stsp #include <stddef.h>
23 86c3caaf 2018-03-09 stsp #include <string.h>
24 86c3caaf 2018-03-09 stsp #include <stdio.h>
25 86c3caaf 2018-03-09 stsp #include <stdlib.h>
26 303e14b5 2019-09-23 stsp #include <time.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <zlib.h>
31 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
32 512f0d0e 2019-01-02 stsp #include <libgen.h>
33 dd038bc6 2021-09-21 thomas.ad
34 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
35 86c3caaf 2018-03-09 stsp
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
57 9d31a1d8 2018-03-11 stsp
58 9d31a1d8 2018-03-11 stsp #ifndef MIN
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 9d31a1d8 2018-03-11 stsp #endif
61 f69721c3 2019-10-21 stsp
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 86c3caaf 2018-03-09 stsp
65 99724ed4 2018-03-10 stsp static const struct got_error *
66 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
67 99724ed4 2018-03-10 stsp {
68 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
69 99724ed4 2018-03-10 stsp char *path;
70 99724ed4 2018-03-10 stsp
71 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
73 99724ed4 2018-03-10 stsp
74 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
75 99724ed4 2018-03-10 stsp free(path);
76 507dc3bb 2018-12-29 stsp return err;
77 507dc3bb 2018-12-29 stsp }
78 507dc3bb 2018-12-29 stsp
79 507dc3bb 2018-12-29 stsp static const struct got_error *
80 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
81 507dc3bb 2018-12-29 stsp {
82 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
83 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
84 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
85 507dc3bb 2018-12-29 stsp char *path = NULL;
86 507dc3bb 2018-12-29 stsp
87 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
89 507dc3bb 2018-12-29 stsp path = NULL;
90 507dc3bb 2018-12-29 stsp goto done;
91 507dc3bb 2018-12-29 stsp }
92 507dc3bb 2018-12-29 stsp
93 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
94 507dc3bb 2018-12-29 stsp if (err)
95 507dc3bb 2018-12-29 stsp goto done;
96 507dc3bb 2018-12-29 stsp
97 507dc3bb 2018-12-29 stsp if (content) {
98 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
99 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
100 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
101 507dc3bb 2018-12-29 stsp goto done;
102 507dc3bb 2018-12-29 stsp }
103 507dc3bb 2018-12-29 stsp }
104 507dc3bb 2018-12-29 stsp
105 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
106 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
107 2a57020b 2019-02-20 stsp unlink(tmppath);
108 507dc3bb 2018-12-29 stsp goto done;
109 507dc3bb 2018-12-29 stsp }
110 507dc3bb 2018-12-29 stsp
111 507dc3bb 2018-12-29 stsp done:
112 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
113 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
114 230a42bd 2019-05-11 jcs free(tmppath);
115 09fe317a 2018-03-11 stsp return err;
116 09fe317a 2018-03-11 stsp }
117 09fe317a 2018-03-11 stsp
118 024e9686 2019-05-14 stsp static const struct got_error *
119 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
120 024e9686 2019-05-14 stsp {
121 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
122 024e9686 2019-05-14 stsp char *refstr = NULL;
123 024e9686 2019-05-14 stsp
124 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
125 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
126 024e9686 2019-05-14 stsp if (refstr == NULL)
127 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
128 024e9686 2019-05-14 stsp } else {
129 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
130 024e9686 2019-05-14 stsp if (refstr == NULL)
131 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
132 024e9686 2019-05-14 stsp }
133 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
134 024e9686 2019-05-14 stsp free(refstr);
135 024e9686 2019-05-14 stsp return err;
136 024e9686 2019-05-14 stsp }
137 024e9686 2019-05-14 stsp
138 86c3caaf 2018-03-09 stsp const struct got_error *
139 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
140 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
141 86c3caaf 2018-03-09 stsp {
142 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
143 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
144 ec22038e 2019-03-10 stsp uuid_t uuid;
145 ec22038e 2019-03-10 stsp uint32_t uuid_status;
146 65596e15 2018-12-24 stsp int obj_type;
147 7ac97322 2018-03-11 stsp char *path_got = NULL;
148 1451e70d 2018-03-10 stsp char *formatstr = NULL;
149 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
150 65596e15 2018-12-24 stsp char *basestr = NULL;
151 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
152 65596e15 2018-12-24 stsp
153 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
154 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
155 0c48fee2 2019-03-11 stsp goto done;
156 0c48fee2 2019-03-11 stsp }
157 0c48fee2 2019-03-11 stsp
158 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
159 65596e15 2018-12-24 stsp if (err)
160 65596e15 2018-12-24 stsp return err;
161 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
162 65596e15 2018-12-24 stsp if (err)
163 65596e15 2018-12-24 stsp return err;
164 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
165 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
166 86c3caaf 2018-03-09 stsp
167 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
168 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
169 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
170 0bb8a95e 2018-03-12 stsp }
171 577ec78f 2018-03-11 stsp
172 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
173 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
174 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
175 86c3caaf 2018-03-09 stsp goto done;
176 86c3caaf 2018-03-09 stsp }
177 86c3caaf 2018-03-09 stsp
178 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
179 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
180 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
181 86c3caaf 2018-03-09 stsp goto done;
182 86c3caaf 2018-03-09 stsp }
183 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
185 86c3caaf 2018-03-09 stsp goto done;
186 86c3caaf 2018-03-09 stsp }
187 86c3caaf 2018-03-09 stsp
188 056e7441 2018-03-11 stsp /* Create an empty lock file. */
189 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
190 056e7441 2018-03-11 stsp if (err)
191 056e7441 2018-03-11 stsp goto done;
192 056e7441 2018-03-11 stsp
193 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
194 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
195 99724ed4 2018-03-10 stsp if (err)
196 86c3caaf 2018-03-09 stsp goto done;
197 86c3caaf 2018-03-09 stsp
198 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
199 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
200 65596e15 2018-12-24 stsp if (err)
201 65596e15 2018-12-24 stsp goto done;
202 65596e15 2018-12-24 stsp
203 65596e15 2018-12-24 stsp /* Record our base commit. */
204 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
205 65596e15 2018-12-24 stsp if (err)
206 65596e15 2018-12-24 stsp goto done;
207 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
208 99724ed4 2018-03-10 stsp if (err)
209 86c3caaf 2018-03-09 stsp goto done;
210 86c3caaf 2018-03-09 stsp
211 1451e70d 2018-03-10 stsp /* Store path to repository. */
212 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
213 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
214 99724ed4 2018-03-10 stsp if (err)
215 86c3caaf 2018-03-09 stsp goto done;
216 86c3caaf 2018-03-09 stsp
217 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
218 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
219 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
220 ec22038e 2019-03-10 stsp if (err)
221 ec22038e 2019-03-10 stsp goto done;
222 ec22038e 2019-03-10 stsp
223 ec22038e 2019-03-10 stsp /* Generate UUID. */
224 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
225 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
226 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
227 ec22038e 2019-03-10 stsp goto done;
228 ec22038e 2019-03-10 stsp }
229 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
230 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
231 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
232 ec22038e 2019-03-10 stsp goto done;
233 ec22038e 2019-03-10 stsp }
234 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
235 577ec78f 2018-03-11 stsp if (err)
236 577ec78f 2018-03-11 stsp goto done;
237 577ec78f 2018-03-11 stsp
238 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
239 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
241 1451e70d 2018-03-10 stsp goto done;
242 1451e70d 2018-03-10 stsp }
243 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
244 99724ed4 2018-03-10 stsp if (err)
245 1451e70d 2018-03-10 stsp goto done;
246 1451e70d 2018-03-10 stsp
247 86c3caaf 2018-03-09 stsp done:
248 65596e15 2018-12-24 stsp free(commit_id);
249 7ac97322 2018-03-11 stsp free(path_got);
250 1451e70d 2018-03-10 stsp free(formatstr);
251 0bb8a95e 2018-03-12 stsp free(absprefix);
252 65596e15 2018-12-24 stsp free(basestr);
253 ec22038e 2019-03-10 stsp free(uuidstr);
254 86c3caaf 2018-03-09 stsp return err;
255 86c3caaf 2018-03-09 stsp }
256 86c3caaf 2018-03-09 stsp
257 247140b2 2019-02-05 stsp const struct got_error *
258 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
259 e5dc7198 2018-12-29 stsp const char *path_prefix)
260 e5dc7198 2018-12-29 stsp {
261 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
262 e5dc7198 2018-12-29 stsp
263 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
264 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
265 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
266 e5dc7198 2018-12-29 stsp }
267 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
268 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
269 e5dc7198 2018-12-29 stsp free(absprefix);
270 e5dc7198 2018-12-29 stsp return NULL;
271 86c3caaf 2018-03-09 stsp }
272 86c3caaf 2018-03-09 stsp
273 bc70eb79 2019-05-09 stsp const char *
274 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
275 86c3caaf 2018-03-09 stsp {
276 36a38700 2019-05-10 stsp return worktree->head_ref_name;
277 024e9686 2019-05-14 stsp }
278 024e9686 2019-05-14 stsp
279 024e9686 2019-05-14 stsp const struct got_error *
280 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
281 024e9686 2019-05-14 stsp struct got_reference *head_ref)
282 024e9686 2019-05-14 stsp {
283 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
284 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
285 024e9686 2019-05-14 stsp
286 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
287 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
288 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
289 024e9686 2019-05-14 stsp path_got = NULL;
290 024e9686 2019-05-14 stsp goto done;
291 024e9686 2019-05-14 stsp }
292 024e9686 2019-05-14 stsp
293 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
294 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
295 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
296 024e9686 2019-05-14 stsp goto done;
297 024e9686 2019-05-14 stsp }
298 024e9686 2019-05-14 stsp
299 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
300 024e9686 2019-05-14 stsp if (err)
301 024e9686 2019-05-14 stsp goto done;
302 024e9686 2019-05-14 stsp
303 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
304 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
305 024e9686 2019-05-14 stsp done:
306 024e9686 2019-05-14 stsp free(path_got);
307 024e9686 2019-05-14 stsp if (err)
308 024e9686 2019-05-14 stsp free(head_ref_name);
309 024e9686 2019-05-14 stsp return err;
310 507dc3bb 2018-12-29 stsp }
311 507dc3bb 2018-12-29 stsp
312 b72f483a 2019-02-05 stsp struct got_object_id *
313 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
314 507dc3bb 2018-12-29 stsp {
315 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
316 86c3caaf 2018-03-09 stsp }
317 86c3caaf 2018-03-09 stsp
318 507dc3bb 2018-12-29 stsp const struct got_error *
319 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
320 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
321 507dc3bb 2018-12-29 stsp {
322 507dc3bb 2018-12-29 stsp const struct got_error *err;
323 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
324 507dc3bb 2018-12-29 stsp char *id_str = NULL;
325 507dc3bb 2018-12-29 stsp char *path_got = NULL;
326 507dc3bb 2018-12-29 stsp
327 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
328 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
329 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
330 507dc3bb 2018-12-29 stsp path_got = NULL;
331 507dc3bb 2018-12-29 stsp goto done;
332 507dc3bb 2018-12-29 stsp }
333 507dc3bb 2018-12-29 stsp
334 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
335 507dc3bb 2018-12-29 stsp if (err)
336 507dc3bb 2018-12-29 stsp return err;
337 507dc3bb 2018-12-29 stsp
338 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
339 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
340 507dc3bb 2018-12-29 stsp goto done;
341 507dc3bb 2018-12-29 stsp }
342 507dc3bb 2018-12-29 stsp
343 507dc3bb 2018-12-29 stsp /* Record our base commit. */
344 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
345 507dc3bb 2018-12-29 stsp if (err)
346 507dc3bb 2018-12-29 stsp goto done;
347 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
348 507dc3bb 2018-12-29 stsp if (err)
349 507dc3bb 2018-12-29 stsp goto done;
350 507dc3bb 2018-12-29 stsp
351 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
352 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
353 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
354 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
355 507dc3bb 2018-12-29 stsp goto done;
356 507dc3bb 2018-12-29 stsp }
357 507dc3bb 2018-12-29 stsp done:
358 507dc3bb 2018-12-29 stsp if (obj)
359 507dc3bb 2018-12-29 stsp got_object_close(obj);
360 507dc3bb 2018-12-29 stsp free(id_str);
361 507dc3bb 2018-12-29 stsp free(path_got);
362 507dc3bb 2018-12-29 stsp return err;
363 50b0790e 2020-09-11 stsp }
364 50b0790e 2020-09-11 stsp
365 50b0790e 2020-09-11 stsp const struct got_gotconfig *
366 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
367 50b0790e 2020-09-11 stsp {
368 50b0790e 2020-09-11 stsp return worktree->gotconfig;
369 507dc3bb 2018-12-29 stsp }
370 507dc3bb 2018-12-29 stsp
371 9d31a1d8 2018-03-11 stsp static const struct got_error *
372 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
373 86c3caaf 2018-03-09 stsp {
374 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
375 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
376 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
377 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
378 86c3caaf 2018-03-09 stsp return NULL;
379 21908da4 2019-01-13 stsp }
380 21908da4 2019-01-13 stsp
381 21908da4 2019-01-13 stsp static const struct got_error *
382 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
383 4a1ddfc2 2019-01-12 stsp {
384 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
385 4a1ddfc2 2019-01-12 stsp char *abspath;
386 4a1ddfc2 2019-01-12 stsp
387 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
388 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
389 4a1ddfc2 2019-01-12 stsp
390 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
391 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
392 ddcd8544 2019-03-11 stsp struct stat sb;
393 ddcd8544 2019-03-11 stsp err = NULL;
394 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
395 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
396 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
397 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
398 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
399 ddcd8544 2019-03-11 stsp }
400 ddcd8544 2019-03-11 stsp }
401 4a1ddfc2 2019-01-12 stsp free(abspath);
402 68c76935 2019-02-19 stsp return err;
403 68c76935 2019-02-19 stsp }
404 68c76935 2019-02-19 stsp
405 68c76935 2019-02-19 stsp static const struct got_error *
406 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
407 68c76935 2019-02-19 stsp {
408 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
409 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
410 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
411 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
412 68c76935 2019-02-19 stsp
413 68c76935 2019-02-19 stsp *same = 1;
414 68c76935 2019-02-19 stsp
415 230a42bd 2019-05-11 jcs for (;;) {
416 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
417 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
418 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
419 68c76935 2019-02-19 stsp break;
420 68c76935 2019-02-19 stsp }
421 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
422 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
423 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
424 68c76935 2019-02-19 stsp break;
425 68c76935 2019-02-19 stsp }
426 68c76935 2019-02-19 stsp if (flen1 == 0) {
427 68c76935 2019-02-19 stsp if (flen2 != 0)
428 68c76935 2019-02-19 stsp *same = 0;
429 68c76935 2019-02-19 stsp break;
430 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
431 68c76935 2019-02-19 stsp if (flen1 != 0)
432 68c76935 2019-02-19 stsp *same = 0;
433 68c76935 2019-02-19 stsp break;
434 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
435 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
436 68c76935 2019-02-19 stsp *same = 0;
437 68c76935 2019-02-19 stsp break;
438 68c76935 2019-02-19 stsp }
439 68c76935 2019-02-19 stsp } else {
440 68c76935 2019-02-19 stsp *same = 0;
441 68c76935 2019-02-19 stsp break;
442 68c76935 2019-02-19 stsp }
443 68c76935 2019-02-19 stsp }
444 68c76935 2019-02-19 stsp
445 68c76935 2019-02-19 stsp return err;
446 68c76935 2019-02-19 stsp }
447 68c76935 2019-02-19 stsp
448 68c76935 2019-02-19 stsp static const struct got_error *
449 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
450 68c76935 2019-02-19 stsp {
451 68c76935 2019-02-19 stsp struct stat sb;
452 68c76935 2019-02-19 stsp size_t size1, size2;
453 68c76935 2019-02-19 stsp
454 68c76935 2019-02-19 stsp *same = 1;
455 68c76935 2019-02-19 stsp
456 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
457 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
458 68c76935 2019-02-19 stsp size1 = sb.st_size;
459 68c76935 2019-02-19 stsp
460 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
461 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
462 68c76935 2019-02-19 stsp size2 = sb.st_size;
463 68c76935 2019-02-19 stsp
464 68c76935 2019-02-19 stsp if (size1 != size2) {
465 68c76935 2019-02-19 stsp *same = 0;
466 68c76935 2019-02-19 stsp return NULL;
467 68c76935 2019-02-19 stsp }
468 68c76935 2019-02-19 stsp
469 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
470 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
471 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
473 68c76935 2019-02-19 stsp
474 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
475 24f136e0 2021-11-20 thomas }
476 24f136e0 2021-11-20 thomas
477 24f136e0 2021-11-20 thomas static const struct got_error *
478 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
479 24f136e0 2021-11-20 thomas {
480 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
481 24f136e0 2021-11-20 thomas size_t flen;
482 24f136e0 2021-11-20 thomas ssize_t outlen;
483 24f136e0 2021-11-20 thomas
484 24f136e0 2021-11-20 thomas *outsize = 0;
485 24f136e0 2021-11-20 thomas
486 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
487 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
488 24f136e0 2021-11-20 thomas
489 24f136e0 2021-11-20 thomas for (;;) {
490 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
491 24f136e0 2021-11-20 thomas if (flen == 0) {
492 24f136e0 2021-11-20 thomas if (ferror(f))
493 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
494 24f136e0 2021-11-20 thomas if (feof(f))
495 24f136e0 2021-11-20 thomas break;
496 24f136e0 2021-11-20 thomas }
497 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
498 24f136e0 2021-11-20 thomas if (outlen == -1)
499 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
500 24f136e0 2021-11-20 thomas if (outlen != flen)
501 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
502 24f136e0 2021-11-20 thomas *outsize += outlen;
503 24f136e0 2021-11-20 thomas }
504 24f136e0 2021-11-20 thomas
505 24f136e0 2021-11-20 thomas return NULL;
506 24f136e0 2021-11-20 thomas }
507 24f136e0 2021-11-20 thomas
508 24f136e0 2021-11-20 thomas static const struct got_error *
509 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
510 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
511 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
512 24f136e0 2021-11-20 thomas const char *ondisk_path)
513 24f136e0 2021-11-20 thomas {
514 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
515 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
516 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
517 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
518 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
519 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
520 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
521 24f136e0 2021-11-20 thomas
522 24f136e0 2021-11-20 thomas *overlapcnt = 0;
523 24f136e0 2021-11-20 thomas
524 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
525 24f136e0 2021-11-20 thomas if (err)
526 24f136e0 2021-11-20 thomas return err;
527 24f136e0 2021-11-20 thomas
528 24f136e0 2021-11-20 thomas if (same_content)
529 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
530 24f136e0 2021-11-20 thomas
531 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
532 24f136e0 2021-11-20 thomas if (err)
533 24f136e0 2021-11-20 thomas return err;
534 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
535 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
536 24f136e0 2021-11-20 thomas if (err)
537 24f136e0 2021-11-20 thomas return err;
538 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
539 24f136e0 2021-11-20 thomas
540 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
541 24f136e0 2021-11-20 thomas *overlapcnt = 1;
542 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
543 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
544 24f136e0 2021-11-20 thomas goto done;
545 24f136e0 2021-11-20 thomas }
546 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
547 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
548 24f136e0 2021-11-20 thomas goto done;
549 24f136e0 2021-11-20 thomas }
550 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
551 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
552 24f136e0 2021-11-20 thomas goto done;
553 24f136e0 2021-11-20 thomas }
554 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
555 24f136e0 2021-11-20 thomas base_path_orig);
556 24f136e0 2021-11-20 thomas if (err)
557 24f136e0 2021-11-20 thomas goto done;
558 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
559 24f136e0 2021-11-20 thomas base_path_deriv);
560 24f136e0 2021-11-20 thomas if (err)
561 24f136e0 2021-11-20 thomas goto done;
562 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
563 24f136e0 2021-11-20 thomas base_path_deriv2);
564 24f136e0 2021-11-20 thomas if (err)
565 24f136e0 2021-11-20 thomas goto done;
566 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
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_deriv, f_deriv, fd_deriv);
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_deriv2, f_deriv2, fd_deriv2);
573 24f136e0 2021-11-20 thomas if (err)
574 24f136e0 2021-11-20 thomas goto done;
575 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
576 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
577 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
578 24f136e0 2021-11-20 thomas goto done;
579 24f136e0 2021-11-20 thomas }
580 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
581 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
582 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
583 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
584 24f136e0 2021-11-20 thomas path_deriv) < 0) {
585 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
586 24f136e0 2021-11-20 thomas goto done;
587 24f136e0 2021-11-20 thomas }
588 24f136e0 2021-11-20 thomas if (size_orig > 0) {
589 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
590 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
591 24f136e0 2021-11-20 thomas label_orig ? " " : "",
592 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
593 24f136e0 2021-11-20 thomas path_orig) < 0) {
594 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
595 24f136e0 2021-11-20 thomas goto done;
596 24f136e0 2021-11-20 thomas }
597 24f136e0 2021-11-20 thomas }
598 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
599 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
600 24f136e0 2021-11-20 thomas path_deriv2,
601 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
602 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
603 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
604 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
605 24f136e0 2021-11-20 thomas goto done;
606 24f136e0 2021-11-20 thomas }
607 24f136e0 2021-11-20 thomas } else if (changed_deriv)
608 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
609 24f136e0 2021-11-20 thomas else if (changed_deriv2)
610 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
611 24f136e0 2021-11-20 thomas done:
612 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
613 24f136e0 2021-11-20 thomas err == NULL)
614 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
615 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
616 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
617 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
618 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
619 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
620 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
621 24f136e0 2021-11-20 thomas free(path_orig);
622 24f136e0 2021-11-20 thomas free(path_deriv);
623 24f136e0 2021-11-20 thomas free(path_deriv2);
624 24f136e0 2021-11-20 thomas free(base_path_orig);
625 24f136e0 2021-11-20 thomas free(base_path_deriv);
626 24f136e0 2021-11-20 thomas free(base_path_deriv2);
627 24f136e0 2021-11-20 thomas return err;
628 6353ad76 2019-02-08 stsp }
629 6353ad76 2019-02-08 stsp
630 6353ad76 2019-02-08 stsp /*
631 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
632 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
633 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
634 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
635 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
636 6353ad76 2019-02-08 stsp */
637 6353ad76 2019-02-08 stsp static const struct got_error *
638 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
639 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
640 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
641 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
642 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
643 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
644 6353ad76 2019-02-08 stsp {
645 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
646 6353ad76 2019-02-08 stsp int merged_fd = -1;
647 db590691 2021-06-02 stsp FILE *f_merged = NULL;
648 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
649 234035bc 2019-06-01 stsp int overlapcnt = 0;
650 3524bbf9 2020-10-20 stsp char *parent = NULL;
651 6353ad76 2019-02-08 stsp
652 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
653 234035bc 2019-06-01 stsp
654 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
655 3524bbf9 2020-10-20 stsp if (err)
656 3524bbf9 2020-10-20 stsp return err;
657 af54ae4a 2019-02-19 stsp
658 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
659 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
660 3524bbf9 2020-10-20 stsp goto done;
661 3524bbf9 2020-10-20 stsp }
662 af54ae4a 2019-02-19 stsp
663 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
664 6353ad76 2019-02-08 stsp if (err)
665 6353ad76 2019-02-08 stsp goto done;
666 3b9f0f87 2020-07-23 stsp
667 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
668 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
669 24f136e0 2021-11-20 thomas if (err) {
670 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
671 24f136e0 2021-11-20 thomas goto done;
672 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
673 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
674 24f136e0 2021-11-20 thomas ondisk_path);
675 24f136e0 2021-11-20 thomas if (err)
676 24f136e0 2021-11-20 thomas goto done;
677 24f136e0 2021-11-20 thomas }
678 6353ad76 2019-02-08 stsp
679 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
680 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
681 1ee397ad 2019-07-12 stsp if (err)
682 1ee397ad 2019-07-12 stsp goto done;
683 6353ad76 2019-02-08 stsp
684 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
685 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
686 816dc654 2019-02-16 stsp goto done;
687 68c76935 2019-02-19 stsp }
688 68c76935 2019-02-19 stsp
689 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
690 db590691 2021-06-02 stsp if (f_merged == NULL) {
691 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
692 db590691 2021-06-02 stsp goto done;
693 db590691 2021-06-02 stsp }
694 db590691 2021-06-02 stsp merged_fd = -1;
695 db590691 2021-06-02 stsp
696 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
697 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
698 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
699 db590691 2021-06-02 stsp f_merged);
700 68c76935 2019-02-19 stsp if (err)
701 68c76935 2019-02-19 stsp goto done;
702 816dc654 2019-02-16 stsp }
703 6353ad76 2019-02-08 stsp
704 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
705 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
706 70a0c8ec 2019-02-20 stsp goto done;
707 70a0c8ec 2019-02-20 stsp }
708 70a0c8ec 2019-02-20 stsp
709 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
710 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
711 230a42bd 2019-05-11 jcs ondisk_path);
712 6353ad76 2019-02-08 stsp goto done;
713 6353ad76 2019-02-08 stsp }
714 6353ad76 2019-02-08 stsp done:
715 fdcb7daf 2019-12-15 stsp if (err) {
716 fdcb7daf 2019-12-15 stsp if (merged_path)
717 fdcb7daf 2019-12-15 stsp unlink(merged_path);
718 3b9f0f87 2020-07-23 stsp }
719 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
720 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
721 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
723 6353ad76 2019-02-08 stsp free(merged_path);
724 af54ae4a 2019-02-19 stsp free(base_path);
725 3524bbf9 2020-10-20 stsp free(parent);
726 af57b12a 2020-07-23 stsp return err;
727 af57b12a 2020-07-23 stsp }
728 af57b12a 2020-07-23 stsp
729 af57b12a 2020-07-23 stsp static const struct got_error *
730 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
731 af57b12a 2020-07-23 stsp size_t target_len)
732 af57b12a 2020-07-23 stsp {
733 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
734 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
735 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
736 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
738 af57b12a 2020-07-23 stsp ondisk_path);
739 af57b12a 2020-07-23 stsp return NULL;
740 af57b12a 2020-07-23 stsp }
741 af57b12a 2020-07-23 stsp
742 af57b12a 2020-07-23 stsp /*
743 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
744 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
745 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
746 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
747 993e2a1b 2020-07-23 stsp *
748 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
749 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
750 993e2a1b 2020-07-23 stsp *
751 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
752 993e2a1b 2020-07-23 stsp * has deleted this symlink.
753 11cc08c1 2020-07-23 stsp */
754 11cc08c1 2020-07-23 stsp static const struct got_error *
755 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
756 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
757 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
758 11cc08c1 2020-07-23 stsp {
759 11cc08c1 2020-07-23 stsp const struct got_error *err;
760 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
761 11cc08c1 2020-07-23 stsp FILE *f = NULL;
762 11cc08c1 2020-07-23 stsp
763 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
764 11cc08c1 2020-07-23 stsp if (err)
765 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
766 11cc08c1 2020-07-23 stsp
767 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
768 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
769 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
770 11cc08c1 2020-07-23 stsp goto done;
771 11cc08c1 2020-07-23 stsp }
772 11cc08c1 2020-07-23 stsp
773 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
774 11cc08c1 2020-07-23 stsp if (err)
775 3818e3c4 2020-11-01 naddy goto done;
776 3818e3c4 2020-11-01 naddy
777 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
778 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
779 11cc08c1 2020-07-23 stsp goto done;
780 3818e3c4 2020-11-01 naddy }
781 11cc08c1 2020-07-23 stsp
782 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
783 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
784 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
785 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
786 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
787 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
790 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
791 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
792 11cc08c1 2020-07-23 stsp goto done;
793 11cc08c1 2020-07-23 stsp }
794 11cc08c1 2020-07-23 stsp
795 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
796 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
797 11cc08c1 2020-07-23 stsp goto done;
798 11cc08c1 2020-07-23 stsp }
799 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
800 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
801 11cc08c1 2020-07-23 stsp goto done;
802 11cc08c1 2020-07-23 stsp }
803 11cc08c1 2020-07-23 stsp done:
804 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
805 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
806 11cc08c1 2020-07-23 stsp free(path);
807 11cc08c1 2020-07-23 stsp free(id_str);
808 11cc08c1 2020-07-23 stsp free(label_deriv);
809 11cc08c1 2020-07-23 stsp return err;
810 11cc08c1 2020-07-23 stsp }
811 d219f183 2020-07-23 stsp
812 d219f183 2020-07-23 stsp /* forward declaration */
813 d219f183 2020-07-23 stsp static const struct got_error *
814 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
815 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
816 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
817 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
818 11cc08c1 2020-07-23 stsp
819 11cc08c1 2020-07-23 stsp /*
820 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
821 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
822 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
823 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
824 af57b12a 2020-07-23 stsp */
825 af57b12a 2020-07-23 stsp static const struct got_error *
826 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
827 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
828 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
829 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
830 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
831 af57b12a 2020-07-23 stsp {
832 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
833 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
834 af57b12a 2020-07-23 stsp struct stat sb;
835 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
836 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
837 11cc08c1 2020-07-23 stsp int have_local_change = 0;
838 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
839 af57b12a 2020-07-23 stsp
840 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
841 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
842 af57b12a 2020-07-23 stsp
843 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
844 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
845 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
846 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
847 af57b12a 2020-07-23 stsp ondisk_path);
848 af57b12a 2020-07-23 stsp goto done;
849 af57b12a 2020-07-23 stsp }
850 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
851 af57b12a 2020-07-23 stsp
852 526a746f 2020-07-23 stsp if (blob_orig) {
853 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
854 526a746f 2020-07-23 stsp if (err)
855 526a746f 2020-07-23 stsp goto done;
856 526a746f 2020-07-23 stsp }
857 af57b12a 2020-07-23 stsp
858 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
859 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
860 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
861 11cc08c1 2020-07-23 stsp have_local_change = 1;
862 11cc08c1 2020-07-23 stsp
863 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
864 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
865 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
866 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
867 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
868 11cc08c1 2020-07-23 stsp
869 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
870 11cc08c1 2020-07-23 stsp if (ancestor_target) {
871 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
872 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
873 11cc08c1 2020-07-23 stsp path);
874 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
875 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
876 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
877 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 11cc08c1 2020-07-23 stsp path);
879 11cc08c1 2020-07-23 stsp } else {
880 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
881 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
882 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
883 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
884 11cc08c1 2020-07-23 stsp if (err)
885 11cc08c1 2020-07-23 stsp goto done;
886 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
887 11cc08c1 2020-07-23 stsp path);
888 11cc08c1 2020-07-23 stsp }
889 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
890 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
891 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
892 af57b12a 2020-07-23 stsp strlen(deriv_target));
893 af57b12a 2020-07-23 stsp if (err)
894 af57b12a 2020-07-23 stsp goto done;
895 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
896 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
897 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
898 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
899 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
900 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
901 ea7786be 2020-07-23 stsp path);
902 ea7786be 2020-07-23 stsp } else {
903 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
904 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
905 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
906 ea7786be 2020-07-23 stsp if (err)
907 ea7786be 2020-07-23 stsp goto done;
908 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
909 ea7786be 2020-07-23 stsp path);
910 ea7786be 2020-07-23 stsp }
911 6353ad76 2019-02-08 stsp }
912 11cc08c1 2020-07-23 stsp
913 af57b12a 2020-07-23 stsp done:
914 af57b12a 2020-07-23 stsp free(ancestor_target);
915 eec2f5a9 2021-06-03 stsp return err;
916 eec2f5a9 2021-06-03 stsp }
917 eec2f5a9 2021-06-03 stsp
918 eec2f5a9 2021-06-03 stsp static const struct got_error *
919 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
920 eec2f5a9 2021-06-03 stsp {
921 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
922 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
923 eec2f5a9 2021-06-03 stsp ssize_t target_len;
924 eec2f5a9 2021-06-03 stsp size_t n;
925 eec2f5a9 2021-06-03 stsp FILE *f;
926 eec2f5a9 2021-06-03 stsp
927 eec2f5a9 2021-06-03 stsp *outfile = NULL;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp f = got_opentemp();
930 eec2f5a9 2021-06-03 stsp if (f == NULL)
931 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
932 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
933 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
934 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
935 eec2f5a9 2021-06-03 stsp goto done;
936 eec2f5a9 2021-06-03 stsp }
937 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
938 eec2f5a9 2021-06-03 stsp if (n != target_len) {
939 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
940 eec2f5a9 2021-06-03 stsp goto done;
941 eec2f5a9 2021-06-03 stsp }
942 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
943 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
944 eec2f5a9 2021-06-03 stsp goto done;
945 eec2f5a9 2021-06-03 stsp }
946 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
947 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
948 eec2f5a9 2021-06-03 stsp goto done;
949 eec2f5a9 2021-06-03 stsp }
950 eec2f5a9 2021-06-03 stsp done:
951 eec2f5a9 2021-06-03 stsp if (err)
952 eec2f5a9 2021-06-03 stsp fclose(f);
953 eec2f5a9 2021-06-03 stsp else
954 eec2f5a9 2021-06-03 stsp *outfile = f;
955 14c901f1 2019-08-08 stsp return err;
956 14c901f1 2019-08-08 stsp }
957 14c901f1 2019-08-08 stsp
958 14c901f1 2019-08-08 stsp /*
959 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
960 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
961 14c901f1 2019-08-08 stsp * acts as the second derived version.
962 14c901f1 2019-08-08 stsp */
963 14c901f1 2019-08-08 stsp static const struct got_error *
964 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
965 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
966 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
967 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
968 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
969 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
970 14c901f1 2019-08-08 stsp {
971 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
972 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
973 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
974 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
975 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
976 14c901f1 2019-08-08 stsp
977 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
978 14c901f1 2019-08-08 stsp
979 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
980 ed6b5030 2020-10-20 stsp if (err)
981 ed6b5030 2020-10-20 stsp return err;
982 14c901f1 2019-08-08 stsp
983 67a66647 2021-05-31 stsp if (blob_orig) {
984 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
985 67a66647 2021-05-31 stsp parent) == -1) {
986 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
987 67a66647 2021-05-31 stsp base_path = NULL;
988 67a66647 2021-05-31 stsp goto done;
989 67a66647 2021-05-31 stsp }
990 67a66647 2021-05-31 stsp
991 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
992 67a66647 2021-05-31 stsp if (err)
993 67a66647 2021-05-31 stsp goto done;
994 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
995 67a66647 2021-05-31 stsp blob_orig);
996 67a66647 2021-05-31 stsp if (err)
997 67a66647 2021-05-31 stsp goto done;
998 67a66647 2021-05-31 stsp free(base_path);
999 db590691 2021-06-02 stsp } else {
1000 db590691 2021-06-02 stsp /*
1001 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1002 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1003 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1004 db590691 2021-06-02 stsp */
1005 db590691 2021-06-02 stsp f_orig = got_opentemp();
1006 db590691 2021-06-02 stsp if (f_orig == NULL) {
1007 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1008 db590691 2021-06-02 stsp goto done;
1009 db590691 2021-06-02 stsp }
1010 67a66647 2021-05-31 stsp }
1011 67a66647 2021-05-31 stsp
1012 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1013 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1014 14c901f1 2019-08-08 stsp base_path = NULL;
1015 14c901f1 2019-08-08 stsp goto done;
1016 14c901f1 2019-08-08 stsp }
1017 14c901f1 2019-08-08 stsp
1018 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1019 14c901f1 2019-08-08 stsp if (err)
1020 14c901f1 2019-08-08 stsp goto done;
1021 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1022 14c901f1 2019-08-08 stsp blob_deriv);
1023 14c901f1 2019-08-08 stsp if (err)
1024 14c901f1 2019-08-08 stsp goto done;
1025 14c901f1 2019-08-08 stsp
1026 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1027 14c901f1 2019-08-08 stsp if (err)
1028 14c901f1 2019-08-08 stsp goto done;
1029 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1030 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1031 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1032 14c901f1 2019-08-08 stsp goto done;
1033 eec2f5a9 2021-06-03 stsp }
1034 eec2f5a9 2021-06-03 stsp
1035 b6b86fd1 2022-08-30 thomas /*
1036 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1037 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1038 eec2f5a9 2021-06-03 stsp */
1039 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1040 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1041 eec2f5a9 2021-06-03 stsp if (err)
1042 eec2f5a9 2021-06-03 stsp goto done;
1043 eec2f5a9 2021-06-03 stsp } else {
1044 eec2f5a9 2021-06-03 stsp int fd;
1045 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1046 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1047 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1048 eec2f5a9 2021-06-03 stsp goto done;
1049 eec2f5a9 2021-06-03 stsp }
1050 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1051 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1052 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1053 eec2f5a9 2021-06-03 stsp close(fd);
1054 eec2f5a9 2021-06-03 stsp goto done;
1055 eec2f5a9 2021-06-03 stsp }
1056 14c901f1 2019-08-08 stsp }
1057 14c901f1 2019-08-08 stsp
1058 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1059 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1060 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1061 14c901f1 2019-08-08 stsp done:
1062 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1063 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1064 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1065 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1066 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1067 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1068 14c901f1 2019-08-08 stsp free(base_path);
1069 67a66647 2021-05-31 stsp if (blob_orig_path) {
1070 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1071 67a66647 2021-05-31 stsp free(blob_orig_path);
1072 67a66647 2021-05-31 stsp }
1073 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1074 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1075 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1076 14c901f1 2019-08-08 stsp }
1077 6353ad76 2019-02-08 stsp free(id_str);
1078 818c7501 2019-07-11 stsp free(label_deriv);
1079 ed6b5030 2020-10-20 stsp free(parent);
1080 4a1ddfc2 2019-01-12 stsp return err;
1081 4a1ddfc2 2019-01-12 stsp }
1082 4a1ddfc2 2019-01-12 stsp
1083 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1084 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1085 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1086 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1087 13d9040b 2019-03-27 stsp {
1088 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1089 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1090 13d9040b 2019-03-27 stsp
1091 65b05cec 2020-07-23 stsp *new_iep = NULL;
1092 65b05cec 2020-07-23 stsp
1093 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1094 054041d0 2020-06-24 stsp if (err)
1095 054041d0 2020-06-24 stsp return err;
1096 054041d0 2020-06-24 stsp
1097 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1098 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1099 054041d0 2020-06-24 stsp if (err)
1100 054041d0 2020-06-24 stsp goto done;
1101 054041d0 2020-06-24 stsp
1102 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1103 054041d0 2020-06-24 stsp done:
1104 054041d0 2020-06-24 stsp if (err)
1105 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1106 65b05cec 2020-07-23 stsp else
1107 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1108 13d9040b 2019-03-27 stsp return err;
1109 1ebedb77 2019-10-19 stsp }
1110 1ebedb77 2019-10-19 stsp
1111 1ebedb77 2019-10-19 stsp static mode_t
1112 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1113 1ebedb77 2019-10-19 stsp {
1114 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1115 1ebedb77 2019-10-19 stsp
1116 1ebedb77 2019-10-19 stsp if (executable) {
1117 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1118 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1119 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1120 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1121 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1122 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1123 1ebedb77 2019-10-19 stsp }
1124 1ebedb77 2019-10-19 stsp
1125 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1126 13d9040b 2019-03-27 stsp }
1127 13d9040b 2019-03-27 stsp
1128 8ba819a3 2020-07-23 stsp /* forward declaration */
1129 13d9040b 2019-03-27 stsp static const struct got_error *
1130 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1131 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1132 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1133 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1134 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1135 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1136 8ba819a3 2020-07-23 stsp
1137 5a1fbc73 2020-07-23 stsp /*
1138 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1139 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1140 5a1fbc73 2020-07-23 stsp */
1141 8ba819a3 2020-07-23 stsp static const struct got_error *
1142 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1143 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1144 5a1fbc73 2020-07-23 stsp {
1145 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1146 5a1fbc73 2020-07-23 stsp ssize_t elen;
1147 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1148 5a1fbc73 2020-07-23 stsp int fd;
1149 5a1fbc73 2020-07-23 stsp
1150 c6e8a826 2021-04-05 stsp *did_something = 0;
1151 c6e8a826 2021-04-05 stsp
1152 5a1fbc73 2020-07-23 stsp /*
1153 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1154 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1155 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1156 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1157 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1158 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1159 5a1fbc73 2020-07-23 stsp */
1160 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1161 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1162 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1163 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1164 5a1fbc73 2020-07-23 stsp
1165 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1166 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1167 5a1fbc73 2020-07-23 stsp if (elen == -1)
1168 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1169 5a1fbc73 2020-07-23 stsp
1170 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1171 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1172 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1173 5a1fbc73 2020-07-23 stsp }
1174 5a1fbc73 2020-07-23 stsp
1175 c6e8a826 2021-04-05 stsp *did_something = 1;
1176 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1177 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1178 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1179 5a1fbc73 2020-07-23 stsp return err;
1180 3c1ec782 2020-07-23 stsp }
1181 3c1ec782 2020-07-23 stsp
1182 3c1ec782 2020-07-23 stsp static const struct got_error *
1183 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1184 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1185 3c1ec782 2020-07-23 stsp {
1186 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1187 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1188 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1189 3c1ec782 2020-07-23 stsp
1190 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1191 3c1ec782 2020-07-23 stsp
1192 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1193 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1194 3c1ec782 2020-07-23 stsp return NULL;
1195 3c1ec782 2020-07-23 stsp }
1196 3c1ec782 2020-07-23 stsp
1197 3c1ec782 2020-07-23 stsp /*
1198 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1199 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1200 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1201 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1202 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1203 3c1ec782 2020-07-23 stsp */
1204 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1205 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1206 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1207 ce031e9e 2020-10-20 stsp if (err)
1208 ce031e9e 2020-10-20 stsp return err;
1209 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1210 ce031e9e 2020-10-20 stsp free(parent);
1211 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1212 ce031e9e 2020-10-20 stsp }
1213 ce031e9e 2020-10-20 stsp free(parent);
1214 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1215 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1216 3c1ec782 2020-07-23 stsp free(abspath);
1217 3c1ec782 2020-07-23 stsp return err;
1218 3c1ec782 2020-07-23 stsp }
1219 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1220 3c1ec782 2020-07-23 stsp free(abspath);
1221 3c1ec782 2020-07-23 stsp if (err)
1222 3c1ec782 2020-07-23 stsp return err;
1223 3c1ec782 2020-07-23 stsp } else {
1224 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1225 3c1ec782 2020-07-23 stsp if (err)
1226 3c1ec782 2020-07-23 stsp return err;
1227 3c1ec782 2020-07-23 stsp }
1228 3c1ec782 2020-07-23 stsp
1229 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1230 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1231 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1232 3c1ec782 2020-07-23 stsp return NULL;
1233 3c1ec782 2020-07-23 stsp }
1234 3c1ec782 2020-07-23 stsp
1235 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1236 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1237 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1238 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1239 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1240 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1241 3c1ec782 2020-07-23 stsp
1242 3c1ec782 2020-07-23 stsp free(path_got);
1243 3c1ec782 2020-07-23 stsp return NULL;
1244 5a1fbc73 2020-07-23 stsp }
1245 5a1fbc73 2020-07-23 stsp
1246 5a1fbc73 2020-07-23 stsp static const struct got_error *
1247 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1248 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1249 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1250 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1251 ace90326 2021-09-27 thomas struct got_repository *repo,
1252 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 9d31a1d8 2018-03-11 stsp {
1254 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1255 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1256 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1257 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1258 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1259 8ba819a3 2020-07-23 stsp
1260 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1261 2e1fa222 2020-07-23 stsp
1262 9e39ca77 2022-07-21 thomas /*
1263 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1264 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1265 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1266 8ba819a3 2020-07-23 stsp * in the blob object.
1267 8ba819a3 2020-07-23 stsp */
1268 8ba819a3 2020-07-23 stsp do {
1269 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1270 3521b466 2022-07-21 thomas if (err)
1271 3521b466 2022-07-21 thomas return err;
1272 3521b466 2022-07-21 thomas
1273 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1274 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1275 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1276 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1277 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1278 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1280 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1281 3b9f0f87 2020-07-23 stsp progress_arg);
1282 8ba819a3 2020-07-23 stsp }
1283 8ba819a3 2020-07-23 stsp if (len > 0) {
1284 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1285 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1286 8ba819a3 2020-07-23 stsp len - hdrlen);
1287 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1288 8ba819a3 2020-07-23 stsp hdrlen = 0;
1289 8ba819a3 2020-07-23 stsp }
1290 8ba819a3 2020-07-23 stsp } while (len != 0);
1291 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1292 8ba819a3 2020-07-23 stsp
1293 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1295 3c1ec782 2020-07-23 stsp if (err)
1296 3c1ec782 2020-07-23 stsp return err;
1297 8ba819a3 2020-07-23 stsp
1298 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1299 906c123b 2020-07-23 stsp /* install as a regular file */
1300 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1301 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1302 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1304 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1305 9e39ca77 2022-07-21 thomas return err;
1306 906c123b 2020-07-23 stsp }
1307 906c123b 2020-07-23 stsp
1308 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1309 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1310 c6e8a826 2021-04-05 stsp int symlink_replaced;
1311 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1312 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1313 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1314 9e39ca77 2022-07-21 thomas return err;
1315 c90c8ce3 2020-07-23 stsp }
1316 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1317 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1318 5a1fbc73 2020-07-23 stsp if (err)
1319 9e39ca77 2022-07-21 thomas return err;
1320 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1321 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1322 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1323 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1324 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1325 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1326 c6e8a826 2021-04-05 stsp } else {
1327 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1328 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1329 c6e8a826 2021-04-05 stsp }
1330 f35fa46a 2020-07-23 stsp }
1331 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1332 f35fa46a 2020-07-23 stsp }
1333 f35fa46a 2020-07-23 stsp
1334 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1335 f4994adc 2020-10-20 stsp char *parent;
1336 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1337 f4994adc 2020-10-20 stsp if (err)
1338 9e39ca77 2022-07-21 thomas return err;
1339 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1340 f4994adc 2020-10-20 stsp free(parent);
1341 f35fa46a 2020-07-23 stsp if (err)
1342 9e39ca77 2022-07-21 thomas return err;
1343 f35fa46a 2020-07-23 stsp /*
1344 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1345 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1346 f35fa46a 2020-07-23 stsp */
1347 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1348 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1349 9e39ca77 2022-07-21 thomas return err;
1350 f35fa46a 2020-07-23 stsp }
1351 f35fa46a 2020-07-23 stsp }
1352 f35fa46a 2020-07-23 stsp
1353 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1354 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1355 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1356 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1357 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1358 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1359 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1361 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1362 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1363 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1364 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1365 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1366 8ba819a3 2020-07-23 stsp } else {
1367 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1368 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1369 8ba819a3 2020-07-23 stsp }
1370 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1371 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1373 8ba819a3 2020-07-23 stsp return err;
1374 8ba819a3 2020-07-23 stsp }
1375 8ba819a3 2020-07-23 stsp
1376 8ba819a3 2020-07-23 stsp static const struct got_error *
1377 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1378 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1379 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1380 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1381 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1382 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1383 8ba819a3 2020-07-23 stsp {
1384 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1385 507dc3bb 2018-12-29 stsp int fd = -1;
1386 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1387 507dc3bb 2018-12-29 stsp int update = 0;
1388 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1389 8ba819a3 2020-07-23 stsp
1390 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1391 06340621 2021-12-31 thomas O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1392 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1393 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1394 f5375317 2020-10-20 stsp char *parent;
1395 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1396 f5375317 2020-10-20 stsp if (err)
1397 f5375317 2020-10-20 stsp return err;
1398 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1399 f5375317 2020-10-20 stsp free(parent);
1400 21908da4 2019-01-13 stsp if (err)
1401 21908da4 2019-01-13 stsp return err;
1402 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1403 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1404 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1405 21908da4 2019-01-13 stsp if (fd == -1)
1406 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1407 230a42bd 2019-05-11 jcs ondisk_path);
1408 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1409 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1410 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1411 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1412 3b9f0f87 2020-07-23 stsp goto done;
1413 3b9f0f87 2020-07-23 stsp }
1414 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1415 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1416 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1417 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1418 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1419 507dc3bb 2018-12-29 stsp goto done;
1420 d70b8e30 2018-12-27 stsp } else {
1421 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1422 507dc3bb 2018-12-29 stsp ondisk_path);
1423 507dc3bb 2018-12-29 stsp if (err)
1424 507dc3bb 2018-12-29 stsp goto done;
1425 507dc3bb 2018-12-29 stsp update = 1;
1426 9d31a1d8 2018-03-11 stsp }
1427 507dc3bb 2018-12-29 stsp } else
1428 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1429 9d31a1d8 2018-03-11 stsp }
1430 9d31a1d8 2018-03-11 stsp
1431 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1432 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1433 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1434 3818e3c4 2020-11-01 naddy goto done;
1435 3818e3c4 2020-11-01 naddy }
1436 3818e3c4 2020-11-01 naddy
1437 bd6aa359 2020-07-23 stsp if (progress_cb) {
1438 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1439 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1440 bd6aa359 2020-07-23 stsp path);
1441 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1442 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1443 bd6aa359 2020-07-23 stsp path);
1444 bd6aa359 2020-07-23 stsp else
1445 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1446 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1447 bd6aa359 2020-07-23 stsp if (err)
1448 bd6aa359 2020-07-23 stsp goto done;
1449 bd6aa359 2020-07-23 stsp }
1450 d7b62c98 2018-12-27 stsp
1451 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1452 9d31a1d8 2018-03-11 stsp do {
1453 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1454 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1455 9d31a1d8 2018-03-11 stsp if (err)
1456 9d31a1d8 2018-03-11 stsp break;
1457 9d31a1d8 2018-03-11 stsp if (len > 0) {
1458 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1459 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1460 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1461 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1462 b87c6f83 2018-12-24 stsp goto done;
1463 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1464 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1465 b87c6f83 2018-12-24 stsp goto done;
1466 9d31a1d8 2018-03-11 stsp }
1467 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1468 9d31a1d8 2018-03-11 stsp }
1469 9d31a1d8 2018-03-11 stsp } while (len != 0);
1470 9d31a1d8 2018-03-11 stsp
1471 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1472 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1473 816dc654 2019-02-16 stsp goto done;
1474 816dc654 2019-02-16 stsp }
1475 9d31a1d8 2018-03-11 stsp
1476 507dc3bb 2018-12-29 stsp if (update) {
1477 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1478 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1479 f6d8c0ac 2020-11-09 stsp goto done;
1480 f6d8c0ac 2020-11-09 stsp }
1481 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1482 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1483 230a42bd 2019-05-11 jcs ondisk_path);
1484 68ed9ba5 2019-02-10 stsp goto done;
1485 68ed9ba5 2019-02-10 stsp }
1486 63df146d 2020-11-09 stsp free(tmppath);
1487 63df146d 2020-11-09 stsp tmppath = NULL;
1488 507dc3bb 2018-12-29 stsp }
1489 507dc3bb 2018-12-29 stsp
1490 9d31a1d8 2018-03-11 stsp done:
1491 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1492 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1493 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1494 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1495 507dc3bb 2018-12-29 stsp free(tmppath);
1496 6353ad76 2019-02-08 stsp return err;
1497 6353ad76 2019-02-08 stsp }
1498 6353ad76 2019-02-08 stsp
1499 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1500 6353ad76 2019-02-08 stsp static const struct got_error *
1501 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1502 7154f6ce 2019-03-27 stsp {
1503 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1504 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1505 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1506 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1507 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1508 7154f6ce 2019-03-27 stsp };
1509 7154f6ce 2019-03-27 stsp int i = 0;
1510 9bdd68dd 2020-01-02 naddy char *line = NULL;
1511 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1512 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1513 7154f6ce 2019-03-27 stsp
1514 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1515 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1516 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1517 7154f6ce 2019-03-27 stsp if (feof(f))
1518 7154f6ce 2019-03-27 stsp break;
1519 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1520 7154f6ce 2019-03-27 stsp break;
1521 7154f6ce 2019-03-27 stsp }
1522 7154f6ce 2019-03-27 stsp
1523 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1524 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1525 19332e6d 2019-05-13 stsp == 0)
1526 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1527 7154f6ce 2019-03-27 stsp else
1528 7154f6ce 2019-03-27 stsp i++;
1529 7154f6ce 2019-03-27 stsp }
1530 7154f6ce 2019-03-27 stsp }
1531 9bdd68dd 2020-01-02 naddy free(line);
1532 7154f6ce 2019-03-27 stsp
1533 7154f6ce 2019-03-27 stsp return err;
1534 e2b1e152 2019-07-13 stsp }
1535 e2b1e152 2019-07-13 stsp
1536 e2b1e152 2019-07-13 stsp static int
1537 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1538 1ebedb77 2019-10-19 stsp {
1539 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1540 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1541 1ebedb77 2019-10-19 stsp }
1542 1ebedb77 2019-10-19 stsp
1543 1ebedb77 2019-10-19 stsp static int
1544 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1545 e2b1e152 2019-07-13 stsp {
1546 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1547 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1548 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1549 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1550 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1551 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1552 c363b2c1 2019-08-03 stsp }
1553 c363b2c1 2019-08-03 stsp
1554 c363b2c1 2019-08-03 stsp static unsigned char
1555 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1556 c363b2c1 2019-08-03 stsp {
1557 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1558 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1559 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1560 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1561 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1562 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1563 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1564 c363b2c1 2019-08-03 stsp default:
1565 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1566 a919d5c4 2020-07-23 stsp }
1567 a919d5c4 2020-07-23 stsp }
1568 a919d5c4 2020-07-23 stsp
1569 a919d5c4 2020-07-23 stsp static const struct got_error *
1570 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1571 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1572 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1573 a919d5c4 2020-07-23 stsp {
1574 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1575 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1576 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1577 a919d5c4 2020-07-23 stsp ssize_t elen;
1578 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1579 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1580 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1581 a919d5c4 2020-07-23 stsp
1582 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1583 a919d5c4 2020-07-23 stsp
1584 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1585 a919d5c4 2020-07-23 stsp do {
1586 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1587 a919d5c4 2020-07-23 stsp if (err)
1588 a919d5c4 2020-07-23 stsp return err;
1589 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1590 a919d5c4 2020-07-23 stsp /*
1591 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1592 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1593 a919d5c4 2020-07-23 stsp */
1594 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1595 a919d5c4 2020-07-23 stsp }
1596 a919d5c4 2020-07-23 stsp if (len > 0) {
1597 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1598 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1599 a919d5c4 2020-07-23 stsp len - hdrlen);
1600 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1601 a919d5c4 2020-07-23 stsp hdrlen = 0;
1602 a919d5c4 2020-07-23 stsp }
1603 a919d5c4 2020-07-23 stsp } while (len != 0);
1604 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1605 a919d5c4 2020-07-23 stsp
1606 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1607 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1608 a919d5c4 2020-07-23 stsp if (elen == -1)
1609 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1610 a919d5c4 2020-07-23 stsp } else {
1611 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1612 a919d5c4 2020-07-23 stsp if (elen == -1)
1613 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1614 c363b2c1 2019-08-03 stsp }
1615 a919d5c4 2020-07-23 stsp
1616 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1617 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1618 a919d5c4 2020-07-23 stsp
1619 a919d5c4 2020-07-23 stsp return NULL;
1620 7154f6ce 2019-03-27 stsp }
1621 7154f6ce 2019-03-27 stsp
1622 7154f6ce 2019-03-27 stsp static const struct got_error *
1623 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1624 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1625 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1626 6353ad76 2019-02-08 stsp {
1627 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1628 6353ad76 2019-02-08 stsp struct got_object_id id;
1629 6353ad76 2019-02-08 stsp size_t hdrlen;
1630 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1631 6353ad76 2019-02-08 stsp FILE *f = NULL;
1632 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1633 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1634 6353ad76 2019-02-08 stsp size_t flen, blen;
1635 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1636 6353ad76 2019-02-08 stsp
1637 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1638 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1639 6353ad76 2019-02-08 stsp
1640 7f91a133 2019-12-13 stsp /*
1641 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1642 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1643 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1644 7f91a133 2019-12-13 stsp */
1645 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1646 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1647 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1648 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1649 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1650 882ef1b9 2019-12-13 stsp else
1651 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1652 882ef1b9 2019-12-13 stsp goto done;
1653 882ef1b9 2019-12-13 stsp }
1654 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1655 3d35a492 2019-12-13 stsp goto done;
1656 3d35a492 2019-12-13 stsp }
1657 7f91a133 2019-12-13 stsp } else {
1658 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1659 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1660 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1661 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1662 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1663 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1664 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1665 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1666 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1667 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1668 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1669 3d35a492 2019-12-13 stsp else
1670 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1671 3d35a492 2019-12-13 stsp goto done;
1672 3d35a492 2019-12-13 stsp }
1673 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1674 1338848f 2019-12-13 stsp goto done;
1675 a378724f 2019-02-10 stsp }
1676 a378724f 2019-02-10 stsp }
1677 6353ad76 2019-02-08 stsp
1678 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1679 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1680 1338848f 2019-12-13 stsp goto done;
1681 3f148bc6 2019-07-27 stsp }
1682 efdd40df 2019-07-27 stsp
1683 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1684 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1685 1338848f 2019-12-13 stsp goto done;
1686 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1687 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1688 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1689 1338848f 2019-12-13 stsp goto done;
1690 d00136be 2019-03-26 stsp }
1691 b8f41171 2019-02-10 stsp
1692 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1693 f179e66d 2020-07-23 stsp goto done;
1694 f179e66d 2020-07-23 stsp
1695 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1696 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1697 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1698 1338848f 2019-12-13 stsp goto done;
1699 f179e66d 2020-07-23 stsp }
1700 6353ad76 2019-02-08 stsp
1701 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1702 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1703 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1704 c363b2c1 2019-08-03 stsp else
1705 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1706 c363b2c1 2019-08-03 stsp
1707 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1708 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1709 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1710 f4ae6ddb 2022-07-01 thomas goto done;
1711 f4ae6ddb 2022-07-01 thomas }
1712 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1713 6353ad76 2019-02-08 stsp if (err)
1714 1338848f 2019-12-13 stsp goto done;
1715 6353ad76 2019-02-08 stsp
1716 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1717 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1718 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1719 a919d5c4 2020-07-23 stsp goto done;
1720 a919d5c4 2020-07-23 stsp }
1721 a919d5c4 2020-07-23 stsp
1722 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1723 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1724 ab0d4361 2019-12-13 stsp if (fd == -1) {
1725 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1726 ab0d4361 2019-12-13 stsp goto done;
1727 ab0d4361 2019-12-13 stsp }
1728 3d35a492 2019-12-13 stsp }
1729 3d35a492 2019-12-13 stsp
1730 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1731 6353ad76 2019-02-08 stsp if (f == NULL) {
1732 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1733 6353ad76 2019-02-08 stsp goto done;
1734 6353ad76 2019-02-08 stsp }
1735 1338848f 2019-12-13 stsp fd = -1;
1736 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1737 656b1f76 2019-05-11 jcs for (;;) {
1738 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1739 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1740 6353ad76 2019-02-08 stsp if (err)
1741 7154f6ce 2019-03-27 stsp goto done;
1742 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1743 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1744 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1745 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1746 7154f6ce 2019-03-27 stsp goto done;
1747 80c5c120 2019-02-19 stsp }
1748 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1749 6353ad76 2019-02-08 stsp if (flen != 0)
1750 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1751 6353ad76 2019-02-08 stsp break;
1752 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1753 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1754 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1755 6353ad76 2019-02-08 stsp break;
1756 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1757 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1758 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1759 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1760 6353ad76 2019-02-08 stsp break;
1761 6353ad76 2019-02-08 stsp }
1762 6353ad76 2019-02-08 stsp } else {
1763 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1764 6353ad76 2019-02-08 stsp break;
1765 6353ad76 2019-02-08 stsp }
1766 6353ad76 2019-02-08 stsp hdrlen = 0;
1767 6353ad76 2019-02-08 stsp }
1768 7154f6ce 2019-03-27 stsp
1769 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1770 7154f6ce 2019-03-27 stsp rewind(f);
1771 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1772 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1773 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1774 6353ad76 2019-02-08 stsp done:
1775 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1776 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1777 6353ad76 2019-02-08 stsp if (blob)
1778 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1779 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1780 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1781 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1782 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1783 9d31a1d8 2018-03-11 stsp return err;
1784 e2b1e152 2019-07-13 stsp }
1785 e2b1e152 2019-07-13 stsp
1786 e2b1e152 2019-07-13 stsp /*
1787 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1788 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1789 e2b1e152 2019-07-13 stsp */
1790 e2b1e152 2019-07-13 stsp static const struct got_error *
1791 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1792 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1793 e2b1e152 2019-07-13 stsp {
1794 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1795 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1796 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1797 e2b1e152 2019-07-13 stsp
1798 e2b1e152 2019-07-13 stsp return NULL;
1799 9d31a1d8 2018-03-11 stsp }
1800 9d31a1d8 2018-03-11 stsp
1801 9d31a1d8 2018-03-11 stsp static const struct got_error *
1802 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1803 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1804 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1805 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1806 0584f854 2019-04-06 stsp void *progress_arg)
1807 9d31a1d8 2018-03-11 stsp {
1808 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1809 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1810 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1811 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1812 b8f41171 2019-02-10 stsp struct stat sb;
1813 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1814 9d31a1d8 2018-03-11 stsp
1815 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1816 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1817 6353ad76 2019-02-08 stsp
1818 abb4604f 2019-07-27 stsp if (ie) {
1819 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1820 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1821 a76c42e6 2019-08-03 stsp goto done;
1822 a76c42e6 2019-08-03 stsp }
1823 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1824 7f91a133 2019-12-13 stsp repo);
1825 abb4604f 2019-07-27 stsp if (err)
1826 abb4604f 2019-07-27 stsp goto done;
1827 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1828 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1829 c90c8ce3 2020-07-23 stsp } else {
1830 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1831 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1832 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1833 e6f4ba31 2021-09-24 thomas ondisk_path);
1834 e6f4ba31 2021-09-24 thomas goto done;
1835 e6f4ba31 2021-09-24 thomas }
1836 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1837 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1838 e6f4ba31 2021-09-24 thomas } else {
1839 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1840 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1841 e6f4ba31 2021-09-24 thomas else
1842 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1843 e6f4ba31 2021-09-24 thomas }
1844 c90c8ce3 2020-07-23 stsp }
1845 6353ad76 2019-02-08 stsp
1846 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1847 2c41dce7 2021-06-27 stsp if (ie)
1848 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1849 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1850 b8f41171 2019-02-10 stsp goto done;
1851 b8f41171 2019-02-10 stsp }
1852 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1853 a769b60b 2021-06-27 stsp if (ie)
1854 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1855 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1856 5036ab18 2020-04-18 stsp path);
1857 5036ab18 2020-04-18 stsp goto done;
1858 5036ab18 2020-04-18 stsp }
1859 b8f41171 2019-02-10 stsp
1860 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1861 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1862 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1863 c6e8a826 2021-04-05 stsp /*
1864 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1865 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1866 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1867 c6e8a826 2021-04-05 stsp * updating contents of this file.
1868 c6e8a826 2021-04-05 stsp */
1869 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1870 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1871 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1872 c6e8a826 2021-04-05 stsp /* Same commit. */
1873 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1874 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1875 e2b1e152 2019-07-13 stsp if (err)
1876 e2b1e152 2019-07-13 stsp goto done;
1877 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1878 b8f41171 2019-02-10 stsp path);
1879 6353ad76 2019-02-08 stsp goto done;
1880 a378724f 2019-02-10 stsp }
1881 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1882 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1883 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1884 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1885 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1886 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1887 0f58026f 2021-04-05 stsp if (err)
1888 0f58026f 2021-04-05 stsp goto done;
1889 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1890 0f58026f 2021-04-05 stsp path);
1891 b8f41171 2019-02-10 stsp goto done;
1892 e2b1e152 2019-07-13 stsp }
1893 9d31a1d8 2018-03-11 stsp }
1894 9d31a1d8 2018-03-11 stsp
1895 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1896 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1897 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1898 f4ae6ddb 2022-07-01 thomas goto done;
1899 f4ae6ddb 2022-07-01 thomas }
1900 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1901 8da9e5f4 2019-01-12 stsp if (err)
1902 6353ad76 2019-02-08 stsp goto done;
1903 512f0d0e 2019-01-02 stsp
1904 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1905 234035bc 2019-06-01 stsp int update_timestamps;
1906 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1907 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1908 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1909 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1910 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1911 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1912 f4ae6ddb 2022-07-01 thomas goto done;
1913 f4ae6ddb 2022-07-01 thomas }
1914 234035bc 2019-06-01 stsp struct got_object_id id2;
1915 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1916 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1917 f4ae6ddb 2022-07-01 thomas fd2);
1918 234035bc 2019-06-01 stsp if (err)
1919 f69721c3 2019-10-21 stsp goto done;
1920 f69721c3 2019-10-21 stsp }
1921 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1922 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1923 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1924 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1925 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1926 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1927 f69721c3 2019-10-21 stsp goto done;
1928 f69721c3 2019-10-21 stsp }
1929 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1930 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1931 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1932 234035bc 2019-06-01 stsp goto done;
1933 f69721c3 2019-10-21 stsp }
1934 234035bc 2019-06-01 stsp }
1935 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1936 36bf999c 2020-07-23 stsp char *link_target;
1937 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1938 36bf999c 2020-07-23 stsp if (err)
1939 36bf999c 2020-07-23 stsp goto done;
1940 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1941 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1942 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1943 36bf999c 2020-07-23 stsp free(link_target);
1944 993e2a1b 2020-07-23 stsp } else {
1945 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1946 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1947 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1948 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1949 993e2a1b 2020-07-23 stsp }
1950 f69721c3 2019-10-21 stsp free(label_orig);
1951 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1952 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1953 f4ae6ddb 2022-07-01 thomas goto done;
1954 f4ae6ddb 2022-07-01 thomas }
1955 234035bc 2019-06-01 stsp if (blob2)
1956 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1957 909d120e 2019-09-22 stsp if (err)
1958 909d120e 2019-09-22 stsp goto done;
1959 234035bc 2019-06-01 stsp /*
1960 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1961 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1962 234035bc 2019-06-01 stsp * unmodified files again.
1963 234035bc 2019-06-01 stsp */
1964 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1965 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1966 234035bc 2019-06-01 stsp update_timestamps);
1967 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1968 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1969 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1970 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1971 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1972 1ee397ad 2019-07-12 stsp if (err)
1973 1ee397ad 2019-07-12 stsp goto done;
1974 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1975 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1976 13d9040b 2019-03-27 stsp if (err)
1977 13d9040b 2019-03-27 stsp goto done;
1978 13d9040b 2019-03-27 stsp } else {
1979 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1980 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1981 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1982 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1983 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1984 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
1985 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
1986 2e1fa222 2020-07-23 stsp } else {
1987 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1988 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1989 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1990 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1991 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1992 2e1fa222 2020-07-23 stsp }
1993 13d9040b 2019-03-27 stsp if (err)
1994 13d9040b 2019-03-27 stsp goto done;
1995 65b05cec 2020-07-23 stsp
1996 054041d0 2020-06-24 stsp if (ie) {
1997 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
1998 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
1999 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2000 054041d0 2020-06-24 stsp } else {
2001 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2002 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2003 054041d0 2020-06-24 stsp &blob->id);
2004 054041d0 2020-06-24 stsp }
2005 13d9040b 2019-03-27 stsp if (err)
2006 13d9040b 2019-03-27 stsp goto done;
2007 65b05cec 2020-07-23 stsp
2008 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2009 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2010 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2011 2e1fa222 2020-07-23 stsp }
2012 13d9040b 2019-03-27 stsp }
2013 f4ae6ddb 2022-07-01 thomas
2014 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2015 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2016 f4ae6ddb 2022-07-01 thomas goto done;
2017 f4ae6ddb 2022-07-01 thomas }
2018 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2019 6353ad76 2019-02-08 stsp done:
2020 6353ad76 2019-02-08 stsp free(ondisk_path);
2021 8da9e5f4 2019-01-12 stsp return err;
2022 90285c3b 2019-01-08 stsp }
2023 90285c3b 2019-01-08 stsp
2024 90285c3b 2019-01-08 stsp static const struct got_error *
2025 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2026 90285c3b 2019-01-08 stsp {
2027 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2028 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2029 90285c3b 2019-01-08 stsp
2030 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2031 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2032 90285c3b 2019-01-08 stsp
2033 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2034 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2035 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2036 c97665ae 2019-01-13 stsp } else {
2037 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2038 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2039 1c4cdd89 2021-06-20 stsp if (err)
2040 1c4cdd89 2021-06-20 stsp goto done;
2041 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2042 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2043 fddefe3b 2020-10-20 stsp free(ondisk_path);
2044 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2045 1c4cdd89 2021-06-20 stsp parent = NULL;
2046 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2047 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2048 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2049 fddefe3b 2020-10-20 stsp ondisk_path);
2050 90285c3b 2019-01-08 stsp break;
2051 90285c3b 2019-01-08 stsp }
2052 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2053 1c4cdd89 2021-06-20 stsp if (err)
2054 1c4cdd89 2021-06-20 stsp break;
2055 1c4cdd89 2021-06-20 stsp }
2056 90285c3b 2019-01-08 stsp }
2057 1c4cdd89 2021-06-20 stsp done:
2058 90285c3b 2019-01-08 stsp free(ondisk_path);
2059 1c4cdd89 2021-06-20 stsp free(parent);
2060 90285c3b 2019-01-08 stsp return err;
2061 512f0d0e 2019-01-02 stsp }
2062 512f0d0e 2019-01-02 stsp
2063 708d8e67 2019-03-27 stsp static const struct got_error *
2064 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2065 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2066 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2067 708d8e67 2019-03-27 stsp {
2068 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2069 708d8e67 2019-03-27 stsp unsigned char status;
2070 708d8e67 2019-03-27 stsp struct stat sb;
2071 708d8e67 2019-03-27 stsp char *ondisk_path;
2072 708d8e67 2019-03-27 stsp
2073 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2074 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2075 a76c42e6 2019-08-03 stsp
2076 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2077 708d8e67 2019-03-27 stsp == -1)
2078 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2079 708d8e67 2019-03-27 stsp
2080 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2081 708d8e67 2019-03-27 stsp if (err)
2082 5a58a424 2020-06-23 stsp goto done;
2083 708d8e67 2019-03-27 stsp
2084 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2085 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2086 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2087 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2088 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2089 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2090 993e2a1b 2020-07-23 stsp goto done;
2091 993e2a1b 2020-07-23 stsp }
2092 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2093 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2094 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2095 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2096 993e2a1b 2020-07-23 stsp if (err)
2097 993e2a1b 2020-07-23 stsp goto done;
2098 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2099 993e2a1b 2020-07-23 stsp ie->path);
2100 993e2a1b 2020-07-23 stsp goto done;
2101 993e2a1b 2020-07-23 stsp }
2102 993e2a1b 2020-07-23 stsp
2103 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2104 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2105 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2106 1ee397ad 2019-07-12 stsp if (err)
2107 5a58a424 2020-06-23 stsp goto done;
2108 fc6346c4 2019-03-27 stsp /*
2109 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2110 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2111 fc6346c4 2019-03-27 stsp */
2112 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2113 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2114 fc6346c4 2019-03-27 stsp } else {
2115 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2116 1ee397ad 2019-07-12 stsp if (err)
2117 5a58a424 2020-06-23 stsp goto done;
2118 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2119 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2120 fc6346c4 2019-03-27 stsp if (err)
2121 5a58a424 2020-06-23 stsp goto done;
2122 fc6346c4 2019-03-27 stsp }
2123 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2124 708d8e67 2019-03-27 stsp }
2125 5a58a424 2020-06-23 stsp done:
2126 5a58a424 2020-06-23 stsp free(ondisk_path);
2127 fc6346c4 2019-03-27 stsp return err;
2128 708d8e67 2019-03-27 stsp }
2129 708d8e67 2019-03-27 stsp
2130 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2131 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2132 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2133 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2134 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2135 8da9e5f4 2019-01-12 stsp void *progress_arg;
2136 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2137 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2138 8da9e5f4 2019-01-12 stsp };
2139 8da9e5f4 2019-01-12 stsp
2140 512f0d0e 2019-01-02 stsp static const struct got_error *
2141 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2142 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2143 512f0d0e 2019-01-02 stsp {
2144 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2145 0584f854 2019-04-06 stsp
2146 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2148 512f0d0e 2019-01-02 stsp
2149 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2150 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2151 8da9e5f4 2019-01-12 stsp }
2152 512f0d0e 2019-01-02 stsp
2153 8da9e5f4 2019-01-12 stsp static const struct got_error *
2154 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2155 8da9e5f4 2019-01-12 stsp {
2156 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2157 7a9df742 2019-01-08 stsp
2158 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2159 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2160 0584f854 2019-04-06 stsp
2161 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2162 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2163 9d31a1d8 2018-03-11 stsp }
2164 9d31a1d8 2018-03-11 stsp
2165 9d31a1d8 2018-03-11 stsp static const struct got_error *
2166 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2167 9d31a1d8 2018-03-11 stsp {
2168 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2169 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2170 8da9e5f4 2019-01-12 stsp char *path;
2171 9d31a1d8 2018-03-11 stsp
2172 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2173 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2174 0584f854 2019-04-06 stsp
2175 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2176 63c5ca5d 2019-08-24 stsp return NULL;
2177 63c5ca5d 2019-08-24 stsp
2178 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2179 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2180 8da9e5f4 2019-01-12 stsp == -1)
2181 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2182 9d31a1d8 2018-03-11 stsp
2183 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2184 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2185 8da9e5f4 2019-01-12 stsp else
2186 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2187 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2188 9d31a1d8 2018-03-11 stsp
2189 8da9e5f4 2019-01-12 stsp free(path);
2190 0cd1c46a 2019-03-11 stsp return err;
2191 0cd1c46a 2019-03-11 stsp }
2192 0cd1c46a 2019-03-11 stsp
2193 b2118c49 2020-07-28 stsp const struct got_error *
2194 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2195 b2118c49 2020-07-28 stsp {
2196 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2197 b2118c49 2020-07-28 stsp
2198 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2199 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2200 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2201 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2202 b2118c49 2020-07-28 stsp }
2203 b2118c49 2020-07-28 stsp
2204 b2118c49 2020-07-28 stsp return NULL;
2205 b2118c49 2020-07-28 stsp }
2206 b2118c49 2020-07-28 stsp
2207 818c7501 2019-07-11 stsp static const struct got_error *
2208 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2209 0cd1c46a 2019-03-11 stsp {
2210 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2211 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2212 0cd1c46a 2019-03-11 stsp
2213 517bab73 2019-03-11 stsp *refname = NULL;
2214 517bab73 2019-03-11 stsp
2215 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2216 b2118c49 2020-07-28 stsp if (err)
2217 b2118c49 2020-07-28 stsp return err;
2218 0cd1c46a 2019-03-11 stsp
2219 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2220 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2221 0647c563 2019-03-11 stsp *refname = NULL;
2222 0cd1c46a 2019-03-11 stsp }
2223 517bab73 2019-03-11 stsp free(uuidstr);
2224 517bab73 2019-03-11 stsp return err;
2225 517bab73 2019-03-11 stsp }
2226 517bab73 2019-03-11 stsp
2227 818c7501 2019-07-11 stsp const struct got_error *
2228 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2229 818c7501 2019-07-11 stsp {
2230 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2231 818c7501 2019-07-11 stsp }
2232 818c7501 2019-07-11 stsp
2233 818c7501 2019-07-11 stsp static const struct got_error *
2234 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2235 818c7501 2019-07-11 stsp {
2236 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2237 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2238 818c7501 2019-07-11 stsp }
2239 818c7501 2019-07-11 stsp
2240 818c7501 2019-07-11 stsp static const struct got_error *
2241 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2242 818c7501 2019-07-11 stsp {
2243 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2244 818c7501 2019-07-11 stsp }
2245 818c7501 2019-07-11 stsp
2246 818c7501 2019-07-11 stsp static const struct got_error *
2247 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2248 818c7501 2019-07-11 stsp {
2249 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2250 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2251 818c7501 2019-07-11 stsp }
2252 818c7501 2019-07-11 stsp
2253 818c7501 2019-07-11 stsp static const struct got_error *
2254 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2255 818c7501 2019-07-11 stsp {
2256 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2257 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2258 0ebf8283 2019-07-24 stsp }
2259 0ebf8283 2019-07-24 stsp
2260 0ebf8283 2019-07-24 stsp static const struct got_error *
2261 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2262 0ebf8283 2019-07-24 stsp {
2263 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2264 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2265 0ebf8283 2019-07-24 stsp }
2266 0ebf8283 2019-07-24 stsp
2267 0ebf8283 2019-07-24 stsp static const struct got_error *
2268 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2269 0ebf8283 2019-07-24 stsp {
2270 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2271 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2272 0ebf8283 2019-07-24 stsp }
2273 0ebf8283 2019-07-24 stsp
2274 0ebf8283 2019-07-24 stsp static const struct got_error *
2275 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2276 0ebf8283 2019-07-24 stsp {
2277 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2278 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2279 818c7501 2019-07-11 stsp }
2280 818c7501 2019-07-11 stsp
2281 0ebf8283 2019-07-24 stsp static const struct got_error *
2282 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2283 0ebf8283 2019-07-24 stsp {
2284 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2285 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2286 0ebf8283 2019-07-24 stsp }
2287 818c7501 2019-07-11 stsp
2288 0ebf8283 2019-07-24 stsp const struct got_error *
2289 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2290 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2291 0ebf8283 2019-07-24 stsp {
2292 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2293 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2294 0ebf8283 2019-07-24 stsp *path = NULL;
2295 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2296 0ebf8283 2019-07-24 stsp }
2297 0ebf8283 2019-07-24 stsp return NULL;
2298 10604dce 2021-09-24 thomas }
2299 10604dce 2021-09-24 thomas
2300 10604dce 2021-09-24 thomas static const struct got_error *
2301 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2302 10604dce 2021-09-24 thomas {
2303 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2304 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2305 0ebf8283 2019-07-24 stsp }
2306 0ebf8283 2019-07-24 stsp
2307 10604dce 2021-09-24 thomas static const struct got_error *
2308 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 10604dce 2021-09-24 thomas {
2310 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2311 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2312 10604dce 2021-09-24 thomas }
2313 10604dce 2021-09-24 thomas
2314 517bab73 2019-03-11 stsp /*
2315 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2316 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2317 517bab73 2019-03-11 stsp */
2318 517bab73 2019-03-11 stsp static const struct got_error *
2319 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2320 517bab73 2019-03-11 stsp {
2321 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2322 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2323 517bab73 2019-03-11 stsp char *refname;
2324 517bab73 2019-03-11 stsp
2325 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2326 517bab73 2019-03-11 stsp if (err)
2327 517bab73 2019-03-11 stsp return err;
2328 0cd1c46a 2019-03-11 stsp
2329 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2330 0cd1c46a 2019-03-11 stsp if (err)
2331 0cd1c46a 2019-03-11 stsp goto done;
2332 0cd1c46a 2019-03-11 stsp
2333 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2334 0cd1c46a 2019-03-11 stsp done:
2335 0cd1c46a 2019-03-11 stsp free(refname);
2336 0cd1c46a 2019-03-11 stsp if (ref)
2337 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2338 3e3a69f1 2019-07-25 stsp return err;
2339 3e3a69f1 2019-07-25 stsp }
2340 3e3a69f1 2019-07-25 stsp
2341 3e3a69f1 2019-07-25 stsp static const struct got_error *
2342 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2343 3e3a69f1 2019-07-25 stsp {
2344 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2345 3e3a69f1 2019-07-25 stsp
2346 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2347 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2348 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2349 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2350 3e3a69f1 2019-07-25 stsp }
2351 9d31a1d8 2018-03-11 stsp return err;
2352 9d31a1d8 2018-03-11 stsp }
2353 9d31a1d8 2018-03-11 stsp
2354 3e3a69f1 2019-07-25 stsp
2355 ebf99748 2019-05-09 stsp static const struct got_error *
2356 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2357 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2358 ebf99748 2019-05-09 stsp {
2359 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2360 ebf99748 2019-05-09 stsp FILE *index = NULL;
2361 0cd1c46a 2019-03-11 stsp
2362 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2363 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2364 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2365 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2366 ebf99748 2019-05-09 stsp
2367 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2368 3e3a69f1 2019-07-25 stsp if (err)
2369 ebf99748 2019-05-09 stsp goto done;
2370 ebf99748 2019-05-09 stsp
2371 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2372 ebf99748 2019-05-09 stsp if (index == NULL) {
2373 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2374 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2375 ebf99748 2019-05-09 stsp } else {
2376 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2377 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2378 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2379 ebf99748 2019-05-09 stsp }
2380 ebf99748 2019-05-09 stsp done:
2381 ebf99748 2019-05-09 stsp if (err) {
2382 ebf99748 2019-05-09 stsp free(*fileindex_path);
2383 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2384 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2385 ebf99748 2019-05-09 stsp *fileindex = NULL;
2386 ebf99748 2019-05-09 stsp }
2387 ebf99748 2019-05-09 stsp return err;
2388 ebf99748 2019-05-09 stsp }
2389 c932eeeb 2019-05-22 stsp
2390 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2391 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2392 c932eeeb 2019-05-22 stsp const char *path;
2393 c932eeeb 2019-05-22 stsp size_t path_len;
2394 c932eeeb 2019-05-22 stsp const char *entry_name;
2395 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2396 a484d721 2019-06-10 stsp void *progress_arg;
2397 c932eeeb 2019-05-22 stsp };
2398 ebf99748 2019-05-09 stsp
2399 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2400 c932eeeb 2019-05-22 stsp static const struct got_error *
2401 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2402 c932eeeb 2019-05-22 stsp {
2403 1ee397ad 2019-07-12 stsp const struct got_error *err;
2404 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2405 c932eeeb 2019-05-22 stsp
2406 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2407 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2408 c932eeeb 2019-05-22 stsp return NULL;
2409 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2410 102ff934 2019-06-11 stsp return NULL;
2411 102ff934 2019-06-11 stsp
2412 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2413 a769b60b 2021-06-27 stsp return NULL;
2414 a769b60b 2021-06-27 stsp
2415 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2416 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2417 c932eeeb 2019-05-22 stsp return NULL;
2418 c932eeeb 2019-05-22 stsp
2419 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2420 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2421 edd02c5e 2019-07-11 stsp ie->path);
2422 1ee397ad 2019-07-12 stsp if (err)
2423 1ee397ad 2019-07-12 stsp return err;
2424 1ee397ad 2019-07-12 stsp }
2425 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2426 c932eeeb 2019-05-22 stsp return NULL;
2427 a615e0e7 2020-12-16 stsp }
2428 a615e0e7 2020-12-16 stsp
2429 a615e0e7 2020-12-16 stsp static const struct got_error *
2430 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2431 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2432 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2433 a615e0e7 2020-12-16 stsp {
2434 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2435 a615e0e7 2020-12-16 stsp
2436 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2437 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2438 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2439 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2440 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2441 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2442 a615e0e7 2020-12-16 stsp
2443 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2444 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2445 9c6338c4 2019-06-02 stsp }
2446 9c6338c4 2019-06-02 stsp
2447 9c6338c4 2019-06-02 stsp static const struct got_error *
2448 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2449 9c6338c4 2019-06-02 stsp {
2450 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2451 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2452 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2453 867630bb 2020-01-17 stsp struct timespec timeout;
2454 9c6338c4 2019-06-02 stsp
2455 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2456 9c6338c4 2019-06-02 stsp fileindex_path);
2457 9c6338c4 2019-06-02 stsp if (err)
2458 9c6338c4 2019-06-02 stsp goto done;
2459 9c6338c4 2019-06-02 stsp
2460 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2461 9c6338c4 2019-06-02 stsp if (err)
2462 9c6338c4 2019-06-02 stsp goto done;
2463 9c6338c4 2019-06-02 stsp
2464 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2465 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2466 9c6338c4 2019-06-02 stsp fileindex_path);
2467 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2468 9c6338c4 2019-06-02 stsp }
2469 867630bb 2020-01-17 stsp
2470 867630bb 2020-01-17 stsp /*
2471 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2472 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2473 867630bb 2020-01-17 stsp * was recorded in the file index.
2474 867630bb 2020-01-17 stsp */
2475 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2476 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2477 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2478 9c6338c4 2019-06-02 stsp done:
2479 9c6338c4 2019-06-02 stsp if (new_index)
2480 9c6338c4 2019-06-02 stsp fclose(new_index);
2481 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2482 9c6338c4 2019-06-02 stsp return err;
2483 c932eeeb 2019-05-22 stsp }
2484 6ced4176 2019-07-12 stsp
2485 6ced4176 2019-07-12 stsp static const struct got_error *
2486 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2487 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2488 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2489 945f9229 2022-04-16 thomas struct got_repository *repo)
2490 6ced4176 2019-07-12 stsp {
2491 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2492 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2493 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2494 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2495 6ced4176 2019-07-12 stsp
2496 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2497 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2498 6ced4176 2019-07-12 stsp *tree_id = NULL;
2499 6ced4176 2019-07-12 stsp
2500 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2501 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2502 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2503 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2504 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2505 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2506 6ced4176 2019-07-12 stsp goto done;
2507 6ced4176 2019-07-12 stsp }
2508 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2509 945f9229 2022-04-16 thomas worktree->path_prefix);
2510 6ced4176 2019-07-12 stsp if (err)
2511 6ced4176 2019-07-12 stsp goto done;
2512 6ced4176 2019-07-12 stsp return NULL;
2513 6ced4176 2019-07-12 stsp }
2514 6ced4176 2019-07-12 stsp
2515 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2516 6ced4176 2019-07-12 stsp
2517 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2518 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2519 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2520 6ced4176 2019-07-12 stsp goto done;
2521 6ced4176 2019-07-12 stsp }
2522 6ced4176 2019-07-12 stsp
2523 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2524 6ced4176 2019-07-12 stsp if (err)
2525 6ced4176 2019-07-12 stsp goto done;
2526 6ced4176 2019-07-12 stsp
2527 6ced4176 2019-07-12 stsp free(in_repo_path);
2528 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2529 6ced4176 2019-07-12 stsp
2530 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2531 6ced4176 2019-07-12 stsp if (err)
2532 6ced4176 2019-07-12 stsp goto done;
2533 c932eeeb 2019-05-22 stsp
2534 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2535 6ced4176 2019-07-12 stsp /* Check out a single file. */
2536 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2537 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2538 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2539 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2540 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2541 6ced4176 2019-07-12 stsp goto done;
2542 6ced4176 2019-07-12 stsp }
2543 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2544 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2545 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2546 6ced4176 2019-07-12 stsp goto done;
2547 6ced4176 2019-07-12 stsp }
2548 6ced4176 2019-07-12 stsp } else {
2549 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2550 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2551 6ced4176 2019-07-12 stsp if (err)
2552 6ced4176 2019-07-12 stsp return err;
2553 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2554 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2555 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2556 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2557 6ced4176 2019-07-12 stsp goto done;
2558 6ced4176 2019-07-12 stsp }
2559 6ced4176 2019-07-12 stsp }
2560 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2561 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2562 6ced4176 2019-07-12 stsp } else {
2563 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2564 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2565 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2566 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2567 6ced4176 2019-07-12 stsp goto done;
2568 6ced4176 2019-07-12 stsp }
2569 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2570 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2571 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2572 6ced4176 2019-07-12 stsp goto done;
2573 6ced4176 2019-07-12 stsp }
2574 6ced4176 2019-07-12 stsp }
2575 6ced4176 2019-07-12 stsp done:
2576 6ced4176 2019-07-12 stsp free(id);
2577 6ced4176 2019-07-12 stsp free(in_repo_path);
2578 6ced4176 2019-07-12 stsp if (err) {
2579 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2580 6ced4176 2019-07-12 stsp free(*tree_relpath);
2581 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2582 6ced4176 2019-07-12 stsp free(*tree_id);
2583 6ced4176 2019-07-12 stsp *tree_id = NULL;
2584 6ced4176 2019-07-12 stsp }
2585 07ed472d 2019-07-12 stsp return err;
2586 07ed472d 2019-07-12 stsp }
2587 07ed472d 2019-07-12 stsp
2588 07ed472d 2019-07-12 stsp static const struct got_error *
2589 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2590 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2591 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2592 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2593 07ed472d 2019-07-12 stsp {
2594 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2595 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2596 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2597 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2598 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2599 07ed472d 2019-07-12 stsp
2600 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2601 7f47418f 2019-12-20 stsp if (err) {
2602 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2603 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2604 7f47418f 2019-12-20 stsp goto done;
2605 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2606 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2607 7f47418f 2019-12-20 stsp if (err)
2608 7f47418f 2019-12-20 stsp return err;
2609 7f47418f 2019-12-20 stsp }
2610 07ed472d 2019-07-12 stsp
2611 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2612 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2613 07ed472d 2019-07-12 stsp if (err)
2614 07ed472d 2019-07-12 stsp goto done;
2615 07ed472d 2019-07-12 stsp
2616 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2617 07ed472d 2019-07-12 stsp if (err)
2618 07ed472d 2019-07-12 stsp goto done;
2619 07ed472d 2019-07-12 stsp
2620 07ed472d 2019-07-12 stsp if (entry_name &&
2621 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2622 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2623 07ed472d 2019-07-12 stsp goto done;
2624 07ed472d 2019-07-12 stsp }
2625 07ed472d 2019-07-12 stsp
2626 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2627 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2628 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2629 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2630 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2631 07ed472d 2019-07-12 stsp arg.repo = repo;
2632 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2633 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2634 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2635 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2636 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2637 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2638 07ed472d 2019-07-12 stsp done:
2639 07ed472d 2019-07-12 stsp if (tree)
2640 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2641 07ed472d 2019-07-12 stsp if (commit)
2642 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2643 6ced4176 2019-07-12 stsp return err;
2644 6ced4176 2019-07-12 stsp }
2645 6ced4176 2019-07-12 stsp
2646 9d31a1d8 2018-03-11 stsp const struct got_error *
2647 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2648 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2649 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2650 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2651 9d31a1d8 2018-03-11 stsp {
2652 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2653 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2654 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2655 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2656 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2657 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2658 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2659 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2660 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2661 f2ea84fa 2019-07-27 stsp int entry_type;
2662 f2ea84fa 2019-07-27 stsp char *relpath;
2663 f2ea84fa 2019-07-27 stsp char *entry_name;
2664 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2665 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2666 9d31a1d8 2018-03-11 stsp
2667 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2668 f2ea84fa 2019-07-27 stsp
2669 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2670 9d31a1d8 2018-03-11 stsp if (err)
2671 9d31a1d8 2018-03-11 stsp return err;
2672 945f9229 2022-04-16 thomas
2673 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2674 945f9229 2022-04-16 thomas worktree->base_commit_id);
2675 945f9229 2022-04-16 thomas if (err)
2676 945f9229 2022-04-16 thomas goto done;
2677 93a30277 2018-12-24 stsp
2678 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2679 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2680 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2681 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2682 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2683 f2ea84fa 2019-07-27 stsp goto done;
2684 f2ea84fa 2019-07-27 stsp }
2685 6ced4176 2019-07-12 stsp
2686 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2687 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2688 945f9229 2022-04-16 thomas worktree, repo);
2689 f2ea84fa 2019-07-27 stsp if (err) {
2690 f2ea84fa 2019-07-27 stsp free(tpd);
2691 6ced4176 2019-07-12 stsp goto done;
2692 6ced4176 2019-07-12 stsp }
2693 f2ea84fa 2019-07-27 stsp
2694 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2695 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2696 f2ea84fa 2019-07-27 stsp if (err) {
2697 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2698 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2699 f2ea84fa 2019-07-27 stsp free(tpd);
2700 f2ea84fa 2019-07-27 stsp goto done;
2701 f2ea84fa 2019-07-27 stsp }
2702 f2ea84fa 2019-07-27 stsp } else
2703 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2704 f2ea84fa 2019-07-27 stsp
2705 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2706 6ced4176 2019-07-12 stsp }
2707 6ced4176 2019-07-12 stsp
2708 51514078 2018-12-25 stsp /*
2709 51514078 2018-12-25 stsp * Read the file index.
2710 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2711 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2712 51514078 2018-12-25 stsp */
2713 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2714 8da9e5f4 2019-01-12 stsp if (err)
2715 c4cdcb68 2019-04-03 stsp goto done;
2716 c4cdcb68 2019-04-03 stsp
2717 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2718 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2719 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2720 f2ea84fa 2019-07-27 stsp
2721 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2722 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2723 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2724 f2ea84fa 2019-07-27 stsp if (err)
2725 f2ea84fa 2019-07-27 stsp break;
2726 f2ea84fa 2019-07-27 stsp
2727 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2728 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2729 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2730 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2731 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2732 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2733 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2734 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2735 f2ea84fa 2019-07-27 stsp if (err)
2736 f2ea84fa 2019-07-27 stsp break;
2737 f2ea84fa 2019-07-27 stsp
2738 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2739 711d9cd9 2019-07-12 stsp }
2740 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2741 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2742 af12c6b9 2019-06-04 stsp err = sync_err;
2743 9d31a1d8 2018-03-11 stsp done:
2744 9c6338c4 2019-06-02 stsp free(fileindex_path);
2745 edfa7d7f 2018-09-11 stsp if (tree)
2746 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2747 9d31a1d8 2018-03-11 stsp if (commit)
2748 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2749 5ade8233 2019-07-12 stsp if (fileindex)
2750 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2751 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2752 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2753 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2754 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2755 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2756 f2ea84fa 2019-07-27 stsp free(tpd);
2757 f2ea84fa 2019-07-27 stsp }
2758 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2759 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2760 9d31a1d8 2018-03-11 stsp err = unlockerr;
2761 f8d1f275 2019-02-04 stsp return err;
2762 f8d1f275 2019-02-04 stsp }
2763 f8d1f275 2019-02-04 stsp
2764 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2765 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2766 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2767 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2768 234035bc 2019-06-01 stsp void *progress_arg;
2769 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2770 234035bc 2019-06-01 stsp void *cancel_arg;
2771 f69721c3 2019-10-21 stsp const char *label_orig;
2772 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2773 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2774 234035bc 2019-06-01 stsp };
2775 234035bc 2019-06-01 stsp
2776 234035bc 2019-06-01 stsp static const struct got_error *
2777 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2778 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2779 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2780 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2781 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2782 234035bc 2019-06-01 stsp {
2783 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2784 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2785 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2786 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2787 234035bc 2019-06-01 stsp struct stat sb;
2788 234035bc 2019-06-01 stsp unsigned char status;
2789 234035bc 2019-06-01 stsp int local_changes_subsumed;
2790 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2791 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2792 234035bc 2019-06-01 stsp
2793 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2794 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2795 d6c87207 2019-08-02 stsp strlen(path2));
2796 1ee397ad 2019-07-12 stsp if (ie == NULL)
2797 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2798 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2799 234035bc 2019-06-01 stsp
2800 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2801 234035bc 2019-06-01 stsp path2) == -1)
2802 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2803 234035bc 2019-06-01 stsp
2804 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2805 7f91a133 2019-12-13 stsp repo);
2806 234035bc 2019-06-01 stsp if (err)
2807 234035bc 2019-06-01 stsp goto done;
2808 234035bc 2019-06-01 stsp
2809 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2810 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2811 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2812 234035bc 2019-06-01 stsp goto done;
2813 234035bc 2019-06-01 stsp }
2814 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2815 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2816 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2817 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2818 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2819 234035bc 2019-06-01 stsp goto done;
2820 234035bc 2019-06-01 stsp }
2821 234035bc 2019-06-01 stsp
2822 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2823 36bf999c 2020-07-23 stsp char *link_target2;
2824 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2825 36bf999c 2020-07-23 stsp if (err)
2826 36bf999c 2020-07-23 stsp goto done;
2827 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2828 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2829 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2830 36bf999c 2020-07-23 stsp free(link_target2);
2831 af57b12a 2020-07-23 stsp } else {
2832 1af628f4 2021-06-03 stsp int fd;
2833 1af628f4 2021-06-03 stsp
2834 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2835 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2836 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2837 1af628f4 2021-06-03 stsp goto done;
2838 1af628f4 2021-06-03 stsp }
2839 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2840 1af628f4 2021-06-03 stsp f_orig, blob1);
2841 1af628f4 2021-06-03 stsp if (err)
2842 1af628f4 2021-06-03 stsp goto done;
2843 1af628f4 2021-06-03 stsp
2844 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2845 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2846 1af628f4 2021-06-03 stsp goto done;
2847 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2848 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2849 1af628f4 2021-06-03 stsp if (err)
2850 1af628f4 2021-06-03 stsp goto done;
2851 1af628f4 2021-06-03 stsp
2852 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2853 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2854 1af628f4 2021-06-03 stsp if (fd == -1) {
2855 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2856 1af628f4 2021-06-03 stsp ondisk_path);
2857 1af628f4 2021-06-03 stsp goto done;
2858 1af628f4 2021-06-03 stsp }
2859 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2860 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2861 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2862 1af628f4 2021-06-03 stsp ondisk_path);
2863 1af628f4 2021-06-03 stsp close(fd);
2864 1af628f4 2021-06-03 stsp goto done;
2865 1af628f4 2021-06-03 stsp }
2866 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2867 1af628f4 2021-06-03 stsp if (err)
2868 1af628f4 2021-06-03 stsp goto done;
2869 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2870 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2871 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2872 1af628f4 2021-06-03 stsp goto done;
2873 1af628f4 2021-06-03 stsp }
2874 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2875 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2876 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2877 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2878 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2879 af57b12a 2020-07-23 stsp }
2880 234035bc 2019-06-01 stsp } else if (blob1) {
2881 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2882 d6c87207 2019-08-02 stsp strlen(path1));
2883 1ee397ad 2019-07-12 stsp if (ie == NULL)
2884 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2885 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2886 2b92fad7 2019-06-02 stsp
2887 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2888 2b92fad7 2019-06-02 stsp path1) == -1)
2889 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2890 2b92fad7 2019-06-02 stsp
2891 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2892 7f91a133 2019-12-13 stsp repo);
2893 2b92fad7 2019-06-02 stsp if (err)
2894 2b92fad7 2019-06-02 stsp goto done;
2895 2b92fad7 2019-06-02 stsp
2896 2b92fad7 2019-06-02 stsp switch (status) {
2897 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2898 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2899 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2900 1ee397ad 2019-07-12 stsp if (err)
2901 1ee397ad 2019-07-12 stsp goto done;
2902 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2903 2b92fad7 2019-06-02 stsp if (err)
2904 2b92fad7 2019-06-02 stsp goto done;
2905 2b92fad7 2019-06-02 stsp if (ie)
2906 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2907 2b92fad7 2019-06-02 stsp break;
2908 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2909 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2910 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2911 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2912 1ee397ad 2019-07-12 stsp if (err)
2913 1ee397ad 2019-07-12 stsp goto done;
2914 2b92fad7 2019-06-02 stsp if (ie)
2915 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2916 2b92fad7 2019-06-02 stsp break;
2917 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2918 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2919 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2920 e8f02263 2022-01-23 thomas off_t blob1_size;
2921 0a22ca1a 2020-09-23 stsp /*
2922 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2923 0a22ca1a 2020-09-23 stsp * exists in the repository.
2924 0a22ca1a 2020-09-23 stsp */
2925 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
2926 e8f02263 2022-01-23 thomas &blob1_size, path1);
2927 0a22ca1a 2020-09-23 stsp if (err)
2928 0a22ca1a 2020-09-23 stsp goto done;
2929 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2930 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2931 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2932 0a22ca1a 2020-09-23 stsp if (err)
2933 0a22ca1a 2020-09-23 stsp goto done;
2934 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2935 0a22ca1a 2020-09-23 stsp path1);
2936 0a22ca1a 2020-09-23 stsp if (err)
2937 0a22ca1a 2020-09-23 stsp goto done;
2938 0a22ca1a 2020-09-23 stsp if (ie)
2939 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2940 0a22ca1a 2020-09-23 stsp ie);
2941 0a22ca1a 2020-09-23 stsp } else {
2942 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2943 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2944 0a22ca1a 2020-09-23 stsp }
2945 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2946 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2947 0a22ca1a 2020-09-23 stsp free(id);
2948 0a22ca1a 2020-09-23 stsp if (err)
2949 0a22ca1a 2020-09-23 stsp goto done;
2950 0a22ca1a 2020-09-23 stsp break;
2951 0a22ca1a 2020-09-23 stsp }
2952 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2953 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2954 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2955 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2956 1ee397ad 2019-07-12 stsp if (err)
2957 1ee397ad 2019-07-12 stsp goto done;
2958 2b92fad7 2019-06-02 stsp break;
2959 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2960 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2961 1ee397ad 2019-07-12 stsp if (err)
2962 1ee397ad 2019-07-12 stsp goto done;
2963 2b92fad7 2019-06-02 stsp break;
2964 2b92fad7 2019-06-02 stsp default:
2965 2b92fad7 2019-06-02 stsp break;
2966 2b92fad7 2019-06-02 stsp }
2967 234035bc 2019-06-01 stsp } else if (blob2) {
2968 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2969 234035bc 2019-06-01 stsp path2) == -1)
2970 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2971 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2972 d6c87207 2019-08-02 stsp strlen(path2));
2973 234035bc 2019-06-01 stsp if (ie) {
2974 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2975 7f91a133 2019-12-13 stsp -1, NULL, repo);
2976 234035bc 2019-06-01 stsp if (err)
2977 234035bc 2019-06-01 stsp goto done;
2978 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2979 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2980 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2981 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2982 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2983 1ee397ad 2019-07-12 stsp status, path2);
2984 234035bc 2019-06-01 stsp goto done;
2985 234035bc 2019-06-01 stsp }
2986 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2987 36bf999c 2020-07-23 stsp char *link_target2;
2988 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2989 36bf999c 2020-07-23 stsp blob2);
2990 36bf999c 2020-07-23 stsp if (err)
2991 36bf999c 2020-07-23 stsp goto done;
2992 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2993 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2994 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2995 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2996 36bf999c 2020-07-23 stsp free(link_target2);
2997 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2998 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2999 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3000 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3001 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3002 526a746f 2020-07-23 stsp a->progress_arg);
3003 dfe9fba0 2020-07-23 stsp } else {
3004 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3005 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3006 526a746f 2020-07-23 stsp }
3007 dfe9fba0 2020-07-23 stsp if (err)
3008 dfe9fba0 2020-07-23 stsp goto done;
3009 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3010 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3011 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3012 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3013 234035bc 2019-06-01 stsp if (err)
3014 234035bc 2019-06-01 stsp goto done;
3015 234035bc 2019-06-01 stsp }
3016 234035bc 2019-06-01 stsp } else {
3017 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3018 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3019 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3020 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3021 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3022 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3023 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3024 2e1fa222 2020-07-23 stsp } else {
3025 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3026 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3027 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3028 2e1fa222 2020-07-23 stsp }
3029 234035bc 2019-06-01 stsp if (err)
3030 234035bc 2019-06-01 stsp goto done;
3031 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3032 234035bc 2019-06-01 stsp if (err)
3033 234035bc 2019-06-01 stsp goto done;
3034 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3035 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3036 3969253a 2020-03-07 stsp if (err) {
3037 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3038 3969253a 2020-03-07 stsp goto done;
3039 3969253a 2020-03-07 stsp }
3040 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3041 2b92fad7 2019-06-02 stsp if (err) {
3042 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3043 2b92fad7 2019-06-02 stsp goto done;
3044 2b92fad7 2019-06-02 stsp }
3045 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3046 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3047 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3048 2e1fa222 2020-07-23 stsp }
3049 234035bc 2019-06-01 stsp }
3050 234035bc 2019-06-01 stsp }
3051 234035bc 2019-06-01 stsp done:
3052 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3053 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3054 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3055 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3056 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3057 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3058 1af628f4 2021-06-03 stsp free(id_str);
3059 54d5be07 2021-06-03 stsp free(label_deriv2);
3060 234035bc 2019-06-01 stsp free(ondisk_path);
3061 234035bc 2019-06-01 stsp return err;
3062 234035bc 2019-06-01 stsp }
3063 234035bc 2019-06-01 stsp
3064 69de9dd4 2021-09-03 stsp static const struct got_error *
3065 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3066 69de9dd4 2021-09-03 stsp {
3067 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3068 69de9dd4 2021-09-03 stsp
3069 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3070 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3071 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3072 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3073 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3074 69de9dd4 2021-09-03 stsp
3075 69de9dd4 2021-09-03 stsp return NULL;
3076 69de9dd4 2021-09-03 stsp }
3077 69de9dd4 2021-09-03 stsp
3078 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3079 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3080 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3081 234035bc 2019-06-01 stsp struct got_repository *repo;
3082 234035bc 2019-06-01 stsp };
3083 234035bc 2019-06-01 stsp
3084 234035bc 2019-06-01 stsp static const struct got_error *
3085 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3086 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3087 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3088 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3089 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3090 234035bc 2019-06-01 stsp {
3091 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3092 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3093 234035bc 2019-06-01 stsp unsigned char status;
3094 234035bc 2019-06-01 stsp struct stat sb;
3095 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3096 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3097 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3098 234035bc 2019-06-01 stsp char *ondisk_path;
3099 234035bc 2019-06-01 stsp
3100 69de9dd4 2021-09-03 stsp if (id == NULL)
3101 69de9dd4 2021-09-03 stsp return NULL;
3102 234035bc 2019-06-01 stsp
3103 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3104 69de9dd4 2021-09-03 stsp if (ie == NULL)
3105 69de9dd4 2021-09-03 stsp return NULL;
3106 69de9dd4 2021-09-03 stsp
3107 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3108 234035bc 2019-06-01 stsp == -1)
3109 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3110 234035bc 2019-06-01 stsp
3111 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3112 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3113 5546d466 2021-09-02 stsp free(ondisk_path);
3114 234035bc 2019-06-01 stsp if (err)
3115 234035bc 2019-06-01 stsp return err;
3116 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3117 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3118 234035bc 2019-06-01 stsp
3119 234035bc 2019-06-01 stsp return NULL;
3120 234035bc 2019-06-01 stsp }
3121 234035bc 2019-06-01 stsp
3122 818c7501 2019-07-11 stsp static const struct got_error *
3123 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3124 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3125 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3126 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3127 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3128 234035bc 2019-06-01 stsp {
3129 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3130 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3131 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3132 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3133 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3134 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3135 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3136 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3137 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3138 234035bc 2019-06-01 stsp
3139 03415a1a 2019-06-02 stsp if (commit_id1) {
3140 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3141 945f9229 2022-04-16 thomas if (err)
3142 945f9229 2022-04-16 thomas goto done;
3143 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3144 03415a1a 2019-06-02 stsp worktree->path_prefix);
3145 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3146 03415a1a 2019-06-02 stsp goto done;
3147 69d57f3d 2020-07-31 stsp }
3148 69d57f3d 2020-07-31 stsp if (tree_id1) {
3149 69d57f3d 2020-07-31 stsp char *id_str;
3150 03415a1a 2019-06-02 stsp
3151 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3152 03415a1a 2019-06-02 stsp if (err)
3153 03415a1a 2019-06-02 stsp goto done;
3154 f69721c3 2019-10-21 stsp
3155 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3156 f69721c3 2019-10-21 stsp if (err)
3157 f69721c3 2019-10-21 stsp goto done;
3158 f69721c3 2019-10-21 stsp
3159 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3160 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3161 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3162 f69721c3 2019-10-21 stsp free(id_str);
3163 f69721c3 2019-10-21 stsp goto done;
3164 f69721c3 2019-10-21 stsp }
3165 f69721c3 2019-10-21 stsp free(id_str);
3166 a0f32f33 2022-06-13 thomas
3167 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3168 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3169 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3170 a0f32f33 2022-06-13 thomas goto done;
3171 a0f32f33 2022-06-13 thomas }
3172 03415a1a 2019-06-02 stsp }
3173 234035bc 2019-06-01 stsp
3174 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3175 945f9229 2022-04-16 thomas if (err)
3176 945f9229 2022-04-16 thomas goto done;
3177 945f9229 2022-04-16 thomas
3178 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3179 234035bc 2019-06-01 stsp worktree->path_prefix);
3180 234035bc 2019-06-01 stsp if (err)
3181 234035bc 2019-06-01 stsp goto done;
3182 234035bc 2019-06-01 stsp
3183 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3184 234035bc 2019-06-01 stsp if (err)
3185 234035bc 2019-06-01 stsp goto done;
3186 234035bc 2019-06-01 stsp
3187 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3188 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3189 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3190 19a6a6b5 2022-07-01 thomas goto done;
3191 19a6a6b5 2022-07-01 thomas }
3192 19a6a6b5 2022-07-01 thomas
3193 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3194 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3195 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3196 a0f32f33 2022-06-13 thomas goto done;
3197 a0f32f33 2022-06-13 thomas }
3198 a0f32f33 2022-06-13 thomas
3199 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3200 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3201 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3202 19a6a6b5 2022-07-01 thomas goto done;
3203 19a6a6b5 2022-07-01 thomas }
3204 19a6a6b5 2022-07-01 thomas
3205 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3206 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3207 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3208 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3209 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3210 69de9dd4 2021-09-03 stsp if (err)
3211 69de9dd4 2021-09-03 stsp goto done;
3212 69de9dd4 2021-09-03 stsp
3213 234035bc 2019-06-01 stsp arg.worktree = worktree;
3214 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3215 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3216 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3217 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3218 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3219 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3220 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3221 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3222 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3223 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3224 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3225 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3226 af12c6b9 2019-06-04 stsp err = sync_err;
3227 234035bc 2019-06-01 stsp done:
3228 945f9229 2022-04-16 thomas if (commit1)
3229 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3230 945f9229 2022-04-16 thomas if (commit2)
3231 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3232 234035bc 2019-06-01 stsp if (tree1)
3233 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3234 234035bc 2019-06-01 stsp if (tree2)
3235 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3236 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3237 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3238 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3239 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3240 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3241 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3242 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3243 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3244 f69721c3 2019-10-21 stsp free(label_orig);
3245 818c7501 2019-07-11 stsp return err;
3246 818c7501 2019-07-11 stsp }
3247 234035bc 2019-06-01 stsp
3248 818c7501 2019-07-11 stsp const struct got_error *
3249 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3250 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3251 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3252 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3253 818c7501 2019-07-11 stsp {
3254 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3255 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3256 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3257 818c7501 2019-07-11 stsp
3258 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3259 818c7501 2019-07-11 stsp if (err)
3260 818c7501 2019-07-11 stsp return err;
3261 818c7501 2019-07-11 stsp
3262 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3263 818c7501 2019-07-11 stsp if (err)
3264 818c7501 2019-07-11 stsp goto done;
3265 818c7501 2019-07-11 stsp
3266 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3267 69de9dd4 2021-09-03 stsp worktree);
3268 818c7501 2019-07-11 stsp if (err)
3269 818c7501 2019-07-11 stsp goto done;
3270 818c7501 2019-07-11 stsp
3271 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3272 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3273 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3274 818c7501 2019-07-11 stsp done:
3275 818c7501 2019-07-11 stsp if (fileindex)
3276 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3277 818c7501 2019-07-11 stsp free(fileindex_path);
3278 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3279 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3280 234035bc 2019-06-01 stsp err = unlockerr;
3281 234035bc 2019-06-01 stsp return err;
3282 234035bc 2019-06-01 stsp }
3283 234035bc 2019-06-01 stsp
3284 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3285 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3286 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3287 927df6b7 2019-02-10 stsp const char *status_path;
3288 927df6b7 2019-02-10 stsp size_t status_path_len;
3289 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3290 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3291 f8d1f275 2019-02-04 stsp void *status_arg;
3292 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3293 f8d1f275 2019-02-04 stsp void *cancel_arg;
3294 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3295 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3296 f2a9dc41 2019-12-13 tracey int report_unchanged;
3297 3143d852 2020-06-25 stsp int no_ignores;
3298 f8d1f275 2019-02-04 stsp };
3299 88d0e355 2019-08-03 stsp
3300 f8d1f275 2019-02-04 stsp static const struct got_error *
3301 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3302 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3303 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3304 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3305 927df6b7 2019-02-10 stsp {
3306 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3307 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3308 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3309 927df6b7 2019-02-10 stsp struct stat sb;
3310 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3311 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3312 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3313 927df6b7 2019-02-10 stsp
3314 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3315 98eaaa12 2019-08-03 stsp if (err)
3316 98eaaa12 2019-08-03 stsp return err;
3317 98eaaa12 2019-08-03 stsp
3318 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3319 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3320 98eaaa12 2019-08-03 stsp return NULL;
3321 98eaaa12 2019-08-03 stsp
3322 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3323 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3324 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3325 98eaaa12 2019-08-03 stsp }
3326 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3327 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3328 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3329 927df6b7 2019-02-10 stsp }
3330 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3331 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3332 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3333 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3334 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3335 98eaaa12 2019-08-03 stsp }
3336 98eaaa12 2019-08-03 stsp
3337 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3338 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3339 927df6b7 2019-02-10 stsp }
3340 927df6b7 2019-02-10 stsp
3341 927df6b7 2019-02-10 stsp static const struct got_error *
3342 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3343 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3344 f8d1f275 2019-02-04 stsp {
3345 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3346 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3347 f8d1f275 2019-02-04 stsp char *abspath;
3348 f8d1f275 2019-02-04 stsp
3349 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3350 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3351 0584f854 2019-04-06 stsp
3352 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3353 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3354 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3355 927df6b7 2019-02-10 stsp return NULL;
3356 927df6b7 2019-02-10 stsp
3357 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3358 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3359 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3360 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3361 f8d1f275 2019-02-04 stsp } else {
3362 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3363 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3364 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3365 f8d1f275 2019-02-04 stsp }
3366 f8d1f275 2019-02-04 stsp
3367 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3368 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3369 f8d1f275 2019-02-04 stsp free(abspath);
3370 9d31a1d8 2018-03-11 stsp return err;
3371 9d31a1d8 2018-03-11 stsp }
3372 f8d1f275 2019-02-04 stsp
3373 f8d1f275 2019-02-04 stsp static const struct got_error *
3374 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3375 f8d1f275 2019-02-04 stsp {
3376 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3377 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3378 2ec1f75b 2019-03-26 stsp unsigned char status;
3379 927df6b7 2019-02-10 stsp
3380 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3381 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3382 0584f854 2019-04-06 stsp
3383 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3384 927df6b7 2019-02-10 stsp return NULL;
3385 927df6b7 2019-02-10 stsp
3386 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3387 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3388 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3389 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3390 2ec1f75b 2019-03-26 stsp else
3391 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3392 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3393 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3394 6841da00 2019-08-08 stsp }
3395 6841da00 2019-08-08 stsp
3396 ef20f542 2022-06-26 thomas static void
3397 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3398 6841da00 2019-08-08 stsp {
3399 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3400 6841da00 2019-08-08 stsp
3401 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3402 6841da00 2019-08-08 stsp free((char *)pe->path);
3403 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3404 6841da00 2019-08-08 stsp }
3405 6841da00 2019-08-08 stsp
3406 ef20f542 2022-06-26 thomas static void
3407 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3408 6841da00 2019-08-08 stsp {
3409 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3410 6841da00 2019-08-08 stsp
3411 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3412 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3413 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3414 6841da00 2019-08-08 stsp free((char *)pe->path);
3415 6841da00 2019-08-08 stsp }
3416 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3417 6841da00 2019-08-08 stsp }
3418 6841da00 2019-08-08 stsp
3419 6841da00 2019-08-08 stsp static const struct got_error *
3420 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3421 6841da00 2019-08-08 stsp {
3422 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3423 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3424 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3425 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3426 6841da00 2019-08-08 stsp size_t linesize = 0;
3427 6841da00 2019-08-08 stsp ssize_t linelen;
3428 6841da00 2019-08-08 stsp
3429 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3430 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3431 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3432 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3433 6841da00 2019-08-08 stsp
3434 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3435 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3436 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3437 bd8de430 2019-10-04 stsp
3438 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3439 bd8de430 2019-10-04 stsp if (line[0] == '#')
3440 bd8de430 2019-10-04 stsp continue;
3441 bd8de430 2019-10-04 stsp
3442 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3443 bd8de430 2019-10-04 stsp if (line[0] == '!')
3444 bd8de430 2019-10-04 stsp continue;
3445 bd8de430 2019-10-04 stsp
3446 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3447 6841da00 2019-08-08 stsp line) == -1) {
3448 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3449 6841da00 2019-08-08 stsp goto done;
3450 6841da00 2019-08-08 stsp }
3451 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3452 6841da00 2019-08-08 stsp if (err)
3453 6841da00 2019-08-08 stsp goto done;
3454 6841da00 2019-08-08 stsp }
3455 6841da00 2019-08-08 stsp if (ferror(f)) {
3456 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3457 6841da00 2019-08-08 stsp goto done;
3458 6841da00 2019-08-08 stsp }
3459 6841da00 2019-08-08 stsp
3460 6841da00 2019-08-08 stsp dirpath = strdup(path);
3461 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3462 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3463 6841da00 2019-08-08 stsp goto done;
3464 6841da00 2019-08-08 stsp }
3465 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3466 6841da00 2019-08-08 stsp done:
3467 6841da00 2019-08-08 stsp free(line);
3468 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3469 6841da00 2019-08-08 stsp free(dirpath);
3470 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3471 6841da00 2019-08-08 stsp }
3472 6841da00 2019-08-08 stsp return err;
3473 6841da00 2019-08-08 stsp }
3474 6841da00 2019-08-08 stsp
3475 ef20f542 2022-06-26 thomas static int
3476 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3477 6841da00 2019-08-08 stsp {
3478 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3479 bd8de430 2019-10-04 stsp
3480 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3481 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3482 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3483 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3484 bd8de430 2019-10-04 stsp
3485 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3486 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3487 6841da00 2019-08-08 stsp
3488 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3489 bd8de430 2019-10-04 stsp continue;
3490 bd8de430 2019-10-04 stsp pattern += 3;
3491 bd8de430 2019-10-04 stsp p = path;
3492 bd8de430 2019-10-04 stsp while (*p) {
3493 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3494 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3495 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3496 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3497 bd8de430 2019-10-04 stsp p++;
3498 bd8de430 2019-10-04 stsp while (*p == '/')
3499 bd8de430 2019-10-04 stsp p++;
3500 bd8de430 2019-10-04 stsp continue;
3501 bd8de430 2019-10-04 stsp }
3502 bd8de430 2019-10-04 stsp return 1;
3503 bd8de430 2019-10-04 stsp }
3504 bd8de430 2019-10-04 stsp }
3505 bd8de430 2019-10-04 stsp }
3506 bd8de430 2019-10-04 stsp
3507 6841da00 2019-08-08 stsp /*
3508 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3509 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3510 6841da00 2019-08-08 stsp * ignores backwards.
3511 6841da00 2019-08-08 stsp */
3512 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3513 6841da00 2019-08-08 stsp while (pe) {
3514 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3515 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3516 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3517 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3518 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3519 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3520 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3521 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3522 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3523 6841da00 2019-08-08 stsp continue;
3524 6841da00 2019-08-08 stsp return 1;
3525 6841da00 2019-08-08 stsp }
3526 6841da00 2019-08-08 stsp }
3527 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3528 6841da00 2019-08-08 stsp }
3529 6841da00 2019-08-08 stsp
3530 6841da00 2019-08-08 stsp return 0;
3531 6841da00 2019-08-08 stsp }
3532 6841da00 2019-08-08 stsp
3533 6841da00 2019-08-08 stsp static const struct got_error *
3534 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3535 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3536 6841da00 2019-08-08 stsp {
3537 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3538 6841da00 2019-08-08 stsp char *ignorespath;
3539 886cec17 2019-12-15 stsp int fd = -1;
3540 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3541 6841da00 2019-08-08 stsp
3542 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3543 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3544 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3545 6841da00 2019-08-08 stsp
3546 886cec17 2019-12-15 stsp if (dirfd != -1) {
3547 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3548 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3549 886cec17 2019-12-15 stsp if (fd == -1) {
3550 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3551 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3552 886cec17 2019-12-15 stsp ignorespath);
3553 886cec17 2019-12-15 stsp } else {
3554 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3555 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3556 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3557 886cec17 2019-12-15 stsp ignorespath);
3558 886cec17 2019-12-15 stsp else {
3559 886cec17 2019-12-15 stsp fd = -1;
3560 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3561 886cec17 2019-12-15 stsp }
3562 886cec17 2019-12-15 stsp }
3563 886cec17 2019-12-15 stsp } else {
3564 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3565 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3566 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3567 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3568 886cec17 2019-12-15 stsp ignorespath);
3569 886cec17 2019-12-15 stsp } else
3570 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3571 886cec17 2019-12-15 stsp }
3572 6841da00 2019-08-08 stsp
3573 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3574 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3575 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3576 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3577 6841da00 2019-08-08 stsp free(ignorespath);
3578 6841da00 2019-08-08 stsp return err;
3579 f8d1f275 2019-02-04 stsp }
3580 f8d1f275 2019-02-04 stsp
3581 f8d1f275 2019-02-04 stsp static const struct got_error *
3582 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3583 6092c299 2021-10-04 thomas int dirfd)
3584 f8d1f275 2019-02-04 stsp {
3585 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3586 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3587 f8d1f275 2019-02-04 stsp char *path = NULL;
3588 6092c299 2021-10-04 thomas
3589 6092c299 2021-10-04 thomas if (ignore != NULL)
3590 6092c299 2021-10-04 thomas *ignore = 0;
3591 f8d1f275 2019-02-04 stsp
3592 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3593 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3594 0584f854 2019-04-06 stsp
3595 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3596 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3597 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3598 f8d1f275 2019-02-04 stsp } else {
3599 f8d1f275 2019-02-04 stsp path = de->d_name;
3600 f8d1f275 2019-02-04 stsp }
3601 f8d1f275 2019-02-04 stsp
3602 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3603 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3604 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3605 6092c299 2021-10-04 thomas *ignore = 1;
3606 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3607 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3608 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3609 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3610 f8d1f275 2019-02-04 stsp if (parent_path[0])
3611 f8d1f275 2019-02-04 stsp free(path);
3612 b72f483a 2019-02-05 stsp return err;
3613 f8d1f275 2019-02-04 stsp }
3614 f8d1f275 2019-02-04 stsp
3615 347d1d3e 2019-07-12 stsp static const struct got_error *
3616 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3617 3143d852 2020-06-25 stsp {
3618 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3619 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3620 3143d852 2020-06-25 stsp
3621 3143d852 2020-06-25 stsp if (a->no_ignores)
3622 3143d852 2020-06-25 stsp return NULL;
3623 3143d852 2020-06-25 stsp
3624 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3625 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3626 3143d852 2020-06-25 stsp if (err)
3627 3143d852 2020-06-25 stsp return err;
3628 3143d852 2020-06-25 stsp
3629 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3630 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3631 3143d852 2020-06-25 stsp
3632 3143d852 2020-06-25 stsp return err;
3633 3143d852 2020-06-25 stsp }
3634 3143d852 2020-06-25 stsp
3635 3143d852 2020-06-25 stsp static const struct got_error *
3636 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3637 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3638 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3639 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3640 abb4604f 2019-07-27 stsp {
3641 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3642 abb4604f 2019-07-27 stsp struct stat sb;
3643 ff56836b 2021-07-08 stsp
3644 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3645 abb4604f 2019-07-27 stsp if (ie)
3646 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3647 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3648 abb4604f 2019-07-27 stsp
3649 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3650 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3651 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3652 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3653 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3654 abb4604f 2019-07-27 stsp }
3655 abb4604f 2019-07-27 stsp
3656 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3657 fe1d8685 2022-02-12 thomas return NULL;
3658 fe1d8685 2022-02-12 thomas
3659 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3660 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3661 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3662 abb4604f 2019-07-27 stsp
3663 abb4604f 2019-07-27 stsp return NULL;
3664 3143d852 2020-06-25 stsp }
3665 3143d852 2020-06-25 stsp
3666 3143d852 2020-06-25 stsp static const struct got_error *
3667 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3668 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3669 3143d852 2020-06-25 stsp {
3670 3143d852 2020-06-25 stsp const struct got_error *err;
3671 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3672 3143d852 2020-06-25 stsp
3673 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3674 3143d852 2020-06-25 stsp ".cvsignore");
3675 3143d852 2020-06-25 stsp if (err)
3676 3143d852 2020-06-25 stsp return err;
3677 3143d852 2020-06-25 stsp
3678 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3679 3143d852 2020-06-25 stsp ".gitignore");
3680 3143d852 2020-06-25 stsp if (err)
3681 3143d852 2020-06-25 stsp return err;
3682 3143d852 2020-06-25 stsp
3683 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3684 3143d852 2020-06-25 stsp if (err) {
3685 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3686 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3687 3143d852 2020-06-25 stsp return err;
3688 3143d852 2020-06-25 stsp }
3689 3143d852 2020-06-25 stsp for (;;) {
3690 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3691 3143d852 2020-06-25 stsp ".cvsignore");
3692 3143d852 2020-06-25 stsp if (err)
3693 3143d852 2020-06-25 stsp break;
3694 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3695 3143d852 2020-06-25 stsp ".gitignore");
3696 3143d852 2020-06-25 stsp if (err)
3697 3143d852 2020-06-25 stsp break;
3698 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3699 3143d852 2020-06-25 stsp if (err) {
3700 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3701 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3702 3143d852 2020-06-25 stsp break;
3703 3143d852 2020-06-25 stsp }
3704 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3705 ff56836b 2021-07-08 stsp break;
3706 b737c85a 2020-06-26 stsp free(parent_path);
3707 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3708 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3709 3143d852 2020-06-25 stsp }
3710 3143d852 2020-06-25 stsp
3711 b737c85a 2020-06-26 stsp free(parent_path);
3712 b737c85a 2020-06-26 stsp free(next_parent_path);
3713 3143d852 2020-06-25 stsp return err;
3714 abb4604f 2019-07-27 stsp }
3715 abb4604f 2019-07-27 stsp
3716 abb4604f 2019-07-27 stsp static const struct got_error *
3717 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3718 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3719 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3720 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3721 f2a9dc41 2019-12-13 tracey int report_unchanged)
3722 f8d1f275 2019-02-04 stsp {
3723 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3724 6fc93f37 2019-12-13 stsp int fd = -1;
3725 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3726 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3727 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3728 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3729 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3730 3143d852 2020-06-25 stsp
3731 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3732 f8d1f275 2019-02-04 stsp
3733 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3734 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3735 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3736 8dc303cc 2019-07-27 stsp
3737 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3738 a78810f8 2022-03-13 thomas if (ie) {
3739 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3740 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3741 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3742 a78810f8 2022-03-13 thomas goto done;
3743 a78810f8 2022-03-13 thomas }
3744 a78810f8 2022-03-13 thomas
3745 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3746 6fc93f37 2019-12-13 stsp if (fd == -1) {
3747 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3748 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3749 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3750 ff56836b 2021-07-08 stsp else {
3751 ff56836b 2021-07-08 stsp if (!no_ignores) {
3752 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3753 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3754 ff56836b 2021-07-08 stsp if (err)
3755 ff56836b 2021-07-08 stsp goto done;
3756 ff56836b 2021-07-08 stsp }
3757 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3758 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3759 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3760 ff56836b 2021-07-08 stsp }
3761 abb4604f 2019-07-27 stsp } else {
3762 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3763 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3764 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3765 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3766 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3767 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3768 abb4604f 2019-07-27 stsp arg.status_path = path;
3769 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3770 abb4604f 2019-07-27 stsp arg.repo = repo;
3771 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3772 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3773 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3774 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3775 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3776 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3777 022fae89 2019-12-06 tracey if (!no_ignores) {
3778 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3779 3143d852 2020-06-25 stsp worktree->root_path, path);
3780 3143d852 2020-06-25 stsp if (err)
3781 3143d852 2020-06-25 stsp goto done;
3782 022fae89 2019-12-06 tracey }
3783 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3784 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3785 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3786 927df6b7 2019-02-10 stsp }
3787 3143d852 2020-06-25 stsp done:
3788 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3789 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3790 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3791 927df6b7 2019-02-10 stsp free(ondisk_path);
3792 347d1d3e 2019-07-12 stsp return err;
3793 347d1d3e 2019-07-12 stsp }
3794 347d1d3e 2019-07-12 stsp
3795 347d1d3e 2019-07-12 stsp const struct got_error *
3796 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3797 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3798 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3799 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3800 347d1d3e 2019-07-12 stsp {
3801 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3802 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3803 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3804 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3805 347d1d3e 2019-07-12 stsp
3806 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3807 347d1d3e 2019-07-12 stsp if (err)
3808 347d1d3e 2019-07-12 stsp return err;
3809 347d1d3e 2019-07-12 stsp
3810 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3811 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3812 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3813 f6343036 2021-06-22 stsp no_ignores, 0);
3814 72ea6654 2019-07-27 stsp if (err)
3815 72ea6654 2019-07-27 stsp break;
3816 72ea6654 2019-07-27 stsp }
3817 f8d1f275 2019-02-04 stsp free(fileindex_path);
3818 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3819 f8d1f275 2019-02-04 stsp return err;
3820 f8d1f275 2019-02-04 stsp }
3821 6c7ab921 2019-03-18 stsp
3822 6c7ab921 2019-03-18 stsp const struct got_error *
3823 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3824 6c7ab921 2019-03-18 stsp const char *arg)
3825 6c7ab921 2019-03-18 stsp {
3826 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3827 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3828 6c7ab921 2019-03-18 stsp size_t len;
3829 00bb5ea0 2020-07-23 stsp struct stat sb;
3830 01740607 2020-11-04 stsp char *abspath = NULL;
3831 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3832 6c7ab921 2019-03-18 stsp
3833 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3834 6c7ab921 2019-03-18 stsp
3835 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3836 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3837 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3838 00bb5ea0 2020-07-23 stsp
3839 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3840 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3841 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3842 00bb5ea0 2020-07-23 stsp goto done;
3843 00bb5ea0 2020-07-23 stsp }
3844 727173c3 2020-11-06 stsp sb.st_mode = 0;
3845 00bb5ea0 2020-07-23 stsp }
3846 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3847 00bb5ea0 2020-07-23 stsp /*
3848 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3849 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3850 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3851 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3852 00bb5ea0 2020-07-23 stsp */
3853 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3854 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3855 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3856 00bb5ea0 2020-07-23 stsp goto done;
3857 00bb5ea0 2020-07-23 stsp }
3858 00bb5ea0 2020-07-23 stsp
3859 00bb5ea0 2020-07-23 stsp }
3860 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3861 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3862 00bb5ea0 2020-07-23 stsp if (err)
3863 d0710d08 2019-07-22 stsp goto done;
3864 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3865 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3866 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3867 00bb5ea0 2020-07-23 stsp goto done;
3868 00bb5ea0 2020-07-23 stsp }
3869 00bb5ea0 2020-07-23 stsp } else {
3870 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3871 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3872 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3873 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3874 00bb5ea0 2020-07-23 stsp goto done;
3875 00bb5ea0 2020-07-23 stsp }
3876 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3877 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3878 01740607 2020-11-04 stsp goto done;
3879 01740607 2020-11-04 stsp }
3880 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3881 01740607 2020-11-04 stsp sizeof(canonpath));
3882 01740607 2020-11-04 stsp if (err)
3883 00bb5ea0 2020-07-23 stsp goto done;
3884 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3885 01740607 2020-11-04 stsp if (resolved == NULL) {
3886 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3887 01740607 2020-11-04 stsp goto done;
3888 00bb5ea0 2020-07-23 stsp }
3889 d0710d08 2019-07-22 stsp }
3890 d0710d08 2019-07-22 stsp }
3891 6c7ab921 2019-03-18 stsp
3892 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3893 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3894 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3895 6c7ab921 2019-03-18 stsp goto done;
3896 6c7ab921 2019-03-18 stsp }
3897 6c7ab921 2019-03-18 stsp
3898 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3899 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3900 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3901 f6d88e1a 2019-05-29 stsp if (err)
3902 f6d88e1a 2019-05-29 stsp goto done;
3903 f6d88e1a 2019-05-29 stsp } else {
3904 f6d88e1a 2019-05-29 stsp path = strdup("");
3905 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3906 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3907 f6d88e1a 2019-05-29 stsp goto done;
3908 f6d88e1a 2019-05-29 stsp }
3909 6c7ab921 2019-03-18 stsp }
3910 6c7ab921 2019-03-18 stsp
3911 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3912 6c7ab921 2019-03-18 stsp len = strlen(path);
3913 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3914 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3915 6c7ab921 2019-03-18 stsp len--;
3916 6c7ab921 2019-03-18 stsp }
3917 6c7ab921 2019-03-18 stsp done:
3918 01740607 2020-11-04 stsp free(abspath);
3919 6c7ab921 2019-03-18 stsp free(resolved);
3920 d0710d08 2019-07-22 stsp free(cwd);
3921 6c7ab921 2019-03-18 stsp if (err == NULL)
3922 6c7ab921 2019-03-18 stsp *wt_path = path;
3923 6c7ab921 2019-03-18 stsp else
3924 6c7ab921 2019-03-18 stsp free(path);
3925 d00136be 2019-03-26 stsp return err;
3926 d00136be 2019-03-26 stsp }
3927 d00136be 2019-03-26 stsp
3928 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3929 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3930 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3931 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3932 4e68cba3 2019-11-23 stsp void *progress_arg;
3933 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3934 4e68cba3 2019-11-23 stsp };
3935 4e68cba3 2019-11-23 stsp
3936 4e68cba3 2019-11-23 stsp static const struct got_error *
3937 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3938 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3939 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3940 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3941 07f5b47a 2019-06-02 stsp {
3942 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3943 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3944 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3945 6d022e97 2019-08-04 stsp struct stat sb;
3946 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3947 07f5b47a 2019-06-02 stsp
3948 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3949 dbb83fbd 2019-12-12 stsp relpath) == -1)
3950 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3951 dbb83fbd 2019-12-12 stsp
3952 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3953 6d022e97 2019-08-04 stsp if (ie) {
3954 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3955 12463d8b 2019-12-13 stsp de_name, a->repo);
3956 6d022e97 2019-08-04 stsp if (err)
3957 dbb83fbd 2019-12-12 stsp goto done;
3958 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3959 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3960 dbb83fbd 2019-12-12 stsp goto done;
3961 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3962 dbb83fbd 2019-12-12 stsp if (err)
3963 dbb83fbd 2019-12-12 stsp goto done;
3964 6d022e97 2019-08-04 stsp }
3965 07f5b47a 2019-06-02 stsp
3966 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3967 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
3968 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
3969 84bf00a6 2022-02-12 thomas else
3970 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3971 dbb83fbd 2019-12-12 stsp goto done;
3972 4e68cba3 2019-11-23 stsp }
3973 07f5b47a 2019-06-02 stsp
3974 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3975 4e68cba3 2019-11-23 stsp if (err)
3976 4e68cba3 2019-11-23 stsp goto done;
3977 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3978 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3979 3969253a 2020-03-07 stsp if (err) {
3980 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3981 3969253a 2020-03-07 stsp goto done;
3982 3969253a 2020-03-07 stsp }
3983 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3984 07f5b47a 2019-06-02 stsp if (err) {
3985 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3986 4e68cba3 2019-11-23 stsp goto done;
3987 07f5b47a 2019-06-02 stsp }
3988 4e68cba3 2019-11-23 stsp done:
3989 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3990 dbb83fbd 2019-12-12 stsp if (err)
3991 dbb83fbd 2019-12-12 stsp return err;
3992 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3993 dbb83fbd 2019-12-12 stsp return NULL;
3994 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3995 07f5b47a 2019-06-02 stsp }
3996 07f5b47a 2019-06-02 stsp
3997 d00136be 2019-03-26 stsp const struct got_error *
3998 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3999 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4000 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4001 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4002 d00136be 2019-03-26 stsp {
4003 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4004 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4005 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4006 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4007 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4008 d00136be 2019-03-26 stsp
4009 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4010 d00136be 2019-03-26 stsp if (err)
4011 d00136be 2019-03-26 stsp return err;
4012 d00136be 2019-03-26 stsp
4013 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4014 d00136be 2019-03-26 stsp if (err)
4015 d00136be 2019-03-26 stsp goto done;
4016 d00136be 2019-03-26 stsp
4017 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4018 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4019 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4020 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4021 4e68cba3 2019-11-23 stsp saa.repo = repo;
4022 4e68cba3 2019-11-23 stsp
4023 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4024 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4025 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4026 1dd54920 2019-05-11 stsp if (err)
4027 af12c6b9 2019-06-04 stsp break;
4028 1dd54920 2019-05-11 stsp }
4029 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4030 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4031 af12c6b9 2019-06-04 stsp err = sync_err;
4032 d00136be 2019-03-26 stsp done:
4033 fb399478 2019-07-12 stsp free(fileindex_path);
4034 d00136be 2019-03-26 stsp if (fileindex)
4035 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4036 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4037 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4038 d00136be 2019-03-26 stsp err = unlockerr;
4039 6c7ab921 2019-03-18 stsp return err;
4040 6c7ab921 2019-03-18 stsp }
4041 17ed4618 2019-06-02 stsp
4042 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4043 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4044 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4045 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4046 f2a9dc41 2019-12-13 tracey void *progress_arg;
4047 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4048 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4049 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4050 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4051 766841c2 2020-08-13 stsp const char *status_codes;
4052 f2a9dc41 2019-12-13 tracey };
4053 f2a9dc41 2019-12-13 tracey
4054 f2a9dc41 2019-12-13 tracey static const struct got_error *
4055 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4056 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4057 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4058 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4059 17ed4618 2019-06-02 stsp {
4060 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4061 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4062 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4063 17ed4618 2019-06-02 stsp struct stat sb;
4064 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4065 d64cc78a 2022-01-25 thomas
4066 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4067 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4068 d64cc78a 2022-01-25 thomas return NULL;
4069 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4070 d64cc78a 2022-01-25 thomas }
4071 17ed4618 2019-06-02 stsp
4072 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4073 17ed4618 2019-06-02 stsp if (ie == NULL)
4074 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4075 2ec1f75b 2019-03-26 stsp
4076 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4077 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4078 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4079 9acbc4fa 2019-08-03 stsp return NULL;
4080 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4081 9acbc4fa 2019-08-03 stsp }
4082 9acbc4fa 2019-08-03 stsp
4083 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4084 f2a9dc41 2019-12-13 tracey relpath) == -1)
4085 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4086 f2a9dc41 2019-12-13 tracey
4087 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4088 12463d8b 2019-12-13 stsp a->repo);
4089 17ed4618 2019-06-02 stsp if (err)
4090 f2a9dc41 2019-12-13 tracey goto done;
4091 17ed4618 2019-06-02 stsp
4092 766841c2 2020-08-13 stsp if (a->status_codes) {
4093 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4094 766841c2 2020-08-13 stsp int i;
4095 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4096 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4097 766841c2 2020-08-13 stsp break;
4098 766841c2 2020-08-13 stsp }
4099 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4100 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4101 766841c2 2020-08-13 stsp free(ondisk_path);
4102 766841c2 2020-08-13 stsp return NULL;
4103 766841c2 2020-08-13 stsp }
4104 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4105 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4106 766841c2 2020-08-13 stsp static char msg[64];
4107 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4108 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4109 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4110 766841c2 2020-08-13 stsp goto done;
4111 766841c2 2020-08-13 stsp }
4112 766841c2 2020-08-13 stsp }
4113 766841c2 2020-08-13 stsp
4114 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4115 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4116 f2a9dc41 2019-12-13 tracey goto done;
4117 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4118 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4119 f2a9dc41 2019-12-13 tracey goto done;
4120 f2a9dc41 2019-12-13 tracey }
4121 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4122 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4123 d64cc78a 2022-01-25 thomas goto done;
4124 d64cc78a 2022-01-25 thomas }
4125 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4126 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4127 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4128 f2a9dc41 2019-12-13 tracey goto done;
4129 f2a9dc41 2019-12-13 tracey }
4130 17ed4618 2019-06-02 stsp }
4131 17ed4618 2019-06-02 stsp
4132 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4133 20a2ad1c 2020-10-20 stsp size_t root_len;
4134 20a2ad1c 2020-10-20 stsp
4135 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4136 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4137 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4138 12463d8b 2019-12-13 stsp ondisk_path);
4139 12463d8b 2019-12-13 stsp goto done;
4140 12463d8b 2019-12-13 stsp }
4141 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4142 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4143 12463d8b 2019-12-13 stsp goto done;
4144 15341bfd 2020-03-05 tracey }
4145 15341bfd 2020-03-05 tracey
4146 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4147 20a2ad1c 2020-10-20 stsp do {
4148 20a2ad1c 2020-10-20 stsp char *parent;
4149 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4150 20a2ad1c 2020-10-20 stsp if (err)
4151 2513f20a 2020-10-20 stsp goto done;
4152 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4153 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4154 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4155 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4156 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4157 20a2ad1c 2020-10-20 stsp ondisk_path);
4158 15341bfd 2020-03-05 tracey break;
4159 15341bfd 2020-03-05 tracey }
4160 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4161 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4162 f2a9dc41 2019-12-13 tracey }
4163 17ed4618 2019-06-02 stsp
4164 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4165 f2a9dc41 2019-12-13 tracey done:
4166 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4167 f2a9dc41 2019-12-13 tracey if (err)
4168 f2a9dc41 2019-12-13 tracey return err;
4169 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4170 f2a9dc41 2019-12-13 tracey return NULL;
4171 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4172 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4173 17ed4618 2019-06-02 stsp }
4174 17ed4618 2019-06-02 stsp
4175 2ec1f75b 2019-03-26 stsp const struct got_error *
4176 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4177 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4178 766841c2 2020-08-13 stsp const char *status_codes,
4179 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4180 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4181 2ec1f75b 2019-03-26 stsp {
4182 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4183 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4184 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4185 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4186 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4187 2ec1f75b 2019-03-26 stsp
4188 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4189 2ec1f75b 2019-03-26 stsp if (err)
4190 2ec1f75b 2019-03-26 stsp return err;
4191 2ec1f75b 2019-03-26 stsp
4192 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4193 2ec1f75b 2019-03-26 stsp if (err)
4194 2ec1f75b 2019-03-26 stsp goto done;
4195 f2a9dc41 2019-12-13 tracey
4196 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4197 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4198 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4199 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4200 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4201 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4202 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4203 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4204 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4205 2ec1f75b 2019-03-26 stsp
4206 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4207 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4208 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4209 17ed4618 2019-06-02 stsp if (err)
4210 af12c6b9 2019-06-04 stsp break;
4211 2ec1f75b 2019-03-26 stsp }
4212 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4213 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4214 af12c6b9 2019-06-04 stsp err = sync_err;
4215 2ec1f75b 2019-03-26 stsp done:
4216 fb399478 2019-07-12 stsp free(fileindex_path);
4217 2ec1f75b 2019-03-26 stsp if (fileindex)
4218 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4219 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4220 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4221 2ec1f75b 2019-03-26 stsp err = unlockerr;
4222 33aa809d 2019-08-08 stsp return err;
4223 33aa809d 2019-08-08 stsp }
4224 33aa809d 2019-08-08 stsp
4225 33aa809d 2019-08-08 stsp static const struct got_error *
4226 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4227 33aa809d 2019-08-08 stsp {
4228 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4229 33aa809d 2019-08-08 stsp char *line = NULL;
4230 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4231 33aa809d 2019-08-08 stsp ssize_t linelen;
4232 33aa809d 2019-08-08 stsp
4233 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4234 33aa809d 2019-08-08 stsp if (linelen == -1) {
4235 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4236 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4237 33aa809d 2019-08-08 stsp goto done;
4238 33aa809d 2019-08-08 stsp }
4239 33aa809d 2019-08-08 stsp return NULL;
4240 33aa809d 2019-08-08 stsp }
4241 33aa809d 2019-08-08 stsp if (outfile) {
4242 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4243 33aa809d 2019-08-08 stsp if (n != linelen) {
4244 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4245 33aa809d 2019-08-08 stsp goto done;
4246 33aa809d 2019-08-08 stsp }
4247 33aa809d 2019-08-08 stsp }
4248 33aa809d 2019-08-08 stsp if (rejectfile) {
4249 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4250 33aa809d 2019-08-08 stsp if (n != linelen)
4251 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4252 33aa809d 2019-08-08 stsp }
4253 33aa809d 2019-08-08 stsp done:
4254 33aa809d 2019-08-08 stsp free(line);
4255 2ec1f75b 2019-03-26 stsp return err;
4256 2ec1f75b 2019-03-26 stsp }
4257 1f1abb7e 2019-08-08 stsp
4258 33aa809d 2019-08-08 stsp static const struct got_error *
4259 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4260 33aa809d 2019-08-08 stsp {
4261 33aa809d 2019-08-08 stsp char *line = NULL;
4262 33aa809d 2019-08-08 stsp size_t linesize = 0;
4263 33aa809d 2019-08-08 stsp ssize_t linelen;
4264 33aa809d 2019-08-08 stsp
4265 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4266 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4267 500467ff 2019-09-25 hiltjo if (ferror(f))
4268 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4269 500467ff 2019-09-25 hiltjo return NULL;
4270 500467ff 2019-09-25 hiltjo }
4271 33aa809d 2019-08-08 stsp free(line);
4272 33aa809d 2019-08-08 stsp return NULL;
4273 33aa809d 2019-08-08 stsp }
4274 33aa809d 2019-08-08 stsp
4275 33aa809d 2019-08-08 stsp static const struct got_error *
4276 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4277 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4278 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4279 abc59930 2021-09-05 naddy {
4280 33aa809d 2019-08-08 stsp const struct got_error *err;
4281 33aa809d 2019-08-08 stsp
4282 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4283 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4284 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4285 33aa809d 2019-08-08 stsp if (err)
4286 33aa809d 2019-08-08 stsp return err;
4287 33aa809d 2019-08-08 stsp (*line_cur1)++;
4288 33aa809d 2019-08-08 stsp }
4289 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4290 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4291 33aa809d 2019-08-08 stsp if (rejectfile)
4292 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4293 33aa809d 2019-08-08 stsp else
4294 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4295 33aa809d 2019-08-08 stsp if (err)
4296 33aa809d 2019-08-08 stsp return err;
4297 33aa809d 2019-08-08 stsp (*line_cur2)++;
4298 33aa809d 2019-08-08 stsp }
4299 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4300 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4301 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4302 33aa809d 2019-08-08 stsp if (err)
4303 33aa809d 2019-08-08 stsp return err;
4304 33aa809d 2019-08-08 stsp (*line_cur2)++;
4305 33aa809d 2019-08-08 stsp }
4306 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4307 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4308 33aa809d 2019-08-08 stsp if (rejectfile)
4309 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4310 33aa809d 2019-08-08 stsp else
4311 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4312 33aa809d 2019-08-08 stsp if (err)
4313 33aa809d 2019-08-08 stsp return err;
4314 33aa809d 2019-08-08 stsp (*line_cur1)++;
4315 33aa809d 2019-08-08 stsp }
4316 f1e81a05 2019-08-10 stsp
4317 f1e81a05 2019-08-10 stsp return NULL;
4318 f1e81a05 2019-08-10 stsp }
4319 f1e81a05 2019-08-10 stsp
4320 f1e81a05 2019-08-10 stsp static const struct got_error *
4321 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4322 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4323 f1e81a05 2019-08-10 stsp {
4324 f1e81a05 2019-08-10 stsp const struct got_error *err;
4325 f1e81a05 2019-08-10 stsp
4326 f1e81a05 2019-08-10 stsp if (outfile) {
4327 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4328 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4329 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4330 f1e81a05 2019-08-10 stsp if (err)
4331 f1e81a05 2019-08-10 stsp return err;
4332 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4333 f1e81a05 2019-08-10 stsp }
4334 f1e81a05 2019-08-10 stsp }
4335 f1e81a05 2019-08-10 stsp if (rejectfile) {
4336 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4337 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4338 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4339 f1e81a05 2019-08-10 stsp if (err)
4340 f1e81a05 2019-08-10 stsp return err;
4341 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4342 f1e81a05 2019-08-10 stsp }
4343 33aa809d 2019-08-08 stsp }
4344 33aa809d 2019-08-08 stsp
4345 33aa809d 2019-08-08 stsp return NULL;
4346 33aa809d 2019-08-08 stsp }
4347 33aa809d 2019-08-08 stsp
4348 33aa809d 2019-08-08 stsp static const struct got_error *
4349 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4350 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4351 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4352 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4353 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4354 33aa809d 2019-08-08 stsp {
4355 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4356 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4357 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4358 33aa809d 2019-08-08 stsp FILE *hunkfile;
4359 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4360 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4361 fe621944 2020-11-10 stsp int rc;
4362 33aa809d 2019-08-08 stsp
4363 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4364 33aa809d 2019-08-08 stsp
4365 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4366 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4367 fe621944 2020-11-10 stsp start_old = cc.left.start;
4368 fe621944 2020-11-10 stsp end_old = cc.left.end;
4369 fe621944 2020-11-10 stsp start_new = cc.right.start;
4370 fe621944 2020-11-10 stsp end_new = cc.right.end;
4371 33aa809d 2019-08-08 stsp
4372 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4373 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4374 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4375 33aa809d 2019-08-08 stsp
4376 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4377 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4378 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4379 33aa809d 2019-08-08 stsp
4380 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4381 fe621944 2020-11-10 stsp if (diff_state == NULL)
4382 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4383 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4384 fe621944 2020-11-10 stsp
4385 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4386 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4387 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4388 33aa809d 2019-08-08 stsp goto done;
4389 33aa809d 2019-08-08 stsp }
4390 fe621944 2020-11-10 stsp
4391 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4392 fe621944 2020-11-10 stsp diff_result, &cc);
4393 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4394 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4395 33aa809d 2019-08-08 stsp goto done;
4396 33aa809d 2019-08-08 stsp }
4397 fe621944 2020-11-10 stsp
4398 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4399 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4400 33aa809d 2019-08-08 stsp goto done;
4401 33aa809d 2019-08-08 stsp }
4402 33aa809d 2019-08-08 stsp
4403 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4404 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4405 33aa809d 2019-08-08 stsp if (err)
4406 33aa809d 2019-08-08 stsp goto done;
4407 33aa809d 2019-08-08 stsp
4408 33aa809d 2019-08-08 stsp switch (*choice) {
4409 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4410 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4411 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4412 33aa809d 2019-08-08 stsp break;
4413 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4414 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4415 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4416 33aa809d 2019-08-08 stsp break;
4417 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4418 33aa809d 2019-08-08 stsp break;
4419 33aa809d 2019-08-08 stsp default:
4420 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4421 33aa809d 2019-08-08 stsp break;
4422 33aa809d 2019-08-08 stsp }
4423 33aa809d 2019-08-08 stsp done:
4424 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4425 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4426 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4427 33aa809d 2019-08-08 stsp return err;
4428 33aa809d 2019-08-08 stsp }
4429 33aa809d 2019-08-08 stsp
4430 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4431 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4432 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4433 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4434 1f1abb7e 2019-08-08 stsp void *progress_arg;
4435 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4436 33aa809d 2019-08-08 stsp void *patch_arg;
4437 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4438 10604dce 2021-09-24 thomas int unlink_added_files;
4439 1f1abb7e 2019-08-08 stsp };
4440 a129376b 2019-03-28 stsp
4441 e20a8b6f 2019-06-04 stsp static const struct got_error *
4442 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4443 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4444 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4445 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4446 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4447 33aa809d 2019-08-08 stsp {
4448 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4449 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4450 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4451 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4452 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4453 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4454 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4455 fe621944 2020-11-10 stsp struct stat sb2;
4456 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4457 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4458 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4459 33aa809d 2019-08-08 stsp
4460 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4461 33aa809d 2019-08-08 stsp
4462 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4463 33aa809d 2019-08-08 stsp if (err)
4464 33aa809d 2019-08-08 stsp return err;
4465 33aa809d 2019-08-08 stsp
4466 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4467 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4468 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4469 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4470 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4471 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4472 fa3cef63 2020-07-23 stsp goto done;
4473 fa3cef63 2020-07-23 stsp }
4474 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4475 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4476 48b4f239 2021-12-31 thomas if (link_len == -1) {
4477 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4478 48b4f239 2021-12-31 thomas path2);
4479 48b4f239 2021-12-31 thomas }
4480 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4481 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4482 12463d8b 2019-12-13 stsp }
4483 12463d8b 2019-12-13 stsp } else {
4484 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4485 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4486 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4487 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4488 fa3cef63 2020-07-23 stsp goto done;
4489 fa3cef63 2020-07-23 stsp }
4490 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4491 fa3cef63 2020-07-23 stsp sizeof(link_target));
4492 fa3cef63 2020-07-23 stsp if (link_len == -1)
4493 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4494 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4495 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4496 12463d8b 2019-12-13 stsp }
4497 1ebedb77 2019-10-19 stsp }
4498 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4499 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4500 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4501 fa3cef63 2020-07-23 stsp goto done;
4502 fa3cef63 2020-07-23 stsp }
4503 1ebedb77 2019-10-19 stsp
4504 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4505 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4506 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4507 fa3cef63 2020-07-23 stsp goto done;
4508 fa3cef63 2020-07-23 stsp }
4509 fa3cef63 2020-07-23 stsp fd2 = -1;
4510 fa3cef63 2020-07-23 stsp } else {
4511 fa3cef63 2020-07-23 stsp size_t n;
4512 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4513 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4514 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4515 fa3cef63 2020-07-23 stsp goto done;
4516 fa3cef63 2020-07-23 stsp }
4517 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4518 fa3cef63 2020-07-23 stsp if (n != link_len) {
4519 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4520 fa3cef63 2020-07-23 stsp goto done;
4521 fa3cef63 2020-07-23 stsp }
4522 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4523 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4524 fa3cef63 2020-07-23 stsp goto done;
4525 fa3cef63 2020-07-23 stsp }
4526 fa3cef63 2020-07-23 stsp rewind(f2);
4527 33aa809d 2019-08-08 stsp }
4528 33aa809d 2019-08-08 stsp
4529 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4530 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4531 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4532 f4ae6ddb 2022-07-01 thomas goto done;
4533 f4ae6ddb 2022-07-01 thomas }
4534 f4ae6ddb 2022-07-01 thomas
4535 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4536 33aa809d 2019-08-08 stsp if (err)
4537 33aa809d 2019-08-08 stsp goto done;
4538 33aa809d 2019-08-08 stsp
4539 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4540 33aa809d 2019-08-08 stsp if (err)
4541 33aa809d 2019-08-08 stsp goto done;
4542 33aa809d 2019-08-08 stsp
4543 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4544 33aa809d 2019-08-08 stsp if (err)
4545 33aa809d 2019-08-08 stsp goto done;
4546 33aa809d 2019-08-08 stsp
4547 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4548 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4549 33aa809d 2019-08-08 stsp if (err)
4550 33aa809d 2019-08-08 stsp goto done;
4551 33aa809d 2019-08-08 stsp
4552 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4553 33aa809d 2019-08-08 stsp if (err)
4554 33aa809d 2019-08-08 stsp goto done;
4555 33aa809d 2019-08-08 stsp
4556 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4557 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4558 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4559 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4560 fe621944 2020-11-10 stsp
4561 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4562 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4563 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4564 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4565 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4566 fe621944 2020-11-10 stsp nchanges++;
4567 fe621944 2020-11-10 stsp }
4568 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4569 33aa809d 2019-08-08 stsp int choice;
4570 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4571 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4572 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4573 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4574 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4575 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4576 33aa809d 2019-08-08 stsp if (err)
4577 33aa809d 2019-08-08 stsp goto done;
4578 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4579 33aa809d 2019-08-08 stsp have_content = 1;
4580 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4581 33aa809d 2019-08-08 stsp break;
4582 33aa809d 2019-08-08 stsp }
4583 1ebedb77 2019-10-19 stsp if (have_content) {
4584 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4585 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4586 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4587 1ebedb77 2019-10-19 stsp if (err)
4588 1ebedb77 2019-10-19 stsp goto done;
4589 1ebedb77 2019-10-19 stsp
4590 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4591 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4592 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4593 fa3cef63 2020-07-23 stsp goto done;
4594 fa3cef63 2020-07-23 stsp }
4595 1ebedb77 2019-10-19 stsp }
4596 1ebedb77 2019-10-19 stsp }
4597 33aa809d 2019-08-08 stsp done:
4598 33aa809d 2019-08-08 stsp free(id_str);
4599 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4600 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4601 33aa809d 2019-08-08 stsp if (blob)
4602 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4603 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4604 fe621944 2020-11-10 stsp if (err == NULL)
4605 fe621944 2020-11-10 stsp err = free_err;
4606 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4607 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4608 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4609 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4610 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4611 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4612 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4613 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4614 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4615 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4616 33aa809d 2019-08-08 stsp if (err || !have_content) {
4617 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4618 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4619 33aa809d 2019-08-08 stsp free(*path_outfile);
4620 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4621 33aa809d 2019-08-08 stsp }
4622 33aa809d 2019-08-08 stsp free(path1);
4623 33aa809d 2019-08-08 stsp return err;
4624 33aa809d 2019-08-08 stsp }
4625 33aa809d 2019-08-08 stsp
4626 33aa809d 2019-08-08 stsp static const struct got_error *
4627 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4628 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4629 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4630 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4631 a129376b 2019-03-28 stsp {
4632 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4633 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4634 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4635 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4636 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4637 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4638 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4639 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4640 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4641 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4642 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4643 f4ae6ddb 2022-07-01 thomas int fd = -1;
4644 a129376b 2019-03-28 stsp
4645 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4646 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4647 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4648 d3bcc3d1 2019-08-08 stsp return NULL;
4649 3d69ad8d 2019-08-17 semarie
4650 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4651 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4652 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4653 d3bcc3d1 2019-08-08 stsp
4654 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4655 65084dad 2019-08-08 stsp if (ie == NULL)
4656 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4657 a129376b 2019-03-28 stsp
4658 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4659 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4660 a129376b 2019-03-28 stsp if (err) {
4661 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4662 a129376b 2019-03-28 stsp goto done;
4663 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4664 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4665 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4666 e20a8b6f 2019-06-04 stsp goto done;
4667 e20a8b6f 2019-06-04 stsp }
4668 a129376b 2019-03-28 stsp }
4669 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4670 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4671 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4672 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4673 a129376b 2019-03-28 stsp goto done;
4674 a129376b 2019-03-28 stsp }
4675 a129376b 2019-03-28 stsp } else {
4676 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4677 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4678 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4679 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4680 a129376b 2019-03-28 stsp goto done;
4681 a129376b 2019-03-28 stsp }
4682 a129376b 2019-03-28 stsp } else {
4683 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4684 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4685 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4686 a129376b 2019-03-28 stsp goto done;
4687 a129376b 2019-03-28 stsp }
4688 a129376b 2019-03-28 stsp }
4689 a129376b 2019-03-28 stsp }
4690 a129376b 2019-03-28 stsp
4691 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4692 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4693 945f9229 2022-04-16 thomas if (err)
4694 945f9229 2022-04-16 thomas goto done;
4695 945f9229 2022-04-16 thomas
4696 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4697 a9fa2909 2019-07-27 stsp if (err) {
4698 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4699 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4700 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4701 a9fa2909 2019-07-27 stsp goto done;
4702 a9fa2909 2019-07-27 stsp } else {
4703 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4704 a9fa2909 2019-07-27 stsp if (err)
4705 a9fa2909 2019-07-27 stsp goto done;
4706 a9fa2909 2019-07-27 stsp
4707 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4708 1233e6b6 2020-10-19 stsp if (err)
4709 a9fa2909 2019-07-27 stsp goto done;
4710 a9fa2909 2019-07-27 stsp
4711 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4712 1233e6b6 2020-10-19 stsp free(te_name);
4713 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4714 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4715 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4716 a9fa2909 2019-07-27 stsp goto done;
4717 a9fa2909 2019-07-27 stsp }
4718 a129376b 2019-03-28 stsp }
4719 a129376b 2019-03-28 stsp
4720 a129376b 2019-03-28 stsp switch (status) {
4721 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4722 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4723 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4724 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4725 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4726 33aa809d 2019-08-08 stsp if (err)
4727 33aa809d 2019-08-08 stsp goto done;
4728 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4729 33aa809d 2019-08-08 stsp break;
4730 33aa809d 2019-08-08 stsp }
4731 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4732 1f1abb7e 2019-08-08 stsp ie->path);
4733 1ee397ad 2019-07-12 stsp if (err)
4734 1ee397ad 2019-07-12 stsp goto done;
4735 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4736 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4737 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4738 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4739 10604dce 2021-09-24 thomas relpath) == -1) {
4740 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4741 10604dce 2021-09-24 thomas goto done;
4742 10604dce 2021-09-24 thomas }
4743 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4744 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4745 10604dce 2021-09-24 thomas ondisk_path);
4746 10604dce 2021-09-24 thomas break;
4747 10604dce 2021-09-24 thomas }
4748 10604dce 2021-09-24 thomas }
4749 a129376b 2019-03-28 stsp break;
4750 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4751 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4752 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4753 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4754 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4755 33aa809d 2019-08-08 stsp if (err)
4756 33aa809d 2019-08-08 stsp goto done;
4757 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4758 33aa809d 2019-08-08 stsp break;
4759 33aa809d 2019-08-08 stsp }
4760 33aa809d 2019-08-08 stsp /* fall through */
4761 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4762 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4763 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4764 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4765 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4766 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4767 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4768 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4769 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4770 24278f30 2019-08-03 stsp } else
4771 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4772 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4773 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4774 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4775 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4776 f4ae6ddb 2022-07-01 thomas goto done;
4777 f4ae6ddb 2022-07-01 thomas }
4778 f4ae6ddb 2022-07-01 thomas
4779 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4780 a129376b 2019-03-28 stsp if (err)
4781 65084dad 2019-08-08 stsp goto done;
4782 65084dad 2019-08-08 stsp
4783 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4784 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4785 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4786 a129376b 2019-03-28 stsp goto done;
4787 65084dad 2019-08-08 stsp }
4788 65084dad 2019-08-08 stsp
4789 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4790 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4791 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4792 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4793 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4794 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4795 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4796 33aa809d 2019-08-08 stsp break;
4797 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4798 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4799 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4800 369fd7e5 2020-07-23 stsp path_content);
4801 369fd7e5 2020-07-23 stsp break;
4802 369fd7e5 2020-07-23 stsp }
4803 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4804 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4805 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4806 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4807 369fd7e5 2020-07-23 stsp } else {
4808 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4809 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4810 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4811 369fd7e5 2020-07-23 stsp goto done;
4812 369fd7e5 2020-07-23 stsp }
4813 33aa809d 2019-08-08 stsp }
4814 33aa809d 2019-08-08 stsp } else {
4815 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4816 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4817 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4818 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4819 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4820 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4821 2e1fa222 2020-07-23 stsp } else {
4822 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4823 2e1fa222 2020-07-23 stsp ie->path,
4824 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4825 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4826 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4827 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4828 2e1fa222 2020-07-23 stsp }
4829 a129376b 2019-03-28 stsp if (err)
4830 a129376b 2019-03-28 stsp goto done;
4831 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4832 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4833 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4834 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4835 437adc9d 2020-12-10 yzhong blob->id.sha1,
4836 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4837 2e1fa222 2020-07-23 stsp if (err)
4838 2e1fa222 2020-07-23 stsp goto done;
4839 2e1fa222 2020-07-23 stsp }
4840 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4841 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4842 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4843 33aa809d 2019-08-08 stsp }
4844 a129376b 2019-03-28 stsp }
4845 a129376b 2019-03-28 stsp break;
4846 e20a8b6f 2019-06-04 stsp }
4847 a129376b 2019-03-28 stsp default:
4848 1f1abb7e 2019-08-08 stsp break;
4849 a129376b 2019-03-28 stsp }
4850 a129376b 2019-03-28 stsp done:
4851 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4852 33aa809d 2019-08-08 stsp free(path_content);
4853 e20a8b6f 2019-06-04 stsp free(parent_path);
4854 a129376b 2019-03-28 stsp free(tree_path);
4855 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4856 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4857 a129376b 2019-03-28 stsp if (blob)
4858 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4859 a129376b 2019-03-28 stsp if (tree)
4860 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4861 a129376b 2019-03-28 stsp free(tree_id);
4862 945f9229 2022-04-16 thomas if (base_commit)
4863 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4864 e20a8b6f 2019-06-04 stsp return err;
4865 e20a8b6f 2019-06-04 stsp }
4866 e20a8b6f 2019-06-04 stsp
4867 e20a8b6f 2019-06-04 stsp const struct got_error *
4868 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4869 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4870 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4871 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4872 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4873 e20a8b6f 2019-06-04 stsp {
4874 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4875 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4876 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4877 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4878 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4879 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4880 e20a8b6f 2019-06-04 stsp
4881 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4882 e20a8b6f 2019-06-04 stsp if (err)
4883 e20a8b6f 2019-06-04 stsp return err;
4884 e20a8b6f 2019-06-04 stsp
4885 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4886 e20a8b6f 2019-06-04 stsp if (err)
4887 e20a8b6f 2019-06-04 stsp goto done;
4888 e20a8b6f 2019-06-04 stsp
4889 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4890 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4891 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4892 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4893 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4894 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4895 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4896 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4897 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4898 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4899 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4900 e20a8b6f 2019-06-04 stsp if (err)
4901 af12c6b9 2019-06-04 stsp break;
4902 e20a8b6f 2019-06-04 stsp }
4903 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4904 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4905 e20a8b6f 2019-06-04 stsp err = sync_err;
4906 af12c6b9 2019-06-04 stsp done:
4907 fb399478 2019-07-12 stsp free(fileindex_path);
4908 a129376b 2019-03-28 stsp if (fileindex)
4909 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4910 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4911 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4912 a129376b 2019-03-28 stsp err = unlockerr;
4913 c4296144 2019-05-09 stsp return err;
4914 c4296144 2019-05-09 stsp }
4915 c4296144 2019-05-09 stsp
4916 cf066bf8 2019-05-09 stsp static void
4917 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4918 cf066bf8 2019-05-09 stsp {
4919 24519714 2019-05-09 stsp free(ct->path);
4920 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4921 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4922 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4923 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4924 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4925 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4926 cf066bf8 2019-05-09 stsp free(ct);
4927 cf066bf8 2019-05-09 stsp }
4928 24519714 2019-05-09 stsp
4929 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4930 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4931 24519714 2019-05-09 stsp struct got_repository *repo;
4932 24519714 2019-05-09 stsp struct got_worktree *worktree;
4933 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4934 5f8a88c6 2019-08-03 stsp int have_staged_files;
4935 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4936 24519714 2019-05-09 stsp };
4937 cf066bf8 2019-05-09 stsp
4938 c4296144 2019-05-09 stsp static const struct got_error *
4939 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4940 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4941 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4942 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4943 c4296144 2019-05-09 stsp {
4944 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4945 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4946 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4947 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4948 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4949 768aea60 2019-05-09 stsp struct stat sb;
4950 c4296144 2019-05-09 stsp
4951 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4952 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4953 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4954 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4955 5f8a88c6 2019-08-03 stsp return NULL;
4956 5f8a88c6 2019-08-03 stsp } else {
4957 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4958 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4959 c4296144 2019-05-09 stsp
4960 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4961 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4962 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4963 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4964 5f8a88c6 2019-08-03 stsp return NULL;
4965 5f8a88c6 2019-08-03 stsp }
4966 0b5cc0d6 2019-05-09 stsp
4967 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4968 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4969 036813ee 2019-05-09 stsp goto done;
4970 036813ee 2019-05-09 stsp }
4971 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4972 036813ee 2019-05-09 stsp parent_path = strdup("");
4973 69960a46 2019-05-09 stsp if (parent_path == NULL)
4974 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4975 69960a46 2019-05-09 stsp } else {
4976 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4977 69960a46 2019-05-09 stsp if (err)
4978 69960a46 2019-05-09 stsp return err;
4979 69960a46 2019-05-09 stsp }
4980 c4296144 2019-05-09 stsp
4981 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4982 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4983 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4984 c4296144 2019-05-09 stsp goto done;
4985 768aea60 2019-05-09 stsp }
4986 768aea60 2019-05-09 stsp
4987 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4988 768aea60 2019-05-09 stsp relpath) == -1) {
4989 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4990 768aea60 2019-05-09 stsp goto done;
4991 768aea60 2019-05-09 stsp }
4992 0aeb8099 2020-07-23 stsp
4993 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4994 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4995 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4996 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4997 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4998 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4999 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5000 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5001 0aeb8099 2020-07-23 stsp break;
5002 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5003 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5004 0aeb8099 2020-07-23 stsp break;
5005 0aeb8099 2020-07-23 stsp default:
5006 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5007 0aeb8099 2020-07-23 stsp goto done;
5008 0aeb8099 2020-07-23 stsp }
5009 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5010 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5011 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5012 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5013 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5014 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5015 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5016 12463d8b 2019-12-13 stsp ct->ondisk_path);
5017 12463d8b 2019-12-13 stsp goto done;
5018 12463d8b 2019-12-13 stsp }
5019 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5020 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5021 768aea60 2019-05-09 stsp goto done;
5022 768aea60 2019-05-09 stsp }
5023 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5024 c4296144 2019-05-09 stsp }
5025 c4296144 2019-05-09 stsp
5026 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5027 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5028 44d03001 2019-05-09 stsp relpath) == -1) {
5029 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5030 44d03001 2019-05-09 stsp goto done;
5031 44d03001 2019-05-09 stsp }
5032 44d03001 2019-05-09 stsp
5033 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5034 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5035 35213c7c 2020-07-23 stsp int is_bad_symlink;
5036 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5037 35213c7c 2020-07-23 stsp ssize_t target_len;
5038 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5039 35213c7c 2020-07-23 stsp sizeof(target_path));
5040 35213c7c 2020-07-23 stsp if (target_len == -1) {
5041 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5042 35213c7c 2020-07-23 stsp ct->ondisk_path);
5043 35213c7c 2020-07-23 stsp goto done;
5044 35213c7c 2020-07-23 stsp }
5045 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5046 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5047 35213c7c 2020-07-23 stsp if (err)
5048 35213c7c 2020-07-23 stsp goto done;
5049 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5050 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5051 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5052 35213c7c 2020-07-23 stsp goto done;
5053 35213c7c 2020-07-23 stsp }
5054 35213c7c 2020-07-23 stsp }
5055 35213c7c 2020-07-23 stsp
5056 35213c7c 2020-07-23 stsp
5057 cf066bf8 2019-05-09 stsp ct->status = status;
5058 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5059 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5060 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5061 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5062 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5063 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5064 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5065 b416585c 2019-05-13 stsp goto done;
5066 b416585c 2019-05-13 stsp }
5067 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5068 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5069 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5070 f0b75401 2019-08-03 stsp goto done;
5071 f0b75401 2019-08-03 stsp }
5072 f0b75401 2019-08-03 stsp }
5073 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5074 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5075 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5076 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5077 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5078 036813ee 2019-05-09 stsp goto done;
5079 036813ee 2019-05-09 stsp }
5080 ca2503ea 2019-05-09 stsp }
5081 24519714 2019-05-09 stsp ct->path = strdup(path);
5082 24519714 2019-05-09 stsp if (ct->path == NULL) {
5083 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5084 24519714 2019-05-09 stsp goto done;
5085 24519714 2019-05-09 stsp }
5086 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5087 c4296144 2019-05-09 stsp done:
5088 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5089 ed175427 2019-05-09 stsp free_commitable(ct);
5090 24519714 2019-05-09 stsp free(parent_path);
5091 036813ee 2019-05-09 stsp free(path);
5092 a129376b 2019-03-28 stsp return err;
5093 a129376b 2019-03-28 stsp }
5094 c4296144 2019-05-09 stsp
5095 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5096 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5097 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5098 036813ee 2019-05-09 stsp struct got_repository *);
5099 ed175427 2019-05-09 stsp
5100 ed175427 2019-05-09 stsp static const struct got_error *
5101 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5102 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5103 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5104 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5105 afa376bf 2019-05-09 stsp struct got_repository *repo)
5106 ed175427 2019-05-09 stsp {
5107 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5108 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5109 036813ee 2019-05-09 stsp char *subpath;
5110 ed175427 2019-05-09 stsp
5111 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5112 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5113 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5114 ed175427 2019-05-09 stsp
5115 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5116 ed175427 2019-05-09 stsp if (err)
5117 ed175427 2019-05-09 stsp return err;
5118 ed175427 2019-05-09 stsp
5119 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5120 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5121 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5122 036813ee 2019-05-09 stsp free(subpath);
5123 036813ee 2019-05-09 stsp return err;
5124 036813ee 2019-05-09 stsp }
5125 ed175427 2019-05-09 stsp
5126 036813ee 2019-05-09 stsp static const struct got_error *
5127 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5128 036813ee 2019-05-09 stsp {
5129 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5130 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5131 ed175427 2019-05-09 stsp
5132 036813ee 2019-05-09 stsp *match = 0;
5133 ed175427 2019-05-09 stsp
5134 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5135 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5136 0f63689d 2019-05-10 stsp return NULL;
5137 036813ee 2019-05-09 stsp }
5138 0b5cc0d6 2019-05-09 stsp
5139 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5140 0f63689d 2019-05-10 stsp if (err)
5141 0f63689d 2019-05-10 stsp return err;
5142 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5143 036813ee 2019-05-09 stsp free(ct_parent_path);
5144 036813ee 2019-05-09 stsp return err;
5145 036813ee 2019-05-09 stsp }
5146 0b5cc0d6 2019-05-09 stsp
5147 768aea60 2019-05-09 stsp static mode_t
5148 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5149 768aea60 2019-05-09 stsp {
5150 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5151 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5152 3d9a4ec4 2020-07-23 stsp
5153 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5154 768aea60 2019-05-09 stsp }
5155 768aea60 2019-05-09 stsp
5156 036813ee 2019-05-09 stsp static const struct got_error *
5157 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5158 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5159 036813ee 2019-05-09 stsp {
5160 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5161 ca2503ea 2019-05-09 stsp
5162 036813ee 2019-05-09 stsp *new_te = NULL;
5163 0b5cc0d6 2019-05-09 stsp
5164 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5165 036813ee 2019-05-09 stsp if (err)
5166 036813ee 2019-05-09 stsp goto done;
5167 0b5cc0d6 2019-05-09 stsp
5168 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5169 036813ee 2019-05-09 stsp
5170 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5171 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5172 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5173 5f8a88c6 2019-08-03 stsp else
5174 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5175 036813ee 2019-05-09 stsp done:
5176 036813ee 2019-05-09 stsp if (err && *new_te) {
5177 56e0773d 2019-11-28 stsp free(*new_te);
5178 036813ee 2019-05-09 stsp *new_te = NULL;
5179 036813ee 2019-05-09 stsp }
5180 036813ee 2019-05-09 stsp return err;
5181 036813ee 2019-05-09 stsp }
5182 036813ee 2019-05-09 stsp
5183 036813ee 2019-05-09 stsp static const struct got_error *
5184 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5185 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5186 036813ee 2019-05-09 stsp {
5187 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5188 102b254e 2020-10-19 stsp char *ct_name = NULL;
5189 036813ee 2019-05-09 stsp
5190 036813ee 2019-05-09 stsp *new_te = NULL;
5191 036813ee 2019-05-09 stsp
5192 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5193 036813ee 2019-05-09 stsp if (*new_te == NULL)
5194 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5195 036813ee 2019-05-09 stsp
5196 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5197 102b254e 2020-10-19 stsp if (err)
5198 036813ee 2019-05-09 stsp goto done;
5199 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5200 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5201 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5202 036813ee 2019-05-09 stsp goto done;
5203 036813ee 2019-05-09 stsp }
5204 036813ee 2019-05-09 stsp
5205 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5206 036813ee 2019-05-09 stsp
5207 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5208 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5209 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5210 5f8a88c6 2019-08-03 stsp else
5211 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5212 036813ee 2019-05-09 stsp done:
5213 102b254e 2020-10-19 stsp free(ct_name);
5214 036813ee 2019-05-09 stsp if (err && *new_te) {
5215 56e0773d 2019-11-28 stsp free(*new_te);
5216 036813ee 2019-05-09 stsp *new_te = NULL;
5217 036813ee 2019-05-09 stsp }
5218 036813ee 2019-05-09 stsp return err;
5219 036813ee 2019-05-09 stsp }
5220 036813ee 2019-05-09 stsp
5221 036813ee 2019-05-09 stsp static const struct got_error *
5222 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5223 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5224 036813ee 2019-05-09 stsp {
5225 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5226 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5227 036813ee 2019-05-09 stsp
5228 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5229 036813ee 2019-05-09 stsp if (err)
5230 036813ee 2019-05-09 stsp return err;
5231 036813ee 2019-05-09 stsp if (new_pe == NULL)
5232 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5233 036813ee 2019-05-09 stsp return NULL;
5234 afa376bf 2019-05-09 stsp }
5235 afa376bf 2019-05-09 stsp
5236 afa376bf 2019-05-09 stsp static const struct got_error *
5237 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5238 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5239 afa376bf 2019-05-09 stsp {
5240 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5241 5f8a88c6 2019-08-03 stsp unsigned char status;
5242 ae1e948a 2021-09-28 thomas
5243 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5244 ae1e948a 2021-09-28 thomas return NULL;
5245 5f8a88c6 2019-08-03 stsp
5246 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5247 afa376bf 2019-05-09 stsp ct_path++;
5248 5f8a88c6 2019-08-03 stsp
5249 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5250 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5251 5f8a88c6 2019-08-03 stsp else
5252 5f8a88c6 2019-08-03 stsp status = ct->status;
5253 5f8a88c6 2019-08-03 stsp
5254 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5255 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5256 036813ee 2019-05-09 stsp }
5257 036813ee 2019-05-09 stsp
5258 036813ee 2019-05-09 stsp static const struct got_error *
5259 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5260 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5261 44d03001 2019-05-09 stsp {
5262 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5263 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5264 44d03001 2019-05-09 stsp char *te_path;
5265 44d03001 2019-05-09 stsp
5266 44d03001 2019-05-09 stsp *modified = 0;
5267 44d03001 2019-05-09 stsp
5268 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5269 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5270 44d03001 2019-05-09 stsp te->name) == -1)
5271 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5272 44d03001 2019-05-09 stsp
5273 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5274 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5275 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5276 44d03001 2019-05-09 stsp strlen(te_path));
5277 62d463ca 2020-10-20 naddy if (*modified)
5278 44d03001 2019-05-09 stsp break;
5279 44d03001 2019-05-09 stsp }
5280 44d03001 2019-05-09 stsp
5281 44d03001 2019-05-09 stsp free(te_path);
5282 44d03001 2019-05-09 stsp return err;
5283 44d03001 2019-05-09 stsp }
5284 44d03001 2019-05-09 stsp
5285 44d03001 2019-05-09 stsp static const struct got_error *
5286 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5287 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5288 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5289 036813ee 2019-05-09 stsp {
5290 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5291 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5292 036813ee 2019-05-09 stsp
5293 036813ee 2019-05-09 stsp *ctp = NULL;
5294 036813ee 2019-05-09 stsp
5295 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5296 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5297 036813ee 2019-05-09 stsp char *ct_name = NULL;
5298 036813ee 2019-05-09 stsp int path_matches;
5299 036813ee 2019-05-09 stsp
5300 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5301 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5302 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5303 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5304 5f8a88c6 2019-08-03 stsp continue;
5305 5f8a88c6 2019-08-03 stsp } else {
5306 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5307 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5308 5f8a88c6 2019-08-03 stsp continue;
5309 5f8a88c6 2019-08-03 stsp }
5310 036813ee 2019-05-09 stsp
5311 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5312 036813ee 2019-05-09 stsp continue;
5313 036813ee 2019-05-09 stsp
5314 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5315 62d463ca 2020-10-20 naddy if (err)
5316 036813ee 2019-05-09 stsp return err;
5317 036813ee 2019-05-09 stsp if (!path_matches)
5318 036813ee 2019-05-09 stsp continue;
5319 036813ee 2019-05-09 stsp
5320 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5321 d34b633e 2020-10-19 stsp if (err)
5322 d34b633e 2020-10-19 stsp return err;
5323 d34b633e 2020-10-19 stsp
5324 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5325 d34b633e 2020-10-19 stsp free(ct_name);
5326 036813ee 2019-05-09 stsp continue;
5327 d34b633e 2020-10-19 stsp }
5328 d34b633e 2020-10-19 stsp free(ct_name);
5329 036813ee 2019-05-09 stsp
5330 036813ee 2019-05-09 stsp *ctp = ct;
5331 036813ee 2019-05-09 stsp break;
5332 036813ee 2019-05-09 stsp }
5333 2b496619 2019-07-10 stsp
5334 2b496619 2019-07-10 stsp return err;
5335 2b496619 2019-07-10 stsp }
5336 2b496619 2019-07-10 stsp
5337 2b496619 2019-07-10 stsp static const struct got_error *
5338 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5339 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5340 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5341 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5342 2b496619 2019-07-10 stsp struct got_repository *repo)
5343 2b496619 2019-07-10 stsp {
5344 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5345 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5346 2b496619 2019-07-10 stsp char *subtree_path;
5347 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5348 ba580f68 2020-03-22 stsp int nentries;
5349 2b496619 2019-07-10 stsp
5350 2b496619 2019-07-10 stsp *new_tep = NULL;
5351 2b496619 2019-07-10 stsp
5352 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5353 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5354 2b496619 2019-07-10 stsp child_path) == -1)
5355 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5356 036813ee 2019-05-09 stsp
5357 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5358 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5359 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5360 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5361 56e0773d 2019-11-28 stsp
5362 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5363 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5364 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5365 2b496619 2019-07-10 stsp goto done;
5366 2b496619 2019-07-10 stsp }
5367 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5368 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5369 2b496619 2019-07-10 stsp if (err) {
5370 56e0773d 2019-11-28 stsp free(new_te);
5371 2b496619 2019-07-10 stsp goto done;
5372 2b496619 2019-07-10 stsp }
5373 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5374 2b496619 2019-07-10 stsp done:
5375 56e0773d 2019-11-28 stsp free(id);
5376 2b496619 2019-07-10 stsp free(subtree_path);
5377 2b496619 2019-07-10 stsp if (err == NULL)
5378 2b496619 2019-07-10 stsp *new_tep = new_te;
5379 036813ee 2019-05-09 stsp return err;
5380 036813ee 2019-05-09 stsp }
5381 036813ee 2019-05-09 stsp
5382 036813ee 2019-05-09 stsp static const struct got_error *
5383 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5384 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5385 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5386 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5387 036813ee 2019-05-09 stsp struct got_repository *repo)
5388 036813ee 2019-05-09 stsp {
5389 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5390 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5391 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5392 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5393 036813ee 2019-05-09 stsp
5394 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5395 ba580f68 2020-03-22 stsp *nentries = 0;
5396 036813ee 2019-05-09 stsp
5397 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5398 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5399 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5400 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5401 036813ee 2019-05-09 stsp
5402 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5403 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5404 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5405 036813ee 2019-05-09 stsp continue;
5406 036813ee 2019-05-09 stsp
5407 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5408 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5409 036813ee 2019-05-09 stsp continue;
5410 036813ee 2019-05-09 stsp
5411 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5412 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5413 036813ee 2019-05-09 stsp if (err)
5414 036813ee 2019-05-09 stsp goto done;
5415 036813ee 2019-05-09 stsp
5416 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5417 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5418 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5419 036813ee 2019-05-09 stsp if (err)
5420 036813ee 2019-05-09 stsp goto done;
5421 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5422 afa376bf 2019-05-09 stsp if (err)
5423 afa376bf 2019-05-09 stsp goto done;
5424 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5425 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5426 2b496619 2019-07-10 stsp if (err)
5427 2f51b5b3 2019-05-09 stsp goto done;
5428 ba580f68 2020-03-22 stsp (*nentries)++;
5429 2b496619 2019-07-10 stsp } else {
5430 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5431 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5432 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5433 2b496619 2019-07-10 stsp == NULL) {
5434 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5435 2b496619 2019-07-10 stsp child_path, path_base_tree,
5436 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5437 2b496619 2019-07-10 stsp repo);
5438 2b496619 2019-07-10 stsp if (err)
5439 2b496619 2019-07-10 stsp goto done;
5440 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5441 2b496619 2019-07-10 stsp if (err)
5442 2b496619 2019-07-10 stsp goto done;
5443 ba580f68 2020-03-22 stsp (*nentries)++;
5444 9ba0479c 2019-05-10 stsp }
5445 ed175427 2019-05-09 stsp }
5446 2f51b5b3 2019-05-09 stsp }
5447 2f51b5b3 2019-05-09 stsp
5448 2f51b5b3 2019-05-09 stsp if (base_tree) {
5449 56e0773d 2019-11-28 stsp int i, nbase_entries;
5450 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5451 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5452 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5453 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5454 63c5ca5d 2019-08-24 stsp
5455 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5456 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5457 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5458 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5459 63c5ca5d 2019-08-24 stsp if (err)
5460 63c5ca5d 2019-08-24 stsp goto done;
5461 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5462 63c5ca5d 2019-08-24 stsp if (err)
5463 63c5ca5d 2019-08-24 stsp goto done;
5464 ba580f68 2020-03-22 stsp (*nentries)++;
5465 63c5ca5d 2019-08-24 stsp continue;
5466 63c5ca5d 2019-08-24 stsp }
5467 2f51b5b3 2019-05-09 stsp
5468 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5469 44d03001 2019-05-09 stsp int modified;
5470 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5471 036813ee 2019-05-09 stsp if (err)
5472 036813ee 2019-05-09 stsp goto done;
5473 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5474 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5475 2f51b5b3 2019-05-09 stsp if (err)
5476 2f51b5b3 2019-05-09 stsp goto done;
5477 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5478 44d03001 2019-05-09 stsp if (modified) {
5479 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5480 ba580f68 2020-03-22 stsp int nsubentries;
5481 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5482 ba580f68 2020-03-22 stsp &nsubentries, te,
5483 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5484 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5485 44d03001 2019-05-09 stsp if (err)
5486 44d03001 2019-05-09 stsp goto done;
5487 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5488 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5489 ba580f68 2020-03-22 stsp free(new_id);
5490 ba580f68 2020-03-22 stsp continue;
5491 ba580f68 2020-03-22 stsp }
5492 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5493 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5494 56e0773d 2019-11-28 stsp free(new_id);
5495 44d03001 2019-05-09 stsp }
5496 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5497 036813ee 2019-05-09 stsp if (err)
5498 036813ee 2019-05-09 stsp goto done;
5499 ba580f68 2020-03-22 stsp (*nentries)++;
5500 2f51b5b3 2019-05-09 stsp continue;
5501 036813ee 2019-05-09 stsp }
5502 2f51b5b3 2019-05-09 stsp
5503 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5504 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5505 f66c734c 2019-09-22 stsp if (err)
5506 f66c734c 2019-09-22 stsp goto done;
5507 2f51b5b3 2019-05-09 stsp if (ct) {
5508 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5509 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5510 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5511 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5512 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5513 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5514 2f51b5b3 2019-05-09 stsp if (err)
5515 2f51b5b3 2019-05-09 stsp goto done;
5516 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5517 2f51b5b3 2019-05-09 stsp if (err)
5518 2f51b5b3 2019-05-09 stsp goto done;
5519 ba580f68 2020-03-22 stsp (*nentries)++;
5520 2f51b5b3 2019-05-09 stsp }
5521 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5522 b416585c 2019-05-13 stsp status_arg);
5523 afa376bf 2019-05-09 stsp if (err)
5524 afa376bf 2019-05-09 stsp goto done;
5525 2f51b5b3 2019-05-09 stsp } else {
5526 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5527 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5528 2f51b5b3 2019-05-09 stsp if (err)
5529 2f51b5b3 2019-05-09 stsp goto done;
5530 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5531 2f51b5b3 2019-05-09 stsp if (err)
5532 2f51b5b3 2019-05-09 stsp goto done;
5533 ba580f68 2020-03-22 stsp (*nentries)++;
5534 2f51b5b3 2019-05-09 stsp }
5535 ca2503ea 2019-05-09 stsp }
5536 ed175427 2019-05-09 stsp }
5537 0b5cc0d6 2019-05-09 stsp
5538 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5539 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5540 ed175427 2019-05-09 stsp done:
5541 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5542 2e1fa222 2020-07-23 stsp return err;
5543 2e1fa222 2020-07-23 stsp }
5544 2e1fa222 2020-07-23 stsp
5545 2e1fa222 2020-07-23 stsp static const struct got_error *
5546 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5547 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5548 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5549 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5550 ebf99748 2019-05-09 stsp {
5551 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5552 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5553 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5554 ebf99748 2019-05-09 stsp
5555 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5556 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5557 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5558 ebf99748 2019-05-09 stsp
5559 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5560 437adc9d 2020-12-10 yzhong
5561 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5562 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5563 437adc9d 2020-12-10 yzhong if (err)
5564 437adc9d 2020-12-10 yzhong goto done;
5565 437adc9d 2020-12-10 yzhong
5566 ebf99748 2019-05-09 stsp if (ie) {
5567 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5568 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5569 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5570 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5571 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5572 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5573 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5574 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5575 437adc9d 2020-12-10 yzhong
5576 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5577 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5578 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5579 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5580 72fd46fa 2019-09-06 stsp !have_staged_files);
5581 ebf99748 2019-05-09 stsp } else
5582 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5583 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5584 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5585 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5586 72fd46fa 2019-09-06 stsp !have_staged_files);
5587 ebf99748 2019-05-09 stsp } else {
5588 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5589 ebf99748 2019-05-09 stsp if (err)
5590 437adc9d 2020-12-10 yzhong goto done;
5591 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5592 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5593 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5594 3969253a 2020-03-07 stsp if (err) {
5595 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5596 437adc9d 2020-12-10 yzhong goto done;
5597 3969253a 2020-03-07 stsp }
5598 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5599 3969253a 2020-03-07 stsp if (err) {
5600 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5601 437adc9d 2020-12-10 yzhong goto done;
5602 3969253a 2020-03-07 stsp }
5603 ebf99748 2019-05-09 stsp }
5604 437adc9d 2020-12-10 yzhong free(relpath);
5605 437adc9d 2020-12-10 yzhong relpath = NULL;
5606 ebf99748 2019-05-09 stsp }
5607 437adc9d 2020-12-10 yzhong done:
5608 437adc9d 2020-12-10 yzhong free(relpath);
5609 d56d26ce 2019-05-10 stsp return err;
5610 d56d26ce 2019-05-10 stsp }
5611 735ef5ac 2019-08-03 stsp
5612 d56d26ce 2019-05-10 stsp
5613 d56d26ce 2019-05-10 stsp static const struct got_error *
5614 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5615 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5616 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5617 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5618 735ef5ac 2019-08-03 stsp int ood_errcode)
5619 d56d26ce 2019-05-10 stsp {
5620 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5621 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5622 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5623 1a36436d 2019-06-10 stsp
5624 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5625 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5626 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5627 1a36436d 2019-06-10 stsp return NULL;
5628 9bead371 2019-07-28 stsp /*
5629 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5630 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5631 9bead371 2019-07-28 stsp */
5632 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5633 945f9229 2022-04-16 thomas if (err)
5634 945f9229 2022-04-16 thomas goto done;
5635 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5636 9bead371 2019-07-28 stsp if (err) {
5637 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5638 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5639 1a36436d 2019-06-10 stsp goto done;
5640 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5641 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5642 1a36436d 2019-06-10 stsp } else {
5643 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5644 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5645 945f9229 2022-04-16 thomas if (err)
5646 945f9229 2022-04-16 thomas goto done;
5647 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5648 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5649 1a36436d 2019-06-10 stsp goto done;
5650 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5651 1a36436d 2019-06-10 stsp }
5652 1a36436d 2019-06-10 stsp done:
5653 1a36436d 2019-06-10 stsp free(id);
5654 945f9229 2022-04-16 thomas if (commit)
5655 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5656 1a36436d 2019-06-10 stsp return err;
5657 ebf99748 2019-05-09 stsp }
5658 ebf99748 2019-05-09 stsp
5659 ef20f542 2022-06-26 thomas static const struct got_error *
5660 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5661 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5662 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5663 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5664 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5665 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5666 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5667 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5668 afa376bf 2019-05-09 stsp struct got_repository *repo)
5669 c4296144 2019-05-09 stsp {
5670 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5671 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5672 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5673 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5674 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5675 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5676 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5677 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5678 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5679 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5680 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5681 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5682 892eab0a 2022-07-22 thomas time_t timestamp;
5683 c4296144 2019-05-09 stsp
5684 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5685 c4296144 2019-05-09 stsp
5686 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5687 675c7539 2019-05-09 stsp
5688 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5689 588edf97 2019-05-10 stsp if (err)
5690 588edf97 2019-05-10 stsp goto done;
5691 de18fc63 2019-05-09 stsp
5692 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5693 588edf97 2019-05-10 stsp if (err)
5694 588edf97 2019-05-10 stsp goto done;
5695 588edf97 2019-05-10 stsp
5696 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5697 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5698 33ad4cbe 2019-05-12 jcs if (err)
5699 33ad4cbe 2019-05-12 jcs goto done;
5700 33ad4cbe 2019-05-12 jcs }
5701 c4296144 2019-05-09 stsp
5702 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5703 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5704 33ad4cbe 2019-05-12 jcs goto done;
5705 33ad4cbe 2019-05-12 jcs }
5706 33ad4cbe 2019-05-12 jcs
5707 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5708 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5709 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5710 cf066bf8 2019-05-09 stsp char *ondisk_path;
5711 cf066bf8 2019-05-09 stsp
5712 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5713 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5714 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5715 5f8a88c6 2019-08-03 stsp continue;
5716 5f8a88c6 2019-08-03 stsp
5717 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5718 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5719 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5720 cf066bf8 2019-05-09 stsp continue;
5721 cf066bf8 2019-05-09 stsp
5722 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5723 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5724 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5725 cf066bf8 2019-05-09 stsp goto done;
5726 cf066bf8 2019-05-09 stsp }
5727 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5728 2e1fa222 2020-07-23 stsp free(ondisk_path);
5729 2e1fa222 2020-07-23 stsp if (err)
5730 cf066bf8 2019-05-09 stsp goto done;
5731 cf066bf8 2019-05-09 stsp }
5732 036813ee 2019-05-09 stsp
5733 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5734 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5735 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5736 036813ee 2019-05-09 stsp if (err)
5737 036813ee 2019-05-09 stsp goto done;
5738 036813ee 2019-05-09 stsp
5739 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5740 de18fc63 2019-05-09 stsp if (err)
5741 de18fc63 2019-05-09 stsp goto done;
5742 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5743 10604dce 2021-09-24 thomas nparents++;
5744 10604dce 2021-09-24 thomas if (parent_id2) {
5745 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
5746 10604dce 2021-09-24 thomas if (err)
5747 10604dce 2021-09-24 thomas goto done;
5748 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5749 10604dce 2021-09-24 thomas nparents++;
5750 10604dce 2021-09-24 thomas }
5751 892eab0a 2022-07-22 thomas timestamp = time(NULL);
5752 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5753 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
5754 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5755 33ad4cbe 2019-05-12 jcs free(logmsg);
5756 9d40349a 2019-05-09 stsp if (err)
5757 9d40349a 2019-05-09 stsp goto done;
5758 9d40349a 2019-05-09 stsp
5759 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5760 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5761 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5762 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5763 09f5bd90 2019-05-09 stsp goto done;
5764 09f5bd90 2019-05-09 stsp }
5765 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5766 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5767 09f5bd90 2019-05-09 stsp if (err)
5768 09f5bd90 2019-05-09 stsp goto done;
5769 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5770 ebf99748 2019-05-09 stsp if (err)
5771 ebf99748 2019-05-09 stsp goto done;
5772 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5773 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5774 09f5bd90 2019-05-09 stsp goto done;
5775 09f5bd90 2019-05-09 stsp }
5776 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5777 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5778 f2c16586 2019-05-09 stsp if (err)
5779 f2c16586 2019-05-09 stsp goto done;
5780 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5781 f2c16586 2019-05-09 stsp if (err)
5782 f2c16586 2019-05-09 stsp goto done;
5783 f2c16586 2019-05-09 stsp
5784 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5785 09f5bd90 2019-05-09 stsp if (err)
5786 09f5bd90 2019-05-09 stsp goto done;
5787 09f5bd90 2019-05-09 stsp
5788 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5789 ebf99748 2019-05-09 stsp if (err)
5790 ebf99748 2019-05-09 stsp goto done;
5791 c4296144 2019-05-09 stsp done:
5792 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
5793 588edf97 2019-05-10 stsp if (head_tree)
5794 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5795 588edf97 2019-05-10 stsp if (head_commit)
5796 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5797 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5798 2f17228e 2019-05-12 stsp if (head_ref2) {
5799 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5800 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5801 2f17228e 2019-05-12 stsp err = unlockerr;
5802 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5803 39cd0ff6 2019-07-12 stsp }
5804 39cd0ff6 2019-07-12 stsp return err;
5805 39cd0ff6 2019-07-12 stsp }
5806 5c1e53bc 2019-07-28 stsp
5807 5c1e53bc 2019-07-28 stsp static const struct got_error *
5808 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5809 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5810 5c1e53bc 2019-07-28 stsp {
5811 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5812 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5813 39cd0ff6 2019-07-12 stsp
5814 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5815 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5816 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5817 5c1e53bc 2019-07-28 stsp
5818 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5819 5c1e53bc 2019-07-28 stsp ct_path++;
5820 5c1e53bc 2019-07-28 stsp
5821 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5822 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5823 5c1e53bc 2019-07-28 stsp break;
5824 5c1e53bc 2019-07-28 stsp }
5825 5c1e53bc 2019-07-28 stsp
5826 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5827 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5828 5c1e53bc 2019-07-28 stsp
5829 5c1e53bc 2019-07-28 stsp return NULL;
5830 5c1e53bc 2019-07-28 stsp }
5831 5c1e53bc 2019-07-28 stsp
5832 f0b75401 2019-08-03 stsp static const struct got_error *
5833 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5834 f0b75401 2019-08-03 stsp {
5835 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5836 f0b75401 2019-08-03 stsp
5837 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5838 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5839 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5840 f0b75401 2019-08-03 stsp }
5841 f0b75401 2019-08-03 stsp
5842 f0b75401 2019-08-03 stsp return NULL;
5843 f0b75401 2019-08-03 stsp }
5844 f0b75401 2019-08-03 stsp
5845 5f8a88c6 2019-08-03 stsp static const struct got_error *
5846 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5847 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5848 5f8a88c6 2019-08-03 stsp {
5849 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5850 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5851 5f8a88c6 2019-08-03 stsp
5852 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5853 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5854 5f8a88c6 2019-08-03 stsp continue;
5855 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5856 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5857 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5858 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5859 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5860 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5861 5f8a88c6 2019-08-03 stsp }
5862 5f8a88c6 2019-08-03 stsp
5863 5f8a88c6 2019-08-03 stsp return NULL;
5864 5f8a88c6 2019-08-03 stsp }
5865 5f8a88c6 2019-08-03 stsp
5866 39cd0ff6 2019-07-12 stsp const struct got_error *
5867 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5868 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5869 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5870 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5871 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5872 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5873 39cd0ff6 2019-07-12 stsp {
5874 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5875 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5876 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5877 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5878 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5879 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5880 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5881 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5882 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5883 39cd0ff6 2019-07-12 stsp
5884 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5885 39cd0ff6 2019-07-12 stsp
5886 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5887 39cd0ff6 2019-07-12 stsp
5888 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5889 39cd0ff6 2019-07-12 stsp if (err)
5890 39cd0ff6 2019-07-12 stsp goto done;
5891 39cd0ff6 2019-07-12 stsp
5892 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5893 39cd0ff6 2019-07-12 stsp if (err)
5894 39cd0ff6 2019-07-12 stsp goto done;
5895 39cd0ff6 2019-07-12 stsp
5896 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5897 39cd0ff6 2019-07-12 stsp if (err)
5898 39cd0ff6 2019-07-12 stsp goto done;
5899 39cd0ff6 2019-07-12 stsp
5900 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5901 39cd0ff6 2019-07-12 stsp if (err)
5902 39cd0ff6 2019-07-12 stsp goto done;
5903 39cd0ff6 2019-07-12 stsp
5904 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5905 5f8a88c6 2019-08-03 stsp &have_staged_files);
5906 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5907 5f8a88c6 2019-08-03 stsp goto done;
5908 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5909 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5910 5f8a88c6 2019-08-03 stsp if (err)
5911 5f8a88c6 2019-08-03 stsp goto done;
5912 5f8a88c6 2019-08-03 stsp }
5913 5f8a88c6 2019-08-03 stsp
5914 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5915 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5916 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5917 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5918 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5919 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5920 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5921 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5922 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5923 5c1e53bc 2019-07-28 stsp if (err)
5924 5c1e53bc 2019-07-28 stsp goto done;
5925 5c1e53bc 2019-07-28 stsp }
5926 39cd0ff6 2019-07-12 stsp
5927 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5928 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5929 39cd0ff6 2019-07-12 stsp goto done;
5930 5c1e53bc 2019-07-28 stsp }
5931 5c1e53bc 2019-07-28 stsp
5932 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5933 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5934 5c1e53bc 2019-07-28 stsp if (err)
5935 5c1e53bc 2019-07-28 stsp goto done;
5936 39cd0ff6 2019-07-12 stsp }
5937 f0b75401 2019-08-03 stsp
5938 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5939 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5940 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5941 f0b75401 2019-08-03 stsp
5942 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5943 f0b75401 2019-08-03 stsp ct_path++;
5944 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5945 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5946 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5947 b50cabdf 2019-07-12 stsp if (err)
5948 b50cabdf 2019-07-12 stsp goto done;
5949 f0b75401 2019-08-03 stsp
5950 b50cabdf 2019-07-12 stsp }
5951 b50cabdf 2019-07-12 stsp
5952 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5953 10604dce 2021-09-24 thomas head_commit_id, NULL, worktree, author, committer,
5954 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5955 39cd0ff6 2019-07-12 stsp if (err)
5956 39cd0ff6 2019-07-12 stsp goto done;
5957 39cd0ff6 2019-07-12 stsp
5958 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5959 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5960 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5961 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5962 39cd0ff6 2019-07-12 stsp err = sync_err;
5963 39cd0ff6 2019-07-12 stsp done:
5964 39cd0ff6 2019-07-12 stsp if (fileindex)
5965 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5966 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5967 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5968 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5969 39cd0ff6 2019-07-12 stsp err = unlockerr;
5970 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5971 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5972 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5973 39cd0ff6 2019-07-12 stsp }
5974 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5975 c4296144 2019-05-09 stsp return err;
5976 8656d6c4 2019-05-20 stsp }
5977 8656d6c4 2019-05-20 stsp
5978 8656d6c4 2019-05-20 stsp const char *
5979 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5980 8656d6c4 2019-05-20 stsp {
5981 8656d6c4 2019-05-20 stsp return ct->path;
5982 8656d6c4 2019-05-20 stsp }
5983 8656d6c4 2019-05-20 stsp
5984 8656d6c4 2019-05-20 stsp unsigned int
5985 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5986 8656d6c4 2019-05-20 stsp {
5987 8656d6c4 2019-05-20 stsp return ct->status;
5988 818c7501 2019-07-11 stsp }
5989 818c7501 2019-07-11 stsp
5990 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5991 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5992 818c7501 2019-07-11 stsp struct got_repository *repo;
5993 818c7501 2019-07-11 stsp };
5994 818c7501 2019-07-11 stsp
5995 818c7501 2019-07-11 stsp static const struct got_error *
5996 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5997 818c7501 2019-07-11 stsp {
5998 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5999 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6000 818c7501 2019-07-11 stsp unsigned char status;
6001 818c7501 2019-07-11 stsp struct stat sb;
6002 818c7501 2019-07-11 stsp char *ondisk_path;
6003 818c7501 2019-07-11 stsp
6004 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6005 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6006 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6007 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6008 818c7501 2019-07-11 stsp
6009 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6010 818c7501 2019-07-11 stsp == -1)
6011 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6012 818c7501 2019-07-11 stsp
6013 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6014 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6015 818c7501 2019-07-11 stsp free(ondisk_path);
6016 818c7501 2019-07-11 stsp if (err)
6017 818c7501 2019-07-11 stsp return err;
6018 818c7501 2019-07-11 stsp
6019 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6020 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6021 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6022 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6023 818c7501 2019-07-11 stsp
6024 818c7501 2019-07-11 stsp return NULL;
6025 818c7501 2019-07-11 stsp }
6026 818c7501 2019-07-11 stsp
6027 818c7501 2019-07-11 stsp const struct got_error *
6028 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6029 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6030 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6031 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6032 818c7501 2019-07-11 stsp {
6033 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6034 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6035 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6036 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6037 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6038 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6039 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6040 818c7501 2019-07-11 stsp
6041 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6042 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6043 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6044 818c7501 2019-07-11 stsp
6045 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6046 818c7501 2019-07-11 stsp if (err)
6047 818c7501 2019-07-11 stsp return err;
6048 818c7501 2019-07-11 stsp
6049 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6050 818c7501 2019-07-11 stsp if (err)
6051 818c7501 2019-07-11 stsp goto done;
6052 818c7501 2019-07-11 stsp
6053 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6054 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6055 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6056 818c7501 2019-07-11 stsp &ok_arg);
6057 818c7501 2019-07-11 stsp if (err)
6058 818c7501 2019-07-11 stsp goto done;
6059 818c7501 2019-07-11 stsp
6060 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6061 818c7501 2019-07-11 stsp if (err)
6062 818c7501 2019-07-11 stsp goto done;
6063 818c7501 2019-07-11 stsp
6064 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6065 818c7501 2019-07-11 stsp if (err)
6066 818c7501 2019-07-11 stsp goto done;
6067 818c7501 2019-07-11 stsp
6068 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6069 818c7501 2019-07-11 stsp if (err)
6070 818c7501 2019-07-11 stsp goto done;
6071 818c7501 2019-07-11 stsp
6072 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6073 818c7501 2019-07-11 stsp 0);
6074 e51d7b55 2020-01-04 stsp if (err)
6075 e51d7b55 2020-01-04 stsp goto done;
6076 e51d7b55 2020-01-04 stsp
6077 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6078 818c7501 2019-07-11 stsp if (err)
6079 818c7501 2019-07-11 stsp goto done;
6080 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6081 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6082 e51d7b55 2020-01-04 stsp goto done;
6083 e51d7b55 2020-01-04 stsp }
6084 818c7501 2019-07-11 stsp
6085 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6086 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6087 818c7501 2019-07-11 stsp if (err)
6088 818c7501 2019-07-11 stsp goto done;
6089 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6090 818c7501 2019-07-11 stsp if (err)
6091 818c7501 2019-07-11 stsp goto done;
6092 818c7501 2019-07-11 stsp
6093 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6094 818c7501 2019-07-11 stsp
6095 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6096 818c7501 2019-07-11 stsp if (err)
6097 818c7501 2019-07-11 stsp goto done;
6098 818c7501 2019-07-11 stsp
6099 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6100 818c7501 2019-07-11 stsp if (err)
6101 818c7501 2019-07-11 stsp goto done;
6102 818c7501 2019-07-11 stsp
6103 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6104 818c7501 2019-07-11 stsp worktree->base_commit_id);
6105 818c7501 2019-07-11 stsp if (err)
6106 818c7501 2019-07-11 stsp goto done;
6107 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6108 818c7501 2019-07-11 stsp if (err)
6109 818c7501 2019-07-11 stsp goto done;
6110 818c7501 2019-07-11 stsp
6111 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6112 818c7501 2019-07-11 stsp if (err)
6113 818c7501 2019-07-11 stsp goto done;
6114 818c7501 2019-07-11 stsp done:
6115 818c7501 2019-07-11 stsp free(fileindex_path);
6116 818c7501 2019-07-11 stsp free(tmp_branch_name);
6117 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6118 818c7501 2019-07-11 stsp free(branch_ref_name);
6119 818c7501 2019-07-11 stsp if (branch_ref)
6120 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6121 818c7501 2019-07-11 stsp if (wt_branch)
6122 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6123 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6124 818c7501 2019-07-11 stsp if (err) {
6125 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6126 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6127 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6128 818c7501 2019-07-11 stsp }
6129 818c7501 2019-07-11 stsp if (*tmp_branch) {
6130 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6131 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6132 818c7501 2019-07-11 stsp }
6133 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6134 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6135 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6136 3e3a69f1 2019-07-25 stsp }
6137 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6138 818c7501 2019-07-11 stsp }
6139 818c7501 2019-07-11 stsp return err;
6140 818c7501 2019-07-11 stsp }
6141 818c7501 2019-07-11 stsp
6142 818c7501 2019-07-11 stsp const struct got_error *
6143 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6144 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6145 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6146 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6147 818c7501 2019-07-11 stsp {
6148 818c7501 2019-07-11 stsp const struct got_error *err;
6149 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6150 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6151 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6152 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6153 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6154 818c7501 2019-07-11 stsp
6155 818c7501 2019-07-11 stsp *commit_id = NULL;
6156 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6157 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6158 3e3a69f1 2019-07-25 stsp *branch = NULL;
6159 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6160 3e3a69f1 2019-07-25 stsp
6161 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6162 3e3a69f1 2019-07-25 stsp if (err)
6163 3e3a69f1 2019-07-25 stsp return err;
6164 818c7501 2019-07-11 stsp
6165 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6166 3e3a69f1 2019-07-25 stsp if (err)
6167 f032f1f7 2019-08-04 stsp goto done;
6168 f032f1f7 2019-08-04 stsp
6169 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6170 f032f1f7 2019-08-04 stsp &have_staged_files);
6171 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6172 f032f1f7 2019-08-04 stsp goto done;
6173 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6174 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6175 3e3a69f1 2019-07-25 stsp goto done;
6176 f032f1f7 2019-08-04 stsp }
6177 3e3a69f1 2019-07-25 stsp
6178 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6179 818c7501 2019-07-11 stsp if (err)
6180 f032f1f7 2019-08-04 stsp goto done;
6181 818c7501 2019-07-11 stsp
6182 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6183 818c7501 2019-07-11 stsp if (err)
6184 818c7501 2019-07-11 stsp goto done;
6185 818c7501 2019-07-11 stsp
6186 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6187 818c7501 2019-07-11 stsp if (err)
6188 818c7501 2019-07-11 stsp goto done;
6189 818c7501 2019-07-11 stsp
6190 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6191 818c7501 2019-07-11 stsp if (err)
6192 818c7501 2019-07-11 stsp goto done;
6193 818c7501 2019-07-11 stsp
6194 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6195 818c7501 2019-07-11 stsp if (err)
6196 818c7501 2019-07-11 stsp goto done;
6197 818c7501 2019-07-11 stsp
6198 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6199 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6200 818c7501 2019-07-11 stsp if (err)
6201 818c7501 2019-07-11 stsp goto done;
6202 818c7501 2019-07-11 stsp
6203 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6204 818c7501 2019-07-11 stsp if (err)
6205 818c7501 2019-07-11 stsp goto done;
6206 818c7501 2019-07-11 stsp
6207 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6208 818c7501 2019-07-11 stsp if (err)
6209 818c7501 2019-07-11 stsp goto done;
6210 818c7501 2019-07-11 stsp
6211 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6212 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6213 818c7501 2019-07-11 stsp if (err)
6214 818c7501 2019-07-11 stsp goto done;
6215 818c7501 2019-07-11 stsp
6216 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6217 818c7501 2019-07-11 stsp if (err)
6218 818c7501 2019-07-11 stsp goto done;
6219 818c7501 2019-07-11 stsp done:
6220 818c7501 2019-07-11 stsp free(commit_ref_name);
6221 818c7501 2019-07-11 stsp free(branch_ref_name);
6222 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6223 818c7501 2019-07-11 stsp if (commit_ref)
6224 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6225 818c7501 2019-07-11 stsp if (branch_ref)
6226 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6227 818c7501 2019-07-11 stsp if (err) {
6228 818c7501 2019-07-11 stsp free(*commit_id);
6229 818c7501 2019-07-11 stsp *commit_id = NULL;
6230 818c7501 2019-07-11 stsp if (*tmp_branch) {
6231 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6232 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6233 818c7501 2019-07-11 stsp }
6234 818c7501 2019-07-11 stsp if (*new_base_branch) {
6235 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6236 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6237 818c7501 2019-07-11 stsp }
6238 818c7501 2019-07-11 stsp if (*branch) {
6239 818c7501 2019-07-11 stsp got_ref_close(*branch);
6240 818c7501 2019-07-11 stsp *branch = NULL;
6241 818c7501 2019-07-11 stsp }
6242 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6243 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6244 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6245 3e3a69f1 2019-07-25 stsp }
6246 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6247 818c7501 2019-07-11 stsp }
6248 818c7501 2019-07-11 stsp return err;
6249 c4296144 2019-05-09 stsp }
6250 818c7501 2019-07-11 stsp
6251 818c7501 2019-07-11 stsp const struct got_error *
6252 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6253 818c7501 2019-07-11 stsp {
6254 818c7501 2019-07-11 stsp const struct got_error *err;
6255 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6256 818c7501 2019-07-11 stsp
6257 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6258 818c7501 2019-07-11 stsp if (err)
6259 818c7501 2019-07-11 stsp return err;
6260 818c7501 2019-07-11 stsp
6261 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6262 818c7501 2019-07-11 stsp free(tmp_branch_name);
6263 818c7501 2019-07-11 stsp return NULL;
6264 818c7501 2019-07-11 stsp }
6265 818c7501 2019-07-11 stsp
6266 818c7501 2019-07-11 stsp static const struct got_error *
6267 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6268 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6269 818c7501 2019-07-11 stsp {
6270 0ebf8283 2019-07-24 stsp *logmsg = arg;
6271 818c7501 2019-07-11 stsp return NULL;
6272 818c7501 2019-07-11 stsp }
6273 818c7501 2019-07-11 stsp
6274 818c7501 2019-07-11 stsp static const struct got_error *
6275 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6276 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6277 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6278 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6279 818c7501 2019-07-11 stsp {
6280 818c7501 2019-07-11 stsp return NULL;
6281 01757395 2019-07-12 stsp }
6282 01757395 2019-07-12 stsp
6283 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6284 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6285 01757395 2019-07-12 stsp void *progress_arg;
6286 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6287 01757395 2019-07-12 stsp };
6288 01757395 2019-07-12 stsp
6289 01757395 2019-07-12 stsp static const struct got_error *
6290 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6291 01757395 2019-07-12 stsp {
6292 01757395 2019-07-12 stsp const struct got_error *err;
6293 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6294 01757395 2019-07-12 stsp char *p;
6295 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6296 01757395 2019-07-12 stsp
6297 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6298 01757395 2019-07-12 stsp if (err)
6299 01757395 2019-07-12 stsp return err;
6300 01757395 2019-07-12 stsp
6301 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6302 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6303 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6304 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6305 01757395 2019-07-12 stsp return NULL;
6306 01757395 2019-07-12 stsp
6307 01757395 2019-07-12 stsp p = strdup(path);
6308 01757395 2019-07-12 stsp if (p == NULL)
6309 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6310 01757395 2019-07-12 stsp
6311 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6312 01757395 2019-07-12 stsp if (err || new == NULL)
6313 01757395 2019-07-12 stsp free(p);
6314 01757395 2019-07-12 stsp return err;
6315 01757395 2019-07-12 stsp }
6316 01757395 2019-07-12 stsp
6317 01757395 2019-07-12 stsp void
6318 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6319 01757395 2019-07-12 stsp {
6320 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6321 01757395 2019-07-12 stsp
6322 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6323 01757395 2019-07-12 stsp free((char *)pe->path);
6324 01757395 2019-07-12 stsp
6325 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6326 818c7501 2019-07-11 stsp }
6327 818c7501 2019-07-11 stsp
6328 0ebf8283 2019-07-24 stsp static const struct got_error *
6329 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6330 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6331 818c7501 2019-07-11 stsp {
6332 818c7501 2019-07-11 stsp const struct got_error *err;
6333 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6334 818c7501 2019-07-11 stsp
6335 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6336 818c7501 2019-07-11 stsp if (err) {
6337 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6338 818c7501 2019-07-11 stsp goto done;
6339 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6340 818c7501 2019-07-11 stsp if (err)
6341 818c7501 2019-07-11 stsp goto done;
6342 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6343 818c7501 2019-07-11 stsp if (err)
6344 818c7501 2019-07-11 stsp goto done;
6345 de05890f 2020-03-05 stsp } else if (is_rebase) {
6346 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6347 818c7501 2019-07-11 stsp int cmp;
6348 818c7501 2019-07-11 stsp
6349 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6350 818c7501 2019-07-11 stsp if (err)
6351 818c7501 2019-07-11 stsp goto done;
6352 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6353 818c7501 2019-07-11 stsp free(stored_id);
6354 818c7501 2019-07-11 stsp if (cmp != 0) {
6355 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6356 818c7501 2019-07-11 stsp goto done;
6357 818c7501 2019-07-11 stsp }
6358 818c7501 2019-07-11 stsp }
6359 0ebf8283 2019-07-24 stsp done:
6360 0ebf8283 2019-07-24 stsp if (commit_ref)
6361 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6362 0ebf8283 2019-07-24 stsp return err;
6363 0ebf8283 2019-07-24 stsp }
6364 0ebf8283 2019-07-24 stsp
6365 0ebf8283 2019-07-24 stsp static const struct got_error *
6366 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6367 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6368 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6369 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6370 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6371 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6372 0ebf8283 2019-07-24 stsp {
6373 0ebf8283 2019-07-24 stsp const struct got_error *err;
6374 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6375 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6376 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6377 818c7501 2019-07-11 stsp
6378 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6379 0ebf8283 2019-07-24 stsp
6380 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6381 0ebf8283 2019-07-24 stsp if (err)
6382 0ebf8283 2019-07-24 stsp return err;
6383 0ebf8283 2019-07-24 stsp
6384 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6385 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6386 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6387 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6388 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6389 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6390 818c7501 2019-07-11 stsp if (commit_ref)
6391 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6392 818c7501 2019-07-11 stsp return err;
6393 818c7501 2019-07-11 stsp }
6394 818c7501 2019-07-11 stsp
6395 818c7501 2019-07-11 stsp const struct got_error *
6396 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6397 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6398 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6399 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6400 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6401 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6402 818c7501 2019-07-11 stsp {
6403 0ebf8283 2019-07-24 stsp const struct got_error *err;
6404 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6405 0ebf8283 2019-07-24 stsp
6406 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6407 0ebf8283 2019-07-24 stsp if (err)
6408 0ebf8283 2019-07-24 stsp return err;
6409 0ebf8283 2019-07-24 stsp
6410 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6411 0ebf8283 2019-07-24 stsp if (err)
6412 0ebf8283 2019-07-24 stsp goto done;
6413 0ebf8283 2019-07-24 stsp
6414 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6415 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6416 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6417 0ebf8283 2019-07-24 stsp done:
6418 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6419 0ebf8283 2019-07-24 stsp return err;
6420 0ebf8283 2019-07-24 stsp }
6421 0ebf8283 2019-07-24 stsp
6422 0ebf8283 2019-07-24 stsp const struct got_error *
6423 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6424 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6425 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6426 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6427 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6428 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6429 0ebf8283 2019-07-24 stsp {
6430 0ebf8283 2019-07-24 stsp const struct got_error *err;
6431 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6432 0ebf8283 2019-07-24 stsp
6433 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6434 0ebf8283 2019-07-24 stsp if (err)
6435 0ebf8283 2019-07-24 stsp return err;
6436 0ebf8283 2019-07-24 stsp
6437 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6438 0ebf8283 2019-07-24 stsp if (err)
6439 0ebf8283 2019-07-24 stsp goto done;
6440 0ebf8283 2019-07-24 stsp
6441 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6442 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6443 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6444 0ebf8283 2019-07-24 stsp done:
6445 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6446 0ebf8283 2019-07-24 stsp return err;
6447 0ebf8283 2019-07-24 stsp }
6448 0ebf8283 2019-07-24 stsp
6449 0ebf8283 2019-07-24 stsp static const struct got_error *
6450 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6451 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6452 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6453 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6454 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6455 150d7275 2022-07-22 thomas struct got_repository *repo)
6456 0ebf8283 2019-07-24 stsp {
6457 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6458 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6459 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6460 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6461 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6462 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6463 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6464 818c7501 2019-07-11 stsp
6465 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6466 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6467 a0e95631 2019-07-12 stsp
6468 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6469 818c7501 2019-07-11 stsp
6470 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6471 a0e95631 2019-07-12 stsp if (err)
6472 3e3a69f1 2019-07-25 stsp return err;
6473 a0e95631 2019-07-12 stsp
6474 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6475 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6476 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6477 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6478 01757395 2019-07-12 stsp /*
6479 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6480 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6481 6c13b005 2021-09-02 stsp *
6482 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6483 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6484 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6485 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6486 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6487 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6488 01757395 2019-07-12 stsp */
6489 01757395 2019-07-12 stsp if (merged_paths) {
6490 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6491 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6492 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6493 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6494 f2a9dc41 2019-12-13 tracey 0);
6495 01757395 2019-07-12 stsp if (err)
6496 01757395 2019-07-12 stsp goto done;
6497 01757395 2019-07-12 stsp }
6498 01757395 2019-07-12 stsp } else {
6499 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6500 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6501 01757395 2019-07-12 stsp if (err)
6502 01757395 2019-07-12 stsp goto done;
6503 01757395 2019-07-12 stsp }
6504 a0e95631 2019-07-12 stsp
6505 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6506 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6507 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6508 a0e95631 2019-07-12 stsp if (err)
6509 a0e95631 2019-07-12 stsp goto done;
6510 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6511 a0e95631 2019-07-12 stsp goto done;
6512 ff0d2220 2019-07-11 stsp }
6513 818c7501 2019-07-11 stsp
6514 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6515 edd02c5e 2019-07-11 stsp if (err)
6516 edd02c5e 2019-07-11 stsp goto done;
6517 edd02c5e 2019-07-11 stsp
6518 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6519 818c7501 2019-07-11 stsp if (err)
6520 818c7501 2019-07-11 stsp goto done;
6521 818c7501 2019-07-11 stsp
6522 5943eee2 2019-08-13 stsp if (new_logmsg) {
6523 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6524 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6525 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6526 5943eee2 2019-08-13 stsp goto done;
6527 5943eee2 2019-08-13 stsp }
6528 5943eee2 2019-08-13 stsp } else {
6529 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6530 5943eee2 2019-08-13 stsp if (err)
6531 5943eee2 2019-08-13 stsp goto done;
6532 5943eee2 2019-08-13 stsp }
6533 0ebf8283 2019-07-24 stsp
6534 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6535 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6536 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6537 8db00f97 2022-07-22 thomas committer ? committer :
6538 8db00f97 2022-07-22 thomas got_object_commit_get_committer(orig_commit),
6539 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6540 a0e95631 2019-07-12 stsp if (err)
6541 a0e95631 2019-07-12 stsp goto done;
6542 a0e95631 2019-07-12 stsp
6543 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6544 a0e95631 2019-07-12 stsp if (err)
6545 a0e95631 2019-07-12 stsp goto done;
6546 a0e95631 2019-07-12 stsp
6547 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6548 a0e95631 2019-07-12 stsp if (err)
6549 a0e95631 2019-07-12 stsp goto done;
6550 a0e95631 2019-07-12 stsp
6551 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6552 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6553 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6554 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6555 a0e95631 2019-07-12 stsp err = sync_err;
6556 818c7501 2019-07-11 stsp done:
6557 a0e95631 2019-07-12 stsp free(fileindex_path);
6558 a0e95631 2019-07-12 stsp free(head_commit_id);
6559 a0e95631 2019-07-12 stsp if (head_ref)
6560 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6561 818c7501 2019-07-11 stsp if (err) {
6562 818c7501 2019-07-11 stsp free(*new_commit_id);
6563 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6564 818c7501 2019-07-11 stsp }
6565 818c7501 2019-07-11 stsp return err;
6566 818c7501 2019-07-11 stsp }
6567 818c7501 2019-07-11 stsp
6568 818c7501 2019-07-11 stsp const struct got_error *
6569 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6570 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6571 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6572 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6573 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6574 0ebf8283 2019-07-24 stsp {
6575 0ebf8283 2019-07-24 stsp const struct got_error *err;
6576 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6577 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6578 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6579 0ebf8283 2019-07-24 stsp
6580 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6581 0ebf8283 2019-07-24 stsp if (err)
6582 0ebf8283 2019-07-24 stsp return err;
6583 0ebf8283 2019-07-24 stsp
6584 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6585 0ebf8283 2019-07-24 stsp if (err)
6586 0ebf8283 2019-07-24 stsp goto done;
6587 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6588 0ebf8283 2019-07-24 stsp if (err)
6589 0ebf8283 2019-07-24 stsp goto done;
6590 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6591 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6592 0ebf8283 2019-07-24 stsp goto done;
6593 0ebf8283 2019-07-24 stsp }
6594 0ebf8283 2019-07-24 stsp
6595 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6596 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6597 150d7275 2022-07-22 thomas NULL, repo);
6598 0ebf8283 2019-07-24 stsp done:
6599 0ebf8283 2019-07-24 stsp if (commit_ref)
6600 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6601 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6602 0ebf8283 2019-07-24 stsp free(commit_id);
6603 0ebf8283 2019-07-24 stsp return err;
6604 0ebf8283 2019-07-24 stsp }
6605 0ebf8283 2019-07-24 stsp
6606 0ebf8283 2019-07-24 stsp const struct got_error *
6607 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6608 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6609 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6610 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6611 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6612 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6613 0ebf8283 2019-07-24 stsp {
6614 0ebf8283 2019-07-24 stsp const struct got_error *err;
6615 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6616 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6617 0ebf8283 2019-07-24 stsp
6618 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6619 0ebf8283 2019-07-24 stsp if (err)
6620 0ebf8283 2019-07-24 stsp return err;
6621 0ebf8283 2019-07-24 stsp
6622 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6623 0ebf8283 2019-07-24 stsp if (err)
6624 0ebf8283 2019-07-24 stsp goto done;
6625 0ebf8283 2019-07-24 stsp
6626 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6627 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6628 150d7275 2022-07-22 thomas new_logmsg, repo);
6629 0ebf8283 2019-07-24 stsp done:
6630 0ebf8283 2019-07-24 stsp if (commit_ref)
6631 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6632 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6633 0ebf8283 2019-07-24 stsp return err;
6634 0ebf8283 2019-07-24 stsp }
6635 0ebf8283 2019-07-24 stsp
6636 0ebf8283 2019-07-24 stsp const struct got_error *
6637 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6638 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6639 818c7501 2019-07-11 stsp {
6640 3e3a69f1 2019-07-25 stsp if (fileindex)
6641 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6642 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6643 69844fba 2019-07-11 stsp }
6644 69844fba 2019-07-11 stsp
6645 69844fba 2019-07-11 stsp static const struct got_error *
6646 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6647 69844fba 2019-07-11 stsp {
6648 69844fba 2019-07-11 stsp const struct got_error *err;
6649 69844fba 2019-07-11 stsp struct got_reference *ref;
6650 69844fba 2019-07-11 stsp
6651 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6652 69844fba 2019-07-11 stsp if (err) {
6653 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6654 69844fba 2019-07-11 stsp return NULL;
6655 69844fba 2019-07-11 stsp return err;
6656 69844fba 2019-07-11 stsp }
6657 69844fba 2019-07-11 stsp
6658 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6659 69844fba 2019-07-11 stsp got_ref_close(ref);
6660 69844fba 2019-07-11 stsp return err;
6661 818c7501 2019-07-11 stsp }
6662 818c7501 2019-07-11 stsp
6663 69844fba 2019-07-11 stsp static const struct got_error *
6664 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6665 69844fba 2019-07-11 stsp {
6666 69844fba 2019-07-11 stsp const struct got_error *err;
6667 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6668 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6669 69844fba 2019-07-11 stsp
6670 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6671 69844fba 2019-07-11 stsp if (err)
6672 69844fba 2019-07-11 stsp goto done;
6673 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6674 69844fba 2019-07-11 stsp if (err)
6675 69844fba 2019-07-11 stsp goto done;
6676 69844fba 2019-07-11 stsp
6677 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6678 69844fba 2019-07-11 stsp if (err)
6679 69844fba 2019-07-11 stsp goto done;
6680 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6681 69844fba 2019-07-11 stsp if (err)
6682 69844fba 2019-07-11 stsp goto done;
6683 69844fba 2019-07-11 stsp
6684 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6685 69844fba 2019-07-11 stsp if (err)
6686 69844fba 2019-07-11 stsp goto done;
6687 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6688 69844fba 2019-07-11 stsp if (err)
6689 69844fba 2019-07-11 stsp goto done;
6690 69844fba 2019-07-11 stsp
6691 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6692 69844fba 2019-07-11 stsp if (err)
6693 69844fba 2019-07-11 stsp goto done;
6694 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6695 69844fba 2019-07-11 stsp if (err)
6696 69844fba 2019-07-11 stsp goto done;
6697 69844fba 2019-07-11 stsp
6698 69844fba 2019-07-11 stsp done:
6699 69844fba 2019-07-11 stsp free(tmp_branch_name);
6700 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6701 69844fba 2019-07-11 stsp free(branch_ref_name);
6702 69844fba 2019-07-11 stsp free(commit_ref_name);
6703 e600f124 2021-03-21 stsp return err;
6704 e600f124 2021-03-21 stsp }
6705 e600f124 2021-03-21 stsp
6706 ef20f542 2022-06-26 thomas static const struct got_error *
6707 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6708 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6709 e600f124 2021-03-21 stsp {
6710 e600f124 2021-03-21 stsp const struct got_error *err;
6711 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6712 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6713 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6714 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6715 e600f124 2021-03-21 stsp char *refname = NULL;
6716 e600f124 2021-03-21 stsp
6717 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6718 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6719 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6720 e600f124 2021-03-21 stsp branch_name += 11;
6721 e600f124 2021-03-21 stsp
6722 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6723 e600f124 2021-03-21 stsp if (err)
6724 e600f124 2021-03-21 stsp return err;
6725 e600f124 2021-03-21 stsp
6726 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6727 e600f124 2021-03-21 stsp new_id_str) == -1) {
6728 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6729 e600f124 2021-03-21 stsp goto done;
6730 e600f124 2021-03-21 stsp }
6731 e600f124 2021-03-21 stsp
6732 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6733 e600f124 2021-03-21 stsp if (err)
6734 e600f124 2021-03-21 stsp goto done;
6735 e600f124 2021-03-21 stsp
6736 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6737 e600f124 2021-03-21 stsp if (err)
6738 e600f124 2021-03-21 stsp goto done;
6739 e600f124 2021-03-21 stsp
6740 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6741 e600f124 2021-03-21 stsp done:
6742 e600f124 2021-03-21 stsp free(new_id_str);
6743 e600f124 2021-03-21 stsp free(refname);
6744 e600f124 2021-03-21 stsp free(old_commit_id);
6745 e600f124 2021-03-21 stsp if (ref)
6746 e600f124 2021-03-21 stsp got_ref_close(ref);
6747 69844fba 2019-07-11 stsp return err;
6748 69844fba 2019-07-11 stsp }
6749 69844fba 2019-07-11 stsp
6750 818c7501 2019-07-11 stsp const struct got_error *
6751 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6752 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6753 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6754 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6755 818c7501 2019-07-11 stsp {
6756 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6757 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6758 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6759 818c7501 2019-07-11 stsp
6760 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6761 818c7501 2019-07-11 stsp if (err)
6762 818c7501 2019-07-11 stsp return err;
6763 e600f124 2021-03-21 stsp
6764 e600f124 2021-03-21 stsp if (create_backup) {
6765 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6766 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6767 e600f124 2021-03-21 stsp if (err)
6768 e600f124 2021-03-21 stsp goto done;
6769 e600f124 2021-03-21 stsp }
6770 818c7501 2019-07-11 stsp
6771 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6772 818c7501 2019-07-11 stsp if (err)
6773 818c7501 2019-07-11 stsp goto done;
6774 818c7501 2019-07-11 stsp
6775 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6776 818c7501 2019-07-11 stsp if (err)
6777 818c7501 2019-07-11 stsp goto done;
6778 818c7501 2019-07-11 stsp
6779 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6780 818c7501 2019-07-11 stsp if (err)
6781 818c7501 2019-07-11 stsp goto done;
6782 818c7501 2019-07-11 stsp
6783 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6784 a615e0e7 2020-12-16 stsp if (err)
6785 a615e0e7 2020-12-16 stsp goto done;
6786 a615e0e7 2020-12-16 stsp
6787 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6788 a615e0e7 2020-12-16 stsp if (err)
6789 a615e0e7 2020-12-16 stsp goto done;
6790 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6791 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6792 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6793 a615e0e7 2020-12-16 stsp err = sync_err;
6794 818c7501 2019-07-11 stsp done:
6795 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6796 a615e0e7 2020-12-16 stsp free(fileindex_path);
6797 818c7501 2019-07-11 stsp free(new_head_commit_id);
6798 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6799 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6800 818c7501 2019-07-11 stsp err = unlockerr;
6801 818c7501 2019-07-11 stsp return err;
6802 818c7501 2019-07-11 stsp }
6803 818c7501 2019-07-11 stsp
6804 818c7501 2019-07-11 stsp const struct got_error *
6805 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6806 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6807 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6808 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6809 818c7501 2019-07-11 stsp {
6810 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6811 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6812 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6813 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6814 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6815 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6816 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6817 818c7501 2019-07-11 stsp
6818 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6819 818c7501 2019-07-11 stsp if (err)
6820 818c7501 2019-07-11 stsp return err;
6821 818c7501 2019-07-11 stsp
6822 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
6823 945f9229 2022-04-16 thomas worktree->base_commit_id);
6824 945f9229 2022-04-16 thomas if (err)
6825 945f9229 2022-04-16 thomas goto done;
6826 945f9229 2022-04-16 thomas
6827 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6828 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6829 818c7501 2019-07-11 stsp if (err)
6830 818c7501 2019-07-11 stsp goto done;
6831 818c7501 2019-07-11 stsp
6832 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6833 818c7501 2019-07-11 stsp if (err)
6834 818c7501 2019-07-11 stsp goto done;
6835 818c7501 2019-07-11 stsp
6836 818c7501 2019-07-11 stsp /*
6837 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6838 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6839 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6840 818c7501 2019-07-11 stsp */
6841 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6842 818c7501 2019-07-11 stsp if (err)
6843 818c7501 2019-07-11 stsp goto done;
6844 818c7501 2019-07-11 stsp
6845 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6846 818c7501 2019-07-11 stsp if (err)
6847 818c7501 2019-07-11 stsp goto done;
6848 818c7501 2019-07-11 stsp
6849 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
6850 945f9229 2022-04-16 thomas worktree->path_prefix);
6851 ca355955 2019-07-12 stsp if (err)
6852 ca355955 2019-07-12 stsp goto done;
6853 ca355955 2019-07-12 stsp
6854 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6855 ca355955 2019-07-12 stsp if (err)
6856 ca355955 2019-07-12 stsp goto done;
6857 ca355955 2019-07-12 stsp
6858 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6859 818c7501 2019-07-11 stsp if (err)
6860 818c7501 2019-07-11 stsp goto done;
6861 818c7501 2019-07-11 stsp
6862 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6863 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6864 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6865 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6866 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6867 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6868 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6869 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
6870 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6871 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
6872 55bd499d 2019-07-12 stsp if (err)
6873 1f1abb7e 2019-08-08 stsp goto sync;
6874 55bd499d 2019-07-12 stsp
6875 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6876 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6877 ca355955 2019-07-12 stsp sync:
6878 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6879 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6880 ca355955 2019-07-12 stsp err = sync_err;
6881 818c7501 2019-07-11 stsp done:
6882 818c7501 2019-07-11 stsp got_ref_close(resolved);
6883 a3a2faf2 2019-07-12 stsp free(tree_id);
6884 818c7501 2019-07-11 stsp free(commit_id);
6885 945f9229 2022-04-16 thomas if (commit)
6886 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6887 0ebf8283 2019-07-24 stsp if (fileindex)
6888 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6889 0ebf8283 2019-07-24 stsp free(fileindex_path);
6890 0ebf8283 2019-07-24 stsp
6891 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6892 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6893 0ebf8283 2019-07-24 stsp err = unlockerr;
6894 0ebf8283 2019-07-24 stsp return err;
6895 0ebf8283 2019-07-24 stsp }
6896 0ebf8283 2019-07-24 stsp
6897 0ebf8283 2019-07-24 stsp const struct got_error *
6898 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6899 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6900 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6901 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6902 0ebf8283 2019-07-24 stsp {
6903 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6904 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6905 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6906 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6907 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6908 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6909 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6910 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6911 0ebf8283 2019-07-24 stsp
6912 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6913 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6914 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6915 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6916 0ebf8283 2019-07-24 stsp
6917 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6918 0ebf8283 2019-07-24 stsp if (err)
6919 0ebf8283 2019-07-24 stsp return err;
6920 0ebf8283 2019-07-24 stsp
6921 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6922 0ebf8283 2019-07-24 stsp if (err)
6923 0ebf8283 2019-07-24 stsp goto done;
6924 0ebf8283 2019-07-24 stsp
6925 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6926 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6927 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6928 0ebf8283 2019-07-24 stsp &ok_arg);
6929 0ebf8283 2019-07-24 stsp if (err)
6930 0ebf8283 2019-07-24 stsp goto done;
6931 0ebf8283 2019-07-24 stsp
6932 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6933 0ebf8283 2019-07-24 stsp if (err)
6934 0ebf8283 2019-07-24 stsp goto done;
6935 0ebf8283 2019-07-24 stsp
6936 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6937 0ebf8283 2019-07-24 stsp if (err)
6938 0ebf8283 2019-07-24 stsp goto done;
6939 0ebf8283 2019-07-24 stsp
6940 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6941 0ebf8283 2019-07-24 stsp worktree);
6942 0ebf8283 2019-07-24 stsp if (err)
6943 0ebf8283 2019-07-24 stsp goto done;
6944 0ebf8283 2019-07-24 stsp
6945 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6946 0ebf8283 2019-07-24 stsp 0);
6947 0ebf8283 2019-07-24 stsp if (err)
6948 0ebf8283 2019-07-24 stsp goto done;
6949 0ebf8283 2019-07-24 stsp
6950 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6951 0ebf8283 2019-07-24 stsp if (err)
6952 0ebf8283 2019-07-24 stsp goto done;
6953 0ebf8283 2019-07-24 stsp
6954 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6955 0ebf8283 2019-07-24 stsp if (err)
6956 0ebf8283 2019-07-24 stsp goto done;
6957 0ebf8283 2019-07-24 stsp
6958 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6959 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6960 0ebf8283 2019-07-24 stsp if (err)
6961 0ebf8283 2019-07-24 stsp goto done;
6962 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6963 0ebf8283 2019-07-24 stsp if (err)
6964 0ebf8283 2019-07-24 stsp goto done;
6965 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6966 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6967 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6968 0ebf8283 2019-07-24 stsp goto done;
6969 0ebf8283 2019-07-24 stsp }
6970 0ebf8283 2019-07-24 stsp
6971 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6972 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6973 0ebf8283 2019-07-24 stsp if (err)
6974 0ebf8283 2019-07-24 stsp goto done;
6975 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6976 0ebf8283 2019-07-24 stsp if (err)
6977 0ebf8283 2019-07-24 stsp goto done;
6978 0ebf8283 2019-07-24 stsp
6979 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6980 0ebf8283 2019-07-24 stsp if (err)
6981 0ebf8283 2019-07-24 stsp goto done;
6982 0ebf8283 2019-07-24 stsp done:
6983 0ebf8283 2019-07-24 stsp free(fileindex_path);
6984 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6985 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6986 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6987 0ebf8283 2019-07-24 stsp if (wt_branch)
6988 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6989 0ebf8283 2019-07-24 stsp if (err) {
6990 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6991 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6992 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6993 0ebf8283 2019-07-24 stsp }
6994 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6995 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6996 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6997 0ebf8283 2019-07-24 stsp }
6998 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6999 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7000 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7001 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7002 3e3a69f1 2019-07-25 stsp }
7003 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7004 0ebf8283 2019-07-24 stsp }
7005 0ebf8283 2019-07-24 stsp return err;
7006 0ebf8283 2019-07-24 stsp }
7007 0ebf8283 2019-07-24 stsp
7008 0ebf8283 2019-07-24 stsp const struct got_error *
7009 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7010 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7011 0ebf8283 2019-07-24 stsp {
7012 3e3a69f1 2019-07-25 stsp if (fileindex)
7013 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7014 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7015 0ebf8283 2019-07-24 stsp }
7016 0ebf8283 2019-07-24 stsp
7017 0ebf8283 2019-07-24 stsp const struct got_error *
7018 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7019 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7020 0ebf8283 2019-07-24 stsp {
7021 0ebf8283 2019-07-24 stsp const struct got_error *err;
7022 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7023 0ebf8283 2019-07-24 stsp
7024 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7025 0ebf8283 2019-07-24 stsp if (err)
7026 0ebf8283 2019-07-24 stsp return err;
7027 0ebf8283 2019-07-24 stsp
7028 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7029 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7030 0ebf8283 2019-07-24 stsp return NULL;
7031 0ebf8283 2019-07-24 stsp }
7032 0ebf8283 2019-07-24 stsp
7033 0ebf8283 2019-07-24 stsp const struct got_error *
7034 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7035 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7036 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7037 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7038 0ebf8283 2019-07-24 stsp {
7039 0ebf8283 2019-07-24 stsp const struct got_error *err;
7040 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7041 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7042 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7043 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7044 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7045 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7046 0ebf8283 2019-07-24 stsp
7047 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7048 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7049 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7050 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7051 0ebf8283 2019-07-24 stsp
7052 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7053 3e3a69f1 2019-07-25 stsp if (err)
7054 3e3a69f1 2019-07-25 stsp return err;
7055 3e3a69f1 2019-07-25 stsp
7056 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7057 3e3a69f1 2019-07-25 stsp if (err)
7058 f032f1f7 2019-08-04 stsp goto done;
7059 f032f1f7 2019-08-04 stsp
7060 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7061 f032f1f7 2019-08-04 stsp &have_staged_files);
7062 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7063 f032f1f7 2019-08-04 stsp goto done;
7064 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7065 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7066 3e3a69f1 2019-07-25 stsp goto done;
7067 f032f1f7 2019-08-04 stsp }
7068 3e3a69f1 2019-07-25 stsp
7069 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7070 0ebf8283 2019-07-24 stsp if (err)
7071 f032f1f7 2019-08-04 stsp goto done;
7072 0ebf8283 2019-07-24 stsp
7073 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7074 0ebf8283 2019-07-24 stsp if (err)
7075 0ebf8283 2019-07-24 stsp goto done;
7076 0ebf8283 2019-07-24 stsp
7077 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7078 0ebf8283 2019-07-24 stsp if (err)
7079 0ebf8283 2019-07-24 stsp goto done;
7080 0ebf8283 2019-07-24 stsp
7081 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7082 0ebf8283 2019-07-24 stsp worktree);
7083 0ebf8283 2019-07-24 stsp if (err)
7084 0ebf8283 2019-07-24 stsp goto done;
7085 0ebf8283 2019-07-24 stsp
7086 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7087 0ebf8283 2019-07-24 stsp if (err)
7088 0ebf8283 2019-07-24 stsp goto done;
7089 0ebf8283 2019-07-24 stsp
7090 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7091 0ebf8283 2019-07-24 stsp if (err)
7092 0ebf8283 2019-07-24 stsp goto done;
7093 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7094 0ebf8283 2019-07-24 stsp if (err)
7095 0ebf8283 2019-07-24 stsp goto done;
7096 0ebf8283 2019-07-24 stsp
7097 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7098 0ebf8283 2019-07-24 stsp if (err)
7099 0ebf8283 2019-07-24 stsp goto done;
7100 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7101 0ebf8283 2019-07-24 stsp if (err)
7102 0ebf8283 2019-07-24 stsp goto done;
7103 0ebf8283 2019-07-24 stsp
7104 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7105 0ebf8283 2019-07-24 stsp if (err)
7106 0ebf8283 2019-07-24 stsp goto done;
7107 0ebf8283 2019-07-24 stsp done:
7108 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7109 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7110 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7111 0ebf8283 2019-07-24 stsp if (commit_ref)
7112 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7113 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7114 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7115 0ebf8283 2019-07-24 stsp if (err) {
7116 0ebf8283 2019-07-24 stsp free(*commit_id);
7117 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7118 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7119 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7120 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7121 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7122 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7123 0ebf8283 2019-07-24 stsp }
7124 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7125 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7126 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7127 3e3a69f1 2019-07-25 stsp }
7128 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7129 0ebf8283 2019-07-24 stsp }
7130 0ebf8283 2019-07-24 stsp return err;
7131 0ebf8283 2019-07-24 stsp }
7132 0ebf8283 2019-07-24 stsp
7133 0ebf8283 2019-07-24 stsp static const struct got_error *
7134 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7135 0ebf8283 2019-07-24 stsp {
7136 0ebf8283 2019-07-24 stsp const struct got_error *err;
7137 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7138 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7139 0ebf8283 2019-07-24 stsp
7140 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7141 0ebf8283 2019-07-24 stsp if (err)
7142 0ebf8283 2019-07-24 stsp goto done;
7143 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7144 0ebf8283 2019-07-24 stsp if (err)
7145 0ebf8283 2019-07-24 stsp goto done;
7146 0ebf8283 2019-07-24 stsp
7147 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7148 0ebf8283 2019-07-24 stsp worktree);
7149 0ebf8283 2019-07-24 stsp if (err)
7150 0ebf8283 2019-07-24 stsp goto done;
7151 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7152 0ebf8283 2019-07-24 stsp if (err)
7153 0ebf8283 2019-07-24 stsp goto done;
7154 0ebf8283 2019-07-24 stsp
7155 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7156 0ebf8283 2019-07-24 stsp if (err)
7157 0ebf8283 2019-07-24 stsp goto done;
7158 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7159 0ebf8283 2019-07-24 stsp if (err)
7160 0ebf8283 2019-07-24 stsp goto done;
7161 0ebf8283 2019-07-24 stsp
7162 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7163 0ebf8283 2019-07-24 stsp if (err)
7164 0ebf8283 2019-07-24 stsp goto done;
7165 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7166 0ebf8283 2019-07-24 stsp if (err)
7167 0ebf8283 2019-07-24 stsp goto done;
7168 0ebf8283 2019-07-24 stsp done:
7169 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7170 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7171 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7172 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7173 0ebf8283 2019-07-24 stsp return err;
7174 0ebf8283 2019-07-24 stsp }
7175 0ebf8283 2019-07-24 stsp
7176 0ebf8283 2019-07-24 stsp const struct got_error *
7177 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7178 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7179 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7180 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7181 0ebf8283 2019-07-24 stsp {
7182 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7183 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7184 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7185 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7186 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7187 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7188 0ebf8283 2019-07-24 stsp
7189 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7190 0ebf8283 2019-07-24 stsp if (err)
7191 0ebf8283 2019-07-24 stsp return err;
7192 0ebf8283 2019-07-24 stsp
7193 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7194 945f9229 2022-04-16 thomas worktree->base_commit_id);
7195 945f9229 2022-04-16 thomas if (err)
7196 945f9229 2022-04-16 thomas goto done;
7197 945f9229 2022-04-16 thomas
7198 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7199 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7200 0ebf8283 2019-07-24 stsp if (err)
7201 0ebf8283 2019-07-24 stsp goto done;
7202 0ebf8283 2019-07-24 stsp
7203 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7204 0ebf8283 2019-07-24 stsp if (err)
7205 0ebf8283 2019-07-24 stsp goto done;
7206 0ebf8283 2019-07-24 stsp
7207 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7208 0ebf8283 2019-07-24 stsp if (err)
7209 0ebf8283 2019-07-24 stsp goto done;
7210 0ebf8283 2019-07-24 stsp
7211 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7212 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7213 0ebf8283 2019-07-24 stsp if (err)
7214 0ebf8283 2019-07-24 stsp goto done;
7215 0ebf8283 2019-07-24 stsp
7216 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7217 0ebf8283 2019-07-24 stsp if (err)
7218 0ebf8283 2019-07-24 stsp goto done;
7219 0ebf8283 2019-07-24 stsp
7220 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7221 0ebf8283 2019-07-24 stsp if (err)
7222 0ebf8283 2019-07-24 stsp goto done;
7223 0ebf8283 2019-07-24 stsp
7224 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7225 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7226 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7227 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7228 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7229 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7230 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7231 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7232 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7233 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7234 0ebf8283 2019-07-24 stsp if (err)
7235 1f1abb7e 2019-08-08 stsp goto sync;
7236 0ebf8283 2019-07-24 stsp
7237 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7238 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7239 0ebf8283 2019-07-24 stsp sync:
7240 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7241 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7242 0ebf8283 2019-07-24 stsp err = sync_err;
7243 0ebf8283 2019-07-24 stsp done:
7244 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7245 0ebf8283 2019-07-24 stsp free(tree_id);
7246 818c7501 2019-07-11 stsp free(fileindex_path);
7247 818c7501 2019-07-11 stsp
7248 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7249 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7250 818c7501 2019-07-11 stsp err = unlockerr;
7251 818c7501 2019-07-11 stsp return err;
7252 818c7501 2019-07-11 stsp }
7253 0ebf8283 2019-07-24 stsp
7254 0ebf8283 2019-07-24 stsp const struct got_error *
7255 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7256 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7257 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7258 0ebf8283 2019-07-24 stsp {
7259 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7260 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7261 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7262 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7263 0ebf8283 2019-07-24 stsp
7264 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7265 0ebf8283 2019-07-24 stsp if (err)
7266 0ebf8283 2019-07-24 stsp return err;
7267 0ebf8283 2019-07-24 stsp
7268 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7269 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7270 e600f124 2021-03-21 stsp if (err)
7271 e600f124 2021-03-21 stsp goto done;
7272 e600f124 2021-03-21 stsp
7273 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7274 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7275 0ebf8283 2019-07-24 stsp if (err)
7276 0ebf8283 2019-07-24 stsp goto done;
7277 0ebf8283 2019-07-24 stsp
7278 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7279 0ebf8283 2019-07-24 stsp if (err)
7280 0ebf8283 2019-07-24 stsp goto done;
7281 0ebf8283 2019-07-24 stsp
7282 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7283 0ebf8283 2019-07-24 stsp if (err)
7284 0ebf8283 2019-07-24 stsp goto done;
7285 0ebf8283 2019-07-24 stsp
7286 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7287 0ebf8283 2019-07-24 stsp if (err)
7288 0ebf8283 2019-07-24 stsp goto done;
7289 0ebf8283 2019-07-24 stsp
7290 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7291 a615e0e7 2020-12-16 stsp if (err)
7292 a615e0e7 2020-12-16 stsp goto done;
7293 a615e0e7 2020-12-16 stsp
7294 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7295 a615e0e7 2020-12-16 stsp if (err)
7296 a615e0e7 2020-12-16 stsp goto done;
7297 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7298 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7299 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7300 a615e0e7 2020-12-16 stsp err = sync_err;
7301 0ebf8283 2019-07-24 stsp done:
7302 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7303 a615e0e7 2020-12-16 stsp free(fileindex_path);
7304 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7305 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7306 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7307 0ebf8283 2019-07-24 stsp err = unlockerr;
7308 0ebf8283 2019-07-24 stsp return err;
7309 0ebf8283 2019-07-24 stsp }
7310 0ebf8283 2019-07-24 stsp
7311 0ebf8283 2019-07-24 stsp const struct got_error *
7312 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7313 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7314 0ebf8283 2019-07-24 stsp {
7315 0ebf8283 2019-07-24 stsp const struct got_error *err;
7316 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7317 0ebf8283 2019-07-24 stsp
7318 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7319 0ebf8283 2019-07-24 stsp if (err)
7320 0ebf8283 2019-07-24 stsp return err;
7321 0ebf8283 2019-07-24 stsp
7322 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7323 0ebf8283 2019-07-24 stsp if (err)
7324 0ebf8283 2019-07-24 stsp goto done;
7325 0ebf8283 2019-07-24 stsp
7326 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7327 0ebf8283 2019-07-24 stsp done:
7328 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7329 2822a352 2019-10-15 stsp return err;
7330 2822a352 2019-10-15 stsp }
7331 2822a352 2019-10-15 stsp
7332 2822a352 2019-10-15 stsp const struct got_error *
7333 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7334 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7335 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7336 2822a352 2019-10-15 stsp struct got_repository *repo)
7337 2822a352 2019-10-15 stsp {
7338 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7339 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7340 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7341 2822a352 2019-10-15 stsp
7342 2822a352 2019-10-15 stsp *fileindex = NULL;
7343 2822a352 2019-10-15 stsp *branch_ref = NULL;
7344 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7345 2822a352 2019-10-15 stsp
7346 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7347 2822a352 2019-10-15 stsp if (err)
7348 2822a352 2019-10-15 stsp return err;
7349 2822a352 2019-10-15 stsp
7350 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7351 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7352 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7353 2822a352 2019-10-15 stsp "update -b or different branch name required");
7354 2822a352 2019-10-15 stsp goto done;
7355 2822a352 2019-10-15 stsp }
7356 2822a352 2019-10-15 stsp
7357 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7358 2822a352 2019-10-15 stsp if (err)
7359 2822a352 2019-10-15 stsp goto done;
7360 2822a352 2019-10-15 stsp
7361 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7362 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7363 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7364 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7365 2822a352 2019-10-15 stsp &ok_arg);
7366 2822a352 2019-10-15 stsp if (err)
7367 2822a352 2019-10-15 stsp goto done;
7368 2822a352 2019-10-15 stsp
7369 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7370 2822a352 2019-10-15 stsp if (err)
7371 2822a352 2019-10-15 stsp goto done;
7372 2822a352 2019-10-15 stsp
7373 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7374 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7375 2822a352 2019-10-15 stsp done:
7376 2822a352 2019-10-15 stsp if (err) {
7377 2822a352 2019-10-15 stsp if (*branch_ref) {
7378 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7379 2822a352 2019-10-15 stsp *branch_ref = NULL;
7380 2822a352 2019-10-15 stsp }
7381 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7382 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7383 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7384 2822a352 2019-10-15 stsp }
7385 2822a352 2019-10-15 stsp if (*fileindex) {
7386 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7387 2822a352 2019-10-15 stsp *fileindex = NULL;
7388 2822a352 2019-10-15 stsp }
7389 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7390 2822a352 2019-10-15 stsp }
7391 0cb83759 2019-08-03 stsp return err;
7392 0cb83759 2019-08-03 stsp }
7393 0cb83759 2019-08-03 stsp
7394 2822a352 2019-10-15 stsp const struct got_error *
7395 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7396 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7397 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7398 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7399 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7400 2822a352 2019-10-15 stsp {
7401 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7402 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7403 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7404 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7405 2822a352 2019-10-15 stsp
7406 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7407 2822a352 2019-10-15 stsp if (err)
7408 2822a352 2019-10-15 stsp goto done;
7409 2822a352 2019-10-15 stsp
7410 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7411 2822a352 2019-10-15 stsp if (err)
7412 2822a352 2019-10-15 stsp goto done;
7413 2822a352 2019-10-15 stsp
7414 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7415 945f9229 2022-04-16 thomas if (err)
7416 945f9229 2022-04-16 thomas goto done;
7417 945f9229 2022-04-16 thomas
7418 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7419 2822a352 2019-10-15 stsp worktree->path_prefix);
7420 2822a352 2019-10-15 stsp if (err)
7421 2822a352 2019-10-15 stsp goto done;
7422 2822a352 2019-10-15 stsp
7423 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7424 2822a352 2019-10-15 stsp if (err)
7425 2822a352 2019-10-15 stsp goto done;
7426 2822a352 2019-10-15 stsp
7427 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7428 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7429 2822a352 2019-10-15 stsp if (err)
7430 2822a352 2019-10-15 stsp goto sync;
7431 2822a352 2019-10-15 stsp
7432 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7433 2822a352 2019-10-15 stsp if (err)
7434 2822a352 2019-10-15 stsp goto sync;
7435 2822a352 2019-10-15 stsp
7436 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7437 6e210706 2021-01-22 stsp if (err)
7438 6e210706 2021-01-22 stsp goto sync;
7439 6e210706 2021-01-22 stsp
7440 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7441 2822a352 2019-10-15 stsp sync:
7442 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7443 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7444 2822a352 2019-10-15 stsp err = sync_err;
7445 2822a352 2019-10-15 stsp
7446 2822a352 2019-10-15 stsp done:
7447 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7448 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7449 2822a352 2019-10-15 stsp err = unlockerr;
7450 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7451 2822a352 2019-10-15 stsp
7452 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7453 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7454 2822a352 2019-10-15 stsp err = unlockerr;
7455 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7456 2822a352 2019-10-15 stsp
7457 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7458 2822a352 2019-10-15 stsp free(fileindex_path);
7459 2822a352 2019-10-15 stsp free(tree_id);
7460 945f9229 2022-04-16 thomas if (commit)
7461 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7462 2822a352 2019-10-15 stsp
7463 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7464 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7465 2822a352 2019-10-15 stsp err = unlockerr;
7466 2822a352 2019-10-15 stsp return err;
7467 2822a352 2019-10-15 stsp }
7468 2822a352 2019-10-15 stsp
7469 2822a352 2019-10-15 stsp const struct got_error *
7470 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7471 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7472 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7473 2822a352 2019-10-15 stsp {
7474 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7475 8b692cd0 2019-10-21 stsp
7476 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7477 8b692cd0 2019-10-21 stsp
7478 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7479 8b692cd0 2019-10-21 stsp
7480 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7481 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7482 8b692cd0 2019-10-21 stsp err = unlockerr;
7483 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7484 8b692cd0 2019-10-21 stsp
7485 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7486 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7487 8b692cd0 2019-10-21 stsp err = unlockerr;
7488 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7489 10604dce 2021-09-24 thomas
7490 10604dce 2021-09-24 thomas return err;
7491 10604dce 2021-09-24 thomas }
7492 10604dce 2021-09-24 thomas
7493 10604dce 2021-09-24 thomas const struct got_error *
7494 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7495 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7496 10604dce 2021-09-24 thomas {
7497 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7498 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7499 10604dce 2021-09-24 thomas
7500 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7501 10604dce 2021-09-24 thomas if (err)
7502 10604dce 2021-09-24 thomas goto done;
7503 8b692cd0 2019-10-21 stsp
7504 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7505 10604dce 2021-09-24 thomas
7506 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7507 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7508 10604dce 2021-09-24 thomas err = sync_err;
7509 10604dce 2021-09-24 thomas done:
7510 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7511 10604dce 2021-09-24 thomas free(fileindex_path);
7512 8b692cd0 2019-10-21 stsp return err;
7513 2822a352 2019-10-15 stsp }
7514 2822a352 2019-10-15 stsp
7515 10604dce 2021-09-24 thomas static const struct got_error *
7516 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7517 10604dce 2021-09-24 thomas {
7518 10604dce 2021-09-24 thomas const struct got_error *err;
7519 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7520 10604dce 2021-09-24 thomas
7521 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7522 10604dce 2021-09-24 thomas if (err)
7523 10604dce 2021-09-24 thomas goto done;
7524 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7525 10604dce 2021-09-24 thomas if (err)
7526 10604dce 2021-09-24 thomas goto done;
7527 10604dce 2021-09-24 thomas
7528 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7529 10604dce 2021-09-24 thomas if (err)
7530 10604dce 2021-09-24 thomas goto done;
7531 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7532 10604dce 2021-09-24 thomas if (err)
7533 10604dce 2021-09-24 thomas goto done;
7534 10604dce 2021-09-24 thomas
7535 10604dce 2021-09-24 thomas done:
7536 10604dce 2021-09-24 thomas free(branch_refname);
7537 10604dce 2021-09-24 thomas free(commit_refname);
7538 10604dce 2021-09-24 thomas return err;
7539 10604dce 2021-09-24 thomas }
7540 10604dce 2021-09-24 thomas
7541 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7542 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7543 10604dce 2021-09-24 thomas const char *branch_name;
7544 10604dce 2021-09-24 thomas };
7545 10604dce 2021-09-24 thomas
7546 10604dce 2021-09-24 thomas static const struct got_error *
7547 10604dce 2021-09-24 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7548 10604dce 2021-09-24 thomas void *arg)
7549 10604dce 2021-09-24 thomas {
7550 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7551 10604dce 2021-09-24 thomas
7552 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7553 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7554 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7555 10604dce 2021-09-24 thomas
7556 10604dce 2021-09-24 thomas return NULL;
7557 10604dce 2021-09-24 thomas }
7558 10604dce 2021-09-24 thomas
7559 10604dce 2021-09-24 thomas
7560 10604dce 2021-09-24 thomas const struct got_error *
7561 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7562 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7563 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7564 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7565 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7566 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7567 10604dce 2021-09-24 thomas {
7568 10604dce 2021-09-24 thomas const struct got_error *err;
7569 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7570 10604dce 2021-09-24 thomas
7571 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7572 10604dce 2021-09-24 thomas if (err)
7573 10604dce 2021-09-24 thomas goto done;
7574 10604dce 2021-09-24 thomas
7575 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7576 10604dce 2021-09-24 thomas worktree);
7577 10604dce 2021-09-24 thomas if (err)
7578 10604dce 2021-09-24 thomas goto done;
7579 10604dce 2021-09-24 thomas
7580 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7581 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7582 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7583 10604dce 2021-09-24 thomas done:
7584 10604dce 2021-09-24 thomas free(fileindex_path);
7585 10604dce 2021-09-24 thomas return err;
7586 10604dce 2021-09-24 thomas }
7587 10604dce 2021-09-24 thomas
7588 10604dce 2021-09-24 thomas const struct got_error *
7589 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7590 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7591 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7592 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7593 ae1e948a 2021-09-28 thomas struct got_repository *repo,
7594 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7595 ae1e948a 2021-09-28 thomas
7596 10604dce 2021-09-24 thomas {
7597 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7598 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7599 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7600 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7601 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7602 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7603 10604dce 2021-09-24 thomas int have_staged_files = 0;
7604 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7605 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7606 10604dce 2021-09-24 thomas
7607 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7608 10604dce 2021-09-24 thomas
7609 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7610 10604dce 2021-09-24 thomas
7611 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7612 10604dce 2021-09-24 thomas if (err)
7613 10604dce 2021-09-24 thomas goto done;
7614 10604dce 2021-09-24 thomas
7615 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7616 10604dce 2021-09-24 thomas if (err)
7617 10604dce 2021-09-24 thomas goto done;
7618 10604dce 2021-09-24 thomas
7619 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7620 10604dce 2021-09-24 thomas if (err)
7621 10604dce 2021-09-24 thomas goto done;
7622 10604dce 2021-09-24 thomas
7623 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7624 10604dce 2021-09-24 thomas &have_staged_files);
7625 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7626 10604dce 2021-09-24 thomas goto done;
7627 10604dce 2021-09-24 thomas if (have_staged_files) {
7628 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7629 10604dce 2021-09-24 thomas goto done;
7630 10604dce 2021-09-24 thomas }
7631 10604dce 2021-09-24 thomas
7632 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7633 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7634 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7635 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7636 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7637 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7638 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7639 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7640 10604dce 2021-09-24 thomas if (err)
7641 10604dce 2021-09-24 thomas goto done;
7642 10604dce 2021-09-24 thomas
7643 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7644 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7645 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7646 10604dce 2021-09-24 thomas goto done;
7647 10604dce 2021-09-24 thomas }
7648 10604dce 2021-09-24 thomas
7649 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7650 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7651 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7652 10604dce 2021-09-24 thomas
7653 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7654 10604dce 2021-09-24 thomas ct_path++;
7655 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7656 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7657 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7658 10604dce 2021-09-24 thomas if (err)
7659 10604dce 2021-09-24 thomas goto done;
7660 10604dce 2021-09-24 thomas
7661 10604dce 2021-09-24 thomas }
7662 10604dce 2021-09-24 thomas
7663 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7664 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7665 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7666 10604dce 2021-09-24 thomas head_commit_id, branch_tip, worktree, author, committer,
7667 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7668 10604dce 2021-09-24 thomas if (err)
7669 10604dce 2021-09-24 thomas goto done;
7670 10604dce 2021-09-24 thomas
7671 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7672 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7673 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7674 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7675 10604dce 2021-09-24 thomas err = sync_err;
7676 10604dce 2021-09-24 thomas done:
7677 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7678 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7679 10604dce 2021-09-24 thomas free_commitable(ct);
7680 10604dce 2021-09-24 thomas }
7681 10604dce 2021-09-24 thomas got_pathlist_free(&commitable_paths);
7682 10604dce 2021-09-24 thomas free(fileindex_path);
7683 10604dce 2021-09-24 thomas return err;
7684 10604dce 2021-09-24 thomas }
7685 10604dce 2021-09-24 thomas
7686 10604dce 2021-09-24 thomas const struct got_error *
7687 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
7688 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
7689 10604dce 2021-09-24 thomas {
7690 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7691 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7692 10604dce 2021-09-24 thomas
7693 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7694 10604dce 2021-09-24 thomas if (err)
7695 10604dce 2021-09-24 thomas goto done;
7696 10604dce 2021-09-24 thomas
7697 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7698 10604dce 2021-09-24 thomas if (err)
7699 10604dce 2021-09-24 thomas goto done;
7700 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7701 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7702 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7703 10604dce 2021-09-24 thomas err = sync_err;
7704 10604dce 2021-09-24 thomas done:
7705 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7706 10604dce 2021-09-24 thomas free(fileindex_path);
7707 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7708 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7709 10604dce 2021-09-24 thomas err = unlockerr;
7710 10604dce 2021-09-24 thomas return err;
7711 10604dce 2021-09-24 thomas }
7712 10604dce 2021-09-24 thomas
7713 10604dce 2021-09-24 thomas const struct got_error *
7714 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7715 10604dce 2021-09-24 thomas struct got_repository *repo)
7716 10604dce 2021-09-24 thomas {
7717 10604dce 2021-09-24 thomas const struct got_error *err;
7718 10604dce 2021-09-24 thomas char *branch_refname = NULL;
7719 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
7720 10604dce 2021-09-24 thomas
7721 10604dce 2021-09-24 thomas *in_progress = 0;
7722 10604dce 2021-09-24 thomas
7723 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7724 10604dce 2021-09-24 thomas if (err)
7725 10604dce 2021-09-24 thomas return err;
7726 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7727 dfe86d1f 2021-09-24 thomas free(branch_refname);
7728 10604dce 2021-09-24 thomas if (err) {
7729 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
7730 10604dce 2021-09-24 thomas return err;
7731 10604dce 2021-09-24 thomas } else
7732 10604dce 2021-09-24 thomas *in_progress = 1;
7733 10604dce 2021-09-24 thomas
7734 10604dce 2021-09-24 thomas return NULL;
7735 10604dce 2021-09-24 thomas }
7736 10604dce 2021-09-24 thomas
7737 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
7738 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
7739 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
7740 10604dce 2021-09-24 thomas {
7741 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
7742 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7743 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7744 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7745 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
7746 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7747 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
7748 10604dce 2021-09-24 thomas
7749 10604dce 2021-09-24 thomas *fileindex = NULL;
7750 10604dce 2021-09-24 thomas
7751 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7752 10604dce 2021-09-24 thomas if (err)
7753 10604dce 2021-09-24 thomas return err;
7754 10604dce 2021-09-24 thomas
7755 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7756 10604dce 2021-09-24 thomas if (err)
7757 10604dce 2021-09-24 thomas goto done;
7758 10604dce 2021-09-24 thomas
7759 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
7760 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
7761 10604dce 2021-09-24 thomas ok_arg.repo = repo;
7762 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7763 10604dce 2021-09-24 thomas &ok_arg);
7764 10604dce 2021-09-24 thomas if (err)
7765 10604dce 2021-09-24 thomas goto done;
7766 10604dce 2021-09-24 thomas
7767 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7768 10604dce 2021-09-24 thomas if (err)
7769 10604dce 2021-09-24 thomas return err;
7770 10604dce 2021-09-24 thomas
7771 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7772 10604dce 2021-09-24 thomas if (err)
7773 10604dce 2021-09-24 thomas return err;
7774 10604dce 2021-09-24 thomas
7775 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7776 10604dce 2021-09-24 thomas 0);
7777 10604dce 2021-09-24 thomas if (err)
7778 10604dce 2021-09-24 thomas goto done;
7779 10604dce 2021-09-24 thomas
7780 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7781 10604dce 2021-09-24 thomas if (err)
7782 10604dce 2021-09-24 thomas goto done;
7783 10604dce 2021-09-24 thomas
7784 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7785 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7786 10604dce 2021-09-24 thomas goto done;
7787 10604dce 2021-09-24 thomas }
7788 10604dce 2021-09-24 thomas
7789 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
7790 10604dce 2021-09-24 thomas if (err)
7791 10604dce 2021-09-24 thomas goto done;
7792 10604dce 2021-09-24 thomas
7793 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7794 10604dce 2021-09-24 thomas if (err)
7795 10604dce 2021-09-24 thomas goto done;
7796 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
7797 10604dce 2021-09-24 thomas if (err)
7798 10604dce 2021-09-24 thomas goto done;
7799 10604dce 2021-09-24 thomas
7800 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7801 10604dce 2021-09-24 thomas if (err)
7802 10604dce 2021-09-24 thomas goto done;
7803 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
7804 10604dce 2021-09-24 thomas if (err)
7805 10604dce 2021-09-24 thomas goto done;
7806 10604dce 2021-09-24 thomas
7807 10604dce 2021-09-24 thomas done:
7808 10604dce 2021-09-24 thomas free(branch_refname);
7809 10604dce 2021-09-24 thomas free(commit_refname);
7810 10604dce 2021-09-24 thomas free(fileindex_path);
7811 10604dce 2021-09-24 thomas if (branch_ref)
7812 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7813 10604dce 2021-09-24 thomas if (commit_ref)
7814 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7815 10604dce 2021-09-24 thomas if (wt_branch)
7816 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
7817 10604dce 2021-09-24 thomas free(wt_branch_tip);
7818 10604dce 2021-09-24 thomas if (err) {
7819 10604dce 2021-09-24 thomas if (*fileindex) {
7820 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7821 10604dce 2021-09-24 thomas *fileindex = NULL;
7822 10604dce 2021-09-24 thomas }
7823 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7824 10604dce 2021-09-24 thomas }
7825 10604dce 2021-09-24 thomas return err;
7826 10604dce 2021-09-24 thomas }
7827 10604dce 2021-09-24 thomas
7828 10604dce 2021-09-24 thomas const struct got_error *
7829 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
7830 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7831 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
7832 10604dce 2021-09-24 thomas {
7833 10604dce 2021-09-24 thomas const struct got_error *err;
7834 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
7835 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7836 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7837 10604dce 2021-09-24 thomas int have_staged_files = 0;
7838 10604dce 2021-09-24 thomas
7839 10604dce 2021-09-24 thomas *branch_name = NULL;
7840 10604dce 2021-09-24 thomas *branch_tip = NULL;
7841 10604dce 2021-09-24 thomas *fileindex = NULL;
7842 10604dce 2021-09-24 thomas
7843 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7844 10604dce 2021-09-24 thomas if (err)
7845 10604dce 2021-09-24 thomas return err;
7846 10604dce 2021-09-24 thomas
7847 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7848 10604dce 2021-09-24 thomas if (err)
7849 10604dce 2021-09-24 thomas goto done;
7850 10604dce 2021-09-24 thomas
7851 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7852 10604dce 2021-09-24 thomas &have_staged_files);
7853 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7854 10604dce 2021-09-24 thomas goto done;
7855 10604dce 2021-09-24 thomas if (have_staged_files) {
7856 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
7857 10604dce 2021-09-24 thomas goto done;
7858 10604dce 2021-09-24 thomas }
7859 10604dce 2021-09-24 thomas
7860 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7861 10604dce 2021-09-24 thomas if (err)
7862 10604dce 2021-09-24 thomas goto done;
7863 10604dce 2021-09-24 thomas
7864 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7865 10604dce 2021-09-24 thomas if (err)
7866 10604dce 2021-09-24 thomas goto done;
7867 10604dce 2021-09-24 thomas
7868 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7869 10604dce 2021-09-24 thomas if (err)
7870 10604dce 2021-09-24 thomas goto done;
7871 10604dce 2021-09-24 thomas
7872 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
7873 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7874 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
7875 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
7876 10604dce 2021-09-24 thomas goto done;
7877 10604dce 2021-09-24 thomas }
7878 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7879 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
7880 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
7881 10604dce 2021-09-24 thomas goto done;
7882 10604dce 2021-09-24 thomas }
7883 10604dce 2021-09-24 thomas
7884 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
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 = got_ref_resolve(branch_tip, repo, commit_ref);
7889 10604dce 2021-09-24 thomas if (err)
7890 10604dce 2021-09-24 thomas goto done;
7891 10604dce 2021-09-24 thomas done:
7892 10604dce 2021-09-24 thomas free(commit_refname);
7893 10604dce 2021-09-24 thomas free(branch_refname);
7894 10604dce 2021-09-24 thomas free(fileindex_path);
7895 10604dce 2021-09-24 thomas if (commit_ref)
7896 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7897 10604dce 2021-09-24 thomas if (branch_ref)
7898 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7899 10604dce 2021-09-24 thomas if (err) {
7900 10604dce 2021-09-24 thomas if (*branch_name) {
7901 10604dce 2021-09-24 thomas free(*branch_name);
7902 10604dce 2021-09-24 thomas *branch_name = NULL;
7903 10604dce 2021-09-24 thomas }
7904 10604dce 2021-09-24 thomas free(*branch_tip);
7905 10604dce 2021-09-24 thomas *branch_tip = NULL;
7906 10604dce 2021-09-24 thomas if (*fileindex) {
7907 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7908 10604dce 2021-09-24 thomas *fileindex = NULL;
7909 10604dce 2021-09-24 thomas }
7910 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7911 10604dce 2021-09-24 thomas }
7912 10604dce 2021-09-24 thomas return err;
7913 10604dce 2021-09-24 thomas }
7914 10604dce 2021-09-24 thomas
7915 10604dce 2021-09-24 thomas const struct got_error *
7916 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
7917 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
7918 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
7919 10604dce 2021-09-24 thomas {
7920 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7921 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
7922 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7923 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7924 10604dce 2021-09-24 thomas struct revert_file_args rfa;
7925 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
7926 10604dce 2021-09-24 thomas
7927 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7928 945f9229 2022-04-16 thomas worktree->base_commit_id);
7929 945f9229 2022-04-16 thomas if (err)
7930 945f9229 2022-04-16 thomas goto done;
7931 945f9229 2022-04-16 thomas
7932 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7933 945f9229 2022-04-16 thomas worktree->path_prefix);
7934 10604dce 2021-09-24 thomas if (err)
7935 10604dce 2021-09-24 thomas goto done;
7936 10604dce 2021-09-24 thomas
7937 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7938 10604dce 2021-09-24 thomas if (err)
7939 10604dce 2021-09-24 thomas goto done;
7940 10604dce 2021-09-24 thomas
7941 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7942 10604dce 2021-09-24 thomas if (err)
7943 10604dce 2021-09-24 thomas goto done;
7944 10604dce 2021-09-24 thomas
7945 10604dce 2021-09-24 thomas rfa.worktree = worktree;
7946 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
7947 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
7948 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
7949 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
7950 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
7951 10604dce 2021-09-24 thomas rfa.repo = repo;
7952 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
7953 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7954 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7955 10604dce 2021-09-24 thomas if (err)
7956 10604dce 2021-09-24 thomas goto sync;
7957 10604dce 2021-09-24 thomas
7958 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7959 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
7960 10604dce 2021-09-24 thomas sync:
7961 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7962 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7963 10604dce 2021-09-24 thomas err = sync_err;
7964 10604dce 2021-09-24 thomas done:
7965 10604dce 2021-09-24 thomas free(tree_id);
7966 10604dce 2021-09-24 thomas free(commit_id);
7967 945f9229 2022-04-16 thomas if (commit)
7968 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7969 10604dce 2021-09-24 thomas if (fileindex)
7970 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7971 10604dce 2021-09-24 thomas free(fileindex_path);
7972 10604dce 2021-09-24 thomas
7973 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7974 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7975 10604dce 2021-09-24 thomas err = unlockerr;
7976 10604dce 2021-09-24 thomas return err;
7977 10604dce 2021-09-24 thomas }
7978 10604dce 2021-09-24 thomas
7979 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7980 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7981 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7982 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7983 2db2652d 2019-08-07 stsp struct got_repository *repo;
7984 2db2652d 2019-08-07 stsp int have_changes;
7985 2db2652d 2019-08-07 stsp };
7986 2db2652d 2019-08-07 stsp
7987 ef20f542 2022-06-26 thomas static const struct got_error *
7988 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7989 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7990 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7991 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7992 735ef5ac 2019-08-03 stsp {
7993 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7994 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7995 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7996 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7997 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7998 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7999 8b13ce36 2019-08-08 stsp
8000 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8001 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8002 8b13ce36 2019-08-08 stsp return NULL;
8003 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8004 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8005 735ef5ac 2019-08-03 stsp
8006 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8007 735ef5ac 2019-08-03 stsp if (ie == NULL)
8008 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8009 735ef5ac 2019-08-03 stsp
8010 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8011 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8012 735ef5ac 2019-08-03 stsp relpath) == -1)
8013 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8014 735ef5ac 2019-08-03 stsp
8015 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8016 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
8017 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8018 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
8019 735ef5ac 2019-08-03 stsp }
8020 735ef5ac 2019-08-03 stsp
8021 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8022 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8023 3aa5969e 2019-08-06 stsp goto done;
8024 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8025 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8026 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8027 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8028 735ef5ac 2019-08-03 stsp goto done;
8029 3aa5969e 2019-08-06 stsp }
8030 735ef5ac 2019-08-03 stsp
8031 2db2652d 2019-08-07 stsp a->have_changes = 1;
8032 2db2652d 2019-08-07 stsp
8033 735ef5ac 2019-08-03 stsp p = in_repo_path;
8034 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8035 735ef5ac 2019-08-03 stsp p++;
8036 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8037 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8038 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8039 735ef5ac 2019-08-03 stsp done:
8040 735ef5ac 2019-08-03 stsp free(in_repo_path);
8041 dc424a06 2019-08-07 stsp return err;
8042 dc424a06 2019-08-07 stsp }
8043 dc424a06 2019-08-07 stsp
8044 2db2652d 2019-08-07 stsp struct stage_path_arg {
8045 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8046 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8047 2db2652d 2019-08-07 stsp struct got_repository *repo;
8048 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8049 2db2652d 2019-08-07 stsp void *status_arg;
8050 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8051 2db2652d 2019-08-07 stsp void *patch_arg;
8052 7b5dc508 2019-10-28 stsp int staged_something;
8053 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8054 2db2652d 2019-08-07 stsp };
8055 2db2652d 2019-08-07 stsp
8056 2db2652d 2019-08-07 stsp static const struct got_error *
8057 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8058 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8059 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8060 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8061 0cb83759 2019-08-03 stsp {
8062 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8063 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8064 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8065 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8066 0cb83759 2019-08-03 stsp uint32_t stage;
8067 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8068 0aeb8099 2020-07-23 stsp struct stat sb;
8069 8b13ce36 2019-08-08 stsp
8070 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8071 8b13ce36 2019-08-08 stsp return NULL;
8072 0cb83759 2019-08-03 stsp
8073 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8074 d3e7c587 2019-08-03 stsp if (ie == NULL)
8075 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8076 0cb83759 2019-08-03 stsp
8077 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8078 2db2652d 2019-08-07 stsp relpath)== -1)
8079 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8080 0cb83759 2019-08-03 stsp
8081 0cb83759 2019-08-03 stsp switch (status) {
8082 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8083 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8084 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8085 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8086 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8087 0aeb8099 2020-07-23 stsp break;
8088 0aeb8099 2020-07-23 stsp }
8089 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8090 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8091 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8092 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8093 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8094 dc424a06 2019-08-07 stsp if (err)
8095 dc424a06 2019-08-07 stsp break;
8096 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8097 dc424a06 2019-08-07 stsp break;
8098 dc424a06 2019-08-07 stsp } else {
8099 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8100 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8101 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8102 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8103 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8104 dc424a06 2019-08-07 stsp break;
8105 dc424a06 2019-08-07 stsp }
8106 dc424a06 2019-08-07 stsp }
8107 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8108 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8109 0cb83759 2019-08-03 stsp if (err)
8110 dc424a06 2019-08-07 stsp break;
8111 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8112 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8113 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8114 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8115 0cb83759 2019-08-03 stsp else
8116 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8117 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8118 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8119 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8120 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8121 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8122 35213c7c 2020-07-23 stsp ssize_t target_len;
8123 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8124 35213c7c 2020-07-23 stsp sizeof(target_path));
8125 35213c7c 2020-07-23 stsp if (target_len == -1) {
8126 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8127 35213c7c 2020-07-23 stsp ondisk_path);
8128 35213c7c 2020-07-23 stsp break;
8129 35213c7c 2020-07-23 stsp }
8130 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8131 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8132 35213c7c 2020-07-23 stsp a->worktree->root_path);
8133 35213c7c 2020-07-23 stsp if (err)
8134 35213c7c 2020-07-23 stsp break;
8135 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8136 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8137 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8138 35213c7c 2020-07-23 stsp break;
8139 35213c7c 2020-07-23 stsp }
8140 35213c7c 2020-07-23 stsp }
8141 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8142 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8143 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8144 35213c7c 2020-07-23 stsp else
8145 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8146 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8147 0aeb8099 2020-07-23 stsp } else {
8148 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8149 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8150 0aeb8099 2020-07-23 stsp }
8151 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8152 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8153 dc424a06 2019-08-07 stsp break;
8154 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8155 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8156 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8157 f289c82c 2022-06-13 thomas if (err)
8158 f289c82c 2022-06-13 thomas break;
8159 f289c82c 2022-06-13 thomas /*
8160 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8161 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8162 f289c82c 2022-06-13 thomas */
8163 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8164 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8165 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8166 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8167 f289c82c 2022-06-13 thomas }
8168 0cb83759 2019-08-03 stsp break;
8169 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8170 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8171 d3e7c587 2019-08-03 stsp break;
8172 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8173 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8174 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8175 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8176 dc424a06 2019-08-07 stsp if (err)
8177 dc424a06 2019-08-07 stsp break;
8178 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8179 88f33a19 2019-08-08 stsp break;
8180 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8181 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8182 dc424a06 2019-08-07 stsp break;
8183 88f33a19 2019-08-08 stsp }
8184 dc424a06 2019-08-07 stsp }
8185 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8186 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8187 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8188 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8189 dc424a06 2019-08-07 stsp break;
8190 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8191 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8192 12463d8b 2019-12-13 stsp de_name);
8193 0cb83759 2019-08-03 stsp break;
8194 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8195 d3e7c587 2019-08-03 stsp break;
8196 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8197 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8198 ebf48fd5 2019-08-03 stsp break;
8199 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8200 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8201 2a06fe5f 2019-08-24 stsp break;
8202 0cb83759 2019-08-03 stsp default:
8203 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8204 537ac44b 2019-08-03 stsp break;
8205 0cb83759 2019-08-03 stsp }
8206 dc424a06 2019-08-07 stsp
8207 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8208 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8209 dc424a06 2019-08-07 stsp free(path_content);
8210 2db2652d 2019-08-07 stsp free(ondisk_path);
8211 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8212 0ebf8283 2019-07-24 stsp return err;
8213 0ebf8283 2019-07-24 stsp }
8214 0cb83759 2019-08-03 stsp
8215 0cb83759 2019-08-03 stsp const struct got_error *
8216 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8217 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8218 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8219 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8220 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8221 0cb83759 2019-08-03 stsp {
8222 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8223 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8224 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8225 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8226 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8227 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8228 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8229 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8230 0cb83759 2019-08-03 stsp
8231 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8232 0cb83759 2019-08-03 stsp if (err)
8233 0cb83759 2019-08-03 stsp return err;
8234 0cb83759 2019-08-03 stsp
8235 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8236 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8237 735ef5ac 2019-08-03 stsp if (err)
8238 735ef5ac 2019-08-03 stsp goto done;
8239 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8240 735ef5ac 2019-08-03 stsp if (err)
8241 735ef5ac 2019-08-03 stsp goto done;
8242 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8243 0cb83759 2019-08-03 stsp if (err)
8244 0cb83759 2019-08-03 stsp goto done;
8245 0cb83759 2019-08-03 stsp
8246 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8247 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8248 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8249 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8250 2db2652d 2019-08-07 stsp oka.repo = repo;
8251 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8252 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8253 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8254 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8255 735ef5ac 2019-08-03 stsp if (err)
8256 735ef5ac 2019-08-03 stsp goto done;
8257 735ef5ac 2019-08-03 stsp }
8258 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8259 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8260 2db2652d 2019-08-07 stsp goto done;
8261 2db2652d 2019-08-07 stsp }
8262 735ef5ac 2019-08-03 stsp
8263 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8264 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8265 2db2652d 2019-08-07 stsp spa.repo = repo;
8266 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8267 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8268 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8269 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8270 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8271 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8272 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8273 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8274 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8275 42005733 2019-08-03 stsp if (err)
8276 2db2652d 2019-08-07 stsp goto done;
8277 7b5dc508 2019-10-28 stsp }
8278 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8279 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8280 7b5dc508 2019-10-28 stsp goto done;
8281 0cb83759 2019-08-03 stsp }
8282 0cb83759 2019-08-03 stsp
8283 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8284 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8285 0cb83759 2019-08-03 stsp err = sync_err;
8286 0cb83759 2019-08-03 stsp done:
8287 735ef5ac 2019-08-03 stsp if (head_ref)
8288 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8289 735ef5ac 2019-08-03 stsp free(head_commit_id);
8290 0cb83759 2019-08-03 stsp free(fileindex_path);
8291 0cb83759 2019-08-03 stsp if (fileindex)
8292 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8293 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8294 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8295 0cb83759 2019-08-03 stsp err = unlockerr;
8296 0cb83759 2019-08-03 stsp return err;
8297 0cb83759 2019-08-03 stsp }
8298 ad493afc 2019-08-03 stsp
8299 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8300 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8301 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8302 ad493afc 2019-08-03 stsp struct got_repository *repo;
8303 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8304 ad493afc 2019-08-03 stsp void *progress_arg;
8305 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8306 2e1f37b0 2019-08-08 stsp void *patch_arg;
8307 ad493afc 2019-08-03 stsp };
8308 2e1f37b0 2019-08-08 stsp
8309 2e1f37b0 2019-08-08 stsp static const struct got_error *
8310 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8311 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8312 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8313 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8314 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8315 2e1f37b0 2019-08-08 stsp {
8316 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8317 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8318 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8319 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8320 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8321 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8322 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8323 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8324 2e1f37b0 2019-08-08 stsp
8325 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8326 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8327 2e1f37b0 2019-08-08 stsp
8328 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8329 2e1f37b0 2019-08-08 stsp if (err)
8330 2e1f37b0 2019-08-08 stsp return err;
8331 f4ae6ddb 2022-07-01 thomas
8332 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8333 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8334 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8335 f4ae6ddb 2022-07-01 thomas goto done;
8336 f4ae6ddb 2022-07-01 thomas }
8337 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8338 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8339 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8340 f4ae6ddb 2022-07-01 thomas goto done;
8341 f4ae6ddb 2022-07-01 thomas }
8342 f4ae6ddb 2022-07-01 thomas
8343 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8344 2e1f37b0 2019-08-08 stsp if (err)
8345 2e1f37b0 2019-08-08 stsp goto done;
8346 2e1f37b0 2019-08-08 stsp
8347 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8348 2e1f37b0 2019-08-08 stsp if (err)
8349 2e1f37b0 2019-08-08 stsp goto done;
8350 2e1f37b0 2019-08-08 stsp
8351 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8352 2e1f37b0 2019-08-08 stsp if (err)
8353 2e1f37b0 2019-08-08 stsp goto done;
8354 2e1f37b0 2019-08-08 stsp
8355 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8356 f4ae6ddb 2022-07-01 thomas fd2);
8357 2e1f37b0 2019-08-08 stsp if (err)
8358 2e1f37b0 2019-08-08 stsp goto done;
8359 2e1f37b0 2019-08-08 stsp
8360 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8361 2e1f37b0 2019-08-08 stsp if (err)
8362 2e1f37b0 2019-08-08 stsp goto done;
8363 ad493afc 2019-08-03 stsp
8364 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8365 2e1f37b0 2019-08-08 stsp if (err)
8366 2e1f37b0 2019-08-08 stsp goto done;
8367 2e1f37b0 2019-08-08 stsp
8368 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8369 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8370 2e1f37b0 2019-08-08 stsp if (err)
8371 2e1f37b0 2019-08-08 stsp goto done;
8372 2e1f37b0 2019-08-08 stsp
8373 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8374 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8375 2e1f37b0 2019-08-08 stsp if (err)
8376 2e1f37b0 2019-08-08 stsp goto done;
8377 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8378 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8379 2e1f37b0 2019-08-08 stsp if (err)
8380 2e1f37b0 2019-08-08 stsp goto done;
8381 2e1f37b0 2019-08-08 stsp
8382 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8383 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8384 2e1f37b0 2019-08-08 stsp goto done;
8385 2e1f37b0 2019-08-08 stsp }
8386 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8387 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8388 2e1f37b0 2019-08-08 stsp goto done;
8389 2e1f37b0 2019-08-08 stsp }
8390 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8391 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8392 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8393 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8394 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8395 fe621944 2020-11-10 stsp nchanges++;
8396 fe621944 2020-11-10 stsp }
8397 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8398 2e1f37b0 2019-08-08 stsp int choice;
8399 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8400 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8401 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8402 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8403 2e1f37b0 2019-08-08 stsp if (err)
8404 2e1f37b0 2019-08-08 stsp goto done;
8405 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8406 2e1f37b0 2019-08-08 stsp have_content = 1;
8407 19e4b907 2019-08-08 stsp else
8408 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8409 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8410 2e1f37b0 2019-08-08 stsp break;
8411 2e1f37b0 2019-08-08 stsp }
8412 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8413 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8414 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8415 2e1f37b0 2019-08-08 stsp done:
8416 2e1f37b0 2019-08-08 stsp free(label1);
8417 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8418 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8419 2e1f37b0 2019-08-08 stsp if (blob)
8420 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8421 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8422 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8423 2e1f37b0 2019-08-08 stsp if (staged_blob)
8424 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8425 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8426 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8427 fe621944 2020-11-10 stsp err = free_err;
8428 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8429 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8430 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8431 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8432 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8433 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8434 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8435 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8436 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8437 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8438 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8439 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8440 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8441 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8442 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8443 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8444 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8445 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8446 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8447 2e1f37b0 2019-08-08 stsp }
8448 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8449 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8450 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8451 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8452 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8453 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8454 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8455 2e1f37b0 2019-08-08 stsp }
8456 2e1f37b0 2019-08-08 stsp free(path1);
8457 2e1f37b0 2019-08-08 stsp free(path2);
8458 fda8017d 2020-07-23 stsp return err;
8459 fda8017d 2020-07-23 stsp }
8460 fda8017d 2020-07-23 stsp
8461 fda8017d 2020-07-23 stsp static const struct got_error *
8462 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8463 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8464 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8465 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8466 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8467 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8468 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8469 fda8017d 2020-07-23 stsp {
8470 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8471 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8472 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8473 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8474 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8475 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8476 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8477 fda8017d 2020-07-23 stsp struct stat sb;
8478 fda8017d 2020-07-23 stsp
8479 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8480 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8481 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8482 fda8017d 2020-07-23 stsp if (err)
8483 fda8017d 2020-07-23 stsp return err;
8484 fda8017d 2020-07-23 stsp
8485 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8486 fda8017d 2020-07-23 stsp return NULL;
8487 fda8017d 2020-07-23 stsp
8488 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8489 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8490 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8491 fda8017d 2020-07-23 stsp if (err)
8492 fda8017d 2020-07-23 stsp goto done;
8493 fda8017d 2020-07-23 stsp }
8494 fda8017d 2020-07-23 stsp
8495 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8496 fda8017d 2020-07-23 stsp if (f == NULL) {
8497 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8498 fda8017d 2020-07-23 stsp path_unstaged_content);
8499 fda8017d 2020-07-23 stsp goto done;
8500 fda8017d 2020-07-23 stsp }
8501 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8502 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8503 fda8017d 2020-07-23 stsp goto done;
8504 fda8017d 2020-07-23 stsp }
8505 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8506 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8507 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8508 fda8017d 2020-07-23 stsp size_t r;
8509 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8510 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8511 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8512 fda8017d 2020-07-23 stsp goto done;
8513 fda8017d 2020-07-23 stsp }
8514 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8515 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8516 fda8017d 2020-07-23 stsp goto done;
8517 fda8017d 2020-07-23 stsp }
8518 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8519 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8520 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8521 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8522 fda8017d 2020-07-23 stsp progress_arg);
8523 fda8017d 2020-07-23 stsp } else {
8524 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8525 67a66647 2021-05-31 stsp
8526 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8527 67a66647 2021-05-31 stsp if (err)
8528 67a66647 2021-05-31 stsp return err;
8529 67a66647 2021-05-31 stsp
8530 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8531 67a66647 2021-05-31 stsp parent) == -1) {
8532 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8533 67a66647 2021-05-31 stsp base_path = NULL;
8534 67a66647 2021-05-31 stsp goto done;
8535 67a66647 2021-05-31 stsp }
8536 67a66647 2021-05-31 stsp
8537 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8538 67a66647 2021-05-31 stsp if (err)
8539 67a66647 2021-05-31 stsp goto done;
8540 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8541 67a66647 2021-05-31 stsp blob_base);
8542 67a66647 2021-05-31 stsp if (err)
8543 67a66647 2021-05-31 stsp goto done;
8544 67a66647 2021-05-31 stsp
8545 b6b86fd1 2022-08-30 thomas /*
8546 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8547 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8548 eec2f5a9 2021-06-03 stsp */
8549 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8550 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8551 eec2f5a9 2021-06-03 stsp ondisk_path);
8552 eec2f5a9 2021-06-03 stsp if (err)
8553 eec2f5a9 2021-06-03 stsp goto done;
8554 eec2f5a9 2021-06-03 stsp } else {
8555 eec2f5a9 2021-06-03 stsp int fd;
8556 06340621 2021-12-31 thomas fd = open(ondisk_path,
8557 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8558 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8559 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8560 eec2f5a9 2021-06-03 stsp goto done;
8561 eec2f5a9 2021-06-03 stsp }
8562 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8563 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8564 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8565 eec2f5a9 2021-06-03 stsp close(fd);
8566 eec2f5a9 2021-06-03 stsp goto done;
8567 eec2f5a9 2021-06-03 stsp }
8568 eec2f5a9 2021-06-03 stsp }
8569 eec2f5a9 2021-06-03 stsp
8570 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8571 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8572 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8573 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8574 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8575 fda8017d 2020-07-23 stsp }
8576 fda8017d 2020-07-23 stsp if (err)
8577 fda8017d 2020-07-23 stsp goto done;
8578 fda8017d 2020-07-23 stsp
8579 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8580 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8581 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8582 2ac8aa02 2020-11-09 stsp } else {
8583 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8584 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8585 2ac8aa02 2020-11-09 stsp }
8586 fda8017d 2020-07-23 stsp done:
8587 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8588 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8589 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8590 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8591 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8592 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8593 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8594 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8595 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8596 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8597 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8598 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8599 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8600 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8601 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8602 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8603 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8604 67a66647 2021-05-31 stsp free(blob_base_path);
8605 67a66647 2021-05-31 stsp free(parent);
8606 67a66647 2021-05-31 stsp free(base_path);
8607 2e1f37b0 2019-08-08 stsp return err;
8608 2e1f37b0 2019-08-08 stsp }
8609 2e1f37b0 2019-08-08 stsp
8610 ad493afc 2019-08-03 stsp static const struct got_error *
8611 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8612 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8613 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8614 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8615 ad493afc 2019-08-03 stsp {
8616 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8617 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8618 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8619 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8620 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8621 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8622 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8623 9bc94a15 2019-08-03 stsp struct stat sb;
8624 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8625 ad493afc 2019-08-03 stsp
8626 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8627 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8628 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8629 2e1f37b0 2019-08-08 stsp return NULL;
8630 2e1f37b0 2019-08-08 stsp
8631 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8632 ad493afc 2019-08-03 stsp if (ie == NULL)
8633 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8634 9bc94a15 2019-08-03 stsp
8635 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8636 9bc94a15 2019-08-03 stsp == -1)
8637 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8638 ad493afc 2019-08-03 stsp
8639 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8640 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8641 f69721c3 2019-10-21 stsp if (err)
8642 f69721c3 2019-10-21 stsp goto done;
8643 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8644 f69721c3 2019-10-21 stsp id_str) == -1) {
8645 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8646 f4ae6ddb 2022-07-01 thomas goto done;
8647 f4ae6ddb 2022-07-01 thomas }
8648 f4ae6ddb 2022-07-01 thomas
8649 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8650 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8651 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8652 f4ae6ddb 2022-07-01 thomas goto done;
8653 f4ae6ddb 2022-07-01 thomas }
8654 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8655 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8656 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8657 f69721c3 2019-10-21 stsp goto done;
8658 f69721c3 2019-10-21 stsp }
8659 f69721c3 2019-10-21 stsp
8660 ad493afc 2019-08-03 stsp switch (staged_status) {
8661 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8662 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8663 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
8664 ad493afc 2019-08-03 stsp if (err)
8665 ad493afc 2019-08-03 stsp break;
8666 ad493afc 2019-08-03 stsp /* fall through */
8667 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8668 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8669 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8670 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8671 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8672 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8673 2e1f37b0 2019-08-08 stsp if (err)
8674 2e1f37b0 2019-08-08 stsp break;
8675 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8676 2e1f37b0 2019-08-08 stsp break;
8677 2e1f37b0 2019-08-08 stsp } else {
8678 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8679 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8680 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8681 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8682 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8683 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8684 2e1f37b0 2019-08-08 stsp }
8685 2e1f37b0 2019-08-08 stsp }
8686 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8687 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
8688 ad493afc 2019-08-03 stsp if (err)
8689 ad493afc 2019-08-03 stsp break;
8690 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8691 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8692 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8693 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8694 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8695 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8696 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8697 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8698 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8699 ea7786be 2020-07-23 stsp break;
8700 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8701 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8702 36bf999c 2020-07-23 stsp char *staged_target;
8703 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8704 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8705 36bf999c 2020-07-23 stsp if (err)
8706 36bf999c 2020-07-23 stsp goto done;
8707 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8708 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8709 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8710 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8711 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8712 36bf999c 2020-07-23 stsp free(staged_target);
8713 dfe9fba0 2020-07-23 stsp } else {
8714 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8715 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8716 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8717 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8718 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8719 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8720 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8721 dfe9fba0 2020-07-23 stsp }
8722 ea7786be 2020-07-23 stsp break;
8723 ea7786be 2020-07-23 stsp default:
8724 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8725 ea7786be 2020-07-23 stsp break;
8726 ea7786be 2020-07-23 stsp }
8727 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8728 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8729 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8730 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8731 2ac8aa02 2020-11-09 stsp }
8732 ad493afc 2019-08-03 stsp break;
8733 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8734 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8735 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8736 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8737 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8738 2e1f37b0 2019-08-08 stsp if (err)
8739 2e1f37b0 2019-08-08 stsp break;
8740 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8741 2e1f37b0 2019-08-08 stsp break;
8742 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8743 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8744 2e1f37b0 2019-08-08 stsp break;
8745 2e1f37b0 2019-08-08 stsp }
8746 2e1f37b0 2019-08-08 stsp }
8747 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8748 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8749 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8750 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8751 9bc94a15 2019-08-03 stsp if (err)
8752 9bc94a15 2019-08-03 stsp break;
8753 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8754 ad493afc 2019-08-03 stsp break;
8755 ad493afc 2019-08-03 stsp }
8756 f69721c3 2019-10-21 stsp done:
8757 ad493afc 2019-08-03 stsp free(ondisk_path);
8758 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8759 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8760 ad493afc 2019-08-03 stsp if (blob_base)
8761 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8762 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8763 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8764 ad493afc 2019-08-03 stsp if (blob_staged)
8765 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8766 f69721c3 2019-10-21 stsp free(id_str);
8767 f69721c3 2019-10-21 stsp free(label_orig);
8768 ad493afc 2019-08-03 stsp return err;
8769 ad493afc 2019-08-03 stsp }
8770 ad493afc 2019-08-03 stsp
8771 ad493afc 2019-08-03 stsp const struct got_error *
8772 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8773 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8774 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8775 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8776 ad493afc 2019-08-03 stsp struct got_repository *repo)
8777 ad493afc 2019-08-03 stsp {
8778 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8779 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8780 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8781 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8782 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8783 ad493afc 2019-08-03 stsp
8784 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8785 ad493afc 2019-08-03 stsp if (err)
8786 ad493afc 2019-08-03 stsp return err;
8787 ad493afc 2019-08-03 stsp
8788 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8789 ad493afc 2019-08-03 stsp if (err)
8790 ad493afc 2019-08-03 stsp goto done;
8791 ad493afc 2019-08-03 stsp
8792 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8793 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8794 ad493afc 2019-08-03 stsp upa.repo = repo;
8795 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8796 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8797 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8798 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8799 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8800 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8801 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
8802 ad493afc 2019-08-03 stsp if (err)
8803 ad493afc 2019-08-03 stsp goto done;
8804 ad493afc 2019-08-03 stsp }
8805 ad493afc 2019-08-03 stsp
8806 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8807 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8808 ad493afc 2019-08-03 stsp err = sync_err;
8809 ad493afc 2019-08-03 stsp done:
8810 ad493afc 2019-08-03 stsp free(fileindex_path);
8811 ad493afc 2019-08-03 stsp if (fileindex)
8812 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8813 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8814 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8815 ad493afc 2019-08-03 stsp err = unlockerr;
8816 ad493afc 2019-08-03 stsp return err;
8817 ad493afc 2019-08-03 stsp }
8818 b2118c49 2020-07-28 stsp
8819 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8820 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8821 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8822 b2118c49 2020-07-28 stsp void *info_arg;
8823 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8824 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8825 b2118c49 2020-07-28 stsp void *cancel_arg;
8826 b2118c49 2020-07-28 stsp };
8827 b2118c49 2020-07-28 stsp
8828 b2118c49 2020-07-28 stsp static const struct got_error *
8829 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8830 b2118c49 2020-07-28 stsp {
8831 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8832 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8833 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8834 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8835 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8836 b2118c49 2020-07-28 stsp int stage;
8837 b2118c49 2020-07-28 stsp
8838 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8839 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8840 b2118c49 2020-07-28 stsp
8841 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8842 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8843 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8844 b2118c49 2020-07-28 stsp break;
8845 b2118c49 2020-07-28 stsp }
8846 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8847 b2118c49 2020-07-28 stsp return NULL;
8848 b2118c49 2020-07-28 stsp
8849 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8850 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8851 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8852 b2118c49 2020-07-28 stsp }
8853 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8854 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8855 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8856 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8857 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8858 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8859 b2118c49 2020-07-28 stsp }
8860 b2118c49 2020-07-28 stsp
8861 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8862 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8863 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8864 b2118c49 2020-07-28 stsp }
8865 b2118c49 2020-07-28 stsp
8866 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8867 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8868 b2118c49 2020-07-28 stsp }
8869 b2118c49 2020-07-28 stsp
8870 b2118c49 2020-07-28 stsp const struct got_error *
8871 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8872 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8873 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8874 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8875 b2118c49 2020-07-28 stsp
8876 b2118c49 2020-07-28 stsp {
8877 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8878 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8879 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8880 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8881 b2118c49 2020-07-28 stsp
8882 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8883 b2118c49 2020-07-28 stsp if (err)
8884 b2118c49 2020-07-28 stsp return err;
8885 b2118c49 2020-07-28 stsp
8886 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8887 b2118c49 2020-07-28 stsp if (err)
8888 b2118c49 2020-07-28 stsp goto done;
8889 b2118c49 2020-07-28 stsp
8890 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8891 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8892 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8893 b2118c49 2020-07-28 stsp arg.paths = paths;
8894 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8895 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8896 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8897 b2118c49 2020-07-28 stsp &arg);
8898 b2118c49 2020-07-28 stsp done:
8899 b2118c49 2020-07-28 stsp free(fileindex_path);
8900 b2118c49 2020-07-28 stsp if (fileindex)
8901 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8902 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8903 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8904 b2118c49 2020-07-28 stsp err = unlockerr;
8905 b2118c49 2020-07-28 stsp return err;
8906 b2118c49 2020-07-28 stsp }
8907 814624e7 2022-03-22 thomas
8908 814624e7 2022-03-22 thomas static const struct got_error *
8909 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
8910 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
8911 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
8912 814624e7 2022-03-22 thomas {
8913 814624e7 2022-03-22 thomas const struct got_error *err;
8914 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
8915 814624e7 2022-03-22 thomas struct stat sb;
8916 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
8917 814624e7 2022-03-22 thomas
8918 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
8919 814624e7 2022-03-22 thomas if (err)
8920 814624e7 2022-03-22 thomas return err;
8921 814624e7 2022-03-22 thomas
8922 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8923 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
8924 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
8925 814624e7 2022-03-22 thomas
8926 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8927 814624e7 2022-03-22 thomas if (ie) {
8928 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
8929 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8930 12de5570 2022-05-01 thomas repo);
8931 814624e7 2022-03-22 thomas if (err)
8932 814624e7 2022-03-22 thomas goto done;
8933 814624e7 2022-03-22 thomas } else {
8934 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
8935 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
8936 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
8937 814624e7 2022-03-22 thomas if (errno != ENOENT) {
8938 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
8939 814624e7 2022-03-22 thomas ondisk_path);
8940 814624e7 2022-03-22 thomas goto done;
8941 814624e7 2022-03-22 thomas }
8942 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
8943 814624e7 2022-03-22 thomas }
8944 814624e7 2022-03-22 thomas }
8945 814624e7 2022-03-22 thomas
8946 814624e7 2022-03-22 thomas done:
8947 814624e7 2022-03-22 thomas free(ondisk_path);
8948 814624e7 2022-03-22 thomas return err;
8949 814624e7 2022-03-22 thomas }
8950 814624e7 2022-03-22 thomas
8951 814624e7 2022-03-22 thomas static const struct got_error *
8952 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
8953 814624e7 2022-03-22 thomas unsigned char staged_status)
8954 814624e7 2022-03-22 thomas {
8955 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
8956 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
8957 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
8958 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
8959 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
8960 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
8961 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8962 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
8963 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8964 814624e7 2022-03-22 thomas return NULL;
8965 814624e7 2022-03-22 thomas }
8966 814624e7 2022-03-22 thomas
8967 814624e7 2022-03-22 thomas static const struct got_error *
8968 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
8969 814624e7 2022-03-22 thomas {
8970 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
8971 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8972 814624e7 2022-03-22 thomas return NULL;
8973 814624e7 2022-03-22 thomas }
8974 814624e7 2022-03-22 thomas
8975 814624e7 2022-03-22 thomas static const struct got_error *
8976 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
8977 814624e7 2022-03-22 thomas unsigned char staged_status)
8978 814624e7 2022-03-22 thomas {
8979 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
8980 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
8981 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
8982 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
8983 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
8984 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8985 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
8986 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8987 814624e7 2022-03-22 thomas return NULL;
8988 814624e7 2022-03-22 thomas }
8989 814624e7 2022-03-22 thomas
8990 814624e7 2022-03-22 thomas const struct got_error *
8991 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
8992 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
8993 814624e7 2022-03-22 thomas {
8994 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
8995 814624e7 2022-03-22 thomas }
8996 814624e7 2022-03-22 thomas
8997 814624e7 2022-03-22 thomas const struct got_error *
8998 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
8999 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9000 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9001 814624e7 2022-03-22 thomas {
9002 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9003 814624e7 2022-03-22 thomas int file_renamed = 0;
9004 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9005 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9006 814624e7 2022-03-22 thomas
9007 814624e7 2022-03-22 thomas *oldpath = NULL;
9008 814624e7 2022-03-22 thomas *newpath = NULL;
9009 814624e7 2022-03-22 thomas
9010 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9011 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9012 814624e7 2022-03-22 thomas if (err)
9013 814624e7 2022-03-22 thomas goto done;
9014 814624e7 2022-03-22 thomas
9015 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9016 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9017 814624e7 2022-03-22 thomas if (err)
9018 814624e7 2022-03-22 thomas goto done;
9019 814624e7 2022-03-22 thomas
9020 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9021 814624e7 2022-03-22 thomas file_renamed = 1;
9022 814624e7 2022-03-22 thomas
9023 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9024 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9025 814624e7 2022-03-22 thomas else if (file_renamed) {
9026 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9027 814624e7 2022-03-22 thomas if (err == NULL)
9028 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9029 814624e7 2022-03-22 thomas } else if (old == NULL)
9030 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9031 814624e7 2022-03-22 thomas else
9032 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9033 814624e7 2022-03-22 thomas
9034 814624e7 2022-03-22 thomas done:
9035 814624e7 2022-03-22 thomas if (err) {
9036 814624e7 2022-03-22 thomas free(*oldpath);
9037 814624e7 2022-03-22 thomas *oldpath = NULL;
9038 814624e7 2022-03-22 thomas free(*newpath);
9039 814624e7 2022-03-22 thomas *newpath = NULL;
9040 814624e7 2022-03-22 thomas }
9041 814624e7 2022-03-22 thomas return err;
9042 814624e7 2022-03-22 thomas }
9043 814624e7 2022-03-22 thomas
9044 814624e7 2022-03-22 thomas const struct got_error *
9045 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9046 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9047 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9048 814624e7 2022-03-22 thomas {
9049 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9050 5e22b737 2022-05-31 thomas
9051 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9052 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9053 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9054 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9055 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9056 5e22b737 2022-05-31 thomas saa.repo = repo;
9057 5e22b737 2022-05-31 thomas
9058 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9059 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9060 814624e7 2022-03-22 thomas }
9061 5e22b737 2022-05-31 thomas
9062 5e22b737 2022-05-31 thomas const struct got_error *
9063 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9064 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9065 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9066 5e22b737 2022-05-31 thomas {
9067 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9068 5e22b737 2022-05-31 thomas
9069 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9070 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9071 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9072 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9073 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9074 5e22b737 2022-05-31 thomas sda.repo = repo;
9075 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9076 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9077 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9078 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9079 5e22b737 2022-05-31 thomas
9080 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9081 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9082 5e22b737 2022-05-31 thomas }
9083 5e22b737 2022-05-31 thomas
9084 5e22b737 2022-05-31 thomas const struct got_error *
9085 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9086 36832a8e 2022-05-31 thomas const char *fileindex_path)
9087 5e22b737 2022-05-31 thomas {
9088 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9089 5e22b737 2022-05-31 thomas
9090 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9091 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9092 5e22b737 2022-05-31 thomas
9093 5e22b737 2022-05-31 thomas return err;
9094 5e22b737 2022-05-31 thomas }