Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 86c3caaf 2018-03-09 stsp
20 d1f6d47b 2019-02-04 stsp #include <dirent.h>
21 12ce7a6c 2019-08-12 stsp #include <limits.h>
22 f5c49f82 2019-01-06 stsp #include <stddef.h>
23 86c3caaf 2018-03-09 stsp #include <string.h>
24 86c3caaf 2018-03-09 stsp #include <stdio.h>
25 86c3caaf 2018-03-09 stsp #include <stdlib.h>
26 303e14b5 2019-09-23 stsp #include <time.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <zlib.h>
31 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
32 512f0d0e 2019-01-02 stsp #include <libgen.h>
33 dd038bc6 2021-09-21 thomas.ad
34 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
35 86c3caaf 2018-03-09 stsp
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
57 9d31a1d8 2018-03-11 stsp
58 9d31a1d8 2018-03-11 stsp #ifndef MIN
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 9d31a1d8 2018-03-11 stsp #endif
61 f69721c3 2019-10-21 stsp
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 a2c162eb 2022-10-30 thomas
65 a2c162eb 2022-10-30 thomas static mode_t apply_umask(mode_t);
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
118 09fe317a 2018-03-11 stsp }
119 09fe317a 2018-03-11 stsp
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
122 024e9686 2019-05-14 stsp {
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
125 024e9686 2019-05-14 stsp
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
130 024e9686 2019-05-14 stsp } else {
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
134 024e9686 2019-05-14 stsp }
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
138 024e9686 2019-05-14 stsp }
139 024e9686 2019-05-14 stsp
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
143 86c3caaf 2018-03-09 stsp {
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
154 65596e15 2018-12-24 stsp
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
157 0c48fee2 2019-03-11 stsp goto done;
158 0c48fee2 2019-03-11 stsp }
159 0c48fee2 2019-03-11 stsp
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
168 86c3caaf 2018-03-09 stsp
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
172 0bb8a95e 2018-03-12 stsp }
173 577ec78f 2018-03-11 stsp
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
183 86c3caaf 2018-03-09 stsp goto done;
184 86c3caaf 2018-03-09 stsp }
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
187 86c3caaf 2018-03-09 stsp goto done;
188 86c3caaf 2018-03-09 stsp }
189 86c3caaf 2018-03-09 stsp
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 056e7441 2018-03-11 stsp if (err)
193 056e7441 2018-03-11 stsp goto done;
194 056e7441 2018-03-11 stsp
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 99724ed4 2018-03-10 stsp if (err)
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
202 65596e15 2018-12-24 stsp if (err)
203 65596e15 2018-12-24 stsp goto done;
204 65596e15 2018-12-24 stsp
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp goto done;
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 99724ed4 2018-03-10 stsp if (err)
211 86c3caaf 2018-03-09 stsp goto done;
212 86c3caaf 2018-03-09 stsp
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
216 99724ed4 2018-03-10 stsp if (err)
217 86c3caaf 2018-03-09 stsp goto done;
218 86c3caaf 2018-03-09 stsp
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
222 ec22038e 2019-03-10 stsp if (err)
223 ec22038e 2019-03-10 stsp goto done;
224 ec22038e 2019-03-10 stsp
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
229 ec22038e 2019-03-10 stsp goto done;
230 ec22038e 2019-03-10 stsp }
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp }
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 577ec78f 2018-03-11 stsp if (err)
238 577ec78f 2018-03-11 stsp goto done;
239 577ec78f 2018-03-11 stsp
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 1451e70d 2018-03-10 stsp goto done;
244 1451e70d 2018-03-10 stsp }
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 99724ed4 2018-03-10 stsp if (err)
247 1451e70d 2018-03-10 stsp goto done;
248 1451e70d 2018-03-10 stsp
249 86c3caaf 2018-03-09 stsp done:
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
257 86c3caaf 2018-03-09 stsp }
258 86c3caaf 2018-03-09 stsp
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
262 e5dc7198 2018-12-29 stsp {
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
264 e5dc7198 2018-12-29 stsp
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
268 e5dc7198 2018-12-29 stsp }
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
273 86c3caaf 2018-03-09 stsp }
274 86c3caaf 2018-03-09 stsp
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
277 86c3caaf 2018-03-09 stsp {
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
279 024e9686 2019-05-14 stsp }
280 024e9686 2019-05-14 stsp
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
284 024e9686 2019-05-14 stsp {
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
287 024e9686 2019-05-14 stsp
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
292 024e9686 2019-05-14 stsp goto done;
293 024e9686 2019-05-14 stsp }
294 024e9686 2019-05-14 stsp
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
298 024e9686 2019-05-14 stsp goto done;
299 024e9686 2019-05-14 stsp }
300 024e9686 2019-05-14 stsp
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
302 024e9686 2019-05-14 stsp if (err)
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
307 024e9686 2019-05-14 stsp done:
308 024e9686 2019-05-14 stsp free(path_got);
309 024e9686 2019-05-14 stsp if (err)
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
312 507dc3bb 2018-12-29 stsp }
313 507dc3bb 2018-12-29 stsp
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
316 507dc3bb 2018-12-29 stsp {
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
318 86c3caaf 2018-03-09 stsp }
319 86c3caaf 2018-03-09 stsp
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
323 507dc3bb 2018-12-29 stsp {
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
328 507dc3bb 2018-12-29 stsp
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
333 507dc3bb 2018-12-29 stsp goto done;
334 507dc3bb 2018-12-29 stsp }
335 507dc3bb 2018-12-29 stsp
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
337 507dc3bb 2018-12-29 stsp if (err)
338 507dc3bb 2018-12-29 stsp return err;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 507dc3bb 2018-12-29 stsp goto done;
343 507dc3bb 2018-12-29 stsp }
344 507dc3bb 2018-12-29 stsp
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
347 507dc3bb 2018-12-29 stsp if (err)
348 507dc3bb 2018-12-29 stsp goto done;
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
357 507dc3bb 2018-12-29 stsp goto done;
358 507dc3bb 2018-12-29 stsp }
359 507dc3bb 2018-12-29 stsp done:
360 507dc3bb 2018-12-29 stsp if (obj)
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
365 50b0790e 2020-09-11 stsp }
366 50b0790e 2020-09-11 stsp
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
369 50b0790e 2020-09-11 stsp {
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
371 507dc3bb 2018-12-29 stsp }
372 507dc3bb 2018-12-29 stsp
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
375 86c3caaf 2018-03-09 stsp {
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
381 21908da4 2019-01-13 stsp }
382 21908da4 2019-01-13 stsp
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
385 4a1ddfc2 2019-01-12 stsp {
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
388 4a1ddfc2 2019-01-12 stsp
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
391 4a1ddfc2 2019-01-12 stsp
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 ddcd8544 2019-03-11 stsp }
402 ddcd8544 2019-03-11 stsp }
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
405 68c76935 2019-02-19 stsp }
406 68c76935 2019-02-19 stsp
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
409 68c76935 2019-02-19 stsp {
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
414 68c76935 2019-02-19 stsp
415 68c76935 2019-02-19 stsp *same = 1;
416 68c76935 2019-02-19 stsp
417 230a42bd 2019-05-11 jcs for (;;) {
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
421 68c76935 2019-02-19 stsp break;
422 68c76935 2019-02-19 stsp }
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp break;
427 68c76935 2019-02-19 stsp }
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
430 68c76935 2019-02-19 stsp *same = 0;
431 68c76935 2019-02-19 stsp break;
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
434 68c76935 2019-02-19 stsp *same = 0;
435 68c76935 2019-02-19 stsp break;
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 68c76935 2019-02-19 stsp *same = 0;
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp } else {
442 68c76935 2019-02-19 stsp *same = 0;
443 68c76935 2019-02-19 stsp break;
444 68c76935 2019-02-19 stsp }
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp
447 68c76935 2019-02-19 stsp return err;
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
452 68c76935 2019-02-19 stsp {
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
455 68c76935 2019-02-19 stsp
456 68c76935 2019-02-19 stsp *same = 1;
457 68c76935 2019-02-19 stsp
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
461 68c76935 2019-02-19 stsp
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
465 68c76935 2019-02-19 stsp
466 68c76935 2019-02-19 stsp if (size1 != size2) {
467 68c76935 2019-02-19 stsp *same = 0;
468 68c76935 2019-02-19 stsp return NULL;
469 68c76935 2019-02-19 stsp }
470 68c76935 2019-02-19 stsp
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
477 24f136e0 2021-11-20 thomas }
478 24f136e0 2021-11-20 thomas
479 24f136e0 2021-11-20 thomas static const struct got_error *
480 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
481 24f136e0 2021-11-20 thomas {
482 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
483 24f136e0 2021-11-20 thomas size_t flen;
484 24f136e0 2021-11-20 thomas ssize_t outlen;
485 24f136e0 2021-11-20 thomas
486 24f136e0 2021-11-20 thomas *outsize = 0;
487 24f136e0 2021-11-20 thomas
488 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
489 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
490 24f136e0 2021-11-20 thomas
491 24f136e0 2021-11-20 thomas for (;;) {
492 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
493 24f136e0 2021-11-20 thomas if (flen == 0) {
494 24f136e0 2021-11-20 thomas if (ferror(f))
495 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
496 24f136e0 2021-11-20 thomas if (feof(f))
497 24f136e0 2021-11-20 thomas break;
498 24f136e0 2021-11-20 thomas }
499 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
500 24f136e0 2021-11-20 thomas if (outlen == -1)
501 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
502 24f136e0 2021-11-20 thomas if (outlen != flen)
503 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
504 24f136e0 2021-11-20 thomas *outsize += outlen;
505 24f136e0 2021-11-20 thomas }
506 24f136e0 2021-11-20 thomas
507 24f136e0 2021-11-20 thomas return NULL;
508 24f136e0 2021-11-20 thomas }
509 24f136e0 2021-11-20 thomas
510 24f136e0 2021-11-20 thomas static const struct got_error *
511 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
512 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 24f136e0 2021-11-20 thomas const char *ondisk_path)
515 24f136e0 2021-11-20 thomas {
516 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
517 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
518 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
522 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
523 24f136e0 2021-11-20 thomas
524 24f136e0 2021-11-20 thomas *overlapcnt = 0;
525 24f136e0 2021-11-20 thomas
526 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 24f136e0 2021-11-20 thomas if (err)
528 24f136e0 2021-11-20 thomas return err;
529 24f136e0 2021-11-20 thomas
530 24f136e0 2021-11-20 thomas if (same_content)
531 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
532 24f136e0 2021-11-20 thomas
533 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
534 24f136e0 2021-11-20 thomas if (err)
535 24f136e0 2021-11-20 thomas return err;
536 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
537 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
538 24f136e0 2021-11-20 thomas if (err)
539 24f136e0 2021-11-20 thomas return err;
540 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
541 24f136e0 2021-11-20 thomas
542 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
543 24f136e0 2021-11-20 thomas *overlapcnt = 1;
544 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
546 24f136e0 2021-11-20 thomas goto done;
547 24f136e0 2021-11-20 thomas }
548 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
550 24f136e0 2021-11-20 thomas goto done;
551 24f136e0 2021-11-20 thomas }
552 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
554 24f136e0 2021-11-20 thomas goto done;
555 24f136e0 2021-11-20 thomas }
556 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 24f136e0 2021-11-20 thomas base_path_orig);
558 24f136e0 2021-11-20 thomas if (err)
559 24f136e0 2021-11-20 thomas goto done;
560 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 24f136e0 2021-11-20 thomas base_path_deriv);
562 24f136e0 2021-11-20 thomas if (err)
563 24f136e0 2021-11-20 thomas goto done;
564 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 24f136e0 2021-11-20 thomas base_path_deriv2);
566 24f136e0 2021-11-20 thomas if (err)
567 24f136e0 2021-11-20 thomas goto done;
568 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 24f136e0 2021-11-20 thomas if (err)
570 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 24f136e0 2021-11-20 thomas if (err)
573 24f136e0 2021-11-20 thomas goto done;
574 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 24f136e0 2021-11-20 thomas if (err)
576 24f136e0 2021-11-20 thomas goto done;
577 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
578 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
579 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
580 24f136e0 2021-11-20 thomas goto done;
581 24f136e0 2021-11-20 thomas }
582 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
585 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
586 24f136e0 2021-11-20 thomas path_deriv) < 0) {
587 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas }
590 24f136e0 2021-11-20 thomas if (size_orig > 0) {
591 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
593 24f136e0 2021-11-20 thomas label_orig ? " " : "",
594 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
595 24f136e0 2021-11-20 thomas path_orig) < 0) {
596 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
597 24f136e0 2021-11-20 thomas goto done;
598 24f136e0 2021-11-20 thomas }
599 24f136e0 2021-11-20 thomas }
600 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
602 24f136e0 2021-11-20 thomas path_deriv2,
603 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
604 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
605 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
606 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
607 24f136e0 2021-11-20 thomas goto done;
608 24f136e0 2021-11-20 thomas }
609 24f136e0 2021-11-20 thomas } else if (changed_deriv)
610 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 24f136e0 2021-11-20 thomas else if (changed_deriv2)
612 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 24f136e0 2021-11-20 thomas done:
614 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 24f136e0 2021-11-20 thomas err == NULL)
616 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
617 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
619 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
621 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
623 24f136e0 2021-11-20 thomas free(path_orig);
624 24f136e0 2021-11-20 thomas free(path_deriv);
625 24f136e0 2021-11-20 thomas free(path_deriv2);
626 24f136e0 2021-11-20 thomas free(base_path_orig);
627 24f136e0 2021-11-20 thomas free(base_path_deriv);
628 24f136e0 2021-11-20 thomas free(base_path_deriv2);
629 24f136e0 2021-11-20 thomas return err;
630 6353ad76 2019-02-08 stsp }
631 6353ad76 2019-02-08 stsp
632 6353ad76 2019-02-08 stsp /*
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
638 6353ad76 2019-02-08 stsp */
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
646 6353ad76 2019-02-08 stsp {
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
653 6353ad76 2019-02-08 stsp
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
655 234035bc 2019-06-01 stsp
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
657 3524bbf9 2020-10-20 stsp if (err)
658 3524bbf9 2020-10-20 stsp return err;
659 af54ae4a 2019-02-19 stsp
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
662 3524bbf9 2020-10-20 stsp goto done;
663 3524bbf9 2020-10-20 stsp }
664 af54ae4a 2019-02-19 stsp
665 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 6353ad76 2019-02-08 stsp if (err)
667 6353ad76 2019-02-08 stsp goto done;
668 3b9f0f87 2020-07-23 stsp
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 24f136e0 2021-11-20 thomas if (err) {
672 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
673 24f136e0 2021-11-20 thomas goto done;
674 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 24f136e0 2021-11-20 thomas ondisk_path);
677 24f136e0 2021-11-20 thomas if (err)
678 24f136e0 2021-11-20 thomas goto done;
679 24f136e0 2021-11-20 thomas }
680 6353ad76 2019-02-08 stsp
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 1ee397ad 2019-07-12 stsp if (err)
684 1ee397ad 2019-07-12 stsp goto done;
685 6353ad76 2019-02-08 stsp
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
688 816dc654 2019-02-16 stsp goto done;
689 68c76935 2019-02-19 stsp }
690 68c76935 2019-02-19 stsp
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
694 db590691 2021-06-02 stsp goto done;
695 db590691 2021-06-02 stsp }
696 db590691 2021-06-02 stsp merged_fd = -1;
697 db590691 2021-06-02 stsp
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
701 db590691 2021-06-02 stsp f_merged);
702 68c76935 2019-02-19 stsp if (err)
703 68c76935 2019-02-19 stsp goto done;
704 816dc654 2019-02-16 stsp }
705 6353ad76 2019-02-08 stsp
706 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
708 70a0c8ec 2019-02-20 stsp goto done;
709 70a0c8ec 2019-02-20 stsp }
710 70a0c8ec 2019-02-20 stsp
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
714 6353ad76 2019-02-08 stsp goto done;
715 6353ad76 2019-02-08 stsp }
716 6353ad76 2019-02-08 stsp done:
717 fdcb7daf 2019-12-15 stsp if (err) {
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
720 3b9f0f87 2020-07-23 stsp }
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
729 af57b12a 2020-07-23 stsp }
730 af57b12a 2020-07-23 stsp
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
734 af57b12a 2020-07-23 stsp {
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
742 af57b12a 2020-07-23 stsp }
743 af57b12a 2020-07-23 stsp
744 af57b12a 2020-07-23 stsp /*
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
749 993e2a1b 2020-07-23 stsp *
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
755 11cc08c1 2020-07-23 stsp */
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
760 11cc08c1 2020-07-23 stsp {
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
764 11cc08c1 2020-07-23 stsp
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
766 11cc08c1 2020-07-23 stsp if (err)
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
768 11cc08c1 2020-07-23 stsp
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp goto done;
773 11cc08c1 2020-07-23 stsp }
774 11cc08c1 2020-07-23 stsp
775 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 11cc08c1 2020-07-23 stsp if (err)
777 3818e3c4 2020-11-01 naddy goto done;
778 3818e3c4 2020-11-01 naddy
779 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
781 11cc08c1 2020-07-23 stsp goto done;
782 3818e3c4 2020-11-01 naddy }
783 11cc08c1 2020-07-23 stsp
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
794 11cc08c1 2020-07-23 stsp goto done;
795 11cc08c1 2020-07-23 stsp }
796 11cc08c1 2020-07-23 stsp
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
799 11cc08c1 2020-07-23 stsp goto done;
800 11cc08c1 2020-07-23 stsp }
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
803 11cc08c1 2020-07-23 stsp goto done;
804 11cc08c1 2020-07-23 stsp }
805 11cc08c1 2020-07-23 stsp done:
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
812 11cc08c1 2020-07-23 stsp }
813 d219f183 2020-07-23 stsp
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
820 11cc08c1 2020-07-23 stsp
821 11cc08c1 2020-07-23 stsp /*
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
826 af57b12a 2020-07-23 stsp */
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
833 af57b12a 2020-07-23 stsp {
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
841 af57b12a 2020-07-23 stsp
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
850 af57b12a 2020-07-23 stsp goto done;
851 af57b12a 2020-07-23 stsp }
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
853 af57b12a 2020-07-23 stsp
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 526a746f 2020-07-23 stsp if (err)
857 526a746f 2020-07-23 stsp goto done;
858 526a746f 2020-07-23 stsp }
859 af57b12a 2020-07-23 stsp
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
864 11cc08c1 2020-07-23 stsp
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 11cc08c1 2020-07-23 stsp path);
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp path);
881 11cc08c1 2020-07-23 stsp } else {
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
886 11cc08c1 2020-07-23 stsp if (err)
887 11cc08c1 2020-07-23 stsp goto done;
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 11cc08c1 2020-07-23 stsp path);
890 11cc08c1 2020-07-23 stsp }
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
895 af57b12a 2020-07-23 stsp if (err)
896 af57b12a 2020-07-23 stsp goto done;
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 ea7786be 2020-07-23 stsp path);
904 ea7786be 2020-07-23 stsp } else {
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
908 ea7786be 2020-07-23 stsp if (err)
909 ea7786be 2020-07-23 stsp goto done;
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 ea7786be 2020-07-23 stsp path);
912 ea7786be 2020-07-23 stsp }
913 6353ad76 2019-02-08 stsp }
914 11cc08c1 2020-07-23 stsp
915 af57b12a 2020-07-23 stsp done:
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
918 eec2f5a9 2021-06-03 stsp }
919 eec2f5a9 2021-06-03 stsp
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
922 eec2f5a9 2021-06-03 stsp {
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
926 eec2f5a9 2021-06-03 stsp size_t n;
927 eec2f5a9 2021-06-03 stsp FILE *f;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
937 eec2f5a9 2021-06-03 stsp goto done;
938 eec2f5a9 2021-06-03 stsp }
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
942 eec2f5a9 2021-06-03 stsp goto done;
943 eec2f5a9 2021-06-03 stsp }
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
946 eec2f5a9 2021-06-03 stsp goto done;
947 eec2f5a9 2021-06-03 stsp }
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
950 eec2f5a9 2021-06-03 stsp goto done;
951 eec2f5a9 2021-06-03 stsp }
952 eec2f5a9 2021-06-03 stsp done:
953 eec2f5a9 2021-06-03 stsp if (err)
954 eec2f5a9 2021-06-03 stsp fclose(f);
955 eec2f5a9 2021-06-03 stsp else
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
958 14c901f1 2019-08-08 stsp }
959 14c901f1 2019-08-08 stsp
960 14c901f1 2019-08-08 stsp /*
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
964 14c901f1 2019-08-08 stsp */
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
972 14c901f1 2019-08-08 stsp {
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
978 14c901f1 2019-08-08 stsp
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
980 14c901f1 2019-08-08 stsp
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
982 ed6b5030 2020-10-20 stsp if (err)
983 ed6b5030 2020-10-20 stsp return err;
984 14c901f1 2019-08-08 stsp
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
990 67a66647 2021-05-31 stsp goto done;
991 67a66647 2021-05-31 stsp }
992 67a66647 2021-05-31 stsp
993 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
994 67a66647 2021-05-31 stsp if (err)
995 67a66647 2021-05-31 stsp goto done;
996 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
997 67a66647 2021-05-31 stsp blob_orig);
998 67a66647 2021-05-31 stsp if (err)
999 67a66647 2021-05-31 stsp goto done;
1000 67a66647 2021-05-31 stsp free(base_path);
1001 db590691 2021-06-02 stsp } else {
1002 db590691 2021-06-02 stsp /*
1003 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1004 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1005 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1006 db590691 2021-06-02 stsp */
1007 db590691 2021-06-02 stsp f_orig = got_opentemp();
1008 db590691 2021-06-02 stsp if (f_orig == NULL) {
1009 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1010 db590691 2021-06-02 stsp goto done;
1011 db590691 2021-06-02 stsp }
1012 67a66647 2021-05-31 stsp }
1013 67a66647 2021-05-31 stsp
1014 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1016 14c901f1 2019-08-08 stsp base_path = NULL;
1017 14c901f1 2019-08-08 stsp goto done;
1018 14c901f1 2019-08-08 stsp }
1019 14c901f1 2019-08-08 stsp
1020 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1021 14c901f1 2019-08-08 stsp if (err)
1022 14c901f1 2019-08-08 stsp goto done;
1023 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1024 14c901f1 2019-08-08 stsp blob_deriv);
1025 14c901f1 2019-08-08 stsp if (err)
1026 14c901f1 2019-08-08 stsp goto done;
1027 14c901f1 2019-08-08 stsp
1028 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1029 14c901f1 2019-08-08 stsp if (err)
1030 14c901f1 2019-08-08 stsp goto done;
1031 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1032 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1034 14c901f1 2019-08-08 stsp goto done;
1035 eec2f5a9 2021-06-03 stsp }
1036 eec2f5a9 2021-06-03 stsp
1037 b6b86fd1 2022-08-30 thomas /*
1038 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1039 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1040 eec2f5a9 2021-06-03 stsp */
1041 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1042 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1043 eec2f5a9 2021-06-03 stsp if (err)
1044 eec2f5a9 2021-06-03 stsp goto done;
1045 eec2f5a9 2021-06-03 stsp } else {
1046 eec2f5a9 2021-06-03 stsp int fd;
1047 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1048 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1049 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1050 eec2f5a9 2021-06-03 stsp goto done;
1051 eec2f5a9 2021-06-03 stsp }
1052 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1053 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1054 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1055 eec2f5a9 2021-06-03 stsp close(fd);
1056 eec2f5a9 2021-06-03 stsp goto done;
1057 eec2f5a9 2021-06-03 stsp }
1058 14c901f1 2019-08-08 stsp }
1059 14c901f1 2019-08-08 stsp
1060 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1063 14c901f1 2019-08-08 stsp done:
1064 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1066 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1068 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1070 14c901f1 2019-08-08 stsp free(base_path);
1071 67a66647 2021-05-31 stsp if (blob_orig_path) {
1072 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1073 67a66647 2021-05-31 stsp free(blob_orig_path);
1074 67a66647 2021-05-31 stsp }
1075 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1076 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1077 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1078 14c901f1 2019-08-08 stsp }
1079 6353ad76 2019-02-08 stsp free(id_str);
1080 818c7501 2019-07-11 stsp free(label_deriv);
1081 ed6b5030 2020-10-20 stsp free(parent);
1082 4a1ddfc2 2019-01-12 stsp return err;
1083 4a1ddfc2 2019-01-12 stsp }
1084 4a1ddfc2 2019-01-12 stsp
1085 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1086 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1089 13d9040b 2019-03-27 stsp {
1090 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1091 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1092 13d9040b 2019-03-27 stsp
1093 65b05cec 2020-07-23 stsp *new_iep = NULL;
1094 65b05cec 2020-07-23 stsp
1095 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1096 054041d0 2020-06-24 stsp if (err)
1097 054041d0 2020-06-24 stsp return err;
1098 054041d0 2020-06-24 stsp
1099 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1101 054041d0 2020-06-24 stsp if (err)
1102 054041d0 2020-06-24 stsp goto done;
1103 054041d0 2020-06-24 stsp
1104 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1105 054041d0 2020-06-24 stsp done:
1106 054041d0 2020-06-24 stsp if (err)
1107 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1108 65b05cec 2020-07-23 stsp else
1109 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1110 13d9040b 2019-03-27 stsp return err;
1111 1ebedb77 2019-10-19 stsp }
1112 1ebedb77 2019-10-19 stsp
1113 1ebedb77 2019-10-19 stsp static mode_t
1114 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1115 1ebedb77 2019-10-19 stsp {
1116 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1117 1ebedb77 2019-10-19 stsp
1118 1ebedb77 2019-10-19 stsp if (executable) {
1119 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1120 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1121 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1122 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1123 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1124 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1125 1ebedb77 2019-10-19 stsp }
1126 1ebedb77 2019-10-19 stsp
1127 a2c162eb 2022-10-30 thomas return st_mode;
1128 a2c162eb 2022-10-30 thomas }
1129 a2c162eb 2022-10-30 thomas
1130 a2c162eb 2022-10-30 thomas static mode_t
1131 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
1132 a2c162eb 2022-10-30 thomas {
1133 a2c162eb 2022-10-30 thomas mode_t um;
1134 a2c162eb 2022-10-30 thomas
1135 a2c162eb 2022-10-30 thomas um = umask(000);
1136 a2c162eb 2022-10-30 thomas umask(um);
1137 a2c162eb 2022-10-30 thomas return mode & ~um;
1138 13d9040b 2019-03-27 stsp }
1139 13d9040b 2019-03-27 stsp
1140 8ba819a3 2020-07-23 stsp /* forward declaration */
1141 13d9040b 2019-03-27 stsp static const struct got_error *
1142 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1143 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1144 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1145 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1146 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1147 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1148 8ba819a3 2020-07-23 stsp
1149 5a1fbc73 2020-07-23 stsp /*
1150 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1151 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1152 5a1fbc73 2020-07-23 stsp */
1153 8ba819a3 2020-07-23 stsp static const struct got_error *
1154 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1155 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1156 5a1fbc73 2020-07-23 stsp {
1157 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1158 5a1fbc73 2020-07-23 stsp ssize_t elen;
1159 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1160 5a1fbc73 2020-07-23 stsp int fd;
1161 5a1fbc73 2020-07-23 stsp
1162 c6e8a826 2021-04-05 stsp *did_something = 0;
1163 c6e8a826 2021-04-05 stsp
1164 5a1fbc73 2020-07-23 stsp /*
1165 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1166 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1167 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1168 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1169 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1170 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1171 5a1fbc73 2020-07-23 stsp */
1172 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1173 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1174 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1175 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1176 5a1fbc73 2020-07-23 stsp
1177 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1178 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1179 5a1fbc73 2020-07-23 stsp if (elen == -1)
1180 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1181 5a1fbc73 2020-07-23 stsp
1182 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1183 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1184 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1185 5a1fbc73 2020-07-23 stsp }
1186 5a1fbc73 2020-07-23 stsp
1187 c6e8a826 2021-04-05 stsp *did_something = 1;
1188 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1189 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1190 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1191 5a1fbc73 2020-07-23 stsp return err;
1192 3c1ec782 2020-07-23 stsp }
1193 3c1ec782 2020-07-23 stsp
1194 3c1ec782 2020-07-23 stsp static const struct got_error *
1195 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1196 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1197 3c1ec782 2020-07-23 stsp {
1198 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1199 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1200 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1201 3c1ec782 2020-07-23 stsp
1202 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1203 3c1ec782 2020-07-23 stsp
1204 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1205 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1206 3c1ec782 2020-07-23 stsp return NULL;
1207 3c1ec782 2020-07-23 stsp }
1208 3c1ec782 2020-07-23 stsp
1209 3c1ec782 2020-07-23 stsp /*
1210 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1211 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1212 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1213 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1214 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1215 3c1ec782 2020-07-23 stsp */
1216 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1217 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1218 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1219 ce031e9e 2020-10-20 stsp if (err)
1220 ce031e9e 2020-10-20 stsp return err;
1221 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1222 ce031e9e 2020-10-20 stsp free(parent);
1223 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1224 ce031e9e 2020-10-20 stsp }
1225 ce031e9e 2020-10-20 stsp free(parent);
1226 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1227 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1228 3c1ec782 2020-07-23 stsp free(abspath);
1229 3c1ec782 2020-07-23 stsp return err;
1230 3c1ec782 2020-07-23 stsp }
1231 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1232 3c1ec782 2020-07-23 stsp free(abspath);
1233 3c1ec782 2020-07-23 stsp if (err)
1234 3c1ec782 2020-07-23 stsp return err;
1235 3c1ec782 2020-07-23 stsp } else {
1236 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1237 3c1ec782 2020-07-23 stsp if (err)
1238 3c1ec782 2020-07-23 stsp return err;
1239 3c1ec782 2020-07-23 stsp }
1240 3c1ec782 2020-07-23 stsp
1241 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1242 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1243 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1244 3c1ec782 2020-07-23 stsp return NULL;
1245 3c1ec782 2020-07-23 stsp }
1246 3c1ec782 2020-07-23 stsp
1247 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1248 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1249 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1250 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1251 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1252 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1253 3c1ec782 2020-07-23 stsp
1254 3c1ec782 2020-07-23 stsp free(path_got);
1255 3c1ec782 2020-07-23 stsp return NULL;
1256 5a1fbc73 2020-07-23 stsp }
1257 5a1fbc73 2020-07-23 stsp
1258 5a1fbc73 2020-07-23 stsp static const struct got_error *
1259 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1260 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1261 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1262 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1263 ace90326 2021-09-27 thomas struct got_repository *repo,
1264 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1265 9d31a1d8 2018-03-11 stsp {
1266 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1267 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1268 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1269 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1270 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1271 8ba819a3 2020-07-23 stsp
1272 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1273 2e1fa222 2020-07-23 stsp
1274 9e39ca77 2022-07-21 thomas /*
1275 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1276 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1277 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1278 8ba819a3 2020-07-23 stsp * in the blob object.
1279 8ba819a3 2020-07-23 stsp */
1280 8ba819a3 2020-07-23 stsp do {
1281 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1282 3521b466 2022-07-21 thomas if (err)
1283 3521b466 2022-07-21 thomas return err;
1284 3521b466 2022-07-21 thomas
1285 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1286 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1287 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1288 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1289 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1290 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1291 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1292 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1293 3b9f0f87 2020-07-23 stsp progress_arg);
1294 8ba819a3 2020-07-23 stsp }
1295 8ba819a3 2020-07-23 stsp if (len > 0) {
1296 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1297 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1298 8ba819a3 2020-07-23 stsp len - hdrlen);
1299 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1300 8ba819a3 2020-07-23 stsp hdrlen = 0;
1301 8ba819a3 2020-07-23 stsp }
1302 8ba819a3 2020-07-23 stsp } while (len != 0);
1303 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1304 8ba819a3 2020-07-23 stsp
1305 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1306 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1307 3c1ec782 2020-07-23 stsp if (err)
1308 3c1ec782 2020-07-23 stsp return err;
1309 8ba819a3 2020-07-23 stsp
1310 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1311 906c123b 2020-07-23 stsp /* install as a regular file */
1312 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1313 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1314 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1315 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1316 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1317 9e39ca77 2022-07-21 thomas return err;
1318 906c123b 2020-07-23 stsp }
1319 906c123b 2020-07-23 stsp
1320 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1321 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1322 c6e8a826 2021-04-05 stsp int symlink_replaced;
1323 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1324 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1325 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1326 9e39ca77 2022-07-21 thomas return err;
1327 c90c8ce3 2020-07-23 stsp }
1328 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1329 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1330 5a1fbc73 2020-07-23 stsp if (err)
1331 9e39ca77 2022-07-21 thomas return err;
1332 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1333 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1334 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1335 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1336 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1337 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1338 c6e8a826 2021-04-05 stsp } else {
1339 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1340 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1341 c6e8a826 2021-04-05 stsp }
1342 f35fa46a 2020-07-23 stsp }
1343 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1344 f35fa46a 2020-07-23 stsp }
1345 f35fa46a 2020-07-23 stsp
1346 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1347 f4994adc 2020-10-20 stsp char *parent;
1348 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1349 f4994adc 2020-10-20 stsp if (err)
1350 9e39ca77 2022-07-21 thomas return err;
1351 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1352 f4994adc 2020-10-20 stsp free(parent);
1353 f35fa46a 2020-07-23 stsp if (err)
1354 9e39ca77 2022-07-21 thomas return err;
1355 f35fa46a 2020-07-23 stsp /*
1356 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1357 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1358 f35fa46a 2020-07-23 stsp */
1359 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1360 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1361 9e39ca77 2022-07-21 thomas return err;
1362 f35fa46a 2020-07-23 stsp }
1363 f35fa46a 2020-07-23 stsp }
1364 f35fa46a 2020-07-23 stsp
1365 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1366 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1367 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1368 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1369 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1370 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1371 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1372 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1373 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1374 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1375 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1376 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1377 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1378 8ba819a3 2020-07-23 stsp } else {
1379 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1380 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1381 8ba819a3 2020-07-23 stsp }
1382 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1383 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1384 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1385 8ba819a3 2020-07-23 stsp return err;
1386 8ba819a3 2020-07-23 stsp }
1387 8ba819a3 2020-07-23 stsp
1388 8ba819a3 2020-07-23 stsp static const struct got_error *
1389 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1390 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1391 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1392 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1393 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1394 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1395 8ba819a3 2020-07-23 stsp {
1396 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1397 507dc3bb 2018-12-29 stsp int fd = -1;
1398 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1399 507dc3bb 2018-12-29 stsp int update = 0;
1400 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1401 a2c162eb 2022-10-30 thomas mode_t mode;
1402 8ba819a3 2020-07-23 stsp
1403 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1404 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1405 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1406 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1407 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1408 f5375317 2020-10-20 stsp char *parent;
1409 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1410 f5375317 2020-10-20 stsp if (err)
1411 f5375317 2020-10-20 stsp return err;
1412 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1413 f5375317 2020-10-20 stsp free(parent);
1414 21908da4 2019-01-13 stsp if (err)
1415 21908da4 2019-01-13 stsp return err;
1416 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1417 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1418 a2c162eb 2022-10-30 thomas mode);
1419 21908da4 2019-01-13 stsp if (fd == -1)
1420 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1421 230a42bd 2019-05-11 jcs ondisk_path);
1422 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1423 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1424 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1425 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1426 3b9f0f87 2020-07-23 stsp goto done;
1427 3b9f0f87 2020-07-23 stsp }
1428 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1429 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1430 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1431 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1432 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1433 507dc3bb 2018-12-29 stsp goto done;
1434 d70b8e30 2018-12-27 stsp } else {
1435 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1436 507dc3bb 2018-12-29 stsp ondisk_path);
1437 507dc3bb 2018-12-29 stsp if (err)
1438 507dc3bb 2018-12-29 stsp goto done;
1439 507dc3bb 2018-12-29 stsp update = 1;
1440 a2c162eb 2022-10-30 thomas
1441 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1442 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1443 a2c162eb 2022-10-30 thomas tmppath);
1444 a2c162eb 2022-10-30 thomas goto done;
1445 a2c162eb 2022-10-30 thomas }
1446 9d31a1d8 2018-03-11 stsp }
1447 507dc3bb 2018-12-29 stsp } else
1448 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1449 3818e3c4 2020-11-01 naddy }
1450 3818e3c4 2020-11-01 naddy
1451 bd6aa359 2020-07-23 stsp if (progress_cb) {
1452 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1453 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1454 bd6aa359 2020-07-23 stsp path);
1455 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1456 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1457 bd6aa359 2020-07-23 stsp path);
1458 bd6aa359 2020-07-23 stsp else
1459 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1460 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1461 bd6aa359 2020-07-23 stsp if (err)
1462 bd6aa359 2020-07-23 stsp goto done;
1463 bd6aa359 2020-07-23 stsp }
1464 d7b62c98 2018-12-27 stsp
1465 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1466 9d31a1d8 2018-03-11 stsp do {
1467 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1468 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1469 9d31a1d8 2018-03-11 stsp if (err)
1470 9d31a1d8 2018-03-11 stsp break;
1471 9d31a1d8 2018-03-11 stsp if (len > 0) {
1472 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1473 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1474 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1475 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1476 b87c6f83 2018-12-24 stsp goto done;
1477 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1478 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1479 b87c6f83 2018-12-24 stsp goto done;
1480 9d31a1d8 2018-03-11 stsp }
1481 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1482 9d31a1d8 2018-03-11 stsp }
1483 9d31a1d8 2018-03-11 stsp } while (len != 0);
1484 9d31a1d8 2018-03-11 stsp
1485 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1486 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1487 816dc654 2019-02-16 stsp goto done;
1488 816dc654 2019-02-16 stsp }
1489 9d31a1d8 2018-03-11 stsp
1490 507dc3bb 2018-12-29 stsp if (update) {
1491 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1492 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1493 f6d8c0ac 2020-11-09 stsp goto done;
1494 f6d8c0ac 2020-11-09 stsp }
1495 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1496 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1497 230a42bd 2019-05-11 jcs ondisk_path);
1498 68ed9ba5 2019-02-10 stsp goto done;
1499 68ed9ba5 2019-02-10 stsp }
1500 63df146d 2020-11-09 stsp free(tmppath);
1501 63df146d 2020-11-09 stsp tmppath = NULL;
1502 507dc3bb 2018-12-29 stsp }
1503 507dc3bb 2018-12-29 stsp
1504 9d31a1d8 2018-03-11 stsp done:
1505 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1506 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1507 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1508 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1509 507dc3bb 2018-12-29 stsp free(tmppath);
1510 6353ad76 2019-02-08 stsp return err;
1511 6353ad76 2019-02-08 stsp }
1512 6353ad76 2019-02-08 stsp
1513 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1514 6353ad76 2019-02-08 stsp static const struct got_error *
1515 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1516 7154f6ce 2019-03-27 stsp {
1517 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1518 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1519 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1520 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1521 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1522 7154f6ce 2019-03-27 stsp };
1523 7154f6ce 2019-03-27 stsp int i = 0;
1524 9bdd68dd 2020-01-02 naddy char *line = NULL;
1525 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1526 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1527 7154f6ce 2019-03-27 stsp
1528 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1529 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1530 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1531 7154f6ce 2019-03-27 stsp if (feof(f))
1532 7154f6ce 2019-03-27 stsp break;
1533 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1534 7154f6ce 2019-03-27 stsp break;
1535 7154f6ce 2019-03-27 stsp }
1536 7154f6ce 2019-03-27 stsp
1537 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1538 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1539 19332e6d 2019-05-13 stsp == 0)
1540 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1541 7154f6ce 2019-03-27 stsp else
1542 7154f6ce 2019-03-27 stsp i++;
1543 7154f6ce 2019-03-27 stsp }
1544 7154f6ce 2019-03-27 stsp }
1545 9bdd68dd 2020-01-02 naddy free(line);
1546 7154f6ce 2019-03-27 stsp
1547 7154f6ce 2019-03-27 stsp return err;
1548 e2b1e152 2019-07-13 stsp }
1549 e2b1e152 2019-07-13 stsp
1550 e2b1e152 2019-07-13 stsp static int
1551 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1552 1ebedb77 2019-10-19 stsp {
1553 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1554 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1555 1ebedb77 2019-10-19 stsp }
1556 1ebedb77 2019-10-19 stsp
1557 1ebedb77 2019-10-19 stsp static int
1558 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1559 e2b1e152 2019-07-13 stsp {
1560 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1561 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1562 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1563 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1564 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1565 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1566 c363b2c1 2019-08-03 stsp }
1567 c363b2c1 2019-08-03 stsp
1568 c363b2c1 2019-08-03 stsp static unsigned char
1569 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1570 c363b2c1 2019-08-03 stsp {
1571 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1572 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1573 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1574 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1575 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1576 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1577 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1578 c363b2c1 2019-08-03 stsp default:
1579 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1580 a919d5c4 2020-07-23 stsp }
1581 a919d5c4 2020-07-23 stsp }
1582 a919d5c4 2020-07-23 stsp
1583 a919d5c4 2020-07-23 stsp static const struct got_error *
1584 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1585 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1586 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1587 a919d5c4 2020-07-23 stsp {
1588 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1589 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1590 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1591 a919d5c4 2020-07-23 stsp ssize_t elen;
1592 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1593 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1594 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1595 a919d5c4 2020-07-23 stsp
1596 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1597 a919d5c4 2020-07-23 stsp
1598 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1599 a919d5c4 2020-07-23 stsp do {
1600 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1601 a919d5c4 2020-07-23 stsp if (err)
1602 a919d5c4 2020-07-23 stsp return err;
1603 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1604 a919d5c4 2020-07-23 stsp /*
1605 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1606 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1607 a919d5c4 2020-07-23 stsp */
1608 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1609 a919d5c4 2020-07-23 stsp }
1610 a919d5c4 2020-07-23 stsp if (len > 0) {
1611 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1612 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1613 a919d5c4 2020-07-23 stsp len - hdrlen);
1614 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1615 a919d5c4 2020-07-23 stsp hdrlen = 0;
1616 a919d5c4 2020-07-23 stsp }
1617 a919d5c4 2020-07-23 stsp } while (len != 0);
1618 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1619 a919d5c4 2020-07-23 stsp
1620 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1621 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1622 a919d5c4 2020-07-23 stsp if (elen == -1)
1623 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1624 a919d5c4 2020-07-23 stsp } else {
1625 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1626 a919d5c4 2020-07-23 stsp if (elen == -1)
1627 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1628 c363b2c1 2019-08-03 stsp }
1629 a919d5c4 2020-07-23 stsp
1630 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1631 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1632 a919d5c4 2020-07-23 stsp
1633 a919d5c4 2020-07-23 stsp return NULL;
1634 7154f6ce 2019-03-27 stsp }
1635 7154f6ce 2019-03-27 stsp
1636 7154f6ce 2019-03-27 stsp static const struct got_error *
1637 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1638 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1639 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1640 6353ad76 2019-02-08 stsp {
1641 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1642 6353ad76 2019-02-08 stsp struct got_object_id id;
1643 6353ad76 2019-02-08 stsp size_t hdrlen;
1644 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1645 6353ad76 2019-02-08 stsp FILE *f = NULL;
1646 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1647 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1648 6353ad76 2019-02-08 stsp size_t flen, blen;
1649 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1650 6353ad76 2019-02-08 stsp
1651 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1652 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1653 6353ad76 2019-02-08 stsp
1654 7f91a133 2019-12-13 stsp /*
1655 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1656 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1657 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1658 7f91a133 2019-12-13 stsp */
1659 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1660 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1661 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1662 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1663 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1664 882ef1b9 2019-12-13 stsp else
1665 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1666 882ef1b9 2019-12-13 stsp goto done;
1667 882ef1b9 2019-12-13 stsp }
1668 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1669 3d35a492 2019-12-13 stsp goto done;
1670 3d35a492 2019-12-13 stsp }
1671 7f91a133 2019-12-13 stsp } else {
1672 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1673 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1674 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1675 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1676 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1677 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1678 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1679 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1680 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1681 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1682 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1683 3d35a492 2019-12-13 stsp else
1684 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1685 3d35a492 2019-12-13 stsp goto done;
1686 3d35a492 2019-12-13 stsp }
1687 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1688 1338848f 2019-12-13 stsp goto done;
1689 a378724f 2019-02-10 stsp }
1690 a378724f 2019-02-10 stsp }
1691 6353ad76 2019-02-08 stsp
1692 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1693 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1694 1338848f 2019-12-13 stsp goto done;
1695 3f148bc6 2019-07-27 stsp }
1696 efdd40df 2019-07-27 stsp
1697 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1698 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1699 1338848f 2019-12-13 stsp goto done;
1700 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1701 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1702 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1703 1338848f 2019-12-13 stsp goto done;
1704 d00136be 2019-03-26 stsp }
1705 b8f41171 2019-02-10 stsp
1706 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1707 f179e66d 2020-07-23 stsp goto done;
1708 f179e66d 2020-07-23 stsp
1709 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1710 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1711 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1712 1338848f 2019-12-13 stsp goto done;
1713 f179e66d 2020-07-23 stsp }
1714 6353ad76 2019-02-08 stsp
1715 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1716 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1717 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1718 c363b2c1 2019-08-03 stsp else
1719 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1720 c363b2c1 2019-08-03 stsp
1721 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1722 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1723 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1724 f4ae6ddb 2022-07-01 thomas goto done;
1725 f4ae6ddb 2022-07-01 thomas }
1726 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1727 6353ad76 2019-02-08 stsp if (err)
1728 1338848f 2019-12-13 stsp goto done;
1729 6353ad76 2019-02-08 stsp
1730 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1731 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1732 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1733 a919d5c4 2020-07-23 stsp goto done;
1734 a919d5c4 2020-07-23 stsp }
1735 a919d5c4 2020-07-23 stsp
1736 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1737 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1738 ab0d4361 2019-12-13 stsp if (fd == -1) {
1739 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1740 ab0d4361 2019-12-13 stsp goto done;
1741 ab0d4361 2019-12-13 stsp }
1742 3d35a492 2019-12-13 stsp }
1743 3d35a492 2019-12-13 stsp
1744 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1745 6353ad76 2019-02-08 stsp if (f == NULL) {
1746 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1747 6353ad76 2019-02-08 stsp goto done;
1748 6353ad76 2019-02-08 stsp }
1749 1338848f 2019-12-13 stsp fd = -1;
1750 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1751 656b1f76 2019-05-11 jcs for (;;) {
1752 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1753 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1754 6353ad76 2019-02-08 stsp if (err)
1755 7154f6ce 2019-03-27 stsp goto done;
1756 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1757 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1758 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1759 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1760 7154f6ce 2019-03-27 stsp goto done;
1761 80c5c120 2019-02-19 stsp }
1762 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1763 6353ad76 2019-02-08 stsp if (flen != 0)
1764 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1765 6353ad76 2019-02-08 stsp break;
1766 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1767 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1768 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1769 6353ad76 2019-02-08 stsp break;
1770 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1771 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1772 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1773 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1774 6353ad76 2019-02-08 stsp break;
1775 6353ad76 2019-02-08 stsp }
1776 6353ad76 2019-02-08 stsp } else {
1777 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1778 6353ad76 2019-02-08 stsp break;
1779 6353ad76 2019-02-08 stsp }
1780 6353ad76 2019-02-08 stsp hdrlen = 0;
1781 6353ad76 2019-02-08 stsp }
1782 7154f6ce 2019-03-27 stsp
1783 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1784 7154f6ce 2019-03-27 stsp rewind(f);
1785 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1786 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1787 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1788 6353ad76 2019-02-08 stsp done:
1789 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1790 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1791 6353ad76 2019-02-08 stsp if (blob)
1792 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1793 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1794 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1795 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1796 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1797 9d31a1d8 2018-03-11 stsp return err;
1798 e2b1e152 2019-07-13 stsp }
1799 e2b1e152 2019-07-13 stsp
1800 e2b1e152 2019-07-13 stsp /*
1801 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1802 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1803 e2b1e152 2019-07-13 stsp */
1804 e2b1e152 2019-07-13 stsp static const struct got_error *
1805 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1806 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1807 e2b1e152 2019-07-13 stsp {
1808 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1809 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1810 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1811 e2b1e152 2019-07-13 stsp
1812 e2b1e152 2019-07-13 stsp return NULL;
1813 9d31a1d8 2018-03-11 stsp }
1814 9d31a1d8 2018-03-11 stsp
1815 9d31a1d8 2018-03-11 stsp static const struct got_error *
1816 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1817 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1818 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1819 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1820 0584f854 2019-04-06 stsp void *progress_arg)
1821 9d31a1d8 2018-03-11 stsp {
1822 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1823 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1824 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1825 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1826 b8f41171 2019-02-10 stsp struct stat sb;
1827 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1828 9d31a1d8 2018-03-11 stsp
1829 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1830 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1831 6353ad76 2019-02-08 stsp
1832 abb4604f 2019-07-27 stsp if (ie) {
1833 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1834 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1835 a76c42e6 2019-08-03 stsp goto done;
1836 a76c42e6 2019-08-03 stsp }
1837 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1838 7f91a133 2019-12-13 stsp repo);
1839 abb4604f 2019-07-27 stsp if (err)
1840 abb4604f 2019-07-27 stsp goto done;
1841 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1842 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1843 c90c8ce3 2020-07-23 stsp } else {
1844 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1845 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1846 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1847 e6f4ba31 2021-09-24 thomas ondisk_path);
1848 e6f4ba31 2021-09-24 thomas goto done;
1849 e6f4ba31 2021-09-24 thomas }
1850 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1851 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1852 e6f4ba31 2021-09-24 thomas } else {
1853 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1854 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1855 e6f4ba31 2021-09-24 thomas else
1856 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1857 e6f4ba31 2021-09-24 thomas }
1858 c90c8ce3 2020-07-23 stsp }
1859 6353ad76 2019-02-08 stsp
1860 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1861 2c41dce7 2021-06-27 stsp if (ie)
1862 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1863 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1864 b8f41171 2019-02-10 stsp goto done;
1865 b8f41171 2019-02-10 stsp }
1866 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1867 a769b60b 2021-06-27 stsp if (ie)
1868 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1869 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1870 5036ab18 2020-04-18 stsp path);
1871 5036ab18 2020-04-18 stsp goto done;
1872 5036ab18 2020-04-18 stsp }
1873 b8f41171 2019-02-10 stsp
1874 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1875 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1876 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1877 c6e8a826 2021-04-05 stsp /*
1878 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1879 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1880 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1881 c6e8a826 2021-04-05 stsp * updating contents of this file.
1882 c6e8a826 2021-04-05 stsp */
1883 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1884 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1885 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1886 c6e8a826 2021-04-05 stsp /* Same commit. */
1887 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1888 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1889 e2b1e152 2019-07-13 stsp if (err)
1890 e2b1e152 2019-07-13 stsp goto done;
1891 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1892 b8f41171 2019-02-10 stsp path);
1893 6353ad76 2019-02-08 stsp goto done;
1894 a378724f 2019-02-10 stsp }
1895 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1896 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1897 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1898 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1899 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1900 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1901 0f58026f 2021-04-05 stsp if (err)
1902 0f58026f 2021-04-05 stsp goto done;
1903 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1904 0f58026f 2021-04-05 stsp path);
1905 b8f41171 2019-02-10 stsp goto done;
1906 e2b1e152 2019-07-13 stsp }
1907 9d31a1d8 2018-03-11 stsp }
1908 9d31a1d8 2018-03-11 stsp
1909 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1910 f4ae6ddb 2022-07-01 thomas if (fd1 == -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 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1915 8da9e5f4 2019-01-12 stsp if (err)
1916 6353ad76 2019-02-08 stsp goto done;
1917 512f0d0e 2019-01-02 stsp
1918 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1919 234035bc 2019-06-01 stsp int update_timestamps;
1920 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1921 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1922 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1923 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
1924 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
1925 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1926 f4ae6ddb 2022-07-01 thomas goto done;
1927 f4ae6ddb 2022-07-01 thomas }
1928 234035bc 2019-06-01 stsp struct got_object_id id2;
1929 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1930 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1931 f4ae6ddb 2022-07-01 thomas fd2);
1932 234035bc 2019-06-01 stsp if (err)
1933 f69721c3 2019-10-21 stsp goto done;
1934 f69721c3 2019-10-21 stsp }
1935 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1936 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1937 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1938 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1939 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1940 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1941 f69721c3 2019-10-21 stsp goto done;
1942 f69721c3 2019-10-21 stsp }
1943 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1944 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1945 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1946 234035bc 2019-06-01 stsp goto done;
1947 f69721c3 2019-10-21 stsp }
1948 234035bc 2019-06-01 stsp }
1949 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1950 36bf999c 2020-07-23 stsp char *link_target;
1951 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1952 36bf999c 2020-07-23 stsp if (err)
1953 36bf999c 2020-07-23 stsp goto done;
1954 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1955 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1956 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1957 36bf999c 2020-07-23 stsp free(link_target);
1958 993e2a1b 2020-07-23 stsp } else {
1959 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1960 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1961 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1962 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1963 993e2a1b 2020-07-23 stsp }
1964 f69721c3 2019-10-21 stsp free(label_orig);
1965 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1966 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1967 f4ae6ddb 2022-07-01 thomas goto done;
1968 f4ae6ddb 2022-07-01 thomas }
1969 234035bc 2019-06-01 stsp if (blob2)
1970 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1971 909d120e 2019-09-22 stsp if (err)
1972 909d120e 2019-09-22 stsp goto done;
1973 234035bc 2019-06-01 stsp /*
1974 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1975 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1976 234035bc 2019-06-01 stsp * unmodified files again.
1977 234035bc 2019-06-01 stsp */
1978 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1979 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1980 234035bc 2019-06-01 stsp update_timestamps);
1981 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1982 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1983 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1984 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1985 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1986 1ee397ad 2019-07-12 stsp if (err)
1987 1ee397ad 2019-07-12 stsp goto done;
1988 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1989 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1990 13d9040b 2019-03-27 stsp if (err)
1991 13d9040b 2019-03-27 stsp goto done;
1992 13d9040b 2019-03-27 stsp } else {
1993 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1994 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1995 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1996 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1997 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1998 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
1999 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2000 2e1fa222 2020-07-23 stsp } else {
2001 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2002 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2003 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2004 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2005 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2006 2e1fa222 2020-07-23 stsp }
2007 13d9040b 2019-03-27 stsp if (err)
2008 13d9040b 2019-03-27 stsp goto done;
2009 65b05cec 2020-07-23 stsp
2010 054041d0 2020-06-24 stsp if (ie) {
2011 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2012 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2013 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2014 054041d0 2020-06-24 stsp } else {
2015 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2016 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2017 054041d0 2020-06-24 stsp &blob->id);
2018 054041d0 2020-06-24 stsp }
2019 13d9040b 2019-03-27 stsp if (err)
2020 13d9040b 2019-03-27 stsp goto done;
2021 65b05cec 2020-07-23 stsp
2022 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2023 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2024 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2025 2e1fa222 2020-07-23 stsp }
2026 13d9040b 2019-03-27 stsp }
2027 f4ae6ddb 2022-07-01 thomas
2028 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2029 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2030 f4ae6ddb 2022-07-01 thomas goto done;
2031 f4ae6ddb 2022-07-01 thomas }
2032 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2033 6353ad76 2019-02-08 stsp done:
2034 6353ad76 2019-02-08 stsp free(ondisk_path);
2035 8da9e5f4 2019-01-12 stsp return err;
2036 90285c3b 2019-01-08 stsp }
2037 90285c3b 2019-01-08 stsp
2038 90285c3b 2019-01-08 stsp static const struct got_error *
2039 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2040 90285c3b 2019-01-08 stsp {
2041 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2042 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2043 90285c3b 2019-01-08 stsp
2044 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2045 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2046 90285c3b 2019-01-08 stsp
2047 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2048 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2049 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2050 c97665ae 2019-01-13 stsp } else {
2051 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
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 goto done;
2055 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2056 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2057 fddefe3b 2020-10-20 stsp free(ondisk_path);
2058 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2059 1c4cdd89 2021-06-20 stsp parent = NULL;
2060 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2061 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2062 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2063 fddefe3b 2020-10-20 stsp ondisk_path);
2064 90285c3b 2019-01-08 stsp break;
2065 90285c3b 2019-01-08 stsp }
2066 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2067 1c4cdd89 2021-06-20 stsp if (err)
2068 1c4cdd89 2021-06-20 stsp break;
2069 1c4cdd89 2021-06-20 stsp }
2070 90285c3b 2019-01-08 stsp }
2071 1c4cdd89 2021-06-20 stsp done:
2072 90285c3b 2019-01-08 stsp free(ondisk_path);
2073 1c4cdd89 2021-06-20 stsp free(parent);
2074 90285c3b 2019-01-08 stsp return err;
2075 512f0d0e 2019-01-02 stsp }
2076 512f0d0e 2019-01-02 stsp
2077 708d8e67 2019-03-27 stsp static const struct got_error *
2078 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2079 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2080 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2081 708d8e67 2019-03-27 stsp {
2082 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2083 708d8e67 2019-03-27 stsp unsigned char status;
2084 708d8e67 2019-03-27 stsp struct stat sb;
2085 708d8e67 2019-03-27 stsp char *ondisk_path;
2086 708d8e67 2019-03-27 stsp
2087 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2088 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2089 a76c42e6 2019-08-03 stsp
2090 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2091 708d8e67 2019-03-27 stsp == -1)
2092 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2093 708d8e67 2019-03-27 stsp
2094 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2095 708d8e67 2019-03-27 stsp if (err)
2096 5a58a424 2020-06-23 stsp goto done;
2097 708d8e67 2019-03-27 stsp
2098 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2099 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2100 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2101 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2102 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2103 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2104 993e2a1b 2020-07-23 stsp goto done;
2105 993e2a1b 2020-07-23 stsp }
2106 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2107 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2108 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2109 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2110 993e2a1b 2020-07-23 stsp if (err)
2111 993e2a1b 2020-07-23 stsp goto done;
2112 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2113 993e2a1b 2020-07-23 stsp ie->path);
2114 993e2a1b 2020-07-23 stsp goto done;
2115 993e2a1b 2020-07-23 stsp }
2116 993e2a1b 2020-07-23 stsp
2117 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2118 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2119 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2120 1ee397ad 2019-07-12 stsp if (err)
2121 5a58a424 2020-06-23 stsp goto done;
2122 fc6346c4 2019-03-27 stsp /*
2123 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2124 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2125 fc6346c4 2019-03-27 stsp */
2126 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2127 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2128 fc6346c4 2019-03-27 stsp } else {
2129 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2130 1ee397ad 2019-07-12 stsp if (err)
2131 5a58a424 2020-06-23 stsp goto done;
2132 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2133 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2134 fc6346c4 2019-03-27 stsp if (err)
2135 5a58a424 2020-06-23 stsp goto done;
2136 fc6346c4 2019-03-27 stsp }
2137 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2138 708d8e67 2019-03-27 stsp }
2139 5a58a424 2020-06-23 stsp done:
2140 5a58a424 2020-06-23 stsp free(ondisk_path);
2141 fc6346c4 2019-03-27 stsp return err;
2142 708d8e67 2019-03-27 stsp }
2143 708d8e67 2019-03-27 stsp
2144 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2145 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2146 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2147 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2148 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2149 8da9e5f4 2019-01-12 stsp void *progress_arg;
2150 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2151 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2152 8da9e5f4 2019-01-12 stsp };
2153 8da9e5f4 2019-01-12 stsp
2154 512f0d0e 2019-01-02 stsp static const struct got_error *
2155 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2156 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2157 512f0d0e 2019-01-02 stsp {
2158 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2159 0584f854 2019-04-06 stsp
2160 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2161 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2162 512f0d0e 2019-01-02 stsp
2163 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2164 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2165 8da9e5f4 2019-01-12 stsp }
2166 512f0d0e 2019-01-02 stsp
2167 8da9e5f4 2019-01-12 stsp static const struct got_error *
2168 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2169 8da9e5f4 2019-01-12 stsp {
2170 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2171 7a9df742 2019-01-08 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 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2176 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2177 9d31a1d8 2018-03-11 stsp }
2178 9d31a1d8 2018-03-11 stsp
2179 9d31a1d8 2018-03-11 stsp static const struct got_error *
2180 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2181 9d31a1d8 2018-03-11 stsp {
2182 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2183 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2184 8da9e5f4 2019-01-12 stsp char *path;
2185 9d31a1d8 2018-03-11 stsp
2186 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2187 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2188 0584f854 2019-04-06 stsp
2189 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2190 63c5ca5d 2019-08-24 stsp return NULL;
2191 63c5ca5d 2019-08-24 stsp
2192 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2193 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2194 8da9e5f4 2019-01-12 stsp == -1)
2195 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2196 9d31a1d8 2018-03-11 stsp
2197 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2198 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2199 8da9e5f4 2019-01-12 stsp else
2200 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2201 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2202 9d31a1d8 2018-03-11 stsp
2203 8da9e5f4 2019-01-12 stsp free(path);
2204 0cd1c46a 2019-03-11 stsp return err;
2205 0cd1c46a 2019-03-11 stsp }
2206 0cd1c46a 2019-03-11 stsp
2207 b2118c49 2020-07-28 stsp const struct got_error *
2208 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2209 b2118c49 2020-07-28 stsp {
2210 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2211 b2118c49 2020-07-28 stsp
2212 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2213 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2214 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2215 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2216 b2118c49 2020-07-28 stsp }
2217 b2118c49 2020-07-28 stsp
2218 b2118c49 2020-07-28 stsp return NULL;
2219 b2118c49 2020-07-28 stsp }
2220 b2118c49 2020-07-28 stsp
2221 818c7501 2019-07-11 stsp static const struct got_error *
2222 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2223 0cd1c46a 2019-03-11 stsp {
2224 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2225 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2226 0cd1c46a 2019-03-11 stsp
2227 517bab73 2019-03-11 stsp *refname = NULL;
2228 517bab73 2019-03-11 stsp
2229 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2230 b2118c49 2020-07-28 stsp if (err)
2231 b2118c49 2020-07-28 stsp return err;
2232 0cd1c46a 2019-03-11 stsp
2233 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2234 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2235 0647c563 2019-03-11 stsp *refname = NULL;
2236 0cd1c46a 2019-03-11 stsp }
2237 517bab73 2019-03-11 stsp free(uuidstr);
2238 517bab73 2019-03-11 stsp return err;
2239 517bab73 2019-03-11 stsp }
2240 517bab73 2019-03-11 stsp
2241 818c7501 2019-07-11 stsp const struct got_error *
2242 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2243 818c7501 2019-07-11 stsp {
2244 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2245 818c7501 2019-07-11 stsp }
2246 818c7501 2019-07-11 stsp
2247 818c7501 2019-07-11 stsp static const struct got_error *
2248 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2249 818c7501 2019-07-11 stsp {
2250 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2251 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2252 818c7501 2019-07-11 stsp }
2253 818c7501 2019-07-11 stsp
2254 818c7501 2019-07-11 stsp static const struct got_error *
2255 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2256 818c7501 2019-07-11 stsp {
2257 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2258 818c7501 2019-07-11 stsp }
2259 818c7501 2019-07-11 stsp
2260 818c7501 2019-07-11 stsp static const struct got_error *
2261 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2262 818c7501 2019-07-11 stsp {
2263 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2264 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2265 818c7501 2019-07-11 stsp }
2266 818c7501 2019-07-11 stsp
2267 818c7501 2019-07-11 stsp static const struct got_error *
2268 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2269 818c7501 2019-07-11 stsp {
2270 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2271 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_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_tmp_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_TMP_REF_PREFIX);
2279 0ebf8283 2019-07-24 stsp }
2280 0ebf8283 2019-07-24 stsp
2281 0ebf8283 2019-07-24 stsp static const struct got_error *
2282 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_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_BRANCH_REF_PREFIX);
2286 0ebf8283 2019-07-24 stsp }
2287 0ebf8283 2019-07-24 stsp
2288 0ebf8283 2019-07-24 stsp static const struct got_error *
2289 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2290 0ebf8283 2019-07-24 stsp {
2291 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2292 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2293 818c7501 2019-07-11 stsp }
2294 818c7501 2019-07-11 stsp
2295 0ebf8283 2019-07-24 stsp static const struct got_error *
2296 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2297 0ebf8283 2019-07-24 stsp {
2298 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2299 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2300 0ebf8283 2019-07-24 stsp }
2301 818c7501 2019-07-11 stsp
2302 0ebf8283 2019-07-24 stsp const struct got_error *
2303 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2304 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2305 0ebf8283 2019-07-24 stsp {
2306 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2307 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2308 0ebf8283 2019-07-24 stsp *path = NULL;
2309 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2310 0ebf8283 2019-07-24 stsp }
2311 0ebf8283 2019-07-24 stsp return NULL;
2312 10604dce 2021-09-24 thomas }
2313 10604dce 2021-09-24 thomas
2314 10604dce 2021-09-24 thomas static const struct got_error *
2315 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2316 10604dce 2021-09-24 thomas {
2317 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2318 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2319 0ebf8283 2019-07-24 stsp }
2320 0ebf8283 2019-07-24 stsp
2321 10604dce 2021-09-24 thomas static const struct got_error *
2322 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2323 10604dce 2021-09-24 thomas {
2324 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2325 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2326 10604dce 2021-09-24 thomas }
2327 10604dce 2021-09-24 thomas
2328 517bab73 2019-03-11 stsp /*
2329 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2330 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2331 517bab73 2019-03-11 stsp */
2332 517bab73 2019-03-11 stsp static const struct got_error *
2333 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2334 517bab73 2019-03-11 stsp {
2335 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2336 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2337 517bab73 2019-03-11 stsp char *refname;
2338 517bab73 2019-03-11 stsp
2339 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2340 517bab73 2019-03-11 stsp if (err)
2341 517bab73 2019-03-11 stsp return err;
2342 0cd1c46a 2019-03-11 stsp
2343 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2344 0cd1c46a 2019-03-11 stsp if (err)
2345 0cd1c46a 2019-03-11 stsp goto done;
2346 0cd1c46a 2019-03-11 stsp
2347 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2348 0cd1c46a 2019-03-11 stsp done:
2349 0cd1c46a 2019-03-11 stsp free(refname);
2350 0cd1c46a 2019-03-11 stsp if (ref)
2351 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2352 3e3a69f1 2019-07-25 stsp return err;
2353 3e3a69f1 2019-07-25 stsp }
2354 3e3a69f1 2019-07-25 stsp
2355 3e3a69f1 2019-07-25 stsp static const struct got_error *
2356 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2357 3e3a69f1 2019-07-25 stsp {
2358 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2359 3e3a69f1 2019-07-25 stsp
2360 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2361 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2362 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2363 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2364 3e3a69f1 2019-07-25 stsp }
2365 9d31a1d8 2018-03-11 stsp return err;
2366 9d31a1d8 2018-03-11 stsp }
2367 9d31a1d8 2018-03-11 stsp
2368 3e3a69f1 2019-07-25 stsp
2369 ebf99748 2019-05-09 stsp static const struct got_error *
2370 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2371 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2372 ebf99748 2019-05-09 stsp {
2373 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2374 ebf99748 2019-05-09 stsp FILE *index = NULL;
2375 0cd1c46a 2019-03-11 stsp
2376 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2377 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2378 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2379 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2380 ebf99748 2019-05-09 stsp
2381 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2382 3e3a69f1 2019-07-25 stsp if (err)
2383 ebf99748 2019-05-09 stsp goto done;
2384 ebf99748 2019-05-09 stsp
2385 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2386 ebf99748 2019-05-09 stsp if (index == NULL) {
2387 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2388 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2389 ebf99748 2019-05-09 stsp } else {
2390 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2391 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2392 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2393 ebf99748 2019-05-09 stsp }
2394 ebf99748 2019-05-09 stsp done:
2395 ebf99748 2019-05-09 stsp if (err) {
2396 ebf99748 2019-05-09 stsp free(*fileindex_path);
2397 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2398 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2399 ebf99748 2019-05-09 stsp *fileindex = NULL;
2400 ebf99748 2019-05-09 stsp }
2401 ebf99748 2019-05-09 stsp return err;
2402 ebf99748 2019-05-09 stsp }
2403 c932eeeb 2019-05-22 stsp
2404 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2405 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2406 c932eeeb 2019-05-22 stsp const char *path;
2407 c932eeeb 2019-05-22 stsp size_t path_len;
2408 c932eeeb 2019-05-22 stsp const char *entry_name;
2409 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2410 a484d721 2019-06-10 stsp void *progress_arg;
2411 c932eeeb 2019-05-22 stsp };
2412 ebf99748 2019-05-09 stsp
2413 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2414 c932eeeb 2019-05-22 stsp static const struct got_error *
2415 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2416 c932eeeb 2019-05-22 stsp {
2417 1ee397ad 2019-07-12 stsp const struct got_error *err;
2418 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2419 c932eeeb 2019-05-22 stsp
2420 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2421 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2422 c932eeeb 2019-05-22 stsp return NULL;
2423 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2424 102ff934 2019-06-11 stsp return NULL;
2425 102ff934 2019-06-11 stsp
2426 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2427 a769b60b 2021-06-27 stsp return NULL;
2428 a769b60b 2021-06-27 stsp
2429 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2430 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2431 c932eeeb 2019-05-22 stsp return NULL;
2432 c932eeeb 2019-05-22 stsp
2433 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2434 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2435 edd02c5e 2019-07-11 stsp ie->path);
2436 1ee397ad 2019-07-12 stsp if (err)
2437 1ee397ad 2019-07-12 stsp return err;
2438 1ee397ad 2019-07-12 stsp }
2439 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2440 c932eeeb 2019-05-22 stsp return NULL;
2441 a615e0e7 2020-12-16 stsp }
2442 a615e0e7 2020-12-16 stsp
2443 a615e0e7 2020-12-16 stsp static const struct got_error *
2444 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2445 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2446 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2447 a615e0e7 2020-12-16 stsp {
2448 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2449 a615e0e7 2020-12-16 stsp
2450 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2451 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2452 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2453 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2454 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2455 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2456 a615e0e7 2020-12-16 stsp
2457 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2458 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2459 9c6338c4 2019-06-02 stsp }
2460 9c6338c4 2019-06-02 stsp
2461 9c6338c4 2019-06-02 stsp static const struct got_error *
2462 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2463 9c6338c4 2019-06-02 stsp {
2464 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2465 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2466 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2467 867630bb 2020-01-17 stsp struct timespec timeout;
2468 9c6338c4 2019-06-02 stsp
2469 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2470 9c6338c4 2019-06-02 stsp fileindex_path);
2471 9c6338c4 2019-06-02 stsp if (err)
2472 9c6338c4 2019-06-02 stsp goto done;
2473 9c6338c4 2019-06-02 stsp
2474 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2475 9c6338c4 2019-06-02 stsp if (err)
2476 9c6338c4 2019-06-02 stsp goto done;
2477 9c6338c4 2019-06-02 stsp
2478 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2479 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2480 9c6338c4 2019-06-02 stsp fileindex_path);
2481 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2482 9c6338c4 2019-06-02 stsp }
2483 867630bb 2020-01-17 stsp
2484 867630bb 2020-01-17 stsp /*
2485 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2486 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2487 867630bb 2020-01-17 stsp * was recorded in the file index.
2488 867630bb 2020-01-17 stsp */
2489 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2490 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2491 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2492 9c6338c4 2019-06-02 stsp done:
2493 9c6338c4 2019-06-02 stsp if (new_index)
2494 9c6338c4 2019-06-02 stsp fclose(new_index);
2495 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2496 9c6338c4 2019-06-02 stsp return err;
2497 c932eeeb 2019-05-22 stsp }
2498 6ced4176 2019-07-12 stsp
2499 6ced4176 2019-07-12 stsp static const struct got_error *
2500 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2501 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2502 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2503 945f9229 2022-04-16 thomas struct got_repository *repo)
2504 6ced4176 2019-07-12 stsp {
2505 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2506 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2507 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2508 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2509 6ced4176 2019-07-12 stsp
2510 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2511 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2512 6ced4176 2019-07-12 stsp *tree_id = NULL;
2513 6ced4176 2019-07-12 stsp
2514 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2515 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2516 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2517 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2518 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2519 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2520 6ced4176 2019-07-12 stsp goto done;
2521 6ced4176 2019-07-12 stsp }
2522 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2523 945f9229 2022-04-16 thomas worktree->path_prefix);
2524 6ced4176 2019-07-12 stsp if (err)
2525 6ced4176 2019-07-12 stsp goto done;
2526 6ced4176 2019-07-12 stsp return NULL;
2527 6ced4176 2019-07-12 stsp }
2528 6ced4176 2019-07-12 stsp
2529 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2530 6ced4176 2019-07-12 stsp
2531 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2532 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2533 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2534 6ced4176 2019-07-12 stsp goto done;
2535 6ced4176 2019-07-12 stsp }
2536 6ced4176 2019-07-12 stsp
2537 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2538 6ced4176 2019-07-12 stsp if (err)
2539 6ced4176 2019-07-12 stsp goto done;
2540 6ced4176 2019-07-12 stsp
2541 6ced4176 2019-07-12 stsp free(in_repo_path);
2542 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2543 6ced4176 2019-07-12 stsp
2544 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2545 6ced4176 2019-07-12 stsp if (err)
2546 6ced4176 2019-07-12 stsp goto done;
2547 c932eeeb 2019-05-22 stsp
2548 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2549 6ced4176 2019-07-12 stsp /* Check out a single file. */
2550 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2551 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2552 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2553 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2554 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2555 6ced4176 2019-07-12 stsp goto done;
2556 6ced4176 2019-07-12 stsp }
2557 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2558 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2559 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2560 6ced4176 2019-07-12 stsp goto done;
2561 6ced4176 2019-07-12 stsp }
2562 6ced4176 2019-07-12 stsp } else {
2563 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2564 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2565 6ced4176 2019-07-12 stsp if (err)
2566 6ced4176 2019-07-12 stsp return err;
2567 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2568 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2569 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2570 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2571 6ced4176 2019-07-12 stsp goto done;
2572 6ced4176 2019-07-12 stsp }
2573 6ced4176 2019-07-12 stsp }
2574 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2575 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2576 6ced4176 2019-07-12 stsp } else {
2577 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2578 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2579 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2580 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2581 6ced4176 2019-07-12 stsp goto done;
2582 6ced4176 2019-07-12 stsp }
2583 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2584 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2585 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2586 6ced4176 2019-07-12 stsp goto done;
2587 6ced4176 2019-07-12 stsp }
2588 6ced4176 2019-07-12 stsp }
2589 6ced4176 2019-07-12 stsp done:
2590 6ced4176 2019-07-12 stsp free(id);
2591 6ced4176 2019-07-12 stsp free(in_repo_path);
2592 6ced4176 2019-07-12 stsp if (err) {
2593 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2594 6ced4176 2019-07-12 stsp free(*tree_relpath);
2595 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2596 6ced4176 2019-07-12 stsp free(*tree_id);
2597 6ced4176 2019-07-12 stsp *tree_id = NULL;
2598 6ced4176 2019-07-12 stsp }
2599 07ed472d 2019-07-12 stsp return err;
2600 07ed472d 2019-07-12 stsp }
2601 07ed472d 2019-07-12 stsp
2602 07ed472d 2019-07-12 stsp static const struct got_error *
2603 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2604 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2605 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2606 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2607 07ed472d 2019-07-12 stsp {
2608 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2609 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2610 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2611 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2612 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2613 07ed472d 2019-07-12 stsp
2614 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2615 7f47418f 2019-12-20 stsp if (err) {
2616 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2617 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2618 7f47418f 2019-12-20 stsp goto done;
2619 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2620 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2621 7f47418f 2019-12-20 stsp if (err)
2622 7f47418f 2019-12-20 stsp return err;
2623 7f47418f 2019-12-20 stsp }
2624 07ed472d 2019-07-12 stsp
2625 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2626 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2627 07ed472d 2019-07-12 stsp if (err)
2628 07ed472d 2019-07-12 stsp goto done;
2629 07ed472d 2019-07-12 stsp
2630 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2631 07ed472d 2019-07-12 stsp if (err)
2632 07ed472d 2019-07-12 stsp goto done;
2633 07ed472d 2019-07-12 stsp
2634 07ed472d 2019-07-12 stsp if (entry_name &&
2635 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2636 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2637 07ed472d 2019-07-12 stsp goto done;
2638 07ed472d 2019-07-12 stsp }
2639 07ed472d 2019-07-12 stsp
2640 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2641 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2642 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2643 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2644 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2645 07ed472d 2019-07-12 stsp arg.repo = repo;
2646 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2647 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2648 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2649 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2650 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2651 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2652 07ed472d 2019-07-12 stsp done:
2653 07ed472d 2019-07-12 stsp if (tree)
2654 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2655 07ed472d 2019-07-12 stsp if (commit)
2656 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2657 6ced4176 2019-07-12 stsp return err;
2658 6ced4176 2019-07-12 stsp }
2659 6ced4176 2019-07-12 stsp
2660 9d31a1d8 2018-03-11 stsp const struct got_error *
2661 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2662 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2663 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2664 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2665 9d31a1d8 2018-03-11 stsp {
2666 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2667 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2668 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2669 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2670 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2671 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2672 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2673 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2674 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2675 f2ea84fa 2019-07-27 stsp int entry_type;
2676 f2ea84fa 2019-07-27 stsp char *relpath;
2677 f2ea84fa 2019-07-27 stsp char *entry_name;
2678 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2679 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2680 9d31a1d8 2018-03-11 stsp
2681 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2682 f2ea84fa 2019-07-27 stsp
2683 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2684 9d31a1d8 2018-03-11 stsp if (err)
2685 9d31a1d8 2018-03-11 stsp return err;
2686 945f9229 2022-04-16 thomas
2687 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2688 945f9229 2022-04-16 thomas worktree->base_commit_id);
2689 945f9229 2022-04-16 thomas if (err)
2690 945f9229 2022-04-16 thomas goto done;
2691 93a30277 2018-12-24 stsp
2692 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2693 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2694 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2695 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2696 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2697 f2ea84fa 2019-07-27 stsp goto done;
2698 f2ea84fa 2019-07-27 stsp }
2699 6ced4176 2019-07-12 stsp
2700 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2701 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2702 945f9229 2022-04-16 thomas worktree, repo);
2703 f2ea84fa 2019-07-27 stsp if (err) {
2704 f2ea84fa 2019-07-27 stsp free(tpd);
2705 6ced4176 2019-07-12 stsp goto done;
2706 6ced4176 2019-07-12 stsp }
2707 f2ea84fa 2019-07-27 stsp
2708 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2709 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2710 f2ea84fa 2019-07-27 stsp if (err) {
2711 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2712 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2713 f2ea84fa 2019-07-27 stsp free(tpd);
2714 f2ea84fa 2019-07-27 stsp goto done;
2715 f2ea84fa 2019-07-27 stsp }
2716 f2ea84fa 2019-07-27 stsp } else
2717 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2718 f2ea84fa 2019-07-27 stsp
2719 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2720 6ced4176 2019-07-12 stsp }
2721 6ced4176 2019-07-12 stsp
2722 51514078 2018-12-25 stsp /*
2723 51514078 2018-12-25 stsp * Read the file index.
2724 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2725 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2726 51514078 2018-12-25 stsp */
2727 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2728 8da9e5f4 2019-01-12 stsp if (err)
2729 c4cdcb68 2019-04-03 stsp goto done;
2730 c4cdcb68 2019-04-03 stsp
2731 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2732 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2733 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2734 f2ea84fa 2019-07-27 stsp
2735 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2736 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2737 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2738 f2ea84fa 2019-07-27 stsp if (err)
2739 f2ea84fa 2019-07-27 stsp break;
2740 f2ea84fa 2019-07-27 stsp
2741 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2742 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2743 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2744 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2745 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2746 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2747 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2748 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2749 f2ea84fa 2019-07-27 stsp if (err)
2750 f2ea84fa 2019-07-27 stsp break;
2751 f2ea84fa 2019-07-27 stsp
2752 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2753 711d9cd9 2019-07-12 stsp }
2754 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2755 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2756 af12c6b9 2019-06-04 stsp err = sync_err;
2757 9d31a1d8 2018-03-11 stsp done:
2758 9c6338c4 2019-06-02 stsp free(fileindex_path);
2759 edfa7d7f 2018-09-11 stsp if (tree)
2760 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2761 9d31a1d8 2018-03-11 stsp if (commit)
2762 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2763 5ade8233 2019-07-12 stsp if (fileindex)
2764 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2765 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2766 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2767 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2768 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2769 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2770 f2ea84fa 2019-07-27 stsp free(tpd);
2771 f2ea84fa 2019-07-27 stsp }
2772 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2773 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2774 9d31a1d8 2018-03-11 stsp err = unlockerr;
2775 f8d1f275 2019-02-04 stsp return err;
2776 f8d1f275 2019-02-04 stsp }
2777 f8d1f275 2019-02-04 stsp
2778 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2779 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2780 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2781 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2782 234035bc 2019-06-01 stsp void *progress_arg;
2783 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2784 234035bc 2019-06-01 stsp void *cancel_arg;
2785 f69721c3 2019-10-21 stsp const char *label_orig;
2786 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2787 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2788 234035bc 2019-06-01 stsp };
2789 234035bc 2019-06-01 stsp
2790 234035bc 2019-06-01 stsp static const struct got_error *
2791 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2792 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2793 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2794 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2795 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2796 234035bc 2019-06-01 stsp {
2797 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2798 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2799 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2800 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2801 234035bc 2019-06-01 stsp struct stat sb;
2802 234035bc 2019-06-01 stsp unsigned char status;
2803 234035bc 2019-06-01 stsp int local_changes_subsumed;
2804 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2805 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2806 234035bc 2019-06-01 stsp
2807 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2808 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2809 d6c87207 2019-08-02 stsp strlen(path2));
2810 1ee397ad 2019-07-12 stsp if (ie == NULL)
2811 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2812 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2813 234035bc 2019-06-01 stsp
2814 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2815 234035bc 2019-06-01 stsp path2) == -1)
2816 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2817 234035bc 2019-06-01 stsp
2818 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2819 7f91a133 2019-12-13 stsp repo);
2820 234035bc 2019-06-01 stsp if (err)
2821 234035bc 2019-06-01 stsp goto done;
2822 234035bc 2019-06-01 stsp
2823 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2824 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2825 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2826 234035bc 2019-06-01 stsp goto done;
2827 234035bc 2019-06-01 stsp }
2828 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2829 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2830 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2831 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2832 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2833 234035bc 2019-06-01 stsp goto done;
2834 234035bc 2019-06-01 stsp }
2835 234035bc 2019-06-01 stsp
2836 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2837 36bf999c 2020-07-23 stsp char *link_target2;
2838 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2839 36bf999c 2020-07-23 stsp if (err)
2840 36bf999c 2020-07-23 stsp goto done;
2841 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2842 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2843 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2844 36bf999c 2020-07-23 stsp free(link_target2);
2845 af57b12a 2020-07-23 stsp } else {
2846 1af628f4 2021-06-03 stsp int fd;
2847 1af628f4 2021-06-03 stsp
2848 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2849 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2850 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2851 1af628f4 2021-06-03 stsp goto done;
2852 1af628f4 2021-06-03 stsp }
2853 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2854 1af628f4 2021-06-03 stsp f_orig, blob1);
2855 1af628f4 2021-06-03 stsp if (err)
2856 1af628f4 2021-06-03 stsp goto done;
2857 1af628f4 2021-06-03 stsp
2858 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2859 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2860 1af628f4 2021-06-03 stsp goto done;
2861 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2862 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2863 1af628f4 2021-06-03 stsp if (err)
2864 1af628f4 2021-06-03 stsp goto done;
2865 1af628f4 2021-06-03 stsp
2866 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
2867 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2868 1af628f4 2021-06-03 stsp if (fd == -1) {
2869 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2870 1af628f4 2021-06-03 stsp ondisk_path);
2871 1af628f4 2021-06-03 stsp goto done;
2872 1af628f4 2021-06-03 stsp }
2873 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2874 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2875 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2876 1af628f4 2021-06-03 stsp ondisk_path);
2877 1af628f4 2021-06-03 stsp close(fd);
2878 1af628f4 2021-06-03 stsp goto done;
2879 1af628f4 2021-06-03 stsp }
2880 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2881 1af628f4 2021-06-03 stsp if (err)
2882 1af628f4 2021-06-03 stsp goto done;
2883 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2884 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2885 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2886 1af628f4 2021-06-03 stsp goto done;
2887 1af628f4 2021-06-03 stsp }
2888 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2889 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2890 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2891 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2892 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2893 af57b12a 2020-07-23 stsp }
2894 234035bc 2019-06-01 stsp } else if (blob1) {
2895 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2896 d6c87207 2019-08-02 stsp strlen(path1));
2897 1ee397ad 2019-07-12 stsp if (ie == NULL)
2898 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2899 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2900 2b92fad7 2019-06-02 stsp
2901 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2902 2b92fad7 2019-06-02 stsp path1) == -1)
2903 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2904 2b92fad7 2019-06-02 stsp
2905 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2906 7f91a133 2019-12-13 stsp repo);
2907 2b92fad7 2019-06-02 stsp if (err)
2908 2b92fad7 2019-06-02 stsp goto done;
2909 2b92fad7 2019-06-02 stsp
2910 2b92fad7 2019-06-02 stsp switch (status) {
2911 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2912 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2913 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2914 1ee397ad 2019-07-12 stsp if (err)
2915 1ee397ad 2019-07-12 stsp goto done;
2916 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2917 2b92fad7 2019-06-02 stsp if (err)
2918 2b92fad7 2019-06-02 stsp goto done;
2919 2b92fad7 2019-06-02 stsp if (ie)
2920 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2921 2b92fad7 2019-06-02 stsp break;
2922 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2923 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2924 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2925 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2926 1ee397ad 2019-07-12 stsp if (err)
2927 1ee397ad 2019-07-12 stsp goto done;
2928 2b92fad7 2019-06-02 stsp if (ie)
2929 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2930 2b92fad7 2019-06-02 stsp break;
2931 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2932 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2933 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2934 e8f02263 2022-01-23 thomas off_t blob1_size;
2935 0a22ca1a 2020-09-23 stsp /*
2936 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2937 0a22ca1a 2020-09-23 stsp * exists in the repository.
2938 0a22ca1a 2020-09-23 stsp */
2939 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
2940 e8f02263 2022-01-23 thomas &blob1_size, path1);
2941 0a22ca1a 2020-09-23 stsp if (err)
2942 0a22ca1a 2020-09-23 stsp goto done;
2943 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2944 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2945 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2946 0a22ca1a 2020-09-23 stsp if (err)
2947 0a22ca1a 2020-09-23 stsp goto done;
2948 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2949 0a22ca1a 2020-09-23 stsp path1);
2950 0a22ca1a 2020-09-23 stsp if (err)
2951 0a22ca1a 2020-09-23 stsp goto done;
2952 0a22ca1a 2020-09-23 stsp if (ie)
2953 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2954 0a22ca1a 2020-09-23 stsp ie);
2955 0a22ca1a 2020-09-23 stsp } else {
2956 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2957 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2958 0a22ca1a 2020-09-23 stsp }
2959 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2960 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2961 0a22ca1a 2020-09-23 stsp free(id);
2962 0a22ca1a 2020-09-23 stsp if (err)
2963 0a22ca1a 2020-09-23 stsp goto done;
2964 0a22ca1a 2020-09-23 stsp break;
2965 0a22ca1a 2020-09-23 stsp }
2966 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2967 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2968 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2969 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2970 1ee397ad 2019-07-12 stsp if (err)
2971 1ee397ad 2019-07-12 stsp goto done;
2972 2b92fad7 2019-06-02 stsp break;
2973 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2974 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2975 1ee397ad 2019-07-12 stsp if (err)
2976 1ee397ad 2019-07-12 stsp goto done;
2977 2b92fad7 2019-06-02 stsp break;
2978 2b92fad7 2019-06-02 stsp default:
2979 2b92fad7 2019-06-02 stsp break;
2980 2b92fad7 2019-06-02 stsp }
2981 234035bc 2019-06-01 stsp } else if (blob2) {
2982 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2983 234035bc 2019-06-01 stsp path2) == -1)
2984 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2985 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2986 d6c87207 2019-08-02 stsp strlen(path2));
2987 234035bc 2019-06-01 stsp if (ie) {
2988 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2989 7f91a133 2019-12-13 stsp -1, NULL, repo);
2990 234035bc 2019-06-01 stsp if (err)
2991 234035bc 2019-06-01 stsp goto done;
2992 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2993 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2994 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2995 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2996 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2997 1ee397ad 2019-07-12 stsp status, path2);
2998 234035bc 2019-06-01 stsp goto done;
2999 234035bc 2019-06-01 stsp }
3000 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3001 36bf999c 2020-07-23 stsp char *link_target2;
3002 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3003 36bf999c 2020-07-23 stsp blob2);
3004 36bf999c 2020-07-23 stsp if (err)
3005 36bf999c 2020-07-23 stsp goto done;
3006 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3007 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3008 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3009 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3010 36bf999c 2020-07-23 stsp free(link_target2);
3011 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3012 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3013 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3014 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3015 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3016 526a746f 2020-07-23 stsp a->progress_arg);
3017 dfe9fba0 2020-07-23 stsp } else {
3018 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3019 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3020 526a746f 2020-07-23 stsp }
3021 dfe9fba0 2020-07-23 stsp if (err)
3022 dfe9fba0 2020-07-23 stsp goto done;
3023 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3024 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3025 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3026 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3027 234035bc 2019-06-01 stsp if (err)
3028 234035bc 2019-06-01 stsp goto done;
3029 234035bc 2019-06-01 stsp }
3030 234035bc 2019-06-01 stsp } else {
3031 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3032 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3033 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3034 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3035 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3036 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
3037 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
3038 2e1fa222 2020-07-23 stsp } else {
3039 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3040 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3041 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3042 2e1fa222 2020-07-23 stsp }
3043 234035bc 2019-06-01 stsp if (err)
3044 234035bc 2019-06-01 stsp goto done;
3045 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3046 234035bc 2019-06-01 stsp if (err)
3047 234035bc 2019-06-01 stsp goto done;
3048 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3049 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3050 3969253a 2020-03-07 stsp if (err) {
3051 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3052 3969253a 2020-03-07 stsp goto done;
3053 3969253a 2020-03-07 stsp }
3054 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3055 2b92fad7 2019-06-02 stsp if (err) {
3056 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3057 2b92fad7 2019-06-02 stsp goto done;
3058 2b92fad7 2019-06-02 stsp }
3059 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3060 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3061 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3062 2e1fa222 2020-07-23 stsp }
3063 234035bc 2019-06-01 stsp }
3064 234035bc 2019-06-01 stsp }
3065 234035bc 2019-06-01 stsp done:
3066 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3067 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3068 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3069 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3070 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3071 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3072 1af628f4 2021-06-03 stsp free(id_str);
3073 54d5be07 2021-06-03 stsp free(label_deriv2);
3074 234035bc 2019-06-01 stsp free(ondisk_path);
3075 234035bc 2019-06-01 stsp return err;
3076 234035bc 2019-06-01 stsp }
3077 234035bc 2019-06-01 stsp
3078 69de9dd4 2021-09-03 stsp static const struct got_error *
3079 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3080 69de9dd4 2021-09-03 stsp {
3081 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3082 69de9dd4 2021-09-03 stsp
3083 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3084 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3085 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3086 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3087 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3088 69de9dd4 2021-09-03 stsp
3089 69de9dd4 2021-09-03 stsp return NULL;
3090 69de9dd4 2021-09-03 stsp }
3091 69de9dd4 2021-09-03 stsp
3092 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3093 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3094 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3095 234035bc 2019-06-01 stsp struct got_repository *repo;
3096 234035bc 2019-06-01 stsp };
3097 234035bc 2019-06-01 stsp
3098 234035bc 2019-06-01 stsp static const struct got_error *
3099 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3100 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3101 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3102 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3103 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3104 234035bc 2019-06-01 stsp {
3105 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3106 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3107 234035bc 2019-06-01 stsp unsigned char status;
3108 234035bc 2019-06-01 stsp struct stat sb;
3109 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3110 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3111 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3112 234035bc 2019-06-01 stsp char *ondisk_path;
3113 234035bc 2019-06-01 stsp
3114 69de9dd4 2021-09-03 stsp if (id == NULL)
3115 69de9dd4 2021-09-03 stsp return NULL;
3116 234035bc 2019-06-01 stsp
3117 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3118 69de9dd4 2021-09-03 stsp if (ie == NULL)
3119 69de9dd4 2021-09-03 stsp return NULL;
3120 69de9dd4 2021-09-03 stsp
3121 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3122 234035bc 2019-06-01 stsp == -1)
3123 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3124 234035bc 2019-06-01 stsp
3125 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3126 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3127 5546d466 2021-09-02 stsp free(ondisk_path);
3128 234035bc 2019-06-01 stsp if (err)
3129 234035bc 2019-06-01 stsp return err;
3130 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3131 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3132 234035bc 2019-06-01 stsp
3133 234035bc 2019-06-01 stsp return NULL;
3134 234035bc 2019-06-01 stsp }
3135 234035bc 2019-06-01 stsp
3136 818c7501 2019-07-11 stsp static const struct got_error *
3137 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3138 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3139 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3140 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3141 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3142 234035bc 2019-06-01 stsp {
3143 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3144 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3145 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3146 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3147 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3148 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3149 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3150 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3151 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3152 234035bc 2019-06-01 stsp
3153 03415a1a 2019-06-02 stsp if (commit_id1) {
3154 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3155 945f9229 2022-04-16 thomas if (err)
3156 945f9229 2022-04-16 thomas goto done;
3157 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3158 03415a1a 2019-06-02 stsp worktree->path_prefix);
3159 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3160 03415a1a 2019-06-02 stsp goto done;
3161 69d57f3d 2020-07-31 stsp }
3162 69d57f3d 2020-07-31 stsp if (tree_id1) {
3163 69d57f3d 2020-07-31 stsp char *id_str;
3164 03415a1a 2019-06-02 stsp
3165 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3166 03415a1a 2019-06-02 stsp if (err)
3167 03415a1a 2019-06-02 stsp goto done;
3168 f69721c3 2019-10-21 stsp
3169 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3170 f69721c3 2019-10-21 stsp if (err)
3171 f69721c3 2019-10-21 stsp goto done;
3172 f69721c3 2019-10-21 stsp
3173 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3174 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3175 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3176 f69721c3 2019-10-21 stsp free(id_str);
3177 f69721c3 2019-10-21 stsp goto done;
3178 f69721c3 2019-10-21 stsp }
3179 f69721c3 2019-10-21 stsp free(id_str);
3180 a0f32f33 2022-06-13 thomas
3181 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3182 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3183 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3184 a0f32f33 2022-06-13 thomas goto done;
3185 a0f32f33 2022-06-13 thomas }
3186 03415a1a 2019-06-02 stsp }
3187 234035bc 2019-06-01 stsp
3188 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3189 945f9229 2022-04-16 thomas if (err)
3190 945f9229 2022-04-16 thomas goto done;
3191 945f9229 2022-04-16 thomas
3192 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3193 234035bc 2019-06-01 stsp worktree->path_prefix);
3194 234035bc 2019-06-01 stsp if (err)
3195 234035bc 2019-06-01 stsp goto done;
3196 234035bc 2019-06-01 stsp
3197 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3198 234035bc 2019-06-01 stsp if (err)
3199 234035bc 2019-06-01 stsp goto done;
3200 234035bc 2019-06-01 stsp
3201 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3202 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3203 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3204 19a6a6b5 2022-07-01 thomas goto done;
3205 19a6a6b5 2022-07-01 thomas }
3206 19a6a6b5 2022-07-01 thomas
3207 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3208 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3209 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3210 a0f32f33 2022-06-13 thomas goto done;
3211 a0f32f33 2022-06-13 thomas }
3212 a0f32f33 2022-06-13 thomas
3213 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3214 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3215 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3216 19a6a6b5 2022-07-01 thomas goto done;
3217 19a6a6b5 2022-07-01 thomas }
3218 19a6a6b5 2022-07-01 thomas
3219 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3220 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3221 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3222 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3223 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3224 69de9dd4 2021-09-03 stsp if (err)
3225 69de9dd4 2021-09-03 stsp goto done;
3226 69de9dd4 2021-09-03 stsp
3227 234035bc 2019-06-01 stsp arg.worktree = worktree;
3228 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3229 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3230 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3231 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3232 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3233 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3234 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3235 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3236 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3237 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3238 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3239 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3240 af12c6b9 2019-06-04 stsp err = sync_err;
3241 234035bc 2019-06-01 stsp done:
3242 945f9229 2022-04-16 thomas if (commit1)
3243 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3244 945f9229 2022-04-16 thomas if (commit2)
3245 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3246 234035bc 2019-06-01 stsp if (tree1)
3247 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3248 234035bc 2019-06-01 stsp if (tree2)
3249 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3250 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3251 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3252 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3253 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3254 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3255 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3256 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3257 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3258 f69721c3 2019-10-21 stsp free(label_orig);
3259 818c7501 2019-07-11 stsp return err;
3260 818c7501 2019-07-11 stsp }
3261 234035bc 2019-06-01 stsp
3262 818c7501 2019-07-11 stsp const struct got_error *
3263 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3264 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3265 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3266 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3267 818c7501 2019-07-11 stsp {
3268 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3269 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3270 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3271 818c7501 2019-07-11 stsp
3272 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3273 818c7501 2019-07-11 stsp if (err)
3274 818c7501 2019-07-11 stsp return err;
3275 818c7501 2019-07-11 stsp
3276 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3277 818c7501 2019-07-11 stsp if (err)
3278 818c7501 2019-07-11 stsp goto done;
3279 818c7501 2019-07-11 stsp
3280 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3281 69de9dd4 2021-09-03 stsp worktree);
3282 818c7501 2019-07-11 stsp if (err)
3283 818c7501 2019-07-11 stsp goto done;
3284 818c7501 2019-07-11 stsp
3285 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3286 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3287 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3288 818c7501 2019-07-11 stsp done:
3289 818c7501 2019-07-11 stsp if (fileindex)
3290 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3291 818c7501 2019-07-11 stsp free(fileindex_path);
3292 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3293 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3294 234035bc 2019-06-01 stsp err = unlockerr;
3295 234035bc 2019-06-01 stsp return err;
3296 234035bc 2019-06-01 stsp }
3297 234035bc 2019-06-01 stsp
3298 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3299 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3300 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3301 927df6b7 2019-02-10 stsp const char *status_path;
3302 927df6b7 2019-02-10 stsp size_t status_path_len;
3303 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3304 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3305 f8d1f275 2019-02-04 stsp void *status_arg;
3306 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3307 f8d1f275 2019-02-04 stsp void *cancel_arg;
3308 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3309 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3310 f2a9dc41 2019-12-13 tracey int report_unchanged;
3311 3143d852 2020-06-25 stsp int no_ignores;
3312 f8d1f275 2019-02-04 stsp };
3313 88d0e355 2019-08-03 stsp
3314 f8d1f275 2019-02-04 stsp static const struct got_error *
3315 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3316 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3317 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3318 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3319 927df6b7 2019-02-10 stsp {
3320 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3321 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3322 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3323 927df6b7 2019-02-10 stsp struct stat sb;
3324 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3325 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3326 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3327 927df6b7 2019-02-10 stsp
3328 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3329 98eaaa12 2019-08-03 stsp if (err)
3330 98eaaa12 2019-08-03 stsp return err;
3331 98eaaa12 2019-08-03 stsp
3332 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3333 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3334 98eaaa12 2019-08-03 stsp return NULL;
3335 98eaaa12 2019-08-03 stsp
3336 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3337 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3338 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3339 98eaaa12 2019-08-03 stsp }
3340 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3341 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3342 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3343 927df6b7 2019-02-10 stsp }
3344 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3345 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3346 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3347 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3348 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3349 98eaaa12 2019-08-03 stsp }
3350 98eaaa12 2019-08-03 stsp
3351 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3352 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3353 927df6b7 2019-02-10 stsp }
3354 927df6b7 2019-02-10 stsp
3355 927df6b7 2019-02-10 stsp static const struct got_error *
3356 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3357 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3358 f8d1f275 2019-02-04 stsp {
3359 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3360 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3361 f8d1f275 2019-02-04 stsp char *abspath;
3362 f8d1f275 2019-02-04 stsp
3363 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3364 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3365 0584f854 2019-04-06 stsp
3366 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3367 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3368 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3369 927df6b7 2019-02-10 stsp return NULL;
3370 927df6b7 2019-02-10 stsp
3371 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3372 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3373 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3374 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3375 f8d1f275 2019-02-04 stsp } else {
3376 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3377 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3378 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3379 f8d1f275 2019-02-04 stsp }
3380 f8d1f275 2019-02-04 stsp
3381 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3382 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3383 f8d1f275 2019-02-04 stsp free(abspath);
3384 9d31a1d8 2018-03-11 stsp return err;
3385 9d31a1d8 2018-03-11 stsp }
3386 f8d1f275 2019-02-04 stsp
3387 f8d1f275 2019-02-04 stsp static const struct got_error *
3388 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3389 f8d1f275 2019-02-04 stsp {
3390 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3391 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3392 2ec1f75b 2019-03-26 stsp unsigned char status;
3393 927df6b7 2019-02-10 stsp
3394 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3395 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3396 0584f854 2019-04-06 stsp
3397 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3398 927df6b7 2019-02-10 stsp return NULL;
3399 927df6b7 2019-02-10 stsp
3400 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3401 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3402 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3403 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3404 2ec1f75b 2019-03-26 stsp else
3405 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3406 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3407 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3408 6841da00 2019-08-08 stsp }
3409 6841da00 2019-08-08 stsp
3410 ef20f542 2022-06-26 thomas static void
3411 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3412 6841da00 2019-08-08 stsp {
3413 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3414 6841da00 2019-08-08 stsp
3415 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3416 6841da00 2019-08-08 stsp free((char *)pe->path);
3417 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3418 6841da00 2019-08-08 stsp }
3419 6841da00 2019-08-08 stsp
3420 ef20f542 2022-06-26 thomas static void
3421 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3422 6841da00 2019-08-08 stsp {
3423 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3424 6841da00 2019-08-08 stsp
3425 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3426 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3427 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3428 6841da00 2019-08-08 stsp free((char *)pe->path);
3429 6841da00 2019-08-08 stsp }
3430 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3431 6841da00 2019-08-08 stsp }
3432 6841da00 2019-08-08 stsp
3433 6841da00 2019-08-08 stsp static const struct got_error *
3434 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3435 6841da00 2019-08-08 stsp {
3436 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3437 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3438 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3439 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3440 6841da00 2019-08-08 stsp size_t linesize = 0;
3441 6841da00 2019-08-08 stsp ssize_t linelen;
3442 6841da00 2019-08-08 stsp
3443 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3444 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3445 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3446 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3447 6841da00 2019-08-08 stsp
3448 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3449 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3450 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3451 bd8de430 2019-10-04 stsp
3452 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3453 bd8de430 2019-10-04 stsp if (line[0] == '#')
3454 bd8de430 2019-10-04 stsp continue;
3455 bd8de430 2019-10-04 stsp
3456 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3457 bd8de430 2019-10-04 stsp if (line[0] == '!')
3458 bd8de430 2019-10-04 stsp continue;
3459 bd8de430 2019-10-04 stsp
3460 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3461 6841da00 2019-08-08 stsp line) == -1) {
3462 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3463 6841da00 2019-08-08 stsp goto done;
3464 6841da00 2019-08-08 stsp }
3465 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3466 6841da00 2019-08-08 stsp if (err)
3467 6841da00 2019-08-08 stsp goto done;
3468 6841da00 2019-08-08 stsp }
3469 6841da00 2019-08-08 stsp if (ferror(f)) {
3470 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3471 6841da00 2019-08-08 stsp goto done;
3472 6841da00 2019-08-08 stsp }
3473 6841da00 2019-08-08 stsp
3474 6841da00 2019-08-08 stsp dirpath = strdup(path);
3475 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3476 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3477 6841da00 2019-08-08 stsp goto done;
3478 6841da00 2019-08-08 stsp }
3479 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3480 6841da00 2019-08-08 stsp done:
3481 6841da00 2019-08-08 stsp free(line);
3482 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3483 6841da00 2019-08-08 stsp free(dirpath);
3484 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3485 6841da00 2019-08-08 stsp }
3486 6841da00 2019-08-08 stsp return err;
3487 6841da00 2019-08-08 stsp }
3488 6841da00 2019-08-08 stsp
3489 ef20f542 2022-06-26 thomas static int
3490 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3491 6841da00 2019-08-08 stsp {
3492 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3493 bd8de430 2019-10-04 stsp
3494 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3495 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3496 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3497 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3498 bd8de430 2019-10-04 stsp
3499 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3500 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3501 6841da00 2019-08-08 stsp
3502 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3503 bd8de430 2019-10-04 stsp continue;
3504 bd8de430 2019-10-04 stsp pattern += 3;
3505 bd8de430 2019-10-04 stsp p = path;
3506 bd8de430 2019-10-04 stsp while (*p) {
3507 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3508 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3509 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3510 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3511 bd8de430 2019-10-04 stsp p++;
3512 bd8de430 2019-10-04 stsp while (*p == '/')
3513 bd8de430 2019-10-04 stsp p++;
3514 bd8de430 2019-10-04 stsp continue;
3515 bd8de430 2019-10-04 stsp }
3516 bd8de430 2019-10-04 stsp return 1;
3517 bd8de430 2019-10-04 stsp }
3518 bd8de430 2019-10-04 stsp }
3519 bd8de430 2019-10-04 stsp }
3520 bd8de430 2019-10-04 stsp
3521 6841da00 2019-08-08 stsp /*
3522 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3523 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3524 6841da00 2019-08-08 stsp * ignores backwards.
3525 6841da00 2019-08-08 stsp */
3526 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3527 6841da00 2019-08-08 stsp while (pe) {
3528 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3529 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3530 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3531 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3532 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3533 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3534 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3535 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3536 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3537 6841da00 2019-08-08 stsp continue;
3538 6841da00 2019-08-08 stsp return 1;
3539 6841da00 2019-08-08 stsp }
3540 6841da00 2019-08-08 stsp }
3541 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3542 6841da00 2019-08-08 stsp }
3543 6841da00 2019-08-08 stsp
3544 6841da00 2019-08-08 stsp return 0;
3545 6841da00 2019-08-08 stsp }
3546 6841da00 2019-08-08 stsp
3547 6841da00 2019-08-08 stsp static const struct got_error *
3548 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3549 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3550 6841da00 2019-08-08 stsp {
3551 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3552 6841da00 2019-08-08 stsp char *ignorespath;
3553 886cec17 2019-12-15 stsp int fd = -1;
3554 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3555 6841da00 2019-08-08 stsp
3556 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3557 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3558 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3559 6841da00 2019-08-08 stsp
3560 886cec17 2019-12-15 stsp if (dirfd != -1) {
3561 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3562 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3563 886cec17 2019-12-15 stsp if (fd == -1) {
3564 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3565 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3566 886cec17 2019-12-15 stsp ignorespath);
3567 886cec17 2019-12-15 stsp } else {
3568 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3569 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3570 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3571 886cec17 2019-12-15 stsp ignorespath);
3572 886cec17 2019-12-15 stsp else {
3573 886cec17 2019-12-15 stsp fd = -1;
3574 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3575 886cec17 2019-12-15 stsp }
3576 886cec17 2019-12-15 stsp }
3577 886cec17 2019-12-15 stsp } else {
3578 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3579 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3580 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3581 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3582 886cec17 2019-12-15 stsp ignorespath);
3583 886cec17 2019-12-15 stsp } else
3584 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3585 886cec17 2019-12-15 stsp }
3586 6841da00 2019-08-08 stsp
3587 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3588 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3589 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3590 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3591 6841da00 2019-08-08 stsp free(ignorespath);
3592 6841da00 2019-08-08 stsp return err;
3593 f8d1f275 2019-02-04 stsp }
3594 f8d1f275 2019-02-04 stsp
3595 f8d1f275 2019-02-04 stsp static const struct got_error *
3596 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3597 6092c299 2021-10-04 thomas int dirfd)
3598 f8d1f275 2019-02-04 stsp {
3599 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3600 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3601 f8d1f275 2019-02-04 stsp char *path = NULL;
3602 6092c299 2021-10-04 thomas
3603 6092c299 2021-10-04 thomas if (ignore != NULL)
3604 6092c299 2021-10-04 thomas *ignore = 0;
3605 f8d1f275 2019-02-04 stsp
3606 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3607 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3608 0584f854 2019-04-06 stsp
3609 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3610 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3611 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3612 f8d1f275 2019-02-04 stsp } else {
3613 f8d1f275 2019-02-04 stsp path = de->d_name;
3614 f8d1f275 2019-02-04 stsp }
3615 f8d1f275 2019-02-04 stsp
3616 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3617 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3618 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3619 6092c299 2021-10-04 thomas *ignore = 1;
3620 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3621 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3622 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3623 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3624 f8d1f275 2019-02-04 stsp if (parent_path[0])
3625 f8d1f275 2019-02-04 stsp free(path);
3626 b72f483a 2019-02-05 stsp return err;
3627 f8d1f275 2019-02-04 stsp }
3628 f8d1f275 2019-02-04 stsp
3629 347d1d3e 2019-07-12 stsp static const struct got_error *
3630 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3631 3143d852 2020-06-25 stsp {
3632 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3633 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3634 3143d852 2020-06-25 stsp
3635 3143d852 2020-06-25 stsp if (a->no_ignores)
3636 3143d852 2020-06-25 stsp return NULL;
3637 3143d852 2020-06-25 stsp
3638 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3639 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3640 3143d852 2020-06-25 stsp if (err)
3641 3143d852 2020-06-25 stsp return err;
3642 3143d852 2020-06-25 stsp
3643 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3644 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3645 3143d852 2020-06-25 stsp
3646 3143d852 2020-06-25 stsp return err;
3647 3143d852 2020-06-25 stsp }
3648 3143d852 2020-06-25 stsp
3649 3143d852 2020-06-25 stsp static const struct got_error *
3650 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3651 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3652 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3653 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3654 abb4604f 2019-07-27 stsp {
3655 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3656 abb4604f 2019-07-27 stsp struct stat sb;
3657 ff56836b 2021-07-08 stsp
3658 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3659 abb4604f 2019-07-27 stsp if (ie)
3660 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3661 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3662 abb4604f 2019-07-27 stsp
3663 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3664 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3665 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3666 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3667 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3668 abb4604f 2019-07-27 stsp }
3669 abb4604f 2019-07-27 stsp
3670 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3671 fe1d8685 2022-02-12 thomas return NULL;
3672 fe1d8685 2022-02-12 thomas
3673 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3674 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3675 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3676 abb4604f 2019-07-27 stsp
3677 abb4604f 2019-07-27 stsp return NULL;
3678 3143d852 2020-06-25 stsp }
3679 3143d852 2020-06-25 stsp
3680 3143d852 2020-06-25 stsp static const struct got_error *
3681 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3682 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3683 3143d852 2020-06-25 stsp {
3684 3143d852 2020-06-25 stsp const struct got_error *err;
3685 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3686 3143d852 2020-06-25 stsp
3687 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3688 3143d852 2020-06-25 stsp ".cvsignore");
3689 3143d852 2020-06-25 stsp if (err)
3690 3143d852 2020-06-25 stsp return err;
3691 3143d852 2020-06-25 stsp
3692 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3693 3143d852 2020-06-25 stsp ".gitignore");
3694 3143d852 2020-06-25 stsp if (err)
3695 3143d852 2020-06-25 stsp return err;
3696 3143d852 2020-06-25 stsp
3697 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3698 3143d852 2020-06-25 stsp if (err) {
3699 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3700 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3701 3143d852 2020-06-25 stsp return err;
3702 3143d852 2020-06-25 stsp }
3703 3143d852 2020-06-25 stsp for (;;) {
3704 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3705 3143d852 2020-06-25 stsp ".cvsignore");
3706 3143d852 2020-06-25 stsp if (err)
3707 3143d852 2020-06-25 stsp break;
3708 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3709 3143d852 2020-06-25 stsp ".gitignore");
3710 3143d852 2020-06-25 stsp if (err)
3711 3143d852 2020-06-25 stsp break;
3712 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3713 3143d852 2020-06-25 stsp if (err) {
3714 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3715 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3716 3143d852 2020-06-25 stsp break;
3717 3143d852 2020-06-25 stsp }
3718 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3719 ff56836b 2021-07-08 stsp break;
3720 b737c85a 2020-06-26 stsp free(parent_path);
3721 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3722 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3723 3143d852 2020-06-25 stsp }
3724 3143d852 2020-06-25 stsp
3725 b737c85a 2020-06-26 stsp free(parent_path);
3726 b737c85a 2020-06-26 stsp free(next_parent_path);
3727 3143d852 2020-06-25 stsp return err;
3728 abb4604f 2019-07-27 stsp }
3729 abb4604f 2019-07-27 stsp
3730 abb4604f 2019-07-27 stsp static const struct got_error *
3731 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3732 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3733 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3734 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3735 f2a9dc41 2019-12-13 tracey int report_unchanged)
3736 f8d1f275 2019-02-04 stsp {
3737 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3738 6fc93f37 2019-12-13 stsp int fd = -1;
3739 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3740 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3741 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3742 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3743 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3744 3143d852 2020-06-25 stsp
3745 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3746 f8d1f275 2019-02-04 stsp
3747 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3748 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3749 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3750 8dc303cc 2019-07-27 stsp
3751 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3752 a78810f8 2022-03-13 thomas if (ie) {
3753 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3754 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3755 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3756 a78810f8 2022-03-13 thomas goto done;
3757 a78810f8 2022-03-13 thomas }
3758 a78810f8 2022-03-13 thomas
3759 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3760 6fc93f37 2019-12-13 stsp if (fd == -1) {
3761 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3762 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3763 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3764 ff56836b 2021-07-08 stsp else {
3765 ff56836b 2021-07-08 stsp if (!no_ignores) {
3766 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3767 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3768 ff56836b 2021-07-08 stsp if (err)
3769 ff56836b 2021-07-08 stsp goto done;
3770 ff56836b 2021-07-08 stsp }
3771 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3772 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3773 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3774 ff56836b 2021-07-08 stsp }
3775 abb4604f 2019-07-27 stsp } else {
3776 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3777 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3778 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3779 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3780 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3781 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3782 abb4604f 2019-07-27 stsp arg.status_path = path;
3783 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3784 abb4604f 2019-07-27 stsp arg.repo = repo;
3785 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3786 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3787 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3788 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3789 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3790 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3791 022fae89 2019-12-06 tracey if (!no_ignores) {
3792 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3793 3143d852 2020-06-25 stsp worktree->root_path, path);
3794 3143d852 2020-06-25 stsp if (err)
3795 3143d852 2020-06-25 stsp goto done;
3796 022fae89 2019-12-06 tracey }
3797 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3798 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3799 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3800 927df6b7 2019-02-10 stsp }
3801 3143d852 2020-06-25 stsp done:
3802 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3803 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3804 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3805 927df6b7 2019-02-10 stsp free(ondisk_path);
3806 347d1d3e 2019-07-12 stsp return err;
3807 347d1d3e 2019-07-12 stsp }
3808 347d1d3e 2019-07-12 stsp
3809 347d1d3e 2019-07-12 stsp const struct got_error *
3810 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3811 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3812 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3813 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3814 347d1d3e 2019-07-12 stsp {
3815 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3816 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3817 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3818 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3819 347d1d3e 2019-07-12 stsp
3820 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3821 347d1d3e 2019-07-12 stsp if (err)
3822 347d1d3e 2019-07-12 stsp return err;
3823 347d1d3e 2019-07-12 stsp
3824 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3825 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3826 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3827 f6343036 2021-06-22 stsp no_ignores, 0);
3828 72ea6654 2019-07-27 stsp if (err)
3829 72ea6654 2019-07-27 stsp break;
3830 72ea6654 2019-07-27 stsp }
3831 f8d1f275 2019-02-04 stsp free(fileindex_path);
3832 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3833 f8d1f275 2019-02-04 stsp return err;
3834 f8d1f275 2019-02-04 stsp }
3835 6c7ab921 2019-03-18 stsp
3836 6c7ab921 2019-03-18 stsp const struct got_error *
3837 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3838 6c7ab921 2019-03-18 stsp const char *arg)
3839 6c7ab921 2019-03-18 stsp {
3840 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3841 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3842 6c7ab921 2019-03-18 stsp size_t len;
3843 00bb5ea0 2020-07-23 stsp struct stat sb;
3844 01740607 2020-11-04 stsp char *abspath = NULL;
3845 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3846 6c7ab921 2019-03-18 stsp
3847 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3848 6c7ab921 2019-03-18 stsp
3849 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3850 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3851 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3852 00bb5ea0 2020-07-23 stsp
3853 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3854 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3855 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3856 00bb5ea0 2020-07-23 stsp goto done;
3857 00bb5ea0 2020-07-23 stsp }
3858 727173c3 2020-11-06 stsp sb.st_mode = 0;
3859 00bb5ea0 2020-07-23 stsp }
3860 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3861 00bb5ea0 2020-07-23 stsp /*
3862 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3863 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3864 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3865 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3866 00bb5ea0 2020-07-23 stsp */
3867 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3868 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3869 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3870 00bb5ea0 2020-07-23 stsp goto done;
3871 00bb5ea0 2020-07-23 stsp }
3872 00bb5ea0 2020-07-23 stsp
3873 00bb5ea0 2020-07-23 stsp }
3874 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3875 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3876 00bb5ea0 2020-07-23 stsp if (err)
3877 d0710d08 2019-07-22 stsp goto done;
3878 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3879 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3880 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3881 00bb5ea0 2020-07-23 stsp goto done;
3882 00bb5ea0 2020-07-23 stsp }
3883 00bb5ea0 2020-07-23 stsp } else {
3884 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3885 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3886 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3887 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3888 00bb5ea0 2020-07-23 stsp goto done;
3889 00bb5ea0 2020-07-23 stsp }
3890 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3891 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3892 01740607 2020-11-04 stsp goto done;
3893 01740607 2020-11-04 stsp }
3894 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3895 01740607 2020-11-04 stsp sizeof(canonpath));
3896 01740607 2020-11-04 stsp if (err)
3897 00bb5ea0 2020-07-23 stsp goto done;
3898 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3899 01740607 2020-11-04 stsp if (resolved == NULL) {
3900 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3901 01740607 2020-11-04 stsp goto done;
3902 00bb5ea0 2020-07-23 stsp }
3903 d0710d08 2019-07-22 stsp }
3904 d0710d08 2019-07-22 stsp }
3905 6c7ab921 2019-03-18 stsp
3906 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3907 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3908 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3909 6c7ab921 2019-03-18 stsp goto done;
3910 6c7ab921 2019-03-18 stsp }
3911 6c7ab921 2019-03-18 stsp
3912 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3913 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3914 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3915 f6d88e1a 2019-05-29 stsp if (err)
3916 f6d88e1a 2019-05-29 stsp goto done;
3917 f6d88e1a 2019-05-29 stsp } else {
3918 f6d88e1a 2019-05-29 stsp path = strdup("");
3919 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3920 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3921 f6d88e1a 2019-05-29 stsp goto done;
3922 f6d88e1a 2019-05-29 stsp }
3923 6c7ab921 2019-03-18 stsp }
3924 6c7ab921 2019-03-18 stsp
3925 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3926 6c7ab921 2019-03-18 stsp len = strlen(path);
3927 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3928 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3929 6c7ab921 2019-03-18 stsp len--;
3930 6c7ab921 2019-03-18 stsp }
3931 6c7ab921 2019-03-18 stsp done:
3932 01740607 2020-11-04 stsp free(abspath);
3933 6c7ab921 2019-03-18 stsp free(resolved);
3934 d0710d08 2019-07-22 stsp free(cwd);
3935 6c7ab921 2019-03-18 stsp if (err == NULL)
3936 6c7ab921 2019-03-18 stsp *wt_path = path;
3937 6c7ab921 2019-03-18 stsp else
3938 6c7ab921 2019-03-18 stsp free(path);
3939 d00136be 2019-03-26 stsp return err;
3940 d00136be 2019-03-26 stsp }
3941 d00136be 2019-03-26 stsp
3942 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3943 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3944 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3945 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3946 4e68cba3 2019-11-23 stsp void *progress_arg;
3947 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3948 4e68cba3 2019-11-23 stsp };
3949 4e68cba3 2019-11-23 stsp
3950 4e68cba3 2019-11-23 stsp static const struct got_error *
3951 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3952 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3953 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3954 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3955 07f5b47a 2019-06-02 stsp {
3956 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3957 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3958 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3959 6d022e97 2019-08-04 stsp struct stat sb;
3960 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3961 07f5b47a 2019-06-02 stsp
3962 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3963 dbb83fbd 2019-12-12 stsp relpath) == -1)
3964 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3965 dbb83fbd 2019-12-12 stsp
3966 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3967 6d022e97 2019-08-04 stsp if (ie) {
3968 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3969 12463d8b 2019-12-13 stsp de_name, a->repo);
3970 6d022e97 2019-08-04 stsp if (err)
3971 dbb83fbd 2019-12-12 stsp goto done;
3972 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3973 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3974 dbb83fbd 2019-12-12 stsp goto done;
3975 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3976 dbb83fbd 2019-12-12 stsp if (err)
3977 dbb83fbd 2019-12-12 stsp goto done;
3978 6d022e97 2019-08-04 stsp }
3979 07f5b47a 2019-06-02 stsp
3980 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3981 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
3982 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
3983 84bf00a6 2022-02-12 thomas else
3984 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3985 dbb83fbd 2019-12-12 stsp goto done;
3986 4e68cba3 2019-11-23 stsp }
3987 07f5b47a 2019-06-02 stsp
3988 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3989 4e68cba3 2019-11-23 stsp if (err)
3990 4e68cba3 2019-11-23 stsp goto done;
3991 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3992 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3993 3969253a 2020-03-07 stsp if (err) {
3994 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3995 3969253a 2020-03-07 stsp goto done;
3996 3969253a 2020-03-07 stsp }
3997 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3998 07f5b47a 2019-06-02 stsp if (err) {
3999 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4000 4e68cba3 2019-11-23 stsp goto done;
4001 07f5b47a 2019-06-02 stsp }
4002 4e68cba3 2019-11-23 stsp done:
4003 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4004 dbb83fbd 2019-12-12 stsp if (err)
4005 dbb83fbd 2019-12-12 stsp return err;
4006 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4007 dbb83fbd 2019-12-12 stsp return NULL;
4008 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4009 07f5b47a 2019-06-02 stsp }
4010 07f5b47a 2019-06-02 stsp
4011 d00136be 2019-03-26 stsp const struct got_error *
4012 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4013 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4014 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4015 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4016 d00136be 2019-03-26 stsp {
4017 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4018 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4019 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4020 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4021 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4022 d00136be 2019-03-26 stsp
4023 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4024 d00136be 2019-03-26 stsp if (err)
4025 d00136be 2019-03-26 stsp return err;
4026 d00136be 2019-03-26 stsp
4027 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4028 d00136be 2019-03-26 stsp if (err)
4029 d00136be 2019-03-26 stsp goto done;
4030 d00136be 2019-03-26 stsp
4031 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4032 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4033 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4034 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4035 4e68cba3 2019-11-23 stsp saa.repo = repo;
4036 4e68cba3 2019-11-23 stsp
4037 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4038 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4039 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4040 1dd54920 2019-05-11 stsp if (err)
4041 af12c6b9 2019-06-04 stsp break;
4042 1dd54920 2019-05-11 stsp }
4043 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4044 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4045 af12c6b9 2019-06-04 stsp err = sync_err;
4046 d00136be 2019-03-26 stsp done:
4047 fb399478 2019-07-12 stsp free(fileindex_path);
4048 d00136be 2019-03-26 stsp if (fileindex)
4049 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4050 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4051 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4052 d00136be 2019-03-26 stsp err = unlockerr;
4053 6c7ab921 2019-03-18 stsp return err;
4054 6c7ab921 2019-03-18 stsp }
4055 17ed4618 2019-06-02 stsp
4056 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4057 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4058 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4059 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4060 f2a9dc41 2019-12-13 tracey void *progress_arg;
4061 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4062 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4063 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4064 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4065 766841c2 2020-08-13 stsp const char *status_codes;
4066 f2a9dc41 2019-12-13 tracey };
4067 f2a9dc41 2019-12-13 tracey
4068 f2a9dc41 2019-12-13 tracey static const struct got_error *
4069 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4070 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4071 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4072 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4073 17ed4618 2019-06-02 stsp {
4074 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4075 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4076 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4077 17ed4618 2019-06-02 stsp struct stat sb;
4078 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4079 d64cc78a 2022-01-25 thomas
4080 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4081 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4082 d64cc78a 2022-01-25 thomas return NULL;
4083 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4084 d64cc78a 2022-01-25 thomas }
4085 17ed4618 2019-06-02 stsp
4086 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4087 17ed4618 2019-06-02 stsp if (ie == NULL)
4088 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4089 2ec1f75b 2019-03-26 stsp
4090 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4091 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4092 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4093 9acbc4fa 2019-08-03 stsp return NULL;
4094 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4095 9acbc4fa 2019-08-03 stsp }
4096 9acbc4fa 2019-08-03 stsp
4097 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4098 f2a9dc41 2019-12-13 tracey relpath) == -1)
4099 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4100 f2a9dc41 2019-12-13 tracey
4101 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4102 12463d8b 2019-12-13 stsp a->repo);
4103 17ed4618 2019-06-02 stsp if (err)
4104 f2a9dc41 2019-12-13 tracey goto done;
4105 17ed4618 2019-06-02 stsp
4106 766841c2 2020-08-13 stsp if (a->status_codes) {
4107 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4108 766841c2 2020-08-13 stsp int i;
4109 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4110 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4111 766841c2 2020-08-13 stsp break;
4112 766841c2 2020-08-13 stsp }
4113 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4114 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4115 766841c2 2020-08-13 stsp free(ondisk_path);
4116 766841c2 2020-08-13 stsp return NULL;
4117 766841c2 2020-08-13 stsp }
4118 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4119 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4120 766841c2 2020-08-13 stsp static char msg[64];
4121 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4122 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4123 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4124 766841c2 2020-08-13 stsp goto done;
4125 766841c2 2020-08-13 stsp }
4126 766841c2 2020-08-13 stsp }
4127 766841c2 2020-08-13 stsp
4128 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4129 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4130 f2a9dc41 2019-12-13 tracey goto done;
4131 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4132 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4133 f2a9dc41 2019-12-13 tracey goto done;
4134 f2a9dc41 2019-12-13 tracey }
4135 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4136 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4137 d64cc78a 2022-01-25 thomas goto done;
4138 d64cc78a 2022-01-25 thomas }
4139 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4140 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4141 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4142 f2a9dc41 2019-12-13 tracey goto done;
4143 f2a9dc41 2019-12-13 tracey }
4144 17ed4618 2019-06-02 stsp }
4145 17ed4618 2019-06-02 stsp
4146 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4147 20a2ad1c 2020-10-20 stsp size_t root_len;
4148 20a2ad1c 2020-10-20 stsp
4149 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4150 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4151 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4152 12463d8b 2019-12-13 stsp ondisk_path);
4153 12463d8b 2019-12-13 stsp goto done;
4154 12463d8b 2019-12-13 stsp }
4155 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4156 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4157 12463d8b 2019-12-13 stsp goto done;
4158 15341bfd 2020-03-05 tracey }
4159 15341bfd 2020-03-05 tracey
4160 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4161 20a2ad1c 2020-10-20 stsp do {
4162 20a2ad1c 2020-10-20 stsp char *parent;
4163 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4164 20a2ad1c 2020-10-20 stsp if (err)
4165 2513f20a 2020-10-20 stsp goto done;
4166 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4167 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4168 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4169 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4170 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4171 20a2ad1c 2020-10-20 stsp ondisk_path);
4172 15341bfd 2020-03-05 tracey break;
4173 15341bfd 2020-03-05 tracey }
4174 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4175 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4176 f2a9dc41 2019-12-13 tracey }
4177 17ed4618 2019-06-02 stsp
4178 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4179 f2a9dc41 2019-12-13 tracey done:
4180 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4181 f2a9dc41 2019-12-13 tracey if (err)
4182 f2a9dc41 2019-12-13 tracey return err;
4183 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4184 f2a9dc41 2019-12-13 tracey return NULL;
4185 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4186 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4187 17ed4618 2019-06-02 stsp }
4188 17ed4618 2019-06-02 stsp
4189 2ec1f75b 2019-03-26 stsp const struct got_error *
4190 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4191 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4192 766841c2 2020-08-13 stsp const char *status_codes,
4193 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4194 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4195 2ec1f75b 2019-03-26 stsp {
4196 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4197 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4198 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4199 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4200 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4201 2ec1f75b 2019-03-26 stsp
4202 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4203 2ec1f75b 2019-03-26 stsp if (err)
4204 2ec1f75b 2019-03-26 stsp return err;
4205 2ec1f75b 2019-03-26 stsp
4206 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4207 2ec1f75b 2019-03-26 stsp if (err)
4208 2ec1f75b 2019-03-26 stsp goto done;
4209 f2a9dc41 2019-12-13 tracey
4210 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4211 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4212 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4213 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4214 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4215 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4216 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4217 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4218 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4219 2ec1f75b 2019-03-26 stsp
4220 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4221 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4222 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4223 17ed4618 2019-06-02 stsp if (err)
4224 af12c6b9 2019-06-04 stsp break;
4225 2ec1f75b 2019-03-26 stsp }
4226 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4227 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4228 af12c6b9 2019-06-04 stsp err = sync_err;
4229 2ec1f75b 2019-03-26 stsp done:
4230 fb399478 2019-07-12 stsp free(fileindex_path);
4231 2ec1f75b 2019-03-26 stsp if (fileindex)
4232 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4233 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4234 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4235 2ec1f75b 2019-03-26 stsp err = unlockerr;
4236 33aa809d 2019-08-08 stsp return err;
4237 33aa809d 2019-08-08 stsp }
4238 33aa809d 2019-08-08 stsp
4239 33aa809d 2019-08-08 stsp static const struct got_error *
4240 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4241 33aa809d 2019-08-08 stsp {
4242 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4243 33aa809d 2019-08-08 stsp char *line = NULL;
4244 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4245 33aa809d 2019-08-08 stsp ssize_t linelen;
4246 33aa809d 2019-08-08 stsp
4247 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4248 33aa809d 2019-08-08 stsp if (linelen == -1) {
4249 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4250 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4251 33aa809d 2019-08-08 stsp goto done;
4252 33aa809d 2019-08-08 stsp }
4253 33aa809d 2019-08-08 stsp return NULL;
4254 33aa809d 2019-08-08 stsp }
4255 33aa809d 2019-08-08 stsp if (outfile) {
4256 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4257 33aa809d 2019-08-08 stsp if (n != linelen) {
4258 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4259 33aa809d 2019-08-08 stsp goto done;
4260 33aa809d 2019-08-08 stsp }
4261 33aa809d 2019-08-08 stsp }
4262 33aa809d 2019-08-08 stsp if (rejectfile) {
4263 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4264 33aa809d 2019-08-08 stsp if (n != linelen)
4265 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4266 33aa809d 2019-08-08 stsp }
4267 33aa809d 2019-08-08 stsp done:
4268 33aa809d 2019-08-08 stsp free(line);
4269 2ec1f75b 2019-03-26 stsp return err;
4270 2ec1f75b 2019-03-26 stsp }
4271 1f1abb7e 2019-08-08 stsp
4272 33aa809d 2019-08-08 stsp static const struct got_error *
4273 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4274 33aa809d 2019-08-08 stsp {
4275 33aa809d 2019-08-08 stsp char *line = NULL;
4276 33aa809d 2019-08-08 stsp size_t linesize = 0;
4277 33aa809d 2019-08-08 stsp ssize_t linelen;
4278 33aa809d 2019-08-08 stsp
4279 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4280 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4281 500467ff 2019-09-25 hiltjo if (ferror(f))
4282 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4283 500467ff 2019-09-25 hiltjo return NULL;
4284 500467ff 2019-09-25 hiltjo }
4285 33aa809d 2019-08-08 stsp free(line);
4286 33aa809d 2019-08-08 stsp return NULL;
4287 33aa809d 2019-08-08 stsp }
4288 33aa809d 2019-08-08 stsp
4289 33aa809d 2019-08-08 stsp static const struct got_error *
4290 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4291 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4292 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4293 abc59930 2021-09-05 naddy {
4294 33aa809d 2019-08-08 stsp const struct got_error *err;
4295 33aa809d 2019-08-08 stsp
4296 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4297 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4298 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4299 33aa809d 2019-08-08 stsp if (err)
4300 33aa809d 2019-08-08 stsp return err;
4301 33aa809d 2019-08-08 stsp (*line_cur1)++;
4302 33aa809d 2019-08-08 stsp }
4303 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4304 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4305 33aa809d 2019-08-08 stsp if (rejectfile)
4306 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4307 33aa809d 2019-08-08 stsp else
4308 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4309 33aa809d 2019-08-08 stsp if (err)
4310 33aa809d 2019-08-08 stsp return err;
4311 33aa809d 2019-08-08 stsp (*line_cur2)++;
4312 33aa809d 2019-08-08 stsp }
4313 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4314 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4315 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4316 33aa809d 2019-08-08 stsp if (err)
4317 33aa809d 2019-08-08 stsp return err;
4318 33aa809d 2019-08-08 stsp (*line_cur2)++;
4319 33aa809d 2019-08-08 stsp }
4320 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4321 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4322 33aa809d 2019-08-08 stsp if (rejectfile)
4323 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4324 33aa809d 2019-08-08 stsp else
4325 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4326 33aa809d 2019-08-08 stsp if (err)
4327 33aa809d 2019-08-08 stsp return err;
4328 33aa809d 2019-08-08 stsp (*line_cur1)++;
4329 33aa809d 2019-08-08 stsp }
4330 f1e81a05 2019-08-10 stsp
4331 f1e81a05 2019-08-10 stsp return NULL;
4332 f1e81a05 2019-08-10 stsp }
4333 f1e81a05 2019-08-10 stsp
4334 f1e81a05 2019-08-10 stsp static const struct got_error *
4335 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4336 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4337 f1e81a05 2019-08-10 stsp {
4338 f1e81a05 2019-08-10 stsp const struct got_error *err;
4339 f1e81a05 2019-08-10 stsp
4340 f1e81a05 2019-08-10 stsp if (outfile) {
4341 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4342 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4343 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4344 f1e81a05 2019-08-10 stsp if (err)
4345 f1e81a05 2019-08-10 stsp return err;
4346 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4347 f1e81a05 2019-08-10 stsp }
4348 f1e81a05 2019-08-10 stsp }
4349 f1e81a05 2019-08-10 stsp if (rejectfile) {
4350 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4351 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4352 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4353 f1e81a05 2019-08-10 stsp if (err)
4354 f1e81a05 2019-08-10 stsp return err;
4355 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4356 f1e81a05 2019-08-10 stsp }
4357 33aa809d 2019-08-08 stsp }
4358 33aa809d 2019-08-08 stsp
4359 33aa809d 2019-08-08 stsp return NULL;
4360 33aa809d 2019-08-08 stsp }
4361 33aa809d 2019-08-08 stsp
4362 33aa809d 2019-08-08 stsp static const struct got_error *
4363 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4364 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4365 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4366 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4367 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4368 33aa809d 2019-08-08 stsp {
4369 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4370 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4371 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4372 33aa809d 2019-08-08 stsp FILE *hunkfile;
4373 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4374 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4375 fe621944 2020-11-10 stsp int rc;
4376 33aa809d 2019-08-08 stsp
4377 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4378 33aa809d 2019-08-08 stsp
4379 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4380 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4381 fe621944 2020-11-10 stsp start_old = cc.left.start;
4382 fe621944 2020-11-10 stsp end_old = cc.left.end;
4383 fe621944 2020-11-10 stsp start_new = cc.right.start;
4384 fe621944 2020-11-10 stsp end_new = cc.right.end;
4385 33aa809d 2019-08-08 stsp
4386 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4387 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4388 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4389 33aa809d 2019-08-08 stsp
4390 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4391 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4392 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4393 33aa809d 2019-08-08 stsp
4394 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4395 fe621944 2020-11-10 stsp if (diff_state == NULL)
4396 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4397 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4398 fe621944 2020-11-10 stsp
4399 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4400 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4401 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4402 33aa809d 2019-08-08 stsp goto done;
4403 33aa809d 2019-08-08 stsp }
4404 fe621944 2020-11-10 stsp
4405 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4406 fe621944 2020-11-10 stsp diff_result, &cc);
4407 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4408 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4409 33aa809d 2019-08-08 stsp goto done;
4410 33aa809d 2019-08-08 stsp }
4411 fe621944 2020-11-10 stsp
4412 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4413 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4414 33aa809d 2019-08-08 stsp goto done;
4415 33aa809d 2019-08-08 stsp }
4416 33aa809d 2019-08-08 stsp
4417 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4418 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4419 33aa809d 2019-08-08 stsp if (err)
4420 33aa809d 2019-08-08 stsp goto done;
4421 33aa809d 2019-08-08 stsp
4422 33aa809d 2019-08-08 stsp switch (*choice) {
4423 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4424 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4425 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4426 33aa809d 2019-08-08 stsp break;
4427 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4428 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4429 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4430 33aa809d 2019-08-08 stsp break;
4431 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4432 33aa809d 2019-08-08 stsp break;
4433 33aa809d 2019-08-08 stsp default:
4434 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4435 33aa809d 2019-08-08 stsp break;
4436 33aa809d 2019-08-08 stsp }
4437 33aa809d 2019-08-08 stsp done:
4438 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4439 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4440 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4441 33aa809d 2019-08-08 stsp return err;
4442 33aa809d 2019-08-08 stsp }
4443 33aa809d 2019-08-08 stsp
4444 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4445 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4446 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4447 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4448 1f1abb7e 2019-08-08 stsp void *progress_arg;
4449 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4450 33aa809d 2019-08-08 stsp void *patch_arg;
4451 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4452 10604dce 2021-09-24 thomas int unlink_added_files;
4453 1f1abb7e 2019-08-08 stsp };
4454 a129376b 2019-03-28 stsp
4455 e20a8b6f 2019-06-04 stsp static const struct got_error *
4456 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4457 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4458 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4459 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4460 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4461 33aa809d 2019-08-08 stsp {
4462 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4463 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4464 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4465 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4466 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4467 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4468 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4469 fe621944 2020-11-10 stsp struct stat sb2;
4470 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4471 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4472 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4473 33aa809d 2019-08-08 stsp
4474 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4475 33aa809d 2019-08-08 stsp
4476 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4477 33aa809d 2019-08-08 stsp if (err)
4478 33aa809d 2019-08-08 stsp return err;
4479 33aa809d 2019-08-08 stsp
4480 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4481 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4482 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4483 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4484 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4485 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4486 fa3cef63 2020-07-23 stsp goto done;
4487 fa3cef63 2020-07-23 stsp }
4488 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4489 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4490 48b4f239 2021-12-31 thomas if (link_len == -1) {
4491 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4492 48b4f239 2021-12-31 thomas path2);
4493 48b4f239 2021-12-31 thomas }
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 12463d8b 2019-12-13 stsp } else {
4498 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4499 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4500 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4501 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4502 fa3cef63 2020-07-23 stsp goto done;
4503 fa3cef63 2020-07-23 stsp }
4504 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4505 fa3cef63 2020-07-23 stsp sizeof(link_target));
4506 fa3cef63 2020-07-23 stsp if (link_len == -1)
4507 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4508 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4509 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4510 12463d8b 2019-12-13 stsp }
4511 1ebedb77 2019-10-19 stsp }
4512 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4513 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4514 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4515 fa3cef63 2020-07-23 stsp goto done;
4516 fa3cef63 2020-07-23 stsp }
4517 1ebedb77 2019-10-19 stsp
4518 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4519 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4520 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4521 fa3cef63 2020-07-23 stsp goto done;
4522 fa3cef63 2020-07-23 stsp }
4523 fa3cef63 2020-07-23 stsp fd2 = -1;
4524 fa3cef63 2020-07-23 stsp } else {
4525 fa3cef63 2020-07-23 stsp size_t n;
4526 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4527 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4528 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4529 fa3cef63 2020-07-23 stsp goto done;
4530 fa3cef63 2020-07-23 stsp }
4531 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4532 fa3cef63 2020-07-23 stsp if (n != link_len) {
4533 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4534 fa3cef63 2020-07-23 stsp goto done;
4535 fa3cef63 2020-07-23 stsp }
4536 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4537 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4538 fa3cef63 2020-07-23 stsp goto done;
4539 fa3cef63 2020-07-23 stsp }
4540 fa3cef63 2020-07-23 stsp rewind(f2);
4541 33aa809d 2019-08-08 stsp }
4542 33aa809d 2019-08-08 stsp
4543 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4544 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4545 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4546 f4ae6ddb 2022-07-01 thomas goto done;
4547 f4ae6ddb 2022-07-01 thomas }
4548 f4ae6ddb 2022-07-01 thomas
4549 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4550 33aa809d 2019-08-08 stsp if (err)
4551 33aa809d 2019-08-08 stsp goto done;
4552 33aa809d 2019-08-08 stsp
4553 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4554 33aa809d 2019-08-08 stsp if (err)
4555 33aa809d 2019-08-08 stsp goto done;
4556 33aa809d 2019-08-08 stsp
4557 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4558 33aa809d 2019-08-08 stsp if (err)
4559 33aa809d 2019-08-08 stsp goto done;
4560 33aa809d 2019-08-08 stsp
4561 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4562 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4563 33aa809d 2019-08-08 stsp if (err)
4564 33aa809d 2019-08-08 stsp goto done;
4565 33aa809d 2019-08-08 stsp
4566 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4567 33aa809d 2019-08-08 stsp if (err)
4568 33aa809d 2019-08-08 stsp goto done;
4569 33aa809d 2019-08-08 stsp
4570 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4571 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4572 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4573 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4574 fe621944 2020-11-10 stsp
4575 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4576 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4577 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4578 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4579 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4580 fe621944 2020-11-10 stsp nchanges++;
4581 fe621944 2020-11-10 stsp }
4582 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4583 33aa809d 2019-08-08 stsp int choice;
4584 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4585 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4586 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4587 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4588 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4589 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4590 33aa809d 2019-08-08 stsp if (err)
4591 33aa809d 2019-08-08 stsp goto done;
4592 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4593 33aa809d 2019-08-08 stsp have_content = 1;
4594 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4595 33aa809d 2019-08-08 stsp break;
4596 33aa809d 2019-08-08 stsp }
4597 1ebedb77 2019-10-19 stsp if (have_content) {
4598 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4599 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4600 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4601 1ebedb77 2019-10-19 stsp if (err)
4602 1ebedb77 2019-10-19 stsp goto done;
4603 1ebedb77 2019-10-19 stsp
4604 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4605 a2c162eb 2022-10-30 thomas mode_t mode;
4606 a2c162eb 2022-10-30 thomas
4607 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4608 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4609 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4610 fa3cef63 2020-07-23 stsp goto done;
4611 fa3cef63 2020-07-23 stsp }
4612 1ebedb77 2019-10-19 stsp }
4613 1ebedb77 2019-10-19 stsp }
4614 33aa809d 2019-08-08 stsp done:
4615 33aa809d 2019-08-08 stsp free(id_str);
4616 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4617 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4618 33aa809d 2019-08-08 stsp if (blob)
4619 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4620 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4621 fe621944 2020-11-10 stsp if (err == NULL)
4622 fe621944 2020-11-10 stsp err = free_err;
4623 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4624 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4625 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4626 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4627 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4628 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4629 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4630 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4631 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4632 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4633 33aa809d 2019-08-08 stsp if (err || !have_content) {
4634 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4635 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4636 33aa809d 2019-08-08 stsp free(*path_outfile);
4637 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4638 33aa809d 2019-08-08 stsp }
4639 33aa809d 2019-08-08 stsp free(path1);
4640 33aa809d 2019-08-08 stsp return err;
4641 33aa809d 2019-08-08 stsp }
4642 33aa809d 2019-08-08 stsp
4643 33aa809d 2019-08-08 stsp static const struct got_error *
4644 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4645 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4646 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4647 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4648 a129376b 2019-03-28 stsp {
4649 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4650 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4651 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4652 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4653 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4654 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4655 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4656 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4657 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4658 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4659 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4660 f4ae6ddb 2022-07-01 thomas int fd = -1;
4661 a129376b 2019-03-28 stsp
4662 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4663 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4664 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4665 d3bcc3d1 2019-08-08 stsp return NULL;
4666 3d69ad8d 2019-08-17 semarie
4667 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4668 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4669 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4670 d3bcc3d1 2019-08-08 stsp
4671 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4672 65084dad 2019-08-08 stsp if (ie == NULL)
4673 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4674 a129376b 2019-03-28 stsp
4675 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4676 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4677 a129376b 2019-03-28 stsp if (err) {
4678 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4679 a129376b 2019-03-28 stsp goto done;
4680 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4681 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4682 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4683 e20a8b6f 2019-06-04 stsp goto done;
4684 e20a8b6f 2019-06-04 stsp }
4685 a129376b 2019-03-28 stsp }
4686 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4687 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4688 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4689 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4690 a129376b 2019-03-28 stsp goto done;
4691 a129376b 2019-03-28 stsp }
4692 a129376b 2019-03-28 stsp } else {
4693 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4694 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4695 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4696 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4697 a129376b 2019-03-28 stsp goto done;
4698 a129376b 2019-03-28 stsp }
4699 a129376b 2019-03-28 stsp } else {
4700 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4701 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4702 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4703 a129376b 2019-03-28 stsp goto done;
4704 a129376b 2019-03-28 stsp }
4705 a129376b 2019-03-28 stsp }
4706 a129376b 2019-03-28 stsp }
4707 a129376b 2019-03-28 stsp
4708 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4709 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4710 945f9229 2022-04-16 thomas if (err)
4711 945f9229 2022-04-16 thomas goto done;
4712 945f9229 2022-04-16 thomas
4713 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4714 a9fa2909 2019-07-27 stsp if (err) {
4715 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4716 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4717 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4718 a9fa2909 2019-07-27 stsp goto done;
4719 a9fa2909 2019-07-27 stsp } else {
4720 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4721 a9fa2909 2019-07-27 stsp if (err)
4722 a9fa2909 2019-07-27 stsp goto done;
4723 a9fa2909 2019-07-27 stsp
4724 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4725 1233e6b6 2020-10-19 stsp if (err)
4726 a9fa2909 2019-07-27 stsp goto done;
4727 a9fa2909 2019-07-27 stsp
4728 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4729 1233e6b6 2020-10-19 stsp free(te_name);
4730 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4731 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4732 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4733 a9fa2909 2019-07-27 stsp goto done;
4734 a9fa2909 2019-07-27 stsp }
4735 a129376b 2019-03-28 stsp }
4736 a129376b 2019-03-28 stsp
4737 a129376b 2019-03-28 stsp switch (status) {
4738 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4739 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4740 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4741 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4742 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4743 33aa809d 2019-08-08 stsp if (err)
4744 33aa809d 2019-08-08 stsp goto done;
4745 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4746 33aa809d 2019-08-08 stsp break;
4747 33aa809d 2019-08-08 stsp }
4748 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4749 1f1abb7e 2019-08-08 stsp ie->path);
4750 1ee397ad 2019-07-12 stsp if (err)
4751 1ee397ad 2019-07-12 stsp goto done;
4752 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4753 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4754 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4755 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4756 10604dce 2021-09-24 thomas relpath) == -1) {
4757 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4758 10604dce 2021-09-24 thomas goto done;
4759 10604dce 2021-09-24 thomas }
4760 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4761 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4762 10604dce 2021-09-24 thomas ondisk_path);
4763 10604dce 2021-09-24 thomas break;
4764 10604dce 2021-09-24 thomas }
4765 10604dce 2021-09-24 thomas }
4766 a129376b 2019-03-28 stsp break;
4767 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4768 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4769 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4770 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4771 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4772 33aa809d 2019-08-08 stsp if (err)
4773 33aa809d 2019-08-08 stsp goto done;
4774 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4775 33aa809d 2019-08-08 stsp break;
4776 33aa809d 2019-08-08 stsp }
4777 33aa809d 2019-08-08 stsp /* fall through */
4778 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4779 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4780 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4781 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4782 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4783 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4784 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4785 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4786 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4787 24278f30 2019-08-03 stsp } else
4788 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4789 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4790 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4791 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4792 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4793 f4ae6ddb 2022-07-01 thomas goto done;
4794 f4ae6ddb 2022-07-01 thomas }
4795 f4ae6ddb 2022-07-01 thomas
4796 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4797 a129376b 2019-03-28 stsp if (err)
4798 65084dad 2019-08-08 stsp goto done;
4799 65084dad 2019-08-08 stsp
4800 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4801 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4802 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4803 a129376b 2019-03-28 stsp goto done;
4804 65084dad 2019-08-08 stsp }
4805 65084dad 2019-08-08 stsp
4806 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4807 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4808 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4809 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4810 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4811 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4812 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4813 33aa809d 2019-08-08 stsp break;
4814 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4815 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4816 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4817 369fd7e5 2020-07-23 stsp path_content);
4818 369fd7e5 2020-07-23 stsp break;
4819 369fd7e5 2020-07-23 stsp }
4820 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4821 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4822 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4823 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4824 369fd7e5 2020-07-23 stsp } else {
4825 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4826 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4827 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4828 369fd7e5 2020-07-23 stsp goto done;
4829 369fd7e5 2020-07-23 stsp }
4830 33aa809d 2019-08-08 stsp }
4831 33aa809d 2019-08-08 stsp } else {
4832 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4833 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4834 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4835 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4836 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4837 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4838 2e1fa222 2020-07-23 stsp } else {
4839 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4840 2e1fa222 2020-07-23 stsp ie->path,
4841 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4842 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4843 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4844 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4845 2e1fa222 2020-07-23 stsp }
4846 a129376b 2019-03-28 stsp if (err)
4847 a129376b 2019-03-28 stsp goto done;
4848 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4849 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4850 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4851 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4852 437adc9d 2020-12-10 yzhong blob->id.sha1,
4853 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4854 2e1fa222 2020-07-23 stsp if (err)
4855 2e1fa222 2020-07-23 stsp goto done;
4856 2e1fa222 2020-07-23 stsp }
4857 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4858 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4859 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4860 33aa809d 2019-08-08 stsp }
4861 a129376b 2019-03-28 stsp }
4862 a129376b 2019-03-28 stsp break;
4863 e20a8b6f 2019-06-04 stsp }
4864 a129376b 2019-03-28 stsp default:
4865 1f1abb7e 2019-08-08 stsp break;
4866 a129376b 2019-03-28 stsp }
4867 a129376b 2019-03-28 stsp done:
4868 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4869 33aa809d 2019-08-08 stsp free(path_content);
4870 e20a8b6f 2019-06-04 stsp free(parent_path);
4871 a129376b 2019-03-28 stsp free(tree_path);
4872 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4873 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4874 a129376b 2019-03-28 stsp if (blob)
4875 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4876 a129376b 2019-03-28 stsp if (tree)
4877 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4878 a129376b 2019-03-28 stsp free(tree_id);
4879 945f9229 2022-04-16 thomas if (base_commit)
4880 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
4881 e20a8b6f 2019-06-04 stsp return err;
4882 e20a8b6f 2019-06-04 stsp }
4883 e20a8b6f 2019-06-04 stsp
4884 e20a8b6f 2019-06-04 stsp const struct got_error *
4885 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4886 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4887 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4888 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4889 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4890 e20a8b6f 2019-06-04 stsp {
4891 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4892 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4893 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4894 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4895 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4896 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4897 e20a8b6f 2019-06-04 stsp
4898 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4899 e20a8b6f 2019-06-04 stsp if (err)
4900 e20a8b6f 2019-06-04 stsp return err;
4901 e20a8b6f 2019-06-04 stsp
4902 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4903 e20a8b6f 2019-06-04 stsp if (err)
4904 e20a8b6f 2019-06-04 stsp goto done;
4905 e20a8b6f 2019-06-04 stsp
4906 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4907 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4908 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4909 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4910 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4911 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4912 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4913 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4914 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4915 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4916 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4917 e20a8b6f 2019-06-04 stsp if (err)
4918 af12c6b9 2019-06-04 stsp break;
4919 e20a8b6f 2019-06-04 stsp }
4920 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4921 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4922 e20a8b6f 2019-06-04 stsp err = sync_err;
4923 af12c6b9 2019-06-04 stsp done:
4924 fb399478 2019-07-12 stsp free(fileindex_path);
4925 a129376b 2019-03-28 stsp if (fileindex)
4926 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4927 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4928 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4929 a129376b 2019-03-28 stsp err = unlockerr;
4930 c4296144 2019-05-09 stsp return err;
4931 c4296144 2019-05-09 stsp }
4932 c4296144 2019-05-09 stsp
4933 cf066bf8 2019-05-09 stsp static void
4934 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4935 cf066bf8 2019-05-09 stsp {
4936 24519714 2019-05-09 stsp free(ct->path);
4937 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4938 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4939 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4940 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4941 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4942 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4943 cf066bf8 2019-05-09 stsp free(ct);
4944 cf066bf8 2019-05-09 stsp }
4945 24519714 2019-05-09 stsp
4946 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4947 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4948 24519714 2019-05-09 stsp struct got_repository *repo;
4949 24519714 2019-05-09 stsp struct got_worktree *worktree;
4950 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4951 5f8a88c6 2019-08-03 stsp int have_staged_files;
4952 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4953 24519714 2019-05-09 stsp };
4954 cf066bf8 2019-05-09 stsp
4955 c4296144 2019-05-09 stsp static const struct got_error *
4956 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4957 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4958 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4959 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4960 c4296144 2019-05-09 stsp {
4961 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4962 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4963 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4964 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4965 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4966 768aea60 2019-05-09 stsp struct stat sb;
4967 c4296144 2019-05-09 stsp
4968 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4969 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4970 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4971 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4972 5f8a88c6 2019-08-03 stsp return NULL;
4973 5f8a88c6 2019-08-03 stsp } else {
4974 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4975 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4976 c4296144 2019-05-09 stsp
4977 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4978 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4979 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4980 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4981 5f8a88c6 2019-08-03 stsp return NULL;
4982 5f8a88c6 2019-08-03 stsp }
4983 0b5cc0d6 2019-05-09 stsp
4984 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4985 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4986 036813ee 2019-05-09 stsp goto done;
4987 036813ee 2019-05-09 stsp }
4988 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4989 036813ee 2019-05-09 stsp parent_path = strdup("");
4990 69960a46 2019-05-09 stsp if (parent_path == NULL)
4991 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4992 69960a46 2019-05-09 stsp } else {
4993 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4994 69960a46 2019-05-09 stsp if (err)
4995 69960a46 2019-05-09 stsp return err;
4996 69960a46 2019-05-09 stsp }
4997 c4296144 2019-05-09 stsp
4998 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4999 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5000 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5001 c4296144 2019-05-09 stsp goto done;
5002 768aea60 2019-05-09 stsp }
5003 768aea60 2019-05-09 stsp
5004 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5005 768aea60 2019-05-09 stsp relpath) == -1) {
5006 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5007 768aea60 2019-05-09 stsp goto done;
5008 768aea60 2019-05-09 stsp }
5009 0aeb8099 2020-07-23 stsp
5010 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5011 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5012 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5013 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5014 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5015 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5016 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5017 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5018 0aeb8099 2020-07-23 stsp break;
5019 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5020 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5021 0aeb8099 2020-07-23 stsp break;
5022 0aeb8099 2020-07-23 stsp default:
5023 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5024 0aeb8099 2020-07-23 stsp goto done;
5025 0aeb8099 2020-07-23 stsp }
5026 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5027 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5028 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5029 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5030 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5031 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5032 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5033 12463d8b 2019-12-13 stsp ct->ondisk_path);
5034 12463d8b 2019-12-13 stsp goto done;
5035 12463d8b 2019-12-13 stsp }
5036 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5037 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5038 768aea60 2019-05-09 stsp goto done;
5039 768aea60 2019-05-09 stsp }
5040 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5041 c4296144 2019-05-09 stsp }
5042 c4296144 2019-05-09 stsp
5043 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5044 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5045 44d03001 2019-05-09 stsp relpath) == -1) {
5046 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5047 44d03001 2019-05-09 stsp goto done;
5048 44d03001 2019-05-09 stsp }
5049 44d03001 2019-05-09 stsp
5050 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5051 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5052 35213c7c 2020-07-23 stsp int is_bad_symlink;
5053 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5054 35213c7c 2020-07-23 stsp ssize_t target_len;
5055 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5056 35213c7c 2020-07-23 stsp sizeof(target_path));
5057 35213c7c 2020-07-23 stsp if (target_len == -1) {
5058 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5059 35213c7c 2020-07-23 stsp ct->ondisk_path);
5060 35213c7c 2020-07-23 stsp goto done;
5061 35213c7c 2020-07-23 stsp }
5062 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5063 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5064 35213c7c 2020-07-23 stsp if (err)
5065 35213c7c 2020-07-23 stsp goto done;
5066 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5067 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5068 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5069 35213c7c 2020-07-23 stsp goto done;
5070 35213c7c 2020-07-23 stsp }
5071 35213c7c 2020-07-23 stsp }
5072 35213c7c 2020-07-23 stsp
5073 35213c7c 2020-07-23 stsp
5074 cf066bf8 2019-05-09 stsp ct->status = status;
5075 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5076 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5077 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5078 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5079 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5080 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5081 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5082 b416585c 2019-05-13 stsp goto done;
5083 b416585c 2019-05-13 stsp }
5084 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5085 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5086 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5087 f0b75401 2019-08-03 stsp goto done;
5088 f0b75401 2019-08-03 stsp }
5089 f0b75401 2019-08-03 stsp }
5090 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5091 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5092 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5093 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5094 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5095 036813ee 2019-05-09 stsp goto done;
5096 036813ee 2019-05-09 stsp }
5097 ca2503ea 2019-05-09 stsp }
5098 24519714 2019-05-09 stsp ct->path = strdup(path);
5099 24519714 2019-05-09 stsp if (ct->path == NULL) {
5100 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5101 24519714 2019-05-09 stsp goto done;
5102 24519714 2019-05-09 stsp }
5103 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5104 c4296144 2019-05-09 stsp done:
5105 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5106 ed175427 2019-05-09 stsp free_commitable(ct);
5107 24519714 2019-05-09 stsp free(parent_path);
5108 036813ee 2019-05-09 stsp free(path);
5109 a129376b 2019-03-28 stsp return err;
5110 a129376b 2019-03-28 stsp }
5111 c4296144 2019-05-09 stsp
5112 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5113 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5114 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5115 036813ee 2019-05-09 stsp struct got_repository *);
5116 ed175427 2019-05-09 stsp
5117 ed175427 2019-05-09 stsp static const struct got_error *
5118 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5119 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5120 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5121 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5122 afa376bf 2019-05-09 stsp struct got_repository *repo)
5123 ed175427 2019-05-09 stsp {
5124 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5125 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5126 036813ee 2019-05-09 stsp char *subpath;
5127 ed175427 2019-05-09 stsp
5128 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5129 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5130 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5131 ed175427 2019-05-09 stsp
5132 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5133 ed175427 2019-05-09 stsp if (err)
5134 ed175427 2019-05-09 stsp return err;
5135 ed175427 2019-05-09 stsp
5136 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5137 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5138 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5139 036813ee 2019-05-09 stsp free(subpath);
5140 036813ee 2019-05-09 stsp return err;
5141 036813ee 2019-05-09 stsp }
5142 ed175427 2019-05-09 stsp
5143 036813ee 2019-05-09 stsp static const struct got_error *
5144 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5145 036813ee 2019-05-09 stsp {
5146 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5147 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5148 ed175427 2019-05-09 stsp
5149 036813ee 2019-05-09 stsp *match = 0;
5150 ed175427 2019-05-09 stsp
5151 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5152 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5153 0f63689d 2019-05-10 stsp return NULL;
5154 036813ee 2019-05-09 stsp }
5155 0b5cc0d6 2019-05-09 stsp
5156 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5157 0f63689d 2019-05-10 stsp if (err)
5158 0f63689d 2019-05-10 stsp return err;
5159 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5160 036813ee 2019-05-09 stsp free(ct_parent_path);
5161 036813ee 2019-05-09 stsp return err;
5162 036813ee 2019-05-09 stsp }
5163 0b5cc0d6 2019-05-09 stsp
5164 768aea60 2019-05-09 stsp static mode_t
5165 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5166 768aea60 2019-05-09 stsp {
5167 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5168 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5169 3d9a4ec4 2020-07-23 stsp
5170 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5171 768aea60 2019-05-09 stsp }
5172 768aea60 2019-05-09 stsp
5173 036813ee 2019-05-09 stsp static const struct got_error *
5174 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5175 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5176 036813ee 2019-05-09 stsp {
5177 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5178 ca2503ea 2019-05-09 stsp
5179 036813ee 2019-05-09 stsp *new_te = NULL;
5180 0b5cc0d6 2019-05-09 stsp
5181 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5182 036813ee 2019-05-09 stsp if (err)
5183 036813ee 2019-05-09 stsp goto done;
5184 0b5cc0d6 2019-05-09 stsp
5185 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5186 036813ee 2019-05-09 stsp
5187 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5188 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5189 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5190 5f8a88c6 2019-08-03 stsp else
5191 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5192 036813ee 2019-05-09 stsp done:
5193 036813ee 2019-05-09 stsp if (err && *new_te) {
5194 56e0773d 2019-11-28 stsp free(*new_te);
5195 036813ee 2019-05-09 stsp *new_te = NULL;
5196 036813ee 2019-05-09 stsp }
5197 036813ee 2019-05-09 stsp return err;
5198 036813ee 2019-05-09 stsp }
5199 036813ee 2019-05-09 stsp
5200 036813ee 2019-05-09 stsp static const struct got_error *
5201 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5202 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5203 036813ee 2019-05-09 stsp {
5204 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5205 102b254e 2020-10-19 stsp char *ct_name = NULL;
5206 036813ee 2019-05-09 stsp
5207 036813ee 2019-05-09 stsp *new_te = NULL;
5208 036813ee 2019-05-09 stsp
5209 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5210 036813ee 2019-05-09 stsp if (*new_te == NULL)
5211 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5212 036813ee 2019-05-09 stsp
5213 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5214 102b254e 2020-10-19 stsp if (err)
5215 036813ee 2019-05-09 stsp goto done;
5216 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5217 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5218 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5219 036813ee 2019-05-09 stsp goto done;
5220 036813ee 2019-05-09 stsp }
5221 036813ee 2019-05-09 stsp
5222 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5223 036813ee 2019-05-09 stsp
5224 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5225 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5226 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5227 5f8a88c6 2019-08-03 stsp else
5228 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5229 036813ee 2019-05-09 stsp done:
5230 102b254e 2020-10-19 stsp free(ct_name);
5231 036813ee 2019-05-09 stsp if (err && *new_te) {
5232 56e0773d 2019-11-28 stsp free(*new_te);
5233 036813ee 2019-05-09 stsp *new_te = NULL;
5234 036813ee 2019-05-09 stsp }
5235 036813ee 2019-05-09 stsp return err;
5236 036813ee 2019-05-09 stsp }
5237 036813ee 2019-05-09 stsp
5238 036813ee 2019-05-09 stsp static const struct got_error *
5239 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5240 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5241 036813ee 2019-05-09 stsp {
5242 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5243 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5244 036813ee 2019-05-09 stsp
5245 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5246 036813ee 2019-05-09 stsp if (err)
5247 036813ee 2019-05-09 stsp return err;
5248 036813ee 2019-05-09 stsp if (new_pe == NULL)
5249 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5250 036813ee 2019-05-09 stsp return NULL;
5251 afa376bf 2019-05-09 stsp }
5252 afa376bf 2019-05-09 stsp
5253 afa376bf 2019-05-09 stsp static const struct got_error *
5254 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5255 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5256 afa376bf 2019-05-09 stsp {
5257 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5258 5f8a88c6 2019-08-03 stsp unsigned char status;
5259 ae1e948a 2021-09-28 thomas
5260 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5261 ae1e948a 2021-09-28 thomas return NULL;
5262 5f8a88c6 2019-08-03 stsp
5263 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5264 afa376bf 2019-05-09 stsp ct_path++;
5265 5f8a88c6 2019-08-03 stsp
5266 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5267 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5268 5f8a88c6 2019-08-03 stsp else
5269 5f8a88c6 2019-08-03 stsp status = ct->status;
5270 5f8a88c6 2019-08-03 stsp
5271 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5272 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5273 036813ee 2019-05-09 stsp }
5274 036813ee 2019-05-09 stsp
5275 036813ee 2019-05-09 stsp static const struct got_error *
5276 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5277 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5278 44d03001 2019-05-09 stsp {
5279 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5280 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5281 44d03001 2019-05-09 stsp char *te_path;
5282 44d03001 2019-05-09 stsp
5283 44d03001 2019-05-09 stsp *modified = 0;
5284 44d03001 2019-05-09 stsp
5285 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5286 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5287 44d03001 2019-05-09 stsp te->name) == -1)
5288 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5289 44d03001 2019-05-09 stsp
5290 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5291 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5292 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5293 44d03001 2019-05-09 stsp strlen(te_path));
5294 62d463ca 2020-10-20 naddy if (*modified)
5295 44d03001 2019-05-09 stsp break;
5296 44d03001 2019-05-09 stsp }
5297 44d03001 2019-05-09 stsp
5298 44d03001 2019-05-09 stsp free(te_path);
5299 44d03001 2019-05-09 stsp return err;
5300 44d03001 2019-05-09 stsp }
5301 44d03001 2019-05-09 stsp
5302 44d03001 2019-05-09 stsp static const struct got_error *
5303 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5304 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5305 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5306 036813ee 2019-05-09 stsp {
5307 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5308 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5309 036813ee 2019-05-09 stsp
5310 036813ee 2019-05-09 stsp *ctp = NULL;
5311 036813ee 2019-05-09 stsp
5312 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5313 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5314 036813ee 2019-05-09 stsp char *ct_name = NULL;
5315 036813ee 2019-05-09 stsp int path_matches;
5316 036813ee 2019-05-09 stsp
5317 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5318 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5319 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5320 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5321 5f8a88c6 2019-08-03 stsp continue;
5322 5f8a88c6 2019-08-03 stsp } else {
5323 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5324 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5325 5f8a88c6 2019-08-03 stsp continue;
5326 5f8a88c6 2019-08-03 stsp }
5327 036813ee 2019-05-09 stsp
5328 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5329 036813ee 2019-05-09 stsp continue;
5330 036813ee 2019-05-09 stsp
5331 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5332 62d463ca 2020-10-20 naddy if (err)
5333 036813ee 2019-05-09 stsp return err;
5334 036813ee 2019-05-09 stsp if (!path_matches)
5335 036813ee 2019-05-09 stsp continue;
5336 036813ee 2019-05-09 stsp
5337 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5338 d34b633e 2020-10-19 stsp if (err)
5339 d34b633e 2020-10-19 stsp return err;
5340 d34b633e 2020-10-19 stsp
5341 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5342 d34b633e 2020-10-19 stsp free(ct_name);
5343 036813ee 2019-05-09 stsp continue;
5344 d34b633e 2020-10-19 stsp }
5345 d34b633e 2020-10-19 stsp free(ct_name);
5346 036813ee 2019-05-09 stsp
5347 036813ee 2019-05-09 stsp *ctp = ct;
5348 036813ee 2019-05-09 stsp break;
5349 036813ee 2019-05-09 stsp }
5350 2b496619 2019-07-10 stsp
5351 2b496619 2019-07-10 stsp return err;
5352 2b496619 2019-07-10 stsp }
5353 2b496619 2019-07-10 stsp
5354 2b496619 2019-07-10 stsp static const struct got_error *
5355 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5356 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5357 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5358 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5359 2b496619 2019-07-10 stsp struct got_repository *repo)
5360 2b496619 2019-07-10 stsp {
5361 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5362 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5363 2b496619 2019-07-10 stsp char *subtree_path;
5364 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5365 ba580f68 2020-03-22 stsp int nentries;
5366 2b496619 2019-07-10 stsp
5367 2b496619 2019-07-10 stsp *new_tep = NULL;
5368 2b496619 2019-07-10 stsp
5369 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5370 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5371 2b496619 2019-07-10 stsp child_path) == -1)
5372 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5373 036813ee 2019-05-09 stsp
5374 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5375 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5376 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5377 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5378 56e0773d 2019-11-28 stsp
5379 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5380 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5381 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5382 2b496619 2019-07-10 stsp goto done;
5383 2b496619 2019-07-10 stsp }
5384 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5385 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5386 2b496619 2019-07-10 stsp if (err) {
5387 56e0773d 2019-11-28 stsp free(new_te);
5388 2b496619 2019-07-10 stsp goto done;
5389 2b496619 2019-07-10 stsp }
5390 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5391 2b496619 2019-07-10 stsp done:
5392 56e0773d 2019-11-28 stsp free(id);
5393 2b496619 2019-07-10 stsp free(subtree_path);
5394 2b496619 2019-07-10 stsp if (err == NULL)
5395 2b496619 2019-07-10 stsp *new_tep = new_te;
5396 036813ee 2019-05-09 stsp return err;
5397 036813ee 2019-05-09 stsp }
5398 036813ee 2019-05-09 stsp
5399 036813ee 2019-05-09 stsp static const struct got_error *
5400 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5401 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5402 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5403 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5404 036813ee 2019-05-09 stsp struct got_repository *repo)
5405 036813ee 2019-05-09 stsp {
5406 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5407 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5408 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5409 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5410 036813ee 2019-05-09 stsp
5411 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5412 ba580f68 2020-03-22 stsp *nentries = 0;
5413 036813ee 2019-05-09 stsp
5414 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5415 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5416 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5417 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5418 036813ee 2019-05-09 stsp
5419 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5420 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5421 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5422 036813ee 2019-05-09 stsp continue;
5423 036813ee 2019-05-09 stsp
5424 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5425 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5426 036813ee 2019-05-09 stsp continue;
5427 036813ee 2019-05-09 stsp
5428 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5429 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5430 036813ee 2019-05-09 stsp if (err)
5431 036813ee 2019-05-09 stsp goto done;
5432 036813ee 2019-05-09 stsp
5433 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5434 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5435 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5436 036813ee 2019-05-09 stsp if (err)
5437 036813ee 2019-05-09 stsp goto done;
5438 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5439 afa376bf 2019-05-09 stsp if (err)
5440 afa376bf 2019-05-09 stsp goto done;
5441 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5442 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5443 2b496619 2019-07-10 stsp if (err)
5444 2f51b5b3 2019-05-09 stsp goto done;
5445 ba580f68 2020-03-22 stsp (*nentries)++;
5446 2b496619 2019-07-10 stsp } else {
5447 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5448 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5449 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5450 2b496619 2019-07-10 stsp == NULL) {
5451 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5452 2b496619 2019-07-10 stsp child_path, path_base_tree,
5453 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5454 2b496619 2019-07-10 stsp repo);
5455 2b496619 2019-07-10 stsp if (err)
5456 2b496619 2019-07-10 stsp goto done;
5457 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5458 2b496619 2019-07-10 stsp if (err)
5459 2b496619 2019-07-10 stsp goto done;
5460 ba580f68 2020-03-22 stsp (*nentries)++;
5461 9ba0479c 2019-05-10 stsp }
5462 ed175427 2019-05-09 stsp }
5463 2f51b5b3 2019-05-09 stsp }
5464 2f51b5b3 2019-05-09 stsp
5465 2f51b5b3 2019-05-09 stsp if (base_tree) {
5466 56e0773d 2019-11-28 stsp int i, nbase_entries;
5467 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5468 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5469 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5470 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5471 63c5ca5d 2019-08-24 stsp
5472 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5473 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5474 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5475 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5476 63c5ca5d 2019-08-24 stsp if (err)
5477 63c5ca5d 2019-08-24 stsp goto done;
5478 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5479 63c5ca5d 2019-08-24 stsp if (err)
5480 63c5ca5d 2019-08-24 stsp goto done;
5481 ba580f68 2020-03-22 stsp (*nentries)++;
5482 63c5ca5d 2019-08-24 stsp continue;
5483 63c5ca5d 2019-08-24 stsp }
5484 2f51b5b3 2019-05-09 stsp
5485 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5486 44d03001 2019-05-09 stsp int modified;
5487 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5488 036813ee 2019-05-09 stsp if (err)
5489 036813ee 2019-05-09 stsp goto done;
5490 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5491 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5492 2f51b5b3 2019-05-09 stsp if (err)
5493 2f51b5b3 2019-05-09 stsp goto done;
5494 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5495 44d03001 2019-05-09 stsp if (modified) {
5496 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5497 ba580f68 2020-03-22 stsp int nsubentries;
5498 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5499 ba580f68 2020-03-22 stsp &nsubentries, te,
5500 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5501 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5502 44d03001 2019-05-09 stsp if (err)
5503 44d03001 2019-05-09 stsp goto done;
5504 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5505 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5506 ba580f68 2020-03-22 stsp free(new_id);
5507 ba580f68 2020-03-22 stsp continue;
5508 ba580f68 2020-03-22 stsp }
5509 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5510 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5511 56e0773d 2019-11-28 stsp free(new_id);
5512 44d03001 2019-05-09 stsp }
5513 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5514 036813ee 2019-05-09 stsp if (err)
5515 036813ee 2019-05-09 stsp goto done;
5516 ba580f68 2020-03-22 stsp (*nentries)++;
5517 2f51b5b3 2019-05-09 stsp continue;
5518 036813ee 2019-05-09 stsp }
5519 2f51b5b3 2019-05-09 stsp
5520 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5521 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5522 f66c734c 2019-09-22 stsp if (err)
5523 f66c734c 2019-09-22 stsp goto done;
5524 2f51b5b3 2019-05-09 stsp if (ct) {
5525 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5526 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5527 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5528 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5529 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5530 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5531 2f51b5b3 2019-05-09 stsp if (err)
5532 2f51b5b3 2019-05-09 stsp goto done;
5533 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5534 2f51b5b3 2019-05-09 stsp if (err)
5535 2f51b5b3 2019-05-09 stsp goto done;
5536 ba580f68 2020-03-22 stsp (*nentries)++;
5537 2f51b5b3 2019-05-09 stsp }
5538 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5539 b416585c 2019-05-13 stsp status_arg);
5540 afa376bf 2019-05-09 stsp if (err)
5541 afa376bf 2019-05-09 stsp goto done;
5542 2f51b5b3 2019-05-09 stsp } else {
5543 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5544 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5545 2f51b5b3 2019-05-09 stsp if (err)
5546 2f51b5b3 2019-05-09 stsp goto done;
5547 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5548 2f51b5b3 2019-05-09 stsp if (err)
5549 2f51b5b3 2019-05-09 stsp goto done;
5550 ba580f68 2020-03-22 stsp (*nentries)++;
5551 2f51b5b3 2019-05-09 stsp }
5552 ca2503ea 2019-05-09 stsp }
5553 ed175427 2019-05-09 stsp }
5554 0b5cc0d6 2019-05-09 stsp
5555 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5556 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5557 ed175427 2019-05-09 stsp done:
5558 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5559 2e1fa222 2020-07-23 stsp return err;
5560 2e1fa222 2020-07-23 stsp }
5561 2e1fa222 2020-07-23 stsp
5562 2e1fa222 2020-07-23 stsp static const struct got_error *
5563 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5564 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5565 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5566 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5567 ebf99748 2019-05-09 stsp {
5568 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5569 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5570 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5571 ebf99748 2019-05-09 stsp
5572 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5573 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5574 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5575 ebf99748 2019-05-09 stsp
5576 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5577 437adc9d 2020-12-10 yzhong
5578 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5579 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5580 437adc9d 2020-12-10 yzhong if (err)
5581 437adc9d 2020-12-10 yzhong goto done;
5582 437adc9d 2020-12-10 yzhong
5583 ebf99748 2019-05-09 stsp if (ie) {
5584 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5585 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5586 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5587 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5588 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5589 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5590 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5591 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5592 437adc9d 2020-12-10 yzhong
5593 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5594 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5595 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5596 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5597 72fd46fa 2019-09-06 stsp !have_staged_files);
5598 ebf99748 2019-05-09 stsp } else
5599 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5600 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5601 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5602 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5603 72fd46fa 2019-09-06 stsp !have_staged_files);
5604 ebf99748 2019-05-09 stsp } else {
5605 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5606 ebf99748 2019-05-09 stsp if (err)
5607 437adc9d 2020-12-10 yzhong goto done;
5608 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5609 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5610 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5611 3969253a 2020-03-07 stsp if (err) {
5612 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5613 437adc9d 2020-12-10 yzhong goto done;
5614 3969253a 2020-03-07 stsp }
5615 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5616 3969253a 2020-03-07 stsp if (err) {
5617 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5618 437adc9d 2020-12-10 yzhong goto done;
5619 3969253a 2020-03-07 stsp }
5620 ebf99748 2019-05-09 stsp }
5621 437adc9d 2020-12-10 yzhong free(relpath);
5622 437adc9d 2020-12-10 yzhong relpath = NULL;
5623 ebf99748 2019-05-09 stsp }
5624 437adc9d 2020-12-10 yzhong done:
5625 437adc9d 2020-12-10 yzhong free(relpath);
5626 d56d26ce 2019-05-10 stsp return err;
5627 d56d26ce 2019-05-10 stsp }
5628 735ef5ac 2019-08-03 stsp
5629 d56d26ce 2019-05-10 stsp
5630 d56d26ce 2019-05-10 stsp static const struct got_error *
5631 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5632 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5633 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5634 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5635 735ef5ac 2019-08-03 stsp int ood_errcode)
5636 d56d26ce 2019-05-10 stsp {
5637 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5638 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5639 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5640 1a36436d 2019-06-10 stsp
5641 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5642 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5643 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5644 1a36436d 2019-06-10 stsp return NULL;
5645 9bead371 2019-07-28 stsp /*
5646 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5647 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5648 9bead371 2019-07-28 stsp */
5649 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5650 945f9229 2022-04-16 thomas if (err)
5651 945f9229 2022-04-16 thomas goto done;
5652 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5653 9bead371 2019-07-28 stsp if (err) {
5654 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5655 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5656 1a36436d 2019-06-10 stsp goto done;
5657 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5658 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5659 1a36436d 2019-06-10 stsp } else {
5660 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5661 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
5662 945f9229 2022-04-16 thomas if (err)
5663 945f9229 2022-04-16 thomas goto done;
5664 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5665 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5666 1a36436d 2019-06-10 stsp goto done;
5667 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5668 1a36436d 2019-06-10 stsp }
5669 1a36436d 2019-06-10 stsp done:
5670 1a36436d 2019-06-10 stsp free(id);
5671 945f9229 2022-04-16 thomas if (commit)
5672 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5673 1a36436d 2019-06-10 stsp return err;
5674 ebf99748 2019-05-09 stsp }
5675 ebf99748 2019-05-09 stsp
5676 ef20f542 2022-06-26 thomas static const struct got_error *
5677 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5678 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5679 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5680 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5681 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5682 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5683 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5684 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5685 afa376bf 2019-05-09 stsp struct got_repository *repo)
5686 c4296144 2019-05-09 stsp {
5687 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5688 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5689 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5690 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5691 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5692 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5693 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5694 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5695 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5696 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5697 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5698 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5699 892eab0a 2022-07-22 thomas time_t timestamp;
5700 c4296144 2019-05-09 stsp
5701 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5702 c4296144 2019-05-09 stsp
5703 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5704 675c7539 2019-05-09 stsp
5705 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5706 588edf97 2019-05-10 stsp if (err)
5707 588edf97 2019-05-10 stsp goto done;
5708 de18fc63 2019-05-09 stsp
5709 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5710 588edf97 2019-05-10 stsp if (err)
5711 588edf97 2019-05-10 stsp goto done;
5712 588edf97 2019-05-10 stsp
5713 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5714 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5715 33ad4cbe 2019-05-12 jcs if (err)
5716 33ad4cbe 2019-05-12 jcs goto done;
5717 33ad4cbe 2019-05-12 jcs }
5718 c4296144 2019-05-09 stsp
5719 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5720 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5721 33ad4cbe 2019-05-12 jcs goto done;
5722 33ad4cbe 2019-05-12 jcs }
5723 33ad4cbe 2019-05-12 jcs
5724 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5725 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5726 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5727 cf066bf8 2019-05-09 stsp char *ondisk_path;
5728 cf066bf8 2019-05-09 stsp
5729 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5730 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5731 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5732 5f8a88c6 2019-08-03 stsp continue;
5733 5f8a88c6 2019-08-03 stsp
5734 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5735 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5736 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5737 cf066bf8 2019-05-09 stsp continue;
5738 cf066bf8 2019-05-09 stsp
5739 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5740 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5741 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5742 cf066bf8 2019-05-09 stsp goto done;
5743 cf066bf8 2019-05-09 stsp }
5744 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5745 2e1fa222 2020-07-23 stsp free(ondisk_path);
5746 2e1fa222 2020-07-23 stsp if (err)
5747 cf066bf8 2019-05-09 stsp goto done;
5748 cf066bf8 2019-05-09 stsp }
5749 036813ee 2019-05-09 stsp
5750 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5751 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5752 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5753 036813ee 2019-05-09 stsp if (err)
5754 036813ee 2019-05-09 stsp goto done;
5755 036813ee 2019-05-09 stsp
5756 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5757 de18fc63 2019-05-09 stsp if (err)
5758 de18fc63 2019-05-09 stsp goto done;
5759 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5760 10604dce 2021-09-24 thomas nparents++;
5761 10604dce 2021-09-24 thomas if (parent_id2) {
5762 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
5763 10604dce 2021-09-24 thomas if (err)
5764 10604dce 2021-09-24 thomas goto done;
5765 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5766 10604dce 2021-09-24 thomas nparents++;
5767 10604dce 2021-09-24 thomas }
5768 892eab0a 2022-07-22 thomas timestamp = time(NULL);
5769 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5770 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
5771 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5772 33ad4cbe 2019-05-12 jcs free(logmsg);
5773 9d40349a 2019-05-09 stsp if (err)
5774 9d40349a 2019-05-09 stsp goto done;
5775 9d40349a 2019-05-09 stsp
5776 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5777 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5778 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5779 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5780 09f5bd90 2019-05-09 stsp goto done;
5781 09f5bd90 2019-05-09 stsp }
5782 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5783 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5784 09f5bd90 2019-05-09 stsp if (err)
5785 09f5bd90 2019-05-09 stsp goto done;
5786 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5787 ebf99748 2019-05-09 stsp if (err)
5788 ebf99748 2019-05-09 stsp goto done;
5789 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5790 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5791 09f5bd90 2019-05-09 stsp goto done;
5792 09f5bd90 2019-05-09 stsp }
5793 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5794 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5795 f2c16586 2019-05-09 stsp if (err)
5796 f2c16586 2019-05-09 stsp goto done;
5797 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5798 f2c16586 2019-05-09 stsp if (err)
5799 f2c16586 2019-05-09 stsp goto done;
5800 f2c16586 2019-05-09 stsp
5801 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5802 09f5bd90 2019-05-09 stsp if (err)
5803 09f5bd90 2019-05-09 stsp goto done;
5804 09f5bd90 2019-05-09 stsp
5805 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5806 ebf99748 2019-05-09 stsp if (err)
5807 ebf99748 2019-05-09 stsp goto done;
5808 c4296144 2019-05-09 stsp done:
5809 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
5810 588edf97 2019-05-10 stsp if (head_tree)
5811 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5812 588edf97 2019-05-10 stsp if (head_commit)
5813 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5814 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5815 2f17228e 2019-05-12 stsp if (head_ref2) {
5816 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5817 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5818 2f17228e 2019-05-12 stsp err = unlockerr;
5819 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5820 39cd0ff6 2019-07-12 stsp }
5821 39cd0ff6 2019-07-12 stsp return err;
5822 39cd0ff6 2019-07-12 stsp }
5823 5c1e53bc 2019-07-28 stsp
5824 5c1e53bc 2019-07-28 stsp static const struct got_error *
5825 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5826 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5827 5c1e53bc 2019-07-28 stsp {
5828 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5829 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5830 39cd0ff6 2019-07-12 stsp
5831 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5832 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5833 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5834 5c1e53bc 2019-07-28 stsp
5835 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5836 5c1e53bc 2019-07-28 stsp ct_path++;
5837 5c1e53bc 2019-07-28 stsp
5838 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5839 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5840 5c1e53bc 2019-07-28 stsp break;
5841 5c1e53bc 2019-07-28 stsp }
5842 5c1e53bc 2019-07-28 stsp
5843 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5844 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5845 5c1e53bc 2019-07-28 stsp
5846 5c1e53bc 2019-07-28 stsp return NULL;
5847 5c1e53bc 2019-07-28 stsp }
5848 5c1e53bc 2019-07-28 stsp
5849 f0b75401 2019-08-03 stsp static const struct got_error *
5850 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5851 f0b75401 2019-08-03 stsp {
5852 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5853 f0b75401 2019-08-03 stsp
5854 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5855 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5856 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5857 f0b75401 2019-08-03 stsp }
5858 f0b75401 2019-08-03 stsp
5859 f0b75401 2019-08-03 stsp return NULL;
5860 f0b75401 2019-08-03 stsp }
5861 f0b75401 2019-08-03 stsp
5862 5f8a88c6 2019-08-03 stsp static const struct got_error *
5863 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5864 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5865 5f8a88c6 2019-08-03 stsp {
5866 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5867 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5868 5f8a88c6 2019-08-03 stsp
5869 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5870 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5871 5f8a88c6 2019-08-03 stsp continue;
5872 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5873 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5874 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5875 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5876 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5877 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5878 5f8a88c6 2019-08-03 stsp }
5879 5f8a88c6 2019-08-03 stsp
5880 5f8a88c6 2019-08-03 stsp return NULL;
5881 5f8a88c6 2019-08-03 stsp }
5882 5f8a88c6 2019-08-03 stsp
5883 39cd0ff6 2019-07-12 stsp const struct got_error *
5884 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5885 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5886 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5887 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5888 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5889 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5890 39cd0ff6 2019-07-12 stsp {
5891 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5892 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5893 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5894 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5895 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5896 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5897 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5898 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5899 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5900 39cd0ff6 2019-07-12 stsp
5901 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5902 39cd0ff6 2019-07-12 stsp
5903 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5904 39cd0ff6 2019-07-12 stsp
5905 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5906 39cd0ff6 2019-07-12 stsp if (err)
5907 39cd0ff6 2019-07-12 stsp goto done;
5908 39cd0ff6 2019-07-12 stsp
5909 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5910 39cd0ff6 2019-07-12 stsp if (err)
5911 39cd0ff6 2019-07-12 stsp goto done;
5912 39cd0ff6 2019-07-12 stsp
5913 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5914 39cd0ff6 2019-07-12 stsp if (err)
5915 39cd0ff6 2019-07-12 stsp goto done;
5916 39cd0ff6 2019-07-12 stsp
5917 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5918 39cd0ff6 2019-07-12 stsp if (err)
5919 39cd0ff6 2019-07-12 stsp goto done;
5920 39cd0ff6 2019-07-12 stsp
5921 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5922 5f8a88c6 2019-08-03 stsp &have_staged_files);
5923 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5924 5f8a88c6 2019-08-03 stsp goto done;
5925 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5926 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5927 5f8a88c6 2019-08-03 stsp if (err)
5928 5f8a88c6 2019-08-03 stsp goto done;
5929 5f8a88c6 2019-08-03 stsp }
5930 5f8a88c6 2019-08-03 stsp
5931 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5932 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5933 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5934 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5935 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5936 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5937 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5938 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5939 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5940 5c1e53bc 2019-07-28 stsp if (err)
5941 5c1e53bc 2019-07-28 stsp goto done;
5942 5c1e53bc 2019-07-28 stsp }
5943 39cd0ff6 2019-07-12 stsp
5944 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5945 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5946 39cd0ff6 2019-07-12 stsp goto done;
5947 5c1e53bc 2019-07-28 stsp }
5948 5c1e53bc 2019-07-28 stsp
5949 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5950 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5951 5c1e53bc 2019-07-28 stsp if (err)
5952 5c1e53bc 2019-07-28 stsp goto done;
5953 39cd0ff6 2019-07-12 stsp }
5954 f0b75401 2019-08-03 stsp
5955 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5956 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5957 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5958 f0b75401 2019-08-03 stsp
5959 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5960 f0b75401 2019-08-03 stsp ct_path++;
5961 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5962 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5963 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5964 b50cabdf 2019-07-12 stsp if (err)
5965 b50cabdf 2019-07-12 stsp goto done;
5966 f0b75401 2019-08-03 stsp
5967 b50cabdf 2019-07-12 stsp }
5968 b50cabdf 2019-07-12 stsp
5969 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5970 10604dce 2021-09-24 thomas head_commit_id, NULL, worktree, author, committer,
5971 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5972 39cd0ff6 2019-07-12 stsp if (err)
5973 39cd0ff6 2019-07-12 stsp goto done;
5974 39cd0ff6 2019-07-12 stsp
5975 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5976 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5977 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5978 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5979 39cd0ff6 2019-07-12 stsp err = sync_err;
5980 39cd0ff6 2019-07-12 stsp done:
5981 39cd0ff6 2019-07-12 stsp if (fileindex)
5982 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5983 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5984 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5985 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5986 39cd0ff6 2019-07-12 stsp err = unlockerr;
5987 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5988 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5989 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5990 39cd0ff6 2019-07-12 stsp }
5991 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5992 c4296144 2019-05-09 stsp return err;
5993 8656d6c4 2019-05-20 stsp }
5994 8656d6c4 2019-05-20 stsp
5995 8656d6c4 2019-05-20 stsp const char *
5996 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5997 8656d6c4 2019-05-20 stsp {
5998 8656d6c4 2019-05-20 stsp return ct->path;
5999 8656d6c4 2019-05-20 stsp }
6000 8656d6c4 2019-05-20 stsp
6001 8656d6c4 2019-05-20 stsp unsigned int
6002 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6003 8656d6c4 2019-05-20 stsp {
6004 8656d6c4 2019-05-20 stsp return ct->status;
6005 818c7501 2019-07-11 stsp }
6006 818c7501 2019-07-11 stsp
6007 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6008 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6009 818c7501 2019-07-11 stsp struct got_repository *repo;
6010 818c7501 2019-07-11 stsp };
6011 818c7501 2019-07-11 stsp
6012 818c7501 2019-07-11 stsp static const struct got_error *
6013 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6014 818c7501 2019-07-11 stsp {
6015 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6016 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6017 818c7501 2019-07-11 stsp unsigned char status;
6018 818c7501 2019-07-11 stsp struct stat sb;
6019 818c7501 2019-07-11 stsp char *ondisk_path;
6020 818c7501 2019-07-11 stsp
6021 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6022 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6023 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6024 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6025 818c7501 2019-07-11 stsp
6026 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6027 818c7501 2019-07-11 stsp == -1)
6028 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6029 818c7501 2019-07-11 stsp
6030 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6031 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6032 818c7501 2019-07-11 stsp free(ondisk_path);
6033 818c7501 2019-07-11 stsp if (err)
6034 818c7501 2019-07-11 stsp return err;
6035 818c7501 2019-07-11 stsp
6036 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6037 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6038 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6039 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6040 818c7501 2019-07-11 stsp
6041 818c7501 2019-07-11 stsp return NULL;
6042 818c7501 2019-07-11 stsp }
6043 818c7501 2019-07-11 stsp
6044 818c7501 2019-07-11 stsp const struct got_error *
6045 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6046 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6047 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6048 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6049 818c7501 2019-07-11 stsp {
6050 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6051 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6052 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6053 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6054 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6055 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6056 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6057 818c7501 2019-07-11 stsp
6058 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6059 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6060 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6061 818c7501 2019-07-11 stsp
6062 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6063 818c7501 2019-07-11 stsp if (err)
6064 818c7501 2019-07-11 stsp return err;
6065 818c7501 2019-07-11 stsp
6066 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6067 818c7501 2019-07-11 stsp if (err)
6068 818c7501 2019-07-11 stsp goto done;
6069 818c7501 2019-07-11 stsp
6070 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6071 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6072 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6073 818c7501 2019-07-11 stsp &ok_arg);
6074 818c7501 2019-07-11 stsp if (err)
6075 818c7501 2019-07-11 stsp goto done;
6076 818c7501 2019-07-11 stsp
6077 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6078 818c7501 2019-07-11 stsp if (err)
6079 818c7501 2019-07-11 stsp goto done;
6080 818c7501 2019-07-11 stsp
6081 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6082 818c7501 2019-07-11 stsp if (err)
6083 818c7501 2019-07-11 stsp goto done;
6084 818c7501 2019-07-11 stsp
6085 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6086 818c7501 2019-07-11 stsp if (err)
6087 818c7501 2019-07-11 stsp goto done;
6088 818c7501 2019-07-11 stsp
6089 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6090 818c7501 2019-07-11 stsp 0);
6091 e51d7b55 2020-01-04 stsp if (err)
6092 e51d7b55 2020-01-04 stsp goto done;
6093 e51d7b55 2020-01-04 stsp
6094 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6095 818c7501 2019-07-11 stsp if (err)
6096 818c7501 2019-07-11 stsp goto done;
6097 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6098 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6099 e51d7b55 2020-01-04 stsp goto done;
6100 e51d7b55 2020-01-04 stsp }
6101 818c7501 2019-07-11 stsp
6102 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6103 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6104 818c7501 2019-07-11 stsp if (err)
6105 818c7501 2019-07-11 stsp goto done;
6106 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6107 818c7501 2019-07-11 stsp if (err)
6108 818c7501 2019-07-11 stsp goto done;
6109 818c7501 2019-07-11 stsp
6110 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6111 818c7501 2019-07-11 stsp
6112 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6113 818c7501 2019-07-11 stsp if (err)
6114 818c7501 2019-07-11 stsp goto done;
6115 818c7501 2019-07-11 stsp
6116 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6117 818c7501 2019-07-11 stsp if (err)
6118 818c7501 2019-07-11 stsp goto done;
6119 818c7501 2019-07-11 stsp
6120 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6121 818c7501 2019-07-11 stsp worktree->base_commit_id);
6122 818c7501 2019-07-11 stsp if (err)
6123 818c7501 2019-07-11 stsp goto done;
6124 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6125 818c7501 2019-07-11 stsp if (err)
6126 818c7501 2019-07-11 stsp goto done;
6127 818c7501 2019-07-11 stsp
6128 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6129 818c7501 2019-07-11 stsp if (err)
6130 818c7501 2019-07-11 stsp goto done;
6131 818c7501 2019-07-11 stsp done:
6132 818c7501 2019-07-11 stsp free(fileindex_path);
6133 818c7501 2019-07-11 stsp free(tmp_branch_name);
6134 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6135 818c7501 2019-07-11 stsp free(branch_ref_name);
6136 818c7501 2019-07-11 stsp if (branch_ref)
6137 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6138 818c7501 2019-07-11 stsp if (wt_branch)
6139 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6140 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6141 818c7501 2019-07-11 stsp if (err) {
6142 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6143 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6144 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6145 818c7501 2019-07-11 stsp }
6146 818c7501 2019-07-11 stsp if (*tmp_branch) {
6147 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6148 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6149 818c7501 2019-07-11 stsp }
6150 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6151 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6152 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6153 3e3a69f1 2019-07-25 stsp }
6154 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6155 818c7501 2019-07-11 stsp }
6156 818c7501 2019-07-11 stsp return err;
6157 818c7501 2019-07-11 stsp }
6158 818c7501 2019-07-11 stsp
6159 818c7501 2019-07-11 stsp const struct got_error *
6160 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6161 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6162 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6163 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6164 818c7501 2019-07-11 stsp {
6165 818c7501 2019-07-11 stsp const struct got_error *err;
6166 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6167 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6168 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6169 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6170 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6171 818c7501 2019-07-11 stsp
6172 818c7501 2019-07-11 stsp *commit_id = NULL;
6173 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6174 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6175 3e3a69f1 2019-07-25 stsp *branch = NULL;
6176 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6177 3e3a69f1 2019-07-25 stsp
6178 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6179 3e3a69f1 2019-07-25 stsp if (err)
6180 3e3a69f1 2019-07-25 stsp return err;
6181 818c7501 2019-07-11 stsp
6182 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6183 3e3a69f1 2019-07-25 stsp if (err)
6184 f032f1f7 2019-08-04 stsp goto done;
6185 f032f1f7 2019-08-04 stsp
6186 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6187 f032f1f7 2019-08-04 stsp &have_staged_files);
6188 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6189 f032f1f7 2019-08-04 stsp goto done;
6190 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6191 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6192 3e3a69f1 2019-07-25 stsp goto done;
6193 f032f1f7 2019-08-04 stsp }
6194 3e3a69f1 2019-07-25 stsp
6195 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6196 818c7501 2019-07-11 stsp if (err)
6197 f032f1f7 2019-08-04 stsp goto done;
6198 818c7501 2019-07-11 stsp
6199 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
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 = get_rebase_commit_ref_name(&commit_ref_name, worktree);
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 = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
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(&branch_ref, repo, branch_ref_name, 0);
6212 818c7501 2019-07-11 stsp if (err)
6213 818c7501 2019-07-11 stsp goto done;
6214 818c7501 2019-07-11 stsp
6215 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6216 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6217 818c7501 2019-07-11 stsp if (err)
6218 818c7501 2019-07-11 stsp goto done;
6219 818c7501 2019-07-11 stsp
6220 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6221 818c7501 2019-07-11 stsp if (err)
6222 818c7501 2019-07-11 stsp goto done;
6223 818c7501 2019-07-11 stsp
6224 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6225 818c7501 2019-07-11 stsp if (err)
6226 818c7501 2019-07-11 stsp goto done;
6227 818c7501 2019-07-11 stsp
6228 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6229 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6230 818c7501 2019-07-11 stsp if (err)
6231 818c7501 2019-07-11 stsp goto done;
6232 818c7501 2019-07-11 stsp
6233 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6234 818c7501 2019-07-11 stsp if (err)
6235 818c7501 2019-07-11 stsp goto done;
6236 818c7501 2019-07-11 stsp done:
6237 818c7501 2019-07-11 stsp free(commit_ref_name);
6238 818c7501 2019-07-11 stsp free(branch_ref_name);
6239 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6240 818c7501 2019-07-11 stsp if (commit_ref)
6241 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6242 818c7501 2019-07-11 stsp if (branch_ref)
6243 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6244 818c7501 2019-07-11 stsp if (err) {
6245 818c7501 2019-07-11 stsp free(*commit_id);
6246 818c7501 2019-07-11 stsp *commit_id = NULL;
6247 818c7501 2019-07-11 stsp if (*tmp_branch) {
6248 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6249 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6250 818c7501 2019-07-11 stsp }
6251 818c7501 2019-07-11 stsp if (*new_base_branch) {
6252 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6253 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6254 818c7501 2019-07-11 stsp }
6255 818c7501 2019-07-11 stsp if (*branch) {
6256 818c7501 2019-07-11 stsp got_ref_close(*branch);
6257 818c7501 2019-07-11 stsp *branch = NULL;
6258 818c7501 2019-07-11 stsp }
6259 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6260 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6261 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6262 3e3a69f1 2019-07-25 stsp }
6263 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6264 818c7501 2019-07-11 stsp }
6265 818c7501 2019-07-11 stsp return err;
6266 c4296144 2019-05-09 stsp }
6267 818c7501 2019-07-11 stsp
6268 818c7501 2019-07-11 stsp const struct got_error *
6269 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6270 818c7501 2019-07-11 stsp {
6271 818c7501 2019-07-11 stsp const struct got_error *err;
6272 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6273 818c7501 2019-07-11 stsp
6274 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6275 818c7501 2019-07-11 stsp if (err)
6276 818c7501 2019-07-11 stsp return err;
6277 818c7501 2019-07-11 stsp
6278 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6279 818c7501 2019-07-11 stsp free(tmp_branch_name);
6280 818c7501 2019-07-11 stsp return NULL;
6281 818c7501 2019-07-11 stsp }
6282 818c7501 2019-07-11 stsp
6283 818c7501 2019-07-11 stsp static const struct got_error *
6284 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6285 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6286 818c7501 2019-07-11 stsp {
6287 0ebf8283 2019-07-24 stsp *logmsg = arg;
6288 818c7501 2019-07-11 stsp return NULL;
6289 818c7501 2019-07-11 stsp }
6290 818c7501 2019-07-11 stsp
6291 818c7501 2019-07-11 stsp static const struct got_error *
6292 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6293 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6294 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6295 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6296 818c7501 2019-07-11 stsp {
6297 818c7501 2019-07-11 stsp return NULL;
6298 01757395 2019-07-12 stsp }
6299 01757395 2019-07-12 stsp
6300 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6301 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6302 01757395 2019-07-12 stsp void *progress_arg;
6303 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6304 01757395 2019-07-12 stsp };
6305 01757395 2019-07-12 stsp
6306 01757395 2019-07-12 stsp static const struct got_error *
6307 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6308 01757395 2019-07-12 stsp {
6309 01757395 2019-07-12 stsp const struct got_error *err;
6310 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6311 01757395 2019-07-12 stsp char *p;
6312 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6313 01757395 2019-07-12 stsp
6314 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6315 01757395 2019-07-12 stsp if (err)
6316 01757395 2019-07-12 stsp return err;
6317 01757395 2019-07-12 stsp
6318 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6319 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6320 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6321 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6322 01757395 2019-07-12 stsp return NULL;
6323 01757395 2019-07-12 stsp
6324 01757395 2019-07-12 stsp p = strdup(path);
6325 01757395 2019-07-12 stsp if (p == NULL)
6326 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6327 01757395 2019-07-12 stsp
6328 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6329 01757395 2019-07-12 stsp if (err || new == NULL)
6330 01757395 2019-07-12 stsp free(p);
6331 01757395 2019-07-12 stsp return err;
6332 01757395 2019-07-12 stsp }
6333 01757395 2019-07-12 stsp
6334 01757395 2019-07-12 stsp void
6335 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6336 01757395 2019-07-12 stsp {
6337 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6338 01757395 2019-07-12 stsp
6339 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6340 01757395 2019-07-12 stsp free((char *)pe->path);
6341 01757395 2019-07-12 stsp
6342 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6343 818c7501 2019-07-11 stsp }
6344 818c7501 2019-07-11 stsp
6345 0ebf8283 2019-07-24 stsp static const struct got_error *
6346 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6347 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6348 818c7501 2019-07-11 stsp {
6349 818c7501 2019-07-11 stsp const struct got_error *err;
6350 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6351 818c7501 2019-07-11 stsp
6352 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6353 818c7501 2019-07-11 stsp if (err) {
6354 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6355 818c7501 2019-07-11 stsp goto done;
6356 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6357 818c7501 2019-07-11 stsp if (err)
6358 818c7501 2019-07-11 stsp goto done;
6359 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6360 818c7501 2019-07-11 stsp if (err)
6361 818c7501 2019-07-11 stsp goto done;
6362 de05890f 2020-03-05 stsp } else if (is_rebase) {
6363 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6364 818c7501 2019-07-11 stsp int cmp;
6365 818c7501 2019-07-11 stsp
6366 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6367 818c7501 2019-07-11 stsp if (err)
6368 818c7501 2019-07-11 stsp goto done;
6369 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6370 818c7501 2019-07-11 stsp free(stored_id);
6371 818c7501 2019-07-11 stsp if (cmp != 0) {
6372 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6373 818c7501 2019-07-11 stsp goto done;
6374 818c7501 2019-07-11 stsp }
6375 818c7501 2019-07-11 stsp }
6376 0ebf8283 2019-07-24 stsp done:
6377 0ebf8283 2019-07-24 stsp if (commit_ref)
6378 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6379 0ebf8283 2019-07-24 stsp return err;
6380 0ebf8283 2019-07-24 stsp }
6381 0ebf8283 2019-07-24 stsp
6382 0ebf8283 2019-07-24 stsp static const struct got_error *
6383 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6384 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6385 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6386 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6387 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6388 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6389 0ebf8283 2019-07-24 stsp {
6390 0ebf8283 2019-07-24 stsp const struct got_error *err;
6391 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6392 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6393 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6394 818c7501 2019-07-11 stsp
6395 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6396 0ebf8283 2019-07-24 stsp
6397 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6398 0ebf8283 2019-07-24 stsp if (err)
6399 0ebf8283 2019-07-24 stsp return err;
6400 0ebf8283 2019-07-24 stsp
6401 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6402 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6403 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6404 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6405 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6406 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6407 818c7501 2019-07-11 stsp if (commit_ref)
6408 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6409 818c7501 2019-07-11 stsp return err;
6410 818c7501 2019-07-11 stsp }
6411 818c7501 2019-07-11 stsp
6412 818c7501 2019-07-11 stsp const struct got_error *
6413 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6414 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6415 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6416 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6417 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6418 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6419 818c7501 2019-07-11 stsp {
6420 0ebf8283 2019-07-24 stsp const struct got_error *err;
6421 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6422 0ebf8283 2019-07-24 stsp
6423 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6424 0ebf8283 2019-07-24 stsp if (err)
6425 0ebf8283 2019-07-24 stsp return err;
6426 0ebf8283 2019-07-24 stsp
6427 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6428 0ebf8283 2019-07-24 stsp if (err)
6429 0ebf8283 2019-07-24 stsp goto done;
6430 0ebf8283 2019-07-24 stsp
6431 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6432 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6433 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6434 0ebf8283 2019-07-24 stsp done:
6435 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6436 0ebf8283 2019-07-24 stsp return err;
6437 0ebf8283 2019-07-24 stsp }
6438 0ebf8283 2019-07-24 stsp
6439 0ebf8283 2019-07-24 stsp const struct got_error *
6440 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6441 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6442 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6443 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6444 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6445 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6446 0ebf8283 2019-07-24 stsp {
6447 0ebf8283 2019-07-24 stsp const struct got_error *err;
6448 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6449 0ebf8283 2019-07-24 stsp
6450 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6451 0ebf8283 2019-07-24 stsp if (err)
6452 0ebf8283 2019-07-24 stsp return err;
6453 0ebf8283 2019-07-24 stsp
6454 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6455 0ebf8283 2019-07-24 stsp if (err)
6456 0ebf8283 2019-07-24 stsp goto done;
6457 0ebf8283 2019-07-24 stsp
6458 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6459 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6460 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6461 0ebf8283 2019-07-24 stsp done:
6462 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6463 0ebf8283 2019-07-24 stsp return err;
6464 0ebf8283 2019-07-24 stsp }
6465 0ebf8283 2019-07-24 stsp
6466 0ebf8283 2019-07-24 stsp static const struct got_error *
6467 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6468 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6469 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6470 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6471 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6472 150d7275 2022-07-22 thomas struct got_repository *repo)
6473 0ebf8283 2019-07-24 stsp {
6474 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6475 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6476 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6477 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6478 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6479 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6480 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6481 818c7501 2019-07-11 stsp
6482 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6483 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6484 a0e95631 2019-07-12 stsp
6485 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6486 818c7501 2019-07-11 stsp
6487 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6488 a0e95631 2019-07-12 stsp if (err)
6489 3e3a69f1 2019-07-25 stsp return err;
6490 a0e95631 2019-07-12 stsp
6491 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6492 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6493 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6494 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6495 01757395 2019-07-12 stsp /*
6496 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6497 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6498 6c13b005 2021-09-02 stsp *
6499 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6500 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6501 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6502 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6503 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6504 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6505 01757395 2019-07-12 stsp */
6506 01757395 2019-07-12 stsp if (merged_paths) {
6507 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6508 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6509 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6510 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6511 f2a9dc41 2019-12-13 tracey 0);
6512 01757395 2019-07-12 stsp if (err)
6513 01757395 2019-07-12 stsp goto done;
6514 01757395 2019-07-12 stsp }
6515 01757395 2019-07-12 stsp } else {
6516 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6517 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6518 01757395 2019-07-12 stsp if (err)
6519 01757395 2019-07-12 stsp goto done;
6520 01757395 2019-07-12 stsp }
6521 a0e95631 2019-07-12 stsp
6522 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6523 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6524 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6525 a0e95631 2019-07-12 stsp if (err)
6526 a0e95631 2019-07-12 stsp goto done;
6527 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6528 a0e95631 2019-07-12 stsp goto done;
6529 ff0d2220 2019-07-11 stsp }
6530 818c7501 2019-07-11 stsp
6531 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6532 edd02c5e 2019-07-11 stsp if (err)
6533 edd02c5e 2019-07-11 stsp goto done;
6534 edd02c5e 2019-07-11 stsp
6535 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6536 818c7501 2019-07-11 stsp if (err)
6537 818c7501 2019-07-11 stsp goto done;
6538 818c7501 2019-07-11 stsp
6539 5943eee2 2019-08-13 stsp if (new_logmsg) {
6540 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6541 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6542 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6543 5943eee2 2019-08-13 stsp goto done;
6544 5943eee2 2019-08-13 stsp }
6545 5943eee2 2019-08-13 stsp } else {
6546 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6547 5943eee2 2019-08-13 stsp if (err)
6548 5943eee2 2019-08-13 stsp goto done;
6549 5943eee2 2019-08-13 stsp }
6550 0ebf8283 2019-07-24 stsp
6551 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6552 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6553 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6554 8db00f97 2022-07-22 thomas committer ? committer :
6555 8db00f97 2022-07-22 thomas got_object_commit_get_committer(orig_commit),
6556 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6557 a0e95631 2019-07-12 stsp if (err)
6558 a0e95631 2019-07-12 stsp goto done;
6559 a0e95631 2019-07-12 stsp
6560 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6561 a0e95631 2019-07-12 stsp if (err)
6562 a0e95631 2019-07-12 stsp goto done;
6563 a0e95631 2019-07-12 stsp
6564 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6565 a0e95631 2019-07-12 stsp if (err)
6566 a0e95631 2019-07-12 stsp goto done;
6567 a0e95631 2019-07-12 stsp
6568 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6569 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6570 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6571 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6572 a0e95631 2019-07-12 stsp err = sync_err;
6573 818c7501 2019-07-11 stsp done:
6574 a0e95631 2019-07-12 stsp free(fileindex_path);
6575 a0e95631 2019-07-12 stsp free(head_commit_id);
6576 a0e95631 2019-07-12 stsp if (head_ref)
6577 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6578 818c7501 2019-07-11 stsp if (err) {
6579 818c7501 2019-07-11 stsp free(*new_commit_id);
6580 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6581 818c7501 2019-07-11 stsp }
6582 818c7501 2019-07-11 stsp return err;
6583 818c7501 2019-07-11 stsp }
6584 818c7501 2019-07-11 stsp
6585 818c7501 2019-07-11 stsp const struct got_error *
6586 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6587 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6588 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6589 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6590 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6591 0ebf8283 2019-07-24 stsp {
6592 0ebf8283 2019-07-24 stsp const struct got_error *err;
6593 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6594 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6595 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6596 0ebf8283 2019-07-24 stsp
6597 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6598 0ebf8283 2019-07-24 stsp if (err)
6599 0ebf8283 2019-07-24 stsp return err;
6600 0ebf8283 2019-07-24 stsp
6601 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6602 0ebf8283 2019-07-24 stsp if (err)
6603 0ebf8283 2019-07-24 stsp goto done;
6604 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6605 0ebf8283 2019-07-24 stsp if (err)
6606 0ebf8283 2019-07-24 stsp goto done;
6607 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6608 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6609 0ebf8283 2019-07-24 stsp goto done;
6610 0ebf8283 2019-07-24 stsp }
6611 0ebf8283 2019-07-24 stsp
6612 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6613 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6614 150d7275 2022-07-22 thomas NULL, repo);
6615 0ebf8283 2019-07-24 stsp done:
6616 0ebf8283 2019-07-24 stsp if (commit_ref)
6617 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6618 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6619 0ebf8283 2019-07-24 stsp free(commit_id);
6620 0ebf8283 2019-07-24 stsp return err;
6621 0ebf8283 2019-07-24 stsp }
6622 0ebf8283 2019-07-24 stsp
6623 0ebf8283 2019-07-24 stsp const struct got_error *
6624 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6625 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6626 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6627 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
6628 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6629 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6630 0ebf8283 2019-07-24 stsp {
6631 0ebf8283 2019-07-24 stsp const struct got_error *err;
6632 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6633 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6634 0ebf8283 2019-07-24 stsp
6635 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6636 0ebf8283 2019-07-24 stsp if (err)
6637 0ebf8283 2019-07-24 stsp return err;
6638 0ebf8283 2019-07-24 stsp
6639 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6640 0ebf8283 2019-07-24 stsp if (err)
6641 0ebf8283 2019-07-24 stsp goto done;
6642 0ebf8283 2019-07-24 stsp
6643 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6644 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
6645 150d7275 2022-07-22 thomas new_logmsg, repo);
6646 0ebf8283 2019-07-24 stsp done:
6647 0ebf8283 2019-07-24 stsp if (commit_ref)
6648 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6649 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6650 0ebf8283 2019-07-24 stsp return err;
6651 0ebf8283 2019-07-24 stsp }
6652 0ebf8283 2019-07-24 stsp
6653 0ebf8283 2019-07-24 stsp const struct got_error *
6654 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6655 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6656 818c7501 2019-07-11 stsp {
6657 3e3a69f1 2019-07-25 stsp if (fileindex)
6658 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6659 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6660 69844fba 2019-07-11 stsp }
6661 69844fba 2019-07-11 stsp
6662 69844fba 2019-07-11 stsp static const struct got_error *
6663 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6664 69844fba 2019-07-11 stsp {
6665 69844fba 2019-07-11 stsp const struct got_error *err;
6666 69844fba 2019-07-11 stsp struct got_reference *ref;
6667 69844fba 2019-07-11 stsp
6668 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6669 69844fba 2019-07-11 stsp if (err) {
6670 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6671 69844fba 2019-07-11 stsp return NULL;
6672 69844fba 2019-07-11 stsp return err;
6673 69844fba 2019-07-11 stsp }
6674 69844fba 2019-07-11 stsp
6675 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6676 69844fba 2019-07-11 stsp got_ref_close(ref);
6677 69844fba 2019-07-11 stsp return err;
6678 818c7501 2019-07-11 stsp }
6679 818c7501 2019-07-11 stsp
6680 69844fba 2019-07-11 stsp static const struct got_error *
6681 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6682 69844fba 2019-07-11 stsp {
6683 69844fba 2019-07-11 stsp const struct got_error *err;
6684 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6685 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6686 69844fba 2019-07-11 stsp
6687 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6688 69844fba 2019-07-11 stsp if (err)
6689 69844fba 2019-07-11 stsp goto done;
6690 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6691 69844fba 2019-07-11 stsp if (err)
6692 69844fba 2019-07-11 stsp goto done;
6693 69844fba 2019-07-11 stsp
6694 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6695 69844fba 2019-07-11 stsp if (err)
6696 69844fba 2019-07-11 stsp goto done;
6697 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6698 69844fba 2019-07-11 stsp if (err)
6699 69844fba 2019-07-11 stsp goto done;
6700 69844fba 2019-07-11 stsp
6701 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6702 69844fba 2019-07-11 stsp if (err)
6703 69844fba 2019-07-11 stsp goto done;
6704 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6705 69844fba 2019-07-11 stsp if (err)
6706 69844fba 2019-07-11 stsp goto done;
6707 69844fba 2019-07-11 stsp
6708 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6709 69844fba 2019-07-11 stsp if (err)
6710 69844fba 2019-07-11 stsp goto done;
6711 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6712 69844fba 2019-07-11 stsp if (err)
6713 69844fba 2019-07-11 stsp goto done;
6714 69844fba 2019-07-11 stsp
6715 69844fba 2019-07-11 stsp done:
6716 69844fba 2019-07-11 stsp free(tmp_branch_name);
6717 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6718 69844fba 2019-07-11 stsp free(branch_ref_name);
6719 69844fba 2019-07-11 stsp free(commit_ref_name);
6720 e600f124 2021-03-21 stsp return err;
6721 e600f124 2021-03-21 stsp }
6722 e600f124 2021-03-21 stsp
6723 ef20f542 2022-06-26 thomas static const struct got_error *
6724 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6725 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6726 e600f124 2021-03-21 stsp {
6727 e600f124 2021-03-21 stsp const struct got_error *err;
6728 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6729 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6730 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6731 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6732 e600f124 2021-03-21 stsp char *refname = NULL;
6733 e600f124 2021-03-21 stsp
6734 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6735 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6736 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6737 e600f124 2021-03-21 stsp branch_name += 11;
6738 e600f124 2021-03-21 stsp
6739 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6740 e600f124 2021-03-21 stsp if (err)
6741 e600f124 2021-03-21 stsp return err;
6742 e600f124 2021-03-21 stsp
6743 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6744 e600f124 2021-03-21 stsp new_id_str) == -1) {
6745 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6746 e600f124 2021-03-21 stsp goto done;
6747 e600f124 2021-03-21 stsp }
6748 e600f124 2021-03-21 stsp
6749 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6750 e600f124 2021-03-21 stsp if (err)
6751 e600f124 2021-03-21 stsp goto done;
6752 e600f124 2021-03-21 stsp
6753 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6754 e600f124 2021-03-21 stsp if (err)
6755 e600f124 2021-03-21 stsp goto done;
6756 e600f124 2021-03-21 stsp
6757 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6758 e600f124 2021-03-21 stsp done:
6759 e600f124 2021-03-21 stsp free(new_id_str);
6760 e600f124 2021-03-21 stsp free(refname);
6761 e600f124 2021-03-21 stsp free(old_commit_id);
6762 e600f124 2021-03-21 stsp if (ref)
6763 e600f124 2021-03-21 stsp got_ref_close(ref);
6764 69844fba 2019-07-11 stsp return err;
6765 69844fba 2019-07-11 stsp }
6766 69844fba 2019-07-11 stsp
6767 818c7501 2019-07-11 stsp const struct got_error *
6768 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6769 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6770 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6771 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6772 818c7501 2019-07-11 stsp {
6773 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6774 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6775 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6776 818c7501 2019-07-11 stsp
6777 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6778 818c7501 2019-07-11 stsp if (err)
6779 818c7501 2019-07-11 stsp return err;
6780 e600f124 2021-03-21 stsp
6781 e600f124 2021-03-21 stsp if (create_backup) {
6782 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6783 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6784 e600f124 2021-03-21 stsp if (err)
6785 e600f124 2021-03-21 stsp goto done;
6786 e600f124 2021-03-21 stsp }
6787 818c7501 2019-07-11 stsp
6788 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6789 818c7501 2019-07-11 stsp if (err)
6790 818c7501 2019-07-11 stsp goto done;
6791 818c7501 2019-07-11 stsp
6792 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6793 818c7501 2019-07-11 stsp if (err)
6794 818c7501 2019-07-11 stsp goto done;
6795 818c7501 2019-07-11 stsp
6796 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6797 818c7501 2019-07-11 stsp if (err)
6798 818c7501 2019-07-11 stsp goto done;
6799 818c7501 2019-07-11 stsp
6800 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6801 a615e0e7 2020-12-16 stsp if (err)
6802 a615e0e7 2020-12-16 stsp goto done;
6803 a615e0e7 2020-12-16 stsp
6804 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6805 a615e0e7 2020-12-16 stsp if (err)
6806 a615e0e7 2020-12-16 stsp goto done;
6807 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6808 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6809 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6810 a615e0e7 2020-12-16 stsp err = sync_err;
6811 818c7501 2019-07-11 stsp done:
6812 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6813 a615e0e7 2020-12-16 stsp free(fileindex_path);
6814 818c7501 2019-07-11 stsp free(new_head_commit_id);
6815 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6816 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6817 818c7501 2019-07-11 stsp err = unlockerr;
6818 818c7501 2019-07-11 stsp return err;
6819 818c7501 2019-07-11 stsp }
6820 818c7501 2019-07-11 stsp
6821 818c7501 2019-07-11 stsp const struct got_error *
6822 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6823 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6824 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6825 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6826 818c7501 2019-07-11 stsp {
6827 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6828 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6829 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6830 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6831 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6832 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6833 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6834 818c7501 2019-07-11 stsp
6835 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6836 818c7501 2019-07-11 stsp if (err)
6837 818c7501 2019-07-11 stsp return err;
6838 818c7501 2019-07-11 stsp
6839 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
6840 945f9229 2022-04-16 thomas worktree->base_commit_id);
6841 945f9229 2022-04-16 thomas if (err)
6842 945f9229 2022-04-16 thomas goto done;
6843 945f9229 2022-04-16 thomas
6844 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6845 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6846 818c7501 2019-07-11 stsp if (err)
6847 818c7501 2019-07-11 stsp goto done;
6848 818c7501 2019-07-11 stsp
6849 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6850 818c7501 2019-07-11 stsp if (err)
6851 818c7501 2019-07-11 stsp goto done;
6852 818c7501 2019-07-11 stsp
6853 818c7501 2019-07-11 stsp /*
6854 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6855 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6856 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6857 818c7501 2019-07-11 stsp */
6858 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6859 818c7501 2019-07-11 stsp if (err)
6860 818c7501 2019-07-11 stsp goto done;
6861 818c7501 2019-07-11 stsp
6862 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6863 818c7501 2019-07-11 stsp if (err)
6864 818c7501 2019-07-11 stsp goto done;
6865 818c7501 2019-07-11 stsp
6866 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
6867 945f9229 2022-04-16 thomas worktree->path_prefix);
6868 ca355955 2019-07-12 stsp if (err)
6869 ca355955 2019-07-12 stsp goto done;
6870 ca355955 2019-07-12 stsp
6871 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6872 ca355955 2019-07-12 stsp if (err)
6873 ca355955 2019-07-12 stsp goto done;
6874 ca355955 2019-07-12 stsp
6875 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6876 818c7501 2019-07-11 stsp if (err)
6877 818c7501 2019-07-11 stsp goto done;
6878 818c7501 2019-07-11 stsp
6879 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6880 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6881 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6882 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6883 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6884 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6885 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6886 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
6887 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6888 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
6889 55bd499d 2019-07-12 stsp if (err)
6890 1f1abb7e 2019-08-08 stsp goto sync;
6891 55bd499d 2019-07-12 stsp
6892 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6893 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6894 ca355955 2019-07-12 stsp sync:
6895 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6896 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6897 ca355955 2019-07-12 stsp err = sync_err;
6898 818c7501 2019-07-11 stsp done:
6899 818c7501 2019-07-11 stsp got_ref_close(resolved);
6900 a3a2faf2 2019-07-12 stsp free(tree_id);
6901 818c7501 2019-07-11 stsp free(commit_id);
6902 945f9229 2022-04-16 thomas if (commit)
6903 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6904 0ebf8283 2019-07-24 stsp if (fileindex)
6905 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6906 0ebf8283 2019-07-24 stsp free(fileindex_path);
6907 0ebf8283 2019-07-24 stsp
6908 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6909 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6910 0ebf8283 2019-07-24 stsp err = unlockerr;
6911 0ebf8283 2019-07-24 stsp return err;
6912 0ebf8283 2019-07-24 stsp }
6913 0ebf8283 2019-07-24 stsp
6914 0ebf8283 2019-07-24 stsp const struct got_error *
6915 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6916 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6917 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6918 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6919 0ebf8283 2019-07-24 stsp {
6920 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6921 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6922 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6923 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6924 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6925 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6926 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6927 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6928 0ebf8283 2019-07-24 stsp
6929 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6930 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6931 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6932 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6933 0ebf8283 2019-07-24 stsp
6934 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6935 0ebf8283 2019-07-24 stsp if (err)
6936 0ebf8283 2019-07-24 stsp return err;
6937 0ebf8283 2019-07-24 stsp
6938 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6939 0ebf8283 2019-07-24 stsp if (err)
6940 0ebf8283 2019-07-24 stsp goto done;
6941 0ebf8283 2019-07-24 stsp
6942 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6943 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6944 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6945 0ebf8283 2019-07-24 stsp &ok_arg);
6946 0ebf8283 2019-07-24 stsp if (err)
6947 0ebf8283 2019-07-24 stsp goto done;
6948 0ebf8283 2019-07-24 stsp
6949 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6950 0ebf8283 2019-07-24 stsp if (err)
6951 0ebf8283 2019-07-24 stsp goto done;
6952 0ebf8283 2019-07-24 stsp
6953 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6954 0ebf8283 2019-07-24 stsp if (err)
6955 0ebf8283 2019-07-24 stsp goto done;
6956 0ebf8283 2019-07-24 stsp
6957 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6958 0ebf8283 2019-07-24 stsp worktree);
6959 0ebf8283 2019-07-24 stsp if (err)
6960 0ebf8283 2019-07-24 stsp goto done;
6961 0ebf8283 2019-07-24 stsp
6962 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6963 0ebf8283 2019-07-24 stsp 0);
6964 0ebf8283 2019-07-24 stsp if (err)
6965 0ebf8283 2019-07-24 stsp goto done;
6966 0ebf8283 2019-07-24 stsp
6967 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6968 0ebf8283 2019-07-24 stsp if (err)
6969 0ebf8283 2019-07-24 stsp goto done;
6970 0ebf8283 2019-07-24 stsp
6971 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6972 0ebf8283 2019-07-24 stsp if (err)
6973 0ebf8283 2019-07-24 stsp goto done;
6974 0ebf8283 2019-07-24 stsp
6975 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6976 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6977 0ebf8283 2019-07-24 stsp if (err)
6978 0ebf8283 2019-07-24 stsp goto done;
6979 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6980 0ebf8283 2019-07-24 stsp if (err)
6981 0ebf8283 2019-07-24 stsp goto done;
6982 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6983 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6984 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6985 0ebf8283 2019-07-24 stsp goto done;
6986 0ebf8283 2019-07-24 stsp }
6987 0ebf8283 2019-07-24 stsp
6988 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6989 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6990 0ebf8283 2019-07-24 stsp if (err)
6991 0ebf8283 2019-07-24 stsp goto done;
6992 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6993 0ebf8283 2019-07-24 stsp if (err)
6994 0ebf8283 2019-07-24 stsp goto done;
6995 0ebf8283 2019-07-24 stsp
6996 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6997 0ebf8283 2019-07-24 stsp if (err)
6998 0ebf8283 2019-07-24 stsp goto done;
6999 0ebf8283 2019-07-24 stsp done:
7000 0ebf8283 2019-07-24 stsp free(fileindex_path);
7001 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7002 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7003 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7004 0ebf8283 2019-07-24 stsp if (wt_branch)
7005 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7006 0ebf8283 2019-07-24 stsp if (err) {
7007 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7008 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7009 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7010 0ebf8283 2019-07-24 stsp }
7011 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7012 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7013 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7014 0ebf8283 2019-07-24 stsp }
7015 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7016 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7017 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7018 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7019 3e3a69f1 2019-07-25 stsp }
7020 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7021 0ebf8283 2019-07-24 stsp }
7022 0ebf8283 2019-07-24 stsp return err;
7023 0ebf8283 2019-07-24 stsp }
7024 0ebf8283 2019-07-24 stsp
7025 0ebf8283 2019-07-24 stsp const struct got_error *
7026 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7027 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7028 0ebf8283 2019-07-24 stsp {
7029 3e3a69f1 2019-07-25 stsp if (fileindex)
7030 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7031 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7032 0ebf8283 2019-07-24 stsp }
7033 0ebf8283 2019-07-24 stsp
7034 0ebf8283 2019-07-24 stsp const struct got_error *
7035 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7036 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7037 0ebf8283 2019-07-24 stsp {
7038 0ebf8283 2019-07-24 stsp const struct got_error *err;
7039 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7040 0ebf8283 2019-07-24 stsp
7041 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7042 0ebf8283 2019-07-24 stsp if (err)
7043 0ebf8283 2019-07-24 stsp return err;
7044 0ebf8283 2019-07-24 stsp
7045 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7046 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7047 0ebf8283 2019-07-24 stsp return NULL;
7048 0ebf8283 2019-07-24 stsp }
7049 0ebf8283 2019-07-24 stsp
7050 0ebf8283 2019-07-24 stsp const struct got_error *
7051 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7052 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7053 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7054 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7055 0ebf8283 2019-07-24 stsp {
7056 0ebf8283 2019-07-24 stsp const struct got_error *err;
7057 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7058 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7059 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7060 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7061 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7062 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7063 0ebf8283 2019-07-24 stsp
7064 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7065 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7066 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7067 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7068 0ebf8283 2019-07-24 stsp
7069 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7070 3e3a69f1 2019-07-25 stsp if (err)
7071 3e3a69f1 2019-07-25 stsp return err;
7072 3e3a69f1 2019-07-25 stsp
7073 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7074 3e3a69f1 2019-07-25 stsp if (err)
7075 f032f1f7 2019-08-04 stsp goto done;
7076 f032f1f7 2019-08-04 stsp
7077 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7078 f032f1f7 2019-08-04 stsp &have_staged_files);
7079 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7080 f032f1f7 2019-08-04 stsp goto done;
7081 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7082 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7083 3e3a69f1 2019-07-25 stsp goto done;
7084 f032f1f7 2019-08-04 stsp }
7085 3e3a69f1 2019-07-25 stsp
7086 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7087 0ebf8283 2019-07-24 stsp if (err)
7088 f032f1f7 2019-08-04 stsp goto done;
7089 0ebf8283 2019-07-24 stsp
7090 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7091 0ebf8283 2019-07-24 stsp if (err)
7092 0ebf8283 2019-07-24 stsp goto done;
7093 0ebf8283 2019-07-24 stsp
7094 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7095 0ebf8283 2019-07-24 stsp if (err)
7096 0ebf8283 2019-07-24 stsp goto done;
7097 0ebf8283 2019-07-24 stsp
7098 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7099 0ebf8283 2019-07-24 stsp worktree);
7100 0ebf8283 2019-07-24 stsp if (err)
7101 0ebf8283 2019-07-24 stsp goto done;
7102 0ebf8283 2019-07-24 stsp
7103 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7104 0ebf8283 2019-07-24 stsp if (err)
7105 0ebf8283 2019-07-24 stsp goto done;
7106 0ebf8283 2019-07-24 stsp
7107 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7108 0ebf8283 2019-07-24 stsp if (err)
7109 0ebf8283 2019-07-24 stsp goto done;
7110 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7111 0ebf8283 2019-07-24 stsp if (err)
7112 0ebf8283 2019-07-24 stsp goto done;
7113 0ebf8283 2019-07-24 stsp
7114 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7115 0ebf8283 2019-07-24 stsp if (err)
7116 0ebf8283 2019-07-24 stsp goto done;
7117 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7118 0ebf8283 2019-07-24 stsp if (err)
7119 0ebf8283 2019-07-24 stsp goto done;
7120 0ebf8283 2019-07-24 stsp
7121 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7122 0ebf8283 2019-07-24 stsp if (err)
7123 0ebf8283 2019-07-24 stsp goto done;
7124 0ebf8283 2019-07-24 stsp done:
7125 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7126 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7127 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7128 0ebf8283 2019-07-24 stsp if (commit_ref)
7129 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7130 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7131 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7132 0ebf8283 2019-07-24 stsp if (err) {
7133 0ebf8283 2019-07-24 stsp free(*commit_id);
7134 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7135 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7136 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7137 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7138 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7139 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7140 0ebf8283 2019-07-24 stsp }
7141 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7142 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7143 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7144 3e3a69f1 2019-07-25 stsp }
7145 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7146 0ebf8283 2019-07-24 stsp }
7147 0ebf8283 2019-07-24 stsp return err;
7148 0ebf8283 2019-07-24 stsp }
7149 0ebf8283 2019-07-24 stsp
7150 0ebf8283 2019-07-24 stsp static const struct got_error *
7151 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7152 0ebf8283 2019-07-24 stsp {
7153 0ebf8283 2019-07-24 stsp const struct got_error *err;
7154 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7155 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7156 0ebf8283 2019-07-24 stsp
7157 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7158 0ebf8283 2019-07-24 stsp if (err)
7159 0ebf8283 2019-07-24 stsp goto done;
7160 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7161 0ebf8283 2019-07-24 stsp if (err)
7162 0ebf8283 2019-07-24 stsp goto done;
7163 0ebf8283 2019-07-24 stsp
7164 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7165 0ebf8283 2019-07-24 stsp worktree);
7166 0ebf8283 2019-07-24 stsp if (err)
7167 0ebf8283 2019-07-24 stsp goto done;
7168 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7169 0ebf8283 2019-07-24 stsp if (err)
7170 0ebf8283 2019-07-24 stsp goto done;
7171 0ebf8283 2019-07-24 stsp
7172 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7173 0ebf8283 2019-07-24 stsp if (err)
7174 0ebf8283 2019-07-24 stsp goto done;
7175 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7176 0ebf8283 2019-07-24 stsp if (err)
7177 0ebf8283 2019-07-24 stsp goto done;
7178 0ebf8283 2019-07-24 stsp
7179 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7180 0ebf8283 2019-07-24 stsp if (err)
7181 0ebf8283 2019-07-24 stsp goto done;
7182 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7183 0ebf8283 2019-07-24 stsp if (err)
7184 0ebf8283 2019-07-24 stsp goto done;
7185 0ebf8283 2019-07-24 stsp done:
7186 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7187 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7188 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7189 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7190 0ebf8283 2019-07-24 stsp return err;
7191 0ebf8283 2019-07-24 stsp }
7192 0ebf8283 2019-07-24 stsp
7193 0ebf8283 2019-07-24 stsp const struct got_error *
7194 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7195 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7196 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7197 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7198 0ebf8283 2019-07-24 stsp {
7199 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7200 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7201 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7202 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7203 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7204 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7205 0ebf8283 2019-07-24 stsp
7206 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7207 0ebf8283 2019-07-24 stsp if (err)
7208 0ebf8283 2019-07-24 stsp return err;
7209 0ebf8283 2019-07-24 stsp
7210 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7211 945f9229 2022-04-16 thomas worktree->base_commit_id);
7212 945f9229 2022-04-16 thomas if (err)
7213 945f9229 2022-04-16 thomas goto done;
7214 945f9229 2022-04-16 thomas
7215 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7216 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7217 0ebf8283 2019-07-24 stsp if (err)
7218 0ebf8283 2019-07-24 stsp goto done;
7219 0ebf8283 2019-07-24 stsp
7220 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7221 0ebf8283 2019-07-24 stsp if (err)
7222 0ebf8283 2019-07-24 stsp goto done;
7223 0ebf8283 2019-07-24 stsp
7224 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7225 0ebf8283 2019-07-24 stsp if (err)
7226 0ebf8283 2019-07-24 stsp goto done;
7227 0ebf8283 2019-07-24 stsp
7228 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7229 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7230 0ebf8283 2019-07-24 stsp if (err)
7231 0ebf8283 2019-07-24 stsp goto done;
7232 0ebf8283 2019-07-24 stsp
7233 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7234 0ebf8283 2019-07-24 stsp if (err)
7235 0ebf8283 2019-07-24 stsp goto done;
7236 0ebf8283 2019-07-24 stsp
7237 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7238 0ebf8283 2019-07-24 stsp if (err)
7239 0ebf8283 2019-07-24 stsp goto done;
7240 0ebf8283 2019-07-24 stsp
7241 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7242 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7243 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7244 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7245 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7246 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7247 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7248 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7249 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7250 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7251 0ebf8283 2019-07-24 stsp if (err)
7252 1f1abb7e 2019-08-08 stsp goto sync;
7253 0ebf8283 2019-07-24 stsp
7254 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7255 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7256 0ebf8283 2019-07-24 stsp sync:
7257 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7258 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7259 0ebf8283 2019-07-24 stsp err = sync_err;
7260 0ebf8283 2019-07-24 stsp done:
7261 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7262 0ebf8283 2019-07-24 stsp free(tree_id);
7263 818c7501 2019-07-11 stsp free(fileindex_path);
7264 818c7501 2019-07-11 stsp
7265 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7266 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7267 818c7501 2019-07-11 stsp err = unlockerr;
7268 818c7501 2019-07-11 stsp return err;
7269 818c7501 2019-07-11 stsp }
7270 0ebf8283 2019-07-24 stsp
7271 0ebf8283 2019-07-24 stsp const struct got_error *
7272 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7273 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7274 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7275 0ebf8283 2019-07-24 stsp {
7276 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7277 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7278 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7279 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7280 0ebf8283 2019-07-24 stsp
7281 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7282 0ebf8283 2019-07-24 stsp if (err)
7283 0ebf8283 2019-07-24 stsp return err;
7284 0ebf8283 2019-07-24 stsp
7285 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7286 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7287 e600f124 2021-03-21 stsp if (err)
7288 e600f124 2021-03-21 stsp goto done;
7289 e600f124 2021-03-21 stsp
7290 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7291 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7292 0ebf8283 2019-07-24 stsp if (err)
7293 0ebf8283 2019-07-24 stsp goto done;
7294 0ebf8283 2019-07-24 stsp
7295 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7296 0ebf8283 2019-07-24 stsp if (err)
7297 0ebf8283 2019-07-24 stsp goto done;
7298 0ebf8283 2019-07-24 stsp
7299 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7300 0ebf8283 2019-07-24 stsp if (err)
7301 0ebf8283 2019-07-24 stsp goto done;
7302 0ebf8283 2019-07-24 stsp
7303 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7304 0ebf8283 2019-07-24 stsp if (err)
7305 0ebf8283 2019-07-24 stsp goto done;
7306 0ebf8283 2019-07-24 stsp
7307 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7308 a615e0e7 2020-12-16 stsp if (err)
7309 a615e0e7 2020-12-16 stsp goto done;
7310 a615e0e7 2020-12-16 stsp
7311 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7312 a615e0e7 2020-12-16 stsp if (err)
7313 a615e0e7 2020-12-16 stsp goto done;
7314 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7315 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7316 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7317 a615e0e7 2020-12-16 stsp err = sync_err;
7318 0ebf8283 2019-07-24 stsp done:
7319 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7320 a615e0e7 2020-12-16 stsp free(fileindex_path);
7321 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7322 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7323 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7324 0ebf8283 2019-07-24 stsp err = unlockerr;
7325 0ebf8283 2019-07-24 stsp return err;
7326 0ebf8283 2019-07-24 stsp }
7327 0ebf8283 2019-07-24 stsp
7328 0ebf8283 2019-07-24 stsp const struct got_error *
7329 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7330 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7331 0ebf8283 2019-07-24 stsp {
7332 0ebf8283 2019-07-24 stsp const struct got_error *err;
7333 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7334 0ebf8283 2019-07-24 stsp
7335 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7336 0ebf8283 2019-07-24 stsp if (err)
7337 0ebf8283 2019-07-24 stsp return err;
7338 0ebf8283 2019-07-24 stsp
7339 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7340 0ebf8283 2019-07-24 stsp if (err)
7341 0ebf8283 2019-07-24 stsp goto done;
7342 0ebf8283 2019-07-24 stsp
7343 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7344 0ebf8283 2019-07-24 stsp done:
7345 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7346 2822a352 2019-10-15 stsp return err;
7347 2822a352 2019-10-15 stsp }
7348 2822a352 2019-10-15 stsp
7349 2822a352 2019-10-15 stsp const struct got_error *
7350 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7351 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7352 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7353 2822a352 2019-10-15 stsp struct got_repository *repo)
7354 2822a352 2019-10-15 stsp {
7355 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7356 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7357 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7358 2822a352 2019-10-15 stsp
7359 2822a352 2019-10-15 stsp *fileindex = NULL;
7360 2822a352 2019-10-15 stsp *branch_ref = NULL;
7361 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7362 2822a352 2019-10-15 stsp
7363 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7364 2822a352 2019-10-15 stsp if (err)
7365 2822a352 2019-10-15 stsp return err;
7366 2822a352 2019-10-15 stsp
7367 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7368 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7369 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7370 2822a352 2019-10-15 stsp "update -b or different branch name required");
7371 2822a352 2019-10-15 stsp goto done;
7372 2822a352 2019-10-15 stsp }
7373 2822a352 2019-10-15 stsp
7374 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7375 2822a352 2019-10-15 stsp if (err)
7376 2822a352 2019-10-15 stsp goto done;
7377 2822a352 2019-10-15 stsp
7378 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7379 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7380 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7381 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7382 2822a352 2019-10-15 stsp &ok_arg);
7383 2822a352 2019-10-15 stsp if (err)
7384 2822a352 2019-10-15 stsp goto done;
7385 2822a352 2019-10-15 stsp
7386 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7387 2822a352 2019-10-15 stsp if (err)
7388 2822a352 2019-10-15 stsp goto done;
7389 2822a352 2019-10-15 stsp
7390 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7391 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7392 2822a352 2019-10-15 stsp done:
7393 2822a352 2019-10-15 stsp if (err) {
7394 2822a352 2019-10-15 stsp if (*branch_ref) {
7395 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7396 2822a352 2019-10-15 stsp *branch_ref = NULL;
7397 2822a352 2019-10-15 stsp }
7398 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7399 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7400 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7401 2822a352 2019-10-15 stsp }
7402 2822a352 2019-10-15 stsp if (*fileindex) {
7403 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7404 2822a352 2019-10-15 stsp *fileindex = NULL;
7405 2822a352 2019-10-15 stsp }
7406 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7407 2822a352 2019-10-15 stsp }
7408 0cb83759 2019-08-03 stsp return err;
7409 0cb83759 2019-08-03 stsp }
7410 0cb83759 2019-08-03 stsp
7411 2822a352 2019-10-15 stsp const struct got_error *
7412 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7413 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7414 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7415 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7416 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7417 2822a352 2019-10-15 stsp {
7418 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7419 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7420 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7421 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7422 2822a352 2019-10-15 stsp
7423 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
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 = got_ref_resolve(&commit_id, repo, branch_ref);
7428 2822a352 2019-10-15 stsp if (err)
7429 2822a352 2019-10-15 stsp goto done;
7430 2822a352 2019-10-15 stsp
7431 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7432 945f9229 2022-04-16 thomas if (err)
7433 945f9229 2022-04-16 thomas goto done;
7434 945f9229 2022-04-16 thomas
7435 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7436 2822a352 2019-10-15 stsp worktree->path_prefix);
7437 2822a352 2019-10-15 stsp if (err)
7438 2822a352 2019-10-15 stsp goto done;
7439 2822a352 2019-10-15 stsp
7440 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7441 2822a352 2019-10-15 stsp if (err)
7442 2822a352 2019-10-15 stsp goto done;
7443 2822a352 2019-10-15 stsp
7444 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7445 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7446 2822a352 2019-10-15 stsp if (err)
7447 2822a352 2019-10-15 stsp goto sync;
7448 2822a352 2019-10-15 stsp
7449 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7450 2822a352 2019-10-15 stsp if (err)
7451 2822a352 2019-10-15 stsp goto sync;
7452 2822a352 2019-10-15 stsp
7453 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7454 6e210706 2021-01-22 stsp if (err)
7455 6e210706 2021-01-22 stsp goto sync;
7456 6e210706 2021-01-22 stsp
7457 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7458 2822a352 2019-10-15 stsp sync:
7459 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7460 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7461 2822a352 2019-10-15 stsp err = sync_err;
7462 2822a352 2019-10-15 stsp
7463 2822a352 2019-10-15 stsp done:
7464 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7465 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7466 2822a352 2019-10-15 stsp err = unlockerr;
7467 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7468 2822a352 2019-10-15 stsp
7469 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7470 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7471 2822a352 2019-10-15 stsp err = unlockerr;
7472 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7473 2822a352 2019-10-15 stsp
7474 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7475 2822a352 2019-10-15 stsp free(fileindex_path);
7476 2822a352 2019-10-15 stsp free(tree_id);
7477 945f9229 2022-04-16 thomas if (commit)
7478 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7479 2822a352 2019-10-15 stsp
7480 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7481 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7482 2822a352 2019-10-15 stsp err = unlockerr;
7483 2822a352 2019-10-15 stsp return err;
7484 2822a352 2019-10-15 stsp }
7485 2822a352 2019-10-15 stsp
7486 2822a352 2019-10-15 stsp const struct got_error *
7487 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7488 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7489 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7490 2822a352 2019-10-15 stsp {
7491 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7492 8b692cd0 2019-10-21 stsp
7493 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7494 8b692cd0 2019-10-21 stsp
7495 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7496 8b692cd0 2019-10-21 stsp
7497 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7498 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7499 8b692cd0 2019-10-21 stsp err = unlockerr;
7500 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7501 8b692cd0 2019-10-21 stsp
7502 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7503 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7504 8b692cd0 2019-10-21 stsp err = unlockerr;
7505 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7506 10604dce 2021-09-24 thomas
7507 10604dce 2021-09-24 thomas return err;
7508 10604dce 2021-09-24 thomas }
7509 10604dce 2021-09-24 thomas
7510 10604dce 2021-09-24 thomas const struct got_error *
7511 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7512 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7513 10604dce 2021-09-24 thomas {
7514 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7515 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7516 10604dce 2021-09-24 thomas
7517 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7518 10604dce 2021-09-24 thomas if (err)
7519 10604dce 2021-09-24 thomas goto done;
7520 8b692cd0 2019-10-21 stsp
7521 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7522 10604dce 2021-09-24 thomas
7523 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7524 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7525 10604dce 2021-09-24 thomas err = sync_err;
7526 10604dce 2021-09-24 thomas done:
7527 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7528 10604dce 2021-09-24 thomas free(fileindex_path);
7529 8b692cd0 2019-10-21 stsp return err;
7530 2822a352 2019-10-15 stsp }
7531 2822a352 2019-10-15 stsp
7532 10604dce 2021-09-24 thomas static const struct got_error *
7533 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7534 10604dce 2021-09-24 thomas {
7535 10604dce 2021-09-24 thomas const struct got_error *err;
7536 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7537 10604dce 2021-09-24 thomas
7538 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7539 10604dce 2021-09-24 thomas if (err)
7540 10604dce 2021-09-24 thomas goto done;
7541 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7542 10604dce 2021-09-24 thomas if (err)
7543 10604dce 2021-09-24 thomas goto done;
7544 10604dce 2021-09-24 thomas
7545 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7546 10604dce 2021-09-24 thomas if (err)
7547 10604dce 2021-09-24 thomas goto done;
7548 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7549 10604dce 2021-09-24 thomas if (err)
7550 10604dce 2021-09-24 thomas goto done;
7551 10604dce 2021-09-24 thomas
7552 10604dce 2021-09-24 thomas done:
7553 10604dce 2021-09-24 thomas free(branch_refname);
7554 10604dce 2021-09-24 thomas free(commit_refname);
7555 10604dce 2021-09-24 thomas return err;
7556 10604dce 2021-09-24 thomas }
7557 10604dce 2021-09-24 thomas
7558 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7559 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7560 10604dce 2021-09-24 thomas const char *branch_name;
7561 10604dce 2021-09-24 thomas };
7562 10604dce 2021-09-24 thomas
7563 10604dce 2021-09-24 thomas static const struct got_error *
7564 10604dce 2021-09-24 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7565 10604dce 2021-09-24 thomas void *arg)
7566 10604dce 2021-09-24 thomas {
7567 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7568 10604dce 2021-09-24 thomas
7569 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7570 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7571 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7572 10604dce 2021-09-24 thomas
7573 10604dce 2021-09-24 thomas return NULL;
7574 10604dce 2021-09-24 thomas }
7575 10604dce 2021-09-24 thomas
7576 10604dce 2021-09-24 thomas
7577 10604dce 2021-09-24 thomas const struct got_error *
7578 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7579 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7580 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7581 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7582 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7583 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7584 10604dce 2021-09-24 thomas {
7585 10604dce 2021-09-24 thomas const struct got_error *err;
7586 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7587 10604dce 2021-09-24 thomas
7588 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7589 10604dce 2021-09-24 thomas if (err)
7590 10604dce 2021-09-24 thomas goto done;
7591 10604dce 2021-09-24 thomas
7592 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7593 10604dce 2021-09-24 thomas worktree);
7594 10604dce 2021-09-24 thomas if (err)
7595 10604dce 2021-09-24 thomas goto done;
7596 10604dce 2021-09-24 thomas
7597 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7598 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7599 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7600 10604dce 2021-09-24 thomas done:
7601 10604dce 2021-09-24 thomas free(fileindex_path);
7602 10604dce 2021-09-24 thomas return err;
7603 10604dce 2021-09-24 thomas }
7604 10604dce 2021-09-24 thomas
7605 10604dce 2021-09-24 thomas const struct got_error *
7606 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7607 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7608 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7609 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7610 ae1e948a 2021-09-28 thomas struct got_repository *repo,
7611 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7612 ae1e948a 2021-09-28 thomas
7613 10604dce 2021-09-24 thomas {
7614 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7615 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7616 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7617 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7618 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7619 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7620 10604dce 2021-09-24 thomas int have_staged_files = 0;
7621 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7622 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7623 10604dce 2021-09-24 thomas
7624 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7625 10604dce 2021-09-24 thomas
7626 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7627 10604dce 2021-09-24 thomas
7628 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7629 10604dce 2021-09-24 thomas if (err)
7630 10604dce 2021-09-24 thomas goto done;
7631 10604dce 2021-09-24 thomas
7632 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7633 10604dce 2021-09-24 thomas if (err)
7634 10604dce 2021-09-24 thomas goto done;
7635 10604dce 2021-09-24 thomas
7636 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7637 10604dce 2021-09-24 thomas if (err)
7638 10604dce 2021-09-24 thomas goto done;
7639 10604dce 2021-09-24 thomas
7640 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7641 10604dce 2021-09-24 thomas &have_staged_files);
7642 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7643 10604dce 2021-09-24 thomas goto done;
7644 10604dce 2021-09-24 thomas if (have_staged_files) {
7645 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
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 cc_arg.commitable_paths = &commitable_paths;
7650 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7651 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7652 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7653 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7654 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7655 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7656 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7657 10604dce 2021-09-24 thomas if (err)
7658 10604dce 2021-09-24 thomas goto done;
7659 10604dce 2021-09-24 thomas
7660 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7661 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7662 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7663 10604dce 2021-09-24 thomas goto done;
7664 10604dce 2021-09-24 thomas }
7665 10604dce 2021-09-24 thomas
7666 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7667 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7668 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7669 10604dce 2021-09-24 thomas
7670 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7671 10604dce 2021-09-24 thomas ct_path++;
7672 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7673 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7674 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7675 10604dce 2021-09-24 thomas if (err)
7676 10604dce 2021-09-24 thomas goto done;
7677 10604dce 2021-09-24 thomas
7678 10604dce 2021-09-24 thomas }
7679 10604dce 2021-09-24 thomas
7680 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7681 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7682 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7683 10604dce 2021-09-24 thomas head_commit_id, branch_tip, worktree, author, committer,
7684 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7685 10604dce 2021-09-24 thomas if (err)
7686 10604dce 2021-09-24 thomas goto done;
7687 10604dce 2021-09-24 thomas
7688 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7689 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7690 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7691 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7692 10604dce 2021-09-24 thomas err = sync_err;
7693 10604dce 2021-09-24 thomas done:
7694 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7695 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7696 10604dce 2021-09-24 thomas free_commitable(ct);
7697 10604dce 2021-09-24 thomas }
7698 10604dce 2021-09-24 thomas got_pathlist_free(&commitable_paths);
7699 10604dce 2021-09-24 thomas free(fileindex_path);
7700 10604dce 2021-09-24 thomas return err;
7701 10604dce 2021-09-24 thomas }
7702 10604dce 2021-09-24 thomas
7703 10604dce 2021-09-24 thomas const struct got_error *
7704 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
7705 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
7706 10604dce 2021-09-24 thomas {
7707 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7708 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7709 10604dce 2021-09-24 thomas
7710 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7711 10604dce 2021-09-24 thomas if (err)
7712 10604dce 2021-09-24 thomas goto done;
7713 10604dce 2021-09-24 thomas
7714 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7715 10604dce 2021-09-24 thomas if (err)
7716 10604dce 2021-09-24 thomas goto done;
7717 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7718 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7719 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7720 10604dce 2021-09-24 thomas err = sync_err;
7721 10604dce 2021-09-24 thomas done:
7722 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7723 10604dce 2021-09-24 thomas free(fileindex_path);
7724 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7725 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7726 10604dce 2021-09-24 thomas err = unlockerr;
7727 10604dce 2021-09-24 thomas return err;
7728 10604dce 2021-09-24 thomas }
7729 10604dce 2021-09-24 thomas
7730 10604dce 2021-09-24 thomas const struct got_error *
7731 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7732 10604dce 2021-09-24 thomas struct got_repository *repo)
7733 10604dce 2021-09-24 thomas {
7734 10604dce 2021-09-24 thomas const struct got_error *err;
7735 10604dce 2021-09-24 thomas char *branch_refname = NULL;
7736 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
7737 10604dce 2021-09-24 thomas
7738 10604dce 2021-09-24 thomas *in_progress = 0;
7739 10604dce 2021-09-24 thomas
7740 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7741 10604dce 2021-09-24 thomas if (err)
7742 10604dce 2021-09-24 thomas return err;
7743 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7744 dfe86d1f 2021-09-24 thomas free(branch_refname);
7745 10604dce 2021-09-24 thomas if (err) {
7746 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
7747 10604dce 2021-09-24 thomas return err;
7748 10604dce 2021-09-24 thomas } else
7749 10604dce 2021-09-24 thomas *in_progress = 1;
7750 10604dce 2021-09-24 thomas
7751 10604dce 2021-09-24 thomas return NULL;
7752 10604dce 2021-09-24 thomas }
7753 10604dce 2021-09-24 thomas
7754 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
7755 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
7756 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
7757 10604dce 2021-09-24 thomas {
7758 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
7759 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7760 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7761 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7762 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
7763 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7764 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
7765 10604dce 2021-09-24 thomas
7766 10604dce 2021-09-24 thomas *fileindex = NULL;
7767 10604dce 2021-09-24 thomas
7768 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7769 10604dce 2021-09-24 thomas if (err)
7770 10604dce 2021-09-24 thomas return err;
7771 10604dce 2021-09-24 thomas
7772 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7773 10604dce 2021-09-24 thomas if (err)
7774 10604dce 2021-09-24 thomas goto done;
7775 10604dce 2021-09-24 thomas
7776 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
7777 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
7778 10604dce 2021-09-24 thomas ok_arg.repo = repo;
7779 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7780 10604dce 2021-09-24 thomas &ok_arg);
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 err = get_merge_branch_ref_name(&branch_refname, worktree);
7785 10604dce 2021-09-24 thomas if (err)
7786 10604dce 2021-09-24 thomas return err;
7787 10604dce 2021-09-24 thomas
7788 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7789 10604dce 2021-09-24 thomas if (err)
7790 10604dce 2021-09-24 thomas return err;
7791 10604dce 2021-09-24 thomas
7792 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7793 10604dce 2021-09-24 thomas 0);
7794 10604dce 2021-09-24 thomas if (err)
7795 10604dce 2021-09-24 thomas goto done;
7796 10604dce 2021-09-24 thomas
7797 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7798 10604dce 2021-09-24 thomas if (err)
7799 10604dce 2021-09-24 thomas goto done;
7800 10604dce 2021-09-24 thomas
7801 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7802 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7803 10604dce 2021-09-24 thomas goto done;
7804 10604dce 2021-09-24 thomas }
7805 10604dce 2021-09-24 thomas
7806 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
7807 10604dce 2021-09-24 thomas if (err)
7808 10604dce 2021-09-24 thomas goto done;
7809 10604dce 2021-09-24 thomas
7810 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7811 10604dce 2021-09-24 thomas if (err)
7812 10604dce 2021-09-24 thomas goto done;
7813 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
7814 10604dce 2021-09-24 thomas if (err)
7815 10604dce 2021-09-24 thomas goto done;
7816 10604dce 2021-09-24 thomas
7817 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7818 10604dce 2021-09-24 thomas if (err)
7819 10604dce 2021-09-24 thomas goto done;
7820 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
7821 10604dce 2021-09-24 thomas if (err)
7822 10604dce 2021-09-24 thomas goto done;
7823 10604dce 2021-09-24 thomas
7824 10604dce 2021-09-24 thomas done:
7825 10604dce 2021-09-24 thomas free(branch_refname);
7826 10604dce 2021-09-24 thomas free(commit_refname);
7827 10604dce 2021-09-24 thomas free(fileindex_path);
7828 10604dce 2021-09-24 thomas if (branch_ref)
7829 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7830 10604dce 2021-09-24 thomas if (commit_ref)
7831 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7832 10604dce 2021-09-24 thomas if (wt_branch)
7833 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
7834 10604dce 2021-09-24 thomas free(wt_branch_tip);
7835 10604dce 2021-09-24 thomas if (err) {
7836 10604dce 2021-09-24 thomas if (*fileindex) {
7837 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7838 10604dce 2021-09-24 thomas *fileindex = NULL;
7839 10604dce 2021-09-24 thomas }
7840 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7841 10604dce 2021-09-24 thomas }
7842 10604dce 2021-09-24 thomas return err;
7843 10604dce 2021-09-24 thomas }
7844 10604dce 2021-09-24 thomas
7845 10604dce 2021-09-24 thomas const struct got_error *
7846 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
7847 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7848 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
7849 10604dce 2021-09-24 thomas {
7850 10604dce 2021-09-24 thomas const struct got_error *err;
7851 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
7852 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7853 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7854 10604dce 2021-09-24 thomas int have_staged_files = 0;
7855 10604dce 2021-09-24 thomas
7856 10604dce 2021-09-24 thomas *branch_name = NULL;
7857 10604dce 2021-09-24 thomas *branch_tip = NULL;
7858 10604dce 2021-09-24 thomas *fileindex = NULL;
7859 10604dce 2021-09-24 thomas
7860 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7861 10604dce 2021-09-24 thomas if (err)
7862 10604dce 2021-09-24 thomas return err;
7863 10604dce 2021-09-24 thomas
7864 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, 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_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7869 10604dce 2021-09-24 thomas &have_staged_files);
7870 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7871 10604dce 2021-09-24 thomas goto done;
7872 10604dce 2021-09-24 thomas if (have_staged_files) {
7873 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
7874 10604dce 2021-09-24 thomas goto done;
7875 10604dce 2021-09-24 thomas }
7876 10604dce 2021-09-24 thomas
7877 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7878 10604dce 2021-09-24 thomas if (err)
7879 10604dce 2021-09-24 thomas goto done;
7880 10604dce 2021-09-24 thomas
7881 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7882 10604dce 2021-09-24 thomas if (err)
7883 10604dce 2021-09-24 thomas goto done;
7884 10604dce 2021-09-24 thomas
7885 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7886 10604dce 2021-09-24 thomas if (err)
7887 10604dce 2021-09-24 thomas goto done;
7888 10604dce 2021-09-24 thomas
7889 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
7890 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7891 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
7892 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
7893 10604dce 2021-09-24 thomas goto done;
7894 10604dce 2021-09-24 thomas }
7895 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7896 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
7897 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
7898 10604dce 2021-09-24 thomas goto done;
7899 10604dce 2021-09-24 thomas }
7900 10604dce 2021-09-24 thomas
7901 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7902 10604dce 2021-09-24 thomas if (err)
7903 10604dce 2021-09-24 thomas goto done;
7904 10604dce 2021-09-24 thomas
7905 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
7906 10604dce 2021-09-24 thomas if (err)
7907 10604dce 2021-09-24 thomas goto done;
7908 10604dce 2021-09-24 thomas done:
7909 10604dce 2021-09-24 thomas free(commit_refname);
7910 10604dce 2021-09-24 thomas free(branch_refname);
7911 10604dce 2021-09-24 thomas free(fileindex_path);
7912 10604dce 2021-09-24 thomas if (commit_ref)
7913 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7914 10604dce 2021-09-24 thomas if (branch_ref)
7915 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7916 10604dce 2021-09-24 thomas if (err) {
7917 10604dce 2021-09-24 thomas if (*branch_name) {
7918 10604dce 2021-09-24 thomas free(*branch_name);
7919 10604dce 2021-09-24 thomas *branch_name = NULL;
7920 10604dce 2021-09-24 thomas }
7921 10604dce 2021-09-24 thomas free(*branch_tip);
7922 10604dce 2021-09-24 thomas *branch_tip = NULL;
7923 10604dce 2021-09-24 thomas if (*fileindex) {
7924 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7925 10604dce 2021-09-24 thomas *fileindex = NULL;
7926 10604dce 2021-09-24 thomas }
7927 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7928 10604dce 2021-09-24 thomas }
7929 10604dce 2021-09-24 thomas return err;
7930 10604dce 2021-09-24 thomas }
7931 10604dce 2021-09-24 thomas
7932 10604dce 2021-09-24 thomas const struct got_error *
7933 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
7934 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
7935 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
7936 10604dce 2021-09-24 thomas {
7937 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7938 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
7939 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7940 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7941 10604dce 2021-09-24 thomas struct revert_file_args rfa;
7942 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
7943 10604dce 2021-09-24 thomas
7944 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
7945 945f9229 2022-04-16 thomas worktree->base_commit_id);
7946 945f9229 2022-04-16 thomas if (err)
7947 945f9229 2022-04-16 thomas goto done;
7948 945f9229 2022-04-16 thomas
7949 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7950 945f9229 2022-04-16 thomas worktree->path_prefix);
7951 10604dce 2021-09-24 thomas if (err)
7952 10604dce 2021-09-24 thomas goto done;
7953 10604dce 2021-09-24 thomas
7954 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7955 10604dce 2021-09-24 thomas if (err)
7956 10604dce 2021-09-24 thomas goto done;
7957 10604dce 2021-09-24 thomas
7958 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7959 10604dce 2021-09-24 thomas if (err)
7960 10604dce 2021-09-24 thomas goto done;
7961 10604dce 2021-09-24 thomas
7962 10604dce 2021-09-24 thomas rfa.worktree = worktree;
7963 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
7964 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
7965 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
7966 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
7967 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
7968 10604dce 2021-09-24 thomas rfa.repo = repo;
7969 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
7970 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7971 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7972 10604dce 2021-09-24 thomas if (err)
7973 10604dce 2021-09-24 thomas goto sync;
7974 10604dce 2021-09-24 thomas
7975 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7976 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
7977 10604dce 2021-09-24 thomas sync:
7978 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7979 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7980 10604dce 2021-09-24 thomas err = sync_err;
7981 10604dce 2021-09-24 thomas done:
7982 10604dce 2021-09-24 thomas free(tree_id);
7983 10604dce 2021-09-24 thomas free(commit_id);
7984 945f9229 2022-04-16 thomas if (commit)
7985 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7986 10604dce 2021-09-24 thomas if (fileindex)
7987 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7988 10604dce 2021-09-24 thomas free(fileindex_path);
7989 10604dce 2021-09-24 thomas
7990 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7991 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7992 10604dce 2021-09-24 thomas err = unlockerr;
7993 10604dce 2021-09-24 thomas return err;
7994 10604dce 2021-09-24 thomas }
7995 10604dce 2021-09-24 thomas
7996 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7997 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7998 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7999 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8000 2db2652d 2019-08-07 stsp struct got_repository *repo;
8001 2db2652d 2019-08-07 stsp int have_changes;
8002 2db2652d 2019-08-07 stsp };
8003 2db2652d 2019-08-07 stsp
8004 ef20f542 2022-06-26 thomas static const struct got_error *
8005 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8006 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8007 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8008 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8009 735ef5ac 2019-08-03 stsp {
8010 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8011 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8012 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8013 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8014 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8015 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8016 8b13ce36 2019-08-08 stsp
8017 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8018 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8019 8b13ce36 2019-08-08 stsp return NULL;
8020 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8021 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8022 735ef5ac 2019-08-03 stsp
8023 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8024 735ef5ac 2019-08-03 stsp if (ie == NULL)
8025 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8026 735ef5ac 2019-08-03 stsp
8027 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8028 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8029 735ef5ac 2019-08-03 stsp relpath) == -1)
8030 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8031 735ef5ac 2019-08-03 stsp
8032 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8033 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
8034 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8035 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
8036 735ef5ac 2019-08-03 stsp }
8037 735ef5ac 2019-08-03 stsp
8038 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8039 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8040 3aa5969e 2019-08-06 stsp goto done;
8041 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8042 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8043 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8044 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8045 735ef5ac 2019-08-03 stsp goto done;
8046 3aa5969e 2019-08-06 stsp }
8047 735ef5ac 2019-08-03 stsp
8048 2db2652d 2019-08-07 stsp a->have_changes = 1;
8049 2db2652d 2019-08-07 stsp
8050 735ef5ac 2019-08-03 stsp p = in_repo_path;
8051 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8052 735ef5ac 2019-08-03 stsp p++;
8053 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8054 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8055 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8056 735ef5ac 2019-08-03 stsp done:
8057 735ef5ac 2019-08-03 stsp free(in_repo_path);
8058 dc424a06 2019-08-07 stsp return err;
8059 dc424a06 2019-08-07 stsp }
8060 dc424a06 2019-08-07 stsp
8061 2db2652d 2019-08-07 stsp struct stage_path_arg {
8062 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8063 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8064 2db2652d 2019-08-07 stsp struct got_repository *repo;
8065 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8066 2db2652d 2019-08-07 stsp void *status_arg;
8067 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8068 2db2652d 2019-08-07 stsp void *patch_arg;
8069 7b5dc508 2019-10-28 stsp int staged_something;
8070 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8071 2db2652d 2019-08-07 stsp };
8072 2db2652d 2019-08-07 stsp
8073 2db2652d 2019-08-07 stsp static const struct got_error *
8074 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8075 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8076 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8077 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8078 0cb83759 2019-08-03 stsp {
8079 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8080 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8081 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8082 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8083 0cb83759 2019-08-03 stsp uint32_t stage;
8084 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8085 0aeb8099 2020-07-23 stsp struct stat sb;
8086 8b13ce36 2019-08-08 stsp
8087 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8088 8b13ce36 2019-08-08 stsp return NULL;
8089 0cb83759 2019-08-03 stsp
8090 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8091 d3e7c587 2019-08-03 stsp if (ie == NULL)
8092 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8093 0cb83759 2019-08-03 stsp
8094 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8095 2db2652d 2019-08-07 stsp relpath)== -1)
8096 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8097 0cb83759 2019-08-03 stsp
8098 0cb83759 2019-08-03 stsp switch (status) {
8099 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8100 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8101 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8102 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8103 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8104 0aeb8099 2020-07-23 stsp break;
8105 0aeb8099 2020-07-23 stsp }
8106 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8107 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8108 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8109 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8110 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8111 dc424a06 2019-08-07 stsp if (err)
8112 dc424a06 2019-08-07 stsp break;
8113 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8114 dc424a06 2019-08-07 stsp break;
8115 dc424a06 2019-08-07 stsp } else {
8116 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8117 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8118 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8119 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8120 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8121 dc424a06 2019-08-07 stsp break;
8122 dc424a06 2019-08-07 stsp }
8123 dc424a06 2019-08-07 stsp }
8124 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8125 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8126 0cb83759 2019-08-03 stsp if (err)
8127 dc424a06 2019-08-07 stsp break;
8128 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8129 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8130 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8131 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8132 0cb83759 2019-08-03 stsp else
8133 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8134 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8135 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8136 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8137 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8138 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8139 35213c7c 2020-07-23 stsp ssize_t target_len;
8140 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8141 35213c7c 2020-07-23 stsp sizeof(target_path));
8142 35213c7c 2020-07-23 stsp if (target_len == -1) {
8143 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8144 35213c7c 2020-07-23 stsp ondisk_path);
8145 35213c7c 2020-07-23 stsp break;
8146 35213c7c 2020-07-23 stsp }
8147 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8148 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8149 35213c7c 2020-07-23 stsp a->worktree->root_path);
8150 35213c7c 2020-07-23 stsp if (err)
8151 35213c7c 2020-07-23 stsp break;
8152 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8153 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8154 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8155 35213c7c 2020-07-23 stsp break;
8156 35213c7c 2020-07-23 stsp }
8157 35213c7c 2020-07-23 stsp }
8158 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8159 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8160 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8161 35213c7c 2020-07-23 stsp else
8162 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8163 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8164 0aeb8099 2020-07-23 stsp } else {
8165 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8166 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8167 0aeb8099 2020-07-23 stsp }
8168 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8169 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8170 dc424a06 2019-08-07 stsp break;
8171 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8172 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8173 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8174 f289c82c 2022-06-13 thomas if (err)
8175 f289c82c 2022-06-13 thomas break;
8176 f289c82c 2022-06-13 thomas /*
8177 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8178 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8179 f289c82c 2022-06-13 thomas */
8180 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8181 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8182 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8183 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8184 f289c82c 2022-06-13 thomas }
8185 0cb83759 2019-08-03 stsp break;
8186 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8187 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8188 d3e7c587 2019-08-03 stsp break;
8189 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8190 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8191 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8192 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8193 dc424a06 2019-08-07 stsp if (err)
8194 dc424a06 2019-08-07 stsp break;
8195 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8196 88f33a19 2019-08-08 stsp break;
8197 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8198 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8199 dc424a06 2019-08-07 stsp break;
8200 88f33a19 2019-08-08 stsp }
8201 dc424a06 2019-08-07 stsp }
8202 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8203 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8204 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8205 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8206 dc424a06 2019-08-07 stsp break;
8207 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8208 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8209 12463d8b 2019-12-13 stsp de_name);
8210 0cb83759 2019-08-03 stsp break;
8211 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8212 d3e7c587 2019-08-03 stsp break;
8213 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8214 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8215 ebf48fd5 2019-08-03 stsp break;
8216 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8217 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8218 2a06fe5f 2019-08-24 stsp break;
8219 0cb83759 2019-08-03 stsp default:
8220 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8221 537ac44b 2019-08-03 stsp break;
8222 0cb83759 2019-08-03 stsp }
8223 dc424a06 2019-08-07 stsp
8224 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8225 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8226 dc424a06 2019-08-07 stsp free(path_content);
8227 2db2652d 2019-08-07 stsp free(ondisk_path);
8228 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8229 0ebf8283 2019-07-24 stsp return err;
8230 0ebf8283 2019-07-24 stsp }
8231 0cb83759 2019-08-03 stsp
8232 0cb83759 2019-08-03 stsp const struct got_error *
8233 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8234 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8235 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8236 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8237 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8238 0cb83759 2019-08-03 stsp {
8239 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8240 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8241 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8242 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8243 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8244 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8245 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8246 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8247 0cb83759 2019-08-03 stsp
8248 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8249 0cb83759 2019-08-03 stsp if (err)
8250 0cb83759 2019-08-03 stsp return err;
8251 0cb83759 2019-08-03 stsp
8252 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8253 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8254 735ef5ac 2019-08-03 stsp if (err)
8255 735ef5ac 2019-08-03 stsp goto done;
8256 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8257 735ef5ac 2019-08-03 stsp if (err)
8258 735ef5ac 2019-08-03 stsp goto done;
8259 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8260 0cb83759 2019-08-03 stsp if (err)
8261 0cb83759 2019-08-03 stsp goto done;
8262 0cb83759 2019-08-03 stsp
8263 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8264 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8265 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8266 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8267 2db2652d 2019-08-07 stsp oka.repo = repo;
8268 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8269 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8270 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8271 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8272 735ef5ac 2019-08-03 stsp if (err)
8273 735ef5ac 2019-08-03 stsp goto done;
8274 735ef5ac 2019-08-03 stsp }
8275 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8276 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8277 2db2652d 2019-08-07 stsp goto done;
8278 2db2652d 2019-08-07 stsp }
8279 735ef5ac 2019-08-03 stsp
8280 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8281 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8282 2db2652d 2019-08-07 stsp spa.repo = repo;
8283 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8284 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8285 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8286 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8287 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8288 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8289 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8290 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8291 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8292 42005733 2019-08-03 stsp if (err)
8293 2db2652d 2019-08-07 stsp goto done;
8294 7b5dc508 2019-10-28 stsp }
8295 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8296 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8297 7b5dc508 2019-10-28 stsp goto done;
8298 0cb83759 2019-08-03 stsp }
8299 0cb83759 2019-08-03 stsp
8300 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8301 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8302 0cb83759 2019-08-03 stsp err = sync_err;
8303 0cb83759 2019-08-03 stsp done:
8304 735ef5ac 2019-08-03 stsp if (head_ref)
8305 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8306 735ef5ac 2019-08-03 stsp free(head_commit_id);
8307 0cb83759 2019-08-03 stsp free(fileindex_path);
8308 0cb83759 2019-08-03 stsp if (fileindex)
8309 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8310 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8311 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8312 0cb83759 2019-08-03 stsp err = unlockerr;
8313 0cb83759 2019-08-03 stsp return err;
8314 0cb83759 2019-08-03 stsp }
8315 ad493afc 2019-08-03 stsp
8316 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8317 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8318 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8319 ad493afc 2019-08-03 stsp struct got_repository *repo;
8320 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8321 ad493afc 2019-08-03 stsp void *progress_arg;
8322 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8323 2e1f37b0 2019-08-08 stsp void *patch_arg;
8324 ad493afc 2019-08-03 stsp };
8325 2e1f37b0 2019-08-08 stsp
8326 2e1f37b0 2019-08-08 stsp static const struct got_error *
8327 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8328 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8329 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8330 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8331 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8332 2e1f37b0 2019-08-08 stsp {
8333 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8334 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8335 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8336 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8337 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8338 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8339 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8340 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8341 2e1f37b0 2019-08-08 stsp
8342 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8343 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8344 2e1f37b0 2019-08-08 stsp
8345 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8346 2e1f37b0 2019-08-08 stsp if (err)
8347 2e1f37b0 2019-08-08 stsp return err;
8348 f4ae6ddb 2022-07-01 thomas
8349 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8350 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8351 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8352 f4ae6ddb 2022-07-01 thomas goto done;
8353 f4ae6ddb 2022-07-01 thomas }
8354 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8355 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8356 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8357 f4ae6ddb 2022-07-01 thomas goto done;
8358 f4ae6ddb 2022-07-01 thomas }
8359 f4ae6ddb 2022-07-01 thomas
8360 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8361 2e1f37b0 2019-08-08 stsp if (err)
8362 2e1f37b0 2019-08-08 stsp goto done;
8363 2e1f37b0 2019-08-08 stsp
8364 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8365 2e1f37b0 2019-08-08 stsp if (err)
8366 2e1f37b0 2019-08-08 stsp goto done;
8367 2e1f37b0 2019-08-08 stsp
8368 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8369 2e1f37b0 2019-08-08 stsp if (err)
8370 2e1f37b0 2019-08-08 stsp goto done;
8371 2e1f37b0 2019-08-08 stsp
8372 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8373 f4ae6ddb 2022-07-01 thomas fd2);
8374 2e1f37b0 2019-08-08 stsp if (err)
8375 2e1f37b0 2019-08-08 stsp goto done;
8376 2e1f37b0 2019-08-08 stsp
8377 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8378 2e1f37b0 2019-08-08 stsp if (err)
8379 2e1f37b0 2019-08-08 stsp goto done;
8380 ad493afc 2019-08-03 stsp
8381 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8382 2e1f37b0 2019-08-08 stsp if (err)
8383 2e1f37b0 2019-08-08 stsp goto done;
8384 2e1f37b0 2019-08-08 stsp
8385 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8386 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8387 2e1f37b0 2019-08-08 stsp if (err)
8388 2e1f37b0 2019-08-08 stsp goto done;
8389 2e1f37b0 2019-08-08 stsp
8390 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8391 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8392 2e1f37b0 2019-08-08 stsp if (err)
8393 2e1f37b0 2019-08-08 stsp goto done;
8394 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8395 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8396 2e1f37b0 2019-08-08 stsp if (err)
8397 2e1f37b0 2019-08-08 stsp goto done;
8398 2e1f37b0 2019-08-08 stsp
8399 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8400 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8401 2e1f37b0 2019-08-08 stsp goto done;
8402 2e1f37b0 2019-08-08 stsp }
8403 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8404 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8405 2e1f37b0 2019-08-08 stsp goto done;
8406 2e1f37b0 2019-08-08 stsp }
8407 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8408 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8409 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
8410 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8411 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8412 fe621944 2020-11-10 stsp nchanges++;
8413 fe621944 2020-11-10 stsp }
8414 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8415 2e1f37b0 2019-08-08 stsp int choice;
8416 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8417 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8418 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8419 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8420 2e1f37b0 2019-08-08 stsp if (err)
8421 2e1f37b0 2019-08-08 stsp goto done;
8422 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8423 2e1f37b0 2019-08-08 stsp have_content = 1;
8424 19e4b907 2019-08-08 stsp else
8425 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8426 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8427 2e1f37b0 2019-08-08 stsp break;
8428 2e1f37b0 2019-08-08 stsp }
8429 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8430 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8431 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8432 2e1f37b0 2019-08-08 stsp done:
8433 2e1f37b0 2019-08-08 stsp free(label1);
8434 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8435 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8436 2e1f37b0 2019-08-08 stsp if (blob)
8437 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8438 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8439 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8440 2e1f37b0 2019-08-08 stsp if (staged_blob)
8441 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8442 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8443 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8444 fe621944 2020-11-10 stsp err = free_err;
8445 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8446 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8447 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8448 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8449 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8450 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8451 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8452 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8453 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8454 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8455 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8456 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8457 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8458 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8459 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8460 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8461 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8462 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8463 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8464 2e1f37b0 2019-08-08 stsp }
8465 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8466 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8467 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8468 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8469 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8470 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8471 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8472 2e1f37b0 2019-08-08 stsp }
8473 2e1f37b0 2019-08-08 stsp free(path1);
8474 2e1f37b0 2019-08-08 stsp free(path2);
8475 fda8017d 2020-07-23 stsp return err;
8476 fda8017d 2020-07-23 stsp }
8477 fda8017d 2020-07-23 stsp
8478 fda8017d 2020-07-23 stsp static const struct got_error *
8479 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8480 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8481 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8482 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8483 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8484 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8485 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8486 fda8017d 2020-07-23 stsp {
8487 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8488 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8489 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8490 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8491 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8492 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8493 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8494 fda8017d 2020-07-23 stsp struct stat sb;
8495 fda8017d 2020-07-23 stsp
8496 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8497 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8498 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8499 fda8017d 2020-07-23 stsp if (err)
8500 fda8017d 2020-07-23 stsp return err;
8501 fda8017d 2020-07-23 stsp
8502 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8503 fda8017d 2020-07-23 stsp return NULL;
8504 fda8017d 2020-07-23 stsp
8505 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8506 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8507 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8508 fda8017d 2020-07-23 stsp if (err)
8509 fda8017d 2020-07-23 stsp goto done;
8510 fda8017d 2020-07-23 stsp }
8511 fda8017d 2020-07-23 stsp
8512 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
8513 fda8017d 2020-07-23 stsp if (f == NULL) {
8514 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8515 fda8017d 2020-07-23 stsp path_unstaged_content);
8516 fda8017d 2020-07-23 stsp goto done;
8517 fda8017d 2020-07-23 stsp }
8518 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8519 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8520 fda8017d 2020-07-23 stsp goto done;
8521 fda8017d 2020-07-23 stsp }
8522 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8523 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8524 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8525 fda8017d 2020-07-23 stsp size_t r;
8526 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8527 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8528 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8529 fda8017d 2020-07-23 stsp goto done;
8530 fda8017d 2020-07-23 stsp }
8531 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8532 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8533 fda8017d 2020-07-23 stsp goto done;
8534 fda8017d 2020-07-23 stsp }
8535 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8536 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8537 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8538 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8539 fda8017d 2020-07-23 stsp progress_arg);
8540 fda8017d 2020-07-23 stsp } else {
8541 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8542 67a66647 2021-05-31 stsp
8543 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8544 67a66647 2021-05-31 stsp if (err)
8545 67a66647 2021-05-31 stsp return err;
8546 67a66647 2021-05-31 stsp
8547 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8548 67a66647 2021-05-31 stsp parent) == -1) {
8549 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8550 67a66647 2021-05-31 stsp base_path = NULL;
8551 67a66647 2021-05-31 stsp goto done;
8552 67a66647 2021-05-31 stsp }
8553 67a66647 2021-05-31 stsp
8554 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8555 67a66647 2021-05-31 stsp if (err)
8556 67a66647 2021-05-31 stsp goto done;
8557 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8558 67a66647 2021-05-31 stsp blob_base);
8559 67a66647 2021-05-31 stsp if (err)
8560 67a66647 2021-05-31 stsp goto done;
8561 67a66647 2021-05-31 stsp
8562 b6b86fd1 2022-08-30 thomas /*
8563 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8564 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8565 eec2f5a9 2021-06-03 stsp */
8566 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8567 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8568 eec2f5a9 2021-06-03 stsp ondisk_path);
8569 eec2f5a9 2021-06-03 stsp if (err)
8570 eec2f5a9 2021-06-03 stsp goto done;
8571 eec2f5a9 2021-06-03 stsp } else {
8572 eec2f5a9 2021-06-03 stsp int fd;
8573 06340621 2021-12-31 thomas fd = open(ondisk_path,
8574 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8575 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8576 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8577 eec2f5a9 2021-06-03 stsp goto done;
8578 eec2f5a9 2021-06-03 stsp }
8579 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8580 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8581 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8582 eec2f5a9 2021-06-03 stsp close(fd);
8583 eec2f5a9 2021-06-03 stsp goto done;
8584 eec2f5a9 2021-06-03 stsp }
8585 eec2f5a9 2021-06-03 stsp }
8586 eec2f5a9 2021-06-03 stsp
8587 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8588 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8589 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8590 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8591 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8592 fda8017d 2020-07-23 stsp }
8593 fda8017d 2020-07-23 stsp if (err)
8594 fda8017d 2020-07-23 stsp goto done;
8595 fda8017d 2020-07-23 stsp
8596 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8597 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8598 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8599 2ac8aa02 2020-11-09 stsp } else {
8600 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8601 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8602 2ac8aa02 2020-11-09 stsp }
8603 fda8017d 2020-07-23 stsp done:
8604 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8605 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8606 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8607 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8608 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8609 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8610 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8611 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8612 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8613 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8614 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8615 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8616 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8617 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8618 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8619 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8620 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8621 67a66647 2021-05-31 stsp free(blob_base_path);
8622 67a66647 2021-05-31 stsp free(parent);
8623 67a66647 2021-05-31 stsp free(base_path);
8624 2e1f37b0 2019-08-08 stsp return err;
8625 2e1f37b0 2019-08-08 stsp }
8626 2e1f37b0 2019-08-08 stsp
8627 ad493afc 2019-08-03 stsp static const struct got_error *
8628 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8629 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8630 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8631 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8632 ad493afc 2019-08-03 stsp {
8633 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8634 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8635 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8636 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8637 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8638 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8639 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8640 9bc94a15 2019-08-03 stsp struct stat sb;
8641 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
8642 ad493afc 2019-08-03 stsp
8643 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8644 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8645 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8646 2e1f37b0 2019-08-08 stsp return NULL;
8647 2e1f37b0 2019-08-08 stsp
8648 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8649 ad493afc 2019-08-03 stsp if (ie == NULL)
8650 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8651 9bc94a15 2019-08-03 stsp
8652 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8653 9bc94a15 2019-08-03 stsp == -1)
8654 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8655 ad493afc 2019-08-03 stsp
8656 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8657 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8658 f69721c3 2019-10-21 stsp if (err)
8659 f69721c3 2019-10-21 stsp goto done;
8660 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8661 f69721c3 2019-10-21 stsp id_str) == -1) {
8662 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8663 f4ae6ddb 2022-07-01 thomas goto done;
8664 f4ae6ddb 2022-07-01 thomas }
8665 f4ae6ddb 2022-07-01 thomas
8666 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
8667 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
8668 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8669 f4ae6ddb 2022-07-01 thomas goto done;
8670 f4ae6ddb 2022-07-01 thomas }
8671 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
8672 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
8673 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
8674 f69721c3 2019-10-21 stsp goto done;
8675 f69721c3 2019-10-21 stsp }
8676 f69721c3 2019-10-21 stsp
8677 ad493afc 2019-08-03 stsp switch (staged_status) {
8678 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8679 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8680 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
8681 ad493afc 2019-08-03 stsp if (err)
8682 ad493afc 2019-08-03 stsp break;
8683 ad493afc 2019-08-03 stsp /* fall through */
8684 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8685 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8686 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8687 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8688 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8689 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8690 2e1f37b0 2019-08-08 stsp if (err)
8691 2e1f37b0 2019-08-08 stsp break;
8692 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8693 2e1f37b0 2019-08-08 stsp break;
8694 2e1f37b0 2019-08-08 stsp } else {
8695 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8696 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8697 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8698 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8699 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8700 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8701 2e1f37b0 2019-08-08 stsp }
8702 2e1f37b0 2019-08-08 stsp }
8703 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8704 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
8705 ad493afc 2019-08-03 stsp if (err)
8706 ad493afc 2019-08-03 stsp break;
8707 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8708 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8709 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8710 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8711 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8712 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8713 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8714 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8715 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8716 ea7786be 2020-07-23 stsp break;
8717 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8718 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8719 36bf999c 2020-07-23 stsp char *staged_target;
8720 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8721 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8722 36bf999c 2020-07-23 stsp if (err)
8723 36bf999c 2020-07-23 stsp goto done;
8724 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8725 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8726 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8727 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8728 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8729 36bf999c 2020-07-23 stsp free(staged_target);
8730 dfe9fba0 2020-07-23 stsp } else {
8731 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8732 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8733 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8734 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8735 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8736 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8737 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8738 dfe9fba0 2020-07-23 stsp }
8739 ea7786be 2020-07-23 stsp break;
8740 ea7786be 2020-07-23 stsp default:
8741 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8742 ea7786be 2020-07-23 stsp break;
8743 ea7786be 2020-07-23 stsp }
8744 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8745 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8746 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8747 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8748 2ac8aa02 2020-11-09 stsp }
8749 ad493afc 2019-08-03 stsp break;
8750 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8751 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8752 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8753 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8754 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8755 2e1f37b0 2019-08-08 stsp if (err)
8756 2e1f37b0 2019-08-08 stsp break;
8757 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8758 2e1f37b0 2019-08-08 stsp break;
8759 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8760 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8761 2e1f37b0 2019-08-08 stsp break;
8762 2e1f37b0 2019-08-08 stsp }
8763 2e1f37b0 2019-08-08 stsp }
8764 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8765 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8766 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8767 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8768 9bc94a15 2019-08-03 stsp if (err)
8769 9bc94a15 2019-08-03 stsp break;
8770 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8771 ad493afc 2019-08-03 stsp break;
8772 ad493afc 2019-08-03 stsp }
8773 f69721c3 2019-10-21 stsp done:
8774 ad493afc 2019-08-03 stsp free(ondisk_path);
8775 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8776 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8777 ad493afc 2019-08-03 stsp if (blob_base)
8778 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8779 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8780 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
8781 ad493afc 2019-08-03 stsp if (blob_staged)
8782 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8783 f69721c3 2019-10-21 stsp free(id_str);
8784 f69721c3 2019-10-21 stsp free(label_orig);
8785 ad493afc 2019-08-03 stsp return err;
8786 ad493afc 2019-08-03 stsp }
8787 ad493afc 2019-08-03 stsp
8788 ad493afc 2019-08-03 stsp const struct got_error *
8789 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8790 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8791 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8792 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8793 ad493afc 2019-08-03 stsp struct got_repository *repo)
8794 ad493afc 2019-08-03 stsp {
8795 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8796 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8797 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8798 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8799 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8800 ad493afc 2019-08-03 stsp
8801 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8802 ad493afc 2019-08-03 stsp if (err)
8803 ad493afc 2019-08-03 stsp return err;
8804 ad493afc 2019-08-03 stsp
8805 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8806 ad493afc 2019-08-03 stsp if (err)
8807 ad493afc 2019-08-03 stsp goto done;
8808 ad493afc 2019-08-03 stsp
8809 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8810 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8811 ad493afc 2019-08-03 stsp upa.repo = repo;
8812 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8813 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8814 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8815 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8816 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8817 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8818 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
8819 ad493afc 2019-08-03 stsp if (err)
8820 ad493afc 2019-08-03 stsp goto done;
8821 ad493afc 2019-08-03 stsp }
8822 ad493afc 2019-08-03 stsp
8823 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8824 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8825 ad493afc 2019-08-03 stsp err = sync_err;
8826 ad493afc 2019-08-03 stsp done:
8827 ad493afc 2019-08-03 stsp free(fileindex_path);
8828 ad493afc 2019-08-03 stsp if (fileindex)
8829 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8830 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8831 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8832 ad493afc 2019-08-03 stsp err = unlockerr;
8833 ad493afc 2019-08-03 stsp return err;
8834 ad493afc 2019-08-03 stsp }
8835 b2118c49 2020-07-28 stsp
8836 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8837 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8838 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8839 b2118c49 2020-07-28 stsp void *info_arg;
8840 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8841 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8842 b2118c49 2020-07-28 stsp void *cancel_arg;
8843 b2118c49 2020-07-28 stsp };
8844 b2118c49 2020-07-28 stsp
8845 b2118c49 2020-07-28 stsp static const struct got_error *
8846 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8847 b2118c49 2020-07-28 stsp {
8848 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8849 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8850 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8851 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8852 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8853 b2118c49 2020-07-28 stsp int stage;
8854 b2118c49 2020-07-28 stsp
8855 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8856 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8857 b2118c49 2020-07-28 stsp
8858 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8859 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8860 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8861 b2118c49 2020-07-28 stsp break;
8862 b2118c49 2020-07-28 stsp }
8863 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8864 b2118c49 2020-07-28 stsp return NULL;
8865 b2118c49 2020-07-28 stsp
8866 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8867 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8868 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8869 b2118c49 2020-07-28 stsp }
8870 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8871 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8872 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8873 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8874 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8875 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8876 b2118c49 2020-07-28 stsp }
8877 b2118c49 2020-07-28 stsp
8878 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8879 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8880 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8881 b2118c49 2020-07-28 stsp }
8882 b2118c49 2020-07-28 stsp
8883 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8884 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8885 b2118c49 2020-07-28 stsp }
8886 b2118c49 2020-07-28 stsp
8887 b2118c49 2020-07-28 stsp const struct got_error *
8888 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8889 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8890 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8891 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8892 b2118c49 2020-07-28 stsp
8893 b2118c49 2020-07-28 stsp {
8894 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8895 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8896 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8897 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8898 b2118c49 2020-07-28 stsp
8899 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8900 b2118c49 2020-07-28 stsp if (err)
8901 b2118c49 2020-07-28 stsp return err;
8902 b2118c49 2020-07-28 stsp
8903 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8904 b2118c49 2020-07-28 stsp if (err)
8905 b2118c49 2020-07-28 stsp goto done;
8906 b2118c49 2020-07-28 stsp
8907 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8908 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8909 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8910 b2118c49 2020-07-28 stsp arg.paths = paths;
8911 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8912 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8913 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8914 b2118c49 2020-07-28 stsp &arg);
8915 b2118c49 2020-07-28 stsp done:
8916 b2118c49 2020-07-28 stsp free(fileindex_path);
8917 b2118c49 2020-07-28 stsp if (fileindex)
8918 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8919 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8920 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8921 b2118c49 2020-07-28 stsp err = unlockerr;
8922 b2118c49 2020-07-28 stsp return err;
8923 b2118c49 2020-07-28 stsp }
8924 814624e7 2022-03-22 thomas
8925 814624e7 2022-03-22 thomas static const struct got_error *
8926 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
8927 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
8928 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
8929 814624e7 2022-03-22 thomas {
8930 814624e7 2022-03-22 thomas const struct got_error *err;
8931 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
8932 814624e7 2022-03-22 thomas struct stat sb;
8933 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
8934 814624e7 2022-03-22 thomas
8935 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
8936 814624e7 2022-03-22 thomas if (err)
8937 814624e7 2022-03-22 thomas return err;
8938 814624e7 2022-03-22 thomas
8939 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8940 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
8941 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
8942 814624e7 2022-03-22 thomas
8943 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8944 814624e7 2022-03-22 thomas if (ie) {
8945 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
8946 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8947 12de5570 2022-05-01 thomas repo);
8948 814624e7 2022-03-22 thomas if (err)
8949 814624e7 2022-03-22 thomas goto done;
8950 814624e7 2022-03-22 thomas } else {
8951 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
8952 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
8953 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
8954 814624e7 2022-03-22 thomas if (errno != ENOENT) {
8955 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
8956 814624e7 2022-03-22 thomas ondisk_path);
8957 814624e7 2022-03-22 thomas goto done;
8958 814624e7 2022-03-22 thomas }
8959 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
8960 814624e7 2022-03-22 thomas }
8961 814624e7 2022-03-22 thomas }
8962 814624e7 2022-03-22 thomas
8963 814624e7 2022-03-22 thomas done:
8964 814624e7 2022-03-22 thomas free(ondisk_path);
8965 814624e7 2022-03-22 thomas return err;
8966 814624e7 2022-03-22 thomas }
8967 814624e7 2022-03-22 thomas
8968 814624e7 2022-03-22 thomas static const struct got_error *
8969 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
8970 814624e7 2022-03-22 thomas unsigned char staged_status)
8971 814624e7 2022-03-22 thomas {
8972 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
8973 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
8974 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
8975 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
8976 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
8977 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
8978 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8979 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
8980 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8981 814624e7 2022-03-22 thomas return NULL;
8982 814624e7 2022-03-22 thomas }
8983 814624e7 2022-03-22 thomas
8984 814624e7 2022-03-22 thomas static const struct got_error *
8985 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
8986 814624e7 2022-03-22 thomas {
8987 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
8988 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
8989 814624e7 2022-03-22 thomas return NULL;
8990 814624e7 2022-03-22 thomas }
8991 814624e7 2022-03-22 thomas
8992 814624e7 2022-03-22 thomas static const struct got_error *
8993 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
8994 814624e7 2022-03-22 thomas unsigned char staged_status)
8995 814624e7 2022-03-22 thomas {
8996 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
8997 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
8998 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
8999 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9000 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9001 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9002 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9003 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9004 814624e7 2022-03-22 thomas return NULL;
9005 814624e7 2022-03-22 thomas }
9006 814624e7 2022-03-22 thomas
9007 814624e7 2022-03-22 thomas const struct got_error *
9008 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9009 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9010 814624e7 2022-03-22 thomas {
9011 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9012 814624e7 2022-03-22 thomas }
9013 814624e7 2022-03-22 thomas
9014 814624e7 2022-03-22 thomas const struct got_error *
9015 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9016 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9017 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9018 814624e7 2022-03-22 thomas {
9019 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9020 814624e7 2022-03-22 thomas int file_renamed = 0;
9021 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9022 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9023 814624e7 2022-03-22 thomas
9024 814624e7 2022-03-22 thomas *oldpath = NULL;
9025 814624e7 2022-03-22 thomas *newpath = NULL;
9026 814624e7 2022-03-22 thomas
9027 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9028 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9029 814624e7 2022-03-22 thomas if (err)
9030 814624e7 2022-03-22 thomas goto done;
9031 814624e7 2022-03-22 thomas
9032 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9033 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9034 814624e7 2022-03-22 thomas if (err)
9035 814624e7 2022-03-22 thomas goto done;
9036 814624e7 2022-03-22 thomas
9037 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9038 814624e7 2022-03-22 thomas file_renamed = 1;
9039 814624e7 2022-03-22 thomas
9040 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9041 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9042 814624e7 2022-03-22 thomas else if (file_renamed) {
9043 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9044 814624e7 2022-03-22 thomas if (err == NULL)
9045 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9046 814624e7 2022-03-22 thomas } else if (old == NULL)
9047 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9048 814624e7 2022-03-22 thomas else
9049 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9050 814624e7 2022-03-22 thomas
9051 814624e7 2022-03-22 thomas done:
9052 814624e7 2022-03-22 thomas if (err) {
9053 814624e7 2022-03-22 thomas free(*oldpath);
9054 814624e7 2022-03-22 thomas *oldpath = NULL;
9055 814624e7 2022-03-22 thomas free(*newpath);
9056 814624e7 2022-03-22 thomas *newpath = NULL;
9057 814624e7 2022-03-22 thomas }
9058 814624e7 2022-03-22 thomas return err;
9059 814624e7 2022-03-22 thomas }
9060 814624e7 2022-03-22 thomas
9061 814624e7 2022-03-22 thomas const struct got_error *
9062 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9063 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9064 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9065 814624e7 2022-03-22 thomas {
9066 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9067 5e22b737 2022-05-31 thomas
9068 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9069 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9070 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9071 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9072 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9073 5e22b737 2022-05-31 thomas saa.repo = repo;
9074 5e22b737 2022-05-31 thomas
9075 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9076 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9077 814624e7 2022-03-22 thomas }
9078 5e22b737 2022-05-31 thomas
9079 5e22b737 2022-05-31 thomas const struct got_error *
9080 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9081 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9082 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9083 5e22b737 2022-05-31 thomas {
9084 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9085 5e22b737 2022-05-31 thomas
9086 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9087 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9088 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9089 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9090 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9091 5e22b737 2022-05-31 thomas sda.repo = repo;
9092 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9093 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9094 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9095 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9096 5e22b737 2022-05-31 thomas
9097 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9098 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9099 5e22b737 2022-05-31 thomas }
9100 5e22b737 2022-05-31 thomas
9101 5e22b737 2022-05-31 thomas const struct got_error *
9102 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9103 36832a8e 2022-05-31 thomas const char *fileindex_path)
9104 5e22b737 2022-05-31 thomas {
9105 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9106 5e22b737 2022-05-31 thomas
9107 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9108 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9109 5e22b737 2022-05-31 thomas
9110 5e22b737 2022-05-31 thomas return err;
9111 5e22b737 2022-05-31 thomas }