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 4fccd2fe 2023-03-08 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 86c3caaf 2018-03-09 stsp
19 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 86c3caaf 2018-03-09 stsp
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 12ce7a6c 2019-08-12 stsp #include <limits.h>
24 f5c49f82 2019-01-06 stsp #include <stddef.h>
25 86c3caaf 2018-03-09 stsp #include <string.h>
26 86c3caaf 2018-03-09 stsp #include <stdio.h>
27 86c3caaf 2018-03-09 stsp #include <stdlib.h>
28 303e14b5 2019-09-23 stsp #include <time.h>
29 86c3caaf 2018-03-09 stsp #include <fcntl.h>
30 86c3caaf 2018-03-09 stsp #include <errno.h>
31 86c3caaf 2018-03-09 stsp #include <unistd.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.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 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
57 9d31a1d8 2018-03-11 stsp
58 9d31a1d8 2018-03-11 stsp #ifndef MIN
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 9d31a1d8 2018-03-11 stsp #endif
61 f69721c3 2019-10-21 stsp
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 a2c162eb 2022-10-30 thomas
65 a2c162eb 2022-10-30 thomas static mode_t apply_umask(mode_t);
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpfile, path, "");
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
118 09fe317a 2018-03-11 stsp }
119 09fe317a 2018-03-11 stsp
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
122 024e9686 2019-05-14 stsp {
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
125 024e9686 2019-05-14 stsp
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
130 024e9686 2019-05-14 stsp } else {
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
134 024e9686 2019-05-14 stsp }
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
138 024e9686 2019-05-14 stsp }
139 024e9686 2019-05-14 stsp
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
143 86c3caaf 2018-03-09 stsp {
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
154 65596e15 2018-12-24 stsp
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
157 0c48fee2 2019-03-11 stsp goto done;
158 0c48fee2 2019-03-11 stsp }
159 0c48fee2 2019-03-11 stsp
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
168 86c3caaf 2018-03-09 stsp
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
172 0bb8a95e 2018-03-12 stsp }
173 577ec78f 2018-03-11 stsp
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
183 86c3caaf 2018-03-09 stsp goto done;
184 86c3caaf 2018-03-09 stsp }
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
187 86c3caaf 2018-03-09 stsp goto done;
188 86c3caaf 2018-03-09 stsp }
189 86c3caaf 2018-03-09 stsp
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 056e7441 2018-03-11 stsp if (err)
193 056e7441 2018-03-11 stsp goto done;
194 056e7441 2018-03-11 stsp
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 99724ed4 2018-03-10 stsp if (err)
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
202 65596e15 2018-12-24 stsp if (err)
203 65596e15 2018-12-24 stsp goto done;
204 65596e15 2018-12-24 stsp
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp goto done;
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 99724ed4 2018-03-10 stsp if (err)
211 86c3caaf 2018-03-09 stsp goto done;
212 86c3caaf 2018-03-09 stsp
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
216 99724ed4 2018-03-10 stsp if (err)
217 86c3caaf 2018-03-09 stsp goto done;
218 86c3caaf 2018-03-09 stsp
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
222 ec22038e 2019-03-10 stsp if (err)
223 ec22038e 2019-03-10 stsp goto done;
224 ec22038e 2019-03-10 stsp
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
229 ec22038e 2019-03-10 stsp goto done;
230 ec22038e 2019-03-10 stsp }
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp }
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 577ec78f 2018-03-11 stsp if (err)
238 577ec78f 2018-03-11 stsp goto done;
239 577ec78f 2018-03-11 stsp
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 1451e70d 2018-03-10 stsp goto done;
244 1451e70d 2018-03-10 stsp }
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 99724ed4 2018-03-10 stsp if (err)
247 1451e70d 2018-03-10 stsp goto done;
248 1451e70d 2018-03-10 stsp
249 86c3caaf 2018-03-09 stsp done:
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
257 86c3caaf 2018-03-09 stsp }
258 86c3caaf 2018-03-09 stsp
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
262 e5dc7198 2018-12-29 stsp {
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
264 e5dc7198 2018-12-29 stsp
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
268 e5dc7198 2018-12-29 stsp }
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
273 86c3caaf 2018-03-09 stsp }
274 86c3caaf 2018-03-09 stsp
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
277 86c3caaf 2018-03-09 stsp {
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
279 024e9686 2019-05-14 stsp }
280 024e9686 2019-05-14 stsp
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
284 024e9686 2019-05-14 stsp {
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
287 024e9686 2019-05-14 stsp
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
292 024e9686 2019-05-14 stsp goto done;
293 024e9686 2019-05-14 stsp }
294 024e9686 2019-05-14 stsp
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
298 024e9686 2019-05-14 stsp goto done;
299 024e9686 2019-05-14 stsp }
300 024e9686 2019-05-14 stsp
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
302 024e9686 2019-05-14 stsp if (err)
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
307 024e9686 2019-05-14 stsp done:
308 024e9686 2019-05-14 stsp free(path_got);
309 024e9686 2019-05-14 stsp if (err)
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
312 507dc3bb 2018-12-29 stsp }
313 507dc3bb 2018-12-29 stsp
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
316 507dc3bb 2018-12-29 stsp {
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
318 86c3caaf 2018-03-09 stsp }
319 86c3caaf 2018-03-09 stsp
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
323 507dc3bb 2018-12-29 stsp {
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
328 507dc3bb 2018-12-29 stsp
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
333 507dc3bb 2018-12-29 stsp goto done;
334 507dc3bb 2018-12-29 stsp }
335 507dc3bb 2018-12-29 stsp
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
337 507dc3bb 2018-12-29 stsp if (err)
338 507dc3bb 2018-12-29 stsp return err;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 507dc3bb 2018-12-29 stsp goto done;
343 507dc3bb 2018-12-29 stsp }
344 507dc3bb 2018-12-29 stsp
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
347 507dc3bb 2018-12-29 stsp if (err)
348 507dc3bb 2018-12-29 stsp goto done;
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
357 507dc3bb 2018-12-29 stsp goto done;
358 507dc3bb 2018-12-29 stsp }
359 507dc3bb 2018-12-29 stsp done:
360 507dc3bb 2018-12-29 stsp if (obj)
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
365 50b0790e 2020-09-11 stsp }
366 50b0790e 2020-09-11 stsp
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
369 50b0790e 2020-09-11 stsp {
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
371 507dc3bb 2018-12-29 stsp }
372 507dc3bb 2018-12-29 stsp
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
375 86c3caaf 2018-03-09 stsp {
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
381 21908da4 2019-01-13 stsp }
382 21908da4 2019-01-13 stsp
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
385 4a1ddfc2 2019-01-12 stsp {
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
388 4a1ddfc2 2019-01-12 stsp
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
391 4a1ddfc2 2019-01-12 stsp
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 ddcd8544 2019-03-11 stsp }
402 ddcd8544 2019-03-11 stsp }
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
405 68c76935 2019-02-19 stsp }
406 68c76935 2019-02-19 stsp
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
409 68c76935 2019-02-19 stsp {
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
414 68c76935 2019-02-19 stsp
415 68c76935 2019-02-19 stsp *same = 1;
416 68c76935 2019-02-19 stsp
417 230a42bd 2019-05-11 jcs for (;;) {
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
421 68c76935 2019-02-19 stsp break;
422 68c76935 2019-02-19 stsp }
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp break;
427 68c76935 2019-02-19 stsp }
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
430 68c76935 2019-02-19 stsp *same = 0;
431 68c76935 2019-02-19 stsp break;
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
434 68c76935 2019-02-19 stsp *same = 0;
435 68c76935 2019-02-19 stsp break;
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 68c76935 2019-02-19 stsp *same = 0;
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp } else {
442 68c76935 2019-02-19 stsp *same = 0;
443 68c76935 2019-02-19 stsp break;
444 68c76935 2019-02-19 stsp }
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp
447 68c76935 2019-02-19 stsp return err;
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
452 68c76935 2019-02-19 stsp {
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
455 68c76935 2019-02-19 stsp
456 68c76935 2019-02-19 stsp *same = 1;
457 68c76935 2019-02-19 stsp
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
461 68c76935 2019-02-19 stsp
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
465 68c76935 2019-02-19 stsp
466 68c76935 2019-02-19 stsp if (size1 != size2) {
467 68c76935 2019-02-19 stsp *same = 0;
468 68c76935 2019-02-19 stsp return NULL;
469 68c76935 2019-02-19 stsp }
470 68c76935 2019-02-19 stsp
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
477 24f136e0 2021-11-20 thomas }
478 24f136e0 2021-11-20 thomas
479 24f136e0 2021-11-20 thomas static const struct got_error *
480 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
481 24f136e0 2021-11-20 thomas {
482 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
483 24f136e0 2021-11-20 thomas size_t flen;
484 24f136e0 2021-11-20 thomas ssize_t outlen;
485 24f136e0 2021-11-20 thomas
486 24f136e0 2021-11-20 thomas *outsize = 0;
487 24f136e0 2021-11-20 thomas
488 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
489 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
490 24f136e0 2021-11-20 thomas
491 24f136e0 2021-11-20 thomas for (;;) {
492 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
493 24f136e0 2021-11-20 thomas if (flen == 0) {
494 24f136e0 2021-11-20 thomas if (ferror(f))
495 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
496 24f136e0 2021-11-20 thomas if (feof(f))
497 24f136e0 2021-11-20 thomas break;
498 24f136e0 2021-11-20 thomas }
499 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
500 24f136e0 2021-11-20 thomas if (outlen == -1)
501 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
502 24f136e0 2021-11-20 thomas if (outlen != flen)
503 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
504 24f136e0 2021-11-20 thomas *outsize += outlen;
505 24f136e0 2021-11-20 thomas }
506 24f136e0 2021-11-20 thomas
507 24f136e0 2021-11-20 thomas return NULL;
508 24f136e0 2021-11-20 thomas }
509 24f136e0 2021-11-20 thomas
510 24f136e0 2021-11-20 thomas static const struct got_error *
511 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
512 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 24f136e0 2021-11-20 thomas const char *ondisk_path)
515 24f136e0 2021-11-20 thomas {
516 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
517 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
518 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
522 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
523 24f136e0 2021-11-20 thomas
524 24f136e0 2021-11-20 thomas *overlapcnt = 0;
525 24f136e0 2021-11-20 thomas
526 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 24f136e0 2021-11-20 thomas if (err)
528 24f136e0 2021-11-20 thomas return err;
529 24f136e0 2021-11-20 thomas
530 24f136e0 2021-11-20 thomas if (same_content)
531 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
532 24f136e0 2021-11-20 thomas
533 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
534 24f136e0 2021-11-20 thomas if (err)
535 24f136e0 2021-11-20 thomas return err;
536 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
537 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
538 24f136e0 2021-11-20 thomas if (err)
539 24f136e0 2021-11-20 thomas return err;
540 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
541 24f136e0 2021-11-20 thomas
542 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
543 24f136e0 2021-11-20 thomas *overlapcnt = 1;
544 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
546 24f136e0 2021-11-20 thomas goto done;
547 24f136e0 2021-11-20 thomas }
548 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
550 24f136e0 2021-11-20 thomas goto done;
551 24f136e0 2021-11-20 thomas }
552 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
554 24f136e0 2021-11-20 thomas goto done;
555 24f136e0 2021-11-20 thomas }
556 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 fc2a50f2 2022-11-01 thomas base_path_orig, "");
558 24f136e0 2021-11-20 thomas if (err)
559 24f136e0 2021-11-20 thomas goto done;
560 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 fc2a50f2 2022-11-01 thomas base_path_deriv, "");
562 24f136e0 2021-11-20 thomas if (err)
563 24f136e0 2021-11-20 thomas goto done;
564 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 fc2a50f2 2022-11-01 thomas base_path_deriv2, "");
566 24f136e0 2021-11-20 thomas if (err)
567 24f136e0 2021-11-20 thomas goto done;
568 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 24f136e0 2021-11-20 thomas if (err)
570 24f136e0 2021-11-20 thomas goto done;
571 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 24f136e0 2021-11-20 thomas if (err)
573 24f136e0 2021-11-20 thomas goto done;
574 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 24f136e0 2021-11-20 thomas if (err)
576 24f136e0 2021-11-20 thomas goto done;
577 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
578 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
579 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
580 24f136e0 2021-11-20 thomas goto done;
581 24f136e0 2021-11-20 thomas }
582 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
585 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
586 24f136e0 2021-11-20 thomas path_deriv) < 0) {
587 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
588 24f136e0 2021-11-20 thomas goto done;
589 24f136e0 2021-11-20 thomas }
590 24f136e0 2021-11-20 thomas if (size_orig > 0) {
591 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
593 24f136e0 2021-11-20 thomas label_orig ? " " : "",
594 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
595 24f136e0 2021-11-20 thomas path_orig) < 0) {
596 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
597 24f136e0 2021-11-20 thomas goto done;
598 24f136e0 2021-11-20 thomas }
599 24f136e0 2021-11-20 thomas }
600 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
602 24f136e0 2021-11-20 thomas path_deriv2,
603 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
604 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
605 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
606 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
607 24f136e0 2021-11-20 thomas goto done;
608 24f136e0 2021-11-20 thomas }
609 24f136e0 2021-11-20 thomas } else if (changed_deriv)
610 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 24f136e0 2021-11-20 thomas else if (changed_deriv2)
612 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 24f136e0 2021-11-20 thomas done:
614 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 24f136e0 2021-11-20 thomas err == NULL)
616 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
617 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
619 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
621 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
623 24f136e0 2021-11-20 thomas free(path_orig);
624 24f136e0 2021-11-20 thomas free(path_deriv);
625 24f136e0 2021-11-20 thomas free(path_deriv2);
626 24f136e0 2021-11-20 thomas free(base_path_orig);
627 24f136e0 2021-11-20 thomas free(base_path_deriv);
628 24f136e0 2021-11-20 thomas free(base_path_deriv2);
629 24f136e0 2021-11-20 thomas return err;
630 6353ad76 2019-02-08 stsp }
631 6353ad76 2019-02-08 stsp
632 6353ad76 2019-02-08 stsp /*
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
638 6353ad76 2019-02-08 stsp */
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
646 6353ad76 2019-02-08 stsp {
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
653 6353ad76 2019-02-08 stsp
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
655 234035bc 2019-06-01 stsp
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
657 3524bbf9 2020-10-20 stsp if (err)
658 3524bbf9 2020-10-20 stsp return err;
659 af54ae4a 2019-02-19 stsp
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
662 3524bbf9 2020-10-20 stsp goto done;
663 3524bbf9 2020-10-20 stsp }
664 af54ae4a 2019-02-19 stsp
665 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
666 6353ad76 2019-02-08 stsp if (err)
667 6353ad76 2019-02-08 stsp goto done;
668 3b9f0f87 2020-07-23 stsp
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 24f136e0 2021-11-20 thomas if (err) {
672 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
673 24f136e0 2021-11-20 thomas goto done;
674 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 24f136e0 2021-11-20 thomas ondisk_path);
677 24f136e0 2021-11-20 thomas if (err)
678 24f136e0 2021-11-20 thomas goto done;
679 24f136e0 2021-11-20 thomas }
680 6353ad76 2019-02-08 stsp
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 1ee397ad 2019-07-12 stsp if (err)
684 1ee397ad 2019-07-12 stsp goto done;
685 6353ad76 2019-02-08 stsp
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
688 816dc654 2019-02-16 stsp goto done;
689 68c76935 2019-02-19 stsp }
690 68c76935 2019-02-19 stsp
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
694 db590691 2021-06-02 stsp goto done;
695 db590691 2021-06-02 stsp }
696 db590691 2021-06-02 stsp merged_fd = -1;
697 db590691 2021-06-02 stsp
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
701 db590691 2021-06-02 stsp f_merged);
702 68c76935 2019-02-19 stsp if (err)
703 68c76935 2019-02-19 stsp goto done;
704 816dc654 2019-02-16 stsp }
705 6353ad76 2019-02-08 stsp
706 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
708 70a0c8ec 2019-02-20 stsp goto done;
709 70a0c8ec 2019-02-20 stsp }
710 70a0c8ec 2019-02-20 stsp
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
714 6353ad76 2019-02-08 stsp goto done;
715 6353ad76 2019-02-08 stsp }
716 6353ad76 2019-02-08 stsp done:
717 fdcb7daf 2019-12-15 stsp if (err) {
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
720 3b9f0f87 2020-07-23 stsp }
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
729 af57b12a 2020-07-23 stsp }
730 af57b12a 2020-07-23 stsp
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
734 af57b12a 2020-07-23 stsp {
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
742 af57b12a 2020-07-23 stsp }
743 af57b12a 2020-07-23 stsp
744 af57b12a 2020-07-23 stsp /*
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
749 993e2a1b 2020-07-23 stsp *
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
755 11cc08c1 2020-07-23 stsp */
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
760 11cc08c1 2020-07-23 stsp {
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
764 11cc08c1 2020-07-23 stsp
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
766 11cc08c1 2020-07-23 stsp if (err)
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
768 11cc08c1 2020-07-23 stsp
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp goto done;
773 11cc08c1 2020-07-23 stsp }
774 11cc08c1 2020-07-23 stsp
775 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
776 11cc08c1 2020-07-23 stsp if (err)
777 3818e3c4 2020-11-01 naddy goto done;
778 3818e3c4 2020-11-01 naddy
779 a2c162eb 2022-10-30 thomas if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
781 11cc08c1 2020-07-23 stsp goto done;
782 3818e3c4 2020-11-01 naddy }
783 11cc08c1 2020-07-23 stsp
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
794 11cc08c1 2020-07-23 stsp goto done;
795 11cc08c1 2020-07-23 stsp }
796 11cc08c1 2020-07-23 stsp
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
799 11cc08c1 2020-07-23 stsp goto done;
800 11cc08c1 2020-07-23 stsp }
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
803 11cc08c1 2020-07-23 stsp goto done;
804 11cc08c1 2020-07-23 stsp }
805 11cc08c1 2020-07-23 stsp done:
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
812 11cc08c1 2020-07-23 stsp }
813 d219f183 2020-07-23 stsp
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
820 11cc08c1 2020-07-23 stsp
821 11cc08c1 2020-07-23 stsp /*
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
826 af57b12a 2020-07-23 stsp */
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
833 af57b12a 2020-07-23 stsp {
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
841 af57b12a 2020-07-23 stsp
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
850 af57b12a 2020-07-23 stsp goto done;
851 af57b12a 2020-07-23 stsp }
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
853 af57b12a 2020-07-23 stsp
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 526a746f 2020-07-23 stsp if (err)
857 526a746f 2020-07-23 stsp goto done;
858 526a746f 2020-07-23 stsp }
859 af57b12a 2020-07-23 stsp
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
864 11cc08c1 2020-07-23 stsp
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 11cc08c1 2020-07-23 stsp path);
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp path);
881 11cc08c1 2020-07-23 stsp } else {
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
886 11cc08c1 2020-07-23 stsp if (err)
887 11cc08c1 2020-07-23 stsp goto done;
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 11cc08c1 2020-07-23 stsp path);
890 11cc08c1 2020-07-23 stsp }
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
895 af57b12a 2020-07-23 stsp if (err)
896 af57b12a 2020-07-23 stsp goto done;
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 ea7786be 2020-07-23 stsp path);
904 ea7786be 2020-07-23 stsp } else {
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
908 ea7786be 2020-07-23 stsp if (err)
909 ea7786be 2020-07-23 stsp goto done;
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 ea7786be 2020-07-23 stsp path);
912 ea7786be 2020-07-23 stsp }
913 6353ad76 2019-02-08 stsp }
914 11cc08c1 2020-07-23 stsp
915 af57b12a 2020-07-23 stsp done:
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
918 eec2f5a9 2021-06-03 stsp }
919 eec2f5a9 2021-06-03 stsp
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
922 eec2f5a9 2021-06-03 stsp {
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
926 eec2f5a9 2021-06-03 stsp size_t n;
927 eec2f5a9 2021-06-03 stsp FILE *f;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
937 eec2f5a9 2021-06-03 stsp goto done;
938 eec2f5a9 2021-06-03 stsp }
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
942 eec2f5a9 2021-06-03 stsp goto done;
943 eec2f5a9 2021-06-03 stsp }
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
946 eec2f5a9 2021-06-03 stsp goto done;
947 eec2f5a9 2021-06-03 stsp }
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
950 eec2f5a9 2021-06-03 stsp goto done;
951 eec2f5a9 2021-06-03 stsp }
952 eec2f5a9 2021-06-03 stsp done:
953 eec2f5a9 2021-06-03 stsp if (err)
954 eec2f5a9 2021-06-03 stsp fclose(f);
955 eec2f5a9 2021-06-03 stsp else
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
958 14c901f1 2019-08-08 stsp }
959 14c901f1 2019-08-08 stsp
960 14c901f1 2019-08-08 stsp /*
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
964 14c901f1 2019-08-08 stsp */
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
972 14c901f1 2019-08-08 stsp {
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
978 14c901f1 2019-08-08 stsp
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
980 14c901f1 2019-08-08 stsp
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
982 ed6b5030 2020-10-20 stsp if (err)
983 ed6b5030 2020-10-20 stsp return err;
984 14c901f1 2019-08-08 stsp
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
990 67a66647 2021-05-31 stsp goto done;
991 67a66647 2021-05-31 stsp }
992 67a66647 2021-05-31 stsp
993 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_orig_path, &f_orig,
994 fc2a50f2 2022-11-01 thomas base_path, "");
995 67a66647 2021-05-31 stsp if (err)
996 67a66647 2021-05-31 stsp goto done;
997 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
998 67a66647 2021-05-31 stsp blob_orig);
999 67a66647 2021-05-31 stsp if (err)
1000 67a66647 2021-05-31 stsp goto done;
1001 67a66647 2021-05-31 stsp free(base_path);
1002 db590691 2021-06-02 stsp } else {
1003 db590691 2021-06-02 stsp /*
1004 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1005 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1006 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1007 db590691 2021-06-02 stsp */
1008 db590691 2021-06-02 stsp f_orig = got_opentemp();
1009 db590691 2021-06-02 stsp if (f_orig == NULL) {
1010 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1011 db590691 2021-06-02 stsp goto done;
1012 db590691 2021-06-02 stsp }
1013 67a66647 2021-05-31 stsp }
1014 67a66647 2021-05-31 stsp
1015 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1017 14c901f1 2019-08-08 stsp base_path = NULL;
1018 14c901f1 2019-08-08 stsp goto done;
1019 14c901f1 2019-08-08 stsp }
1020 14c901f1 2019-08-08 stsp
1021 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1022 14c901f1 2019-08-08 stsp if (err)
1023 14c901f1 2019-08-08 stsp goto done;
1024 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 14c901f1 2019-08-08 stsp blob_deriv);
1026 14c901f1 2019-08-08 stsp if (err)
1027 14c901f1 2019-08-08 stsp goto done;
1028 14c901f1 2019-08-08 stsp
1029 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1030 14c901f1 2019-08-08 stsp if (err)
1031 14c901f1 2019-08-08 stsp goto done;
1032 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1033 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1035 14c901f1 2019-08-08 stsp goto done;
1036 eec2f5a9 2021-06-03 stsp }
1037 eec2f5a9 2021-06-03 stsp
1038 b6b86fd1 2022-08-30 thomas /*
1039 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1040 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1041 eec2f5a9 2021-06-03 stsp */
1042 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1043 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 eec2f5a9 2021-06-03 stsp if (err)
1045 eec2f5a9 2021-06-03 stsp goto done;
1046 eec2f5a9 2021-06-03 stsp } else {
1047 eec2f5a9 2021-06-03 stsp int fd;
1048 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1050 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1051 eec2f5a9 2021-06-03 stsp goto done;
1052 eec2f5a9 2021-06-03 stsp }
1053 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1054 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1055 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1056 eec2f5a9 2021-06-03 stsp close(fd);
1057 eec2f5a9 2021-06-03 stsp goto done;
1058 eec2f5a9 2021-06-03 stsp }
1059 14c901f1 2019-08-08 stsp }
1060 14c901f1 2019-08-08 stsp
1061 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 14c901f1 2019-08-08 stsp done:
1065 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1067 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1069 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1071 14c901f1 2019-08-08 stsp free(base_path);
1072 67a66647 2021-05-31 stsp if (blob_orig_path) {
1073 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1074 67a66647 2021-05-31 stsp free(blob_orig_path);
1075 67a66647 2021-05-31 stsp }
1076 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1077 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1078 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1079 14c901f1 2019-08-08 stsp }
1080 6353ad76 2019-02-08 stsp free(id_str);
1081 818c7501 2019-07-11 stsp free(label_deriv);
1082 ed6b5030 2020-10-20 stsp free(parent);
1083 4a1ddfc2 2019-01-12 stsp return err;
1084 4a1ddfc2 2019-01-12 stsp }
1085 4a1ddfc2 2019-01-12 stsp
1086 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1087 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1090 13d9040b 2019-03-27 stsp {
1091 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1092 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1093 13d9040b 2019-03-27 stsp
1094 65b05cec 2020-07-23 stsp *new_iep = NULL;
1095 65b05cec 2020-07-23 stsp
1096 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1097 054041d0 2020-06-24 stsp if (err)
1098 054041d0 2020-06-24 stsp return err;
1099 054041d0 2020-06-24 stsp
1100 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1102 054041d0 2020-06-24 stsp if (err)
1103 054041d0 2020-06-24 stsp goto done;
1104 054041d0 2020-06-24 stsp
1105 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1106 054041d0 2020-06-24 stsp done:
1107 054041d0 2020-06-24 stsp if (err)
1108 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1109 65b05cec 2020-07-23 stsp else
1110 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1111 13d9040b 2019-03-27 stsp return err;
1112 1ebedb77 2019-10-19 stsp }
1113 1ebedb77 2019-10-19 stsp
1114 1ebedb77 2019-10-19 stsp static mode_t
1115 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1116 1ebedb77 2019-10-19 stsp {
1117 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1118 1ebedb77 2019-10-19 stsp
1119 1ebedb77 2019-10-19 stsp if (executable) {
1120 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1121 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1122 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1123 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1124 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1125 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1126 1ebedb77 2019-10-19 stsp }
1127 1ebedb77 2019-10-19 stsp
1128 a2c162eb 2022-10-30 thomas return st_mode;
1129 a2c162eb 2022-10-30 thomas }
1130 a2c162eb 2022-10-30 thomas
1131 a2c162eb 2022-10-30 thomas static mode_t
1132 a2c162eb 2022-10-30 thomas apply_umask(mode_t mode)
1133 a2c162eb 2022-10-30 thomas {
1134 a2c162eb 2022-10-30 thomas mode_t um;
1135 a2c162eb 2022-10-30 thomas
1136 a2c162eb 2022-10-30 thomas um = umask(000);
1137 a2c162eb 2022-10-30 thomas umask(um);
1138 a2c162eb 2022-10-30 thomas return mode & ~um;
1139 13d9040b 2019-03-27 stsp }
1140 13d9040b 2019-03-27 stsp
1141 8ba819a3 2020-07-23 stsp /* forward declaration */
1142 13d9040b 2019-03-27 stsp static const struct got_error *
1143 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1145 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1146 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1147 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1148 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1149 8ba819a3 2020-07-23 stsp
1150 5a1fbc73 2020-07-23 stsp /*
1151 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1152 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1153 5a1fbc73 2020-07-23 stsp */
1154 8ba819a3 2020-07-23 stsp static const struct got_error *
1155 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1157 5a1fbc73 2020-07-23 stsp {
1158 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1159 5a1fbc73 2020-07-23 stsp ssize_t elen;
1160 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1161 5a1fbc73 2020-07-23 stsp int fd;
1162 5a1fbc73 2020-07-23 stsp
1163 c6e8a826 2021-04-05 stsp *did_something = 0;
1164 c6e8a826 2021-04-05 stsp
1165 5a1fbc73 2020-07-23 stsp /*
1166 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1167 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1168 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1169 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1170 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1171 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1172 5a1fbc73 2020-07-23 stsp */
1173 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1175 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1176 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1177 5a1fbc73 2020-07-23 stsp
1178 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1179 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 5a1fbc73 2020-07-23 stsp if (elen == -1)
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1182 5a1fbc73 2020-07-23 stsp
1183 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1184 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1185 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1186 5a1fbc73 2020-07-23 stsp }
1187 5a1fbc73 2020-07-23 stsp
1188 c6e8a826 2021-04-05 stsp *did_something = 1;
1189 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1190 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1191 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1192 5a1fbc73 2020-07-23 stsp return err;
1193 3c1ec782 2020-07-23 stsp }
1194 3c1ec782 2020-07-23 stsp
1195 3c1ec782 2020-07-23 stsp static const struct got_error *
1196 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1198 3c1ec782 2020-07-23 stsp {
1199 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1200 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1201 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1202 3c1ec782 2020-07-23 stsp
1203 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1204 3c1ec782 2020-07-23 stsp
1205 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1206 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1207 3c1ec782 2020-07-23 stsp return NULL;
1208 3c1ec782 2020-07-23 stsp }
1209 3c1ec782 2020-07-23 stsp
1210 3c1ec782 2020-07-23 stsp /*
1211 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1212 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1213 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1214 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1215 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1216 3c1ec782 2020-07-23 stsp */
1217 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1218 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1219 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1220 ce031e9e 2020-10-20 stsp if (err)
1221 ce031e9e 2020-10-20 stsp return err;
1222 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 ce031e9e 2020-10-20 stsp free(parent);
1224 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1225 ce031e9e 2020-10-20 stsp }
1226 ce031e9e 2020-10-20 stsp free(parent);
1227 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1228 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1229 3c1ec782 2020-07-23 stsp free(abspath);
1230 3c1ec782 2020-07-23 stsp return err;
1231 3c1ec782 2020-07-23 stsp }
1232 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1233 3c1ec782 2020-07-23 stsp free(abspath);
1234 3c1ec782 2020-07-23 stsp if (err)
1235 3c1ec782 2020-07-23 stsp return err;
1236 3c1ec782 2020-07-23 stsp } else {
1237 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1238 3c1ec782 2020-07-23 stsp if (err)
1239 3c1ec782 2020-07-23 stsp return err;
1240 3c1ec782 2020-07-23 stsp }
1241 3c1ec782 2020-07-23 stsp
1242 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1243 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1245 3c1ec782 2020-07-23 stsp return NULL;
1246 3c1ec782 2020-07-23 stsp }
1247 3c1ec782 2020-07-23 stsp
1248 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1249 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1251 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1252 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1254 3c1ec782 2020-07-23 stsp
1255 3c1ec782 2020-07-23 stsp free(path_got);
1256 3c1ec782 2020-07-23 stsp return NULL;
1257 5a1fbc73 2020-07-23 stsp }
1258 5a1fbc73 2020-07-23 stsp
1259 5a1fbc73 2020-07-23 stsp static const struct got_error *
1260 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1263 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1264 ace90326 2021-09-27 thomas struct got_repository *repo,
1265 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1266 9d31a1d8 2018-03-11 stsp {
1267 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1268 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1269 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1270 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1272 8ba819a3 2020-07-23 stsp
1273 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1274 2e1fa222 2020-07-23 stsp
1275 9e39ca77 2022-07-21 thomas /*
1276 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1277 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1278 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1279 8ba819a3 2020-07-23 stsp * in the blob object.
1280 8ba819a3 2020-07-23 stsp */
1281 8ba819a3 2020-07-23 stsp do {
1282 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1283 3521b466 2022-07-21 thomas if (err)
1284 3521b466 2022-07-21 thomas return err;
1285 3521b466 2022-07-21 thomas
1286 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1287 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1288 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1289 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1290 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1291 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1293 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1294 3b9f0f87 2020-07-23 stsp progress_arg);
1295 8ba819a3 2020-07-23 stsp }
1296 8ba819a3 2020-07-23 stsp if (len > 0) {
1297 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1298 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1299 8ba819a3 2020-07-23 stsp len - hdrlen);
1300 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1301 8ba819a3 2020-07-23 stsp hdrlen = 0;
1302 8ba819a3 2020-07-23 stsp }
1303 8ba819a3 2020-07-23 stsp } while (len != 0);
1304 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1305 8ba819a3 2020-07-23 stsp
1306 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1308 3c1ec782 2020-07-23 stsp if (err)
1309 3c1ec782 2020-07-23 stsp return err;
1310 8ba819a3 2020-07-23 stsp
1311 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1312 906c123b 2020-07-23 stsp /* install as a regular file */
1313 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1314 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1315 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1317 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1318 9e39ca77 2022-07-21 thomas return err;
1319 906c123b 2020-07-23 stsp }
1320 906c123b 2020-07-23 stsp
1321 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1322 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1323 c6e8a826 2021-04-05 stsp int symlink_replaced;
1324 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1325 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1326 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1327 9e39ca77 2022-07-21 thomas return err;
1328 c90c8ce3 2020-07-23 stsp }
1329 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1330 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1331 5a1fbc73 2020-07-23 stsp if (err)
1332 9e39ca77 2022-07-21 thomas return err;
1333 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1334 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1335 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1336 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1337 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1338 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1339 c6e8a826 2021-04-05 stsp } else {
1340 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1341 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1342 c6e8a826 2021-04-05 stsp }
1343 f35fa46a 2020-07-23 stsp }
1344 9e39ca77 2022-07-21 thomas return err; /* Nothing else to do. */
1345 f35fa46a 2020-07-23 stsp }
1346 f35fa46a 2020-07-23 stsp
1347 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1348 f4994adc 2020-10-20 stsp char *parent;
1349 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1350 f4994adc 2020-10-20 stsp if (err)
1351 9e39ca77 2022-07-21 thomas return err;
1352 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1353 f4994adc 2020-10-20 stsp free(parent);
1354 f35fa46a 2020-07-23 stsp if (err)
1355 9e39ca77 2022-07-21 thomas return err;
1356 f35fa46a 2020-07-23 stsp /*
1357 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1358 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1359 f35fa46a 2020-07-23 stsp */
1360 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1361 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1362 9e39ca77 2022-07-21 thomas return err;
1363 f35fa46a 2020-07-23 stsp }
1364 f35fa46a 2020-07-23 stsp }
1365 f35fa46a 2020-07-23 stsp
1366 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1367 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1368 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1369 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1370 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1371 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1372 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1374 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1375 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1376 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1377 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1378 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1379 8ba819a3 2020-07-23 stsp } else {
1380 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1381 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1382 8ba819a3 2020-07-23 stsp }
1383 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1384 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1386 8ba819a3 2020-07-23 stsp return err;
1387 8ba819a3 2020-07-23 stsp }
1388 8ba819a3 2020-07-23 stsp
1389 8ba819a3 2020-07-23 stsp static const struct got_error *
1390 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1392 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1393 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1394 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1395 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1396 8ba819a3 2020-07-23 stsp {
1397 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1398 507dc3bb 2018-12-29 stsp int fd = -1;
1399 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1400 507dc3bb 2018-12-29 stsp int update = 0;
1401 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1402 a2c162eb 2022-10-30 thomas mode_t mode;
1403 8ba819a3 2020-07-23 stsp
1404 a2c162eb 2022-10-30 thomas mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 a2c162eb 2022-10-30 thomas O_CLOEXEC, mode);
1407 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1408 9f323212 2023-03-10 thomas if (errno == ENOENT || errno == ENOTDIR) {
1409 f5375317 2020-10-20 stsp char *parent;
1410 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1411 f5375317 2020-10-20 stsp if (err)
1412 f5375317 2020-10-20 stsp return err;
1413 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1414 9f323212 2023-03-10 thomas if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1415 9f323212 2023-03-10 thomas err = got_error_path(path, err->code);
1416 f5375317 2020-10-20 stsp free(parent);
1417 21908da4 2019-01-13 stsp if (err)
1418 21908da4 2019-01-13 stsp return err;
1419 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1420 06340621 2021-12-31 thomas O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 a2c162eb 2022-10-30 thomas mode);
1422 21908da4 2019-01-13 stsp if (fd == -1)
1423 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1424 230a42bd 2019-05-11 jcs ondisk_path);
1425 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1426 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1427 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1428 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1429 3b9f0f87 2020-07-23 stsp goto done;
1430 3b9f0f87 2020-07-23 stsp }
1431 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1434 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1435 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1436 507dc3bb 2018-12-29 stsp goto done;
1437 d70b8e30 2018-12-27 stsp } else {
1438 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1439 fc2a50f2 2022-11-01 thomas ondisk_path, "");
1440 507dc3bb 2018-12-29 stsp if (err)
1441 507dc3bb 2018-12-29 stsp goto done;
1442 507dc3bb 2018-12-29 stsp update = 1;
1443 a2c162eb 2022-10-30 thomas
1444 a2c162eb 2022-10-30 thomas if (fchmod(fd, apply_umask(mode)) == -1) {
1445 a2c162eb 2022-10-30 thomas err = got_error_from_errno2("fchmod",
1446 a2c162eb 2022-10-30 thomas tmppath);
1447 a2c162eb 2022-10-30 thomas goto done;
1448 a2c162eb 2022-10-30 thomas }
1449 9d31a1d8 2018-03-11 stsp }
1450 507dc3bb 2018-12-29 stsp } else
1451 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1452 3818e3c4 2020-11-01 naddy }
1453 3818e3c4 2020-11-01 naddy
1454 bd6aa359 2020-07-23 stsp if (progress_cb) {
1455 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1456 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 bd6aa359 2020-07-23 stsp path);
1458 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1459 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 bd6aa359 2020-07-23 stsp path);
1461 bd6aa359 2020-07-23 stsp else
1462 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1463 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 bd6aa359 2020-07-23 stsp if (err)
1465 bd6aa359 2020-07-23 stsp goto done;
1466 bd6aa359 2020-07-23 stsp }
1467 d7b62c98 2018-12-27 stsp
1468 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1469 9d31a1d8 2018-03-11 stsp do {
1470 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1472 9d31a1d8 2018-03-11 stsp if (err)
1473 9d31a1d8 2018-03-11 stsp break;
1474 9d31a1d8 2018-03-11 stsp if (len > 0) {
1475 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1476 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1478 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1479 b87c6f83 2018-12-24 stsp goto done;
1480 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1481 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1482 b87c6f83 2018-12-24 stsp goto done;
1483 9d31a1d8 2018-03-11 stsp }
1484 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1485 9d31a1d8 2018-03-11 stsp }
1486 9d31a1d8 2018-03-11 stsp } while (len != 0);
1487 9d31a1d8 2018-03-11 stsp
1488 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1490 816dc654 2019-02-16 stsp goto done;
1491 816dc654 2019-02-16 stsp }
1492 9d31a1d8 2018-03-11 stsp
1493 507dc3bb 2018-12-29 stsp if (update) {
1494 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1496 f6d8c0ac 2020-11-09 stsp goto done;
1497 f6d8c0ac 2020-11-09 stsp }
1498 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1499 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1500 230a42bd 2019-05-11 jcs ondisk_path);
1501 68ed9ba5 2019-02-10 stsp goto done;
1502 68ed9ba5 2019-02-10 stsp }
1503 63df146d 2020-11-09 stsp free(tmppath);
1504 63df146d 2020-11-09 stsp tmppath = NULL;
1505 507dc3bb 2018-12-29 stsp }
1506 507dc3bb 2018-12-29 stsp
1507 9d31a1d8 2018-03-11 stsp done:
1508 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1510 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1512 507dc3bb 2018-12-29 stsp free(tmppath);
1513 6353ad76 2019-02-08 stsp return err;
1514 6353ad76 2019-02-08 stsp }
1515 6353ad76 2019-02-08 stsp
1516 be94c032 2023-02-20 thomas /*
1517 be94c032 2023-02-20 thomas * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 be94c032 2023-02-20 thomas * conflict marker is found in newly added lines only.
1519 be94c032 2023-02-20 thomas */
1520 6353ad76 2019-02-08 stsp static const struct got_error *
1521 be94c032 2023-02-20 thomas get_modified_file_content_status(unsigned char *status,
1522 be94c032 2023-02-20 thomas struct got_blob_object *blob, const char *path, struct stat *sb,
1523 be94c032 2023-02-20 thomas FILE *ondisk_file)
1524 7154f6ce 2019-03-27 stsp {
1525 8de9d8ad 2023-02-20 thomas const struct got_error *err, *free_err;
1526 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1529 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1530 7154f6ce 2019-03-27 stsp };
1531 8de9d8ad 2023-02-20 thomas FILE *f1 = NULL;
1532 8de9d8ad 2023-02-20 thomas struct got_diffreg_result *diffreg_result = NULL;
1533 8de9d8ad 2023-02-20 thomas struct diff_result *r;
1534 8de9d8ad 2023-02-20 thomas int nchunks_parsed, n, i = 0, ln = 0;
1535 9bdd68dd 2020-01-02 naddy char *line = NULL;
1536 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1537 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1538 7154f6ce 2019-03-27 stsp
1539 8de9d8ad 2023-02-20 thomas if (*status != GOT_STATUS_MODIFY)
1540 8de9d8ad 2023-02-20 thomas return NULL;
1541 be94c032 2023-02-20 thomas
1542 8de9d8ad 2023-02-20 thomas f1 = got_opentemp();
1543 8de9d8ad 2023-02-20 thomas if (f1 == NULL)
1544 8de9d8ad 2023-02-20 thomas return got_error_from_errno("got_opentemp");
1545 be94c032 2023-02-20 thomas
1546 8de9d8ad 2023-02-20 thomas if (blob) {
1547 8de9d8ad 2023-02-20 thomas got_object_blob_rewind(blob);
1548 8de9d8ad 2023-02-20 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1549 be94c032 2023-02-20 thomas if (err)
1550 be94c032 2023-02-20 thomas goto done;
1551 8de9d8ad 2023-02-20 thomas }
1552 be94c032 2023-02-20 thomas
1553 8de9d8ad 2023-02-20 thomas err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 8de9d8ad 2023-02-20 thomas 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1555 8de9d8ad 2023-02-20 thomas if (err)
1556 8de9d8ad 2023-02-20 thomas goto done;
1557 8de9d8ad 2023-02-20 thomas
1558 8de9d8ad 2023-02-20 thomas r = diffreg_result->result;
1559 8de9d8ad 2023-02-20 thomas
1560 8de9d8ad 2023-02-20 thomas for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 8de9d8ad 2023-02-20 thomas struct diff_chunk *c;
1562 8de9d8ad 2023-02-20 thomas struct diff_chunk_context cc = {};
1563 450d9f6b 2023-02-20 thomas off_t pos;
1564 8de9d8ad 2023-02-20 thomas
1565 8de9d8ad 2023-02-20 thomas /*
1566 8de9d8ad 2023-02-20 thomas * We can optimise a little by advancing straight
1567 8de9d8ad 2023-02-20 thomas * to the next chunk if this one has no added lines.
1568 8de9d8ad 2023-02-20 thomas */
1569 8de9d8ad 2023-02-20 thomas c = diff_chunk_get(r, n);
1570 8de9d8ad 2023-02-20 thomas
1571 47c7ee21 2023-02-20 thomas if (diff_chunk_type(c) != CHUNK_PLUS) {
1572 8de9d8ad 2023-02-20 thomas nchunks_parsed = 1;
1573 450d9f6b 2023-02-20 thomas continue; /* removed or unchanged lines */
1574 7154f6ce 2019-03-27 stsp }
1575 7154f6ce 2019-03-27 stsp
1576 450d9f6b 2023-02-20 thomas pos = diff_chunk_get_right_start_pos(c);
1577 450d9f6b 2023-02-20 thomas if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1578 450d9f6b 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1579 450d9f6b 2023-02-20 thomas goto done;
1580 7154f6ce 2019-03-27 stsp }
1581 8de9d8ad 2023-02-20 thomas
1582 450d9f6b 2023-02-20 thomas diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1583 450d9f6b 2023-02-20 thomas ln = cc.right.start;
1584 450d9f6b 2023-02-20 thomas
1585 8de9d8ad 2023-02-20 thomas while (ln < cc.right.end) {
1586 8de9d8ad 2023-02-20 thomas linelen = getline(&line, &linesize, ondisk_file);
1587 8de9d8ad 2023-02-20 thomas if (linelen == -1) {
1588 8de9d8ad 2023-02-20 thomas if (feof(ondisk_file))
1589 8de9d8ad 2023-02-20 thomas break;
1590 8de9d8ad 2023-02-20 thomas err = got_ferror(ondisk_file, GOT_ERR_IO);
1591 8de9d8ad 2023-02-20 thomas break;
1592 8de9d8ad 2023-02-20 thomas }
1593 8de9d8ad 2023-02-20 thomas
1594 8de9d8ad 2023-02-20 thomas if (line && strncmp(line, markers[i],
1595 8de9d8ad 2023-02-20 thomas strlen(markers[i])) == 0) {
1596 8de9d8ad 2023-02-20 thomas if (strcmp(markers[i],
1597 8de9d8ad 2023-02-20 thomas GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1598 8de9d8ad 2023-02-20 thomas *status = GOT_STATUS_CONFLICT;
1599 8de9d8ad 2023-02-20 thomas goto done;
1600 8de9d8ad 2023-02-20 thomas } else
1601 8de9d8ad 2023-02-20 thomas i++;
1602 8de9d8ad 2023-02-20 thomas }
1603 8de9d8ad 2023-02-20 thomas ++ln;
1604 8de9d8ad 2023-02-20 thomas }
1605 7154f6ce 2019-03-27 stsp }
1606 be94c032 2023-02-20 thomas
1607 be94c032 2023-02-20 thomas done:
1608 9bdd68dd 2020-01-02 naddy free(line);
1609 be94c032 2023-02-20 thomas if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1610 be94c032 2023-02-20 thomas err = got_error_from_errno("fclose");
1611 8de9d8ad 2023-02-20 thomas free_err = got_diffreg_result_free(diffreg_result);
1612 8de9d8ad 2023-02-20 thomas if (err == NULL)
1613 8de9d8ad 2023-02-20 thomas err = free_err;
1614 7154f6ce 2019-03-27 stsp
1615 7154f6ce 2019-03-27 stsp return err;
1616 e2b1e152 2019-07-13 stsp }
1617 e2b1e152 2019-07-13 stsp
1618 e2b1e152 2019-07-13 stsp static int
1619 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1620 1ebedb77 2019-10-19 stsp {
1621 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1622 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1623 1ebedb77 2019-10-19 stsp }
1624 1ebedb77 2019-10-19 stsp
1625 1ebedb77 2019-10-19 stsp static int
1626 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1627 e2b1e152 2019-07-13 stsp {
1628 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1629 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1630 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1631 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1632 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1633 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1634 c363b2c1 2019-08-03 stsp }
1635 c363b2c1 2019-08-03 stsp
1636 c363b2c1 2019-08-03 stsp static unsigned char
1637 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1638 c363b2c1 2019-08-03 stsp {
1639 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1640 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1641 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1642 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1643 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1644 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1645 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1646 c363b2c1 2019-08-03 stsp default:
1647 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1648 a919d5c4 2020-07-23 stsp }
1649 a919d5c4 2020-07-23 stsp }
1650 a919d5c4 2020-07-23 stsp
1651 a919d5c4 2020-07-23 stsp static const struct got_error *
1652 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1653 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1654 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1655 a919d5c4 2020-07-23 stsp {
1656 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1657 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1658 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1659 a919d5c4 2020-07-23 stsp ssize_t elen;
1660 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1661 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1662 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1663 a919d5c4 2020-07-23 stsp
1664 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1665 a919d5c4 2020-07-23 stsp
1666 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1667 a919d5c4 2020-07-23 stsp do {
1668 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1669 a919d5c4 2020-07-23 stsp if (err)
1670 a919d5c4 2020-07-23 stsp return err;
1671 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1672 a919d5c4 2020-07-23 stsp /*
1673 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1674 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1675 a919d5c4 2020-07-23 stsp */
1676 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1677 a919d5c4 2020-07-23 stsp }
1678 a919d5c4 2020-07-23 stsp if (len > 0) {
1679 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1680 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1681 a919d5c4 2020-07-23 stsp len - hdrlen);
1682 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1683 a919d5c4 2020-07-23 stsp hdrlen = 0;
1684 a919d5c4 2020-07-23 stsp }
1685 a919d5c4 2020-07-23 stsp } while (len != 0);
1686 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1687 a919d5c4 2020-07-23 stsp
1688 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1689 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1690 a919d5c4 2020-07-23 stsp if (elen == -1)
1691 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1692 a919d5c4 2020-07-23 stsp } else {
1693 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1694 a919d5c4 2020-07-23 stsp if (elen == -1)
1695 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1696 c363b2c1 2019-08-03 stsp }
1697 a919d5c4 2020-07-23 stsp
1698 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1699 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1700 a919d5c4 2020-07-23 stsp
1701 a919d5c4 2020-07-23 stsp return NULL;
1702 7154f6ce 2019-03-27 stsp }
1703 7154f6ce 2019-03-27 stsp
1704 7154f6ce 2019-03-27 stsp static const struct got_error *
1705 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1706 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1707 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1708 6353ad76 2019-02-08 stsp {
1709 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1710 6353ad76 2019-02-08 stsp struct got_object_id id;
1711 6353ad76 2019-02-08 stsp size_t hdrlen;
1712 f4ae6ddb 2022-07-01 thomas int fd = -1, fd1 = -1;
1713 6353ad76 2019-02-08 stsp FILE *f = NULL;
1714 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1715 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1716 6353ad76 2019-02-08 stsp size_t flen, blen;
1717 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
1718 6353ad76 2019-02-08 stsp
1719 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
1720 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1721 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1722 6353ad76 2019-02-08 stsp
1723 7f91a133 2019-12-13 stsp /*
1724 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1725 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1726 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1727 7f91a133 2019-12-13 stsp */
1728 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1729 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1731 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1732 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1733 882ef1b9 2019-12-13 stsp else
1734 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1735 882ef1b9 2019-12-13 stsp goto done;
1736 882ef1b9 2019-12-13 stsp }
1737 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1738 3d35a492 2019-12-13 stsp goto done;
1739 3d35a492 2019-12-13 stsp }
1740 7f91a133 2019-12-13 stsp } else {
1741 06340621 2021-12-31 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1743 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1744 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1745 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1746 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1747 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1748 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1749 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1750 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1751 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1752 3d35a492 2019-12-13 stsp else
1753 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1754 3d35a492 2019-12-13 stsp goto done;
1755 3d35a492 2019-12-13 stsp }
1756 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1757 1338848f 2019-12-13 stsp goto done;
1758 a378724f 2019-02-10 stsp }
1759 a378724f 2019-02-10 stsp }
1760 6353ad76 2019-02-08 stsp
1761 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1762 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1763 1338848f 2019-12-13 stsp goto done;
1764 3f148bc6 2019-07-27 stsp }
1765 efdd40df 2019-07-27 stsp
1766 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1767 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1768 1338848f 2019-12-13 stsp goto done;
1769 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1770 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1771 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1772 1338848f 2019-12-13 stsp goto done;
1773 d00136be 2019-03-26 stsp }
1774 b8f41171 2019-02-10 stsp
1775 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1776 f179e66d 2020-07-23 stsp goto done;
1777 f179e66d 2020-07-23 stsp
1778 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1779 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1780 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1781 1338848f 2019-12-13 stsp goto done;
1782 f179e66d 2020-07-23 stsp }
1783 6353ad76 2019-02-08 stsp
1784 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1785 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1786 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
1787 c363b2c1 2019-08-03 stsp else
1788 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
1789 c363b2c1 2019-08-03 stsp
1790 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
1791 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
1792 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
1793 f4ae6ddb 2022-07-01 thomas goto done;
1794 f4ae6ddb 2022-07-01 thomas }
1795 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1796 6353ad76 2019-02-08 stsp if (err)
1797 1338848f 2019-12-13 stsp goto done;
1798 6353ad76 2019-02-08 stsp
1799 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1800 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1801 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1802 a919d5c4 2020-07-23 stsp goto done;
1803 a919d5c4 2020-07-23 stsp }
1804 a919d5c4 2020-07-23 stsp
1805 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1806 fc63f50d 2021-12-31 thomas fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1807 ab0d4361 2019-12-13 stsp if (fd == -1) {
1808 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1809 ab0d4361 2019-12-13 stsp goto done;
1810 ab0d4361 2019-12-13 stsp }
1811 3d35a492 2019-12-13 stsp }
1812 3d35a492 2019-12-13 stsp
1813 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1814 6353ad76 2019-02-08 stsp if (f == NULL) {
1815 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1816 6353ad76 2019-02-08 stsp goto done;
1817 6353ad76 2019-02-08 stsp }
1818 1338848f 2019-12-13 stsp fd = -1;
1819 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1820 656b1f76 2019-05-11 jcs for (;;) {
1821 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1822 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1823 6353ad76 2019-02-08 stsp if (err)
1824 7154f6ce 2019-03-27 stsp goto done;
1825 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1826 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1827 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1828 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1829 7154f6ce 2019-03-27 stsp goto done;
1830 80c5c120 2019-02-19 stsp }
1831 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1832 6353ad76 2019-02-08 stsp if (flen != 0)
1833 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1834 6353ad76 2019-02-08 stsp break;
1835 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1836 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1837 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1838 6353ad76 2019-02-08 stsp break;
1839 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1840 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1841 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1842 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1843 6353ad76 2019-02-08 stsp break;
1844 6353ad76 2019-02-08 stsp }
1845 6353ad76 2019-02-08 stsp } else {
1846 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1847 6353ad76 2019-02-08 stsp break;
1848 6353ad76 2019-02-08 stsp }
1849 6353ad76 2019-02-08 stsp hdrlen = 0;
1850 6353ad76 2019-02-08 stsp }
1851 7154f6ce 2019-03-27 stsp
1852 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1853 7154f6ce 2019-03-27 stsp rewind(f);
1854 be94c032 2023-02-20 thomas err = get_modified_file_content_status(status, blob, ie->path,
1855 be94c032 2023-02-20 thomas sb, f);
1856 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1857 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1858 6353ad76 2019-02-08 stsp done:
1859 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1860 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
1861 6353ad76 2019-02-08 stsp if (blob)
1862 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1863 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1864 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1865 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1866 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1867 9d31a1d8 2018-03-11 stsp return err;
1868 e2b1e152 2019-07-13 stsp }
1869 e2b1e152 2019-07-13 stsp
1870 e2b1e152 2019-07-13 stsp /*
1871 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1872 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1873 e2b1e152 2019-07-13 stsp */
1874 e2b1e152 2019-07-13 stsp static const struct got_error *
1875 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1877 e2b1e152 2019-07-13 stsp {
1878 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1880 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1881 e2b1e152 2019-07-13 stsp
1882 e2b1e152 2019-07-13 stsp return NULL;
1883 9d31a1d8 2018-03-11 stsp }
1884 9d31a1d8 2018-03-11 stsp
1885 9f323212 2023-03-10 thomas static const struct got_error *remove_ondisk_file(const char *, const char *);
1886 9f323212 2023-03-10 thomas
1887 9d31a1d8 2018-03-11 stsp static const struct got_error *
1888 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1889 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1890 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1891 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1892 0584f854 2019-04-06 stsp void *progress_arg)
1893 9d31a1d8 2018-03-11 stsp {
1894 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1895 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1896 f4ae6ddb 2022-07-01 thomas char *ondisk_path = NULL;
1897 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1898 b8f41171 2019-02-10 stsp struct stat sb;
1899 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
1900 9d31a1d8 2018-03-11 stsp
1901 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1902 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1903 6353ad76 2019-02-08 stsp
1904 abb4604f 2019-07-27 stsp if (ie) {
1905 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1906 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1907 a76c42e6 2019-08-03 stsp goto done;
1908 a76c42e6 2019-08-03 stsp }
1909 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1910 7f91a133 2019-12-13 stsp repo);
1911 abb4604f 2019-07-27 stsp if (err)
1912 abb4604f 2019-07-27 stsp goto done;
1913 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1914 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1915 c90c8ce3 2020-07-23 stsp } else {
1916 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1917 9f323212 2023-03-10 thomas if (errno != ENOENT && errno != ENOTDIR) {
1918 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1919 e6f4ba31 2021-09-24 thomas ondisk_path);
1920 e6f4ba31 2021-09-24 thomas goto done;
1921 e6f4ba31 2021-09-24 thomas }
1922 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1923 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1924 e6f4ba31 2021-09-24 thomas } else {
1925 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1926 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1927 e6f4ba31 2021-09-24 thomas else
1928 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1929 e6f4ba31 2021-09-24 thomas }
1930 c90c8ce3 2020-07-23 stsp }
1931 6353ad76 2019-02-08 stsp
1932 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1933 2c41dce7 2021-06-27 stsp if (ie)
1934 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1935 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1936 b8f41171 2019-02-10 stsp goto done;
1937 b8f41171 2019-02-10 stsp }
1938 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1939 a769b60b 2021-06-27 stsp if (ie)
1940 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1941 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1942 5036ab18 2020-04-18 stsp path);
1943 5036ab18 2020-04-18 stsp goto done;
1944 9f323212 2023-03-10 thomas }
1945 9f323212 2023-03-10 thomas
1946 9f323212 2023-03-10 thomas if (S_ISDIR(te->mode)) { /* file changing into a directory */
1947 9f323212 2023-03-10 thomas if (status == GOT_STATUS_UNVERSIONED) {
1948 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, status, path);
1949 9f323212 2023-03-10 thomas } else if (status != GOT_STATUS_NO_CHANGE &&
1950 9f323212 2023-03-10 thomas status != GOT_STATUS_DELETE &&
1951 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1952 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1953 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg,
1954 9f323212 2023-03-10 thomas GOT_STATUS_CANNOT_DELETE, path);
1955 9f323212 2023-03-10 thomas } else if (ie) {
1956 9f323212 2023-03-10 thomas if (status != GOT_STATUS_DELETE &&
1957 9f323212 2023-03-10 thomas status != GOT_STATUS_NONEXISTENT &&
1958 9f323212 2023-03-10 thomas status != GOT_STATUS_MISSING) {
1959 9f323212 2023-03-10 thomas err = remove_ondisk_file(worktree->root_path,
1960 9f323212 2023-03-10 thomas ie->path);
1961 9f323212 2023-03-10 thomas if (err && !(err->code == GOT_ERR_ERRNO &&
1962 9f323212 2023-03-10 thomas errno == ENOENT))
1963 9f323212 2023-03-10 thomas goto done;
1964 9f323212 2023-03-10 thomas }
1965 9f323212 2023-03-10 thomas got_fileindex_entry_remove(fileindex, ie);
1966 9f323212 2023-03-10 thomas err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1967 9f323212 2023-03-10 thomas ie->path);
1968 9f323212 2023-03-10 thomas }
1969 9f323212 2023-03-10 thomas goto done; /* nothing else to do */
1970 5036ab18 2020-04-18 stsp }
1971 b8f41171 2019-02-10 stsp
1972 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1973 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1974 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1975 c6e8a826 2021-04-05 stsp /*
1976 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1977 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1978 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1979 c6e8a826 2021-04-05 stsp * updating contents of this file.
1980 c6e8a826 2021-04-05 stsp */
1981 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1982 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1983 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1984 c6e8a826 2021-04-05 stsp /* Same commit. */
1985 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1986 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1987 e2b1e152 2019-07-13 stsp if (err)
1988 e2b1e152 2019-07-13 stsp goto done;
1989 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1990 b8f41171 2019-02-10 stsp path);
1991 6353ad76 2019-02-08 stsp goto done;
1992 a378724f 2019-02-10 stsp }
1993 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1994 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1995 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1996 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1997 b36a9774 2023-04-14 thomas if (got_fileindex_entry_has_commit(ie)) {
1998 b36a9774 2023-04-14 thomas /* Update the base commit ID of this file. */
1999 b36a9774 2023-04-14 thomas memcpy(ie->commit_sha1,
2000 b36a9774 2023-04-14 thomas worktree->base_commit_id->sha1,
2001 b36a9774 2023-04-14 thomas sizeof(ie->commit_sha1));
2002 b36a9774 2023-04-14 thomas }
2003 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2004 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2005 0f58026f 2021-04-05 stsp if (err)
2006 0f58026f 2021-04-05 stsp goto done;
2007 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2008 0f58026f 2021-04-05 stsp path);
2009 b8f41171 2019-02-10 stsp goto done;
2010 e2b1e152 2019-07-13 stsp }
2011 9d31a1d8 2018-03-11 stsp }
2012 9d31a1d8 2018-03-11 stsp
2013 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
2014 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
2015 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2016 f4ae6ddb 2022-07-01 thomas goto done;
2017 f4ae6ddb 2022-07-01 thomas }
2018 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2019 8da9e5f4 2019-01-12 stsp if (err)
2020 6353ad76 2019-02-08 stsp goto done;
2021 512f0d0e 2019-01-02 stsp
2022 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2023 234035bc 2019-06-01 stsp int update_timestamps;
2024 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2025 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2026 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2027 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
2028 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
2029 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
2030 f4ae6ddb 2022-07-01 thomas goto done;
2031 f4ae6ddb 2022-07-01 thomas }
2032 234035bc 2019-06-01 stsp struct got_object_id id2;
2033 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id2, ie);
2034 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2035 f4ae6ddb 2022-07-01 thomas fd2);
2036 234035bc 2019-06-01 stsp if (err)
2037 f69721c3 2019-10-21 stsp goto done;
2038 f69721c3 2019-10-21 stsp }
2039 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2040 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2041 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2042 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2043 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2044 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2045 f69721c3 2019-10-21 stsp goto done;
2046 f69721c3 2019-10-21 stsp }
2047 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2048 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2049 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2050 234035bc 2019-06-01 stsp goto done;
2051 f69721c3 2019-10-21 stsp }
2052 234035bc 2019-06-01 stsp }
2053 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2054 36bf999c 2020-07-23 stsp char *link_target;
2055 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2056 36bf999c 2020-07-23 stsp if (err)
2057 36bf999c 2020-07-23 stsp goto done;
2058 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2059 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2060 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2061 36bf999c 2020-07-23 stsp free(link_target);
2062 993e2a1b 2020-07-23 stsp } else {
2063 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2064 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2065 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2066 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2067 993e2a1b 2020-07-23 stsp }
2068 f69721c3 2019-10-21 stsp free(label_orig);
2069 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2070 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2071 f4ae6ddb 2022-07-01 thomas goto done;
2072 f4ae6ddb 2022-07-01 thomas }
2073 234035bc 2019-06-01 stsp if (blob2)
2074 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2075 909d120e 2019-09-22 stsp if (err)
2076 909d120e 2019-09-22 stsp goto done;
2077 234035bc 2019-06-01 stsp /*
2078 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2079 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2080 234035bc 2019-06-01 stsp * unmodified files again.
2081 234035bc 2019-06-01 stsp */
2082 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2083 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2084 234035bc 2019-06-01 stsp update_timestamps);
2085 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2086 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2087 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2088 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2089 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2090 1ee397ad 2019-07-12 stsp if (err)
2091 1ee397ad 2019-07-12 stsp goto done;
2092 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2093 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2094 13d9040b 2019-03-27 stsp if (err)
2095 13d9040b 2019-03-27 stsp goto done;
2096 13d9040b 2019-03-27 stsp } else {
2097 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2098 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2099 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2100 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2101 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2102 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
2103 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
2104 2e1fa222 2020-07-23 stsp } else {
2105 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2106 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2107 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2108 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2109 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2110 2e1fa222 2020-07-23 stsp }
2111 13d9040b 2019-03-27 stsp if (err)
2112 13d9040b 2019-03-27 stsp goto done;
2113 65b05cec 2020-07-23 stsp
2114 054041d0 2020-06-24 stsp if (ie) {
2115 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2116 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2117 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2118 054041d0 2020-06-24 stsp } else {
2119 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2120 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2121 054041d0 2020-06-24 stsp &blob->id);
2122 054041d0 2020-06-24 stsp }
2123 13d9040b 2019-03-27 stsp if (err)
2124 13d9040b 2019-03-27 stsp goto done;
2125 65b05cec 2020-07-23 stsp
2126 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2127 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2128 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2129 2e1fa222 2020-07-23 stsp }
2130 13d9040b 2019-03-27 stsp }
2131 f4ae6ddb 2022-07-01 thomas
2132 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2133 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
2134 f4ae6ddb 2022-07-01 thomas goto done;
2135 f4ae6ddb 2022-07-01 thomas }
2136 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2137 6353ad76 2019-02-08 stsp done:
2138 6353ad76 2019-02-08 stsp free(ondisk_path);
2139 8da9e5f4 2019-01-12 stsp return err;
2140 90285c3b 2019-01-08 stsp }
2141 90285c3b 2019-01-08 stsp
2142 90285c3b 2019-01-08 stsp static const struct got_error *
2143 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2144 90285c3b 2019-01-08 stsp {
2145 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2146 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2147 90285c3b 2019-01-08 stsp
2148 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2149 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2150 90285c3b 2019-01-08 stsp
2151 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2152 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2153 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2154 c97665ae 2019-01-13 stsp } else {
2155 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2156 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2157 1c4cdd89 2021-06-20 stsp if (err)
2158 1c4cdd89 2021-06-20 stsp goto done;
2159 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2160 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2161 fddefe3b 2020-10-20 stsp free(ondisk_path);
2162 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2163 1c4cdd89 2021-06-20 stsp parent = NULL;
2164 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2165 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2166 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2167 fddefe3b 2020-10-20 stsp ondisk_path);
2168 90285c3b 2019-01-08 stsp break;
2169 90285c3b 2019-01-08 stsp }
2170 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2171 1c4cdd89 2021-06-20 stsp if (err)
2172 1c4cdd89 2021-06-20 stsp break;
2173 1c4cdd89 2021-06-20 stsp }
2174 90285c3b 2019-01-08 stsp }
2175 1c4cdd89 2021-06-20 stsp done:
2176 90285c3b 2019-01-08 stsp free(ondisk_path);
2177 1c4cdd89 2021-06-20 stsp free(parent);
2178 90285c3b 2019-01-08 stsp return err;
2179 512f0d0e 2019-01-02 stsp }
2180 512f0d0e 2019-01-02 stsp
2181 708d8e67 2019-03-27 stsp static const struct got_error *
2182 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2183 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2184 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2185 708d8e67 2019-03-27 stsp {
2186 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2187 708d8e67 2019-03-27 stsp unsigned char status;
2188 708d8e67 2019-03-27 stsp struct stat sb;
2189 708d8e67 2019-03-27 stsp char *ondisk_path;
2190 708d8e67 2019-03-27 stsp
2191 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2192 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2193 a76c42e6 2019-08-03 stsp
2194 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2195 708d8e67 2019-03-27 stsp == -1)
2196 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2197 708d8e67 2019-03-27 stsp
2198 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2199 708d8e67 2019-03-27 stsp if (err)
2200 5a58a424 2020-06-23 stsp goto done;
2201 708d8e67 2019-03-27 stsp
2202 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2203 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2204 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2205 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2206 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2207 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2208 993e2a1b 2020-07-23 stsp goto done;
2209 993e2a1b 2020-07-23 stsp }
2210 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2211 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2212 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2213 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2214 993e2a1b 2020-07-23 stsp if (err)
2215 993e2a1b 2020-07-23 stsp goto done;
2216 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2217 993e2a1b 2020-07-23 stsp ie->path);
2218 993e2a1b 2020-07-23 stsp goto done;
2219 993e2a1b 2020-07-23 stsp }
2220 993e2a1b 2020-07-23 stsp
2221 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2222 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2223 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2224 1ee397ad 2019-07-12 stsp if (err)
2225 5a58a424 2020-06-23 stsp goto done;
2226 fc6346c4 2019-03-27 stsp /*
2227 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2228 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2229 fc6346c4 2019-03-27 stsp */
2230 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2231 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2232 fc6346c4 2019-03-27 stsp } else {
2233 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2234 1ee397ad 2019-07-12 stsp if (err)
2235 5a58a424 2020-06-23 stsp goto done;
2236 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2237 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2238 fc6346c4 2019-03-27 stsp if (err)
2239 5a58a424 2020-06-23 stsp goto done;
2240 fc6346c4 2019-03-27 stsp }
2241 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2242 708d8e67 2019-03-27 stsp }
2243 5a58a424 2020-06-23 stsp done:
2244 5a58a424 2020-06-23 stsp free(ondisk_path);
2245 fc6346c4 2019-03-27 stsp return err;
2246 708d8e67 2019-03-27 stsp }
2247 708d8e67 2019-03-27 stsp
2248 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2249 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2250 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2251 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2252 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2253 8da9e5f4 2019-01-12 stsp void *progress_arg;
2254 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2255 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2256 8da9e5f4 2019-01-12 stsp };
2257 8da9e5f4 2019-01-12 stsp
2258 512f0d0e 2019-01-02 stsp static const struct got_error *
2259 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2260 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2261 512f0d0e 2019-01-02 stsp {
2262 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2263 0584f854 2019-04-06 stsp
2264 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2265 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2266 512f0d0e 2019-01-02 stsp
2267 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2268 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2269 8da9e5f4 2019-01-12 stsp }
2270 512f0d0e 2019-01-02 stsp
2271 8da9e5f4 2019-01-12 stsp static const struct got_error *
2272 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2273 8da9e5f4 2019-01-12 stsp {
2274 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2275 7a9df742 2019-01-08 stsp
2276 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2277 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2278 0584f854 2019-04-06 stsp
2279 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2280 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2281 9d31a1d8 2018-03-11 stsp }
2282 9d31a1d8 2018-03-11 stsp
2283 9d31a1d8 2018-03-11 stsp static const struct got_error *
2284 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2285 9d31a1d8 2018-03-11 stsp {
2286 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2287 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2288 8da9e5f4 2019-01-12 stsp char *path;
2289 9d31a1d8 2018-03-11 stsp
2290 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2292 0584f854 2019-04-06 stsp
2293 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2294 63c5ca5d 2019-08-24 stsp return NULL;
2295 63c5ca5d 2019-08-24 stsp
2296 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2297 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2298 8da9e5f4 2019-01-12 stsp == -1)
2299 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2300 9d31a1d8 2018-03-11 stsp
2301 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2302 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2303 8da9e5f4 2019-01-12 stsp else
2304 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2305 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2306 9d31a1d8 2018-03-11 stsp
2307 8da9e5f4 2019-01-12 stsp free(path);
2308 0cd1c46a 2019-03-11 stsp return err;
2309 0cd1c46a 2019-03-11 stsp }
2310 0cd1c46a 2019-03-11 stsp
2311 b2118c49 2020-07-28 stsp const struct got_error *
2312 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2313 b2118c49 2020-07-28 stsp {
2314 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2315 b2118c49 2020-07-28 stsp
2316 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2317 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2318 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2319 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2320 b2118c49 2020-07-28 stsp }
2321 b2118c49 2020-07-28 stsp
2322 b2118c49 2020-07-28 stsp return NULL;
2323 b2118c49 2020-07-28 stsp }
2324 b2118c49 2020-07-28 stsp
2325 818c7501 2019-07-11 stsp static const struct got_error *
2326 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2327 0cd1c46a 2019-03-11 stsp {
2328 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2329 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2330 0cd1c46a 2019-03-11 stsp
2331 517bab73 2019-03-11 stsp *refname = NULL;
2332 517bab73 2019-03-11 stsp
2333 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2334 b2118c49 2020-07-28 stsp if (err)
2335 b2118c49 2020-07-28 stsp return err;
2336 0cd1c46a 2019-03-11 stsp
2337 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2338 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2339 0647c563 2019-03-11 stsp *refname = NULL;
2340 0cd1c46a 2019-03-11 stsp }
2341 517bab73 2019-03-11 stsp free(uuidstr);
2342 517bab73 2019-03-11 stsp return err;
2343 517bab73 2019-03-11 stsp }
2344 517bab73 2019-03-11 stsp
2345 818c7501 2019-07-11 stsp const struct got_error *
2346 f6cd0243 2023-01-28 thomas got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2347 f6cd0243 2023-01-28 thomas const char *prefix)
2348 f6cd0243 2023-01-28 thomas {
2349 f6cd0243 2023-01-28 thomas return get_ref_name(refname, worktree, prefix);
2350 f6cd0243 2023-01-28 thomas }
2351 f6cd0243 2023-01-28 thomas
2352 f6cd0243 2023-01-28 thomas const struct got_error *
2353 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2354 818c7501 2019-07-11 stsp {
2355 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2356 818c7501 2019-07-11 stsp }
2357 818c7501 2019-07-11 stsp
2358 818c7501 2019-07-11 stsp static const struct got_error *
2359 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2360 818c7501 2019-07-11 stsp {
2361 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2362 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2363 818c7501 2019-07-11 stsp }
2364 818c7501 2019-07-11 stsp
2365 818c7501 2019-07-11 stsp static const struct got_error *
2366 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2367 818c7501 2019-07-11 stsp {
2368 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2369 818c7501 2019-07-11 stsp }
2370 818c7501 2019-07-11 stsp
2371 818c7501 2019-07-11 stsp static const struct got_error *
2372 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2373 818c7501 2019-07-11 stsp {
2374 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2375 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2376 818c7501 2019-07-11 stsp }
2377 818c7501 2019-07-11 stsp
2378 818c7501 2019-07-11 stsp static const struct got_error *
2379 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2380 818c7501 2019-07-11 stsp {
2381 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2382 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2383 0ebf8283 2019-07-24 stsp }
2384 0ebf8283 2019-07-24 stsp
2385 0ebf8283 2019-07-24 stsp static const struct got_error *
2386 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2387 0ebf8283 2019-07-24 stsp {
2388 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2389 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2390 0ebf8283 2019-07-24 stsp }
2391 0ebf8283 2019-07-24 stsp
2392 0ebf8283 2019-07-24 stsp static const struct got_error *
2393 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2394 0ebf8283 2019-07-24 stsp {
2395 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2396 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2397 0ebf8283 2019-07-24 stsp }
2398 0ebf8283 2019-07-24 stsp
2399 0ebf8283 2019-07-24 stsp static const struct got_error *
2400 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2401 0ebf8283 2019-07-24 stsp {
2402 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2403 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2404 818c7501 2019-07-11 stsp }
2405 818c7501 2019-07-11 stsp
2406 0ebf8283 2019-07-24 stsp static const struct got_error *
2407 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2408 0ebf8283 2019-07-24 stsp {
2409 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2410 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2411 0ebf8283 2019-07-24 stsp }
2412 818c7501 2019-07-11 stsp
2413 0ebf8283 2019-07-24 stsp const struct got_error *
2414 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2415 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2416 0ebf8283 2019-07-24 stsp {
2417 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2418 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2419 0ebf8283 2019-07-24 stsp *path = NULL;
2420 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2421 0ebf8283 2019-07-24 stsp }
2422 0ebf8283 2019-07-24 stsp return NULL;
2423 10604dce 2021-09-24 thomas }
2424 10604dce 2021-09-24 thomas
2425 10604dce 2021-09-24 thomas static const struct got_error *
2426 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2427 10604dce 2021-09-24 thomas {
2428 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2429 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2430 0ebf8283 2019-07-24 stsp }
2431 0ebf8283 2019-07-24 stsp
2432 10604dce 2021-09-24 thomas static const struct got_error *
2433 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2434 10604dce 2021-09-24 thomas {
2435 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2436 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2437 10604dce 2021-09-24 thomas }
2438 10604dce 2021-09-24 thomas
2439 517bab73 2019-03-11 stsp /*
2440 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2441 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2442 517bab73 2019-03-11 stsp */
2443 517bab73 2019-03-11 stsp static const struct got_error *
2444 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2445 517bab73 2019-03-11 stsp {
2446 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2447 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2448 517bab73 2019-03-11 stsp char *refname;
2449 517bab73 2019-03-11 stsp
2450 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2451 517bab73 2019-03-11 stsp if (err)
2452 517bab73 2019-03-11 stsp return err;
2453 0cd1c46a 2019-03-11 stsp
2454 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2455 0cd1c46a 2019-03-11 stsp if (err)
2456 0cd1c46a 2019-03-11 stsp goto done;
2457 0cd1c46a 2019-03-11 stsp
2458 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2459 0cd1c46a 2019-03-11 stsp done:
2460 0cd1c46a 2019-03-11 stsp free(refname);
2461 0cd1c46a 2019-03-11 stsp if (ref)
2462 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2463 3e3a69f1 2019-07-25 stsp return err;
2464 3e3a69f1 2019-07-25 stsp }
2465 3e3a69f1 2019-07-25 stsp
2466 3e3a69f1 2019-07-25 stsp static const struct got_error *
2467 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2468 3e3a69f1 2019-07-25 stsp {
2469 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2470 3e3a69f1 2019-07-25 stsp
2471 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2472 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2473 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2474 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2475 3e3a69f1 2019-07-25 stsp }
2476 9d31a1d8 2018-03-11 stsp return err;
2477 9d31a1d8 2018-03-11 stsp }
2478 9d31a1d8 2018-03-11 stsp
2479 3e3a69f1 2019-07-25 stsp
2480 ebf99748 2019-05-09 stsp static const struct got_error *
2481 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2482 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2483 ebf99748 2019-05-09 stsp {
2484 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2485 ebf99748 2019-05-09 stsp FILE *index = NULL;
2486 0cd1c46a 2019-03-11 stsp
2487 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2488 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2489 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2490 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2491 ebf99748 2019-05-09 stsp
2492 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2493 3e3a69f1 2019-07-25 stsp if (err)
2494 ebf99748 2019-05-09 stsp goto done;
2495 ebf99748 2019-05-09 stsp
2496 c56c5d8a 2021-12-31 thomas index = fopen(*fileindex_path, "rbe");
2497 ebf99748 2019-05-09 stsp if (index == NULL) {
2498 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2499 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2500 ebf99748 2019-05-09 stsp } else {
2501 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2502 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2503 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2504 ebf99748 2019-05-09 stsp }
2505 ebf99748 2019-05-09 stsp done:
2506 ebf99748 2019-05-09 stsp if (err) {
2507 ebf99748 2019-05-09 stsp free(*fileindex_path);
2508 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2509 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2510 ebf99748 2019-05-09 stsp *fileindex = NULL;
2511 ebf99748 2019-05-09 stsp }
2512 ebf99748 2019-05-09 stsp return err;
2513 ebf99748 2019-05-09 stsp }
2514 c932eeeb 2019-05-22 stsp
2515 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2516 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2517 c932eeeb 2019-05-22 stsp const char *path;
2518 c932eeeb 2019-05-22 stsp size_t path_len;
2519 c932eeeb 2019-05-22 stsp const char *entry_name;
2520 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2521 a484d721 2019-06-10 stsp void *progress_arg;
2522 c932eeeb 2019-05-22 stsp };
2523 ebf99748 2019-05-09 stsp
2524 c932eeeb 2019-05-22 stsp static const struct got_error *
2525 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2526 c932eeeb 2019-05-22 stsp {
2527 1ee397ad 2019-07-12 stsp const struct got_error *err;
2528 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2529 c932eeeb 2019-05-22 stsp
2530 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2531 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2532 c932eeeb 2019-05-22 stsp return NULL;
2533 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2534 102ff934 2019-06-11 stsp return NULL;
2535 102ff934 2019-06-11 stsp
2536 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2537 a769b60b 2021-06-27 stsp return NULL;
2538 a769b60b 2021-06-27 stsp
2539 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2540 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2541 c932eeeb 2019-05-22 stsp return NULL;
2542 c932eeeb 2019-05-22 stsp
2543 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2544 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2545 edd02c5e 2019-07-11 stsp ie->path);
2546 1ee397ad 2019-07-12 stsp if (err)
2547 1ee397ad 2019-07-12 stsp return err;
2548 1ee397ad 2019-07-12 stsp }
2549 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2550 c932eeeb 2019-05-22 stsp return NULL;
2551 a615e0e7 2020-12-16 stsp }
2552 a615e0e7 2020-12-16 stsp
2553 748c5641 2023-02-07 thomas /* Bump base commit ID of all files within an updated part of the work tree. */
2554 a615e0e7 2020-12-16 stsp static const struct got_error *
2555 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2556 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2557 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2558 a615e0e7 2020-12-16 stsp {
2559 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2560 a615e0e7 2020-12-16 stsp
2561 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2562 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2563 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2564 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2565 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2566 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2567 a615e0e7 2020-12-16 stsp
2568 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2569 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2570 9c6338c4 2019-06-02 stsp }
2571 9c6338c4 2019-06-02 stsp
2572 9c6338c4 2019-06-02 stsp static const struct got_error *
2573 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2574 9c6338c4 2019-06-02 stsp {
2575 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2576 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2577 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2578 867630bb 2020-01-17 stsp struct timespec timeout;
2579 9c6338c4 2019-06-02 stsp
2580 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2581 fc2a50f2 2022-11-01 thomas fileindex_path, "");
2582 9c6338c4 2019-06-02 stsp if (err)
2583 9c6338c4 2019-06-02 stsp goto done;
2584 9c6338c4 2019-06-02 stsp
2585 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2586 9c6338c4 2019-06-02 stsp if (err)
2587 9c6338c4 2019-06-02 stsp goto done;
2588 9c6338c4 2019-06-02 stsp
2589 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2590 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2591 9c6338c4 2019-06-02 stsp fileindex_path);
2592 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2593 9c6338c4 2019-06-02 stsp }
2594 867630bb 2020-01-17 stsp
2595 867630bb 2020-01-17 stsp /*
2596 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2597 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2598 867630bb 2020-01-17 stsp * was recorded in the file index.
2599 867630bb 2020-01-17 stsp */
2600 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2601 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2602 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2603 9c6338c4 2019-06-02 stsp done:
2604 9c6338c4 2019-06-02 stsp if (new_index)
2605 9c6338c4 2019-06-02 stsp fclose(new_index);
2606 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2607 9c6338c4 2019-06-02 stsp return err;
2608 c932eeeb 2019-05-22 stsp }
2609 6ced4176 2019-07-12 stsp
2610 6ced4176 2019-07-12 stsp static const struct got_error *
2611 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2612 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2613 945f9229 2022-04-16 thomas struct got_commit_object *base_commit, struct got_worktree *worktree,
2614 945f9229 2022-04-16 thomas struct got_repository *repo)
2615 6ced4176 2019-07-12 stsp {
2616 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2617 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2618 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2619 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2620 6ced4176 2019-07-12 stsp
2621 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2622 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2623 6ced4176 2019-07-12 stsp *tree_id = NULL;
2624 6ced4176 2019-07-12 stsp
2625 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2626 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2627 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2628 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2629 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2630 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2631 6ced4176 2019-07-12 stsp goto done;
2632 6ced4176 2019-07-12 stsp }
2633 945f9229 2022-04-16 thomas err = got_object_id_by_path(tree_id, repo, base_commit,
2634 945f9229 2022-04-16 thomas worktree->path_prefix);
2635 6ced4176 2019-07-12 stsp if (err)
2636 6ced4176 2019-07-12 stsp goto done;
2637 6ced4176 2019-07-12 stsp return NULL;
2638 6ced4176 2019-07-12 stsp }
2639 6ced4176 2019-07-12 stsp
2640 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2641 6ced4176 2019-07-12 stsp
2642 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2643 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2644 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2645 6ced4176 2019-07-12 stsp goto done;
2646 6ced4176 2019-07-12 stsp }
2647 6ced4176 2019-07-12 stsp
2648 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2649 6ced4176 2019-07-12 stsp if (err)
2650 6ced4176 2019-07-12 stsp goto done;
2651 6ced4176 2019-07-12 stsp
2652 6ced4176 2019-07-12 stsp free(in_repo_path);
2653 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2654 6ced4176 2019-07-12 stsp
2655 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2656 6ced4176 2019-07-12 stsp if (err)
2657 6ced4176 2019-07-12 stsp goto done;
2658 c932eeeb 2019-05-22 stsp
2659 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2660 6ced4176 2019-07-12 stsp /* Check out a single file. */
2661 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2662 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2663 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2664 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2665 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2666 6ced4176 2019-07-12 stsp goto done;
2667 6ced4176 2019-07-12 stsp }
2668 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2669 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2670 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2671 6ced4176 2019-07-12 stsp goto done;
2672 6ced4176 2019-07-12 stsp }
2673 6ced4176 2019-07-12 stsp } else {
2674 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2675 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2676 6ced4176 2019-07-12 stsp if (err)
2677 6ced4176 2019-07-12 stsp return err;
2678 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2679 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2680 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2681 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2682 6ced4176 2019-07-12 stsp goto done;
2683 6ced4176 2019-07-12 stsp }
2684 6ced4176 2019-07-12 stsp }
2685 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2686 945f9229 2022-04-16 thomas base_commit, in_repo_path);
2687 6ced4176 2019-07-12 stsp } else {
2688 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2689 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2690 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2691 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2692 6ced4176 2019-07-12 stsp goto done;
2693 6ced4176 2019-07-12 stsp }
2694 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2695 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2696 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2697 6ced4176 2019-07-12 stsp goto done;
2698 6ced4176 2019-07-12 stsp }
2699 6ced4176 2019-07-12 stsp }
2700 6ced4176 2019-07-12 stsp done:
2701 6ced4176 2019-07-12 stsp free(id);
2702 6ced4176 2019-07-12 stsp free(in_repo_path);
2703 6ced4176 2019-07-12 stsp if (err) {
2704 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2705 6ced4176 2019-07-12 stsp free(*tree_relpath);
2706 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2707 6ced4176 2019-07-12 stsp free(*tree_id);
2708 6ced4176 2019-07-12 stsp *tree_id = NULL;
2709 6ced4176 2019-07-12 stsp }
2710 07ed472d 2019-07-12 stsp return err;
2711 07ed472d 2019-07-12 stsp }
2712 07ed472d 2019-07-12 stsp
2713 07ed472d 2019-07-12 stsp static const struct got_error *
2714 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2715 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2716 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2717 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2718 07ed472d 2019-07-12 stsp {
2719 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2720 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2721 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2722 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2723 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2724 07ed472d 2019-07-12 stsp
2725 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2726 7f47418f 2019-12-20 stsp if (err) {
2727 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2728 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2729 7f47418f 2019-12-20 stsp goto done;
2730 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2731 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2732 7f47418f 2019-12-20 stsp if (err)
2733 7f47418f 2019-12-20 stsp return err;
2734 7f47418f 2019-12-20 stsp }
2735 07ed472d 2019-07-12 stsp
2736 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2737 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2738 07ed472d 2019-07-12 stsp if (err)
2739 07ed472d 2019-07-12 stsp goto done;
2740 07ed472d 2019-07-12 stsp
2741 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2742 07ed472d 2019-07-12 stsp if (err)
2743 07ed472d 2019-07-12 stsp goto done;
2744 07ed472d 2019-07-12 stsp
2745 07ed472d 2019-07-12 stsp if (entry_name &&
2746 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2747 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2748 07ed472d 2019-07-12 stsp goto done;
2749 07ed472d 2019-07-12 stsp }
2750 07ed472d 2019-07-12 stsp
2751 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2752 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2753 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2754 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2755 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2756 07ed472d 2019-07-12 stsp arg.repo = repo;
2757 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2758 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2759 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2760 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2761 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2762 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2763 07ed472d 2019-07-12 stsp done:
2764 07ed472d 2019-07-12 stsp if (tree)
2765 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2766 07ed472d 2019-07-12 stsp if (commit)
2767 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2768 6ced4176 2019-07-12 stsp return err;
2769 6ced4176 2019-07-12 stsp }
2770 6ced4176 2019-07-12 stsp
2771 9d31a1d8 2018-03-11 stsp const struct got_error *
2772 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2773 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2774 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2775 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2776 9d31a1d8 2018-03-11 stsp {
2777 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2778 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2779 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2780 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2781 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2782 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2783 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2784 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2785 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2786 f2ea84fa 2019-07-27 stsp int entry_type;
2787 f2ea84fa 2019-07-27 stsp char *relpath;
2788 f2ea84fa 2019-07-27 stsp char *entry_name;
2789 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2790 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2791 9d31a1d8 2018-03-11 stsp
2792 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2793 f2ea84fa 2019-07-27 stsp
2794 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2795 9d31a1d8 2018-03-11 stsp if (err)
2796 9d31a1d8 2018-03-11 stsp return err;
2797 945f9229 2022-04-16 thomas
2798 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
2799 945f9229 2022-04-16 thomas worktree->base_commit_id);
2800 945f9229 2022-04-16 thomas if (err)
2801 945f9229 2022-04-16 thomas goto done;
2802 93a30277 2018-12-24 stsp
2803 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2804 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2805 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2806 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2807 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2808 f2ea84fa 2019-07-27 stsp goto done;
2809 f2ea84fa 2019-07-27 stsp }
2810 6ced4176 2019-07-12 stsp
2811 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2812 945f9229 2022-04-16 thomas &tpd->relpath, &tpd->tree_id, pe->path, commit,
2813 945f9229 2022-04-16 thomas worktree, repo);
2814 f2ea84fa 2019-07-27 stsp if (err) {
2815 f2ea84fa 2019-07-27 stsp free(tpd);
2816 6ced4176 2019-07-12 stsp goto done;
2817 6ced4176 2019-07-12 stsp }
2818 f2ea84fa 2019-07-27 stsp
2819 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2820 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2821 f2ea84fa 2019-07-27 stsp if (err) {
2822 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2823 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2824 f2ea84fa 2019-07-27 stsp free(tpd);
2825 f2ea84fa 2019-07-27 stsp goto done;
2826 f2ea84fa 2019-07-27 stsp }
2827 f2ea84fa 2019-07-27 stsp } else
2828 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2829 f2ea84fa 2019-07-27 stsp
2830 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2831 6ced4176 2019-07-12 stsp }
2832 6ced4176 2019-07-12 stsp
2833 51514078 2018-12-25 stsp /*
2834 51514078 2018-12-25 stsp * Read the file index.
2835 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2836 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2837 51514078 2018-12-25 stsp */
2838 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2839 8da9e5f4 2019-01-12 stsp if (err)
2840 c4cdcb68 2019-04-03 stsp goto done;
2841 c4cdcb68 2019-04-03 stsp
2842 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2843 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2844 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2845 f2ea84fa 2019-07-27 stsp
2846 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2847 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2848 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2849 f2ea84fa 2019-07-27 stsp if (err)
2850 f2ea84fa 2019-07-27 stsp break;
2851 f2ea84fa 2019-07-27 stsp
2852 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2853 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2854 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2855 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2856 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2857 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2858 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2859 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2860 f2ea84fa 2019-07-27 stsp if (err)
2861 f2ea84fa 2019-07-27 stsp break;
2862 f2ea84fa 2019-07-27 stsp
2863 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2864 711d9cd9 2019-07-12 stsp }
2865 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2866 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2867 af12c6b9 2019-06-04 stsp err = sync_err;
2868 9d31a1d8 2018-03-11 stsp done:
2869 9c6338c4 2019-06-02 stsp free(fileindex_path);
2870 edfa7d7f 2018-09-11 stsp if (tree)
2871 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2872 9d31a1d8 2018-03-11 stsp if (commit)
2873 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2874 5ade8233 2019-07-12 stsp if (fileindex)
2875 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2876 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2877 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2878 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2879 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2880 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2881 f2ea84fa 2019-07-27 stsp free(tpd);
2882 f2ea84fa 2019-07-27 stsp }
2883 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2884 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2885 9d31a1d8 2018-03-11 stsp err = unlockerr;
2886 f8d1f275 2019-02-04 stsp return err;
2887 f8d1f275 2019-02-04 stsp }
2888 f8d1f275 2019-02-04 stsp
2889 0529f8df 2023-03-10 thomas static const struct got_error *
2890 0529f8df 2023-03-10 thomas add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2891 0529f8df 2023-03-10 thomas struct got_fileindex_entry *ie, const char *ondisk_path,
2892 0529f8df 2023-03-10 thomas const char *path2, struct got_blob_object *blob2, mode_t mode2,
2893 0529f8df 2023-03-10 thomas int restoring_missing_file, int reverting_versioned_file,
2894 0529f8df 2023-03-10 thomas int path_is_unversioned, int allow_bad_symlinks,
2895 0529f8df 2023-03-10 thomas struct got_repository *repo,
2896 0529f8df 2023-03-10 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
2897 0529f8df 2023-03-10 thomas {
2898 0529f8df 2023-03-10 thomas const struct got_error *err = NULL;
2899 0529f8df 2023-03-10 thomas int is_bad_symlink = 0;
2900 0529f8df 2023-03-10 thomas
2901 0529f8df 2023-03-10 thomas if (S_ISLNK(mode2)) {
2902 0529f8df 2023-03-10 thomas err = install_symlink(&is_bad_symlink,
2903 0529f8df 2023-03-10 thomas worktree, ondisk_path, path2, blob2,
2904 0529f8df 2023-03-10 thomas restoring_missing_file,
2905 0529f8df 2023-03-10 thomas reverting_versioned_file,
2906 0529f8df 2023-03-10 thomas path_is_unversioned, allow_bad_symlinks,
2907 0529f8df 2023-03-10 thomas repo, progress_cb, progress_arg);
2908 0529f8df 2023-03-10 thomas } else {
2909 0529f8df 2023-03-10 thomas err = install_blob(worktree, ondisk_path, path2,
2910 0529f8df 2023-03-10 thomas mode2, GOT_DEFAULT_FILE_MODE, blob2,
2911 0529f8df 2023-03-10 thomas restoring_missing_file, reverting_versioned_file, 0,
2912 0529f8df 2023-03-10 thomas path_is_unversioned, repo, progress_cb, progress_arg);
2913 0529f8df 2023-03-10 thomas }
2914 0529f8df 2023-03-10 thomas if (err)
2915 0529f8df 2023-03-10 thomas return err;
2916 0529f8df 2023-03-10 thomas if (ie == NULL) {
2917 0529f8df 2023-03-10 thomas /* Adding an unversioned file. */
2918 0529f8df 2023-03-10 thomas err = got_fileindex_entry_alloc(&ie, path2);
2919 0529f8df 2023-03-10 thomas if (err)
2920 0529f8df 2023-03-10 thomas return err;
2921 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2922 0529f8df 2023-03-10 thomas worktree->root_fd, path2, NULL, NULL, 1);
2923 0529f8df 2023-03-10 thomas if (err) {
2924 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2925 0529f8df 2023-03-10 thomas return err;
2926 0529f8df 2023-03-10 thomas }
2927 0529f8df 2023-03-10 thomas err = got_fileindex_entry_add(fileindex, ie);
2928 0529f8df 2023-03-10 thomas if (err) {
2929 0529f8df 2023-03-10 thomas got_fileindex_entry_free(ie);
2930 0529f8df 2023-03-10 thomas return err;
2931 0529f8df 2023-03-10 thomas }
2932 0529f8df 2023-03-10 thomas } else {
2933 0529f8df 2023-03-10 thomas /* Re-adding a locally deleted file. */
2934 0529f8df 2023-03-10 thomas err = got_fileindex_entry_update(ie,
2935 0529f8df 2023-03-10 thomas worktree->root_fd, path2, ie->blob_sha1,
2936 0529f8df 2023-03-10 thomas worktree->base_commit_id->sha1, 0);
2937 0529f8df 2023-03-10 thomas if (err)
2938 0529f8df 2023-03-10 thomas return err;
2939 0529f8df 2023-03-10 thomas }
2940 0529f8df 2023-03-10 thomas
2941 0529f8df 2023-03-10 thomas if (is_bad_symlink) {
2942 0529f8df 2023-03-10 thomas got_fileindex_entry_filetype_set(ie,
2943 0529f8df 2023-03-10 thomas GOT_FILEIDX_MODE_BAD_SYMLINK);
2944 0529f8df 2023-03-10 thomas }
2945 0529f8df 2023-03-10 thomas
2946 0529f8df 2023-03-10 thomas return NULL;
2947 0529f8df 2023-03-10 thomas }
2948 0529f8df 2023-03-10 thomas
2949 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2950 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2951 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2952 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2953 234035bc 2019-06-01 stsp void *progress_arg;
2954 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2955 234035bc 2019-06-01 stsp void *cancel_arg;
2956 f69721c3 2019-10-21 stsp const char *label_orig;
2957 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2958 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2959 234035bc 2019-06-01 stsp };
2960 234035bc 2019-06-01 stsp
2961 234035bc 2019-06-01 stsp static const struct got_error *
2962 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2963 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
2964 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
2965 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
2966 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2967 234035bc 2019-06-01 stsp {
2968 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2969 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2970 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2971 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2972 234035bc 2019-06-01 stsp struct stat sb;
2973 234035bc 2019-06-01 stsp unsigned char status;
2974 234035bc 2019-06-01 stsp int local_changes_subsumed;
2975 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2976 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2977 234035bc 2019-06-01 stsp
2978 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2979 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2980 d6c87207 2019-08-02 stsp strlen(path2));
2981 1ee397ad 2019-07-12 stsp if (ie == NULL)
2982 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2983 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2984 234035bc 2019-06-01 stsp
2985 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2986 234035bc 2019-06-01 stsp path2) == -1)
2987 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2988 234035bc 2019-06-01 stsp
2989 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2990 7f91a133 2019-12-13 stsp repo);
2991 234035bc 2019-06-01 stsp if (err)
2992 234035bc 2019-06-01 stsp goto done;
2993 234035bc 2019-06-01 stsp
2994 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2995 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2996 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2997 234035bc 2019-06-01 stsp goto done;
2998 234035bc 2019-06-01 stsp }
2999 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3000 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3001 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3002 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3003 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3004 234035bc 2019-06-01 stsp goto done;
3005 234035bc 2019-06-01 stsp }
3006 234035bc 2019-06-01 stsp
3007 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3008 36bf999c 2020-07-23 stsp char *link_target2;
3009 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3010 36bf999c 2020-07-23 stsp if (err)
3011 36bf999c 2020-07-23 stsp goto done;
3012 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3013 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3014 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3015 36bf999c 2020-07-23 stsp free(link_target2);
3016 af57b12a 2020-07-23 stsp } else {
3017 1af628f4 2021-06-03 stsp int fd;
3018 1af628f4 2021-06-03 stsp
3019 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3020 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3021 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3022 1af628f4 2021-06-03 stsp goto done;
3023 1af628f4 2021-06-03 stsp }
3024 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3025 1af628f4 2021-06-03 stsp f_orig, blob1);
3026 1af628f4 2021-06-03 stsp if (err)
3027 1af628f4 2021-06-03 stsp goto done;
3028 1af628f4 2021-06-03 stsp
3029 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3030 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3031 1af628f4 2021-06-03 stsp goto done;
3032 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3033 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3034 1af628f4 2021-06-03 stsp if (err)
3035 1af628f4 2021-06-03 stsp goto done;
3036 1af628f4 2021-06-03 stsp
3037 48b4f239 2021-12-31 thomas fd = open(ondisk_path,
3038 48b4f239 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3039 1af628f4 2021-06-03 stsp if (fd == -1) {
3040 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3041 1af628f4 2021-06-03 stsp ondisk_path);
3042 1af628f4 2021-06-03 stsp goto done;
3043 1af628f4 2021-06-03 stsp }
3044 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3045 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3046 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3047 1af628f4 2021-06-03 stsp ondisk_path);
3048 1af628f4 2021-06-03 stsp close(fd);
3049 1af628f4 2021-06-03 stsp goto done;
3050 1af628f4 2021-06-03 stsp }
3051 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3052 1af628f4 2021-06-03 stsp if (err)
3053 1af628f4 2021-06-03 stsp goto done;
3054 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3055 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3056 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3057 1af628f4 2021-06-03 stsp goto done;
3058 1af628f4 2021-06-03 stsp }
3059 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3060 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3061 630fc61f 2023-01-29 thomas mode2, a->label_orig, NULL, label_deriv2,
3062 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3063 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3064 af57b12a 2020-07-23 stsp }
3065 234035bc 2019-06-01 stsp } else if (blob1) {
3066 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3067 d6c87207 2019-08-02 stsp strlen(path1));
3068 1ee397ad 2019-07-12 stsp if (ie == NULL)
3069 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3070 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3071 2b92fad7 2019-06-02 stsp
3072 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3073 2b92fad7 2019-06-02 stsp path1) == -1)
3074 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3075 2b92fad7 2019-06-02 stsp
3076 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3077 7f91a133 2019-12-13 stsp repo);
3078 2b92fad7 2019-06-02 stsp if (err)
3079 2b92fad7 2019-06-02 stsp goto done;
3080 2b92fad7 2019-06-02 stsp
3081 2b92fad7 2019-06-02 stsp switch (status) {
3082 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3083 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3084 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3085 1ee397ad 2019-07-12 stsp if (err)
3086 1ee397ad 2019-07-12 stsp goto done;
3087 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3088 2b92fad7 2019-06-02 stsp if (err)
3089 2b92fad7 2019-06-02 stsp goto done;
3090 2b92fad7 2019-06-02 stsp if (ie)
3091 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3092 2b92fad7 2019-06-02 stsp break;
3093 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3094 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3095 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3096 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3097 1ee397ad 2019-07-12 stsp if (err)
3098 1ee397ad 2019-07-12 stsp goto done;
3099 2b92fad7 2019-06-02 stsp if (ie)
3100 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3101 2b92fad7 2019-06-02 stsp break;
3102 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3103 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3104 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3105 e8f02263 2022-01-23 thomas off_t blob1_size;
3106 0a22ca1a 2020-09-23 stsp /*
3107 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3108 0a22ca1a 2020-09-23 stsp * exists in the repository.
3109 0a22ca1a 2020-09-23 stsp */
3110 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(&id, &blob1_f,
3111 e8f02263 2022-01-23 thomas &blob1_size, path1);
3112 0a22ca1a 2020-09-23 stsp if (err)
3113 0a22ca1a 2020-09-23 stsp goto done;
3114 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3115 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3116 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3117 0a22ca1a 2020-09-23 stsp if (err)
3118 0a22ca1a 2020-09-23 stsp goto done;
3119 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3120 0a22ca1a 2020-09-23 stsp path1);
3121 0a22ca1a 2020-09-23 stsp if (err)
3122 0a22ca1a 2020-09-23 stsp goto done;
3123 0a22ca1a 2020-09-23 stsp if (ie)
3124 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3125 0a22ca1a 2020-09-23 stsp ie);
3126 0a22ca1a 2020-09-23 stsp } else {
3127 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3128 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3129 0a22ca1a 2020-09-23 stsp }
3130 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3131 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3132 0a22ca1a 2020-09-23 stsp free(id);
3133 0a22ca1a 2020-09-23 stsp if (err)
3134 0a22ca1a 2020-09-23 stsp goto done;
3135 0a22ca1a 2020-09-23 stsp break;
3136 0a22ca1a 2020-09-23 stsp }
3137 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3138 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3139 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3140 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3141 1ee397ad 2019-07-12 stsp if (err)
3142 1ee397ad 2019-07-12 stsp goto done;
3143 2b92fad7 2019-06-02 stsp break;
3144 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3145 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3146 1ee397ad 2019-07-12 stsp if (err)
3147 1ee397ad 2019-07-12 stsp goto done;
3148 2b92fad7 2019-06-02 stsp break;
3149 2b92fad7 2019-06-02 stsp default:
3150 2b92fad7 2019-06-02 stsp break;
3151 2b92fad7 2019-06-02 stsp }
3152 234035bc 2019-06-01 stsp } else if (blob2) {
3153 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3154 234035bc 2019-06-01 stsp path2) == -1)
3155 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3156 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3157 d6c87207 2019-08-02 stsp strlen(path2));
3158 234035bc 2019-06-01 stsp if (ie) {
3159 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3160 7f91a133 2019-12-13 stsp -1, NULL, repo);
3161 234035bc 2019-06-01 stsp if (err)
3162 234035bc 2019-06-01 stsp goto done;
3163 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3164 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3165 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3166 0529f8df 2023-03-10 thomas status != GOT_STATUS_ADD &&
3167 0529f8df 2023-03-10 thomas status != GOT_STATUS_DELETE) {
3168 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3169 1ee397ad 2019-07-12 stsp status, path2);
3170 234035bc 2019-06-01 stsp goto done;
3171 234035bc 2019-06-01 stsp }
3172 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3173 36bf999c 2020-07-23 stsp char *link_target2;
3174 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3175 36bf999c 2020-07-23 stsp blob2);
3176 36bf999c 2020-07-23 stsp if (err)
3177 36bf999c 2020-07-23 stsp goto done;
3178 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3179 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3180 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3181 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3182 36bf999c 2020-07-23 stsp free(link_target2);
3183 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3184 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3185 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3186 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3187 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3188 526a746f 2020-07-23 stsp a->progress_arg);
3189 0529f8df 2023-03-10 thomas } else if (status != GOT_STATUS_DELETE) {
3190 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3191 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3192 526a746f 2020-07-23 stsp }
3193 dfe9fba0 2020-07-23 stsp if (err)
3194 dfe9fba0 2020-07-23 stsp goto done;
3195 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3196 0529f8df 2023-03-10 thomas /* Re-add file with content from new blob. */
3197 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, ie,
3198 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3199 0529f8df 2023-03-10 thomas 0, 0, 0, a->allow_bad_symlinks,
3200 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3201 234035bc 2019-06-01 stsp if (err)
3202 234035bc 2019-06-01 stsp goto done;
3203 234035bc 2019-06-01 stsp }
3204 234035bc 2019-06-01 stsp } else {
3205 0529f8df 2023-03-10 thomas err = add_file(a->worktree, a->fileindex, NULL,
3206 0529f8df 2023-03-10 thomas ondisk_path, path2, blob2, mode2,
3207 0529f8df 2023-03-10 thomas 0, 0, 1, a->allow_bad_symlinks,
3208 0529f8df 2023-03-10 thomas repo, a->progress_cb, a->progress_arg);
3209 234035bc 2019-06-01 stsp if (err)
3210 2b92fad7 2019-06-02 stsp goto done;
3211 234035bc 2019-06-01 stsp }
3212 234035bc 2019-06-01 stsp }
3213 234035bc 2019-06-01 stsp done:
3214 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3215 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3216 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3217 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3218 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3219 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3220 1af628f4 2021-06-03 stsp free(id_str);
3221 54d5be07 2021-06-03 stsp free(label_deriv2);
3222 234035bc 2019-06-01 stsp free(ondisk_path);
3223 234035bc 2019-06-01 stsp return err;
3224 234035bc 2019-06-01 stsp }
3225 234035bc 2019-06-01 stsp
3226 69de9dd4 2021-09-03 stsp static const struct got_error *
3227 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3228 69de9dd4 2021-09-03 stsp {
3229 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3230 69de9dd4 2021-09-03 stsp
3231 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3232 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3233 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3234 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3235 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3236 69de9dd4 2021-09-03 stsp
3237 69de9dd4 2021-09-03 stsp return NULL;
3238 69de9dd4 2021-09-03 stsp }
3239 69de9dd4 2021-09-03 stsp
3240 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3241 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3242 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3243 234035bc 2019-06-01 stsp struct got_repository *repo;
3244 234035bc 2019-06-01 stsp };
3245 234035bc 2019-06-01 stsp
3246 234035bc 2019-06-01 stsp static const struct got_error *
3247 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3248 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
3249 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
3250 a0f32f33 2022-06-13 thomas const char *path1, const char *path2,
3251 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3252 234035bc 2019-06-01 stsp {
3253 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3254 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3255 234035bc 2019-06-01 stsp unsigned char status;
3256 234035bc 2019-06-01 stsp struct stat sb;
3257 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3258 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3259 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3260 234035bc 2019-06-01 stsp char *ondisk_path;
3261 234035bc 2019-06-01 stsp
3262 69de9dd4 2021-09-03 stsp if (id == NULL)
3263 69de9dd4 2021-09-03 stsp return NULL;
3264 234035bc 2019-06-01 stsp
3265 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3266 69de9dd4 2021-09-03 stsp if (ie == NULL)
3267 69de9dd4 2021-09-03 stsp return NULL;
3268 69de9dd4 2021-09-03 stsp
3269 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3270 234035bc 2019-06-01 stsp == -1)
3271 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3272 234035bc 2019-06-01 stsp
3273 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3274 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3275 5546d466 2021-09-02 stsp free(ondisk_path);
3276 234035bc 2019-06-01 stsp if (err)
3277 234035bc 2019-06-01 stsp return err;
3278 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3279 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3280 234035bc 2019-06-01 stsp
3281 234035bc 2019-06-01 stsp return NULL;
3282 234035bc 2019-06-01 stsp }
3283 234035bc 2019-06-01 stsp
3284 818c7501 2019-07-11 stsp static const struct got_error *
3285 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3286 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3287 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3288 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3289 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3290 234035bc 2019-06-01 stsp {
3291 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3292 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3293 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3294 945f9229 2022-04-16 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3295 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3296 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3297 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3298 a0f32f33 2022-06-13 thomas FILE *f1 = NULL, *f2 = NULL;
3299 19a6a6b5 2022-07-01 thomas int fd1 = -1, fd2 = -1;
3300 234035bc 2019-06-01 stsp
3301 03415a1a 2019-06-02 stsp if (commit_id1) {
3302 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit1, repo, commit_id1);
3303 945f9229 2022-04-16 thomas if (err)
3304 945f9229 2022-04-16 thomas goto done;
3305 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id1, repo, commit1,
3306 03415a1a 2019-06-02 stsp worktree->path_prefix);
3307 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3308 03415a1a 2019-06-02 stsp goto done;
3309 69d57f3d 2020-07-31 stsp }
3310 69d57f3d 2020-07-31 stsp if (tree_id1) {
3311 69d57f3d 2020-07-31 stsp char *id_str;
3312 03415a1a 2019-06-02 stsp
3313 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3314 03415a1a 2019-06-02 stsp if (err)
3315 03415a1a 2019-06-02 stsp goto done;
3316 f69721c3 2019-10-21 stsp
3317 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3318 f69721c3 2019-10-21 stsp if (err)
3319 f69721c3 2019-10-21 stsp goto done;
3320 f69721c3 2019-10-21 stsp
3321 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3322 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3323 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3324 f69721c3 2019-10-21 stsp free(id_str);
3325 f69721c3 2019-10-21 stsp goto done;
3326 f69721c3 2019-10-21 stsp }
3327 f69721c3 2019-10-21 stsp free(id_str);
3328 a0f32f33 2022-06-13 thomas
3329 a0f32f33 2022-06-13 thomas f1 = got_opentemp();
3330 a0f32f33 2022-06-13 thomas if (f1 == NULL) {
3331 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3332 a0f32f33 2022-06-13 thomas goto done;
3333 a0f32f33 2022-06-13 thomas }
3334 03415a1a 2019-06-02 stsp }
3335 234035bc 2019-06-01 stsp
3336 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit2, repo, commit_id2);
3337 945f9229 2022-04-16 thomas if (err)
3338 945f9229 2022-04-16 thomas goto done;
3339 945f9229 2022-04-16 thomas
3340 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id2, repo, commit2,
3341 234035bc 2019-06-01 stsp worktree->path_prefix);
3342 234035bc 2019-06-01 stsp if (err)
3343 234035bc 2019-06-01 stsp goto done;
3344 234035bc 2019-06-01 stsp
3345 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3346 234035bc 2019-06-01 stsp if (err)
3347 234035bc 2019-06-01 stsp goto done;
3348 234035bc 2019-06-01 stsp
3349 a0f32f33 2022-06-13 thomas f2 = got_opentemp();
3350 a0f32f33 2022-06-13 thomas if (f2 == NULL) {
3351 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3352 19a6a6b5 2022-07-01 thomas goto done;
3353 19a6a6b5 2022-07-01 thomas }
3354 19a6a6b5 2022-07-01 thomas
3355 19a6a6b5 2022-07-01 thomas fd1 = got_opentempfd();
3356 19a6a6b5 2022-07-01 thomas if (fd1 == -1) {
3357 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3358 a0f32f33 2022-06-13 thomas goto done;
3359 a0f32f33 2022-06-13 thomas }
3360 a0f32f33 2022-06-13 thomas
3361 19a6a6b5 2022-07-01 thomas fd2 = got_opentempfd();
3362 19a6a6b5 2022-07-01 thomas if (fd2 == -1) {
3363 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
3364 19a6a6b5 2022-07-01 thomas goto done;
3365 19a6a6b5 2022-07-01 thomas }
3366 19a6a6b5 2022-07-01 thomas
3367 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3368 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3369 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3370 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3371 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3372 69de9dd4 2021-09-03 stsp if (err)
3373 69de9dd4 2021-09-03 stsp goto done;
3374 69de9dd4 2021-09-03 stsp
3375 234035bc 2019-06-01 stsp arg.worktree = worktree;
3376 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3377 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3378 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3379 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3380 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3381 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3382 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3383 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3384 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3385 a0f32f33 2022-06-13 thomas merge_file_cb, &arg, 1);
3386 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3387 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3388 af12c6b9 2019-06-04 stsp err = sync_err;
3389 234035bc 2019-06-01 stsp done:
3390 945f9229 2022-04-16 thomas if (commit1)
3391 945f9229 2022-04-16 thomas got_object_commit_close(commit1);
3392 945f9229 2022-04-16 thomas if (commit2)
3393 945f9229 2022-04-16 thomas got_object_commit_close(commit2);
3394 234035bc 2019-06-01 stsp if (tree1)
3395 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3396 234035bc 2019-06-01 stsp if (tree2)
3397 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3398 a0f32f33 2022-06-13 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3399 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3400 a0f32f33 2022-06-13 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3401 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3402 19a6a6b5 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3403 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3404 19a6a6b5 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3405 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
3406 f69721c3 2019-10-21 stsp free(label_orig);
3407 818c7501 2019-07-11 stsp return err;
3408 818c7501 2019-07-11 stsp }
3409 234035bc 2019-06-01 stsp
3410 818c7501 2019-07-11 stsp const struct got_error *
3411 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3412 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3413 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3414 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3415 818c7501 2019-07-11 stsp {
3416 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3417 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3418 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3419 818c7501 2019-07-11 stsp
3420 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3421 818c7501 2019-07-11 stsp if (err)
3422 818c7501 2019-07-11 stsp return err;
3423 818c7501 2019-07-11 stsp
3424 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3425 818c7501 2019-07-11 stsp if (err)
3426 818c7501 2019-07-11 stsp goto done;
3427 818c7501 2019-07-11 stsp
3428 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3429 69de9dd4 2021-09-03 stsp worktree);
3430 818c7501 2019-07-11 stsp if (err)
3431 818c7501 2019-07-11 stsp goto done;
3432 818c7501 2019-07-11 stsp
3433 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3434 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3435 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3436 818c7501 2019-07-11 stsp done:
3437 818c7501 2019-07-11 stsp if (fileindex)
3438 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3439 818c7501 2019-07-11 stsp free(fileindex_path);
3440 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3441 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3442 234035bc 2019-06-01 stsp err = unlockerr;
3443 234035bc 2019-06-01 stsp return err;
3444 234035bc 2019-06-01 stsp }
3445 234035bc 2019-06-01 stsp
3446 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3447 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3448 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3449 927df6b7 2019-02-10 stsp const char *status_path;
3450 927df6b7 2019-02-10 stsp size_t status_path_len;
3451 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3452 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3453 f8d1f275 2019-02-04 stsp void *status_arg;
3454 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3455 f8d1f275 2019-02-04 stsp void *cancel_arg;
3456 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3457 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3458 f2a9dc41 2019-12-13 tracey int report_unchanged;
3459 3143d852 2020-06-25 stsp int no_ignores;
3460 f8d1f275 2019-02-04 stsp };
3461 88d0e355 2019-08-03 stsp
3462 f8d1f275 2019-02-04 stsp static const struct got_error *
3463 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3464 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3465 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3466 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3467 927df6b7 2019-02-10 stsp {
3468 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3469 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3470 dfd83cb6 2023-02-17 thomas unsigned char staged_status;
3471 927df6b7 2019-02-10 stsp struct stat sb;
3472 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3473 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3474 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3475 927df6b7 2019-02-10 stsp
3476 dfd83cb6 2023-02-17 thomas staged_status = get_staged_status(ie);
3477 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3478 98eaaa12 2019-08-03 stsp if (err)
3479 98eaaa12 2019-08-03 stsp return err;
3480 98eaaa12 2019-08-03 stsp
3481 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3482 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3483 98eaaa12 2019-08-03 stsp return NULL;
3484 98eaaa12 2019-08-03 stsp
3485 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
3486 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3487 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
3488 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3489 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3490 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3491 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3492 43010591 2023-02-17 thomas &staged_blob_id, ie);
3493 98eaaa12 2019-08-03 stsp }
3494 98eaaa12 2019-08-03 stsp
3495 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3496 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3497 927df6b7 2019-02-10 stsp }
3498 927df6b7 2019-02-10 stsp
3499 927df6b7 2019-02-10 stsp static const struct got_error *
3500 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3501 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3502 f8d1f275 2019-02-04 stsp {
3503 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3504 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3505 f8d1f275 2019-02-04 stsp char *abspath;
3506 f8d1f275 2019-02-04 stsp
3507 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3508 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3509 0584f854 2019-04-06 stsp
3510 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3511 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3512 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3513 927df6b7 2019-02-10 stsp return NULL;
3514 927df6b7 2019-02-10 stsp
3515 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3516 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3517 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3518 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3519 f8d1f275 2019-02-04 stsp } else {
3520 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3521 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3522 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3523 f8d1f275 2019-02-04 stsp }
3524 f8d1f275 2019-02-04 stsp
3525 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3526 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3527 f8d1f275 2019-02-04 stsp free(abspath);
3528 9d31a1d8 2018-03-11 stsp return err;
3529 9d31a1d8 2018-03-11 stsp }
3530 f8d1f275 2019-02-04 stsp
3531 f8d1f275 2019-02-04 stsp static const struct got_error *
3532 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3533 f8d1f275 2019-02-04 stsp {
3534 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3535 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3536 2ec1f75b 2019-03-26 stsp unsigned char status;
3537 927df6b7 2019-02-10 stsp
3538 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3539 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3540 0584f854 2019-04-06 stsp
3541 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3542 927df6b7 2019-02-10 stsp return NULL;
3543 927df6b7 2019-02-10 stsp
3544 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&blob_id, ie);
3545 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(&commit_id, ie);
3546 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3547 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3548 2ec1f75b 2019-03-26 stsp else
3549 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3550 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3551 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3552 6841da00 2019-08-08 stsp }
3553 6841da00 2019-08-08 stsp
3554 ef20f542 2022-06-26 thomas static void
3555 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3556 6841da00 2019-08-08 stsp {
3557 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3558 6841da00 2019-08-08 stsp
3559 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3560 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3561 21c2d8be 2023-01-10 thomas
3562 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3563 6841da00 2019-08-08 stsp }
3564 21c2d8be 2023-01-10 thomas got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3565 6841da00 2019-08-08 stsp }
3566 6841da00 2019-08-08 stsp
3567 6841da00 2019-08-08 stsp static const struct got_error *
3568 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3569 6841da00 2019-08-08 stsp {
3570 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3571 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3572 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3573 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3574 6841da00 2019-08-08 stsp size_t linesize = 0;
3575 6841da00 2019-08-08 stsp ssize_t linelen;
3576 6841da00 2019-08-08 stsp
3577 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3578 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3579 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3580 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3581 6841da00 2019-08-08 stsp
3582 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3583 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3584 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3585 bd8de430 2019-10-04 stsp
3586 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3587 bd8de430 2019-10-04 stsp if (line[0] == '#')
3588 bd8de430 2019-10-04 stsp continue;
3589 bd8de430 2019-10-04 stsp
3590 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3591 bd8de430 2019-10-04 stsp if (line[0] == '!')
3592 bd8de430 2019-10-04 stsp continue;
3593 bd8de430 2019-10-04 stsp
3594 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3595 6841da00 2019-08-08 stsp line) == -1) {
3596 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3597 6841da00 2019-08-08 stsp goto done;
3598 6841da00 2019-08-08 stsp }
3599 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3600 6841da00 2019-08-08 stsp if (err)
3601 6841da00 2019-08-08 stsp goto done;
3602 6841da00 2019-08-08 stsp }
3603 6841da00 2019-08-08 stsp if (ferror(f)) {
3604 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3605 6841da00 2019-08-08 stsp goto done;
3606 6841da00 2019-08-08 stsp }
3607 6841da00 2019-08-08 stsp
3608 6841da00 2019-08-08 stsp dirpath = strdup(path);
3609 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3610 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3611 6841da00 2019-08-08 stsp goto done;
3612 6841da00 2019-08-08 stsp }
3613 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3614 6841da00 2019-08-08 stsp done:
3615 6841da00 2019-08-08 stsp free(line);
3616 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3617 6841da00 2019-08-08 stsp free(dirpath);
3618 21c2d8be 2023-01-10 thomas got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3619 6841da00 2019-08-08 stsp }
3620 6841da00 2019-08-08 stsp return err;
3621 1b5d300f 2023-02-20 thomas }
3622 1b5d300f 2023-02-20 thomas
3623 1b5d300f 2023-02-20 thomas static int
3624 1b5d300f 2023-02-20 thomas match_path(const char *pattern, size_t pattern_len, const char *path,
3625 1b5d300f 2023-02-20 thomas int flags)
3626 1b5d300f 2023-02-20 thomas {
3627 1b5d300f 2023-02-20 thomas char buf[PATH_MAX];
3628 1b5d300f 2023-02-20 thomas
3629 1b5d300f 2023-02-20 thomas /*
3630 1b5d300f 2023-02-20 thomas * Trailing slashes signify directories.
3631 1b5d300f 2023-02-20 thomas * Append a * to make such patterns conform to fnmatch rules.
3632 1b5d300f 2023-02-20 thomas */
3633 1b5d300f 2023-02-20 thomas if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3634 1b5d300f 2023-02-20 thomas if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3635 1b5d300f 2023-02-20 thomas return FNM_NOMATCH; /* XXX */
3636 1b5d300f 2023-02-20 thomas
3637 1b5d300f 2023-02-20 thomas return fnmatch(buf, path, flags);
3638 1b5d300f 2023-02-20 thomas }
3639 1b5d300f 2023-02-20 thomas
3640 1b5d300f 2023-02-20 thomas return fnmatch(pattern, path, flags);
3641 6841da00 2019-08-08 stsp }
3642 6841da00 2019-08-08 stsp
3643 ef20f542 2022-06-26 thomas static int
3644 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3645 6841da00 2019-08-08 stsp {
3646 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3647 bd8de430 2019-10-04 stsp
3648 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3649 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3650 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3651 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3652 bd8de430 2019-10-04 stsp
3653 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3654 1b5d300f 2023-02-20 thomas const char *p;
3655 6841da00 2019-08-08 stsp
3656 1b5d300f 2023-02-20 thomas if (pi->path_len < 3 ||
3657 1b5d300f 2023-02-20 thomas strncmp(pi->path, "**/", 3) != 0)
3658 bd8de430 2019-10-04 stsp continue;
3659 bd8de430 2019-10-04 stsp p = path;
3660 bd8de430 2019-10-04 stsp while (*p) {
3661 1b5d300f 2023-02-20 thomas if (match_path(pi->path + 3,
3662 1b5d300f 2023-02-20 thomas pi->path_len - 3, p,
3663 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3664 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3665 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3666 bd8de430 2019-10-04 stsp p++;
3667 bd8de430 2019-10-04 stsp while (*p == '/')
3668 bd8de430 2019-10-04 stsp p++;
3669 bd8de430 2019-10-04 stsp continue;
3670 bd8de430 2019-10-04 stsp }
3671 bd8de430 2019-10-04 stsp return 1;
3672 bd8de430 2019-10-04 stsp }
3673 bd8de430 2019-10-04 stsp }
3674 bd8de430 2019-10-04 stsp }
3675 bd8de430 2019-10-04 stsp
3676 6841da00 2019-08-08 stsp /*
3677 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3678 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3679 6841da00 2019-08-08 stsp * ignores backwards.
3680 6841da00 2019-08-08 stsp */
3681 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3682 6841da00 2019-08-08 stsp while (pe) {
3683 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3684 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3685 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3686 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3687 1b5d300f 2023-02-20 thomas int flags = FNM_LEADING_DIR;
3688 1b5d300f 2023-02-20 thomas if (strstr(pi->path, "/**/") == NULL)
3689 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3690 1b5d300f 2023-02-20 thomas if (match_path(pi->path, pi->path_len,
3691 1b5d300f 2023-02-20 thomas path, flags))
3692 6841da00 2019-08-08 stsp continue;
3693 6841da00 2019-08-08 stsp return 1;
3694 6841da00 2019-08-08 stsp }
3695 6841da00 2019-08-08 stsp }
3696 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3697 6841da00 2019-08-08 stsp }
3698 6841da00 2019-08-08 stsp
3699 6841da00 2019-08-08 stsp return 0;
3700 6841da00 2019-08-08 stsp }
3701 6841da00 2019-08-08 stsp
3702 6841da00 2019-08-08 stsp static const struct got_error *
3703 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3704 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3705 6841da00 2019-08-08 stsp {
3706 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3707 6841da00 2019-08-08 stsp char *ignorespath;
3708 886cec17 2019-12-15 stsp int fd = -1;
3709 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3710 6841da00 2019-08-08 stsp
3711 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3712 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3713 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3714 6841da00 2019-08-08 stsp
3715 886cec17 2019-12-15 stsp if (dirfd != -1) {
3716 fc63f50d 2021-12-31 thomas fd = openat(dirfd, ignores_filename,
3717 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3718 886cec17 2019-12-15 stsp if (fd == -1) {
3719 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3720 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3721 886cec17 2019-12-15 stsp ignorespath);
3722 886cec17 2019-12-15 stsp } else {
3723 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3724 b6b86fd1 2022-08-30 thomas if (ignoresfile == NULL)
3725 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3726 886cec17 2019-12-15 stsp ignorespath);
3727 886cec17 2019-12-15 stsp else {
3728 886cec17 2019-12-15 stsp fd = -1;
3729 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3730 886cec17 2019-12-15 stsp }
3731 886cec17 2019-12-15 stsp }
3732 886cec17 2019-12-15 stsp } else {
3733 c56c5d8a 2021-12-31 thomas ignoresfile = fopen(ignorespath, "re");
3734 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3735 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3736 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3737 886cec17 2019-12-15 stsp ignorespath);
3738 886cec17 2019-12-15 stsp } else
3739 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3740 886cec17 2019-12-15 stsp }
3741 6841da00 2019-08-08 stsp
3742 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3743 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3744 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3745 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3746 6841da00 2019-08-08 stsp free(ignorespath);
3747 6841da00 2019-08-08 stsp return err;
3748 f8d1f275 2019-02-04 stsp }
3749 f8d1f275 2019-02-04 stsp
3750 f8d1f275 2019-02-04 stsp static const struct got_error *
3751 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3752 6092c299 2021-10-04 thomas int dirfd)
3753 f8d1f275 2019-02-04 stsp {
3754 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3755 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3756 f8d1f275 2019-02-04 stsp char *path = NULL;
3757 6092c299 2021-10-04 thomas
3758 6092c299 2021-10-04 thomas if (ignore != NULL)
3759 6092c299 2021-10-04 thomas *ignore = 0;
3760 f8d1f275 2019-02-04 stsp
3761 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3762 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3763 0584f854 2019-04-06 stsp
3764 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3765 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3766 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3767 f8d1f275 2019-02-04 stsp } else {
3768 f8d1f275 2019-02-04 stsp path = de->d_name;
3769 f8d1f275 2019-02-04 stsp }
3770 f8d1f275 2019-02-04 stsp
3771 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3772 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3773 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3774 6092c299 2021-10-04 thomas *ignore = 1;
3775 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3776 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3777 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3778 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3779 f8d1f275 2019-02-04 stsp if (parent_path[0])
3780 f8d1f275 2019-02-04 stsp free(path);
3781 b72f483a 2019-02-05 stsp return err;
3782 f8d1f275 2019-02-04 stsp }
3783 f8d1f275 2019-02-04 stsp
3784 347d1d3e 2019-07-12 stsp static const struct got_error *
3785 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3786 3143d852 2020-06-25 stsp {
3787 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3788 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3789 3143d852 2020-06-25 stsp
3790 3143d852 2020-06-25 stsp if (a->no_ignores)
3791 3143d852 2020-06-25 stsp return NULL;
3792 3143d852 2020-06-25 stsp
3793 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3794 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3795 3143d852 2020-06-25 stsp if (err)
3796 3143d852 2020-06-25 stsp return err;
3797 3143d852 2020-06-25 stsp
3798 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3799 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3800 3143d852 2020-06-25 stsp
3801 3143d852 2020-06-25 stsp return err;
3802 3143d852 2020-06-25 stsp }
3803 3143d852 2020-06-25 stsp
3804 3143d852 2020-06-25 stsp static const struct got_error *
3805 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3806 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3807 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3808 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3809 abb4604f 2019-07-27 stsp {
3810 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3811 abb4604f 2019-07-27 stsp struct stat sb;
3812 ff56836b 2021-07-08 stsp
3813 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3814 abb4604f 2019-07-27 stsp if (ie)
3815 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3816 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3817 abb4604f 2019-07-27 stsp
3818 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3819 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3820 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3821 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3822 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3823 abb4604f 2019-07-27 stsp }
3824 abb4604f 2019-07-27 stsp
3825 fe1d8685 2022-02-12 thomas if (!no_ignores && match_ignores(ignores, path))
3826 fe1d8685 2022-02-12 thomas return NULL;
3827 fe1d8685 2022-02-12 thomas
3828 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3829 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3830 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3831 abb4604f 2019-07-27 stsp
3832 abb4604f 2019-07-27 stsp return NULL;
3833 3143d852 2020-06-25 stsp }
3834 3143d852 2020-06-25 stsp
3835 3143d852 2020-06-25 stsp static const struct got_error *
3836 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3837 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3838 3143d852 2020-06-25 stsp {
3839 3143d852 2020-06-25 stsp const struct got_error *err;
3840 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3841 3143d852 2020-06-25 stsp
3842 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3843 3143d852 2020-06-25 stsp ".cvsignore");
3844 3143d852 2020-06-25 stsp if (err)
3845 3143d852 2020-06-25 stsp return err;
3846 3143d852 2020-06-25 stsp
3847 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3848 3143d852 2020-06-25 stsp ".gitignore");
3849 3143d852 2020-06-25 stsp if (err)
3850 3143d852 2020-06-25 stsp return err;
3851 3143d852 2020-06-25 stsp
3852 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3853 3143d852 2020-06-25 stsp if (err) {
3854 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3855 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3856 3143d852 2020-06-25 stsp return err;
3857 3143d852 2020-06-25 stsp }
3858 3143d852 2020-06-25 stsp for (;;) {
3859 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3860 3143d852 2020-06-25 stsp ".cvsignore");
3861 3143d852 2020-06-25 stsp if (err)
3862 3143d852 2020-06-25 stsp break;
3863 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3864 3143d852 2020-06-25 stsp ".gitignore");
3865 3143d852 2020-06-25 stsp if (err)
3866 3143d852 2020-06-25 stsp break;
3867 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3868 3143d852 2020-06-25 stsp if (err) {
3869 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3870 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3871 3143d852 2020-06-25 stsp break;
3872 3143d852 2020-06-25 stsp }
3873 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3874 ff56836b 2021-07-08 stsp break;
3875 b737c85a 2020-06-26 stsp free(parent_path);
3876 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3877 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3878 3143d852 2020-06-25 stsp }
3879 3143d852 2020-06-25 stsp
3880 b737c85a 2020-06-26 stsp free(parent_path);
3881 b737c85a 2020-06-26 stsp free(next_parent_path);
3882 3143d852 2020-06-25 stsp return err;
3883 abb4604f 2019-07-27 stsp }
3884 f8836425 2023-04-14 thomas
3885 f8836425 2023-04-14 thomas struct find_missing_children_args {
3886 f8836425 2023-04-14 thomas const char *parent_path;
3887 f8836425 2023-04-14 thomas size_t parent_len;
3888 f8836425 2023-04-14 thomas struct got_pathlist_head *children;
3889 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb;
3890 f8836425 2023-04-14 thomas void *cancel_arg;
3891 f8836425 2023-04-14 thomas };
3892 f8836425 2023-04-14 thomas
3893 abb4604f 2019-07-27 stsp static const struct got_error *
3894 f8836425 2023-04-14 thomas find_missing_children(void *arg, struct got_fileindex_entry *ie)
3895 f8836425 2023-04-14 thomas {
3896 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3897 f8836425 2023-04-14 thomas struct find_missing_children_args *a = arg;
3898 f8836425 2023-04-14 thomas
3899 f8836425 2023-04-14 thomas if (a->cancel_cb) {
3900 f8836425 2023-04-14 thomas err = a->cancel_cb(a->cancel_arg);
3901 f8836425 2023-04-14 thomas if (err)
3902 f8836425 2023-04-14 thomas return err;
3903 f8836425 2023-04-14 thomas }
3904 f8836425 2023-04-14 thomas
3905 f8836425 2023-04-14 thomas if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3906 f8836425 2023-04-14 thomas err = got_pathlist_append(a->children, ie->path, NULL);
3907 f8836425 2023-04-14 thomas
3908 f8836425 2023-04-14 thomas return err;
3909 f8836425 2023-04-14 thomas }
3910 f8836425 2023-04-14 thomas
3911 f8836425 2023-04-14 thomas static const struct got_error *
3912 f8836425 2023-04-14 thomas report_children(struct got_pathlist_head *children,
3913 f8836425 2023-04-14 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
3914 f8836425 2023-04-14 thomas struct got_repository *repo, int is_root_dir, int report_unchanged,
3915 f8836425 2023-04-14 thomas struct got_pathlist_head *ignores, int no_ignores,
3916 f8836425 2023-04-14 thomas got_worktree_status_cb status_cb, void *status_arg,
3917 f8836425 2023-04-14 thomas got_cancel_cb cancel_cb, void *cancel_arg)
3918 f8836425 2023-04-14 thomas {
3919 f8836425 2023-04-14 thomas const struct got_error *err = NULL;
3920 f8836425 2023-04-14 thomas struct got_pathlist_entry *pe;
3921 f8836425 2023-04-14 thomas char *ondisk_path = NULL;
3922 f8836425 2023-04-14 thomas
3923 f8836425 2023-04-14 thomas TAILQ_FOREACH(pe, children, entry) {
3924 f8836425 2023-04-14 thomas if (cancel_cb) {
3925 f8836425 2023-04-14 thomas err = cancel_cb(cancel_arg);
3926 f8836425 2023-04-14 thomas if (err)
3927 f8836425 2023-04-14 thomas break;
3928 f8836425 2023-04-14 thomas }
3929 f8836425 2023-04-14 thomas
3930 f8836425 2023-04-14 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3931 f8836425 2023-04-14 thomas !is_root_dir ? "/" : "", pe->path) == -1) {
3932 f8836425 2023-04-14 thomas err = got_error_from_errno("asprintf");
3933 f8836425 2023-04-14 thomas ondisk_path = NULL;
3934 f8836425 2023-04-14 thomas break;
3935 f8836425 2023-04-14 thomas }
3936 f8836425 2023-04-14 thomas
3937 f8836425 2023-04-14 thomas err = report_single_file_status(pe->path, ondisk_path,
3938 f8836425 2023-04-14 thomas fileindex, status_cb, status_arg, repo, report_unchanged,
3939 f8836425 2023-04-14 thomas ignores, no_ignores);
3940 f8836425 2023-04-14 thomas if (err)
3941 f8836425 2023-04-14 thomas break;
3942 f8836425 2023-04-14 thomas
3943 f8836425 2023-04-14 thomas free(ondisk_path);
3944 f8836425 2023-04-14 thomas ondisk_path = NULL;
3945 f8836425 2023-04-14 thomas }
3946 f8836425 2023-04-14 thomas
3947 f8836425 2023-04-14 thomas free(ondisk_path);
3948 f8836425 2023-04-14 thomas return err;
3949 f8836425 2023-04-14 thomas }
3950 f8836425 2023-04-14 thomas
3951 f8836425 2023-04-14 thomas static const struct got_error *
3952 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3953 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3954 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3955 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3956 f2a9dc41 2019-12-13 tracey int report_unchanged)
3957 f8d1f275 2019-02-04 stsp {
3958 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3959 6fc93f37 2019-12-13 stsp int fd = -1;
3960 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3961 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3962 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3963 f8836425 2023-04-14 thomas struct got_pathlist_head ignores, missing_children;
3964 a78810f8 2022-03-13 thomas struct got_fileindex_entry *ie;
3965 3143d852 2020-06-25 stsp
3966 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3967 f8836425 2023-04-14 thomas TAILQ_INIT(&missing_children);
3968 f8d1f275 2019-02-04 stsp
3969 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3970 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3971 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3972 8dc303cc 2019-07-27 stsp
3973 a78810f8 2022-03-13 thomas ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3974 a78810f8 2022-03-13 thomas if (ie) {
3975 a78810f8 2022-03-13 thomas err = report_single_file_status(path, ondisk_path,
3976 a78810f8 2022-03-13 thomas fileindex, status_cb, status_arg, repo,
3977 a78810f8 2022-03-13 thomas report_unchanged, &ignores, no_ignores);
3978 a78810f8 2022-03-13 thomas goto done;
3979 f8836425 2023-04-14 thomas } else {
3980 f8836425 2023-04-14 thomas struct find_missing_children_args fmca;
3981 f8836425 2023-04-14 thomas fmca.parent_path = path;
3982 f8836425 2023-04-14 thomas fmca.parent_len = strlen(path);
3983 f8836425 2023-04-14 thomas fmca.children = &missing_children;
3984 f8836425 2023-04-14 thomas fmca.cancel_cb = cancel_cb;
3985 f8836425 2023-04-14 thomas fmca.cancel_arg = cancel_arg;
3986 f8836425 2023-04-14 thomas err = got_fileindex_for_each_entry_safe(fileindex,
3987 f8836425 2023-04-14 thomas find_missing_children, &fmca);
3988 f8836425 2023-04-14 thomas if (err)
3989 f8836425 2023-04-14 thomas goto done;
3990 a78810f8 2022-03-13 thomas }
3991 a78810f8 2022-03-13 thomas
3992 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3993 6fc93f37 2019-12-13 stsp if (fd == -1) {
3994 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3995 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3996 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3997 ff56836b 2021-07-08 stsp else {
3998 ff56836b 2021-07-08 stsp if (!no_ignores) {
3999 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4000 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4001 ff56836b 2021-07-08 stsp if (err)
4002 ff56836b 2021-07-08 stsp goto done;
4003 ff56836b 2021-07-08 stsp }
4004 f8836425 2023-04-14 thomas if (TAILQ_EMPTY(&missing_children)) {
4005 f8836425 2023-04-14 thomas err = report_single_file_status(path,
4006 f8836425 2023-04-14 thomas ondisk_path, fileindex,
4007 f8836425 2023-04-14 thomas status_cb, status_arg, repo,
4008 f8836425 2023-04-14 thomas report_unchanged, &ignores, no_ignores);
4009 f8836425 2023-04-14 thomas if (err)
4010 f8836425 2023-04-14 thomas goto done;
4011 f8836425 2023-04-14 thomas } else {
4012 f8836425 2023-04-14 thomas err = report_children(&missing_children,
4013 f8836425 2023-04-14 thomas worktree, fileindex, repo,
4014 f8836425 2023-04-14 thomas (path[0] == '\0'), report_unchanged,
4015 f8836425 2023-04-14 thomas &ignores, no_ignores,
4016 f8836425 2023-04-14 thomas status_cb, status_arg,
4017 f8836425 2023-04-14 thomas cancel_cb, cancel_arg);
4018 6f97aa83 2023-04-14 thomas if (err)
4019 6f97aa83 2023-04-14 thomas goto done;
4020 f8836425 2023-04-14 thomas }
4021 ff56836b 2021-07-08 stsp }
4022 abb4604f 2019-07-27 stsp } else {
4023 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4024 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4025 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4026 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4027 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4028 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4029 abb4604f 2019-07-27 stsp arg.status_path = path;
4030 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4031 abb4604f 2019-07-27 stsp arg.repo = repo;
4032 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4033 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4034 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4035 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4036 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4037 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4038 022fae89 2019-12-06 tracey if (!no_ignores) {
4039 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4040 3143d852 2020-06-25 stsp worktree->root_path, path);
4041 3143d852 2020-06-25 stsp if (err)
4042 3143d852 2020-06-25 stsp goto done;
4043 022fae89 2019-12-06 tracey }
4044 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4045 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4046 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4047 927df6b7 2019-02-10 stsp }
4048 3143d852 2020-06-25 stsp done:
4049 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4050 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4051 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4052 927df6b7 2019-02-10 stsp free(ondisk_path);
4053 347d1d3e 2019-07-12 stsp return err;
4054 347d1d3e 2019-07-12 stsp }
4055 347d1d3e 2019-07-12 stsp
4056 347d1d3e 2019-07-12 stsp const struct got_error *
4057 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4058 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4059 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4060 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4061 347d1d3e 2019-07-12 stsp {
4062 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4063 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4064 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4065 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4066 347d1d3e 2019-07-12 stsp
4067 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4068 347d1d3e 2019-07-12 stsp if (err)
4069 347d1d3e 2019-07-12 stsp return err;
4070 347d1d3e 2019-07-12 stsp
4071 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4072 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4073 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4074 f6343036 2021-06-22 stsp no_ignores, 0);
4075 72ea6654 2019-07-27 stsp if (err)
4076 72ea6654 2019-07-27 stsp break;
4077 72ea6654 2019-07-27 stsp }
4078 f8d1f275 2019-02-04 stsp free(fileindex_path);
4079 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4080 f8d1f275 2019-02-04 stsp return err;
4081 f8d1f275 2019-02-04 stsp }
4082 6c7ab921 2019-03-18 stsp
4083 6c7ab921 2019-03-18 stsp const struct got_error *
4084 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4085 6c7ab921 2019-03-18 stsp const char *arg)
4086 6c7ab921 2019-03-18 stsp {
4087 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4088 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4089 6c7ab921 2019-03-18 stsp size_t len;
4090 00bb5ea0 2020-07-23 stsp struct stat sb;
4091 01740607 2020-11-04 stsp char *abspath = NULL;
4092 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4093 6c7ab921 2019-03-18 stsp
4094 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4095 6c7ab921 2019-03-18 stsp
4096 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4097 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4098 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4099 00bb5ea0 2020-07-23 stsp
4100 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4101 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4102 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4103 00bb5ea0 2020-07-23 stsp goto done;
4104 00bb5ea0 2020-07-23 stsp }
4105 727173c3 2020-11-06 stsp sb.st_mode = 0;
4106 00bb5ea0 2020-07-23 stsp }
4107 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4108 00bb5ea0 2020-07-23 stsp /*
4109 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4110 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4111 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4112 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4113 00bb5ea0 2020-07-23 stsp */
4114 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4115 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4116 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4117 00bb5ea0 2020-07-23 stsp goto done;
4118 00bb5ea0 2020-07-23 stsp }
4119 00bb5ea0 2020-07-23 stsp
4120 00bb5ea0 2020-07-23 stsp }
4121 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4122 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4123 00bb5ea0 2020-07-23 stsp if (err)
4124 d0710d08 2019-07-22 stsp goto done;
4125 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4126 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4127 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4128 00bb5ea0 2020-07-23 stsp goto done;
4129 00bb5ea0 2020-07-23 stsp }
4130 00bb5ea0 2020-07-23 stsp } else {
4131 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4132 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4133 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4134 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4135 00bb5ea0 2020-07-23 stsp goto done;
4136 00bb5ea0 2020-07-23 stsp }
4137 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4138 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4139 01740607 2020-11-04 stsp goto done;
4140 01740607 2020-11-04 stsp }
4141 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4142 01740607 2020-11-04 stsp sizeof(canonpath));
4143 01740607 2020-11-04 stsp if (err)
4144 00bb5ea0 2020-07-23 stsp goto done;
4145 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4146 01740607 2020-11-04 stsp if (resolved == NULL) {
4147 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4148 01740607 2020-11-04 stsp goto done;
4149 00bb5ea0 2020-07-23 stsp }
4150 d0710d08 2019-07-22 stsp }
4151 d0710d08 2019-07-22 stsp }
4152 6c7ab921 2019-03-18 stsp
4153 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4154 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4155 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4156 6c7ab921 2019-03-18 stsp goto done;
4157 6c7ab921 2019-03-18 stsp }
4158 6c7ab921 2019-03-18 stsp
4159 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4160 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4161 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4162 f6d88e1a 2019-05-29 stsp if (err)
4163 f6d88e1a 2019-05-29 stsp goto done;
4164 f6d88e1a 2019-05-29 stsp } else {
4165 f6d88e1a 2019-05-29 stsp path = strdup("");
4166 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4167 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4168 f6d88e1a 2019-05-29 stsp goto done;
4169 f6d88e1a 2019-05-29 stsp }
4170 6c7ab921 2019-03-18 stsp }
4171 6c7ab921 2019-03-18 stsp
4172 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4173 6c7ab921 2019-03-18 stsp len = strlen(path);
4174 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4175 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4176 6c7ab921 2019-03-18 stsp len--;
4177 6c7ab921 2019-03-18 stsp }
4178 6c7ab921 2019-03-18 stsp done:
4179 01740607 2020-11-04 stsp free(abspath);
4180 6c7ab921 2019-03-18 stsp free(resolved);
4181 d0710d08 2019-07-22 stsp free(cwd);
4182 6c7ab921 2019-03-18 stsp if (err == NULL)
4183 6c7ab921 2019-03-18 stsp *wt_path = path;
4184 6c7ab921 2019-03-18 stsp else
4185 6c7ab921 2019-03-18 stsp free(path);
4186 d00136be 2019-03-26 stsp return err;
4187 d00136be 2019-03-26 stsp }
4188 d00136be 2019-03-26 stsp
4189 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4190 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4191 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4192 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4193 4e68cba3 2019-11-23 stsp void *progress_arg;
4194 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4195 4e68cba3 2019-11-23 stsp };
4196 4e68cba3 2019-11-23 stsp
4197 4e68cba3 2019-11-23 stsp static const struct got_error *
4198 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4199 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4200 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4201 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4202 07f5b47a 2019-06-02 stsp {
4203 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4204 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4205 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4206 6d022e97 2019-08-04 stsp struct stat sb;
4207 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4208 07f5b47a 2019-06-02 stsp
4209 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4210 dbb83fbd 2019-12-12 stsp relpath) == -1)
4211 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4212 dbb83fbd 2019-12-12 stsp
4213 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4214 6d022e97 2019-08-04 stsp if (ie) {
4215 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4216 12463d8b 2019-12-13 stsp de_name, a->repo);
4217 6d022e97 2019-08-04 stsp if (err)
4218 dbb83fbd 2019-12-12 stsp goto done;
4219 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4220 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4221 dbb83fbd 2019-12-12 stsp goto done;
4222 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4223 dbb83fbd 2019-12-12 stsp if (err)
4224 dbb83fbd 2019-12-12 stsp goto done;
4225 6d022e97 2019-08-04 stsp }
4226 07f5b47a 2019-06-02 stsp
4227 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4228 84bf00a6 2022-02-12 thomas if (status == GOT_STATUS_NONEXISTENT)
4229 84bf00a6 2022-02-12 thomas err = got_error_set_errno(ENOENT, ondisk_path);
4230 84bf00a6 2022-02-12 thomas else
4231 84bf00a6 2022-02-12 thomas err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4232 dbb83fbd 2019-12-12 stsp goto done;
4233 4e68cba3 2019-11-23 stsp }
4234 07f5b47a 2019-06-02 stsp
4235 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4236 4e68cba3 2019-11-23 stsp if (err)
4237 4e68cba3 2019-11-23 stsp goto done;
4238 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4239 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4240 3969253a 2020-03-07 stsp if (err) {
4241 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4242 3969253a 2020-03-07 stsp goto done;
4243 3969253a 2020-03-07 stsp }
4244 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4245 07f5b47a 2019-06-02 stsp if (err) {
4246 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4247 4e68cba3 2019-11-23 stsp goto done;
4248 07f5b47a 2019-06-02 stsp }
4249 4e68cba3 2019-11-23 stsp done:
4250 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4251 dbb83fbd 2019-12-12 stsp if (err)
4252 dbb83fbd 2019-12-12 stsp return err;
4253 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4254 dbb83fbd 2019-12-12 stsp return NULL;
4255 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4256 07f5b47a 2019-06-02 stsp }
4257 07f5b47a 2019-06-02 stsp
4258 d00136be 2019-03-26 stsp const struct got_error *
4259 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4260 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4261 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4262 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4263 d00136be 2019-03-26 stsp {
4264 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4265 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4266 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4267 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4268 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4269 d00136be 2019-03-26 stsp
4270 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4271 d00136be 2019-03-26 stsp if (err)
4272 d00136be 2019-03-26 stsp return err;
4273 d00136be 2019-03-26 stsp
4274 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4275 d00136be 2019-03-26 stsp if (err)
4276 d00136be 2019-03-26 stsp goto done;
4277 d00136be 2019-03-26 stsp
4278 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4279 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4280 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4281 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4282 4e68cba3 2019-11-23 stsp saa.repo = repo;
4283 4e68cba3 2019-11-23 stsp
4284 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4285 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4286 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4287 1dd54920 2019-05-11 stsp if (err)
4288 af12c6b9 2019-06-04 stsp break;
4289 1dd54920 2019-05-11 stsp }
4290 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4291 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4292 af12c6b9 2019-06-04 stsp err = sync_err;
4293 d00136be 2019-03-26 stsp done:
4294 fb399478 2019-07-12 stsp free(fileindex_path);
4295 d00136be 2019-03-26 stsp if (fileindex)
4296 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4297 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4298 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4299 d00136be 2019-03-26 stsp err = unlockerr;
4300 6c7ab921 2019-03-18 stsp return err;
4301 6c7ab921 2019-03-18 stsp }
4302 17ed4618 2019-06-02 stsp
4303 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4304 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4305 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4306 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4307 f2a9dc41 2019-12-13 tracey void *progress_arg;
4308 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4309 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4310 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4311 d64cc78a 2022-01-25 thomas int ignore_missing_paths;
4312 766841c2 2020-08-13 stsp const char *status_codes;
4313 f2a9dc41 2019-12-13 tracey };
4314 f2a9dc41 2019-12-13 tracey
4315 f2a9dc41 2019-12-13 tracey static const struct got_error *
4316 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4317 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4318 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4319 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4320 17ed4618 2019-06-02 stsp {
4321 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4322 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4323 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4324 17ed4618 2019-06-02 stsp struct stat sb;
4325 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4326 d64cc78a 2022-01-25 thomas
4327 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_NONEXISTENT) {
4328 d64cc78a 2022-01-25 thomas if (a->ignore_missing_paths)
4329 d64cc78a 2022-01-25 thomas return NULL;
4330 d64cc78a 2022-01-25 thomas return got_error_set_errno(ENOENT, relpath);
4331 d64cc78a 2022-01-25 thomas }
4332 17ed4618 2019-06-02 stsp
4333 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4334 17ed4618 2019-06-02 stsp if (ie == NULL)
4335 d5a18ace 2022-01-25 thomas return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4336 2ec1f75b 2019-03-26 stsp
4337 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4338 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4339 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4340 9acbc4fa 2019-08-03 stsp return NULL;
4341 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4342 9acbc4fa 2019-08-03 stsp }
4343 9acbc4fa 2019-08-03 stsp
4344 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4345 f2a9dc41 2019-12-13 tracey relpath) == -1)
4346 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4347 f2a9dc41 2019-12-13 tracey
4348 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4349 12463d8b 2019-12-13 stsp a->repo);
4350 17ed4618 2019-06-02 stsp if (err)
4351 f2a9dc41 2019-12-13 tracey goto done;
4352 17ed4618 2019-06-02 stsp
4353 766841c2 2020-08-13 stsp if (a->status_codes) {
4354 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4355 766841c2 2020-08-13 stsp int i;
4356 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4357 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4358 766841c2 2020-08-13 stsp break;
4359 766841c2 2020-08-13 stsp }
4360 b6b86fd1 2022-08-30 thomas if (i == ncodes) {
4361 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4362 766841c2 2020-08-13 stsp free(ondisk_path);
4363 766841c2 2020-08-13 stsp return NULL;
4364 766841c2 2020-08-13 stsp }
4365 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4366 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4367 766841c2 2020-08-13 stsp static char msg[64];
4368 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4369 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4370 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4371 766841c2 2020-08-13 stsp goto done;
4372 766841c2 2020-08-13 stsp }
4373 766841c2 2020-08-13 stsp }
4374 766841c2 2020-08-13 stsp
4375 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4376 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4377 f2a9dc41 2019-12-13 tracey goto done;
4378 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4379 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4380 f2a9dc41 2019-12-13 tracey goto done;
4381 f2a9dc41 2019-12-13 tracey }
4382 d64cc78a 2022-01-25 thomas if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4383 d64cc78a 2022-01-25 thomas err = got_error_set_errno(ENOENT, relpath);
4384 d64cc78a 2022-01-25 thomas goto done;
4385 d64cc78a 2022-01-25 thomas }
4386 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4387 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4388 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4389 f2a9dc41 2019-12-13 tracey goto done;
4390 f2a9dc41 2019-12-13 tracey }
4391 17ed4618 2019-06-02 stsp }
4392 17ed4618 2019-06-02 stsp
4393 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4394 20a2ad1c 2020-10-20 stsp size_t root_len;
4395 20a2ad1c 2020-10-20 stsp
4396 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4397 e6b88e16 2022-10-16 thomas if (unlinkat(dirfd, de_name, 0) == -1) {
4398 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4399 12463d8b 2019-12-13 stsp ondisk_path);
4400 12463d8b 2019-12-13 stsp goto done;
4401 12463d8b 2019-12-13 stsp }
4402 e6b88e16 2022-10-16 thomas } else if (unlink(ondisk_path) == -1) {
4403 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4404 12463d8b 2019-12-13 stsp goto done;
4405 15341bfd 2020-03-05 tracey }
4406 15341bfd 2020-03-05 tracey
4407 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4408 20a2ad1c 2020-10-20 stsp do {
4409 20a2ad1c 2020-10-20 stsp char *parent;
4410 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4411 20a2ad1c 2020-10-20 stsp if (err)
4412 2513f20a 2020-10-20 stsp goto done;
4413 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4414 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4415 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4416 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4417 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4418 20a2ad1c 2020-10-20 stsp ondisk_path);
4419 15341bfd 2020-03-05 tracey break;
4420 15341bfd 2020-03-05 tracey }
4421 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4422 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4423 f2a9dc41 2019-12-13 tracey }
4424 17ed4618 2019-06-02 stsp
4425 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4426 f2a9dc41 2019-12-13 tracey done:
4427 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4428 f2a9dc41 2019-12-13 tracey if (err)
4429 f2a9dc41 2019-12-13 tracey return err;
4430 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4431 f2a9dc41 2019-12-13 tracey return NULL;
4432 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4433 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4434 17ed4618 2019-06-02 stsp }
4435 17ed4618 2019-06-02 stsp
4436 2ec1f75b 2019-03-26 stsp const struct got_error *
4437 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4438 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4439 766841c2 2020-08-13 stsp const char *status_codes,
4440 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4441 d64cc78a 2022-01-25 thomas struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4442 2ec1f75b 2019-03-26 stsp {
4443 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4444 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4445 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4446 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4447 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4448 2ec1f75b 2019-03-26 stsp
4449 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4450 2ec1f75b 2019-03-26 stsp if (err)
4451 2ec1f75b 2019-03-26 stsp return err;
4452 2ec1f75b 2019-03-26 stsp
4453 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4454 2ec1f75b 2019-03-26 stsp if (err)
4455 2ec1f75b 2019-03-26 stsp goto done;
4456 f2a9dc41 2019-12-13 tracey
4457 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4458 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4459 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4460 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4461 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4462 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4463 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4464 d64cc78a 2022-01-25 thomas sda.ignore_missing_paths = ignore_missing_paths;
4465 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4466 2ec1f75b 2019-03-26 stsp
4467 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4468 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4469 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4470 17ed4618 2019-06-02 stsp if (err)
4471 af12c6b9 2019-06-04 stsp break;
4472 2ec1f75b 2019-03-26 stsp }
4473 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4474 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4475 af12c6b9 2019-06-04 stsp err = sync_err;
4476 2ec1f75b 2019-03-26 stsp done:
4477 fb399478 2019-07-12 stsp free(fileindex_path);
4478 2ec1f75b 2019-03-26 stsp if (fileindex)
4479 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4480 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4481 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4482 2ec1f75b 2019-03-26 stsp err = unlockerr;
4483 33aa809d 2019-08-08 stsp return err;
4484 33aa809d 2019-08-08 stsp }
4485 33aa809d 2019-08-08 stsp
4486 33aa809d 2019-08-08 stsp static const struct got_error *
4487 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4488 33aa809d 2019-08-08 stsp {
4489 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4490 33aa809d 2019-08-08 stsp char *line = NULL;
4491 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4492 33aa809d 2019-08-08 stsp ssize_t linelen;
4493 33aa809d 2019-08-08 stsp
4494 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4495 33aa809d 2019-08-08 stsp if (linelen == -1) {
4496 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4497 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4498 33aa809d 2019-08-08 stsp goto done;
4499 33aa809d 2019-08-08 stsp }
4500 33aa809d 2019-08-08 stsp return NULL;
4501 33aa809d 2019-08-08 stsp }
4502 33aa809d 2019-08-08 stsp if (outfile) {
4503 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4504 33aa809d 2019-08-08 stsp if (n != linelen) {
4505 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4506 33aa809d 2019-08-08 stsp goto done;
4507 33aa809d 2019-08-08 stsp }
4508 33aa809d 2019-08-08 stsp }
4509 33aa809d 2019-08-08 stsp if (rejectfile) {
4510 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4511 33aa809d 2019-08-08 stsp if (n != linelen)
4512 48a59395 2023-01-14 thomas err = got_ferror(rejectfile, GOT_ERR_IO);
4513 33aa809d 2019-08-08 stsp }
4514 33aa809d 2019-08-08 stsp done:
4515 33aa809d 2019-08-08 stsp free(line);
4516 2ec1f75b 2019-03-26 stsp return err;
4517 2ec1f75b 2019-03-26 stsp }
4518 1f1abb7e 2019-08-08 stsp
4519 33aa809d 2019-08-08 stsp static const struct got_error *
4520 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4521 33aa809d 2019-08-08 stsp {
4522 33aa809d 2019-08-08 stsp char *line = NULL;
4523 33aa809d 2019-08-08 stsp size_t linesize = 0;
4524 33aa809d 2019-08-08 stsp ssize_t linelen;
4525 33aa809d 2019-08-08 stsp
4526 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4527 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4528 500467ff 2019-09-25 hiltjo if (ferror(f))
4529 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4530 500467ff 2019-09-25 hiltjo return NULL;
4531 500467ff 2019-09-25 hiltjo }
4532 33aa809d 2019-08-08 stsp free(line);
4533 33aa809d 2019-08-08 stsp return NULL;
4534 33aa809d 2019-08-08 stsp }
4535 33aa809d 2019-08-08 stsp
4536 33aa809d 2019-08-08 stsp static const struct got_error *
4537 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4538 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4539 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4540 abc59930 2021-09-05 naddy {
4541 33aa809d 2019-08-08 stsp const struct got_error *err;
4542 33aa809d 2019-08-08 stsp
4543 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4544 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4545 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4546 33aa809d 2019-08-08 stsp if (err)
4547 33aa809d 2019-08-08 stsp return err;
4548 33aa809d 2019-08-08 stsp (*line_cur1)++;
4549 33aa809d 2019-08-08 stsp }
4550 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4551 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4552 33aa809d 2019-08-08 stsp if (rejectfile)
4553 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4554 33aa809d 2019-08-08 stsp else
4555 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4556 33aa809d 2019-08-08 stsp if (err)
4557 33aa809d 2019-08-08 stsp return err;
4558 33aa809d 2019-08-08 stsp (*line_cur2)++;
4559 33aa809d 2019-08-08 stsp }
4560 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4561 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4562 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4563 33aa809d 2019-08-08 stsp if (err)
4564 33aa809d 2019-08-08 stsp return err;
4565 33aa809d 2019-08-08 stsp (*line_cur2)++;
4566 33aa809d 2019-08-08 stsp }
4567 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4568 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4569 33aa809d 2019-08-08 stsp if (rejectfile)
4570 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4571 33aa809d 2019-08-08 stsp else
4572 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4573 33aa809d 2019-08-08 stsp if (err)
4574 33aa809d 2019-08-08 stsp return err;
4575 33aa809d 2019-08-08 stsp (*line_cur1)++;
4576 33aa809d 2019-08-08 stsp }
4577 f1e81a05 2019-08-10 stsp
4578 f1e81a05 2019-08-10 stsp return NULL;
4579 f1e81a05 2019-08-10 stsp }
4580 f1e81a05 2019-08-10 stsp
4581 f1e81a05 2019-08-10 stsp static const struct got_error *
4582 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4583 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4584 f1e81a05 2019-08-10 stsp {
4585 f1e81a05 2019-08-10 stsp const struct got_error *err;
4586 f1e81a05 2019-08-10 stsp
4587 f1e81a05 2019-08-10 stsp if (outfile) {
4588 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4589 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4590 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4591 f1e81a05 2019-08-10 stsp if (err)
4592 f1e81a05 2019-08-10 stsp return err;
4593 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4594 f1e81a05 2019-08-10 stsp }
4595 f1e81a05 2019-08-10 stsp }
4596 f1e81a05 2019-08-10 stsp if (rejectfile) {
4597 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4598 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4599 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4600 f1e81a05 2019-08-10 stsp if (err)
4601 f1e81a05 2019-08-10 stsp return err;
4602 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4603 f1e81a05 2019-08-10 stsp }
4604 33aa809d 2019-08-08 stsp }
4605 33aa809d 2019-08-08 stsp
4606 33aa809d 2019-08-08 stsp return NULL;
4607 33aa809d 2019-08-08 stsp }
4608 33aa809d 2019-08-08 stsp
4609 33aa809d 2019-08-08 stsp static const struct got_error *
4610 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4611 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4612 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4613 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4614 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4615 33aa809d 2019-08-08 stsp {
4616 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4617 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4618 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4619 33aa809d 2019-08-08 stsp FILE *hunkfile;
4620 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4621 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4622 fe621944 2020-11-10 stsp int rc;
4623 33aa809d 2019-08-08 stsp
4624 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4625 33aa809d 2019-08-08 stsp
4626 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4627 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4628 fe621944 2020-11-10 stsp start_old = cc.left.start;
4629 fe621944 2020-11-10 stsp end_old = cc.left.end;
4630 fe621944 2020-11-10 stsp start_new = cc.right.start;
4631 fe621944 2020-11-10 stsp end_new = cc.right.end;
4632 33aa809d 2019-08-08 stsp
4633 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4634 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4635 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4636 33aa809d 2019-08-08 stsp
4637 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4638 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4639 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4640 33aa809d 2019-08-08 stsp
4641 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4642 fe621944 2020-11-10 stsp if (diff_state == NULL)
4643 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4644 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4645 fe621944 2020-11-10 stsp
4646 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4647 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4648 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4649 33aa809d 2019-08-08 stsp goto done;
4650 33aa809d 2019-08-08 stsp }
4651 fe621944 2020-11-10 stsp
4652 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4653 fe621944 2020-11-10 stsp diff_result, &cc);
4654 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4655 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4656 33aa809d 2019-08-08 stsp goto done;
4657 33aa809d 2019-08-08 stsp }
4658 fe621944 2020-11-10 stsp
4659 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4660 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4661 33aa809d 2019-08-08 stsp goto done;
4662 33aa809d 2019-08-08 stsp }
4663 33aa809d 2019-08-08 stsp
4664 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4665 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4666 33aa809d 2019-08-08 stsp if (err)
4667 33aa809d 2019-08-08 stsp goto done;
4668 33aa809d 2019-08-08 stsp
4669 33aa809d 2019-08-08 stsp switch (*choice) {
4670 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4671 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4672 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4673 33aa809d 2019-08-08 stsp break;
4674 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4675 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4676 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4677 33aa809d 2019-08-08 stsp break;
4678 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4679 33aa809d 2019-08-08 stsp break;
4680 33aa809d 2019-08-08 stsp default:
4681 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4682 33aa809d 2019-08-08 stsp break;
4683 33aa809d 2019-08-08 stsp }
4684 33aa809d 2019-08-08 stsp done:
4685 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4686 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4687 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4688 33aa809d 2019-08-08 stsp return err;
4689 33aa809d 2019-08-08 stsp }
4690 33aa809d 2019-08-08 stsp
4691 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4692 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4693 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4694 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4695 1f1abb7e 2019-08-08 stsp void *progress_arg;
4696 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4697 33aa809d 2019-08-08 stsp void *patch_arg;
4698 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4699 10604dce 2021-09-24 thomas int unlink_added_files;
4700 8641a332 2023-04-14 thomas struct got_pathlist_head *added_files_to_unlink;
4701 1f1abb7e 2019-08-08 stsp };
4702 a129376b 2019-03-28 stsp
4703 e20a8b6f 2019-06-04 stsp static const struct got_error *
4704 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4705 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4706 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4707 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4708 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4709 33aa809d 2019-08-08 stsp {
4710 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4711 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4712 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4713 f4ae6ddb 2022-07-01 thomas int fd = -1, fd2 = -1;
4714 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4715 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4716 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4717 fe621944 2020-11-10 stsp struct stat sb2;
4718 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4719 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4720 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4721 33aa809d 2019-08-08 stsp
4722 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4723 33aa809d 2019-08-08 stsp
4724 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4725 33aa809d 2019-08-08 stsp if (err)
4726 33aa809d 2019-08-08 stsp return err;
4727 33aa809d 2019-08-08 stsp
4728 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4729 fc63f50d 2021-12-31 thomas fd2 = openat(dirfd2, de_name2,
4730 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4731 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4732 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4733 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4734 fa3cef63 2020-07-23 stsp goto done;
4735 fa3cef63 2020-07-23 stsp }
4736 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4737 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4738 48b4f239 2021-12-31 thomas if (link_len == -1) {
4739 48b4f239 2021-12-31 thomas return got_error_from_errno2("readlinkat",
4740 48b4f239 2021-12-31 thomas path2);
4741 48b4f239 2021-12-31 thomas }
4742 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4743 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4744 12463d8b 2019-12-13 stsp }
4745 12463d8b 2019-12-13 stsp } else {
4746 06340621 2021-12-31 thomas fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4747 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4748 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4749 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4750 fa3cef63 2020-07-23 stsp goto done;
4751 fa3cef63 2020-07-23 stsp }
4752 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4753 fa3cef63 2020-07-23 stsp sizeof(link_target));
4754 fa3cef63 2020-07-23 stsp if (link_len == -1)
4755 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4756 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4757 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4758 12463d8b 2019-12-13 stsp }
4759 1ebedb77 2019-10-19 stsp }
4760 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4761 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4762 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4763 fa3cef63 2020-07-23 stsp goto done;
4764 fa3cef63 2020-07-23 stsp }
4765 1ebedb77 2019-10-19 stsp
4766 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4767 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4768 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4769 fa3cef63 2020-07-23 stsp goto done;
4770 fa3cef63 2020-07-23 stsp }
4771 fa3cef63 2020-07-23 stsp fd2 = -1;
4772 fa3cef63 2020-07-23 stsp } else {
4773 fa3cef63 2020-07-23 stsp size_t n;
4774 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4775 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4776 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4777 fa3cef63 2020-07-23 stsp goto done;
4778 fa3cef63 2020-07-23 stsp }
4779 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4780 fa3cef63 2020-07-23 stsp if (n != link_len) {
4781 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4782 fa3cef63 2020-07-23 stsp goto done;
4783 fa3cef63 2020-07-23 stsp }
4784 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4785 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4786 fa3cef63 2020-07-23 stsp goto done;
4787 fa3cef63 2020-07-23 stsp }
4788 fa3cef63 2020-07-23 stsp rewind(f2);
4789 33aa809d 2019-08-08 stsp }
4790 33aa809d 2019-08-08 stsp
4791 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
4792 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
4793 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4794 f4ae6ddb 2022-07-01 thomas goto done;
4795 f4ae6ddb 2022-07-01 thomas }
4796 f4ae6ddb 2022-07-01 thomas
4797 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4798 33aa809d 2019-08-08 stsp if (err)
4799 33aa809d 2019-08-08 stsp goto done;
4800 33aa809d 2019-08-08 stsp
4801 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4802 33aa809d 2019-08-08 stsp if (err)
4803 33aa809d 2019-08-08 stsp goto done;
4804 33aa809d 2019-08-08 stsp
4805 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4806 33aa809d 2019-08-08 stsp if (err)
4807 33aa809d 2019-08-08 stsp goto done;
4808 33aa809d 2019-08-08 stsp
4809 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4810 25ec7006 2022-07-01 thomas 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4811 33aa809d 2019-08-08 stsp if (err)
4812 33aa809d 2019-08-08 stsp goto done;
4813 33aa809d 2019-08-08 stsp
4814 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4815 fc2a50f2 2022-11-01 thomas "");
4816 33aa809d 2019-08-08 stsp if (err)
4817 33aa809d 2019-08-08 stsp goto done;
4818 33aa809d 2019-08-08 stsp
4819 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4820 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4821 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4822 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4823 fe621944 2020-11-10 stsp
4824 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4825 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4826 b6b86fd1 2022-08-30 thomas struct diff_chunk_context cc = {};
4827 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4828 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4829 fe621944 2020-11-10 stsp nchanges++;
4830 fe621944 2020-11-10 stsp }
4831 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4832 33aa809d 2019-08-08 stsp int choice;
4833 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4834 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4835 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4836 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4837 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4838 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4839 33aa809d 2019-08-08 stsp if (err)
4840 33aa809d 2019-08-08 stsp goto done;
4841 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4842 33aa809d 2019-08-08 stsp have_content = 1;
4843 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4844 33aa809d 2019-08-08 stsp break;
4845 33aa809d 2019-08-08 stsp }
4846 1ebedb77 2019-10-19 stsp if (have_content) {
4847 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4848 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4849 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4850 1ebedb77 2019-10-19 stsp if (err)
4851 1ebedb77 2019-10-19 stsp goto done;
4852 1ebedb77 2019-10-19 stsp
4853 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4854 a2c162eb 2022-10-30 thomas mode_t mode;
4855 a2c162eb 2022-10-30 thomas
4856 a2c162eb 2022-10-30 thomas mode = apply_umask(sb2.st_mode);
4857 a2c162eb 2022-10-30 thomas if (fchmod(fileno(outfile), mode) == -1) {
4858 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4859 fa3cef63 2020-07-23 stsp goto done;
4860 fa3cef63 2020-07-23 stsp }
4861 1ebedb77 2019-10-19 stsp }
4862 1ebedb77 2019-10-19 stsp }
4863 33aa809d 2019-08-08 stsp done:
4864 33aa809d 2019-08-08 stsp free(id_str);
4865 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4866 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
4867 33aa809d 2019-08-08 stsp if (blob)
4868 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4869 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4870 fe621944 2020-11-10 stsp if (err == NULL)
4871 fe621944 2020-11-10 stsp err = free_err;
4872 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4873 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4874 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4875 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4876 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4877 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4878 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4879 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4880 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4881 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4882 33aa809d 2019-08-08 stsp if (err || !have_content) {
4883 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4884 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4885 33aa809d 2019-08-08 stsp free(*path_outfile);
4886 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4887 33aa809d 2019-08-08 stsp }
4888 33aa809d 2019-08-08 stsp free(path1);
4889 33aa809d 2019-08-08 stsp return err;
4890 33aa809d 2019-08-08 stsp }
4891 33aa809d 2019-08-08 stsp
4892 33aa809d 2019-08-08 stsp static const struct got_error *
4893 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4894 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4895 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4896 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4897 a129376b 2019-03-28 stsp {
4898 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4899 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4900 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4901 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4902 945f9229 2022-04-16 thomas struct got_commit_object *base_commit = NULL;
4903 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4904 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4905 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4906 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4907 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4908 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4909 f4ae6ddb 2022-07-01 thomas int fd = -1;
4910 a129376b 2019-03-28 stsp
4911 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4912 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4913 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4914 d3bcc3d1 2019-08-08 stsp return NULL;
4915 3d69ad8d 2019-08-17 semarie
4916 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4917 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4918 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4919 d3bcc3d1 2019-08-08 stsp
4920 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4921 65084dad 2019-08-08 stsp if (ie == NULL)
4922 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4923 a129376b 2019-03-28 stsp
4924 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4925 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4926 a129376b 2019-03-28 stsp if (err) {
4927 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4928 a129376b 2019-03-28 stsp goto done;
4929 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4930 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4931 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4932 e20a8b6f 2019-06-04 stsp goto done;
4933 e20a8b6f 2019-06-04 stsp }
4934 a129376b 2019-03-28 stsp }
4935 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4936 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4937 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4938 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4939 a129376b 2019-03-28 stsp goto done;
4940 a129376b 2019-03-28 stsp }
4941 a129376b 2019-03-28 stsp } else {
4942 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4943 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4944 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4945 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4946 a129376b 2019-03-28 stsp goto done;
4947 a129376b 2019-03-28 stsp }
4948 a129376b 2019-03-28 stsp } else {
4949 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4950 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4951 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4952 a129376b 2019-03-28 stsp goto done;
4953 a129376b 2019-03-28 stsp }
4954 a129376b 2019-03-28 stsp }
4955 a129376b 2019-03-28 stsp }
4956 a129376b 2019-03-28 stsp
4957 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&base_commit, a->repo,
4958 945f9229 2022-04-16 thomas a->worktree->base_commit_id);
4959 945f9229 2022-04-16 thomas if (err)
4960 945f9229 2022-04-16 thomas goto done;
4961 945f9229 2022-04-16 thomas
4962 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4963 a9fa2909 2019-07-27 stsp if (err) {
4964 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4965 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4966 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4967 a9fa2909 2019-07-27 stsp goto done;
4968 a9fa2909 2019-07-27 stsp } else {
4969 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4970 a9fa2909 2019-07-27 stsp if (err)
4971 a9fa2909 2019-07-27 stsp goto done;
4972 a9fa2909 2019-07-27 stsp
4973 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4974 1233e6b6 2020-10-19 stsp if (err)
4975 a9fa2909 2019-07-27 stsp goto done;
4976 a9fa2909 2019-07-27 stsp
4977 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4978 1233e6b6 2020-10-19 stsp free(te_name);
4979 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4980 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4981 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4982 a9fa2909 2019-07-27 stsp goto done;
4983 a9fa2909 2019-07-27 stsp }
4984 a129376b 2019-03-28 stsp }
4985 a129376b 2019-03-28 stsp
4986 a129376b 2019-03-28 stsp switch (status) {
4987 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4988 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4989 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4990 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4991 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4992 33aa809d 2019-08-08 stsp if (err)
4993 33aa809d 2019-08-08 stsp goto done;
4994 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4995 33aa809d 2019-08-08 stsp break;
4996 33aa809d 2019-08-08 stsp }
4997 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4998 1f1abb7e 2019-08-08 stsp ie->path);
4999 1ee397ad 2019-07-12 stsp if (err)
5000 1ee397ad 2019-07-12 stsp goto done;
5001 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5002 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
5003 8641a332 2023-04-14 thomas int do_unlink = a->added_files_to_unlink ? 0 : 1;
5004 8641a332 2023-04-14 thomas
5005 8641a332 2023-04-14 thomas if (a->added_files_to_unlink) {
5006 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
5007 8641a332 2023-04-14 thomas
5008 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, a->added_files_to_unlink,
5009 8641a332 2023-04-14 thomas entry) {
5010 8641a332 2023-04-14 thomas if (got_path_cmp(pe->path, relpath,
5011 8641a332 2023-04-14 thomas pe->path_len, strlen(relpath)))
5012 8641a332 2023-04-14 thomas continue;
5013 8641a332 2023-04-14 thomas do_unlink = 1;
5014 8641a332 2023-04-14 thomas break;
5015 8641a332 2023-04-14 thomas }
5016 10604dce 2021-09-24 thomas }
5017 8641a332 2023-04-14 thomas
5018 8641a332 2023-04-14 thomas if (do_unlink) {
5019 8641a332 2023-04-14 thomas if (asprintf(&ondisk_path, "%s/%s",
5020 8641a332 2023-04-14 thomas got_worktree_get_root_path(a->worktree),
5021 8641a332 2023-04-14 thomas relpath) == -1) {
5022 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
5023 8641a332 2023-04-14 thomas goto done;
5024 8641a332 2023-04-14 thomas }
5025 8641a332 2023-04-14 thomas if (unlink(ondisk_path) == -1) {
5026 8641a332 2023-04-14 thomas err = got_error_from_errno2("unlink",
5027 8641a332 2023-04-14 thomas ondisk_path);
5028 8641a332 2023-04-14 thomas break;
5029 8641a332 2023-04-14 thomas }
5030 10604dce 2021-09-24 thomas }
5031 10604dce 2021-09-24 thomas }
5032 a129376b 2019-03-28 stsp break;
5033 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5034 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5035 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5036 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5037 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5038 33aa809d 2019-08-08 stsp if (err)
5039 33aa809d 2019-08-08 stsp goto done;
5040 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5041 33aa809d 2019-08-08 stsp break;
5042 33aa809d 2019-08-08 stsp }
5043 33aa809d 2019-08-08 stsp /* fall through */
5044 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5045 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5046 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5047 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5048 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5049 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5050 43010591 2023-02-17 thomas staged_status == GOT_STATUS_MODIFY)
5051 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(&id, ie);
5052 43010591 2023-02-17 thomas else
5053 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(&id, ie);
5054 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5055 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5056 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5057 f4ae6ddb 2022-07-01 thomas goto done;
5058 f4ae6ddb 2022-07-01 thomas }
5059 f4ae6ddb 2022-07-01 thomas
5060 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5061 a129376b 2019-03-28 stsp if (err)
5062 65084dad 2019-08-08 stsp goto done;
5063 65084dad 2019-08-08 stsp
5064 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5065 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5066 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5067 a129376b 2019-03-28 stsp goto done;
5068 65084dad 2019-08-08 stsp }
5069 65084dad 2019-08-08 stsp
5070 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5071 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5072 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5073 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5074 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5075 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5076 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5077 33aa809d 2019-08-08 stsp break;
5078 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5079 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5080 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5081 369fd7e5 2020-07-23 stsp path_content);
5082 369fd7e5 2020-07-23 stsp break;
5083 369fd7e5 2020-07-23 stsp }
5084 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5085 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5086 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5087 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5088 369fd7e5 2020-07-23 stsp } else {
5089 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5090 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5091 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5092 369fd7e5 2020-07-23 stsp goto done;
5093 369fd7e5 2020-07-23 stsp }
5094 33aa809d 2019-08-08 stsp }
5095 33aa809d 2019-08-08 stsp } else {
5096 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5097 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5098 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5099 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5100 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
5101 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5102 2e1fa222 2020-07-23 stsp } else {
5103 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5104 2e1fa222 2020-07-23 stsp ie->path,
5105 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5106 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5107 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5108 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5109 2e1fa222 2020-07-23 stsp }
5110 a129376b 2019-03-28 stsp if (err)
5111 a129376b 2019-03-28 stsp goto done;
5112 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5113 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5114 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5115 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5116 437adc9d 2020-12-10 yzhong blob->id.sha1,
5117 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5118 2e1fa222 2020-07-23 stsp if (err)
5119 2e1fa222 2020-07-23 stsp goto done;
5120 2e1fa222 2020-07-23 stsp }
5121 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5122 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5123 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5124 33aa809d 2019-08-08 stsp }
5125 a129376b 2019-03-28 stsp }
5126 a129376b 2019-03-28 stsp break;
5127 e20a8b6f 2019-06-04 stsp }
5128 a129376b 2019-03-28 stsp default:
5129 1f1abb7e 2019-08-08 stsp break;
5130 a129376b 2019-03-28 stsp }
5131 a129376b 2019-03-28 stsp done:
5132 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5133 33aa809d 2019-08-08 stsp free(path_content);
5134 e20a8b6f 2019-06-04 stsp free(parent_path);
5135 a129376b 2019-03-28 stsp free(tree_path);
5136 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5137 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5138 a129376b 2019-03-28 stsp if (blob)
5139 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5140 a129376b 2019-03-28 stsp if (tree)
5141 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5142 a129376b 2019-03-28 stsp free(tree_id);
5143 945f9229 2022-04-16 thomas if (base_commit)
5144 945f9229 2022-04-16 thomas got_object_commit_close(base_commit);
5145 e20a8b6f 2019-06-04 stsp return err;
5146 e20a8b6f 2019-06-04 stsp }
5147 e20a8b6f 2019-06-04 stsp
5148 e20a8b6f 2019-06-04 stsp const struct got_error *
5149 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5150 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5151 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5152 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5153 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5154 e20a8b6f 2019-06-04 stsp {
5155 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5156 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5157 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5158 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5159 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5160 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5161 e20a8b6f 2019-06-04 stsp
5162 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5163 e20a8b6f 2019-06-04 stsp if (err)
5164 e20a8b6f 2019-06-04 stsp return err;
5165 e20a8b6f 2019-06-04 stsp
5166 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5167 e20a8b6f 2019-06-04 stsp if (err)
5168 e20a8b6f 2019-06-04 stsp goto done;
5169 e20a8b6f 2019-06-04 stsp
5170 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5171 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5172 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5173 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5174 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5175 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5176 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5177 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
5178 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5179 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5180 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
5181 e20a8b6f 2019-06-04 stsp if (err)
5182 af12c6b9 2019-06-04 stsp break;
5183 e20a8b6f 2019-06-04 stsp }
5184 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5185 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5186 e20a8b6f 2019-06-04 stsp err = sync_err;
5187 af12c6b9 2019-06-04 stsp done:
5188 fb399478 2019-07-12 stsp free(fileindex_path);
5189 a129376b 2019-03-28 stsp if (fileindex)
5190 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5191 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5192 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5193 a129376b 2019-03-28 stsp err = unlockerr;
5194 c4296144 2019-05-09 stsp return err;
5195 c4296144 2019-05-09 stsp }
5196 c4296144 2019-05-09 stsp
5197 cf066bf8 2019-05-09 stsp static void
5198 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5199 cf066bf8 2019-05-09 stsp {
5200 24519714 2019-05-09 stsp free(ct->path);
5201 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5202 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5203 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5204 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5205 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5206 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5207 cf066bf8 2019-05-09 stsp free(ct);
5208 cf066bf8 2019-05-09 stsp }
5209 24519714 2019-05-09 stsp
5210 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5211 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5212 24519714 2019-05-09 stsp struct got_repository *repo;
5213 24519714 2019-05-09 stsp struct got_worktree *worktree;
5214 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5215 5f8a88c6 2019-08-03 stsp int have_staged_files;
5216 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5217 ef899790 2022-11-01 thomas int diff_header_shown;
5218 be94c032 2023-02-20 thomas int commit_conflicts;
5219 ef899790 2022-11-01 thomas FILE *diff_outfile;
5220 ef899790 2022-11-01 thomas FILE *f1;
5221 ef899790 2022-11-01 thomas FILE *f2;
5222 24519714 2019-05-09 stsp };
5223 ef899790 2022-11-01 thomas
5224 ef899790 2022-11-01 thomas /*
5225 ef899790 2022-11-01 thomas * Create a file which contains the target path of a symlink so we can feed
5226 ef899790 2022-11-01 thomas * it as content to the diff engine.
5227 ef899790 2022-11-01 thomas */
5228 ef899790 2022-11-01 thomas static const struct got_error *
5229 ef899790 2022-11-01 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5230 ef899790 2022-11-01 thomas const char *abspath)
5231 ef899790 2022-11-01 thomas {
5232 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5233 ef899790 2022-11-01 thomas char target_path[PATH_MAX];
5234 ef899790 2022-11-01 thomas ssize_t target_len, outlen;
5235 ef899790 2022-11-01 thomas
5236 ef899790 2022-11-01 thomas *fd = -1;
5237 ef899790 2022-11-01 thomas
5238 ef899790 2022-11-01 thomas if (dirfd != -1) {
5239 ef899790 2022-11-01 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5240 ef899790 2022-11-01 thomas if (target_len == -1)
5241 ef899790 2022-11-01 thomas return got_error_from_errno2("readlinkat", abspath);
5242 ef899790 2022-11-01 thomas } else {
5243 ef899790 2022-11-01 thomas target_len = readlink(abspath, target_path, PATH_MAX);
5244 ef899790 2022-11-01 thomas if (target_len == -1)
5245 ef899790 2022-11-01 thomas return got_error_from_errno2("readlink", abspath);
5246 ef899790 2022-11-01 thomas }
5247 ef899790 2022-11-01 thomas
5248 ef899790 2022-11-01 thomas *fd = got_opentempfd();
5249 ef899790 2022-11-01 thomas if (*fd == -1)
5250 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentempfd");
5251 ef899790 2022-11-01 thomas
5252 ef899790 2022-11-01 thomas outlen = write(*fd, target_path, target_len);
5253 ef899790 2022-11-01 thomas if (outlen == -1) {
5254 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5255 ef899790 2022-11-01 thomas goto done;
5256 ef899790 2022-11-01 thomas }
5257 ef899790 2022-11-01 thomas
5258 ef899790 2022-11-01 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
5259 ef899790 2022-11-01 thomas err = got_error_from_errno2("lseek", abspath);
5260 ef899790 2022-11-01 thomas goto done;
5261 ef899790 2022-11-01 thomas }
5262 ef899790 2022-11-01 thomas done:
5263 ef899790 2022-11-01 thomas if (err) {
5264 ef899790 2022-11-01 thomas close(*fd);
5265 ef899790 2022-11-01 thomas *fd = -1;
5266 ef899790 2022-11-01 thomas }
5267 ef899790 2022-11-01 thomas return err;
5268 ef899790 2022-11-01 thomas }
5269 ef899790 2022-11-01 thomas
5270 ef899790 2022-11-01 thomas static const struct got_error *
5271 ef899790 2022-11-01 thomas append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5272 ef899790 2022-11-01 thomas FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5273 b5bedbbb 2022-11-01 thomas int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5274 ef899790 2022-11-01 thomas {
5275 ef899790 2022-11-01 thomas const struct got_error *err = NULL;
5276 ef899790 2022-11-01 thomas struct got_blob_object *blob1 = NULL;
5277 ef899790 2022-11-01 thomas int fd = -1, fd1 = -1, fd2 = -1;
5278 ef899790 2022-11-01 thomas FILE *ondisk_file = NULL;
5279 ef899790 2022-11-01 thomas char *label1 = NULL;
5280 ef899790 2022-11-01 thomas struct stat sb;
5281 ef899790 2022-11-01 thomas off_t size1 = 0;
5282 ef899790 2022-11-01 thomas int f2_exists = 0;
5283 ef899790 2022-11-01 thomas char *id_str = NULL;
5284 ef899790 2022-11-01 thomas
5285 ef899790 2022-11-01 thomas memset(&sb, 0, sizeof(sb));
5286 d259e491 2022-11-01 thomas
5287 d259e491 2022-11-01 thomas if (diff_staged) {
5288 d259e491 2022-11-01 thomas if (ct->staged_status != GOT_STATUS_MODIFY &&
5289 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_ADD &&
5290 d259e491 2022-11-01 thomas ct->staged_status != GOT_STATUS_DELETE)
5291 d259e491 2022-11-01 thomas return NULL;
5292 d259e491 2022-11-01 thomas } else {
5293 d259e491 2022-11-01 thomas if (ct->status != GOT_STATUS_MODIFY &&
5294 d259e491 2022-11-01 thomas ct->status != GOT_STATUS_ADD &&
5295 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5296 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5297 d259e491 2022-11-01 thomas return NULL;
5298 d259e491 2022-11-01 thomas }
5299 ef899790 2022-11-01 thomas
5300 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f1);
5301 ef899790 2022-11-01 thomas if (err)
5302 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5303 ef899790 2022-11-01 thomas err = got_opentemp_truncate(f2);
5304 ef899790 2022-11-01 thomas if (err)
5305 ef899790 2022-11-01 thomas return got_error_from_errno("got_opentemp_truncate");
5306 ef899790 2022-11-01 thomas
5307 ef899790 2022-11-01 thomas if (!*diff_header_shown) {
5308 ef899790 2022-11-01 thomas err = got_object_id_str(&id_str, worktree->base_commit_id);
5309 ef899790 2022-11-01 thomas if (err)
5310 ef899790 2022-11-01 thomas return err;
5311 ef899790 2022-11-01 thomas fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5312 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree));
5313 ef899790 2022-11-01 thomas fprintf(diff_outfile, "commit - %s\n", id_str);
5314 ef899790 2022-11-01 thomas fprintf(diff_outfile, "path + %s%s\n",
5315 ef899790 2022-11-01 thomas got_worktree_get_root_path(worktree),
5316 ef899790 2022-11-01 thomas diff_staged ? " (staged changes)" : "");
5317 ef899790 2022-11-01 thomas *diff_header_shown = 1;
5318 ef899790 2022-11-01 thomas }
5319 ef899790 2022-11-01 thomas
5320 ef899790 2022-11-01 thomas if (diff_staged) {
5321 ef899790 2022-11-01 thomas const char *label1 = NULL, *label2 = NULL;
5322 ef899790 2022-11-01 thomas switch (ct->staged_status) {
5323 ef899790 2022-11-01 thomas case GOT_STATUS_MODIFY:
5324 ef899790 2022-11-01 thomas label1 = ct->path;
5325 ef899790 2022-11-01 thomas label2 = ct->path;
5326 ef899790 2022-11-01 thomas break;
5327 ef899790 2022-11-01 thomas case GOT_STATUS_ADD:
5328 ef899790 2022-11-01 thomas label2 = ct->path;
5329 ef899790 2022-11-01 thomas break;
5330 ef899790 2022-11-01 thomas case GOT_STATUS_DELETE:
5331 ef899790 2022-11-01 thomas label1 = ct->path;
5332 ef899790 2022-11-01 thomas break;
5333 ef899790 2022-11-01 thomas default:
5334 ef899790 2022-11-01 thomas return got_error(GOT_ERR_FILE_STATUS);
5335 ef899790 2022-11-01 thomas }
5336 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5337 ef899790 2022-11-01 thomas if (fd1 == -1) {
5338 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5339 ef899790 2022-11-01 thomas goto done;
5340 ef899790 2022-11-01 thomas }
5341 ef899790 2022-11-01 thomas fd2 = got_opentempfd();
5342 ef899790 2022-11-01 thomas if (fd2 == -1) {
5343 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5344 ef899790 2022-11-01 thomas goto done;
5345 ef899790 2022-11-01 thomas }
5346 ef899790 2022-11-01 thomas err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5347 ef899790 2022-11-01 thomas fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5348 be97ab03 2023-01-19 thomas label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5349 53d03f97 2023-01-10 thomas NULL, repo, diff_outfile);
5350 ef899790 2022-11-01 thomas goto done;
5351 ef899790 2022-11-01 thomas }
5352 ef899790 2022-11-01 thomas
5353 ef899790 2022-11-01 thomas fd1 = got_opentempfd();
5354 ef899790 2022-11-01 thomas if (fd1 == -1) {
5355 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentempfd");
5356 ef899790 2022-11-01 thomas goto done;
5357 ef899790 2022-11-01 thomas }
5358 ef899790 2022-11-01 thomas
5359 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_ADD) {
5360 ef899790 2022-11-01 thomas err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5361 ef899790 2022-11-01 thomas 8192, fd1);
5362 ef899790 2022-11-01 thomas if (err)
5363 ef899790 2022-11-01 thomas goto done;
5364 ef899790 2022-11-01 thomas }
5365 ef899790 2022-11-01 thomas
5366 ef899790 2022-11-01 thomas if (ct->status != GOT_STATUS_DELETE) {
5367 ef899790 2022-11-01 thomas if (dirfd != -1) {
5368 ef899790 2022-11-01 thomas fd = openat(dirfd, de_name,
5369 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5370 ef899790 2022-11-01 thomas if (fd == -1) {
5371 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5372 ef899790 2022-11-01 thomas err = got_error_from_errno2("openat",
5373 ef899790 2022-11-01 thomas ct->ondisk_path);
5374 ef899790 2022-11-01 thomas goto done;
5375 ef899790 2022-11-01 thomas }
5376 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5377 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5378 ef899790 2022-11-01 thomas if (err)
5379 ef899790 2022-11-01 thomas goto done;
5380 ef899790 2022-11-01 thomas }
5381 ef899790 2022-11-01 thomas } else {
5382 ef899790 2022-11-01 thomas fd = open(ct->ondisk_path,
5383 ef899790 2022-11-01 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5384 ef899790 2022-11-01 thomas if (fd == -1) {
5385 ef899790 2022-11-01 thomas if (!got_err_open_nofollow_on_symlink()) {
5386 ef899790 2022-11-01 thomas err = got_error_from_errno2("open",
5387 ef899790 2022-11-01 thomas ct->ondisk_path);
5388 ef899790 2022-11-01 thomas goto done;
5389 ef899790 2022-11-01 thomas }
5390 ef899790 2022-11-01 thomas err = get_symlink_target_file(&fd, dirfd,
5391 ef899790 2022-11-01 thomas de_name, ct->ondisk_path);
5392 ef899790 2022-11-01 thomas if (err)
5393 ef899790 2022-11-01 thomas goto done;
5394 ef899790 2022-11-01 thomas }
5395 ef899790 2022-11-01 thomas }
5396 ef899790 2022-11-01 thomas if (fstatat(fd, ct->ondisk_path, &sb,
5397 ef899790 2022-11-01 thomas AT_SYMLINK_NOFOLLOW) == -1) {
5398 ef899790 2022-11-01 thomas err = got_error_from_errno2("fstatat", ct->ondisk_path);
5399 ef899790 2022-11-01 thomas goto done;
5400 ef899790 2022-11-01 thomas }
5401 ef899790 2022-11-01 thomas ondisk_file = fdopen(fd, "r");
5402 ef899790 2022-11-01 thomas if (ondisk_file == NULL) {
5403 ef899790 2022-11-01 thomas err = got_error_from_errno2("fdopen", ct->ondisk_path);
5404 ef899790 2022-11-01 thomas goto done;
5405 ef899790 2022-11-01 thomas }
5406 ef899790 2022-11-01 thomas fd = -1;
5407 ef899790 2022-11-01 thomas f2_exists = 1;
5408 ef899790 2022-11-01 thomas }
5409 ef899790 2022-11-01 thomas
5410 ef899790 2022-11-01 thomas if (blob1) {
5411 ef899790 2022-11-01 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5412 ef899790 2022-11-01 thomas f1, blob1);
5413 ef899790 2022-11-01 thomas if (err)
5414 ef899790 2022-11-01 thomas goto done;
5415 ef899790 2022-11-01 thomas }
5416 ef899790 2022-11-01 thomas
5417 ef899790 2022-11-01 thomas err = got_diff_blob_file(blob1, f1, size1, label1,
5418 ef899790 2022-11-01 thomas ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5419 be97ab03 2023-01-19 thomas GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5420 ef899790 2022-11-01 thomas done:
5421 ef899790 2022-11-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5422 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5423 ef899790 2022-11-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5424 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5425 ef899790 2022-11-01 thomas if (blob1)
5426 ef899790 2022-11-01 thomas got_object_blob_close(blob1);
5427 ef899790 2022-11-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5428 ef899790 2022-11-01 thomas err = got_error_from_errno("close");
5429 ef899790 2022-11-01 thomas if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5430 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
5431 ef899790 2022-11-01 thomas return err;
5432 ef899790 2022-11-01 thomas }
5433 cf066bf8 2019-05-09 stsp
5434 c4296144 2019-05-09 stsp static const struct got_error *
5435 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5436 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5437 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5438 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5439 c4296144 2019-05-09 stsp {
5440 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5441 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5442 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5443 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5444 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5445 768aea60 2019-05-09 stsp struct stat sb;
5446 c4296144 2019-05-09 stsp
5447 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5448 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5449 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5450 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5451 5f8a88c6 2019-08-03 stsp return NULL;
5452 5f8a88c6 2019-08-03 stsp } else {
5453 be94c032 2023-02-20 thomas if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5454 be94c032 2023-02-20 thomas printf("C %s\n", relpath);
5455 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5456 be94c032 2023-02-20 thomas }
5457 c4296144 2019-05-09 stsp
5458 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5459 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5460 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5461 be94c032 2023-02-20 thomas status != GOT_STATUS_DELETE &&
5462 be94c032 2023-02-20 thomas status != GOT_STATUS_CONFLICT)
5463 5f8a88c6 2019-08-03 stsp return NULL;
5464 5f8a88c6 2019-08-03 stsp }
5465 0b5cc0d6 2019-05-09 stsp
5466 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5467 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5468 036813ee 2019-05-09 stsp goto done;
5469 036813ee 2019-05-09 stsp }
5470 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5471 036813ee 2019-05-09 stsp parent_path = strdup("");
5472 69960a46 2019-05-09 stsp if (parent_path == NULL)
5473 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5474 69960a46 2019-05-09 stsp } else {
5475 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5476 69960a46 2019-05-09 stsp if (err)
5477 69960a46 2019-05-09 stsp return err;
5478 69960a46 2019-05-09 stsp }
5479 c4296144 2019-05-09 stsp
5480 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5481 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5482 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5483 c4296144 2019-05-09 stsp goto done;
5484 768aea60 2019-05-09 stsp }
5485 768aea60 2019-05-09 stsp
5486 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5487 768aea60 2019-05-09 stsp relpath) == -1) {
5488 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5489 768aea60 2019-05-09 stsp goto done;
5490 768aea60 2019-05-09 stsp }
5491 0aeb8099 2020-07-23 stsp
5492 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5493 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5494 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5495 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5496 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5497 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5498 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5499 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5500 0aeb8099 2020-07-23 stsp break;
5501 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5502 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5503 0aeb8099 2020-07-23 stsp break;
5504 0aeb8099 2020-07-23 stsp default:
5505 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5506 0aeb8099 2020-07-23 stsp goto done;
5507 0aeb8099 2020-07-23 stsp }
5508 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5509 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5510 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5511 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5512 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5513 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5514 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5515 12463d8b 2019-12-13 stsp ct->ondisk_path);
5516 12463d8b 2019-12-13 stsp goto done;
5517 12463d8b 2019-12-13 stsp }
5518 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5519 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5520 768aea60 2019-05-09 stsp goto done;
5521 768aea60 2019-05-09 stsp }
5522 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5523 c4296144 2019-05-09 stsp }
5524 c4296144 2019-05-09 stsp
5525 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5526 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5527 44d03001 2019-05-09 stsp relpath) == -1) {
5528 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5529 44d03001 2019-05-09 stsp goto done;
5530 44d03001 2019-05-09 stsp }
5531 44d03001 2019-05-09 stsp
5532 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5533 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5534 35213c7c 2020-07-23 stsp int is_bad_symlink;
5535 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5536 35213c7c 2020-07-23 stsp ssize_t target_len;
5537 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5538 35213c7c 2020-07-23 stsp sizeof(target_path));
5539 35213c7c 2020-07-23 stsp if (target_len == -1) {
5540 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5541 35213c7c 2020-07-23 stsp ct->ondisk_path);
5542 35213c7c 2020-07-23 stsp goto done;
5543 35213c7c 2020-07-23 stsp }
5544 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5545 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5546 35213c7c 2020-07-23 stsp if (err)
5547 35213c7c 2020-07-23 stsp goto done;
5548 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5549 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5550 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5551 35213c7c 2020-07-23 stsp goto done;
5552 35213c7c 2020-07-23 stsp }
5553 35213c7c 2020-07-23 stsp }
5554 35213c7c 2020-07-23 stsp
5555 35213c7c 2020-07-23 stsp
5556 cf066bf8 2019-05-09 stsp ct->status = status;
5557 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5558 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5559 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5560 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5561 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5562 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5563 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5564 b416585c 2019-05-13 stsp goto done;
5565 b416585c 2019-05-13 stsp }
5566 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5567 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5568 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5569 f0b75401 2019-08-03 stsp goto done;
5570 f0b75401 2019-08-03 stsp }
5571 f0b75401 2019-08-03 stsp }
5572 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5573 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5574 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5575 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5576 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5577 036813ee 2019-05-09 stsp goto done;
5578 036813ee 2019-05-09 stsp }
5579 ca2503ea 2019-05-09 stsp }
5580 24519714 2019-05-09 stsp ct->path = strdup(path);
5581 24519714 2019-05-09 stsp if (ct->path == NULL) {
5582 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5583 24519714 2019-05-09 stsp goto done;
5584 24519714 2019-05-09 stsp }
5585 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5586 ef899790 2022-11-01 thomas if (err)
5587 ef899790 2022-11-01 thomas goto done;
5588 ef899790 2022-11-01 thomas
5589 ef899790 2022-11-01 thomas if (a->diff_outfile && ct && new != NULL) {
5590 ef899790 2022-11-01 thomas err = append_ct_diff(ct, &a->diff_header_shown,
5591 ef899790 2022-11-01 thomas a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5592 b5bedbbb 2022-11-01 thomas a->have_staged_files, a->repo, a->worktree);
5593 ef899790 2022-11-01 thomas if (err)
5594 ef899790 2022-11-01 thomas goto done;
5595 ef899790 2022-11-01 thomas }
5596 c4296144 2019-05-09 stsp done:
5597 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5598 ed175427 2019-05-09 stsp free_commitable(ct);
5599 24519714 2019-05-09 stsp free(parent_path);
5600 036813ee 2019-05-09 stsp free(path);
5601 a129376b 2019-03-28 stsp return err;
5602 a129376b 2019-03-28 stsp }
5603 c4296144 2019-05-09 stsp
5604 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5605 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5606 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5607 036813ee 2019-05-09 stsp struct got_repository *);
5608 ed175427 2019-05-09 stsp
5609 ed175427 2019-05-09 stsp static const struct got_error *
5610 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5611 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5612 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5613 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5614 afa376bf 2019-05-09 stsp struct got_repository *repo)
5615 ed175427 2019-05-09 stsp {
5616 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5617 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5618 036813ee 2019-05-09 stsp char *subpath;
5619 ed175427 2019-05-09 stsp
5620 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5621 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5622 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5623 ed175427 2019-05-09 stsp
5624 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5625 ed175427 2019-05-09 stsp if (err)
5626 ed175427 2019-05-09 stsp return err;
5627 ed175427 2019-05-09 stsp
5628 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5629 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5630 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5631 036813ee 2019-05-09 stsp free(subpath);
5632 036813ee 2019-05-09 stsp return err;
5633 036813ee 2019-05-09 stsp }
5634 ed175427 2019-05-09 stsp
5635 036813ee 2019-05-09 stsp static const struct got_error *
5636 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5637 036813ee 2019-05-09 stsp {
5638 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5639 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5640 ed175427 2019-05-09 stsp
5641 036813ee 2019-05-09 stsp *match = 0;
5642 ed175427 2019-05-09 stsp
5643 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5644 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5645 0f63689d 2019-05-10 stsp return NULL;
5646 036813ee 2019-05-09 stsp }
5647 0b5cc0d6 2019-05-09 stsp
5648 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5649 0f63689d 2019-05-10 stsp if (err)
5650 0f63689d 2019-05-10 stsp return err;
5651 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5652 036813ee 2019-05-09 stsp free(ct_parent_path);
5653 036813ee 2019-05-09 stsp return err;
5654 036813ee 2019-05-09 stsp }
5655 0b5cc0d6 2019-05-09 stsp
5656 768aea60 2019-05-09 stsp static mode_t
5657 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5658 768aea60 2019-05-09 stsp {
5659 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5660 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5661 3d9a4ec4 2020-07-23 stsp
5662 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5663 768aea60 2019-05-09 stsp }
5664 768aea60 2019-05-09 stsp
5665 036813ee 2019-05-09 stsp static const struct got_error *
5666 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5667 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5668 036813ee 2019-05-09 stsp {
5669 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5670 ca2503ea 2019-05-09 stsp
5671 036813ee 2019-05-09 stsp *new_te = NULL;
5672 0b5cc0d6 2019-05-09 stsp
5673 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5674 036813ee 2019-05-09 stsp if (err)
5675 036813ee 2019-05-09 stsp goto done;
5676 0b5cc0d6 2019-05-09 stsp
5677 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5678 036813ee 2019-05-09 stsp
5679 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5680 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5681 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5682 5f8a88c6 2019-08-03 stsp else
5683 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5684 036813ee 2019-05-09 stsp done:
5685 036813ee 2019-05-09 stsp if (err && *new_te) {
5686 56e0773d 2019-11-28 stsp free(*new_te);
5687 036813ee 2019-05-09 stsp *new_te = NULL;
5688 036813ee 2019-05-09 stsp }
5689 036813ee 2019-05-09 stsp return err;
5690 036813ee 2019-05-09 stsp }
5691 036813ee 2019-05-09 stsp
5692 036813ee 2019-05-09 stsp static const struct got_error *
5693 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5694 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5695 036813ee 2019-05-09 stsp {
5696 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5697 102b254e 2020-10-19 stsp char *ct_name = NULL;
5698 036813ee 2019-05-09 stsp
5699 036813ee 2019-05-09 stsp *new_te = NULL;
5700 036813ee 2019-05-09 stsp
5701 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5702 036813ee 2019-05-09 stsp if (*new_te == NULL)
5703 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5704 036813ee 2019-05-09 stsp
5705 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5706 102b254e 2020-10-19 stsp if (err)
5707 036813ee 2019-05-09 stsp goto done;
5708 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5709 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5710 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5711 036813ee 2019-05-09 stsp goto done;
5712 036813ee 2019-05-09 stsp }
5713 036813ee 2019-05-09 stsp
5714 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5715 036813ee 2019-05-09 stsp
5716 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5717 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5718 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5719 5f8a88c6 2019-08-03 stsp else
5720 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5721 036813ee 2019-05-09 stsp done:
5722 102b254e 2020-10-19 stsp free(ct_name);
5723 036813ee 2019-05-09 stsp if (err && *new_te) {
5724 56e0773d 2019-11-28 stsp free(*new_te);
5725 036813ee 2019-05-09 stsp *new_te = NULL;
5726 036813ee 2019-05-09 stsp }
5727 036813ee 2019-05-09 stsp return err;
5728 036813ee 2019-05-09 stsp }
5729 036813ee 2019-05-09 stsp
5730 036813ee 2019-05-09 stsp static const struct got_error *
5731 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5732 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5733 036813ee 2019-05-09 stsp {
5734 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5735 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5736 036813ee 2019-05-09 stsp
5737 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5738 036813ee 2019-05-09 stsp if (err)
5739 036813ee 2019-05-09 stsp return err;
5740 036813ee 2019-05-09 stsp if (new_pe == NULL)
5741 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5742 036813ee 2019-05-09 stsp return NULL;
5743 afa376bf 2019-05-09 stsp }
5744 afa376bf 2019-05-09 stsp
5745 afa376bf 2019-05-09 stsp static const struct got_error *
5746 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5747 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5748 afa376bf 2019-05-09 stsp {
5749 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5750 5f8a88c6 2019-08-03 stsp unsigned char status;
5751 ae1e948a 2021-09-28 thomas
5752 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5753 ae1e948a 2021-09-28 thomas return NULL;
5754 5f8a88c6 2019-08-03 stsp
5755 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5756 afa376bf 2019-05-09 stsp ct_path++;
5757 5f8a88c6 2019-08-03 stsp
5758 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5759 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5760 5f8a88c6 2019-08-03 stsp else
5761 5f8a88c6 2019-08-03 stsp status = ct->status;
5762 5f8a88c6 2019-08-03 stsp
5763 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5764 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5765 036813ee 2019-05-09 stsp }
5766 036813ee 2019-05-09 stsp
5767 036813ee 2019-05-09 stsp static const struct got_error *
5768 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5769 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5770 44d03001 2019-05-09 stsp {
5771 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5772 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5773 44d03001 2019-05-09 stsp char *te_path;
5774 44d03001 2019-05-09 stsp
5775 44d03001 2019-05-09 stsp *modified = 0;
5776 44d03001 2019-05-09 stsp
5777 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5778 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5779 44d03001 2019-05-09 stsp te->name) == -1)
5780 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5781 44d03001 2019-05-09 stsp
5782 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5783 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5784 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5785 44d03001 2019-05-09 stsp strlen(te_path));
5786 62d463ca 2020-10-20 naddy if (*modified)
5787 44d03001 2019-05-09 stsp break;
5788 44d03001 2019-05-09 stsp }
5789 44d03001 2019-05-09 stsp
5790 44d03001 2019-05-09 stsp free(te_path);
5791 44d03001 2019-05-09 stsp return err;
5792 44d03001 2019-05-09 stsp }
5793 44d03001 2019-05-09 stsp
5794 44d03001 2019-05-09 stsp static const struct got_error *
5795 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5796 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5797 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5798 036813ee 2019-05-09 stsp {
5799 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5800 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5801 036813ee 2019-05-09 stsp
5802 036813ee 2019-05-09 stsp *ctp = NULL;
5803 036813ee 2019-05-09 stsp
5804 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5805 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5806 036813ee 2019-05-09 stsp char *ct_name = NULL;
5807 036813ee 2019-05-09 stsp int path_matches;
5808 036813ee 2019-05-09 stsp
5809 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5810 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5811 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5812 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_DELETE &&
5813 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
5814 5f8a88c6 2019-08-03 stsp continue;
5815 5f8a88c6 2019-08-03 stsp } else {
5816 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5817 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5818 5f8a88c6 2019-08-03 stsp continue;
5819 5f8a88c6 2019-08-03 stsp }
5820 036813ee 2019-05-09 stsp
5821 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5822 036813ee 2019-05-09 stsp continue;
5823 036813ee 2019-05-09 stsp
5824 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5825 62d463ca 2020-10-20 naddy if (err)
5826 036813ee 2019-05-09 stsp return err;
5827 036813ee 2019-05-09 stsp if (!path_matches)
5828 036813ee 2019-05-09 stsp continue;
5829 036813ee 2019-05-09 stsp
5830 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5831 d34b633e 2020-10-19 stsp if (err)
5832 d34b633e 2020-10-19 stsp return err;
5833 d34b633e 2020-10-19 stsp
5834 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5835 d34b633e 2020-10-19 stsp free(ct_name);
5836 036813ee 2019-05-09 stsp continue;
5837 d34b633e 2020-10-19 stsp }
5838 d34b633e 2020-10-19 stsp free(ct_name);
5839 036813ee 2019-05-09 stsp
5840 036813ee 2019-05-09 stsp *ctp = ct;
5841 036813ee 2019-05-09 stsp break;
5842 036813ee 2019-05-09 stsp }
5843 2b496619 2019-07-10 stsp
5844 2b496619 2019-07-10 stsp return err;
5845 2b496619 2019-07-10 stsp }
5846 2b496619 2019-07-10 stsp
5847 2b496619 2019-07-10 stsp static const struct got_error *
5848 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5849 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5850 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5851 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5852 2b496619 2019-07-10 stsp struct got_repository *repo)
5853 2b496619 2019-07-10 stsp {
5854 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5855 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5856 2b496619 2019-07-10 stsp char *subtree_path;
5857 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5858 ba580f68 2020-03-22 stsp int nentries;
5859 2b496619 2019-07-10 stsp
5860 2b496619 2019-07-10 stsp *new_tep = NULL;
5861 2b496619 2019-07-10 stsp
5862 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5863 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5864 2b496619 2019-07-10 stsp child_path) == -1)
5865 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5866 036813ee 2019-05-09 stsp
5867 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5868 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5869 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5870 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5871 56e0773d 2019-11-28 stsp
5872 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5873 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5874 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5875 2b496619 2019-07-10 stsp goto done;
5876 2b496619 2019-07-10 stsp }
5877 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5878 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5879 2b496619 2019-07-10 stsp if (err) {
5880 56e0773d 2019-11-28 stsp free(new_te);
5881 2b496619 2019-07-10 stsp goto done;
5882 2b496619 2019-07-10 stsp }
5883 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5884 2b496619 2019-07-10 stsp done:
5885 56e0773d 2019-11-28 stsp free(id);
5886 2b496619 2019-07-10 stsp free(subtree_path);
5887 2b496619 2019-07-10 stsp if (err == NULL)
5888 2b496619 2019-07-10 stsp *new_tep = new_te;
5889 036813ee 2019-05-09 stsp return err;
5890 036813ee 2019-05-09 stsp }
5891 036813ee 2019-05-09 stsp
5892 036813ee 2019-05-09 stsp static const struct got_error *
5893 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5894 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5895 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5896 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5897 036813ee 2019-05-09 stsp struct got_repository *repo)
5898 036813ee 2019-05-09 stsp {
5899 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5900 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5901 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5902 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5903 036813ee 2019-05-09 stsp
5904 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5905 ba580f68 2020-03-22 stsp *nentries = 0;
5906 036813ee 2019-05-09 stsp
5907 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5908 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5909 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5910 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5911 036813ee 2019-05-09 stsp
5912 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5913 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5914 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5915 036813ee 2019-05-09 stsp continue;
5916 036813ee 2019-05-09 stsp
5917 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5918 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5919 036813ee 2019-05-09 stsp continue;
5920 036813ee 2019-05-09 stsp
5921 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5922 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5923 036813ee 2019-05-09 stsp if (err)
5924 036813ee 2019-05-09 stsp goto done;
5925 036813ee 2019-05-09 stsp
5926 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5927 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5928 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5929 036813ee 2019-05-09 stsp if (err)
5930 036813ee 2019-05-09 stsp goto done;
5931 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5932 afa376bf 2019-05-09 stsp if (err)
5933 afa376bf 2019-05-09 stsp goto done;
5934 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5935 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5936 2b496619 2019-07-10 stsp if (err)
5937 2f51b5b3 2019-05-09 stsp goto done;
5938 ba580f68 2020-03-22 stsp (*nentries)++;
5939 2b496619 2019-07-10 stsp } else {
5940 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5941 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5942 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5943 2b496619 2019-07-10 stsp == NULL) {
5944 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5945 2b496619 2019-07-10 stsp child_path, path_base_tree,
5946 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5947 2b496619 2019-07-10 stsp repo);
5948 2b496619 2019-07-10 stsp if (err)
5949 2b496619 2019-07-10 stsp goto done;
5950 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5951 2b496619 2019-07-10 stsp if (err)
5952 2b496619 2019-07-10 stsp goto done;
5953 ba580f68 2020-03-22 stsp (*nentries)++;
5954 9ba0479c 2019-05-10 stsp }
5955 ed175427 2019-05-09 stsp }
5956 2f51b5b3 2019-05-09 stsp }
5957 2f51b5b3 2019-05-09 stsp
5958 2f51b5b3 2019-05-09 stsp if (base_tree) {
5959 56e0773d 2019-11-28 stsp int i, nbase_entries;
5960 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5961 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5962 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5963 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5964 63c5ca5d 2019-08-24 stsp
5965 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5966 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5967 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5968 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5969 63c5ca5d 2019-08-24 stsp if (err)
5970 63c5ca5d 2019-08-24 stsp goto done;
5971 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5972 63c5ca5d 2019-08-24 stsp if (err)
5973 63c5ca5d 2019-08-24 stsp goto done;
5974 ba580f68 2020-03-22 stsp (*nentries)++;
5975 63c5ca5d 2019-08-24 stsp continue;
5976 63c5ca5d 2019-08-24 stsp }
5977 2f51b5b3 2019-05-09 stsp
5978 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5979 44d03001 2019-05-09 stsp int modified;
5980 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5981 036813ee 2019-05-09 stsp if (err)
5982 036813ee 2019-05-09 stsp goto done;
5983 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5984 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5985 2f51b5b3 2019-05-09 stsp if (err)
5986 2f51b5b3 2019-05-09 stsp goto done;
5987 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5988 44d03001 2019-05-09 stsp if (modified) {
5989 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5990 ba580f68 2020-03-22 stsp int nsubentries;
5991 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5992 ba580f68 2020-03-22 stsp &nsubentries, te,
5993 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5994 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5995 44d03001 2019-05-09 stsp if (err)
5996 44d03001 2019-05-09 stsp goto done;
5997 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5998 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5999 ba580f68 2020-03-22 stsp free(new_id);
6000 ba580f68 2020-03-22 stsp continue;
6001 ba580f68 2020-03-22 stsp }
6002 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6003 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6004 56e0773d 2019-11-28 stsp free(new_id);
6005 44d03001 2019-05-09 stsp }
6006 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6007 036813ee 2019-05-09 stsp if (err)
6008 036813ee 2019-05-09 stsp goto done;
6009 ba580f68 2020-03-22 stsp (*nentries)++;
6010 2f51b5b3 2019-05-09 stsp continue;
6011 036813ee 2019-05-09 stsp }
6012 2f51b5b3 2019-05-09 stsp
6013 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6014 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6015 f66c734c 2019-09-22 stsp if (err)
6016 f66c734c 2019-09-22 stsp goto done;
6017 2f51b5b3 2019-05-09 stsp if (ct) {
6018 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6019 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6020 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6021 be94c032 2023-02-20 thomas ct->status == GOT_STATUS_CONFLICT ||
6022 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6023 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6024 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6025 2f51b5b3 2019-05-09 stsp if (err)
6026 2f51b5b3 2019-05-09 stsp goto done;
6027 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6028 2f51b5b3 2019-05-09 stsp if (err)
6029 2f51b5b3 2019-05-09 stsp goto done;
6030 ba580f68 2020-03-22 stsp (*nentries)++;
6031 2f51b5b3 2019-05-09 stsp }
6032 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6033 b416585c 2019-05-13 stsp status_arg);
6034 afa376bf 2019-05-09 stsp if (err)
6035 afa376bf 2019-05-09 stsp goto done;
6036 2f51b5b3 2019-05-09 stsp } else {
6037 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6038 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6039 2f51b5b3 2019-05-09 stsp if (err)
6040 2f51b5b3 2019-05-09 stsp goto done;
6041 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6042 2f51b5b3 2019-05-09 stsp if (err)
6043 2f51b5b3 2019-05-09 stsp goto done;
6044 ba580f68 2020-03-22 stsp (*nentries)++;
6045 2f51b5b3 2019-05-09 stsp }
6046 ca2503ea 2019-05-09 stsp }
6047 ed175427 2019-05-09 stsp }
6048 0b5cc0d6 2019-05-09 stsp
6049 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6050 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6051 ed175427 2019-05-09 stsp done:
6052 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6053 2e1fa222 2020-07-23 stsp return err;
6054 2e1fa222 2020-07-23 stsp }
6055 2e1fa222 2020-07-23 stsp
6056 2e1fa222 2020-07-23 stsp static const struct got_error *
6057 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6058 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6059 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6060 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6061 ebf99748 2019-05-09 stsp {
6062 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6063 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6064 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6065 ebf99748 2019-05-09 stsp
6066 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6067 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6068 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6069 ebf99748 2019-05-09 stsp
6070 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6071 437adc9d 2020-12-10 yzhong
6072 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6073 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6074 437adc9d 2020-12-10 yzhong if (err)
6075 437adc9d 2020-12-10 yzhong goto done;
6076 437adc9d 2020-12-10 yzhong
6077 ebf99748 2019-05-09 stsp if (ie) {
6078 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6079 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6080 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6081 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6082 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6083 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6084 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6085 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6086 437adc9d 2020-12-10 yzhong
6087 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6088 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6089 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6090 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6091 72fd46fa 2019-09-06 stsp !have_staged_files);
6092 ebf99748 2019-05-09 stsp } else
6093 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6094 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6095 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6096 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6097 72fd46fa 2019-09-06 stsp !have_staged_files);
6098 ebf99748 2019-05-09 stsp } else {
6099 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6100 ebf99748 2019-05-09 stsp if (err)
6101 437adc9d 2020-12-10 yzhong goto done;
6102 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6103 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6104 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6105 3969253a 2020-03-07 stsp if (err) {
6106 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6107 437adc9d 2020-12-10 yzhong goto done;
6108 3969253a 2020-03-07 stsp }
6109 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6110 3969253a 2020-03-07 stsp if (err) {
6111 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6112 437adc9d 2020-12-10 yzhong goto done;
6113 3969253a 2020-03-07 stsp }
6114 ebf99748 2019-05-09 stsp }
6115 437adc9d 2020-12-10 yzhong free(relpath);
6116 437adc9d 2020-12-10 yzhong relpath = NULL;
6117 ebf99748 2019-05-09 stsp }
6118 437adc9d 2020-12-10 yzhong done:
6119 437adc9d 2020-12-10 yzhong free(relpath);
6120 d56d26ce 2019-05-10 stsp return err;
6121 d56d26ce 2019-05-10 stsp }
6122 735ef5ac 2019-08-03 stsp
6123 d56d26ce 2019-05-10 stsp
6124 d56d26ce 2019-05-10 stsp static const struct got_error *
6125 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6126 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6127 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6128 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6129 735ef5ac 2019-08-03 stsp int ood_errcode)
6130 d56d26ce 2019-05-10 stsp {
6131 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6132 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6133 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6134 1a36436d 2019-06-10 stsp
6135 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6136 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6137 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6138 1a36436d 2019-06-10 stsp return NULL;
6139 9bead371 2019-07-28 stsp /*
6140 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6141 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6142 9bead371 2019-07-28 stsp */
6143 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6144 945f9229 2022-04-16 thomas if (err)
6145 945f9229 2022-04-16 thomas goto done;
6146 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6147 9bead371 2019-07-28 stsp if (err) {
6148 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6149 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6150 1a36436d 2019-06-10 stsp goto done;
6151 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6152 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6153 1a36436d 2019-06-10 stsp } else {
6154 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6155 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, head_commit_id);
6156 945f9229 2022-04-16 thomas if (err)
6157 945f9229 2022-04-16 thomas goto done;
6158 945f9229 2022-04-16 thomas err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6159 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6160 1a36436d 2019-06-10 stsp goto done;
6161 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6162 1a36436d 2019-06-10 stsp }
6163 1a36436d 2019-06-10 stsp done:
6164 1a36436d 2019-06-10 stsp free(id);
6165 945f9229 2022-04-16 thomas if (commit)
6166 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6167 1a36436d 2019-06-10 stsp return err;
6168 ebf99748 2019-05-09 stsp }
6169 ebf99748 2019-05-09 stsp
6170 ef20f542 2022-06-26 thomas static const struct got_error *
6171 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6172 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6173 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
6174 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
6175 10604dce 2021-09-24 thomas struct got_worktree *worktree,
6176 ef899790 2022-11-01 thomas const char *author, const char *committer, char *diff_path,
6177 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6178 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6179 afa376bf 2019-05-09 stsp struct got_repository *repo)
6180 c4296144 2019-05-09 stsp {
6181 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6182 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6183 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6184 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6185 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6186 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6187 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6188 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6189 10604dce 2021-09-24 thomas int nentries, nparents = 0;
6190 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6191 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6192 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6193 892eab0a 2022-07-22 thomas time_t timestamp;
6194 c4296144 2019-05-09 stsp
6195 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6196 c4296144 2019-05-09 stsp
6197 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6198 675c7539 2019-05-09 stsp
6199 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6200 588edf97 2019-05-10 stsp if (err)
6201 588edf97 2019-05-10 stsp goto done;
6202 de18fc63 2019-05-09 stsp
6203 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6204 588edf97 2019-05-10 stsp if (err)
6205 588edf97 2019-05-10 stsp goto done;
6206 588edf97 2019-05-10 stsp
6207 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6208 ef899790 2022-11-01 thomas err = commit_msg_cb(commitable_paths, diff_path,
6209 ef899790 2022-11-01 thomas &logmsg, commit_arg);
6210 33ad4cbe 2019-05-12 jcs if (err)
6211 33ad4cbe 2019-05-12 jcs goto done;
6212 33ad4cbe 2019-05-12 jcs }
6213 c4296144 2019-05-09 stsp
6214 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6215 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6216 33ad4cbe 2019-05-12 jcs goto done;
6217 33ad4cbe 2019-05-12 jcs }
6218 33ad4cbe 2019-05-12 jcs
6219 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6220 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6221 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6222 cf066bf8 2019-05-09 stsp char *ondisk_path;
6223 cf066bf8 2019-05-09 stsp
6224 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6225 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6226 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6227 5f8a88c6 2019-08-03 stsp continue;
6228 5f8a88c6 2019-08-03 stsp
6229 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6230 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6231 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_MODE_CHANGE &&
6232 be94c032 2023-02-20 thomas ct->status != GOT_STATUS_CONFLICT)
6233 cf066bf8 2019-05-09 stsp continue;
6234 cf066bf8 2019-05-09 stsp
6235 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6236 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6237 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6238 cf066bf8 2019-05-09 stsp goto done;
6239 cf066bf8 2019-05-09 stsp }
6240 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6241 2e1fa222 2020-07-23 stsp free(ondisk_path);
6242 2e1fa222 2020-07-23 stsp if (err)
6243 cf066bf8 2019-05-09 stsp goto done;
6244 cf066bf8 2019-05-09 stsp }
6245 036813ee 2019-05-09 stsp
6246 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6247 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6248 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6249 036813ee 2019-05-09 stsp if (err)
6250 036813ee 2019-05-09 stsp goto done;
6251 036813ee 2019-05-09 stsp
6252 538aebb5 2023-04-22 thomas err = got_object_qid_alloc(&pid, head_commit_id);
6253 de18fc63 2019-05-09 stsp if (err)
6254 de18fc63 2019-05-09 stsp goto done;
6255 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6256 10604dce 2021-09-24 thomas nparents++;
6257 10604dce 2021-09-24 thomas if (parent_id2) {
6258 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
6259 10604dce 2021-09-24 thomas if (err)
6260 10604dce 2021-09-24 thomas goto done;
6261 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6262 10604dce 2021-09-24 thomas nparents++;
6263 10604dce 2021-09-24 thomas }
6264 892eab0a 2022-07-22 thomas timestamp = time(NULL);
6265 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6266 892eab0a 2022-07-22 thomas nparents, author, timestamp, committer, timestamp, logmsg, repo);
6267 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6268 33ad4cbe 2019-05-12 jcs free(logmsg);
6269 9d40349a 2019-05-09 stsp if (err)
6270 9d40349a 2019-05-09 stsp goto done;
6271 9d40349a 2019-05-09 stsp
6272 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6273 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6274 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6275 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6276 09f5bd90 2019-05-09 stsp goto done;
6277 09f5bd90 2019-05-09 stsp }
6278 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6279 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6280 09f5bd90 2019-05-09 stsp if (err)
6281 09f5bd90 2019-05-09 stsp goto done;
6282 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6283 ebf99748 2019-05-09 stsp if (err)
6284 ebf99748 2019-05-09 stsp goto done;
6285 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6286 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6287 09f5bd90 2019-05-09 stsp goto done;
6288 09f5bd90 2019-05-09 stsp }
6289 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6290 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6291 f2c16586 2019-05-09 stsp if (err)
6292 f2c16586 2019-05-09 stsp goto done;
6293 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6294 f2c16586 2019-05-09 stsp if (err)
6295 f2c16586 2019-05-09 stsp goto done;
6296 f2c16586 2019-05-09 stsp
6297 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6298 09f5bd90 2019-05-09 stsp if (err)
6299 09f5bd90 2019-05-09 stsp goto done;
6300 09f5bd90 2019-05-09 stsp
6301 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6302 ebf99748 2019-05-09 stsp if (err)
6303 ebf99748 2019-05-09 stsp goto done;
6304 c4296144 2019-05-09 stsp done:
6305 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
6306 588edf97 2019-05-10 stsp if (head_tree)
6307 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6308 588edf97 2019-05-10 stsp if (head_commit)
6309 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6310 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6311 2f17228e 2019-05-12 stsp if (head_ref2) {
6312 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6313 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6314 2f17228e 2019-05-12 stsp err = unlockerr;
6315 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6316 39cd0ff6 2019-07-12 stsp }
6317 39cd0ff6 2019-07-12 stsp return err;
6318 39cd0ff6 2019-07-12 stsp }
6319 5c1e53bc 2019-07-28 stsp
6320 5c1e53bc 2019-07-28 stsp static const struct got_error *
6321 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6322 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6323 5c1e53bc 2019-07-28 stsp {
6324 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6325 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6326 39cd0ff6 2019-07-12 stsp
6327 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6328 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6329 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6330 5c1e53bc 2019-07-28 stsp
6331 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6332 5c1e53bc 2019-07-28 stsp ct_path++;
6333 5c1e53bc 2019-07-28 stsp
6334 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6335 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6336 5c1e53bc 2019-07-28 stsp break;
6337 5c1e53bc 2019-07-28 stsp }
6338 5c1e53bc 2019-07-28 stsp
6339 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6340 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6341 5c1e53bc 2019-07-28 stsp
6342 5c1e53bc 2019-07-28 stsp return NULL;
6343 5c1e53bc 2019-07-28 stsp }
6344 5c1e53bc 2019-07-28 stsp
6345 f0b75401 2019-08-03 stsp static const struct got_error *
6346 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6347 f0b75401 2019-08-03 stsp {
6348 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6349 f0b75401 2019-08-03 stsp
6350 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6351 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6352 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6353 f0b75401 2019-08-03 stsp }
6354 f0b75401 2019-08-03 stsp
6355 f0b75401 2019-08-03 stsp return NULL;
6356 f0b75401 2019-08-03 stsp }
6357 f0b75401 2019-08-03 stsp
6358 5f8a88c6 2019-08-03 stsp static const struct got_error *
6359 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6360 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6361 5f8a88c6 2019-08-03 stsp {
6362 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6363 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6364 5f8a88c6 2019-08-03 stsp
6365 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6366 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6367 5f8a88c6 2019-08-03 stsp continue;
6368 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6369 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6370 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6371 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6372 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6373 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6374 5f8a88c6 2019-08-03 stsp }
6375 5f8a88c6 2019-08-03 stsp
6376 5f8a88c6 2019-08-03 stsp return NULL;
6377 5f8a88c6 2019-08-03 stsp }
6378 5f8a88c6 2019-08-03 stsp
6379 39cd0ff6 2019-07-12 stsp const struct got_error *
6380 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6381 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6382 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6383 be94c032 2023-02-20 thomas int show_diff, int commit_conflicts,
6384 be94c032 2023-02-20 thomas got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6385 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6386 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6387 39cd0ff6 2019-07-12 stsp {
6388 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6389 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6390 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6391 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6392 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6393 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6394 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6395 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6396 ef899790 2022-11-01 thomas char *diff_path = NULL;
6397 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6398 39cd0ff6 2019-07-12 stsp
6399 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6400 39cd0ff6 2019-07-12 stsp
6401 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
6402 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6403 39cd0ff6 2019-07-12 stsp
6404 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6405 39cd0ff6 2019-07-12 stsp if (err)
6406 39cd0ff6 2019-07-12 stsp goto done;
6407 39cd0ff6 2019-07-12 stsp
6408 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6409 39cd0ff6 2019-07-12 stsp if (err)
6410 39cd0ff6 2019-07-12 stsp goto done;
6411 39cd0ff6 2019-07-12 stsp
6412 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6413 39cd0ff6 2019-07-12 stsp if (err)
6414 39cd0ff6 2019-07-12 stsp goto done;
6415 39cd0ff6 2019-07-12 stsp
6416 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6417 39cd0ff6 2019-07-12 stsp if (err)
6418 39cd0ff6 2019-07-12 stsp goto done;
6419 39cd0ff6 2019-07-12 stsp
6420 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6421 5f8a88c6 2019-08-03 stsp &have_staged_files);
6422 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6423 5f8a88c6 2019-08-03 stsp goto done;
6424 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6425 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6426 5f8a88c6 2019-08-03 stsp if (err)
6427 5f8a88c6 2019-08-03 stsp goto done;
6428 5f8a88c6 2019-08-03 stsp }
6429 5f8a88c6 2019-08-03 stsp
6430 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6431 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6432 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6433 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6434 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6435 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6436 ef899790 2022-11-01 thomas cc_arg.diff_header_shown = 0;
6437 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = commit_conflicts;
6438 ef899790 2022-11-01 thomas if (show_diff) {
6439 ef899790 2022-11-01 thomas err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6440 fc2a50f2 2022-11-01 thomas GOT_TMPDIR_STR "/got", ".diff");
6441 ef899790 2022-11-01 thomas if (err)
6442 ef899790 2022-11-01 thomas goto done;
6443 ef899790 2022-11-01 thomas cc_arg.f1 = got_opentemp();
6444 ef899790 2022-11-01 thomas if (cc_arg.f1 == NULL) {
6445 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6446 ef899790 2022-11-01 thomas goto done;
6447 ef899790 2022-11-01 thomas }
6448 ef899790 2022-11-01 thomas cc_arg.f2 = got_opentemp();
6449 ef899790 2022-11-01 thomas if (cc_arg.f2 == NULL) {
6450 ef899790 2022-11-01 thomas err = got_error_from_errno("got_opentemp");
6451 ef899790 2022-11-01 thomas goto done;
6452 ef899790 2022-11-01 thomas }
6453 ef899790 2022-11-01 thomas }
6454 ef899790 2022-11-01 thomas
6455 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6456 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6457 43896ae6 2022-09-02 thomas collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6458 5c1e53bc 2019-07-28 stsp if (err)
6459 5c1e53bc 2019-07-28 stsp goto done;
6460 5c1e53bc 2019-07-28 stsp }
6461 39cd0ff6 2019-07-12 stsp
6462 ef899790 2022-11-01 thomas if (show_diff) {
6463 ef899790 2022-11-01 thomas if (fflush(cc_arg.diff_outfile) == EOF) {
6464 ef899790 2022-11-01 thomas err = got_error_from_errno("fflush");
6465 ef899790 2022-11-01 thomas goto done;
6466 ef899790 2022-11-01 thomas }
6467 ef899790 2022-11-01 thomas }
6468 ef899790 2022-11-01 thomas
6469 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6470 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6471 39cd0ff6 2019-07-12 stsp goto done;
6472 5c1e53bc 2019-07-28 stsp }
6473 5c1e53bc 2019-07-28 stsp
6474 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6475 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6476 5c1e53bc 2019-07-28 stsp if (err)
6477 5c1e53bc 2019-07-28 stsp goto done;
6478 39cd0ff6 2019-07-12 stsp }
6479 f0b75401 2019-08-03 stsp
6480 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6481 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6482 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6483 f0b75401 2019-08-03 stsp
6484 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6485 f0b75401 2019-08-03 stsp ct_path++;
6486 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6487 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6488 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6489 b50cabdf 2019-07-12 stsp if (err)
6490 b50cabdf 2019-07-12 stsp goto done;
6491 f0b75401 2019-08-03 stsp
6492 b50cabdf 2019-07-12 stsp }
6493 b50cabdf 2019-07-12 stsp
6494 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6495 9e1ed038 2022-11-01 thomas head_commit_id, NULL, worktree, author, committer,
6496 9e1ed038 2022-11-01 thomas (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6497 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6498 39cd0ff6 2019-07-12 stsp if (err)
6499 39cd0ff6 2019-07-12 stsp goto done;
6500 39cd0ff6 2019-07-12 stsp
6501 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6502 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6503 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6504 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6505 39cd0ff6 2019-07-12 stsp err = sync_err;
6506 39cd0ff6 2019-07-12 stsp done:
6507 39cd0ff6 2019-07-12 stsp if (fileindex)
6508 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6509 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6510 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6511 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6512 39cd0ff6 2019-07-12 stsp err = unlockerr;
6513 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6514 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6515 21c2d8be 2023-01-10 thomas
6516 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6517 39cd0ff6 2019-07-12 stsp }
6518 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6519 ef899790 2022-11-01 thomas if (diff_path && unlink(diff_path) == -1 && err == NULL)
6520 ef899790 2022-11-01 thomas err = got_error_from_errno2("unlink", diff_path);
6521 ef899790 2022-11-01 thomas free(diff_path);
6522 ef899790 2022-11-01 thomas if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6523 ef899790 2022-11-01 thomas err == NULL)
6524 ef899790 2022-11-01 thomas err = got_error_from_errno("fclose");
6525 c4296144 2019-05-09 stsp return err;
6526 8656d6c4 2019-05-20 stsp }
6527 8656d6c4 2019-05-20 stsp
6528 8656d6c4 2019-05-20 stsp const char *
6529 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6530 8656d6c4 2019-05-20 stsp {
6531 8656d6c4 2019-05-20 stsp return ct->path;
6532 8656d6c4 2019-05-20 stsp }
6533 8656d6c4 2019-05-20 stsp
6534 8656d6c4 2019-05-20 stsp unsigned int
6535 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6536 8656d6c4 2019-05-20 stsp {
6537 8656d6c4 2019-05-20 stsp return ct->status;
6538 818c7501 2019-07-11 stsp }
6539 818c7501 2019-07-11 stsp
6540 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6541 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6542 818c7501 2019-07-11 stsp struct got_repository *repo;
6543 818c7501 2019-07-11 stsp };
6544 818c7501 2019-07-11 stsp
6545 818c7501 2019-07-11 stsp static const struct got_error *
6546 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6547 818c7501 2019-07-11 stsp {
6548 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6549 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6550 818c7501 2019-07-11 stsp unsigned char status;
6551 818c7501 2019-07-11 stsp struct stat sb;
6552 818c7501 2019-07-11 stsp char *ondisk_path;
6553 818c7501 2019-07-11 stsp
6554 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6555 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6556 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6557 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6558 818c7501 2019-07-11 stsp
6559 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6560 818c7501 2019-07-11 stsp == -1)
6561 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6562 818c7501 2019-07-11 stsp
6563 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6564 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6565 818c7501 2019-07-11 stsp free(ondisk_path);
6566 818c7501 2019-07-11 stsp if (err)
6567 818c7501 2019-07-11 stsp return err;
6568 818c7501 2019-07-11 stsp
6569 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6570 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6571 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6572 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6573 818c7501 2019-07-11 stsp
6574 818c7501 2019-07-11 stsp return NULL;
6575 818c7501 2019-07-11 stsp }
6576 818c7501 2019-07-11 stsp
6577 818c7501 2019-07-11 stsp const struct got_error *
6578 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6579 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6580 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6581 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6582 818c7501 2019-07-11 stsp {
6583 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6584 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6585 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6586 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6587 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6588 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6589 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6590 818c7501 2019-07-11 stsp
6591 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6592 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6593 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6594 818c7501 2019-07-11 stsp
6595 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6596 818c7501 2019-07-11 stsp if (err)
6597 818c7501 2019-07-11 stsp return err;
6598 818c7501 2019-07-11 stsp
6599 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6600 818c7501 2019-07-11 stsp if (err)
6601 818c7501 2019-07-11 stsp goto done;
6602 818c7501 2019-07-11 stsp
6603 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6604 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6605 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6606 818c7501 2019-07-11 stsp &ok_arg);
6607 818c7501 2019-07-11 stsp if (err)
6608 818c7501 2019-07-11 stsp goto done;
6609 818c7501 2019-07-11 stsp
6610 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6611 818c7501 2019-07-11 stsp if (err)
6612 818c7501 2019-07-11 stsp goto done;
6613 818c7501 2019-07-11 stsp
6614 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6615 818c7501 2019-07-11 stsp if (err)
6616 818c7501 2019-07-11 stsp goto done;
6617 818c7501 2019-07-11 stsp
6618 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6619 818c7501 2019-07-11 stsp if (err)
6620 818c7501 2019-07-11 stsp goto done;
6621 818c7501 2019-07-11 stsp
6622 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6623 818c7501 2019-07-11 stsp 0);
6624 e51d7b55 2020-01-04 stsp if (err)
6625 e51d7b55 2020-01-04 stsp goto done;
6626 e51d7b55 2020-01-04 stsp
6627 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6628 818c7501 2019-07-11 stsp if (err)
6629 818c7501 2019-07-11 stsp goto done;
6630 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6631 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6632 e51d7b55 2020-01-04 stsp goto done;
6633 e51d7b55 2020-01-04 stsp }
6634 818c7501 2019-07-11 stsp
6635 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6636 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6637 818c7501 2019-07-11 stsp if (err)
6638 818c7501 2019-07-11 stsp goto done;
6639 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6640 818c7501 2019-07-11 stsp if (err)
6641 818c7501 2019-07-11 stsp goto done;
6642 818c7501 2019-07-11 stsp
6643 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6644 818c7501 2019-07-11 stsp
6645 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6646 818c7501 2019-07-11 stsp if (err)
6647 818c7501 2019-07-11 stsp goto done;
6648 818c7501 2019-07-11 stsp
6649 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6650 818c7501 2019-07-11 stsp if (err)
6651 818c7501 2019-07-11 stsp goto done;
6652 818c7501 2019-07-11 stsp
6653 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6654 818c7501 2019-07-11 stsp worktree->base_commit_id);
6655 818c7501 2019-07-11 stsp if (err)
6656 818c7501 2019-07-11 stsp goto done;
6657 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6658 818c7501 2019-07-11 stsp if (err)
6659 818c7501 2019-07-11 stsp goto done;
6660 818c7501 2019-07-11 stsp
6661 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6662 818c7501 2019-07-11 stsp if (err)
6663 818c7501 2019-07-11 stsp goto done;
6664 818c7501 2019-07-11 stsp done:
6665 818c7501 2019-07-11 stsp free(fileindex_path);
6666 818c7501 2019-07-11 stsp free(tmp_branch_name);
6667 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6668 818c7501 2019-07-11 stsp free(branch_ref_name);
6669 818c7501 2019-07-11 stsp if (branch_ref)
6670 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6671 818c7501 2019-07-11 stsp if (wt_branch)
6672 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6673 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6674 818c7501 2019-07-11 stsp if (err) {
6675 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6676 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6677 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6678 818c7501 2019-07-11 stsp }
6679 818c7501 2019-07-11 stsp if (*tmp_branch) {
6680 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6681 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6682 818c7501 2019-07-11 stsp }
6683 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6684 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6685 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6686 3e3a69f1 2019-07-25 stsp }
6687 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6688 818c7501 2019-07-11 stsp }
6689 818c7501 2019-07-11 stsp return err;
6690 818c7501 2019-07-11 stsp }
6691 818c7501 2019-07-11 stsp
6692 818c7501 2019-07-11 stsp const struct got_error *
6693 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6694 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6695 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6696 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6697 818c7501 2019-07-11 stsp {
6698 818c7501 2019-07-11 stsp const struct got_error *err;
6699 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6700 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6701 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6702 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6703 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6704 818c7501 2019-07-11 stsp
6705 818c7501 2019-07-11 stsp *commit_id = NULL;
6706 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6707 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6708 3e3a69f1 2019-07-25 stsp *branch = NULL;
6709 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6710 3e3a69f1 2019-07-25 stsp
6711 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6712 3e3a69f1 2019-07-25 stsp if (err)
6713 3e3a69f1 2019-07-25 stsp return err;
6714 818c7501 2019-07-11 stsp
6715 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6716 3e3a69f1 2019-07-25 stsp if (err)
6717 f032f1f7 2019-08-04 stsp goto done;
6718 f032f1f7 2019-08-04 stsp
6719 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6720 f032f1f7 2019-08-04 stsp &have_staged_files);
6721 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6722 f032f1f7 2019-08-04 stsp goto done;
6723 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6724 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6725 3e3a69f1 2019-07-25 stsp goto done;
6726 f032f1f7 2019-08-04 stsp }
6727 3e3a69f1 2019-07-25 stsp
6728 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6729 818c7501 2019-07-11 stsp if (err)
6730 f032f1f7 2019-08-04 stsp goto done;
6731 818c7501 2019-07-11 stsp
6732 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6733 818c7501 2019-07-11 stsp if (err)
6734 818c7501 2019-07-11 stsp goto done;
6735 818c7501 2019-07-11 stsp
6736 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6737 818c7501 2019-07-11 stsp if (err)
6738 818c7501 2019-07-11 stsp goto done;
6739 818c7501 2019-07-11 stsp
6740 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6741 818c7501 2019-07-11 stsp if (err)
6742 818c7501 2019-07-11 stsp goto done;
6743 818c7501 2019-07-11 stsp
6744 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6745 818c7501 2019-07-11 stsp if (err)
6746 818c7501 2019-07-11 stsp goto done;
6747 818c7501 2019-07-11 stsp
6748 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6749 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6750 818c7501 2019-07-11 stsp if (err)
6751 818c7501 2019-07-11 stsp goto done;
6752 818c7501 2019-07-11 stsp
6753 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6754 818c7501 2019-07-11 stsp if (err)
6755 818c7501 2019-07-11 stsp goto done;
6756 818c7501 2019-07-11 stsp
6757 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6758 818c7501 2019-07-11 stsp if (err)
6759 818c7501 2019-07-11 stsp goto done;
6760 818c7501 2019-07-11 stsp
6761 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6762 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6763 818c7501 2019-07-11 stsp if (err)
6764 818c7501 2019-07-11 stsp goto done;
6765 818c7501 2019-07-11 stsp
6766 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6767 818c7501 2019-07-11 stsp if (err)
6768 818c7501 2019-07-11 stsp goto done;
6769 818c7501 2019-07-11 stsp done:
6770 818c7501 2019-07-11 stsp free(commit_ref_name);
6771 818c7501 2019-07-11 stsp free(branch_ref_name);
6772 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6773 818c7501 2019-07-11 stsp if (commit_ref)
6774 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6775 818c7501 2019-07-11 stsp if (branch_ref)
6776 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6777 818c7501 2019-07-11 stsp if (err) {
6778 818c7501 2019-07-11 stsp free(*commit_id);
6779 818c7501 2019-07-11 stsp *commit_id = NULL;
6780 818c7501 2019-07-11 stsp if (*tmp_branch) {
6781 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6782 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6783 818c7501 2019-07-11 stsp }
6784 818c7501 2019-07-11 stsp if (*new_base_branch) {
6785 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6786 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6787 818c7501 2019-07-11 stsp }
6788 818c7501 2019-07-11 stsp if (*branch) {
6789 818c7501 2019-07-11 stsp got_ref_close(*branch);
6790 818c7501 2019-07-11 stsp *branch = NULL;
6791 818c7501 2019-07-11 stsp }
6792 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6793 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6794 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6795 3e3a69f1 2019-07-25 stsp }
6796 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6797 818c7501 2019-07-11 stsp }
6798 818c7501 2019-07-11 stsp return err;
6799 c4296144 2019-05-09 stsp }
6800 818c7501 2019-07-11 stsp
6801 818c7501 2019-07-11 stsp const struct got_error *
6802 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6803 818c7501 2019-07-11 stsp {
6804 818c7501 2019-07-11 stsp const struct got_error *err;
6805 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6806 818c7501 2019-07-11 stsp
6807 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6808 818c7501 2019-07-11 stsp if (err)
6809 818c7501 2019-07-11 stsp return err;
6810 818c7501 2019-07-11 stsp
6811 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6812 818c7501 2019-07-11 stsp free(tmp_branch_name);
6813 818c7501 2019-07-11 stsp return NULL;
6814 818c7501 2019-07-11 stsp }
6815 818c7501 2019-07-11 stsp
6816 818c7501 2019-07-11 stsp static const struct got_error *
6817 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6818 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
6819 818c7501 2019-07-11 stsp {
6820 0ebf8283 2019-07-24 stsp *logmsg = arg;
6821 818c7501 2019-07-11 stsp return NULL;
6822 818c7501 2019-07-11 stsp }
6823 818c7501 2019-07-11 stsp
6824 818c7501 2019-07-11 stsp static const struct got_error *
6825 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6826 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6827 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6828 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6829 818c7501 2019-07-11 stsp {
6830 818c7501 2019-07-11 stsp return NULL;
6831 01757395 2019-07-12 stsp }
6832 01757395 2019-07-12 stsp
6833 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6834 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6835 01757395 2019-07-12 stsp void *progress_arg;
6836 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6837 01757395 2019-07-12 stsp };
6838 01757395 2019-07-12 stsp
6839 01757395 2019-07-12 stsp static const struct got_error *
6840 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6841 01757395 2019-07-12 stsp {
6842 01757395 2019-07-12 stsp const struct got_error *err;
6843 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6844 01757395 2019-07-12 stsp char *p;
6845 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6846 01757395 2019-07-12 stsp
6847 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6848 01757395 2019-07-12 stsp if (err)
6849 01757395 2019-07-12 stsp return err;
6850 01757395 2019-07-12 stsp
6851 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6852 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6853 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6854 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6855 01757395 2019-07-12 stsp return NULL;
6856 01757395 2019-07-12 stsp
6857 01757395 2019-07-12 stsp p = strdup(path);
6858 01757395 2019-07-12 stsp if (p == NULL)
6859 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6860 01757395 2019-07-12 stsp
6861 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6862 01757395 2019-07-12 stsp if (err || new == NULL)
6863 01757395 2019-07-12 stsp free(p);
6864 01757395 2019-07-12 stsp return err;
6865 818c7501 2019-07-11 stsp }
6866 818c7501 2019-07-11 stsp
6867 0ebf8283 2019-07-24 stsp static const struct got_error *
6868 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6869 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6870 818c7501 2019-07-11 stsp {
6871 818c7501 2019-07-11 stsp const struct got_error *err;
6872 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6873 818c7501 2019-07-11 stsp
6874 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6875 818c7501 2019-07-11 stsp if (err) {
6876 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6877 818c7501 2019-07-11 stsp goto done;
6878 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6879 818c7501 2019-07-11 stsp if (err)
6880 818c7501 2019-07-11 stsp goto done;
6881 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6882 818c7501 2019-07-11 stsp if (err)
6883 818c7501 2019-07-11 stsp goto done;
6884 de05890f 2020-03-05 stsp } else if (is_rebase) {
6885 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6886 818c7501 2019-07-11 stsp int cmp;
6887 818c7501 2019-07-11 stsp
6888 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6889 818c7501 2019-07-11 stsp if (err)
6890 818c7501 2019-07-11 stsp goto done;
6891 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6892 818c7501 2019-07-11 stsp free(stored_id);
6893 818c7501 2019-07-11 stsp if (cmp != 0) {
6894 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6895 818c7501 2019-07-11 stsp goto done;
6896 818c7501 2019-07-11 stsp }
6897 818c7501 2019-07-11 stsp }
6898 0ebf8283 2019-07-24 stsp done:
6899 0ebf8283 2019-07-24 stsp if (commit_ref)
6900 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6901 0ebf8283 2019-07-24 stsp return err;
6902 0ebf8283 2019-07-24 stsp }
6903 0ebf8283 2019-07-24 stsp
6904 0ebf8283 2019-07-24 stsp static const struct got_error *
6905 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6906 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6907 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6908 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6909 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6910 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6911 0ebf8283 2019-07-24 stsp {
6912 0ebf8283 2019-07-24 stsp const struct got_error *err;
6913 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6914 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6915 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6916 818c7501 2019-07-11 stsp
6917 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6918 0ebf8283 2019-07-24 stsp
6919 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6920 0ebf8283 2019-07-24 stsp if (err)
6921 0ebf8283 2019-07-24 stsp return err;
6922 0ebf8283 2019-07-24 stsp
6923 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6924 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6925 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6926 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6927 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6928 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6929 818c7501 2019-07-11 stsp if (commit_ref)
6930 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6931 818c7501 2019-07-11 stsp return err;
6932 818c7501 2019-07-11 stsp }
6933 818c7501 2019-07-11 stsp
6934 818c7501 2019-07-11 stsp const struct got_error *
6935 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6936 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6937 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6938 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6939 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6940 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6941 818c7501 2019-07-11 stsp {
6942 0ebf8283 2019-07-24 stsp const struct got_error *err;
6943 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6944 0ebf8283 2019-07-24 stsp
6945 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6946 0ebf8283 2019-07-24 stsp if (err)
6947 0ebf8283 2019-07-24 stsp return err;
6948 0ebf8283 2019-07-24 stsp
6949 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
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 = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6954 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6955 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6956 0ebf8283 2019-07-24 stsp done:
6957 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6958 0ebf8283 2019-07-24 stsp return err;
6959 0ebf8283 2019-07-24 stsp }
6960 0ebf8283 2019-07-24 stsp
6961 0ebf8283 2019-07-24 stsp const struct got_error *
6962 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6963 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6964 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6965 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6966 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6967 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6968 0ebf8283 2019-07-24 stsp {
6969 0ebf8283 2019-07-24 stsp const struct got_error *err;
6970 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6971 0ebf8283 2019-07-24 stsp
6972 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6973 0ebf8283 2019-07-24 stsp if (err)
6974 0ebf8283 2019-07-24 stsp return err;
6975 0ebf8283 2019-07-24 stsp
6976 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6977 0ebf8283 2019-07-24 stsp if (err)
6978 0ebf8283 2019-07-24 stsp goto done;
6979 0ebf8283 2019-07-24 stsp
6980 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6981 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6982 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6983 0ebf8283 2019-07-24 stsp done:
6984 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6985 0ebf8283 2019-07-24 stsp return err;
6986 0ebf8283 2019-07-24 stsp }
6987 0ebf8283 2019-07-24 stsp
6988 0ebf8283 2019-07-24 stsp static const struct got_error *
6989 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6990 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6991 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6992 150d7275 2022-07-22 thomas struct got_reference *tmp_branch, const char *committer,
6993 150d7275 2022-07-22 thomas struct got_commit_object *orig_commit, const char *new_logmsg,
6994 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
6995 0ebf8283 2019-07-24 stsp {
6996 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6997 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6998 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6999 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7000 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7001 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7002 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7003 818c7501 2019-07-11 stsp
7004 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
7005 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7006 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7007 a0e95631 2019-07-12 stsp
7008 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7009 818c7501 2019-07-11 stsp
7010 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7011 a0e95631 2019-07-12 stsp if (err)
7012 3e3a69f1 2019-07-25 stsp return err;
7013 a0e95631 2019-07-12 stsp
7014 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7015 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7016 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7017 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7018 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
7019 01757395 2019-07-12 stsp /*
7020 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7021 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7022 6c13b005 2021-09-02 stsp *
7023 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7024 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7025 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7026 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7027 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7028 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7029 01757395 2019-07-12 stsp */
7030 01757395 2019-07-12 stsp if (merged_paths) {
7031 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7032 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7033 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7034 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7035 f2a9dc41 2019-12-13 tracey 0);
7036 01757395 2019-07-12 stsp if (err)
7037 01757395 2019-07-12 stsp goto done;
7038 01757395 2019-07-12 stsp }
7039 01757395 2019-07-12 stsp } else {
7040 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7041 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7042 01757395 2019-07-12 stsp if (err)
7043 01757395 2019-07-12 stsp goto done;
7044 01757395 2019-07-12 stsp }
7045 a0e95631 2019-07-12 stsp
7046 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7047 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7048 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7049 a0e95631 2019-07-12 stsp if (err)
7050 a0e95631 2019-07-12 stsp goto done;
7051 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7052 a0e95631 2019-07-12 stsp goto done;
7053 ff0d2220 2019-07-11 stsp }
7054 818c7501 2019-07-11 stsp
7055 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7056 edd02c5e 2019-07-11 stsp if (err)
7057 edd02c5e 2019-07-11 stsp goto done;
7058 edd02c5e 2019-07-11 stsp
7059 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7060 818c7501 2019-07-11 stsp if (err)
7061 818c7501 2019-07-11 stsp goto done;
7062 818c7501 2019-07-11 stsp
7063 5943eee2 2019-08-13 stsp if (new_logmsg) {
7064 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7065 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7066 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7067 5943eee2 2019-08-13 stsp goto done;
7068 5943eee2 2019-08-13 stsp }
7069 5943eee2 2019-08-13 stsp } else {
7070 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7071 5943eee2 2019-08-13 stsp if (err)
7072 5943eee2 2019-08-13 stsp goto done;
7073 5943eee2 2019-08-13 stsp }
7074 0ebf8283 2019-07-24 stsp
7075 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7076 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7077 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
7078 8db00f97 2022-07-22 thomas committer ? committer :
7079 ef899790 2022-11-01 thomas got_object_commit_get_committer(orig_commit), NULL,
7080 8db00f97 2022-07-22 thomas collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7081 a0e95631 2019-07-12 stsp if (err)
7082 a0e95631 2019-07-12 stsp goto done;
7083 a0e95631 2019-07-12 stsp
7084 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7085 a0e95631 2019-07-12 stsp if (err)
7086 a0e95631 2019-07-12 stsp goto done;
7087 a0e95631 2019-07-12 stsp
7088 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7089 a0e95631 2019-07-12 stsp if (err)
7090 a0e95631 2019-07-12 stsp goto done;
7091 a0e95631 2019-07-12 stsp
7092 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7093 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7094 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7095 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7096 a0e95631 2019-07-12 stsp err = sync_err;
7097 818c7501 2019-07-11 stsp done:
7098 a0e95631 2019-07-12 stsp free(fileindex_path);
7099 a0e95631 2019-07-12 stsp free(head_commit_id);
7100 a0e95631 2019-07-12 stsp if (head_ref)
7101 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7102 818c7501 2019-07-11 stsp if (err) {
7103 818c7501 2019-07-11 stsp free(*new_commit_id);
7104 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7105 818c7501 2019-07-11 stsp }
7106 818c7501 2019-07-11 stsp return err;
7107 818c7501 2019-07-11 stsp }
7108 818c7501 2019-07-11 stsp
7109 818c7501 2019-07-11 stsp const struct got_error *
7110 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7111 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7112 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7113 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7114 be94c032 2023-02-20 thomas struct got_object_id *orig_commit_id, int allow_conflict,
7115 be94c032 2023-02-20 thomas struct got_repository *repo)
7116 0ebf8283 2019-07-24 stsp {
7117 0ebf8283 2019-07-24 stsp const struct got_error *err;
7118 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7119 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7120 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7121 0ebf8283 2019-07-24 stsp
7122 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7123 0ebf8283 2019-07-24 stsp if (err)
7124 0ebf8283 2019-07-24 stsp return err;
7125 0ebf8283 2019-07-24 stsp
7126 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7127 0ebf8283 2019-07-24 stsp if (err)
7128 0ebf8283 2019-07-24 stsp goto done;
7129 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7130 0ebf8283 2019-07-24 stsp if (err)
7131 0ebf8283 2019-07-24 stsp goto done;
7132 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7133 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7134 0ebf8283 2019-07-24 stsp goto done;
7135 0ebf8283 2019-07-24 stsp }
7136 0ebf8283 2019-07-24 stsp
7137 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7138 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7139 be94c032 2023-02-20 thomas NULL, allow_conflict, repo);
7140 0ebf8283 2019-07-24 stsp done:
7141 0ebf8283 2019-07-24 stsp if (commit_ref)
7142 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7143 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7144 0ebf8283 2019-07-24 stsp free(commit_id);
7145 0ebf8283 2019-07-24 stsp return err;
7146 0ebf8283 2019-07-24 stsp }
7147 0ebf8283 2019-07-24 stsp
7148 0ebf8283 2019-07-24 stsp const struct got_error *
7149 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7150 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7151 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7152 150d7275 2022-07-22 thomas const char *committer, struct got_commit_object *orig_commit,
7153 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7154 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo)
7155 0ebf8283 2019-07-24 stsp {
7156 0ebf8283 2019-07-24 stsp const struct got_error *err;
7157 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7158 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7159 0ebf8283 2019-07-24 stsp
7160 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7161 0ebf8283 2019-07-24 stsp if (err)
7162 0ebf8283 2019-07-24 stsp return err;
7163 0ebf8283 2019-07-24 stsp
7164 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7165 0ebf8283 2019-07-24 stsp if (err)
7166 0ebf8283 2019-07-24 stsp goto done;
7167 0ebf8283 2019-07-24 stsp
7168 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7169 150d7275 2022-07-22 thomas worktree, fileindex, tmp_branch, committer, orig_commit,
7170 be94c032 2023-02-20 thomas new_logmsg, allow_conflict, repo);
7171 0ebf8283 2019-07-24 stsp done:
7172 0ebf8283 2019-07-24 stsp if (commit_ref)
7173 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7174 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7175 0ebf8283 2019-07-24 stsp return err;
7176 0ebf8283 2019-07-24 stsp }
7177 0ebf8283 2019-07-24 stsp
7178 0ebf8283 2019-07-24 stsp const struct got_error *
7179 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7180 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7181 818c7501 2019-07-11 stsp {
7182 3e3a69f1 2019-07-25 stsp if (fileindex)
7183 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7184 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7185 69844fba 2019-07-11 stsp }
7186 69844fba 2019-07-11 stsp
7187 69844fba 2019-07-11 stsp static const struct got_error *
7188 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7189 69844fba 2019-07-11 stsp {
7190 69844fba 2019-07-11 stsp const struct got_error *err;
7191 69844fba 2019-07-11 stsp struct got_reference *ref;
7192 69844fba 2019-07-11 stsp
7193 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7194 69844fba 2019-07-11 stsp if (err) {
7195 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7196 69844fba 2019-07-11 stsp return NULL;
7197 69844fba 2019-07-11 stsp return err;
7198 69844fba 2019-07-11 stsp }
7199 69844fba 2019-07-11 stsp
7200 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7201 69844fba 2019-07-11 stsp got_ref_close(ref);
7202 69844fba 2019-07-11 stsp return err;
7203 818c7501 2019-07-11 stsp }
7204 818c7501 2019-07-11 stsp
7205 69844fba 2019-07-11 stsp static const struct got_error *
7206 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7207 69844fba 2019-07-11 stsp {
7208 69844fba 2019-07-11 stsp const struct got_error *err;
7209 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7210 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7211 69844fba 2019-07-11 stsp
7212 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7213 69844fba 2019-07-11 stsp if (err)
7214 69844fba 2019-07-11 stsp goto done;
7215 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7216 69844fba 2019-07-11 stsp if (err)
7217 69844fba 2019-07-11 stsp goto done;
7218 69844fba 2019-07-11 stsp
7219 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7220 69844fba 2019-07-11 stsp if (err)
7221 69844fba 2019-07-11 stsp goto done;
7222 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7223 69844fba 2019-07-11 stsp if (err)
7224 69844fba 2019-07-11 stsp goto done;
7225 69844fba 2019-07-11 stsp
7226 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7227 69844fba 2019-07-11 stsp if (err)
7228 69844fba 2019-07-11 stsp goto done;
7229 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7230 69844fba 2019-07-11 stsp if (err)
7231 69844fba 2019-07-11 stsp goto done;
7232 69844fba 2019-07-11 stsp
7233 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7234 69844fba 2019-07-11 stsp if (err)
7235 69844fba 2019-07-11 stsp goto done;
7236 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7237 69844fba 2019-07-11 stsp if (err)
7238 69844fba 2019-07-11 stsp goto done;
7239 69844fba 2019-07-11 stsp
7240 69844fba 2019-07-11 stsp done:
7241 69844fba 2019-07-11 stsp free(tmp_branch_name);
7242 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7243 69844fba 2019-07-11 stsp free(branch_ref_name);
7244 69844fba 2019-07-11 stsp free(commit_ref_name);
7245 e600f124 2021-03-21 stsp return err;
7246 e600f124 2021-03-21 stsp }
7247 e600f124 2021-03-21 stsp
7248 ef20f542 2022-06-26 thomas static const struct got_error *
7249 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7250 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7251 e600f124 2021-03-21 stsp {
7252 e600f124 2021-03-21 stsp const struct got_error *err;
7253 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7254 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7255 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7256 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7257 e600f124 2021-03-21 stsp char *refname = NULL;
7258 e600f124 2021-03-21 stsp
7259 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7260 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7261 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7262 e600f124 2021-03-21 stsp branch_name += 11;
7263 e600f124 2021-03-21 stsp
7264 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7265 e600f124 2021-03-21 stsp if (err)
7266 e600f124 2021-03-21 stsp return err;
7267 e600f124 2021-03-21 stsp
7268 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7269 e600f124 2021-03-21 stsp new_id_str) == -1) {
7270 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7271 e600f124 2021-03-21 stsp goto done;
7272 e600f124 2021-03-21 stsp }
7273 e600f124 2021-03-21 stsp
7274 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7275 e600f124 2021-03-21 stsp if (err)
7276 e600f124 2021-03-21 stsp goto done;
7277 e600f124 2021-03-21 stsp
7278 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7279 e600f124 2021-03-21 stsp if (err)
7280 e600f124 2021-03-21 stsp goto done;
7281 e600f124 2021-03-21 stsp
7282 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7283 e600f124 2021-03-21 stsp done:
7284 e600f124 2021-03-21 stsp free(new_id_str);
7285 e600f124 2021-03-21 stsp free(refname);
7286 e600f124 2021-03-21 stsp free(old_commit_id);
7287 e600f124 2021-03-21 stsp if (ref)
7288 e600f124 2021-03-21 stsp got_ref_close(ref);
7289 69844fba 2019-07-11 stsp return err;
7290 69844fba 2019-07-11 stsp }
7291 69844fba 2019-07-11 stsp
7292 818c7501 2019-07-11 stsp const struct got_error *
7293 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7294 f08f28be 2023-02-03 thomas struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7295 f08f28be 2023-02-03 thomas struct got_reference *rebased_branch, struct got_repository *repo,
7296 f08f28be 2023-02-03 thomas int create_backup)
7297 818c7501 2019-07-11 stsp {
7298 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7299 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7300 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7301 818c7501 2019-07-11 stsp
7302 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7303 818c7501 2019-07-11 stsp if (err)
7304 818c7501 2019-07-11 stsp return err;
7305 e600f124 2021-03-21 stsp
7306 e600f124 2021-03-21 stsp if (create_backup) {
7307 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7308 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7309 e600f124 2021-03-21 stsp if (err)
7310 e600f124 2021-03-21 stsp goto done;
7311 e600f124 2021-03-21 stsp }
7312 818c7501 2019-07-11 stsp
7313 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7314 818c7501 2019-07-11 stsp if (err)
7315 818c7501 2019-07-11 stsp goto done;
7316 818c7501 2019-07-11 stsp
7317 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7318 818c7501 2019-07-11 stsp if (err)
7319 818c7501 2019-07-11 stsp goto done;
7320 818c7501 2019-07-11 stsp
7321 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7322 818c7501 2019-07-11 stsp if (err)
7323 818c7501 2019-07-11 stsp goto done;
7324 818c7501 2019-07-11 stsp
7325 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7326 a615e0e7 2020-12-16 stsp if (err)
7327 a615e0e7 2020-12-16 stsp goto done;
7328 a615e0e7 2020-12-16 stsp
7329 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7330 a615e0e7 2020-12-16 stsp if (err)
7331 a615e0e7 2020-12-16 stsp goto done;
7332 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7333 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7334 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7335 a615e0e7 2020-12-16 stsp err = sync_err;
7336 818c7501 2019-07-11 stsp done:
7337 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7338 a615e0e7 2020-12-16 stsp free(fileindex_path);
7339 818c7501 2019-07-11 stsp free(new_head_commit_id);
7340 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7341 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7342 818c7501 2019-07-11 stsp err = unlockerr;
7343 818c7501 2019-07-11 stsp return err;
7344 818c7501 2019-07-11 stsp }
7345 8641a332 2023-04-14 thomas
7346 8641a332 2023-04-14 thomas static const struct got_error *
7347 8641a332 2023-04-14 thomas get_paths_changed_between_commits(struct got_pathlist_head *paths,
7348 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7349 8641a332 2023-04-14 thomas struct got_repository *repo)
7350 8641a332 2023-04-14 thomas {
7351 8641a332 2023-04-14 thomas const struct got_error *err;
7352 8641a332 2023-04-14 thomas struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7353 8641a332 2023-04-14 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7354 8641a332 2023-04-14 thomas
7355 8641a332 2023-04-14 thomas if (id1) {
7356 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit1, repo, id1);
7357 8641a332 2023-04-14 thomas if (err)
7358 8641a332 2023-04-14 thomas goto done;
7359 8641a332 2023-04-14 thomas
7360 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree1, repo,
7361 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit1));
7362 8641a332 2023-04-14 thomas if (err)
7363 8641a332 2023-04-14 thomas goto done;
7364 8641a332 2023-04-14 thomas }
7365 818c7501 2019-07-11 stsp
7366 8641a332 2023-04-14 thomas if (id2) {
7367 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit2, repo, id2);
7368 8641a332 2023-04-14 thomas if (err)
7369 8641a332 2023-04-14 thomas goto done;
7370 8641a332 2023-04-14 thomas
7371 8641a332 2023-04-14 thomas err = got_object_open_as_tree(&tree2, repo,
7372 8641a332 2023-04-14 thomas got_object_commit_get_tree_id(commit2));
7373 8641a332 2023-04-14 thomas if (err)
7374 8641a332 2023-04-14 thomas goto done;
7375 8641a332 2023-04-14 thomas }
7376 8641a332 2023-04-14 thomas
7377 8641a332 2023-04-14 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7378 8641a332 2023-04-14 thomas got_diff_tree_collect_changed_paths, paths, 0);
7379 8641a332 2023-04-14 thomas if (err)
7380 8641a332 2023-04-14 thomas goto done;
7381 8641a332 2023-04-14 thomas done:
7382 8641a332 2023-04-14 thomas if (commit1)
7383 8641a332 2023-04-14 thomas got_object_commit_close(commit1);
7384 8641a332 2023-04-14 thomas if (commit2)
7385 8641a332 2023-04-14 thomas got_object_commit_close(commit2);
7386 8641a332 2023-04-14 thomas if (tree1)
7387 8641a332 2023-04-14 thomas got_object_tree_close(tree1);
7388 8641a332 2023-04-14 thomas if (tree2)
7389 8641a332 2023-04-14 thomas got_object_tree_close(tree2);
7390 8641a332 2023-04-14 thomas return err;
7391 8641a332 2023-04-14 thomas }
7392 8641a332 2023-04-14 thomas
7393 8641a332 2023-04-14 thomas static const struct got_error *
7394 8641a332 2023-04-14 thomas get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7395 8641a332 2023-04-14 thomas struct got_object_id *id1, struct got_object_id *id2,
7396 8641a332 2023-04-14 thomas const char *path_prefix, struct got_repository *repo)
7397 8641a332 2023-04-14 thomas {
7398 8641a332 2023-04-14 thomas const struct got_error *err;
7399 8641a332 2023-04-14 thomas struct got_pathlist_head merged_paths;
7400 8641a332 2023-04-14 thomas struct got_pathlist_entry *pe;
7401 8641a332 2023-04-14 thomas char *abspath = NULL, *wt_path = NULL;
7402 8641a332 2023-04-14 thomas
7403 8641a332 2023-04-14 thomas TAILQ_INIT(&merged_paths);
7404 8641a332 2023-04-14 thomas
7405 8641a332 2023-04-14 thomas err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7406 8641a332 2023-04-14 thomas if (err)
7407 8641a332 2023-04-14 thomas goto done;
7408 8641a332 2023-04-14 thomas
7409 8641a332 2023-04-14 thomas TAILQ_FOREACH(pe, &merged_paths, entry) {
7410 8641a332 2023-04-14 thomas struct got_diff_changed_path *change = pe->data;
7411 8641a332 2023-04-14 thomas
7412 8641a332 2023-04-14 thomas if (change->status != GOT_STATUS_ADD)
7413 8641a332 2023-04-14 thomas continue;
7414 8641a332 2023-04-14 thomas
7415 8641a332 2023-04-14 thomas if (got_path_is_root_dir(path_prefix)) {
7416 8641a332 2023-04-14 thomas wt_path = strdup(pe->path);
7417 8641a332 2023-04-14 thomas if (wt_path == NULL) {
7418 8641a332 2023-04-14 thomas err = got_error_from_errno("strdup");
7419 8641a332 2023-04-14 thomas goto done;
7420 8641a332 2023-04-14 thomas }
7421 8641a332 2023-04-14 thomas } else {
7422 8641a332 2023-04-14 thomas if (asprintf(&abspath, "/%s", pe->path) == -1) {
7423 8641a332 2023-04-14 thomas err = got_error_from_errno("asprintf");
7424 8641a332 2023-04-14 thomas goto done;
7425 8641a332 2023-04-14 thomas }
7426 8641a332 2023-04-14 thomas
7427 8641a332 2023-04-14 thomas err = got_path_skip_common_ancestor(&wt_path,
7428 8641a332 2023-04-14 thomas path_prefix, abspath);
7429 8641a332 2023-04-14 thomas if (err)
7430 8641a332 2023-04-14 thomas goto done;
7431 8641a332 2023-04-14 thomas free(abspath);
7432 8641a332 2023-04-14 thomas abspath = NULL;
7433 8641a332 2023-04-14 thomas }
7434 8641a332 2023-04-14 thomas
7435 8641a332 2023-04-14 thomas err = got_pathlist_append(added_paths, wt_path, NULL);
7436 8641a332 2023-04-14 thomas if (err)
7437 8641a332 2023-04-14 thomas goto done;
7438 8641a332 2023-04-14 thomas wt_path = NULL;
7439 8641a332 2023-04-14 thomas }
7440 8641a332 2023-04-14 thomas
7441 8641a332 2023-04-14 thomas done:
7442 8641a332 2023-04-14 thomas got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7443 8641a332 2023-04-14 thomas free(abspath);
7444 8641a332 2023-04-14 thomas free(wt_path);
7445 8641a332 2023-04-14 thomas return err;
7446 8641a332 2023-04-14 thomas }
7447 8641a332 2023-04-14 thomas
7448 8641a332 2023-04-14 thomas static const struct got_error *
7449 8641a332 2023-04-14 thomas get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7450 8641a332 2023-04-14 thomas struct got_object_id *id, const char *path_prefix,
7451 8641a332 2023-04-14 thomas struct got_repository *repo)
7452 8641a332 2023-04-14 thomas {
7453 8641a332 2023-04-14 thomas const struct got_error *err;
7454 8641a332 2023-04-14 thomas struct got_commit_object *commit = NULL;
7455 8641a332 2023-04-14 thomas struct got_object_qid *pid;
7456 8641a332 2023-04-14 thomas
7457 8641a332 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo, id);
7458 8641a332 2023-04-14 thomas if (err)
7459 8641a332 2023-04-14 thomas goto done;
7460 8641a332 2023-04-14 thomas
7461 8641a332 2023-04-14 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7462 8641a332 2023-04-14 thomas
7463 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(added_paths,
7464 8641a332 2023-04-14 thomas pid ? &pid->id : NULL, id, path_prefix, repo);
7465 8641a332 2023-04-14 thomas if (err)
7466 8641a332 2023-04-14 thomas goto done;
7467 8641a332 2023-04-14 thomas done:
7468 8641a332 2023-04-14 thomas if (commit)
7469 8641a332 2023-04-14 thomas got_object_commit_close(commit);
7470 8641a332 2023-04-14 thomas return err;
7471 8641a332 2023-04-14 thomas }
7472 8641a332 2023-04-14 thomas
7473 818c7501 2019-07-11 stsp const struct got_error *
7474 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7475 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7476 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7477 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7478 818c7501 2019-07-11 stsp {
7479 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7480 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7481 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7482 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7483 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7484 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7485 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7486 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7487 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7488 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7489 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7490 818c7501 2019-07-11 stsp
7491 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7492 8641a332 2023-04-14 thomas
7493 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7494 818c7501 2019-07-11 stsp if (err)
7495 818c7501 2019-07-11 stsp return err;
7496 8641a332 2023-04-14 thomas
7497 8641a332 2023-04-14 thomas err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7498 8641a332 2023-04-14 thomas if (err)
7499 8641a332 2023-04-14 thomas goto done;
7500 8641a332 2023-04-14 thomas
7501 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7502 8641a332 2023-04-14 thomas if (err)
7503 8641a332 2023-04-14 thomas goto done;
7504 8641a332 2023-04-14 thomas
7505 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7506 8641a332 2023-04-14 thomas if (err)
7507 8641a332 2023-04-14 thomas goto done;
7508 945f9229 2022-04-16 thomas
7509 8641a332 2023-04-14 thomas /*
7510 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7511 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7512 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
7513 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
7514 8641a332 2023-04-14 thomas */
7515 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7516 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7517 8641a332 2023-04-14 thomas if (err)
7518 8641a332 2023-04-14 thomas goto done;
7519 8641a332 2023-04-14 thomas
7520 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7521 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7522 818c7501 2019-07-11 stsp if (err)
7523 818c7501 2019-07-11 stsp goto done;
7524 818c7501 2019-07-11 stsp
7525 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7526 818c7501 2019-07-11 stsp if (err)
7527 818c7501 2019-07-11 stsp goto done;
7528 818c7501 2019-07-11 stsp
7529 818c7501 2019-07-11 stsp /*
7530 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7531 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7532 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7533 818c7501 2019-07-11 stsp */
7534 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7535 818c7501 2019-07-11 stsp if (err)
7536 818c7501 2019-07-11 stsp goto done;
7537 818c7501 2019-07-11 stsp
7538 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7539 25a102ed 2023-04-14 thomas if (err)
7540 25a102ed 2023-04-14 thomas goto done;
7541 25a102ed 2023-04-14 thomas
7542 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7543 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7544 818c7501 2019-07-11 stsp if (err)
7545 818c7501 2019-07-11 stsp goto done;
7546 818c7501 2019-07-11 stsp
7547 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7548 945f9229 2022-04-16 thomas worktree->path_prefix);
7549 ca355955 2019-07-12 stsp if (err)
7550 ca355955 2019-07-12 stsp goto done;
7551 ca355955 2019-07-12 stsp
7552 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7553 ca355955 2019-07-12 stsp if (err)
7554 ca355955 2019-07-12 stsp goto done;
7555 ca355955 2019-07-12 stsp
7556 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7557 818c7501 2019-07-11 stsp if (err)
7558 818c7501 2019-07-11 stsp goto done;
7559 818c7501 2019-07-11 stsp
7560 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7561 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7562 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7563 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7564 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7565 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7566 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7567 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7568 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7569 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7570 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7571 55bd499d 2019-07-12 stsp if (err)
7572 1f1abb7e 2019-08-08 stsp goto sync;
7573 55bd499d 2019-07-12 stsp
7574 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7575 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7576 ca355955 2019-07-12 stsp sync:
7577 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7578 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7579 ca355955 2019-07-12 stsp err = sync_err;
7580 818c7501 2019-07-11 stsp done:
7581 8641a332 2023-04-14 thomas got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7582 818c7501 2019-07-11 stsp got_ref_close(resolved);
7583 a3a2faf2 2019-07-12 stsp free(tree_id);
7584 818c7501 2019-07-11 stsp free(commit_id);
7585 8641a332 2023-04-14 thomas free(merged_commit_id);
7586 945f9229 2022-04-16 thomas if (commit)
7587 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7588 0ebf8283 2019-07-24 stsp if (fileindex)
7589 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7590 0ebf8283 2019-07-24 stsp free(fileindex_path);
7591 8641a332 2023-04-14 thomas free(commit_ref_name);
7592 8641a332 2023-04-14 thomas if (commit_ref)
7593 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7594 0ebf8283 2019-07-24 stsp
7595 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7596 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7597 0ebf8283 2019-07-24 stsp err = unlockerr;
7598 0ebf8283 2019-07-24 stsp return err;
7599 0ebf8283 2019-07-24 stsp }
7600 0ebf8283 2019-07-24 stsp
7601 0ebf8283 2019-07-24 stsp const struct got_error *
7602 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7603 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7604 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7605 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7606 0ebf8283 2019-07-24 stsp {
7607 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7608 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7609 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7610 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7611 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7612 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7613 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7614 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7615 0ebf8283 2019-07-24 stsp
7616 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7617 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7618 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7619 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7620 0ebf8283 2019-07-24 stsp
7621 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7622 0ebf8283 2019-07-24 stsp if (err)
7623 0ebf8283 2019-07-24 stsp return err;
7624 0ebf8283 2019-07-24 stsp
7625 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7626 0ebf8283 2019-07-24 stsp if (err)
7627 0ebf8283 2019-07-24 stsp goto done;
7628 0ebf8283 2019-07-24 stsp
7629 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7630 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7631 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7632 0ebf8283 2019-07-24 stsp &ok_arg);
7633 0ebf8283 2019-07-24 stsp if (err)
7634 0ebf8283 2019-07-24 stsp goto done;
7635 0ebf8283 2019-07-24 stsp
7636 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7637 0ebf8283 2019-07-24 stsp if (err)
7638 0ebf8283 2019-07-24 stsp goto done;
7639 0ebf8283 2019-07-24 stsp
7640 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7641 0ebf8283 2019-07-24 stsp if (err)
7642 0ebf8283 2019-07-24 stsp goto done;
7643 0ebf8283 2019-07-24 stsp
7644 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7645 0ebf8283 2019-07-24 stsp worktree);
7646 0ebf8283 2019-07-24 stsp if (err)
7647 0ebf8283 2019-07-24 stsp goto done;
7648 0ebf8283 2019-07-24 stsp
7649 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7650 0ebf8283 2019-07-24 stsp 0);
7651 0ebf8283 2019-07-24 stsp if (err)
7652 0ebf8283 2019-07-24 stsp goto done;
7653 0ebf8283 2019-07-24 stsp
7654 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7655 0ebf8283 2019-07-24 stsp if (err)
7656 0ebf8283 2019-07-24 stsp goto done;
7657 0ebf8283 2019-07-24 stsp
7658 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7659 0ebf8283 2019-07-24 stsp if (err)
7660 0ebf8283 2019-07-24 stsp goto done;
7661 0ebf8283 2019-07-24 stsp
7662 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7663 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7664 0ebf8283 2019-07-24 stsp if (err)
7665 0ebf8283 2019-07-24 stsp goto done;
7666 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7667 0ebf8283 2019-07-24 stsp if (err)
7668 0ebf8283 2019-07-24 stsp goto done;
7669 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7670 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7671 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7672 0ebf8283 2019-07-24 stsp goto done;
7673 0ebf8283 2019-07-24 stsp }
7674 0ebf8283 2019-07-24 stsp
7675 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7676 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7677 0ebf8283 2019-07-24 stsp if (err)
7678 0ebf8283 2019-07-24 stsp goto done;
7679 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7680 0ebf8283 2019-07-24 stsp if (err)
7681 0ebf8283 2019-07-24 stsp goto done;
7682 0ebf8283 2019-07-24 stsp
7683 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7684 0ebf8283 2019-07-24 stsp if (err)
7685 0ebf8283 2019-07-24 stsp goto done;
7686 0ebf8283 2019-07-24 stsp done:
7687 0ebf8283 2019-07-24 stsp free(fileindex_path);
7688 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7689 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7690 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7691 0ebf8283 2019-07-24 stsp if (wt_branch)
7692 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7693 0ebf8283 2019-07-24 stsp if (err) {
7694 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7695 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7696 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7697 0ebf8283 2019-07-24 stsp }
7698 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7699 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7700 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7701 0ebf8283 2019-07-24 stsp }
7702 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7703 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7704 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7705 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7706 3e3a69f1 2019-07-25 stsp }
7707 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7708 0ebf8283 2019-07-24 stsp }
7709 0ebf8283 2019-07-24 stsp return err;
7710 0ebf8283 2019-07-24 stsp }
7711 0ebf8283 2019-07-24 stsp
7712 0ebf8283 2019-07-24 stsp const struct got_error *
7713 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7714 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7715 0ebf8283 2019-07-24 stsp {
7716 3e3a69f1 2019-07-25 stsp if (fileindex)
7717 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7718 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7719 0ebf8283 2019-07-24 stsp }
7720 0ebf8283 2019-07-24 stsp
7721 0ebf8283 2019-07-24 stsp const struct got_error *
7722 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7723 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7724 0ebf8283 2019-07-24 stsp {
7725 0ebf8283 2019-07-24 stsp const struct got_error *err;
7726 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7727 0ebf8283 2019-07-24 stsp
7728 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7729 0ebf8283 2019-07-24 stsp if (err)
7730 0ebf8283 2019-07-24 stsp return err;
7731 0ebf8283 2019-07-24 stsp
7732 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7733 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7734 0ebf8283 2019-07-24 stsp return NULL;
7735 0ebf8283 2019-07-24 stsp }
7736 0ebf8283 2019-07-24 stsp
7737 0ebf8283 2019-07-24 stsp const struct got_error *
7738 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7739 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7740 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7741 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7742 0ebf8283 2019-07-24 stsp {
7743 0ebf8283 2019-07-24 stsp const struct got_error *err;
7744 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7745 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7746 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7747 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7748 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7749 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7750 0ebf8283 2019-07-24 stsp
7751 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7752 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7753 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7754 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7755 0ebf8283 2019-07-24 stsp
7756 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7757 3e3a69f1 2019-07-25 stsp if (err)
7758 3e3a69f1 2019-07-25 stsp return err;
7759 3e3a69f1 2019-07-25 stsp
7760 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7761 3e3a69f1 2019-07-25 stsp if (err)
7762 f032f1f7 2019-08-04 stsp goto done;
7763 f032f1f7 2019-08-04 stsp
7764 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7765 f032f1f7 2019-08-04 stsp &have_staged_files);
7766 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7767 f032f1f7 2019-08-04 stsp goto done;
7768 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7769 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7770 3e3a69f1 2019-07-25 stsp goto done;
7771 f032f1f7 2019-08-04 stsp }
7772 3e3a69f1 2019-07-25 stsp
7773 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7774 0ebf8283 2019-07-24 stsp if (err)
7775 f032f1f7 2019-08-04 stsp goto done;
7776 0ebf8283 2019-07-24 stsp
7777 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7778 0ebf8283 2019-07-24 stsp if (err)
7779 0ebf8283 2019-07-24 stsp goto done;
7780 0ebf8283 2019-07-24 stsp
7781 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7782 0ebf8283 2019-07-24 stsp if (err)
7783 0ebf8283 2019-07-24 stsp goto done;
7784 0ebf8283 2019-07-24 stsp
7785 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7786 0ebf8283 2019-07-24 stsp worktree);
7787 0ebf8283 2019-07-24 stsp if (err)
7788 0ebf8283 2019-07-24 stsp goto done;
7789 0ebf8283 2019-07-24 stsp
7790 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7791 0ebf8283 2019-07-24 stsp if (err)
7792 0ebf8283 2019-07-24 stsp goto done;
7793 0ebf8283 2019-07-24 stsp
7794 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7795 0ebf8283 2019-07-24 stsp if (err)
7796 0ebf8283 2019-07-24 stsp goto done;
7797 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7798 0ebf8283 2019-07-24 stsp if (err)
7799 0ebf8283 2019-07-24 stsp goto done;
7800 0ebf8283 2019-07-24 stsp
7801 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7802 0ebf8283 2019-07-24 stsp if (err)
7803 0ebf8283 2019-07-24 stsp goto done;
7804 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7805 0ebf8283 2019-07-24 stsp if (err)
7806 0ebf8283 2019-07-24 stsp goto done;
7807 0ebf8283 2019-07-24 stsp
7808 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7809 0ebf8283 2019-07-24 stsp if (err)
7810 0ebf8283 2019-07-24 stsp goto done;
7811 0ebf8283 2019-07-24 stsp done:
7812 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7813 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7814 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7815 0ebf8283 2019-07-24 stsp if (commit_ref)
7816 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7817 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7818 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7819 0ebf8283 2019-07-24 stsp if (err) {
7820 0ebf8283 2019-07-24 stsp free(*commit_id);
7821 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7822 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7823 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7824 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7825 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7826 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7827 0ebf8283 2019-07-24 stsp }
7828 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7829 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7830 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7831 3e3a69f1 2019-07-25 stsp }
7832 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7833 0ebf8283 2019-07-24 stsp }
7834 0ebf8283 2019-07-24 stsp return err;
7835 0ebf8283 2019-07-24 stsp }
7836 0ebf8283 2019-07-24 stsp
7837 0ebf8283 2019-07-24 stsp static const struct got_error *
7838 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7839 0ebf8283 2019-07-24 stsp {
7840 0ebf8283 2019-07-24 stsp const struct got_error *err;
7841 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7842 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7843 0ebf8283 2019-07-24 stsp
7844 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7845 0ebf8283 2019-07-24 stsp if (err)
7846 0ebf8283 2019-07-24 stsp goto done;
7847 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7848 0ebf8283 2019-07-24 stsp if (err)
7849 0ebf8283 2019-07-24 stsp goto done;
7850 0ebf8283 2019-07-24 stsp
7851 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7852 0ebf8283 2019-07-24 stsp worktree);
7853 0ebf8283 2019-07-24 stsp if (err)
7854 0ebf8283 2019-07-24 stsp goto done;
7855 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7856 0ebf8283 2019-07-24 stsp if (err)
7857 0ebf8283 2019-07-24 stsp goto done;
7858 0ebf8283 2019-07-24 stsp
7859 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7860 0ebf8283 2019-07-24 stsp if (err)
7861 0ebf8283 2019-07-24 stsp goto done;
7862 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7863 0ebf8283 2019-07-24 stsp if (err)
7864 0ebf8283 2019-07-24 stsp goto done;
7865 0ebf8283 2019-07-24 stsp
7866 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7867 0ebf8283 2019-07-24 stsp if (err)
7868 0ebf8283 2019-07-24 stsp goto done;
7869 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7870 0ebf8283 2019-07-24 stsp if (err)
7871 0ebf8283 2019-07-24 stsp goto done;
7872 0ebf8283 2019-07-24 stsp done:
7873 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7874 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7875 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7876 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7877 0ebf8283 2019-07-24 stsp return err;
7878 0ebf8283 2019-07-24 stsp }
7879 0ebf8283 2019-07-24 stsp
7880 0ebf8283 2019-07-24 stsp const struct got_error *
7881 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7882 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7883 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7884 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7885 0ebf8283 2019-07-24 stsp {
7886 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7887 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7888 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7889 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
7890 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7891 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
7892 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
7893 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7894 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7895 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
7896 0ebf8283 2019-07-24 stsp
7897 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
7898 8641a332 2023-04-14 thomas
7899 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7900 0ebf8283 2019-07-24 stsp if (err)
7901 0ebf8283 2019-07-24 stsp return err;
7902 945f9229 2022-04-16 thomas
7903 8641a332 2023-04-14 thomas err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7904 8641a332 2023-04-14 thomas if (err)
7905 8641a332 2023-04-14 thomas goto done;
7906 8641a332 2023-04-14 thomas
7907 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7908 8641a332 2023-04-14 thomas if (err) {
7909 8641a332 2023-04-14 thomas if (err->code != GOT_ERR_NOT_REF)
7910 8641a332 2023-04-14 thomas goto done;
7911 8641a332 2023-04-14 thomas /* Can happen on early abort due to invalid histedit script. */
7912 8641a332 2023-04-14 thomas commit_ref = NULL;
7913 8641a332 2023-04-14 thomas }
7914 8641a332 2023-04-14 thomas
7915 8641a332 2023-04-14 thomas if (commit_ref) {
7916 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7917 8641a332 2023-04-14 thomas if (err)
7918 8641a332 2023-04-14 thomas goto done;
7919 8641a332 2023-04-14 thomas
7920 8641a332 2023-04-14 thomas /*
7921 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
7922 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
7923 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files added by the
7924 8641a332 2023-04-14 thomas * user during conflict resolution or during histedit -e.
7925 8641a332 2023-04-14 thomas */
7926 8641a332 2023-04-14 thomas err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7927 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
7928 8641a332 2023-04-14 thomas if (err)
7929 8641a332 2023-04-14 thomas goto done;
7930 8641a332 2023-04-14 thomas }
7931 8641a332 2023-04-14 thomas
7932 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7933 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7934 0ebf8283 2019-07-24 stsp if (err)
7935 0ebf8283 2019-07-24 stsp goto done;
7936 0ebf8283 2019-07-24 stsp
7937 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7938 0ebf8283 2019-07-24 stsp if (err)
7939 0ebf8283 2019-07-24 stsp goto done;
7940 0ebf8283 2019-07-24 stsp
7941 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7942 0ebf8283 2019-07-24 stsp if (err)
7943 0ebf8283 2019-07-24 stsp goto done;
7944 0ebf8283 2019-07-24 stsp
7945 25a102ed 2023-04-14 thomas err = got_object_open_as_commit(&commit, repo,
7946 25a102ed 2023-04-14 thomas worktree->base_commit_id);
7947 25a102ed 2023-04-14 thomas if (err)
7948 25a102ed 2023-04-14 thomas goto done;
7949 25a102ed 2023-04-14 thomas
7950 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
7951 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7952 0ebf8283 2019-07-24 stsp if (err)
7953 0ebf8283 2019-07-24 stsp goto done;
7954 0ebf8283 2019-07-24 stsp
7955 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7956 0ebf8283 2019-07-24 stsp if (err)
7957 0ebf8283 2019-07-24 stsp goto done;
7958 0ebf8283 2019-07-24 stsp
7959 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7960 0ebf8283 2019-07-24 stsp if (err)
7961 0ebf8283 2019-07-24 stsp goto done;
7962 0ebf8283 2019-07-24 stsp
7963 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7964 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7965 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7966 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7967 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7968 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7969 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7970 8641a332 2023-04-14 thomas rfa.unlink_added_files = 1;
7971 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
7972 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7973 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7974 0ebf8283 2019-07-24 stsp if (err)
7975 1f1abb7e 2019-08-08 stsp goto sync;
7976 0ebf8283 2019-07-24 stsp
7977 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7978 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7979 0ebf8283 2019-07-24 stsp sync:
7980 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7981 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7982 0ebf8283 2019-07-24 stsp err = sync_err;
7983 0ebf8283 2019-07-24 stsp done:
7984 8641a332 2023-04-14 thomas if (resolved)
7985 8641a332 2023-04-14 thomas got_ref_close(resolved);
7986 8641a332 2023-04-14 thomas if (commit_ref)
7987 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
7988 8641a332 2023-04-14 thomas free(merged_commit_id);
7989 0ebf8283 2019-07-24 stsp free(tree_id);
7990 818c7501 2019-07-11 stsp free(fileindex_path);
7991 8641a332 2023-04-14 thomas free(commit_ref_name);
7992 818c7501 2019-07-11 stsp
7993 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7994 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7995 818c7501 2019-07-11 stsp err = unlockerr;
7996 818c7501 2019-07-11 stsp return err;
7997 818c7501 2019-07-11 stsp }
7998 0ebf8283 2019-07-24 stsp
7999 0ebf8283 2019-07-24 stsp const struct got_error *
8000 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8001 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8002 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8003 0ebf8283 2019-07-24 stsp {
8004 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8005 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8006 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8007 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8008 0ebf8283 2019-07-24 stsp
8009 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8010 0ebf8283 2019-07-24 stsp if (err)
8011 0ebf8283 2019-07-24 stsp return err;
8012 0ebf8283 2019-07-24 stsp
8013 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8014 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8015 e600f124 2021-03-21 stsp if (err)
8016 e600f124 2021-03-21 stsp goto done;
8017 e600f124 2021-03-21 stsp
8018 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8019 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8020 0ebf8283 2019-07-24 stsp if (err)
8021 0ebf8283 2019-07-24 stsp goto done;
8022 0ebf8283 2019-07-24 stsp
8023 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8024 0ebf8283 2019-07-24 stsp if (err)
8025 0ebf8283 2019-07-24 stsp goto done;
8026 0ebf8283 2019-07-24 stsp
8027 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8028 0ebf8283 2019-07-24 stsp if (err)
8029 0ebf8283 2019-07-24 stsp goto done;
8030 0ebf8283 2019-07-24 stsp
8031 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8032 0ebf8283 2019-07-24 stsp if (err)
8033 0ebf8283 2019-07-24 stsp goto done;
8034 0ebf8283 2019-07-24 stsp
8035 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8036 a615e0e7 2020-12-16 stsp if (err)
8037 a615e0e7 2020-12-16 stsp goto done;
8038 a615e0e7 2020-12-16 stsp
8039 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8040 a615e0e7 2020-12-16 stsp if (err)
8041 a615e0e7 2020-12-16 stsp goto done;
8042 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8043 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8044 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8045 a615e0e7 2020-12-16 stsp err = sync_err;
8046 0ebf8283 2019-07-24 stsp done:
8047 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8048 a615e0e7 2020-12-16 stsp free(fileindex_path);
8049 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8050 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8051 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8052 0ebf8283 2019-07-24 stsp err = unlockerr;
8053 0ebf8283 2019-07-24 stsp return err;
8054 0ebf8283 2019-07-24 stsp }
8055 0ebf8283 2019-07-24 stsp
8056 0ebf8283 2019-07-24 stsp const struct got_error *
8057 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8058 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8059 0ebf8283 2019-07-24 stsp {
8060 0ebf8283 2019-07-24 stsp const struct got_error *err;
8061 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8062 0ebf8283 2019-07-24 stsp
8063 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8064 0ebf8283 2019-07-24 stsp if (err)
8065 0ebf8283 2019-07-24 stsp return err;
8066 0ebf8283 2019-07-24 stsp
8067 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8068 0ebf8283 2019-07-24 stsp if (err)
8069 0ebf8283 2019-07-24 stsp goto done;
8070 0ebf8283 2019-07-24 stsp
8071 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8072 0ebf8283 2019-07-24 stsp done:
8073 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8074 2822a352 2019-10-15 stsp return err;
8075 2822a352 2019-10-15 stsp }
8076 2822a352 2019-10-15 stsp
8077 2822a352 2019-10-15 stsp const struct got_error *
8078 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8079 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8080 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8081 2822a352 2019-10-15 stsp struct got_repository *repo)
8082 2822a352 2019-10-15 stsp {
8083 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8084 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8085 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8086 2822a352 2019-10-15 stsp
8087 2822a352 2019-10-15 stsp *fileindex = NULL;
8088 2822a352 2019-10-15 stsp *branch_ref = NULL;
8089 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8090 2822a352 2019-10-15 stsp
8091 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8092 2822a352 2019-10-15 stsp if (err)
8093 2822a352 2019-10-15 stsp return err;
8094 2822a352 2019-10-15 stsp
8095 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8096 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8097 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8098 2822a352 2019-10-15 stsp "update -b or different branch name required");
8099 2822a352 2019-10-15 stsp goto done;
8100 2822a352 2019-10-15 stsp }
8101 2822a352 2019-10-15 stsp
8102 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8103 2822a352 2019-10-15 stsp if (err)
8104 2822a352 2019-10-15 stsp goto done;
8105 2822a352 2019-10-15 stsp
8106 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8107 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8108 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8109 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8110 2822a352 2019-10-15 stsp &ok_arg);
8111 2822a352 2019-10-15 stsp if (err)
8112 2822a352 2019-10-15 stsp goto done;
8113 2822a352 2019-10-15 stsp
8114 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8115 2822a352 2019-10-15 stsp if (err)
8116 2822a352 2019-10-15 stsp goto done;
8117 2822a352 2019-10-15 stsp
8118 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8119 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8120 2822a352 2019-10-15 stsp done:
8121 2822a352 2019-10-15 stsp if (err) {
8122 2822a352 2019-10-15 stsp if (*branch_ref) {
8123 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8124 2822a352 2019-10-15 stsp *branch_ref = NULL;
8125 2822a352 2019-10-15 stsp }
8126 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8127 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8128 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8129 2822a352 2019-10-15 stsp }
8130 2822a352 2019-10-15 stsp if (*fileindex) {
8131 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8132 2822a352 2019-10-15 stsp *fileindex = NULL;
8133 2822a352 2019-10-15 stsp }
8134 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8135 2822a352 2019-10-15 stsp }
8136 0cb83759 2019-08-03 stsp return err;
8137 0cb83759 2019-08-03 stsp }
8138 0cb83759 2019-08-03 stsp
8139 2822a352 2019-10-15 stsp const struct got_error *
8140 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8141 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8142 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8143 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8144 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8145 2822a352 2019-10-15 stsp {
8146 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8147 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8148 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8149 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8150 2822a352 2019-10-15 stsp
8151 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8152 2822a352 2019-10-15 stsp if (err)
8153 2822a352 2019-10-15 stsp goto done;
8154 2822a352 2019-10-15 stsp
8155 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8156 2822a352 2019-10-15 stsp if (err)
8157 2822a352 2019-10-15 stsp goto done;
8158 2822a352 2019-10-15 stsp
8159 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
8160 945f9229 2022-04-16 thomas if (err)
8161 945f9229 2022-04-16 thomas goto done;
8162 945f9229 2022-04-16 thomas
8163 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8164 2822a352 2019-10-15 stsp worktree->path_prefix);
8165 2822a352 2019-10-15 stsp if (err)
8166 2822a352 2019-10-15 stsp goto done;
8167 2822a352 2019-10-15 stsp
8168 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8169 2822a352 2019-10-15 stsp if (err)
8170 2822a352 2019-10-15 stsp goto done;
8171 2822a352 2019-10-15 stsp
8172 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8173 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8174 2822a352 2019-10-15 stsp if (err)
8175 2822a352 2019-10-15 stsp goto sync;
8176 2822a352 2019-10-15 stsp
8177 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8178 2822a352 2019-10-15 stsp if (err)
8179 2822a352 2019-10-15 stsp goto sync;
8180 2822a352 2019-10-15 stsp
8181 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8182 6e210706 2021-01-22 stsp if (err)
8183 6e210706 2021-01-22 stsp goto sync;
8184 6e210706 2021-01-22 stsp
8185 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8186 2822a352 2019-10-15 stsp sync:
8187 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8188 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8189 2822a352 2019-10-15 stsp err = sync_err;
8190 2822a352 2019-10-15 stsp
8191 2822a352 2019-10-15 stsp done:
8192 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8193 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8194 2822a352 2019-10-15 stsp err = unlockerr;
8195 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8196 2822a352 2019-10-15 stsp
8197 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8198 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8199 2822a352 2019-10-15 stsp err = unlockerr;
8200 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8201 2822a352 2019-10-15 stsp
8202 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8203 2822a352 2019-10-15 stsp free(fileindex_path);
8204 2822a352 2019-10-15 stsp free(tree_id);
8205 945f9229 2022-04-16 thomas if (commit)
8206 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8207 2822a352 2019-10-15 stsp
8208 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8209 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8210 2822a352 2019-10-15 stsp err = unlockerr;
8211 2822a352 2019-10-15 stsp return err;
8212 2822a352 2019-10-15 stsp }
8213 2822a352 2019-10-15 stsp
8214 2822a352 2019-10-15 stsp const struct got_error *
8215 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8216 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8217 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8218 2822a352 2019-10-15 stsp {
8219 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8220 8b692cd0 2019-10-21 stsp
8221 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8222 8b692cd0 2019-10-21 stsp
8223 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8224 8b692cd0 2019-10-21 stsp
8225 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8226 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8227 8b692cd0 2019-10-21 stsp err = unlockerr;
8228 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8229 8b692cd0 2019-10-21 stsp
8230 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8231 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8232 8b692cd0 2019-10-21 stsp err = unlockerr;
8233 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8234 10604dce 2021-09-24 thomas
8235 10604dce 2021-09-24 thomas return err;
8236 10604dce 2021-09-24 thomas }
8237 10604dce 2021-09-24 thomas
8238 10604dce 2021-09-24 thomas const struct got_error *
8239 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
8240 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
8241 10604dce 2021-09-24 thomas {
8242 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
8243 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8244 10604dce 2021-09-24 thomas
8245 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8246 10604dce 2021-09-24 thomas if (err)
8247 10604dce 2021-09-24 thomas goto done;
8248 8b692cd0 2019-10-21 stsp
8249 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8250 10604dce 2021-09-24 thomas
8251 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
8252 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8253 10604dce 2021-09-24 thomas err = sync_err;
8254 10604dce 2021-09-24 thomas done:
8255 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8256 10604dce 2021-09-24 thomas free(fileindex_path);
8257 8b692cd0 2019-10-21 stsp return err;
8258 2822a352 2019-10-15 stsp }
8259 2822a352 2019-10-15 stsp
8260 10604dce 2021-09-24 thomas static const struct got_error *
8261 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8262 10604dce 2021-09-24 thomas {
8263 10604dce 2021-09-24 thomas const struct got_error *err;
8264 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8265 10604dce 2021-09-24 thomas
8266 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8267 10604dce 2021-09-24 thomas if (err)
8268 10604dce 2021-09-24 thomas goto done;
8269 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
8270 10604dce 2021-09-24 thomas if (err)
8271 10604dce 2021-09-24 thomas goto done;
8272 10604dce 2021-09-24 thomas
8273 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8274 10604dce 2021-09-24 thomas if (err)
8275 10604dce 2021-09-24 thomas goto done;
8276 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
8277 10604dce 2021-09-24 thomas if (err)
8278 10604dce 2021-09-24 thomas goto done;
8279 10604dce 2021-09-24 thomas
8280 10604dce 2021-09-24 thomas done:
8281 10604dce 2021-09-24 thomas free(branch_refname);
8282 10604dce 2021-09-24 thomas free(commit_refname);
8283 10604dce 2021-09-24 thomas return err;
8284 10604dce 2021-09-24 thomas }
8285 10604dce 2021-09-24 thomas
8286 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
8287 10604dce 2021-09-24 thomas struct got_worktree *worktree;
8288 10604dce 2021-09-24 thomas const char *branch_name;
8289 10604dce 2021-09-24 thomas };
8290 10604dce 2021-09-24 thomas
8291 10604dce 2021-09-24 thomas static const struct got_error *
8292 ef899790 2022-11-01 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8293 ef899790 2022-11-01 thomas const char *diff_path, char **logmsg, void *arg)
8294 10604dce 2021-09-24 thomas {
8295 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
8296 10604dce 2021-09-24 thomas
8297 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8298 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
8299 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
8300 10604dce 2021-09-24 thomas
8301 10604dce 2021-09-24 thomas return NULL;
8302 10604dce 2021-09-24 thomas }
8303 10604dce 2021-09-24 thomas
8304 10604dce 2021-09-24 thomas
8305 10604dce 2021-09-24 thomas const struct got_error *
8306 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
8307 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
8308 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
8309 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
8310 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8311 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8312 10604dce 2021-09-24 thomas {
8313 10604dce 2021-09-24 thomas const struct got_error *err;
8314 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8315 10604dce 2021-09-24 thomas
8316 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8317 10604dce 2021-09-24 thomas if (err)
8318 10604dce 2021-09-24 thomas goto done;
8319 10604dce 2021-09-24 thomas
8320 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8321 10604dce 2021-09-24 thomas worktree);
8322 10604dce 2021-09-24 thomas if (err)
8323 10604dce 2021-09-24 thomas goto done;
8324 10604dce 2021-09-24 thomas
8325 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8326 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
8327 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
8328 10604dce 2021-09-24 thomas done:
8329 10604dce 2021-09-24 thomas free(fileindex_path);
8330 10604dce 2021-09-24 thomas return err;
8331 10604dce 2021-09-24 thomas }
8332 10604dce 2021-09-24 thomas
8333 10604dce 2021-09-24 thomas const struct got_error *
8334 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
8335 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
8336 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
8337 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
8338 be94c032 2023-02-20 thomas int allow_conflict, struct got_repository *repo,
8339 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
8340 ae1e948a 2021-09-28 thomas
8341 10604dce 2021-09-24 thomas {
8342 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
8343 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
8344 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
8345 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
8346 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
8347 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
8348 10604dce 2021-09-24 thomas int have_staged_files = 0;
8349 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
8350 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8351 10604dce 2021-09-24 thomas
8352 ef899790 2022-11-01 thomas memset(&cc_arg, 0, sizeof(cc_arg));
8353 10604dce 2021-09-24 thomas *new_commit_id = NULL;
8354 10604dce 2021-09-24 thomas
8355 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
8356 10604dce 2021-09-24 thomas
8357 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8358 10604dce 2021-09-24 thomas if (err)
8359 10604dce 2021-09-24 thomas goto done;
8360 10604dce 2021-09-24 thomas
8361 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8362 10604dce 2021-09-24 thomas if (err)
8363 10604dce 2021-09-24 thomas goto done;
8364 10604dce 2021-09-24 thomas
8365 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
8366 10604dce 2021-09-24 thomas if (err)
8367 10604dce 2021-09-24 thomas goto done;
8368 10604dce 2021-09-24 thomas
8369 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8370 10604dce 2021-09-24 thomas &have_staged_files);
8371 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8372 10604dce 2021-09-24 thomas goto done;
8373 10604dce 2021-09-24 thomas if (have_staged_files) {
8374 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8375 10604dce 2021-09-24 thomas goto done;
8376 10604dce 2021-09-24 thomas }
8377 10604dce 2021-09-24 thomas
8378 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
8379 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
8380 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
8381 10604dce 2021-09-24 thomas cc_arg.repo = repo;
8382 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
8383 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8384 be94c032 2023-02-20 thomas cc_arg.commit_conflicts = allow_conflict;
8385 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8386 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8387 10604dce 2021-09-24 thomas if (err)
8388 10604dce 2021-09-24 thomas goto done;
8389 10604dce 2021-09-24 thomas
8390 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8391 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8392 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
8393 10604dce 2021-09-24 thomas
8394 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
8395 10604dce 2021-09-24 thomas ct_path++;
8396 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
8397 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8398 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8399 10604dce 2021-09-24 thomas if (err)
8400 10604dce 2021-09-24 thomas goto done;
8401 10604dce 2021-09-24 thomas
8402 10604dce 2021-09-24 thomas }
8403 10604dce 2021-09-24 thomas
8404 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
8405 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
8406 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
8407 ef899790 2022-11-01 thomas head_commit_id, branch_tip, worktree, author, committer, NULL,
8408 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8409 10604dce 2021-09-24 thomas if (err)
8410 10604dce 2021-09-24 thomas goto done;
8411 10604dce 2021-09-24 thomas
8412 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
8413 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
8414 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8415 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8416 10604dce 2021-09-24 thomas err = sync_err;
8417 10604dce 2021-09-24 thomas done:
8418 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
8419 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
8420 21c2d8be 2023-01-10 thomas
8421 10604dce 2021-09-24 thomas free_commitable(ct);
8422 10604dce 2021-09-24 thomas }
8423 21c2d8be 2023-01-10 thomas got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8424 10604dce 2021-09-24 thomas free(fileindex_path);
8425 10604dce 2021-09-24 thomas return err;
8426 10604dce 2021-09-24 thomas }
8427 10604dce 2021-09-24 thomas
8428 10604dce 2021-09-24 thomas const struct got_error *
8429 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
8430 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
8431 10604dce 2021-09-24 thomas {
8432 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8433 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8434 10604dce 2021-09-24 thomas
8435 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8436 10604dce 2021-09-24 thomas if (err)
8437 10604dce 2021-09-24 thomas goto done;
8438 10604dce 2021-09-24 thomas
8439 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8440 10604dce 2021-09-24 thomas if (err)
8441 10604dce 2021-09-24 thomas goto done;
8442 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8443 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8444 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8445 10604dce 2021-09-24 thomas err = sync_err;
8446 10604dce 2021-09-24 thomas done:
8447 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8448 10604dce 2021-09-24 thomas free(fileindex_path);
8449 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8450 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8451 10604dce 2021-09-24 thomas err = unlockerr;
8452 10604dce 2021-09-24 thomas return err;
8453 10604dce 2021-09-24 thomas }
8454 10604dce 2021-09-24 thomas
8455 10604dce 2021-09-24 thomas const struct got_error *
8456 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8457 10604dce 2021-09-24 thomas struct got_repository *repo)
8458 10604dce 2021-09-24 thomas {
8459 10604dce 2021-09-24 thomas const struct got_error *err;
8460 10604dce 2021-09-24 thomas char *branch_refname = NULL;
8461 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
8462 10604dce 2021-09-24 thomas
8463 10604dce 2021-09-24 thomas *in_progress = 0;
8464 10604dce 2021-09-24 thomas
8465 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8466 10604dce 2021-09-24 thomas if (err)
8467 10604dce 2021-09-24 thomas return err;
8468 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8469 dfe86d1f 2021-09-24 thomas free(branch_refname);
8470 10604dce 2021-09-24 thomas if (err) {
8471 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
8472 10604dce 2021-09-24 thomas return err;
8473 10604dce 2021-09-24 thomas } else
8474 10604dce 2021-09-24 thomas *in_progress = 1;
8475 10604dce 2021-09-24 thomas
8476 10604dce 2021-09-24 thomas return NULL;
8477 10604dce 2021-09-24 thomas }
8478 10604dce 2021-09-24 thomas
8479 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
8480 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
8481 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
8482 10604dce 2021-09-24 thomas {
8483 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
8484 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8485 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
8486 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8487 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
8488 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8489 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
8490 10604dce 2021-09-24 thomas
8491 10604dce 2021-09-24 thomas *fileindex = NULL;
8492 10604dce 2021-09-24 thomas
8493 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8494 10604dce 2021-09-24 thomas if (err)
8495 10604dce 2021-09-24 thomas return err;
8496 10604dce 2021-09-24 thomas
8497 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8498 10604dce 2021-09-24 thomas if (err)
8499 10604dce 2021-09-24 thomas goto done;
8500 10604dce 2021-09-24 thomas
8501 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
8502 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
8503 10604dce 2021-09-24 thomas ok_arg.repo = repo;
8504 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8505 10604dce 2021-09-24 thomas &ok_arg);
8506 10604dce 2021-09-24 thomas if (err)
8507 10604dce 2021-09-24 thomas goto done;
8508 10604dce 2021-09-24 thomas
8509 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8510 10604dce 2021-09-24 thomas if (err)
8511 10604dce 2021-09-24 thomas return err;
8512 10604dce 2021-09-24 thomas
8513 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8514 10604dce 2021-09-24 thomas if (err)
8515 10604dce 2021-09-24 thomas return err;
8516 10604dce 2021-09-24 thomas
8517 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8518 10604dce 2021-09-24 thomas 0);
8519 10604dce 2021-09-24 thomas if (err)
8520 10604dce 2021-09-24 thomas goto done;
8521 10604dce 2021-09-24 thomas
8522 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8523 10604dce 2021-09-24 thomas if (err)
8524 10604dce 2021-09-24 thomas goto done;
8525 10604dce 2021-09-24 thomas
8526 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8527 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8528 10604dce 2021-09-24 thomas goto done;
8529 10604dce 2021-09-24 thomas }
8530 10604dce 2021-09-24 thomas
8531 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
8532 10604dce 2021-09-24 thomas if (err)
8533 10604dce 2021-09-24 thomas goto done;
8534 10604dce 2021-09-24 thomas
8535 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8536 10604dce 2021-09-24 thomas if (err)
8537 10604dce 2021-09-24 thomas goto done;
8538 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
8539 10604dce 2021-09-24 thomas if (err)
8540 10604dce 2021-09-24 thomas goto done;
8541 10604dce 2021-09-24 thomas
8542 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8543 10604dce 2021-09-24 thomas if (err)
8544 10604dce 2021-09-24 thomas goto done;
8545 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
8546 10604dce 2021-09-24 thomas if (err)
8547 10604dce 2021-09-24 thomas goto done;
8548 10604dce 2021-09-24 thomas
8549 10604dce 2021-09-24 thomas done:
8550 10604dce 2021-09-24 thomas free(branch_refname);
8551 10604dce 2021-09-24 thomas free(commit_refname);
8552 10604dce 2021-09-24 thomas free(fileindex_path);
8553 10604dce 2021-09-24 thomas if (branch_ref)
8554 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8555 10604dce 2021-09-24 thomas if (commit_ref)
8556 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8557 10604dce 2021-09-24 thomas if (wt_branch)
8558 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
8559 10604dce 2021-09-24 thomas free(wt_branch_tip);
8560 10604dce 2021-09-24 thomas if (err) {
8561 10604dce 2021-09-24 thomas if (*fileindex) {
8562 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8563 10604dce 2021-09-24 thomas *fileindex = NULL;
8564 10604dce 2021-09-24 thomas }
8565 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8566 10604dce 2021-09-24 thomas }
8567 10604dce 2021-09-24 thomas return err;
8568 10604dce 2021-09-24 thomas }
8569 10604dce 2021-09-24 thomas
8570 10604dce 2021-09-24 thomas const struct got_error *
8571 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
8572 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8573 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
8574 10604dce 2021-09-24 thomas {
8575 10604dce 2021-09-24 thomas const struct got_error *err;
8576 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
8577 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8578 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8579 10604dce 2021-09-24 thomas int have_staged_files = 0;
8580 10604dce 2021-09-24 thomas
8581 10604dce 2021-09-24 thomas *branch_name = NULL;
8582 10604dce 2021-09-24 thomas *branch_tip = NULL;
8583 10604dce 2021-09-24 thomas *fileindex = NULL;
8584 10604dce 2021-09-24 thomas
8585 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
8586 10604dce 2021-09-24 thomas if (err)
8587 10604dce 2021-09-24 thomas return err;
8588 10604dce 2021-09-24 thomas
8589 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
8590 10604dce 2021-09-24 thomas if (err)
8591 10604dce 2021-09-24 thomas goto done;
8592 10604dce 2021-09-24 thomas
8593 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8594 10604dce 2021-09-24 thomas &have_staged_files);
8595 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
8596 10604dce 2021-09-24 thomas goto done;
8597 10604dce 2021-09-24 thomas if (have_staged_files) {
8598 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
8599 10604dce 2021-09-24 thomas goto done;
8600 10604dce 2021-09-24 thomas }
8601 10604dce 2021-09-24 thomas
8602 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
8603 10604dce 2021-09-24 thomas if (err)
8604 10604dce 2021-09-24 thomas goto done;
8605 10604dce 2021-09-24 thomas
8606 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
8607 10604dce 2021-09-24 thomas if (err)
8608 10604dce 2021-09-24 thomas goto done;
8609 10604dce 2021-09-24 thomas
8610 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8611 10604dce 2021-09-24 thomas if (err)
8612 10604dce 2021-09-24 thomas goto done;
8613 10604dce 2021-09-24 thomas
8614 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
8615 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8616 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
8617 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
8618 10604dce 2021-09-24 thomas goto done;
8619 10604dce 2021-09-24 thomas }
8620 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8621 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
8622 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
8623 10604dce 2021-09-24 thomas goto done;
8624 10604dce 2021-09-24 thomas }
8625 10604dce 2021-09-24 thomas
8626 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8627 10604dce 2021-09-24 thomas if (err)
8628 10604dce 2021-09-24 thomas goto done;
8629 10604dce 2021-09-24 thomas
8630 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
8631 10604dce 2021-09-24 thomas if (err)
8632 10604dce 2021-09-24 thomas goto done;
8633 10604dce 2021-09-24 thomas done:
8634 10604dce 2021-09-24 thomas free(commit_refname);
8635 10604dce 2021-09-24 thomas free(branch_refname);
8636 10604dce 2021-09-24 thomas free(fileindex_path);
8637 10604dce 2021-09-24 thomas if (commit_ref)
8638 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
8639 10604dce 2021-09-24 thomas if (branch_ref)
8640 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
8641 10604dce 2021-09-24 thomas if (err) {
8642 10604dce 2021-09-24 thomas if (*branch_name) {
8643 10604dce 2021-09-24 thomas free(*branch_name);
8644 10604dce 2021-09-24 thomas *branch_name = NULL;
8645 10604dce 2021-09-24 thomas }
8646 10604dce 2021-09-24 thomas free(*branch_tip);
8647 10604dce 2021-09-24 thomas *branch_tip = NULL;
8648 10604dce 2021-09-24 thomas if (*fileindex) {
8649 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
8650 10604dce 2021-09-24 thomas *fileindex = NULL;
8651 10604dce 2021-09-24 thomas }
8652 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
8653 10604dce 2021-09-24 thomas }
8654 10604dce 2021-09-24 thomas return err;
8655 10604dce 2021-09-24 thomas }
8656 10604dce 2021-09-24 thomas
8657 10604dce 2021-09-24 thomas const struct got_error *
8658 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
8659 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
8660 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
8661 10604dce 2021-09-24 thomas {
8662 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
8663 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8664 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
8665 10604dce 2021-09-24 thomas struct revert_file_args rfa;
8666 8641a332 2023-04-14 thomas char *commit_ref_name = NULL;
8667 8641a332 2023-04-14 thomas struct got_reference *commit_ref = NULL;
8668 8641a332 2023-04-14 thomas struct got_object_id *merged_commit_id = NULL;
8669 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
8670 8641a332 2023-04-14 thomas struct got_pathlist_head added_paths;
8671 8641a332 2023-04-14 thomas
8672 8641a332 2023-04-14 thomas TAILQ_INIT(&added_paths);
8673 8641a332 2023-04-14 thomas
8674 8641a332 2023-04-14 thomas err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8675 8641a332 2023-04-14 thomas if (err)
8676 8641a332 2023-04-14 thomas goto done;
8677 8641a332 2023-04-14 thomas
8678 8641a332 2023-04-14 thomas err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8679 8641a332 2023-04-14 thomas if (err)
8680 8641a332 2023-04-14 thomas goto done;
8681 8641a332 2023-04-14 thomas
8682 8641a332 2023-04-14 thomas err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8683 8641a332 2023-04-14 thomas if (err)
8684 8641a332 2023-04-14 thomas goto done;
8685 8641a332 2023-04-14 thomas
8686 8641a332 2023-04-14 thomas /*
8687 8641a332 2023-04-14 thomas * Determine which files in added status can be safely removed
8688 8641a332 2023-04-14 thomas * from disk while reverting changes in the work tree.
8689 8641a332 2023-04-14 thomas * We want to avoid deleting unrelated files which were added by
8690 8641a332 2023-04-14 thomas * the user for conflict resolution purposes.
8691 8641a332 2023-04-14 thomas */
8692 8641a332 2023-04-14 thomas err = get_paths_added_between_commits(&added_paths,
8693 8641a332 2023-04-14 thomas got_worktree_get_base_commit_id(worktree), merged_commit_id,
8694 8641a332 2023-04-14 thomas got_worktree_get_path_prefix(worktree), repo);
8695 8641a332 2023-04-14 thomas if (err)
8696 8641a332 2023-04-14 thomas goto done;
8697 10604dce 2021-09-24 thomas
8698 8641a332 2023-04-14 thomas
8699 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, repo,
8700 945f9229 2022-04-16 thomas worktree->base_commit_id);
8701 945f9229 2022-04-16 thomas if (err)
8702 945f9229 2022-04-16 thomas goto done;
8703 945f9229 2022-04-16 thomas
8704 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, repo, commit,
8705 945f9229 2022-04-16 thomas worktree->path_prefix);
8706 10604dce 2021-09-24 thomas if (err)
8707 10604dce 2021-09-24 thomas goto done;
8708 10604dce 2021-09-24 thomas
8709 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
8710 10604dce 2021-09-24 thomas if (err)
8711 10604dce 2021-09-24 thomas goto done;
8712 10604dce 2021-09-24 thomas
8713 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
8714 10604dce 2021-09-24 thomas if (err)
8715 10604dce 2021-09-24 thomas goto done;
8716 10604dce 2021-09-24 thomas
8717 10604dce 2021-09-24 thomas rfa.worktree = worktree;
8718 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
8719 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
8720 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
8721 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
8722 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
8723 10604dce 2021-09-24 thomas rfa.repo = repo;
8724 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
8725 8641a332 2023-04-14 thomas rfa.added_files_to_unlink = &added_paths;
8726 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
8727 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
8728 10604dce 2021-09-24 thomas if (err)
8729 10604dce 2021-09-24 thomas goto sync;
8730 10604dce 2021-09-24 thomas
8731 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8732 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
8733 10604dce 2021-09-24 thomas sync:
8734 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
8735 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
8736 10604dce 2021-09-24 thomas err = sync_err;
8737 10604dce 2021-09-24 thomas done:
8738 10604dce 2021-09-24 thomas free(tree_id);
8739 8641a332 2023-04-14 thomas free(merged_commit_id);
8740 945f9229 2022-04-16 thomas if (commit)
8741 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8742 10604dce 2021-09-24 thomas if (fileindex)
8743 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
8744 10604dce 2021-09-24 thomas free(fileindex_path);
8745 8641a332 2023-04-14 thomas if (commit_ref)
8746 8641a332 2023-04-14 thomas got_ref_close(commit_ref);
8747 8641a332 2023-04-14 thomas free(commit_ref_name);
8748 10604dce 2021-09-24 thomas
8749 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
8750 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
8751 10604dce 2021-09-24 thomas err = unlockerr;
8752 10604dce 2021-09-24 thomas return err;
8753 10604dce 2021-09-24 thomas }
8754 10604dce 2021-09-24 thomas
8755 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8756 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8757 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8758 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8759 2db2652d 2019-08-07 stsp struct got_repository *repo;
8760 2db2652d 2019-08-07 stsp int have_changes;
8761 2db2652d 2019-08-07 stsp };
8762 2db2652d 2019-08-07 stsp
8763 ef20f542 2022-06-26 thomas static const struct got_error *
8764 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8765 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8766 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8767 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8768 735ef5ac 2019-08-03 stsp {
8769 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8770 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8771 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8772 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8773 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8774 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8775 8b13ce36 2019-08-08 stsp
8776 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8777 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8778 8b13ce36 2019-08-08 stsp return NULL;
8779 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8780 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8781 735ef5ac 2019-08-03 stsp
8782 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8783 735ef5ac 2019-08-03 stsp if (ie == NULL)
8784 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8785 735ef5ac 2019-08-03 stsp
8786 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8787 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8788 735ef5ac 2019-08-03 stsp relpath) == -1)
8789 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8790 735ef5ac 2019-08-03 stsp
8791 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8792 43010591 2023-02-17 thomas base_commit_idp = got_fileindex_entry_get_commit_id(
8793 43010591 2023-02-17 thomas &base_commit_id, ie);
8794 735ef5ac 2019-08-03 stsp }
8795 735ef5ac 2019-08-03 stsp
8796 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8797 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8798 3aa5969e 2019-08-06 stsp goto done;
8799 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8800 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8801 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8802 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8803 735ef5ac 2019-08-03 stsp goto done;
8804 3aa5969e 2019-08-06 stsp }
8805 735ef5ac 2019-08-03 stsp
8806 2db2652d 2019-08-07 stsp a->have_changes = 1;
8807 2db2652d 2019-08-07 stsp
8808 735ef5ac 2019-08-03 stsp p = in_repo_path;
8809 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8810 735ef5ac 2019-08-03 stsp p++;
8811 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8812 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8813 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8814 735ef5ac 2019-08-03 stsp done:
8815 735ef5ac 2019-08-03 stsp free(in_repo_path);
8816 dc424a06 2019-08-07 stsp return err;
8817 dc424a06 2019-08-07 stsp }
8818 dc424a06 2019-08-07 stsp
8819 2db2652d 2019-08-07 stsp struct stage_path_arg {
8820 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8821 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8822 2db2652d 2019-08-07 stsp struct got_repository *repo;
8823 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8824 2db2652d 2019-08-07 stsp void *status_arg;
8825 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8826 2db2652d 2019-08-07 stsp void *patch_arg;
8827 7b5dc508 2019-10-28 stsp int staged_something;
8828 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8829 2db2652d 2019-08-07 stsp };
8830 2db2652d 2019-08-07 stsp
8831 2db2652d 2019-08-07 stsp static const struct got_error *
8832 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8833 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8834 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8835 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8836 0cb83759 2019-08-03 stsp {
8837 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8838 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8839 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8840 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8841 0cb83759 2019-08-03 stsp uint32_t stage;
8842 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8843 0aeb8099 2020-07-23 stsp struct stat sb;
8844 8b13ce36 2019-08-08 stsp
8845 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8846 8b13ce36 2019-08-08 stsp return NULL;
8847 0cb83759 2019-08-03 stsp
8848 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8849 d3e7c587 2019-08-03 stsp if (ie == NULL)
8850 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8851 0cb83759 2019-08-03 stsp
8852 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8853 2db2652d 2019-08-07 stsp relpath)== -1)
8854 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8855 0cb83759 2019-08-03 stsp
8856 0cb83759 2019-08-03 stsp switch (status) {
8857 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8858 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8859 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8860 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8861 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8862 0aeb8099 2020-07-23 stsp break;
8863 0aeb8099 2020-07-23 stsp }
8864 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8865 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8866 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8867 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8868 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8869 dc424a06 2019-08-07 stsp if (err)
8870 dc424a06 2019-08-07 stsp break;
8871 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8872 dc424a06 2019-08-07 stsp break;
8873 dc424a06 2019-08-07 stsp } else {
8874 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8875 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8876 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8877 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8878 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8879 dc424a06 2019-08-07 stsp break;
8880 dc424a06 2019-08-07 stsp }
8881 dc424a06 2019-08-07 stsp }
8882 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8883 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8884 0cb83759 2019-08-03 stsp if (err)
8885 dc424a06 2019-08-07 stsp break;
8886 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8887 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8888 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8889 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8890 0cb83759 2019-08-03 stsp else
8891 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8892 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8893 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8894 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8895 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8896 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8897 35213c7c 2020-07-23 stsp ssize_t target_len;
8898 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8899 35213c7c 2020-07-23 stsp sizeof(target_path));
8900 35213c7c 2020-07-23 stsp if (target_len == -1) {
8901 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8902 35213c7c 2020-07-23 stsp ondisk_path);
8903 35213c7c 2020-07-23 stsp break;
8904 35213c7c 2020-07-23 stsp }
8905 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8906 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8907 35213c7c 2020-07-23 stsp a->worktree->root_path);
8908 35213c7c 2020-07-23 stsp if (err)
8909 35213c7c 2020-07-23 stsp break;
8910 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8911 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8912 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8913 35213c7c 2020-07-23 stsp break;
8914 35213c7c 2020-07-23 stsp }
8915 35213c7c 2020-07-23 stsp }
8916 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8917 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8918 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8919 35213c7c 2020-07-23 stsp else
8920 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8921 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8922 0aeb8099 2020-07-23 stsp } else {
8923 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8924 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8925 0aeb8099 2020-07-23 stsp }
8926 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8927 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8928 dc424a06 2019-08-07 stsp break;
8929 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8930 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8931 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8932 f289c82c 2022-06-13 thomas if (err)
8933 f289c82c 2022-06-13 thomas break;
8934 f289c82c 2022-06-13 thomas /*
8935 f289c82c 2022-06-13 thomas * When staging the reverse of the staged diff,
8936 f289c82c 2022-06-13 thomas * implicitly unstage the file.
8937 f289c82c 2022-06-13 thomas */
8938 f289c82c 2022-06-13 thomas if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8939 f289c82c 2022-06-13 thomas sizeof(ie->blob_sha1)) == 0) {
8940 f289c82c 2022-06-13 thomas got_fileindex_entry_stage_set(ie,
8941 f289c82c 2022-06-13 thomas GOT_FILEIDX_STAGE_NONE);
8942 f289c82c 2022-06-13 thomas }
8943 0cb83759 2019-08-03 stsp break;
8944 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8945 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8946 d3e7c587 2019-08-03 stsp break;
8947 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8948 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8949 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8950 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8951 dc424a06 2019-08-07 stsp if (err)
8952 dc424a06 2019-08-07 stsp break;
8953 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8954 88f33a19 2019-08-08 stsp break;
8955 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8956 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8957 dc424a06 2019-08-07 stsp break;
8958 88f33a19 2019-08-08 stsp }
8959 dc424a06 2019-08-07 stsp }
8960 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8961 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8962 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8963 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8964 dc424a06 2019-08-07 stsp break;
8965 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8966 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8967 12463d8b 2019-12-13 stsp de_name);
8968 0cb83759 2019-08-03 stsp break;
8969 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8970 d3e7c587 2019-08-03 stsp break;
8971 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8972 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8973 ebf48fd5 2019-08-03 stsp break;
8974 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8975 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8976 2a06fe5f 2019-08-24 stsp break;
8977 0cb83759 2019-08-03 stsp default:
8978 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8979 537ac44b 2019-08-03 stsp break;
8980 0cb83759 2019-08-03 stsp }
8981 dc424a06 2019-08-07 stsp
8982 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8983 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8984 dc424a06 2019-08-07 stsp free(path_content);
8985 2db2652d 2019-08-07 stsp free(ondisk_path);
8986 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8987 0ebf8283 2019-07-24 stsp return err;
8988 0ebf8283 2019-07-24 stsp }
8989 0cb83759 2019-08-03 stsp
8990 0cb83759 2019-08-03 stsp const struct got_error *
8991 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8992 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8993 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8994 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8995 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8996 0cb83759 2019-08-03 stsp {
8997 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8998 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8999 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9000 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9001 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9002 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9003 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9004 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9005 0cb83759 2019-08-03 stsp
9006 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9007 0cb83759 2019-08-03 stsp if (err)
9008 0cb83759 2019-08-03 stsp return err;
9009 0cb83759 2019-08-03 stsp
9010 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9011 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9012 735ef5ac 2019-08-03 stsp if (err)
9013 735ef5ac 2019-08-03 stsp goto done;
9014 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9015 735ef5ac 2019-08-03 stsp if (err)
9016 735ef5ac 2019-08-03 stsp goto done;
9017 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9018 0cb83759 2019-08-03 stsp if (err)
9019 0cb83759 2019-08-03 stsp goto done;
9020 0cb83759 2019-08-03 stsp
9021 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9022 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9023 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9024 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9025 2db2652d 2019-08-07 stsp oka.repo = repo;
9026 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9027 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9028 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9029 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
9030 735ef5ac 2019-08-03 stsp if (err)
9031 735ef5ac 2019-08-03 stsp goto done;
9032 735ef5ac 2019-08-03 stsp }
9033 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9034 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9035 2db2652d 2019-08-07 stsp goto done;
9036 2db2652d 2019-08-07 stsp }
9037 735ef5ac 2019-08-03 stsp
9038 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9039 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9040 2db2652d 2019-08-07 stsp spa.repo = repo;
9041 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9042 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9043 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9044 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9045 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9046 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9047 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9048 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9049 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
9050 42005733 2019-08-03 stsp if (err)
9051 2db2652d 2019-08-07 stsp goto done;
9052 7b5dc508 2019-10-28 stsp }
9053 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9054 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9055 7b5dc508 2019-10-28 stsp goto done;
9056 0cb83759 2019-08-03 stsp }
9057 0cb83759 2019-08-03 stsp
9058 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9059 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9060 0cb83759 2019-08-03 stsp err = sync_err;
9061 0cb83759 2019-08-03 stsp done:
9062 735ef5ac 2019-08-03 stsp if (head_ref)
9063 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9064 735ef5ac 2019-08-03 stsp free(head_commit_id);
9065 0cb83759 2019-08-03 stsp free(fileindex_path);
9066 0cb83759 2019-08-03 stsp if (fileindex)
9067 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9068 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9069 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9070 0cb83759 2019-08-03 stsp err = unlockerr;
9071 0cb83759 2019-08-03 stsp return err;
9072 0cb83759 2019-08-03 stsp }
9073 ad493afc 2019-08-03 stsp
9074 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9075 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9076 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9077 ad493afc 2019-08-03 stsp struct got_repository *repo;
9078 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9079 ad493afc 2019-08-03 stsp void *progress_arg;
9080 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9081 2e1f37b0 2019-08-08 stsp void *patch_arg;
9082 ad493afc 2019-08-03 stsp };
9083 2e1f37b0 2019-08-08 stsp
9084 2e1f37b0 2019-08-08 stsp static const struct got_error *
9085 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9086 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9087 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9088 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9089 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9090 2e1f37b0 2019-08-08 stsp {
9091 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9092 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9093 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9094 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9095 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9096 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9097 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9098 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9099 2e1f37b0 2019-08-08 stsp
9100 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9101 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9102 2e1f37b0 2019-08-08 stsp
9103 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9104 2e1f37b0 2019-08-08 stsp if (err)
9105 2e1f37b0 2019-08-08 stsp return err;
9106 f4ae6ddb 2022-07-01 thomas
9107 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9108 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9109 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9110 f4ae6ddb 2022-07-01 thomas goto done;
9111 f4ae6ddb 2022-07-01 thomas }
9112 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9113 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9114 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9115 f4ae6ddb 2022-07-01 thomas goto done;
9116 f4ae6ddb 2022-07-01 thomas }
9117 f4ae6ddb 2022-07-01 thomas
9118 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9119 2e1f37b0 2019-08-08 stsp if (err)
9120 2e1f37b0 2019-08-08 stsp goto done;
9121 2e1f37b0 2019-08-08 stsp
9122 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9123 2e1f37b0 2019-08-08 stsp if (err)
9124 2e1f37b0 2019-08-08 stsp goto done;
9125 2e1f37b0 2019-08-08 stsp
9126 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9127 2e1f37b0 2019-08-08 stsp if (err)
9128 2e1f37b0 2019-08-08 stsp goto done;
9129 2e1f37b0 2019-08-08 stsp
9130 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9131 f4ae6ddb 2022-07-01 thomas fd2);
9132 2e1f37b0 2019-08-08 stsp if (err)
9133 2e1f37b0 2019-08-08 stsp goto done;
9134 2e1f37b0 2019-08-08 stsp
9135 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9136 2e1f37b0 2019-08-08 stsp if (err)
9137 2e1f37b0 2019-08-08 stsp goto done;
9138 ad493afc 2019-08-03 stsp
9139 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9140 2e1f37b0 2019-08-08 stsp if (err)
9141 2e1f37b0 2019-08-08 stsp goto done;
9142 2e1f37b0 2019-08-08 stsp
9143 dd2e2f52 2022-07-01 thomas err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9144 25ec7006 2022-07-01 thomas path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9145 2e1f37b0 2019-08-08 stsp if (err)
9146 2e1f37b0 2019-08-08 stsp goto done;
9147 2e1f37b0 2019-08-08 stsp
9148 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9149 fc2a50f2 2022-11-01 thomas "got-unstaged-content", "");
9150 2e1f37b0 2019-08-08 stsp if (err)
9151 2e1f37b0 2019-08-08 stsp goto done;
9152 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9153 fc2a50f2 2022-11-01 thomas "got-new-staged-content", "");
9154 2e1f37b0 2019-08-08 stsp if (err)
9155 2e1f37b0 2019-08-08 stsp goto done;
9156 2e1f37b0 2019-08-08 stsp
9157 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9158 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9159 2e1f37b0 2019-08-08 stsp goto done;
9160 2e1f37b0 2019-08-08 stsp }
9161 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9162 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9163 2e1f37b0 2019-08-08 stsp goto done;
9164 2e1f37b0 2019-08-08 stsp }
9165 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9166 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9167 f4ae6ddb 2022-07-01 thomas struct diff_chunk_context cc = {};
9168 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9169 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9170 fe621944 2020-11-10 stsp nchanges++;
9171 fe621944 2020-11-10 stsp }
9172 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9173 2e1f37b0 2019-08-08 stsp int choice;
9174 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9175 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9176 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9177 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9178 2e1f37b0 2019-08-08 stsp if (err)
9179 2e1f37b0 2019-08-08 stsp goto done;
9180 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9181 2e1f37b0 2019-08-08 stsp have_content = 1;
9182 19e4b907 2019-08-08 stsp else
9183 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9184 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9185 2e1f37b0 2019-08-08 stsp break;
9186 2e1f37b0 2019-08-08 stsp }
9187 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9188 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9189 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9190 2e1f37b0 2019-08-08 stsp done:
9191 2e1f37b0 2019-08-08 stsp free(label1);
9192 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9193 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9194 2e1f37b0 2019-08-08 stsp if (blob)
9195 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9196 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9197 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9198 2e1f37b0 2019-08-08 stsp if (staged_blob)
9199 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9200 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9201 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9202 fe621944 2020-11-10 stsp err = free_err;
9203 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9204 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9205 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9206 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9207 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9208 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9209 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9210 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9211 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9212 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9213 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9214 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9215 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9216 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9217 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9218 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9219 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9220 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9221 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9222 2e1f37b0 2019-08-08 stsp }
9223 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9224 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9225 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9226 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9227 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9228 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9229 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9230 2e1f37b0 2019-08-08 stsp }
9231 2e1f37b0 2019-08-08 stsp free(path1);
9232 2e1f37b0 2019-08-08 stsp free(path2);
9233 fda8017d 2020-07-23 stsp return err;
9234 fda8017d 2020-07-23 stsp }
9235 fda8017d 2020-07-23 stsp
9236 fda8017d 2020-07-23 stsp static const struct got_error *
9237 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9238 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9239 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9240 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9241 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9242 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9243 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9244 fda8017d 2020-07-23 stsp {
9245 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9246 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9247 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9248 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9249 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9250 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9251 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9252 fda8017d 2020-07-23 stsp struct stat sb;
9253 fda8017d 2020-07-23 stsp
9254 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9255 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9256 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9257 fda8017d 2020-07-23 stsp if (err)
9258 fda8017d 2020-07-23 stsp return err;
9259 fda8017d 2020-07-23 stsp
9260 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9261 fda8017d 2020-07-23 stsp return NULL;
9262 fda8017d 2020-07-23 stsp
9263 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9264 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9265 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9266 fda8017d 2020-07-23 stsp if (err)
9267 fda8017d 2020-07-23 stsp goto done;
9268 fda8017d 2020-07-23 stsp }
9269 fda8017d 2020-07-23 stsp
9270 c56c5d8a 2021-12-31 thomas f = fopen(path_unstaged_content, "re");
9271 fda8017d 2020-07-23 stsp if (f == NULL) {
9272 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9273 fda8017d 2020-07-23 stsp path_unstaged_content);
9274 fda8017d 2020-07-23 stsp goto done;
9275 fda8017d 2020-07-23 stsp }
9276 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9277 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9278 fda8017d 2020-07-23 stsp goto done;
9279 fda8017d 2020-07-23 stsp }
9280 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9281 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9282 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9283 fda8017d 2020-07-23 stsp size_t r;
9284 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9285 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9286 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9287 fda8017d 2020-07-23 stsp goto done;
9288 fda8017d 2020-07-23 stsp }
9289 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9290 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9291 fda8017d 2020-07-23 stsp goto done;
9292 fda8017d 2020-07-23 stsp }
9293 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9294 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9295 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9296 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9297 fda8017d 2020-07-23 stsp progress_arg);
9298 fda8017d 2020-07-23 stsp } else {
9299 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9300 67a66647 2021-05-31 stsp
9301 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9302 67a66647 2021-05-31 stsp if (err)
9303 67a66647 2021-05-31 stsp return err;
9304 67a66647 2021-05-31 stsp
9305 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9306 67a66647 2021-05-31 stsp parent) == -1) {
9307 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9308 67a66647 2021-05-31 stsp base_path = NULL;
9309 67a66647 2021-05-31 stsp goto done;
9310 67a66647 2021-05-31 stsp }
9311 67a66647 2021-05-31 stsp
9312 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&blob_base_path, &f_base,
9313 fc2a50f2 2022-11-01 thomas base_path, "");
9314 67a66647 2021-05-31 stsp if (err)
9315 67a66647 2021-05-31 stsp goto done;
9316 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9317 67a66647 2021-05-31 stsp blob_base);
9318 67a66647 2021-05-31 stsp if (err)
9319 67a66647 2021-05-31 stsp goto done;
9320 67a66647 2021-05-31 stsp
9321 b6b86fd1 2022-08-30 thomas /*
9322 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9323 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9324 eec2f5a9 2021-06-03 stsp */
9325 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9326 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9327 eec2f5a9 2021-06-03 stsp ondisk_path);
9328 eec2f5a9 2021-06-03 stsp if (err)
9329 eec2f5a9 2021-06-03 stsp goto done;
9330 eec2f5a9 2021-06-03 stsp } else {
9331 eec2f5a9 2021-06-03 stsp int fd;
9332 06340621 2021-12-31 thomas fd = open(ondisk_path,
9333 06340621 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9334 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9335 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9336 eec2f5a9 2021-06-03 stsp goto done;
9337 eec2f5a9 2021-06-03 stsp }
9338 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9339 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9340 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9341 eec2f5a9 2021-06-03 stsp close(fd);
9342 eec2f5a9 2021-06-03 stsp goto done;
9343 eec2f5a9 2021-06-03 stsp }
9344 eec2f5a9 2021-06-03 stsp }
9345 eec2f5a9 2021-06-03 stsp
9346 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9347 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9348 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9349 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9350 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9351 fda8017d 2020-07-23 stsp }
9352 fda8017d 2020-07-23 stsp if (err)
9353 fda8017d 2020-07-23 stsp goto done;
9354 fda8017d 2020-07-23 stsp
9355 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9356 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9357 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9358 2ac8aa02 2020-11-09 stsp } else {
9359 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9360 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9361 2ac8aa02 2020-11-09 stsp }
9362 fda8017d 2020-07-23 stsp done:
9363 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9364 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9365 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9366 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9367 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9368 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9369 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9370 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9371 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9372 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9373 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9374 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9375 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9376 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9377 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9378 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9379 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9380 67a66647 2021-05-31 stsp free(blob_base_path);
9381 67a66647 2021-05-31 stsp free(parent);
9382 67a66647 2021-05-31 stsp free(base_path);
9383 2e1f37b0 2019-08-08 stsp return err;
9384 2e1f37b0 2019-08-08 stsp }
9385 2e1f37b0 2019-08-08 stsp
9386 ad493afc 2019-08-03 stsp static const struct got_error *
9387 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9388 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9389 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9390 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9391 ad493afc 2019-08-03 stsp {
9392 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9393 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9394 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9395 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9396 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9397 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9398 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9399 9bc94a15 2019-08-03 stsp struct stat sb;
9400 f4ae6ddb 2022-07-01 thomas int fd1 = -1, fd2 = -1;
9401 ad493afc 2019-08-03 stsp
9402 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9403 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9404 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9405 2e1f37b0 2019-08-08 stsp return NULL;
9406 2e1f37b0 2019-08-08 stsp
9407 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9408 ad493afc 2019-08-03 stsp if (ie == NULL)
9409 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9410 9bc94a15 2019-08-03 stsp
9411 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9412 9bc94a15 2019-08-03 stsp == -1)
9413 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9414 ad493afc 2019-08-03 stsp
9415 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9416 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9417 f69721c3 2019-10-21 stsp if (err)
9418 f69721c3 2019-10-21 stsp goto done;
9419 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9420 f69721c3 2019-10-21 stsp id_str) == -1) {
9421 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9422 f4ae6ddb 2022-07-01 thomas goto done;
9423 f4ae6ddb 2022-07-01 thomas }
9424 f4ae6ddb 2022-07-01 thomas
9425 f4ae6ddb 2022-07-01 thomas fd1 = got_opentempfd();
9426 f4ae6ddb 2022-07-01 thomas if (fd1 == -1) {
9427 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9428 f4ae6ddb 2022-07-01 thomas goto done;
9429 f4ae6ddb 2022-07-01 thomas }
9430 f4ae6ddb 2022-07-01 thomas fd2 = got_opentempfd();
9431 f4ae6ddb 2022-07-01 thomas if (fd2 == -1) {
9432 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
9433 f69721c3 2019-10-21 stsp goto done;
9434 f69721c3 2019-10-21 stsp }
9435 f69721c3 2019-10-21 stsp
9436 ad493afc 2019-08-03 stsp switch (staged_status) {
9437 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9438 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9439 f4ae6ddb 2022-07-01 thomas blob_id, 8192, fd1);
9440 ad493afc 2019-08-03 stsp if (err)
9441 ad493afc 2019-08-03 stsp break;
9442 ad493afc 2019-08-03 stsp /* fall through */
9443 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9444 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9445 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9446 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9447 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9448 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9449 2e1f37b0 2019-08-08 stsp if (err)
9450 2e1f37b0 2019-08-08 stsp break;
9451 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9452 2e1f37b0 2019-08-08 stsp break;
9453 2e1f37b0 2019-08-08 stsp } else {
9454 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9455 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9456 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9457 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9458 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9459 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9460 2e1f37b0 2019-08-08 stsp }
9461 2e1f37b0 2019-08-08 stsp }
9462 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9463 f4ae6ddb 2022-07-01 thomas staged_blob_id, 8192, fd2);
9464 ad493afc 2019-08-03 stsp if (err)
9465 ad493afc 2019-08-03 stsp break;
9466 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9467 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9468 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9469 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9470 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9471 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9472 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9473 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9474 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9475 ea7786be 2020-07-23 stsp break;
9476 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9477 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9478 36bf999c 2020-07-23 stsp char *staged_target;
9479 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9480 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9481 36bf999c 2020-07-23 stsp if (err)
9482 36bf999c 2020-07-23 stsp goto done;
9483 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9484 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9485 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9486 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9487 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9488 36bf999c 2020-07-23 stsp free(staged_target);
9489 dfe9fba0 2020-07-23 stsp } else {
9490 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9491 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9492 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9493 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9494 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9495 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9496 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9497 dfe9fba0 2020-07-23 stsp }
9498 ea7786be 2020-07-23 stsp break;
9499 ea7786be 2020-07-23 stsp default:
9500 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9501 ea7786be 2020-07-23 stsp break;
9502 ea7786be 2020-07-23 stsp }
9503 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9504 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9505 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9506 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9507 2ac8aa02 2020-11-09 stsp }
9508 ad493afc 2019-08-03 stsp break;
9509 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9510 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9511 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9512 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9513 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9514 2e1f37b0 2019-08-08 stsp if (err)
9515 2e1f37b0 2019-08-08 stsp break;
9516 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9517 2e1f37b0 2019-08-08 stsp break;
9518 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9519 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9520 2e1f37b0 2019-08-08 stsp break;
9521 2e1f37b0 2019-08-08 stsp }
9522 2e1f37b0 2019-08-08 stsp }
9523 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9524 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9525 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9526 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9527 9bc94a15 2019-08-03 stsp if (err)
9528 9bc94a15 2019-08-03 stsp break;
9529 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9530 ad493afc 2019-08-03 stsp break;
9531 ad493afc 2019-08-03 stsp }
9532 f69721c3 2019-10-21 stsp done:
9533 ad493afc 2019-08-03 stsp free(ondisk_path);
9534 f4ae6ddb 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9535 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9536 ad493afc 2019-08-03 stsp if (blob_base)
9537 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9538 f4ae6ddb 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9539 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
9540 ad493afc 2019-08-03 stsp if (blob_staged)
9541 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9542 f69721c3 2019-10-21 stsp free(id_str);
9543 f69721c3 2019-10-21 stsp free(label_orig);
9544 ad493afc 2019-08-03 stsp return err;
9545 ad493afc 2019-08-03 stsp }
9546 ad493afc 2019-08-03 stsp
9547 ad493afc 2019-08-03 stsp const struct got_error *
9548 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9549 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9550 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9551 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9552 ad493afc 2019-08-03 stsp struct got_repository *repo)
9553 ad493afc 2019-08-03 stsp {
9554 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9555 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9556 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9557 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9558 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9559 ad493afc 2019-08-03 stsp
9560 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9561 ad493afc 2019-08-03 stsp if (err)
9562 ad493afc 2019-08-03 stsp return err;
9563 ad493afc 2019-08-03 stsp
9564 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9565 ad493afc 2019-08-03 stsp if (err)
9566 ad493afc 2019-08-03 stsp goto done;
9567 ad493afc 2019-08-03 stsp
9568 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9569 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9570 ad493afc 2019-08-03 stsp upa.repo = repo;
9571 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9572 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9573 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9574 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9575 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9576 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9577 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
9578 ad493afc 2019-08-03 stsp if (err)
9579 ad493afc 2019-08-03 stsp goto done;
9580 ad493afc 2019-08-03 stsp }
9581 ad493afc 2019-08-03 stsp
9582 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9583 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9584 ad493afc 2019-08-03 stsp err = sync_err;
9585 ad493afc 2019-08-03 stsp done:
9586 ad493afc 2019-08-03 stsp free(fileindex_path);
9587 ad493afc 2019-08-03 stsp if (fileindex)
9588 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9589 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9590 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9591 ad493afc 2019-08-03 stsp err = unlockerr;
9592 ad493afc 2019-08-03 stsp return err;
9593 ad493afc 2019-08-03 stsp }
9594 b2118c49 2020-07-28 stsp
9595 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9596 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9597 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9598 b2118c49 2020-07-28 stsp void *info_arg;
9599 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9600 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9601 b2118c49 2020-07-28 stsp void *cancel_arg;
9602 b2118c49 2020-07-28 stsp };
9603 b2118c49 2020-07-28 stsp
9604 b2118c49 2020-07-28 stsp static const struct got_error *
9605 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9606 b2118c49 2020-07-28 stsp {
9607 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9608 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9609 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9610 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9611 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9612 b2118c49 2020-07-28 stsp int stage;
9613 b2118c49 2020-07-28 stsp
9614 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9615 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9616 b2118c49 2020-07-28 stsp
9617 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9618 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9619 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9620 b2118c49 2020-07-28 stsp break;
9621 b2118c49 2020-07-28 stsp }
9622 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9623 b2118c49 2020-07-28 stsp return NULL;
9624 b2118c49 2020-07-28 stsp
9625 43010591 2023-02-17 thomas if (got_fileindex_entry_has_blob(ie))
9626 43010591 2023-02-17 thomas blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9627 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9628 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9629 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9630 43010591 2023-02-17 thomas staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9631 43010591 2023-02-17 thomas &staged_blob_id, ie);
9632 b2118c49 2020-07-28 stsp }
9633 b2118c49 2020-07-28 stsp
9634 43010591 2023-02-17 thomas if (got_fileindex_entry_has_commit(ie))
9635 43010591 2023-02-17 thomas commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9636 b2118c49 2020-07-28 stsp
9637 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9638 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9639 b2118c49 2020-07-28 stsp }
9640 b2118c49 2020-07-28 stsp
9641 b2118c49 2020-07-28 stsp const struct got_error *
9642 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9643 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9644 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9645 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9646 b2118c49 2020-07-28 stsp
9647 b2118c49 2020-07-28 stsp {
9648 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9649 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9650 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9651 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9652 b2118c49 2020-07-28 stsp
9653 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9654 b2118c49 2020-07-28 stsp if (err)
9655 b2118c49 2020-07-28 stsp return err;
9656 b2118c49 2020-07-28 stsp
9657 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9658 b2118c49 2020-07-28 stsp if (err)
9659 b2118c49 2020-07-28 stsp goto done;
9660 b2118c49 2020-07-28 stsp
9661 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9662 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9663 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9664 b2118c49 2020-07-28 stsp arg.paths = paths;
9665 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9666 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9667 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9668 b2118c49 2020-07-28 stsp &arg);
9669 b2118c49 2020-07-28 stsp done:
9670 b2118c49 2020-07-28 stsp free(fileindex_path);
9671 b2118c49 2020-07-28 stsp if (fileindex)
9672 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9673 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9674 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9675 b2118c49 2020-07-28 stsp err = unlockerr;
9676 b2118c49 2020-07-28 stsp return err;
9677 b2118c49 2020-07-28 stsp }
9678 814624e7 2022-03-22 thomas
9679 814624e7 2022-03-22 thomas static const struct got_error *
9680 814624e7 2022-03-22 thomas patch_check_path(const char *p, char **path, unsigned char *status,
9681 814624e7 2022-03-22 thomas unsigned char *staged_status, struct got_fileindex *fileindex,
9682 814624e7 2022-03-22 thomas struct got_worktree *worktree, struct got_repository *repo)
9683 814624e7 2022-03-22 thomas {
9684 814624e7 2022-03-22 thomas const struct got_error *err;
9685 814624e7 2022-03-22 thomas struct got_fileindex_entry *ie;
9686 814624e7 2022-03-22 thomas struct stat sb;
9687 814624e7 2022-03-22 thomas char *ondisk_path = NULL;
9688 814624e7 2022-03-22 thomas
9689 814624e7 2022-03-22 thomas err = got_worktree_resolve_path(path, worktree, p);
9690 814624e7 2022-03-22 thomas if (err)
9691 814624e7 2022-03-22 thomas return err;
9692 814624e7 2022-03-22 thomas
9693 814624e7 2022-03-22 thomas if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9694 814624e7 2022-03-22 thomas *path[0] ? "/" : "", *path) == -1)
9695 814624e7 2022-03-22 thomas return got_error_from_errno("asprintf");
9696 814624e7 2022-03-22 thomas
9697 814624e7 2022-03-22 thomas ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9698 814624e7 2022-03-22 thomas if (ie) {
9699 814624e7 2022-03-22 thomas *staged_status = get_staged_status(ie);
9700 12de5570 2022-05-01 thomas err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9701 12de5570 2022-05-01 thomas repo);
9702 814624e7 2022-03-22 thomas if (err)
9703 814624e7 2022-03-22 thomas goto done;
9704 814624e7 2022-03-22 thomas } else {
9705 814624e7 2022-03-22 thomas *staged_status = GOT_STATUS_NO_CHANGE;
9706 814624e7 2022-03-22 thomas *status = GOT_STATUS_UNVERSIONED;
9707 814624e7 2022-03-22 thomas if (lstat(ondisk_path, &sb) == -1) {
9708 814624e7 2022-03-22 thomas if (errno != ENOENT) {
9709 814624e7 2022-03-22 thomas err = got_error_from_errno2("lstat",
9710 814624e7 2022-03-22 thomas ondisk_path);
9711 814624e7 2022-03-22 thomas goto done;
9712 814624e7 2022-03-22 thomas }
9713 814624e7 2022-03-22 thomas *status = GOT_STATUS_NONEXISTENT;
9714 814624e7 2022-03-22 thomas }
9715 814624e7 2022-03-22 thomas }
9716 814624e7 2022-03-22 thomas
9717 814624e7 2022-03-22 thomas done:
9718 814624e7 2022-03-22 thomas free(ondisk_path);
9719 814624e7 2022-03-22 thomas return err;
9720 814624e7 2022-03-22 thomas }
9721 814624e7 2022-03-22 thomas
9722 814624e7 2022-03-22 thomas static const struct got_error *
9723 814624e7 2022-03-22 thomas patch_can_rm(const char *path, unsigned char status,
9724 814624e7 2022-03-22 thomas unsigned char staged_status)
9725 814624e7 2022-03-22 thomas {
9726 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9727 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9728 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9729 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9730 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY &&
9731 814624e7 2022-03-22 thomas status != GOT_STATUS_MODE_CHANGE)
9732 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9733 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9734 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9735 814624e7 2022-03-22 thomas return NULL;
9736 814624e7 2022-03-22 thomas }
9737 814624e7 2022-03-22 thomas
9738 814624e7 2022-03-22 thomas static const struct got_error *
9739 814624e7 2022-03-22 thomas patch_can_add(const char *path, unsigned char status)
9740 814624e7 2022-03-22 thomas {
9741 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NONEXISTENT)
9742 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9743 814624e7 2022-03-22 thomas return NULL;
9744 814624e7 2022-03-22 thomas }
9745 814624e7 2022-03-22 thomas
9746 814624e7 2022-03-22 thomas static const struct got_error *
9747 814624e7 2022-03-22 thomas patch_can_edit(const char *path, unsigned char status,
9748 814624e7 2022-03-22 thomas unsigned char staged_status)
9749 814624e7 2022-03-22 thomas {
9750 814624e7 2022-03-22 thomas if (status == GOT_STATUS_NONEXISTENT)
9751 814624e7 2022-03-22 thomas return got_error_set_errno(ENOENT, path);
9752 814624e7 2022-03-22 thomas if (status != GOT_STATUS_NO_CHANGE &&
9753 814624e7 2022-03-22 thomas status != GOT_STATUS_ADD &&
9754 814624e7 2022-03-22 thomas status != GOT_STATUS_MODIFY)
9755 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9756 814624e7 2022-03-22 thomas if (staged_status == GOT_STATUS_DELETE)
9757 814624e7 2022-03-22 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
9758 814624e7 2022-03-22 thomas return NULL;
9759 814624e7 2022-03-22 thomas }
9760 814624e7 2022-03-22 thomas
9761 814624e7 2022-03-22 thomas const struct got_error *
9762 814624e7 2022-03-22 thomas got_worktree_patch_prepare(struct got_fileindex **fileindex,
9763 5e22b737 2022-05-31 thomas char **fileindex_path, struct got_worktree *worktree)
9764 814624e7 2022-03-22 thomas {
9765 5e22b737 2022-05-31 thomas return open_fileindex(fileindex, fileindex_path, worktree);
9766 814624e7 2022-03-22 thomas }
9767 814624e7 2022-03-22 thomas
9768 814624e7 2022-03-22 thomas const struct got_error *
9769 814624e7 2022-03-22 thomas got_worktree_patch_check_path(const char *old, const char *new,
9770 814624e7 2022-03-22 thomas char **oldpath, char **newpath, struct got_worktree *worktree,
9771 814624e7 2022-03-22 thomas struct got_repository *repo, struct got_fileindex *fileindex)
9772 814624e7 2022-03-22 thomas {
9773 814624e7 2022-03-22 thomas const struct got_error *err = NULL;
9774 814624e7 2022-03-22 thomas int file_renamed = 0;
9775 814624e7 2022-03-22 thomas unsigned char status_old, staged_status_old;
9776 814624e7 2022-03-22 thomas unsigned char status_new, staged_status_new;
9777 814624e7 2022-03-22 thomas
9778 814624e7 2022-03-22 thomas *oldpath = NULL;
9779 814624e7 2022-03-22 thomas *newpath = NULL;
9780 814624e7 2022-03-22 thomas
9781 814624e7 2022-03-22 thomas err = patch_check_path(old != NULL ? old : new, oldpath,
9782 814624e7 2022-03-22 thomas &status_old, &staged_status_old, fileindex, worktree, repo);
9783 814624e7 2022-03-22 thomas if (err)
9784 814624e7 2022-03-22 thomas goto done;
9785 814624e7 2022-03-22 thomas
9786 814624e7 2022-03-22 thomas err = patch_check_path(new != NULL ? new : old, newpath,
9787 814624e7 2022-03-22 thomas &status_new, &staged_status_new, fileindex, worktree, repo);
9788 814624e7 2022-03-22 thomas if (err)
9789 814624e7 2022-03-22 thomas goto done;
9790 814624e7 2022-03-22 thomas
9791 814624e7 2022-03-22 thomas if (old != NULL && new != NULL && strcmp(old, new) != 0)
9792 814624e7 2022-03-22 thomas file_renamed = 1;
9793 814624e7 2022-03-22 thomas
9794 814624e7 2022-03-22 thomas if (old != NULL && new == NULL)
9795 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9796 814624e7 2022-03-22 thomas else if (file_renamed) {
9797 814624e7 2022-03-22 thomas err = patch_can_rm(*oldpath, status_old, staged_status_old);
9798 814624e7 2022-03-22 thomas if (err == NULL)
9799 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9800 814624e7 2022-03-22 thomas } else if (old == NULL)
9801 814624e7 2022-03-22 thomas err = patch_can_add(*newpath, status_new);
9802 814624e7 2022-03-22 thomas else
9803 814624e7 2022-03-22 thomas err = patch_can_edit(*newpath, status_new, staged_status_new);
9804 814624e7 2022-03-22 thomas
9805 814624e7 2022-03-22 thomas done:
9806 814624e7 2022-03-22 thomas if (err) {
9807 814624e7 2022-03-22 thomas free(*oldpath);
9808 814624e7 2022-03-22 thomas *oldpath = NULL;
9809 814624e7 2022-03-22 thomas free(*newpath);
9810 814624e7 2022-03-22 thomas *newpath = NULL;
9811 814624e7 2022-03-22 thomas }
9812 814624e7 2022-03-22 thomas return err;
9813 814624e7 2022-03-22 thomas }
9814 814624e7 2022-03-22 thomas
9815 814624e7 2022-03-22 thomas const struct got_error *
9816 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9817 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9818 5e22b737 2022-05-31 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
9819 814624e7 2022-03-22 thomas {
9820 5e22b737 2022-05-31 thomas struct schedule_addition_args saa;
9821 5e22b737 2022-05-31 thomas
9822 5e22b737 2022-05-31 thomas memset(&saa, 0, sizeof(saa));
9823 5e22b737 2022-05-31 thomas saa.worktree = worktree;
9824 5e22b737 2022-05-31 thomas saa.fileindex = fileindex;
9825 5e22b737 2022-05-31 thomas saa.progress_cb = progress_cb;
9826 5e22b737 2022-05-31 thomas saa.progress_arg = progress_arg;
9827 5e22b737 2022-05-31 thomas saa.repo = repo;
9828 5e22b737 2022-05-31 thomas
9829 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9830 5e22b737 2022-05-31 thomas schedule_addition, &saa, NULL, NULL, 1, 0);
9831 814624e7 2022-03-22 thomas }
9832 5e22b737 2022-05-31 thomas
9833 5e22b737 2022-05-31 thomas const struct got_error *
9834 5e22b737 2022-05-31 thomas got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9835 5e22b737 2022-05-31 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
9836 5e22b737 2022-05-31 thomas got_worktree_delete_cb progress_cb, void *progress_arg)
9837 5e22b737 2022-05-31 thomas {
9838 5e22b737 2022-05-31 thomas struct schedule_deletion_args sda;
9839 5e22b737 2022-05-31 thomas
9840 5e22b737 2022-05-31 thomas memset(&sda, 0, sizeof(sda));
9841 5e22b737 2022-05-31 thomas sda.worktree = worktree;
9842 5e22b737 2022-05-31 thomas sda.fileindex = fileindex;
9843 5e22b737 2022-05-31 thomas sda.progress_cb = progress_cb;
9844 5e22b737 2022-05-31 thomas sda.progress_arg = progress_arg;
9845 5e22b737 2022-05-31 thomas sda.repo = repo;
9846 5e22b737 2022-05-31 thomas sda.delete_local_mods = 0;
9847 5e22b737 2022-05-31 thomas sda.keep_on_disk = 0;
9848 5e22b737 2022-05-31 thomas sda.ignore_missing_paths = 0;
9849 5e22b737 2022-05-31 thomas sda.status_codes = NULL;
9850 5e22b737 2022-05-31 thomas
9851 5e22b737 2022-05-31 thomas return worktree_status(worktree, path, fileindex, repo,
9852 5e22b737 2022-05-31 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9853 5e22b737 2022-05-31 thomas }
9854 5e22b737 2022-05-31 thomas
9855 5e22b737 2022-05-31 thomas const struct got_error *
9856 5e22b737 2022-05-31 thomas got_worktree_patch_complete(struct got_fileindex *fileindex,
9857 36832a8e 2022-05-31 thomas const char *fileindex_path)
9858 5e22b737 2022-05-31 thomas {
9859 5e22b737 2022-05-31 thomas const struct got_error *err = NULL;
9860 5e22b737 2022-05-31 thomas
9861 36832a8e 2022-05-31 thomas err = sync_fileindex(fileindex, fileindex_path);
9862 36832a8e 2022-05-31 thomas got_fileindex_free(fileindex);
9863 5e22b737 2022-05-31 thomas
9864 5e22b737 2022-05-31 thomas return err;
9865 5e22b737 2022-05-31 thomas }